public final class CosmosLoadBalancingPolicy extends Object implements com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy
LoadBalancingPolicy implementation with an option to specify read and write datacenters to route requests.
This is the default load-balancing policy when you take a dependency on this package. It provides a good out-of-box experience for communicating with Cosmos Cassandra API instances. Its behavior is specified in configuration:
datastax-java-driver.basic.load-balancing-policy {
class = com.azure.cosmos.cassandra.CosmosLoadBalancingPolicy
preferred-regions = [<datacenter-name>[,...]]
multi-region-writes = [true|false]
}
If a list of preferred-regions is specified, nodes in those regions are prioritized for
read requests. If multi-region-writes is true, this list is used to prioritize write requests as
well. Otherwise, if multi-region-writes is false, all write requests will be routed to the primary
region. This is assumed to be the address you specify with datastax-java-driver.basic.contact-points.
| Constructor and Description |
|---|
CosmosLoadBalancingPolicy(com.datastax.oss.driver.api.core.context.DriverContext driverContext,
String profileName)
Initializes a newly created
CosmosLoadBalancingPolicy object from configuration. |
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes the current
CosmosLoadBalancingPolicy instance. |
boolean |
getMultiRegionWritesEnabled()
Returns a value of
true, if multi region writes are enabled. |
List<com.datastax.oss.driver.api.core.metadata.Node> |
getNodesForReading()
Returns a copy of the current list of nodes for reading.
|
List<com.datastax.oss.driver.api.core.metadata.Node> |
getNodesForWriting()
Returns a copy of the current list of nodes for writing.
|
List<String> |
getPreferredReadRegions()
Gets the list of preferred regions for failover on read operations.
|
List<String> |
getPreferredWriteRegions()
Gets the list of preferred regions for failover on write operations.
|
void |
init(Map<UUID,com.datastax.oss.driver.api.core.metadata.Node> nodes,
com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy.DistanceReporter distanceReporter)
Initializes the list of hosts in read, write, local, and remote categories.
|
Queue<com.datastax.oss.driver.api.core.metadata.Node> |
newQueryPlan(com.datastax.oss.driver.api.core.session.Request request,
com.datastax.oss.driver.api.core.session.Session session)
Returns an
ordered list of coordinators to use for a new query. |
void |
onAdd(com.datastax.oss.driver.api.core.metadata.Node node) |
void |
onDown(com.datastax.oss.driver.api.core.metadata.Node node) |
void |
onRemove(com.datastax.oss.driver.api.core.metadata.Node node) |
void |
onUp(com.datastax.oss.driver.api.core.metadata.Node node) |
String |
toString() |
public CosmosLoadBalancingPolicy(com.datastax.oss.driver.api.core.context.DriverContext driverContext,
String profileName)
CosmosLoadBalancingPolicy object from configuration.driverContext - an object holding the context of the current driver instance.profileName - name of the configuration profile to apply.public boolean getMultiRegionWritesEnabled()
true, if multi region writes are enabled.true, if multi region writes are enabled; false otherwise.public List<com.datastax.oss.driver.api.core.metadata.Node> getNodesForReading()
The nodes are sorted in priority order as determined by the preferred regions list. When multi-region writes are enabled this list will be exactly the same as the list of nodes for writing. A node representing the global endpoint will appear in the list. It will be the last node, if the primary region is absent from the list of preferred regions.
In the rare case of a regional outage or transient loss of connectivity, this list can be empty. This is extremely unlikely when your data is globally distributed.
public List<com.datastax.oss.driver.api.core.metadata.Node> getNodesForWriting()
The nodes are sorted in priority order as determined by the preferred regions list. When multi-region writes are enabled this list will be exactly the same as the list of nodes for reading. When multi-region writes are disabled this list will contain a single node representing the global endpoint. In the rare case of a regional outage or transient loss of connectivity, this list can be empty. This is extremely unlikely when your data is globally distributed and multi-region writes are enabled.
public List<String> getPreferredReadRegions()
When multi-region writes are enabled, this list will be the same as the one returned by getPreferredWriteRegions().
public List<String> getPreferredWriteRegions()
When multi-region writes are enabled, this list will be the same as the one returned by getPreferredReadRegions().
public void close()
CosmosLoadBalancingPolicy instance.
This method performs no action. It is a noop.
close in interface com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicyclose in interface AutoCloseablepublic void init(@NonNull
Map<UUID,com.datastax.oss.driver.api.core.metadata.Node> nodes,
@NonNull
com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy.DistanceReporter distanceReporter)
init in interface com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicynodes - the nodes to be examined.distanceReporter - an object that the policy uses to signal decisions it makes about node distances.@NonNull public Queue<com.datastax.oss.driver.api.core.metadata.Node> newQueryPlan(@Nullable com.datastax.oss.driver.api.core.session.Request request, @Nullable com.datastax.oss.driver.api.core.session.Session session)
ordered list of coordinators to use for a new query.
For read requests, the coordinators are ordered to ensure that each known host in the read datacenter is tried first. If none of these coordinators are reachable, all other hosts will be tried. For writes and all other requests, the coordinators are ordered to ensure that each known host in the write datacenter or the default write location (looked up and cached from the global-endpoint) are tried first. If none of these coordinators are reachable, all other hosts will be tried.
newQueryPlan in interface com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicyrequest - the current request or null.session - the current session or null.LoadBalancingPolicy.newQueryPlan(com.datastax.oss.driver.api.core.session.Request, com.datastax.oss.driver.api.core.session.Session) contract.public void onAdd(@NonNull
com.datastax.oss.driver.api.core.metadata.Node node)
onAdd in interface com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicypublic void onDown(@NonNull
com.datastax.oss.driver.api.core.metadata.Node node)
onDown in interface com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicypublic void onRemove(@NonNull
com.datastax.oss.driver.api.core.metadata.Node node)
onRemove in interface com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicypublic void onUp(@NonNull
com.datastax.oss.driver.api.core.metadata.Node node)
onUp in interface com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicyCopyright © 2021. All rights reserved.