Skip to content
This repository
Browse code

basic background/scheduled flush

  • Loading branch information...
commit 7bf18cbf878963cba1017f89dc017e14f0e8b923 1 parent e1ea606
Paul Asmuth authored
7 fnordmetric-enterprise/TODO.md
Source Rendered
... ... @@ -1,7 +1,4 @@
1   -
2   -
3   -+ first sampled value is not flushed (only after first flush interval was reached and new data is submitted)
  1 ++ background rbuf_flush -> swapfile every N seconds
  2 ++ rbuf size in time
4 3 + warm up ring buffers on start
5 4 + accept millisecond timestamps
6   -+ background rbuf_flush thread every N seconds
7   -+ rbuf size in time
36 fnordmetric-enterprise/src/BackgroundThread.scala
... ... @@ -0,0 +1,36 @@
  1 +// FnordMetric Enterprise
  2 +// (c) 2011-2013 Paul Asmuth <paul@paulasmuth.com>
  3 +//
  4 +// Licensed under the MIT License (the "License"); you may not use this
  5 +// file except in compliance with the License. You may obtain a copy of
  6 +// the License at: http://opensource.org/licenses/MIT
  7 +
  8 +package com.fnordmetric.enterprise
  9 +
  10 +class BackgroundThread extends Thread {
  11 +
  12 + val TICK = 10
  13 +
  14 + // this runs in the background and tries to invoke some flushes in
  15 + // the background. it is not neccessary for the liveliness of the
  16 + // application that each iteration of the loop finishes in a fixed
  17 + // time interval. even if this thread dies completely all data will
  18 + // be correctly recorded, but the last sample on each metric won't
  19 + // be flushed until new data arrives
  20 + override def run : Unit =
  21 + while (true) next
  22 +
  23 + private def next : Unit = {
  24 + val now = FnordMetric.now
  25 +
  26 + // search for metrics with a pending flush interest and fush them
  27 + // if the now > flush_interest
  28 + for (metric <- MetricFactory.metric_map)
  29 + if (metric._2.flush_interest > 0 && metric._2.flush_interest <= now)
  30 + metric._2.flush_bucket
  31 +
  32 + // to avoid burning CPU we sleep for a few ms
  33 + Thread.sleep(TICK)
  34 + }
  35 +
  36 +}
2  fnordmetric-enterprise/src/FnordMetric.scala
@@ -102,6 +102,8 @@ object FnordMetric {
102 102 if (flock == null)
103 103 error("cannot aquire server.lck", true)
104 104
  105 + val bg_thread = new BackgroundThread
  106 + bg_thread.start
105 107
106 108 if (CONFIG contains 'http)
107 109 error("FIXPAUL: not yet implemented: http-server", true)
8 fnordmetric-enterprise/src/Metric.scala
@@ -28,7 +28,7 @@ class Metric(key: MetricKey) {
28 28
29 29 // adds an aggregated value to the in memory ring buffer after it has
30 30 // been flushed from the bucket
31   - def flush_bucket : Unit = {
  31 + def flush_bucket : Unit = this.synchronized {
32 32 val nxt = bucket.flush_every(key.flush_interval)
33 33
34 34 // indicate to the background thread that this metric has pending data
@@ -62,6 +62,9 @@ class Metric(key: MetricKey) {
62 62 // now at least one slot in the ring buffer is free so we can just
63 63 // push our sample
64 64 rbuf.push(nxt)
  65 +
  66 + // mark this metric as "no pending flushes"
  67 + flush_interest = 0
65 68 }
66 69
67 70 // tries to persist as much data from the in memory ring buffer to disk
@@ -75,9 +78,6 @@ class Metric(key: MetricKey) {
75 78
76 79 // mark the range as "read to be overwritten
77 80 rbuf_seek_pos += flush_range
78   -
79   - // mark this metric as "no pending flushes"
80   - flush_interest = 0
81 81 }
82 82
83 83 // returns this metrics value at time0 if a value was recorded at that

0 comments on commit 7bf18cb

Please sign in to comment.
Something went wrong with that request. Please try again.