public class ClusterState extends Object implements Closeable
ClusterState
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
ClusterState
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 ClusterState
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 | Field and Description |
---|---|
protected ConcurrentMap<String,ServerState> |
myServers
The complete list of servers.
|
Constructor and Description |
---|
ClusterState(MongoClientConfiguration config)
Creates a new CLusterState.
|
Modifier and Type | Method and Description |
---|---|
ServerState |
add(String address)
Adds a
ServerState to the ClusterState for the address
provided if one does not already exist. |
void |
addListener(PropertyChangeListener listener)
Adds a listener to the state.
|
protected double[] |
cdf(List<ServerState> servers)
Computes a relative CDF (cumulative distribution function) for the
servers based on the latency from the client.
|
void |
close() |
protected List<ServerState> |
findCandidateServer(ReadPreference readPreference)
Finds the candidate server, if known.
|
List<ServerState> |
findCandidateServers(ReadPreference readPreference)
Returns the set of servers that can be used based on the provided
ReadPreference . |
protected List<ServerState> |
findNearestCandidates(ReadPreference readPreference)
Returns the list of servers that match the read preference's tags.
|
protected List<ServerState> |
findNonWritableCandidates(ReadPreference readPreference)
Returns the list of non-writable servers that match the read preference's
tags.
|
protected List<ServerState> |
findWritableCandidates(ReadPreference readPreference)
Returns the list of writable servers that match the read preference's
tags.
|
ServerState |
get(String address)
Returns the server state for the address provided.
|
List<ServerState> |
getNonWritableServers()
Returns a copy of the list of non-writable servers.
|
List<ServerState> |
getServers()
Returns a copy of the list of servers.
|
List<ServerState> |
getWritableServers()
Returns a copy of the list of writable servers.
|
void |
markNotWritable(ServerState server)
Marks the server as non-writable.
|
void |
markWritable(ServerState server)
Marks the server as writable.
|
void |
removeListener(PropertyChangeListener listener)
Removes a listener to the state.
|
protected void |
sort(List<ServerState> servers)
Sorts the servers based on the latency from the client.
|
protected final ConcurrentMap<String,ServerState> myServers
public ClusterState(MongoClientConfiguration config)
config
- The configuration for the cluster.public ServerState add(String address)
ServerState
to the ClusterState
for the address
provided if one does not already exist.
This method is equivalent to calling get(String)
.
address
- The address of the ServerState
to return.ServerState
for the address.public void addListener(PropertyChangeListener listener)
listener
- The listener for the state changes.public void close()
Overridden to ensure all of the ServerState
connections are
closed.
close
in interface Closeable
close
in interface AutoCloseable
public List<ServerState> 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 ServerState get(String address)
ServerState
does not already exist a non-writable state is
created and returned.address
- The address of the ServerState
to return.ServerState
for the address.public List<ServerState> getNonWritableServers()
public List<ServerState> getServers()
public List<ServerState> getWritableServers()
public void markNotWritable(ServerState server)
PropertyChangeEvent
if
the server was previously writable.server
- The server to mark non-writable.public void markWritable(ServerState server)
PropertyChangeEvent
if the
server was previously non-writable.server
- The server to mark writable.public void removeListener(PropertyChangeListener listener)
listener
- The listener for the state changes.protected final double[] cdf(List<ServerState> 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<ServerState> findCandidateServer(ReadPreference readPreference)
readPreference
- The read preference to match the server against.protected List<ServerState> findNearestCandidates(ReadPreference readPreference)
readPreference
- The read preference to match the server against.sort(java.util.List<com.allanbank.mongodb.connection.state.ServerState>)
protected List<ServerState> findNonWritableCandidates(ReadPreference readPreference)
readPreference
- The read preference to match the server against.sort(java.util.List<com.allanbank.mongodb.connection.state.ServerState>)
protected List<ServerState> findWritableCandidates(ReadPreference readPreference)
readPreference
- The read preference to match the server against.sort(java.util.List<com.allanbank.mongodb.connection.state.ServerState>)
protected final void sort(List<ServerState> 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)
Copyright © 2011-2013 Allanbank Consulting, Inc.. All Rights Reserved.