Second replica fails handshake in Redis challenge (#HD5)

I’m stuck on Stage #HD5 (Replication - Multi Replica Command Propagation)

I can spawn multiple replicas on my machine, and write commands are correctly propagated to each. Something like

Terminal 1: ./your_program.sh
Terminal 2: ./your_program.sh --port 1234 --replicaof "localhost 6379"
Terminal 3: ./your_program.sh --port 1235 --replicaof "localhost 6379"
Terminal 4: redis-cli SET foo bar
Terminal 4: redis-cli -p 1234 GET foo
Terminal 4: redis-cli -p 1235 GET foo

does what it should.

When I run codecrafters test, the test for this stage successfully launches a first replica, but when it attempts to launch a second replica it cannot complete the handshake.

Specifically, when the second replica sends a PING, the server does not respond.

The last part of the logs (showing successful creation of the first replica and the failure of the second) are:

...
[tester::#HD5] [handshake] replica-1: Received RESP simple string: "FULLRESYNC aILQSGgs02FpYqCScYHxaEc1UdGhwGl5p6DYEqFn 0"
[tester::#HD5] [handshake] Received "FULLRESYNC aILQSGgs02FpYqCScYHxaEc1UdGhwGl5p6DYEqFn 0"
[tester::#HD5] [handshake] Reading RDB file...
[tester::#HD5] [handshake] replica-1: Received bytes: "$88\r\nREDIS0011\xfa\tredis-ver\x057.2.0\xfa\nredis-bits\xc0@\xfa\x05ctime\xc2m\b\xbce\xfa\bused-mem°\xc4\x10\x00\xfa\baof-base\xc0\x00\xff\xf0n;\xfe\xc0\xffZ\xa2"
[tester::#HD5] [handshake] Received RDB file
[tester::#HD5] Creating replica: 2
[tester::#HD5] [handshake] replica-2: $ redis-cli PING
[tester::#HD5] [handshake] replica-2: Sent bytes: "*1\r\n$4\r\nPING\r\n"
[tester::#HD5] Received: "" (no content received)
[tester::#HD5]            ^ error
[tester::#HD5] Error: Expected start of a new RESP2 value (either +, -, :, $ or *)
[tester::#HD5] Test failed

To reiterate, the second replica completes a handshake when I launch two on my local machine.

The codebase is quite large by now, and I’m not sure what part of the code would be helpful to include, but has anybody encountered a similar issue?

I’ll take a look shortly.

Hi @hlud6646, it seems that the issue arises from using BufferedReader and readLine().

You may have noticed that the error occurred because the code was reading Null from the first connection:

In fact, our tester doesn’t send data through the first connection until handshakes from all replicas are complete.

To address this, your code should wait until data is available. Unfortunately, BufferedReader and readLine() may not handle this scenario well, as they attempt to read immediately and return Null if no data is present.

Consider read() directly from clientSocket.getInputStream(). While this approach may require refactoring other parts of your code, the current setup is inadequate to pass the stage.


Tip: You can use our CLI to test against previous stages while refactoring the code:

codecrafters test --previous

Thanks for the quick advice Andy.

I’ll do the suggested refactor and see how it goes.

Thanks

1 Like

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

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