2017-05-31 11:38:06 +00:00
|
|
|
|
|
|
|
/*
|
2017-07-05 12:42:49 +00:00
|
|
|
* Odissey.
|
2017-05-31 11:38:06 +00:00
|
|
|
*
|
2017-07-05 12:42:49 +00:00
|
|
|
* Advanced 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>
|
2017-05-31 15:47:15 +00:00
|
|
|
#include <inttypes.h>
|
2017-05-31 11:38:06 +00:00
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <machinarium.h>
|
2017-06-07 11:50:58 +00:00
|
|
|
#include <shapito.h>
|
2017-05-31 11:38:06 +00:00
|
|
|
|
2017-07-05 12:15:17 +00:00
|
|
|
#include "sources/macro.h"
|
|
|
|
#include "sources/version.h"
|
|
|
|
#include "sources/list.h"
|
|
|
|
#include "sources/pid.h"
|
|
|
|
#include "sources/id.h"
|
2017-07-26 14:05:29 +00:00
|
|
|
#include "sources/log_file.h"
|
|
|
|
#include "sources/log_system.h"
|
|
|
|
#include "sources/logger.h"
|
2017-07-05 12:15:17 +00:00
|
|
|
#include "sources/daemon.h"
|
|
|
|
#include "sources/scheme.h"
|
2017-07-14 13:40:31 +00:00
|
|
|
#include "sources/scheme_mgr.h"
|
2017-07-05 12:15:17 +00:00
|
|
|
#include "sources/config.h"
|
|
|
|
#include "sources/msg.h"
|
|
|
|
#include "sources/system.h"
|
|
|
|
#include "sources/instance.h"
|
|
|
|
#include "sources/server.h"
|
|
|
|
#include "sources/server_pool.h"
|
|
|
|
#include "sources/client.h"
|
|
|
|
#include "sources/client_pool.h"
|
|
|
|
#include "sources/route_id.h"
|
|
|
|
#include "sources/route.h"
|
|
|
|
#include "sources/route_pool.h"
|
|
|
|
#include "sources/io.h"
|
|
|
|
#include "sources/router.h"
|
|
|
|
#include "sources/pooler.h"
|
|
|
|
#include "sources/relay.h"
|
|
|
|
#include "sources/frontend.h"
|
|
|
|
#include "sources/backend.h"
|
|
|
|
#include "sources/periodic.h"
|
2017-05-31 11:38:06 +00:00
|
|
|
|
|
|
|
static inline void
|
2017-07-10 13:10:48 +00:00
|
|
|
od_periodic_stats(od_router_t *router)
|
2017-05-31 11:38:06 +00:00
|
|
|
{
|
|
|
|
od_instance_t *instance = router->system->instance;
|
|
|
|
if (router->route_pool.count == 0)
|
|
|
|
return;
|
2017-07-26 14:05:29 +00:00
|
|
|
od_log(&instance->logger, "statistics");
|
2017-05-31 11:38:06 +00:00
|
|
|
od_list_t *i;
|
|
|
|
od_list_foreach(&router->route_pool.list, i) {
|
|
|
|
od_route_t *route;
|
|
|
|
route = od_container_of(i, od_route_t, link);
|
2017-07-26 14:05:29 +00:00
|
|
|
od_log(&instance->logger,
|
2017-07-24 12:06:18 +00:00
|
|
|
" [%.*s.%.*s.%d] %sclients %d, "
|
2017-05-31 15:47:15 +00:00
|
|
|
"pool_active %d, "
|
|
|
|
"pool_idle %d ",
|
2017-05-31 11:38:06 +00:00
|
|
|
route->id.database_len,
|
|
|
|
route->id.database,
|
|
|
|
route->id.user_len,
|
|
|
|
route->id.user,
|
2017-07-21 14:46:40 +00:00
|
|
|
route->scheme->version,
|
2017-07-24 12:06:18 +00:00
|
|
|
route->scheme->is_obsolete ? "(obsolete) " : "",
|
2017-07-17 13:45:51 +00:00
|
|
|
od_clientpool_total(&route->client_pool),
|
2017-05-31 11:38:06 +00:00
|
|
|
route->server_pool.count_active,
|
|
|
|
route->server_pool.count_idle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
2017-07-17 13:45:51 +00:00
|
|
|
od_periodic_expire_mark(od_server_t *server, void *arg)
|
2017-05-31 11:38:06 +00:00
|
|
|
{
|
|
|
|
od_router_t *router = arg;
|
|
|
|
od_instance_t *instance = router->system->instance;
|
|
|
|
od_route_t *route = server->route;
|
2017-07-18 10:30:23 +00:00
|
|
|
|
2017-07-24 12:06:18 +00:00
|
|
|
/* expire by server scheme obsoletion */
|
2017-07-21 14:29:01 +00:00
|
|
|
if (route->scheme->is_obsolete &&
|
2017-07-18 10:30:23 +00:00
|
|
|
od_clientpool_total(&route->client_pool) == 0) {
|
2017-07-26 14:05:29 +00:00
|
|
|
od_debug_server(&instance->logger, &server->id, "expire",
|
2017-07-24 12:06:18 +00:00
|
|
|
"scheme marked as obsolete, schedule closing");
|
2017-07-18 10:30:23 +00:00
|
|
|
od_serverpool_set(&route->server_pool, server,
|
|
|
|
OD_SEXPIRE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* expire by time-to-live */
|
2017-06-21 13:08:33 +00:00
|
|
|
if (! route->scheme->pool_ttl)
|
2017-05-31 11:38:06 +00:00
|
|
|
return 0;
|
2017-07-18 10:30:23 +00:00
|
|
|
|
2017-07-26 14:05:29 +00:00
|
|
|
od_debug_server(&instance->logger, &server->id, "expire",
|
2017-05-31 15:47:15 +00:00
|
|
|
"idle time: %d",
|
|
|
|
server->idle_time);
|
2017-06-21 13:08:33 +00:00
|
|
|
if (server->idle_time < route->scheme->pool_ttl) {
|
2017-05-31 11:38:06 +00:00
|
|
|
server->idle_time++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
od_serverpool_set(&route->server_pool, server,
|
|
|
|
OD_SEXPIRE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-17 13:45:51 +00:00
|
|
|
static inline void
|
|
|
|
od_periodic_expire(od_periodic_t *periodic)
|
|
|
|
{
|
|
|
|
od_router_t *router = periodic->system->router;
|
|
|
|
od_instance_t *instance = periodic->system->instance;
|
|
|
|
|
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.
|
|
|
|
*
|
2017-07-24 12:06:18 +00:00
|
|
|
* - If a server scheme 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 */
|
2017-07-17 13:45:51 +00:00
|
|
|
od_routepool_foreach(&router->route_pool, OD_SIDLE,
|
|
|
|
od_periodic_expire_mark,
|
|
|
|
router);
|
|
|
|
|
2017-07-18 10:30:23 +00:00
|
|
|
/* sweep */
|
2017-07-17 13:45:51 +00:00
|
|
|
for (;;) {
|
|
|
|
od_server_t *server;
|
|
|
|
server = od_routepool_next(&router->route_pool, OD_SEXPIRE);
|
|
|
|
if (server == NULL)
|
|
|
|
break;
|
2017-07-26 14:05:29 +00:00
|
|
|
od_debug_server(&instance->logger, &server->id, "expire",
|
2017-07-17 13:45:51 +00:00
|
|
|
"closing idle connection (%d secs)",
|
|
|
|
server->idle_time);
|
|
|
|
server->idle_time = 0;
|
|
|
|
|
|
|
|
od_route_t *route = server->route;
|
|
|
|
server->route = NULL;
|
|
|
|
od_serverpool_set(&route->server_pool, server, OD_SUNDEF);
|
|
|
|
|
|
|
|
machine_io_attach(server->io);
|
|
|
|
|
|
|
|
od_backend_terminate(server);
|
|
|
|
od_backend_close(server);
|
|
|
|
}
|
2017-07-19 15:25:44 +00:00
|
|
|
|
|
|
|
/* cleanup unused dynamic routes and obsolete
|
|
|
|
* db schemes */
|
|
|
|
od_routepool_gc(&router->route_pool);
|
2017-07-17 13:45:51 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 14:05:39 +00:00
|
|
|
static void
|
|
|
|
od_periodic(void *arg)
|
2017-05-31 11:38:06 +00:00
|
|
|
{
|
2017-06-05 14:05:39 +00:00
|
|
|
od_periodic_t *periodic = arg;
|
|
|
|
od_router_t *router = periodic->system->router;
|
|
|
|
od_instance_t *instance = periodic->system->instance;
|
2017-05-31 11:38:06 +00:00
|
|
|
|
|
|
|
int tick = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
2017-07-17 13:45:51 +00:00
|
|
|
/* mark and sweep expired idle server connections */
|
|
|
|
od_periodic_expire(periodic);
|
2017-05-31 11:38:06 +00:00
|
|
|
|
|
|
|
/* stats */
|
2017-06-23 10:34:51 +00:00
|
|
|
if (instance->scheme.log_statistics > 0) {
|
2017-05-31 11:38:06 +00:00
|
|
|
tick++;
|
2017-06-23 10:34:51 +00:00
|
|
|
if (tick >= instance->scheme.log_statistics) {
|
2017-05-31 11:38:06 +00:00
|
|
|
od_periodic_stats(router);
|
|
|
|
tick = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 1 second soft interval */
|
|
|
|
machine_sleep(1000);
|
|
|
|
}
|
|
|
|
}
|
2017-06-05 14:05:39 +00:00
|
|
|
|
|
|
|
int od_periodic_init(od_periodic_t *periodic, od_system_t *system)
|
|
|
|
{
|
|
|
|
periodic->system = system;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int od_periodic_start(od_periodic_t *periodic)
|
|
|
|
{
|
|
|
|
od_instance_t *instance = periodic->system->instance;
|
|
|
|
int64_t coroutine_id;
|
|
|
|
coroutine_id = machine_coroutine_create(od_periodic, periodic);
|
|
|
|
if (coroutine_id == -1) {
|
2017-07-26 14:05:29 +00:00
|
|
|
od_error(&instance->logger, "periodic", "failed to start periodic coroutine");
|
2017-06-19 10:30:56 +00:00
|
|
|
return -1;
|
2017-06-05 14:05:39 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|