static int notify_complete(struct dnet_net_state *state, struct dnet_cmd *cmd, void *priv) { struct dnet_io_notification *io; char str[64]; struct tm tm; struct timeval tv; FILE *stream = priv; if (is_trans_destroyed(state, cmd)) return 0; if (cmd->size != sizeof(struct dnet_io_notification)) return 0; gettimeofday(&tv, NULL); localtime_r((time_t *)&tv.tv_sec, &tm); strftime(str, sizeof(str), "%F %R:%S", &tm); fprintf(stream, "%s.%06lu : ", str, (unsigned long)tv.tv_usec); io = (struct dnet_io_notification *)(cmd + 1); dnet_convert_io_notification(io); fprintf(stream, "%s: client: %s, size: %llu, offset: %llu, flags: %x\n", dnet_dump_id_str(io->io.id), dnet_server_convert_dnet_addr(&io->addr.addr), (unsigned long long)io->io.size, (unsigned long long)io->io.offset, io->io.flags); fflush(stream); return 0; }
void iterate_node(Ctx &ctx, const dnet_addr &node) { std::cout << "Iterating node: " << dnet_server_convert_dnet_addr(const_cast<dnet_addr*>(&node)) << ":" << node.family << std::endl; std::vector<dnet_iterator_range> ranges; if (ctx.iflags & DNET_IFLAGS_KEY_RANGE) ranges.push_back(ctx.key_range); dnet_id id; bool found = false; for (auto it = ctx.routes.begin(), end = ctx.routes.end(); it != end; ++it) { if (dnet_addr_equal(&it->second, const_cast<dnet_addr*>(&node))) { id = it->first; found = true; break; } } if (!found) { std::cerr << "Node was'n found in route list" << std::endl; return; } ctx.session->set_groups(std::vector<int>(1, id.group_id)); auto res = ctx.session->start_iterator(ioremap::elliptics::key(id), ranges, DNET_ITYPE_NETWORK, ctx.iflags, ctx.time_begin, ctx.time_end); char buffer[2*DNET_ID_SIZE + 1] = {0}; for (auto it = res.begin(), end = res.end(); it != end; ++it) { std::cout << "node: " << dnet_server_convert_dnet_addr(const_cast<dnet_addr*>(&node)) << node.family << ", key: " << dnet_dump_id_len_raw(it->reply()->key.id, DNET_ID_SIZE, buffer) << ", flags: " << it->reply()->user_flags << ", ts: " << it->reply()->timestamp.tsec << "/" << it->reply()->timestamp.tnsec << " size: " << it->reply_data().size() << " data: " << it->reply_data().to_string() << std::endl; } }
static std::set<std::tuple<std::string, int, uint32_t>> get_unique_hosts(session &sess) { std::vector<dnet_route_entry> routes = sess.get_routes(); std::set<std::tuple<std::string, int, uint32_t>> unique_hosts; for (auto it = routes.begin(); it != routes.end(); ++it) { dnet_route_entry &entry = *it; std::string addr = dnet_server_convert_dnet_addr(&entry.addr); unique_hosts.insert(std::make_tuple(addr, entry.group_id, entry.backend_id)); } return unique_hosts; }
int dnet_ids_update(int update_local, const char *file, struct dnet_addr *cfg_addrs, char *remotes) { char remote_id[1024]; sprintf(remote_id, "elliptics_node_ids_%s", dnet_server_convert_dnet_addr(cfg_addrs)); ioremap::elliptics::file_logger log("/dev/null", 0); ioremap::elliptics::node node(log); ioremap::elliptics::session session(node); session.set_exceptions_policy(ioremap::elliptics::session::no_exceptions); std::stringstream remotes_stream; remotes_stream << remotes; std::string addr; while (std::getline(remotes_stream, addr, ' ')) { try { node.add_remote(addr.c_str()); } catch(...) {} } auto routes = session.get_routes(); std::set<int> groups_set; for(auto it = routes.begin(), end = routes.end(); it != end; ++it) { groups_set.insert(it->first.group_id); } session.set_groups(std::vector<int>(groups_set.begin(), groups_set.end())); try { if (update_local) session.read_file(std::string(remote_id), file, 0, 0); else session.write_file(std::string(remote_id), file, 0, 0, 0); } catch(...) { return -1; } return 0; }
static int dnet_bulk_add_id(struct dnet_node *n, struct dnet_bulk_array *bulk_array, struct dnet_id *id, struct dnet_meta_container *mc, struct dnet_check_params *params) { int err = 0; struct dnet_bulk_state tmp; struct dnet_bulk_state *state = NULL; struct dnet_net_state *st = dnet_state_get_first(n, id); struct dnet_bulk_id *bulk_id; struct dnet_meta_update mu; dnet_log(n, DNET_LOG_DEBUG, "BULK: adding ID %s to array\n", dnet_dump_id(id)); if (!st) return -1; memcpy(&tmp.addr, &st->addr, sizeof(struct dnet_addr)); dnet_state_put(st); dnet_log(n, DNET_LOG_DEBUG, "BULK: Searching state in states array\n"); state = bsearch(&tmp, bulk_array->states, bulk_array->num, sizeof(struct dnet_bulk_state), dnet_compare_bulk_state); if (!state) return -1; if (!dnet_get_meta_update(n, mc, &mu)) return -ENOENT; dnet_log(n, DNET_LOG_DEBUG, "BULK: addr = %s state->num = %d\n", dnet_server_convert_dnet_addr(&state->addr), state->num); //pthread_mutex_lock(&state->state_lock); if (state->num >= DNET_BULK_IDS_SIZE || state->num < 0) goto err_out_unlock; bulk_id = &state->ids[state->num]; memset(bulk_id, 0, sizeof(struct dnet_bulk_id)); memcpy(&bulk_id->id, &id->id, DNET_ID_SIZE); dnet_log(n, DNET_LOG_DEBUG, "BULK: ID: %s, last_update->tsec=%llu, last_update->tnsec=%llu, flags=%02llx\n", dnet_dump_id_str(bulk_id->id.id), (unsigned long long)mu.tm.tsec, (unsigned long long)mu.tm.tnsec, (unsigned long long)mu.flags); dnet_convert_meta_update(&mu); memcpy(&bulk_id->last_update, &mu, sizeof(struct dnet_meta_update)); state->num++; dnet_log(n, DNET_LOG_DEBUG, "BULK: addr = %s state->num = %d\n", dnet_server_convert_dnet_addr(&state->addr), state->num); if (state->num == DNET_BULK_IDS_SIZE) { err = dnet_request_bulk_check(n, state, params); state->num = 0; if (err) goto err_out_unlock; } //pthread_mutex_unlock(&state->state_lock); return 0; err_out_unlock: //pthread_mutex_unlock(&state->state_lock); return -2; }
int dnet_request_bulk_check(struct dnet_node *n, struct dnet_bulk_state *state, struct dnet_check_params *params) { struct dnet_trans_control ctl; struct dnet_net_state *st; struct dnet_bulk_check_priv *p; struct timespec wait_ts; int err; p = (struct dnet_bulk_check_priv *)malloc(sizeof(struct dnet_bulk_check_priv)); if (!p) { err = -ENOMEM; goto err_out_exit; } atomic_init(&p->refcnt, 2); p->db = params->db; dnet_check_temp_db_get(p->db); p->w = dnet_wait_alloc(0); if (!p->w) { err = -ENOMEM; goto err_out_free; } p->group_num = params->group_num; p->groups = (int *)malloc(sizeof(int) * params->group_num); if (!p->groups) { err = -ENOMEM; goto err_out_put; } memcpy(p->groups, params->groups, sizeof(int) * params->group_num); memset(&ctl, 0, sizeof(struct dnet_trans_control)); ctl.cmd = DNET_CMD_LIST; ctl.complete = dnet_bulk_check_complete; ctl.priv = p; ctl.cflags = DNET_FLAGS_NEED_ACK | DNET_FLAGS_NOLOCK | DNET_ATTR_BULK_CHECK; ctl.data = state->ids; ctl.size = sizeof(struct dnet_bulk_id) * state->num; st = dnet_state_search_by_addr(n, &state->addr); if (!st) { err = -ENOENT; goto err_out_put; } dnet_setup_id(&ctl.id, st->idc->group->group_id, st->idc->ids[0].raw.id); dnet_wait_get(p->w); dnet_log(n, DNET_LOG_DEBUG, "BULK: sending %u bytes of data to %s (%s)\n", ctl.size, dnet_dump_id(&ctl.id), dnet_server_convert_dnet_addr(&state->addr)); err = dnet_trans_alloc_send_state(st, &ctl); dnet_state_put(st); wait_ts = n->wait_ts; wait_ts.tv_sec *= DNET_BULK_IDS_SIZE; err = dnet_wait_event(p->w, p->w->cond != 0, &wait_ts); if (err) goto err_out_put; if (p->w->status) { err = p->w->status; goto err_out_put; } dnet_wait_put(p->w); if (atomic_dec_and_test(&p->refcnt)) { free(p->groups); free(p); } return 0; err_out_put: dnet_wait_put(p->w); err_out_free: if (atomic_dec_and_test(&p->refcnt)) { free(p->groups); free(p); } err_out_exit: dnet_log(n, DNET_LOG_ERROR, "Bulk check exited with status %d\n", err); return err; }