* Update the internal TopologyDescription with a ServerDescription * * @param serverDescription - The server to update in the internal list of server descriptions
(serverDescription: ServerDescription)
| 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 |