nizhikov commented on code in PR #10524:
URL: https://github.com/apache/ignite/pull/10524#discussion_r1159772857


##########
modules/core/src/main/java/org/apache/ignite/internal/visor/cdc/VisorCdcCacheDataResendTask.java:
##########
@@ -0,0 +1,231 @@
+/*
+ * 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.visor.cdc;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.UUID;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.compute.ComputeJobResult;
+import org.apache.ignite.internal.pagemem.wal.IgniteWriteAheadLogManager;
+import org.apache.ignite.internal.pagemem.wal.record.CdcDataRecord;
+import org.apache.ignite.internal.pagemem.wal.record.DataEntry;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.processors.cache.GridCacheOperation;
+import 
org.apache.ignite.internal.processors.cache.GridCachePartitionExchangeManager;
+import org.apache.ignite.internal.processors.cache.IgniteInternalCache;
+import org.apache.ignite.internal.processors.cache.KeyCacheObject;
+import 
org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture;
+import org.apache.ignite.internal.processors.cache.persistence.CacheDataRow;
+import org.apache.ignite.internal.processors.task.GridInternal;
+import org.apache.ignite.internal.util.lang.GridIterator;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.visor.VisorJob;
+import org.apache.ignite.internal.visor.VisorMultiNodeTask;
+import org.apache.ignite.internal.visor.VisorTaskArgument;
+import org.apache.ignite.resources.LoggerResource;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Task to forcefully resend all cache data to CDC.
+ * Iterates over caches and writes data records to the WAL to get captured by 
CDC.
+ */
+@GridInternal
+public class VisorCdcCacheDataResendTask extends 
VisorMultiNodeTask<VisorCdcCacheDataResendTaskArg, Void, Void> {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** Topology version when task was started. */
+    private AffinityTopologyVersion topVer;
+
+    /** {@inheritDoc} */
+    @Override protected VisorJob<VisorCdcCacheDataResendTaskArg, Void> 
job(VisorCdcCacheDataResendTaskArg arg) {
+        return new VisorCdcCacheDataResendJob(arg, topVer);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected Collection<UUID> 
jobNodes(VisorTaskArgument<VisorCdcCacheDataResendTaskArg> arg) {
+        // Check there is no rebalance.
+        GridDhtPartitionsExchangeFuture fut = 
ignite.context().cache().context().exchange().lastFinishedFuture();
+
+        if (!fut.rebalanced()) {
+            throw new IgniteException("CDC cache data resend cancelled. 
Rebalance sheduled " +
+                "[topVer=" + fut.topologyVersion() + ']');
+        }
+
+        // Cancel resend if affinity will change.
+        topVer = 
ignite.context().cache().context().exchange().lastAffinityChangedTopologyVersion(fut.topologyVersion());
+
+        return F.nodeIds(ignite.cluster().forServers().nodes());
+    }
+
+    /** {@inheritDoc} */
+    @Override protected @Nullable Void reduce0(List<ComputeJobResult> results) 
throws IgniteException {
+        for (ComputeJobResult res : results) {
+            if (res.getException() != null) {
+                throw new IgniteException("CDC cache data resend cancelled. 
Failed to resend cache data " +
+                    "on the node [nodeId=" + res.getNode().id() + ']', 
res.getException());
+            }
+        }
+
+        return null;
+    }
+
+    /** */
+    private static class VisorCdcCacheDataResendJob extends 
VisorJob<VisorCdcCacheDataResendTaskArg, Void> {
+        /** */
+        private static final long serialVersionUID = 0L;
+
+        /** Injected logger. */
+        @LoggerResource
+        protected IgniteLogger log;
+
+        /** */
+        private IgniteWriteAheadLogManager wal;
+
+        /** Topology version when task was started. */
+        private final AffinityTopologyVersion topVer;
+
+        /**
+         * @param arg Job argument.
+         * @param topVer Topology version when task was started.
+         */
+        protected VisorCdcCacheDataResendJob(VisorCdcCacheDataResendTaskArg 
arg, AffinityTopologyVersion topVer) {
+            super(arg, false);
+
+            this.topVer = topVer;
+        }
+
+        /** {@inheritDoc} */
+        @Override protected Void run(VisorCdcCacheDataResendTaskArg arg) 
throws IgniteException {
+            if (F.isEmpty(arg.caches()))
+                throw new IllegalArgumentException("Caches are not 
specified.");
+
+            List<IgniteInternalCache<?, ?>> caches = new ArrayList<>();
+
+            for (String name : arg.caches()) {
+                IgniteInternalCache<?, ?> cache = 
ignite.context().cache().cache(name);
+
+                if (cache == null)
+                    throw new IgniteException("Cache does not exist 
[cacheName=" + name + ']');
+
+                if (!cache.context().dataRegion().config().isCdcEnabled()) {
+                    throw new IgniteException("CDC is not enabled for given 
cache [cacheName=" + name +
+                        ", dataRegionName=" + 
cache.context().dataRegion().config().getName() + ']');
+                }
+
+                caches.add(cache);
+            }
+
+            if (log.isInfoEnabled())
+                log.info("CDC cache data resend started [caches=" + 
String.join(", ", arg.caches()) + ']');
+
+            wal = ignite.context().cache().context().wal(true);
+
+            try {
+                Iterator<IgniteInternalCache<?, ?>> iter = caches.iterator();
+
+                while (iter.hasNext() && !isCancelled())
+                    resendCacheData(iter.next());
+
+                wal.flush(null, true);
+
+                if (log.isInfoEnabled()) {
+                    log.info("CDC cache data resend " + (isCancelled() ? 
"cancelled" : "finished") +
+                        " [caches=" + String.join(", ", arg.caches()) + ']');
+                }
+            }
+            catch (IgniteCheckedException e) {
+                throw new IgniteException(e);
+            }
+
+            return null;
+        }
+
+        /** @param cache Cache. */
+        private void resendCacheData(IgniteInternalCache<?, ?> cache) throws 
IgniteCheckedException {
+            if (log.isInfoEnabled())
+                log.info("CDC cache data resend started [cacheName=" + 
cache.name() + ']');
+
+            GridCacheContext<?, ?> cctx = cache.context();
+            GridCachePartitionExchangeManager<Object, Object> exchange = 
ignite.context().cache().context().exchange();
+
+            GridIterator<CacheDataRow> localRows = cctx.offheap()
+                .cacheIterator(cctx.cacheId(), true, false, 
AffinityTopologyVersion.NONE, null, null);
+
+            long cnt = 0;
+            Set<Integer> parts = new TreeSet<>();
+            GridDhtPartitionsExchangeFuture lastFut = null;
+
+            for (CacheDataRow row : localRows) {
+                if (isCancelled())
+                    break;
+
+                GridDhtPartitionsExchangeFuture fut = 
exchange.lastFinishedFuture();

Review Comment:
   let's extract check from 186 line to 197 to method 
`ensureTopologyNotChanged()`



-- 
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]

Reply via email to