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

Function updateRsFromPrimary

src/sdam/topology_description.ts:372–493  ·  view source on GitHub ↗
(
  serverDescriptions: Map<string, ServerDescription>,
  serverDescription: ServerDescription,
  setName: string | null = null,
  maxSetVersion: number | null = null,
  maxElectionId: ObjectId | null = null
)

Source from the content-addressed store, hash-verified

370}
371
372function updateRsFromPrimary(
373 serverDescriptions: Map<string, ServerDescription>,
374 serverDescription: ServerDescription,
375 setName: string | null = null,
376 maxSetVersion: number | null = null,
377 maxElectionId: ObjectId | null = null
378): [TopologyType, string | null, number | null, ObjectId | null] {
379 const setVersionElectionIdMismatch = (
380 serverDescription: ServerDescription,
381 maxSetVersion: number | null,
382 maxElectionId: ObjectId | null
383 ) => {
384 return (
385 `primary marked stale due to electionId/setVersion mismatch:` +
386 ` server setVersion: ${serverDescription.setVersion},` +
387 ` server electionId: ${serverDescription.electionId},` +
388 ` topology setVersion: ${maxSetVersion},` +
389 ` topology electionId: ${maxElectionId}`
390 );
391 };
392 setName = setName || serverDescription.setName;
393 if (setName !== serverDescription.setName) {
394 serverDescriptions.delete(serverDescription.address);
395 return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
396 }
397
398 if (serverDescription.maxWireVersion >= 17) {
399 const electionIdComparison = compareObjectId(maxElectionId, serverDescription.electionId);
400 const maxElectionIdIsEqual = electionIdComparison === 0;
401 const maxElectionIdIsLess = electionIdComparison === -1;
402 const maxSetVersionIsLessOrEqual =
403 (maxSetVersion ?? -1) <= (serverDescription.setVersion ?? -1);
404
405 if (maxElectionIdIsLess || (maxElectionIdIsEqual && maxSetVersionIsLessOrEqual)) {
406 // The reported electionId was greater
407 // or the electionId was equal and reported setVersion was greater
408 // Always update both values, they are a tuple
409 maxElectionId = serverDescription.electionId;
410 maxSetVersion = serverDescription.setVersion;
411 } else {
412 // Stale primary
413 // replace serverDescription with a default ServerDescription of type "Unknown"
414 serverDescriptions.set(
415 serverDescription.address,
416 new ServerDescription(serverDescription.address, undefined, {
417 error: new MongoStalePrimaryError(
418 setVersionElectionIdMismatch(serverDescription, maxSetVersion, maxElectionId)
419 )
420 })
421 );
422
423 return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
424 }
425 } else {
426 const electionId = serverDescription.electionId ? serverDescription.electionId : null;
427 if (serverDescription.setVersion && electionId) {
428 if (maxSetVersion && maxElectionId) {
429 if (

Callers 1

updateMethod · 0.85

Calls 7

compareObjectIdFunction · 0.90
checkHasPrimaryFunction · 0.85
setMethod · 0.80
deleteMethod · 0.45
hasMethod · 0.45
filterMethod · 0.45

Tested by

no test coverage detected