package com.datastax.oss.driver.internal.core.util.concurrent;
import com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting;
import com.datastax.oss.driver.shaded.guava.common.graph.Graphs;
import com.datastax.oss.driver.shaded.guava.common.graph.MutableValueGraph;
import com.datastax.oss.driver.shaded.guava.common.graph.ValueGraphBuilder;
import net.jcip.annotations.ThreadSafe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ThreadSafe
public class CycleDetector {
private static final boolean ENABLED =
Boolean.getBoolean("com.datastax.oss.driver.DETECT_CYCLES");
private static final Logger LOG = LoggerFactory.getLogger(CycleDetector.class);
private final String errorMessage;
private final boolean enabled;
private final MutableValueGraph<String, String> graph;
public CycleDetector(String errorMessage) {
this(errorMessage, ENABLED);
}
@VisibleForTesting
CycleDetector(String errorMessage, boolean enabled) {
this.errorMessage = errorMessage;
this.enabled = enabled;
this.graph = enabled ? ValueGraphBuilder.directed().build() : null;
}
void onTryLock(LazyReference<?> reference) {
if (enabled) {
synchronized (this) {
Thread me = Thread.currentThread();
LOG.debug("{} wants to initialize {}", me, reference.getName());
graph.putEdgeValue(me.getName(), reference.getName(), "wants to initialize");
LOG.debug("{}", graph);
if (Graphs.hasCycle(graph.asGraph())) {
throw new IllegalStateException(errorMessage + " " + graph);
}
}
}
}
void onLockAcquired(LazyReference<?> reference) {
if (enabled) {
synchronized (this) {
Thread me = Thread.currentThread();
LOG.debug("{} is initializing {}", me, reference.getName());
String old = graph.removeEdge(me.getName(), reference.getName());
assert "wants to initialize".equals(old);
graph.putEdgeValue(reference.getName(), me.getName(), "is getting initialized by");
}
}
}
void onReleaseLock(LazyReference<?> reference) {
if (enabled) {
synchronized (this) {
Thread me = Thread.currentThread();
LOG.debug("{} is done initializing {}", me, reference.getName());
graph.removeEdge(reference.getName(), me.getName());
}
}
}
}