abstract class BaseCluster extends java.lang.Object implements Cluster
Modifier and Type | Class and Description |
---|---|
private static class |
BaseCluster.ServerSelectionRequest |
private class |
BaseCluster.WaitQueueHandler |
Modifier and Type | Field and Description |
---|---|
private ClusterId |
clusterId |
private ClusterListener |
clusterListener |
private ClusterDescription |
description |
private boolean |
isClosed |
private static Logger |
LOGGER |
private java.util.concurrent.atomic.AtomicReference<java.util.concurrent.CountDownLatch> |
phase |
private java.lang.ThreadLocal<java.util.Random> |
random |
private ClusterableServerFactory |
serverFactory |
private ClusterSettings |
settings |
private java.util.Deque<BaseCluster.ServerSelectionRequest> |
waitQueue |
private java.lang.Thread |
waitQueueHandler |
private java.util.concurrent.atomic.AtomicInteger |
waitQueueSize |
Constructor and Description |
---|
BaseCluster(ClusterId clusterId,
ClusterSettings settings,
ClusterableServerFactory serverFactory) |
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes connections to the servers in the cluster.
|
protected abstract void |
connect() |
private MongoIncompatibleDriverException |
createIncompatibleException(ClusterDescription curDescription) |
protected ClusterableServer |
createServer(ServerAddress serverAddress,
ServerListener serverListener) |
private MongoTimeoutException |
createTimeoutException(ServerSelector serverSelector,
ClusterDescription curDescription) |
private MongoWaitQueueFullException |
createWaitQueueFullException() |
protected void |
fireChangeEvent(ClusterDescriptionChangedEvent event) |
protected ClusterId |
getClusterId() |
private ServerSelector |
getCompositeServerSelector(ServerSelector serverSelector) |
(package private) ClusterDescription |
getCurrentDescription() |
ClusterDescription |
getDescription()
Get the description of this cluster.
|
private long |
getMaxWaitTimeNanos() |
private long |
getMinWaitTimeNanos() |
private java.util.Random |
getRandom() |
private ClusterableServer |
getRandomServer(java.util.List<ServerDescription> serverDescriptions) |
protected abstract ClusterableServer |
getServer(ServerAddress serverAddress)
Return the server at the given address.
|
ClusterableServerFactory |
getServerFactory() |
ClusterSettings |
getSettings()
Gets the cluster settings with which this cluster was created.
|
private boolean |
handleServerSelectionRequest(BaseCluster.ServerSelectionRequest request,
java.util.concurrent.CountDownLatch currentPhase,
ClusterDescription description) |
boolean |
isClosed()
Whether all the servers in the cluster are closed or not.
|
private void |
logServerSelectionFailure(ServerSelector serverSelector,
ClusterDescription curDescription) |
private void |
notifyWaitQueueHandler(BaseCluster.ServerSelectionRequest request) |
private Server |
selectRandomServer(ServerSelector serverSelector,
ClusterDescription clusterDescription) |
Server |
selectServer(ServerSelector serverSelector)
Get a MongoDB server that matches the criteria defined by the serverSelector
|
void |
selectServerAsync(ServerSelector serverSelector,
SingleResultCallback<Server> callback)
Asynchronously gets a MongoDB server that matches the criteria defined by the serverSelector.
|
private void |
stopWaitQueueHandler() |
private void |
throwIfIncompatible(ClusterDescription curDescription) |
protected void |
updateDescription(ClusterDescription newDescription) |
private static final Logger LOGGER
private final java.util.concurrent.atomic.AtomicReference<java.util.concurrent.CountDownLatch> phase
private final ClusterableServerFactory serverFactory
private final java.lang.ThreadLocal<java.util.Random> random
private final ClusterId clusterId
private final ClusterSettings settings
private final ClusterListener clusterListener
private final java.util.Deque<BaseCluster.ServerSelectionRequest> waitQueue
private final java.util.concurrent.atomic.AtomicInteger waitQueueSize
private java.lang.Thread waitQueueHandler
private volatile boolean isClosed
private volatile ClusterDescription description
BaseCluster(ClusterId clusterId, ClusterSettings settings, ClusterableServerFactory serverFactory)
public Server selectServer(ServerSelector serverSelector)
Cluster
selectServer
in interface Cluster
serverSelector
- a ServerSelector that defines how to select the required Serverpublic void selectServerAsync(ServerSelector serverSelector, SingleResultCallback<Server> callback)
Cluster
selectServerAsync
in interface Cluster
serverSelector
- a ServerSelector that defines how to select the required Servercallback
- the callback to invoke when the server is found or an error occurspublic ClusterDescription getDescription()
Cluster
getDescription
in interface Cluster
protected ClusterId getClusterId()
public ClusterSettings getSettings()
Cluster
getSettings
in interface Cluster
public ClusterableServerFactory getServerFactory()
protected abstract void connect()
public void close()
Cluster
public boolean isClosed()
Cluster
protected abstract ClusterableServer getServer(ServerAddress serverAddress)
serverAddress
- the addressprotected void updateDescription(ClusterDescription newDescription)
protected void fireChangeEvent(ClusterDescriptionChangedEvent event)
ClusterDescription getCurrentDescription()
private long getMaxWaitTimeNanos()
private long getMinWaitTimeNanos()
private boolean handleServerSelectionRequest(BaseCluster.ServerSelectionRequest request, java.util.concurrent.CountDownLatch currentPhase, ClusterDescription description)
private void logServerSelectionFailure(ServerSelector serverSelector, ClusterDescription curDescription)
private Server selectRandomServer(ServerSelector serverSelector, ClusterDescription clusterDescription)
private ServerSelector getCompositeServerSelector(ServerSelector serverSelector)
private ClusterableServer getRandomServer(java.util.List<ServerDescription> serverDescriptions)
private java.util.Random getRandom()
protected ClusterableServer createServer(ServerAddress serverAddress, ServerListener serverListener)
private void throwIfIncompatible(ClusterDescription curDescription)
private MongoIncompatibleDriverException createIncompatibleException(ClusterDescription curDescription)
private MongoTimeoutException createTimeoutException(ServerSelector serverSelector, ClusterDescription curDescription)
private MongoWaitQueueFullException createWaitQueueFullException()
private void notifyWaitQueueHandler(BaseCluster.ServerSelectionRequest request)
private void stopWaitQueueHandler()