as_status aerospike_job_info( aerospike* as, as_error* err, const as_policy_info* policy, const char* module, uint64_t job_id, bool stop_if_in_progress, as_job_info* info) { as_error_reset(err); if (! policy) { policy = &as->config.policies.info; } char command[128]; sprintf(command, "jobs:module=%s;cmd=get-job;trid=%" PRIu64 "\n", module, job_id); info->status = AS_JOB_STATUS_UNDEF; info->progress_pct = 0; info->records_read = 0; as_status status = AEROSPIKE_ERR_CLUSTER; uint64_t deadline = as_socket_deadline(policy->timeout); as_cluster* cluster = as->cluster; as_nodes* nodes = as_nodes_reserve(cluster); for (uint32_t i = 0; i < nodes->size; i++) { as_node* node = nodes->array[i]; struct sockaddr_in* sa_in = as_node_get_address(node); char* response = 0; status = as_info_command_host(cluster, err, sa_in, command, true, deadline, &response); if (status == AEROSPIKE_OK) { as_job_process(response, info); free(response); if (stop_if_in_progress && info->status == AS_JOB_STATUS_INPROGRESS) { break; } } else if (status == AEROSPIKE_ERR_RECORD_NOT_FOUND) { if (info->status == AS_JOB_STATUS_UNDEF) { info->status = AS_JOB_STATUS_COMPLETED; } as_error_reset(err); status = AEROSPIKE_OK; } else { if (status != AEROSPIKE_ERR_CLUSTER) { break; } } } as_nodes_release(nodes); return status; }
/** * Send an info request to a specific socket address. The response must be freed by the caller on success. * * ~~~~~~~~~~{.c} * char * res = NULL; * if ( aerospike_info_socket_address(&as, &err, NULL, &socket_addr, "info", &res) != AEROSPIKE_OK ) { * // handle error * } * else { * // handle response * free(res); * res = NULL; * } * ~~~~~~~~~~ * * @param as The aerospike instance to use for this operation. * @param err The as_error to be populated if an error occurs. * @param policy The policy to use for this operation. If NULL, then the default policy will be used. * @param sa_in The IP address and port to send the request to. * @param req The info request to send. * @param res The response from the node. The response will be a NULL terminated string, allocated by the function, and must be freed by the caller. * * @return AEROSPIKE_OK on success. Otherwise an error. * * @ingroup info_operations */ as_status aerospike_info_socket_address( aerospike * as, as_error * err, const as_policy_info * policy, struct sockaddr_in* sa_in, const char * req, char ** res) { as_error_reset(err); if (! policy) { policy = &as->config.policies.info; } uint64_t deadline = as_socket_deadline(policy->timeout); return as_info_command_host(as->cluster, err, sa_in, (char*)req, policy->send_as_is, deadline, res); }
static as_status as_admin_execute(aerospike* as, as_error* err, const as_policy_admin* policy, uint8_t* buffer, uint8_t* end) { uint32_t timeout_ms = (policy)? policy->timeout : as->config.policies.admin.timeout; if (timeout_ms <= 0) { timeout_ms = DEFAULT_TIMEOUT; } uint64_t deadline_ms = as_socket_deadline(timeout_ms); as_cluster* cluster = as->cluster; as_node* node = as_node_get_random(cluster); if (! node) { return as_error_set_message(err, AEROSPIKE_ERR_CLIENT, "Failed to find server node."); } int fd; as_status status = as_node_get_connection(err, node, deadline_ms, &fd); if (status) { as_node_release(node); return status; } status = as_admin_send(err, fd, buffer, end, deadline_ms); if (status) { as_node_close_connection(node, fd); as_node_release(node); return status; } status = as_socket_read_deadline(err, fd, buffer, HEADER_SIZE, deadline_ms); if (status) { as_node_close_connection(node, fd); as_node_release(node); return status; } as_node_put_connection(node, fd); as_node_release(node); status = buffer[RESULT_CODE]; if (status) { return as_error_set_message(err, status, as_error_string(status)); } return status; }
/** * Send an info request to a specific host. The response must be freed by the caller on success. * * ~~~~~~~~~~{.c} * char * res = NULL; * if ( aerospike_info_host(&as, &err, NULL, "127.0.0.1", 3000, "info", &res) != AEROSPIKE_OK ) { * // handle error * } * else { * // handle response * free(res); * res = NULL; * } * ~~~~~~~~~~ * * @param as The aerospike instance to use for this operation. * @param err The as_error to be populated if an error occurs. * @param policy The policy to use for this operation. If NULL, then the default policy will be used. * @param addr The IP address or hostname to send the request to. * @param port The port to send the request to. * @param req The info request to send. * @param res The response from the node. The response will be a NULL terminated string, allocated by the function, and must be freed by the caller. * * @return AEROSPIKE_OK on success. Otherwise an error. * * @ingroup info_operations */ as_status aerospike_info_host( aerospike * as, as_error * err, const as_policy_info * policy, const char * addr, uint16_t port, const char * req, char ** res) { as_error_reset(err); if (! policy) { policy = &as->config.policies.info; } as_vector sockaddr_in_v; as_vector_inita(&sockaddr_in_v, sizeof(struct sockaddr_in), 5); as_status status = as_lookup(NULL, err, (char*)addr, port, &sockaddr_in_v); if (status) { as_vector_destroy(&sockaddr_in_v); return status; } uint64_t deadline = as_socket_deadline(policy->timeout); as_cluster* cluster = as->cluster; status = AEROSPIKE_ERR_CLUSTER; bool loop = true; for (uint32_t i = 0; i < sockaddr_in_v.size && loop; i++) { struct sockaddr_in* sa_in = as_vector_get(&sockaddr_in_v, i); status = as_info_command_host(cluster, err, sa_in, (char*)req, policy->send_as_is, deadline, res); switch (status) { case AEROSPIKE_OK: case AEROSPIKE_ERR_TIMEOUT: case AEROSPIKE_ERR_INDEX_FOUND: case AEROSPIKE_ERR_INDEX_NOT_FOUND: loop = false; break; default: break; } } as_vector_destroy(&sockaddr_in_v); return status; }
static as_status as_admin_read_list(aerospike* as, as_error* err, const as_policy_admin* policy, uint8_t* command, uint8_t* end, as_admin_parse_fn parse_fn, as_vector* list) { int timeout_ms = (policy)? policy->timeout : as->config.policies.admin.timeout; if (timeout_ms <= 0) { timeout_ms = DEFAULT_TIMEOUT; } uint64_t deadline_ms = as_socket_deadline(timeout_ms); as_cluster* cluster = as->cluster; as_node* node = as_node_get_random(cluster); if (! node) { return as_error_set_message(err, AEROSPIKE_ERR_CLIENT, "Failed to find server node."); } int fd; as_status status = as_node_get_connection(err, node, deadline_ms, &fd); if (status) { as_node_release(node); return status; } status = as_admin_send(err, fd, command, end, deadline_ms); if (status) { as_node_close_connection(node, fd); as_node_release(node); return status; } status = as_admin_read_blocks(err, fd, deadline_ms, parse_fn, list); if (status) { as_node_close_connection(node, fd); as_node_release(node); return status; } as_node_put_connection(node, fd); as_node_release(node); return status; }
/** * Send an info request to the entire cluster. * * ~~~~~~~~~~{.c} * if ( aerospike_info_foreach(&as, &err, NULL, "info", callback, NULL) != AEROSPIKE_OK ) { * // handle error * } * ~~~~~~~~~~ * * The callback takes a response string. The caller should not free this string. * * ~~~~~~~~~~{.c} * bool callback(const as_error * err, const as_node * node, const char * req, char * res, void * udata) { * // handle response * } * ~~~~~~~~~~ * * * @param as The aerospike instance to use for this operation. * @param err The as_error to be populated if an error occurs. * @param policy The policy to use for this operation. If NULL, then the default policy will be used. * @param req The info request to send. * @param callback The function to call when a response is received. * @param udata User-data to send to the callback. * * @return AEROSPIKE_OK on success. Otherwise an error. * * @ingroup info_operations */ as_status aerospike_info_foreach( aerospike * as, as_error * err, const as_policy_info * policy, const char * req, aerospike_info_foreach_callback callback, void * udata) { as_error_reset(err); if (! policy) { policy = &as->config.policies.info; } as_status status = AEROSPIKE_ERR_CLUSTER; uint64_t deadline = as_socket_deadline(policy->timeout); as_cluster* cluster = as->cluster; as_nodes* nodes = as_nodes_reserve(cluster); for (uint32_t i = 0; i < nodes->size; i++) { as_node* node = nodes->array[i]; struct sockaddr_in* sa_in = as_node_get_address(node); char* response = 0; status = as_info_command_host(cluster, err, sa_in, (char*)req, policy->send_as_is, deadline, &response); if (status == AEROSPIKE_OK) { bool result = callback(err, node, req, response, udata); free(response); if (! result) { status = AEROSPIKE_ERR_QUERY_ABORTED; break; } } else { if (status != AEROSPIKE_ERR_CLUSTER) { break; } } } as_nodes_release(nodes); return status; }
/** * Send an info request to a node in the cluster. If node request fails, send request to the next * node in the cluster. Repeat until the node request succeeds. The response must be freed by * the caller on success. * * ~~~~~~~~~~{.c} * char * res = NULL; * if ( aerospike_info_any(&as, &err, NULL, "info", &res) != AEROSPIKE_OK ) { * // handle error * } * else { * // handle response * free(res); * res = NULL; * } * ~~~~~~~~~~ * * @param as The aerospike instance to use for this operation. * @param err The as_error to be populated if an error occurs. * @param policy The policy to use for this operation. If NULL, then the default policy will be used. * @param req The info request to send. * @param res The response from the node. The response will be a NULL terminated string, allocated by the function, and must be freed by the caller. * * @return AEROSPIKE_OK on success. Otherwise an error. * * @ingroup info_operations */ as_status aerospike_info_any( aerospike * as, as_error * err, const as_policy_info * policy, const char * req, char ** res) { as_error_reset(err); if (! policy) { policy = &as->config.policies.info; } as_status status = AEROSPIKE_ERR_CLUSTER; uint64_t deadline = as_socket_deadline(policy->timeout); as_cluster* cluster = as->cluster; as_nodes* nodes = as_nodes_reserve(cluster); bool loop = true; for (uint32_t i = 0; i < nodes->size && loop; i++) { as_node* node = nodes->array[i]; struct sockaddr_in* sa_in = as_node_get_address(node); status = as_info_command_host(cluster, err, sa_in, (char*)req, policy->send_as_is, deadline, res); switch (status) { case AEROSPIKE_OK: case AEROSPIKE_ERR_TIMEOUT: case AEROSPIKE_ERR_INDEX_FOUND: case AEROSPIKE_ERR_INDEX_NOT_FOUND: loop = false; break; default: break; } } as_nodes_release(nodes); return status; }
static as_status as_lookup_node(as_cluster* cluster, as_error* err, struct sockaddr_in* addr, as_node_info* node_info) { uint64_t deadline = as_socket_deadline(cluster->conn_timeout_ms); int fd; as_status status = as_info_create_socket(cluster, err, addr, deadline, &fd); if (status) { return status; } char* response = 0; status = as_info_command(err, fd, "node\nfeatures\n", true, deadline, 0, &response); if (status) { as_close(fd); return status; } as_vector values; as_vector_inita(&values, sizeof(as_name_value), 2); as_info_parse_multi_response(response, &values); if (values.size != 2) { goto Error; } as_name_value* nv = as_vector_get(&values, 0); char* node_name = nv->value; if (node_name == 0 || *node_name == 0) { goto Error; } as_strncpy(node_info->name, node_name, AS_NODE_NAME_SIZE); node_info->fd = fd; nv = as_vector_get(&values, 1); char* features = nv->value; if (features == 0) { goto Error; } char* begin = features; char* end = begin; uint8_t has_batch_index = 0; uint8_t has_replicas_all = 0; uint8_t has_double = 0; uint8_t has_geo = 0; while (*begin && ! (has_batch_index && has_replicas_all && has_double && has_geo)) { while (*end) { if (*end == ';') { *end++ = 0; break; } end++; } if (strcmp(begin, "batch-index") == 0) { has_batch_index = 1; } if (strcmp(begin, "replicas-all") == 0) { has_replicas_all = 1; } if (strcmp(begin, "float") == 0) { has_double = 1; } if (strcmp(begin, "geo") == 0) { has_geo = 1; } begin = end; } node_info->has_batch_index = has_batch_index; node_info->has_replicas_all = has_replicas_all; node_info->has_double = has_double; node_info->has_geo = has_geo; cf_free(response); return AEROSPIKE_OK; Error: { char addr_name[INET_ADDRSTRLEN]; as_socket_address_name(addr, addr_name); as_error_update(err, status, "Invalid node info response from %s: %s", addr_name, response); cf_free(response); as_close(fd); return AEROSPIKE_ERR_CLIENT; } }
as_status as_command_execute(as_cluster* cluster, as_error * err, as_command_node* cn, uint8_t* command, size_t command_len, uint32_t timeout_ms, uint32_t retry, as_parse_results_fn parse_results_fn, void* parse_results_data ) { uint64_t deadline_ms = as_socket_deadline(timeout_ms); uint32_t sleep_between_retries_ms = 0; uint32_t failed_nodes = 0; uint32_t failed_conns = 0; uint32_t iterations = 0; bool release_node; // Execute command until successful, timed out or maximum iterations have been reached. while (true) { as_node* node; if (cn->node) { node = cn->node; release_node = false; } else { node = as_node_get(cluster, cn->ns, cn->digest, cn->write, cn->replica); release_node = true; } if (!node) { failed_nodes++; sleep_between_retries_ms = 10; goto Retry; } int fd; as_status status = as_node_get_connection(err, node, deadline_ms, &fd); if (status) { if (release_node) { as_node_release(node); } failed_conns++; sleep_between_retries_ms = 1; goto Retry; } // Send command. status = as_socket_write_deadline(err, fd, command, command_len, deadline_ms); if (status) { // Socket errors are considered temporary anomalies. Retry. // Close socket to flush out possible garbage. Do not put back in pool. as_close(fd); if (release_node) { as_node_release(node); } sleep_between_retries_ms = 0; goto Retry; } // Parse results returned by server. status = parse_results_fn(err, fd, deadline_ms, parse_results_data); if (status == AEROSPIKE_OK) { // Reset error code if retry had occurred. if (iterations > 0) { as_error_reset(err); } } else { switch (status) { // Retry on timeout. case AEROSPIKE_ERR_TIMEOUT: as_close(fd); if (release_node) { as_node_release(node); } sleep_between_retries_ms = 0; goto Retry; // Close socket on errors that can leave unread data in socket. case AEROSPIKE_ERR_QUERY_ABORTED: case AEROSPIKE_ERR_SCAN_ABORTED: case AEROSPIKE_ERR_CLIENT_ABORT: case AEROSPIKE_ERR_CLIENT: as_close(fd); if (release_node) { as_node_release(node); } err->code = status; return status; default: err->code = status; break; } } // Put connection back in pool. as_node_put_connection(node, fd, cluster->conn_queue_size); // Release resources. if (release_node) { as_node_release(node); } return status; Retry: // Check if max retries reached. if (++iterations > retry) { break; } // Check for client timeout. if (deadline_ms > 0) { int remaining_ms = (int)(deadline_ms - cf_getms() - sleep_between_retries_ms); if (remaining_ms <= 0) { break; } // Reset timeout in send buffer (destined for server). *(uint32_t*)(command + 22) = cf_swap_to_be32(remaining_ms); } if (sleep_between_retries_ms > 0) { // Sleep before trying again. usleep(sleep_between_retries_ms * 1000); } } return as_error_update(err, AEROSPIKE_ERR_TIMEOUT, "Client timeout: timeout=%d iterations=%u failedNodes=%u failedConns=%u", timeout_ms, iterations, failed_nodes, failed_conns); }