com.mongodb.internal.binding.AsyncClusterBinding Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mongodb-driver-core Show documentation
Show all versions of mongodb-driver-core Show documentation
The Java operations layer for the MongoDB Java Driver.
Third parties can wrap this layer to provide custom higher-level APIs
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.internal.binding;
import com.mongodb.ReadConcern;
import com.mongodb.ReadPreference;
import com.mongodb.RequestContext;
import com.mongodb.ServerAddress;
import com.mongodb.ServerApi;
import com.mongodb.connection.ClusterConnectionMode;
import com.mongodb.connection.ServerDescription;
import com.mongodb.internal.async.SingleResultCallback;
import com.mongodb.internal.connection.AsyncConnection;
import com.mongodb.internal.connection.Cluster;
import com.mongodb.internal.connection.OperationContext;
import com.mongodb.internal.connection.ReadConcernAwareNoOpSessionContext;
import com.mongodb.internal.connection.Server;
import com.mongodb.internal.selector.ReadPreferenceServerSelector;
import com.mongodb.internal.selector.ReadPreferenceWithFallbackServerSelector;
import com.mongodb.internal.selector.ServerAddressSelector;
import com.mongodb.internal.selector.WritableServerSelector;
import com.mongodb.internal.session.SessionContext;
import com.mongodb.lang.Nullable;
import com.mongodb.selector.ServerSelector;
import static com.mongodb.assertions.Assertions.notNull;
/**
* A simple ReadWriteBinding implementation that supplies write connection sources bound to a possibly different primary each time, and a
* read connection source bound to a possible different server each time.
*
* This class is not part of the public API and may be removed or changed at any time
*/
public class AsyncClusterBinding extends AbstractReferenceCounted implements AsyncClusterAwareReadWriteBinding {
private final Cluster cluster;
private final ReadPreference readPreference;
private final ReadConcern readConcern;
@Nullable
private final ServerApi serverApi;
private final RequestContext requestContext;
private final OperationContext operationContext;
/**
* Creates an instance.
*
* @param cluster a non-null Cluster which will be used to select a server to bind to
* @param readPreference a non-null ReadPreference for read operations
* @param readConcern a non-null read concern
* @param serverApi a server API, which may be null
* @param requestContext the request context
* This class is not part of the public API and may be removed or changed at any time
*/
public AsyncClusterBinding(final Cluster cluster, final ReadPreference readPreference, final ReadConcern readConcern,
@Nullable final ServerApi serverApi, final RequestContext requestContext) {
this.cluster = notNull("cluster", cluster);
this.readPreference = notNull("readPreference", readPreference);
this.readConcern = (notNull("readConcern", readConcern));
this.serverApi = serverApi;
this.requestContext = notNull("requestContext", requestContext);
operationContext = new OperationContext();
}
@Override
public AsyncClusterAwareReadWriteBinding retain() {
super.retain();
return this;
}
@Override
public ReadPreference getReadPreference() {
return readPreference;
}
@Override
public SessionContext getSessionContext() {
return new ReadConcernAwareNoOpSessionContext(readConcern);
}
@Override
@Nullable
public ServerApi getServerApi() {
return serverApi;
}
@Override
public RequestContext getRequestContext() {
return requestContext;
}
@Override
public OperationContext getOperationContext() {
return operationContext;
}
@Override
public void getReadConnectionSource(final SingleResultCallback callback) {
getAsyncClusterBindingConnectionSource(new ReadPreferenceServerSelector(readPreference), callback);
}
@Override
public void getReadConnectionSource(final int minWireVersion, final ReadPreference fallbackReadPreference,
final SingleResultCallback callback) {
// Assume 5.0+ for load-balanced mode
if (cluster.getSettings().getMode() == ClusterConnectionMode.LOAD_BALANCED) {
getReadConnectionSource(callback);
} else {
ReadPreferenceWithFallbackServerSelector readPreferenceWithFallbackServerSelector
= new ReadPreferenceWithFallbackServerSelector(readPreference, minWireVersion, fallbackReadPreference);
cluster.selectServerAsync(readPreferenceWithFallbackServerSelector, operationContext, (result, t) -> {
if (t != null) {
callback.onResult(null, t);
} else {
callback.onResult(new AsyncClusterBindingConnectionSource(result.getServer(), result.getServerDescription(),
readPreferenceWithFallbackServerSelector.getAppliedReadPreference()), null);
}
});
}
}
@Override
public void getWriteConnectionSource(final SingleResultCallback callback) {
getAsyncClusterBindingConnectionSource(new WritableServerSelector(), callback);
}
@Override
public void getConnectionSource(final ServerAddress serverAddress, final SingleResultCallback callback) {
getAsyncClusterBindingConnectionSource(new ServerAddressSelector(serverAddress), callback);
}
private void getAsyncClusterBindingConnectionSource(final ServerSelector serverSelector,
final SingleResultCallback callback) {
cluster.selectServerAsync(serverSelector, operationContext, (result, t) -> {
if (t != null) {
callback.onResult(null, t);
} else {
callback.onResult(new AsyncClusterBindingConnectionSource(result.getServer(), result.getServerDescription(),
readPreference), null);
}
});
}
private final class AsyncClusterBindingConnectionSource extends AbstractReferenceCounted implements AsyncConnectionSource {
private final Server server;
private final ServerDescription serverDescription;
private final ReadPreference appliedReadPreference;
private AsyncClusterBindingConnectionSource(final Server server, final ServerDescription serverDescription,
final ReadPreference appliedReadPreference) {
this.server = server;
this.serverDescription = serverDescription;
this.appliedReadPreference = appliedReadPreference;
AsyncClusterBinding.this.retain();
}
@Override
public ServerDescription getServerDescription() {
return serverDescription;
}
@Override
public SessionContext getSessionContext() {
return new ReadConcernAwareNoOpSessionContext(readConcern);
}
@Override
@Nullable
public ServerApi getServerApi() {
return serverApi;
}
@Override
public RequestContext getRequestContext() {
return requestContext;
}
@Override
public OperationContext getOperationContext() {
return operationContext;
}
@Override
public ReadPreference getReadPreference() {
return appliedReadPreference;
}
@Override
public void getConnection(final SingleResultCallback callback) {
server.getConnectionAsync(operationContext, callback);
}
public AsyncConnectionSource retain() {
super.retain();
AsyncClusterBinding.this.retain();
return this;
}
@Override
public int release() {
int count = super.release();
AsyncClusterBinding.this.release();
return count;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy