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