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

szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis.git


The following commit(s) were added to refs/heads/master by this push:
     new 394b473  RATIS-1412. Add ratis-shell snapshot command (#580)
394b473 is described below

commit 394b47313216f0892ca36a1b17d0081cb212bee1
Author: Yaolong Liu <[email protected]>
AuthorDate: Thu Jan 20 14:19:15 2022 +0800

    RATIS-1412. Add ratis-shell snapshot command (#580)
---
 .../shell/cli/sh/command/SnapshotCommand.java      | 94 +++++++++++++++++++++
 .../shell/cli/sh/snapshot/TakeSnapshotCommand.java | 97 ++++++++++++++++++++++
 2 files changed, 191 insertions(+)

diff --git 
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/SnapshotCommand.java
 
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/SnapshotCommand.java
new file mode 100644
index 0000000..ecc52ad
--- /dev/null
+++ 
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/SnapshotCommand.java
@@ -0,0 +1,94 @@
+/*
+ * 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.ratis.shell.cli.sh.command;
+
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.ratis.shell.cli.Command;
+import org.apache.ratis.shell.cli.sh.snapshot.TakeSnapshotCommand;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/**
+ * Command for the ratis snapshot
+ */
+public class SnapshotCommand extends AbstractRatisCommand {
+
+  private static final List<Function<Context, AbstractRatisCommand>> 
SUB_COMMAND_CONSTRUCTORS
+      = Collections.unmodifiableList(Arrays.asList(TakeSnapshotCommand::new));
+
+  private final Map<String, Command> subs;
+
+  /**
+   * @param context command context
+   */
+  public SnapshotCommand(Context context) {
+    super(context);
+    this.subs = Collections.unmodifiableMap(SUB_COMMAND_CONSTRUCTORS.stream()
+        .map(constructor -> constructor.apply(context))
+        .collect(Collectors.toMap(Command::getCommandName, 
Function.identity())));
+  }
+
+  @Override
+  public String getCommandName() {
+    return "snapshot";
+  }
+
+  @Override
+  public String getUsage() {
+
+    StringBuilder usage = new StringBuilder(getCommandName());
+    for (String cmd : subs.keySet()) {
+      usage.append(" [").append(cmd).append("]");
+    }
+    return usage.toString();
+  }
+
+  @Override
+  public String getDescription() {
+    return description();
+  }
+
+  @Override
+  public Map<String, Command> getSubCommands() {
+    return subs;
+  }
+
+  @Override
+  public Options getOptions() {
+    return super.getOptions().addOption(
+        Option.builder()
+            .option(GROUPID_OPTION_NAME)
+            .hasArg()
+            .required()
+            .desc("the group id")
+            .build());
+  }
+
+  /**
+   * @return command's description
+   */
+  public static String description() {
+    return "Manage ratis snapshot; see the sub-commands for the details.";
+  }
+}
diff --git 
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/snapshot/TakeSnapshotCommand.java
 
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/snapshot/TakeSnapshotCommand.java
new file mode 100644
index 0000000..cc54291
--- /dev/null
+++ 
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/snapshot/TakeSnapshotCommand.java
@@ -0,0 +1,97 @@
+/*
+ * 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.ratis.shell.cli.sh.snapshot;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.ratis.client.RaftClient;
+import org.apache.ratis.protocol.RaftClientReply;
+import org.apache.ratis.shell.cli.RaftUtils;
+import org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand;
+import org.apache.ratis.shell.cli.sh.command.Context;
+
+import java.io.IOException;
+
+/**
+ * Command for make a ratis server take snapshot.
+ */
+public class TakeSnapshotCommand extends AbstractRatisCommand {
+  public static final String TAKE_SNAPSHOT_TIMEOUT_OPTION_NAME = 
"snapshotTimeout";
+
+  /**
+   * @param context command context
+   */
+  public TakeSnapshotCommand(Context context) {
+    super(context);
+  }
+
+  @Override
+  public String getCommandName() {
+    return "create";
+  }
+
+  @Override
+  public int run(CommandLine cl) throws IOException {
+    super.run(cl);
+    long timeout;
+    if (cl.hasOption(TAKE_SNAPSHOT_TIMEOUT_OPTION_NAME)) {
+      timeout = 
Long.parseLong(cl.getOptionValue(TAKE_SNAPSHOT_TIMEOUT_OPTION_NAME));
+    } else {
+      timeout = 3000;
+    }
+    try(final RaftClient raftClient = RaftUtils.createClient(getRaftGroup())) {
+      RaftClientReply reply = 
raftClient.getSnapshotManagementApi().create(timeout);
+      processReply(reply, () -> String.format("Failed to take snapshot of 
peerId %s", raftClient.getLeaderId()));
+      printf(String.format("Successful take snapshot, the latest snapshot 
index is %d",
+          reply.getLogIndex()));
+    }
+    return 0;
+  }
+
+  @Override
+  public String getUsage() {
+    return String.format("%s"
+            + " -%s 
<PEER0_HOST:PEER0_PORT,PEER1_HOST:PEER1_PORT,PEER2_HOST:PEER2_PORT>"
+            + " [-%s <RAFT_GROUP_ID>]"
+            + " [-%s <timeoutInMs>]",
+        getCommandName(), PEER_OPTION_NAME, GROUPID_OPTION_NAME, 
TAKE_SNAPSHOT_TIMEOUT_OPTION_NAME);
+  }
+
+  @Override
+  public String getDescription() {
+    return description();
+  }
+
+  @Override
+  public Options getOptions() {
+    return super.getOptions().addOption(
+        Option.builder()
+            .option(TAKE_SNAPSHOT_TIMEOUT_OPTION_NAME)
+            .hasArg()
+            .desc("timeout to wait taking snapshot in ms")
+            .build());
+  }
+
+  /**
+   * @return command's description
+   */
+  public static String description() {
+    return "Make a ratis server take snapshot";
+  }
+}

Reply via email to