27
27
import com .mongodb .connection .ClusterDescription ;
28
28
import com .mongodb .connection .ClusterId ;
29
29
import com .mongodb .connection .ClusterSettings ;
30
- import com .mongodb .connection .ClusterType ;
31
30
import com .mongodb .connection .ServerDescription ;
32
31
import com .mongodb .event .ClusterClosedEvent ;
33
32
import com .mongodb .event .ClusterDescriptionChangedEvent ;
50
49
import com .mongodb .selector .CompositeServerSelector ;
51
50
import com .mongodb .selector .ServerSelector ;
52
51
53
- import java .util .Collections ;
54
52
import java .util .Deque ;
55
53
import java .util .Iterator ;
56
54
import java .util .List ;
64
62
import static com .mongodb .assertions .Assertions .assertNotNull ;
65
63
import static com .mongodb .assertions .Assertions .isTrue ;
66
64
import static com .mongodb .assertions .Assertions .notNull ;
65
+ import static com .mongodb .connection .ClusterType .UNKNOWN ;
67
66
import static com .mongodb .connection .ServerDescription .MAX_DRIVER_WIRE_VERSION ;
68
67
import static com .mongodb .connection .ServerDescription .MIN_DRIVER_SERVER_VERSION ;
69
68
import static com .mongodb .connection .ServerDescription .MIN_DRIVER_WIRE_VERSION ;
72
71
import static com .mongodb .internal .connection .EventHelper .wouldDescriptionsGenerateEquivalentEvents ;
73
72
import static com .mongodb .internal .event .EventListenerHelper .singleClusterListener ;
74
73
import static com .mongodb .internal .logging .LogMessage .Component .SERVER_SELECTION ;
74
+ import static com .mongodb .internal .logging .LogMessage .Component .TOPOLOGY ;
75
75
import static com .mongodb .internal .logging .LogMessage .Entry .Name .FAILURE ;
76
76
import static com .mongodb .internal .logging .LogMessage .Entry .Name .OPERATION ;
77
77
import static com .mongodb .internal .logging .LogMessage .Entry .Name .OPERATION_ID ;
80
80
import static com .mongodb .internal .logging .LogMessage .Entry .Name .SERVER_HOST ;
81
81
import static com .mongodb .internal .logging .LogMessage .Entry .Name .SERVER_PORT ;
82
82
import 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 ;
83
86
import static com .mongodb .internal .logging .LogMessage .Level .DEBUG ;
84
87
import static com .mongodb .internal .logging .LogMessage .Level .INFO ;
85
88
import static com .mongodb .internal .time .Timeout .ZeroSemantics .ZERO_DURATION_MEANS_EXPIRED ;
86
89
import static java .lang .String .format ;
87
90
import static java .util .Arrays .asList ;
91
+ import static java .util .Collections .emptyList ;
92
+ import static java .util .Collections .singletonList ;
88
93
import static java .util .concurrent .TimeUnit .MILLISECONDS ;
89
94
import static java .util .concurrent .TimeUnit .NANOSECONDS ;
90
95
import static java .util .stream .Collectors .toList ;
@@ -111,8 +116,10 @@ abstract class BaseCluster implements Cluster {
111
116
this .settings = notNull ("settings" , settings );
112
117
this .serverFactory = notNull ("serverFactory" , serverFactory );
113
118
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 (),
116
123
settings , serverFactory .getSettings ());
117
124
}
118
125
@@ -210,7 +217,11 @@ public void close() {
210
217
if (!isClosed ()) {
211
218
isClosed = true ;
212
219
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 );
214
225
stopWaitQueueHandler ();
215
226
}
216
227
}
@@ -237,8 +248,9 @@ protected void updateDescription(final ClusterDescription newDescription) {
237
248
*/
238
249
protected void fireChangeEvent (final ClusterDescription newDescription , final ClusterDescription previousDescription ) {
239
250
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 );
242
254
}
243
255
}
244
256
@@ -619,4 +631,43 @@ static void logServerSelectionSucceeded(
619
631
+ " Selector: {}, topology description: {}" ));
620
632
}
621
633
}
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
+
622
673
}
0 commit comments