Changeset dcd539d74168658a6f7fae5302239f74553717bb

Show
Ignore:
Timestamp:
07/01/08 20:16:09 (6 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1214943369 +0000
git-parent:

[a19b8157a03891ec782d7a880a640be1c6c98663]

git-author:
Theo Schlossnagle <jesus@omniti.com> 1214943369 +0000
Message:

implements stratcon configuration storage in DB, refs #27

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/noit_conf.c

    r70451c5 rdcd539d  
    462462  int len; 
    463463  int allocd; 
    464   enum { CONFIG_RAW = 0, CONFIG_COMPRESSED, CONFIG_B64 } encoded; 
     464  enum { CONFIG_RAW = 0, CONFIG_COMPRESSED, CONFIG_B64 } target, encoded; 
    465465}; 
    466466static int 
     
    494494 
    495495  if(clv->buff == NULL) { 
    496     clv->encoded = CONFIG_B64
     496    clv->encoded = clv->target
    497497    return 0; 
    498498  } 
    499499  clv->raw_len = clv->len; 
    500500  assert(clv->encoded == CONFIG_RAW); 
     501  if(clv->encoded == clv->target) return 0; 
     502 
    501503  /* Compress */ 
    502504  initial_dlen = dlen = compressBound(clv->len); 
     
    514516  clv->len = dlen; 
    515517  clv->encoded = CONFIG_COMPRESSED; 
     518  if(clv->encoded == clv->target) return 0; 
     519 
    516520  /* Encode */ 
    517521  initial_dlen = ((clv->len + 2) / 3) * 4; 
     
    528532  clv->len = dlen; 
    529533  clv->encoded = CONFIG_B64; 
    530   return 0; 
     534  if(clv->encoded == clv->target) return 0; 
     535  return -1; 
    531536} 
    532537 
     
    592597  return 0; 
    593598} 
     599char * 
     600noit_conf_xml_in_mem(size_t *len) { 
     601  struct config_line_vstr *clv; 
     602  xmlOutputBufferPtr out; 
     603  xmlCharEncodingHandlerPtr enc; 
     604  char *rv; 
     605 
     606  clv = calloc(1, sizeof(*clv)); 
     607  clv->target = CONFIG_RAW; 
     608  enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8); 
     609  out = xmlOutputBufferCreateIO(noit_config_log_write_xml, 
     610                                noit_config_log_close_xml, 
     611                                clv, enc); 
     612  xmlSaveFormatFileTo(out, master_config, "utf8", 1); 
     613  if(clv->encoded != CONFIG_RAW) { 
     614    noitL(noit_error, "Error logging configuration\n"); 
     615    if(clv->buff) free(clv->buff); 
     616    free(clv); 
     617    return NULL; 
     618  } 
     619  rv = clv->buff; 
     620  *len = clv->len; 
     621  free(clv); 
     622  return rv; 
     623} 
     624 
    594625int 
    595626noit_conf_write_log() { 
     
    607638  gettimeofday(&__now, NULL); 
    608639  clv = calloc(1, sizeof(*clv)); 
     640  clv->target = CONFIG_B64; 
    609641  enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8); 
    610642  out = xmlOutputBufferCreateIO(noit_config_log_write_xml, 
  • src/noit_conf.h

    r70451c5 rdcd539d  
    8484                       noit_console_state_t *state, void *closure); 
    8585 
     86API_EXPORT(char *) 
     87  noit_conf_xml_in_mem(size_t *len); 
     88 
    8689API_EXPORT(int) 
    8790  noit_conf_write_log(); 
  • src/noitd.c

    r70451c5 rdcd539d  
    111111 
    112112  /* Write our log out, and setup a watchdog to write it out on change. */ 
    113   noit_conf_write_log(); 
     113  noit_conf_write_log(NULL); 
    114114  noit_conf_coalesce_changes(10); /* 10 seconds of no changes before we write */ 
    115115  noit_conf_watch_and_journal_watchdog(noit_conf_write_log, NULL); 
  • src/stratcon_datastore.c

    r70451c5 rdcd539d  
    3636 
    3737#define MAX_PARAMS 8 
     38#define POSTGRES_PARTS \ 
     39  int nparams; \ 
     40  int metric_type; \ 
     41  char *paramValues[MAX_PARAMS]; \ 
     42  int paramLengths[MAX_PARAMS]; \ 
     43  int paramFormats[MAX_PARAMS]; \ 
     44  int paramAllocd[MAX_PARAMS]; 
     45 
     46typedef struct ds_single_detail { 
     47  POSTGRES_PARTS 
     48} ds_single_detail; 
    3849typedef struct ds_job_detail { 
     50  /* Postgres specific stuff */ 
     51  POSTGRES_PARTS 
     52 
    3953  char *data;  /* The raw string, NULL means the stream is done -- commit. */ 
    4054  int problematic; 
    4155  eventer_t completion_event; /* This event should be registered if non NULL */ 
    4256  struct ds_job_detail *next; 
    43  
    44   /* Postgres specific stuff */ 
    45   int nparams; 
    46   int metric_type; 
    47   char *paramValues[MAX_PARAMS]; 
    48   int paramLengths[MAX_PARAMS]; 
    49   int paramFormats[MAX_PARAMS]; 
    50   int paramAllocd[MAX_PARAMS]; 
    5157} ds_job_detail; 
    5258 
     
    6167 
    6268static void 
    63 free_params(ds_job_detail *d) { 
     69free_params(ds_single_detail *d) { 
    6470  int i; 
    6571  for(i=0; i<d->nparams; i++) 
     
    8389    next = q->head; 
    8490    q->head = q->head->next; 
    85     free_params(next); 
     91    free_params((ds_single_detail *)next); 
    8692    if(next->data) free(next->data); 
    8793    free(next); 
     
    215221        if(len <= 0) { 
    216222          noitL(noit_error, "noitd config base64 decoding error.\n"); 
     223          free(final_buff); 
    217224          goto bad_row; 
    218225        } 
     
    221228                              (unsigned char *)token, len)) { 
    222229          noitL(noit_error, "noitd config decompression failure.\n"); 
     230          free(final_buff); 
    223231          goto bad_row; 
    224232        } 
    225233        if(final_len != actual_final_len) { 
    226234          noitL(noit_error, "noitd config decompression error.\n"); 
     235          free(final_buff); 
    227236          goto bad_row; 
    228237        } 
     
    485494  } 
    486495} 
     496 
     497int 
     498stratcon_datastore_saveconfig(void *unused) { 
     499  int rv = -1; 
     500  conn_q _cq = { 0 }, *cq = &_cq; 
     501  char *buff; 
     502  ds_single_detail _d = { 0 }, *d = &_d; 
     503 
     504  if(stratcon_database_connect(cq) == 0) { 
     505    char time_as_str[20]; 
     506    size_t len; 
     507    buff = noit_conf_xml_in_mem(&len); 
     508    if(!buff) goto bad_row; 
     509 
     510    snprintf(time_as_str, sizeof(time_as_str), "%lu", time(NULL)); 
     511    DECLARE_PARAM_STR("0.0.0.0", 7); 
     512    DECLARE_PARAM_STR("stratcond", 9); 
     513    DECLARE_PARAM_STR(time_as_str, strlen(time_as_str)); 
     514    DECLARE_PARAM_STR(buff, len); 
     515    free(buff); 
     516 
     517    GET_QUERY(config_insert); 
     518    PG_EXEC(config_insert); 
     519    rv = 0; 
     520 
     521    bad_row: 
     522      free_params(d); 
     523  } 
     524  if(cq->dbh) PQfinish(cq->dbh); 
     525  return rv; 
     526} 
  • src/stratcon_datastore.h

    ra7304b5 rdcd539d  
    2323                          struct sockaddr *, void *); 
    2424 
     25API_EXPORT(int) 
     26  stratcon_datastore_saveconfig(void *unused); 
     27 
    2528#endif 
  • src/stratcond.c

    ra7304b5 rdcd539d  
    1616#include "noit_conf.h" 
    1717#include "stratcon_jlog_streamer.h" 
     18#include "stratcon_datastore.h" 
    1819 
    1920#define APPNAME "stratcon" 
     
    101102  stratcon_jlog_streamer_init(APPNAME); 
    102103 
     104  /* Write our log out, and setup a watchdog to write it out on change. */ 
     105  stratcon_datastore_saveconfig(NULL); 
     106  noit_conf_coalesce_changes(10); /* 10 seconds of no changes before we write */ 
     107  noit_conf_watch_and_journal_watchdog(stratcon_datastore_saveconfig, NULL); 
     108 
    103109  eventer_loop(); 
    104110  return 0;