#include "db_config.h"
#define __INCLUDE_NETWORKING 1
#define __INCLUDE_SELECT_H 1
#include "db_int.h"
#ifdef _POSIX_THREAD_ATTR_STACKSIZE
size_t __repmgr_guesstimated_max = (128 * 1024);
#endif
static int __repmgr_conn_work __P((ENV *,
REPMGR_CONNECTION *, fd_set *, fd_set *, int));
static int finish_connecting __P((ENV *, REPMGR_CONNECTION *));
int
__repmgr_thread_start(env, runnable)
ENV *env;
REPMGR_RUNNABLE *runnable;
{
pthread_attr_t *attrp;
#ifdef _POSIX_THREAD_ATTR_STACKSIZE
pthread_attr_t attributes;
size_t size;
int ret;
#endif
runnable->finished = FALSE;
#ifdef _POSIX_THREAD_ATTR_STACKSIZE
attrp = &attributes;
if ((ret = pthread_attr_init(&attributes)) != 0) {
__db_err(env,
ret, "pthread_attr_init in repmgr_thread_start");
return (ret);
}
size = __repmgr_guesstimated_max;
if (sizeof(size_t) > 4)
size *= 2;
#ifdef PTHREAD_STACK_MIN
if (size < PTHREAD_STACK_MIN)
size = PTHREAD_STACK_MIN;
#endif
if ((ret = pthread_attr_setstacksize(&attributes, size)) != 0) {
__db_err(env,
ret, "pthread_attr_setstacksize in repmgr_thread_start");
return (ret);
}
#else
attrp = NULL;
#endif
return (pthread_create(&runnable->thread_id, attrp,
runnable->run, env));
}
int
__repmgr_thread_join(thread)
REPMGR_RUNNABLE *thread;
{
return (pthread_join(thread->thread_id, NULL));
}
int
__repmgr_set_nonblocking(fd)
socket_t fd;
{
int flags;
if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
return (errno);
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)
return (errno);
return (0);
}
int
__repmgr_wake_waiting_senders(env)
ENV *env;
{
return (pthread_cond_broadcast(&env->rep_handle->ack_condition));
}
int
__repmgr_await_ack(env, lsnp)
ENV *env;
const DB_LSN *lsnp;
{
DB_REP *db_rep;
struct timespec deadline;
int ret, timed;
db_rep = env->rep_handle;
if ((timed = (db_rep->ack_timeout > 0)))
__repmgr_compute_wait_deadline(env, &deadline,
db_rep->ack_timeout);
else
COMPQUIET(deadline.tv_sec, 0);
while (!__repmgr_is_permanent(env, lsnp)) {
if (timed)
ret = pthread_cond_timedwait(&db_rep->ack_condition,
&db_rep->mutex, &deadline);
else
ret = pthread_cond_wait(&db_rep->ack_condition,
&db_rep->mutex);
if (db_rep->finished)
return (DB_REP_UNAVAIL);
if (ret != 0)
return (ret);
}
return (0);
}
void
__repmgr_compute_wait_deadline(env, result, wait)
ENV *env;
struct timespec *result;
db_timeout_t wait;
{
__os_gettime(env, (db_timespec *)result, 0);
TIMESPEC_ADD_DB_TIMEOUT(result, wait);
}
int
__repmgr_await_drain(env, conn, timeout)
ENV *env;
REPMGR_CONNECTION *conn;
db_timeout_t timeout;
{
DB_REP *db_rep;
struct timespec deadline;
int ret;
db_rep = env->rep_handle;
__repmgr_compute_wait_deadline(env, &deadline, timeout);
ret = 0;
while (conn->out_queue_length >= OUT_QUEUE_LIMIT) {
ret = pthread_cond_timedwait(&conn->drained,
&db_rep->mutex, &deadline);
switch (ret) {
case 0:
if (db_rep->finished)
goto out;
if (conn->state == CONN_DEFUNCT) {
ret = DB_REP_UNAVAIL;
goto out;
}
break;
case ETIMEDOUT:
conn->state = CONN_CONGESTED;
ret = 0;
goto out;
default:
goto out;
}
}
out:
return (ret);
}
int
__repmgr_alloc_cond(c)
cond_var_t *c;
{
return (pthread_cond_init(c, NULL));
}
int
__repmgr_free_cond(c)
cond_var_t *c;
{
return (pthread_cond_destroy(c));
}
int
__repmgr_init_sync(env, db_rep)
ENV *env;
DB_REP *db_rep;
{
int ret, mutex_inited, ack_inited, elect_inited, queue_inited,
file_desc[2];
COMPQUIET(env, NULL);
mutex_inited = ack_inited = elect_inited = queue_inited = FALSE;
if ((ret = pthread_mutex_init(&db_rep->mutex, NULL)) != 0)
goto err;
mutex_inited = TRUE;
if ((ret = pthread_cond_init(&db_rep->ack_condition, NULL)) != 0)
goto err;
ack_inited = TRUE;
if ((ret = pthread_cond_init(&db_rep->check_election, NULL)) != 0)
goto err;
elect_inited = TRUE;
if ((ret = pthread_cond_init(&db_rep->queue_nonempty, NULL)) != 0)
goto err;
queue_inited = TRUE;
if ((ret = pipe(file_desc)) == -1) {
ret = errno;
goto err;
}
db_rep->read_pipe = file_desc[0];
db_rep->write_pipe = file_desc[1];
return (0);
err:
if (queue_inited)
(void)pthread_cond_destroy(&db_rep->queue_nonempty);
if (elect_inited)
(void)pthread_cond_destroy(&db_rep->check_election);
if (ack_inited)
(void)pthread_cond_destroy(&db_rep->ack_condition);
if (mutex_inited)
(void)pthread_mutex_destroy(&db_rep->mutex);
db_rep->read_pipe = db_rep->write_pipe = -1;
return (ret);
}
int
__repmgr_close_sync(env)
ENV *env;
{
DB_REP *db_rep;
int ret, t_ret;
db_rep = env->rep_handle;
if (!(REPMGR_SYNC_INITED(db_rep)))
return (0);
ret = pthread_cond_destroy(&db_rep->queue_nonempty);
if ((t_ret = pthread_cond_destroy(&db_rep->check_election)) != 0 &&
ret == 0)
ret = t_ret;
if ((t_ret = pthread_cond_destroy(&db_rep->ack_condition)) != 0 &&
ret == 0)
ret = t_ret;
if ((t_ret = pthread_mutex_destroy(&db_rep->mutex)) != 0 &&
ret == 0)
ret = t_ret;
if (close(db_rep->read_pipe) == -1 && ret == 0)
ret = errno;
if (close(db_rep->write_pipe) == -1 && ret == 0)
ret = errno;
db_rep->read_pipe = db_rep->write_pipe = -1;
return (ret);
}
int
__repmgr_net_init(env, db_rep)
ENV *env;
DB_REP *db_rep;
{
int ret;
struct sigaction sigact;
if ((ret = __repmgr_listen(env)) != 0)
return (ret);
if (sigaction(SIGPIPE, NULL, &sigact) == -1) {
ret = errno;
__db_err(env, ret, "can't access signal handler");
goto err;
}
if ((db_rep->chg_sig_handler = (sigact.sa_handler == SIG_DFL))) {
sigact.sa_handler = SIG_IGN;
sigact.sa_flags = 0;
if (sigaction(SIGPIPE, &sigact, NULL) == -1) {
ret = errno;
__db_err(env, ret, "can't access signal handler");
goto err;
}
}
return (0);
err:
(void)closesocket(db_rep->listen_fd);
db_rep->listen_fd = INVALID_SOCKET;
return (ret);
}
int
__repmgr_lock_mutex(mutex)
mgr_mutex_t *mutex;
{
return (pthread_mutex_lock(mutex));
}
int
__repmgr_unlock_mutex(mutex)
mgr_mutex_t *mutex;
{
return (pthread_mutex_unlock(mutex));
}
int
__repmgr_signal(v)
cond_var_t *v;
{
return (pthread_cond_broadcast(v));
}
int
__repmgr_wake_main_thread(env)
ENV *env;
{
DB_REP *db_rep;
u_int8_t any_value;
COMPQUIET(any_value, 0);
db_rep = env->rep_handle;
if (write(db_rep->write_pipe, &any_value, 1) == -1)
return (errno);
return (0);
}
int
__repmgr_writev(fd, iovec, buf_count, byte_count_p)
socket_t fd;
db_iovec_t *iovec;
int buf_count;
size_t *byte_count_p;
{
int nw;
if ((nw = writev(fd, iovec, buf_count)) == -1)
return (errno);
*byte_count_p = (size_t)nw;
return (0);
}
int
__repmgr_readv(fd, iovec, buf_count, byte_count_p)
socket_t fd;
db_iovec_t *iovec;
int buf_count;
size_t *byte_count_p;
{
ssize_t nw;
if ((nw = readv(fd, iovec, buf_count)) == -1)
return (errno);
*byte_count_p = (size_t)nw;
return (0);
}
int
__repmgr_select_loop(env)
ENV *env;
{
struct timeval select_timeout, *select_timeout_p;
DB_REP *db_rep;
REPMGR_CONNECTION *conn, *next;
db_timespec timeout;
fd_set reads, writes;
int ret, flow_control, maxfd;
u_int8_t buf[10];
flow_control = FALSE;
db_rep = env->rep_handle;
LOCK_MUTEX(db_rep->mutex);
if ((ret = __repmgr_first_try_connections(env)) != 0)
goto out;
for (;;) {
FD_ZERO(&reads);
FD_ZERO(&writes);
FD_SET((u_int)db_rep->listen_fd, &reads);
maxfd = db_rep->listen_fd;
FD_SET((u_int)db_rep->read_pipe, &reads);
if (db_rep->read_pipe > maxfd)
maxfd = db_rep->read_pipe;
for (conn = TAILQ_FIRST(&db_rep->connections);
conn != NULL;
conn = next) {
next = TAILQ_NEXT(conn, entries);
if (conn->state == CONN_DEFUNCT) {
if ((ret = __repmgr_cleanup_connection(env,
conn)) != 0)
goto out;
continue;
}
if (conn->state == CONN_CONNECTING) {
FD_SET((u_int)conn->fd, &reads);
FD_SET((u_int)conn->fd, &writes);
if (conn->fd > maxfd)
maxfd = conn->fd;
continue;
}
if (!STAILQ_EMPTY(&conn->outbound_queue)) {
FD_SET((u_int)conn->fd, &writes);
if (conn->fd > maxfd)
maxfd = conn->fd;
}
if (!flow_control || !IS_VALID_EID(conn->eid)) {
FD_SET((u_int)conn->fd, &reads);
if (conn->fd > maxfd)
maxfd = conn->fd;
}
}
if (__repmgr_compute_timeout(env, &timeout)) {
select_timeout.tv_sec = timeout.tv_sec;
select_timeout.tv_usec = timeout.tv_nsec / NS_PER_US;
select_timeout_p = &select_timeout;
} else {
select_timeout_p = NULL;
}
UNLOCK_MUTEX(db_rep->mutex);
if ((ret = select(maxfd + 1,
&reads, &writes, NULL, select_timeout_p)) == -1) {
switch (ret = errno) {
case EINTR:
case EWOULDBLOCK:
LOCK_MUTEX(db_rep->mutex);
continue;
default:
__db_err(env, ret, "select");
return (ret);
}
}
LOCK_MUTEX(db_rep->mutex);
if ((ret = __repmgr_check_timeouts(env)) != 0)
goto out;
TAILQ_FOREACH(conn, &db_rep->connections, entries) {
if (conn->state == CONN_DEFUNCT)
continue;
if ((ret = __repmgr_conn_work(env,
conn, &reads, &writes, flow_control)) != 0)
goto out;
}
if (FD_ISSET((u_int)db_rep->read_pipe, &reads)) {
if (read(db_rep->read_pipe, buf, sizeof(buf)) <= 0) {
ret = errno;
goto out;
} else if (db_rep->finished) {
ret = 0;
goto out;
}
}
if (FD_ISSET((u_int)db_rep->listen_fd, &reads) &&
(ret = __repmgr_accept(env)) != 0)
goto out;
}
out:
UNLOCK_MUTEX(db_rep->mutex);
return (ret);
}
static int
__repmgr_conn_work(env, conn, reads, writes, flow_control)
ENV *env;
REPMGR_CONNECTION *conn;
fd_set *reads, *writes;
int flow_control;
{
int ret;
u_int fd;
ret = 0;
fd = (u_int)conn->fd;
if (conn->state == CONN_CONNECTING) {
if (FD_ISSET(fd, reads) || FD_ISSET(fd, writes))
ret = finish_connecting(env, conn);
} else {
if (FD_ISSET(fd, writes))
ret = __repmgr_write_some(env, conn);
if (ret == 0 && !flow_control && FD_ISSET(fd, reads))
ret = __repmgr_read_from_site(env, conn);
}
if (ret == DB_REP_UNAVAIL)
ret = __repmgr_bust_connection(env, conn);
return (ret);
}
static int
finish_connecting(env, conn)
ENV *env;
REPMGR_CONNECTION *conn;
{
DB_REP *db_rep;
REPMGR_SITE *site;
socklen_t len;
SITE_STRING_BUFFER buffer;
u_int eid;
int error, ret;
len = sizeof(error);
if (getsockopt(
conn->fd, SOL_SOCKET, SO_ERROR, (sockopt_t)&error, &len) < 0)
goto err_rpt;
if (error) {
errno = error;
goto err_rpt;
}
conn->state = CONN_CONNECTED;
return (__repmgr_propose_version(env, conn));
err_rpt:
db_rep = env->rep_handle;
DB_ASSERT(env, IS_VALID_EID(conn->eid));
eid = (u_int)conn->eid;
site = SITE_FROM_EID(eid);
__db_err(env, errno,
"connecting to %s", __repmgr_format_site_loc(site, buffer));
if (ADDR_LIST_NEXT(&site->net_addr) == NULL) {
STAT(db_rep->region->mstat.st_connect_fail++);
return (DB_REP_UNAVAIL);
}
DISABLE_CONNECTION(conn);
ret = __repmgr_connect_site(env, eid);
DB_ASSERT(env, ret != DB_REP_UNAVAIL);
return (ret);
}