I’m stuck on Stage 5 (tests failing for stage 4).
I implemented ECHO command, but suddenly the PING command for concurrent clients started giving wierd behaviour. The requests for first two clients are getting served, while before further connection could establish for other clients, the request seem to be dropped out
I’ve tried checking if there’s some shared resource causing the unexpected behaviour, verified the serverSocket object has not closed due to some unexepected exception, verified the executorservice has not shut down due to some exception.
Here are my logs:
[stage-4] Running tests for Stage #4: Handle concurrent clients
[stage-4] $ ./spawn_redis_server.sh
[your_program] Waiting for connection
[stage-4] client-1: $ redis-cli PING
[stage-4] client-1: Sent bytes: "*1\r\n$4\r\nPING\r\n"
[your_program] Connection established
[your_program] Waiting for connection
[stage-4] client-1: Received bytes: "+PONG\r\n"
[stage-4] client-1: Received RESP value: "PONG"
[stage-4] Received "PONG"
[stage-4] client-2: $ redis-cli PING
[stage-4] client-2: Sent bytes: "*1\r\n$4\r\nPING\r\n"
[your_program] Connection established
[your_program] Waiting for connection
[stage-4] client-2: Received bytes: "+PONG\r\n"
[stage-4] client-2: Received RESP value: "PONG"
[stage-4] Received "PONG"
[stage-4] client-1: $ redis-cli PING
[stage-4] client-1: Sent bytes: "*1\r\n$4\r\nPING\r\n"
[stage-4] Received: "" (no content received)
[stage-4] ^ error
[stage-4] Error: Expected start of a new RESP2 value (either +, -, :, $ or *)
[stage-4] Test failed
And here’s a snippet of my code:
public class Main {
public static void main(String[] args){
ExecutorService executorService = null;
try (ServerSocket serverSocket = runServer()) {
executorService = Executors.newCachedThreadPool();
while (true) {
System.out.println("Waiting for connection");
Socket socket = serverSocket.accept();
System.out.println("Connection established");
executorService.submit(new RequestHandler(socket));
}
} catch (Exception exception) {
System.out.println("Exception received " + exception.getMessage());
} finally {
executorService.shutdown();
}
}
private static ServerSocket runServer() throws IOException {
final ServerSocket serverSocket = new ServerSocket(6379);
serverSocket.setReuseAddress(true);
return serverSocket;
}
}
The request from third client seem to be timed out before the connection could be established.
The RequestHandler class is implementing Runnable interface and there’s no shared resource among threads. Hence not adding code for it.