import static java.util.Objects.requireNonNull;
-import com.google.common.annotations.VisibleForTesting;
import com.google.common.util.concurrent.FluentFuture;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.SettableFuture;
import io.netty.util.Timeout;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.VarHandle;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicBoolean;
import org.checkerframework.checker.lock.qual.GuardedBy;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
private static final Logger LOG = LoggerFactory.getLogger(ServerSessionManager.class);
private static final long DEFAULT_HOLD_STATE_NANOS = TimeUnit.MINUTES.toNanos(5);
+ private static final VarHandle CLOSED;
+
+ static {
+ try {
+ CLOSED = MethodHandles.lookup().findVarHandle(ServerSessionManager.class, "closed", boolean.class);
+ } catch (NoSuchFieldException | IllegalAccessException e) {
+ throw new ExceptionInInitializerError(e);
+ }
+ }
private final @NonNull KeyedInstanceIdentifier<Topology, TopologyKey> topology;
private final @NonNull PCEPTopologyProviderDependencies dependencies;
+ private final @NonNull GraphKey graphKey;
- @VisibleForTesting
- final AtomicBoolean isClosed = new AtomicBoolean(false);
@GuardedBy("this")
private final Map<NodeId, TopologySessionListener> nodes = new HashMap<>();
@GuardedBy("this")
private final Map<NodeId, TopologyNodeState> state = new HashMap<>();
private volatile short rpcTimeout;
+ private volatile boolean closed;
- private final GraphKey graphKey;
-
- ServerSessionManager(final KeyedInstanceIdentifier<Topology, TopologyKey> instanceIdentifier,
+ ServerSessionManager(final KeyedInstanceIdentifier<Topology, TopologyKey> topology,
final PCEPTopologyProviderDependencies dependencies, final short rpcTimeout, final GraphKey graphKey) {
this.dependencies = requireNonNull(dependencies);
- topology = requireNonNull(instanceIdentifier);
+ this.topology = requireNonNull(topology);
this.rpcTimeout = rpcTimeout;
this.graphKey = requireNonNull(graphKey);
}
@Override
public void onSuccess(final CommitInfo result) {
LOG.info("PCEP Topology {} created successfully.", topologyId());
- isClosed.set(false);
+ closed = false;
future.set(Boolean.TRUE);
}
@Override
public void onFailure(final Throwable failure) {
LOG.error("Failed to create PCEP Topology {}.", topologyId(), failure);
- isClosed.set(true);
+ closed = true;
future.set(Boolean.FALSE);
}
}, MoreExecutors.directExecutor());
return future;
}
+ final boolean isClosed() {
+ return closed;
+ }
+
final synchronized FluentFuture<? extends CommitInfo> stop() {
- if (isClosed.getAndSet(true)) {
+ if (!CLOSED.compareAndSet(this, false, true)) {
LOG.error("Session Manager has already been closed.");
return CommitInfo.emptyFluentFuture();
}
final synchronized void releaseNodeState(final TopologyNodeState nodeState, final InetAddress peerAddress,
final boolean persistNode) {
- if (isClosed.get()) {
+ if (isClosed()) {
LOG.error("Session Manager has already been closed.");
return;
}
final synchronized TopologyNodeState takeNodeState(final InetAddress address,
final TopologySessionListener sessionListener, final boolean retrieveNode) {
final NodeId id = createNodeId(address);
- if (isClosed.get()) {
+ if (isClosed()) {
LOG.error("Server Session Manager is closed. Unable to create topology node {} with listener {}", id,
sessionListener);
return null;