Skip to content

Commit ffe64ec

Browse files
authored
Refactor internal APIs to avoid requiring duplicate data in method/constructor params (#871)
JAVA-4471
1 parent 47c99b5 commit ffe64ec

21 files changed

+109
-156
lines changed

driver-core/src/main/com/mongodb/internal/connection/AbstractMultiServerCluster.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,6 @@ public ClusterableServer getServer(final ServerAddress serverAddress) {
131131
return serverTuple.server;
132132
}
133133

134-
135-
private final class DefaultServerDescriptionChangedListener implements ServerDescriptionChangedListener {
136-
@Override
137-
public void serverDescriptionChanged(final ServerDescriptionChangedEvent event) {
138-
onChange(event);
139-
}
140-
}
141-
142134
void onChange(final Collection<ServerAddress> newHosts) {
143135
withLock(() -> {
144136
if (isClosed()) {
@@ -167,7 +159,8 @@ void onChange(final Collection<ServerAddress> newHosts) {
167159
});
168160
}
169161

170-
private void onChange(final ServerDescriptionChangedEvent event) {
162+
@Override
163+
public void onChange(final ServerDescriptionChangedEvent event) {
171164
withLock(() -> {
172165
if (isClosed()) {
173166
return;
@@ -347,7 +340,7 @@ private void addServer(final ServerAddress serverAddress) {
347340
if (LOGGER.isInfoEnabled()) {
348341
LOGGER.info(format("Adding discovered server %s to client view of cluster", serverAddress));
349342
}
350-
ClusterableServer server = createServer(serverAddress, new DefaultServerDescriptionChangedListener());
343+
ClusterableServer server = createServer(serverAddress);
351344
addressToServerTupleMap.put(serverAddress, new ServerTuple(server, getConnectingServerDescription(serverAddress)));
352345
}
353346
}

driver-core/src/main/com/mongodb/internal/connection/BaseCluster.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import com.mongodb.lang.Nullable;
3939
import com.mongodb.selector.CompositeServerSelector;
4040
import com.mongodb.selector.ServerSelector;
41-
import org.bson.BsonTimestamp;
4241

4342
import java.util.ArrayList;
4443
import java.util.Collections;
@@ -91,8 +90,8 @@ abstract class BaseCluster implements Cluster {
9190
}
9291

9392
@Override
94-
public BsonTimestamp getClusterTime() {
95-
return clusterClock.getClusterTime();
93+
public ClusterClock getClock() {
94+
return clusterClock;
9695
}
9796

9897
@Override
@@ -392,9 +391,8 @@ private ServerSelector getCompositeServerSelector(final ServerSelector serverSel
392391
}
393392
}
394393

395-
protected ClusterableServer createServer(final ServerAddress serverAddress,
396-
final ServerDescriptionChangedListener serverDescriptionChangedListener) {
397-
return serverFactory.create(this, serverAddress, serverDescriptionChangedListener, clusterClock);
394+
protected ClusterableServer createServer(final ServerAddress serverAddress) {
395+
return serverFactory.create(this, serverAddress);
398396
}
399397

400398
private void throwIfIncompatible(final ClusterDescription curDescription) {

driver-core/src/main/com/mongodb/internal/connection/Cluster.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919

2020
import com.mongodb.ServerAddress;
2121
import com.mongodb.connection.ClusterId;
22+
import com.mongodb.event.ServerDescriptionChangedEvent;
2223
import com.mongodb.internal.VisibleForTesting;
2324
import com.mongodb.internal.async.SingleResultCallback;
2425
import com.mongodb.connection.ClusterDescription;
2526
import com.mongodb.connection.ClusterSettings;
2627
import com.mongodb.lang.Nullable;
2728
import com.mongodb.selector.ServerSelector;
28-
import org.bson.BsonTimestamp;
2929

3030
import java.io.Closeable;
3131

@@ -69,13 +69,9 @@ public interface Cluster extends Closeable {
6969
ClusterDescription getCurrentDescription();
7070

7171
/**
72-
* Get the last seen cluster time
73-
*
74-
* @since 3.8
75-
* @return the last seen cluster time or null if not set
72+
* Get the {@link ClusterClock} from which one may get the last seen cluster time.
7673
*/
77-
@Nullable
78-
BsonTimestamp getClusterTime();
74+
ClusterClock getClock();
7975

8076
ServerTuple selectServer(ServerSelector serverSelector);
8177

@@ -99,4 +95,11 @@ public interface Cluster extends Closeable {
9995
* @param action The action to {@linkplain Runnable#run() do}.
10096
*/
10197
void withLock(Runnable action);
98+
99+
/**
100+
* This method allows {@link Server}s to notify the {@link Cluster} about changes in their state as per the
101+
* <a href="https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst">
102+
* Server Discovery And Monitoring</a> specification.
103+
*/
104+
void onChange(ServerDescriptionChangedEvent event);
102105
}

driver-core/src/main/com/mongodb/internal/connection/ClusterableServerFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
import com.mongodb.connection.ServerSettings;
2121

2222
public interface ClusterableServerFactory {
23-
ClusterableServer create(Cluster cluster, ServerAddress serverAddress,
24-
ServerDescriptionChangedListener serverDescriptionChangedListener, ClusterClock clusterClock);
23+
ClusterableServer create(Cluster cluster, ServerAddress serverAddress);
2524

2625
ServerSettings getSettings();
2726
}

driver-core/src/main/com/mongodb/internal/connection/DefaultClusterFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ public Cluster createCluster(final ClusterSettings originalClusterSettings, fina
106106
DnsSrvRecordMonitorFactory dnsSrvRecordMonitorFactory = new DefaultDnsSrvRecordMonitorFactory(clusterId, serverSettings);
107107

108108
if (clusterSettings.getMode() == ClusterConnectionMode.LOAD_BALANCED) {
109-
ClusterableServerFactory serverFactory = new LoadBalancedClusterableServerFactory(clusterId, serverSettings,
109+
ClusterableServerFactory serverFactory = new LoadBalancedClusterableServerFactory(serverSettings,
110110
connectionPoolSettings, internalConnectionPoolSettings, streamFactory, credential, commandListener, applicationName,
111111
mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build(), compressorList,
112112
serverApi);
113113
return new LoadBalancedCluster(clusterId, clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
114114
} else {
115-
ClusterableServerFactory serverFactory = new DefaultClusterableServerFactory(clusterId, clusterSettings, serverSettings,
115+
ClusterableServerFactory serverFactory = new DefaultClusterableServerFactory(serverSettings,
116116
connectionPoolSettings, internalConnectionPoolSettings,
117117
streamFactory, heartbeatStreamFactory, credential, commandListener, applicationName,
118118
mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build(), compressorList,

driver-core/src/main/com/mongodb/internal/connection/DefaultClusterableServerFactory.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
import com.mongodb.MongoDriverInformation;
2222
import com.mongodb.ServerAddress;
2323
import com.mongodb.ServerApi;
24-
import com.mongodb.connection.ClusterId;
25-
import com.mongodb.connection.ClusterSettings;
24+
import com.mongodb.connection.ClusterConnectionMode;
2625
import com.mongodb.connection.ConnectionPoolSettings;
2726
import com.mongodb.connection.ServerId;
2827
import com.mongodb.connection.ServerSettings;
@@ -38,8 +37,6 @@
3837
import static java.util.Collections.emptyList;
3938

4039
public class DefaultClusterableServerFactory implements ClusterableServerFactory {
41-
private final ClusterId clusterId;
42-
private final ClusterSettings clusterSettings;
4340
private final ServerSettings serverSettings;
4441
private final ConnectionPoolSettings connectionPoolSettings;
4542
private final InternalConnectionPoolSettings internalConnectionPoolSettings;
@@ -53,15 +50,13 @@ public class DefaultClusterableServerFactory implements ClusterableServerFactory
5350
@Nullable
5451
private final ServerApi serverApi;
5552

56-
public DefaultClusterableServerFactory(final ClusterId clusterId, final ClusterSettings clusterSettings,
53+
public DefaultClusterableServerFactory(
5754
final ServerSettings serverSettings, final ConnectionPoolSettings connectionPoolSettings,
5855
final InternalConnectionPoolSettings internalConnectionPoolSettings,
5956
final StreamFactory streamFactory, final StreamFactory heartbeatStreamFactory,
6057
final MongoCredential credential, final CommandListener commandListener,
6158
final String applicationName, final MongoDriverInformation mongoDriverInformation,
6259
final List<MongoCompressor> compressorList, final @Nullable ServerApi serverApi) {
63-
this.clusterId = clusterId;
64-
this.clusterSettings = clusterSettings;
6560
this.serverSettings = serverSettings;
6661
this.connectionPoolSettings = connectionPoolSettings;
6762
this.internalConnectionPoolSettings = internalConnectionPoolSettings;
@@ -76,27 +71,26 @@ public DefaultClusterableServerFactory(final ClusterId clusterId, final ClusterS
7671
}
7772

7873
@Override
79-
public ClusterableServer create(final Cluster cluster, final ServerAddress serverAddress,
80-
final ServerDescriptionChangedListener serverDescriptionChangedListener,
81-
final ClusterClock clusterClock) {
82-
ServerId serverId = new ServerId(clusterId, serverAddress);
74+
public ClusterableServer create(final Cluster cluster, final ServerAddress serverAddress) {
75+
ServerId serverId = new ServerId(cluster.getClusterId(), serverAddress);
76+
ClusterConnectionMode clusterMode = cluster.getSettings().getMode();
8377
SameObjectProvider<SdamServerDescriptionManager> sdamProvider = SameObjectProvider.uninitialized();
84-
ServerMonitor serverMonitor = new DefaultServerMonitor(serverId, serverSettings, clusterClock,
78+
ServerMonitor serverMonitor = new DefaultServerMonitor(serverId, serverSettings, cluster.getClock(),
8579
// no credentials, compressor list, or command listener for the server monitor factory
86-
new InternalStreamConnectionFactory(clusterSettings.getMode(), heartbeatStreamFactory, null, applicationName,
80+
new InternalStreamConnectionFactory(clusterMode, heartbeatStreamFactory, null, applicationName,
8781
mongoDriverInformation, emptyList(), null, serverApi),
8882
serverApi, sdamProvider);
8983
ConnectionPool connectionPool = new DefaultConnectionPool(serverId,
90-
new InternalStreamConnectionFactory(clusterSettings.getMode(), streamFactory, credential, applicationName,
84+
new InternalStreamConnectionFactory(clusterMode, streamFactory, credential, applicationName,
9185
mongoDriverInformation, compressorList, commandListener, serverApi),
9286
connectionPoolSettings, internalConnectionPoolSettings, sdamProvider);
9387
ServerListener serverListener = singleServerListener(serverSettings);
94-
SdamServerDescriptionManager sdam = new DefaultSdamServerDescriptionManager(cluster, serverId, serverDescriptionChangedListener,
95-
serverListener, serverMonitor, connectionPool, clusterSettings.getMode());
88+
SdamServerDescriptionManager sdam = new DefaultSdamServerDescriptionManager(cluster, serverId, serverListener, serverMonitor,
89+
connectionPool, clusterMode);
9690
sdamProvider.initialize(sdam);
9791
serverMonitor.start();
98-
return new DefaultServer(serverId, clusterSettings.getMode(), connectionPool, new DefaultConnectionFactory(), serverMonitor,
99-
sdam, serverListener, commandListener, clusterClock, true);
92+
return new DefaultServer(serverId, clusterMode, connectionPool, new DefaultConnectionFactory(), serverMonitor,
93+
sdam, serverListener, commandListener, cluster.getClock(), true);
10094
}
10195

10296
@Override

driver-core/src/main/com/mongodb/internal/connection/DefaultSdamServerDescriptionManager.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
final class DefaultSdamServerDescriptionManager implements SdamServerDescriptionManager {
3636
private final Cluster cluster;
3737
private final ServerId serverId;
38-
private final ServerDescriptionChangedListener serverDescriptionChangedListener;
3938
private final ServerListener serverListener;
4039
private final ServerMonitor serverMonitor;
4140
private final ConnectionPool connectionPool;
@@ -44,13 +43,11 @@ final class DefaultSdamServerDescriptionManager implements SdamServerDescription
4443

4544
DefaultSdamServerDescriptionManager(final Cluster cluster,
4645
final ServerId serverId,
47-
final ServerDescriptionChangedListener serverDescriptionChangedListener,
4846
final ServerListener serverListener, final ServerMonitor serverMonitor,
4947
final ConnectionPool connectionPool,
5048
final ClusterConnectionMode connectionMode) {
5149
this.cluster = cluster;
5250
this.serverId = assertNotNull(serverId);
53-
this.serverDescriptionChangedListener = assertNotNull(serverDescriptionChangedListener);
5451
this.serverListener = assertNotNull(serverListener);
5552
this.serverMonitor = assertNotNull(serverMonitor);
5653
this.connectionPool = assertNotNull(connectionPool);
@@ -113,7 +110,7 @@ private void updateDescription(final ServerDescription newDescription) {
113110
if (!wouldDescriptionsGenerateEquivalentEvents(newDescription, previousDescription)) {
114111
serverListener.serverDescriptionChanged(serverDescriptionChangedEvent);
115112
}
116-
serverDescriptionChangedListener.serverDescriptionChanged(serverDescriptionChangedEvent);
113+
cluster.onChange(serverDescriptionChangedEvent);
117114
}
118115

119116
private void handleException(final SdamIssue sdamIssue, final boolean beforeHandshake) {

driver-core/src/main/com/mongodb/internal/connection/LoadBalancedCluster.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636
import com.mongodb.event.ClusterDescriptionChangedEvent;
3737
import com.mongodb.event.ClusterListener;
3838
import com.mongodb.event.ClusterOpeningEvent;
39+
import com.mongodb.event.ServerDescriptionChangedEvent;
3940
import com.mongodb.internal.async.SingleResultCallback;
4041
import com.mongodb.lang.Nullable;
4142
import com.mongodb.selector.ServerSelector;
42-
import org.bson.BsonTimestamp;
4343

4444
import java.util.ArrayList;
4545
import java.util.Collection;
@@ -159,7 +159,7 @@ private void init(final ClusterId clusterId, final ClusterableServerFactory serv
159159
.address(host)
160160
.build()),
161161
settings, serverFactory.getSettings());
162-
server = serverFactory.create(this, host, event -> { }, clusterClock);
162+
server = serverFactory.create(this, host);
163163

164164
clusterListener.clusterDescriptionChanged(new ClusterDescriptionChangedEvent(clusterId, description, initialDescription));
165165
}
@@ -196,9 +196,9 @@ public ClusterDescription getCurrentDescription() {
196196
}
197197

198198
@Override
199-
public BsonTimestamp getClusterTime() {
199+
public ClusterClock getClock() {
200200
isTrue("open", !isClosed());
201-
return clusterClock.getClusterTime();
201+
return clusterClock;
202202
}
203203

204204
@Override
@@ -287,6 +287,11 @@ public void withLock(final Runnable action) {
287287
fail();
288288
}
289289

290+
@Override
291+
public void onChange(final ServerDescriptionChangedEvent event) {
292+
fail();
293+
}
294+
290295
private void handleServerSelectionRequest(final ServerSelectionRequest serverSelectionRequest) {
291296
assertTrue(initializationCompleted);
292297
if (srvRecordResolvedToMultipleHosts) {

driver-core/src/main/com/mongodb/internal/connection/LoadBalancedClusterableServerFactory.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.mongodb.ServerApi;
2424
import com.mongodb.annotations.ThreadSafe;
2525
import com.mongodb.connection.ClusterConnectionMode;
26-
import com.mongodb.connection.ClusterId;
2726
import com.mongodb.connection.ConnectionPoolSettings;
2827
import com.mongodb.connection.ServerId;
2928
import com.mongodb.connection.ServerSettings;
@@ -37,7 +36,6 @@
3736

3837
@ThreadSafe
3938
public class LoadBalancedClusterableServerFactory implements ClusterableServerFactory {
40-
private final ClusterId clusterId;
4139
private final ServerSettings serverSettings;
4240
private final ConnectionPoolSettings connectionPoolSettings;
4341
private final InternalConnectionPoolSettings internalConnectionPoolSettings;
@@ -49,14 +47,13 @@ public class LoadBalancedClusterableServerFactory implements ClusterableServerFa
4947
private final List<MongoCompressor> compressorList;
5048
private final ServerApi serverApi;
5149

52-
public LoadBalancedClusterableServerFactory(final ClusterId clusterId, final ServerSettings serverSettings,
50+
public LoadBalancedClusterableServerFactory(final ServerSettings serverSettings,
5351
final ConnectionPoolSettings connectionPoolSettings,
5452
final InternalConnectionPoolSettings internalConnectionPoolSettings,
5553
final StreamFactory streamFactory, final MongoCredential credential,
5654
final CommandListener commandListener,
5755
final String applicationName, final MongoDriverInformation mongoDriverInformation,
5856
final List<MongoCompressor> compressorList, final ServerApi serverApi) {
59-
this.clusterId = clusterId;
6057
this.serverSettings = serverSettings;
6158
this.connectionPoolSettings = connectionPoolSettings;
6259
this.internalConnectionPoolSettings = internalConnectionPoolSettings;
@@ -70,17 +67,15 @@ public LoadBalancedClusterableServerFactory(final ClusterId clusterId, final Ser
7067
}
7168

7269
@Override
73-
public ClusterableServer create(final Cluster cluster, final ServerAddress serverAddress,
74-
final ServerDescriptionChangedListener serverDescriptionChangedListener,
75-
final ClusterClock clusterClock) {
76-
ConnectionPool connectionPool = new DefaultConnectionPool(new ServerId(clusterId, serverAddress),
70+
public ClusterableServer create(final Cluster cluster, final ServerAddress serverAddress) {
71+
ConnectionPool connectionPool = new DefaultConnectionPool(new ServerId(cluster.getClusterId(), serverAddress),
7772
new InternalStreamConnectionFactory(ClusterConnectionMode.LOAD_BALANCED, streamFactory, credential, applicationName,
7873
mongoDriverInformation, compressorList, commandListener, serverApi),
7974
connectionPoolSettings, internalConnectionPoolSettings, EmptyProvider.instance());
8075
connectionPool.ready();
8176

82-
return new LoadBalancedServer(new ServerId(clusterId, serverAddress), connectionPool, new DefaultConnectionFactory(),
83-
singleServerListener(serverSettings), clusterClock);
77+
return new LoadBalancedServer(new ServerId(cluster.getClusterId(), serverAddress), connectionPool, new DefaultConnectionFactory(),
78+
singleServerListener(serverSettings), cluster.getClock());
8479
}
8580

8681
@Override

driver-core/src/main/com/mongodb/internal/connection/ServerDescriptionChangedListener.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)