Commit 493561b4 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

write to swapfile on rbuf_flush

parent 7efe0e1f
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ case class MetricKey(key: String, mode: String, flush_interval: Long)

class Metric(key: MetricKey) {
  val bucket = BucketFactory.new_bucket(key.mode)
  val swap = new SwapFile(key)
  var rbuf = new RingBuffer[(Long, Double)](1000)
  var rbuf_seek_pos = 0

@@ -40,14 +41,19 @@ class Metric(key: MetricKey) {
    }

    rbuf.push(((time, value)))
    //println("RINGBUF", rbuf.tail(10))

    flush_rbuf // FIXPAUL: remove me
  }

  // tries to persist as much data from the in memory ring buffer to disk
  // as possible but doesnt remove it from the buffer yet
  def flush_rbuf = this.synchronized {
    val flush_range = rbuf.size - rbuf_seek_pos
    //println("RBUF_FLUSH", rbuf.tail(flush_range))

    for (sample <- rbuf.tail(flush_range))
      swap.put(sample._1, sample._2)

    swap.flush
    rbuf_seek_pos += flush_range
  }

+4 −1
Original line number Diff line number Diff line
@@ -14,6 +14,8 @@ import java.nio.ByteOrder

class SwapFile(metric_key: MetricKey) {

  var write_pos = 0

  val file_name = "metric-" + metric_key.key +
    metric_key.mode + "-" + metric_key.flush_interval

@@ -35,8 +37,9 @@ class SwapFile(metric_key: MetricKey) {
  }

  def flush : Unit = {
    file.seek(file.length)
    file.seek(write_pos)
    file.write(buffer.array)
    write_pos += 18
    buffer.rewind
  }