Skip to content

Commit 67fee06

Browse files
committed
add various fixes
1 parent 7f0cc55 commit 67fee06

12 files changed

+101
-38
lines changed

.dockerignore

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.idea/
2+
.github/
3+
docs/
4+
binaries/
5+
build/
6+
.gradle/
7+
.DS_Store
8+
gradle.properties
9+
10+
testproject/
11+
run.sh
12+
13+
dump.kdb
14+
15+
keva.test.properties

.gitignore

-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ gradle.properties
77
testproject/
88
run.sh
99

10-
# data.zip
11-
# KevaData
12-
# KevaDataIndex
13-
1410
dump.kdb
1511

1612
keva.test.properties

.tokeignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
docs/
22
examples/
33
gradle/
4-
.github/
4+
.github/

Dockerfile

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM openjdk:12 AS builder
2+
3+
RUN mkdir -p /root/src/keva
4+
WORKDIR /root/src/keva
5+
6+
COPY gradle ./gradle
7+
COPY build.gradle gradlew settings.gradle ./
8+
COPY ./server/build.gradle ./server/keva.properties ./server/
9+
RUN ./gradlew dependencies
10+
11+
COPY . .
12+
RUN ./gradlew clean :server:build -x test
13+
14+
FROM openjdk:12-jdk-alpine
15+
16+
RUN mkdir -p /root/binary/keva
17+
WORKDIR /root/binary/keva
18+
19+
COPY --from=builder /root/src/keva/server/build/libs/server-1.0-SNAPSHOT-all.jar /root/binary/keva/keva.jar
20+
21+
EXPOSE 6767
22+
23+
ENTRYPOINT ["java","-jar","keva.jar","--add-opens=java.base/sun.nio.ch=ALL-UNNAMED"]
24+
25+
# docker build -t keva-server .
26+
# docker run -d --name keva-server --network=host -p 6767:6767 keva-server:latest

