public class Cluster extends Object implements ClusterStats
Cluster
tracks the state of the cluster of MongoDB servers.
PropertyChangeEvents are fired when a server is added or marked writable/not
writable.
This class uses brute force synchronization to protect its internal state. It
is assumed that multiple connections will be concurrently updating the
Cluster
at once and that at any given time this class may not contain
the absolute truth about the state of the cluster. Instead connections should
keep querying for the state of the cluster via their connection until the
view the server returned and the Cluster
are consistent. Since this
class will not fire a PropertyChangeEvent
when the state is not truly
modified the simplest mechanism is to keep querying for the cluster state on
the connection until no addition change events are seen.
Modifier and Type | Class and Description |
---|---|
protected class |
Cluster.ServerListener
ServerListener provides a listener for the state updates of the
Server . |
Modifier and Type | Field and Description |
---|---|
protected MongoClientConfiguration |
myConfig
The configuration for connecting to the servers.
|
protected ConcurrentMap<String,Server> |
myServers
The complete list of servers.
|
protected VersionRange |
myServerVersionRange
The range of versions within the cluster.
|
protected int |
mySmallestMaxBatchedWriteOperations
The smallest maximum number of operations in a batch in the cluster.
|
protected long |
mySmallestMaxBsonObjectSize
The smallest maximum document size in the cluster.
|
static String |
SERVER_PROP
The property sued for adding a new server.
|
static String |
WRITABLE_PROP
The property name for if there is a writable server.
|
Constructor and Description |
---|
Cluster(MongoClientConfiguration config,
ClusterType type)
Creates a new CLusterState.
|
Modifier and Type | Method and Description |
---|---|
Server |
add(InetSocketAddress address)
|
Server |
add(String address)
|
void |
addListener(PropertyChangeListener listener)
Adds a listener to the state.
|
protected double[] |
cdf(List<Server> servers)
Computes a relative CDF (cumulative distribution function) for the
servers based on the latency from the client.
|
void |
clear()
Removes all of the servers from the cluster.
|
protected List<Server> |
findCandidateServer(ReadPreference readPreference)
Finds the candidate server, if known.
|
List<Server> |
findCandidateServers(ReadPreference readPreference)
Returns the set of servers that can be used based on the provided
ReadPreference . |
protected List<Server> |
findNearestCandidates(ReadPreference readPreference)
Returns the list of servers that match the read preference's tags.
|
protected List<Server> |
findNonWritableCandidates(ReadPreference readPreference)
Returns the list of non-writable servers that match the read preference's
tags.
|
List<Server> |
findServers(Message message1,
Message message2)
Locates the set of servers that can be used to send the specified
messages.
|
protected List<Server> |
findWritableCandidates(ReadPreference readPreference)
Returns the list of writable servers that match the read preference's
tags.
|
Server |
get(String address)
Returns the server state for the address provided.
|
List<Server> |
getNonWritableServers()
Returns a copy of the list of non-writable servers.
|
List<Server> |
getServers()
Returns a copy of the list of servers.
|
VersionRange |
getServerVersionRange()
Returns the range of versions that we currently see within the cluster.
|
int |
getSmallestMaxBatchedWriteOperations()
Returns smallest value for the maximum number of write operations allowed
in a single write command.
|
long |
getSmallestMaxBsonObjectSize()
Returns the smallest value for the maximum BSON object size within the
cluster.
|
ClusterType |
getType()
Returns the type of cluster.
|
List<Server> |
getWritableServers()
Returns a copy of the list of writable servers.
|
void |
remove(Server server)
Removes the specified server from the cluster.
|
void |
removeListener(PropertyChangeListener listener)
Removes a listener to the state.
|
protected void |
sort(List<Server> servers)
Sorts the servers based on the latency from the client.
|
protected void |
updateVersions()
Updates the min/max versions across all servers.
|
public static final String SERVER_PROP
public static final String WRITABLE_PROP
protected final MongoClientConfiguration myConfig
protected final ConcurrentMap<String,Server> myServers
protected VersionRange myServerVersionRange
protected int mySmallestMaxBatchedWriteOperations
protected long mySmallestMaxBsonObjectSize
public Cluster(MongoClientConfiguration config, ClusterType type)
config
- The configuration for the cluster.type
- The type of the cluster.public Server add(InetSocketAddress address)
public Server add(String address)
Server
to the Cluster
for the address provided if
one does not already exist.
This method is equivalent to calling add(ServerNameUtils.parse(address))
.
public void addListener(PropertyChangeListener listener)
listener
- The listener for the state changes.public void clear()
public List<Server> findCandidateServers(ReadPreference readPreference)
ReadPreference
.readPreference
- The ReadPreference
to filter the servers.List
of servers that can be used. Servers will be
ordered by preference to be used, most preferred to least
preferred.public List<Server> findServers(Message message1, Message message2)
message1
- The first message to send.message2
- The second message to send. May be null
.public Server get(String address)
Server
does not already exist a non-writable state is created and returned.
This method is equivalent to calling add(address)
.
public List<Server> getNonWritableServers()
public List<Server> getServers()
public VersionRange getServerVersionRange()
getServerVersionRange
in interface ClusterStats
public int getSmallestMaxBatchedWriteOperations()
getSmallestMaxBatchedWriteOperations
in interface ClusterStats
public long getSmallestMaxBsonObjectSize()
getSmallestMaxBsonObjectSize
in interface ClusterStats
public ClusterType getType()
public List<Server> getWritableServers()
public void remove(Server server)
server
- The server to remove from the cluster.public void removeListener(PropertyChangeListener listener)
listener
- The listener for the state changes.protected final double[] cdf(List<Server> servers)
The latency of each server is used to create a strict ordering of servers from lowest latency to highest. The relative latency of the i'th server is then calculated based on the function:
latency[0] relative_latency[i] = ---------- latency[i]
The relative latencies are then then summed and the probability of selecting each server is then calculated by:
relative_latency[i] probability[i] = ------------------------------------------------- sum(relative_latency[0], ... relative_latency[n])
The CDF over these probabilities is returned.
servers
- The servers to compute the CDF for.protected List<Server> findCandidateServer(ReadPreference readPreference)
readPreference
- The read preference to match the server against.protected List<Server> findNearestCandidates(ReadPreference readPreference)
readPreference
- The read preference to match the server against.sort(java.util.List<com.allanbank.mongodb.client.state.Server>)
protected List<Server> findNonWritableCandidates(ReadPreference readPreference)
readPreference
- The read preference to match the server against.sort(java.util.List<com.allanbank.mongodb.client.state.Server>)
protected List<Server> findWritableCandidates(ReadPreference readPreference)
readPreference
- The read preference to match the server against.sort(java.util.List<com.allanbank.mongodb.client.state.Server>)
protected final void sort(List<Server> servers)
To distribute the requests across servers more evenly the first server is replaced with a random server based on a single sided simplified Gaussian distribution.
servers
- The servers to be sorted.cdf(List)
protected void updateVersions()
Copyright © 2011–2014 Allanbank Consulting, Inc.. All rights reserved.