• 通过扩展redis-cli来实现在redis集群中移动槽位


    下面的扩展代码基于redis 5.0.2进行扩展, 对于其他的redis版本, 我没有进行相关的测试。考虑到redis集群的修改频率,这段代码应该同时适用于其他的redis版本。

    下面为修改的代码:

    static clusterManagerNode *clusterManagerByIpPort(const char* ip, int port);
    static
    int clusterManagerCommandMoveSlot(int argc, char **argv);
    {"move-slot", clusterManagerCommandMoveSlot, 3,
    "from-host:from-port to-host:to-port slotNo", NULL},
    // 根据ip和端口号来获取对应的redis集群节点
    clusterManagerNode *clusterManagerByIpPort(const char* ip, int port) { if (cluster_manager.nodes == NULL) return NULL; clusterManagerNode *found = NULL; sds lcip = sdsempty(); lcip = sdscpy(lcip, ip); sdstolower(lcip); listIter li; listNode *ln; listRewind(cluster_manager.nodes, &li); while ((ln = listNext(&li)) != NULL) { clusterManagerNode *n = ln->value; if (n->ip && !sdscmp(n->ip, lcip) && (n->port == port)) { found = n; break; } } sdsfree(lcip); return found; }
    // 在redis集群中实现移动槽位功能
    static
    int clusterManagerCommandMoveSlot(int argc, char **argv) { assert(argc == 3); char *to_ip = NULL, *from_ip = NULL; int to_port = 0, from_port = 0; if (!getClusterHostFromCmdArgs(1, argv + 1, &to_ip, &to_port)) goto invalid_args; if (!getClusterHostFromCmdArgs(1, argv, &from_ip, &from_port)) goto invalid_args; char *end = NULL; char *slotStr = argv[2]; int slotNo = strtol(slotStr, &end, 10); if ((size_t)(end - slotStr) != strlen(slotStr)) { clusterManagerLogErr("*** the slotNo should be number. "); return 0; } clusterManagerLogInfo("Moving slot number: %d from node %s:%d to node %s:%d ", slotNo, from_ip, from_port, to_ip, to_port); clusterManagerNode *tonode = clusterManagerNewNode(to_ip, to_port); if (!clusterManagerLoadInfoFromNode(tonode, 0)) return 0; clusterManagerCheckCluster(0); if (cluster_manager.errors && listLength(cluster_manager.errors) > 0) { fflush(stdout); fprintf(stderr, "*** Please fix your cluster problems before moving slot "); return 0; } clusterManagerNode *fromnode = clusterManagerByIpPort(from_ip, from_port); // check fromnode and tonode is valid const char *invalid_node_msg = "*** The specified node (%s:%d) is not known " "or not a master, please retry. "; if (!fromnode || (fromnode->flags & CLUSTER_MANAGER_FLAG_SLAVE)) { clusterManagerLogErr(invalid_node_msg, from_ip, from_port); return 0; } else if (!tonode || (tonode->flags & CLUSTER_MANAGER_FLAG_SLAVE)) { clusterManagerLogErr(invalid_node_msg, to_ip, to_port); return 0; } else if (!strcmp(fromnode->name, tonode->name) && (fromnode->port == tonode->port)){ clusterManagerLogErr( "*** It is not possible to use " "the target node as " "source node. "); return 0; } // check fromnode has the slot if (slotNo < 0 || slotNo >= CLUSTER_MANAGER_SLOTS) { // Don't give a range hint for using it when someone is familiar with redis clusterManagerLogErr("*** The slotNo is invalid, slotNo: %d ", slotNo); return 0; } else if (fromnode->slots[slotNo] == (uint8_t)(0)) { clusterManagerLogErr("*** From node don't have the slot: %d ", slotNo); return 0; } int opts = CLUSTER_MANAGER_OPT_VERBOSE | CLUSTER_MANAGER_OPT_UPDATE; char *err = NULL; int result = clusterManagerMoveSlot(fromnode, tonode, slotNo, opts, &err); if (!result) { if (err != NULL) { //clusterManagerLogErr(" %s ", err); zfree(err); } } else { clusterManagerLogInfo("Moving finished with success. "); } return result; invalid_args: fprintf(stderr, CLUSTER_MANAGER_INVALID_HOST_ARG); return 0; }

    上述代码, 就是新增的在集群中移动槽位的功能, 使用方法如下:

    (1)在redis-cli.c文件中添加上述代码

    (2)在redis根目录调用make命令, 编译代码

    (3)对编译生成的redis-cli执行如下的命令:

      $ src/redis-cli --cluster help

    其中的move-slot from-host:from-port to-host:to-port slotNo就是新增的子命令。假设192.168.5.1:6379分配了槽位0-5460,192.168.5.2:6379分配了槽位5461-10922,192.168.5.3:6379分配了槽位10923-16383, 通过调用src/redis-cli --cluster move-slot 192.168.5.1:6379 192.168.5.2:6379 10,这样192.168.5.1:6379当前拥有槽位0-9,11-5460,而192.168.5.2:6379拥有槽位10,5461-10922。

    对于redis5.0.2的redis-cli.c文件的修改后的文件可以参考:

    https://github.com/ss-torres/redis-cli-addition.git

    如果有什么好的建议或者提议,欢迎提出。

  • 相关阅读:
    递归--数字黑洞--蓝桥杯
    王、后问题
    递归--简单题--求二项式值
    有问题的题
    LeetCode----994. 腐烂的橘子「深度优先搜索」
    SpringBoot ---- MyBatis Plus 入门
    Spring Boot ---- 整合 MyBatis (注解方式)
    Android笔记
    LeetCode----跳跃游戏Ⅱ「动态规划」
    2020年米哈游秋季招聘程序 B卷编程题
  • 原文地址:https://www.cnblogs.com/albizzia/p/10822405.html
Copyright © 2020-2023  润新知