2017-05-31 11:38:06 +00:00
|
|
|
|
|
|
|
/*
|
2018-03-12 14:03:15 +00:00
|
|
|
* Odyssey.
|
2017-05-31 11:38:06 +00:00
|
|
|
*
|
2018-04-04 13:19:58 +00:00
|
|
|
* Scalable PostgreSQL connection pooler.
|
2017-05-31 11:38:06 +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-31 11:38:06 +00:00
|
|
|
|
|
|
|
#include <machinarium.h>
|
2018-08-28 14:43:46 +00:00
|
|
|
#include <kiwi.h>
|
|
|
|
#include <odyssey.h>
|
2017-05-31 11:38:06 +00:00
|
|
|
|
2018-08-02 16:02:02 +00:00
|
|
|
static int
|
|
|
|
od_cron_stat_cb(od_route_t *route, od_stat_t *current, od_stat_t *avg,
|
|
|
|
void *arg)
|
2017-08-11 10:24:41 +00:00
|
|
|
{
|
2018-08-02 16:02:02 +00:00
|
|
|
od_router_t *router = arg;
|
|
|
|
od_instance_t *instance = router->global->instance;
|
|
|
|
|
|
|
|
/* update route stats */
|
2018-08-09 14:39:36 +00:00
|
|
|
route->stats_prev = *current;
|
2018-08-02 16:02:02 +00:00
|
|
|
|
|
|
|
if (! instance->config.log_stats)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
od_log(&instance->logger, "stats", NULL, NULL,
|
|
|
|
"[%.*s.%.*s] %d clients, "
|
|
|
|
"%d active servers, "
|
|
|
|
"%d idle servers, "
|
|
|
|
"%" PRIu64 " transactions/sec (%" PRIu64 " usec) "
|
|
|
|
"%" PRIu64 " queries/sec (%" PRIu64 " usec) "
|
|
|
|
"%" PRIu64 " in bytes/sec, "
|
|
|
|
"%" PRIu64 " out bytes/sec",
|
|
|
|
route->id.database_len - 1,
|
|
|
|
route->id.database,
|
|
|
|
route->id.user_len - 1,
|
|
|
|
route->id.user,
|
2018-08-28 14:43:46 +00:00
|
|
|
od_client_pool_total(&route->client_pool),
|
2018-08-02 16:02:02 +00:00
|
|
|
route->server_pool.count_active,
|
|
|
|
route->server_pool.count_idle,
|
|
|
|
avg->count_tx,
|
|
|
|
avg->tx_time,
|
|
|
|
avg->count_query,
|
|
|
|
avg->query_time,
|
|
|
|
avg->recv_client,
|
|
|
|
avg->recv_server);
|
|
|
|
|
2017-08-11 10:24:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-31 11:38:06 +00:00
|
|
|
static inline void
|
2018-08-28 14:43:46 +00:00
|
|
|
od_cron_stat(od_cron_t *cron, od_router_t *router)
|
2017-05-31 11:38:06 +00:00
|
|
|
{
|
2018-03-13 13:17:27 +00:00
|
|
|
od_instance_t *instance = router->global->instance;
|
2018-01-24 14:28:48 +00:00
|
|
|
|
2018-03-06 15:23:52 +00:00
|
|
|
if (instance->config.log_stats)
|
2018-02-12 13:29:40 +00:00
|
|
|
{
|
2018-09-05 13:04:36 +00:00
|
|
|
uint64_t count_machine = 0;
|
|
|
|
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;
|
2018-01-09 14:51:58 +00:00
|
|
|
machinarium_stat(&count_machine, &count_coroutine,
|
2018-08-30 14:45:13 +00:00
|
|
|
&count_coroutine_cache,
|
|
|
|
&msg_allocated,
|
|
|
|
&msg_cache_count,
|
2018-09-05 13:04:36 +00:00
|
|
|
&msg_cache_gc_count,
|
2018-08-30 14:45:13 +00:00
|
|
|
&msg_cache_size);
|
2018-02-12 13:29:40 +00:00
|
|
|
od_log(&instance->logger, "stats", NULL, NULL,
|
2018-09-10 14:15:31 +00:00
|
|
|
"msg (%" PRIu64 " allocated, %" PRIu64 " cached, %" PRIu64 " gc, %" PRIu64 " cache_size), "
|
2018-09-05 13:04:36 +00:00
|
|
|
"coroutines (%" PRIu64 " active, %"PRIu64 " cached)",
|
2018-08-30 14:45:13 +00:00
|
|
|
msg_allocated,
|
|
|
|
msg_cache_count,
|
2018-09-10 14:15:31 +00:00
|
|
|
msg_cache_gc_count,
|
2018-08-30 14:45:13 +00:00
|
|
|
msg_cache_size,
|
2018-02-19 13:18:55 +00:00
|
|
|
count_coroutine,
|
|
|
|
count_coroutine_cache);
|
2018-08-30 14:45:13 +00:00
|
|
|
|
|
|
|
od_log(&instance->logger, "stats", NULL, NULL,
|
|
|
|
"clients %d", router->clients);
|
2018-01-09 14:51:58 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 14:45:13 +00:00
|
|
|
if (router->route_pool.count == 0)
|
|
|
|
return;
|
|
|
|
|
2018-08-02 16:02:02 +00:00
|
|
|
/* update stats per route */
|
2018-08-28 14:43:46 +00:00
|
|
|
od_route_pool_stat(&router->route_pool, od_cron_stat_cb,
|
|
|
|
cron->stat_time_us,
|
|
|
|
router);
|
2017-08-11 10:24:41 +00:00
|
|
|
|
2018-08-02 16:02:02 +00:00
|
|
|
/* update current stat time mark */
|
|
|
|
cron->stat_time_us = machine_time();
|
2017-05-31 11:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
2018-03-02 13:12:32 +00:00
|
|
|
od_cron_expire_mark(od_server_t *server, void *arg)
|
2017-05-31 11:38:06 +00:00
|
|
|
{
|
|
|
|
od_router_t *router = arg;
|
2018-03-13 13:17:27 +00:00
|
|
|
od_instance_t *instance = router->global->instance;
|
2017-05-31 11:38:06 +00:00
|
|
|
od_route_t *route = server->route;
|
2017-07-18 10:30:23 +00:00
|
|
|
|
|
|
|
/* expire by time-to-live */
|
2018-03-06 15:23:52 +00:00
|
|
|
if (! route->config->pool_ttl)
|
2017-05-31 11:38:06 +00:00
|
|
|
return 0;
|
2017-07-18 10:30:23 +00:00
|
|
|
|
2017-09-21 13:44:19 +00:00
|
|
|
od_debug(&instance->logger, "expire", NULL, server,
|
|
|
|
"idle time: %d",
|
|
|
|
server->idle_time);
|
2018-03-21 14:36:57 +00:00
|
|
|
|
2018-03-06 15:23:52 +00:00
|
|
|
if (server->idle_time < route->config->pool_ttl) {
|
2017-05-31 11:38:06 +00:00
|
|
|
server->idle_time++;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-08-28 14:43:46 +00:00
|
|
|
od_server_pool_set(&route->server_pool, server,
|
|
|
|
OD_SERVER_EXPIRE);
|
2017-05-31 11:38:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-17 13:45:51 +00:00
|
|
|
static inline void
|
2018-03-02 13:12:32 +00:00
|
|
|
od_cron_expire(od_cron_t *cron)
|
2017-07-17 13:45:51 +00:00
|
|
|
{
|
2018-03-13 13:17:27 +00:00
|
|
|
od_router_t *router = cron->global->router;
|
|
|
|
od_instance_t *instance = cron->global->instance;
|
2017-07-17 13:45:51 +00:00
|
|
|
|
2017-07-18 10:30:23 +00:00
|
|
|
/* Idle servers expire.
|
|
|
|
*
|
|
|
|
* It is important that mark logic stage must not yield
|
|
|
|
* to maintain iterator consistency.
|
|
|
|
*
|
|
|
|
* mark:
|
2017-07-17 13:45:51 +00:00
|
|
|
*
|
2017-07-18 10:30:23 +00:00
|
|
|
* - If a server idle time is equal to ttl, then move
|
2017-07-17 13:45:51 +00:00
|
|
|
* it to the EXPIRE queue.
|
|
|
|
*
|
2018-03-06 15:23:52 +00:00
|
|
|
* - If a server config marked as obsolete and route has
|
2017-07-19 15:25:44 +00:00
|
|
|
* no remaining clients, then move it to the EXPIRE queue.
|
2017-07-18 10:30:23 +00:00
|
|
|
*
|
|
|
|
* - Add plus one idle second on each traversal.
|
|
|
|
*
|
|
|
|
* sweep:
|
2017-07-17 13:45:51 +00:00
|
|
|
*
|
2017-07-18 10:30:23 +00:00
|
|
|
* - Foreach servers in EXPIRE queue, send Terminate
|
2017-07-17 13:45:51 +00:00
|
|
|
* and close the connection.
|
|
|
|
*/
|
|
|
|
|
2017-07-18 10:30:23 +00:00
|
|
|
/* mark */
|
2018-08-28 14:43:46 +00:00
|
|
|
od_route_pool_server_foreach(&router->route_pool, OD_SERVER_IDLE,
|
|
|
|
od_cron_expire_mark,
|
|
|
|
router);
|
2017-07-17 13:45:51 +00:00
|
|
|
|
2017-07-18 10:30:23 +00:00
|
|
|
/* sweep */
|
2018-08-28 14:43:46 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
2017-07-17 13:45:51 +00:00
|
|
|
od_server_t *server;
|
2018-08-28 14:43:46 +00:00
|
|
|
server = od_route_pool_next(&router->route_pool, OD_SERVER_EXPIRE);
|
2017-07-17 13:45:51 +00:00
|
|
|
if (server == NULL)
|
|
|
|
break;
|
2017-09-21 13:44:19 +00:00
|
|
|
od_debug(&instance->logger, "expire", NULL, server,
|
|
|
|
"closing idle server connection (%d secs)",
|
|
|
|
server->idle_time);
|
2017-07-17 13:45:51 +00:00
|
|
|
server->idle_time = 0;
|
|
|
|
|
|
|
|
od_route_t *route = server->route;
|
|
|
|
server->route = NULL;
|
2018-08-28 14:43:46 +00:00
|
|
|
od_server_pool_set(&route->server_pool, server, OD_SERVER_UNDEF);
|
2017-07-17 13:45:51 +00:00
|
|
|
|
2018-02-02 13:53:24 +00:00
|
|
|
if (instance->is_shared)
|
|
|
|
machine_io_attach(server->io);
|
2017-07-17 13:45:51 +00:00
|
|
|
|
2018-02-15 13:06:46 +00:00
|
|
|
od_backend_close_connection(server);
|
2017-07-17 13:45:51 +00:00
|
|
|
od_backend_close(server);
|
|
|
|
}
|
2017-07-19 15:25:44 +00:00
|
|
|
|
2018-03-21 14:36:57 +00:00
|
|
|
/* cleanup unused dynamic routes */
|
2018-08-28 14:43:46 +00:00
|
|
|
od_route_pool_gc(&router->route_pool);
|
2017-07-17 13:45:51 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 14:05:39 +00:00
|
|
|
static void
|
2018-03-02 13:12:32 +00:00
|
|
|
od_cron(void *arg)
|
2017-05-31 11:38:06 +00:00
|
|
|
{
|
2018-03-02 13:12:32 +00:00
|
|
|
od_cron_t *cron = arg;
|
2018-03-13 13:17:27 +00:00
|
|
|
od_router_t *router = cron->global->router;
|
|
|
|
od_instance_t *instance = cron->global->instance;
|
2017-05-31 11:38:06 +00:00
|
|
|
|
2018-08-02 16:02:02 +00:00
|
|
|
cron->stat_time_us = machine_time();
|
|
|
|
|
2017-08-11 14:11:44 +00:00
|
|
|
int stats_tick = 0;
|
2017-05-31 11:38:06 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
2017-07-17 13:45:51 +00:00
|
|
|
/* mark and sweep expired idle server connections */
|
2018-03-02 13:12:32 +00:00
|
|
|
od_cron_expire(cron);
|
2017-05-31 11:38:06 +00:00
|
|
|
|
2018-08-02 16:02:02 +00:00
|
|
|
/* update statistics */
|
2018-03-06 15:23:52 +00:00
|
|
|
if (++stats_tick >= instance->config.stats_interval) {
|
2018-08-28 14:43:46 +00:00
|
|
|
od_cron_stat(cron, router);
|
2017-08-11 14:11:44 +00:00
|
|
|
stats_tick = 0;
|
2017-05-31 11:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 1 second soft interval */
|
|
|
|
machine_sleep(1000);
|
|
|
|
}
|
|
|
|
}
|
2017-06-05 14:05:39 +00:00
|
|
|
|
2018-08-28 14:43:46 +00:00
|
|
|
void
|
|
|
|
od_cron_init(od_cron_t *cron, od_global_t *global)
|
2017-06-05 14:05:39 +00:00
|
|
|
{
|
2018-03-13 13:17:27 +00:00
|
|
|
cron->global = global;
|
2018-08-02 16:02:02 +00:00
|
|
|
cron->stat_time_us = 0;
|
2017-06-05 14:05:39 +00:00
|
|
|
}
|
|
|
|
|
2018-08-28 14:43:46 +00:00
|
|
|
int
|
|
|
|
od_cron_start(od_cron_t *cron)
|
2017-06-05 14:05:39 +00:00
|
|
|
{
|
2018-03-13 13:17:27 +00:00
|
|
|
od_instance_t *instance = cron->global->instance;
|
2017-06-05 14:05:39 +00:00
|
|
|
int64_t coroutine_id;
|
2018-03-02 13:12:32 +00:00
|
|
|
coroutine_id = machine_coroutine_create(od_cron, cron);
|
2017-06-05 14:05:39 +00:00
|
|
|
if (coroutine_id == -1) {
|
2018-03-02 13:12:32 +00:00
|
|
|
od_error(&instance->logger, "cron", NULL, NULL,
|
|
|
|
"failed to start cron coroutine");
|
2017-06-19 10:30:56 +00:00
|
|
|
return -1;
|
2017-06-05 14:05:39 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|