iamaleksey commented on code in PR #4330: URL: https://github.com/apache/cassandra/pull/4330#discussion_r2300998502
########## src/java/org/apache/cassandra/replication/OutgoingMutations.java: ########## @@ -0,0 +1,154 @@ +/* + * 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.replication; + +import org.agrona.collections.IntHashSet; +import org.apache.cassandra.db.Mutation; +import org.apache.cassandra.exceptions.RequestFailureReason; +import org.apache.cassandra.locator.InetAddressAndPort; +import org.jctools.maps.NonBlockingHashMapLong; + +/** + * Keep track of outgoing mutations so that we don't double-send any mutations + * when read reconciliation writes race with regular writes and/or each other. + * </p> + * Regular writes have a callback and need to be retried; + * Reconciliation writes have no callback, and only need to be retried if the writing node owns the log. + * </p> + * Note: this could live inside {@link CoordinatorLog}, since we cannot ever send a mutation + * that we don't have an instance of {@link CoordinatorLog} for, but for symmetry with + * {@link IncomingMutations} we put this inside {@link MutationTrackingService}. + */ +class OutgoingMutations +{ + private final NonBlockingHashMapLong<LogOutgoingMutations> log2MutationsMap = new NonBlockingHashMapLong<>(); + + void sentWriteRequest(Mutation mutation, IntHashSet toHostIds) + { + getOrCreate(mutation.id()).sentWriteRequest(mutation, toHostIds); + } + + void receivedWriteResponse(ShortMutationId mutationId, int fromHostId) + { + getOrCreate(mutationId).receivedWriteResponse(mutationId, fromHostId); + } + + void writeFailed(ShortMutationId mutationId, RequestFailureReason reason, InetAddressAndPort onHost) + { + getOrCreate(mutationId).writeFailed(mutationId, reason, onHost); + } + + private LogOutgoingMutations getOrCreate(ShortMutationId mutationId) + { + LogOutgoingMutations mutations = log2MutationsMap.get(mutationId.logId()); + if (mutations == null) + mutations = log2MutationsMap.computeIfAbsent(mutationId.logId(), ignore -> new LogOutgoingMutations()); + return mutations; + } + + private static class LogOutgoingMutations + { + // {host, offset} -> per-participant mutation state map + private final NonBlockingHashMapLong<OutgoingState> states = new NonBlockingHashMapLong<>(); + + void sentWriteRequest(Mutation mutation, IntHashSet toHostIds) + { + IntHashSet.IntIterator iterator = toHostIds.iterator(); + while (iterator.hasNext()) + { + OutgoingState state = getOrCreateState(iterator.nextValue(), mutation.id().offset()); + state.setInFlight(); + } + } + + void receivedWriteResponse(ShortMutationId mutationId, int fromHostId) + { + OutgoingState state = remove(fromHostId, mutationId.offset()); + if (state != null) + state.receivedWriteResponse(); + } + + void writeFailed(ShortMutationId mutationId, RequestFailureReason reason, InetAddressAndPort onHost) + { + // TODO check if need to implement this + } + + // TODO FIXME: proper implementation + OutgoingState getOrCreateState(int hostId, int offset) + { + long key = key(hostId, offset); + OutgoingState state = states.get(key); + if (state != null) + return state; + return states.putIfAbsent(key, new OutgoingState()); Review Comment: Yes, you are correct. I had it fixed elsewhere, but then ended up just getting the incomplete `OutgoingMutations` out of the way for now and lost the fix. -- 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