21
21
import com .mongodb .connection .ClusterId ;
22
22
import com .mongodb .connection .ClusterSettings ;
23
23
import com .mongodb .connection .ClusterType ;
24
+ import com .mongodb .connection .ServerDescription ;
24
25
import com .mongodb .lang .Nullable ;
25
26
26
27
import java .util .ArrayList ;
27
28
import java .util .Collection ;
28
29
import java .util .Collections ;
29
30
import java .util .List ;
30
31
import java .util .concurrent .ThreadLocalRandom ;
32
+ import java .util .stream .Collectors ;
31
33
32
34
import static com .mongodb .assertions .Assertions .assertNotNull ;
33
35
@@ -38,7 +40,6 @@ public final class DnsMultiServerCluster extends AbstractMultiServerCluster {
38
40
private final DnsSrvRecordMonitor dnsSrvRecordMonitor ;
39
41
private volatile MongoException srvResolutionException ;
40
42
41
-
42
43
public DnsMultiServerCluster (final ClusterId clusterId , final ClusterSettings settings , final ClusterableServerFactory serverFactory ,
43
44
final DnsSrvRecordMonitorFactory dnsSrvRecordMonitorFactory ) {
44
45
super (clusterId , settings , serverFactory );
@@ -57,17 +58,33 @@ public void initialize(final Collection<ServerAddress> hosts) {
57
58
}
58
59
}
59
60
60
- private Collection <ServerAddress > applySrvMaxHosts (final Collection <ServerAddress > hosts ) {
61
- Collection <ServerAddress > newHosts = hosts ;
61
+ private Collection <ServerAddress > applySrvMaxHosts (final Collection <ServerAddress > latestSrvHosts ) {
62
62
Integer srvMaxHosts = getSettings ().getSrvMaxHosts ();
63
- if (srvMaxHosts != null && srvMaxHosts > 0 ) {
64
- if (srvMaxHosts < hosts .size ()) {
65
- List <ServerAddress > newHostsList = new ArrayList <>(hosts );
66
- Collections .shuffle (newHostsList , ThreadLocalRandom .current ());
67
- newHosts = newHostsList .subList (0 , srvMaxHosts );
68
- }
63
+ if (srvMaxHosts == null || srvMaxHosts <= 0 || latestSrvHosts .size () <= srvMaxHosts ) {
64
+ return new ArrayList <>(latestSrvHosts );
69
65
}
70
- return newHosts ;
66
+ List <ServerAddress > activeHosts = getActivePriorHosts (latestSrvHosts );
67
+ int numNewHostsToAdd = srvMaxHosts - activeHosts .size ();
68
+ activeHosts .addAll (addShuffledHosts (latestSrvHosts , activeHosts , numNewHostsToAdd ));
69
+
70
+ return activeHosts ;
71
+ }
72
+
73
+ private List <ServerAddress > getActivePriorHosts (final Collection <ServerAddress > latestSrvHosts ) {
74
+ List <ServerAddress > priorHosts = DnsMultiServerCluster .this .getCurrentDescription ().getServerDescriptions ().stream ()
75
+ .map (ServerDescription ::getAddress ).collect (Collectors .toList ());
76
+ priorHosts .removeIf (host -> !latestSrvHosts .contains (host ));
77
+
78
+ return priorHosts ;
79
+ }
80
+
81
+ private List <ServerAddress > addShuffledHosts (final Collection <ServerAddress > latestSrvHosts ,
82
+ final List <ServerAddress > activePriorHosts , final int numNewHostsToAdd ) {
83
+ List <ServerAddress > addedHosts = new ArrayList <>(latestSrvHosts );
84
+ addedHosts .removeAll (activePriorHosts );
85
+ Collections .shuffle (addedHosts , ThreadLocalRandom .current ());
86
+
87
+ return addedHosts .subList (0 , numNewHostsToAdd );
71
88
}
72
89
73
90
@ Override
0 commit comments