Skip to content
This repository has been archived by the owner on Nov 8, 2021. It is now read-only.

Commit

Permalink
Keva: Refactor Internal (#53)
Browse files Browse the repository at this point in the history
* rename keva socket

* init initial

* impl store

* add publish
  • Loading branch information
tuhuynh27 authored Mar 6, 2021
1 parent 183ca52 commit 4443d19
Show file tree
Hide file tree
Showing 18 changed files with 114 additions and 60 deletions.
8 changes: 1 addition & 7 deletions keva/client/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
plugins {
id 'java'
id 'idea'
id 'application'
id 'com.github.johnrengelman.shadow' version '6.0.0'
id 'com.adarshr.test-logger' version '2.1.0'
Expand All @@ -12,14 +10,10 @@ version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.12'
implementation 'com.google.code.gson:gson:2.8.6'

dependencies {
compile group: 'ch.qos.logback', name:'logback-classic', version: '1.0.9'
compile group: 'ch.qos.logback', name:'logback-core', version: '1.0.9'

annotationProcessor 'org.projectlombok:lombok:1.18.12'
}

shadowJar {
Expand Down
8 changes: 2 additions & 6 deletions keva/server/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
plugins {
id 'java'
id 'idea'
id 'application'
id 'com.github.johnrengelman.shadow' version '6.0.0'
id 'com.adarshr.test-logger' version '2.1.0'
Expand All @@ -12,14 +10,12 @@ version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}

dependencies {
compileOnly 'org.projectlombok:lombok:1.18.12'
implementation 'com.google.code.gson:gson:2.8.6'
compile project(':keva:store')

compile group: 'ch.qos.logback', name:'logback-classic', version: '1.0.9'
compile group: 'ch.qos.logback', name:'logback-core', version: '1.0.9'

annotationProcessor 'org.projectlombok:lombok:1.18.12'
}

shadowJar {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.jinyframework.keva.server.command;

import com.jinyframework.keva.server.noheap.NoHeapStore;
import com.jinyframework.keva.store.NoHeapStore;
import com.jinyframework.keva.server.storage.StorageFactory;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.jinyframework.keva.server.command;

import com.jinyframework.keva.server.noheap.NoHeapStore;
import com.jinyframework.keva.store.NoHeapStore;
import com.jinyframework.keva.server.storage.StorageFactory;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.jinyframework.keva.server.command;

import com.jinyframework.keva.server.noheap.NoHeapStore;
import com.jinyframework.keva.store.NoHeapStore;
import com.jinyframework.keva.server.storage.StorageFactory;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.jinyframework.keva.server.command;

import com.jinyframework.keva.server.noheap.NoHeapStore;
import com.jinyframework.keva.store.NoHeapStore;
import com.jinyframework.keva.server.storage.StorageFactory;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jinyframework.keva.server.core;

public interface ConnectionService {
void handleConnection(KevaSocket kevaSocket);
void handleConnection(ServerSocket serverSocket);

long getCurrentConnectedClients();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,30 @@
public class ConnectionServiceImpl implements ConnectionService {
private final CommandService commandService = ServiceFactory.getCommandService();

private final Map<String, KevaSocket> socketMap = StorageFactory.getSocketHashMap();
private final Map<String, ServerSocket> socketMap = StorageFactory.getSocketHashMap();

@Override
public void handleConnection(KevaSocket kevaSocket) {
val socketId = kevaSocket.getId();
public void handleConnection(ServerSocket serverSocket) {
val socketId = serverSocket.getId();
try {
socketMap.put(socketId, kevaSocket);
val socket = kevaSocket.getSocket();
socketMap.put(socketId, serverSocket);
val socket = serverSocket.getSocket();
val remoteAddr = socket.getRemoteSocketAddress();
log.info("{} {} connected", remoteAddr, socketId);

@Cleanup
val socketIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
@Cleanup
val socketOut = new PrintWriter(socket.getOutputStream());
while (kevaSocket.isAlive()) {
while (serverSocket.isAlive()) {
val line = socketIn.readLine();
if (line == null) {
socketMap.remove(socketId);
log.info("{} {} disconnected", remoteAddr, socketId);
break;
}
kevaSocket.getLastOnlineLong().set(System.currentTimeMillis());
log.info("{} sent {}", kevaSocket.getId(), line);
serverSocket.getLastOnlineLong().set(System.currentTimeMillis());
log.info("{} sent {}", serverSocket.getId(), line);
commandService.handleCommand(socketOut, line);
}
} catch (SocketException e) {
Expand All @@ -61,16 +61,16 @@ public Runnable getHeartbeatRunnable(long sockTimeout) {
return () -> {
log.info("Running heartbeat");
val now = System.currentTimeMillis();
socketMap.values().forEach(kevaSocket -> {
if (kevaSocket.getLastOnline() + sockTimeout < now) {
kevaSocket.getAlive().set(false);
socketMap.values().forEach(serverSocket -> {
if (serverSocket.getLastOnline() + sockTimeout < now) {
serverSocket.getAlive().set(false);
try {
kevaSocket.getSocket().close();
serverSocket.getSocket().close();
} catch (IOException e) {
log.error("Error while closing socket {}: {}", kevaSocket.getId(), e);
log.error("Error while closing socket {}: {}", serverSocket.getId(), e);
}
socketMap.remove(kevaSocket.getId());
log.info("{} {} closed from timeout", kevaSocket.getSocket().getRemoteSocketAddress(), kevaSocket.getId());
socketMap.remove(serverSocket.getId());
log.info("{} {} closed from timeout", serverSocket.getSocket().getRemoteSocketAddress(), serverSocket.getId());
}
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.UUID;
Expand All @@ -26,7 +25,7 @@ public class Server {
private final AtomicBoolean serverStopped = new AtomicBoolean(false);

private final ConfigHolder config;
private ServerSocket serverSocket;
private java.net.ServerSocket serverSocket;
private ExecutorService executor;

public Server(ConfigHolder config) {
Expand All @@ -39,7 +38,7 @@ private void startServer() throws IOException {
executor = Executors.newCachedThreadPool();
val socketAddress = new InetSocketAddress(host, port);
if (serverSocket == null) {
serverSocket = new ServerSocket();
serverSocket = new java.net.ServerSocket();
}
serverSocket.bind(socketAddress);

Expand Down Expand Up @@ -73,7 +72,7 @@ public void run() throws IOException {
break;
}
executor.execute(() -> {
val kevaSocket = KevaSocket.builder()
val kevaSocket = ServerSocket.builder()
.socket(socket)
.id(UUID.randomUUID().toString())
.lastOnlineLong(new AtomicLong(System.currentTimeMillis()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Builder
@Getter
@Setter
public class KevaSocket {
public class ServerSocket {
private final Socket socket;
private final String id;
private AtomicLong lastOnlineLong;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.jinyframework.keva.server.storage;

import com.jinyframework.keva.store.NoHeapStore;
import com.jinyframework.keva.store.NoHeapStoreManager;
import com.jinyframework.keva.server.config.ConfigManager;
import com.jinyframework.keva.server.core.KevaSocket;
import com.jinyframework.keva.server.noheap.NoHeapStore;
import com.jinyframework.keva.server.noheap.NoHeapStoreManager;
import com.jinyframework.keva.server.core.ServerSocket;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
Expand All @@ -14,7 +14,7 @@
@Slf4j
public final class StorageFactory {
private static NoHeapStore noHeapStore;
private static ConcurrentHashMap<String, KevaSocket> socketHashMap;
private static ConcurrentHashMap<String, ServerSocket> socketHashMap;

public synchronized static NoHeapStore getNoHeapDBStore() {
if (noHeapStore == null) {
Expand All @@ -24,7 +24,7 @@ public synchronized static NoHeapStore getNoHeapDBStore() {
val heapSizeInMegabytes = ConfigManager.getConfig().getHeapSize();
db.createStore("Keva",
shouldPersist ? NoHeapStore.Storage.PERSISTED : NoHeapStore.Storage.IN_MEMORY,
heapSizeInMegabytes);
heapSizeInMegabytes, ConfigManager.getConfig().getSnapshotLocation());
noHeapStore = db.getStore("Keva");
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
Expand All @@ -36,7 +36,7 @@ public synchronized static NoHeapStore getNoHeapDBStore() {
return noHeapStore;
}

public synchronized static ConcurrentHashMap<String, KevaSocket> getSocketHashMap() {
public synchronized static ConcurrentHashMap<String, ServerSocket> getSocketHashMap() {
if (socketHashMap == null) {
socketHashMap = new ConcurrentHashMap<>();
}
Expand Down
67 changes: 67 additions & 0 deletions keva/store/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
plugins {
id 'com.adarshr.test-logger' version '2.1.0'
}

java {
withJavadocJar()
withSourcesJar()
}

publishing {
publications {
mavenJava(MavenPublication) {
artifactId = 'keva-store'
from components.java
versionMapping {
usage('java-api') {
fromResolutionOf('runtimeClasspath')
}
usage('java-runtime') {
fromResolutionResult()
}
}
pom {
name = 'Jiny Keva'
description = 'Performant & low-latency in-memory data structure store, used as a database or cache'
url = 'https://jinyframework.com/'
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'https://github.com/huynhminhtufu/jiny/blob/master/LICENSE'
}
}
developers {
developer {
id = 'tuhuynh27'
name = 'Tu Huynh'
email = '[email protected]'
}
}
scm {
connection = 'scm:git:git://github.com/huynhminhtufu/jiny.git'
developerConnection = 'scm:git:ssh://github.com:huynhminhtu/jiny.git'
url = 'https://github.com/huynhminhtufu/jiny/'
}
}
}
}
}

javadoc {
if (JavaVersion.current().isJava9Compatible()) {
options.addBooleanOption('html5', true)
}
}

signing {
sign publishing.publications.mavenJava
}

test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
outputs.upToDateWhen { false }
showStandardStreams = true
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jinyframework.keva.server.noheap;
package com.jinyframework.keva.store;

public interface IndexStore {
void put(String k, Long v);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jinyframework.keva.server.noheap;
package com.jinyframework.keva.store;

import lombok.extern.slf4j.Slf4j;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jinyframework.keva.server.noheap;
package com.jinyframework.keva.store;

public interface NoHeapStore {
byte INACTIVE_RECORD = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jinyframework.keva.server.noheap;
package com.jinyframework.keva.store;

import lombok.extern.slf4j.Slf4j;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package com.jinyframework.keva.server.noheap;

import com.jinyframework.keva.server.config.ConfigManager;
package com.jinyframework.keva.store;

import java.util.HashMap;

Expand All @@ -10,17 +8,10 @@ public class NoHeapStoreManager {

HashMap<String, NoHeapStore> stores = new HashMap<>();

String snapshotConnection = ConfigManager.getConfig().getSnapshotLocation();
String homeDirectory = snapshotConnection.length() == 0 ? System.getProperty("user.dir") : snapshotConnection;

public NoHeapStoreManager() {
}

public NoHeapStoreManager(String homeDirectory) {
this.homeDirectory = homeDirectory;
}

public boolean createStore(String name) throws Exception {
public boolean createStore(String name) {
return createStore(name,
NoHeapStore.Storage.IN_MEMORY,
100);
Expand All @@ -36,6 +27,13 @@ public boolean createStore(String name,
public boolean createStore(String name,
NoHeapStore.Storage storageType,
int size) {
return createStore(name, storageType, size, System.getProperty("user.dir"));
}

public boolean createStore(String name,
NoHeapStore.Storage storageType,
int size,
String homeDirectory) {
NoHeapStoreImpl noHeapDB = new
NoHeapStoreImpl(homeDirectory, name, storageType,
size * MEGABYTE, true);
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ include 'core'
include 'middlewares:cors'
include 'middlewares:jwt'
include 'websocket'
include 'keva:store'
include 'keva:server'
include 'keva:client'

// Test only (with gitignore)
include 'testproject'

0 comments on commit 4443d19

Please sign in to comment.