Skip to content

Commit 8602b6b

Browse files
authored
Merge pull request #410 from codecrafters-io/TropicolX-patch-55
Revise "Send handshake (2/3) #eh4" (stage 48)
2 parents 69e6513 + 0b2c612 commit 8602b6b

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed
Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
In this stage, you'll implement part 2 of the handshake that happens when a replica connects to master.
1+
In this stage, you'll implement the second part of the replication handshake.
22

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

5-
As a recap, there are three parts to the handshake:
5+
As a recap, there are three parts to the handshake process:
66

7-
- The replica sends a `PING` to the master (Previous stage)
8-
- The replica sends `REPLCONF` twice to the master (**This stage**)
9-
- The replica sends `PSYNC` to the master (Next stage)
7+
1. The replica sends a `PING` to the master (Handled in the previous stage)
8+
2. The replica sends `REPLCONF` twice to the master
9+
3. The replica sends `PSYNC` to the master
1010

11-
After receiving a response to `PING`, the replica then sends 2 [REPLCONF](https://redis.io/commands/replconf/) commands to the master.
11+
For this stage, you'll handle the second part of this process.
1212

13-
The `REPLCONF` command is used to configure replication. Replicas will send this command to the master twice:
13+
### The `REPLCONF` Command
1414

15-
- The first time, it'll be sent like this: `REPLCONF listening-port <PORT>`
16-
- This is the replica notifying the master of the port it's listening on (for [monitoring/logging purposes](https://github.com/redis/redis/blob/90178712f6eccf1e5b61daa677c5c103114bda3a/src/replication.c#L107-L130), not for actual propagation).
17-
- The second time, it'll be sent like this: `REPLCONF capa psync2`
18-
- This is the replica notifying the master of its capabilities ("capa" is short for "capabilities")
19-
- You can safely hardcode these capabilities for now, we won't need to use them in this challenge.
15+
The `REPLCONF` command is used to configure a connected replica. After receiving a response to `PING`, the replica sends two `REPLCONF` commands to the master:
2016

21-
These commands should be sent as RESP Arrays, so the exact bytes will look something like this:
17+
1. `REPLCONF listening-port <PORT>`: This tells the master which port the replica is listening on. This value is used for [monitoring and logging](https://github.com/redis/redis/blob/90178712f6eccf1e5b61daa677c5c103114bda3a/src/replication.c#L107-L130), not for replication itself.
18+
2. `REPLCONF capa psync2`: This notifies the master of the replica's capabilities.
19+
- `capa` stands for "capabilities". It indicates that the next argument is a feature the replica supports.
20+
- `psync2` signals that the replica supports the PSYNC 2.0 protocol, which is an improved version of the [partial synchronization](https://redis.io/docs/latest/operate/oss_and_stack/management/replication/) feature used to resynchronize a replica with its master.
21+
- You can safely hardcode `capa psync2` for now.
22+
23+
Both commands should be sent as RESP arrays, so the exact bytes will look something like this:
2224

2325
```
2426
# REPLCONF listening-port <PORT>
@@ -28,7 +30,7 @@ These commands should be sent as RESP Arrays, so the exact bytes will look somet
2830
*3\r\n$8\r\nREPLCONF\r\n$4\r\ncapa\r\n$6\r\npsync2\r\n
2931
```
3032

31-
For both commands, the master will respond with `+OK\r\n` ("OK" encoded as a RESP Simple String).
33+
For both commands, the master will respond with `+OK\r\n`. That's the string `OK` encoded as a [simple string](https://redis.io/docs/latest/develop/reference/protocol-spec/#simple-strings).
3234

3335
### Tests
3436

@@ -38,12 +40,12 @@ The tester will execute your program like this:
3840
./your_program.sh --port <PORT> --replicaof "<MASTER_HOST> <MASTER_PORT>"
3941
```
4042

41-
It'll then assert that the replica connects to the master and:
43+
It will then assert that the replica connects to the master and sends the following:
4244

43-
- **(a)** sends the `PING` command
44-
- **(b)** sends the `REPLCONF` command with `listening-port` and `<PORT>` as arguments
45-
- **(c)** sends the `REPLCONF` command with `capa psync2` as arguments
45+
1. The `PING` command
46+
2. The `REPLCONF` command with `listening-port` and `<PORT>` as the arguments
47+
3. The `REPLCONF` command with `capa psync2` as the arguments
4648

4749
**Notes**
4850

49-
- The response to `REPLCONF` will always be `+OK\r\n` ("OK" encoded as a RESP Simple String)
51+
- The response to `REPLCONF` will always be `+OK\r\n`.

0 commit comments

Comments
 (0)