Ping Requests getting dropped for #ZU2: Concurrent clients

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.

@vermaankit484 what does RequestHandler looks like? Could you try adding logs there to figure out where it’s stuck when the timeout occurs? Is it waiting on more data from the client?

Note that the third request here isn’t from a separate client, it is from client-1. So it looks like the bug here is that the program can handle the first ping from a client but not the second (from the same client).

Going to close this for now, please let us know if you still need help!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.

Note: I’ve updated the title of this post to include the stage ID (#ZU2). You can learn about the stages rename here: Upcoming change: Stages overhaul.