主从同步,实现新的更改应用到slave上,实现同步slave未上线时间的更改。
This commit is contained in:
70
kvs_slave.c
Normal file
70
kvs_slave.c
Normal file
@@ -0,0 +1,70 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include "server.h"
|
||||
|
||||
static int kvs_write_u8(uint8_t **pp, uint8_t v) {
|
||||
uint8_t *p = *pp;
|
||||
*p = v;
|
||||
*pp = p + 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int kvs_write_u32(uint8_t **pp, uint32_t v) {
|
||||
uint8_t *p = *pp;
|
||||
uint32_t be = htonl(v);
|
||||
memcpy(p, &be, 4);
|
||||
*pp = p + 4;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int try_connect_master(char *ip, int port){
|
||||
|
||||
struct sockaddr_in addr;
|
||||
memset(&addr, 0, sizeof(struct sockaddr));
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
addr.sin_addr.s_addr = inet_addr(ip);
|
||||
|
||||
int rt = 1;
|
||||
while(1){
|
||||
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if(fd < 0){
|
||||
continue;
|
||||
}
|
||||
|
||||
if(0 == connect(fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_in))){
|
||||
// [OP KVS_CMD_PSYNC=15][ARGC 1][ARGLEN 4][ARG offset]
|
||||
|
||||
char buf[100];
|
||||
char *p = buf;
|
||||
|
||||
kvs_write_u8((uint8_t**)&p, 15);
|
||||
kvs_write_u8((uint8_t**)&p, 1);
|
||||
|
||||
uint64_t len = sizeof(uint64_t);
|
||||
kvs_write_u32((uint8_t**)&p, len);
|
||||
|
||||
uint64_t offset = 0;
|
||||
memcpy(p, (void*)&offset, len);
|
||||
|
||||
p += len;
|
||||
|
||||
send(fd, buf, p-buf, 0);
|
||||
recv(fd, buf, 100, 0);
|
||||
return fd;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
Reference in New Issue
Block a user