From: Yunkai Zhang <qiushu.zyk at taobao.com> Cluster subcommand callbacks, such as: cluster_shutdown/cluster_cleanup/..., share almost the same code, all of them send empty request which only contains request-header without body content. So let's abstract the common part of the them into a new function: send_empty_req() which could make code more compactness. Signed-off-by: Yunkai Zhang <qiushu.zyk at taobao.com> --- collie/cluster.c | 105 +++++++++++++------------------------------------------ 1 file changed, 24 insertions(+), 81 deletions(-) diff --git a/collie/cluster.c b/collie/cluster.c index 35bfa23..718f6c4 100644 --- a/collie/cluster.c +++ b/collie/cluster.c @@ -202,23 +202,22 @@ error: return EXIT_SYSFAIL; } -static int cluster_shutdown(int argc, char **argv) +/* + * Empty request only contains header, without body content. + */ +static int send_empty_req(struct sd_req *hdr) { int fd, ret; - struct sd_req hdr; - struct sd_rsp *rsp = (struct sd_rsp *)&hdr; + struct sd_rsp *rsp = (struct sd_rsp *)hdr; unsigned rlen, wlen; fd = connect_to(sdhost, sdport); if (fd < 0) return EXIT_SYSFAIL; - sd_init_req(&hdr, SD_OP_SHUTDOWN); - hdr.epoch = sd_epoch; - rlen = 0; wlen = 0; - ret = exec_req(fd, &hdr, NULL, &wlen, &rlen); + ret = exec_req(fd, hdr, NULL, &wlen, &rlen); close(fd); if (ret) { @@ -227,46 +226,36 @@ static int cluster_shutdown(int argc, char **argv) } if (rsp->result != SD_RES_SUCCESS) { - fprintf(stderr, "Shutdown failed: %s\n", - sd_strerror(rsp->result)); + fprintf(stderr, "failed: %s\n", sd_strerror(rsp->result)); return EXIT_FAILURE; } return EXIT_SUCCESS; } -static int restore_snap(uint32_t epoch) +static int cluster_shutdown(int argc, char **argv) { - int fd, ret; struct sd_req hdr; - struct sd_rsp *rsp = (struct sd_rsp *)&hdr; - unsigned rlen, wlen; - fd = connect_to(sdhost, sdport); - if (fd < 0) - return EXIT_SYSFAIL; + sd_init_req(&hdr, SD_OP_SHUTDOWN); + hdr.epoch = sd_epoch; - sd_init_req(&hdr, SD_OP_RESTORE); - hdr.obj.tgt_epoch = epoch; + return send_empty_req(&hdr); +} - rlen = 0; - wlen = 0; - ret = exec_req(fd, &hdr, NULL, &wlen, &rlen); - close(fd); +static int restore_snap(uint32_t epoch) +{ + int ret; + struct sd_req hdr; - if (ret) { - fprintf(stderr, "Failed to connect\n"); - return EXIT_SYSFAIL; - } + sd_init_req(&hdr, SD_OP_RESTORE); + hdr.obj.tgt_epoch = epoch; - if (rsp->result != SD_RES_SUCCESS) { - fprintf(stderr, "Restore failed: %s\n", - sd_strerror(rsp->result)); - return EXIT_FAILURE; - } + ret = send_empty_req(&hdr); + if (ret == EXIT_SUCCESS) + printf("Cluster restore to the snapshot %d\n", epoch); - printf("Cluster restore to the snapshot %d\n", epoch); - return EXIT_SUCCESS; + return ret; } static void print_list(void *buf, unsigned len) @@ -327,34 +316,11 @@ out: static int do_snapshot(void) { - int fd, ret; struct sd_req hdr; - struct sd_rsp *rsp = (struct sd_rsp *)&hdr; - unsigned rlen, wlen; - - fd = connect_to(sdhost, sdport); - if (fd < 0) - return EXIT_SYSFAIL; sd_init_req(&hdr, SD_OP_SNAPSHOT); - rlen = 0; - wlen = 0; - ret = exec_req(fd, &hdr, NULL, &wlen, &rlen); - close(fd); - - if (ret) { - fprintf(stderr, "Failed to connect\n"); - return EXIT_SYSFAIL; - } - - if (rsp->result != SD_RES_SUCCESS) { - fprintf(stderr, "Snapshot failed: %s\n", - sd_strerror(rsp->result)); - return EXIT_FAILURE; - } - - return EXIT_SUCCESS; + return send_empty_req(&hdr); } static int cluster_snapshot(int argc, char **argv) @@ -371,34 +337,11 @@ static int cluster_snapshot(int argc, char **argv) static int cluster_cleanup(int argc, char **argv) { - int fd, ret; struct sd_req hdr; - struct sd_rsp *rsp = (struct sd_rsp *)&hdr; - unsigned rlen, wlen; - - fd = connect_to(sdhost, sdport); - if (fd < 0) - return EXIT_SYSFAIL; sd_init_req(&hdr, SD_OP_CLEANUP); - rlen = 0; - wlen = 0; - ret = exec_req(fd, &hdr, NULL, &wlen, &rlen); - close(fd); - - if (ret) { - fprintf(stderr, "Failed to connect\n"); - return EXIT_SYSFAIL; - } - - if (rsp->result != SD_RES_SUCCESS) { - fprintf(stderr, "Cleanup failed: %s\n", - sd_strerror(rsp->result)); - return EXIT_FAILURE; - } - - return EXIT_SUCCESS; + return send_empty_req(&hdr); } #define RECOVER_PRINT \ -- 1.7.11.2 |