37
37
import com .mongodb .client .vault .ClientEncryption ;
38
38
import com .mongodb .connection .ClusterConnectionMode ;
39
39
import com .mongodb .connection .ClusterDescription ;
40
- import com .mongodb .connection .ConnectionId ;
41
- import com .mongodb .connection .ServerId ;
42
- import com .mongodb .event .CommandEvent ;
43
- import com .mongodb .event .CommandFailedEvent ;
44
- import com .mongodb .event .CommandListener ;
45
- import com .mongodb .event .CommandStartedEvent ;
46
- import com .mongodb .event .CommandSucceededEvent ;
47
- import com .mongodb .event .ConnectionCheckOutFailedEvent ;
48
- import com .mongodb .event .ConnectionCheckOutStartedEvent ;
49
- import com .mongodb .event .ConnectionCheckedInEvent ;
50
- import com .mongodb .event .ConnectionCheckedOutEvent ;
51
- import com .mongodb .event .ConnectionClosedEvent ;
52
- import com .mongodb .event .ConnectionCreatedEvent ;
53
- import com .mongodb .event .ConnectionPoolClearedEvent ;
54
- import com .mongodb .event .ConnectionPoolClosedEvent ;
55
- import com .mongodb .event .ConnectionPoolCreatedEvent ;
56
- import com .mongodb .event .ConnectionPoolListener ;
57
- import com .mongodb .event .ConnectionPoolReadyEvent ;
58
- import com .mongodb .event .ConnectionReadyEvent ;
59
40
import com .mongodb .event .TestServerMonitorListener ;
60
41
import com .mongodb .internal .connection .ServerMonitoringModeUtil ;
61
42
import com .mongodb .internal .connection .TestClusterListener ;
62
43
import com .mongodb .internal .connection .TestCommandListener ;
63
44
import com .mongodb .internal .connection .TestConnectionPoolListener ;
64
45
import com .mongodb .internal .connection .TestServerListener ;
65
46
import com .mongodb .internal .logging .LogMessage ;
66
- import com .mongodb .lang .NonNull ;
67
47
import com .mongodb .lang .Nullable ;
68
48
import com .mongodb .logging .TestLoggingInterceptor ;
69
49
import org .bson .BsonArray ;
70
50
import org .bson .BsonBoolean ;
71
51
import org .bson .BsonDocument ;
72
- import org .bson .BsonDouble ;
73
52
import org .bson .BsonInt32 ;
74
- import org .bson .BsonInt64 ;
75
53
import org .bson .BsonString ;
76
54
import org .bson .BsonValue ;
77
55
97
75
import static com .mongodb .assertions .Assertions .notNull ;
98
76
import static com .mongodb .client .Fixture .getMongoClientSettingsBuilder ;
99
77
import static com .mongodb .client .Fixture .getMultiMongosMongoClientSettingsBuilder ;
100
- import static com .mongodb .client .unified .EventMatcher .getReasonString ;
101
78
import static com .mongodb .client .unified .UnifiedClientEncryptionHelper .createKmsProvidersMap ;
102
79
import static com .mongodb .client .unified .UnifiedCrudHelper .asReadConcern ;
103
80
import static com .mongodb .client .unified .UnifiedCrudHelper .asReadPreference ;
104
81
import static com .mongodb .client .unified .UnifiedCrudHelper .asWriteConcern ;
105
82
import static com .mongodb .internal .connection .AbstractConnectionPoolTest .waitForPoolAsyncWorkManagerStart ;
106
83
import static java .lang .System .getenv ;
107
84
import static java .util .Arrays .asList ;
108
- import static java .util .Collections .synchronizedList ;
109
85
import static org .junit .Assume .assumeTrue ;
110
86
111
87
public final class Entities {
112
88
private static final Set <String > SUPPORTED_CLIENT_ENTITY_OPTIONS = new HashSet <>(
113
89
asList (
114
- "id" , "uriOptions" , "serverApi" , "useMultipleMongoses" , "storeEventsAsEntities " ,
115
- "observeEvents" , " observeLogMessages" , "observeSensitiveCommands" , "ignoreCommandMonitoringEvents" ));
90
+ "id" , "uriOptions" , "serverApi" , "useMultipleMongoses" , "observeEvents " ,
91
+ "observeLogMessages" , "observeSensitiveCommands" , "ignoreCommandMonitoringEvents" ));
116
92
private final Set <String > entityNames = new HashSet <>();
117
93
private final Map <String , ExecutorService > threads = new HashMap <>();
118
94
private final Map <String , ArrayList <Future <?>>> tasks = new HashMap <>();
@@ -132,60 +108,8 @@ public final class Entities {
132
108
private final Map <String , TestServerMonitorListener > serverMonitorListeners = new HashMap <>();
133
109
private final Map <String , MongoCursor <BsonDocument >> cursors = new HashMap <>();
134
110
private final Map <String , ClusterDescription > topologyDescriptions = new HashMap <>();
135
- private final Map <String , Long > successCounts = new HashMap <>();
136
- private final Map <String , Long > iterationCounts = new HashMap <>();
137
- private final Map <String , BsonArray > errorDocumentsMap = new HashMap <>();
138
- private final Map <String , BsonArray > failureDocumentsMap = new HashMap <>();
139
111
private final Map <String , List <BsonDocument >> eventsMap = new HashMap <>();
140
112
141
- public boolean hasSuccessCount (final String id ) {
142
- return successCounts .containsKey (id );
143
- }
144
-
145
- public void addSuccessCount (final String id , final long count ) {
146
- putEntity (id , count , successCounts );
147
- }
148
-
149
- public Long getSuccessCount (final String id ) {
150
- return getEntity (id , successCounts , "successCount" );
151
- }
152
-
153
- public boolean hasIterationCount (final String id ) {
154
- return iterationCounts .containsKey (id );
155
- }
156
-
157
- public void addIterationCount (final String id , final long count ) {
158
- putEntity (id , count , iterationCounts );
159
- }
160
-
161
- public Long getIterationCount (final String id ) {
162
- return getEntity (id , iterationCounts , "successCount" );
163
- }
164
-
165
- public boolean hasErrorDocuments (final String id ) {
166
- return errorDocumentsMap .containsKey (id );
167
- }
168
-
169
- public void addErrorDocuments (final String id , final BsonArray errorDocuments ) {
170
- putEntity (id , errorDocuments , errorDocumentsMap );
171
- }
172
-
173
- public BsonArray getErrorDocuments (final String id ) {
174
- return getEntity (id , errorDocumentsMap , "errorDocuments" );
175
- }
176
-
177
- public boolean hasFailureDocuments (final String id ) {
178
- return failureDocumentsMap .containsKey (id );
179
- }
180
-
181
- public void addFailureDocuments (final String id , final BsonArray failureDocuments ) {
182
- putEntity (id , failureDocuments , failureDocumentsMap );
183
- }
184
-
185
- public BsonArray getFailureDocuments (final String id ) {
186
- return getEntity (id , failureDocumentsMap , "failureDocuments" );
187
- }
188
-
189
113
public boolean hasEvents (final String id ) {
190
114
return eventsMap .containsKey (id );
191
115
}
@@ -433,35 +357,6 @@ private void initClient(final BsonDocument entity, final String id,
433
357
putEntity (id + "-connection-pool-listener" , testConnectionPoolListener , clientConnectionPoolListeners );
434
358
}
435
359
436
- if (entity .containsKey ("storeEventsAsEntities" )) {
437
- BsonArray storeEventsAsEntitiesArray = entity .getArray ("storeEventsAsEntities" );
438
- for (BsonValue eventValue : storeEventsAsEntitiesArray ) {
439
- BsonDocument eventDocument = eventValue .asDocument ();
440
- String key = eventDocument .getString ("id" ).getValue ();
441
- BsonArray eventList = eventDocument .getArray ("events" );
442
- List <BsonDocument > eventDocumentList = synchronizedList (new ArrayList <>());
443
- putEntity (key , eventDocumentList , eventsMap );
444
-
445
- if (eventList .stream ()
446
- .map (value -> value .asString ().getValue ())
447
- .anyMatch (value -> value .startsWith ("Command" ))) {
448
- clientSettingsBuilder .addCommandListener (new EntityCommandListener (eventList .stream ()
449
- .map (value -> value .asString ().getValue ())
450
- .collect (Collectors .toSet ()),
451
- eventDocumentList ));
452
- }
453
- if (eventList .stream ()
454
- .map (value -> value .asString ().getValue ())
455
- .anyMatch (value -> value .startsWith ("Pool" ) || value .startsWith ("Connection" ))) {
456
- clientSettingsBuilder .
457
- applyToConnectionPoolSettings (builder ->
458
- builder .addConnectionPoolListener (new EntityConnectionPoolListener (eventList .stream ()
459
- .map (value -> value .asString ().getValue ())
460
- .collect (Collectors .toSet ()),
461
- eventDocumentList )));
462
- }
463
- }
464
- }
465
360
clientSettingsBuilder .applyToServerSettings (builder -> {
466
361
builder .heartbeatFrequency (50 , TimeUnit .MILLISECONDS );
467
362
builder .minHeartbeatFrequency (50 , TimeUnit .MILLISECONDS );
@@ -795,154 +690,4 @@ public void close() {
795
690
clientLoggingInterceptors .values ().forEach (TestLoggingInterceptor ::close );
796
691
threads .values ().forEach (ExecutorService ::shutdownNow );
797
692
}
798
-
799
- private static class EntityCommandListener implements CommandListener {
800
- private final List <BsonDocument > eventDocumentList ;
801
- private final Set <String > enabledEvents ;
802
-
803
- EntityCommandListener (final Set <String > enabledEvents , final List <BsonDocument > eventDocumentList ) {
804
- this .eventDocumentList = eventDocumentList ;
805
- this .enabledEvents = enabledEvents ;
806
- }
807
-
808
- @ Override
809
- public void commandStarted (final CommandStartedEvent event ) {
810
- if (enabledEvents .contains ("CommandStartedEvent" )) {
811
- eventDocumentList .add (createEventDocument (event , "CommandStartedEvent" )
812
- .append ("databaseName" , new BsonString (event .getDatabaseName ())));
813
- }
814
- }
815
-
816
- @ Override
817
- public void commandSucceeded (final CommandSucceededEvent event ) {
818
- if (enabledEvents .contains ("CommandSucceededEvent" )) {
819
- eventDocumentList .add (createEventDocument (event , "CommandSucceededEvent" )
820
- .append ("duration" , new BsonInt64 (event .getElapsedTime (TimeUnit .MILLISECONDS ))));
821
- }
822
- }
823
-
824
- @ Override
825
- public void commandFailed (final CommandFailedEvent event ) {
826
- if (enabledEvents .contains ("CommandFailedEvent" )) {
827
- eventDocumentList .add (createEventDocument (event , "CommandFailedEvent" )
828
- .append ("duration" ,
829
- new BsonDouble (event .getElapsedTime (TimeUnit .NANOSECONDS ) / 1_000_000_000.0 ))
830
- .append ("failure" , new BsonString (event .getThrowable ().toString ())));
831
- }
832
- }
833
-
834
- private BsonDocument createEventDocument (final CommandEvent event , final String name ) {
835
- return new BsonDocument ()
836
- .append ("name" , new BsonString (name ))
837
- .append ("observedAt" , new BsonDouble (System .currentTimeMillis () / 1000.0 ))
838
- .append ("commandName" , new BsonString (event .getCommandName ()))
839
- .append ("requestId" , new BsonInt32 (event .getRequestId ()));
840
- }
841
- }
842
-
843
- private static class EntityConnectionPoolListener implements ConnectionPoolListener {
844
- private final List <BsonDocument > eventDocumentList ;
845
- private final Set <String > enabledEvents ;
846
-
847
- EntityConnectionPoolListener (final Set <String > enabledEvents , final List <BsonDocument > eventDocumentList ) {
848
- this .eventDocumentList = eventDocumentList ;
849
- this .enabledEvents = enabledEvents ;
850
- }
851
-
852
- @ Override
853
- public void connectionPoolCreated (final ConnectionPoolCreatedEvent event ) {
854
- if (enabledEvents .contains ("PoolCreatedEvent" )) {
855
- eventDocumentList .add (createEventDocument ("PoolCreatedEvent" , event .getServerId ()));
856
- }
857
- }
858
-
859
- @ Override
860
- public void connectionPoolCleared (final ConnectionPoolClearedEvent event ) {
861
- if (enabledEvents .contains ("PoolClearedEvent" )) {
862
- eventDocumentList .add (createEventDocument ("PoolClearedEvent" , event .getServerId ()));
863
- }
864
- }
865
-
866
- @ Override
867
- public void connectionPoolReady (final ConnectionPoolReadyEvent event ) {
868
- if (enabledEvents .contains ("PoolReadyEvent" )) {
869
- eventDocumentList .add (createEventDocument ("PoolReadyEvent" , event .getServerId ()));
870
- }
871
- }
872
-
873
- @ Override
874
- public void connectionPoolClosed (final ConnectionPoolClosedEvent event ) {
875
- if (enabledEvents .contains ("PoolClosedEvent" )) {
876
- eventDocumentList .add (createEventDocument ("PoolClosedEvent" , event .getServerId ()));
877
- }
878
- }
879
-
880
- @ Override
881
- public void connectionCheckOutStarted (final ConnectionCheckOutStartedEvent event ) {
882
- if (enabledEvents .contains ("ConnectionCheckOutStartedEvent" )) {
883
- eventDocumentList .add (createEventDocument ("ConnectionCheckOutStartedEvent" , event .getServerId ()));
884
- }
885
- }
886
-
887
- @ Override
888
- public void connectionCheckedOut (final ConnectionCheckedOutEvent event ) {
889
- if (enabledEvents .contains ("ConnectionCheckedOutEvent" )) {
890
- eventDocumentList .add (createEventDocument ("ConnectionCheckedOutEvent" , event .getConnectionId ()));
891
- }
892
- }
893
-
894
- @ Override
895
- public void connectionCheckOutFailed (final ConnectionCheckOutFailedEvent event ) {
896
- if (enabledEvents .contains ("ConnectionCheckOutFailedEvent" )) {
897
- eventDocumentList .add (createEventDocument ("ConnectionCheckOutFailedEvent" , event .getServerId ())
898
- .append ("reason" , new BsonString (getReasonString (event .getReason ()))));
899
- }
900
- }
901
-
902
- @ Override
903
- public void connectionCheckedIn (final ConnectionCheckedInEvent event ) {
904
- if (enabledEvents .contains ("ConnectionCheckedInEvent" )) {
905
- eventDocumentList .add (createEventDocument ("ConnectionCheckedInEvent" , event .getConnectionId ()));
906
- }
907
- }
908
-
909
- @ Override
910
- public void connectionCreated (final ConnectionCreatedEvent event ) {
911
- if (enabledEvents .contains ("ConnectionCreatedEvent" )) {
912
- eventDocumentList .add (createEventDocument ("ConnectionCreatedEvent" , event .getConnectionId ()));
913
- }
914
- }
915
-
916
- @ Override
917
- public void connectionReady (final ConnectionReadyEvent event ) {
918
- if (enabledEvents .contains ("ConnectionReadyEvent" )) {
919
- eventDocumentList .add (createEventDocument ("ConnectionReadyEvent" , event .getConnectionId ()));
920
- }
921
- }
922
-
923
- @ Override
924
- public void connectionClosed (final ConnectionClosedEvent event ) {
925
- if (enabledEvents .contains ("ConnectionClosedEvent" )) {
926
- eventDocumentList .add (createEventDocument ("ConnectionClosedEvent" , event .getConnectionId ())
927
- .append ("reason" , new BsonString (getReasonString (event .getReason ()))));
928
- }
929
- }
930
-
931
- private BsonDocument createEventDocument (final String name , final ConnectionId connectionId ) {
932
- return createEventDocument (name , connectionId .getServerId ())
933
- .append ("connectionId" , new BsonString (Long .toString (connectionId .getLocalValue ())));
934
- }
935
-
936
- private BsonDocument createEventDocument (final String name , final ServerId serverId ) {
937
- return new BsonDocument ()
938
- .append ("name" , new BsonString (name ))
939
- .append ("observedAt" , new BsonDouble (System .currentTimeMillis () / 1000.0 ))
940
- .append ("address" , new BsonString (getAddressAsString (serverId )));
941
- }
942
-
943
- @ NonNull
944
- private String getAddressAsString (final ServerId serverId ) {
945
- return serverId .getAddress ().getHost () + ":" + serverId .getAddress ().getPort ();
946
- }
947
- }
948
693
}
0 commit comments