Skip to content

Commit

Permalink
remove proto, switch to JSON (MappingJackson2HttpMessageConverter) fo…
Browse files Browse the repository at this point in the history
…r NodeInfo; add NodeAction; add test stubs for implementation
  • Loading branch information
alesavin committed Feb 14, 2018
1 parent c6d154a commit fcf5ad8
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import org.assertj.core.api.SoftAssertions;
import org.junit.Test;
import ru.csc.bdse.kv.KeyValueApi;
import ru.csc.bdse.proto.ClusterInfo;
import ru.csc.bdse.util.Constants;
import ru.csc.bdse.util.Random;

Expand Down Expand Up @@ -96,8 +94,9 @@ public void deleteNonexistentValue() {
public void getClusterInfoValue() {
SoftAssertions softAssert = new SoftAssertions();

ClusterInfo clusterInfo = api.getClusterInfo();
softAssert.assertThat(clusterInfo.getNodesList()).hasSize(1);
Set<NodeInfo> info = api.getInfo();
softAssert.assertThat(info).hasSize(1);
softAssert.assertThat(info.iterator().next().getStatus()).isEqualTo(NodeStatus.UP);

softAssert.assertAll();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package ru.csc.bdse.kv;

import org.junit.ClassRule;
import org.junit.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.images.builder.ImageFromDockerfile;
import ru.csc.bdse.util.Env;

import java.io.File;
import java.time.Duration;

import static java.time.temporal.ChronoUnit.SECONDS;

/**
* Test have to be implemented
*
* @author alesavin
*/
public class KeyValueApiHttpClientTest2 {

@ClassRule
public static final GenericContainer node = new GenericContainer(
new ImageFromDockerfile()
.withFileFromFile("target/bdse-kvnode-0.0.1-SNAPSHOT.jar", new File
("../bdse-kvnode/target/bdse-kvnode-0.0.1-SNAPSHOT.jar"))
.withFileFromClasspath("Dockerfile", "kvnode/Dockerfile"))
.withEnv(Env.KVNODE_NAME, "node-0")
.withExposedPorts(8080)
.withStartupTimeout(Duration.of(30, SECONDS));

private KeyValueApi api = newKeyValueApi();

private KeyValueApi newKeyValueApi() {
final String baseUrl = "http://localhost:" + node.getMappedPort(8080);
return new KeyValueApiHttpClient(baseUrl);
}

@Test
public void deleteByKey() {
// TODO use tombstones?
}

@Test
public void concurrentPuts() {
// TODO simultanious puts for the key value
}

@Test
public void concurrentDeleteAndKeys() {
//TODO simultanious delete by key and keys listing
}

@Test
public void actionUpDown() {
//TODO test up/down actions
}

@Test
public void putWithStoppedNode() {
//TODO test put if node/container was stopped
}

@Test
public void getWithStoppedNode() {
//TODO test get if node/container was stopped
}

@Test
public void getKeysByPrefixWithStoppedNode() {
//TODO test getKeysByPrefix if node/container was stopped
}

@Test
public void loadMillionKeys() {
}
}


16 changes: 0 additions & 16 deletions bdse-kvnode/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,4 @@
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>bdse-kvnode</artifactId>

<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import ru.csc.bdse.kv.KeyValueApi;
import ru.csc.bdse.util.Serializing;
import ru.csc.bdse.kv.NodeAction;
import ru.csc.bdse.kv.NodeInfo;

import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;

/**
* Provides HTTP API for the storage unit
Expand Down Expand Up @@ -35,20 +37,24 @@ public byte[] get(@PathVariable final String key) {
}

@RequestMapping(method = RequestMethod.GET, value = "/key-value")
public byte[] getKeys(@RequestParam("prefix") String prefix) {
return Serializing.serializeStringSet(keyValueApi.getKeys(prefix));
public Set<String> getKeys(@RequestParam("prefix") String prefix) {
return keyValueApi.getKeys(prefix);
}

@RequestMapping(method = RequestMethod.DELETE, value = "/key-value/{key}")
public void delete(@PathVariable final String key) {
keyValueApi.delete(key);
}

@RequestMapping(method = RequestMethod.GET, value = "/cluster-info")
public byte[] getClusterInfo() {
return keyValueApi.getClusterInfo().toByteArray();
@RequestMapping(method = RequestMethod.GET, value = "/info")
public Set<NodeInfo> getInfo() {
return keyValueApi.getInfo();
}

@RequestMapping(method = RequestMethod.POST, value = "/action/{node}/{action}")
public void action(@PathVariable final String node,
@PathVariable final NodeAction action) { keyValueApi.action(node, action); }

@ExceptionHandler(NoSuchElementException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handle(NoSuchElementException e) {
Expand Down
20 changes: 10 additions & 10 deletions bdse-kvnode/src/main/java/ru/csc/bdse/kv/InMemoryKeyValueApi.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package ru.csc.bdse.kv;

import ru.csc.bdse.proto.ClusterInfo;
import ru.csc.bdse.proto.NodeInfo;
import ru.csc.bdse.proto.NodeStatus;
import ru.csc.bdse.util.Require;
import ru.csc.bdse.kv.KeyValueApi;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -56,11 +54,13 @@ public void delete(final String key) {
}

@Override
public ClusterInfo getClusterInfo() {
return ClusterInfo.newBuilder()
.addNodes(NodeInfo.newBuilder()
.setName(name)
.setStatus(NodeStatus.UP))
.build();
public Set<NodeInfo> getInfo() {
return Collections.singleton(new NodeInfo(name, NodeStatus.UP));
}

@Override
public void action(String node, NodeAction action) {
throw new NotImplementedException();
}

}
13 changes: 7 additions & 6 deletions bdse-kvnode/src/main/java/ru/csc/bdse/kv/KeyValueApi.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package ru.csc.bdse.kv;

import ru.csc.bdse.proto.ClusterInfo;

import java.util.Optional;
import java.util.Set;

Expand All @@ -25,15 +23,18 @@ public interface KeyValueApi {
*/
Set<String> getKeys(String prefix);

/**
/**
* Deletes value associated with specified key from the storage.
*/
void delete(String key);

/**
* Returns info about all nodes in the cluster.
* Returns info about all nodes.
*/
ClusterInfo getClusterInfo();
Set<NodeInfo> getInfo();

// TODO: add method for turn-off and turn-on nodes (or for do some abstract command)
/**
* Do action on specified node.
*/
void action(String node, NodeAction action);
}
33 changes: 18 additions & 15 deletions bdse-kvnode/src/main/java/ru/csc/bdse/kv/KeyValueApiHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import ru.csc.bdse.kv.KeyValueApi;
import ru.csc.bdse.proto.ClusterInfo;
import ru.csc.bdse.util.Constants;
import ru.csc.bdse.util.Encoding;
import ru.csc.bdse.util.Serializing;
import ru.csc.bdse.util.Require;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.Optional;
import java.util.Set;
import java.util.*;

/**
* Http client for storage unit.
Expand Down Expand Up @@ -64,11 +62,11 @@ public Set<String> getKeys(String prefix) {
Require.nonNull(prefix, "null prefix");

final String url = baseUrl + "/key-value?prefix=" + Encoding.encodeUrl(prefix);
final ResponseEntity<byte[]> responseEntity = request(url, HttpMethod.GET, Constants.EMPTY_BYTE_ARRAY);
if (responseEntity.getStatusCode() != HttpStatus.OK) {
throw new RuntimeException("Response error: " + responseEntity);
try {
return new HashSet<>(Arrays.asList(rest.getForObject(url, String[].class)));
} catch (RestClientException e) {
throw new RuntimeException("Response error: " + e.getMessage());
}
return Serializing.deserializeStringSet(responseEntity.getBody());
}

@Override
Expand All @@ -83,13 +81,18 @@ public void delete(String key) {
}

@Override
public ClusterInfo getClusterInfo() {
final String url = baseUrl + "/cluster-info";
final ResponseEntity<byte[]> responseEntity = request(url, HttpMethod.GET, Constants.EMPTY_BYTE_ARRAY);
if (responseEntity.getStatusCode() != HttpStatus.OK) {
throw new RuntimeException("Response error: " + responseEntity);
public Set<NodeInfo> getInfo() {
final String url = baseUrl + "/info";
try {
return new HashSet<>(Arrays.asList(rest.getForObject(url, NodeInfo[].class)));
} catch (RestClientException e) {
throw new RuntimeException("Response error: " + e.getMessage());
}
return Serializing.deserializeClusterInfo(responseEntity.getBody());
}

@Override
public void action(String node, NodeAction action) {
throw new NotImplementedException();
}

private ResponseEntity<byte[]> request(final String url,
Expand Down
11 changes: 11 additions & 0 deletions bdse-kvnode/src/main/java/ru/csc/bdse/kv/NodeAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.csc.bdse.kv;

/**
* Action for node
*
* @author alesavin
*/
public enum NodeAction {
UP,
DOWN
}
36 changes: 36 additions & 0 deletions bdse-kvnode/src/main/java/ru/csc/bdse/kv/NodeInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ru.csc.bdse.kv;

/**
* Represent node information
*
* @author alesavin
*/
public class NodeInfo {

private String name;
private NodeStatus status;

public NodeInfo(String name, NodeStatus status) {
this.name = name;
this.status = status;
}

public NodeInfo() {
}

public void setName(String name) {
this.name = name;
}

public void setStatus(NodeStatus status) {
this.status = status;
}

public String getName() {
return name;
}

public NodeStatus getStatus() {
return status;
}
}
11 changes: 11 additions & 0 deletions bdse-kvnode/src/main/java/ru/csc/bdse/kv/NodeStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.csc.bdse.kv;

/**
* Status of the node
*
* @author alesavin
*/
public enum NodeStatus {
UP,
DOWN
}
41 changes: 0 additions & 41 deletions bdse-kvnode/src/main/java/ru/csc/bdse/util/Serializing.java

This file was deleted.

Loading

0 comments on commit fcf5ad8

Please sign in to comment.