@@ -117,8 +117,6 @@ private void onChange(final ChangeEvent<ServerDescription> event) {
117117 return ;
118118 }
119119
120- serverTuple .description = newDescription ;
121-
122120 if (event .getNewValue ().isOk ()) {
123121 if (clusterType == Unknown ) {
124122 clusterType = newDescription .getClusterType ();
@@ -139,6 +137,8 @@ private void onChange(final ChangeEvent<ServerDescription> event) {
139137 break ;
140138 }
141139 }
140+
141+ serverTuple .description = newDescription ;
142142 updateDescription ();
143143 }
144144 fireChangeEvent ();
@@ -178,10 +178,18 @@ private void handleReplicaSetMemberChanged(final ServerDescription newDescriptio
178178 }
179179
180180 if (newDescription .isPrimary ()) {
181+ if (isNotAlreadyPrimary (newDescription .getAddress ())) {
182+ LOGGER .info (format ("Discovered replica set primary %s" , newDescription .getAddress ()));
183+ }
181184 invalidateOldPrimaries (newDescription .getAddress ());
182185 }
183186 }
184187
188+ private boolean isNotAlreadyPrimary (final ServerAddress address ) {
189+ ServerTuple serverTuple = addressToServerTupleMap .get (address );
190+ return serverTuple == null || !serverTuple .description .isPrimary ();
191+ }
192+
185193 private void handleShardRouterChanged (final ServerDescription newDescription ) {
186194 if (newDescription .getClusterType () != Sharded ) {
187195 LOGGER .severe (format ("Expecting a %s, but found a %s. Removing %s from client view of cluster." ,
@@ -213,7 +221,6 @@ private void removeServer(final ServerAddress serverAddress) {
213221 }
214222
215223 private void invalidateOldPrimaries (final ServerAddress newPrimary ) {
216- LOGGER .info (format ("Replica set primary has changed to %s" , newPrimary ));
217224 for (ServerTuple serverTuple : addressToServerTupleMap .values ()) {
218225 if (!serverTuple .description .getAddress ().equals (newPrimary ) && serverTuple .description .isPrimary ()) {
219226 LOGGER .info (format ("Rediscovering type of existing primary %s" , serverTuple .description .getAddress ()));
0 commit comments