You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by tk...@apache.org on 2025/06/12 14:48:18 UTC

(ignite-3) 01/01: IGNITE-25673 wip

This is an automated email from the ASF dual-hosted git repository.

tkalkirill pushed a commit to branch ignite-25673
in repository https://gitbox.apache.org/repos/asf/ignite-3.git

commit 809cd21a42f03c1bbca0b0d6bd52896b94282143
Author: Kirill Tkalenko <tk...@yandex.ru>
AuthorDate: Thu Jun 12 17:47:58 2025 +0300

    IGNITE-25673 wip
---
 .../storage/snapshot/SnapshotCountDownEvent.java   | 130 +++++++++++++++++++++
 .../storage/snapshot/SnapshotExecutorImpl.java     |   9 +-
 2 files changed, 134 insertions(+), 5 deletions(-)

diff --git a/modules/raft/src/main/java/org/apache/ignite/raft/jraft/storage/snapshot/SnapshotCountDownEvent.java b/modules/raft/src/main/java/org/apache/ignite/raft/jraft/storage/snapshot/SnapshotCountDownEvent.java
new file mode 100644
index 00000000000..e7e7fc5ce72
--- /dev/null
+++ b/modules/raft/src/main/java/org/apache/ignite/raft/jraft/storage/snapshot/SnapshotCountDownEvent.java
@@ -0,0 +1,130 @@
+/*
+ * 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.raft.jraft.storage.snapshot;
+
+import java.time.Instant;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+import org.apache.ignite.internal.util.FastTimestamps;
+
+/** Snapshot countdown event, with logging of long waits for events to complete. */
+class SnapshotCountDownEvent {
+    private static final IgniteLogger LOG = Loggers.forClass(SnapshotCountDownEvent.class);
+
+    private static final DateTimeFormatter DATE_TIME_FORMATTER =
+            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z").withZone(ZoneId.systemDefault());
+
+    static final int INIT_SNAPSHOT_OP = 0;
+
+    static final int DO_SNAPSHOT_OP = 1;
+
+    static final int REGISTER_DOWNLOADING_SNAPSHOT_OP = 2;
+
+    private final Lock lock = new ReentrantLock();
+
+    private final Condition condition = lock.newCondition();
+
+    /** Guarded by {@link #lock}. */
+    private final Deque<State> stateStack = new ArrayDeque<>();
+
+    public int incrementAndGet(int state) {
+        lock.lock();
+        try {
+            stateStack.add(new State(state));
+
+            return stateStack.size();
+        }
+        finally {
+            lock.unlock();
+        }
+    }
+
+    public void countDown() {
+        lock.lock();
+        try {
+            stateStack.pollLast();
+
+            if (stateStack.isEmpty()) {
+                condition.signalAll();
+            }
+        }
+        finally {
+            lock.unlock();
+        }
+    }
+
+    public void await() throws InterruptedException {
+        lock.lock();
+        try {
+            while (!stateStack.isEmpty()) {
+                if (!condition.await(30, TimeUnit.SECONDS)) {
+                    LOG.warn("Failed to wait for all snapshot events: [remaining={}]", stateStack);
+                }
+            }
+        }
+        finally {
+            lock.unlock();
+        }
+    }
+
+    private static final class State {
+        private final int state;
+
+        private final long coarseTimeMillis;
+
+        private State(int state, long coarseTimeMillis) {
+            this.state = state;
+            this.coarseTimeMillis = coarseTimeMillis;
+        }
+
+        private State(int state) {
+            this(state, FastTimestamps.coarseCurrentTimeMillis());
+        }
+
+        @Override public String toString() {
+            return "State ["
+                + "state=" + toStringIntState(state)
+                + ", coarseTime=" + toStringCoarseTimeMillis(coarseTimeMillis)
+                + ']';
+        }
+    }
+
+    private static String toStringIntState(int state) {
+        switch (state) {
+            case INIT_SNAPSHOT_OP:
+                return "init";
+            case DO_SNAPSHOT_OP:
+                return "do";
+            case REGISTER_DOWNLOADING_SNAPSHOT_OP:
+                return "register downloading snapshot";
+            default:
+                return "unknown_" + state;
+        }
+    }
+
+    private static String toStringCoarseTimeMillis(long coarseTimeMillis) {
+        return DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(coarseTimeMillis));
+    }
+}
diff --git a/modules/raft/src/main/java/org/apache/ignite/raft/jraft/storage/snapshot/SnapshotExecutorImpl.java b/modules/raft/src/main/java/org/apache/ignite/raft/jraft/storage/snapshot/SnapshotExecutorImpl.java
index 4ef3d8eef7a..dd44c727fab 100644
--- a/modules/raft/src/main/java/org/apache/ignite/raft/jraft/storage/snapshot/SnapshotExecutorImpl.java
+++ b/modules/raft/src/main/java/org/apache/ignite/raft/jraft/storage/snapshot/SnapshotExecutorImpl.java
@@ -45,7 +45,6 @@ import org.apache.ignite.raft.jraft.storage.LogManager;
 import org.apache.ignite.raft.jraft.storage.SnapshotExecutor;
 import org.apache.ignite.raft.jraft.storage.SnapshotStorage;
 import org.apache.ignite.raft.jraft.storage.snapshot.local.LocalSnapshotStorage;
-import org.apache.ignite.raft.jraft.util.CountDownEvent;
 import org.apache.ignite.raft.jraft.util.OnlyForTest;
 import org.apache.ignite.raft.jraft.util.Requires;
 import org.apache.ignite.raft.jraft.util.StringUtils;
@@ -72,7 +71,7 @@ public class SnapshotExecutorImpl implements SnapshotExecutor {
     private LogManager logManager;
     private final AtomicReference<DownloadingSnapshot> downloadingSnapshot = new AtomicReference<>(null);
     private SnapshotMeta loadingSnapshotMeta;
-    private final CountDownEvent runningJobs = new CountDownEvent();
+    private final SnapshotCountDownEvent runningJobs = new SnapshotCountDownEvent();
     private RaftMessagesFactory msgFactory;
 
     /**
@@ -245,7 +244,7 @@ public class SnapshotExecutorImpl implements SnapshotExecutor {
         }
         LOG.info("Loading snapshot, meta={}.", this.loadingSnapshotMeta);
         this.loadingSnapshot = true;
-        this.runningJobs.incrementAndGet();
+        this.runningJobs.incrementAndGet(SnapshotCountDownEvent.INIT_SNAPSHOT_OP);
         final FirstSnapshotLoadDone done = new FirstSnapshotLoadDone(reader);
         Requires.requireTrue(this.fsmCaller.onSnapshotLoad(done));
         try {
@@ -351,7 +350,7 @@ public class SnapshotExecutorImpl implements SnapshotExecutor {
                 Utils.runClosureInThread(this.node.getOptions().getCommonExecutor(), done, new Status(RaftError.EHOSTDOWN, "The raft node is down."));
                 return;
             }
-            this.runningJobs.incrementAndGet();
+            this.runningJobs.incrementAndGet(SnapshotCountDownEvent.DO_SNAPSHOT_OP);
         }
         finally {
             if (doUnlock) {
@@ -600,7 +599,7 @@ public class SnapshotExecutorImpl implements SnapshotExecutor {
                         .newResponse(msgFactory, RaftError.EINVAL, "Fail to copy from: %s", ds.request.uri()));
                     return false;
                 }
-                this.runningJobs.incrementAndGet();
+                this.runningJobs.incrementAndGet(SnapshotCountDownEvent.REGISTER_DOWNLOADING_SNAPSHOT_OP);
                 return true;
             }