You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2021/06/01 15:02:47 UTC

[GitHub] [ignite] anton-vinogradov commented on a change in pull request #8909: IGNITE-13581 Capture Data Change implementation

anton-vinogradov commented on a change in pull request #8909:
URL: https://github.com/apache/ignite/pull/8909#discussion_r643190241



##########
File path: modules/core/src/main/java/org/apache/ignite/internal/cdc/ChangeDataCapture.java
##########
@@ -0,0 +1,545 @@
+/*
+ * 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.cdc;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.Serializable;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.stream.Stream;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cdc.ChangeDataCaptureConfiguration;
+import org.apache.ignite.cdc.ChangeDataCaptureConsumer;
+import org.apache.ignite.cdc.ChangeDataCaptureEvent;
+import org.apache.ignite.configuration.DataStorageConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteInterruptedCheckedException;
+import org.apache.ignite.internal.IgnitionEx;
+import org.apache.ignite.internal.MarshallerContextImpl;
+import org.apache.ignite.internal.pagemem.wal.WALIterator;
+import org.apache.ignite.internal.processors.cache.binary.CacheObjectBinaryProcessorImpl;
+import org.apache.ignite.internal.processors.cache.persistence.filename.PdsFolderResolver;
+import org.apache.ignite.internal.processors.cache.persistence.filename.PdsFolderSettings;
+import org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer;
+import org.apache.ignite.internal.processors.cache.persistence.wal.reader.IgniteWalIteratorFactory;
+import org.apache.ignite.internal.processors.resource.GridResourceIoc;
+import org.apache.ignite.internal.processors.resource.GridResourceLoggerInjector;
+import org.apache.ignite.internal.processors.resource.GridSpringResourceContext;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.CU;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.lang.IgniteBiTuple;
+import org.apache.ignite.resources.LoggerResource;
+import org.apache.ignite.resources.SpringApplicationContextResource;
+import org.apache.ignite.resources.SpringResource;
+import org.apache.ignite.startup.cmdline.ChangeDataCaptureCommandLineStartup;
+
+import static org.apache.ignite.internal.IgniteKernal.NL;
+import static org.apache.ignite.internal.IgniteKernal.SITE;
+import static org.apache.ignite.internal.IgniteVersionUtils.ACK_VER_STR;
+import static org.apache.ignite.internal.IgniteVersionUtils.COPYRIGHT;
+import static org.apache.ignite.internal.pagemem.wal.record.WALRecord.RecordType.DATA_RECORD_V2;
+import static org.apache.ignite.internal.processors.cache.persistence.wal.FileWriteAheadLogManager.WAL_SEGMENT_FILE_FILTER;
+
+/**
+ * Change Data Capture(CDC) application.
+ * Application run independently of Ignite node process and provide ability for the {@link ChangeDataCaptureConsumer}
+ * to consume events({@link ChangeDataCaptureEvent}) from WAL segments.
+ * User should responsible {@link ChangeDataCaptureConsumer} implementation with custom consumption logic.
+ *
+ * Ignite node should be explicitly configured for using {@link ChangeDataCapture}.
+ * <ol>
+ *     <li>Set {@link DataStorageConfiguration#setChangeDataCaptureEnabled(boolean)} to true.</li>
+ *     <li>Optional: Set {@link DataStorageConfiguration#setChangeDataCaptureWalPath(String)} to path to the directory
+ *     to store WAL segments for CDC.</li>
+ *     <li>Optional: Set {@link DataStorageConfiguration#setWalForceArchiveTimeout(long)} to configure timeout for
+ *     force WAL rollover, so new events will be available for consumptions with the predicted time.</li>
+ * </ol>
+ *
+ * When {@link DataStorageConfiguration#getChangeDataCaptureWalPath()} is true then Ignite node on each WAL segment
+ * rollover creates hard link to archive WAL segment in
+ * {@link DataStorageConfiguration#getChangeDataCaptureWalPath()} directory. {@link ChangeDataCapture} application takes
+ * segment file and consumes events from it.
+ * After successful consumption (see {@link ChangeDataCaptureConsumer#onEvents(Iterator)}) WAL segment will be deleted
+ * from directory.
+ *
+ * Several Ignite nodes can be started on the same host.
+ * If your deployment done with custom consistent id then you should specify it via
+ * {@link IgniteConfiguration#setConsistentId(Serializable)} in provided {@link IgniteConfiguration}.
+ *
+ * Application works as follows:
+ * <ol>
+ *     <li>Search node work directory based on provided {@link IgniteConfiguration}.</li>
+ *     <li>Await for creation of CDC directory if it not exists.</li>
+ *     <li>Acquire file lock to ensure exclusive consumption.</li>
+ *     <li>Loads state of consumption if it exists.</li>
+ *     <li>Infinetely wait for new available segment and process it.</li>
+ * </ol>
+ *
+ * @see DataStorageConfiguration#setChangeDataCaptureEnabled(boolean)
+ * @see DataStorageConfiguration#setChangeDataCaptureWalPath(String)
+ * @see DataStorageConfiguration#setWalForceArchiveTimeout(long)
+ * @see ChangeDataCaptureCommandLineStartup
+ * @see ChangeDataCaptureConsumer
+ * @see DataStorageConfiguration#DFLT_WAL_CDC_PATH
+ */
+public class ChangeDataCapture implements Runnable {
+    /** */
+    public static final String ERR_MSG = "Persistence disabled. Capture Data Change can't run!";
+
+    /** State dir. */
+    public static final String STATE_DIR = "state";
+
+    /** Ignite configuration. */
+    private final IgniteConfiguration igniteCfg;
+
+    /** Spring resource context. */
+    private final GridSpringResourceContext ctx;
+
+    /** Change Data Capture configuration. */
+    private final ChangeDataCaptureConfiguration cdcCfg;
+
+    /** WAL iterator factory. */
+    private final IgniteWalIteratorFactory factory;
+
+    /** Events consumer. */
+    private final WALRecordsConsumer<?, ?> consumer;
+
+    /** Logger. */
+    private final IgniteLogger log;
+
+    /** Change Data Capture directory. */
+    private Path cdcDir;
+
+    /** Binary meta directory. */
+    private File binaryMeta;
+
+    /** Marshaller directory. */
+    private File marshaller;
+
+    /** Change Data Capture state. */
+    private ChangeDataCaptureConsumerState state;
+
+    /** Save state to start from. */
+    private WALPointer initState;
+
+    /** Stopped flag. */
+    private volatile boolean stopped;
+
+    /** Already processed segments. */
+    private final List<Path> processed = new ArrayList<>();
+
+    /**
+     * @param igniteCfg Ignite configuration.
+     * @param ctx Spring resource context.
+     * @param cdcCfg Change Data Capture configuration.
+     */
+    public ChangeDataCapture(
+        IgniteConfiguration igniteCfg,
+        GridSpringResourceContext ctx,
+        ChangeDataCaptureConfiguration cdcCfg) {
+        this.igniteCfg = new IgniteConfiguration(igniteCfg);
+        this.ctx = ctx;
+        this.cdcCfg = cdcCfg;
+
+        initWorkDir(this.igniteCfg);
+
+        log = logger(this.igniteCfg);
+
+        consumer = new WALRecordsConsumer<>(cdcCfg.getConsumer(), log);
+
+        factory = new IgniteWalIteratorFactory(log);
+    }
+
+    /** Runs Change Data Capture. */
+    @Override public void run() {
+        synchronized (this) {
+            if (stopped)
+                return;
+        }
+
+        try {
+            runX();
+        }
+        catch (Throwable e) {
+            e.printStackTrace();
+
+            throw new IgniteException(e);
+        }
+    }
+
+    /** Runs Change Data Capture application with possible exception. */
+    public void runX() throws Exception {
+        if (!CU.isPersistenceEnabled(igniteCfg)) {
+            log.error(ERR_MSG);
+
+            throw new IllegalArgumentException(ERR_MSG);
+        }
+
+        PdsFolderSettings<ChangeDataCaptureFileLockHolder> settings =
+            new PdsFolderResolver<>(igniteCfg, log, null, this::tryLock).resolve();
+
+        if (settings == null)
+            throw new IgniteException("Can't find PDS folder!");

Review comment:
       So, the message should be "PDS folder location can't be resolved"?
   And the question here is why?




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org