build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ subprojects {
99
apply plugin: 'maven-publish'
1010
apply plugin: 'jacoco'
1111

12-
group 'com.jinyframework'
13-
version '0.3.3'
12+
group 'dev.keva'
13+
version '1.0.0'
1414

1515
repositories {
1616
mavenCentral()
@@ -27,7 +27,7 @@ subprojects {
2727
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
2828

2929
// SLF4J as a facade
30-
compile group: 'org.slf4j', name:'slf4j-api', version: '1.7.2'
30+
implementation 'org.slf4j:slf4j-api:1.7.2'
3131
}
3232

3333
jacocoTestReport {

docs/src/guide/install.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
## Download
99

1010
```command
11-
curl -o /usr/local/bin/keva-server https://go.keva.dev/keva-server
11+
curl -L -o /usr/local/bin/keva-server https://download.keva.dev
1212
chmod +x /usr/local/bin/keva-server
1313
```
1414

server/build.gradle

+6-7
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ repositories {
1111
mavenCentral()
1212
}
1313

14-
def nettyVersion = '4.1.65.Final'
1514
dependencies {
16-
compile project(':store')
15+
implementation project(':store')
1716

18-
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.0.9'
19-
compile group: 'ch.qos.logback', name: 'logback-core', version: '1.0.9'
20-
compile group: 'io.netty', name: 'netty-handler', version: "${nettyVersion}"
21-
compile group: 'io.netty', name: 'netty-buffer', version: "${nettyVersion}"
17+
implementation 'ch.qos.logback:logback-classic:1.2.6'
18+
implementation 'ch.qos.logback:logback-core:1.2.6'
19+
implementation 'io.netty:netty-handler:4.1.69.Final'
20+
implementation 'io.netty:netty-buffer:4.1.69.Final'
2221

2322
implementation 'com.google.guava:guava:31.0.1-jre'
2423
implementation 'org.reflections:reflections:0.10.1'
@@ -27,7 +26,7 @@ dependencies {
2726

2827
// MTC lib depends on junit 4
2928
testImplementation group: 'junit', name: 'junit', version: '4.12'
30-
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.7.2")
29+
testRuntimeOnly('org.junit.vintage:junit-vintage-engine:5.8.1')
3130
}
3231

3332
shadowJar {

server/src/main/java/dev/keva/server/core/NettyServer.java

+31-19
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@
55
import dev.keva.server.command.setup.CommandServiceImpl;
66
import dev.keva.server.config.ConfigHolder;
77
import dev.keva.server.replication.master.ReplicationService;
8-
import dev.keva.server.replication.master.ReplicationServiceImpl;
98
import dev.keva.server.replication.slave.SlaveService;
10-
import dev.keva.server.replication.slave.SlaveServiceImpl;
119
import dev.keva.server.storage.NoHeapStorageServiceImpl;
1210
import dev.keva.server.storage.StorageService;
1311
import dev.keva.store.NoHeapConfig;
1412
import dev.keva.store.NoHeapFactory;
1513
import dev.keva.store.NoHeapStore;
1614
import io.netty.bootstrap.ServerBootstrap;
15+
import io.netty.buffer.PooledByteBufAllocator;
1716
import io.netty.channel.Channel;
1817
import io.netty.channel.ChannelFuture;
18+
import io.netty.channel.ChannelOption;
1919
import io.netty.channel.EventLoopGroup;
2020
import io.netty.channel.nio.NioEventLoopGroup;
2121
import io.netty.channel.socket.nio.NioServerSocketChannel;
22-
import io.netty.handler.logging.LogLevel;
23-
import io.netty.handler.logging.LoggingHandler;
2422
import lombok.Getter;
2523
import lombok.extern.slf4j.Slf4j;
2624
import lombok.val;
@@ -31,6 +29,8 @@
3129

3230
@Slf4j
3331
public class NettyServer implements Server {
32+
private static final int BUFFER_SIZE = 1024 * 1024;
33+
3434
private final ConfigHolder config;
3535

3636
// Executors
@@ -46,34 +46,40 @@ public class NettyServer implements Server {
4646
private CommandService commandService;
4747
@Getter // use for testing should change to dedicated command or extract from INFO
4848
private ReplicationService replicationService;
49-
private WriteLog writeLog;
49+
// private WriteLog writeLog;
5050
private Channel channel;
51-
private ServerBootstrap server;
5251
private NoHeapStore noHeapStore;
5352

5453
public NettyServer(ConfigHolder config) {
5554
this.config = config;
5655
}
5756

5857
private void initServices(boolean isFreshStart) {
59-
if (isFreshStart) {
60-
writeLog = new WriteLog(config.getWriteLogSize());
61-
}
58+
// TODO: re-enable rep mode
59+
// if (isFreshStart) {
60+
// writeLog = new WriteLog(config.getWriteLogSize());
61+
// }
6262
initStorageService(isFreshStart);
63-
replicationService = new ReplicationServiceImpl(healthCheckerPool, repWorkerPool, storageService, writeLog);
63+
// replicationService = new ReplicationServiceImpl(healthCheckerPool, repWorkerPool, storageService, writeLog);
6464

6565
connectionService = new ConnectionServiceImpl();
6666
final CommandRegistrar commandRegistrar = new CommandRegistrar(storageService, replicationService, connectionService);
6767
commandService = new CommandServiceImpl(commandRegistrar.getHandlerMap(), replicationService);
68-
slaveService = new SlaveServiceImpl(healthCheckerPool, writeLog, commandService);
68+
// slaveService = new SlaveServiceImpl(healthCheckerPool, writeLog, commandService);
6969
}
7070

7171
public ServerBootstrap bootstrapServer() {
7272
final ServerBootstrap b = new ServerBootstrap();
7373
b.group(bossGroup, workerGroup)
7474
.channel(NioServerSocketChannel.class)
75-
.handler(new LoggingHandler(LogLevel.TRACE))
76-
.childHandler(new RedisCodecInitializer(new ServerHandler(connectionService, commandService)));
75+
.childHandler(new RedisCodecInitializer(new ServerHandler(connectionService, commandService)))
76+
.option(ChannelOption.SO_BACKLOG, 100)
77+
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
78+
.childOption(ChannelOption.SO_RCVBUF, BUFFER_SIZE)
79+
.childOption(ChannelOption.SO_SNDBUF, BUFFER_SIZE)
80+
.childOption(ChannelOption.SO_KEEPALIVE, true)
81+
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
82+
.childOption(ChannelOption.TCP_NODELAY, true);
7783
return b;
7884
}
7985

@@ -110,8 +116,11 @@ public void shutdown() {
110116
healthCheckerPool.shutdown();
111117
bossGroup.shutdownGracefully();
112118
workerGroup.shutdownGracefully();
113-
channel.close();
114-
log.info("Database server at {} stopped", config.getPort());
119+
120+
if (channel != null) {
121+
channel.close();
122+
}
123+
log.info("Keva server at {} stopped", config.getPort());
115124
}
116125

117126
@Override
@@ -120,12 +129,15 @@ public void run(boolean isFreshStart) {
120129
initExecutors();
121130
initServices(isFreshStart);
122131
startSlaveService();
123-
server = bootstrapServer();
132+
ServerBootstrap server = bootstrapServer();
124133
final ChannelFuture sync = server.bind(config.getPort()).sync();
125-
log.info("Database server started at {}", config.getPort());
134+
sync.syncUninterruptibly();
135+
log.info("Keva server started at {}", config.getPort());
126136

127-
channel = sync.channel();
128-
channel.closeFuture().sync();
137+
if (channel != null) {
138+
channel = sync.channel();
139+
channel.closeFuture().sync();
140+
}
129141
} catch (InterruptedException e) {
130142
log.error("Failed to start server: ", e);
131143
Thread.currentThread().interrupt();

server/src/main/java/dev/keva/server/core/RedisCodecInitializer.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.netty.channel.ChannelInitializer;
77
import io.netty.channel.ChannelPipeline;
88
import io.netty.channel.socket.SocketChannel;
9+
import io.netty.handler.timeout.IdleStateHandler;
910

1011
public class RedisCodecInitializer extends ChannelInitializer<SocketChannel> {
1112
private ChannelHandler handler;
@@ -18,9 +19,10 @@ public RedisCodecInitializer() {
1819
}
1920

2021
@Override
21-
protected void initChannel(SocketChannel ch) throws Exception {
22+
protected void initChannel(SocketChannel ch) {
2223
final ChannelPipeline p = ch.pipeline();
2324

25+
p.addLast(new IdleStateHandler(0, 0, 5 * 60));
2426
p.addLast(new RedisCommandDecoder());
2527
p.addLast(new RedisReplyEncoder());
2628

server/src/main/java/dev/keva/server/core/ServerHandler.java

+14
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import dev.keva.server.command.setup.CommandService;
44
import dev.keva.server.protocol.redis.Command;
55
import dev.keva.server.protocol.redis.Reply;
6+
import io.netty.channel.Channel;
67
import io.netty.channel.ChannelHandler;
78
import io.netty.channel.ChannelHandlerContext;
89
import io.netty.channel.SimpleChannelInboundHandler;
10+
import io.netty.handler.timeout.IdleStateEvent;
911
import io.netty.util.AttributeKey;
1012
import lombok.extern.slf4j.Slf4j;
1113

@@ -65,4 +67,16 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
6567
log.error("Handler exception caught: ", cause);
6668
ctx.close();
6769
}
70+
71+
@Override
72+
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
73+
if (evt instanceof IdleStateEvent) {
74+
Channel channel = ctx.channel();
75+
log.info("IdleStateEvent triggered, close channel " + channel);
76+
channelUnregistered(ctx);
77+
ctx.close();
78+
} else {
79+
super.userEventTriggered(ctx, evt);
80+
}
81+
}
6882
}

settings.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
rootProject.name = 'keva'
22

3-
include 'cli-client'
43
include 'server'
54
include 'store'

store/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ java {
88
}
99

1010
dependencies {
11-
implementation 'net.openhft:chronicle-map:3.21.85'
11+
implementation 'net.openhft:chronicle-map:3.21.86'
1212
}
1313

1414
javadoc {

0 commit comments

Comments
 (0)