mirror of https://github.com/yandex/odyssey.git
odissey: implement random id manager
This commit is contained in:
parent
f48b883bae
commit
01787aec95
|
@ -8,6 +8,7 @@ set(od_src
|
||||||
od_lex.c
|
od_lex.c
|
||||||
od_scheme.c
|
od_scheme.c
|
||||||
od_config.c
|
od_config.c
|
||||||
|
od_id.c
|
||||||
od_instance.c
|
od_instance.c
|
||||||
od_server_pool.c
|
od_server_pool.c
|
||||||
od_client_pool.c
|
od_client_pool.c
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
#include "od_scheme.h"
|
#include "od_scheme.h"
|
||||||
#include "od_lex.h"
|
#include "od_lex.h"
|
||||||
#include "od_config.h"
|
#include "od_config.h"
|
||||||
|
#include "od_id.h"
|
||||||
#include "od_msg.h"
|
#include "od_msg.h"
|
||||||
#include "od_system.h"
|
#include "od_system.h"
|
||||||
#include "od_instance.h"
|
#include "od_instance.h"
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
#include "od_scheme.h"
|
#include "od_scheme.h"
|
||||||
#include "od_lex.h"
|
#include "od_lex.h"
|
||||||
#include "od_config.h"
|
#include "od_config.h"
|
||||||
|
#include "od_id.h"
|
||||||
#include "od_msg.h"
|
#include "od_msg.h"
|
||||||
#include "od_system.h"
|
#include "od_system.h"
|
||||||
#include "od_instance.h"
|
#include "od_instance.h"
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
#include "od_scheme.h"
|
#include "od_scheme.h"
|
||||||
#include "od_lex.h"
|
#include "od_lex.h"
|
||||||
#include "od_config.h"
|
#include "od_config.h"
|
||||||
|
#include "od_id.h"
|
||||||
#include "od_msg.h"
|
#include "od_msg.h"
|
||||||
#include "od_system.h"
|
#include "od_system.h"
|
||||||
#include "od_instance.h"
|
#include "od_instance.h"
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
#include "od_scheme.h"
|
#include "od_scheme.h"
|
||||||
#include "od_lex.h"
|
#include "od_lex.h"
|
||||||
#include "od_config.h"
|
#include "od_config.h"
|
||||||
|
#include "od_id.h"
|
||||||
#include "od_system.h"
|
#include "od_system.h"
|
||||||
#include "od_server.h"
|
#include "od_server.h"
|
||||||
#include "od_server_pool.h"
|
#include "od_server_pool.h"
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
#include "od_scheme.h"
|
#include "od_scheme.h"
|
||||||
#include "od_lex.h"
|
#include "od_lex.h"
|
||||||
#include "od_config.h"
|
#include "od_config.h"
|
||||||
|
#include "od_id.h"
|
||||||
#include "od_msg.h"
|
#include "od_msg.h"
|
||||||
#include "od_system.h"
|
#include "od_system.h"
|
||||||
#include "od_instance.h"
|
#include "od_instance.h"
|
||||||
|
|
|
@ -0,0 +1,130 @@
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ODISSEY.
|
||||||
|
*
|
||||||
|
* PostgreSQL connection pooler and request router.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define _GNU_SOURCE
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/tcp.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
|
||||||
|
#include <machinarium.h>
|
||||||
|
#include <shapito.h>
|
||||||
|
|
||||||
|
#include "od_macro.h"
|
||||||
|
#include "od_pid.h"
|
||||||
|
#include "od_syslog.h"
|
||||||
|
#include "od_log.h"
|
||||||
|
#include "od_id.h"
|
||||||
|
|
||||||
|
void od_idmgr_init(od_idmgr_t *mgr)
|
||||||
|
{
|
||||||
|
memset(mgr->seed, 0, sizeof(mgr->seed));
|
||||||
|
mgr->seq = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int od_idmgr_seed(od_idmgr_t *mgr, od_log_t *log)
|
||||||
|
{
|
||||||
|
mgr->uid = getuid();
|
||||||
|
mgr->pid = getpid();
|
||||||
|
|
||||||
|
struct timeval tv;
|
||||||
|
gettimeofday(&tv, 0);
|
||||||
|
srand((mgr->pid << 16) ^ mgr->uid ^ tv.tv_sec ^ tv.tv_usec);
|
||||||
|
int i = 0;
|
||||||
|
for (; i < 8; i++)
|
||||||
|
mgr->seed[i] = (rand() >> 7) & 0xFF;
|
||||||
|
int fd;
|
||||||
|
fd = open("/dev/urandom", O_RDONLY);
|
||||||
|
if (fd == -1)
|
||||||
|
fd = open("/dev/random", O_RDONLY);
|
||||||
|
if (fd == -1) {
|
||||||
|
od_error(log, "id_manager", "failed to open /dev/{u}random");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
char seed[8];
|
||||||
|
int seed_read = 0;
|
||||||
|
while (seed_read <= 8) {
|
||||||
|
int rc;
|
||||||
|
rc = read(fd, seed, sizeof(seed));
|
||||||
|
if (rc == -1) {
|
||||||
|
if (errno == EAGAIN || errno == EINTR)
|
||||||
|
continue;
|
||||||
|
close(fd);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
seed_read += rc;
|
||||||
|
}
|
||||||
|
for (i = 0; i < 8; i++)
|
||||||
|
mgr->seed[i] ^= seed[i];
|
||||||
|
close(fd);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const uint8_t codetable[] =
|
||||||
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx";
|
||||||
|
static const size_t codetable_size = sizeof(codetable);
|
||||||
|
static const size_t codetable_single_divisor = sizeof(codetable) * 1;
|
||||||
|
static const size_t codetable_double_divisor = sizeof(codetable) * 2;
|
||||||
|
static const size_t codetable_triple_divisor = sizeof(codetable) * 3;
|
||||||
|
|
||||||
|
void od_idmgr_generate(od_idmgr_t *mgr, od_id_t *id)
|
||||||
|
{
|
||||||
|
struct timespec t;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &t);
|
||||||
|
|
||||||
|
time_t current_time = t.tv_sec;
|
||||||
|
|
||||||
|
uint8_t second;
|
||||||
|
second = current_time % 60;
|
||||||
|
current_time /= 60;
|
||||||
|
|
||||||
|
uint8_t minute;
|
||||||
|
minute = current_time % 60;
|
||||||
|
current_time /= 60;
|
||||||
|
|
||||||
|
uint8_t hour;
|
||||||
|
hour = current_time % 24;
|
||||||
|
current_time /= 60;
|
||||||
|
|
||||||
|
uint8_t day;
|
||||||
|
day = current_time % 30;
|
||||||
|
current_time /= 60;
|
||||||
|
|
||||||
|
uint8_t month;
|
||||||
|
month = current_time % 12;
|
||||||
|
current_time /= 60;
|
||||||
|
|
||||||
|
uint64_t seq;
|
||||||
|
seq = ++mgr->seq;
|
||||||
|
seq ^= t.tv_nsec;
|
||||||
|
|
||||||
|
uint8_t *seed = &id->id[0];
|
||||||
|
seed[0] = mgr->seed[0] ^ codetable[second];
|
||||||
|
seed[1] = mgr->seed[1] ^ codetable[minute];
|
||||||
|
seed[2] = mgr->seed[2] ^ codetable[(hour + day + month) % codetable_size];
|
||||||
|
seed[3] = mgr->seed[3] ^ codetable[(seq) % codetable_size];
|
||||||
|
seed[4] = mgr->seed[4] ^ codetable[(seq / codetable_single_divisor) % codetable_size];
|
||||||
|
seed[5] = mgr->seed[5] ^ codetable[(seq / codetable_double_divisor) % codetable_size];
|
||||||
|
seed[6] = mgr->seed[6] ^ codetable[(seq / codetable_triple_divisor) % codetable_size];
|
||||||
|
seed[7] = mgr->seed[7] ^ codetable[(uintptr_t)id % codetable_size];
|
||||||
|
seed[8] = mgr->seed[8] ^ codetable[(mgr->pid + mgr->uid) % codetable_size];
|
||||||
|
memcpy(mgr->seed, seed, 8);
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
#ifndef OD_ID_H
|
||||||
|
#define OD_ID_H
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ODISSEY.
|
||||||
|
*
|
||||||
|
* PostgreSQL connection pooler and request router.
|
||||||
|
*/
|
||||||
|
|
||||||
|
typedef struct od_id od_id_t;
|
||||||
|
typedef struct od_idmgr od_idmgr_t;
|
||||||
|
|
||||||
|
struct od_id
|
||||||
|
{
|
||||||
|
uint8_t id[8];
|
||||||
|
};
|
||||||
|
|
||||||
|
struct od_idmgr
|
||||||
|
{
|
||||||
|
uint64_t seq;
|
||||||
|
uint8_t seed[8];
|
||||||
|
pid_t pid;
|
||||||
|
uid_t uid;
|
||||||
|
};
|
||||||
|
|
||||||
|
void od_idmgr_init(od_idmgr_t*);
|
||||||
|
int od_idmgr_seed(od_idmgr_t*, od_log_t*);
|
||||||
|
void od_idmgr_generate(od_idmgr_t*, od_id_t*);
|
||||||
|
|
||||||
|
#endif /* OD_ID_H */
|
|
@ -26,6 +26,7 @@
|
||||||
#include "od_scheme.h"
|
#include "od_scheme.h"
|
||||||
#include "od_lex.h"
|
#include "od_lex.h"
|
||||||
#include "od_config.h"
|
#include "od_config.h"
|
||||||
|
#include "od_id.h"
|
||||||
#include "od_msg.h"
|
#include "od_msg.h"
|
||||||
#include "od_system.h"
|
#include "od_system.h"
|
||||||
#include "od_instance.h"
|
#include "od_instance.h"
|
||||||
|
@ -53,6 +54,7 @@ void od_instance_init(od_instance_t *instance)
|
||||||
od_log_init(&instance->log, &instance->pid, &instance->syslog);
|
od_log_init(&instance->log, &instance->pid, &instance->syslog);
|
||||||
od_scheme_init(&instance->scheme);
|
od_scheme_init(&instance->scheme);
|
||||||
od_config_init(&instance->config, &instance->log, &instance->scheme);
|
od_config_init(&instance->config, &instance->log, &instance->scheme);
|
||||||
|
od_idmgr_init(&instance->id_mgr);
|
||||||
|
|
||||||
sigset_t mask;
|
sigset_t mask;
|
||||||
sigemptyset(&mask);
|
sigemptyset(&mask);
|
||||||
|
@ -145,7 +147,8 @@ int od_instance_main(od_instance_t *instance, int argc, char **argv)
|
||||||
/* create pid file */
|
/* create pid file */
|
||||||
if (instance->scheme.pid_file)
|
if (instance->scheme.pid_file)
|
||||||
od_pid_create(&instance->pid, instance->scheme.pid_file);
|
od_pid_create(&instance->pid, instance->scheme.pid_file);
|
||||||
|
/* seed id manager */
|
||||||
|
od_idmgr_seed(&instance->id_mgr, &instance->log);
|
||||||
/* run system services */
|
/* run system services */
|
||||||
od_router_t router;
|
od_router_t router;
|
||||||
od_periodic_t periodic;
|
od_periodic_t periodic;
|
||||||
|
|
|
@ -16,6 +16,7 @@ struct od_instance
|
||||||
od_log_t log;
|
od_log_t log;
|
||||||
od_config_t config;
|
od_config_t config;
|
||||||
od_scheme_t scheme;
|
od_scheme_t scheme;
|
||||||
|
od_idmgr_t id_mgr;
|
||||||
};
|
};
|
||||||
|
|
||||||
void od_instance_init(od_instance_t*);
|
void od_instance_init(od_instance_t*);
|
||||||
|
|
|
@ -26,7 +26,6 @@
|
||||||
#include "od_pid.h"
|
#include "od_pid.h"
|
||||||
#include "od_syslog.h"
|
#include "od_syslog.h"
|
||||||
#include "od_log.h"
|
#include "od_log.h"
|
||||||
#include "od_io.h"
|
|
||||||
|
|
||||||
int od_log_init(od_log_t *log, od_pid_t *pid, od_syslog_t *syslog)
|
int od_log_init(od_log_t *log, od_pid_t *pid, od_syslog_t *syslog)
|
||||||
{
|
{
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
#include "od_scheme.h"
|
#include "od_scheme.h"
|
||||||
#include "od_lex.h"
|
#include "od_lex.h"
|
||||||
#include "od_config.h"
|
#include "od_config.h"
|
||||||
|
#include "od_id.h"
|
||||||
#include "od_msg.h"
|
#include "od_msg.h"
|
||||||
#include "od_system.h"
|
#include "od_system.h"
|
||||||
#include "od_instance.h"
|
#include "od_instance.h"
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
#include "od_scheme.h"
|
#include "od_scheme.h"
|
||||||
#include "od_lex.h"
|
#include "od_lex.h"
|
||||||
#include "od_config.h"
|
#include "od_config.h"
|
||||||
|
#include "od_id.h"
|
||||||
#include "od_msg.h"
|
#include "od_msg.h"
|
||||||
#include "od_system.h"
|
#include "od_system.h"
|
||||||
#include "od_instance.h"
|
#include "od_instance.h"
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
#include "od_scheme.h"
|
#include "od_scheme.h"
|
||||||
#include "od_lex.h"
|
#include "od_lex.h"
|
||||||
#include "od_config.h"
|
#include "od_config.h"
|
||||||
|
#include "od_id.h"
|
||||||
#include "od_msg.h"
|
#include "od_msg.h"
|
||||||
#include "od_system.h"
|
#include "od_system.h"
|
||||||
#include "od_instance.h"
|
#include "od_instance.h"
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
#include "od_scheme.h"
|
#include "od_scheme.h"
|
||||||
#include "od_lex.h"
|
#include "od_lex.h"
|
||||||
#include "od_config.h"
|
#include "od_config.h"
|
||||||
|
#include "od_id.h"
|
||||||
#include "od_msg.h"
|
#include "od_msg.h"
|
||||||
#include "od_system.h"
|
#include "od_system.h"
|
||||||
#include "od_instance.h"
|
#include "od_instance.h"
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
#include "od_scheme.h"
|
#include "od_scheme.h"
|
||||||
#include "od_lex.h"
|
#include "od_lex.h"
|
||||||
#include "od_config.h"
|
#include "od_config.h"
|
||||||
|
#include "od_id.h"
|
||||||
#include "od_system.h"
|
#include "od_system.h"
|
||||||
#include "od_server.h"
|
#include "od_server.h"
|
||||||
#include "od_server_pool.h"
|
#include "od_server_pool.h"
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
#include "od_scheme.h"
|
#include "od_scheme.h"
|
||||||
#include "od_lex.h"
|
#include "od_lex.h"
|
||||||
#include "od_config.h"
|
#include "od_config.h"
|
||||||
|
#include "od_id.h"
|
||||||
#include "od_msg.h"
|
#include "od_msg.h"
|
||||||
#include "od_system.h"
|
#include "od_system.h"
|
||||||
#include "od_instance.h"
|
#include "od_instance.h"
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
#include "od_scheme.h"
|
#include "od_scheme.h"
|
||||||
#include "od_lex.h"
|
#include "od_lex.h"
|
||||||
#include "od_config.h"
|
#include "od_config.h"
|
||||||
|
#include "od_id.h"
|
||||||
#include "od_system.h"
|
#include "od_system.h"
|
||||||
#include "od_server.h"
|
#include "od_server.h"
|
||||||
#include "od_server_pool.h"
|
#include "od_server_pool.h"
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
#include "od_scheme.h"
|
#include "od_scheme.h"
|
||||||
#include "od_lex.h"
|
#include "od_lex.h"
|
||||||
#include "od_config.h"
|
#include "od_config.h"
|
||||||
|
#include "od_id.h"
|
||||||
#include "od_msg.h"
|
#include "od_msg.h"
|
||||||
#include "od_system.h"
|
#include "od_system.h"
|
||||||
#include "od_instance.h"
|
#include "od_instance.h"
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
#include "od_scheme.h"
|
#include "od_scheme.h"
|
||||||
#include "od_lex.h"
|
#include "od_lex.h"
|
||||||
#include "od_config.h"
|
#include "od_config.h"
|
||||||
|
#include "od_id.h"
|
||||||
#include "od_instance.h"
|
#include "od_instance.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
|
|
Loading…
Reference in New Issue