iamaleksey commented on code in PR #4119: URL: https://github.com/apache/cassandra/pull/4119#discussion_r2086884561
########## src/java/org/apache/cassandra/service/reads/tracked/TrackedRead.java: ########## @@ -0,0 +1,633 @@ +/* + * 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 org.apache.cassandra.service.reads.tracked; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Iterables; +import com.google.common.collect.Sets; + +import org.apache.cassandra.concurrent.Stage; +import org.apache.cassandra.db.*; +import org.apache.cassandra.db.partitions.AbstractPartitionIterator; +import org.apache.cassandra.db.partitions.PartitionIterator; +import org.apache.cassandra.db.rows.RowIterator; +import org.apache.cassandra.exceptions.ReadFailureException; +import org.apache.cassandra.exceptions.ReadTimeoutException; +import org.apache.cassandra.exceptions.RequestFailureReason; +import org.apache.cassandra.io.IVersionedSerializer; +import org.apache.cassandra.io.util.DataInputPlus; +import org.apache.cassandra.io.util.DataOutputPlus; +import org.apache.cassandra.locator.*; +import org.apache.cassandra.net.*; +import org.apache.cassandra.replication.MutationSummary; +import org.apache.cassandra.replication.MutationTrackingService; +import org.apache.cassandra.service.reads.SpeculativeRetryPolicy; +import org.apache.cassandra.tcm.ClusterMetadata; +import org.apache.cassandra.transport.Dispatcher; +import org.apache.cassandra.utils.Clock; +import org.apache.cassandra.utils.CollectionSerializer; +import org.apache.cassandra.utils.FBUtilities; +import org.apache.cassandra.utils.concurrent.AsyncPromise; +import org.apache.cassandra.utils.concurrent.Future; +import org.apache.cassandra.utils.concurrent.UncheckedInterruptedException; + +import java.io.IOException; +import java.util.Collections; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicLong; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.apache.cassandra.locator.InetAddressAndPort.Serializer.inetAddressAndPortSerializer; +import static org.apache.cassandra.metrics.ClientRequestsMetricsHolder.readMetrics; + +public abstract class TrackedRead<E extends Endpoints<E>, P extends ReplicaPlan.ForRead<E, P>> implements RequestCallback<TrackedDataResponse> +{ + private static final Logger logger = LoggerFactory.getLogger(TrackedRead.class); + + public static class Id + { + private static final int nodeId = ClusterMetadata.current().myNodeId().id(); + private static final AtomicLong lastHlc = new AtomicLong(); + + private final int node; + private final long hlc; + + public Id(int node, long hlc) + { + this.node = node; + this.hlc = hlc; + } + + @Override + public boolean equals(Object o) + { + if (o == null || getClass() != o.getClass()) return false; + Id id = (Id) o; + return node == id.node && hlc == id.hlc; + } + + @Override + public int hashCode() + { + return Integer.hashCode(node) * 31 + Long.hashCode(hlc); + } + + @Override + public String toString() + { + return "Id{" + node + ':' + hlc + '}'; + } + + public static final IVersionedSerializer<Id> serializer = new IVersionedSerializer<Id>() + { + @Override + public void serialize(Id id, DataOutputPlus out, int version) throws IOException + { + out.writeInt(id.node); + out.writeLong(id.hlc); + } + + @Override + public Id deserialize(DataInputPlus in, int version) throws IOException + { + int node = in.readInt(); + long hlc = in.readLong(); + return new Id(node, hlc); + } + + @Override + public long serializedSize(Id id, int version) + { + return TypeSizes.sizeof(id.node) + TypeSizes.sizeof(id.hlc); + } + }; + + public static Id nextId() + { + while (true) + { + long lastMicros = lastHlc.get(); + long nextMicros = Math.max(lastMicros + 1, TimeUnit.MILLISECONDS.toMicros(Clock.Global.currentTimeMillis())); + if (lastHlc.compareAndSet(lastMicros, nextMicros)) + return new Id(nodeId, nextMicros); + } + } + } + + private final AsyncPromise<PartitionIterator> future = new AsyncPromise<>(); + + private final Id readId = Id.nextId(); + private final ReplicaPlan.AbstractForRead<E, P> replicaPlan; + private final ConsistencyLevel consistencyLevel; + + private static class RequestFailure extends Throwable + { + private final InetAddressAndPort from; + private final RequestFailureReason reason; + + public RequestFailure(InetAddressAndPort from, RequestFailureReason reason) + { + this.from = from; + this.reason = reason; + } + + public Map<InetAddressAndPort, RequestFailureReason> reasonByEndpoint() + { + return Map.of(from, reason); + } + } + + public TrackedRead(ReplicaPlan.AbstractForRead<E, P> replicaPlan, ConsistencyLevel consistencyLevel) + { + this.replicaPlan = replicaPlan; + this.consistencyLevel = consistencyLevel; + } + + @Override + public String toString() + { + return "TrackedRead." + getClass().getSimpleName() + '{' + readId + '}'; + } + + protected abstract ReadCommand command(); Review Comment: Could just be a field, as both classes currently duplicate the implementation? -- 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: pr-unsubscr...@cassandra.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org For additional commands, e-mail: pr-h...@cassandra.apache.org