void dnode_peer_pool_deinit(struct array *server_pool) { uint32_t i, npool; for (i = 0, npool = array_n(server_pool); i < npool; i++) { struct server_pool *sp; sp = array_pop(server_pool); ASSERT(sp->p_conn == NULL); //fixe me to use different variables ASSERT(TAILQ_EMPTY(&sp->c_conn_q) && sp->dn_conn_q == 0); dnode_peer_deinit(&sp->peers); array_each(&sp->racks, rack_destroy, NULL); sp->nlive_server = 0; log_debug(LOG_DEBUG, "dyn: deinit peer pool %"PRIu32" '%.*s'", sp->idx, sp->name.len, sp->name.data); } //array_deinit(server_pool); log_debug(LOG_DEBUG, "deinit %"PRIu32" peer pools", npool); }
rstatus_t dnode_peer_each_pool_init(void *elem, void *context) { struct server_pool *sp = (struct server_pool *) elem; //struct context *ctx = context; struct array *conf_seeds = &sp->conf_pool->dyn_seeds; struct array * seeds = &sp->seeds; struct array * peers = &sp->peers; rstatus_t status; uint32_t nseed; /* init seeds list */ nseed = array_n(conf_seeds); if(nseed == 0) { log_debug(LOG_INFO, "dyn: look like you are running with no seeds deifined. This is ok for running with just one node."); // add current node to peers array status = array_init(peers, CONF_DEFAULT_PEERS, sizeof(struct server)); if (status != DN_OK) { return status; } struct server *peer = array_push(peers); ASSERT(peer != NULL); status = dnode_peer_add_local(sp, peer); if (status != DN_OK) { dnode_peer_deinit(peers); } dnode_peer_pool_run(sp); return status; } ASSERT(array_n(seeds) == 0); status = array_init(seeds, nseed, sizeof(struct server)); if (status != DN_OK) { return status; } /* transform conf seeds to seeds */ status = array_each(conf_seeds, conf_seed_each_transform, seeds); if (status != DN_OK) { dnode_peer_deinit(seeds); return status; } ASSERT(array_n(seeds) == nseed); /* set seed owner */ status = array_each(seeds, dnode_peer_each_set_owner, sp); if (status != DN_OK) { dnode_peer_deinit(seeds); return status; } /* initialize peers list = seeds list */ ASSERT(array_n(peers) == 0); // add current node to peers array uint32_t peer_cnt = nseed + 1; status = array_init(peers, CONF_DEFAULT_PEERS, sizeof(struct server)); if (status != DN_OK) { return status; } struct server *peer = array_push(peers); ASSERT(peer != NULL); status = dnode_peer_add_local(sp, peer); if (status != DN_OK) { dnode_peer_deinit(seeds); dnode_peer_deinit(peers); return status; } status = array_each(conf_seeds, conf_seed_each_transform, peers); if (status != DN_OK) { dnode_peer_deinit(seeds); dnode_peer_deinit(peers); return status; } ASSERT(array_n(peers) == peer_cnt); status = array_each(peers, dnode_peer_each_set_owner, sp); if (status != DN_OK) { dnode_peer_deinit(seeds); dnode_peer_deinit(peers); return status; } dnode_peer_pool_run(sp); log_debug(LOG_DEBUG, "init %"PRIu32" seeds and peers in pool %"PRIu32" '%.*s'", nseed, sp->idx, sp->name.len, sp->name.data); return DN_OK; }
static struct context * core_ctx_create(struct instance *nci) { rstatus_t status; struct context *ctx; srand((unsigned) time(NULL)); ctx = dn_alloc(sizeof(*ctx)); if (ctx == NULL) { return NULL; } ctx->id = ++ctx_id; ctx->cf = NULL; ctx->stats = NULL; ctx->evb = NULL; array_null(&ctx->pool); ctx->max_timeout = nci->stats_interval; ctx->timeout = ctx->max_timeout; ctx->dyn_state = INIT; /* parse and create configuration */ ctx->cf = conf_create(nci->conf_filename); if (ctx->cf == NULL) { loga("Failed to create context!!!"); dn_free(ctx); return NULL; } /* initialize server pool from configuration */ status = server_pool_init(&ctx->pool, &ctx->cf->pool, ctx); if (status != DN_OK) { loga("Failed to initialize server pool!!!"); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } /* crypto init */ status = crypto_init(ctx); if (status != DN_OK) { loga("Failed to initialize crypto!!!"); dn_free(ctx); return NULL; } /* create stats per server pool */ ctx->stats = stats_create(nci->stats_port, nci->stats_addr, nci->stats_interval, nci->hostname, &ctx->pool, ctx); if (ctx->stats == NULL) { loga("Failed to create stats!!!"); crypto_deinit(); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } /* initialize event handling for client, proxy and server */ ctx->evb = event_base_create(EVENT_SIZE, &core_core); if (ctx->evb == NULL) { loga("Failed to create socket event handling!!!"); crypto_deinit(); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } /* preconnect? servers in server pool */ status = server_pool_preconnect(ctx); if (status != DN_OK) { loga("Failed to preconnect for server pool!!!"); crypto_deinit(); server_pool_disconnect(ctx); event_base_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } /* initialize proxy per server pool */ status = proxy_init(ctx); if (status != DN_OK) { loga("Failed to initialize proxy!!!"); crypto_deinit(); server_pool_disconnect(ctx); event_base_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } /* initialize dnode listener per server pool */ status = dnode_init(ctx); if (status != DN_OK) { loga("Failed to initialize dnode!!!"); crypto_deinit(); server_pool_disconnect(ctx); event_base_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } ctx->dyn_state = JOINING; //TODOS: change this to JOINING /* initialize peers */ status = dnode_peer_init(&ctx->pool, ctx); if (status != DN_OK) { loga("Failed to initialize dnode peers!!!"); crypto_deinit(); dnode_deinit(ctx); server_pool_disconnect(ctx); event_base_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } core_debug(ctx); /* preconntect peers - probably start gossip here */ status = dnode_peer_pool_preconnect(ctx); if (status != DN_OK) { loga("Failed to preconnect dnode peers!!!"); crypto_deinit(); dnode_peer_deinit(&ctx->pool); dnode_deinit(ctx); server_pool_disconnect(ctx); event_base_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } //init ring msg queue CBUF_Init(C2G_InQ); CBUF_Init(C2G_OutQ); //init stats msg queue CBUF_Init(C2S_InQ); CBUF_Init(C2S_OutQ); gossip_pool_init(ctx); log_debug(LOG_VVERB, "created ctx %p id %"PRIu32"", ctx, ctx->id); return ctx; }