|
27 | 27 | import com.mongodb.event.ConnectionPoolListener; |
28 | 28 | import com.mongodb.event.ServerListener; |
29 | 29 | import com.mongodb.event.ServerMonitorListener; |
| 30 | +import com.mongodb.selector.ServerSelector; |
30 | 31 | import org.bson.codecs.configuration.CodecRegistry; |
31 | 32 |
|
32 | 33 | import javax.net.SocketFactory; |
@@ -65,6 +66,7 @@ public class MongoClientOptions { |
65 | 66 | private final boolean retryWrites; |
66 | 67 | private final ReadConcern readConcern; |
67 | 68 | private final CodecRegistry codecRegistry; |
| 69 | + private final ServerSelector serverSelector; |
68 | 70 |
|
69 | 71 | private final int minConnectionsPerHost; |
70 | 72 | private final int maxConnectionsPerHost; |
@@ -121,6 +123,7 @@ private MongoClientOptions(final Builder builder) { |
121 | 123 | retryWrites = builder.retryWrites; |
122 | 124 | readConcern = builder.readConcern; |
123 | 125 | codecRegistry = builder.codecRegistry; |
| 126 | + serverSelector = builder.serverSelector; |
124 | 127 | sslEnabled = builder.sslEnabled; |
125 | 128 | sslInvalidHostNameAllowed = builder.sslInvalidHostNameAllowed; |
126 | 129 | sslContext = builder.sslContext; |
@@ -564,6 +567,34 @@ public CodecRegistry getCodecRegistry() { |
564 | 567 | return codecRegistry; |
565 | 568 | } |
566 | 569 |
|
| 570 | + /** |
| 571 | + * Gets the server selector. |
| 572 | + * |
| 573 | + * <p>The server selector augments the normal server selection rules applied by the driver when determining |
| 574 | + * which server to send an operation to. At the point that it's called by the driver, the |
| 575 | + * {@link com.mongodb.connection.ClusterDescription} which is passed to it contains a list of |
| 576 | + * {@link com.mongodb.connection.ServerDescription} instances which satisfy either the configured {@link ReadPreference} for any |
| 577 | + * read operation or ones that can take writes (e.g. a standalone, mongos, or replica set primary).</p> |
| 578 | + * <p>The server selector can then filter the {@code ServerDescription} list using whatever criteria that is required by the |
| 579 | + * application.</p> |
| 580 | + * <p>After this selector executes, two additional selectors are applied by the driver:</p> |
| 581 | + * <ul> |
| 582 | + * <li>select from within the latency window</li> |
| 583 | + * <li>select a random server from those remaining</li> |
| 584 | + * </ul> |
| 585 | + * <p>To skip the latency window selector, an application can:</p> |
| 586 | + * <ul> |
| 587 | + * <li>configure the local threshold to a sufficiently high value so that it doesn't exclude any servers</li> |
| 588 | + * <li>return a list containing a single server from this selector (which will also make the random member selector a no-op)</li> |
| 589 | + * </ul> |
| 590 | + * |
| 591 | + * @return the server selector, which may be null |
| 592 | + * @since 3.6 |
| 593 | + */ |
| 594 | + public ServerSelector getServerSelector() { |
| 595 | + return serverSelector; |
| 596 | + } |
| 597 | + |
567 | 598 | /** |
568 | 599 | * Gets the list of added {@code ClusterListener}. The default is an empty list. |
569 | 600 | * |
@@ -799,6 +830,9 @@ public boolean equals(final Object o) { |
799 | 830 | if (!codecRegistry.equals(that.codecRegistry)) { |
800 | 831 | return false; |
801 | 832 | } |
| 833 | + if (serverSelector != null ? !serverSelector.equals(that.serverSelector) : that.serverSelector != null) { |
| 834 | + return false; |
| 835 | + } |
802 | 836 | if (!clusterListeners.equals(that.clusterListeners)) { |
803 | 837 | return false; |
804 | 838 | } |
@@ -828,6 +862,7 @@ public int hashCode() { |
828 | 862 | result = 31 * result + (retryWrites ? 1 : 0); |
829 | 863 | result = 31 * result + (readConcern != null ? readConcern.hashCode() : 0); |
830 | 864 | result = 31 * result + codecRegistry.hashCode(); |
| 865 | + result = 31 * result + (serverSelector != null ? serverSelector.hashCode() : 0); |
831 | 866 | result = 31 * result + clusterListeners.hashCode(); |
832 | 867 | result = 31 * result + commandListeners.hashCode(); |
833 | 868 | result = 31 * result + minConnectionsPerHost; |
@@ -869,6 +904,7 @@ public String toString() { |
869 | 904 | + ", retryWrites=" + retryWrites |
870 | 905 | + ", readConcern=" + readConcern |
871 | 906 | + ", codecRegistry=" + codecRegistry |
| 907 | + + ", serverSelector=" + serverSelector |
872 | 908 | + ", clusterListeners=" + clusterListeners |
873 | 909 | + ", commandListeners=" + commandListeners |
874 | 910 | + ", minConnectionsPerHost=" + minConnectionsPerHost |
@@ -923,7 +959,7 @@ public static class Builder { |
923 | 959 | private boolean retryWrites = false; |
924 | 960 | private ReadConcern readConcern = ReadConcern.DEFAULT; |
925 | 961 | private CodecRegistry codecRegistry = MongoClient.getDefaultCodecRegistry(); |
926 | | - |
| 962 | + private ServerSelector serverSelector; |
927 | 963 | private int minConnectionsPerHost; |
928 | 964 | private int maxConnectionsPerHost = 100; |
929 | 965 | private int threadsAllowedToBlockForConnectionMultiplier = 5; |
@@ -987,6 +1023,7 @@ public Builder(final MongoClientOptions options) { |
987 | 1023 | retryWrites = options.getRetryWrites(); |
988 | 1024 | readConcern = options.getReadConcern(); |
989 | 1025 | codecRegistry = options.getCodecRegistry(); |
| 1026 | + serverSelector = options.getServerSelector(); |
990 | 1027 | sslEnabled = options.isSslEnabled(); |
991 | 1028 | sslInvalidHostNameAllowed = options.isSslInvalidHostNameAllowed(); |
992 | 1029 | sslContext = options.getSslContext(); |
@@ -1313,6 +1350,19 @@ public Builder codecRegistry(final CodecRegistry codecRegistry) { |
1313 | 1350 | return this; |
1314 | 1351 | } |
1315 | 1352 |
|
| 1353 | + /** |
| 1354 | + * Sets a server selector that augments the normal server selection rules applied by the driver when determining |
| 1355 | + * which server to send an operation to. See {@link #getServerSelector()} for further details. |
| 1356 | + * |
| 1357 | + * @param serverSelector the server selector |
| 1358 | + * @return this |
| 1359 | + * @since 3.6 |
| 1360 | + * @see #getServerSelector() |
| 1361 | + */ |
| 1362 | + public Builder serverSelector(final ServerSelector serverSelector) { |
| 1363 | + this.serverSelector = serverSelector; |
| 1364 | + return this; |
| 1365 | + } |
1316 | 1366 | /** |
1317 | 1367 | * Adds the given command listener. |
1318 | 1368 | * |
|
0 commit comments