docs/design/sync_en.md
slaveof command on the slave side.slaveof command, changes its state to slave, and changes the connection state.Establishing sync for a partition on the Slave:
Master handling the sync request:
The master receives the BinlogSyncRequest and sends a batch of BinlogSyncResponses starting from the sync point.
After receiving BinlogSyncResponse, the slave writes to the local binlog and then repeats step 1.
Pika's sync is handled by the ReplicaManager (RM) module. RM has a two-layer structure: the logic layer handles sync logic, and the transport layer handles connection management, data parsing, and transmission.
The minimum unit of data sync is a Partition. Each Pika instance maintains its own primary partitions (MasterPartitions) and slave partitions (SlavePartitions). For MasterPartitions, it records the sync information of following slaves, and the logic layer syncs information to slaves based on this. For SlavePartitions, it records master information, and the logic layer sends sync requests to the master as needed.
The logic layer maintains two data structures: one is MasterPartitions, which records following SlaveNode information (mainly the slave's sync state and current sessionId); the other is SlavePartitions, which records master information.
The transport layer is divided into two sub-modules: ReplicationClient is responsible for initiating connections, and ReplicationServer is responsible for response packets. All partitions between any two instances share a single connection.
The logic layer handles MasterPartition sync events, syncing data to corresponding slaves.
After reading MasterPartition Binlog information, the BinlogOffsetInfo is recorded in the SlaveNode's own window.
The Binlog is temporarily stored in a pending send queue.
The auxiliary thread (Auxiliary thread) periodically sends data from the temporary pending send queue to the corresponding slave node via RM's transport layer.
Upon receiving the slave's BinlogSyncResponse, obtaining the slave's received BinlogOffset information, updating the SlaveNode window, and repeating step 1 to continue syncing.
To control each SlaveNode's sync speed and avoid a few SlaveNodes consuming too many resources, a window is set for each SlaveNode. As shown below, Pika received ack responses for BinlogOffset 100 to 200, removes elements with BinlogOffset 100 to 200 from the window, then continues sending BinlogOffset 1100 and 1200, adding the corresponding BinlogOffset to the window.
The logic layer handles SlavePartition sync events, receives sync data sent by the master, and sends corresponding response information to the master.
Based on parsed Partition information, binlog write tasks are assigned to corresponding threads.
After the thread writes binlog, it calls the transport layer to send BinlogSyncResponse.
Based on the binlog's key, write-to-DB tasks are assigned to corresponding threads.