Added HCL_SERVER_WORKER_MAX_COUNT

changed the main server loop to purge dead clients as soon as the multiplex is finished
This commit is contained in:
hyung-hwan 2018-03-16 14:57:34 +00:00
parent 70ef4b6299
commit 0b7acc1fd8
5 changed files with 81 additions and 37 deletions

View File

@ -31,38 +31,46 @@ static hcl_ooch_t errstr_1[] = {'g','e','n','e','r','i','c',' ','e','r','r','o',
static hcl_ooch_t errstr_2[] = {'n','o','t',' ','i','m','p','l','e','m','e','n','t','e','d','\0'};
static hcl_ooch_t errstr_3[] = {'s','u','b','s','y','s','t','e','m',' ','e','r','r','o','r','\0'};
static hcl_ooch_t errstr_4[] = {'i','n','t','e','r','n','a','l',' ','e','r','r','o','r',' ','t','h','a','t',' ','s','h','o','u','l','d',' ','n','e','v','e','r',' ','h','a','v','e',' ','h','a','p','p','e','n','e','d','\0'};
static hcl_ooch_t errstr_5[] = {'i','n','s','u','f','f','i','c','i','e','n','t',' ','s','y','s','t','e','m',' ','m','e','m','o','r','y','\0'};
static hcl_ooch_t errstr_6[] = {'i','n','s','u','f','f','i','c','i','e','n','t',' ','o','b','j','e','c','t',' ','m','e','m','o','r','y','\0'};
static hcl_ooch_t errstr_7[] = {'i','n','v','a','l','i','d',' ','c','l','a','s','s','/','t','y','p','e','\0'};
static hcl_ooch_t errstr_8[] = {'i','n','v','a','l','i','d',' ','p','a','r','a','m','e','t','e','r',' ','o','r',' ','a','r','g','u','m','e','n','t','\0'};
static hcl_ooch_t errstr_8[] = {'i','n','v','a','l','i','d',' ','p','a','r','a','m','e','t','e','r','/','a','r','g','u','m','e','n','t','\0'};
static hcl_ooch_t errstr_9[] = {'d','a','t','a',' ','n','o','t',' ','f','o','u','n','d','\0'};
static hcl_ooch_t errstr_10[] = {'e','x','i','s','t','i','n','g','/','d','u','p','l','i','c','a','t','e',' ','d','a','t','a','\0'};
static hcl_ooch_t errstr_11[] = {'b','u','s','y','\0'};
static hcl_ooch_t errstr_12[] = {'a','c','c','e','s','s',' ','d','e','n','i','e','d','\0'};
static hcl_ooch_t errstr_13[] = {'o','p','e','r','a','t','i','o','n',' ','n','o','t',' ','p','e','r','m','i','t','t','e','d','\0'};
static hcl_ooch_t errstr_14[] = {'n','o','t',' ','a',' ','d','i','r','e','c','t','o','r','y','\0'};
static hcl_ooch_t errstr_15[] = {'i','n','t','e','r','r','u','p','t','e','d','\0'};
static hcl_ooch_t errstr_16[] = {'p','i','p','e',' ','e','r','r','o','r','\0'};
static hcl_ooch_t errstr_17[] = {'r','e','s','o','u','r','c','e',' ','t','e','m','p','o','r','a','r','i','l','y',' ','u','n','a','v','a','i','l','a','b','l','e','\0'};
static hcl_ooch_t errstr_18[] = {'b','a','d',' ','s','y','s','t','e','m',' ','h','a','n','d','l','e','\0'};
static hcl_ooch_t errstr_19[] = {'t','o','o',' ','m','a','n','y',' ','f','r','a','m','e','s','\0'};
static hcl_ooch_t errstr_20[] = {'m','e','s','s','a','g','e',' ','r','e','c','e','i','v','e','r',' ','e','r','r','o','r','\0'};
static hcl_ooch_t errstr_21[] = {'m','e','s','s','a','g','e',' ','s','e','n','d','i','n','g',' ','e','r','r','o','r','\0'};
static hcl_ooch_t errstr_22[] = {'w','r','o','n','g',' ','n','u','m','b','e','r',' ','o','f',' ','a','r','g','u','m','e','n','t','s','\0'};
static hcl_ooch_t errstr_23[] = {'r','a','n','g','e',' ','e','r','r','o','r','\0'};
static hcl_ooch_t errstr_24[] = {'b','y','t','e','-','c','o','d','e',' ','f','u','l','l','\0'};
static hcl_ooch_t errstr_25[] = {'d','i','c','t','i','o','n','a','r','y',' ','f','u','l','l','\0'};
static hcl_ooch_t errstr_26[] = {'n','o',' ','m','o','r','e',' ','i','n','t','u','t','\0'};
static hcl_ooch_t errstr_27[] = {'t','o','o',' ','m','a','n','y',' ','p','a','r','a','m','e','t','e','r','s','\0'};
static hcl_ooch_t errstr_28[] = {'*','*','*',' ','u','n','d','e','f','i','n','e','d',' ','e','r','r','o','r',' ','*','*','*','\0'};
static hcl_ooch_t errstr_26[] = {'p','r','o','c','e','s','s','o','r',' ','f','u','l','l','\0'};
static hcl_ooch_t errstr_27[] = {'n','o',' ','m','o','r','e',' ','i','n','p','u','t','\0'};
static hcl_ooch_t errstr_28[] = {'t','o','o',' ','m','a','n','y',' ','i','t','e','m','s','\0'};
static hcl_ooch_t errstr_29[] = {'d','i','v','i','d','e',' ','b','y',' ','z','e','r','o','\0'};
static hcl_ooch_t errstr_30[] = {'I','/','O',' ','e','r','r','o','r','\0'};
static hcl_ooch_t errstr_31[] = {'e','n','c','o','d','i','n','g',' ','c','o','n','v','e','r','s','i','o','n',' ','e','r','r','o','r','\0'};
static hcl_ooch_t errstr_32[] = {'b','u','f','f','e','r',' ','f','u','l','l','\0'};
static hcl_ooch_t errstr_33[] = {'s','y','n','t','a','x',' ','e','r','r','o','r','\0'};
static hcl_ooch_t errstr_34[] = {'c','a','l','l',' ','e','r','r','o','r','\0'};
static hcl_ooch_t errstr_35[] = {'r','e','c','a','l','l',' ','p','r','o','h','i','b','i','t','e','d','\0'};
static hcl_ooch_t errstr_36[] = {'a','r','g','u','m','e','n','t',' ','n','u','m','b','e','r',' ','e','r','r','o','r','\0'};
static hcl_ooch_t* errstr[] =
{
errstr_0, errstr_1, errstr_2, errstr_3, errstr_4, errstr_5, errstr_6, errstr_7,

View File

@ -32,6 +32,9 @@
#include <string.h>
#include <errno.h>
#define TOKEN_NAME_ALIGN 64
#define WID_MAP_ALIGN 512
#if defined(_WIN32)
# include <windows.h>
# include <tchar.h>
@ -266,6 +269,7 @@ struct hcl_server_t
unsigned int trait;
unsigned int logmask;
hcl_oow_t worker_stack_size;
hcl_oow_t worker_max_count;
hcl_ntime_t worker_idle_timeout;
hcl_oow_t actor_heap_size;
hcl_ntime_t actor_max_runtime;
@ -275,6 +279,7 @@ struct hcl_server_t
{
hcl_server_worker_t* head;
hcl_server_worker_t* tail;
hcl_oow_t count;
} worker_list[2];
struct
@ -283,7 +288,7 @@ struct hcl_server_t
hcl_oow_t capa;
hcl_oow_t free_first;
hcl_oow_t free_last;
} wid_map;
} wid_map; /* worker's id map */
int mux_pipe[2]; /* pipe to break the blocking multiplexer in the main server loop */
@ -1273,7 +1278,7 @@ static HCL_INLINE int add_token_char (hcl_server_proto_t* proto, hcl_ooch_t c)
hcl_ooch_t* tmp;
hcl_oow_t capa;
capa = HCL_ALIGN_POW2(proto->tok.len + 1, 64);
capa = HCL_ALIGN_POW2(proto->tok.len + 1, TOKEN_NAME_ALIGN);
tmp = (hcl_ooch_t*)HCL_MMGR_REALLOC(proto->worker->server->mmgr, proto->tok.ptr, capa * HCL_SIZEOF(*tmp));
if (!tmp)
{
@ -1385,7 +1390,7 @@ static void exec_runtime_handler (hcl_tmr_t* tmr, const hcl_ntime_t* now, hcl_tm
hcl_server_proto_t* proto;
proto = (hcl_server_proto_t*)evt->ctx;
HCL_LOG0 (proto->worker->server->dummy_hcl, SERVER_LOGMASK_INFO, "Aborting script execution for max_actor_runtime exceeded\n"); /* TODO: include worker id into the message.. */
HCL_LOG1 (proto->worker->server->dummy_hcl, SERVER_LOGMASK_INFO, "Aborting script execution for max_actor_runtime exceeded [%zu]\n", proto->worker->wid);
hcl_abort (proto->hcl);
}
@ -1780,15 +1785,12 @@ static HCL_INLINE int prepare_to_acquire_wid (hcl_server_t* server)
HCL_ASSERT (server->dummy_hcl, server->wid_map.free_first == HCL_SERVER_WID_INVALID);
HCL_ASSERT (server->dummy_hcl, server->wid_map.free_last == HCL_SERVER_WID_INVALID);
new_capa = server->wid_map.capa + 128; /* TODO: adjust this incremental size ? */
new_capa = HCL_ALIGN_POW2(server->wid_map.capa + 1, WID_MAP_ALIGN);
if (new_capa > HCL_SERVER_WID_MAX)
{
if (server->wid_map.capa >= HCL_SERVER_WID_MAX)
{
/* too many workers??? */
/* TODO: error handling */
//hcl_seterrbfmt (hcl, HCL_EPFULL, "maximum number(%zd) of processes reached", HCL_SMOOI_MAX);
printf ("too many workers??? \n");
hcl_server_seterrnum (server, HCL_EFLOOD);
return -1;
}
@ -1798,8 +1800,7 @@ printf ("too many workers??? \n");
tmp = (hcl_server_wid_map_data_t*)HCL_MMGR_REALLOC(server->mmgr, server->wid_map.ptr, HCL_SIZEOF(*tmp) * new_capa);
if (!tmp)
{
// TODO: error handling ....
printf ("cannot reallocate wid map...\n");
hcl_server_seterrnum (server, HCL_ESYSERR);
return -1;
}
@ -1879,11 +1880,11 @@ static void close_worker_socket (hcl_server_worker_t* worker)
{
if (worker->proto)
{
HCL_LOG1 (worker->proto->hcl, SERVER_LOGMASK_INFO, "Closing worker socket %d\n", worker->sck);
HCL_LOG2 (worker->proto->hcl, SERVER_LOGMASK_INFO, "Closing worker socket %d [%zu]\n", worker->sck, worker->wid);
}
else
{
HCL_LOG1 (worker->server->dummy_hcl, SERVER_LOGMASK_INFO, "Closing worker socket %d\n", worker->sck);
HCL_LOG2 (worker->server->dummy_hcl, SERVER_LOGMASK_INFO, "Closing worker socket %d [%zu]\n", worker->sck, worker->wid);
}
close (worker->sck);
worker->sck = -1;
@ -1893,11 +1894,21 @@ static void close_worker_socket (hcl_server_worker_t* worker)
static void free_worker (hcl_server_worker_t* worker)
{
close_worker_socket (worker);
if (worker->proto)
{
HCL_LOG1 (worker->proto->hcl, SERVER_LOGMASK_INFO, "Killing worker [%zu]\n", worker->wid);
}
else
{
HCL_LOG1 (worker->server->dummy_hcl, SERVER_LOGMASK_INFO, "Killing worker [%zu]\n", worker->wid);
}
release_wid (worker->server, worker);
HCL_MMGR_FREE (worker->server->mmgr, worker);
}
static void add_hcl_server_worker_to_server (hcl_server_t* server, hcl_server_worker_state_t wstate, hcl_server_worker_t* worker)
static void add_worker_to_server (hcl_server_t* server, hcl_server_worker_state_t wstate, hcl_server_worker_t* worker)
{
HCL_ASSERT (server->dummy_hcl, worker->server == server);
@ -1916,6 +1927,7 @@ static void add_hcl_server_worker_to_server (hcl_server_t* server, hcl_server_wo
worker->next_worker = HCL_NULL;
}
server->worker_list[wstate].count++;
worker->state = wstate;
}
@ -1931,6 +1943,8 @@ static void zap_worker_in_server (hcl_server_t* server, hcl_server_worker_t* wor
if (worker->next_worker) worker->next_worker->prev_worker = worker->prev_worker;
else server->worker_list[wstate].tail = worker->prev_worker;
HCL_ASSERT (server->dummy_hcl, server->worker_list[wstate].count > 0);
server->worker_list[wstate].count--;
worker->prev_worker = HCL_NULL;
worker->next_worker = HCL_NULL;
}
@ -1953,7 +1967,7 @@ static void* worker_main (void* ctx)
}
pthread_mutex_lock (&server->worker_mutex);
add_hcl_server_worker_to_server (server, HCL_SERVER_WORKER_STATE_ALIVE, worker);
add_worker_to_server (server, HCL_SERVER_WORKER_STATE_ALIVE, worker);
pthread_mutex_unlock (&server->worker_mutex);
while (!server->stopreq)
@ -1969,7 +1983,7 @@ static void* worker_main (void* ctx)
if (!worker->claimed)
{
zap_worker_in_server (server, worker);
add_hcl_server_worker_to_server (server, HCL_SERVER_WORKER_STATE_DEAD, worker);
add_worker_to_server (server, HCL_SERVER_WORKER_STATE_DEAD, worker);
}
pthread_mutex_unlock (&server->worker_mutex);
@ -2129,8 +2143,6 @@ int hcl_server_start (hcl_server_t* server, const hcl_bch_t* addrs)
struct pollfd pfd[2];
int n, pc;
purge_all_workers (server, HCL_SERVER_WORKER_STATE_DEAD);
pthread_mutex_lock (&server->tmr_mutex);
n = hcl_tmr_gettmout(server->tmr, HCL_NULL, &tmout);
pthread_mutex_unlock (&server->tmr_mutex);
@ -2144,6 +2156,7 @@ int hcl_server_start (hcl_server_t* server, const hcl_bch_t* addrs)
pfd[pc++].events = POLLIN | POLLERR;
n = poll(pfd, pc, HCL_SECNSEC_TO_MSEC(tmout.sec, tmout.nsec));
purge_all_workers (server, HCL_SERVER_WORKER_STATE_DEAD);
if (n <= -1)
{
if (server->stopreq) break; /* normal termination requested */
@ -2176,22 +2189,30 @@ int hcl_server_start (hcl_server_t* server, const hcl_bch_t* addrs)
if (server->stopreq) break; /* normal termination requested */
if (errno == EINTR) continue; /* interrupted but not termination requested */
set_err_with_syserr (server, errno, "unable to accept worker on socket %d", pfd[n]);
set_err_with_syserr (server, errno, "unable to accept worker on server socket %d", pfd[n]);
xret = -1;
break;
}
HCL_LOG1 (server->dummy_hcl, SERVER_LOGMASK_INFO, "Accepted worker - socket %d\n", cli_fd);
if (server->cfg.worker_max_count > 0)
{
int flood;
pthread_mutex_lock (&server->worker_mutex);
flood = (server->worker_list[HCL_SERVER_WORKER_STATE_ALIVE].count >= server->cfg.worker_max_count);
pthread_mutex_unlock (&server->worker_mutex);
if (flood) goto unable_to_accomodate;
}
worker = alloc_worker(server, cli_fd);
if (!worker)
{
HCL_LOG1 (server->dummy_hcl, SERVER_LOGMASK_ERROR, "Unable to accomodate accepted worker - socket %d\n", cli_fd);
unable_to_accomodate:
HCL_LOG1 (server->dummy_hcl, SERVER_LOGMASK_ERROR, "Unable to accomodate worker - socket %d\n", cli_fd);
close (cli_fd);
}
else
{
HCL_LOG2 (server->dummy_hcl, SERVER_LOGMASK_INFO, "Assigned WID(%zd) to the accepted server socket %d\n", worker->wid, cli_fd);
HCL_LOG2 (server->dummy_hcl, SERVER_LOGMASK_INFO, "Accomodated worker [%zu] - socket %d\n", worker->wid, cli_fd);
if (pthread_create(&thr, &thr_attr, worker_main, worker) != 0)
{
free_worker (worker);
@ -2251,6 +2272,10 @@ int hcl_server_setoption (hcl_server_t* server, hcl_server_option_t id, const vo
}
return 0;
case HCL_SERVER_WORKER_MAX_COUNT:
server->cfg.worker_max_count = *(hcl_oow_t*)value;
return 0;
case HCL_SERVER_WORKER_STACK_SIZE:
server->cfg.worker_stack_size = *(hcl_oow_t*)value;
return 0;
@ -2284,6 +2309,10 @@ int hcl_server_getoption (hcl_server_t* server, hcl_server_option_t id, void* va
*(unsigned int*)value = server->cfg.logmask;
return 0;
case HCL_SERVER_WORKER_MAX_COUNT:
*(hcl_oow_t*)value = server->cfg.worker_max_count;
return 0;
case HCL_SERVER_WORKER_STACK_SIZE:
*(hcl_oow_t*)value = server->cfg.worker_stack_size;
return 0;

View File

@ -37,6 +37,7 @@ enum hcl_server_option_t
{
HCL_SERVER_TRAIT,
HCL_SERVER_LOG_MASK,
HCL_SERVER_WORKER_MAX_COUNT,
HCL_SERVER_WORKER_STACK_SIZE,
HCL_SERVER_WORKER_IDLE_TIMEOUT,
HCL_SERVER_ACTOR_HEAP_SIZE,
@ -66,7 +67,7 @@ typedef void (*hcl_server_log_write_t) (
const hcl_ooch_t* msg,
hcl_oow_t len
);
struct hcl_server_prim_t
{
hcl_server_log_write_t log_write;

View File

@ -47,43 +47,46 @@ enum hcl_errnum_t
{
HCL_ENOERR, /**< no error */
HCL_EGENERIC, /**< generic error */
HCL_ENOIMPL, /**< not implemented */
HCL_ESYSERR, /**< subsystem error */
HCL_EINTERN, /**< internal error */
HCL_ESYSMEM, /**< insufficient system memory */
HCL_EOOMEM, /**< insufficient object memory */
HCL_ETYPE, /**< invalid class/type */
HCL_EINVAL, /**< invalid parameter or data */
HCL_ENOENT, /**< data not found */
HCL_EEXIST, /**< existing/duplicate data */
HCL_EBUSY,
HCL_EACCES,
HCL_EPERM,
HCL_ENOTDIR,
HCL_EINTR,
HCL_EPIPE,
HCL_EAGAIN,
HCL_EBADHND,
HCL_EFRMFLOOD, /**< too many frames */
HCL_EMSGRCV, /**< mesasge receiver error */
HCL_EMSGSND, /**< message sending error. even doesNotUnderstand: is not found */
HCL_ENUMARGS, /**< wrong number of arguments */
HCL_ERANGE, /**< range error. overflow and underflow */
HCL_EBCFULL, /**< byte-code full */
HCL_EDFULL, /**< dictionary full */
HCL_EPFULL, /**< processor full */
HCL_EFINIS, /**< unexpected end of data/input/stream/etc */
HCL_EXXXXX2, /**< **** not used ***** */
HCL_EFLOOD, /**< too many items/data */
HCL_EDIVBY0, /**< divide by zero */
HCL_EIOERR, /**< I/O error */
HCL_EECERR, /**< encoding conversion error */
HCL_EBUFFULL, /**< buffer full */
HCL_ESYNERR, /**< syntax error */
HCL_ECALL, /**< runtime error - cannot call */
HCL_ERECALL, /**< runtime error - cannot call again */
HCL_ECALLARG /**< runtime error - wrong number of arguments to call */
};

View File

@ -413,6 +413,7 @@ int main (int argc, char* argv[])
{
{ ":log", 'l' },
{ "large-pages", '\0' },
{ ":worker-max-count", '\0' },
{ ":worker-stack-size", '\0' },
{ ":worker-idle-timeout",'\0' },
{ ":actor-heap-size", 'm' },
@ -435,6 +436,7 @@ int main (int argc, char* argv[])
const char* logopt = HCL_NULL;
const char* dbgopt = HCL_NULL;
hcl_oow_t worker_max_count = 0;
hcl_oow_t worker_stack_size = MIN_ACTOR_HEAP_SIZE;
hcl_ntime_t worker_idle_timeout = { 0, 0 };
hcl_oow_t actor_heap_size = MIN_ACTOR_HEAP_SIZE;
@ -468,7 +470,10 @@ int main (int argc, char* argv[])
if (hcl_compbcstr(opt.lngopt, "large-pages") == 0)
{
large_pages = 1;
break;
}
else if (hcl_compbcstr(opt.lngopt, "worker-max-count") == 0)
{
worker_max_count = strtoul(opt.arg, HCL_NULL, 0);
}
else if (hcl_compbcstr(opt.lngopt, "worker-stack-size") == 0)
{
@ -478,22 +483,19 @@ int main (int argc, char* argv[])
else if (hcl_compbcstr(opt.lngopt, "worker-idle-timeout") == 0)
{
worker_idle_timeout.sec = strtoul(opt.arg, HCL_NULL, 0);
break;
}
else if (hcl_compbcstr(opt.lngopt, "actor-max-runtime") == 0)
{
actor_max_runtime.sec = strtoul(opt.arg, HCL_NULL, 0);
break;
}
#if defined(HCL_BUILD_DEBUG)
else if (hcl_compbcstr(opt.lngopt, "debug") == 0)
{
dbgopt = opt.arg;
break;
}
#endif
goto print_usage;
else goto print_usage;
break;
case ':':
if (opt.lngopt)
@ -543,6 +545,7 @@ int main (int argc, char* argv[])
else trait &= ~HCL_SERVER_TRAIT_USE_LARGE_PAGES;
hcl_server_setoption (server, HCL_SERVER_TRAIT, &trait);
hcl_server_setoption (server, HCL_SERVER_WORKER_MAX_COUNT, &worker_max_count);
hcl_server_setoption (server, HCL_SERVER_WORKER_STACK_SIZE, &worker_stack_size);
hcl_server_setoption (server, HCL_SERVER_WORKER_IDLE_TIMEOUT, &worker_idle_timeout);
hcl_server_setoption (server, HCL_SERVER_ACTOR_HEAP_SIZE, &actor_heap_size);