I’m stuck on Stage #ZN8.
Getting below error when running codecrafters test.
But everything works fine when I execute it manually by starting master, replica and client connection.
Here are my logs:
[tester::#ZN8] Running tests for Stage #ZN8 (Replication - Single-replica propagation)
[tester::#ZN8] $ ./spawn_redis_server.sh --port 6379
[tester::#ZN8] [handshake] replica: $ redis-cli PING
[tester::#ZN8] [handshake] replica: Sent bytes: "*1\r\n$4\r\nPING\r\n"
[your_program] Server started successfully
[your_program] Server started listening on port 6379
[your_program] Server started listening on port 6379
[your_program] Received input: *1
[your_program] Received input: $4
[your_program] Received input: PING
[tester::#ZN8] [handshake] replica: Received bytes: "$4\r\nPONG\r\n"
[tester::#ZN8] [handshake] replica: Received RESP bulk string: "PONG"
[tester::#ZN8] [handshake] Received "PONG"
[tester::#ZN8] [handshake] replica: > REPLCONF listening-port 6380
[tester::#ZN8] [handshake] replica: Sent bytes: "*3\r\n$8\r\nREPLCONF\r\n$14\r\nlistening-port\r\n$4\r\n6380\r\n"
[your_program] Received input: *3
[your_program] Received input: $8
[your_program] Received input: REPLCONF
[your_program] Received input: $14
[your_program] Received input: listening-port
[your_program] Received input: $4
[your_program] Received input: 6380
[tester::#ZN8] [handshake] replica: Received bytes: "+OK\r\n"
[tester::#ZN8] [handshake] replica: Received RESP simple string: "OK"
[tester::#ZN8] [handshake] Received "OK"
[tester::#ZN8] [handshake] replica: > REPLCONF capa psync2
[tester::#ZN8] [handshake] replica: Sent bytes: "*3\r\n$8\r\nREPLCONF\r\n$4\r\ncapa\r\n$6\r\npsync2\r\n"
[your_program] Received input: *3
[your_program] Received input: $8
[your_program] Received input: REPLCONF
[your_program] Received input: $4
[your_program] Received input: capa
[your_program] Received input: $6
[your_program] Received input: psync2
[tester::#ZN8] [handshake] replica: Received bytes: "+OK\r\n"
[tester::#ZN8] [handshake] replica: Received RESP simple string: "OK"
[tester::#ZN8] [handshake] Received "OK"
[tester::#ZN8] [handshake] replica: > PSYNC ? -1
[tester::#ZN8] [handshake] replica: Sent bytes: "*3\r\n$5\r\nPSYNC\r\n$1\r\n?\r\n$2\r\n-1\r\n"
[your_program] Received input: *3
[your_program] Received input: $5
[your_program] Received input: PSYNC
[your_program] Received input: $1
[your_program] Received input: ?
[your_program] Received input: $2
[your_program] Received input: -1
[tester::#ZN8] [handshake] replica: Received bytes: "+FULLRESYNC O9jeeQbNYaW4G6T021bZWflrqUOSAYwABBilHaRY 0\r\n"
[tester::#ZN8] [handshake] replica: Received RESP simple string: "FULLRESYNC O9jeeQbNYaW4G6T021bZWflrqUOSAYwABBilHaRY 0"
[tester::#ZN8] [handshake] Received "FULLRESYNC O9jeeQbNYaW4G6T021bZWflrqUOSAYwABBilHaRY 0"
[tester::#ZN8] [handshake] Reading RDB file...
[tester::#ZN8] Received: "" (no content received)
[tester::#ZN8] ^ error
[tester::#ZN8] Error: Expected first byte of RDB file message to be $
[tester::#ZN8] Test failed
[tester::#ZN8] Terminating program
[your_program] java.net.SocketException: Broken pipe
[your_program] at java.base/sun.nio.ch.SocketDispatcher.write0(Native Method)
[your_program] at java.base/sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:62)
[your_program] at java.base/sun.nio.ch.NioSocketImpl.tryWrite(NioSocketImpl.java:394)
[your_program] at java.base/sun.nio.ch.NioSocketImpl.implWrite(NioSocketImpl.java:410)
[your_program] at java.base/sun.nio.ch.NioSocketImpl.write(NioSocketImpl.java:440)
[your_program] at java.base/sun.nio.ch.NioSocketImpl$2.write(NioSocketImpl.java:819)
[your_program] at java.base/java.net.Socket$SocketOutputStream.write(Socket.java:1195)
[your_program] at java.base/java.io.OutputStream.write(OutputStream.java:124)
[your_program] at MasterServer.lambda$startFullResync$1(MasterServer.java:124)
[your_program] at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
[your_program] at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
[your_program] at java.base/java.lang.Thread.run(Thread.java:1583)
[tester::#ZN8] Program terminated successfully
And here’s a snippet of my code:
private void startFullResync(Socket connection) {
executor.execute(() -> {
try {
byte[] data = Base64.getDecoder().decode(
"UkVESVMwMDEx+glyZWRpcy12ZXIFNy4yLjD6CnJlZGlzLWJpdHPAQPoFY3RpbWXCbQi8ZfoIdXNlZC1tZW3CsMQQAPoIYW9mLWJhc2XAAP/wbjv+wP9aog==");
var out = connection.getOutputStream();
out.write(("$" + data.length + "\r\n").getBytes());
out.write(data);
out.flush();
} catch (IOException e) {
e.printStackTrace();
config.removeReplica(connection);
}
});
}