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

williamsong pushed a commit to branch snapshot-3
in repository https://gitbox.apache.org/repos/asf/ratis.git

commit 52e8db6975501017bfa21deac9644b2c7834b4c2
Author: szywilliam <[email protected]>
AuthorDate: Wed Mar 27 14:09:48 2024 +0800

    RATIS-2050. Add creationGap param to snapshot management API (#1058)
---
 .../ratis/client/api/SnapshotManagementApi.java    | 22 ++++++++++++++++++++--
 .../apache/ratis/client/impl/ClientProtoUtils.java |  5 +++--
 .../ratis/client/impl/SnapshotManagementImpl.java  |  5 +++--
 .../ratis/protocol/SnapshotManagementRequest.java  | 18 ++++++++++++++++--
 ratis-proto/src/main/proto/Raft.proto              |  2 +-
 .../apache/ratis/server/impl/RaftServerImpl.java   |  5 +++--
 6 files changed, 46 insertions(+), 11 deletions(-)

diff --git 
a/ratis-client/src/main/java/org/apache/ratis/client/api/SnapshotManagementApi.java
 
b/ratis-client/src/main/java/org/apache/ratis/client/api/SnapshotManagementApi.java
index edd047544..f83d97604 100644
--- 
a/ratis-client/src/main/java/org/apache/ratis/client/api/SnapshotManagementApi.java
+++ 
b/ratis-client/src/main/java/org/apache/ratis/client/api/SnapshotManagementApi.java
@@ -27,6 +27,24 @@ import java.io.IOException;
  */
 public interface SnapshotManagementApi {
 
-  /** trigger create snapshot file. */
-  RaftClientReply create(long timeoutMs) throws IOException;
+  /** The same as create(0, timeoutMs). */
+  default RaftClientReply create(long timeoutMs) throws IOException {
+    return create(0, timeoutMs);
+  }
+
+    /** The same as create(force? 1 : 0, timeoutMs). */
+  default RaftClientReply create(boolean force, long timeoutMs) throws 
IOException {
+    return create(force? 1 : 0, timeoutMs);
+  }
+
+    /**
+   * Trigger to create a snapshot.
+   *
+   * @param creationGap When (creationGap > 0) and (astAppliedIndex - 
lastSnapshotIndex < creationGap),
+   *                    return lastSnapshotIndex; otherwise, take a new 
snapshot and then return its index.
+   *                    When creationGap == 0, use the server configured value 
as the creationGap.
+   * @return a reply.  When {@link RaftClientReply#isSuccess()} is true,
+   *         {@link RaftClientReply#getLogIndex()} is the snapshot index 
fulfilling the operation.
+   */
+  RaftClientReply create(long creationGap, long timeoutMs) throws IOException;
 }
diff --git 
a/ratis-client/src/main/java/org/apache/ratis/client/impl/ClientProtoUtils.java 
b/ratis-client/src/main/java/org/apache/ratis/client/impl/ClientProtoUtils.java
index db1983195..cfa849047 100644
--- 
a/ratis-client/src/main/java/org/apache/ratis/client/impl/ClientProtoUtils.java
+++ 
b/ratis-client/src/main/java/org/apache/ratis/client/impl/ClientProtoUtils.java
@@ -657,7 +657,8 @@ public interface ClientProtoUtils {
     switch(p.getOpCase()) {
       case CREATE:
         return SnapshotManagementRequest.newCreate(clientId, serverId,
-            ProtoUtils.toRaftGroupId(m.getRaftGroupId()), m.getCallId(), 
m.getTimeoutMs());
+            ProtoUtils.toRaftGroupId(m.getRaftGroupId()), m.getCallId(), 
m.getTimeoutMs(),
+            p.getCreate().getCreationGap());
       default:
         throw new IllegalArgumentException("Unexpected op " + p.getOpCase() + 
" in " + p);
     }
@@ -669,7 +670,7 @@ public interface ClientProtoUtils {
         .setRpcRequest(toRaftRpcRequestProtoBuilder(request));
     final SnapshotManagementRequest.Create create = request.getCreate();
     if (create != null) {
-      b.setCreate(SnapshotCreateRequestProto.newBuilder().build());
+      
b.setCreate(SnapshotCreateRequestProto.newBuilder().setCreationGap(create.getCreationGap()).build());
     }
     return b.build();
   }
diff --git 
a/ratis-client/src/main/java/org/apache/ratis/client/impl/SnapshotManagementImpl.java
 
b/ratis-client/src/main/java/org/apache/ratis/client/impl/SnapshotManagementImpl.java
index 1762dc0e4..65c54d0f2 100644
--- 
a/ratis-client/src/main/java/org/apache/ratis/client/impl/SnapshotManagementImpl.java
+++ 
b/ratis-client/src/main/java/org/apache/ratis/client/impl/SnapshotManagementImpl.java
@@ -37,9 +37,10 @@ class SnapshotManagementImpl implements 
SnapshotManagementApi {
   }
 
   @Override
-  public RaftClientReply create(long timeoutMs) throws IOException {
+  public RaftClientReply create(long creationGap, long timeoutMs) throws 
IOException {
     final long callId = CallId.getAndIncrement();
     return client.io().sendRequestWithRetry(() -> 
SnapshotManagementRequest.newCreate(client.getId(),
-        Optional.ofNullable(server).orElseGet(client::getLeaderId), 
client.getGroupId(), callId, timeoutMs));
+        Optional.ofNullable(server).orElseGet(client::getLeaderId),
+        client.getGroupId(), callId, timeoutMs, creationGap));
   }
 }
