MCPcopy
hub / github.com/mongodb/node-mongodb-native / secondarySelector

Function secondarySelector

src/sdam/server_selection.ts:285–376  ·  view source on GitHub ↗
(
  readPreference: ReadPreference,
  topologyDescription: TopologyDescription,
  servers: ServerDescription[],
  deprioritized: DeprioritizedServers
)

Source from the content-addressed store, hash-verified

283}
284
285function secondarySelector(
286 readPreference: ReadPreference,
287 topologyDescription: TopologyDescription,
288 servers: ServerDescription[],
289 deprioritized: DeprioritizedServers
290) {
291 const mode = readPreference.mode;
292 switch (mode) {
293 case 'primary':
294 // Note: no need to filter for deprioritized servers. A replica set has only one primary; that means that
295 // we are in one of two scenarios:
296 // 1. deprioritized servers is empty - return the primary.
297 // 2. deprioritized servers contains the primary - return the primary.
298 return servers.filter(primaryFilter);
299 case 'primaryPreferred': {
300 const primary = servers.filter(primaryFilter);
301
302 // If there is a primary and it is not deprioritized, use the primary. Otherwise,
303 // check for secondaries.
304 const eligiblePrimary = primary.filter(isDeprioritizedFactory(deprioritized));
305 if (eligiblePrimary.length) {
306 return eligiblePrimary;
307 }
308
309 // If we make it here, we either have:
310 // 1. a deprioritized primary
311 // 2. no eligible primary
312 // secondaries take precedence of deprioritized primaries.
313 const secondaries = tagSetReducer(
314 readPreference,
315 maxStalenessReducer(readPreference, topologyDescription, servers.filter(secondaryFilter))
316 );
317
318 const eligibleSecondaries = secondaries.filter(isDeprioritizedFactory(deprioritized));
319 if (eligibleSecondaries.length) {
320 return latencyWindowReducer(topologyDescription, eligibleSecondaries);
321 }
322
323 // if we make it here, we have no primaries or secondaries that not deprioritized.
324 // prefer the primary (which may not exist, if the topology has no primary).
325 // otherwise, return the secondaries (which also may not exist, but there is nothing else to check here).
326 return primary.length ? primary : latencyWindowReducer(topologyDescription, secondaries);
327 }
328 case 'nearest': {
329 const eligible = filterDeprioritized(
330 tagSetReducer(
331 readPreference,
332 maxStalenessReducer(readPreference, topologyDescription, servers.filter(nearestFilter))
333 ),
334 deprioritized
335 );
336 return latencyWindowReducer(topologyDescription, eligible);
337 }
338 case 'secondary':
339 case 'secondaryPreferred': {
340 const secondaries = tagSetReducer(
341 readPreference,
342 maxStalenessReducer(readPreference, topologyDescription, servers.filter(secondaryFilter))

Callers 1

Calls 6

isDeprioritizedFactoryFunction · 0.85
tagSetReducerFunction · 0.85
maxStalenessReducerFunction · 0.85
latencyWindowReducerFunction · 0.85
filterDeprioritizedFunction · 0.85
filterMethod · 0.45

Tested by

no test coverage detected