2017-05-26 11:49:17 +00:00
|
|
|
|
|
|
|
/*
|
2017-07-05 12:42:49 +00:00
|
|
|
* Odissey.
|
2017-05-26 11:49:17 +00:00
|
|
|
*
|
2017-07-05 12:42:49 +00:00
|
|
|
* Advanced 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>
|
2017-05-31 15:47:15 +00:00
|
|
|
#include <inttypes.h>
|
2017-05-26 11:49:17 +00:00
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <machinarium.h>
|
2017-06-07 11:50:58 +00:00
|
|
|
#include <shapito.h>
|
2017-05-26 11:49:17 +00:00
|
|
|
|
2017-07-05 12:15:17 +00:00
|
|
|
#include "sources/macro.h"
|
|
|
|
#include "sources/version.h"
|
2017-08-08 13:50:50 +00:00
|
|
|
#include "sources/atomic.h"
|
2017-11-27 12:54:16 +00:00
|
|
|
#include "sources/util.h"
|
|
|
|
#include "sources/error.h"
|
2017-07-05 12:15:17 +00:00
|
|
|
#include "sources/list.h"
|
|
|
|
#include "sources/pid.h"
|
|
|
|
#include "sources/id.h"
|
2017-07-26 14:05:29 +00:00
|
|
|
#include "sources/logger.h"
|
2017-07-05 12:15:17 +00:00
|
|
|
#include "sources/daemon.h"
|
2018-03-06 15:23:52 +00:00
|
|
|
#include "sources/config.h"
|
|
|
|
#include "sources/config_mgr.h"
|
2018-03-05 14:24:30 +00:00
|
|
|
#include "sources/config_reader.h"
|
2017-07-05 12:15:17 +00:00
|
|
|
#include "sources/msg.h"
|
|
|
|
#include "sources/system.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"
|
2017-09-15 12:58:29 +00:00
|
|
|
#include "sources/instance.h"
|
2018-02-22 13:43:52 +00:00
|
|
|
#include "sources/router_cancel.h"
|
2017-07-05 12:15:17 +00:00
|
|
|
#include "sources/router.h"
|
|
|
|
#include "sources/pooler.h"
|
2018-03-02 10:00:52 +00:00
|
|
|
#include "sources/worker.h"
|
2017-07-05 12:15:17 +00:00
|
|
|
#include "sources/frontend.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;
|
|
|
|
od_instance_t *instance = worker->system->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) {
|
|
|
|
case OD_MCLIENT_NEW:
|
|
|
|
{
|
|
|
|
od_client_t *client;
|
|
|
|
client = *(od_client_t**)machine_msg_get_data(msg);
|
2018-03-02 10:00:52 +00:00
|
|
|
client->system = worker->system;
|
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;
|
|
|
|
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-03-02 10:00:52 +00:00
|
|
|
void od_worker_init(od_worker_t *worker, od_system_t *system, int id)
|
2017-05-26 11:49:17 +00:00
|
|
|
{
|
2018-03-02 10:00:52 +00:00
|
|
|
worker->machine = -1;
|
|
|
|
worker->id = id;
|
|
|
|
worker->system = system;
|
2017-05-26 11:49:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 10:00:52 +00:00
|
|
|
int od_worker_start(od_worker_t *worker)
|
2017-05-26 11:49:17 +00:00
|
|
|
{
|
2018-03-02 10:00:52 +00:00
|
|
|
od_instance_t *instance = worker->system->instance;
|
2017-06-19 10:30:56 +00:00
|
|
|
|
2018-03-02 10:00:52 +00:00
|
|
|
worker->task_channel = machine_channel_create(instance->is_shared);
|
|
|
|
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-02-02 13:44:47 +00:00
|
|
|
if (instance->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;
|
|
|
|
}
|