Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion docs/_docs/persistence/change-data-capture.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ The distributed properties listed in the table below allow you to configure CDC
|===
|Property | Description | Default Value
|`cdc.disabled`| Disables CDC in the cluster to avoid disk overflow. Note that cache changes will be lost when CDC
is disabled. Useful if the CDC application is down for a long time. | false
is disabled. Useful if the CDC application is down for a long time.
See link:tools/control-script#working-with-cluster-properties[Working with Cluster Properties]. | false
|===

== API
Expand Down Expand Up @@ -207,6 +208,51 @@ The command will iterate over caches and writes primary copies of data entries t

NOTE: There are no guarantees of notifying the CDC consumer on concurrent cache updates: use the `CdcEvent#version` to resolve version.

== Clear CDC directory

Sometimes the CDC application has to start from scratch, for example, after a long CDC outage or when replication
is being set up again. In this case you can delete all WAL segment links and the CDC application state from the CDC
directory.

NOTE: Use the link:#handling-skipped-segments[`--cdc delete_lost_segment_links`] command if the CDC application fails
because of skipped segments. Use the `--cdc clear` command to drop all the accumulated changes and the CDC application
state.

WARNING: All changes in deleted segment links will be lost!

To clear the CDC directory, follow the steps below. Steps 3–5 are required only if changes must be captured after
clearing.

1. Shut down the CDC application. The command fails if the CDC directory is locked by the running application.
2. Run the following link:tools/control-script[Control Script] command:
+
[source,shell]
----
# Clear CDC directories on all server nodes.
control.sh|bat --cdc clear

# Clear CDC directory on a node.
control.sh|bat --cdc clear --node-id node_id

# Clear CDC directories completely, including the CDC mode state.
control.sh|bat --cdc clear --reset-mode
----
+
If the command fails, fix the reported error and run it again. Do not start the CDC application until the command
succeeds.
3. If the command was run with `--reset-mode`, restart the cleared Ignite nodes with CDC enabled (before the restart,
make sure the `cdc.disabled` distributed property is set to `false`). Without the restart, the CDC application does not
capture changes.
4. Start the CDC application.
5. Synchronize data with the destination (e.g. a remote cluster), because changes in the cleared segment links are not
transmitted. Use the link:#forcefully-resend-all-cache-data-to-cdc[resend command], a snapshot or other methods.

NOTE: There is no need to disable CDC with the `cdc.disabled` distributed property before clearing. Segment links
created after clearing are kept, and the CDC application consumes them after start.

NOTE: The command clears CDC directories only on the server nodes that are in the topology. CDC directories of offline
nodes are not cleared. After such a node joins the cluster, clear its CDC directory with the `--node-id` argument.

== cdc-ext

Ignite extensions project has link:https://github.com/apache/ignite-extensions/tree/master/modules/cdc-ext[cdc-ext] module which provides two way to setup cross cluster replication based on CDC.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -86,6 +87,12 @@ public class CdcCommandTest extends GridCommandHandlerAbstractTest {
/** */
public static final String RESEND = "resend";

/** */
public static final String CLEAR = "clear";

/** */
public static final String RESET_MODE = "--reset-mode";

/** */
private IgniteEx srv0;

Expand Down Expand Up @@ -524,6 +531,249 @@ public void testResendOnClientJoin() throws Exception {
waitForSize(cnsmr1, srv1.cache(DEFAULT_CACHE_NAME).localSize(CachePeekMode.PRIMARY));
}

/** */
@Test
public void testCdcClearOnAllNodes() throws Exception {
injectTestSystemOut();

prepareForClear();

// Temporary mode state can remain if the CDC application failed while saving it.
File tmpModeState = fileTree(0).tmpCdcModeState().toFile();

assertTrue(tmpModeState.createNewFile());

File[] links0 = cdcLinks(0);
File[] links1 = cdcLinks(1);

assertContains(log, executeCommand(EXIT_CODE_OK, CDC, CLEAR), "CDC directories were successfully cleared.");

checkCdcCleared(0, links0, true);
checkCdcCleared(1, links1, true);
}

/** */
@Test
public void testCdcClearOnOneNode() throws Exception {
injectTestSystemOut();

prepareForClear();

File[] links0 = cdcLinks(0);
File[] links1 = cdcLinks(1);

assertContains(log, executeCommand(EXIT_CODE_OK, CDC, CLEAR, NODE_ID, grid(1).localNode().id().toString()),
"CDC directories were successfully cleared.");

for (File link : links0)
assertTrue("Segment link is deleted: " + link, link.exists());

checkCdcNotCleared(0, 2);
checkCdcCleared(1, links1, true);
}

/** */
@Test
public void testCdcClearOnClientNode() throws Exception {
injectTestSystemOut();

IgniteEx client = startClientGrid("client");

assertContains(log,
executeCommand(EXIT_CODE_INVALID_ARGUMENTS, CDC, CLEAR, NODE_ID, client.localNode().id().toString()),
"Client nodes do not support CDC clear command.");
}

/** */
@Test
public void testCdcClearWithResetMode() throws Exception {
injectTestSystemOut();

prepareForClear();

File[] links0 = cdcLinks(0);
File[] links1 = cdcLinks(1);

assertContains(log, executeCommand(EXIT_CODE_OK, CDC, CLEAR, RESET_MODE),
"CDC directories were successfully cleared.");

checkCdcCleared(0, links0, false);
checkCdcCleared(1, links1, false);
}

