Skip to content

Commit

Permalink
demo: add simple 3 node demo
Browse files Browse the repository at this point in the history
  • Loading branch information
areyouok committed Jan 12, 2025
1 parent 3f25437 commit 7f0f9ba
Show file tree
Hide file tree
Showing 11 changed files with 288 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class RaftBenchmark extends BenchBase {
private KvClient[] clients;

public static void main(String[] args) throws Exception {
RaftBenchmark benchmark = new RaftBenchmark(CLIENT_COUNT, 100, 100);
RaftBenchmark benchmark = new RaftBenchmark(CLIENT_COUNT, 5000, 100);
benchmark.setLogRt(true);
benchmark.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@
public class KvClient extends AbstractLifeCircle {
private final RaftClient raftClient;
private List<RaftNode> initServers;
private int initGroupId;
private int[] initGroupIds;

public KvClient() {
NioClientConfig nioClientConfig = new NioClientConfig();
this.raftClient = new RaftClient(nioClientConfig);
}

public KvClient(int initGroupId, String initServers) {
this.initGroupId = initGroupId;
public KvClient(String initServers, int... initGroupIds) {
this.initGroupIds = initGroupIds;
NioClientConfig nioClientConfig = new NioClientConfig();
this.raftClient = new RaftClient(nioClientConfig);
this.initServers = RaftNode.parseServers(initServers);
Expand Down Expand Up @@ -144,7 +144,9 @@ public CompletableFuture<Void> mkdir(int groupId, String key, DtTime timeout) {
protected void doStart() {
raftClient.start();
if (initServers != null) {
raftClient.addOrUpdateGroup(initGroupId, initServers);
for (int initGroupId : initGroupIds) {
raftClient.addOrUpdateGroup(initGroupId, initServers);
}
}
}

Expand Down
52 changes: 52 additions & 0 deletions demos/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright The Dongting Project
~
~ The Dongting Project 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:
~
~ https://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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>dongting</artifactId>
<groupId>com.github.dtprj.dongting</groupId>
<version>0.2-SNAPSHOT</version>
</parent>
<artifactId>dongting-demos</artifactId>

<properties>
<maven.install.skip>true</maven.install.skip>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
<dependencies>
<dependency>
<groupId>com.github.dtprj.dongting</groupId>
<artifactId>dongting-server</artifactId>
<version>0.2-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
<release>17</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright The Dongting Project
*
* The Dongting Project 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:
*
* https://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 com.github.dtprj.dongting.demos.cluster;

import com.github.dtprj.dongting.common.DtTime;
import com.github.dtprj.dongting.dtkv.KvClient;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
* @author huangli
*/
public class DemoClient {

public static void main(String[] args) throws Exception {
String servers = "1,127.0.0.1:5001;2,127.0.0.1:5002;3,127.0.0.1:5003";
int groupId = 0;
KvClient client = new KvClient(servers, groupId);
client.start();

long startTime = System.currentTimeMillis();
int loop = 3_000;
CountDownLatch latch = new CountDownLatch(loop);
for (int i = 0; i < loop; i++) {
String key = "key" + (i % 10_000);
DtTime timeout = new DtTime(3, TimeUnit.SECONDS);
CompletableFuture<Void> f = client.put(groupId, key, "value".getBytes(), timeout);
f.whenComplete((v, e) -> {
if (e == null) {
latch.countDown();
} else {
e.printStackTrace();
}
});
}
latch.await();
System.out.println("put " + loop + " keys cost " + (System.currentTimeMillis() - startTime) + "ms");

client.stop(new DtTime(3, TimeUnit.SECONDS));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright The Dongting Project
*
* The Dongting Project 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:
*
* https://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 com.github.dtprj.dongting.demos.cluster;

import com.github.dtprj.dongting.dtkv.server.DtKV;
import com.github.dtprj.dongting.dtkv.server.KvConfig;
import com.github.dtprj.dongting.dtkv.server.KvServerUtil;
import com.github.dtprj.dongting.raft.server.DefaultRaftFactory;
import com.github.dtprj.dongting.raft.server.RaftGroupConfig;
import com.github.dtprj.dongting.raft.server.RaftGroupConfigEx;
import com.github.dtprj.dongting.raft.server.RaftServer;
import com.github.dtprj.dongting.raft.server.RaftServerConfig;
import com.github.dtprj.dongting.raft.sm.StateMachine;

import java.util.ArrayList;
import java.util.List;

/**
* @author huangli
*/
public class Server {

protected static RaftServer startServer(int nodeId, String servers, String members,
String observers, int[] groupIds) {
RaftServerConfig serverConfig = new RaftServerConfig();
serverConfig.setServers(servers);
serverConfig.setNodeId(nodeId);
serverConfig.setReplicatePort(4000 + nodeId);
serverConfig.setServicePort(5000 + nodeId);
serverConfig.setElectTimeout(3000);
serverConfig.setHeartbeatInterval(1000);

List<RaftGroupConfig> groupConfigs = new ArrayList<>();
for (int groupId : groupIds) {
RaftGroupConfig groupConfig = RaftGroupConfig.newInstance(groupId, members, observers);
groupConfig.setDataDir("target/raft_data_" + groupId + "_node" + nodeId);
groupConfigs.add(groupConfig);
}

DefaultRaftFactory raftFactory = new DefaultRaftFactory() {
@Override
public StateMachine createStateMachine(RaftGroupConfigEx groupConfig) {
return new DtKV(groupConfig, new KvConfig());
}
};

RaftServer raftServer = new RaftServer(serverConfig, groupConfigs, raftFactory);
KvServerUtil.initKvServer(raftServer);

raftServer.start();
return raftServer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright The Dongting Project
*
* The Dongting Project 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:
*
* https://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 com.github.dtprj.dongting.demos.cluster;

/**
* @author huangli
*/
public class Server1 extends Server {
public static void main(String[] args) {
int nodeId = 1;
String servers = "1,127.0.0.1:4001;2,127.0.0.1:4002;3,127.0.0.1:4003";
String members = "1,2,3";
String observers = "";
int groupId = 0;
startServer(nodeId, servers, members, observers, new int[]{groupId});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright The Dongting Project
*
* The Dongting Project 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:
*
* https://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 com.github.dtprj.dongting.demos.cluster;

/**
* @author huangli
*/
public class Server2 extends Server {
public static void main(String[] args) {
int nodeId = 2;
String servers = "1,127.0.0.1:4001;2,127.0.0.1:4002;3,127.0.0.1:4003";
String members = "1,2,3";
String observers = "";
int groupId = 0;
startServer(nodeId, servers, members, observers, new int[]{groupId});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright The Dongting Project
*
* The Dongting Project 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:
*
* https://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 com.github.dtprj.dongting.demos.cluster;

/**
* @author huangli
*/
public class Server3 extends Server {
public static void main(String[] args) {
int nodeId = 3;
String servers = "1,127.0.0.1:4001;2,127.0.0.1:4002;3,127.0.0.1:4003";
String members = "1,2,3";
String observers = "";
int groupId = 0;
startServer(nodeId, servers, members, observers, new int[]{groupId});
}
}
15 changes: 15 additions & 0 deletions demos/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{mm:ss.SSS} [%thread] %-5level %logger{0} : %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
</filter>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
<logger name="com.github.dtprj.dongting.net" level="INFO"/>
</configuration>
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<module>server</module>
<module>report</module>
<module>benchmark</module>
<module>demos</module>
</modules>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void test() throws Exception {
waitStart(s1);
DtTime timeout = new DtTime(5, TimeUnit.SECONDS);

KvClient client = new KvClient(1, "1, 127.0.0.1:5001");
KvClient client = new KvClient("1, 127.0.0.1:5001", 1);
client.start();
client.mkdir(1, "dir1", timeout).get();
client.put(1, "dir1.k1", "v1".getBytes(), timeout).get(1, TimeUnit.SECONDS);
Expand Down

0 comments on commit 7f0f9ba

Please sign in to comment.