-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
288 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
56 changes: 56 additions & 0 deletions
56
demos/src/main/java/com/github/dtprj/dongting/demos/cluster/DemoClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
demos/src/main/java/com/github/dtprj/dongting/demos/cluster/Server.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
demos/src/main/java/com/github/dtprj/dongting/demos/cluster/Server1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
demos/src/main/java/com/github/dtprj/dongting/demos/cluster/Server2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
demos/src/main/java/com/github/dtprj/dongting/demos/cluster/Server3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters