belliottsmith commented on code in PR #256: URL: https://github.com/apache/cassandra-accord/pull/256#discussion_r2492061621
########## accord-core/src/main/java/accord/topology/PendingEpoch.java: ########## @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 accord.topology; + +import java.util.ArrayDeque; +import java.util.Set; +import javax.annotation.concurrent.GuardedBy; + +import accord.api.Timeouts; +import accord.coordinate.EpochTimeout; +import accord.local.Node; +import accord.primitives.Ranges; +import accord.utils.async.AsyncResult; +import accord.utils.async.AsyncResults; +import org.agrona.collections.ObjectHashSet; + +import static accord.primitives.AbstractRanges.UnionMode.MERGE_ADJACENT; +import static java.util.concurrent.TimeUnit.MICROSECONDS; + +class PendingEpoch implements Timeouts.Timeout +{ + final long epoch; + + private final TopologyManager manager; + private boolean isActive; + private final ArrayDeque<WaitingForEpoch> waiting = new ArrayDeque<>(); + private Timeouts.RegisteredTimeout timeout; + + private volatile Topology topology; + volatile int fetchAttempts; + AsyncResult<Topology> fetching; + + @GuardedBy("TopologyManager.this") + final Set<Node.Id> ready = new ObjectHashSet<>(); + @GuardedBy("TopologyManager.this") + Ranges closed = Ranges.EMPTY, retired = Ranges.EMPTY; + + public PendingEpoch(long epoch, TopologyManager manager) + { + this.epoch = epoch; + this.manager = manager; + } + + @GuardedBy("TopologyManager") + void remoteReadyToCoordinate(Node.Id id) + { + ready.add(id); + } + + @GuardedBy("TopologyManager") + Ranges closed(Ranges ranges) + { + ranges = ranges.without(closed); + closed = closed.union(MERGE_ADJACENT, ranges); + return ranges; + } + + @GuardedBy("TopologyManager") + Ranges retired(Ranges ranges) + { + ranges = ranges.without(retired); + retired = retired.union(MERGE_ADJACENT, ranges); + return ranges; + } + + Topology topology() + { + return topology; + } + + void setTopology(Topology topology) + { + this.topology = topology; + } + + // TODO (expected): pass through request deadline + AsyncResult<Void> whenActive() + { + WaitingForEpoch result, last; + synchronized (this) + { + if (isActive) + return AsyncResults.success(null); + + long timeoutMicros = manager.node.agent().expireEpochWait(MICROSECONDS); + long deadlineMicros = manager.time.elapsed(MICROSECONDS) + timeoutMicros; + result = last = waiting.peekLast(); + if (last == null || last.deadlineMicros < deadlineMicros) + waiting.add(result = new WaitingForEpoch(deadlineMicros + (timeoutMicros / 10))); + } + if (last == null) Review Comment: The safety depended on the fact that only one `whenActive` call could witness the `last = null`, and the worst possible race would be that the `RegisteredTimeout` might be overwritten by a (much) later one, which is implausible but would be innocuous (just wouldn't be cancelled). But, I found it was easier/clearer to just make the field managed by an `AtomicReference`. The reason to not perform this inside the lock is to avoid reentrancy from within `timeouts.registerAt` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

