主从同步,实现新的更改应用到slave上,实现同步slave未上线时间的更改。
This commit is contained in:
78
reactor.c
78
reactor.c
@@ -12,7 +12,7 @@
|
||||
#include <sys/epoll.h>
|
||||
#include <errno.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <sys/eventfd.h>
|
||||
|
||||
#include "server.h"
|
||||
|
||||
@@ -28,14 +28,17 @@
|
||||
#if ENABLE_KVSTORE
|
||||
|
||||
// typedef int (*msg_handler)(char *msg, int length, char *response);
|
||||
typedef int (*msg_handler)(char *request, int request_length, char *response, int *response_length);
|
||||
// typedef int (*msg_handler)(char *request, int request_length, char *response, int *response_length);
|
||||
typedef int (*msg_handler)(struct conn* conn);
|
||||
|
||||
extern int try_connect_master(char *ip, int port);
|
||||
|
||||
static msg_handler kvs_handler;
|
||||
|
||||
// 0 need more, -1 error, =1 suc
|
||||
int kvs_request(struct conn *c) {
|
||||
int consumed_out = kvs_handler(c->rbuffer, c->rlength, c->wbuffer, &c->wlength);
|
||||
// int consumed_out = kvs_handler(c->rbuffer, c->rlength, c->wbuffer, &c->wlength);
|
||||
int consumed_out = kvs_handler(c);
|
||||
return consumed_out;
|
||||
}
|
||||
|
||||
@@ -59,12 +62,14 @@ int send_cb(int fd);
|
||||
int epfd = 0;
|
||||
struct timeval begin;
|
||||
|
||||
int wakeup_fd = -1;
|
||||
|
||||
|
||||
struct conn conn_list[CONNECTION_SIZE] = {0};
|
||||
// fd
|
||||
|
||||
|
||||
// 1 add, 0 mod
|
||||
int set_event(int fd, int event, int flag) {
|
||||
|
||||
if (flag) { // non-zero add
|
||||
@@ -101,6 +106,8 @@ int event_register(int fd, int event) {
|
||||
memset(conn_list[fd].wbuffer, 0, BUFFER_LENGTH);
|
||||
conn_list[fd].wlength = 0;
|
||||
|
||||
conn_list[fd].is_from_master = 0;
|
||||
|
||||
set_event(fd, event, 1);
|
||||
}
|
||||
|
||||
@@ -250,9 +257,17 @@ int send_cb(int fd) {
|
||||
}
|
||||
#else
|
||||
// printf("wlength: %d\n", conn_list[fd].wlength);
|
||||
|
||||
pthread_mutex_lock(&conn_list[fd].g_sync_lock);
|
||||
if (conn_list[fd].wlength != 0) {
|
||||
// for(int i = 0;i < conn_list[fd].wlength; ++i){
|
||||
// printf("%02x", conn_list[fd].wbuffer[i]);
|
||||
// }
|
||||
// printf("\n");
|
||||
count = send(fd, conn_list[fd].wbuffer, conn_list[fd].wlength, 0);
|
||||
conn_list[fd].wlength = 0;
|
||||
}
|
||||
pthread_mutex_unlock(&conn_list[fd].g_sync_lock);
|
||||
|
||||
set_event(fd, EPOLLIN, 0);
|
||||
|
||||
@@ -262,7 +277,43 @@ int send_cb(int fd) {
|
||||
return count;
|
||||
}
|
||||
|
||||
// wakup fd
|
||||
|
||||
int handle_wakeup_fd_cb(int fd);
|
||||
|
||||
int init_wakeup_fd(void) {
|
||||
wakeup_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
|
||||
if (wakeup_fd < 0) {
|
||||
printf("eventfd failed: errno=%d %s\n", errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
conn_list[wakeup_fd].fd = wakeup_fd;
|
||||
conn_list[wakeup_fd].r_action.recv_callback = handle_wakeup_fd_cb;
|
||||
set_event(wakeup_fd, EPOLLIN, 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// EPOLLOUT
|
||||
void sync_wakeup(int fd) {
|
||||
if (wakeup_fd < 0) return;
|
||||
set_event(fd, EPOLLOUT, 0);
|
||||
|
||||
uint64_t one = 1;
|
||||
ssize_t n = write(wakeup_fd, &one, sizeof(one));
|
||||
}
|
||||
|
||||
int handle_wakeup_fd_cb(int fd) {
|
||||
uint64_t v;
|
||||
while (1) {
|
||||
ssize_t n = read(wakeup_fd, &v, sizeof(v));
|
||||
if (n == sizeof(v)) continue;
|
||||
if (n < 0 && errno == EAGAIN) break; // 已经读空
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int r_init_server(unsigned short port) {
|
||||
|
||||
@@ -284,13 +335,25 @@ int r_init_server(unsigned short port) {
|
||||
|
||||
}
|
||||
|
||||
int reactor_start(unsigned short port, msg_handler handler) {
|
||||
int reactor_start(unsigned short port, msg_handler handler, char *m_ip, int m_port) {
|
||||
|
||||
//unsigned short port = 2000;
|
||||
kvs_handler = handler;
|
||||
|
||||
epfd = epoll_create(1);
|
||||
|
||||
if(init_wakeup_fd() < 0){
|
||||
close(epfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// slave
|
||||
if(m_ip != NULL){
|
||||
int masterfd = try_connect_master(m_ip, m_port);
|
||||
event_register(masterfd, EPOLLIN);
|
||||
conn_list[masterfd].is_from_master = 1;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
|
||||
for (i = 0;i < MAX_PORTS;i ++) {
|
||||
@@ -299,7 +362,8 @@ int reactor_start(unsigned short port, msg_handler handler) {
|
||||
|
||||
conn_list[sockfd].fd = sockfd;
|
||||
conn_list[sockfd].r_action.recv_callback = accept_cb;
|
||||
|
||||
conn_list[sockfd].is_from_master = 0;
|
||||
|
||||
set_event(sockfd, EPOLLIN, 1);
|
||||
}
|
||||
|
||||
@@ -335,7 +399,9 @@ int reactor_start(unsigned short port, msg_handler handler) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (wakeup_fd >= 0) close(wakeup_fd);
|
||||
if (epfd >= 0) close(epfd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user