Skip to content

Commit 724b903

Browse files
authored
Merge pull request #413 from codecrafters-io/TropicolX-patch-58
Revise "Receive handshake (2/2) #vm3" (stage 51)
2 parents 2ab62b8 + 79d9d10 commit 724b903

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed
Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
11
In this stage, you'll add support for receiving the [`PSYNC`](https://redis.io/commands/psync/) command from the replica.
22

3-
### Handshake (continued from previous stage)
3+
### Handshake (Recap)
44

5-
As a recap, there are three parts to the handshake:
5+
As a recap, the master receives the following for the handshake:
66

7-
- The master receives a `PING` from the replica (You've already implemented this)
8-
- The master receives `REPLCONF` twice from the replica (You've already implemented this)
9-
- The master receives `PSYNC` from the replica (**This stage**)
7+
1. A `PING` from the replica
8+
2. `REPLCONF` twice from the replica
9+
3. `PSYNC` from the replica
1010

11-
After the replica sends `REPLCONF` twice, it'll send a `PSYNC ? -1` command to the master.
11+
After the replica sends `REPLCONF` twice, it will send a `PSYNC` command with the arguments `? -1` to the master:
1212

13-
- The first argument is `?`
14-
- This is the replication ID of the master, it is `?` because this is the first time the replica is connecting to the master.
15-
- The second argument is `-1`
16-
- This is the replication offset, it is `-1` because this is the first time the replica is connecting to the master.
13+
- The replication ID is `?` because the replica doesn't know the master's ID yet.
14+
- The offset is `-1` since the replica has no data from the master yet.
1715

18-
The final command you receive will look something like this:
16+
The final command you'll receive will look something like this:
1917

2018
```
2119
*3\r\n$5\r\nPSYNC\r\n$1\r\n?\r\n$2\r\n-1\r\n
2220
```
2321

24-
(That's `["PSYNC", "?", "-1"]` encoded as a RESP Array)
22+
That's `["PSYNC", "?", "-1"]` encoded as a RESP array.
2523

26-
The master needs to respond with `+FULLRESYNC <REPL_ID> 0\r\n` ("FULLRESYNC <REPL_ID> 0" encoded as a RESP Simple String). Here's what
27-
the response means:
24+
The master needs to respond with `+FULLRESYNC <REPL_ID> 0\r\n`, which is `FULLRESYNC <REPL_ID> 0` encoded as a simple string. Here's what the response means:
2825

29-
- `FULLRESYNC` means that the master cannot perform incremental replication with the replica, and will thus start a "full" resynchronization.
30-
- `<REPL_ID>` is the replication ID of the master. You've already set this in the "Replication ID & Offset" stage.
31-
- As an example, you can hardcode `8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb` as the replication ID.
32-
- `0` is the replication offset of the master. You've already set this in the "Replication ID & Offset" stage.
26+
- `FULLRESYNC` means that the master cannot perform an incremental update to the replica, and will start a full resynchronization.
27+
- `<REPL_ID>` is the replication ID of the master (the 40-character string you initialized in a previous stage).
28+
- `0` is the replication offset of the master (which you initialized in a previous stage).
29+
30+
For example, if your replication ID is `8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb`, you'd respond with:
31+
```bash
32+
+FULLRESYNC 8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb 0\r\n
33+
```
3334

3435
### Tests
3536

@@ -39,13 +40,13 @@ The tester will execute your program like this:
3940
./your_program.sh --port <PORT>
4041
```
4142

42-
It'll then connect to your TCP server as a replica and execute the following commands:
43+
It will then connect to your TCP server as a replica and send the following commands:
4344

44-
1. `PING` (expecting `+PONG\r\n` back)
45-
2. `REPLCONF listening-port <PORT>` (expecting `+OK\r\n` back)
46-
3. `REPLCONF capa eof capa psync2` (expecting `+OK\r\n` back)
47-
4. `PSYNC ? -1` (expecting `+FULLRESYNC <REPL_ID> 0\r\n` back)
45+
1. `PING` - expecting `+PONG\r\n` back
46+
2. `REPLCONF listening-port <PORT>` - expecting `+OK\r\n` back
47+
3. `REPLCONF capa psync2` - expecting `+OK\r\n` back
48+
4. `PSYNC ? -1` - expecting `+FULLRESYNC <REPL_ID> 0\r\n` back
4849

4950
**Notes**:
5051

51-
- In the response, `<REPL_ID>` needs to be replaced with the replication ID of the master which you've initialized in previous stages.
52+
- In the response, `<REPL_ID>` needs to be replaced with the replication ID of the master, which you've initialized in previous stages.

0 commit comments

Comments
 (0)