odyssey/sources/worker.c

136 lines
3.4 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 <kiwi.h>
2017-05-26 11:49:17 +00:00
#include <machinarium.h>
#include <odyssey.h>
Enabled writing stats in Prometheus format. (#346) * Early implementation * Added description for gauges * Added prometheus lib finding to Cmake * Fixed FindProm comments * Small changes * free to prom_free * Moved metrics to separate file * Refactored & added new methods to prom_metrics * Refactored cron.c to use prom_metrics.h * Refactored CMake & odyssey.h * Added new metrics * Small refactoring * Small method renaming * Small fix in init * Added methods for use in od_cron_stats_cb * Small fixes * Refactored metrics to use separate collectors * Small fix * Passing metrics to od_cron_stat_cb now. * Removed unused imports * Removed old od_log calls * Removed TODO * Revert "Removed old od_log calls" This reverts commit 60000c8321e313e39bf5c63cf4715adc51401b89. * Uncommented od_log calls * Added processed clients field to metrics * Refactored metrics init * Fixed write_stat * Added method for worker stats * Added method for worker stat to metrics header file * Refactored worker stat method * Reverted changes in odyssey.h * Added writing metrics to od_worker * Added new method to logger Method provides writing big strings to log without formatting. * Fixed prometheus log writing * More fixes * Fixed log writing * Fixed log calls * Added TODO * Added assertion whether prom.h found * Fixed no format logger method Now logs follow log format * Fixed logger * Added log_stats_prom option to config * Renamed od_logger_write_no_fmt to od_logger_write_plain * Formatted * More ifdefs * Added memory deallocating in od_cron_stop * Updated configuration.md * Removed outdated TODO * Changed label * Formatted
2021-08-17 12:07:16 +00:00
#ifdef PROM_FOUND
#include <prom_metric.h>
#endif
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;
od_router_t *router = worker->global->router;
2017-05-26 11:49:17 +00:00
/* thread global initializtion */
od_thread_global **gl = od_thread_global_get();
od_retcode_t rc = od_thread_global_init(gl);
if (rc != OK_RESPONSE) {
// TODO: set errno
od_fatal(&instance->logger, "worker_init", NULL, NULL,
"failed to init worker thread info");
return;
}
(*gl)->wid = worker->id;
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: {
od_client_t *client;
client = *(od_client_t **)machine_msg_data(msg);
client->global = worker->global;
int64_t coroutine_id;
coroutine_id =
machine_coroutine_create(od_frontend, client);
if (coroutine_id == -1) {
od_error(&instance->logger, "worker", client,
NULL, "failed to create coroutine");
od_io_close(&client->io);
od_client_free(client);
od_atomic_u32_dec(&router->clients_routing);
break;
}
client->coroutine_id = coroutine_id;
worker->clients_processed++;
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);
Enabled writing stats in Prometheus format. (#346) * Early implementation * Added description for gauges * Added prometheus lib finding to Cmake * Fixed FindProm comments * Small changes * free to prom_free * Moved metrics to separate file * Refactored & added new methods to prom_metrics * Refactored cron.c to use prom_metrics.h * Refactored CMake & odyssey.h * Added new metrics * Small refactoring * Small method renaming * Small fix in init * Added methods for use in od_cron_stats_cb * Small fixes * Refactored metrics to use separate collectors * Small fix * Passing metrics to od_cron_stat_cb now. * Removed unused imports * Removed old od_log calls * Removed TODO * Revert "Removed old od_log calls" This reverts commit 60000c8321e313e39bf5c63cf4715adc51401b89. * Uncommented od_log calls * Added processed clients field to metrics * Refactored metrics init * Fixed write_stat * Added method for worker stats * Added method for worker stat to metrics header file * Refactored worker stat method * Reverted changes in odyssey.h * Added writing metrics to od_worker * Added new method to logger Method provides writing big strings to log without formatting. * Fixed prometheus log writing * More fixes * Fixed log writing * Fixed log calls * Added TODO * Added assertion whether prom.h found * Fixed no format logger method Now logs follow log format * Fixed logger * Added log_stats_prom option to config * Renamed od_logger_write_no_fmt to od_logger_write_plain * Formatted * More ifdefs * Added memory deallocating in od_cron_stop * Updated configuration.md * Removed outdated TODO * Changed label * Formatted
2021-08-17 12:07:16 +00:00
#ifdef PROM_FOUND
od_prom_metrics_write_worker_stat(
((od_cron_t *)(worker->global->cron))->metrics,
worker->id, msg_allocated, msg_cache_count,
msg_cache_gc_count, msg_cache_size,
count_coroutine, count_coroutine_cache,
worker->clients_processed);
#endif
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;
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;
worker->task_channel = machine_channel_create();
if (worker->task_channel == NULL) {
od_error(&instance->logger, "worker", NULL, NULL,
"failed to create task channel");
return -1;
}
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;
2017-05-26 11:49:17 +00:00
}
2017-05-26 11:49:17 +00:00
return 0;
}