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

Method serverUpdateHandler

src/sdam/topology.ts:680–755  ·  view source on GitHub ↗

* Update the internal TopologyDescription with a ServerDescription * * @param serverDescription - The server to update in the internal list of server descriptions

(serverDescription: ServerDescription)

Source from the content-addressed store, hash-verified

678 * @param serverDescription - The server to update in the internal list of server descriptions
679 */
680 serverUpdateHandler(serverDescription: ServerDescription): void {
681 if (!this.s.description.hasServer(serverDescription.address)) {
682 return;
683 }
684
685 // ignore this server update if its from an outdated topologyVersion
686 if (isStaleServerDescription(this.s.description, serverDescription)) {
687 return;
688 }
689
690 // these will be used for monitoring events later
691 const previousTopologyDescription = this.s.description;
692 const previousServerDescription = this.s.description.servers.get(serverDescription.address);
693 if (!previousServerDescription) {
694 return;
695 }
696
697 // Driver Sessions Spec: "Whenever a driver receives a cluster time from
698 // a server it MUST compare it to the current highest seen cluster time
699 // for the deployment. If the new cluster time is higher than the
700 // highest seen cluster time it MUST become the new highest seen cluster
701 // time. Two cluster times are compared using only the BsonTimestamp
702 // value of the clusterTime embedded field."
703 const clusterTime = serverDescription.$clusterTime;
704 if (clusterTime) {
705 _advanceClusterTime(this, clusterTime);
706 }
707
708 // If we already know all the information contained in this updated description, then
709 // we don't need to emit SDAM events, but still need to update the description, in order
710 // to keep client-tracked attributes like last update time and round trip time up to date
711 const equalDescriptions =
712 previousServerDescription && previousServerDescription.equals(serverDescription);
713
714 // first update the TopologyDescription
715 this.s.description = this.s.description.update(serverDescription);
716 if (this.s.description.compatibilityError) {
717 this.emit(Topology.ERROR, new MongoCompatibilityError(this.s.description.compatibilityError));
718 return;
719 }
720
721 // emit monitoring events for this change
722 if (!equalDescriptions) {
723 const newDescription = this.s.description.servers.get(serverDescription.address);
724 if (newDescription) {
725 this.emit(
726 Topology.SERVER_DESCRIPTION_CHANGED,
727 new ServerDescriptionChangedEvent(
728 this.s.id,
729 serverDescription.address,
730 previousServerDescription,
731 newDescription
732 )
733 );
734 }
735 }
736
737 // update server list from updated descriptions

Callers 6

_connectMethod · 0.95
setupTestFunction · 0.80
executeSDAMTestFunction · 0.80
createAndConnectServerFunction · 0.80

Calls 10

_advanceClusterTimeFunction · 0.90
isStaleServerDescriptionFunction · 0.85
updateServersFunction · 0.85
processWaitQueueFunction · 0.85
hasServerMethod · 0.80
emitAndLogMethod · 0.80
emitMethod · 0.65
getMethod · 0.45
equalsMethod · 0.45
updateMethod · 0.45

Tested by 1

executeSDAMTestFunction · 0.64