| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2007, OmniTI Computer Consulting, Inc. |
|---|
| 3 |
* All rights reserved. |
|---|
| 4 |
* |
|---|
| 5 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 6 |
* modification, are permitted provided that the following conditions are |
|---|
| 7 |
* met: |
|---|
| 8 |
* |
|---|
| 9 |
* * Redistributions of source code must retain the above copyright |
|---|
| 10 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 11 |
* * Redistributions in binary form must reproduce the above |
|---|
| 12 |
* copyright notice, this list of conditions and the following |
|---|
| 13 |
* disclaimer in the documentation and/or other materials provided |
|---|
| 14 |
* with the distribution. |
|---|
| 15 |
* * Neither the name OmniTI Computer Consulting, Inc. nor the names |
|---|
| 16 |
* of its contributors may be used to endorse or promote products |
|---|
| 17 |
* derived from this software without specific prior written |
|---|
| 18 |
* permission. |
|---|
| 19 |
* |
|---|
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 31 |
*/ |
|---|
| 32 |
|
|---|
| 33 |
#include "noit_defines.h" |
|---|
| 34 |
#include "eventer/eventer.h" |
|---|
| 35 |
#include "utils/noit_log.h" |
|---|
| 36 |
#include "utils/noit_b64.h" |
|---|
| 37 |
#include "noit_jlog_listener.h" |
|---|
| 38 |
#include "stratcon_jlog_streamer.h" |
|---|
| 39 |
#include "stratcon_datastore.h" |
|---|
| 40 |
#include "stratcon_iep.h" |
|---|
| 41 |
#include "noit_conf.h" |
|---|
| 42 |
#include "noit_check.h" |
|---|
| 43 |
|
|---|
| 44 |
#include <sys/types.h> |
|---|
| 45 |
#ifdef HAVE_SYS_WAIT_H |
|---|
| 46 |
#include <sys/wait.h> |
|---|
| 47 |
#endif |
|---|
| 48 |
#include <sys/stat.h> |
|---|
| 49 |
#include <fcntl.h> |
|---|
| 50 |
#include <unistd.h> |
|---|
| 51 |
#include <sys/fcntl.h> |
|---|
| 52 |
#ifdef HAVE_SYS_FILIO_H |
|---|
| 53 |
#include <sys/filio.h> |
|---|
| 54 |
#endif |
|---|
| 55 |
#include <signal.h> |
|---|
| 56 |
#include <errno.h> |
|---|
| 57 |
#include <assert.h> |
|---|
| 58 |
|
|---|
| 59 |
eventer_jobq_t iep_jobq; |
|---|
| 60 |
static noit_log_stream_t noit_iep = NULL; |
|---|
| 61 |
static noit_spinlock_t iep_conn_cnt = 0; |
|---|
| 62 |
|
|---|
| 63 |
static pthread_key_t iep_connection; |
|---|
| 64 |
static noit_hash_table mq_drivers = NOIT_HASH_EMPTY; |
|---|
| 65 |
static mq_driver_t *mq_driver = NULL; |
|---|
| 66 |
|
|---|
| 67 |
static int iep_system_enabled = 1; |
|---|
| 68 |
int stratcon_iep_get_enabled() { return iep_system_enabled; } |
|---|
| 69 |
void stratcon_iep_set_enabled(int n) { iep_system_enabled = n; } |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
struct iep_job_closure { |
|---|
| 73 |
char *line; /* This is a copy and gets trashed during processing */ |
|---|
| 74 |
char *remote; |
|---|
| 75 |
char *doc_str; |
|---|
| 76 |
}; |
|---|
| 77 |
|
|---|
| 78 |
static void |
|---|
| 79 |
start_iep_daemon(); |
|---|
| 80 |
|
|---|
| 81 |
static double |
|---|
| 82 |
stratcon_iep_age_from_line(char *data, struct timeval now) { |
|---|
| 83 |
double n, t; |
|---|
| 84 |
if(data && (*data == 'S' || *data == 'M')) { |
|---|
| 85 |
if(data[1] != '\t') return 0; |
|---|
| 86 |
t = strtod(data + 2, NULL); |
|---|
| 87 |
n = (float)now.tv_sec + (float)now.tv_usec / 1000000.0; |
|---|
| 88 |
return n - t; |
|---|
| 89 |
} |
|---|
| 90 |
return 0; |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
struct statement_node { |
|---|
| 94 |
char *id; |
|---|
| 95 |
char *statement; |
|---|
| 96 |
char *provides; |
|---|
| 97 |
int marked; /* helps with identifying cycles */ |
|---|
| 98 |
int nrequires; |
|---|
| 99 |
struct statement_node **requires; |
|---|
| 100 |
}; |
|---|
| 101 |
static void |
|---|
| 102 |
statement_node_free(void *vstmt) { |
|---|
| 103 |
struct statement_node *stmt = vstmt; |
|---|
| 104 |
if(stmt->id) free(stmt->id); |
|---|
| 105 |
if(stmt->statement) free(stmt->statement); |
|---|
| 106 |
if(stmt->provides) free(stmt->provides); |
|---|
| 107 |
if(stmt->requires) free(stmt->requires); |
|---|
| 108 |
} |
|---|
| 109 |
static int |
|---|
| 110 |
stmt_mark_dag(struct statement_node *stmt, int mgen) { |
|---|
| 111 |
int i; |
|---|
| 112 |
assert(stmt->marked <= mgen); |
|---|
| 113 |
if(stmt->marked == mgen) return -1; |
|---|
| 114 |
if(stmt->marked > 0) return 0; /* validated in a previous sweep */ |
|---|
| 115 |
stmt->marked = mgen; |
|---|
| 116 |
for(i=0; i<stmt->nrequires; i++) |
|---|
| 117 |
if(stmt_mark_dag(stmt->requires[i], mgen) < 0) return -1; |
|---|
| 118 |
return 0; |
|---|
| 119 |
} |
|---|
| 120 |
static void |
|---|
| 121 |
submit_statement_node(struct statement_node *stmt) { |
|---|
| 122 |
int line_len, i; |
|---|
| 123 |
char *line, *cp; |
|---|
| 124 |
|
|---|
| 125 |
if(stmt->marked) return; |
|---|
| 126 |
for(i=0; i<stmt->nrequires; i++) |
|---|
| 127 |
submit_statement_node(stmt->requires[i]); |
|---|
| 128 |
|
|---|
| 129 |
line_len = 3 /* 2 tabs + \0 */ + |
|---|
| 130 |
1 /* 'D' */ + 1 /* '\n' */ + |
|---|
| 131 |
strlen(stmt->id) + strlen(stmt->statement); |
|---|
| 132 |
line = malloc(line_len); |
|---|
| 133 |
snprintf(line, line_len, "D\t%s\t%s\n", stmt->id, stmt->statement); |
|---|
| 134 |
cp = line; |
|---|
| 135 |
while(cp[0] && cp[1]) { |
|---|
| 136 |
if(*cp == '\n') *cp = ' '; |
|---|
| 137 |
cp++; |
|---|
| 138 |
} |
|---|
| 139 |
noitL(noit_error, "submitting statement: %s\n", line); |
|---|
| 140 |
stratcon_iep_line_processor(DS_OP_INSERT, NULL, NULL, line, NULL); |
|---|
| 141 |
stmt->marked = 1; |
|---|
| 142 |
} |
|---|
| 143 |
void stratcon_iep_submit_statements() { |
|---|
| 144 |
int i, cnt = 0; |
|---|
| 145 |
noit_conf_section_t *statement_configs; |
|---|
| 146 |
char path[256]; |
|---|
| 147 |
struct statement_node *stmt; |
|---|
| 148 |
void *vstmt; |
|---|
| 149 |
noit_hash_table stmt_by_id = NOIT_HASH_EMPTY; |
|---|
| 150 |
noit_hash_table stmt_by_provider = NOIT_HASH_EMPTY; |
|---|
| 151 |
noit_hash_iter iter = NOIT_HASH_ITER_ZERO; |
|---|
| 152 |
const char *key; |
|---|
| 153 |
int klen, mgen = 0; |
|---|
| 154 |
|
|---|
| 155 |
snprintf(path, sizeof(path), "/stratcon/iep/queries[@master=\"stratcond\"]//statement"); |
|---|
| 156 |
statement_configs = noit_conf_get_sections(NULL, path, &cnt); |
|---|
| 157 |
noitL(noit_debug, "Found %d %s stanzas\n", cnt, path); |
|---|
| 158 |
|
|---|
| 159 |
/* Phase 1: sweep in all the statements */ |
|---|
| 160 |
for(i=0; i<cnt; i++) { |
|---|
| 161 |
char id[UUID_STR_LEN+1]; |
|---|
| 162 |
char provides[256]; |
|---|
| 163 |
char *statement; |
|---|
| 164 |
|
|---|
| 165 |
if(!noit_conf_get_stringbuf(statement_configs[i], |
|---|
| 166 |
"self::node()/@id", |
|---|
| 167 |
id, sizeof(id))) { |
|---|
| 168 |
noitL(noit_iep, "No uuid specified in query\n"); |
|---|
| 169 |
continue; |
|---|
| 170 |
} |
|---|
| 171 |
if(!noit_conf_get_stringbuf(statement_configs[i], |
|---|
| 172 |
"ancestor-or-self::node()/@provides", |
|---|
| 173 |
provides, sizeof(provides))) { |
|---|
| 174 |
provides[0] = '\0'; |
|---|
| 175 |
} |
|---|
| 176 |
if(!noit_conf_get_string(statement_configs[i], "self::node()/epl", |
|---|
| 177 |
&statement)) { |
|---|
| 178 |
noitL(noit_iep, "No contents specified in statement\n"); |
|---|
| 179 |
continue; |
|---|
| 180 |
} |
|---|
| 181 |
stmt = calloc(1, sizeof(*stmt)); |
|---|
| 182 |
stmt->id = strdup(id); |
|---|
| 183 |
stmt->statement = statement; |
|---|
| 184 |
stmt->provides = provides[0] ? strdup(provides) : NULL; |
|---|
| 185 |
if(!noit_hash_store(&stmt_by_id, stmt->id, strlen(stmt->id), stmt)) { |
|---|
| 186 |
noitL(noit_error, "Duplicate statement id: %s\n", stmt->id); |
|---|
| 187 |
exit(-1); |
|---|
| 188 |
} |
|---|
| 189 |
if(stmt->provides) { |
|---|
| 190 |
if(!noit_hash_store(&stmt_by_provider, stmt->provides, |
|---|
| 191 |
strlen(stmt->provides), stmt)) { |
|---|
| 192 |
noitL(noit_error, "Two statements provide: '%s'\n", stmt->provides); |
|---|
| 193 |
exit(-1); |
|---|
| 194 |
} |
|---|
| 195 |
} |
|---|
| 196 |
} |
|---|
| 197 |
|
|---|
| 198 |
/* Phase 2: load the requires graph */ |
|---|
| 199 |
for(i=0; i<cnt; i++) { |
|---|
| 200 |
char id[UUID_STR_LEN+1]; |
|---|
| 201 |
int rcnt, j; |
|---|
| 202 |
char *requires; |
|---|
| 203 |
noit_conf_section_t *reqs; |
|---|
| 204 |
|
|---|
| 205 |
if(!noit_conf_get_stringbuf(statement_configs[i], |
|---|
| 206 |
"self::node()/@id", |
|---|
| 207 |
id, sizeof(id))) { |
|---|
| 208 |
noitL(noit_iep, "No uuid specified in query\n"); |
|---|
| 209 |
continue; |
|---|
| 210 |
} |
|---|
| 211 |
if(!noit_hash_retrieve(&stmt_by_id, id, strlen(id), &vstmt)) { |
|---|
| 212 |
noitL(noit_error, "Cannot find statement: %s\n", id); |
|---|
| 213 |
exit(-1); |
|---|
| 214 |
} |
|---|
| 215 |
stmt = vstmt; |
|---|
| 216 |
reqs = noit_conf_get_sections(statement_configs[i], |
|---|
| 217 |
"self::node()/requires", &rcnt); |
|---|
| 218 |
if(rcnt > 0) { |
|---|
| 219 |
stmt->requires = malloc(rcnt * sizeof(*(stmt->requires))); |
|---|
| 220 |
for(j=0; j<rcnt; j++) { |
|---|
| 221 |
void *vrstmt; |
|---|
| 222 |
if(!noit_conf_get_string(reqs[j], "self::node()", |
|---|
| 223 |
&requires) || requires[0] == '\0') { |
|---|
| 224 |
continue; |
|---|
| 225 |
} |
|---|
| 226 |
if(!noit_hash_retrieve(&stmt_by_provider, requires, strlen(requires), |
|---|
| 227 |
&vrstmt)) { |
|---|
| 228 |
noitL(noit_error, |
|---|
| 229 |
"Statement %s requires %s which no one provides.\n", |
|---|
| 230 |
stmt->id, requires); |
|---|
| 231 |
exit(-1); |
|---|
| 232 |
} |
|---|
| 233 |
stmt->requires[stmt->nrequires++] = vrstmt; |
|---|
| 234 |
} |
|---|
| 235 |
} |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
/* Phase 3: Recursive sweep and mark to detect cycles. |
|---|
| 239 |
We're walking the graph backwards here from dependent to provider, |
|---|
| 240 |
but a cycle is a cycle, so this validates the graph. */ |
|---|
| 241 |
while(noit_hash_next(&stmt_by_id, &iter, &key, &klen, &vstmt)) { |
|---|
| 242 |
stmt = vstmt; |
|---|
| 243 |
if(stmt_mark_dag(stmt, ++mgen) < 0) { |
|---|
| 244 |
noitL(noit_error, "Statement %s has a cyclic requirement\n", stmt->id); |
|---|
| 245 |
exit(-1); |
|---|
| 246 |
} |
|---|
| 247 |
} |
|---|
| 248 |
|
|---|
| 249 |
/* Phase 4: clean the markings */ |
|---|
| 250 |
memset(&iter, 0, sizeof(iter)); |
|---|
| 251 |
while(noit_hash_next(&stmt_by_id, &iter, &key, &klen, &vstmt)) { |
|---|
| 252 |
stmt = vstmt; |
|---|
| 253 |
stmt->marked = 0; |
|---|
| 254 |
} |
|---|
| 255 |
|
|---|
| 256 |
/* Phase 5: do the load */ |
|---|
| 257 |
memset(&iter, 0, sizeof(iter)); |
|---|
| 258 |
while(noit_hash_next(&stmt_by_id, &iter, &key, &klen, &vstmt)) { |
|---|
| 259 |
stmt = vstmt; |
|---|
| 260 |
submit_statement_node(stmt); |
|---|
| 261 |
} |
|---|
| 262 |
|
|---|
| 263 |
noit_hash_destroy(&stmt_by_provider, NULL, NULL); |
|---|
| 264 |
noit_hash_destroy(&stmt_by_id, NULL, statement_node_free); |
|---|
| 265 |
free(statement_configs); |
|---|
| 266 |
} |
|---|
| 267 |
|
|---|
| 268 |
void stratcon_iep_submit_queries() { |
|---|
| 269 |
int i, cnt = 0; |
|---|
| 270 |
noit_conf_section_t *query_configs; |
|---|
| 271 |
char path[256]; |
|---|
| 272 |
|
|---|
| 273 |
snprintf(path, sizeof(path), "/stratcon/iep/queries[@master=\"stratcond\"]//query"); |
|---|
| 274 |
query_configs = noit_conf_get_sections(NULL, path, &cnt); |
|---|
| 275 |
noitL(noit_debug, "Found %d %s stanzas\n", cnt, path); |
|---|
| 276 |
for(i=0; i<cnt; i++) { |
|---|
| 277 |
char id[UUID_STR_LEN+1]; |
|---|
| 278 |
char topic[256]; |
|---|
| 279 |
char *query; |
|---|
| 280 |
char *line; |
|---|
| 281 |
int line_len; |
|---|
| 282 |
|
|---|
| 283 |
if(!noit_conf_get_stringbuf(query_configs[i], |
|---|
| 284 |
"self::node()/@id", |
|---|
| 285 |
id, sizeof(id))) { |
|---|
| 286 |
noitL(noit_iep, "No uuid specified in query\n"); |
|---|
| 287 |
continue; |
|---|
| 288 |
} |
|---|
| 289 |
if(!noit_conf_get_stringbuf(query_configs[i], |
|---|
| 290 |
"ancestor-or-self::node()/@topic", |
|---|
| 291 |
topic, sizeof(topic))) { |
|---|
| 292 |
noitL(noit_iep, "No topic specified in query\n"); |
|---|
| 293 |
continue; |
|---|
| 294 |
} |
|---|
| 295 |
if(!noit_conf_get_string(query_configs[i], "self::node()/epl", |
|---|
| 296 |
&query)) { |
|---|
| 297 |
noitL(noit_iep, "No contents specified in query\n"); |
|---|
| 298 |
continue; |
|---|
| 299 |
} |
|---|
| 300 |
line_len = 4 /* 3 tabs + \0 */ + |
|---|
| 301 |
1 /* 'Q' */ + 1 /* '\n' */ + |
|---|
| 302 |
strlen(id) + strlen(topic) + strlen(query); |
|---|
| 303 |
line = malloc(line_len); |
|---|
| 304 |
snprintf(line, line_len, "Q\t%s\t%s\t%s\n", id, topic, query); |
|---|
| 305 |
free(query); |
|---|
| 306 |
query = line; |
|---|
| 307 |
while(query[0] && query[1]) { |
|---|
| 308 |
if(*query == '\n') *query = ' '; |
|---|
| 309 |
query++; |
|---|
| 310 |
} |
|---|
| 311 |
stratcon_iep_line_processor(DS_OP_INSERT, NULL, NULL, line, NULL); |
|---|
| 312 |
} |
|---|
| 313 |
free(query_configs); |
|---|
| 314 |
} |
|---|
| 315 |
|
|---|
| 316 |
static |
|---|
| 317 |
struct iep_thread_driver *stratcon_iep_get_connection() { |
|---|
| 318 |
int rc; |
|---|
| 319 |
struct iep_thread_driver *driver; |
|---|
| 320 |
driver = pthread_getspecific(iep_connection); |
|---|
| 321 |
if(!driver) { |
|---|
| 322 |
driver = mq_driver->allocate(); |
|---|
| 323 |
pthread_setspecific(iep_connection, driver); |
|---|
| 324 |
} |
|---|
| 325 |
|
|---|
| 326 |
rc = mq_driver->connect(driver); |
|---|
| 327 |
if(rc < 0) return NULL; |
|---|
| 328 |
if(rc == 0) { |
|---|
| 329 |
/* Initial connect */ |
|---|
| 330 |
/* TODO: this should be requested by Esper, not blindly pushed */ |
|---|
| 331 |
stratcon_iep_submit_statements(); |
|---|
| 332 |
stratcon_datastore_iep_check_preload(); |
|---|
| 333 |
stratcon_iep_submit_queries(); |
|---|
| 334 |
} |
|---|
| 335 |
|
|---|
| 336 |
return driver; |
|---|
| 337 |
} |
|---|
| 338 |
|
|---|
| 339 |
static int |
|---|
| 340 |
setup_iep_connection_callback(eventer_t e, int mask, void *closure, |
|---|
| 341 |
struct timeval *now) { |
|---|
| 342 |
noit_spinlock_unlock(&iep_conn_cnt); |
|---|
| 343 |
stratcon_iep_line_processor(DS_OP_INSERT, NULL, NULL, NULL, NULL); |
|---|
| 344 |
return 0; |
|---|
| 345 |
} |
|---|
| 346 |
|
|---|
| 347 |
static void |
|---|
| 348 |
setup_iep_connection_later(int seconds) { |
|---|
| 349 |
eventer_t newe; |
|---|
| 350 |
if(!noit_spinlock_trylock(&iep_conn_cnt)) return; |
|---|
| 351 |
newe = eventer_alloc(); |
|---|
| 352 |
gettimeofday(&newe->whence, NULL); |
|---|
| 353 |
newe->whence.tv_sec += seconds; |
|---|
| 354 |
newe->mask = EVENTER_TIMER; |
|---|
| 355 |
newe->callback = setup_iep_connection_callback; |
|---|
| 356 |
newe->closure = NULL; |
|---|
| 357 |
eventer_add(newe); |
|---|
| 358 |
} |
|---|
| 359 |
|
|---|
| 360 |
static int |
|---|
| 361 |
stratcon_iep_submitter(eventer_t e, int mask, void *closure, |
|---|
| 362 |
struct timeval *now) { |
|---|
| 363 |
double age; |
|---|
| 364 |
struct iep_job_closure *job = closure; |
|---|
| 365 |
struct iep_thread_driver *driver; |
|---|
| 366 |
/* We only play when it is an asynch event */ |
|---|
| 367 |
if(!(mask & EVENTER_ASYNCH_WORK)) return 0; |
|---|
| 368 |
|
|---|
| 369 |
if(mask & EVENTER_ASYNCH_CLEANUP) { |
|---|
| 370 |
/* free all the memory associated with the batch */ |
|---|
| 371 |
if(job) { |
|---|
| 372 |
if(job->line) free(job->line); |
|---|
| 373 |
if(job->remote) free(job->remote); |
|---|
| 374 |
if(job->doc_str) free(job->doc_str); |
|---|
| 375 |
free(job); |
|---|
| 376 |
} |
|---|
| 377 |
return 0; |
|---|
| 378 |
} |
|---|
| 379 |
driver = stratcon_iep_get_connection(); |
|---|
| 380 |
if(!driver) setup_iep_connection_later(1); |
|---|
| 381 |
|
|---|
| 382 |
if(!job->line || job->line[0] == '\0') return 0; |
|---|
| 383 |
|
|---|
| 384 |
if((age = stratcon_iep_age_from_line(job->line, *now)) > 60) { |
|---|
| 385 |
noitL(noit_debug, "Skipping old event from %s, %f seconds old.\n", |
|---|
| 386 |
job->remote ? job->remote : "(null)", age); |
|---|
| 387 |
return 0; |
|---|
| 388 |
} |
|---|
| 389 |
/* Submit */ |
|---|
| 390 |
if(driver) { |
|---|
| 391 |
int line_len = strlen(job->line); |
|---|
| 392 |
int remote_len = strlen(job->remote); |
|---|
| 393 |
const char *toff = strchr(job->line, '\t'); |
|---|
| 394 |
int token_off = 2; |
|---|
| 395 |
if(toff) token_off = toff - job->line + 1; |
|---|
| 396 |
|
|---|
| 397 |
job->doc_str = (char*)calloc(line_len + 1 /* \t */ + |
|---|
| 398 |
remote_len + 2, 1); |
|---|
| 399 |
strncpy(job->doc_str, job->line, token_off); |
|---|
| 400 |
strncat(job->doc_str, job->remote, remote_len); |
|---|
| 401 |
strncat(job->doc_str, "\t", 1); |
|---|
| 402 |
strncat(job->doc_str, job->line + token_off, line_len - token_off); |
|---|
| 403 |
|
|---|
| 404 |
/* Don't need to catch error here, next submit will catch it */ |
|---|
| 405 |
if(mq_driver->submit(driver, job->doc_str, line_len + remote_len + 1) != 0) { |
|---|
| 406 |
noitL(noit_debug, "failed to MQ submit.\n"); |
|---|
| 407 |
} |
|---|
| 408 |
} |
|---|
| 409 |
else { |
|---|
| 410 |
noitL(noit_iep, "no iep connection, skipping line: '%s'\n", job->line); |
|---|
| 411 |
} |
|---|
| 412 |
return 0; |
|---|
| 413 |
} |
|---|
| 414 |
|
|---|
| 415 |
void |
|---|
| 416 |
stratcon_iep_line_processor(stratcon_datastore_op_t op, |
|---|
| 417 |
struct sockaddr *remote, const char *remote_cn, |
|---|
| 418 |
void *operand, eventer_t completion) { |
|---|
| 419 |
int len; |
|---|
| 420 |
char remote_str[128]; |
|---|
| 421 |
struct iep_job_closure *jc; |
|---|
| 422 |
eventer_t newe; |
|---|
| 423 |
struct timeval __now, iep_timeout = { 10L, 0L }; |
|---|
| 424 |
/* We only care about inserts */ |
|---|
| 425 |
|
|---|
| 426 |
if(op == DS_OP_CHKPT) { |
|---|
| 427 |
if(completion) eventer_add(completion); |
|---|
| 428 |
return; |
|---|
| 429 |
} |
|---|
| 430 |
if(op != DS_OP_INSERT) return; |
|---|
| 431 |
|
|---|
| 432 |
snprintf(remote_str, sizeof(remote_str), "%s", "0.0.0.0"); |
|---|
| 433 |
if(remote) { |
|---|
| 434 |
switch(remote->sa_family) { |
|---|
| 435 |
case AF_INET: |
|---|
| 436 |
len = sizeof(struct sockaddr_in); |
|---|
| 437 |
inet_ntop(remote->sa_family, &((struct sockaddr_in *)remote)->sin_addr, |
|---|
| 438 |
remote_str, len); |
|---|
| 439 |
break; |
|---|
| 440 |
case AF_INET6: |
|---|
| 441 |
len = sizeof(struct sockaddr_in6); |
|---|
| 442 |
inet_ntop(remote->sa_family, &((struct sockaddr_in6 *)remote)->sin6_addr, |
|---|
| 443 |
remote_str, len); |
|---|
| 444 |
break; |
|---|
| 445 |
case AF_UNIX: |
|---|
| 446 |
snprintf(remote_str, sizeof(remote_str), "%s", ((struct sockaddr_un *)remote)->sun_path); |
|---|
| 447 |
break; |
|---|
| 448 |
} |
|---|
| 449 |
} |
|---|
| 450 |
|
|---|
| 451 |
/* process operand and push onto queue */ |
|---|
| 452 |
gettimeofday(&__now, NULL); |
|---|
| 453 |
newe = eventer_alloc(); |
|---|
| 454 |
newe->mask = EVENTER_ASYNCH; |
|---|
| 455 |
add_timeval(__now, iep_timeout, &newe->whence); |
|---|
| 456 |
newe->callback = stratcon_iep_submitter; |
|---|
| 457 |
jc = calloc(1, sizeof(*jc)); |
|---|
| 458 |
jc->line = operand; |
|---|
| 459 |
jc->remote = strdup(remote_str); |
|---|
| 460 |
newe->closure = jc; |
|---|
| 461 |
|
|---|
| 462 |
eventer_add_asynch(&iep_jobq, newe); |
|---|
| 463 |
} |
|---|
| 464 |
|
|---|
| 465 |
static void connection_destroy(void *vd) { |
|---|
| 466 |
struct iep_thread_driver *driver = vd; |
|---|
| 467 |
mq_driver->disconnect(driver); |
|---|
| 468 |
mq_driver->deallocate(driver); |
|---|
| 469 |
} |
|---|
| 470 |
|
|---|
| 471 |
jlog_streamer_ctx_t * |
|---|
| 472 |
stratcon_jlog_streamer_iep_ctx_alloc(void) { |
|---|
| 473 |
jlog_streamer_ctx_t *ctx; |
|---|
| 474 |
ctx = stratcon_jlog_streamer_ctx_alloc(); |
|---|
| 475 |
ctx->jlog_feed_cmd = htonl(NOIT_JLOG_DATA_TEMP_FEED); |
|---|
| 476 |
ctx->push = stratcon_iep_line_processor; |
|---|
| 477 |
return ctx; |
|---|
| 478 |
} |
|---|
| 479 |
|
|---|
| 480 |
struct iep_daemon_info { |
|---|
| 481 |
pid_t child; |
|---|
| 482 |
int stdin_pipe[2]; |
|---|
| 483 |
int stderr_pipe[2]; |
|---|
| 484 |
char *directory; |
|---|
| 485 |
char *command; |
|---|
| 486 |
}; |
|---|
| 487 |
|
|---|
| 488 |
static void |
|---|
| 489 |
iep_daemon_info_free(struct iep_daemon_info *info) { |
|---|
| 490 |
if(!info) return; |
|---|
| 491 |
if(info->directory) free(info->directory); |
|---|
| 492 |
if(info->command) free(info->command); |
|---|
| 493 |
if(info->stdin_pipe[0] >= 0) close(info->stdin_pipe[0]); |
|---|
| 494 |
if(info->stdin_pipe[1] >= 0) close(info->stdin_pipe[1]); |
|---|
| 495 |
if(info->stderr_pipe[0] >= 0) close(info->stderr_pipe[0]); |
|---|
| 496 |
if(info->stderr_pipe[1] >= 0) close(info->stderr_pipe[1]); |
|---|
| 497 |
free(info); |
|---|
| 498 |
} |
|---|
| 499 |
|
|---|
| 500 |
static int |
|---|
| 501 |
stratcon_iep_err_handler(eventer_t e, int mask, void *closure, |
|---|
| 502 |
struct timeval *now) { |
|---|
| 503 |
int len, newmask; |
|---|
| 504 |
char buff[4096]; |
|---|
| 505 |
struct iep_daemon_info *info = (struct iep_daemon_info *)closure; |
|---|
| 506 |
|
|---|
| 507 |
if(mask & EVENTER_EXCEPTION) { |
|---|
| 508 |
int rv; |
|---|
| 509 |
read_error: |
|---|
| 510 |
kill(info->child, SIGKILL); |
|---|
| 511 |
if(waitpid(info->child, &rv, 0) != info->child) { |
|---|
| 512 |
noitL(noit_error, "Failed to reap IEP daemon\n"); |
|---|
| 513 |
exit(-1); |
|---|
| 514 |
} |
|---|
| 515 |
noitL(noit_error, "IEP daemon is done, starting a new one\n"); |
|---|
| 516 |
start_iep_daemon(); |
|---|
| 517 |
eventer_remove_fd(e->fd); |
|---|
| 518 |
iep_daemon_info_free(info); |
|---|
| 519 |
return 0; |
|---|
| 520 |
} |
|---|
| 521 |
while(1) { |
|---|
| 522 |
len = e->opset->read(e->fd, buff, sizeof(buff)-1, &newmask, e); |
|---|
| 523 |
if(len == -1 && (errno == EAGAIN || errno == EINTR)) |
|---|
| 524 |
return newmask | EVENTER_EXCEPTION; |
|---|
| 525 |
if(len <= 0) goto read_error; |
|---|
| 526 |
assert(len < sizeof(buff)); |
|---|
| 527 |
buff[len] = '\0'; |
|---|
| 528 |
noitL(noit_iep, "%s", buff); |
|---|
| 529 |
} |
|---|
| 530 |
} |
|---|
| 531 |
|
|---|
| 532 |
static void |
|---|
| 533 |
start_iep_daemon() { |
|---|
| 534 |
eventer_t newe; |
|---|
| 535 |
struct iep_daemon_info *info; |
|---|
| 536 |
char *cmd = NULL; |
|---|
| 537 |
|
|---|
| 538 |
if(!noit_conf_get_string(NULL, "/stratcon/iep/start/@command", |
|---|
| 539 |
&cmd)) { |
|---|
| 540 |
noitL(noit_error, "No IEP start command provided. You're on your own.\n"); |
|---|
| 541 |
setup_iep_connection_later(0); |
|---|
| 542 |
return; |
|---|
| 543 |
} |
|---|
| 544 |
|
|---|
| 545 |
info = calloc(1, sizeof(*info)); |
|---|
| 546 |
info->stdin_pipe[0] = info->stdin_pipe[1] = -1; |
|---|
| 547 |
info->stderr_pipe[0] = info->stderr_pipe[1] = -1; |
|---|
| 548 |
info->command = cmd; |
|---|
| 549 |
|
|---|
| 550 |
if(!noit_conf_get_string(NULL, "/stratcon/iep/start/@directory", |
|---|
| 551 |
&info->directory)) |
|---|
| 552 |
info->directory = strdup("."); |
|---|
| 553 |
if(pipe(info->stdin_pipe) != 0 || |
|---|
| 554 |
pipe(info->stderr_pipe) != 0) { |
|---|
| 555 |
noitL(noit_error, "pipe: %s\n", strerror(errno)); |
|---|
| 556 |
goto bail; |
|---|
| 557 |
} |
|---|
| 558 |
info->child = fork(); |
|---|
| 559 |
if(info->child == -1) { |
|---|
| 560 |
noitL(noit_error, "fork: %s\n", strerror(errno)); |
|---|
| 561 |
goto bail; |
|---|
| 562 |
} |
|---|
| 563 |
if(info->child == 0) { |
|---|
| 564 |
char *argv[3] = { "run-iep", NULL, NULL }; |
|---|
| 565 |
int stdout_fileno; |
|---|
| 566 |
|
|---|
| 567 |
argv[1] = noit_conf_config_filename(); |
|---|
| 568 |
|
|---|
| 569 |
if(chdir(info->directory) != 0) { |
|---|
| 570 |
noitL(noit_error, "Starting IEP daemon, chdir failed: %s\n", |
|---|
| 571 |
strerror(errno)); |
|---|
| 572 |
exit(-1); |
|---|
| 573 |
} |
|---|
| 574 |
|
|---|
| 575 |
close(info->stdin_pipe[1]); |
|---|
| 576 |
close(info->stderr_pipe[0]); |
|---|
| 577 |
dup2(info->stdin_pipe[0], 0); |
|---|
| 578 |
dup2(info->stderr_pipe[1], 2); |
|---|
| 579 |
stdout_fileno = open("/dev/null", O_WRONLY); |
|---|
| 580 |
dup2(stdout_fileno, 1); |
|---|
| 581 |
|
|---|
| 582 |
exit(execv(info->command, argv)); |
|---|
| 583 |
} |
|---|
| 584 |
/* in the parent */ |
|---|
| 585 |
close(info->stdin_pipe[0]); |
|---|
| 586 |
info->stdin_pipe[0] = -1; |
|---|
| 587 |
close(info->stderr_pipe[1]); |
|---|
| 588 |
info->stderr_pipe[1] = -1; |
|---|
| 589 |
if(eventer_set_fd_nonblocking(info->stderr_pipe[0])) { |
|---|
| 590 |
goto bail; |
|---|
| 591 |
} |
|---|
| 592 |
|
|---|
| 593 |
newe = eventer_alloc(); |
|---|
| 594 |
newe->fd = info->stderr_pipe[0]; |
|---|
| 595 |
newe->mask = EVENTER_READ | EVENTER_EXCEPTION; |
|---|
| 596 |
newe->callback = stratcon_iep_err_handler; |
|---|
| 597 |
newe->closure = info; |
|---|
| 598 |
eventer_add(newe); |
|---|
| 599 |
info = NULL; |
|---|
| 600 |
|
|---|
| 601 |
setup_iep_connection_later(1); |
|---|
| 602 |
|
|---|
| 603 |
return; |
|---|
| 604 |
|
|---|
| 605 |
bail: |
|---|
| 606 |
if(info) { |
|---|
| 607 |
iep_daemon_info_free(info); |
|---|
| 608 |
} |
|---|
| 609 |
noitL(noit_error, "Failed to start IEP daemon\n"); |
|---|
| 610 |
exit(-1); |
|---|
| 611 |
return; |
|---|
| 612 |
} |
|---|
| 613 |
|
|---|
| 614 |
void |
|---|
| 615 |
stratcon_iep_mq_driver_register(const char *name, mq_driver_t *d) { |
|---|
| 616 |
noit_hash_replace(&mq_drivers, strdup(name), strlen(name), d, free, NULL); |
|---|
| 617 |
} |
|---|
| 618 |
|
|---|
| 619 |
void |
|---|
| 620 |
stratcon_iep_init() { |
|---|
| 621 |
noit_boolean disabled = noit_false; |
|---|
| 622 |
char mq_type[128] = "stomp"; |
|---|
| 623 |
void *vdriver; |
|---|
| 624 |
|
|---|
| 625 |
if(noit_conf_get_boolean(NULL, "/stratcon/iep/@disabled", &disabled) && |
|---|
| 626 |
disabled == noit_true) { |
|---|
| 627 |
noitL(noit_error, "IEP system is disabled!\n"); |
|---|
| 628 |
return; |
|---|
| 629 |
} |
|---|
| 630 |
|
|---|
| 631 |
if(!noit_conf_get_stringbuf(NULL, "/stratcon/iep/mq/@type", |
|---|
| 632 |
mq_type, sizeof(mq_type))) { |
|---|
| 633 |
noitL(noit_error, "You must specify an <mq type=\"...\"> that is valid.\n"); |
|---|
| 634 |
exit(-2); |
|---|
| 635 |
} |
|---|
| 636 |
if(!noit_hash_retrieve(&mq_drivers, mq_type, strlen(mq_type), &vdriver) || |
|---|
| 637 |
vdriver == NULL) { |
|---|
| 638 |
noitL(noit_error, "Cannot find MQ driver type: %s\n", mq_type); |
|---|
| 639 |
noitL(noit_error, "Did you forget to load a module?\n"); |
|---|
| 640 |
exit(-2); |
|---|
| 641 |
} |
|---|
| 642 |
mq_driver = (mq_driver_t *)vdriver; |
|---|
| 643 |
|
|---|
| 644 |
noit_iep = noit_log_stream_find("error/iep"); |
|---|
| 645 |
if(!noit_iep) noit_iep = noit_error; |
|---|
| 646 |
|
|---|
| 647 |
eventer_name_callback("stratcon_iep_submitter", stratcon_iep_submitter); |
|---|
| 648 |
eventer_name_callback("stratcon_iep_err_handler", stratcon_iep_err_handler); |
|---|
| 649 |
eventer_name_callback("setup_iep_connection_callback", setup_iep_connection_callback); |
|---|
| 650 |
pthread_key_create(&iep_connection, connection_destroy); |
|---|
| 651 |
|
|---|
| 652 |
/* start up a thread pool of one */ |
|---|
| 653 |
memset(&iep_jobq, 0, sizeof(iep_jobq)); |
|---|
| 654 |
eventer_jobq_init(&iep_jobq, "iep_submitter"); |
|---|
| 655 |
iep_jobq.backq = eventer_default_backq(); |
|---|
| 656 |
eventer_jobq_increase_concurrency(&iep_jobq); |
|---|
| 657 |
|
|---|
| 658 |
start_iep_daemon(); |
|---|
| 659 |
|
|---|
| 660 |
/* setup our live jlog stream */ |
|---|
| 661 |
stratcon_streamer_connection(NULL, NULL, |
|---|
| 662 |
stratcon_jlog_recv_handler, |
|---|
| 663 |
(void *(*)())stratcon_jlog_streamer_iep_ctx_alloc, |
|---|
| 664 |
NULL, |
|---|
| 665 |
jlog_streamer_ctx_free); |
|---|
| 666 |
} |
|---|
| 667 |
|
|---|