/** */
@Test
public void testCdcClearWithLockAcquire() throws Exception {
injectTestSystemOut();

runCdc(grid(0));

archiveSegment();

waitForCdcState(0);

File[] links1 = cdcLinks(1);

assertTrue(links1.length > 0);

assertContains(log, executeCommand(EXIT_CODE_UNEXPECTED_ERROR, CDC, CLEAR),
"Unable to acquire lock to the CDC folder. Make sure a CDC app is shut down");

checkCdcNotCleared(0, 1);
checkCdcCleared(1, links1, true);

stopThreads(log);

File[] links0 = cdcLinks(0);

links1 = cdcLinks(1);

assertContains(log, executeCommand(EXIT_CODE_OK, CDC, CLEAR), "CDC directories were successfully cleared.");

checkCdcCleared(0, links0, true);
checkCdcCleared(1, links1, true);
}

/**
* Checks that the CDC application captures changes after the CDC directory is cleared. The CDC application is not
* started on the second node before clear, so its CDC mode is not saved.
*/
@Test
public void testCdcCapturesChangesAfterClear() throws Exception {
injectTestSystemOut();

prepareForClear(false);

assertFalse(fileTree(1).cdcModeState().toFile().exists());

assertContains(log, executeCommand(EXIT_CODE_OK, CDC, CLEAR), "CDC directories were successfully cleared.");

UserCdcConsumer cnsmr0 = runCdc(grid(0));
UserCdcConsumer cnsmr1 = runCdc(grid(1));

addData(grid(0).cache(DEFAULT_CACHE_NAME), KEYS_CNT, 2 * KEYS_CNT);

checkPrimaryKeysCaptured(grid(0), cnsmr0, KEYS_CNT, 2 * KEYS_CNT);
checkPrimaryKeysCaptured(grid(1), cnsmr1, KEYS_CNT, 2 * KEYS_CNT);
}

/**
* @param srv Server node.
* @param cnsmr CDC consumer of the node.
* @param from First key, inclusive.
* @param to Last key, exclusive.
*/
private void checkPrimaryKeysCaptured(IgniteEx srv, UserCdcConsumer cnsmr, int from, int to) throws Exception {
List<Integer> expKeys = new ArrayList<>();

for (int key = from; key < to; key++) {
if (srv.affinity(DEFAULT_CACHE_NAME).isPrimary(srv.localNode(), key))
expKeys.add(key);
}

assertFalse(expKeys.isEmpty());

assertTrue(waitForCondition(() -> cnsmr.data(UPDATE, CU.cacheId(DEFAULT_CACHE_NAME)).containsAll(expKeys),
getTestTimeout()));
}

/** */
private void prepareForClear() throws Exception {
prepareForClear(true);
}

/** @param runCdcOnSecondNode Whether to run the CDC application on the second node. */
private void prepareForClear(boolean runCdcOnSecondNode) throws Exception {
for (int i = 0; i < 2; i++) {
NodeFileTree ft = fileTree(i);

assertEquals(0, ft.walCdcSegments().length);
assertFalse(ft.cdcState().toFile().exists());
}

runCdc(grid(0));

if (runCdcOnSecondNode)
runCdc(grid(1));

archiveSegment();

waitForCdcState(0);

if (runCdcOnSecondNode)
waitForCdcState(1);

stopThreads(log);

// Segments archived after CDC applications stop are not processed and not deleted.
archiveSegment();
archiveSegment();

checkCdcNotCleared(0, 2);

if (runCdcOnSecondNode)
checkCdcNotCleared(1, 2);
}

/** @param idx Node index. */
private void waitForCdcState(int idx) throws Exception {
NodeFileTree ft = fileTree(idx);

assertTrue(waitForCondition(() -> ft.cdcWalState().toFile().exists(), getTestTimeout()));
}

/**
* @param idx Node index.
* @param minSegCnt Minimal expected count of segment links.
*/
private void checkCdcNotCleared(int idx, int minSegCnt) throws Exception {
NodeFileTree ft = fileTree(idx);

assertTrue(waitForCondition(() -> ft.walCdcSegments().length >= minSegCnt, getTestTimeout()));
assertTrue(ft.cdcWalState().toFile().exists());
assertTrue(ft.cdcModeState().toFile().exists());
}

/**
* @param idx Node index.
* @return CDC segment links of the node.
*/
private File[] cdcLinks(int idx) {
return fileTree(idx).walCdcSegments();
}

/**
* @param idx Node index.
* @return File tree of the node.
*/
private NodeFileTree fileTree(int idx) {
return grid(idx).context().pdsFolderResolver().fileTree();
}

/**
* Checks that the CDC directory is cleared. Links created after clearing are kept, because CDC is enabled and
* segments can be archived by timeout.
*
* @param idx Node index.
* @param links Segment links existed before clearing.
* @param modeExists Whether CDC mode state is expected to exist.
*/
private void checkCdcCleared(int idx, File[] links, boolean modeExists) {
NodeFileTree ft = fileTree(idx);

for (File link : links)
assertFalse("Segment link is not deleted: " + link, link.exists());

assertEquals(modeExists, ft.cdcState().toFile().exists());

if (modeExists) {
File[] stateFiles = ft.cdcState().toFile().listFiles();

assertEquals("Only CDC mode state is expected", 1, stateFiles.length);
assertEquals("Unexpected state file", ft.cdcModeState().toFile(), stateFiles[0]);
}
}

/** */
public static UserCdcConsumer runCdc(Ignite ign) {
UserCdcConsumer cnsmr = new UserCdcConsumer();
Expand Down
Loading
Loading