2017-05-26 11:49:17 +00:00
|
|
|
|
|
|
|
/*
|
2018-03-12 14:03:15 +00:00
|
|
|
* Odyssey.
|
2017-05-26 11:49:17 +00:00
|
|
|
*
|
2018-04-04 13:19:58 +00:00
|
|
|
* Scalable PostgreSQL connection pooler.
|
2017-05-26 11:49:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2018-08-28 14:43:46 +00:00
|
|
|
#include <ctype.h>
|
2017-05-31 15:47:15 +00:00
|
|
|
#include <inttypes.h>
|
2018-08-28 14:43:46 +00:00
|
|
|
#include <assert.h>
|
2017-05-26 11:49:17 +00:00
|
|
|
|
|
|
|
#include <machinarium.h>
|
2018-08-28 14:43:46 +00:00
|
|
|
#include <kiwi.h>
|
|
|
|
#include <odyssey.h>
|
2017-05-26 11:49:17 +00:00
|
|
|
|
|
|
|
static inline void
|
2018-03-02 10:00:52 +00:00
|
|
|
od_worker(void *arg)
|
2017-05-26 11:49:17 +00:00
|
|
|
{
|
2018-03-02 10:00:52 +00:00
|
|
|
od_worker_t *worker = arg;
|
2018-03-13 13:17:27 +00:00
|
|
|
od_instance_t *instance = worker->global->instance;
|
2017-05-26 11:49:17 +00:00
|
|
|
|
2017-05-26 13:44:42 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
2017-06-13 11:57:54 +00:00
|
|
|
machine_msg_t *msg;
|
2018-03-02 10:00:52 +00:00
|
|
|
msg = machine_channel_read(worker->task_channel, UINT32_MAX);
|
2017-05-26 11:49:17 +00:00
|
|
|
if (msg == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
od_msg_t msg_type;
|
|
|
|
msg_type = machine_msg_get_type(msg);
|
|
|
|
switch (msg_type) {
|
2018-12-06 14:23:15 +00:00
|
|
|
case OD_MSG_CLIENT_NEW:
|
2017-05-26 11:49:17 +00:00
|
|
|
{
|
|
|
|
od_client_t *client;
|
|
|
|
client = *(od_client_t**)machine_msg_get_data(msg);
|
2018-03-13 13:17:27 +00:00
|
|
|
client->global = worker->global;
|
2018-08-28 14:43:46 +00:00
|
|
|
|
2017-05-26 11:49:17 +00:00
|
|
|
int64_t coroutine_id;
|
2017-05-26 12:17:45 +00:00
|
|
|
coroutine_id = machine_coroutine_create(od_frontend, client);
|
2017-05-26 11:49:17 +00:00
|
|
|
if (coroutine_id == -1) {
|
2018-03-02 10:00:52 +00:00
|
|
|
od_error(&instance->logger, "worker", client, NULL,
|
2017-09-21 13:44:19 +00:00
|
|
|
"failed to create coroutine");
|
2017-05-26 11:49:17 +00:00
|
|
|
machine_close(client->io);
|
|
|
|
od_client_free(client);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
client->coroutine_id = coroutine_id;
|
2018-11-19 15:32:05 +00:00
|
|
|
|
|
|
|
worker->clients_processed++;
|
2017-05-26 11:49:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-12-06 14:23:15 +00:00
|
|
|
case OD_MSG_STAT:
|
2018-11-19 14:52:37 +00:00
|
|
|
{
|
|
|
|
uint64_t count_coroutine = 0;
|
|
|
|
uint64_t count_coroutine_cache = 0;
|
|
|
|
uint64_t msg_allocated = 0;
|
|
|
|
uint64_t msg_cache_count = 0;
|
|
|
|
uint64_t msg_cache_gc_count = 0;
|
|
|
|
uint64_t msg_cache_size = 0;
|
|
|
|
machine_stat(&count_coroutine,
|
|
|
|
&count_coroutine_cache,
|
|
|
|
&msg_allocated,
|
|
|
|
&msg_cache_count,
|
|
|
|
&msg_cache_gc_count,
|
|
|
|
&msg_cache_size);
|
|
|
|
od_log(&instance->logger, "stats", NULL, NULL,
|
|
|
|
"worker[%d]: msg (%" PRIu64 " allocated, %" PRIu64 " cached, %" PRIu64 " freed, %" PRIu64 " cache_size), "
|
2018-11-19 15:32:05 +00:00
|
|
|
"coroutines (%" PRIu64 " active, %"PRIu64 " cached), clients_processed: %" PRIu64,
|
2018-11-19 14:52:37 +00:00
|
|
|
worker->id,
|
|
|
|
msg_allocated,
|
|
|
|
msg_cache_count,
|
|
|
|
msg_cache_gc_count,
|
|
|
|
msg_cache_size,
|
|
|
|
count_coroutine,
|
2018-11-19 15:32:05 +00:00
|
|
|
count_coroutine_cache,
|
|
|
|
worker->clients_processed);
|
2018-11-19 14:52:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-05-26 13:44:42 +00:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
break;
|
2017-05-26 11:49:17 +00:00
|
|
|
}
|
2017-05-26 12:17:45 +00:00
|
|
|
|
2017-05-26 11:49:17 +00:00
|
|
|
machine_msg_free(msg);
|
|
|
|
}
|
|
|
|
|
2018-03-02 10:00:52 +00:00
|
|
|
od_log(&instance->logger, "worker", NULL, NULL, "stopped");
|
2017-05-26 11:49:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-28 14:43:46 +00:00
|
|
|
void
|
|
|
|
od_worker_init(od_worker_t *worker, od_global_t *global, int id)
|
2017-05-26 11:49:17 +00:00
|
|
|
{
|
2018-03-02 10:00:52 +00:00
|
|
|
worker->machine = -1;
|
|
|
|
worker->id = id;
|
2018-03-13 13:17:27 +00:00
|
|
|
worker->global = global;
|
2018-11-19 15:32:05 +00:00
|
|
|
worker->clients_processed = 0;
|
2017-05-26 11:49:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-28 14:43:46 +00:00
|
|
|
int
|
|
|
|
od_worker_start(od_worker_t *worker)
|
2017-05-26 11:49:17 +00:00
|
|
|
{
|
2018-03-13 13:17:27 +00:00
|
|
|
od_instance_t *instance = worker->global->instance;
|
2017-06-19 10:30:56 +00:00
|
|
|
|
2018-12-06 14:23:15 +00:00
|
|
|
int is_shared;
|
|
|
|
is_shared = od_config_is_multi_workers(&instance->config);
|
|
|
|
|
|
|
|
worker->task_channel = machine_channel_create(is_shared);
|
2018-03-02 10:00:52 +00:00
|
|
|
if (worker->task_channel == NULL) {
|
|
|
|
od_error(&instance->logger, "worker", NULL, NULL,
|
2018-02-02 11:49:10 +00:00
|
|
|
"failed to create task channel");
|
2017-06-19 10:30:56 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-12-06 14:23:15 +00:00
|
|
|
if (is_shared) {
|
2018-02-02 13:09:26 +00:00
|
|
|
char name[32];
|
2018-03-02 10:00:52 +00:00
|
|
|
od_snprintf(name, sizeof(name), "worker: %d", worker->id);
|
|
|
|
worker->machine = machine_create(name, od_worker, worker);
|
|
|
|
if (worker->machine == -1) {
|
|
|
|
machine_channel_free(worker->task_channel);
|
|
|
|
od_error(&instance->logger, "worker", NULL, NULL,
|
|
|
|
"failed to start worker");
|
2018-02-02 13:09:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int64_t coroutine_id;
|
2018-03-02 10:00:52 +00:00
|
|
|
coroutine_id = machine_coroutine_create(od_worker, worker);
|
2018-02-02 13:09:26 +00:00
|
|
|
if (coroutine_id == -1) {
|
2018-03-02 10:00:52 +00:00
|
|
|
od_error(&instance->logger, "worker", NULL, NULL,
|
|
|
|
"failed to create worker coroutine");
|
|
|
|
machine_channel_free(worker->task_channel);
|
2018-02-02 13:09:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2017-05-26 11:49:17 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|