node::node(const logger &l, const std::string &config_path) : m_data(new node_data) { m_data->log = l; struct dnet_config cfg; memset(&cfg, 0, sizeof(struct dnet_config)); cfg.log = m_data->log.get_native(); std::list<address> remotes; std::vector<int> groups; parse_config(config_path, cfg, remotes, groups, cfg.log->log_level); m_data->node_ptr = dnet_node_create(&cfg); if (!m_data->node_ptr) { throw std::bad_alloc(); } for (std::list<address>::iterator it = remotes.begin(); it != remotes.end(); ++it) { try { add_remote(it->host.c_str(), it->port, it->family); } catch (...) { continue; } } }
node::node(const logger &l, struct dnet_config &cfg) : m_data(new node_data) { m_data->log = l; cfg.log = m_data->log.get_native(); m_data->node_ptr = dnet_node_create(&cfg); if (!m_data->node_ptr) { throw std::bad_alloc(); } }
node::node(const logger &l) : m_data(new node_data) { m_data->log = l; struct dnet_config cfg; memset(&cfg, 0, sizeof(cfg)); cfg.wait_timeout = 5; cfg.check_timeout = 20; cfg.log = m_data->log.get_native(); m_data->node_ptr = dnet_node_create(&cfg); if (!m_data->node_ptr) { throw std::bad_alloc(); } }
struct dnet_node *dnet_server_node_create(struct dnet_config *cfg) { struct dnet_node *n; struct dnet_raw_id *ids = NULL; int id_num = 0; int err = -ENOMEM; n = dnet_node_create(cfg); if (!n) { goto err_out_exit; } if (!n->notify_hash_size) { n->notify_hash_size = DNET_DEFAULT_NOTIFY_HASH_SIZE; err = dnet_notify_init(n); if (err) goto err_out_node_destroy; dnet_log(n, DNET_LOG_NOTICE, "No notify hash size provided, using default %d.\n", n->notify_hash_size); } err = dnet_cache_init(n); if (err) goto err_out_notify_exit; if (cfg->flags & DNET_CFG_JOIN_NETWORK) { int s; err = dnet_locks_init(n, cfg->oplock_num); if (err) goto err_out_cache_cleanup; ids = dnet_ids_init(n, cfg->history_env, &id_num, cfg->storage_free); if (!ids) goto err_out_locks_destroy; n->addr.addr_len = sizeof(n->addr.addr); err = dnet_socket_create(n, cfg, &n->addr, 1); if (err < 0) goto err_out_ids_cleanup; s = err; dnet_setup_id(&n->id, cfg->group_id, ids[0].id); n->st = dnet_state_create(n, cfg->group_id, ids, id_num, &n->addr, s, &err, DNET_JOIN, dnet_state_accept_process); if (!n->st) { close(s); goto err_out_state_destroy; } free(ids); ids = NULL; err = dnet_srw_init(n, cfg); if (err) { dnet_log(n, DNET_LOG_ERROR, "srw: initialization failure: %s %d\n", strerror(-err), err); } } dnet_log(n, DNET_LOG_DSA, "New server node has been created at %s, ids: %d.\n", dnet_dump_node(n), id_num); return n; err_out_state_destroy: dnet_srw_cleanup(n); dnet_state_put(n->st); err_out_ids_cleanup: free(ids); err_out_locks_destroy: dnet_locks_destroy(n); err_out_cache_cleanup: dnet_cache_cleanup(n); err_out_notify_exit: dnet_notify_exit(n); err_out_node_destroy: dnet_node_destroy(n); err_out_exit: return NULL; }
int main(int argc, char *argv[]) { int ch, err; struct dnet_node *n = NULL; struct dnet_config cfg; char *remote_addr = NULL; int remote_port = -1; int remote_family = -1; char *logfile = default_log; int daemonize = 0; FILE *log = NULL; struct dnet_check_request r, *req, *req2; struct tm tm; char *file = NULL; int group_num = 0, *groups; char *ns = NULL; int nsize = 0; struct dnet_session *s; memset(&cfg, 0, sizeof(struct dnet_config)); cfg.wait_timeout = INT_MAX; check_logger.log_level = DNET_LOG_INFO; cfg.check_timeout = 60; memset(&tm, 0, sizeof(tm)); memset(&r, 0, sizeof(r)); r.thread_num = 1; while ((ch = getopt(argc, argv, "b:B:DN:f:n:t:u:U:MRm:w:l:dr:g:h")) != -1) { switch (ch) { case 'b': r.blob_start = atoi(optarg); break; case 'B': r.blob_num = atoi(optarg); break; case 'N': ns = optarg; nsize = strlen(optarg); break; case 'f': file = optarg; break; case 'n': r.thread_num = atoi(optarg); if (r.thread_num > 1) fprintf(stderr, "You are going to run your recovery process with %d threads, " "this can heavily screw up your system performance.\n", r.thread_num); break; case 't': if (!strptime(optarg, "%F %T", &tm)) { fprintf(stderr, "Invalid timestamp string in -t\n"); check_usage(argv[0]); return -EINVAL; } r.timestamp = mktime(&tm); break; case 'u': if (!strptime(optarg, "%F %T", &tm)) { fprintf(stderr, "Invalid timestamp string in -u\n"); check_usage(argv[0]); return -EINVAL; } r.updatestamp_start = mktime(&tm); break; case 'U': if (!strptime(optarg, "%F %T", &tm)) { fprintf(stderr, "Invalid timestamp string in -U\n"); check_usage(argv[0]); return -EINVAL; } r.updatestamp_stop = mktime(&tm); break; case 'D': r.flags |= DNET_CHECK_DRY_RUN; break; case 'M': r.flags |= DNET_CHECK_MERGE; break; // case 'F': // r.flags |= DNET_CHECK_FULL; // break; case 'R': r.flags |= DNET_CHECK_DELETE; break; case 'm': check_logger.log_level = strtoul(optarg, NULL, 0); break; case 'w': cfg.check_timeout = cfg.wait_timeout = atoi(optarg); break; case 'l': logfile = optarg; break; case 'd': daemonize = 1; break; case 'r': err = dnet_parse_addr(optarg, &remote_port, &remote_family); if (err) return err; remote_addr = optarg; break; case 'g': group_num = dnet_parse_groups(optarg, &groups); if (group_num <= 0) return -1; break; case 'h': default: check_usage(argv[0]); return -1; } } if (!remote_addr) { fprintf(stderr, "No remote node specified to route requests.\n"); return -ENOENT; } log = fopen(logfile, "a"); if (!log) { err = -errno; fprintf(stderr, "Failed to open log file %s: %s.\n", logfile, strerror(errno)); return err; } if (daemonize) { if (logfile == default_log) { fprintf(stderr, "You should specify log file for daemon mode\n"); } else { dnet_background(); } } check_logger.log_private = log; check_logger.log = dnet_common_log; cfg.log = &check_logger; n = dnet_node_create(&cfg); if (!n) return -1; err = dnet_add_state(n, remote_addr, remote_port, remote_family, DNET_CFG_NO_ROUTE_LIST); if (err) return err; s = dnet_session_create(n); if (!s) return -ENOMEM; err = dnet_session_set_ns(s, ns, nsize); if (err) return err; req = &r; if (file) { req = dnet_check_gen_request(s, &r, file); if (!req) return -EINVAL; } if (group_num > 0) { req2 = malloc(sizeof(struct dnet_check_request) + req->obj_num * sizeof(struct dnet_id) + group_num * sizeof(int)); if (!req2) return -ENOMEM; memcpy(req2, req, sizeof(struct dnet_check_request) + req->obj_num * sizeof(struct dnet_id)); memcpy((char *)req2 + sizeof(struct dnet_check_request) + req->obj_num * sizeof(struct dnet_id), groups, group_num * sizeof(int)); req2->group_num = group_num; req = req2; } return dnet_request_check(s, req); }
int main(int argc, char *argv[]) { int ch, err, have_remote = 0, i; struct dnet_node *n = NULL; struct dnet_config cfg, rem; int max_id_idx = 1000, id_idx = 0, group_id = 0; unsigned char id[max_id_idx][DNET_ID_SIZE]; char *logfile = "/dev/stderr", *notify_file = "/dev/stdout"; FILE *log = NULL, *notify; memset(&cfg, 0, sizeof(struct dnet_config)); cfg.sock_type = SOCK_STREAM; cfg.proto = IPPROTO_TCP; cfg.wait_timeout = 60*60; notify_logger.log_mask = DNET_LOG_ERROR | DNET_LOG_INFO; memcpy(&rem, &cfg, sizeof(struct dnet_config)); while ((ch = getopt(argc, argv, "g:m:w:l:I:a:r:h")) != -1) { switch (ch) { case 'm': notify_logger.log_mask = strtoul(optarg, NULL, 0); break; case 'w': cfg.wait_timeout = atoi(optarg); break; case 'L': notify_file = optarg; break; case 'l': logfile = optarg; break; case 'I': if (id_idx < max_id_idx) { err = dnet_parse_numeric_id(optarg, id[id_idx]); if (err) return err; id_idx++; } break; case 'g': group_id = atoi(optarg); break; case 'a': err = dnet_parse_addr(optarg, &cfg); if (err) return err; break; case 'r': err = dnet_parse_addr(optarg, &rem); if (err) return err; have_remote = 1; break; case 'h': default: notify_usage(argv[0]); return -1; } } if (!id_idx) { fprintf(stderr, "No ID specified to watch.\n"); return -EINVAL; } if (!have_remote) { fprintf(stderr, "No remote node specified to route requests.\n"); return -ENOENT; } log = fopen(logfile, "a"); if (!log) { err = -errno; fprintf(stderr, "Failed to open log file %s: %s.\n", logfile, strerror(errno)); return err; } notify_logger.log_private = log; notify_logger.log = dnet_common_log; cfg.log = ¬ify_logger; notify = fopen(notify_file, "a"); if (!notify) { err = -errno; fprintf(stderr, "Failed to open notify file %s: %s.\n", notify_file, strerror(errno)); return err; } n = dnet_node_create(&cfg); if (!n) return -1; err = dnet_add_state(n, &rem); if (err) return err; for (i=0; i<id_idx; ++i) { struct dnet_id raw; dnet_setup_id(&raw, group_id, id[i]); err = dnet_request_notification(n, &raw, notify_complete, notify); } while (1) { sleep(1); } return 0; }
struct dnet_node *dnet_server_node_create(struct dnet_config_data *cfg_data) { struct dnet_node *n; struct dnet_config *cfg = &cfg_data->cfg_state; struct dnet_addr *addrs = cfg_data->cfg_addrs; int addr_num = cfg_data->cfg_addr_num; int err = -ENOMEM; sigset_t previous_sigset; sigset_t sigset; sigemptyset(&sigset); sigaddset(&sigset, SIGINT); sigaddset(&sigset, SIGTERM); sigaddset(&sigset, SIGALRM); sigaddset(&sigset, SIGQUIT); pthread_sigmask(SIG_BLOCK, &sigset, &previous_sigset); n = dnet_node_create(cfg); if (!n) goto err_out_exit; n->config_data = cfg_data; err = dnet_server_io_init(n); if (err) goto err_out_node_destroy; err = dnet_monitor_init(n, cfg); if (err) goto err_out_node_destroy; err = dnet_node_check_stack(n); if (err) goto err_out_monitor_destroy; if (!n->notify_hash_size) { n->notify_hash_size = DNET_DEFAULT_NOTIFY_HASH_SIZE; err = dnet_notify_init(n); if (err) goto err_out_monitor_destroy; dnet_log(n, DNET_LOG_NOTICE, "No notify hash size provided, using default %d.", n->notify_hash_size); } err = dnet_local_addr_add(n, addrs, addr_num); if (err) goto err_out_notify_exit; if (cfg->flags & DNET_CFG_JOIN_NETWORK) { int s; struct dnet_addr la; err = dnet_locks_init(n, 1024); if (err) { dnet_log(n, DNET_LOG_ERROR, "failed to init locks: %s %d", strerror(-err), err); goto err_out_addr_cleanup; } n->route = dnet_route_list_create(n); if (!n->route) { dnet_log(n, DNET_LOG_ERROR, "failed to create route list: %s %d", strerror(-err), err); goto err_out_locks_destroy; } err = dnet_create_addr(&la, NULL, cfg->port, cfg->family); if (err < 0) { dnet_log(n, DNET_LOG_ERROR, "Failed to get address info for 0.0.0.0:%d, family: %d, err: %d: %s.", cfg->port, cfg->family, err, strerror(-err)); goto err_out_route_list_destroy; } err = dnet_socket_create_listening(n, &la); if (err < 0) goto err_out_route_list_destroy; s = err; if (s < 0) { err = s; dnet_log(n, DNET_LOG_ERROR, "failed to create socket: %s %d", strerror(-err), err); goto err_out_route_list_destroy; } n->st = dnet_state_create(n, NULL, 0, n->addrs, s, &err, DNET_JOIN, 1, 0, 1, n->addrs, n->addr_num); if (!n->st) { dnet_log(n, DNET_LOG_ERROR, "failed to create state: %s %d", strerror(-err), err); goto err_out_state_destroy; } // @dnet_state_create() returns state pointer which holds 2 references - one for originally created state // and another for caller in case he wants to do something with the state. The first reference is owned // by network thread given state was attached to, and it can already release it. dnet_state_put(n->st); err = dnet_backend_init_all(n); if (err) { dnet_log(n, DNET_LOG_ERROR, "failed to init backends: %s %d", strerror(-err), err); goto err_out_state_destroy; } if (!cfg->srw.config) { dnet_log(n, DNET_LOG_INFO, "srw: no config"); n->srw = NULL; } else { err = dnet_srw_init(n, cfg); if (err) { dnet_log(n, DNET_LOG_ERROR, "srw: initialization failure: %s %d", strerror(-err), err); goto err_out_backends_cleanup; } } } dnet_log(n, DNET_LOG_DEBUG, "New server node has been created at port %d.", cfg->port); pthread_sigmask(SIG_SETMASK, &previous_sigset, NULL); return n; dnet_srw_cleanup(n); err_out_backends_cleanup: dnet_set_need_exit(n); dnet_backend_cleanup_all(n); err_out_state_destroy: dnet_state_put(n->st); err_out_route_list_destroy: dnet_route_list_destroy(n->route); err_out_locks_destroy: dnet_locks_destroy(n); err_out_addr_cleanup: dnet_local_addr_cleanup(n); err_out_notify_exit: n->need_exit = err; dnet_notify_exit(n); err_out_monitor_destroy: dnet_monitor_exit(n); err_out_node_destroy: dnet_node_destroy(n); err_out_exit: pthread_sigmask(SIG_SETMASK, &previous_sigset, NULL); return NULL; }
struct dnet_node *dnet_server_node_create(struct dnet_config_data *cfg_data, struct dnet_config *cfg, struct dnet_addr *addrs, int addr_num) { struct dnet_node *n; struct dnet_raw_id *ids = NULL; int id_num = 0; int err = -ENOMEM; sigset_t previous_sigset; sigset_t sigset; sigemptyset(&sigset); sigaddset(&sigset, SIGINT); sigaddset(&sigset, SIGTERM); sigaddset(&sigset, SIGALRM); sigaddset(&sigset, SIGQUIT); pthread_sigmask(SIG_BLOCK, &sigset, &previous_sigset); n = dnet_node_create(cfg); if (!n) goto err_out_exit; n->config_data = cfg_data; err = dnet_node_check_stack(n); if (err) goto err_out_node_destroy; if (!n->notify_hash_size) { n->notify_hash_size = DNET_DEFAULT_NOTIFY_HASH_SIZE; err = dnet_notify_init(n); if (err) goto err_out_node_destroy; dnet_log(n, DNET_LOG_NOTICE, "No notify hash size provided, using default %d.\n", n->notify_hash_size); } err = dnet_cache_init(n); if (err) goto err_out_notify_exit; err = dnet_local_addr_add(n, addrs, addr_num); if (err) goto err_out_cache_cleanup; if (cfg->flags & DNET_CFG_JOIN_NETWORK) { struct dnet_addr la; int s; err = dnet_locks_init(n, 1024); if (err) goto err_out_addr_cleanup; ids = dnet_ids_init(n, cfg->history_env, &id_num, cfg->storage_free, cfg_data->cfg_addrs, cfg_data->cfg_remotes); if (!ids) goto err_out_locks_destroy; memset(&la, 0, sizeof(struct dnet_addr)); la.addr_len = sizeof(la.addr); la.family = cfg->family; err = dnet_socket_create(n, NULL, cfg->port, &la, 1); if (err < 0) goto err_out_ids_cleanup; s = err; dnet_setup_id(&n->id, cfg->group_id, ids[0].id); n->st = dnet_state_create(n, cfg->group_id, ids, id_num, &la, s, &err, DNET_JOIN, -1, dnet_state_accept_process); if (!n->st) { close(s); goto err_out_state_destroy; } free(ids); ids = NULL; if (!cfg->srw.config) { dnet_log(n, DNET_LOG_INFO, "srw: no config\n"); n->srw = NULL; } else { err = dnet_srw_init(n, cfg); if (err) { dnet_log(n, DNET_LOG_ERROR, "srw: initialization failure: %s %d\n", strerror(-err), err); goto err_out_state_destroy; } } } dnet_log(n, DNET_LOG_DEBUG, "New server node has been created at port %d, ids: %d.\n", cfg->port, id_num); pthread_sigmask(SIG_SETMASK, &previous_sigset, NULL); return n; dnet_srw_cleanup(n); err_out_state_destroy: dnet_state_put(n->st); err_out_ids_cleanup: free(ids); err_out_locks_destroy: dnet_locks_destroy(n); err_out_addr_cleanup: dnet_local_addr_cleanup(n); err_out_cache_cleanup: dnet_cache_cleanup(n); err_out_notify_exit: dnet_notify_exit(n); err_out_node_destroy: dnet_node_destroy(n); err_out_exit: pthread_sigmask(SIG_SETMASK, &previous_sigset, NULL); return NULL; }
int main(int argc, char *argv[]) { int ch, err, i, have_remote = 0; struct dnet_node *n = NULL; struct dnet_session *s = NULL; struct dnet_config cfg, rem; int max_id_idx = 1000, id_idx = 0; int timeout; unsigned char id[max_id_idx][DNET_ID_SIZE]; char *logfile = "/dev/stderr", *statfile = "/dev/stdout"; FILE *log = NULL, *stat; memset(&cfg, 0, sizeof(struct dnet_config)); cfg.sock_type = SOCK_STREAM; cfg.proto = IPPROTO_TCP; cfg.wait_timeout = 60*60; stat_logger.log_level = DNET_LOG_ERROR; timeout = 1; memcpy(&rem, &cfg, sizeof(struct dnet_config)); while ((ch = getopt(argc, argv, "MFAt:m:w:l:I:r:h")) != -1) { switch (ch) { case 'M': stat_mem = 1; break; case 'F': stat_fs = 1; break; case 'A': stat_la = 1; break; case 't': timeout = atoi(optarg); break; case 'm': stat_logger.log_level = strtoul(optarg, NULL, 0); break; case 'w': cfg.wait_timeout = atoi(optarg); break; case 'L': statfile = optarg; break; case 'l': logfile = optarg; break; case 'I': if (id_idx < max_id_idx) { err = dnet_parse_numeric_id(optarg, id[id_idx]); if (err) return err; id_idx++; } break; case 'r': err = dnet_parse_addr(optarg, &rem); if (err) return err; have_remote = 1; break; case 'h': default: stat_usage(argv[0]); return -1; } } if (!have_remote) { fprintf(stderr, "No remote node specified to route requests.\n"); return -ENOENT; } log = fopen(logfile, "a"); if (!log) { err = -errno; fprintf(stderr, "Failed to open log file %s: %s.\n", logfile, strerror(errno)); return err; } stat_logger.log_private = log; stat_logger.log = dnet_common_log; cfg.log = &stat_logger; stat = fopen(statfile, "a"); if (!stat) { err = -errno; fprintf(stderr, "Failed to open stat file %s: %s.\n", statfile, strerror(errno)); return err; } n = dnet_node_create(&cfg); if (!n) return -1; s = dnet_session_create(n); if (!s) return -1; err = dnet_add_state(n, &rem); if (err) return err; while (1) { struct dnet_id raw; if (!id_idx) { err = dnet_request_stat(s, NULL, DNET_CMD_STAT, 0, stat_complete, stat); if (err < 0) return err; } for (i=0; i<id_idx; ++i) { dnet_setup_id(&raw, 0, id[i]); err = dnet_request_stat(s, &raw, DNET_CMD_STAT, 0, stat_complete, stat); if (err < 0) return err; } sleep(timeout); } return 0; }