//注意走到这里是通过epoll触发走到这里面的 可以通过客户端telnet 127.0.0.1 22222来获取 static rstatus_t stats_send_rsp(struct stats *st) { rstatus_t status; ssize_t n; int sd; status = stats_make_rsp(st); if (status != NC_OK) { return status; } sd = accept(st->sd, NULL, NULL); if (sd < 0) { log_error("accept on m %d failed: %s", st->sd, strerror(errno)); return NC_ERROR; } log_debug(LOG_VERB, "send stats on sd %d %d bytes", sd, st->buf.len); n = nc_sendn(sd, st->buf.data, st->buf.len); if (n < 0) { log_error("send stats on sd %d failed: %s", sd, strerror(errno)); close(sd); return NC_ERROR; } close(sd); return NC_OK; }
static rstatus_t stats_send_rsp(struct stats *st) { rstatus_t status; ssize_t n; //int sd; //int fd; status = stats_make_rsp(st); if (status != NC_OK) { return status; } /* sd = accept(st->sd, NULL, NULL); if (sd < 0) { log_error("accept on m %d failed: %s", st->sd, strerror(errno)); return NC_ERROR; } log_debug(LOG_VERB, "send stats on sd %d %d bytes", sd, st->buf.len); n = nc_sendn(sd, st->buf.data, st->buf.len); if (n < 0) { log_error("send stats on sd %d failed: %s", sd, strerror(errno)); close(sd); return NC_ERROR; } close(sd); */ nc_channel_msg_t message; memset(&message, 0, sizeof(nc_channel_msg_t)); message.command = NC_CMD_GET_STATS; message.len = st->buf.len; nc_write_channel(nc_worker_channel, &message, sizeof(nc_channel_msg_t)); n = nc_sendn(nc_worker_channel, st->buf.data, st->buf.len); if (n < 0) { log_error("nc_sendn %d failed: %s", nc_worker_channel, strerror(errno)); return NC_ERROR; } stats_swap(st); /* int fd = get_logger_fd(); if (fd < 0) { return; } n = nc_write(fd, st->buf.data, st->buf.len); if (n < 0) { log_error("nc_write %d failed: %s", fd, strerror(errno)); return NC_ERROR; } */ return NC_OK; }
static rstatus_t stats_master_send_rsp(int *psd) { int n; ssize_t len; int sd; int i; nc_channel_msg_t message; char buf[1000]; memset(buf, 0, sizeof(char)*1000); char *snd_buf = NULL; memset(&message, 0, sizeof(nc_channel_msg_t)); message.command = NC_CMD_GET_STATS; if (*psd) { sd = accept(*psd, NULL, NULL); if (sd < 0) { log_error("accept on m %d failed: %s", sd, strerror(errno)); return NC_ERROR; } } // still in reconfiguration process if (nc_reload_start) { sprintf(buf, "%s", "no stats get due to still during reconfiguration period."); if (*psd) { len = nc_strlen(buf)+1; len = nc_sendn(sd, buf, len); if (len < 0) { log_error("send stats on sd %d failed: %s", sd, strerror(errno)); } close(sd); } return NC_OK; } //broadcast for (i = 0; i < nc_last_process; i++) { if (nc_processes[i].pid == -1 || nc_processes[i].pid == 0) { continue; } if (nc_write_channel(nc_processes[i].channel[0], &message, sizeof(nc_channel_msg_t)) == NC_OK) { if (nc_set_blocking(nc_processes[i].channel[0]) < 0) { log_error("set channel %d block failed while core timeout %s", nc_processes[i].channel[0] , strerror(errno)); continue; } n = nc_read_channel(nc_processes[i].channel[0], &env_global.ctrl_msg, sizeof(nc_channel_msg_t)); if (env_global.ctrl_msg.command != NC_CMD_GET_STATS || n < 0) { log_error("failure: get stats from worker %d receive length, %s", nc_processes[i].channel[0] , strerror(errno)); } else { log_error("success: get stats from worker %d receive length, %d", nc_processes[i].channel[0] , n); snd_buf = nc_alloc(n + n); len = nc_recvn(nc_processes[i].channel[0], snd_buf, n); if (len < 0) { if (*psd) { log_error("recv stats on sd %d failed: %s", sd, strerror(errno)); } nc_free(snd_buf); snd_buf = NULL; continue; } if (*psd) { len = nc_sendn(sd, snd_buf, len); if (len < 0) { log_error("send stats on sd %d failed: %s", sd, strerror(errno)); nc_free(snd_buf); snd_buf = NULL; continue; } } len = nc_write(env_global.stats_fd, snd_buf, len); if (len < 0) { log_error("nc_write %d failed: %s", env_global.stats_fd, strerror(errno)); continue; } nc_free(snd_buf); snd_buf = NULL; } if (nc_set_nonblocking(nc_processes[i].channel[0]) < 0) { log_error("set channel %d nonblock failed while core timeout %s", nc_processes[i].channel[0] , strerror(errno)); } } } if (*psd) { shutdown(sd, SHUT_RDWR); close(sd); } return NC_OK; }