| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2007, OmniTI Computer Consulting, Inc. |
|---|
| 3 |
* All rights reserved. |
|---|
| 4 |
*/ |
|---|
| 5 |
|
|---|
| 6 |
#include "noit_defines.h" |
|---|
| 7 |
#include "eventer/eventer.h" |
|---|
| 8 |
#include "noit_listener.h" |
|---|
| 9 |
#include "utils/noit_hash.h" |
|---|
| 10 |
#include "utils/noit_log.h" |
|---|
| 11 |
#include "jlog/jlog.h" |
|---|
| 12 |
#include "noit_jlog_listener.h" |
|---|
| 13 |
|
|---|
| 14 |
#include <unistd.h> |
|---|
| 15 |
#include <sys/ioctl.h> |
|---|
| 16 |
|
|---|
| 17 |
void |
|---|
| 18 |
noit_jlog_listener_init() { |
|---|
| 19 |
eventer_name_callback("log_transit", noit_jlog_handler); |
|---|
| 20 |
} |
|---|
| 21 |
|
|---|
| 22 |
typedef struct { |
|---|
| 23 |
jlog_ctx *jlog; |
|---|
| 24 |
jlog_id chkpt; |
|---|
| 25 |
jlog_id start; |
|---|
| 26 |
jlog_id finish; |
|---|
| 27 |
int count; |
|---|
| 28 |
int wants_shutdown; |
|---|
| 29 |
} noit_jlog_closure_t; |
|---|
| 30 |
|
|---|
| 31 |
noit_jlog_closure_t * |
|---|
| 32 |
noit_jlog_closure_alloc(void) { |
|---|
| 33 |
noit_jlog_closure_t *jcl; |
|---|
| 34 |
jcl = calloc(1, sizeof(*jcl)); |
|---|
| 35 |
return jcl; |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
void |
|---|
| 39 |
noit_jlog_closure_free(noit_jlog_closure_t *jcl) { |
|---|
| 40 |
if(jcl->jlog) jlog_ctx_close(jcl->jlog); |
|---|
| 41 |
free(jcl); |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
#define Ewrite(a,b) e->opset->write(e->fd, a, b, &mask, e) |
|---|
| 45 |
static int |
|---|
| 46 |
noit_jlog_push(eventer_t e, noit_jlog_closure_t *jcl) { |
|---|
| 47 |
jlog_message msg; |
|---|
| 48 |
int mask; |
|---|
| 49 |
u_int32_t n_count; |
|---|
| 50 |
n_count = htonl(jcl->count); |
|---|
| 51 |
if(Ewrite(&n_count, sizeof(n_count)) != sizeof(n_count)) |
|---|
| 52 |
return -1; |
|---|
| 53 |
while(jcl->count > 0) { |
|---|
| 54 |
struct { u_int32_t n_sec, n_usec, n_len; } payload; |
|---|
| 55 |
if(jlog_ctx_read_message(jcl->jlog, &jcl->start, &msg) == -1) |
|---|
| 56 |
return -1; |
|---|
| 57 |
|
|---|
| 58 |
/* Here we actually push the message */ |
|---|
| 59 |
payload.n_sec = htonl(msg.header->tv_sec); |
|---|
| 60 |
payload.n_usec = htonl(msg.header->tv_usec); |
|---|
| 61 |
payload.n_len = htonl(msg.mess_len); |
|---|
| 62 |
if(Ewrite(&payload, sizeof(payload)) != sizeof(payload)) |
|---|
| 63 |
return -1; |
|---|
| 64 |
if(Ewrite(msg.mess, msg.mess_len) != msg.mess_len) |
|---|
| 65 |
return -1; |
|---|
| 66 |
/* Note what the client must checkpoint */ |
|---|
| 67 |
jcl->chkpt = jcl->start; |
|---|
| 68 |
|
|---|
| 69 |
JLOG_ID_ADVANCE(&jcl->start); |
|---|
| 70 |
jcl->count--; |
|---|
| 71 |
} |
|---|
| 72 |
return 0; |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
void * |
|---|
| 76 |
noit_jlog_thread_main(void *e_vptr) { |
|---|
| 77 |
int mask, bytes_read; |
|---|
| 78 |
eventer_t e = e_vptr; |
|---|
| 79 |
acceptor_closure_t *ac = e->closure; |
|---|
| 80 |
noit_jlog_closure_t *jcl = ac->service_ctx; |
|---|
| 81 |
long off = 0; |
|---|
| 82 |
char inbuff[sizeof(jlog_id)]; |
|---|
| 83 |
|
|---|
| 84 |
/* Go into blocking mode */ |
|---|
| 85 |
ioctl(e->fd, FIONBIO, &off); |
|---|
| 86 |
|
|---|
| 87 |
while(1) { |
|---|
| 88 |
jlog_id client_chkpt; |
|---|
| 89 |
jlog_get_checkpoint(jcl->jlog, ac->remote_cn, &jcl->chkpt); |
|---|
| 90 |
jcl->count = jlog_ctx_read_interval(jcl->jlog, &jcl->start, &jcl->finish); |
|---|
| 91 |
if(jcl->count > 0) { |
|---|
| 92 |
if(noit_jlog_push(e, jcl)) { |
|---|
| 93 |
goto alldone; |
|---|
| 94 |
} |
|---|
| 95 |
/* Read our jlog_id accounting for possibly short reads */ |
|---|
| 96 |
bytes_read = 0; |
|---|
| 97 |
while(bytes_read < sizeof(jlog_id)) { |
|---|
| 98 |
int len; |
|---|
| 99 |
if((len = e->opset->read(e->fd, inbuff + bytes_read, |
|---|
| 100 |
sizeof(jlog_id) - bytes_read, |
|---|
| 101 |
&mask, e)) <= 0) |
|---|
| 102 |
goto alldone; |
|---|
| 103 |
bytes_read += len; |
|---|
| 104 |
} |
|---|
| 105 |
memcpy(&client_chkpt, inbuff, sizeof(jlog_id)); |
|---|
| 106 |
/* Fix the endian */ |
|---|
| 107 |
client_chkpt.log = ntohl(client_chkpt.log); |
|---|
| 108 |
client_chkpt.marker = ntohl(client_chkpt.marker); |
|---|
| 109 |
|
|---|
| 110 |
if(memcmp(&jcl->chkpt, &client_chkpt, sizeof(jlog_id))) { |
|---|
| 111 |
noitL(noit_error, |
|---|
| 112 |
"client %s submitted invalid checkpoint %u:%u expected %u:%u\n", |
|---|
| 113 |
ac->remote_cn, client_chkpt.log, client_chkpt.marker, |
|---|
| 114 |
jcl->chkpt.log, jcl->chkpt.marker); |
|---|
| 115 |
goto alldone; |
|---|
| 116 |
} |
|---|
| 117 |
jlog_ctx_read_checkpoint(jcl->jlog, &jcl->chkpt); |
|---|
| 118 |
} |
|---|
| 119 |
sleep(5); |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
alldone: |
|---|
| 123 |
e->opset->close(e->fd, &mask, e); |
|---|
| 124 |
if(jcl) noit_jlog_closure_free(jcl); |
|---|
| 125 |
if(ac) acceptor_closure_free(ac); |
|---|
| 126 |
return NULL; |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
int |
|---|
| 130 |
noit_jlog_handler(eventer_t e, int mask, void *closure, |
|---|
| 131 |
struct timeval *now) { |
|---|
| 132 |
eventer_t newe; |
|---|
| 133 |
pthread_t tid; |
|---|
| 134 |
int newmask = EVENTER_READ | EVENTER_EXCEPTION; |
|---|
| 135 |
acceptor_closure_t *ac = closure; |
|---|
| 136 |
noit_jlog_closure_t *jcl = ac->service_ctx; |
|---|
| 137 |
|
|---|
| 138 |
if(mask & EVENTER_EXCEPTION || (jcl && jcl->wants_shutdown)) { |
|---|
| 139 |
socket_error: |
|---|
| 140 |
/* Exceptions cause us to simply snip the connection */ |
|---|
| 141 |
eventer_remove_fd(e->fd); |
|---|
| 142 |
e->opset->close(e->fd, &newmask, e); |
|---|
| 143 |
if(jcl) noit_jlog_closure_free(jcl); |
|---|
| 144 |
if(ac) acceptor_closure_free(ac); |
|---|
| 145 |
return 0; |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
if(!ac->service_ctx) { |
|---|
| 149 |
noit_log_stream_t ls; |
|---|
| 150 |
const char *logname; |
|---|
| 151 |
jcl = ac->service_ctx = noit_jlog_closure_alloc(); |
|---|
| 152 |
if(!noit_hash_retrieve(ac->config, "log", strlen("log"), |
|---|
| 153 |
(void **)&logname)) { |
|---|
| 154 |
noitL(noit_error, "No 'log' specified in log_transit.\n"); |
|---|
| 155 |
goto socket_error; |
|---|
| 156 |
} |
|---|
| 157 |
ls = noit_log_stream_find(logname); |
|---|
| 158 |
if(!ls) { |
|---|
| 159 |
noitL(noit_error, "Could not find log '%s' for log_transit.\n", |
|---|
| 160 |
logname); |
|---|
| 161 |
goto socket_error; |
|---|
| 162 |
} |
|---|
| 163 |
if(!ls->type || strcmp(ls->type, "jlog")) { |
|---|
| 164 |
noitL(noit_error, "Log '%s' for log_transit is not a jlog.\n", |
|---|
| 165 |
logname); |
|---|
| 166 |
goto socket_error; |
|---|
| 167 |
} |
|---|
| 168 |
if(!ac->remote_cn) { |
|---|
| 169 |
noitL(noit_error, "jlog transit started to unidentified party.\n"); |
|---|
| 170 |
goto socket_error; |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
jcl->jlog = jlog_new(ls->path); |
|---|
| 174 |
if(jlog_ctx_open_reader(jcl->jlog, ac->remote_cn) == -1) { |
|---|
| 175 |
noitL(noit_error, "jlog reader[%s] error: %s\n", ac->remote_cn, |
|---|
| 176 |
jlog_ctx_err_string(jcl->jlog)); |
|---|
| 177 |
goto socket_error; |
|---|
| 178 |
} |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
/* The jlog stuff is disk I/O and can block us. |
|---|
| 182 |
* We'll create a new thread to just handle this connection. |
|---|
| 183 |
*/ |
|---|
| 184 |
eventer_remove_fd(e->fd); |
|---|
| 185 |
newe = eventer_alloc(); |
|---|
| 186 |
memcpy(newe, e, sizeof(*e)); |
|---|
| 187 |
if(pthread_create(&tid, NULL, noit_jlog_thread_main, newe) == 0) { |
|---|
| 188 |
return 0; |
|---|
| 189 |
} |
|---|
| 190 |
|
|---|
| 191 |
/* Undo our dup */ |
|---|
| 192 |
eventer_free(newe); |
|---|
| 193 |
/* Creating the thread failed, close it down and deschedule. */ |
|---|
| 194 |
e->opset->close(e->fd, &newmask, e); |
|---|
| 195 |
return 0; |
|---|
| 196 |
} |
|---|