diff --git a/java/benchmarks/src/main/java/javababushka/benchmarks/clients/JniSyncClient.java b/java/benchmarks/src/main/java/javababushka/benchmarks/clients/JniSyncClient.java index 0f05bf8622..1c3c376f07 100644 --- a/java/benchmarks/src/main/java/javababushka/benchmarks/clients/JniSyncClient.java +++ b/java/benchmarks/src/main/java/javababushka/benchmarks/clients/JniSyncClient.java @@ -7,6 +7,7 @@ import java.nio.channels.SocketChannel; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import javababushka.benchmarks.utils.ConnectionSettings; import javababushka.client.RedisClient; import org.apache.commons.lang3.tuple.MutablePair; @@ -141,7 +142,7 @@ public String get(String key) { makeRedisRequest(getStringRequest.toByteArray()); ResponseOuterClass.Response response = receiveRedisResponse(); - return response.toString(); + return response == null ? null : response.toString(); } @Override @@ -159,7 +160,7 @@ private static Pair decodeVarint(byte[] buffer, int pos) throws E long result = 0; while (true) { byte b = buffer[pos]; - result |= (b & 0x7F) << shift; + result |= (long) (b & 0x7F) << shift; pos += 1; if ((b & 0x80) == 0) { result &= mask; @@ -175,23 +176,22 @@ private static Pair decodeVarint(byte[] buffer, int pos) throws E private static ResponseOuterClass.Response decodeMessage(byte[] buffer) throws Exception { Pair pair = decodeVarint(buffer, 0); - int startIdx = (int) pair.getRight(); + int startIdx = pair.getRight(); byte[] responseBytes = Arrays.copyOfRange(buffer, startIdx, startIdx + (int) (long) pair.getLeft()); - ResponseOuterClass.Response response = ResponseOuterClass.Response.parseFrom(responseBytes); - return response; + return ResponseOuterClass.Response.parseFrom(responseBytes); } private static Byte[] varintBytes(int value) { - ArrayList output = new ArrayList(); + List output = new ArrayList<>(); int bits = value & 0x7F; value >>= 7; while (value > 0) { - output.add(new Byte((byte) (0x80 | bits))); + output.add((byte) (0x80 | bits)); bits = value & 0x7F; value >>= 7; } - output.add(new Byte((byte) bits)); + output.add((byte) bits); Byte[] arr = new Byte[] {}; return output.toArray(arr); } @@ -200,7 +200,9 @@ private void makeRedisRequest(byte[] request) { Byte[] varint = varintBytes(request.length); // System.out.println("Request: \n" + request.toString()); - ByteBuffer buffer = ByteBuffer.allocate(1024); + // javadocs: https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html#putInt%28int%29 + // BufferOverflowException - If there are fewer than four bytes remaining in this buffer + ByteBuffer buffer = ByteBuffer.allocate(request.length + 4); buffer.clear(); for (Byte b : varint) { buffer.put(b); @@ -220,6 +222,7 @@ private void makeRedisRequest(byte[] request) { } private ResponseOuterClass.Response receiveRedisResponse() { + // TODO what if buffer is too small? re-allocate? ByteBuffer readBuffer = ByteBuffer.allocate(1024); int timeout = 0; @@ -233,7 +236,7 @@ private ResponseOuterClass.Response receiveRedisResponse() { throw new RuntimeException("Max timeout reached"); } - bytesRead = channel.read(readBuffer); + bytesRead += channel.read(readBuffer); Thread.sleep(TIMEOUT_INTERVAL); } } @@ -247,7 +250,7 @@ private ResponseOuterClass.Response receiveRedisResponse() { try { response = decodeMessage(bytes); } catch (Exception e) { - e.printStackTrace(); + // e.printStackTrace(); } return response; } @@ -305,7 +308,7 @@ private ResponseOuterClass.Response makeRedisRequest( try { response = decodeMessage(responseBuffer); } catch (Exception e) { - e.printStackTrace(); + // e.printStackTrace(); } return response; }