@@ -59,34 +59,40 @@ public BaseCluster(final String clusterId, final ClusterSettings settings, final
59
59
}
60
60
61
61
@ Override
62
- public Server getServer (final ServerSelector serverSelector , final long maxWaitTime , final TimeUnit timeUnit ) {
62
+ public Server getServer (final ServerSelector serverSelector , final long maxWaitTime , final TimeUnit maxWaitTimeTimeUnit ,
63
+ final long connectRetryFrequency , final TimeUnit connectRetryFrequencyTimeUnit ) {
63
64
isTrue ("open" , !isClosed ());
64
65
65
66
try {
66
67
CountDownLatch currentPhase = phase .get ();
67
68
ClusterDescription curDescription = description ;
68
69
List <ServerDescription > serverDescriptions = serverSelector .choose (curDescription );
69
- final long endTime = System .nanoTime () + NANOSECONDS .convert (maxWaitTime , timeUnit );
70
+ final long endTime = System .nanoTime () + NANOSECONDS .convert (maxWaitTime , maxWaitTimeTimeUnit );
71
+ boolean selectionFailureLogged = false ;
70
72
while (true ) {
71
73
throwIfIncompatible (curDescription );
74
+
72
75
if (!serverDescriptions .isEmpty ()) {
73
76
ClusterableServer server = getRandomServer (new ArrayList <ServerDescription >(serverDescriptions ));
74
77
if (server != null ) {
75
78
return new WrappedServer (server );
76
79
}
77
80
}
78
81
79
- final long timeout = endTime - System .nanoTime ();
80
-
81
- LOGGER . info ( format ( "No server chosen by %s from cluster description %s. Waiting for %d ms before timing out" ,
82
- serverSelector , curDescription , MILLISECONDS . convert ( timeout , NANOSECONDS )));
82
+ if ( System .nanoTime () > endTime ) {
83
+ throw new MongoTimeoutException ( format ( "Timed out while waiting for a server that matches %s after %d ms" ,
84
+ serverSelector , MILLISECONDS . convert ( maxWaitTime , maxWaitTimeTimeUnit )));
85
+ }
83
86
87
+ if (!selectionFailureLogged ) {
88
+ LOGGER .info (format ("No server chosen by %s from cluster description %s. Waiting for %d ms before timing out" ,
89
+ serverSelector , curDescription , MILLISECONDS .convert (maxWaitTime , maxWaitTimeTimeUnit )));
90
+ selectionFailureLogged = true ;
91
+ }
84
92
connect ();
85
93
86
- if (!currentPhase .await (timeout , NANOSECONDS )) {
87
- throw new MongoTimeoutException (format ("Timed out while waiting for a server that matches %s after %d ms" ,
88
- serverSelector , MILLISECONDS .convert (maxWaitTime , timeUnit )));
89
- }
94
+ currentPhase .await (connectRetryFrequency , connectRetryFrequencyTimeUnit );
95
+
90
96
currentPhase = phase .get ();
91
97
curDescription = description ;
92
98
serverDescriptions = serverSelector .choose (curDescription );
@@ -97,25 +103,32 @@ public Server getServer(final ServerSelector serverSelector, final long maxWaitT
97
103
}
98
104
99
105
@ Override
100
- public ClusterDescription getDescription (final long maxWaitTime , final TimeUnit timeUnit ) {
106
+ public ClusterDescription getDescription (final long maxWaitTime , final TimeUnit maxWaitTimeTimeUnit ,
107
+ final long connectRetryFrequency , final TimeUnit connectRetryFrequencyTimeUnit ) {
101
108
isTrue ("open" , !isClosed ());
102
109
103
110
try {
104
111
CountDownLatch currentPhase = phase .get ();
105
112
ClusterDescription curDescription = description ;
106
- final long endTime = System .nanoTime () + NANOSECONDS .convert (maxWaitTime , timeUnit );
113
+ final long endTime = System .nanoTime () + NANOSECONDS .convert (maxWaitTime , maxWaitTimeTimeUnit );
114
+ boolean selectionFailureLogged = false ;
107
115
while (curDescription .getType () == ClusterType .Unknown ) {
108
- final long timeout = endTime - System .nanoTime ();
109
116
110
- LOGGER .info (format ("Cluster description not yet available. Waiting for %d ms before timing out" ,
111
- MILLISECONDS .convert (timeout , NANOSECONDS )));
117
+ if (System .nanoTime () > endTime ) {
118
+ throw new MongoTimeoutException (format ("Timed out while waiting to connect after %d ms" ,
119
+ MILLISECONDS .convert (maxWaitTime , maxWaitTimeTimeUnit )));
120
+ }
121
+
122
+ if (!selectionFailureLogged ) {
123
+ LOGGER .info (format ("Cluster description not yet available. Waiting for %d ms before timing out" ,
124
+ MILLISECONDS .convert (maxWaitTime , maxWaitTimeTimeUnit )));
125
+ selectionFailureLogged = true ;
126
+ }
112
127
113
128
connect ();
114
129
115
- if (!currentPhase .await (timeout , NANOSECONDS )) {
116
- throw new MongoTimeoutException (format ("Timed out while waiting to connect after %d ms" ,
117
- MILLISECONDS .convert (maxWaitTime , timeUnit )));
118
- }
130
+ currentPhase .await (connectRetryFrequency , connectRetryFrequencyTimeUnit );
131
+
119
132
currentPhase = phase .get ();
120
133
curDescription = description ;
121
134
}
0 commit comments