odyssey/sources/worker.c

140 lines
3.3 KiB
C
Raw Normal View History

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>
#include <ctype.h>
2017-05-31 15:47:15 +00:00
#include <inttypes.h>
#include <assert.h>
2017-05-26 11:49:17 +00:00
#include <machinarium.h>
#include <kiwi.h>
#include <odyssey.h>
2017-05-26 11:49:17 +00:00
static inline void
od_worker(void *arg)
2017-05-26 11:49:17 +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
for (;;)
{
2017-06-13 11:57:54 +00:00
machine_msg_t *msg;
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_type(msg);
2017-05-26 11:49:17 +00:00
switch (msg_type) {
case OD_MSG_CLIENT_NEW:
2017-05-26 11:49:17 +00:00
{
od_client_t *client;
client = *(od_client_t**)machine_msg_data(msg);
2018-03-13 13:17:27 +00:00
client->global = worker->global;
2017-05-26 11:49:17 +00:00
int64_t coroutine_id;
coroutine_id = machine_coroutine_create(od_frontend, client);
2017-05-26 11:49:17 +00:00
if (coroutine_id == -1) {
od_error(&instance->logger, "worker", client, NULL,
"failed to create coroutine");
od_io_close(&client->io);
2017-05-26 11:49:17 +00:00
od_client_free(client);
break;
}
client->coroutine_id = coroutine_id;
worker->clients_processed++;
2017-05-26 11:49:17 +00:00
break;
}
case OD_MSG_STAT:
{
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), "
"coroutines (%" PRIu64 " active, %"PRIu64 " cached), clients_processed: %" PRIu64,
worker->id,
msg_allocated,
msg_cache_count,
msg_cache_gc_count,
msg_cache_size,
count_coroutine,
count_coroutine_cache,
worker->clients_processed);
break;
}
default:
assert(0);
break;
2017-05-26 11:49:17 +00:00
}
2017-05-26 11:49:17 +00:00
machine_msg_free(msg);
}
od_log(&instance->logger, "worker", NULL, NULL, "stopped");
2017-05-26 11:49:17 +00:00
}
void
od_worker_init(od_worker_t *worker, od_global_t *global, int id)
2017-05-26 11:49:17 +00:00
{
worker->machine = -1;
worker->id = id;
2018-03-13 13:17:27 +00:00
worker->global = global;
worker->clients_processed = 0;
2017-05-26 11:49:17 +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;
int is_shared;
is_shared = od_config_is_multi_workers(&instance->config);
worker->task_channel = machine_channel_create(is_shared);
if (worker->task_channel == NULL) {
od_error(&instance->logger, "worker", NULL, NULL,
"failed to create task channel");
return -1;
}
if (is_shared) {
char name[32];
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");
return -1;
}
} else {
int64_t coroutine_id;
coroutine_id = machine_coroutine_create(od_worker, worker);
if (coroutine_id == -1) {
od_error(&instance->logger, "worker", NULL, NULL,
"failed to create worker coroutine");
machine_channel_free(worker->task_channel);
return -1;
}
2017-05-26 11:49:17 +00:00
}
return 0;
}