static void core_ctx_destroy(struct context *ctx) { log_debug(LOG_VVERB, "destroy ctx %p id %"PRIu32"", ctx, ctx->id); proxy_deinit(ctx); server_pool_disconnect(ctx); event_deinit(ctx); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); }
static struct context * core_ctx_create(struct instance *nci) { rstatus_t status; struct context *ctx; struct conn *sentinel_conn; ctx = nc_alloc(sizeof(*ctx)); if (ctx == NULL) { return NULL; } ctx->id = ++ctx_id; ctx->cf = NULL; ctx->stats = NULL; array_null(&ctx->pool); ctx->ep = -1; ctx->nevent = EVENT_SIZE_HINT; ctx->max_timeout = nci->stats_interval; ctx->timeout = ctx->max_timeout; ctx->event = NULL; ctx->server_reconnect_interval = nci->server_reconnect_interval; ctx->whitelist = nci->whitelist; /* parse and create configuration */ ctx->cf = conf_create(nci->conf_filename); if (ctx->cf == NULL) { nc_free(ctx); return NULL; } /* initialize server pool from configuration */ status = server_pool_init(&ctx->pool, &ctx->cf->pool, ctx); if (status != NC_OK) { conf_destroy(ctx->cf); nc_free(ctx); return NULL; } /* initialize sentinel server */ ctx->sentinel = sentinel_init(nci->sentinel_port, nci->sentinel_addr); if (ctx->sentinel == NULL) { server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_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); if (ctx->stats == NULL) { sentinel_deinit(ctx->sentinel); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } /* initialize event handling for client, proxy and server */ status = event_init(ctx, EVENT_SIZE_HINT); if (status != NC_OK) { stats_destroy(ctx->stats); sentinel_deinit(ctx->sentinel); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } /* preconnect? servers in server pool */ status = server_pool_preconnect(ctx); if (status != NC_OK) { server_pool_disconnect(ctx); event_deinit(ctx); stats_destroy(ctx->stats); sentinel_deinit(ctx->sentinel); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } /* initialize sentinel server conn */ sentinel_conn = sentinel_connect(ctx); if (sentinel_conn == NULL) { server_pool_disconnect(ctx); event_deinit(ctx); stats_destroy(ctx->stats); sentinel_deinit(ctx->sentinel); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); } /* initialize proxy per server pool */ status = proxy_init(ctx); if (status != NC_OK) { sentinel_conn->close(ctx, sentinel_conn); server_pool_disconnect(ctx); event_deinit(ctx); stats_destroy(ctx->stats); sentinel_deinit(ctx->sentinel); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } log_debug(LOG_VVERB, "created ctx %p id %"PRIu32"", ctx, ctx->id); return ctx; }
static struct context * core_ctx_create(struct instance *nci) { rstatus_t status; struct context *ctx; ctx = nc_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->max_nfd = 0; ctx->max_ncconn = 0; ctx->max_nsconn = 0; /* parse and create configuration */ ctx->cf = conf_create(nci->conf_filename); if (ctx->cf == NULL) { nc_free(ctx); return NULL; } /* initialize server pool from configuration */ status = server_pool_init(&ctx->pool, &ctx->cf->pool, ctx); if (status != NC_OK) { conf_destroy(ctx->cf); nc_free(ctx); return NULL; } /* * Get rlimit and calculate max client connections after we have * calculated max server connections */ status = core_calc_connections(ctx); if (status != NC_OK) { server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_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); if (ctx->stats == NULL) { server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_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) { stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } /* preconnect? servers in server pool */ status = server_pool_preconnect(ctx); if (status != NC_OK) { server_pool_disconnect(ctx); event_base_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } /* initialize proxy per server pool */ status = proxy_init(ctx); if (status != NC_OK) { server_pool_disconnect(ctx); event_base_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } log_debug(LOG_VVERB, "created ctx %p id %"PRIu32"", ctx, ctx->id); return ctx; }
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; }
static struct context * core_ctx_create(struct instance *nci) { rstatus_t status; struct context *ctx; int64_t now; uint32_t npool; ctx = nc_alloc(sizeof(*ctx)); if (ctx == NULL) { return NULL; } now = nc_msec_now(); if (now < 0) { nc_free(ctx); 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->next_tick = now + NC_TICK_INTERVAL; /* parse and create configuration */ ctx->cf = conf_create(nci->conf_filename); if (ctx->cf == NULL) { nc_free(ctx); return NULL; } npool = array_n(&ctx->cf->pool); /* initialize server pool from configuration */ if (npool != 0) { status = server_pool_init(&ctx->pool, &ctx->cf->pool, ctx); if (status != NC_OK) { conf_destroy(ctx->cf); nc_free(ctx); return NULL; } } /* create stats per server pool */ if (npool != 0) { ctx->stats = stats_create(nci->stats_port, nci->stats_addr, nci->stats_interval, nci->hostname, &ctx->pool); if (ctx->stats == NULL) { server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } } /* initialize event handling for client, proxy and server */ ctx->evb = evbase_create(NC_EVENT_SIZE, &core_core); if (ctx->evb == NULL) { stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } /* preconnect? servers in server pool */ if (npool != 0) { status = server_pool_preconnect(ctx); if (status != NC_OK) { server_pool_disconnect(ctx); evbase_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } } /* initialize proxy per server pool */ if (npool != 0) { status = proxy_init(ctx); if (status != NC_OK) { server_pool_disconnect(ctx); evbase_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } } log_debug(LOG_VVERB, "created ctx %p id %"PRIu32"", ctx, ctx->id); return ctx; }