You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ratis.apache.org by sz...@apache.org on 2022/02/24 16:54:22 UTC
[ratis] branch master updated: RATIS-1527. Add a ratis server demo for debug (#605)
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 d581460 RATIS-1527. Add a ratis server demo for debug (#605)
d581460 is described below
commit d58146078389fed9c914a113ee0e85197d4e8b1e
Author: Yaolong Liu <ly...@163.com>
AuthorDate: Fri Feb 25 00:54:17 2022 +0800
RATIS-1527. Add a ratis server demo for debug (#605)
---
.../apache/ratis/examples/common/Constants.java | 81 ++++++++++++++++++++++
.../ratis/examples/counter/CounterCommon.java | 50 -------------
.../examples/counter/client/CounterClient.java | 4 +-
.../examples/counter/server/CounterServer.java | 61 +++++++++-------
.../apache/ratis/examples/debug/server/Server.java | 51 ++++++++++++++
ratis-examples/src/main/resources/conf.properties | 18 +++++
6 files changed, 188 insertions(+), 77 deletions(-)
diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/common/Constants.java b/ratis-examples/src/main/java/org/apache/ratis/examples/common/Constants.java
new file mode 100644
index 0000000..9da6409
--- /dev/null
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/common/Constants.java
@@ -0,0 +1,81 @@
+/*
+ * 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.examples.common;
+
+import org.apache.ratis.protocol.RaftGroup;
+import org.apache.ratis.protocol.RaftGroupId;
+import org.apache.ratis.protocol.RaftPeer;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.UUID;
+
+/**
+ * Constants across servers and clients
+ */
+public final class Constants {
+ public static final List<RaftPeer> PEERS;
+ public static final String PATH;
+
+ static {
+ final Properties properties = new Properties();
+ final String conf = "ratis-examples/src/main/resources/conf.properties";
+ try(InputStream inputStream = new FileInputStream(conf);
+ Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
+ BufferedReader bufferedReader = new BufferedReader(reader)) {
+ properties.load(bufferedReader);
+ } catch (IOException e) {
+ throw new IllegalStateException("Failed to load " + conf, e);
+ }
+ final String key = "raft.server.address.list";
+ final String[] addresses = Optional.ofNullable(properties.getProperty(key))
+ .map(s -> s.split(","))
+ .orElse(null);
+ if (addresses == null || addresses.length == 0) {
+ throw new IllegalArgumentException("Failed to get " + key + " from " + conf);
+ }
+
+ final String key1 = "raft.server.root.storage.path";
+ final String path = properties.getProperty(key1);
+ PATH = path == null ? "./ratis-examples/target" : path;
+ final List<RaftPeer> peers = new ArrayList<>(addresses.length);
+ for (int i = 0; i < addresses.length; i++) {
+ peers.add(RaftPeer.newBuilder().setId("n" + i).setAddress(addresses[i]).build());
+ }
+ PEERS = Collections.unmodifiableList(peers);
+ }
+
+ private static final UUID CLUSTER_GROUP_ID = UUID.fromString("02511d47-d67c-49a3-9011-abb3109a44c1");
+
+ public static final RaftGroup RAFT_GROUP = RaftGroup.valueOf(
+ RaftGroupId.valueOf(Constants.CLUSTER_GROUP_ID), PEERS);
+
+ private Constants() {
+ }
+}
diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/counter/CounterCommon.java b/ratis-examples/src/main/java/org/apache/ratis/examples/counter/CounterCommon.java
deleted file mode 100644
index 4e9f6a4..0000000
--- a/ratis-examples/src/main/java/org/apache/ratis/examples/counter/CounterCommon.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * 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.examples.counter;
-
-import org.apache.ratis.protocol.RaftGroup;
-import org.apache.ratis.protocol.RaftGroupId;
-import org.apache.ratis.protocol.RaftPeer;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.UUID;
-
-/**
- * Common Constant across servers and client
- */
-public final class CounterCommon {
- public static final List<RaftPeer> PEERS;
-
- static {
- List<RaftPeer> peers = new ArrayList<>(3);
- peers.add(RaftPeer.newBuilder().setId("n1").setAddress("127.0.0.1:6000").build());
- peers.add(RaftPeer.newBuilder().setId("n2").setAddress("127.0.0.1:6001").build());
- peers.add(RaftPeer.newBuilder().setId("n3").setAddress("127.0.0.1:6002").build());
- PEERS = Collections.unmodifiableList(peers);
- }
-
- private CounterCommon() {
- }
-
- private static final UUID CLUSTER_GROUP_ID = UUID.fromString("02511d47-d67c-49a3-9011-abb3109a44c1");
- public static final RaftGroup RAFT_GROUP = RaftGroup.valueOf(
- RaftGroupId.valueOf(CounterCommon.CLUSTER_GROUP_ID), PEERS);
-}
diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/counter/client/CounterClient.java b/ratis-examples/src/main/java/org/apache/ratis/examples/counter/client/CounterClient.java
index d0d8e73..a3f28cb 100644
--- a/ratis-examples/src/main/java/org/apache/ratis/examples/counter/client/CounterClient.java
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/counter/client/CounterClient.java
@@ -22,7 +22,7 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.ratis.client.RaftClient;
import org.apache.ratis.conf.Parameters;
import org.apache.ratis.conf.RaftProperties;
-import org.apache.ratis.examples.counter.CounterCommon;
+import org.apache.ratis.examples.common.Constants;
import org.apache.ratis.grpc.GrpcFactory;
import org.apache.ratis.protocol.ClientId;
import org.apache.ratis.protocol.Message;
@@ -87,7 +87,7 @@ public final class CounterClient {
RaftProperties raftProperties = new RaftProperties();
RaftClient.Builder builder = RaftClient.newBuilder()
.setProperties(raftProperties)
- .setRaftGroup(CounterCommon.RAFT_GROUP)
+ .setRaftGroup(Constants.RAFT_GROUP)
.setClientRpc(
new GrpcFactory(new Parameters())
.newRaftClientRpc(ClientId.randomId(), raftProperties));
diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/counter/server/CounterServer.java b/ratis-examples/src/main/java/org/apache/ratis/examples/counter/server/CounterServer.java
index 3350676..b6fc8c7 100644
--- a/ratis-examples/src/main/java/org/apache/ratis/examples/counter/server/CounterServer.java
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/counter/server/CounterServer.java
@@ -19,13 +19,14 @@
package org.apache.ratis.examples.counter.server;
import org.apache.ratis.conf.RaftProperties;
-import org.apache.ratis.examples.counter.CounterCommon;
+import org.apache.ratis.examples.common.Constants;
import org.apache.ratis.grpc.GrpcConfigKeys;
import org.apache.ratis.protocol.RaftPeer;
import org.apache.ratis.server.RaftServer;
import org.apache.ratis.server.RaftServerConfigKeys;
import org.apache.ratis.util.NetUtils;
+import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
@@ -37,54 +38,64 @@ import static java.nio.charset.StandardCharsets.UTF_8;
* Simplest Ratis server, use a simple state machine {@link CounterStateMachine}
* which maintain a counter across multi server.
* This server application designed to run several times with different
- * parameters (1,2 or 3). server addresses hard coded in {@link CounterCommon}
+ * parameters (1,2 or 3). server addresses hard coded in {@link Constants}
* <p>
* Run this application three times with three different parameter set-up a
* ratis cluster which maintain a counter value replicated in each server memory
*/
-public final class CounterServer {
-
- private CounterServer(){
- }
-
- public static void main(String[] args) throws IOException {
- if (args.length < 1) {
- System.err.println("Usage: java -cp *.jar org.apache.ratis.examples.counter.server.CounterServer {serverIndex}");
- System.err.println("{serverIndex} could be 1, 2 or 3");
- System.exit(1);
- }
-
- //find current peer object based on application parameter
- RaftPeer currentPeer =
- CounterCommon.PEERS.get(Integer.parseInt(args[0]) - 1);
+public final class CounterServer implements Closeable {
+ private final RaftServer server;
+ public CounterServer(RaftPeer peer, File storageDir) throws IOException {
//create a property object
RaftProperties properties = new RaftProperties();
//set the storage directory (different for each peer) in RaftProperty object
- File raftStorageDir = new File("./" + currentPeer.getId().toString());
- RaftServerConfigKeys.setStorageDir(properties,
- Collections.singletonList(raftStorageDir));
+ RaftServerConfigKeys.setStorageDir(properties, Collections.singletonList(storageDir));
//set the port which server listen to in RaftProperty object
- final int port = NetUtils.createSocketAddr(currentPeer.getAddress()).getPort();
+ final int port = NetUtils.createSocketAddr(peer.getAddress()).getPort();
GrpcConfigKeys.Server.setPort(properties, port);
//create the counter state machine which hold the counter value
CounterStateMachine counterStateMachine = new CounterStateMachine();
//create and start the Raft server
- RaftServer server = RaftServer.newBuilder()
- .setGroup(CounterCommon.RAFT_GROUP)
+ this.server = RaftServer.newBuilder()
+ .setGroup(Constants.RAFT_GROUP)
.setProperties(properties)
- .setServerId(currentPeer.getId())
+ .setServerId(peer.getId())
.setStateMachine(counterStateMachine)
.build();
+ }
+
+ public void start() throws IOException {
server.start();
+ }
+
+ @Override
+ public void close() throws IOException {
+ server.close();
+ }
+
+ public static void main(String[] args) throws IOException {
+ if (args.length < 1) {
+ System.err.println("Usage: java -cp *.jar org.apache.ratis.examples.counter.server.CounterServer {serverIndex}");
+ System.err.println("{serverIndex} could be 1, 2 or 3");
+ System.exit(1);
+ }
+
+ //find current peer object based on application parameter
+ final RaftPeer currentPeer = Constants.PEERS.get(Integer.parseInt(args[0]) - 1);
+
+ //start a counter server
+ final File storageDir = new File("./" + currentPeer.getId());
+ final CounterServer counterServer = new CounterServer(currentPeer, storageDir);
+ counterServer.start();
//exit when any input entered
Scanner scanner = new Scanner(System.in, UTF_8.name());
scanner.nextLine();
- server.close();
+ counterServer.close();
}
}
diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/debug/server/Server.java b/ratis-examples/src/main/java/org/apache/ratis/examples/debug/server/Server.java
new file mode 100644
index 0000000..a2ab8e1
--- /dev/null
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/debug/server/Server.java
@@ -0,0 +1,51 @@
+/**
+ * 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.examples.debug.server;
+
+import org.apache.ratis.examples.common.Constants;
+import org.apache.ratis.examples.counter.server.CounterServer;
+import org.apache.ratis.protocol.RaftPeer;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * For running a {@link CounterServer}.
+ */
+public final class Server {
+
+ private Server(){
+ }
+
+ public static void main(String[] args) throws IOException {
+ if (args.length < 1) {
+ System.err.println("The arguments should be <ip:port>");
+ System.exit(1);
+ }
+
+ //find current peer object based on application parameter
+ final RaftPeer currentPeer = Constants.PEERS.stream()
+ .filter(raftPeer -> raftPeer.getAddress().equals(args[0]))
+ .findFirst().orElseThrow(() -> new IllegalArgumentException("Peer not found: " + args[0]));
+
+ final File storageDir = new File(Constants.PATH, currentPeer.getId().toString());
+ final CounterServer counterServer = new CounterServer(currentPeer, storageDir);
+ counterServer.start();
+ }
+}
diff --git a/ratis-examples/src/main/resources/conf.properties b/ratis-examples/src/main/resources/conf.properties
new file mode 100644
index 0000000..8390901
--- /dev/null
+++ b/ratis-examples/src/main/resources/conf.properties
@@ -0,0 +1,18 @@
+# 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.
+
+raft.server.address.list=127.0.0.1:10024,127.0.0.1:10124,127.0.0.1:11124
+# raft.server.root.storage.path
\ No newline at end of file