szetszwo commented on a change in pull request #605:
URL: https://github.com/apache/ratis/pull/605#discussion_r813062137
##########
File path:
ratis-examples/src/main/java/org/apache/ratis/examples/common/Common.java
##########
@@ -16,35 +16,55 @@
* limitations under the License.
*/
-package org.apache.ratis.examples.counter;
+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.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import java.util.Properties;
import java.util.UUID;
/**
* Common Constant across servers and client
*/
-public final class CounterCommon {
+public final class Common {
Review comment:
Let's call it `Constants`.
##########
File path:
ratis-examples/src/main/java/org/apache/ratis/examples/common/StateMachine.java
##########
@@ -16,7 +16,7 @@
* limitations under the License.
*/
-package org.apache.ratis.examples.counter.server;
+package org.apache.ratis.examples.common;
Review comment:
Let's don't change CounterStateMachine at all since this state machine
is specific to the counter example.
Later, we may let the user choose other state machines.
##########
File path:
ratis-examples/src/main/java/org/apache/ratis/examples/debug/server/Server.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.debug.server;
+
+import org.apache.ratis.conf.RaftProperties;
+import org.apache.ratis.examples.common.StateMachine;
+import org.apache.ratis.examples.common.Common;
+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.File;
+import java.io.IOException;
+import java.util.Collections;
+
+/**
+ * Simplest Ratis server, use a simple state machine {@link StateMachine}
+ * which maintain a counter across multi server.
+ *
+ * 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 Server {
+
+ private Server(){
+ }
+
+ public static void main(String[] args) throws IOException {
+ if (args.length < 1) {
+ System.err.println("Ratis sever address should be provided in the style
<ip:port>");
+ System.exit(1);
+ }
+
+ //find current peer object based on application parameter
+ RaftPeer currentPeer =
+ Common.PEERS.stream()
+ .filter(raftPeer -> raftPeer.getAddress().equals(args[0]))
+ .findFirst().get();
+
+ //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));
+
+ //set the port which server listen to in RaftProperty object
+ final int port =
NetUtils.createSocketAddr(currentPeer.getAddress()).getPort();
+ GrpcConfigKeys.Server.setPort(properties, port);
+
+ //create the counter state machine which hold the counter value
+ StateMachine stateMachine = new StateMachine();
+
+ //create and start the Raft server
+ RaftServer server = RaftServer.newBuilder()
+ .setGroup(Common.RAFT_GROUP)
+ .setProperties(properties)
+ .setServerId(currentPeer.getId())
+ .setStateMachine(stateMachine)
+ .build();
+ server.start();
Review comment:
Let's refactor the CounterServer so that we can reuse the code.
##########
File path:
ratis-examples/src/main/java/org/apache/ratis/examples/common/Common.java
##########
@@ -16,35 +16,55 @@
* limitations under the License.
*/
-package org.apache.ratis.examples.counter;
+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.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import java.util.Properties;
import java.util.UUID;
/**
* Common Constant across servers and client
*/
-public final class CounterCommon {
+public final class Common {
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());
+ Properties properties = new Properties();
+
+ try(InputStream inputStream = new
FileInputStream("ratis-examples/src/main/resources/conf.properties");
+ Reader reader = new InputStreamReader(inputStream, "UTF-8");
+ BufferedReader bufferedReader = new BufferedReader(reader)) {
+ properties.load(bufferedReader);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ String[] address =
properties.getProperty("raft.server.address").split(",");
+ if (address.length <= 0) {
+ throw new IllegalArgumentException("Can't get the ratis server address");
+ }
+ for (int i = 0; i < address.length; i++) {
+ peers.add(RaftPeer.newBuilder().setId("n" +
i).setAddress(address[i]).build());
+ }
Review comment:
- Let's print out the conf file and the key in the error messages.
- Use Optional to handle null.
```
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 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);
}
```
##########
File path:
ratis-examples/src/main/java/org/apache/ratis/examples/debug/server/Server.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.debug.server;
+
+import org.apache.ratis.conf.RaftProperties;
+import org.apache.ratis.examples.common.StateMachine;
+import org.apache.ratis.examples.common.Common;
+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.File;
+import java.io.IOException;
+import java.util.Collections;
+
+/**
+ * Simplest Ratis server, use a simple state machine {@link StateMachine}
+ * which maintain a counter across multi server.
+ *
+ * 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 Server {
+
+ private Server(){
+ }
+
+ public static void main(String[] args) throws IOException {
+ if (args.length < 1) {
+ System.err.println("Ratis sever address should be provided in the style
<ip:port>");
+ System.exit(1);
+ }
+
+ //find current peer object based on application parameter
+ RaftPeer currentPeer =
+ Common.PEERS.stream()
+ .filter(raftPeer -> raftPeer.getAddress().equals(args[0]))
+ .findFirst().get();
+
+ //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());
Review comment:
It should use a directory inside ./ratis-examples/target . How about we
pass the root path in args?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]