diff --git 
a/ratis-common/src/main/java/org/apache/ratis/protocol/SnapshotManagementRequest.java
 
b/ratis-common/src/main/java/org/apache/ratis/protocol/SnapshotManagementRequest.java
index 2ea2059b5..269fdfc59 100644
--- 
a/ratis-common/src/main/java/org/apache/ratis/protocol/SnapshotManagementRequest.java
+++ 
b/ratis-common/src/main/java/org/apache/ratis/protocol/SnapshotManagementRequest.java
@@ -24,7 +24,16 @@ public final class SnapshotManagementRequest extends 
RaftClientRequest {
   public abstract static class Op {
 
   }
-  public static class Create extends Op {
+
+  public static final class Create extends Op {
+    private final long creationGap;
+    private Create(long creationGap) {
+      this.creationGap = creationGap;
+    }
+
+    public long getCreationGap() {
+      return creationGap;
+    }
 
     @Override
     public String toString() {
@@ -35,8 +44,13 @@ public final class SnapshotManagementRequest extends 
RaftClientRequest {
 
   public static SnapshotManagementRequest newCreate(ClientId clientId,
       RaftPeerId serverId, RaftGroupId groupId, long callId, long timeoutMs) {
+    return newCreate(clientId, serverId, groupId, callId, timeoutMs, 0);
+  }
+
+  public static SnapshotManagementRequest newCreate(ClientId clientId,
+      RaftPeerId serverId, RaftGroupId groupId, long callId, long timeoutMs, 
long creationGap) {
     return new SnapshotManagementRequest(clientId,
-        serverId, groupId, callId, timeoutMs,new 
SnapshotManagementRequest.Create());
+        serverId, groupId, callId, timeoutMs, new 
SnapshotManagementRequest.Create(creationGap));
   }
 
   private final Op op;
diff --git a/ratis-proto/src/main/proto/Raft.proto 
b/ratis-proto/src/main/proto/Raft.proto
index d8a1b626a..10139d448 100644
--- a/ratis-proto/src/main/proto/Raft.proto
+++ b/ratis-proto/src/main/proto/Raft.proto
@@ -469,7 +469,7 @@ message SnapshotManagementRequestProto {
 }
 
 message SnapshotCreateRequestProto {
-
+  uint64 creationGap = 1;
 }
 
 message StartLeaderElectionRequestProto {
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java
index 4ae60acaa..a6dba8ab6 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java
@@ -1209,9 +1209,10 @@ class RaftServerImpl implements RaftServer.Division,
     LOG.info("{}: takeSnapshotAsync {}", getMemberId(), request);
     assertLifeCycleState(LifeCycle.States.RUNNING);
     assertGroup(request.getRequestorId(), request.getRaftGroupId());
+    Preconditions.assertNotNull(request.getCreate(), "create");
 
-    //TODO(liuyaolong): get the gap value from shell command
-    long minGapValue = 
RaftServerConfigKeys.Snapshot.creationGap(proxy.getProperties());
+    final long creationGap = request.getCreate().getCreationGap();
+    long minGapValue = creationGap > 0? creationGap : 
RaftServerConfigKeys.Snapshot.creationGap(proxy.getProperties());
     final long lastSnapshotIndex = 
Optional.ofNullable(stateMachine.getLatestSnapshot())
         .map(SnapshotInfo::getIndex)
         .orElse(0L);

Reply via email to