nizhikov commented on code in PR #10314: URL: https://github.com/apache/ignite/pull/10314#discussion_r1027653903
########## modules/core/src/main/java/org/apache/ignite/internal/processors/cache/consistentcut/ConsistentCut.java: ########## @@ -0,0 +1,260 @@ +/* + * 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.ignite.internal.processors.cache.consistentcut; + +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.IgniteLogger; +import org.apache.ignite.internal.IgniteInternalFuture; +import org.apache.ignite.internal.pagemem.wal.record.ConsistentCutFinishRecord; +import org.apache.ignite.internal.pagemem.wal.record.ConsistentCutStartRecord; +import org.apache.ignite.internal.pagemem.wal.record.RolloverType; +import org.apache.ignite.internal.pagemem.wal.record.WALRecord; +import org.apache.ignite.internal.processors.cache.GridCacheSharedContext; +import org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer; +import org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx; +import org.apache.ignite.internal.processors.cache.transactions.IgniteTxManager; +import org.apache.ignite.internal.processors.cache.version.GridCacheVersion; +import org.apache.ignite.internal.util.future.GridCompoundFuture; +import org.apache.ignite.internal.util.future.GridFutureAdapter; +import org.apache.ignite.internal.util.tostring.GridToStringInclude; +import org.apache.ignite.internal.util.typedef.internal.CU; +import org.apache.ignite.internal.util.typedef.internal.U; +import org.apache.ignite.lang.IgniteUuid; +import org.jetbrains.annotations.Nullable; + +import static org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx.FinalizationStatus.RECOVERY_FINISH; +import static org.apache.ignite.transactions.TransactionState.ACTIVE; +import static org.apache.ignite.transactions.TransactionState.COMMITTED; +import static org.apache.ignite.transactions.TransactionState.MARKED_ROLLBACK; +import static org.apache.ignite.transactions.TransactionState.ROLLED_BACK; +import static org.apache.ignite.transactions.TransactionState.UNKNOWN; + +/** + * Describes current Consistent Cut. + */ +public class ConsistentCut extends GridFutureAdapter<WALPointer> { + /** */ + private final GridCacheSharedContext<?, ?> cctx; + + /** */ + private final IgniteLogger log; + + /** ID of Consistent Cut. */ + private final UUID id; + + /** Set of checked transactions belonging to the BEFORE side. */ + @GridToStringInclude + private Set<GridCacheVersion> beforeCut; + + /** Set of checked transactions belonging to the AFTER side. */ + @GridToStringInclude + private Set<GridCacheVersion> afterCut; + + /** Collection of transactions removed from {@link IgniteTxManager#activeTransactions()}. */ + private volatile Set<IgniteInternalFuture<IgniteInternalTx>> removedActive = ConcurrentHashMap.newKeySet(); + + /** */ + ConsistentCut(GridCacheSharedContext<?, ?> cctx, UUID id) { + this.cctx = cctx; + this.id = id; + + log = cctx.logger(ConsistentCut.class); + } + + /** */ + public UUID id() { + return id; + } + + /** + * Inits local Consistent Cut: prepares list of active transactions to check which side of Consistent Cut they belong to. + */ + protected void init() throws IgniteCheckedException { + walLog(new ConsistentCutStartRecord(id), false); + + beforeCut = ConcurrentHashMap.newKeySet(); + afterCut = ConcurrentHashMap.newKeySet(); + + GridCompoundFuture<Boolean, Boolean> checkFut = new GridCompoundFuture<>(CU.boolReducer()); + + Iterator<IgniteInternalFuture<IgniteInternalTx>> finFutIt = cctx.tm().activeTransactions().stream() + .filter(tx -> tx.state() != ACTIVE) + .map(IgniteInternalTx::finishFuture) + .iterator(); + + // Invoke sequentially over two iterators: + // 1. iterators are weakly consistent. + // 2. we need a guarantee to handle `removedActive` after `activeTxs` to avoid missed transactions. + checkTransactions(finFutIt, checkFut); + checkTransactions(removedActive.iterator(), checkFut); + + removedActive = null; + + checkFut.markInitialized(); + + checkFut.listen(finish -> { + if (Boolean.FALSE.equals(finish.result()) || isDone()) { + if (log.isDebugEnabled()) + log.debug("Cut might be inconsistent for id " + id + ". Skip writing FinishRecord."); + + onDone(new IgniteCheckedException("Cut is inconsistent.")); + + return; + } + + try { + WALPointer ptr = walLog(new ConsistentCutFinishRecord(id, beforeCut, afterCut), true); + + onDone(ptr); + } + catch (IgniteCheckedException e) { + U.error(log, "Failed to write ConsistentCutFinishRecord to WAL for id " + id, e); + + onDone(e); + } + }); + } + + /** + * Collects a transaction before it is removed from {@link IgniteTxManager#activeTransactions()}. + * + * @param txFinFut Transaction finish future. + */ + public void onRemoveActiveTransaction(IgniteInternalFuture<IgniteInternalTx> txFinFut) { + Set<IgniteInternalFuture<IgniteInternalTx>> txs = removedActive; + + if (txs != null) + txs.add(txFinFut); + } + + /** + * Checks active transactions - decides which side of Consistent Cut they belong to after they finished. + * + * @param activeTxFinFuts Collection of active transactions to check. + * @param checkFut Compound future that reduces finishes of checked transactions. + */ + private void checkTransactions( Review Comment: This function can be combined with the `checkFut.listen` callback from line 116. No need to continue check transaactions in case we found something errouness and can just cancel cut. -- 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]
