static void zipper_init_config(zipper_t *zipper, const char *config_file_path) { zipper->config_ = conf_create(config_file_path); if (zipper->config_ == NULL) ASSERT(0, "config file doesn't exists or has syntax error\n", config_file_path); }
END_TEST START_TEST (test_conf_add_group) { configuration *conf; int ret; conf = conf_create (); fail_unless (conf != 0); ret = conf_add_group (conf, NULL, head_group); fail_unless (ret == 0); conf_free (conf); conf = conf_create (); fail_unless (conf != 0); ret = conf_add_group (conf, "agroup", head_group); fail_unless (ret == 0); conf_free (conf); }
/* * Returns true if configuration file has a valid syntax, otherwise * returns false */ static bool nc_test_conf(struct env_master *env) { struct conf *cf; cf = conf_create(env->conf_filename); if (cf == NULL) { log_stderr("nutcracker: configuration file '%s' syntax is invalid", env->conf_filename); return false; } conf_destroy(cf); log_stderr("nutcracker: configuration file '%s' syntax is ok", env->conf_filename); return true; }
/* * Returns true if configuration file has a valid syntax, otherwise * returns false */ static bool dn_test_conf(struct instance *nci) { struct conf *cf; cf = conf_create(nci->conf_filename); if (cf == NULL) { log_stderr("dynomite: configuration file '%s' syntax is invalid", nci->conf_filename); return false; } conf_destroy(cf); log_stderr("dynomite: configuration file '%s' syntax is ok", nci->conf_filename); return true; }
// USR1 signal: re-read configuration file static void sigusr1_cb (G_GNUC_UNUSED evutil_socket_t sig, G_GNUC_UNUSED short events, G_GNUC_UNUSED void *user_data) { ConfData *conf_new = conf_create(); ConfData *conf_old = _app->conf; LOG_err (APP_LOG, "Got SIGUSR1"); if (!conf_parse_file (conf_new, _app->conf_path)) { LOG_err (APP_LOG, "Failed to parse configuration file: %s", _app->conf_path); conf_destroy(conf_new); } else { const gchar *copy_entries[] = {"s3.host", "s3.port", "s3.access_key_id", "s3.secret_access_key", "s3.bucket_name", NULL}; int i; _app->conf = conf_new; for (i = 0; copy_entries[i]; i++) { conf_copy_entry (_app->conf, conf_old, copy_entries[i], FALSE); } conf_destroy (conf_old); log_level = conf_get_int(_app->conf, "log.level"); } }
END_TEST START_TEST (test_conf_default) { configuration *conf; int ret; int i; unsigned int u; char c; int bt; int bf; float f; const char *s; int valid; conf = conf_create (); fail_unless (conf != 0); ret = conf_add_group (conf, NULL, head_group); fail_unless (ret == 0); ret = conf_add_group (conf, "agroup", head_group); fail_unless (ret == 0); i = conf_get_int (conf, NULL, "h_int", &valid); fail_unless (valid == 1); fail_unless (i == 1, "Got %d expected %d", i, 1); u = conf_get_uint (conf, NULL, "h_uint", &valid); fail_unless (valid == 1); fail_unless (u == 10); c = conf_get_char (conf, NULL, "h_char", &valid); fail_unless (valid == 1); fail_unless (c == 'A'); bf = conf_get_bool (conf, NULL, "h_bool_false", &valid); fail_unless (valid == 1); fail_unless (bf == 0); bt = conf_get_bool (conf, NULL, "h_bool_true", &valid); fail_unless (valid == 1); fail_unless (bt == 1); f = conf_get_float (conf, NULL, "h_float", &valid); fail_unless (valid == 1); fail_unless (abs (f - 5.5f) < 0.0001f, "Got %f expected %f", f, 5.5f); s = conf_get_string (conf, NULL, "h_string", &valid); fail_unless (valid == 1); fail_unless (strcasecmp (s, "default") == 0); i = conf_get_int (conf, "agroup", "h_int", &valid); fail_unless (valid == 1); fail_unless (i == 1, "Got %d expected %d", i, 1); u = conf_get_uint (conf, "agroup", "h_uint", &valid); fail_unless (valid == 1); fail_unless (u == 10); c = conf_get_char (conf, "agroup", "h_char", &valid); fail_unless (valid == 1); fail_unless (c == 'A'); bf = conf_get_bool (conf, "agroup", "h_bool_false", &valid); fail_unless (valid == 1); fail_unless (bf == 0); bt = conf_get_bool (conf, "agroup", "h_bool_true", &valid); fail_unless (valid == 1); fail_unless (bt == 1); f = conf_get_float (conf, "agroup", "h_float", &valid); fail_unless (valid == 1); fail_unless (abs (f - 5.5f) < 0.0001f, "Got %f expected %f", f, 5.5f); s = conf_get_string (conf, "agroup", "h_string", &valid); fail_unless (valid == 1); fail_unless (strcasecmp (s, "default") == 0); conf_free (conf); }
END_TEST START_TEST (test_conf_errors) { configuration *conf; int ret; int i; unsigned int u; char c; int bt; int bf; float f; const char *s; int valid; /* * Test Add group errors. */ conf = conf_create (); fail_unless (conf != 0); /* Add group to non conf. */ ret = conf_add_group (NULL, NULL, head_group); fail_unless (ret == -1); conf_free (conf); /* Add two groups with no group name under root. */ conf = conf_create (); fail_unless (conf != 0); ret = conf_add_group (conf, NULL, head_group); fail_unless (ret == 0); ret = conf_add_group (conf, NULL, head_group); fail_unless (ret == -1); conf_free (conf); /* Add group with bad parameter type. */ conf = conf_create (); fail_unless (conf != 0); ret = conf_add_group (conf, NULL, bad_1_group); fail_unless (ret < 0); conf_free (conf); /* * Test load errors. */ conf = conf_create (); fail_unless (conf != 0); /* Load with no groups. */ ret = conf_load (conf, CONFDIR "test.conf"); fail_unless (ret == -1); /* Test filename. */ ret = conf_add_group (conf, "a group", head_group); fail_unless (ret == 0); ret = conf_load (conf, NULL); fail_unless (ret == -1); ret = conf_load (conf, "/dev/null/something"); fail_unless (ret == -1); /* Test group names. */ ret = conf_load (conf, CONFDIR "test_bad1.conf"); fail_unless (ret < 0); ret = conf_load (conf, CONFDIR "test_bad2.conf"); fail_unless (ret < 0); ret = conf_load (conf, CONFDIR "test_bad3.conf"); fail_unless (ret < 0); ret = conf_load (conf, CONFDIR "test_bad4.conf"); fail_unless (ret < 0); ret = conf_load (conf, CONFDIR "test_bad5.conf"); fail_unless (ret < 0); ret = conf_load (conf, CONFDIR "test_bad6.conf"); fail_unless (ret < 0); ret = conf_load (conf, CONFDIR "test_bad7.conf"); fail_unless (ret < 0); ret = conf_load (conf, CONFDIR "test_bad8.conf"); fail_unless (ret < 0); ret = conf_load (conf, CONFDIR "test_bad9.conf"); fail_unless (ret < 0); ret = conf_load (conf, CONFDIR "test_bad10.conf"); fail_unless (ret != 0); ret = conf_add_group (conf, NULL, head_group); fail_unless (ret == 0); ret = conf_load (conf, CONFDIR "test_bad11.conf"); fail_unless (ret == -15, "%d", ret); ret = conf_load (conf, CONFDIR "test_bad12.conf"); fail_unless (ret == -13); ret = conf_load (conf, CONFDIR "test_bad13.conf"); fail_unless (ret != -14); ret = conf_load (conf, CONFDIR "test_bad14.conf"); fail_unless (ret == -17, "%d", ret); conf_free (conf); /* Test getting values */ conf = conf_create (); fail_unless (conf != 0); ret = conf_add_group (conf, "a group", head_group); fail_unless (ret == 0); i = conf_get_int (conf, NULL, "h_int", &valid); fail_unless (valid == 0); u = conf_get_uint (conf, NULL, "h_uint", &valid); fail_unless (valid == 0); c = conf_get_char (conf, NULL, "h_char", &valid); fail_unless (valid == 0); bf = conf_get_bool (conf, NULL, "h_bool_false", &valid); fail_unless (valid == 0); bt = conf_get_bool (conf, NULL, "h_bool_true", &valid); fail_unless (valid == 0); f = conf_get_float (conf, NULL, "h_float", &valid); fail_unless (valid == 0); s = conf_get_string (conf, NULL, "h_string", &valid); fail_unless (valid == 0); i = conf_get_int (conf, "a group", "X_int", &valid); fail_unless (valid == 0); u = conf_get_uint (conf, "a group", "X_uint", &valid); fail_unless (valid == 0); c = conf_get_char (conf, "a group", "X_char", &valid); fail_unless (valid == 0); bf = conf_get_bool (conf, "a group", "X_bool_false", &valid); fail_unless (valid == 0); bt = conf_get_bool (conf, "a group", "X_bool_true", &valid); fail_unless (valid == 0); f = conf_get_float (conf, "a group", "X_float", &valid); fail_unless (valid == 0); s = conf_get_string (conf, "a group", "X_string", &valid); fail_unless (valid == 0); conf_free (conf); }
END_TEST START_TEST (test_conf_file) { configuration *conf; int ret; int i; unsigned int u; char c; int bt; int bf; float f; const char *s; int valid; conf = conf_create (); fail_unless (conf != 0); ret = conf_add_group (conf, NULL, head_group); fail_unless (ret == 0); ret = conf_add_group (conf, "agroup", head_group); fail_unless (ret == 0); ret = conf_add_group (conf, "c group", head_group); fail_unless (ret == 0); ret = conf_add_group (conf, "b group", head_group); fail_unless (ret == 0); ret = conf_add_group (conf, "e group", head_group); fail_unless (ret == 0); ret = conf_add_group (conf, "d group", head_group); fail_unless (ret == 0); ret = conf_load (conf, CONFDIR "test.conf"); fail_unless (ret == 0, "ret = %d", ret); i = conf_get_int (conf, NULL, "h_int", &valid); fail_unless (valid == 1); fail_unless (i == 10, "Got %d expected %d", i, 10); u = conf_get_uint (conf, NULL, "h_uint", &valid); fail_unless (valid == 1); fail_unless (u == 55, "Got %u expected %u", u, 55); c = conf_get_char (conf, NULL, "h_char", &valid); fail_unless (valid == 1); fail_unless (c == 'B', "Got %c expected %c", c, 'B'); bf = conf_get_bool (conf, NULL, "h_bool_false", &valid); fail_unless (valid == 1); fail_unless (bf == 0, "Got %d expected %d", bf, 0); bt = conf_get_bool (conf, NULL, "h_bool_true", &valid); fail_unless (valid == 1); fail_unless (bt == 1, "Got %d expected %d", i, 1); f = conf_get_float (conf, NULL, "h_float", &valid); fail_unless (valid == 1); fail_unless (abs (f - 15.5f) < 0.0001f, "Got %f expected %f", f, 15.5f); s = conf_get_string (conf, NULL, "h_string", &valid); fail_unless (valid == 1); fail_unless (strcasecmp (s, "Not Default") == 0, "Got %d expected 'Not Default", i); i = conf_get_int (conf, "agroup", "h_int", &valid); fail_unless (valid == 1); fail_unless (i == 10, "Got %d expected %d", i, 10); u = conf_get_uint (conf, "agroup", "h_uint", &valid); fail_unless (valid == 1); fail_unless (u == 75, "Got %u expected %u", u, 75); c = conf_get_char (conf, "agroup", "h_char", &valid); fail_unless (valid == 1); fail_unless (c == 'B', "Got %c expected %c", c, 'B'); bf = conf_get_bool (conf, "agroup", "h_bool_false", &valid); fail_unless (valid == 1); fail_unless (bf == 0, "Got %d expected %d", bf, 0); bt = conf_get_bool (conf, "agroup", "h_bool_true", &valid); fail_unless (valid == 1); fail_unless (bt == 1, "Got %d expected %d", i, 1); f = conf_get_float (conf, "agroup", "h_float", &valid); fail_unless (valid == 1); fail_unless (abs (f - 15.5f) < 0.0001f, "Got %f expected %f", f, 15.5f); s = conf_get_string (conf, "agroup", "h_string", &valid); fail_unless (valid == 1); fail_unless (strcasecmp (s, "Not") == 0, "Got '%s' expected 'Not'", s); i = conf_get_int (conf, "b group", "h_int", &valid); fail_unless (valid == 1); fail_unless (i == 10, "Got %d expected %d", i, 10); u = conf_get_uint (conf, "b group", "h_uint", &valid); fail_unless (valid == 1); fail_unless (u == 85, "Got %u expected %u", u, 85); c = conf_get_char (conf, "b group", "h_char", &valid); fail_unless (valid == 1); fail_unless (c == 'B', "Got %c expected %c", c, 'B'); bf = conf_get_bool (conf, "b group", "h_bool_false", &valid); fail_unless (valid == 1); fail_unless (bf == 0, "Got %d expected %d", bf, 0); bt = conf_get_bool (conf, "b group", "h_bool_true", &valid); fail_unless (valid == 1); fail_unless (bt == 1, "Got %d expected %d", i, 1); f = conf_get_float (conf, "b group", "h_float", &valid); fail_unless (valid == 1); fail_unless (abs (f + 25.5f) < 0.0001f, "Got %f expected %f", f, -25.5f); s = conf_get_string (conf, "b group", "h_string", &valid); fail_unless (valid == 1); fail_unless (strcasecmp (s, "Not Default") == 0, "Got %d expected 'Not Default", i); i = conf_get_int (conf, "c group", "h_int", &valid); fail_unless (valid == 1); fail_unless (i == -16, "Got %d expected %d", i, -16); u = conf_get_uint (conf, "c group", "h_uint", &valid); fail_unless (valid == 1); fail_unless (u == 16, "Got %u expected %u", u, 16); c = conf_get_char (conf, "c group", "h_char", &valid); fail_unless (valid == 1); fail_unless (c == '\0', "Got %c expected %c", c, '\0'); bf = conf_get_bool (conf, "c group", "h_bool_false", &valid); fail_unless (valid == 1); fail_unless (bf == 0, "Got %d expected %d", bf, 0); bt = conf_get_bool (conf, "c group", "h_bool_true", &valid); fail_unless (valid == 1); fail_unless (bt == 1, "Got %d expected %d", i, 1); f = conf_get_float (conf, "c group", "h_float", &valid); fail_unless (valid == 1); fail_unless (abs (f - 15.5f) < 0.0001f, "Got %f expected %f", f, 15.5f); s = conf_get_string (conf, "c group", "h_string", &valid); fail_unless (valid == 1); fail_unless (strcasecmp (s, " Not Default") == 0, "Got %d expected 'Not Default", s); i = conf_get_int (conf, "d group", "h_int", &valid); fail_unless (valid == 1); fail_unless (i == 100, "Got %d expected %d", i, 100); u = conf_get_uint (conf, "d group", "h_uint", &valid); fail_unless (valid == 1); fail_unless (u == 100, "Got %u expected %u", u, 100); c = conf_get_char (conf, "d group", "h_char", &valid); fail_unless (valid == 1); fail_unless (c == '\07', "Got %c expected %c", c, '\07'); bf = conf_get_bool (conf, "d group", "h_bool_false", &valid); fail_unless (valid == 1); fail_unless (bf == 0, "Got %d expected %d", bf, 0); bt = conf_get_bool (conf, "d group", "h_bool_true", &valid); fail_unless (valid == 1); fail_unless (bt == 1, "Got %d expected %d", i, 1); f = conf_get_float (conf, "d group", "h_float", &valid); fail_unless (valid == 1); fail_unless (abs (f - 100.0f) < 0.0001f, "Got %f expected %f", f, +100.0f); s = conf_get_string (conf, "d group", "h_string", &valid); fail_unless (valid == 1); fail_unless (strcasecmp (s, "\b\v\t\n\r\f\a\\\?\'\"\x10\010") == 0, "Got %d expected 'Not Default", s); i = conf_get_int (conf, "e group", "h_int", &valid); fail_unless (valid == 1); fail_unless (i == -100, "Got %d expected %d", i, -100); u = conf_get_uint (conf, "e group", "h_uint", &valid); fail_unless (valid == 1); fail_unless (u == 0, "Got %u expected %u", u, 0); c = conf_get_char (conf, "e group", "h_char", &valid); fail_unless (valid == 1); fail_unless (c == '\t', "Got %c expected %c", c, '\t'); bf = conf_get_bool (conf, "e group", "h_bool_false", &valid); fail_unless (valid == 1); fail_unless (bf == 0, "Got %d expected %d", bf, 0); bt = conf_get_bool (conf, "e group", "h_bool_true", &valid); fail_unless (valid == 1); fail_unless (bt == 1, "Got %d expected %d", i, 1); f = conf_get_float (conf, "e group", "h_float", &valid); fail_unless (valid == 1); fail_unless (abs (f + 100) < 0.0001f, "Got %f expected %f", f, -100.0f); s = conf_get_string (conf, "e group", "h_string", &valid); fail_unless (valid == 1); fail_unless (strlen (s) == 0, "Got '%s' expected ''", s); conf_free (conf); }
int main (int argc, char *argv[]) { gchar *in_dir; GList *tmp; struct evhttp_uri *uri; struct timeval tv; log_level = LOG_debug; event_set_mem_functions (g_malloc, g_realloc, g_free); // init SSL libraries CRYPTO_set_mem_functions (g_malloc0, g_realloc, g_free); ENGINE_load_builtin_engines (); ENGINE_register_all_complete (); ERR_load_crypto_strings (); OpenSSL_add_all_algorithms (); SSL_load_error_strings (); SSL_library_init (); if (!RAND_poll ()) { fprintf(stderr, "RAND_poll() failed.\n"); return 1; } g_random_set_seed (time (NULL)); in_dir = g_dir_make_tmp (NULL, NULL); g_assert (in_dir); app = g_new0 (Application, 1); app->files_count = 10; app->evbase = event_base_new (); app->dns_base = evdns_base_new (app->evbase, 1); app->conf = conf_create (); conf_add_boolean (app->conf, "log.use_syslog", TRUE); conf_add_uint (app->conf, "auth.ttl", 85800); conf_add_int (app->conf, "pool.writers", 2); conf_add_int (app->conf, "pool.readers", 2); conf_add_int (app->conf, "pool.operations", 4); conf_add_uint (app->conf, "pool.max_requests_per_pool", 100); conf_add_int (app->conf, "connection.timeout", 20); conf_add_int (app->conf, "connection.retries", -1); conf_add_uint (app->conf, "filesystem.dir_cache_max_time", 5); conf_add_boolean (app->conf, "filesystem.cache_enabled", TRUE); conf_add_string (app->conf, "filesystem.cache_dir", "/tmp/hydrafs"); conf_add_string (app->conf, "filesystem.cache_dir_max_size", "1Gb"); conf_add_boolean (app->conf, "statistics.enabled", TRUE); conf_add_int (app->conf, "statistics.port", 8011); conf_add_string (app->conf, "auth.user", "test:tester"); conf_add_string (app->conf, "auth.key", "testing"); uri = evhttp_uri_parse ("https://10.0.0.104:8080/auth/v1.0"); app->ssl_ctx = SSL_CTX_new (TLSv1_client_method ()); app->stats = hfs_stats_srv_create (app); app->auth_client = auth_client_create (app, uri); app->http = http_client_create (app); // start server start_srv (app->evbase, in_dir); app->timeout = evtimer_new (app->evbase, on_output_timer, NULL); evutil_timerclear(&tv); tv.tv_sec = 0; tv.tv_usec = 500; event_add (app->timeout, &tv); event_base_dispatch (app->evbase); evhttp_uri_free (uri); event_del (app->timeout); event_free (app->timeout); evhttp_free (app->http_srv); auth_client_destroy (app->auth_client); evdns_base_free (app->dns_base, 0); event_base_free (app->evbase); conf_destroy (app->conf); g_free (app); return 0; }
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; }
rstatus_t init_server(struct instance *nci) { rstatus_t status; uint32_t i; redisDb *db; vr_conf *conf; conf = conf_create(nci->conf_filename); server.pid = getpid(); server.configfile = getAbsolutePath(nci->conf_filename); server.hz = 10; server.dbnum = 16; array_init(&server.dbs, server.dbnum, sizeof(redisDb)); server.pidfile = nci->pid_filename; server.executable = NULL; server.activerehashing = CONFIG_DEFAULT_ACTIVE_REHASHING; get_random_hex_chars(server.runid, CONFIG_RUN_ID_SIZE); server.arch_bits = (sizeof(long) == 8) ? 64 : 32; server.requirepass = NULL; server.starttime = time(NULL); server.maxclients = conf->max_clients; server.maxmemory = conf->maxmemory == CONF_UNSET_NUM ? 0 : conf->maxmemory; server.maxmemory_policy = CONFIG_DEFAULT_MAXMEMORY_POLICY; server.client_max_querybuf_len = PROTO_MAX_QUERYBUF_LEN; server.commands = dictCreate(&commandTableDictType,NULL); populateCommandTable(); server.delCommand = lookupCommandByCString("del"); server.multiCommand = lookupCommandByCString("multi"); server.lpushCommand = lookupCommandByCString("lpush"); server.lpopCommand = lookupCommandByCString("lpop"); server.rpopCommand = lookupCommandByCString("rpop"); server.sremCommand = lookupCommandByCString("srem"); server.execCommand = lookupCommandByCString("exec"); for (i = 0; i < server.dbnum; i ++) { db = array_push(&server.dbs); redisDbInit(db); } server.monitors = listCreate(); server.loading = 0; server.lua_timedout = 0; server.aof_state = AOF_OFF; server.stop_writes_on_bgsave_err = 0; server.ready_keys = listCreate(); server.slowlog = listCreate(); server.slowlog_entry_id = 0; server.slowlog_log_slower_than = -1; server.slowlog_max_len = CONFIG_DEFAULT_SLOWLOG_MAX_LEN; server.stat_peak_memory = 0; server.system_memory_size = zmalloc_get_memory_size(); server.rdb_child_pid = -1; server.aof_child_pid = -1; server.hash_max_ziplist_entries = OBJ_HASH_MAX_ZIPLIST_ENTRIES; server.hash_max_ziplist_value = OBJ_HASH_MAX_ZIPLIST_VALUE; server.list_max_ziplist_size = OBJ_LIST_MAX_ZIPLIST_SIZE; server.list_compress_depth = OBJ_LIST_COMPRESS_DEPTH; server.set_max_intset_entries = OBJ_SET_MAX_INTSET_ENTRIES; server.zset_max_ziplist_entries = OBJ_ZSET_MAX_ZIPLIST_ENTRIES; server.zset_max_ziplist_value = OBJ_ZSET_MAX_ZIPLIST_VALUE; server.hll_sparse_max_bytes = CONFIG_DEFAULT_HLL_SPARSE_MAX_BYTES; vr_replication_init(); createSharedObjects(); status = master_init(conf); if (status != VR_OK) { log_error("init master thread failed"); return VR_ERROR; } server.port = master.listen->port; status = workers_init(nci->thread_num); if (status != VR_OK) { log_error("init worker threads failed"); return VR_ERROR; } log_debug(LOG_NOTICE, "mem_alloc_lock_type: %s", malloc_lock_type()); log_debug(LOG_NOTICE, "malloc lib: %s", VR_MALLOC_LIB); return VR_OK; }
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; }
/*{{{ main */ int main (int argc, char *argv[]) { Application *app; gboolean verbose = FALSE; gboolean version = FALSE; GError *error = NULL; GOptionContext *context; gchar **s_params = NULL; gchar **s_config = NULL; gboolean foreground = FALSE; gchar conf_str[1023]; struct stat st; gchar **cache_dir = NULL; gchar **s_fuse_opts = NULL; gchar **s_log_file = NULL; guint32 part_size = 0; gboolean disable_syslog = FALSE; gboolean disable_stats = FALSE; gboolean force_head_requests = FALSE; gint uid = -1; gint gid = -1; gint fmode = -1; gint dmode = -1; struct event_config *ev_config; srand (time (NULL)); app = g_new0 (Application, 1); app->conf_path = g_build_filename (SYSCONFDIR, "riofs.conf.xml", NULL); g_snprintf (conf_str, sizeof (conf_str), "Path to configuration file. Default: %s", app->conf_path); GOptionEntry entries[] = { { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &s_params, NULL, NULL }, { "config", 'c', 0, G_OPTION_ARG_FILENAME_ARRAY, &s_config, conf_str, NULL}, { "uid", 0, 0, G_OPTION_ARG_INT, &uid, "Set UID of filesystem owner.", NULL }, { "gid", 0, 0, G_OPTION_ARG_INT, &gid, "Set GID of filesystem owner.", NULL }, { "fmode", 0, 0, G_OPTION_ARG_INT, &fmode, "Set mode for all files.", NULL }, { "dmode", 0, 0, G_OPTION_ARG_INT, &dmode, "Set mode for all directories.", NULL }, { "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground, "Flag. Do not daemonize process.", NULL }, { "cache-dir", 0, 0, G_OPTION_ARG_STRING_ARRAY, &cache_dir, "Set cache directory.", NULL }, { "fuse-options", 'o', 0, G_OPTION_ARG_STRING_ARRAY, &s_fuse_opts, "Fuse options.", "\"opt[,opt...]\"" }, { "disable-syslog", 0, 0, G_OPTION_ARG_NONE, &disable_syslog, "Flag. Disable logging to syslog.", NULL }, { "disable-stats", 0, 0, G_OPTION_ARG_NONE, &disable_stats, "Flag. Disable Statistics HTTP interface.", NULL }, { "part-size", 0, 0, G_OPTION_ARG_INT, &part_size, "Set file part size (in bytes).", NULL }, { "log-file", 'l', 0, G_OPTION_ARG_STRING_ARRAY, &s_log_file, "File to write output.", NULL }, { "force-head-requests", 0, 0, G_OPTION_ARG_NONE, &force_head_requests, "Flag. Send HEAD request for each file.", NULL }, { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Verbose output.", NULL }, { "version", 'V', 0, G_OPTION_ARG_NONE, &version, "Show application version and exit.", NULL }, { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL } }; // init libraries CRYPTO_set_mem_functions (g_malloc0, g_realloc, g_free); ENGINE_load_builtin_engines (); ENGINE_register_all_complete (); ERR_load_crypto_strings (); OpenSSL_add_all_algorithms (); #ifdef SSL_ENABLED SSL_load_error_strings (); SSL_library_init (); #endif g_random_set_seed (time (NULL)); // init main app structure ev_config = event_config_new (); #if defined(__APPLE__) // method select is the preferred method on OS X. kqueue and poll are not supported. event_config_avoid_method (ev_config, "kqueue"); event_config_avoid_method (ev_config, "poll"); #endif app->evbase = event_base_new_with_config (ev_config); event_config_free (ev_config); if (!app->evbase) { LOG_err (APP_LOG, "Failed to create event base !"); application_destroy (app); return -1; } app->dns_base = evdns_base_new (app->evbase, 1); if (!app->dns_base) { LOG_err (APP_LOG, "Failed to create DNS base !"); application_destroy (app); return -1; } app->f_log = NULL; app->log_file_name = NULL; /*{{{ cmd line args */ // parse command line options context = g_option_context_new ("[bucketname] [mountpoint]"); g_option_context_add_main_entries (context, entries, NULL); g_option_context_set_description (context, "Please set both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables!"); if (!g_option_context_parse (context, &argc, &argv, &error)) { g_fprintf (stderr, "Failed to parse command line options: %s\n", error->message); application_destroy (app); g_option_context_free (context); return -1; } g_option_context_free (context); // check if --version is specified if (version) { g_fprintf (stdout, "RioFS File System v%s\n", VERSION); g_fprintf (stdout, "Copyright (C) 2012-2014 Paul Ionkin <*****@*****.**>\n"); g_fprintf (stdout, "Copyright (C) 2012-2014 Skoobe GmbH. All rights reserved.\n"); g_fprintf (stdout, "Libraries:\n"); g_fprintf (stdout, " GLib: %d.%d.%d libevent: %s fuse: %d.%d", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION, LIBEVENT_VERSION, FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION ); #if defined(__APPLE__) || defined(__FreeBSD__) || !defined(__GLIBC__) g_fprintf (stdout, "\n"); #else g_fprintf (stdout, " glibc: %s\n", gnu_get_libc_version ()); #endif g_fprintf (stdout, "Features:\n"); g_fprintf (stdout, " libevent backend method: %s\n", event_base_get_method(app->evbase)); #ifdef SSL_ENABLED g_fprintf (stdout, " SSL enabled\n"); #endif /* { int i; const char **methods = event_get_supported_methods (); g_fprintf (stdout, " Available libevent backend methods:\n"); for (i = 0; methods[i] != NULL; ++i) { g_fprintf (stdout, " %s\n", methods[i]); } } */ return 0; } if (!s_params || g_strv_length (s_params) != 2) { LOG_err (APP_LOG, "Wrong number of provided arguments!\nTry `%s --help' for more information.", argv[0]); application_destroy (app); return -1; } if (verbose) log_level = LOG_debug; else log_level = LOG_msg; /*}}}*/ /*{{{ parse config file */ // user provided alternative config path if (s_config && g_strv_length (s_config) > 0) { g_free (app->conf_path); app->conf_path = g_strdup (s_config[0]); g_strfreev (s_config); } app->conf = conf_create (); if (access (app->conf_path, R_OK) == 0) { LOG_debug (APP_LOG, "Using config file: %s", app->conf_path); if (!conf_parse_file (app->conf, app->conf_path)) { LOG_err (APP_LOG, "Failed to parse configuration file: %s", app->conf_path); application_destroy (app); return -1; } } else { LOG_err (APP_LOG, "Configuration file is not found !"); application_destroy (app); return -1; } if (!conf_check_keys (app->conf, conf_keys_str, conf_keys_len)) { LOG_err (APP_LOG, "Configuration file is missing keys, please re-check your configuration file: %s", app->conf_path); application_destroy (app); return -1; } if (disable_syslog) { conf_set_boolean (app->conf, "log.use_syslog", FALSE); } // update logging settings logger_set_syslog (conf_get_boolean (app->conf, "log.use_syslog")); logger_set_color (conf_get_boolean (app->conf, "log.use_color")); if (cache_dir && g_strv_length (cache_dir) > 0) { conf_set_string (app->conf, "filesystem.cache_dir", cache_dir[0]); g_strfreev (cache_dir); } if (!verbose) log_level = conf_get_int (app->conf, "log.level"); if (uid >= 0) conf_set_int (app->conf, "filesystem.uid", uid); if (gid >= 0) conf_set_int (app->conf, "filesystem.gid", gid); if (fmode >= 0) conf_set_int (app->conf, "filesystem.file_mode", fmode); if (dmode >= 0) conf_set_int (app->conf, "filesystem.dir_mode", dmode); /*}}}*/ // try to get access parameters from the environment if (getenv ("AWS_ACCESS_KEY_ID")) { conf_set_string (app->conf, "s3.access_key_id", getenv ("AWS_ACCESS_KEY_ID")); // else check if it's set it the config file } else { if (!conf_node_exists (app->conf, "s3.access_key_id")) { LOG_err (APP_LOG, "Environment variables are not set!\nTry `%s --help' for more information.", argv[0]); application_destroy (app); return -1; } } if (getenv ("AWS_SECRET_ACCESS_KEY")) { conf_set_string (app->conf, "s3.secret_access_key", getenv ("AWS_SECRET_ACCESS_KEY")); } else { if (!conf_node_exists (app->conf, "s3.secret_access_key")) { LOG_err (APP_LOG, "Environment variables are not set!\nTry `%s --help' for more information.", argv[0]); application_destroy (app); return -1; } } // check if both strings are set if (!conf_get_string (app->conf, "s3.access_key_id") || !conf_get_string (app->conf, "s3.secret_access_key")) { LOG_err (APP_LOG, "Environment variables are not set!\nTry `%s --help' for more information.", argv[0]); application_destroy (app); return -1; } // foreground is set if (foreground) conf_set_boolean (app->conf, "app.foreground", foreground); if (part_size) conf_set_uint (app->conf, "s3.part_size", part_size); if (disable_stats) conf_set_boolean (app->conf, "statistics.enabled", FALSE); if (force_head_requests) conf_set_boolean (app->conf, "s3.force_head_requests_on_lookup", TRUE); else conf_set_boolean (app->conf, "s3.force_head_requests_on_lookup", FALSE); conf_set_string (app->conf, "s3.bucket_name", s_params[0]); if (!application_set_url (app, conf_get_string (app->conf, "s3.endpoint"))) { application_destroy (app); return -1; } if (s_fuse_opts && g_strv_length (s_fuse_opts) > 0) { app->fuse_opts = g_strdup (s_fuse_opts[0]); g_strfreev (s_fuse_opts); } if (s_log_file && g_strv_length (s_log_file) > 0) { app->log_file_name = g_strdup (s_log_file[0]); app->f_log = fopen (s_log_file[0], "a+"); if (!app->f_log) { LOG_err (APP_LOG, "Failed to open log file: %s Error: %s", s_log_file[0], strerror (errno)); application_destroy (app); return -1; } LOG_debug (APP_LOG, "Using %s for storing application logs.", s_log_file[0]); logger_set_file (app->f_log); g_strfreev (s_log_file); } conf_set_string (app->conf, "app.mountpoint", s_params[1]); // check if directory exists if (stat (conf_get_string (app->conf, "app.mountpoint"), &st) == -1) { LOG_err (APP_LOG, "Mountpoint %s does not exist! Please check directory permissions!", conf_get_string (app->conf, "app.mountpoint")); application_destroy (app); return -1; } // check if it's a directory if (!S_ISDIR (st.st_mode)) { LOG_err (APP_LOG, "Mountpoint %s is not a directory!", conf_get_string (app->conf, "app.mountpoint")); application_destroy (app); return -1; } g_strfreev (s_params); #ifdef SSL_ENABLED app->ssl_ctx = SSL_CTX_new (SSLv23_client_method ()); if (!app->ssl_ctx) { LOG_err (APP_LOG, "Failed to initialize SSL engine !"); application_exit (app); return -1; } SSL_CTX_set_options (app->ssl_ctx, SSL_OP_ALL); #endif #ifdef MAGIC_ENABLED app->magic_ctx = magic_open(MAGIC_MIME_TYPE); if (!app->magic_ctx) { LOG_err(APP_LOG, "Failed to initialize magic library\n"); return -1; } if (magic_load(app->magic_ctx, NULL)) { LOG_err(APP_LOG, "Failed to load magic database: %s\n", magic_error(app->magic_ctx)); magic_close(app->magic_ctx); return -1; } #endif app->stat_srv = stat_srv_create (app); if (!app->stat_srv) { application_exit (app); return -1; } // perform the initial request to get bucket ACL (handles redirect as well) app->service_con = http_connection_create (app); if (!app->service_con) { application_destroy (app); return -1; } bucket_client_get (app->service_con, "/?acl", application_on_bucket_acl_cb, app); // start the loop event_base_dispatch (app->evbase); application_destroy (app); return 0; }
int main (int argc, char *argv[]) { struct event_base *evbase; struct evdns_base *dns_base; int i; int test_id = -1; struct evhttp_uri *uri; event_set_mem_functions (g_malloc, g_realloc, g_free); evbase = event_base_new (); dns_base = evdns_base_new (evbase, 1); app = g_new0 (Application, 1); app->evbase = evbase; app->dns_base = dns_base; app->stats = hfs_stats_srv_create (app); app->conf = conf_create (); conf_add_boolean (app->conf, "log.use_syslog", TRUE); conf_add_uint (app->conf, "auth.ttl", 85800); conf_add_int (app->conf, "pool.writers", 2); conf_add_int (app->conf, "pool.readers", 2); conf_add_int (app->conf, "pool.operations", 4); conf_add_uint (app->conf, "pool.max_requests_per_pool", 100); conf_add_int (app->conf, "connection.timeout", 20); conf_add_int (app->conf, "connection.retries", -1); conf_add_uint (app->conf, "filesystem.dir_cache_max_time", 5); conf_add_boolean (app->conf, "filesystem.cache_enabled", TRUE); conf_add_string (app->conf, "filesystem.cache_dir", "/tmp/hydrafs"); conf_add_string (app->conf, "filesystem.cache_dir_max_size", "1Gb"); conf_add_boolean (app->conf, "statistics.enabled", TRUE); conf_add_int (app->conf, "statistics.port", 8011); conf_add_string (app->conf, "auth.user", "test"); conf_add_string (app->conf, "auth.key", "test"); uri = evhttp_uri_parse ("http://127.0.0.1:8011/get_auth"); app->auth_client = auth_client_create (app, uri); if (argc > 1) test_id = atoi (argv[1]); if (test_id >= 0) // run_responce_test (evbase, dns_base, test_id); run_request_test (evbase, dns_base, test_id); else { for (i = 0; i < TID_last_test; i++) { run_responce_test (evbase, dns_base, i); } } evdns_base_free (dns_base, 0); event_base_free (evbase); return 0; }
int main(int argc, char **argv) { rstatus_t status; int i, count; //argv [0] = 'master'; if (nc_save_argv(argc, argv) != NC_OK) { exit(1); } nc_set_default_options(&env_global); env_global.worker_processes = sysconf(_SC_NPROCESSORS_ONLN); env_global.cpu_mask = (1<<env_global.worker_processes) - 1; status = nc_get_options(nc_argc, nc_argv, &env_global); if (status != NC_OK) { nc_show_usage(); exit(1); } if (show_version) { log_stderr("This is nutcracker-%s" CRLF, NC_VERSION_STRING); if (show_help) { nc_show_usage(); } if (describe_stats) { stats_describe(); } exit(0); } if (test_conf) { if (!nc_test_conf(&env_global)) { exit(1); } exit(0); } memcpy(&nci_global, &env_global, sizeof(struct instance)); // context will be initialized in later nc_run. if (nc_init_setproctitle() != NC_OK) { exit(1); } status = nc_master_pre_run(&env_global); if (status != NC_OK) { nc_master_post_run(&env_global); exit(1); } count = 0; for(i = 0; i<sysconf(_SC_NPROCESSORS_ONLN); i++) { count+= env_global.cpu_mask & (0x1<<i)?1:0; //log_error("count %d", count); } if (count < env_global.worker_processes) { log_error('cpu mask %x is not fit in worker processes %d', env_global.cpu_mask, env_global.worker_processes); nc_master_post_run(&env_global); exit(1); } /* parse and create configuration for master */ struct conf* cf = malloc(sizeof(struct conf)); memset(cf, 0, sizeof(struct conf)); cf = conf_create(env_global.conf_filename); if (status != NC_OK) { conf_destroy(cf); exit(1); } /* initialize server pool from configuration */ status = tw_master_conf_init(&env_global.pool, &cf->pool); if (status != NC_OK) { conf_destroy(cf); // release array server pool exit(1); } tw_master_cycle(&env_global); nc_master_post_run(&env_global); exit(1); }
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; }
void server_init( char* conf_file_name, int map_szx, int map_szy, int tdepth, int speed_min, int speed_max, int apple_map_ratio, int apple_pl_ratio, int wall_map_ratio, int wall_pl_ratio, char* balname ) { int i; rand_seed = (unsigned int) time( NULL ); srand( (unsigned int) time( NULL ) ); conf_t* c = conf_create(); assert(c); conf_parse_file( c, conf_file_name ); /* server */ sv.port = conf_get_int( c, "server.port" ); //sv.num_threads = conf_get_int( c, "server.number_of_threads" ); sv.update_interval = conf_get_int( c, "server.update_interval" ); sv.stats_interval = conf_get_int( c, "server.stats_interval" ); assert( sv.port > 1023 ); assert( sv.num_threads > 0 && sv.num_threads <= MAX_THREADS ); assert( sv.update_interval > 0 ); assert( sv.stats_interval > 0 ); /* quests */ sv.quest_between = conf_get_int( c, "server.quest_between" ); sv.quest_length = conf_get_int( c, "server.quest_length" ); assert( sv.quest_between > 0 && sv.quest_length > 0 ); assert( sv.quest_between > sv.update_interval && sv.quest_length > sv.update_interval ); /* initialize clients array */ sv.n_clients = 0; sv.clients = new tm_p_tm_sv_client_t[MAX_ENTITIES]; assert( sv.clients ); /* initialize world */ server_traces_init(); actions_init( c ); server_init_multiple_actions(); entity_types_init( c ); // override the speed settings read from the config file entity_types[ ET_PLAYER ]->attr_types[ PL_SPEED ].min = speed_min; entity_types[ ET_PLAYER ]->attr_types[ PL_SPEED ].max = speed_max; // override the ratio settings read from the config file entity_types[ ET_APPLE ]->ratio = apple_map_ratio; entity_types[ ET_APPLE ]->pl_ratio = apple_pl_ratio; entity_types[ ET_WALL ]->ratio = wall_map_ratio; entity_types[ ET_WALL ]->pl_ratio = wall_pl_ratio; tm_worldmap_init( c, map_szx, map_szy, tdepth ); server_init_quests(); tm_worldmap_generate(); tm_worldmap_is_valid(); /* //burceam: if heuristic1 is turned on, allocate structures for feedback/info sv.h1_dbg_num_ent = NULL; sv.h1_dbg_num_set = NULL; //temporarily turned it always on, for potential study; //if (sv.heuristic1 != 0) { sv.h1_dbg_num_ent = (int *) malloc (sv.wl_cycles * sizeof (int)); sv.h1_dbg_num_set = (int *) malloc (sv.wl_cycles * sizeof (int)); assert ((sv.h1_dbg_num_ent != NULL) && (sv.h1_dbg_num_set != NULL)); int i; for (i = 0; i < sv.wl_cycles; i++) { sv.h1_dbg_num_ent [i] = 0; sv.h1_dbg_num_set [i] = 0; } } //burceam: for heuristic 2, hopefully temporary //note that this may need to be resized at some point: new players may join, //and existing players can drop out. //change_grain_to_entity_for_h3 is obviously meant to be used by heuristic h3. { sv.change_grain_to_entity = (unsigned char *) malloc (sv.wl_client_count * sizeof (unsigned char)); sv.change_grain_to_entity_for_h3 = (unsigned char *) malloc (sv.wl_client_count * sizeof (unsigned char)); int i; for (i = 0; i < sv.wl_client_count; i ++) { sv.change_grain_to_entity [i] = 0; sv.change_grain_to_entity_for_h3 [i] = 0; } } //burceam: this field is used for debugging sv.num_invocations_collision_detection [0] = 0; sv.num_invocations_collision_detection [1] = 0; sv.num_invocations_collision_detection [2] = 0; sv.num_invocations_collision_detection [3] = 0; //burceam: create and initialize the list of area node h_meta pointers. //this MUST be done after tm_worldmap_init(), where I think the area node tree is created //and initialized. We need the depth here. { int i, num_area_nodes = 1; //IMPORTANT: the _actual_ depth of the tree is tdepth+1 !! //root is level "depth", and they keep building until level reaches 0, including for 0! //(nodes at level 0 are the leaves). So for depth=8 entered on cmd line, we really have 9 levels. num_area_nodes = 1 << (tm_wm.depth + 1); sv.hmeta_list = (ptr_t *) malloc (num_area_nodes * sizeof (ptr_t)); for (i = 0; i < num_area_nodes; i++) sv.hmeta_list [i] = NULL; } */ /* initialize synthetic workload */ server_generate_workload(); tm_worldmap_is_valid(); loadb_init( balname ); /* initialize syncronization & server threads */ barrier_init( &sv.barrier, sv.num_threads ); server_stats_init(); sv.done = 0; svts = (server_thread_t*) malloc( sv.num_threads * sizeof(server_thread_t) ); for( i = 0; i < sv.num_threads; ++i ) server_thread_init( &svts[i], i ); log_info( "[I] Server init done.\n" ); }