Skip to content

Commit

Permalink
Check return value of libcouchbase_apply_vbucket_config()
Browse files Browse the repository at this point in the history
Change-Id: If47504d6dbe3d60ce28fa7e71498ea49e47389ca
Reviewed-on: http://review.couchbase.org/16132
Tested-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Tested-by: Mordechai Nunberg <mnunberg@haskalah.org>
Reviewed-by: Mordechai Nunberg <mnunberg@haskalah.org>
Reviewed-by: Sergey Avseyev <sergey.avseyev@gmail.com>
  • Loading branch information
avsej committed May 23, 2012
1 parent 36afdb2 commit 922d22b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
42 changes: 31 additions & 11 deletions src/instance.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ static int sasl_get_password(sasl_conn_t *conn, void *context, int id,
return SASL_OK;
}

void libcouchbase_apply_vbucket_config(libcouchbase_t instance, VBUCKET_CONFIG_HANDLE config)
libcouchbase_error_t libcouchbase_apply_vbucket_config(libcouchbase_t instance, VBUCKET_CONFIG_HANDLE config)
{
libcouchbase_uint16_t ii, max;
libcouchbase_size_t num;
Expand All @@ -315,17 +315,22 @@ void libcouchbase_apply_vbucket_config(libcouchbase_t instance, VBUCKET_CONFIG_H
{ SASL_CB_LIST_END, NULL, NULL }
};

instance->vbucket_config = config;
num = (libcouchbase_size_t)vbucket_config_get_num_servers(config);
instance->nservers = num;
/* servers array should be freed in the caller */
instance->servers = calloc(num, sizeof(libcouchbase_server_t));
instance->vbucket_config = config;

if (instance->servers == NULL) {
return libcouchbase_error_handler(instance, LIBCOUCHBASE_ENOMEM, "Failed to allocate memory");
}
instance->nservers = num;
free_backup_nodes(instance);
instance->backup_nodes = calloc(num, sizeof(char *));
if (instance->backup_nodes == NULL) {
return libcouchbase_error_handler(instance, LIBCOUCHBASE_ENOMEM, "Failed to allocate memory");
}
instance->nbackup_nodes = num;

sprintf(curnode, "%s:%s", instance->host, instance->port);
for (ii = 0; ii < (libcouchbase_size_t)num; ++ii) {
snprintf(curnode, sizeof(curnode), "%s:%s", instance->host, instance->port);
for (ii = 0; ii < num; ++ii) {
instance->servers[ii].instance = instance;
libcouchbase_server_initialize(instance->servers + ii, (int)ii);
if (strcmp(curnode, instance->servers[ii].rest_api_server) == 0) {
Expand All @@ -350,8 +355,7 @@ void libcouchbase_apply_vbucket_config(libcouchbase_t instance, VBUCKET_CONFIG_H
if (instance->sasl.password.secret.len < sizeof(instance->sasl.password.buffer) - offsetof(sasl_secret_t, data)) {
memcpy(instance->sasl.password.secret.data, passwd, instance->sasl.password.secret.len);
} else {
libcouchbase_error_handler(instance, LIBCOUCHBASE_EINVAL, "Password too long");
return;
return libcouchbase_error_handler(instance, LIBCOUCHBASE_EINVAL, "Password too long");
}
}
memcpy(instance->sasl.callbacks, sasl_callbacks, sizeof(sasl_callbacks));
Expand All @@ -365,9 +369,13 @@ void libcouchbase_apply_vbucket_config(libcouchbase_t instance, VBUCKET_CONFIG_H
instance->nvbuckets = max;
free(instance->vb_server_map);
instance->vb_server_map = calloc(max, sizeof(libcouchbase_vbucket_t));
if (instance->vb_server_map == NULL) {
return libcouchbase_error_handler(instance, LIBCOUCHBASE_ENOMEM, "Failed to allocate memory");
}
for (ii = 0; ii < max; ++ii) {
instance->vb_server_map[ii] = vbucket_get_master(instance->vbucket_config, ii);
}
return LIBCOUCHBASE_SUCCESS;
}

static void relocate_packets(libcouchbase_server_t *src,
Expand Down Expand Up @@ -443,6 +451,7 @@ static void libcouchbase_update_serverlist(libcouchbase_t instance)
instance->vbucket_stream.input.data) != 0) {
libcouchbase_error_handler(instance, LIBCOUCHBASE_PROTOCOL_ERROR,
vbucket_get_error_message(next_config));
vbucket_config_destroy(next_config);
return;
}
instance->vbucket_stream.input.avail = 0;
Expand All @@ -453,7 +462,11 @@ static void libcouchbase_update_serverlist(libcouchbase_t instance)
VBUCKET_DISTRIBUTION_TYPE dist_t = vbucket_config_get_distribution_type(next_config);
nservers = instance->nservers;
servers = instance->servers;
libcouchbase_apply_vbucket_config(instance, next_config);
if (libcouchbase_apply_vbucket_config(instance, next_config) != LIBCOUCHBASE_SUCCESS) {
vbucket_free_diff(diff);
vbucket_config_destroy(next_config);
return;
}
for (ii = 0; ii < nservers; ++ii) {
ss = servers + ii;
if (dist_t == VBUCKET_DISTRIBUTION_VBUCKET) {
Expand All @@ -466,6 +479,7 @@ static void libcouchbase_update_serverlist(libcouchbase_t instance)
}
libcouchbase_server_destroy(ss);
}
free(servers);

/* Destroy old config */
vbucket_config_destroy(curr_config);
Expand All @@ -483,10 +497,16 @@ static void libcouchbase_update_serverlist(libcouchbase_t instance)
} else {
vbucket_config_destroy(next_config);
}
if (diff) {
vbucket_free_diff(diff);
}
} else {
assert(instance->servers == NULL);
assert(instance->nservers == 0);
libcouchbase_apply_vbucket_config(instance, next_config);
if (libcouchbase_apply_vbucket_config(instance, next_config) != LIBCOUCHBASE_SUCCESS) {
vbucket_config_destroy(next_config);
return;
}

/* Notify anyone interested in this event... */
if (instance->vbucket_state_listener != NULL) {
Expand Down
4 changes: 2 additions & 2 deletions src/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ extern "C" {
struct addrinfo **curr_ai,
int *connerr);

void libcouchbase_apply_vbucket_config(libcouchbase_t instance,
VBUCKET_CONFIG_HANDLE config);
libcouchbase_error_t libcouchbase_apply_vbucket_config(libcouchbase_t instance,
VBUCKET_CONFIG_HANDLE config);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 922d22b

Please sign in to comment.