Changeset db7317dba191f1eb1c0e46a8c023bfc5a725b9c2

Show
Ignore:
Timestamp:
07/30/10 20:09:40 (8 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1280520580 +0000
git-parent:

[19a8d2097287941bcdf89ae61e22aa77ee355f0c]

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

log rotation support, needs docs. refs #300

Files:

Legend:

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

    rfed0d5d rdb7317d  
    865865} 
    866866 
     867struct log_rotate_crutch { 
     868  noit_log_stream_t ls; 
     869  int seconds; 
     870  size_t max_size; 
     871}; 
     872 
     873static int 
     874noit_conf_log_rotate_size(eventer_t e, int mask, void *closure, 
     875                          struct timeval *now) { 
     876  struct log_rotate_crutch *lrc = closure; 
     877  if(noit_log_stream_written(lrc->ls) > lrc->max_size) { 
     878    noit_log_stream_rename(lrc->ls, NOIT_LOG_RENAME_AUTOTIME); 
     879    noit_log_stream_reopen(lrc->ls); 
     880  } 
     881  /* Yes the 5 is arbitrary, but this is cheap */ 
     882  eventer_add_in_s_us(noit_conf_log_rotate_size, closure, 5, 0); 
     883  return 0; 
     884} 
     885static int 
     886noit_conf_log_rotate_time(eventer_t e, int mask, void *closure, 
     887                          struct timeval *now) { 
     888  struct timeval lnow; 
     889  eventer_t newe; 
     890  struct log_rotate_crutch *lrc = closure; 
     891 
     892  if(now) { 
     893    noit_log_stream_rename(lrc->ls, NOIT_LOG_RENAME_AUTOTIME); 
     894    noit_log_stream_reopen(lrc->ls); 
     895  } 
     896   
     897  newe = eventer_alloc(); 
     898  newe->closure = closure; 
     899  if(!now) { gettimeofday(&lnow, NULL); now = &lnow; } 
     900  if(e) 
     901    memcpy(&newe->whence, &e->whence, sizeof(newe->whence)); 
     902  else if(now) { 
     903    memcpy(&newe->whence, now, sizeof(newe->whence)); 
     904    newe->whence.tv_sec = (newe->whence.tv_sec / lrc->seconds) * lrc->seconds; 
     905  } 
     906  newe->whence.tv_sec += lrc->seconds; 
     907  newe->mask = EVENTER_TIMER; 
     908  newe->callback = noit_conf_log_rotate_time; 
     909  eventer_add(newe); 
     910  return 0; 
     911} 
     912int 
     913noit_conf_log_init_rotate(const char *toplevel, noit_boolean validate) { 
     914  int i, cnt = 0, max_time, max_size, rv = 0; 
     915  noit_conf_section_t *log_configs; 
     916  char path[256]; 
     917 
     918  snprintf(path, sizeof(path), "/%s/logs//log", toplevel); 
     919  log_configs = noit_conf_get_sections(NULL, path, &cnt); 
     920  noitL(noit_debug, "Found %d %s stanzas\n", cnt, path); 
     921  for(i=0; i<cnt; i++) { 
     922    noit_log_stream_t ls; 
     923    char name[256]; 
     924 
     925    if(!noit_conf_get_stringbuf(log_configs[i], 
     926                                "ancestor-or-self::node()/@name", 
     927                                name, sizeof(name))) { 
     928      noitL(noit_error, "log section %d does not have a name attribute\n", i+1); 
     929      if(validate) { rv = -1; break; } 
     930      else exit(-2); 
     931    } 
     932    ls = noit_log_stream_find(name); 
     933    if(!ls) continue; 
     934 
     935    if(noit_conf_get_int(log_configs[i],     
     936                         "ancestor-or-self::node()/@rotate_seconds", 
     937                         &max_time) && max_time) { 
     938      struct log_rotate_crutch *lrc; 
     939      if(max_time < 600) { 
     940        fprintf(stderr, "rotate_seconds must be >= 600s (10 minutes)\n"); 
     941        if(validate) { rv = -1; break; } 
     942        else exit(-2); 
     943      } 
     944      if(!validate) { 
     945        lrc = calloc(1, sizeof(*lrc)); 
     946        lrc->ls = ls; 
     947        lrc->seconds = max_time; 
     948        noit_conf_log_rotate_time(NULL, EVENTER_TIMER, lrc, NULL); 
     949      } 
     950    } 
     951 
     952    if(noit_conf_get_int(log_configs[i],     
     953                         "ancestor-or-self::node()/@rotate_bytes", 
     954                         &max_size) && max_size) { 
     955      struct log_rotate_crutch *lrc; 
     956      if(max_size < 102400) { 
     957        fprintf(stderr, "rotate_bytes must be >= 102400 (100k)\n"); 
     958        if(validate) { rv = -1; break; } 
     959        else exit(-2); 
     960      } 
     961      if(!validate) { 
     962        lrc = calloc(1, sizeof(*lrc)); 
     963        lrc->ls = ls; 
     964        lrc->max_size = max_size; 
     965        noit_conf_log_rotate_size(NULL, EVENTER_TIMER, lrc, NULL); 
     966      } 
     967    } 
     968  } 
     969  free(log_configs); 
     970  return rv; 
     971} 
    867972void 
    868973noit_conf_log_init(const char *toplevel) { 
     
    9171022    if(noit_conf_get_boolean(log_configs[i], 
    9181023                             "ancestor-or-self::node()/@timestamps", 
    919                              &timestamps) && timestamps
    920       ls->timestamps = 1
    921        
     1024                             &timestamps)
     1025      ls->timestamps = timestamps ? 1 : 0
     1026   
    9221027    outlets = noit_conf_get_sections(log_configs[i], 
    9231028                                     "ancestor-or-self::node()/outlet", &ocnt); 
     
    9411046  } 
    9421047  if(log_configs) free(log_configs); 
     1048  if(noit_conf_log_init_rotate(toplevel, noit_true)) exit(-1); 
    9431049} 
    9441050 
  • src/noit_conf.h

    r4790fc8 rdb7317d  
    128128 
    129129API_EXPORT(void) noit_conf_log_init(const char *toplevel); 
     130API_EXPORT(int) noit_conf_log_init_rotate(const char *, noit_boolean); 
    130131 
    131132#define EXPOSE_CHECKER(name) \ 
  • src/noitd.c

    rc6a5f68 rdb7317d  
    210210    exit(-1); 
    211211  } 
     212  /* rotation init requires, eventer_init() */ 
     213  noit_conf_log_init_rotate(APPNAME, noit_false); 
    212214 
    213215  /* Setup our heartbeat */ 
  • src/stratcond.c

    rc6a5f68 rdb7317d  
    238238    exit(2); 
    239239  } 
     240  /* rotation init requires, eventer_init() */ 
     241  noit_conf_log_init_rotate(APPNAME, noit_false); 
    240242 
    241243  noit_watchdog_child_eventer_heartbeat(); 
  • src/utils/noit_log.c

    rfed0d5d rdb7317d  
    5050#include "utils/noit_log.h" 
    5151#include "utils/noit_hash.h" 
     52#include "utils/noit_atomic.h" 
    5253#include "jlog/jlog.h" 
    5354#include "jlog/jlog_private.h" 
     
    8586  struct _noit_log_stream_outlet_list *outlets; 
    8687  pthread_rwlock_t *lock; 
     88  noit_atomic64_t written; 
    8789  unsigned deps_materialized:1; 
    8890  unsigned debug_below:1; 
     
    113115    for(node = ls->outlets; node; node = node->next) { 
    114116      MATERIALIZE_DEPS(node->outlet); 
    115       if(!ls->debug) ls->debug_below = node->outlet->debug
    116       if(!ls->timestamps) ls->timestamps_below = node->outlet->timestamps
     117      if(!ls->debug) ls->debug_below = node->outlet->debug_below
     118      if(!ls->timestamps) ls->timestamps_below = node->outlet->timestamps_below
    117119    } 
    118120  } 
     
    139141    oldfd = (int)(vpsized_int)ls->op_ctx; 
    140142    newfd = open(ls->path, O_CREAT|O_WRONLY|O_APPEND, ls->mode); 
     143    ls->written = 0; 
    141144    if(newfd >= 0) { 
     145      struct stat sb; 
    142146      ls->op_ctx = (void *)(vpsized_int)newfd; 
    143147      if(oldfd >= 0) close(oldfd); 
    144148      rv = 0; 
     149      if(fstat(newfd, &sb) == 0) ls->written = (size_t)sb.st_size; 
    145150    } 
    146151    if(ls->lock) pthread_rwlock_unlock(ls->lock); 
     
    157162  if(fd >= 0) rv = write(fd, buf, len); 
    158163  if(ls->lock) pthread_rwlock_unlock(ls->lock); 
     164  if(rv > 0) noit_atomic_add64(&ls->written, rv); 
    159165  return rv; 
    160166} 
     
    167173  if(fd >= 0) rv = writev(fd, iov, iovcnt); 
    168174  if(ls->lock) pthread_rwlock_unlock(ls->lock); 
     175  if(rv > 0) noit_atomic_add64(&ls->written, rv); 
    169176  return rv; 
    170177} 
     
    194201posix_logio_rename(noit_log_stream_t ls, const char *name) { 
    195202  int rv = 0; 
     203  char autoname[PATH_MAX]; 
     204  if(name == NOIT_LOG_RENAME_AUTOTIME) { 
     205    time_t now = time(NULL); 
     206    snprintf(autoname, sizeof(autoname), "%s.%llu", 
     207             ls->path, (unsigned long long)now); 
     208    name = autoname; 
     209  } 
    196210  if(!strcmp(name, ls->path)) return 0; /* noop */ 
    197211  if(ls->lock) pthread_rwlock_rdlock(ls->lock); 
     
    599613  if(ls->ops && ls->ops->sizeop) return ls->ops->sizeop(ls); 
    600614  return -1; 
     615} 
     616 
     617size_t 
     618noit_log_stream_written(noit_log_stream_t ls) { 
     619  return ls->written; 
    601620} 
    602621 
  • src/utils/noit_log.h

    rfed0d5d rdb7317d  
    9696                                               const char *name); 
    9797API_EXPORT(void) noit_log_stream_reopen(noit_log_stream_t ls); 
     98 
     99#define NOIT_LOG_RENAME_AUTOTIME ((const char *)-1) 
     100 
    98101API_EXPORT(int) noit_log_stream_rename(noit_log_stream_t ls, const char *); 
    99102API_EXPORT(void) noit_log_stream_close(noit_log_stream_t ls); 
    100103API_EXPORT(size_t) noit_log_stream_size(noit_log_stream_t ls); 
     104API_EXPORT(size_t) noit_log_stream_written(noit_log_stream_t ls); 
    101105API_EXPORT(void) noit_log_stream_free(noit_log_stream_t ls); 
    102106API_EXPORT(int) noit_vlog(noit_log_stream_t ls, struct timeval *,