/*
* Copyright DataStax, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datastax.oss.driver.internal.core.loadbalancing;
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
import com.datastax.oss.driver.api.core.context.DriverContext;
import com.datastax.oss.driver.api.core.metadata.Node;
import com.datastax.oss.driver.internal.core.loadbalancing.helper.MandatoryLocalDcHelper;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import net.jcip.annotations.ThreadSafe;
The default load balancing policy implementation.
To activate this policy, modify the basic.load-balancing-policy
section in the driver configuration, for example:
datastax-java-driver {
basic.load-balancing-policy {
class = DefaultLoadBalancingPolicy
local-datacenter = datacenter1
}
}
See reference.conf
(in the manual or core driver JAR) for more details. Local datacenter: This implementation requires a local datacenter to be defined, otherwise it will throw an IllegalStateException
. A local datacenter can be supplied either:
- Programmatically with
SessionBuilder#withLocalDatacenter(String)
; - Through configuration, by defining the option
basic.load-balancing-policy.local-datacenter
; - Or implicitly, if and only if no explicit contact points were provided: in this case this
implementation will infer the local datacenter from the implicit contact point (localhost).
Query plan: see BasicLoadBalancingPolicy
for details on the computation of query plans.
/**
* The default load balancing policy implementation.
*
* <p>To activate this policy, modify the {@code basic.load-balancing-policy} section in the driver
* configuration, for example:
*
* <pre>
* datastax-java-driver {
* basic.load-balancing-policy {
* class = DefaultLoadBalancingPolicy
* local-datacenter = datacenter1
* }
* }
* </pre>
*
* See {@code reference.conf} (in the manual or core driver JAR) for more details.
*
* <p><b>Local datacenter</b>: This implementation requires a local datacenter to be defined,
* otherwise it will throw an {@link IllegalStateException}. A local datacenter can be supplied
* either:
*
* <ol>
* <li>Programmatically with {@link
* com.datastax.oss.driver.api.core.session.SessionBuilder#withLocalDatacenter(String)
* SessionBuilder#withLocalDatacenter(String)};
* <li>Through configuration, by defining the option {@link
* DefaultDriverOption#LOAD_BALANCING_LOCAL_DATACENTER
* basic.load-balancing-policy.local-datacenter};
* <li>Or implicitly, if and only if no explicit contact points were provided: in this case this
* implementation will infer the local datacenter from the implicit contact point (localhost).
* </ol>
*
* <p><b>Query plan</b>: see {@link BasicLoadBalancingPolicy} for details on the computation of
* query plans.
*/
@ThreadSafe
public class DefaultLoadBalancingPolicy extends BasicLoadBalancingPolicy {
public DefaultLoadBalancingPolicy(@NonNull DriverContext context, @NonNull String profileName) {
super(context, profileName);
}
@NonNull
@Override
protected Optional<String> discoverLocalDc(@NonNull Map<UUID, Node> nodes) {
return new MandatoryLocalDcHelper(context, profile, logPrefix).discoverLocalDc(nodes);
}
}