public class ShardedConnectionFactory extends Object implements ConnectionFactory
Modifier and Type | Class and Description |
---|---|
protected static class |
ShardedConnectionFactory.BootstrapState
BootstrapState provides the ability to track the state of the bootstrap
for the sharded cluster.
|
Modifier and Type | Field and Description |
---|---|
protected static Log |
LOG
The logger for the
ShardedConnectionFactory . |
protected Cluster |
myCluster
The state of the cluster.
|
protected MongoClientConfiguration |
myConfig
The MongoDB client configuration.
|
protected ProxiedConnectionFactory |
myConnectionFactory
The factory to create proxied connections.
|
protected ClusterPinger |
myPinger
Pings the servers in the cluster collecting latency and tags.
|
protected ServerSelector |
mySelector
The selector for the mongos instance to use.
|
Constructor and Description |
---|
ShardedConnectionFactory(ProxiedConnectionFactory factory,
MongoClientConfiguration config)
Creates a new
ShardedConnectionFactory . |
Modifier and Type | Method and Description |
---|---|
void |
bootstrap()
Finds the mongos servers.
|
void |
close() |
Connection |
connect()
Creates a new connection to the shared mongos servers.
|
protected ShardedConnectionFactory.BootstrapState |
createBootstrapState()
Creates a new
ShardedConnectionFactory.BootstrapState . |
protected Cluster |
createCluster(MongoClientConfiguration config)
Creates a
Cluster object to track the state of the servers across
the cluster. |
protected ClusterPinger |
createClusterPinger(ProxiedConnectionFactory factory,
MongoClientConfiguration config)
Creates a
ClusterPinger object to periodically update the status
of the servers. |
protected ServerSelector |
createSelector()
Creates a
ServerSelector object to select the (presumed) optimal
server to handle a request. |
protected boolean |
findMongosServers(Connection conn)
Performs a find on the config database's mongos
collection to return the id for all of the mongos servers in the cluster.
|
protected Cluster |
getCluster()
Returns the clusterState value.
|
ClusterStats |
getClusterStats()
Returns the meta-data on the current cluster.
|
ClusterType |
getClusterType()
Returns the type of cluster the connection factory connects to.
|
ReconnectStrategy |
getReconnectStrategy()
Returns the reconnection strategy for the type of connections.
|
protected void |
update(ShardedConnectionFactory.BootstrapState state,
Connection conn)
Queries for the addresses for the
mongos servers via the
findMongosServers(Connection) method. |
protected Connection |
wrap(Connection primaryConn,
Server server)
Wraps the connection in a shard-aware connection.
|
protected static final Log LOG
ShardedConnectionFactory
.protected final Cluster myCluster
protected final MongoClientConfiguration myConfig
protected final ProxiedConnectionFactory myConnectionFactory
protected final ClusterPinger myPinger
protected final ServerSelector mySelector
public ShardedConnectionFactory(ProxiedConnectionFactory factory, MongoClientConfiguration config)
ShardedConnectionFactory
.factory
- The factory to create proxied connections.config
- The initial configuration.public void bootstrap()
public void close()
Overridden to close the cluster state and the
ProxiedConnectionFactory
.
close
in interface Closeable
close
in interface AutoCloseable
public Connection connect() throws IOException
connect
in interface ConnectionFactory
IOException
- On a failure connecting to the server.ConnectionFactory.connect()
public ClusterStats getClusterStats()
Overridden to return the Cluster
.
getClusterStats
in interface ConnectionFactory
public ClusterType getClusterType()
Overridden to return ClusterType.SHARDED
cluster type.
getClusterType
in interface ConnectionFactory
public ReconnectStrategy getReconnectStrategy()
Overridden to return the delegates strategy but replace his state and selector with our own.
getReconnectStrategy
in interface ConnectionFactory
protected ShardedConnectionFactory.BootstrapState createBootstrapState()
ShardedConnectionFactory.BootstrapState
.ShardedConnectionFactory.BootstrapState
to track state of loading the cluster
information.protected Cluster createCluster(MongoClientConfiguration config)
Cluster
object to track the state of the servers across
the cluster.config
- The configuration for the cluster.Cluster
to track the servers across the cluster.protected ClusterPinger createClusterPinger(ProxiedConnectionFactory factory, MongoClientConfiguration config)
ClusterPinger
object to periodically update the status
of the servers.factory
- The factory for creating the connections to the servers.config
- The configuration for the client.ClusterPinger
object to periodically update the
status of the servers.protected ServerSelector createSelector()
ServerSelector
object to select the (presumed) optimal
server to handle a request.
For a sharded cluster this defaults to the LatencyServerSelector
.
ServerSelector
object to select the (presumed)
optimal server to handle a request.protected boolean findMongosServers(Connection conn) throws InterruptedException, ExecutionException
A single mongos entry looks like:
{ "_id" : "mongos.example.com:27017", "ping" : ISODate("2011-12-05T23:54:03.122Z"), "up" : 330 }
conn
- The connection to request from.ExecutionException
- On a failure to recover the response from the server.InterruptedException
- On a failure to receive a response from the server.protected Cluster getCluster()
protected void update(ShardedConnectionFactory.BootstrapState state, Connection conn) throws InterruptedException, ExecutionException
mongos
servers via the
findMongosServers(Connection)
method.state
- The state of the bootstrap to be updated.conn
- The connection to use to locate the mongos
serversInterruptedException
- On a failure to wait for the reply to the query due to the
thread being interrupted.ExecutionException
- On a failure to execute the query.protected Connection wrap(Connection primaryConn, Server server)
primaryConn
- The primary shard connection.server
- The server the connection is connected to.Copyright © 2011–2014 Allanbank Consulting, Inc.. All rights reserved.