char* ExecutorGetConfig(const char* key) { char* rtn = NULL; if (!strncmp("loglevel" , key , 8)) { WRLock_LockR(g_executor.conf_lock_); rtn = ylstrdup(log_level_map[g_executor.log_level]); WRLock_UnlockR(g_executor.conf_lock_); } else if (!strncmp("port" , key , 4)) { rtn = ylmalloc(10); snprintf(rtn , 10 , "%d" , g_executor.port_); } else if (!strncmp("concurrent_task" , key , 15)) { rtn = ylmalloc(5); snprintf(rtn , 5 , "%d" , g_executor.max_concurrent_task_); } else if (!strncmp("pluse_interval" , key , 14)) { rtn = ylmalloc(10); WRLock_LockR(g_executor.conf_lock_); snprintf(rtn , 10 , "%d" , g_executor.pluse_interval_); WRLock_UnlockR(g_executor.conf_lock_); } else if (!strncmp("log_path" , key , 8)) { rtn = ylstrdup(g_executor.log_path_); } else if (!strncmp("storage_server" , key , 14)) { rtn = ylmalloc(256); snprintf(rtn , 256, "%s:%d",g_executor.store_server_addr_ , g_executor.store_server_port_); } else if (!strncmp("save_result_to" , key , 14)) { rtn = ylstrdup(g_executor.save_result_to); } return rtn; }
void* ylutfile_fread(unsigned int* outsz, void* f, int btext) { unsigned int osz = YLErr_io; unsigned char* buf = NULL; FILE* fh = (FILE*)f; unsigned int sz; /* do not check error.. very rare to fail!! */ if (0 > fseek(fh, 0, SEEK_END)) { osz = YLErr_io; goto bail; } sz = ftell(fh); if (0 > sz) { osz = YLErr_io; goto bail; } if (0 > fseek(fh, 0, SEEK_SET)) { osz = YLErr_io; goto bail; } /* handle special case - empty file */ if (0 == sz) buf = (btext)? (unsigned char*)ylmalloc(1): NULL; else { /* +1 for trailing 0 */ buf = ylmalloc((unsigned int)sz + ((btext)? 1: 0)); if (!buf) { osz = YLErr_out_of_memory; goto bail; } if (1 != fread(buf, sz, 1, fh)) { osz = YLErr_io; goto bail; } } if (btext) { osz = sz+1; buf[sz] = 0; /* add trailing 0 */ } else osz = sz; /* check case (reading empty file) */ if (!buf) osz = YLOk; if (outsz) *outsz = osz; return buf; bail: if (outsz) *outsz = osz; if (buf) ylfree(buf); return NULL; }
void ExecutorStart() { pthread_t* send_threads = ylmalloc(sizeof(pthread_t) * CLIENT_SEND_QUEUES); int* n_send_thread = ylmalloc(sizeof(int) * CLIENT_SEND_QUEUES); pthread_t* recv_threads = ylmalloc(sizeof(pthread_t) * CLIENT_RECV_QUEUES); int* n_recv_thread = ylmalloc(sizeof(int) * CLIENT_RECV_QUEUES); pthread_t client_thread; for(int i = 0; i != CLIENT_SEND_QUEUES; ++i) { n_send_thread[i] = i; pthread_create(&send_threads[i] , NULL , _send_buffer_monitor , &n_send_thread[i]); } for(int i = 0; i != CLIENT_RECV_QUEUES; ++i) { n_recv_thread[i] = i; pthread_create(&recv_threads[i] , NULL , _recv_buffer_monitor , &n_recv_thread[i]); } pthread_create(&client_thread , NULL , _client_monitor , NULL); Event_Main(g_executor.evloop); Event_DestroyLoop(g_executor.evloop); }
int ExcutorInitSingalHandlers() { signal(SIGHUP, SIG_IGN); signal(SIGPIPE, SIG_IGN); sigset_t* sig = ylmalloc(sizeof(sigset_t)); sigemptyset(sig); sigaddset(sig , SIGTERM); sigaddset(sig , SIGCHLD); if (pthread_sigmask(SIG_BLOCK, sig, NULL)) { return ERR_TASK_MONITOR_INIT_FAIL; } pthread_t sig_thread; pthread_create(&sig_thread, NULL, _executor_sig_monitor_thread, (void*) sig); return 0; }
int ExecutorLoadConfig(const char* filename) { g_executor.port_ = DEFAULT_EXECUTOR_RUN_PORT; g_executor.pluse_interval_ = DEFAULT_EXECUTOR_PLUSE_INTVAL; g_executor.max_concurrent_task_ = DEFAULT_EXECUTOR_CONCURRENT; g_executor.log_level = LOG_DEBUG; int tmp_log_path_len = strlen(DEFAULT_EXECUTOR_LOG_PATH) + strlen(DEFAULT_EXECUTOR_LOG_FILE) + 1; g_executor.log_path_ = ylmalloc(tmp_log_path_len); snprintf(g_executor.log_path_, tmp_log_path_len, "%s%s", DEFAULT_EXECUTOR_LOG_PATH, DEFAULT_EXECUTOR_LOG_FILE); char* tmp_server = ylstrdup(DEFAULT_EXECUTOR_STORAGE_SERVER); char* str_pos = strstr(tmp_server, ":"); if (!str_pos || !is_numeric(str_pos + 1)) { /*this should not be happened, unless someone had written some incorrect code*/ ylfree(g_executor.log_path_); ylfree(tmp_server); return ERR_EXECUTOR_CONFIG_INCORRECT; } *str_pos++ = '\0'; snprintf(g_executor.store_server_addr_, IP_ADDR_LEN, "%s", tmp_server); g_executor.store_server_port_ = atoi(str_pos); ylfree(tmp_server); g_executor.save_result_to = ylstrdup(DEFAULT_EXECUTOR_SAVE_RESULT_TO); //Read Configure File FILE* fp; if (!filename) { return 0; } if ((fp = fopen(filename , "r")) == NULL) { return ERR_EXECUTOR_CONFIG_FILE_NOT_EXIST; } char line_buffer[MAX_CONFIG_LINE]; char filter_line[MAX_CONFIG_LINE]; int c; int pos = 0; while( (c = fgetc(fp)) != EOF ) { if ('\n' == c || pos == MAX_CONFIG_LINE_LEN) { line_buffer[pos] = '\0'; //cancel the notation str_pos = strstr(line_buffer , "#"); if (str_pos) { *str_pos = '\0'; } //trim the input buffer pos = 0; str_pos = line_buffer; while(*str_pos != '\0') { if (*str_pos != ' ' && *str_pos != '\t') { filter_line[pos++] = *str_pos; } ++str_pos; } filter_line[pos] = '\0'; if (pos) { // printf("[%s]\n",filter_line); str_pos = strstr(filter_line , "="); if (!str_pos || !*(str_pos + 1)) { return ERR_EXECUTOR_CONFIG_INCORRECT; } *str_pos++ = '\0'; if (!strncmp("port" , filter_line , pos)) { if (!is_numeric(str_pos)) { return ERR_EXECUTOR_CONFIG_INCORRECT; } g_executor.port_ = atoi(str_pos); } else if (!strncmp("concurrent_task" , filter_line , pos)) { if (!is_numeric(str_pos)) { return ERR_EXECUTOR_CONFIG_INCORRECT; } g_executor.max_concurrent_task_ = atoi(str_pos); } else if (!strncmp("loglevel" , filter_line , pos)) { int loglevel; if (!strncmp("ERROR", str_pos, 5)) { loglevel = LOG_ERROR; } else if (!strncmp("WARNING", str_pos, 7)) { loglevel = LOG_WARNING; } else if (!strncmp("NOTICE", str_pos, 6)) { loglevel = LOG_NOTICE; } else if (!strncmp("DEBUG", str_pos, 5)) { loglevel = LOG_DEBUG; } else { return ERR_EXECUTOR_CONFIG_INCORRECT; } g_executor.log_level = loglevel; } else if (!strncmp("pluse_interval" , filter_line , pos)) { if (!is_numeric(str_pos)) { return ERR_EXECUTOR_CONFIG_INCORRECT; } g_executor.pluse_interval_ = atoi(str_pos); if (g_executor.pluse_interval_ < DEFAULT_EXECUTOR_PLUSE_INTVAL) { g_executor.pluse_interval_ = DEFAULT_EXECUTOR_PLUSE_INTVAL; } } else if (!strncmp("log_path" , filter_line , pos)) { int tmp_len = pos - (int)(str_pos - filter_line); if (tmp_len >= strlen(g_executor.log_path_)) { g_executor.log_path_ = ylrealloc(g_executor.log_path_ , tmp_len + 1); } snprintf(g_executor.log_path_ , tmp_len + 1, "%s" , str_pos); } else if (!strncmp("storage_server" , filter_line , pos)) { char* tmp = strstr(str_pos , ":"); if (!tmp || !is_numeric(tmp + 1)) { return ERR_EXECUTOR_CONFIG_INCORRECT; } *tmp++ = '\0'; snprintf(g_executor.store_server_addr_ , IP_ADDR_LEN , "%s" , str_pos); g_executor.store_server_port_ = atoi(tmp); } else if (!strncmp("save_result_to" , filter_line , pos)) { int tmp_len = pos - (int)(str_pos - filter_line); if (tmp_len >= strlen(g_executor.save_result_to)) { g_executor.save_result_to = ylrealloc(g_executor.save_result_to , tmp_len + 1); } snprintf(g_executor.save_result_to , tmp_len + 1, "%s" , str_pos); } else if (!strncmp("daemonize" , filter_line , pos)) { if (!strncmp(str_pos , "yes" , 3)) { g_executor.daemonize = true; } else { g_executor.daemonize = false; } } else { printf("%s\n" , filter_line); return ERR_EXECUTOR_CONFIG_UNRECOGNIZED; } } pos = 0; } else { line_buffer[pos++] = (char)c; } } return 0; }
static void* _pb_alloc(void* alloc_data , size_t size) { AVOID_NOUSED(alloc_data); if (size <= 0) return NULL; return ylmalloc(size); }
static void _read_query_from_client(EventLoop_t evloop , int fd , void* clientData , int mask) { AVOID_NOUSED(evloop); AVOID_NOUSED(mask); // AVOID_NOUSED(clientData); // Client_t* c = Map_GetPtr(g_executor.clients , &fd); Client_t* c = (Client_t*)clientData; int nread; char errmsg[MAX_NET_ERR_LEN]; StdMutex_Lock(c->oplock); nread = netRead(errmsg, c->fd, c->query_buffer + c->buffer_len, CLIENTS_BUFFER_LEN - c->buffer_len); if (nread == ERR_NETWORK_EXCEPTION) { //ERR HAPPEND StdMutex_Unlock(c->oplock); Log(LOG_ERROR,"Read query from client failed.reason:##%s##", errmsg); _release_client(fd, RELEASE_BY_ERROR); return; } else if (nread == 0) { //Client Close the connection Log(LOG_NOTICE,"[CLIENT_DISCONNECT][%s:%d]", c->addr, c->port); StdMutex_Unlock(c->oplock); _release_client(fd, RELEASE_BY_CLIENT); return; } c->buffer_len += nread; //try to extract as many package as possible char* iterator = c->query_buffer; while (1) { if (c->buffer_len < MSGHEAD_SIZE) { break; } if (!isValidRequest(iterator)) { //not a valid request, clear client Log(LOG_NOTICE,"invalid request received from [%s:%d], close the connection" , c->addr , c->port); StdMutex_Unlock(c->oplock); _release_client(fd , RELEASE_BY_ERROR); return; } Message_t new_msg; new_msg.msgid = *((int32_t*)iterator + 1); new_msg.pkg_len = *((int32_t*)iterator + 2); if (c->status == CLI_INIT && new_msg.msgid != MSG_EXCU_AUTH && new_msg.msgid != MSG_EXCU_PLUSE) { //client not login, kick out Log(LOG_NOTICE,"client [%s:%d] should get authentication first before anyother request" , c->addr , c->port); StdMutex_Unlock(c->oplock); _release_client(fd , RELEASE_BY_ERROR); return; } c->last_seen = time(NULL); if (new_msg.pkg_len + MSGHEAD_SIZE > c->buffer_len) { //not a complete package int offset = iterator - c->query_buffer; for (int pos = 0; pos != c->buffer_len; ++pos) { c->query_buffer[pos] = c->query_buffer[pos + offset]; } break; } //extract one package new_msg.sockfd = c->fd; uint8_t* pkg = ylmalloc(new_msg.pkg_len); memcpy(pkg , iterator + MSGHEAD_SIZE , new_msg.pkg_len); new_msg.package = pkg; uint8_t recv_buf = atomic_add_then_fetch((int*)&g_executor.cur_recv_buffer_ , 1)%CLIENT_RECV_QUEUES; Queue_Push(g_executor.recv_buffer_[recv_buf] , &new_msg); sem_post(&g_executor.recv_buffer_num_[recv_buf]); int msglen = new_msg.pkg_len + MSGHEAD_SIZE; c->buffer_len -= msglen; iterator += msglen; } StdMutex_Unlock(c->oplock); }
static void* _client_dup_func(const void* src_client) { Client_t* client = ylmalloc(sizeof(Client_t)); memcpy(client , src_client , sizeof(Client_t)); return client; }