struct stats * stats_create(uint16_t stats_port, char *stats_ip, int stats_interval, char *source, struct array *server_pool) { rstatus_t status; struct stats *st; char *server_name; st = nc_alloc(sizeof(*st)); if (st == NULL) { return NULL; } st->port = stats_port; st->interval = stats_interval; string_set_raw(&st->addr, stats_ip); st->start_ts = (int64_t)time(NULL); st->buf.len = 0; st->buf.data = NULL; st->buf.size = 0; array_null(&st->current); array_null(&st->shadow); array_null(&st->sum); st->tid = (pthread_t) -1; st->sd = -1; string_set_text(&st->service_str, "service"); //string_set_text(&st->service, "bilitw"); server_name = nc_alloc(100); memset(server_name, 0, 100); sprintf(server_name, "bilitw worker %d", nc_worker_index); string_set_raw(&st->service, server_name); //&st->service->len = strlen(server_name) + 1; //&st->service->data = (uint8_t *)server_name; string_set_text(&st->source_str, "source"); string_set_raw(&st->source, source); string_set_text(&st->version_str, "version"); string_set_text(&st->version, NC_VERSION_STRING); string_set_text(&st->uptime_str, "uptime"); string_set_text(&st->timestamp_str, "timestamp"); string_set_text(&st->ntotal_conn_str, "total_connections"); string_set_text(&st->ncurr_conn_str, "curr_connections"); st->updated = 0; st->aggregate = 0; /* map server pool to current (a), shadow (b) and sum (c) */ status = stats_pool_map(&st->current, server_pool); if (status != NC_OK) { goto error; } status = stats_pool_map(&st->shadow, server_pool); if (status != NC_OK) { goto error; } status = stats_pool_map(&st->sum, server_pool); if (status != NC_OK) { goto error; } status = stats_create_buf(st); if (status != NC_OK) { goto error; } status = stats_start_aggregator(st); if (status != NC_OK) { goto error; } return st; error: stats_destroy(st); return NULL; }
struct stats * stats_create(uint16_t stats_port, char *stats_ip, int stats_interval, char *source, struct array *server_pool, struct context *ctx) { rstatus_t status; struct stats *st; st = dn_alloc(sizeof(*st)); if (st == NULL) { return NULL; } st->port = stats_port; st->interval = stats_interval; string_set_raw(&st->addr, stats_ip); st->start_ts = (int64_t)time(NULL); st->buf.len = 0; st->buf.data = NULL; st->buf.size = 0; array_null(&st->current); array_null(&st->shadow); array_null(&st->sum); st->tid = (pthread_t) -1; st->sd = -1; string_set_text(&st->service_str, "service"); string_set_text(&st->service, "dynomite"); string_set_text(&st->source_str, "source"); string_set_raw(&st->source, source); string_set_text(&st->version_str, "version"); string_set_text(&st->version, DN_VERSION_STRING); string_set_text(&st->uptime_str, "uptime"); string_set_text(&st->timestamp_str, "timestamp"); //for latency histo string_set_text(&st->latency_999th_str, "latency_999th"); string_set_text(&st->latency_99th_str, "latency_99th"); string_set_text(&st->latency_95th_str, "latency_95th"); string_set_text(&st->latency_mean_str, "latency_mean"); string_set_text(&st->latency_max_str, "latency_max"); //for payload size histo string_set_text(&st->payload_size_999th_str, "payload_size_999th"); string_set_text(&st->payload_size_99th_str, "payload_size_99th"); string_set_text(&st->payload_size_95th_str, "payload_size_95th"); string_set_text(&st->payload_size_mean_str, "payload_size_mean"); string_set_text(&st->payload_size_max_str, "payload_size_max"); string_set_text(&st->alloc_msgs_str, "alloc_msgs"); //only display the first pool struct server_pool *sp = (struct server_pool*) array_get(server_pool, 0); string_set_text(&st->rack_str, "rack"); string_copy(&st->rack, sp->rack.data, sp->rack.len); string_set_text(&st->dc_str, "dc"); string_copy(&st->dc, sp->dc.data, sp->dc.len); st->updated = 0; st->aggregate = 0; histo_init(&st->latency_histo); histo_init(&st->payload_size_histo); st->alloc_msgs = 0; /* map server pool to current (a), shadow (b) and sum (c) */ status = stats_pool_map(&st->current, server_pool); if (status != DN_OK) { goto error; } status = stats_pool_map(&st->shadow, server_pool); if (status != DN_OK) { goto error; } status = stats_pool_map(&st->sum, server_pool); if (status != DN_OK) { goto error; } status = stats_create_buf(st); if (status != DN_OK) { goto error; } status = stats_start_aggregator(st); if (status != DN_OK) { goto error; } st->ctx = ctx; return st; error: stats_destroy(st); return NULL; }
struct stats * stats_create(uint16_t stats_port, char *stats_ip, int stats_interval, char *source, struct array *server_pool) { rstatus_t status; struct stats *st; st = nc_alloc(sizeof(*st)); if (st == NULL) { return NULL; } st->port = stats_port; st->interval = stats_interval; string_set_raw(&st->addr, stats_ip); st->start_ts = (int64_t)time(NULL); st->buf.len = 0; st->buf.data = NULL; st->buf.size = 0; array_null(&st->current); array_null(&st->shadow); array_null(&st->sum); st->tid = (pthread_t) -1; st->sd = -1; string_set_text(&st->service_str, "service"); string_set_text(&st->service, "nutcracker"); string_set_text(&st->source_str, "source"); string_set_raw(&st->source, source); string_set_text(&st->version_str, "version"); string_set_text(&st->version, NC_VERSION_STRING); string_set_text(&st->uptime_str, "uptime"); string_set_text(&st->timestamp_str, "timestamp"); string_set_text(&st->ntotal_conn_str, "total_connections"); string_set_text(&st->ncurr_conn_str, "curr_connections"); st->updated = 0; st->aggregate = 0; /* map server pool to current (a), shadow (b) and sum (c) */ status = stats_pool_map(&st->current, server_pool); if (status != NC_OK) { goto error; } status = stats_pool_map(&st->shadow, server_pool); if (status != NC_OK) { goto error; } status = stats_pool_map(&st->sum, server_pool); if (status != NC_OK) { goto error; } status = stats_create_buf(st); //为stats格式信息分配buf空间 if (status != NC_OK) { goto error; } //stats状态信息专门起一个线程来处理 status = stats_start_aggregator(st); if (status != NC_OK) { goto error; } return st; error: stats_destroy(st); return NULL; }
rstatus_t stats_master_server(uint16_t stats_port, char *stats_ip) { rstatus_t status; //struct stats *st; struct sockinfo si; struct string addr; //int sd; string_set_raw(&addr, stats_ip); status = nc_resolve(&addr, stats_port, &si); if (status < 0) { return status; } nc_stats_listen_sd = socket(si.family, SOCK_STREAM, 0); if (nc_stats_listen_sd < 0) { log_error("socket failed: %s", strerror(errno)); return NC_ERROR; } status = nc_set_reuseaddr(nc_stats_listen_sd); if (status < 0) { log_error("set reuseaddr on m %d failed: %s", nc_stats_listen_sd, strerror(errno)); return NC_ERROR; } pthread_t *ptid = nc_alloc(sizeof(pthread_t)); *ptid = (pthread_t) -1; log_error("master sd %d", nc_stats_listen_sd); status = pthread_create(ptid, NULL, stats_master_loop, (void*)(&nc_stats_listen_sd)); if (status < 0) { log_error("stats aggregator create failed: %s", strerror(status)); return NC_ERROR; } log_debug(LOG_NOTICE, "pthread stats master create success %d'", status); status = bind(nc_stats_listen_sd, (struct sockaddr *)&si.addr, si.addrlen); if (status < 0) { log_error("bind on m %d to addr '%.*s:%u' failed: %s", nc_stats_listen_sd, addr.len, addr.data, stats_port, strerror(errno)); return NC_ERROR; } status = listen(nc_stats_listen_sd, SOMAXCONN); if (status < 0) { log_error("listen on m %d failed: %s", nc_stats_listen_sd, strerror(errno)); return NC_ERROR; } log_debug(LOG_NOTICE, "m %d listening on '%.*s:%u'",nc_stats_listen_sd, addr.len, addr.data, stats_port); return NC_OK; }