2727import com .mongodb .connection .ClusterDescription ;
2828import com .mongodb .connection .ClusterId ;
2929import com .mongodb .connection .ClusterSettings ;
30- import com .mongodb .connection .ClusterType ;
3130import com .mongodb .connection .ServerDescription ;
3231import com .mongodb .event .ClusterClosedEvent ;
3332import com .mongodb .event .ClusterDescriptionChangedEvent ;
5049import com .mongodb .selector .CompositeServerSelector ;
5150import com .mongodb .selector .ServerSelector ;
5251
53- import java .util .Collections ;
5452import java .util .Deque ;
5553import java .util .Iterator ;
5654import java .util .List ;
6462import static com .mongodb .assertions .Assertions .assertNotNull ;
6563import static com .mongodb .assertions .Assertions .isTrue ;
6664import static com .mongodb .assertions .Assertions .notNull ;
65+ import static com .mongodb .connection .ClusterType .UNKNOWN ;
6766import static com .mongodb .connection .ServerDescription .MAX_DRIVER_WIRE_VERSION ;
6867import static com .mongodb .connection .ServerDescription .MIN_DRIVER_SERVER_VERSION ;
6968import static com .mongodb .connection .ServerDescription .MIN_DRIVER_WIRE_VERSION ;
7271import static com .mongodb .internal .connection .EventHelper .wouldDescriptionsGenerateEquivalentEvents ;
7372import static com .mongodb .internal .event .EventListenerHelper .singleClusterListener ;
7473import static com .mongodb .internal .logging .LogMessage .Component .SERVER_SELECTION ;
74+ import static com .mongodb .internal .logging .LogMessage .Component .TOPOLOGY ;
7575import static com .mongodb .internal .logging .LogMessage .Entry .Name .FAILURE ;
7676import static com .mongodb .internal .logging .LogMessage .Entry .Name .OPERATION ;
7777import static com .mongodb .internal .logging .LogMessage .Entry .Name .OPERATION_ID ;
8080import static com .mongodb .internal .logging .LogMessage .Entry .Name .SERVER_HOST ;
8181import static com .mongodb .internal .logging .LogMessage .Entry .Name .SERVER_PORT ;
8282import static com .mongodb .internal .logging .LogMessage .Entry .Name .TOPOLOGY_DESCRIPTION ;
83+ import static com .mongodb .internal .logging .LogMessage .Entry .Name .TOPOLOGY_ID ;
84+ import static com .mongodb .internal .logging .LogMessage .Entry .Name .TOPOLOGY_NEW_DESCRIPTION ;
85+ import static com .mongodb .internal .logging .LogMessage .Entry .Name .TOPOLOGY_PREVIOUS_DESCRIPTION ;
8386import static com .mongodb .internal .logging .LogMessage .Level .DEBUG ;
8487import static com .mongodb .internal .logging .LogMessage .Level .INFO ;
8588import static com .mongodb .internal .time .Timeout .ZeroSemantics .ZERO_DURATION_MEANS_EXPIRED ;
8689import static java .lang .String .format ;
8790import static java .util .Arrays .asList ;
91+ import static java .util .Collections .emptyList ;
92+ import static java .util .Collections .singletonList ;
8893import static java .util .concurrent .TimeUnit .MILLISECONDS ;
8994import static java .util .concurrent .TimeUnit .NANOSECONDS ;
9095import static java .util .stream .Collectors .toList ;
@@ -111,8 +116,10 @@ abstract class BaseCluster implements Cluster {
111116 this .settings = notNull ("settings" , settings );
112117 this .serverFactory = notNull ("serverFactory" , serverFactory );
113118 this .clusterListener = singleClusterListener (settings );
114- clusterListener .clusterOpening (new ClusterOpeningEvent (clusterId ));
115- description = new ClusterDescription (settings .getMode (), ClusterType .UNKNOWN , Collections .emptyList (),
119+ ClusterOpeningEvent clusterOpeningEvent = new ClusterOpeningEvent (clusterId );
120+ clusterListener .clusterOpening (clusterOpeningEvent );
121+ logTopologyOpening (clusterId , clusterOpeningEvent );
122+ description = new ClusterDescription (settings .getMode (), UNKNOWN , emptyList (),
116123 settings , serverFactory .getSettings ());
117124 }
118125
@@ -210,7 +217,11 @@ public void close() {
210217 if (!isClosed ()) {
211218 isClosed = true ;
212219 phase .get ().countDown ();
213- clusterListener .clusterClosed (new ClusterClosedEvent (clusterId ));
220+ fireChangeEvent (new ClusterDescription (settings .getMode (), UNKNOWN , emptyList (), settings , serverFactory .getSettings ()),
221+ description );
222+ ClusterClosedEvent clusterClosedEvent = new ClusterClosedEvent (clusterId );
223+ clusterListener .clusterClosed (clusterClosedEvent );
224+ logTopologyClosedEvent (clusterId , clusterClosedEvent );
214225 stopWaitQueueHandler ();
215226 }
216227 }
@@ -237,8 +248,9 @@ protected void updateDescription(final ClusterDescription newDescription) {
237248 */
238249 protected void fireChangeEvent (final ClusterDescription newDescription , final ClusterDescription previousDescription ) {
239250 if (!wouldDescriptionsGenerateEquivalentEvents (newDescription , previousDescription )) {
240- clusterListener .clusterDescriptionChanged (
241- new ClusterDescriptionChangedEvent (getClusterId (), newDescription , previousDescription ));
251+ ClusterDescriptionChangedEvent changedEvent = new ClusterDescriptionChangedEvent (getClusterId (), newDescription , previousDescription );
252+ clusterListener .clusterDescriptionChanged (changedEvent );
253+ logTopologyDescriptionChanged (getClusterId (), changedEvent );
242254 }
243255 }
244256
@@ -619,4 +631,43 @@ static void logServerSelectionSucceeded(
619631 + " Selector: {}, topology description: {}" ));
620632 }
621633 }
634+
635+ static void logTopologyOpening (
636+ final ClusterId clusterId ,
637+ final ClusterOpeningEvent clusterOpeningEvent ) {
638+ if (STRUCTURED_LOGGER .isRequired (DEBUG , clusterId )) {
639+ STRUCTURED_LOGGER .log (new LogMessage (
640+ TOPOLOGY , DEBUG , "Starting topology monitoring" , clusterId ,
641+ singletonList (new Entry (TOPOLOGY_ID , clusterId )),
642+ "Starting monitoring for topology with ID {}" ));
643+ }
644+ }
645+
646+ static void logTopologyDescriptionChanged (
647+ final ClusterId clusterId ,
648+ final ClusterDescriptionChangedEvent clusterDescriptionChangedEvent ) {
649+ if (STRUCTURED_LOGGER .isRequired (DEBUG , clusterId )) {
650+ STRUCTURED_LOGGER .log (new LogMessage (
651+ TOPOLOGY , DEBUG , "Topology description changed" , clusterId ,
652+ asList (
653+ new Entry (TOPOLOGY_ID , clusterId ),
654+ new Entry (TOPOLOGY_PREVIOUS_DESCRIPTION ,
655+ clusterDescriptionChangedEvent .getPreviousDescription ().getShortDescription ()),
656+ new Entry (TOPOLOGY_NEW_DESCRIPTION ,
657+ clusterDescriptionChangedEvent .getNewDescription ().getShortDescription ())),
658+ "Description changed for topology with ID {}. Previous description: {}. New description: {}" ));
659+ }
660+ }
661+
662+ static void logTopologyClosedEvent (
663+ final ClusterId clusterId ,
664+ final ClusterClosedEvent clusterClosedEvent ) {
665+ if (STRUCTURED_LOGGER .isRequired (DEBUG , clusterId )) {
666+ STRUCTURED_LOGGER .log (new LogMessage (
667+ TOPOLOGY , DEBUG , "Stopped topology monitoring" , clusterId ,
668+ singletonList (new Entry (TOPOLOGY_ID , clusterId )),
669+ "Stopped monitoring for topology with ID {}" ));
670+ }
671+ }
672+
622673}
0 commit comments