#include #include #include #include #include /* 创建并监听用于接收快照的 TCP 监听 socket,成功返回 listen fd,失败返回 -1 */ static int create_listen_socket(const char *ip, int port){ } /* 主动连接 master 指定地址,用于控制面通信(SSYNC / SREADY),返回连接 fd 或 -1 */ static int connect_master(const char *master_ip, int master_port){ } /* 通过控制连接向 master 发送 SSYNC 请求,声明本 slave 的快照接收地址 */ static int send_ssync(int ctrl_fd, const char *listen_ip, int listen_port){ } /* 接收并校验 master 对 SSYNC 的确认响应(如 +OK),成功返回 0 */ static int recv_ssync_ok(int ctrl_fd){ } /* 在快照监听 socket 上阻塞等待 master 的快照发送连接,返回已建立连接的 fd */ static int accept_snapshot_conn(int listen_fd){ } /* 从快照连接中接收完整快照数据并构建内存状态,确保快照已完全应用 */ static int recv_and_apply_snapshot(int snapshot_fd){ } /* 通过控制连接向 master 发送 SREADY 通知,表示快照已应用,slave 即将进入服务态 */ int send_sready(int ctrl_fd){ } int slave_bootstrap( const char *listen_ip, int listen_port, const char *master_ip, int master_port ) { int listen_fd = -1; int ctrl_fd = -1; int snap_fd = -1; /* 1. 监听 snapshot port */ listen_fd = create_listen_socket(listen_ip, listen_port); if (listen_fd < 0) { goto fail; } /* 2. 连接 master, 发送 SSYNC */ ctrl_fd = connect_master(master_ip, master_port); if (ctrl_fd < 0) { goto fail; } if (send_ssync(ctrl_fd, listen_ip, listen_port) < 0) { goto fail; } if (recv_ssync_ok(ctrl_fd) < 0) { goto fail; } close(ctrl_fd); ctrl_fd = -1; /* 3. accept snapshot 连接 */ snap_fd = accept_snapshot_conn(listen_fd); if (snap_fd < 0) { goto fail; } /* 4. 接收 snapshot */ if (recv_and_apply_snapshot(snap_fd) < 0) { goto fail; } close(snap_fd); snap_fd = -1; close(listen_fd); listen_fd = -1; /* 5. 通知 master 快照传输完毕 */ ctrl_fd = connect_master(master_ip, master_port); if (ctrl_fd >= 0) { send_sready(ctrl_fd); close(ctrl_fd); ctrl_fd = -1; } /* 6. bootstrap complete */ return 0; fail: if (snap_fd >= 0) close(snap_fd); if (ctrl_fd >= 0) close(ctrl_fd); if (listen_fd >= 0) close(listen_fd); return -1; }