Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration files and configuration classes #387

Merged
merged 15 commits into from
Dec 28, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/run_dst_server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 7 additions & 2 deletions server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<!--List the dependency versions of the project-->
<properties>
<logback.version>1.2.3</logback.version>
<typesafe.version>1.4.0</typesafe.version>
</properties>

<build>
Expand All @@ -26,7 +27,7 @@
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.distkv.dst.server.service.DstServer</mainClass>
<mainClass>com.distkv.dst.server.DstServer</mainClass>
</manifest>
</archive>
</configuration>
Expand Down Expand Up @@ -66,6 +67,10 @@
<version>2.6</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>${typesafe.version}</version>
</dependency>
</dependencies>
</project>
110 changes: 110 additions & 0 deletions server/src/main/java/com/distkv/dst/server/DstServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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 com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DstServer {

private static final Logger LOGGER = LoggerFactory.getLogger(DstServer.class);

private boolean isMaster;
kairbon marked this conversation as resolved.
Show resolved Hide resolved

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(config);
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() {
kairbon marked this conversation as resolved.
Show resolved Hide resolved
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) {
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]);
} 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(isMaster)
.port(listeningPort)
.shardNum(shardNum)
.build();
DstServer dstServer = new DstServer(config);
System.out.println(WELCOME_WORDS);
dstServer.run();
}
}
81 changes: 81 additions & 0 deletions server/src/main/java/com/distkv/dst/server/DstServerConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.distkv.dst.server;

import com.distkv.drpc.config.ServerConfig;

public class DstServerConfig {
private int listeningPort;
private int workerThreadNum;
private boolean isMaster;
private int shardNum;

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 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() {
return new DstServerConfig.DstServerConfigBuilder();
}

public static class DstServerConfigBuilder {
kairbon marked this conversation as resolved.
Show resolved Hide resolved
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;
}

public DstServerConfig build() {
return new DstServerConfig(
listeningPort,
workerThreadNum,
isMaster,
shardNum);
}
}
}


Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ public class WorkerPool {

private final int shardNum;

private boolean isMaster;

private final ImmutableList<Worker> workers;

public WorkerPool(int shardNum) {
public WorkerPool(int shardNum, boolean isMaster) {
this.shardNum = shardNum;
this.isMaster = isMaster;
ImmutableList.Builder<Worker> builder = new ImmutableList.Builder<>();
for (int i = 0; i < shardNum; ++i) {
Worker worker = new Worker();
Expand Down
95 changes: 0 additions & 95 deletions server/src/main/java/com/distkv/dst/server/service/DstServer.java

This file was deleted.

8 changes: 8 additions & 0 deletions server/src/main/resources/dst_server.conf
Original file line number Diff line number Diff line change
@@ -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
kairbon marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion server/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<maxHistory>120</maxHistory>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
<level>info</level>
</filter>

<encoder>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
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;
import java.util.List;

public class KVSSortedListTest extends BaseTestSupplier {
public class KVSSortedListTest {
@Test
public void testSortedList() throws InterruptedException {
KVStore store = new KVStoreImpl();
Expand Down
Loading