| 1117 | } |
| 1118 | |
| 1119 | static int fetchClusterConfiguration() { |
| 1120 | int success = 1; |
| 1121 | redisContext *ctx = NULL; |
| 1122 | redisReply *reply = NULL; |
| 1123 | ctx = getRedisContext(config.hostip, config.hostport, config.hostsocket); |
| 1124 | if (ctx == NULL) { |
| 1125 | exit(1); |
| 1126 | } |
| 1127 | clusterNode *firstNode = createClusterNode((char *) config.hostip, |
| 1128 | config.hostport); |
| 1129 | if (!firstNode) {success = 0; goto cleanup;} |
| 1130 | reply = redisCommand(ctx, "CLUSTER NODES"); |
| 1131 | success = (reply != NULL); |
| 1132 | if (!success) goto cleanup; |
| 1133 | success = (reply->type != REDIS_REPLY_ERROR); |
| 1134 | if (!success) { |
| 1135 | if (config.hostsocket == NULL) { |
| 1136 | fprintf(stderr, "Cluster node %s:%d replied with error:\n%s\n", |
| 1137 | config.hostip, config.hostport, reply->str); |
| 1138 | } else { |
| 1139 | fprintf(stderr, "Cluster node %s replied with error:\n%s\n", |
| 1140 | config.hostsocket, reply->str); |
| 1141 | } |
| 1142 | goto cleanup; |
| 1143 | } |
| 1144 | char *lines = reply->str, *p, *line; |
| 1145 | while ((p = strstr(lines, "\n")) != NULL) { |
| 1146 | *p = '\0'; |
| 1147 | line = lines; |
| 1148 | lines = p + 1; |
| 1149 | char *name = NULL, *addr = NULL, *flags = NULL, *master_id = NULL; |
| 1150 | int i = 0; |
| 1151 | while ((p = strchr(line, ' ')) != NULL) { |
| 1152 | *p = '\0'; |
| 1153 | char *token = line; |
| 1154 | line = p + 1; |
| 1155 | switch(i++){ |
| 1156 | case 0: name = token; break; |
| 1157 | case 1: addr = token; break; |
| 1158 | case 2: flags = token; break; |
| 1159 | case 3: master_id = token; break; |
| 1160 | } |
| 1161 | if (i == 8) break; // Slots |
| 1162 | } |
| 1163 | if (!flags) { |
| 1164 | fprintf(stderr, "Invalid CLUSTER NODES reply: missing flags.\n"); |
| 1165 | success = 0; |
| 1166 | goto cleanup; |
| 1167 | } |
| 1168 | int myself = (strstr(flags, "myself") != NULL); |
| 1169 | int is_replica = (strstr(flags, "slave") != NULL || |
| 1170 | (master_id != NULL && master_id[0] != '-')); |
| 1171 | if (is_replica) continue; |
| 1172 | if (addr == NULL) { |
| 1173 | fprintf(stderr, "Invalid CLUSTER NODES reply: missing addr.\n"); |
| 1174 | success = 0; |
| 1175 | goto cleanup; |
| 1176 | } |
no test coverage detected