static int parse_ketama_config(VBUCKET_CONFIG_HANDLE vb, cJSON *config) { cJSON *json, *node, *hostname; char *buf; int ii; json = cJSON_GetObjectItem(config, "nodes"); if (json == NULL || json->type != cJSON_Array) { vb->errmsg = strdup("Expected array for nodes"); return -1; } vb->num_servers = cJSON_GetArraySize(json); if (vb->num_servers == 0) { vb->errmsg = strdup("Empty serverList"); return -1; } vb->servers = calloc(vb->num_servers, sizeof(struct server_st)); for (ii = 0; ii < vb->num_servers; ++ii) { node = cJSON_GetArrayItem(json, ii); if (node == NULL || node->type != cJSON_Object) { vb->errmsg = strdup("Expected object for nodes array item"); return -1; } buf = calloc(MAX_AUTORITY_SIZE, sizeof(char)); if (buf == NULL) { vb->errmsg = strdup("Failed to allocate storage for node authority"); return -1; } if (get_node_authority(vb, node, &buf, MAX_AUTORITY_SIZE) < 0) { return -1; } vb->servers[ii].authority = buf; hostname = cJSON_GetObjectItem(node, "hostname"); if (hostname == NULL || hostname->type != cJSON_String) { vb->errmsg = strdup("Expected string for node's hostname"); return -1; } buf = strdup(hostname->valuestring); if (buf == NULL) { vb->errmsg = strdup("Failed to allocate storage for hostname string"); return -1; } buf = substitute_localhost_marker(vb, buf); if (buf == NULL) { vb->errmsg = strdup("Failed to allocate storage for hostname string during $HOST substitution"); return -1; } vb->servers[ii].rest_api_authority = buf; } qsort(vb->servers, vb->num_servers, sizeof(struct server_st), server_cmp); update_ketama_continuum(vb); return 0; }
static VBUCKET_CONFIG_HANDLE parse_ketama_config(VBUCKET_CONFIG_HANDLE vb, cJSON *config) { cJSON *json, *node; char *buf; int ii; json = cJSON_GetObjectItem(config, "nodes"); if (json == NULL || json->type != cJSON_Array) { errstr = "Expected array for nodes"; return NULL; } vb->num_servers = cJSON_GetArraySize(json); if (vb->num_servers == 0) { errstr = "Empty serverList"; return NULL; } vb->servers = calloc(vb->num_servers, sizeof(struct server_st)); for (ii = 0; ii < vb->num_servers; ++ii) { node = cJSON_GetArrayItem(json, ii); if (node == NULL || node->type != cJSON_Object) { errstr = "Expected object for nodes array item"; return NULL; } buf = calloc(MAX_AUTORITY_SIZE, sizeof(char)); if (buf == NULL) { errstr = "Failed to allocate storage for node authority"; return NULL; } if (get_node_authority(node, buf, MAX_AUTORITY_SIZE) < 0) { return NULL; } vb->servers[ii].authority = buf; } update_ketama_continuum(vb); return vb; }
static int lookup_server_struct(struct vbucket_config_st *vb, cJSON *c) { char *authority = NULL; int idx = -1, ii; authority = calloc(MAX_AUTORITY_SIZE, sizeof(char)); if (authority == NULL) { errstr = "Failed to allocate storage for authority string"; return -1; } if (get_node_authority(c, authority, MAX_AUTORITY_SIZE) < 0) { free(authority); return -1; } for (ii = 0; ii < vb->num_servers; ++ii) { if (strcmp(vb->servers[ii].authority, authority) == 0) { idx = ii; break; } } free(authority); return idx; }