aweisberg commented on code in PR #3395: URL: https://github.com/apache/cassandra/pull/3395#discussion_r1706088293
########## src/java/org/apache/cassandra/service/consensus/migration/ConsensusMigrationMutationHelper.java: ########## @@ -0,0 +1,399 @@ +/* + * 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.consensus.migration; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.function.Predicate; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import com.google.common.collect.ImmutableList; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import accord.primitives.Keys; +import accord.primitives.Txn; +import accord.primitives.TxnId; +import org.apache.cassandra.db.ColumnFamilyStore; +import org.apache.cassandra.db.ConsistencyLevel; +import org.apache.cassandra.db.DecoratedKey; +import org.apache.cassandra.db.IMutation; +import org.apache.cassandra.db.Mutation; +import org.apache.cassandra.db.partitions.PartitionUpdate; +import org.apache.cassandra.dht.Token; +import org.apache.cassandra.exceptions.CoordinatorBehindException; +import org.apache.cassandra.exceptions.RetryOnDifferentSystemException; +import org.apache.cassandra.schema.Schema; +import org.apache.cassandra.schema.SchemaConstants; +import org.apache.cassandra.schema.TableId; +import org.apache.cassandra.schema.TableMetadata; +import org.apache.cassandra.service.StorageProxy.WriteResponseHandlerWrapper; +import org.apache.cassandra.service.accord.AccordService; +import org.apache.cassandra.service.accord.IAccordService; +import org.apache.cassandra.service.accord.api.PartitionKey; +import org.apache.cassandra.service.accord.txn.TxnCondition; +import org.apache.cassandra.service.accord.txn.TxnQuery; +import org.apache.cassandra.service.accord.txn.TxnRead; +import org.apache.cassandra.service.accord.txn.TxnReferenceOperations; +import org.apache.cassandra.service.accord.txn.TxnResult; +import org.apache.cassandra.service.accord.txn.TxnUpdate; +import org.apache.cassandra.service.accord.txn.TxnWrite; +import org.apache.cassandra.service.consensus.TransactionalMode; +import org.apache.cassandra.tcm.ClusterMetadata; +import org.apache.cassandra.tracing.Tracing; +import org.apache.cassandra.utils.Pair; +import org.apache.cassandra.utils.Throwables; +import org.apache.cassandra.utils.concurrent.Future; + +import static com.google.common.base.Preconditions.checkState; +import static java.util.function.Predicate.not; +import static org.apache.cassandra.dht.Range.isInNormalizedRanges; +import static org.apache.cassandra.metrics.ClientRequestsMetricsHolder.writeMetrics; +import static org.apache.cassandra.metrics.ClientRequestsMetricsHolder.writeMetricsForLevel; +import static org.apache.cassandra.service.StorageProxy.mutate; +import static org.apache.cassandra.service.accord.txn.TxnResult.Kind.retry_new_protocol; +import static org.apache.cassandra.service.consensus.migration.ConsensusRequestRouter.getTableMetadata; +import static org.apache.cassandra.utils.Throwables.unchecked; + +/** + * Applying mutations can fail with RetryOnDifferentSystemException if a + * mutation conflicts with a table and range that needs to be managed + * transactionally. This impacts mutations, logged/unlogged batches, hints,and blocking read repair. + * + * This class contains the logic needed for managing these retry loops and splitting the mutations up + */ +public class ConsensusMigrationMutationHelper +{ + private static final Logger logger = LoggerFactory.getLogger(ConsensusMigrationMutationHelper.class); + + private static ConsistencyLevel consistencyLevelForCommit(Collection<? extends IMutation> mutations, @Nullable ConsistencyLevel consistencyLevel) + { + // Null means no specific consistency behavior is required from Accord, it's functionally similar to ANY + // if you aren't reading the result back via Accord + if (consistencyLevel == null) + return null; + + for (IMutation mutation : mutations) + { + for (TableId tableId : mutation.getTableIds()) + { + TransactionalMode mode = Schema.instance.getTableMetadata(tableId).params.transactionalMode; + // commitCLForStrategy should return either null or the supplied consistency level + // in which case we will commit everything at that CL since Accord doesn't support per table + // commit consistency + ConsistencyLevel commitCL = mode.commitCLForStrategy(consistencyLevel); + if (commitCL != null) + return commitCL; + } + } + return null; + } + + public static <T extends IMutation> Pair<List<T>, List<T>> splitMutationsIntoAccordAndNormal(List<T> mutations) + { + ClusterMetadata cm = ClusterMetadata.current(); + List<T> accordMutations = null; + List<T> normalMutations = null; + for (int i=0,mi=mutations.size(); i<mi; i++) + { + T mutation = mutations.get(i); + Pair<T, T> accordAndNormalMutation = splitMutationIntoAccordAndNormal(mutation, cm); + T accordMutation = accordAndNormalMutation.left; + if (accordMutation != null) + { + if (accordMutations == null) + accordMutations = new ArrayList<>(Math.min(mutations.size(), 10)); Review Comment: When splitting we don't know what portion will end up in accord and non-Accord so if `mutations.size()` is larger than 10 (default empty `ArrayList` size) only allocate 10 and then let `ArrayList` resizing take care of the rest. Specifying the allocation size is to reduce it below 10 (better than just using no-arg `ArrayList`). I fussed with these loops a bunch of times trying to make them low/no allocation and then sort of gave up and just tried to make the common case (array list size 1) relatively minimal even if it isn't free. The simplest optimization is probably just to check for the single mutation routed to a single place case and return for that without allocating. I'll also add that. -- 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]

