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"
|
|
|
|
#include "sources/list.h"
|
|
|
|
#include "sources/pid.h"
|
|
|
|
#include "sources/id.h"
|
|
|
|
#include "sources/syslog.h"
|
|
|
|
#include "sources/log.h"
|
|
|
|
#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/tls.h"
|
|
|
|
#include "sources/auth.h"
|
|
|
|
#include "sources/console.h"
|
2017-05-26 11:49:17 +00:00
|
|
|
|
|
|
|
void od_frontend_close(od_client_t *client)
|
|
|
|
{
|
2017-05-31 10:49:12 +00:00
|
|
|
assert(client->route == NULL);
|
|
|
|
assert(client->server == NULL);
|
2017-05-26 11:49:17 +00:00
|
|
|
if (client->io) {
|
|
|
|
machine_close(client->io);
|
|
|
|
machine_io_free(client->io);
|
|
|
|
client->io = NULL;
|
|
|
|
}
|
|
|
|
od_client_free(client);
|
|
|
|
}
|
|
|
|
|
2017-06-08 11:30:00 +00:00
|
|
|
int od_frontend_error(od_client_t *client, char *code, char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
char msg[512];
|
|
|
|
int msg_len;
|
2017-06-28 11:47:42 +00:00
|
|
|
msg_len = snprintf(msg, sizeof(msg), "odissey: c%.*s: ",
|
2017-07-06 13:56:17 +00:00
|
|
|
(signed)sizeof(client->id.id),
|
2017-06-28 11:47:42 +00:00
|
|
|
client->id.id);
|
2017-06-09 13:21:40 +00:00
|
|
|
msg_len += vsnprintf(msg + msg_len, sizeof(msg) - msg_len, fmt, args);
|
2017-06-08 11:30:00 +00:00
|
|
|
va_end(args);
|
2017-07-06 13:36:14 +00:00
|
|
|
shapito_stream_t *stream = &client->stream;
|
|
|
|
shapito_stream_reset(stream);
|
2017-06-08 11:30:00 +00:00
|
|
|
int rc;
|
2017-07-06 13:36:14 +00:00
|
|
|
rc = shapito_be_write_error(stream, code, msg, msg_len);
|
2017-06-08 11:30:00 +00:00
|
|
|
if (rc == -1)
|
|
|
|
return -1;
|
|
|
|
rc = od_write(client->io, stream);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2017-05-26 11:49:17 +00:00
|
|
|
static int
|
|
|
|
od_frontend_startup_read(od_client_t *client)
|
|
|
|
{
|
2017-05-27 14:21:39 +00:00
|
|
|
od_instance_t *instance = client->system->instance;
|
2017-05-26 11:49:17 +00:00
|
|
|
|
2017-07-06 13:36:14 +00:00
|
|
|
shapito_stream_t *stream = &client->stream;
|
|
|
|
shapito_stream_reset(stream);
|
2017-05-26 11:49:17 +00:00
|
|
|
for (;;) {
|
2017-07-06 13:36:14 +00:00
|
|
|
uint32_t pos_size = shapito_stream_used(stream);
|
2017-07-04 13:27:42 +00:00
|
|
|
char *pos_data = stream->start;
|
2017-05-26 11:49:17 +00:00
|
|
|
uint32_t len;
|
|
|
|
int to_read;
|
2017-07-06 13:36:14 +00:00
|
|
|
to_read = shapito_read_startup(&len, &pos_data, &pos_size);
|
2017-05-26 11:49:17 +00:00
|
|
|
if (to_read == 0)
|
|
|
|
break;
|
|
|
|
if (to_read == -1) {
|
2017-06-20 15:39:52 +00:00
|
|
|
od_error_client(&instance->log, &client->id, "startup",
|
2017-06-08 12:36:21 +00:00
|
|
|
"failed to read startup packet, closing");
|
2017-05-26 11:49:17 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2017-07-06 13:36:14 +00:00
|
|
|
int rc = shapito_stream_ensure(stream, to_read);
|
2017-05-26 11:49:17 +00:00
|
|
|
if (rc == -1)
|
|
|
|
return -1;
|
2017-07-04 13:27:42 +00:00
|
|
|
rc = machine_read(client->io, stream->pos, to_read, UINT32_MAX);
|
2017-06-14 12:35:04 +00:00
|
|
|
if (rc == -1) {
|
2017-06-20 15:39:52 +00:00
|
|
|
od_error_client(&instance->log, &client->id, "startup",
|
2017-05-31 15:47:15 +00:00
|
|
|
"read error: %s",
|
|
|
|
machine_error(client->io));
|
2017-05-26 11:49:17 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2017-07-06 13:36:14 +00:00
|
|
|
shapito_stream_advance(stream, to_read);
|
2017-05-26 11:49:17 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
od_frontend_startup(od_client_t *client)
|
|
|
|
{
|
2017-06-02 13:49:20 +00:00
|
|
|
od_instance_t *instance = client->system->instance;
|
|
|
|
|
2017-05-26 11:49:17 +00:00
|
|
|
int rc;
|
|
|
|
rc = od_frontend_startup_read(client);
|
|
|
|
if (rc == -1)
|
|
|
|
return -1;
|
2017-07-06 13:36:14 +00:00
|
|
|
shapito_stream_t *stream = &client->stream;
|
|
|
|
rc = shapito_be_read_startup(&client->startup, stream->start,
|
|
|
|
shapito_stream_used(stream));
|
2017-05-26 11:49:17 +00:00
|
|
|
if (rc == -1)
|
2017-06-08 12:36:21 +00:00
|
|
|
goto error;
|
2017-05-26 11:49:17 +00:00
|
|
|
|
|
|
|
/* client ssl request */
|
2017-06-02 13:49:20 +00:00
|
|
|
rc = od_tls_frontend_accept(client, &instance->log,
|
|
|
|
&instance->scheme,
|
2017-06-26 13:23:50 +00:00
|
|
|
client->tls);
|
2017-05-26 11:49:17 +00:00
|
|
|
if (rc == -1)
|
|
|
|
return -1;
|
2017-06-02 13:49:20 +00:00
|
|
|
|
2017-05-26 11:49:17 +00:00
|
|
|
if (! client->startup.is_ssl_request)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* read startup-cancel message followed after ssl
|
|
|
|
* negotiation */
|
|
|
|
assert(client->startup.is_ssl_request);
|
|
|
|
rc = od_frontend_startup_read(client);
|
|
|
|
if (rc == -1)
|
|
|
|
return -1;
|
2017-07-06 13:36:14 +00:00
|
|
|
rc = shapito_be_read_startup(&client->startup, stream->start,
|
|
|
|
shapito_stream_used(stream));
|
2017-05-26 11:49:17 +00:00
|
|
|
if (rc == -1)
|
2017-06-08 12:36:21 +00:00
|
|
|
goto error;
|
2017-05-26 11:49:17 +00:00
|
|
|
return 0;
|
2017-06-08 12:36:21 +00:00
|
|
|
|
|
|
|
error:
|
2017-06-20 15:39:52 +00:00
|
|
|
od_error_client(&instance->log, &client->id, "startup",
|
2017-06-08 12:36:21 +00:00
|
|
|
"incorrect startup packet");
|
2017-07-06 13:36:14 +00:00
|
|
|
od_frontend_error(client, SHAPITO_PROTOCOL_VIOLATION,
|
2017-06-08 12:36:21 +00:00
|
|
|
"bad startup packet");
|
|
|
|
return -1;
|
2017-05-26 11:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
od_frontend_key(od_client_t *client)
|
|
|
|
{
|
2017-06-20 15:39:52 +00:00
|
|
|
client->key.key_pid = *(uint32_t*)client->id.id;
|
2017-05-26 11:49:17 +00:00
|
|
|
client->key.key = 1 + rand();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
od_frontend_setup(od_client_t *client)
|
|
|
|
{
|
2017-05-27 14:21:39 +00:00
|
|
|
od_instance_t *instance = client->system->instance;
|
2017-05-26 11:49:17 +00:00
|
|
|
|
2017-07-06 13:36:14 +00:00
|
|
|
shapito_stream_t *stream = &client->stream;
|
|
|
|
shapito_stream_reset(stream);
|
2017-05-26 11:49:17 +00:00
|
|
|
int rc;
|
2017-07-06 13:36:14 +00:00
|
|
|
rc = shapito_be_write_backend_key_data(stream, client->key.key_pid,
|
|
|
|
client->key.key);
|
2017-05-26 11:49:17 +00:00
|
|
|
if (rc == -1)
|
|
|
|
return -1;
|
2017-07-06 13:36:14 +00:00
|
|
|
rc = shapito_be_write_parameter_status(stream, "", 1, "", 1);
|
2017-05-26 11:49:17 +00:00
|
|
|
if (rc == -1)
|
|
|
|
return -1;
|
|
|
|
rc = od_write(client->io, stream);
|
|
|
|
if (rc == -1) {
|
2017-06-20 15:39:52 +00:00
|
|
|
od_error_client(&instance->log, &client->id, "setup",
|
2017-05-31 15:47:15 +00:00
|
|
|
"write error: %s",
|
|
|
|
machine_error(client->io));
|
2017-05-26 11:49:17 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
od_frontend_ready(od_client_t *client)
|
|
|
|
{
|
2017-05-27 14:21:39 +00:00
|
|
|
od_instance_t *instance = client->system->instance;
|
2017-05-26 11:49:17 +00:00
|
|
|
|
2017-07-06 13:36:14 +00:00
|
|
|
shapito_stream_t *stream = &client->stream;
|
|
|
|
shapito_stream_reset(stream);
|
2017-05-26 11:49:17 +00:00
|
|
|
int rc;
|
2017-07-06 13:36:14 +00:00
|
|
|
rc = shapito_be_write_ready(stream, 'I');
|
2017-05-26 11:49:17 +00:00
|
|
|
if (rc == -1)
|
|
|
|
return -1;
|
|
|
|
rc = od_write(client->io, stream);
|
|
|
|
if (rc == -1) {
|
2017-06-20 15:39:52 +00:00
|
|
|
od_error_client(&instance->log, &client->id,
|
2017-05-31 15:47:15 +00:00
|
|
|
"write error: %s",
|
|
|
|
machine_error(client->io));
|
2017-05-26 11:49:17 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-29 14:53:21 +00:00
|
|
|
enum {
|
|
|
|
OD_RS_UNDEF,
|
|
|
|
OD_RS_OK,
|
2017-06-01 09:28:23 +00:00
|
|
|
OD_RS_EATTACH,
|
2017-06-16 11:57:43 +00:00
|
|
|
OD_RS_ESERVER_CONNECT,
|
2017-05-29 14:53:21 +00:00
|
|
|
OD_RS_ESERVER_CONFIGURE,
|
|
|
|
OD_RS_ESERVER_READ,
|
|
|
|
OD_RS_ESERVER_WRITE,
|
|
|
|
OD_RS_ECLIENT_READ,
|
|
|
|
OD_RS_ECLIENT_WRITE
|
|
|
|
};
|
|
|
|
|
2017-06-07 12:46:32 +00:00
|
|
|
static inline int
|
2017-05-29 14:53:21 +00:00
|
|
|
od_frontend_copy_in(od_client_t *client)
|
|
|
|
{
|
|
|
|
od_instance_t *instance = client->system->instance;
|
|
|
|
od_server_t *server = client->server;
|
|
|
|
|
|
|
|
assert(! server->is_copy);
|
|
|
|
server->is_copy = 1;
|
|
|
|
|
|
|
|
int rc, type;
|
2017-07-06 13:36:14 +00:00
|
|
|
shapito_stream_t *stream = &client->stream;
|
2017-05-29 14:53:21 +00:00
|
|
|
for (;;) {
|
2017-07-06 13:36:14 +00:00
|
|
|
shapito_stream_reset(stream);
|
2017-05-29 14:53:21 +00:00
|
|
|
rc = od_read(client->io, stream, UINT32_MAX);
|
|
|
|
if (rc == -1)
|
|
|
|
return OD_RS_ECLIENT_READ;
|
2017-07-04 13:27:42 +00:00
|
|
|
type = *stream->start;
|
2017-06-20 15:39:52 +00:00
|
|
|
od_debug_client(&instance->log, &client->id, "copy",
|
2017-07-04 13:27:42 +00:00
|
|
|
"%c", *stream->start);
|
2017-05-29 14:53:21 +00:00
|
|
|
rc = od_write(server->io, stream);
|
|
|
|
if (rc == -1)
|
|
|
|
return OD_RS_ESERVER_WRITE;
|
|
|
|
|
|
|
|
/* copy complete or fail */
|
|
|
|
if (type == 'c' || type == 'f')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
server->is_copy = 0;
|
|
|
|
return OD_RS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-06-22 12:29:39 +00:00
|
|
|
od_frontend_remote(od_client_t *client)
|
2017-06-01 10:41:14 +00:00
|
|
|
{
|
|
|
|
od_instance_t *instance = client->system->instance;
|
2017-06-23 10:23:10 +00:00
|
|
|
od_route_t *route = client->route;
|
2017-06-01 10:41:14 +00:00
|
|
|
|
|
|
|
od_server_t *server = NULL;
|
2017-07-06 13:36:14 +00:00
|
|
|
shapito_stream_t *stream = &client->stream;
|
2017-06-01 10:41:14 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* client to server */
|
2017-07-06 13:36:14 +00:00
|
|
|
shapito_stream_reset(stream);
|
2017-06-23 10:23:10 +00:00
|
|
|
int rc;
|
2017-06-01 10:41:14 +00:00
|
|
|
rc = od_read(client->io, stream, UINT32_MAX);
|
|
|
|
if (rc == -1)
|
|
|
|
return OD_RS_ECLIENT_READ;
|
2017-06-21 13:31:51 +00:00
|
|
|
int offset = rc;
|
2017-07-04 13:27:42 +00:00
|
|
|
int type = stream->start[offset];
|
2017-06-20 15:39:52 +00:00
|
|
|
od_debug_client(&instance->log, &client->id, NULL,
|
2017-06-01 10:41:14 +00:00
|
|
|
"%c", type);
|
|
|
|
|
2017-06-07 12:46:32 +00:00
|
|
|
/* Terminate (client graceful shutdown) */
|
2017-06-01 10:41:14 +00:00
|
|
|
if (type == 'X')
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* get server connection from the route pool */
|
2017-06-16 11:57:43 +00:00
|
|
|
if (server == NULL)
|
|
|
|
{
|
2017-06-01 10:41:14 +00:00
|
|
|
od_routerstatus_t status;
|
|
|
|
status = od_router_attach(client);
|
|
|
|
if (status != OD_ROK)
|
|
|
|
return OD_RS_EATTACH;
|
|
|
|
server = client->server;
|
2017-06-20 15:39:52 +00:00
|
|
|
od_debug_client(&instance->log, &client->id, NULL,
|
|
|
|
"attached to s%.*s",
|
|
|
|
sizeof(server->id.id),
|
|
|
|
server->id.id);
|
2017-06-01 10:41:14 +00:00
|
|
|
|
2017-06-09 12:30:53 +00:00
|
|
|
/* configure server using client startup parameters,
|
|
|
|
* if it has not been configured before. */
|
2017-06-20 15:39:52 +00:00
|
|
|
if (od_idmgr_cmp(&server->last_client_id, &client->id)) {
|
2017-06-16 11:57:43 +00:00
|
|
|
assert(server->io != NULL);
|
2017-06-20 15:39:52 +00:00
|
|
|
od_debug_client(&instance->log, &client->id, NULL,
|
|
|
|
"previously owned, no need to reconfigure s%.*s",
|
|
|
|
sizeof(server->id.id),
|
|
|
|
server->id.id);
|
2017-06-09 12:30:53 +00:00
|
|
|
} else {
|
2017-06-16 11:57:43 +00:00
|
|
|
/* connect to server, if necessary */
|
|
|
|
if (server->io == NULL) {
|
|
|
|
rc = od_backend_connect(server);
|
|
|
|
if (rc == -1)
|
|
|
|
return OD_RS_ESERVER_CONNECT;
|
|
|
|
} else {
|
|
|
|
/* discard last server configuration */
|
2017-07-03 11:02:01 +00:00
|
|
|
if (route->scheme->pool_discard) {
|
2017-06-09 12:49:55 +00:00
|
|
|
rc = od_backend_discard(client->server);
|
|
|
|
if (rc == -1)
|
|
|
|
return OD_RS_ESERVER_CONFIGURE;
|
|
|
|
}
|
|
|
|
}
|
2017-06-16 11:57:43 +00:00
|
|
|
|
2017-06-09 12:49:55 +00:00
|
|
|
/* set client parameters */
|
2017-06-09 12:30:53 +00:00
|
|
|
rc = od_backend_configure(client->server, &client->startup);
|
|
|
|
if (rc == -1)
|
|
|
|
return OD_RS_ESERVER_CONFIGURE;
|
|
|
|
}
|
2017-06-01 10:41:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = od_write(server->io, stream);
|
|
|
|
if (rc == -1)
|
|
|
|
return OD_RS_ESERVER_WRITE;
|
|
|
|
server->count_request++;
|
|
|
|
|
2017-07-06 13:36:14 +00:00
|
|
|
shapito_stream_reset(stream);
|
2017-06-01 10:41:14 +00:00
|
|
|
for (;;) {
|
2017-06-07 12:40:08 +00:00
|
|
|
/* read server reply */
|
2017-06-01 10:41:14 +00:00
|
|
|
for (;;) {
|
|
|
|
rc = od_read(server->io, stream, 1000);
|
|
|
|
if (rc >= 0)
|
|
|
|
break;
|
|
|
|
/* client watchdog.
|
|
|
|
*
|
|
|
|
* ensure that client has not closed
|
|
|
|
* the connection */
|
|
|
|
if (! machine_timedout())
|
|
|
|
return OD_RS_ESERVER_READ;
|
|
|
|
if (machine_connected(client->io))
|
|
|
|
continue;
|
2017-06-20 15:39:52 +00:00
|
|
|
od_debug_server(&instance->log, &server->id, "watchdog",
|
2017-06-01 10:41:14 +00:00
|
|
|
"client disconnected");
|
|
|
|
return OD_RS_ECLIENT_READ;
|
|
|
|
}
|
2017-06-21 13:31:51 +00:00
|
|
|
offset = rc;
|
2017-07-04 13:27:42 +00:00
|
|
|
type = stream->start[offset];
|
2017-06-20 15:39:52 +00:00
|
|
|
od_debug_server(&instance->log, &server->id, NULL,
|
2017-06-01 10:41:14 +00:00
|
|
|
"%c", type);
|
|
|
|
|
2017-06-15 13:16:44 +00:00
|
|
|
/* ErrorResponse */
|
|
|
|
if (type == 'E') {
|
|
|
|
od_backend_error(server, NULL,
|
2017-07-04 13:27:42 +00:00
|
|
|
stream->start + offset,
|
2017-07-06 13:36:14 +00:00
|
|
|
shapito_stream_used(stream) - offset);
|
2017-06-15 13:16:44 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:46:32 +00:00
|
|
|
/* ReadyForQuery */
|
2017-06-01 10:41:14 +00:00
|
|
|
if (type == 'Z') {
|
2017-07-04 13:27:42 +00:00
|
|
|
rc = od_backend_ready(server, stream->start + offset,
|
2017-07-06 13:36:14 +00:00
|
|
|
shapito_stream_used(stream) - offset);
|
2017-06-01 10:41:14 +00:00
|
|
|
if (rc == -1)
|
|
|
|
return OD_RS_ECLIENT_READ;
|
|
|
|
|
2017-06-07 12:40:08 +00:00
|
|
|
/* force buffer flush to client */
|
2017-06-01 10:41:14 +00:00
|
|
|
rc = od_write(client->io, stream);
|
|
|
|
if (rc == -1)
|
|
|
|
return OD_RS_ECLIENT_WRITE;
|
|
|
|
|
2017-06-01 10:51:51 +00:00
|
|
|
/* transaction pooling */
|
2017-07-03 10:56:47 +00:00
|
|
|
if (route->scheme->pool == OD_PTRANSACTION) {
|
2017-06-01 10:51:51 +00:00
|
|
|
if (! server->is_transaction) {
|
|
|
|
/* cleanup server */
|
|
|
|
rc = od_backend_reset(server);
|
|
|
|
if (rc == -1)
|
|
|
|
return OD_RS_ESERVER_WRITE;
|
|
|
|
/* push server connection back to route pool */
|
|
|
|
od_router_detach(client);
|
|
|
|
server = NULL;
|
|
|
|
}
|
2017-06-01 10:41:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CopyInResponse */
|
|
|
|
if (type == 'G') {
|
2017-06-07 12:40:08 +00:00
|
|
|
/* force buffer flush to client */
|
2017-06-01 10:41:14 +00:00
|
|
|
rc = od_write(client->io, stream);
|
|
|
|
if (rc == -1)
|
|
|
|
return OD_RS_ECLIENT_WRITE;
|
2017-06-07 12:40:08 +00:00
|
|
|
|
2017-06-07 12:46:32 +00:00
|
|
|
/* switch to CopyIn mode */
|
2017-06-01 10:41:14 +00:00
|
|
|
rc = od_frontend_copy_in(client);
|
|
|
|
if (rc != OD_RS_OK)
|
|
|
|
return rc;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* CopyOutResponse */
|
|
|
|
if (type == 'H') {
|
|
|
|
assert(! server->is_copy);
|
|
|
|
server->is_copy = 1;
|
|
|
|
}
|
2017-06-15 14:24:09 +00:00
|
|
|
/* CopyDone */
|
2017-06-01 10:41:14 +00:00
|
|
|
if (type == 'c') {
|
|
|
|
server->is_copy = 0;
|
|
|
|
}
|
2017-06-07 12:40:08 +00:00
|
|
|
|
|
|
|
/* server pipelining buffer flush */
|
2017-07-06 13:36:14 +00:00
|
|
|
if (shapito_stream_used(stream) >= instance->scheme.server_pipelining) {
|
2017-06-07 12:40:08 +00:00
|
|
|
rc = od_write(client->io, stream);
|
|
|
|
if (rc == -1)
|
|
|
|
return OD_RS_ECLIENT_WRITE;
|
2017-07-06 13:36:14 +00:00
|
|
|
shapito_stream_reset(stream);
|
2017-06-07 12:40:08 +00:00
|
|
|
}
|
2017-06-01 10:41:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return OD_RS_OK;
|
|
|
|
}
|
|
|
|
|
2017-06-22 12:29:39 +00:00
|
|
|
static int
|
|
|
|
od_frontend_local(od_client_t *client)
|
|
|
|
{
|
2017-06-22 13:37:56 +00:00
|
|
|
od_instance_t *instance = client->system->instance;
|
|
|
|
int rc;
|
|
|
|
|
2017-07-06 13:36:14 +00:00
|
|
|
shapito_stream_t *stream = &client->stream;
|
2017-06-22 13:37:56 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* read client request */
|
2017-07-06 13:36:14 +00:00
|
|
|
shapito_stream_reset(stream);
|
2017-06-22 13:37:56 +00:00
|
|
|
rc = od_read(client->io, stream, UINT32_MAX);
|
|
|
|
if (rc == -1)
|
|
|
|
return OD_RS_ECLIENT_READ;
|
|
|
|
int offset = rc;
|
2017-07-04 13:27:42 +00:00
|
|
|
int type = stream->start[offset];
|
2017-06-22 13:37:56 +00:00
|
|
|
od_debug_client(&instance->log, &client->id, NULL,
|
|
|
|
"%c", type);
|
|
|
|
|
|
|
|
/* Terminate */
|
|
|
|
if (type == 'X')
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Query */
|
|
|
|
if (type == 'Q') {
|
2017-06-24 14:59:28 +00:00
|
|
|
od_consolestatus_t cs;
|
2017-07-04 13:27:42 +00:00
|
|
|
cs = od_console_request(client, stream->start + offset);
|
2017-06-24 14:59:28 +00:00
|
|
|
if (cs == OD_CERROR) {
|
|
|
|
}
|
|
|
|
rc = od_write(client->io, stream);
|
|
|
|
if (rc == -1)
|
|
|
|
return OD_RS_ECLIENT_WRITE;
|
|
|
|
continue;
|
2017-06-22 13:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* unsupported */
|
|
|
|
od_error_client(&instance->log, &client->id, "local",
|
|
|
|
"unsupported request '%c'",
|
|
|
|
type);
|
2017-07-06 13:36:14 +00:00
|
|
|
od_frontend_error(client, SHAPITO_FEATURE_NOT_SUPPORTED,
|
2017-06-28 11:47:42 +00:00
|
|
|
"unsupported request '%c'", type);
|
2017-07-06 13:36:14 +00:00
|
|
|
shapito_stream_reset(stream);
|
|
|
|
rc = shapito_be_write_ready(stream, 'I');
|
2017-06-22 13:37:56 +00:00
|
|
|
if (rc == -1)
|
|
|
|
return OD_RS_ECLIENT_WRITE;
|
|
|
|
rc = od_write(client->io, stream);
|
|
|
|
if (rc == -1)
|
|
|
|
return OD_RS_ECLIENT_WRITE;
|
|
|
|
}
|
2017-06-22 12:29:39 +00:00
|
|
|
return OD_RS_OK;
|
|
|
|
}
|
|
|
|
|
2017-05-26 12:17:45 +00:00
|
|
|
void od_frontend(void *arg)
|
2017-05-26 11:49:17 +00:00
|
|
|
{
|
|
|
|
od_client_t *client = arg;
|
2017-05-27 14:21:39 +00:00
|
|
|
od_instance_t *instance = client->system->instance;
|
2017-05-26 11:49:17 +00:00
|
|
|
|
2017-06-28 10:58:21 +00:00
|
|
|
/* log client connection */
|
|
|
|
if (instance->scheme.log_session) {
|
|
|
|
char peer[128];
|
|
|
|
od_getpeername(client->io, peer, sizeof(peer));
|
|
|
|
od_log_client(&instance->log, &client->id, NULL,
|
|
|
|
"new client connection %s",
|
|
|
|
peer);
|
|
|
|
}
|
2017-05-26 11:49:17 +00:00
|
|
|
|
|
|
|
/* attach client io to relay machine event loop */
|
|
|
|
int rc;
|
|
|
|
rc = machine_io_attach(client->io);
|
|
|
|
if (rc == -1) {
|
2017-06-20 15:39:52 +00:00
|
|
|
od_error_client(&instance->log, &client->id, NULL,
|
2017-05-31 15:47:15 +00:00
|
|
|
"failed to transfer client io");
|
2017-05-26 11:49:17 +00:00
|
|
|
machine_close(client->io);
|
|
|
|
od_client_free(client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* client startup */
|
|
|
|
rc = od_frontend_startup(client);
|
|
|
|
if (rc == -1) {
|
|
|
|
od_frontend_close(client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* client cancel request */
|
|
|
|
if (client->startup.is_cancel) {
|
2017-06-20 15:39:52 +00:00
|
|
|
od_debug_client(&instance->log, &client->id, NULL,
|
2017-05-31 15:47:15 +00:00
|
|
|
"cancel request");
|
2017-05-30 11:34:08 +00:00
|
|
|
od_router_cancel(client);
|
2017-05-29 15:33:44 +00:00
|
|
|
od_frontend_close(client);
|
2017-05-26 11:49:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate backend key for the client.
|
|
|
|
*
|
|
|
|
* This key will be used to identify a server by
|
|
|
|
* user cancel requests. The key must be regenerated
|
|
|
|
* for each new client-server assignment, to avoid
|
|
|
|
* possibility of cancelling requests by a previous
|
|
|
|
* server owners.
|
|
|
|
*/
|
|
|
|
od_frontend_key(client);
|
|
|
|
|
2017-05-26 13:44:42 +00:00
|
|
|
/* route client */
|
|
|
|
od_routerstatus_t status;
|
2017-05-29 15:07:22 +00:00
|
|
|
status = od_route(client);
|
2017-05-26 11:49:17 +00:00
|
|
|
switch (status) {
|
2017-05-26 13:44:42 +00:00
|
|
|
case OD_RERROR:
|
2017-06-20 15:39:52 +00:00
|
|
|
od_error_client(&instance->log, &client->id, NULL,
|
2017-05-31 15:47:15 +00:00
|
|
|
"routing failed, closing");
|
2017-07-06 13:36:14 +00:00
|
|
|
od_frontend_error(client, SHAPITO_SYSTEM_ERROR,
|
2017-06-08 12:36:21 +00:00
|
|
|
"client routing failed");
|
2017-05-26 13:44:42 +00:00
|
|
|
od_frontend_close(client);
|
|
|
|
return;
|
|
|
|
case OD_RERROR_NOT_FOUND:
|
2017-06-20 15:39:52 +00:00
|
|
|
od_error_client(&instance->log, &client->id, NULL,
|
2017-07-03 14:32:48 +00:00
|
|
|
"route '%s.%s' not matched, closing",
|
2017-07-06 13:36:14 +00:00
|
|
|
shapito_parameter_value(client->startup.database),
|
|
|
|
shapito_parameter_value(client->startup.user));
|
|
|
|
od_frontend_error(client, SHAPITO_UNDEFINED_DATABASE,
|
2017-07-03 14:32:48 +00:00
|
|
|
"route is not matched");
|
2017-06-08 12:36:21 +00:00
|
|
|
od_frontend_close(client);
|
|
|
|
return;
|
2017-05-26 13:44:42 +00:00
|
|
|
case OD_RERROR_LIMIT:
|
2017-06-20 15:39:52 +00:00
|
|
|
od_error_client(&instance->log, &client->id, NULL,
|
2017-05-31 15:47:15 +00:00
|
|
|
"route connection limit reached, closing");
|
2017-07-06 13:36:14 +00:00
|
|
|
od_frontend_error(client, SHAPITO_TOO_MANY_CONNECTIONS,
|
2017-06-08 11:30:00 +00:00
|
|
|
"too many connections");
|
2017-05-26 13:44:42 +00:00
|
|
|
od_frontend_close(client);
|
|
|
|
return;
|
2017-06-22 13:37:56 +00:00
|
|
|
case OD_ROK:
|
2017-06-22 12:29:39 +00:00
|
|
|
{
|
2017-05-26 13:44:42 +00:00
|
|
|
od_route_t *route = client->route;
|
2017-06-20 15:39:52 +00:00
|
|
|
od_debug_client(&instance->log, &client->id, NULL,
|
2017-07-03 14:32:48 +00:00
|
|
|
"route to '%s.%s' (using '%s' storage)",
|
2017-07-21 14:29:01 +00:00
|
|
|
route->scheme->db_name,
|
|
|
|
route->scheme->user_name,
|
2017-06-21 12:18:48 +00:00
|
|
|
route->scheme->storage->name);
|
2017-05-26 11:49:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-06-29 14:05:48 +00:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* client authentication */
|
|
|
|
rc = od_auth_frontend(client);
|
|
|
|
if (rc == -1) {
|
2017-07-03 14:32:48 +00:00
|
|
|
od_unroute(client);
|
2017-06-29 14:05:48 +00:00
|
|
|
od_frontend_close(client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set client backend options and the key */
|
|
|
|
rc = od_frontend_setup(client);
|
|
|
|
if (rc == -1) {
|
2017-07-03 14:32:48 +00:00
|
|
|
od_unroute(client);
|
2017-06-29 14:05:48 +00:00
|
|
|
od_frontend_close(client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* notify client that we are ready */
|
|
|
|
rc = od_frontend_ready(client);
|
|
|
|
if (rc == -1) {
|
2017-07-03 14:32:48 +00:00
|
|
|
od_unroute(client);
|
2017-06-29 14:05:48 +00:00
|
|
|
od_frontend_close(client);
|
|
|
|
return;
|
2017-06-22 12:29:39 +00:00
|
|
|
}
|
2017-05-26 13:44:42 +00:00
|
|
|
|
2017-06-01 10:41:14 +00:00
|
|
|
/* client main */
|
2017-06-22 12:29:39 +00:00
|
|
|
od_route_t *route = client->route;
|
|
|
|
switch (route->scheme->storage->storage_type) {
|
|
|
|
case OD_SREMOTE:
|
|
|
|
rc = od_frontend_remote(client);
|
|
|
|
break;
|
|
|
|
case OD_SLOCAL:
|
|
|
|
rc = od_frontend_local(client);
|
|
|
|
break;
|
|
|
|
}
|
2017-06-01 10:41:14 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
2017-05-31 10:49:12 +00:00
|
|
|
od_server_t *server = client->server;
|
2017-05-29 14:53:21 +00:00
|
|
|
switch (rc) {
|
2017-06-01 09:28:23 +00:00
|
|
|
case OD_RS_EATTACH:
|
2017-05-29 14:53:21 +00:00
|
|
|
assert(server == NULL);
|
2017-06-01 09:28:23 +00:00
|
|
|
assert(client->route != NULL);
|
2017-07-06 13:36:14 +00:00
|
|
|
od_frontend_error(client, SHAPITO_CONNECTION_FAILURE,
|
2017-06-28 11:47:42 +00:00
|
|
|
"failed to get remote server connection");
|
2017-06-01 09:28:23 +00:00
|
|
|
/* detach client from route */
|
|
|
|
od_unroute(client);
|
2017-05-29 14:53:21 +00:00
|
|
|
break;
|
2017-06-16 11:57:43 +00:00
|
|
|
|
2017-05-29 14:53:21 +00:00
|
|
|
case OD_RS_OK:
|
2017-06-01 10:41:14 +00:00
|
|
|
/* graceful disconnect */
|
2017-06-28 10:58:21 +00:00
|
|
|
if (instance->scheme.log_session) {
|
|
|
|
od_log_client(&instance->log, &client->id, NULL,
|
|
|
|
"disconnected");
|
|
|
|
}
|
2017-06-01 10:41:14 +00:00
|
|
|
if (! client->server) {
|
|
|
|
od_unroute(client);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rc = od_backend_reset(server);
|
|
|
|
if (rc != 1) {
|
|
|
|
/* close backend connection */
|
|
|
|
od_router_close_and_unroute(client);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* push server to router server pool */
|
|
|
|
od_router_detach_and_unroute(client);
|
|
|
|
break;
|
2017-06-16 11:57:43 +00:00
|
|
|
|
2017-05-29 14:53:21 +00:00
|
|
|
case OD_RS_ECLIENT_READ:
|
|
|
|
case OD_RS_ECLIENT_WRITE:
|
|
|
|
/* close client connection and reuse server
|
2017-06-01 10:41:14 +00:00
|
|
|
* link in case of client errors */
|
2017-06-20 15:39:52 +00:00
|
|
|
od_log_client(&instance->log, &client->id, NULL,
|
2017-06-01 10:41:14 +00:00
|
|
|
"disconnected (read/write error): %s",
|
|
|
|
machine_error(client->io));
|
2017-06-05 13:45:28 +00:00
|
|
|
if (! client->server) {
|
|
|
|
od_unroute(client);
|
|
|
|
break;
|
|
|
|
}
|
2017-05-29 14:53:21 +00:00
|
|
|
rc = od_backend_reset(server);
|
|
|
|
if (rc != 1) {
|
2017-05-30 15:04:59 +00:00
|
|
|
/* close backend connection */
|
2017-05-31 10:49:12 +00:00
|
|
|
od_router_close_and_unroute(client);
|
2017-05-29 14:53:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-05-29 15:07:22 +00:00
|
|
|
/* push server to router server pool */
|
2017-05-31 10:49:12 +00:00
|
|
|
od_router_detach_and_unroute(client);
|
2017-05-29 14:53:21 +00:00
|
|
|
break;
|
2017-06-16 11:57:43 +00:00
|
|
|
|
|
|
|
case OD_RS_ESERVER_CONNECT:
|
|
|
|
/* server attached to client and connection failed */
|
2017-07-06 13:36:14 +00:00
|
|
|
od_frontend_error(client, SHAPITO_CONNECTION_FAILURE,
|
2017-06-28 11:47:42 +00:00
|
|
|
"failed to connect to remote server s%.*s",
|
2017-06-20 15:39:52 +00:00
|
|
|
sizeof(server->id.id),
|
|
|
|
server->id.id);
|
2017-06-16 11:57:43 +00:00
|
|
|
/* close backend connection */
|
|
|
|
od_router_close_and_unroute(client);
|
|
|
|
break;
|
|
|
|
|
2017-05-29 14:53:21 +00:00
|
|
|
case OD_RS_ESERVER_CONFIGURE:
|
2017-06-20 15:39:52 +00:00
|
|
|
od_log_server(&instance->log, &server->id, NULL,
|
2017-06-16 11:57:43 +00:00
|
|
|
"disconnected (server configure error)");
|
2017-07-06 13:36:14 +00:00
|
|
|
od_frontend_error(client, SHAPITO_CONNECTION_FAILURE,
|
2017-06-28 11:47:42 +00:00
|
|
|
"failed to configure remote server s%.*s",
|
2017-06-20 15:39:52 +00:00
|
|
|
sizeof(server->id.id),
|
|
|
|
server->id.id);
|
2017-05-30 15:04:59 +00:00
|
|
|
/* close backend connection */
|
2017-05-31 10:49:12 +00:00
|
|
|
od_router_close_and_unroute(client);
|
2017-05-29 14:53:21 +00:00
|
|
|
break;
|
2017-06-16 11:57:43 +00:00
|
|
|
|
2017-05-29 14:53:21 +00:00
|
|
|
case OD_RS_ESERVER_READ:
|
|
|
|
case OD_RS_ESERVER_WRITE:
|
|
|
|
/* close client connection and close server
|
|
|
|
* connection in case of server errors */
|
2017-06-20 15:39:52 +00:00
|
|
|
od_log_server(&instance->log, &server->id, NULL,
|
2017-05-31 15:47:15 +00:00
|
|
|
"disconnected (read/write error): %s",
|
|
|
|
machine_error(server->io));
|
2017-07-06 13:36:14 +00:00
|
|
|
od_frontend_error(client, SHAPITO_CONNECTION_FAILURE,
|
2017-06-28 11:47:42 +00:00
|
|
|
"remote server read/write error s%.*s",
|
2017-06-20 15:39:52 +00:00
|
|
|
sizeof(server->id.id),
|
|
|
|
server->id.id);
|
2017-05-30 15:04:59 +00:00
|
|
|
/* close backend connection */
|
2017-05-31 10:49:12 +00:00
|
|
|
od_router_close_and_unroute(client);
|
2017-05-29 14:53:21 +00:00
|
|
|
break;
|
2017-06-16 11:57:43 +00:00
|
|
|
|
2017-05-29 14:53:21 +00:00
|
|
|
case OD_RS_UNDEF:
|
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* close frontend connection */
|
2017-05-26 13:44:42 +00:00
|
|
|
od_frontend_close(client);
|
2017-05-26 11:49:17 +00:00
|
|
|
}
|