package com.datastax.oss.driver.internal.core.metadata;
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
import com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy;
import com.datastax.oss.driver.api.core.loadbalancing.NodeDistance;
import com.datastax.oss.driver.api.core.metadata.Metadata;
import com.datastax.oss.driver.api.core.metadata.Node;
import com.datastax.oss.driver.api.core.metadata.NodeState;
import com.datastax.oss.driver.api.core.session.Request;
import com.datastax.oss.driver.api.core.session.Session;
import com.datastax.oss.driver.internal.core.context.InternalDriverContext;
import com.datastax.oss.driver.internal.core.util.concurrent.ReplayingEventFilter;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import net.jcip.annotations.GuardedBy;
import net.jcip.annotations.ThreadSafe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ThreadSafe
public class LoadBalancingPolicyWrapper implements AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(LoadBalancingPolicyWrapper.class);
private enum State {
BEFORE_INIT,
DURING_INIT,
RUNNING,
CLOSING
}
private final InternalDriverContext context;
private final Set<LoadBalancingPolicy> policies;
private final Map<String, LoadBalancingPolicy> policiesPerProfile;
private final Map<LoadBalancingPolicy, SinglePolicyDistanceReporter> reporters;
private final Lock distancesLock = new ReentrantLock();
@GuardedBy("distancesLock")
private final Map<Node, Map<LoadBalancingPolicy, NodeDistance>> distances;
private final String logPrefix;
private final ReplayingEventFilter<NodeStateEvent> eventFilter =
new ReplayingEventFilter<>(this::processNodeStateEvent);
private final AtomicReference<State> stateRef = new AtomicReference<>(State.BEFORE_INIT);
public LoadBalancingPolicyWrapper(
@NonNull InternalDriverContext context,
@NonNull Map<String, LoadBalancingPolicy> policiesPerProfile) {
this.context = context;
this.policiesPerProfile = policiesPerProfile;
ImmutableMap.Builder<LoadBalancingPolicy, SinglePolicyDistanceReporter> reportersBuilder =
ImmutableMap.builder();
for (LoadBalancingPolicy policy : ImmutableSet.copyOf(policiesPerProfile.values())) {
reportersBuilder.put(policy, new SinglePolicyDistanceReporter(policy));
}
this.reporters = reportersBuilder.build();
this.policies = reporters.keySet();
this.distances = new HashMap<>();
this.logPrefix = context.getSessionName();
context.getEventBus().register(NodeStateEvent.class, this::onNodeStateEvent);
}
public void init() {
if (stateRef.compareAndSet(State.BEFORE_INIT, State.DURING_INIT)) {
LOG.debug("[{}] Initializing policies", logPrefix);
eventFilter.start();
MetadataManager metadataManager = context.getMetadataManager();
Metadata metadata = metadataManager.getMetadata();
for (LoadBalancingPolicy policy : policies) {
policy.init(metadata.getNodes(), reporters.get(policy));
}
if (stateRef.compareAndSet(State.DURING_INIT, State.RUNNING)) {
eventFilter.markReady();
} else {
assert stateRef.get() == State.CLOSING;
for (LoadBalancingPolicy policy : policies) {
policy.close();
}
}
}
}
@NonNull
public Queue<Node> newQueryPlan(
@Nullable Request request, @NonNull String executionProfileName, @Nullable Session session) {
switch (stateRef.get()) {
case BEFORE_INIT:
case DURING_INIT:
List<Node> nodes = new ArrayList<>(context.getMetadataManager().getContactPoints());
Collections.shuffle(nodes);
return new ConcurrentLinkedQueue<>(nodes);
case RUNNING:
LoadBalancingPolicy policy = policiesPerProfile.get(executionProfileName);
if (policy == null) {
policy = policiesPerProfile.get(DriverExecutionProfile.DEFAULT_NAME);
}
return policy.newQueryPlan(request, session);
default:
return new ConcurrentLinkedQueue<>();
}
}
@NonNull
public Queue<Node> newQueryPlan() {
return newQueryPlan(null, DriverExecutionProfile.DEFAULT_NAME, null);
}
private void onNodeStateEvent(NodeStateEvent event) {
eventFilter.accept(event);
}
private void processNodeStateEvent(NodeStateEvent event) {
switch (stateRef.get()) {
case BEFORE_INIT:
case DURING_INIT:
throw new AssertionError("Filter should not be marked ready until LBP init");
case CLOSING:
return;
case RUNNING:
for (LoadBalancingPolicy policy : policies) {
if (event.newState == NodeState.UP) {
policy.onUp(event.node);
} else if (event.newState == NodeState.DOWN || event.newState == NodeState.FORCED_DOWN) {
policy.onDown(event.node);
} else if (event.newState == NodeState.UNKNOWN) {
policy.onAdd(event.node);
} else if (event.newState == null) {
policy.onRemove(event.node);
} else {
LOG.warn("[{}] Unsupported event: {}", logPrefix, event);
}
}
break;
}
}
@Override
public void close() {
State old;
while (true) {
old = stateRef.get();
if (old == State.CLOSING) {
return;
} else if (stateRef.compareAndSet(old, State.CLOSING)) {
break;
}
}
if (old == State.RUNNING) {
for (LoadBalancingPolicy policy : policies) {
policy.close();
}
}
}
private class SinglePolicyDistanceReporter implements LoadBalancingPolicy.DistanceReporter {
private final LoadBalancingPolicy policy;
private SinglePolicyDistanceReporter(LoadBalancingPolicy policy) {
this.policy = policy;
}
@Override
public void setDistance(@NonNull Node node, @NonNull NodeDistance suggestedDistance) {
LOG.debug(
"[{}] {} suggested {} to {}, checking what other policies said",
logPrefix,
policy,
node,
suggestedDistance);
distancesLock.lock();
try {
Map<LoadBalancingPolicy, NodeDistance> distancesForNode =
distances.computeIfAbsent(node, (n) -> new HashMap<>());
distancesForNode.put(policy, suggestedDistance);
NodeDistance newDistance = aggregate(distancesForNode);
LOG.debug("[{}] Shortest distance across all policies is {}", logPrefix, newDistance);
NodeDistance oldDistance = node.getDistance();
if (!oldDistance.equals(newDistance)) {
LOG.debug("[{}] {} was {}, changing to {}", logPrefix, node, oldDistance, newDistance);
DefaultNode defaultNode = (DefaultNode) node;
defaultNode.distance = newDistance;
context.getEventBus().fire(new DistanceEvent(newDistance, defaultNode));
} else {
LOG.debug("[{}] {} was already {}, ignoring", logPrefix, node, oldDistance);
}
} finally {
distancesLock.unlock();
}
}
private NodeDistance aggregate(Map<LoadBalancingPolicy, NodeDistance> distances) {
NodeDistance minimum = NodeDistance.IGNORED;
for (NodeDistance candidate : distances.values()) {
if (candidate.compareTo(minimum) < 0) {
minimum = candidate;
}
}
return minimum;
}
}
}