Class PoolingOptions
- java.lang.Object
-
- com.datastax.driver.core.PoolingOptions
-
public class PoolingOptions extends Object
Options related to connection pooling.The driver uses connections in an asynchronous manner, meaning that multiple requests can be submitted on the same connection at the same time. Therefore only a relatively small number of connections is needed. For each host, the driver uses a connection pool that may have a variable size (it will automatically adjust to the current load).
With
ProtocolVersion#V2or below, there are at most 128 simultaneous requests per connection, so the pool defaults to a variable size. You will typically raise the maximum capacity by adding more connections withsetMaxConnectionsPerHost(HostDistance, int).With
ProtocolVersion#V3or above, there are up to 32768 requests per connection, and the pool defaults to a fixed size of 1. You will typically raise the maximum capacity by allowing more simultaneous requests per connection (setMaxRequestsPerConnection(HostDistance, int)).All parameters can be separately set for
LOCALandREMOTEhosts (HostDistance). ForIGNOREDhosts, no connections are created so these settings cannot be changed.
-
-
Field Summary
Fields Modifier and Type Field Description static StringCORE_POOL_LOCAL_KEYstatic StringCORE_POOL_REMOTE_KEYstatic intDEFAULT_HEARTBEAT_INTERVAL_SECONDSThe default value forgetHeartbeatIntervalSeconds()(30).static intDEFAULT_IDLE_TIMEOUT_SECONDSThe default value forgetIdleTimeoutSeconds()(120).static intDEFAULT_MAX_QUEUE_SIZEThe default value forgetMaxQueueSize()(256).static intDEFAULT_POOL_TIMEOUT_MILLISThe default value forgetPoolTimeoutMillis()(5000).static Map<ProtocolVersion,Map<String,Integer>>DEFAULTSThe default values for connection options, that depend on the native protocol version.static StringMAX_POOL_LOCAL_KEYstatic StringMAX_POOL_REMOTE_KEYstatic StringMAX_REQUESTS_PER_CONNECTION_LOCAL_KEYstatic StringMAX_REQUESTS_PER_CONNECTION_REMOTE_KEYstatic StringNEW_CONNECTION_THRESHOLD_LOCAL_KEYstatic StringNEW_CONNECTION_THRESHOLD_REMOTE_KEYstatic intUNSETThe value returned for connection options when they have not been set by the client, and the protocol version is not known yet.
-
Constructor Summary
Constructors Constructor Description PoolingOptions()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intgetCoreConnectionsPerHost(HostDistance distance)Returns the core number of connections per host.intgetHeartbeatIntervalSeconds()Returns the heart beat interval, after which a message is sent on an idle connection to make sure it's still alive.intgetIdleTimeoutSeconds()Returns the timeout before an idle connection is removed.ExecutorgetInitializationExecutor()Returns the executor to use for connection initialization.intgetMaxConnectionsPerHost(HostDistance distance)Returns the maximum number of connections per host.intgetMaxQueueSize()Returns the maximum number of requests that get enqueued if no connection is available.intgetMaxRequestsPerConnection(HostDistance distance)Returns the maximum number of requests per connection.intgetNewConnectionThreshold(HostDistance distance)Returns the threshold that triggers the creation of a new connection to a host.intgetPoolTimeoutMillis()Returns the timeout when trying to acquire a connection from a host's pool.voidrefreshConnectedHost(Host host)Requests the driver to re-evaluate theHostDistancefor a given node.voidrefreshConnectedHosts()Requests the driver to re-evaluate theHostDistance(through the configuredLoadBalancingPolicy.distance(com.datastax.driver.core.Host)) for every known hosts and to drop/add connections to each hosts according to the computed distance.PoolingOptionssetConnectionsPerHost(HostDistance distance, int core, int max)Sets the core and maximum number of connections per host in one call.PoolingOptionssetCoreConnectionsPerHost(HostDistance distance, int newCoreConnections)Sets the core number of connections per host.PoolingOptionssetHeartbeatIntervalSeconds(int heartbeatIntervalSeconds)Sets the heart beat interval, after which a message is sent on an idle connection to make sure it's still alive.PoolingOptionssetIdleTimeoutSeconds(int idleTimeoutSeconds)Sets the timeout before an idle connection is removed.PoolingOptionssetInitializationExecutor(Executor initializationExecutor)Sets the executor to use for connection initialization.PoolingOptionssetMaxConnectionsPerHost(HostDistance distance, int newMaxConnections)Sets the maximum number of connections per host.PoolingOptionssetMaxQueueSize(int maxQueueSize)Sets the maximum number of requests that get enqueued if no connection is available.PoolingOptionssetMaxRequestsPerConnection(HostDistance distance, int newMaxRequests)Sets the maximum number of requests per connection.PoolingOptionssetNewConnectionThreshold(HostDistance distance, int newValue)Sets the threshold that triggers the creation of a new connection to a host.PoolingOptionssetPoolTimeoutMillis(int poolTimeoutMillis)Sets the timeout when trying to acquire a connection from a host's pool.
-
-
-
Field Detail
-
UNSET
public static final int UNSET
The value returned for connection options when they have not been set by the client, and the protocol version is not known yet.Once a
PoolingOptionsobject is associated to aClusterand that cluster initializes, the protocol version will be detected, and connection options will take their default values for that protocol version.The methods that may return this value are:
getCoreConnectionsPerHost(HostDistance),getMaxConnectionsPerHost(HostDistance),getNewConnectionThreshold(HostDistance),getMaxRequestsPerConnection(HostDistance).- See Also:
- Constant Field Values
-
CORE_POOL_LOCAL_KEY
public static final String CORE_POOL_LOCAL_KEY
- See Also:
- Constant Field Values
-
MAX_POOL_LOCAL_KEY
public static final String MAX_POOL_LOCAL_KEY
- See Also:
- Constant Field Values
-
CORE_POOL_REMOTE_KEY
public static final String CORE_POOL_REMOTE_KEY
- See Also:
- Constant Field Values
-
MAX_POOL_REMOTE_KEY
public static final String MAX_POOL_REMOTE_KEY
- See Also:
- Constant Field Values
-
NEW_CONNECTION_THRESHOLD_LOCAL_KEY
public static final String NEW_CONNECTION_THRESHOLD_LOCAL_KEY
- See Also:
- Constant Field Values
-
NEW_CONNECTION_THRESHOLD_REMOTE_KEY
public static final String NEW_CONNECTION_THRESHOLD_REMOTE_KEY
- See Also:
- Constant Field Values
-
MAX_REQUESTS_PER_CONNECTION_LOCAL_KEY
public static final String MAX_REQUESTS_PER_CONNECTION_LOCAL_KEY
- See Also:
- Constant Field Values
-
MAX_REQUESTS_PER_CONNECTION_REMOTE_KEY
public static final String MAX_REQUESTS_PER_CONNECTION_REMOTE_KEY
- See Also:
- Constant Field Values
-
DEFAULTS
public static final Map<ProtocolVersion,Map<String,Integer>> DEFAULTS
The default values for connection options, that depend on the native protocol version.The map stores protocol versions in ascending order, and only the versions that introduced a change are present. To find the defaults for a particular version, look for the highest key that is less than or equal to that version, in other words:
Once you've extracted the underlying map, use the keysProtocolVersion referenceVersion = null; for (ProtocolVersion key : DEFAULTS.keySet()) { if (key.compareTo(actualVersion) > 0) break; else referenceVersion = key; } Map<String, Integer> defaults = DEFAULTS.get(referenceVersion);CORE_POOL_LOCAL_KEY,MAX_POOL_LOCAL_KEY,CORE_POOL_REMOTE_KEY,MAX_POOL_REMOTE_KEY,NEW_CONNECTION_THRESHOLD_LOCAL_KEY,NEW_CONNECTION_THRESHOLD_REMOTE_KEY,MAX_REQUESTS_PER_CONNECTION_LOCAL_KEYandMAX_REQUESTS_PER_CONNECTION_REMOTE_KEY.- See Also:
UNSET
-
DEFAULT_IDLE_TIMEOUT_SECONDS
public static final int DEFAULT_IDLE_TIMEOUT_SECONDS
The default value forgetIdleTimeoutSeconds()(120).- See Also:
- Constant Field Values
-
DEFAULT_POOL_TIMEOUT_MILLIS
public static final int DEFAULT_POOL_TIMEOUT_MILLIS
The default value forgetPoolTimeoutMillis()(5000).- See Also:
- Constant Field Values
-
DEFAULT_MAX_QUEUE_SIZE
public static final int DEFAULT_MAX_QUEUE_SIZE
The default value forgetMaxQueueSize()(256).- See Also:
- Constant Field Values
-
DEFAULT_HEARTBEAT_INTERVAL_SECONDS
public static final int DEFAULT_HEARTBEAT_INTERVAL_SECONDS
The default value forgetHeartbeatIntervalSeconds()(30).- See Also:
- Constant Field Values
-
-
Method Detail
-
getCoreConnectionsPerHost
public int getCoreConnectionsPerHost(HostDistance distance)
Returns the core number of connections per host.- Parameters:
distance- theHostDistancefor which to return this threshold.- Returns:
- the core number of connections per host at distance
distance.
-
setCoreConnectionsPerHost
public PoolingOptions setCoreConnectionsPerHost(HostDistance distance, int newCoreConnections)
Sets the core number of connections per host.For the provided
distance, this corresponds to the number of connections initially created and kept open to each host of that distance.The default value is:
- with
ProtocolVersion#V2or below: 2 forLOCALhosts and 1 forREMOTEhosts. - with
ProtocolVersion#V3or above: 1 for all hosts.
- Parameters:
distance- theHostDistancefor which to set this threshold.newCoreConnections- the value to set- Returns:
- this
PoolingOptions. - Throws:
IllegalArgumentException- ifdistance == HostDistance.IGNORED, or ifnewCoreConnectionsis greater than the maximum value for this distance.- See Also:
setConnectionsPerHost(HostDistance, int, int)
- with
-
getMaxConnectionsPerHost
public int getMaxConnectionsPerHost(HostDistance distance)
Returns the maximum number of connections per host.- Parameters:
distance- theHostDistancefor which to return this threshold.- Returns:
- the maximum number of connections per host at distance
distance.
-
setMaxConnectionsPerHost
public PoolingOptions setMaxConnectionsPerHost(HostDistance distance, int newMaxConnections)
Sets the maximum number of connections per host.For the provided
distance, this corresponds to the maximum number of connections that can be created per host at that distance.The default value is:
- with
ProtocolVersion#V2or below: 8 forLOCALhosts and 2 forREMOTEhosts. - with
ProtocolVersion#V3or above: 1 for all hosts.
- Parameters:
distance- theHostDistancefor which to set this threshold.newMaxConnections- the value to set- Returns:
- this
PoolingOptions. - Throws:
IllegalArgumentException- ifdistance == HostDistance.IGNORED, or ifnewMaxConnectionsis less than the core value for this distance.- See Also:
setConnectionsPerHost(HostDistance, int, int)
- with
-
setConnectionsPerHost
public PoolingOptions setConnectionsPerHost(HostDistance distance, int core, int max)
Sets the core and maximum number of connections per host in one call.This is a convenience method that is equivalent to calling
setCoreConnectionsPerHost(HostDistance, int)andsetMaxConnectionsPerHost(HostDistance, int).- Parameters:
distance- theHostDistancefor which to set these threshold.core- the core number of connections.max- the max number of connections.- Returns:
- this
PoolingOptions. - Throws:
IllegalArgumentException- ifdistance == HostDistance.IGNORED, or ifcore>max.
-
getNewConnectionThreshold
public int getNewConnectionThreshold(HostDistance distance)
Returns the threshold that triggers the creation of a new connection to a host.- Parameters:
distance- theHostDistancefor which to return this threshold.- Returns:
- the configured threshold, or the default one if none have been set.
- See Also:
setNewConnectionThreshold(HostDistance, int)
-
setNewConnectionThreshold
public PoolingOptions setNewConnectionThreshold(HostDistance distance, int newValue)
Sets the threshold that triggers the creation of a new connection to a host.A new connection gets created if:
- N connections are open
- N <
getMaxConnectionsPerHost(HostDistance) - the number of active requests is more than (N - 1) *
getMaxRequestsPerConnection(HostDistance)+getNewConnectionThreshold(HostDistance)
The default value is:
- with
ProtocolVersion#V2or below: 100 for all hosts. - with
ProtocolVersion#V3or above: 800 forLOCALhosts and 200 forREMOTEhosts.
- Parameters:
distance- theHostDistancefor which to configure this threshold.newValue- the value to set (between 0 and 128).- Returns:
- this
PoolingOptions. - Throws:
IllegalArgumentException- ifdistance == HostDistance.IGNORED, or ifmaxSimultaneousRequestsis not in range, or ifnewValueis less than the minimum value for this distance.
-
getMaxRequestsPerConnection
public int getMaxRequestsPerConnection(HostDistance distance)
Returns the maximum number of requests per connection.- Parameters:
distance- theHostDistancefor which to return this threshold.- Returns:
- the maximum number of requests per connection at distance
distance. - See Also:
setMaxRequestsPerConnection(HostDistance, int)
-
setMaxRequestsPerConnection
public PoolingOptions setMaxRequestsPerConnection(HostDistance distance, int newMaxRequests)
Sets the maximum number of requests per connection.The default value is:
- with
ProtocolVersion#V2or below: 128 for all hosts (there should not be any reason to change this). - with
ProtocolVersion#V3or above: 1024 forLOCALhosts and 256 forREMOTEhosts. These values were chosen so that the default V2 and V3 configuration generate the same load on a Cassandra cluster. Protocol V3 can go much higher (up to 32768), so if your number of clients is low, don't hesitate to experiment with higher values. If you have more than one connection per host, consider also adjustingsetNewConnectionThreshold(HostDistance, int).
- Parameters:
distance- theHostDistancefor which to set this threshold.newMaxRequests- the value to set.- Returns:
- this
PoolingOptions. - Throws:
IllegalArgumentException- ifdistance == HostDistance.IGNORED, or ifnewMaxConnectionsis not within the allowed range.
- with
-
getIdleTimeoutSeconds
public int getIdleTimeoutSeconds()
Returns the timeout before an idle connection is removed.- Returns:
- the timeout.
-
setIdleTimeoutSeconds
public PoolingOptions setIdleTimeoutSeconds(int idleTimeoutSeconds)
Sets the timeout before an idle connection is removed.The order of magnitude should be a few minutes (the default is 120 seconds). The timeout that triggers the removal has a granularity of 10 seconds.
- Parameters:
idleTimeoutSeconds- the new timeout in seconds.- Returns:
- this
PoolingOptions. - Throws:
IllegalArgumentException- if the timeout is negative.
-
getPoolTimeoutMillis
public int getPoolTimeoutMillis()
Returns the timeout when trying to acquire a connection from a host's pool.- Returns:
- the timeout.
-
setPoolTimeoutMillis
public PoolingOptions setPoolTimeoutMillis(int poolTimeoutMillis)
Sets the timeout when trying to acquire a connection from a host's pool.This option works in concert with
setMaxQueueSize(int)to determine what happens if the driver tries to borrow a connection from the pool but none is available:- if either option is set to zero, the attempt is rejected immediately;
- else if more than
maxQueueSizerequests are already waiting for a connection, the attempt is also rejected; - otherwise, the attempt is enqueued; if a connection becomes available before
poolTimeoutMillishas elapsed, then the attempt succeeds, otherwise it is rejected.
LoadBalancingPolicy.newQueryPlan(String, Statement)query plan}.The default is 5 seconds. If this option is set to zero, the driver won't wait at all.
- Parameters:
poolTimeoutMillis- the new value in milliseconds.- Returns:
- this
PoolingOptions - Throws:
IllegalArgumentException- if the timeout is negative.
-
getMaxQueueSize
public int getMaxQueueSize()
Returns the maximum number of requests that get enqueued if no connection is available.- Returns:
- the maximum queue size.
-
setMaxQueueSize
public PoolingOptions setMaxQueueSize(int maxQueueSize)
Sets the maximum number of requests that get enqueued if no connection is available.This option works in concert with
setPoolTimeoutMillis(int)to determine what happens if the driver tries to borrow a connection from the pool but none is available:- if either options is set to zero, the attempt is rejected immediately;
- else if more than
maxQueueSizerequests are already waiting for a connection, the attempt is also rejected; - otherwise, the attempt is enqueued; if a connection becomes available before
poolTimeoutMillishas elapsed, then the attempt succeeds, otherwise it is rejected.
LoadBalancingPolicy.newQueryPlan(String, Statement)query plan}.The default value is 256. If this option is set to zero, the driver will never enqueue requests.
- Parameters:
maxQueueSize- the new value.- Returns:
- this
PoolingOptions - Throws:
IllegalArgumentException- if the value is negative.
-
getHeartbeatIntervalSeconds
public int getHeartbeatIntervalSeconds()
Returns the heart beat interval, after which a message is sent on an idle connection to make sure it's still alive.- Returns:
- the interval.
-
setHeartbeatIntervalSeconds
public PoolingOptions setHeartbeatIntervalSeconds(int heartbeatIntervalSeconds)
Sets the heart beat interval, after which a message is sent on an idle connection to make sure it's still alive.This is an application-level keep-alive, provided for convenience since adjusting the TCP keep-alive might not be practical in all environments.
This option should be set higher than
SocketOptions.getReadTimeoutMillis().The default value for this option is 30 seconds.
- Parameters:
heartbeatIntervalSeconds- the new value in seconds. If set to 0, it will disable the feature.- Returns:
- this
PoolingOptions - Throws:
IllegalArgumentException- if the interval is negative.
-
getInitializationExecutor
public Executor getInitializationExecutor()
Returns the executor to use for connection initialization.- Returns:
- the executor.
- See Also:
setInitializationExecutor(java.util.concurrent.Executor)
-
setInitializationExecutor
public PoolingOptions setInitializationExecutor(Executor initializationExecutor)
Sets the executor to use for connection initialization.Connections are open in a completely asynchronous manner. Since initializing the transport requires separate CQL queries, the futures representing the completion of these queries are transformed and chained. This executor is where these transformations happen.
This is an advanced option, which should be rarely needed in practice. It defaults to Guava's
MoreExecutors.sameThreadExecutor(), which results in running the transformations on the network I/O threads; this is fine if the transformations are fast and not I/O bound (which is the case by default). One reason why you might want to provide a custom executor is if you use authentication with a customAuthenticatorimplementation that performs blocking calls.- Parameters:
initializationExecutor- the executor to use- Returns:
- this
PoolingOptions - Throws:
NullPointerException- if the executor is null
-
refreshConnectedHosts
public void refreshConnectedHosts()
Requests the driver to re-evaluate theHostDistance(through the configuredLoadBalancingPolicy.distance(com.datastax.driver.core.Host)) for every known hosts and to drop/add connections to each hosts according to the computed distance.Note that, due to backward compatibility issues, this method is not interruptible. If the caller thread gets interrupted, the method will complete and only then re-interrupt the thread (which you can check with
Thread.currentThread().isInterrupted()).
-
refreshConnectedHost
public void refreshConnectedHost(Host host)
Requests the driver to re-evaluate theHostDistancefor a given node.- Parameters:
host- the host to refresh.- See Also:
refreshConnectedHosts()
-
-