From ccc31f0894d4c517e76cfa931fc213685dc2ac6f Mon Sep 17 00:00:00 2001 From: kairbon Date: Tue, 24 Dec 2019 19:11:34 +0800 Subject: [PATCH 01/12] move DstServer --- scripts/run_dst_server.sh | 2 +- server/pom.xml | 2 +- .../java/com/distkv/dst/server/DstServer.java | 101 ++++++++++++++++++ .../distkv/dst/server/DstServerConfig.java | 62 +++++++++++ .../com/distkv/dst/server/ProcessInfo.java | 18 ++++ .../distkv/dst/server/service/DstServer.java | 95 ---------------- server/src/main/resources/logback.xml | 2 +- .../distkv/dst/test/supplier/TestUtil.java | 2 +- 8 files changed, 185 insertions(+), 99 deletions(-) create mode 100644 server/src/main/java/com/distkv/dst/server/DstServer.java create mode 100644 server/src/main/java/com/distkv/dst/server/DstServerConfig.java create mode 100644 server/src/main/java/com/distkv/dst/server/ProcessInfo.java delete mode 100644 server/src/main/java/com/distkv/dst/server/service/DstServer.java diff --git a/scripts/run_dst_server.sh b/scripts/run_dst_server.sh index 0902edd72..08d92d57f 100644 --- a/scripts/run_dst_server.sh +++ b/scripts/run_dst_server.sh @@ -12,4 +12,4 @@ fi SCRIPT_DIR=$(dirname $SCRIPT_ABS) -java -classpath $SCRIPT_DIR/dst-server-0.1.2-SNAPSHOT-jar-with-dependencies.jar com.distkv.dst.server.service.DstServer +java -classpath $SCRIPT_DIR/dst-server-0.1.2-SNAPSHOT-jar-with-dependencies.jar com.distkv.dst.server.DstServer diff --git a/server/pom.xml b/server/pom.xml index ef4cd1087..0cc3922d5 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -26,7 +26,7 @@ - com.distkv.dst.server.service.DstServer + com.distkv.dst.server.DstServer diff --git a/server/src/main/java/com/distkv/dst/server/DstServer.java b/server/src/main/java/com/distkv/dst/server/DstServer.java new file mode 100644 index 000000000..af46c4f4b --- /dev/null +++ b/server/src/main/java/com/distkv/dst/server/DstServer.java @@ -0,0 +1,101 @@ +package com.distkv.dst.server; + +import com.distkv.drpc.DrpcServer; +import com.distkv.dst.rpc.service.DstDictService; +import com.distkv.dst.rpc.service.DstListService; +import com.distkv.dst.rpc.service.DstSetService; +import com.distkv.dst.rpc.service.DstSortedListService; +import com.distkv.dst.rpc.service.DstStringService; +import com.distkv.dst.server.runtime.DstRuntime; +import com.distkv.dst.server.service.DstDictServiceImpl; +import com.distkv.dst.server.service.DstListServiceImpl; +import com.distkv.dst.server.service.DstSetServiceImpl; +import com.distkv.dst.server.service.DstSortedListServiceImpl; +import com.distkv.dst.server.service.DstStringServiceImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DstServer { + + private static final Logger LOGGER = LoggerFactory.getLogger(DstServer.class); + + private boolean isMaster; + + private DrpcServer drpcServer; + + private DstRuntime runtime; + + public static int listeningPort = 8082; + + private static String WELCOME_WORDS = + " \n" + + " \n" + + " ,---, ___ \n" + + " .' .' `\\ ,--.'|_ \n" + + ",---.' \\ | | :,' \n" + + "| | .`\\ | .--.--. : : ' : \n" + + ": : | ' | / / '.;__,' / \n" + + "| ' ' ; :| : /`./| | | \n" + + "' | ; . || : ;_ :__,'| : \n" + + "| | : | ' \\ \\ `. ' : |__ \n" + + "' : | / ; `----. \\| | '.'| \n" + + "| | '` ,/ / /`--' /; : ; \n" + + "; : .' '--'. / | , / \n" + + "| ,.' `--'---' ---`-' \n" + + "'---' \n" + + " "; + + public DstServer(DstServerConfig config) { + drpcServer = new DrpcServer(config.genRpcConfig()); + listeningPort = config.getPort(); + isMaster = config.isMaster(); + runtime = new DstRuntime(); + initRpc(); + } + + public void run() { + drpcServer.run(); + LOGGER.error("Succeeded to start dst server on port {}.", listeningPort); + synchronized (DstServer.class) { + try { + DstServer.class.wait(); + } catch (Throwable e) { + LOGGER.error("Failed with the exception: {}", e.toString()); + System.exit(-1); + } + } + } + + private void initRpc() { + drpcServer.registerService( + DstStringService.class, new DstStringServiceImpl(this.runtime)); + drpcServer.registerService( + DstListService.class, new DstListServiceImpl(this.runtime)); + drpcServer.registerService( + DstSetService.class, new DstSetServiceImpl(this.runtime)); + drpcServer.registerService( + DstDictService.class, new DstDictServiceImpl(this.runtime)); + drpcServer.registerService( + DstSortedListService.class, new DstSortedListServiceImpl(this.runtime)); + } + + public static void main(String[] args) { + if (args.length == 1) { + try { + listeningPort = Integer.valueOf(args[0]); + } catch (NumberFormatException e) { + LOGGER.error("Failed to start dst server, because the port is incorrect format: {}", + args[0]); + System.exit(0); + } + } + DstServerConfig.DstServerConfigBuilder builder = DstServerConfig.builder(); + DstServerConfig config = builder + .isMaster(true) + .port(listeningPort) + .build(); + DstServer dstServer = new DstServer(config); + System.out.println(WELCOME_WORDS); + dstServer.run(); + } +} diff --git a/server/src/main/java/com/distkv/dst/server/DstServerConfig.java b/server/src/main/java/com/distkv/dst/server/DstServerConfig.java new file mode 100644 index 000000000..9b2902301 --- /dev/null +++ b/server/src/main/java/com/distkv/dst/server/DstServerConfig.java @@ -0,0 +1,62 @@ +package com.distkv.dst.server; + +import com.distkv.drpc.config.ServerConfig; + +public class DstServerConfig { + private int listeningPort; + private int workerThreadNum; + private boolean isMaster; + + public boolean isMaster() { + return isMaster; + } + + public ServerConfig genRpcConfig() { + ServerConfig serverConfig = ServerConfig.builder() + .port(listeningPort) + .workerThreadNum(workerThreadNum) + .build(); + return serverConfig; + } + + public int getPort() { + return listeningPort; + } + + DstServerConfig(int listeningPort, int workerThreadNum, boolean isMaster) { + this.listeningPort = listeningPort; + this.isMaster = isMaster; + this.workerThreadNum = workerThreadNum; + } + + public static DstServerConfig.DstServerConfigBuilder builder() { + return new DstServerConfig.DstServerConfigBuilder(); + } + + public static class DstServerConfigBuilder { + private int listeningPort; + private int workerThreadNum; + private boolean isMaster; + + public DstServerConfig.DstServerConfigBuilder port(int port) { + this.listeningPort = port; + return this; + } + + public DstServerConfig.DstServerConfigBuilder workerThreadNum(int num) { + this.workerThreadNum = num; + return this; + } + + public DstServerConfig.DstServerConfigBuilder isMaster(boolean isMaster) { + this.isMaster = isMaster; + return this; + } + + public DstServerConfig build() { + return new DstServerConfig(listeningPort, workerThreadNum, isMaster); + } + } +} + + diff --git a/server/src/main/java/com/distkv/dst/server/ProcessInfo.java b/server/src/main/java/com/distkv/dst/server/ProcessInfo.java new file mode 100644 index 000000000..325e5f266 --- /dev/null +++ b/server/src/main/java/com/distkv/dst/server/ProcessInfo.java @@ -0,0 +1,18 @@ +package com.distkv.dst.server; + +public class ProcessInfo { + + private boolean isMaster; + + public static ProcessInfo processInfo = new ProcessInfo(); + + private ProcessInfo() {} + + public boolean isMaster() { + return isMaster; + } + + public void setMaster(boolean master) { + isMaster = master; + } +} diff --git a/server/src/main/java/com/distkv/dst/server/service/DstServer.java b/server/src/main/java/com/distkv/dst/server/service/DstServer.java deleted file mode 100644 index 757c210ca..000000000 --- a/server/src/main/java/com/distkv/dst/server/service/DstServer.java +++ /dev/null @@ -1,95 +0,0 @@ -package com.distkv.dst.server.service; - -import com.distkv.drpc.DrpcServer; -import com.distkv.drpc.config.ServerConfig; -import com.distkv.dst.rpc.service.DstDictService; -import com.distkv.dst.rpc.service.DstListService; -import com.distkv.dst.rpc.service.DstStringService; -import com.distkv.dst.rpc.service.DstSetService; -import com.distkv.dst.rpc.service.DstSortedListService; -import com.distkv.dst.server.runtime.DstRuntime; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class DstServer { - - private static final Logger LOGGER = LoggerFactory.getLogger(DstServer.class); - - private static final int LISTENING_PORT = 8082; - - - private DstRuntime runtime; - - private DstServer() { - runtime = new DstRuntime(); - } - - private static String WELCOME_WORDS = - " \n" + - " \n" + - " ,---, ___ \n" + - " .' .' `\\ ,--.'|_ \n" + - ",---.' \\ | | :,' \n" + - "| | .`\\ | .--.--. : : ' : \n" + - ": : | ' | / / '.;__,' / \n" + - "| ' ' ; :| : /`./| | | \n" + - "' | ; . || : ;_ :__,'| : \n" + - "| | : | ' \\ \\ `. ' : |__ \n" + - "' : | / ; `----. \\| | '.'| \n" + - "| | '` ,/ / /`--' /; : ; \n" + - "; : .' '--'. / | , / \n" + - "| ,.' `--'---' ---`-' \n" + - "'---' \n" + - " "; - - public static void main(String[] args) { - - DstServer server = new DstServer(); - - int listeningPort = LISTENING_PORT; - - if (args.length == 1) { - try { - listeningPort = Integer.valueOf(args[0]); - } catch (NumberFormatException e) { - LOGGER.error("Failed to start dst server, because the port is incorrect format: {}", - args[0]); - System.exit(0); - } - } - - ServerConfig serverConfig = ServerConfig.builder() - .port(listeningPort) - .build(); - - DrpcServer drpcServer = new DrpcServer(serverConfig); - drpcServer.registerService( - DstStringService.class, new DstStringServiceImpl(server.runtime)); - drpcServer.registerService( - DstListService.class, new DstListServiceImpl(server.runtime)); - drpcServer.registerService( - DstSetService.class, new DstSetServiceImpl(server.runtime)); - drpcServer.registerService( - DstDictService.class, new DstDictServiceImpl(server.runtime)); - drpcServer.registerService( - DstSortedListService.class, new DstSortedListServiceImpl(server.runtime)); - - drpcServer.run(); - - LOGGER.info("Succeeded to start dst server on port {}.", listeningPort); - - // Print welcome words. - System.out.println(WELCOME_WORDS); - - // Run Dst server. - synchronized (DstServer.class) { - try { - DstServer.class.wait(); - } catch (Throwable e) { - LOGGER.error("Failed with the exception: {}", e.toString()); - System.exit(-1); - } - } - - } -} diff --git a/server/src/main/resources/logback.xml b/server/src/main/resources/logback.xml index e4c832ce2..9089c2ed3 100644 --- a/server/src/main/resources/logback.xml +++ b/server/src/main/resources/logback.xml @@ -30,7 +30,7 @@ 120 - WARN + info diff --git a/test/src/test/java/com/distkv/dst/test/supplier/TestUtil.java b/test/src/test/java/com/distkv/dst/test/supplier/TestUtil.java index 7e84e6573..e955cfcae 100644 --- a/test/src/test/java/com/distkv/dst/test/supplier/TestUtil.java +++ b/test/src/test/java/com/distkv/dst/test/supplier/TestUtil.java @@ -50,7 +50,7 @@ public static void startRpcServer(int serverPort) { "java", "-classpath", jarDir, - "com.distkv.dst.server.service.DstServer", + "com.distkv.dst.server.DstServer", String.valueOf(serverPort) ); executeCommand(startCommand); From 88c7e875560fd9de4d9c1129c644a8cc03a59d60 Mon Sep 17 00:00:00 2001 From: kairbon Date: Wed, 25 Dec 2019 00:03:13 +0800 Subject: [PATCH 02/12] fix --- server/pom.xml | 7 ++++++- .../src/main/java/com/distkv/dst/server/DstServer.java | 10 +++++++++- server/src/main/resources/dst_server.conf | 8 ++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 server/src/main/resources/dst_server.conf diff --git a/server/pom.xml b/server/pom.xml index 0cc3922d5..86b21f785 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -14,6 +14,7 @@ 1.2.3 + 1.4.0 @@ -66,6 +67,10 @@ 2.6 compile - + + com.typesafe + config + ${typesafe.version} + diff --git a/server/src/main/java/com/distkv/dst/server/DstServer.java b/server/src/main/java/com/distkv/dst/server/DstServer.java index af46c4f4b..f330cf75b 100644 --- a/server/src/main/java/com/distkv/dst/server/DstServer.java +++ b/server/src/main/java/com/distkv/dst/server/DstServer.java @@ -12,6 +12,8 @@ import com.distkv.dst.server.service.DstSetServiceImpl; import com.distkv.dst.server.service.DstSortedListServiceImpl; import com.distkv.dst.server.service.DstStringServiceImpl; +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -80,6 +82,11 @@ private void initRpc() { } public static void main(String[] args) { + Config conf = ConfigFactory.load("dst_server"); + listeningPort = conf.getInt("DstServer.listeningPort"); + boolean isMaster = conf.getBoolean("DstServer.isMaster"); + int shardNum = conf.getInt("DstServer.shardNum"); + if (args.length == 1) { try { listeningPort = Integer.valueOf(args[0]); @@ -89,9 +96,10 @@ public static void main(String[] args) { System.exit(0); } } + DstServerConfig.DstServerConfigBuilder builder = DstServerConfig.builder(); DstServerConfig config = builder - .isMaster(true) + .isMaster(isMaster) .port(listeningPort) .build(); DstServer dstServer = new DstServer(config); diff --git a/server/src/main/resources/dst_server.conf b/server/src/main/resources/dst_server.conf new file mode 100644 index 000000000..87c87ba51 --- /dev/null +++ b/server/src/main/resources/dst_server.conf @@ -0,0 +1,8 @@ +# Here is the server port that Dst-Server listens on. +DstServer.listeningPort = 10086 + +# This configures whether the current process is the master in Dst partation. +DstServer.isMaster = true + +# Change how many shards Dst-Server has +DstServer.shardNum = 8 \ No newline at end of file From 01ab4f5120d04b64dc7e5fe3cfa3a74dc9fe177e Mon Sep 17 00:00:00 2001 From: kairbon Date: Wed, 25 Dec 2019 23:01:47 +0800 Subject: [PATCH 03/12] fix --- .../java/com/distkv/dst/server/DstServer.java | 1 + .../distkv/dst/server/DstServerConfig.java | 23 +++++++++++++++++-- .../server/runtime/workerpool/WorkerPool.java | 11 +++++---- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/server/src/main/java/com/distkv/dst/server/DstServer.java b/server/src/main/java/com/distkv/dst/server/DstServer.java index f330cf75b..31dea35dc 100644 --- a/server/src/main/java/com/distkv/dst/server/DstServer.java +++ b/server/src/main/java/com/distkv/dst/server/DstServer.java @@ -101,6 +101,7 @@ public static void main(String[] args) { DstServerConfig config = builder .isMaster(isMaster) .port(listeningPort) + .shardNum(shardNum) .build(); DstServer dstServer = new DstServer(config); System.out.println(WELCOME_WORDS); diff --git a/server/src/main/java/com/distkv/dst/server/DstServerConfig.java b/server/src/main/java/com/distkv/dst/server/DstServerConfig.java index 9b2902301..c100b86bb 100644 --- a/server/src/main/java/com/distkv/dst/server/DstServerConfig.java +++ b/server/src/main/java/com/distkv/dst/server/DstServerConfig.java @@ -6,6 +6,7 @@ public class DstServerConfig { private int listeningPort; private int workerThreadNum; private boolean isMaster; + private int shardNum; public boolean isMaster() { return isMaster; @@ -23,10 +24,18 @@ public int getPort() { return listeningPort; } - DstServerConfig(int listeningPort, int workerThreadNum, boolean isMaster) { + public int getShardNum() { + return shardNum; + } + + DstServerConfig(int listeningPort, + int workerThreadNum, + boolean isMaster, + int shardNum) { this.listeningPort = listeningPort; this.isMaster = isMaster; this.workerThreadNum = workerThreadNum; + this.shardNum = shardNum; } public static DstServerConfig.DstServerConfigBuilder builder() { @@ -37,6 +46,7 @@ public static class DstServerConfigBuilder { private int listeningPort; private int workerThreadNum; private boolean isMaster; + private int shardNum; public DstServerConfig.DstServerConfigBuilder port(int port) { this.listeningPort = port; @@ -48,13 +58,22 @@ public DstServerConfig.DstServerConfigBuilder workerThreadNum(int num) { return this; } + public DstServerConfig.DstServerConfigBuilder shardNum(int num) { + this.shardNum = num; + return this; + } + public DstServerConfig.DstServerConfigBuilder isMaster(boolean isMaster) { this.isMaster = isMaster; return this; } public DstServerConfig build() { - return new DstServerConfig(listeningPort, workerThreadNum, isMaster); + return new DstServerConfig( + listeningPort, + workerThreadNum, + isMaster, + shardNum); } } } diff --git a/server/src/main/java/com/distkv/dst/server/runtime/workerpool/WorkerPool.java b/server/src/main/java/com/distkv/dst/server/runtime/workerpool/WorkerPool.java index 3a2332cb3..ec2161259 100644 --- a/server/src/main/java/com/distkv/dst/server/runtime/workerpool/WorkerPool.java +++ b/server/src/main/java/com/distkv/dst/server/runtime/workerpool/WorkerPool.java @@ -1,6 +1,7 @@ package com.distkv.dst.server.runtime.workerpool; import com.distkv.dst.common.RequestTypeEnum; +import com.distkv.dst.server.DstServerConfig; import com.google.common.collect.ImmutableList; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -9,14 +10,14 @@ public class WorkerPool { private static final Logger LOGGER = LoggerFactory.getLogger(WorkerPool.class); - private final int shardNum; + private DstServerConfig config; private final ImmutableList workers; - public WorkerPool(int shardNum) { - this.shardNum = shardNum; + public WorkerPool(DstServerConfig config) { + this.config = config; ImmutableList.Builder builder = new ImmutableList.Builder<>(); - for (int i = 0; i < shardNum; ++i) { + for (int i = 0; i < config.getShardNum(); ++i) { Worker worker = new Worker(); builder.add(worker); worker.start(); @@ -26,7 +27,7 @@ public WorkerPool(int shardNum) { public void postRequest( String key, RequestTypeEnum requestType, Object request, Object completableFuture) { - final int workerIndex = Math.abs(key.hashCode()) % shardNum; + final int workerIndex = Math.abs(key.hashCode()) % config.getShardNum(); Worker worker = workers.get(workerIndex); try { worker.post(new InternalRequest(requestType, request, completableFuture)); From 06eb5ad9d6ce6193d24142153b90fffac60be7ef Mon Sep 17 00:00:00 2001 From: kairbon Date: Wed, 25 Dec 2019 23:16:25 +0800 Subject: [PATCH 04/12] fix --- .../main/java/com/distkv/dst/server/DstServer.java | 2 +- .../com/distkv/dst/server/runtime/DstRuntime.java | 8 +++++--- .../dst/server/runtime/workerpool/WorkerPool.java | 14 ++++++++------ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/server/src/main/java/com/distkv/dst/server/DstServer.java b/server/src/main/java/com/distkv/dst/server/DstServer.java index 31dea35dc..5d44007f9 100644 --- a/server/src/main/java/com/distkv/dst/server/DstServer.java +++ b/server/src/main/java/com/distkv/dst/server/DstServer.java @@ -51,7 +51,7 @@ public DstServer(DstServerConfig config) { drpcServer = new DrpcServer(config.genRpcConfig()); listeningPort = config.getPort(); isMaster = config.isMaster(); - runtime = new DstRuntime(); + runtime = new DstRuntime(config); initRpc(); } diff --git a/server/src/main/java/com/distkv/dst/server/runtime/DstRuntime.java b/server/src/main/java/com/distkv/dst/server/runtime/DstRuntime.java index 3a607d13d..30c77f649 100644 --- a/server/src/main/java/com/distkv/dst/server/runtime/DstRuntime.java +++ b/server/src/main/java/com/distkv/dst/server/runtime/DstRuntime.java @@ -1,15 +1,17 @@ package com.distkv.dst.server.runtime; +import com.distkv.dst.server.DstServerConfig; import com.distkv.dst.server.runtime.workerpool.WorkerPool; public class DstRuntime { - private static final int shardNum = 8; + private DstServerConfig config; private WorkerPool workerPool; - public DstRuntime() { - workerPool = new WorkerPool(shardNum); + public DstRuntime(DstServerConfig config) { + this.config = config; + workerPool = new WorkerPool(config.getShardNum(), config.isMaster()); } public WorkerPool getWorkerPool() { diff --git a/server/src/main/java/com/distkv/dst/server/runtime/workerpool/WorkerPool.java b/server/src/main/java/com/distkv/dst/server/runtime/workerpool/WorkerPool.java index ec2161259..ea0fbc289 100644 --- a/server/src/main/java/com/distkv/dst/server/runtime/workerpool/WorkerPool.java +++ b/server/src/main/java/com/distkv/dst/server/runtime/workerpool/WorkerPool.java @@ -1,7 +1,6 @@ package com.distkv.dst.server.runtime.workerpool; import com.distkv.dst.common.RequestTypeEnum; -import com.distkv.dst.server.DstServerConfig; import com.google.common.collect.ImmutableList; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -10,14 +9,17 @@ public class WorkerPool { private static final Logger LOGGER = LoggerFactory.getLogger(WorkerPool.class); - private DstServerConfig config; + private final int shardNum; + + private boolean isMaster; private final ImmutableList workers; - public WorkerPool(DstServerConfig config) { - this.config = config; + public WorkerPool(int shardNum, boolean isMaster) { + this.shardNum = shardNum; + this.isMaster = isMaster; ImmutableList.Builder builder = new ImmutableList.Builder<>(); - for (int i = 0; i < config.getShardNum(); ++i) { + for (int i = 0; i < shardNum; ++i) { Worker worker = new Worker(); builder.add(worker); worker.start(); @@ -27,7 +29,7 @@ public WorkerPool(DstServerConfig config) { public void postRequest( String key, RequestTypeEnum requestType, Object request, Object completableFuture) { - final int workerIndex = Math.abs(key.hashCode()) % config.getShardNum(); + final int workerIndex = Math.abs(key.hashCode()) % shardNum; Worker worker = workers.get(workerIndex); try { worker.post(new InternalRequest(requestType, request, completableFuture)); From a439c7f0537a08766d662c87217a28d653270c76 Mon Sep 17 00:00:00 2001 From: kairbon Date: Wed, 25 Dec 2019 23:18:00 +0800 Subject: [PATCH 05/12] fix --- .../com/distkv/dst/server/ProcessInfo.java | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 server/src/main/java/com/distkv/dst/server/ProcessInfo.java diff --git a/server/src/main/java/com/distkv/dst/server/ProcessInfo.java b/server/src/main/java/com/distkv/dst/server/ProcessInfo.java deleted file mode 100644 index 325e5f266..000000000 --- a/server/src/main/java/com/distkv/dst/server/ProcessInfo.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.distkv.dst.server; - -public class ProcessInfo { - - private boolean isMaster; - - public static ProcessInfo processInfo = new ProcessInfo(); - - private ProcessInfo() {} - - public boolean isMaster() { - return isMaster; - } - - public void setMaster(boolean master) { - isMaster = master; - } -} From 71be38d80b81c594bd1d7e188eed262372476c31 Mon Sep 17 00:00:00 2001 From: kairbon Date: Wed, 25 Dec 2019 23:27:45 +0800 Subject: [PATCH 06/12] fix --- .../com/distkv/dst/test/core/operator/KVSSortedListTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/src/test/java/com/distkv/dst/test/core/operator/KVSSortedListTest.java b/test/src/test/java/com/distkv/dst/test/core/operator/KVSSortedListTest.java index 56edef6fe..46930312c 100644 --- a/test/src/test/java/com/distkv/dst/test/core/operator/KVSSortedListTest.java +++ b/test/src/test/java/com/distkv/dst/test/core/operator/KVSSortedListTest.java @@ -9,7 +9,7 @@ import java.util.LinkedList; import java.util.List; -public class KVSSortedListTest extends BaseTestSupplier { +public class KVSSortedListTest { @Test public void testSortedList() throws InterruptedException { KVStore store = new KVStoreImpl(); From 158dafaded969370cf206a9346882ccd429edfb8 Mon Sep 17 00:00:00 2001 From: kairbon Date: Wed, 25 Dec 2019 23:29:39 +0800 Subject: [PATCH 07/12] fix --- .../com/distkv/dst/test/core/operator/KVSSortedListTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/test/src/test/java/com/distkv/dst/test/core/operator/KVSSortedListTest.java b/test/src/test/java/com/distkv/dst/test/core/operator/KVSSortedListTest.java index 46930312c..60499d2a8 100644 --- a/test/src/test/java/com/distkv/dst/test/core/operator/KVSSortedListTest.java +++ b/test/src/test/java/com/distkv/dst/test/core/operator/KVSSortedListTest.java @@ -3,7 +3,6 @@ import com.distkv.dst.core.KVStore; import com.distkv.dst.core.KVStoreImpl; import com.distkv.dst.common.entity.sortedList.SortedListEntity; -import com.distkv.dst.test.supplier.BaseTestSupplier; import org.testng.Assert; import org.testng.annotations.Test; import java.util.LinkedList; From 8493d6aff22f910e2eab614248fd17ba96a7b37a Mon Sep 17 00:00:00 2001 From: kairbon Date: Sat, 28 Dec 2019 00:27:10 +0800 Subject: [PATCH 08/12] fix --- .../java/com/distkv/dst/server/DstServer.java | 27 ++---- .../distkv/dst/server/DstServerConfig.java | 93 +++++++++---------- server/src/main/resources/Dst.default.conf | 8 ++ server/src/main/resources/dst_server.conf | 8 -- 4 files changed, 60 insertions(+), 76 deletions(-) create mode 100644 server/src/main/resources/Dst.default.conf delete mode 100644 server/src/main/resources/dst_server.conf diff --git a/server/src/main/java/com/distkv/dst/server/DstServer.java b/server/src/main/java/com/distkv/dst/server/DstServer.java index 5d44007f9..0343bd34a 100644 --- a/server/src/main/java/com/distkv/dst/server/DstServer.java +++ b/server/src/main/java/com/distkv/dst/server/DstServer.java @@ -21,13 +21,11 @@ public class DstServer { private static final Logger LOGGER = LoggerFactory.getLogger(DstServer.class); - private boolean isMaster; - private DrpcServer drpcServer; private DstRuntime runtime; - public static int listeningPort = 8082; + private DstServerConfig config; private static String WELCOME_WORDS = " \n" + @@ -49,15 +47,13 @@ public class DstServer { public DstServer(DstServerConfig config) { drpcServer = new DrpcServer(config.genRpcConfig()); - listeningPort = config.getPort(); - isMaster = config.isMaster(); runtime = new DstRuntime(config); - initRpc(); + registerAllRpcServices(); } public void run() { drpcServer.run(); - LOGGER.error("Succeeded to start dst server on port {}.", listeningPort); + LOGGER.error("Succeeded to start dst server on port {}.", config.getPort()); synchronized (DstServer.class) { try { DstServer.class.wait(); @@ -68,7 +64,7 @@ public void run() { } } - private void initRpc() { + private void registerAllRpcServices() { drpcServer.registerService( DstStringService.class, new DstStringServiceImpl(this.runtime)); drpcServer.registerService( @@ -82,11 +78,8 @@ private void initRpc() { } public static void main(String[] args) { - Config conf = ConfigFactory.load("dst_server"); - listeningPort = conf.getInt("DstServer.listeningPort"); - boolean isMaster = conf.getBoolean("DstServer.isMaster"); - int shardNum = conf.getInt("DstServer.shardNum"); + int listeningPort = -1; if (args.length == 1) { try { listeningPort = Integer.valueOf(args[0]); @@ -97,12 +90,10 @@ public static void main(String[] args) { } } - DstServerConfig.DstServerConfigBuilder builder = DstServerConfig.builder(); - DstServerConfig config = builder - .isMaster(isMaster) - .port(listeningPort) - .shardNum(shardNum) - .build(); + DstServerConfig config = DstServerConfig.create(); + if (listeningPort != -1) { + config.setPort(listeningPort); + } DstServer dstServer = new DstServer(config); System.out.println(WELCOME_WORDS); dstServer.run(); diff --git a/server/src/main/java/com/distkv/dst/server/DstServerConfig.java b/server/src/main/java/com/distkv/dst/server/DstServerConfig.java index c100b86bb..a76887f03 100644 --- a/server/src/main/java/com/distkv/dst/server/DstServerConfig.java +++ b/server/src/main/java/com/distkv/dst/server/DstServerConfig.java @@ -1,10 +1,21 @@ package com.distkv.dst.server; import com.distkv.drpc.config.ServerConfig; +import com.google.common.base.Strings; +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; public class DstServerConfig { + private static final Logger LOGGER = LoggerFactory.getLogger(DstServer.class); + + public static final String CUSTOM_CONFIG_FILE = "Dst.conf"; + public static final String DEFAULT_CONFIG_FILE = "Dst.default.conf"; + private int listeningPort; - private int workerThreadNum; private boolean isMaster; private int shardNum; @@ -12,69 +23,51 @@ public boolean isMaster() { return isMaster; } - public ServerConfig genRpcConfig() { - ServerConfig serverConfig = ServerConfig.builder() - .port(listeningPort) - .workerThreadNum(workerThreadNum) - .build(); - return serverConfig; + public int getPort() { + return listeningPort; } - public int getPort() { - return listeningPort; + public void setPort(int port) { + listeningPort = port; } public int getShardNum() { return shardNum; } - DstServerConfig(int listeningPort, - int workerThreadNum, - boolean isMaster, - int shardNum) { - this.listeningPort = listeningPort; - this.isMaster = isMaster; - this.workerThreadNum = workerThreadNum; - this.shardNum = shardNum; + public ServerConfig genRpcConfig() { + ServerConfig serverConfig = ServerConfig.builder() + .port(listeningPort) + .build(); + return serverConfig; } - public static DstServerConfig.DstServerConfigBuilder builder() { - return new DstServerConfig.DstServerConfigBuilder(); + public DstServerConfig(Config config) { + listeningPort = config.getInt("store.listeningPort"); + isMaster = config.getBoolean("store.isMaster"); + shardNum = config.getInt("store.shardNum"); } - public static class DstServerConfigBuilder { - private int listeningPort; - private int workerThreadNum; - private boolean isMaster; - private int shardNum; - - public DstServerConfig.DstServerConfigBuilder port(int port) { - this.listeningPort = port; - return this; - } - - public DstServerConfig.DstServerConfigBuilder workerThreadNum(int num) { - this.workerThreadNum = num; - return this; - } - - public DstServerConfig.DstServerConfigBuilder shardNum(int num) { - this.shardNum = num; - return this; - } - - public DstServerConfig.DstServerConfigBuilder isMaster(boolean isMaster) { - this.isMaster = isMaster; - return this; - } + @Override + public String toString() { + return "listeningPort: " + listeningPort + ";\n" + + "isMaster: " + isMaster + ";\n" + + "shardNum: " + shardNum + ";\n"; + } - public DstServerConfig build() { - return new DstServerConfig( - listeningPort, - workerThreadNum, - isMaster, - shardNum); + public static DstServerConfig create() { + ConfigFactory.invalidateCaches(); + Config config = ConfigFactory.systemProperties(); + String configPath = System.getProperty("Dst.config"); + if (Strings.isNullOrEmpty(configPath)) { + LOGGER.info("Loading config from \"Dst.conf\" file in classpath."); + config = config.withFallback(ConfigFactory.load(CUSTOM_CONFIG_FILE)); + } else { + LOGGER.info("Loading config from " + configPath + "."); + config = config.withFallback(ConfigFactory.parseFile(new File(configPath))); } + config = config.withFallback(ConfigFactory.load(DEFAULT_CONFIG_FILE)); + return new DstServerConfig(config); } } diff --git a/server/src/main/resources/Dst.default.conf b/server/src/main/resources/Dst.default.conf new file mode 100644 index 000000000..388f36eab --- /dev/null +++ b/server/src/main/resources/Dst.default.conf @@ -0,0 +1,8 @@ +store { + # Here is the server port that Dst-Server listens on. + listeningPort: 10086 + # This configures whether the current process is the master in Dst partation. + isMaster: true + # Change how many shards Dst-Server has + shardNum: 8 +} \ No newline at end of file diff --git a/server/src/main/resources/dst_server.conf b/server/src/main/resources/dst_server.conf deleted file mode 100644 index 87c87ba51..000000000 --- a/server/src/main/resources/dst_server.conf +++ /dev/null @@ -1,8 +0,0 @@ -# Here is the server port that Dst-Server listens on. -DstServer.listeningPort = 10086 - -# This configures whether the current process is the master in Dst partation. -DstServer.isMaster = true - -# Change how many shards Dst-Server has -DstServer.shardNum = 8 \ No newline at end of file From b538fffb2d736616a5d456c33de822787e91c5f8 Mon Sep 17 00:00:00 2001 From: kairbon Date: Sat, 28 Dec 2019 00:42:56 +0800 Subject: [PATCH 09/12] fix --- server/src/main/java/com/distkv/dst/server/DstServer.java | 7 +++---- server/src/main/resources/Dst.default.conf | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/com/distkv/dst/server/DstServer.java b/server/src/main/java/com/distkv/dst/server/DstServer.java index 0343bd34a..5b493854f 100644 --- a/server/src/main/java/com/distkv/dst/server/DstServer.java +++ b/server/src/main/java/com/distkv/dst/server/DstServer.java @@ -12,8 +12,6 @@ import com.distkv.dst.server.service.DstSetServiceImpl; import com.distkv.dst.server.service.DstSortedListServiceImpl; import com.distkv.dst.server.service.DstStringServiceImpl; -import com.typesafe.config.Config; -import com.typesafe.config.ConfigFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -46,9 +44,10 @@ public class DstServer { " "; public DstServer(DstServerConfig config) { + this.config = config; drpcServer = new DrpcServer(config.genRpcConfig()); - runtime = new DstRuntime(config); registerAllRpcServices(); + runtime = new DstRuntime(config); } public void run() { @@ -91,7 +90,7 @@ public static void main(String[] args) { } DstServerConfig config = DstServerConfig.create(); - if (listeningPort != -1) { + if (listeningPort < 0) { config.setPort(listeningPort); } DstServer dstServer = new DstServer(config); diff --git a/server/src/main/resources/Dst.default.conf b/server/src/main/resources/Dst.default.conf index 388f36eab..b4e6e4075 100644 --- a/server/src/main/resources/Dst.default.conf +++ b/server/src/main/resources/Dst.default.conf @@ -1,6 +1,6 @@ store { # Here is the server port that Dst-Server listens on. - listeningPort: 10086 + listeningPort: 8082 # This configures whether the current process is the master in Dst partation. isMaster: true # Change how many shards Dst-Server has From ed6f2fbbbe90b8d41d068d0eaa6cdf88278a6607 Mon Sep 17 00:00:00 2001 From: kairbon Date: Sat, 28 Dec 2019 10:20:40 +0800 Subject: [PATCH 10/12] fix --- server/src/main/java/com/distkv/dst/server/DstServer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/main/java/com/distkv/dst/server/DstServer.java b/server/src/main/java/com/distkv/dst/server/DstServer.java index 5b493854f..78e3101f9 100644 --- a/server/src/main/java/com/distkv/dst/server/DstServer.java +++ b/server/src/main/java/com/distkv/dst/server/DstServer.java @@ -90,7 +90,7 @@ public static void main(String[] args) { } DstServerConfig config = DstServerConfig.create(); - if (listeningPort < 0) { + if (listeningPort > 0) { config.setPort(listeningPort); } DstServer dstServer = new DstServer(config); From 4e886cec18b0aadd4b60764a79b29ef9f1288f1a Mon Sep 17 00:00:00 2001 From: kairbon Date: Sat, 28 Dec 2019 10:31:33 +0800 Subject: [PATCH 11/12] fix --- server/src/main/java/com/distkv/dst/server/DstServer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/main/java/com/distkv/dst/server/DstServer.java b/server/src/main/java/com/distkv/dst/server/DstServer.java index 78e3101f9..748d32442 100644 --- a/server/src/main/java/com/distkv/dst/server/DstServer.java +++ b/server/src/main/java/com/distkv/dst/server/DstServer.java @@ -46,8 +46,8 @@ public class DstServer { public DstServer(DstServerConfig config) { this.config = config; drpcServer = new DrpcServer(config.genRpcConfig()); - registerAllRpcServices(); runtime = new DstRuntime(config); + registerAllRpcServices(); } public void run() { From f3fac403c674409f8787695275be64588519e751 Mon Sep 17 00:00:00 2001 From: kairbon Date: Sat, 28 Dec 2019 22:50:27 +0800 Subject: [PATCH 12/12] fix --- .../java/com/distkv/dst/server/DstServer.java | 6 +++++- .../distkv/dst/server/DstServerConfig.java | 19 +++++-------------- .../{Dst.default.conf => dst.default.conf} | 2 +- 3 files changed, 11 insertions(+), 16 deletions(-) rename server/src/main/resources/{Dst.default.conf => dst.default.conf} (99%) diff --git a/server/src/main/java/com/distkv/dst/server/DstServer.java b/server/src/main/java/com/distkv/dst/server/DstServer.java index 748d32442..584b2c3b5 100644 --- a/server/src/main/java/com/distkv/dst/server/DstServer.java +++ b/server/src/main/java/com/distkv/dst/server/DstServer.java @@ -1,6 +1,7 @@ package com.distkv.dst.server; import com.distkv.drpc.DrpcServer; +import com.distkv.drpc.config.ServerConfig; import com.distkv.dst.rpc.service.DstDictService; import com.distkv.dst.rpc.service.DstListService; import com.distkv.dst.rpc.service.DstSetService; @@ -45,7 +46,10 @@ public class DstServer { public DstServer(DstServerConfig config) { this.config = config; - drpcServer = new DrpcServer(config.genRpcConfig()); + ServerConfig config1 = ServerConfig.builder() + .port(config.getPort()) + .build(); + drpcServer = new DrpcServer(config1); runtime = new DstRuntime(config); registerAllRpcServices(); } diff --git a/server/src/main/java/com/distkv/dst/server/DstServerConfig.java b/server/src/main/java/com/distkv/dst/server/DstServerConfig.java index a76887f03..ff0e6f6c2 100644 --- a/server/src/main/java/com/distkv/dst/server/DstServerConfig.java +++ b/server/src/main/java/com/distkv/dst/server/DstServerConfig.java @@ -1,19 +1,17 @@ package com.distkv.dst.server; -import com.distkv.drpc.config.ServerConfig; import com.google.common.base.Strings; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import java.io.File; public class DstServerConfig { - private static final Logger LOGGER = LoggerFactory.getLogger(DstServer.class); + private static final Logger LOGGER = LoggerFactory.getLogger(DstServerConfig.class); - public static final String CUSTOM_CONFIG_FILE = "Dst.conf"; - public static final String DEFAULT_CONFIG_FILE = "Dst.default.conf"; + public static final String CUSTOM_CONFIG_FILE = "dst.conf"; + public static final String DEFAULT_CONFIG_FILE = "dst.default.conf"; private int listeningPort; private boolean isMaster; @@ -35,13 +33,6 @@ public int getShardNum() { return shardNum; } - public ServerConfig genRpcConfig() { - ServerConfig serverConfig = ServerConfig.builder() - .port(listeningPort) - .build(); - return serverConfig; - } - public DstServerConfig(Config config) { listeningPort = config.getInt("store.listeningPort"); isMaster = config.getBoolean("store.isMaster"); @@ -58,9 +49,9 @@ public String toString() { public static DstServerConfig create() { ConfigFactory.invalidateCaches(); Config config = ConfigFactory.systemProperties(); - String configPath = System.getProperty("Dst.config"); + String configPath = System.getProperty("dst.config"); if (Strings.isNullOrEmpty(configPath)) { - LOGGER.info("Loading config from \"Dst.conf\" file in classpath."); + LOGGER.info("Loading config from \"dst.conf\" file in classpath."); config = config.withFallback(ConfigFactory.load(CUSTOM_CONFIG_FILE)); } else { LOGGER.info("Loading config from " + configPath + "."); diff --git a/server/src/main/resources/Dst.default.conf b/server/src/main/resources/dst.default.conf similarity index 99% rename from server/src/main/resources/Dst.default.conf rename to server/src/main/resources/dst.default.conf index b4e6e4075..4489e2c7e 100644 --- a/server/src/main/resources/Dst.default.conf +++ b/server/src/main/resources/dst.default.conf @@ -5,4 +5,4 @@ store { isMaster: true # Change how many shards Dst-Server has shardNum: 8 -} \ No newline at end of file +}