Permalink
Browse files

wipe skipped epochs

  • Loading branch information...
1 parent ce697b4 commit f23ca3970d77b736adc1744eca1cce97fc85cf9d @igrigorik committed May 9, 2010
Showing with 31 additions and 14 deletions.
  1. +12 −11 lib/rrrdtool.rb
  2. +19 −3 spec/rrrdtool_spec.rb
View
@@ -7,8 +7,6 @@ def initialize(opts)
@debug = opts[:debug] || false
@current = nil
- @signature = nil
-
@db = Redis.new
end
@@ -25,16 +23,19 @@ def buckets(set)
end
def epoch(set)
- e = time_epoch
- s = Time.now.to_i / @step
- now = set + ":" + e.to_s
+ current_epoch = time_epoch
+ last_epoch = @db.get("#{set}:epoch").to_i
+ now = set + ":" + current_epoch.to_s
- if now != @current and s != @signature
- debug [:new_epoch, e]
- @current = now
- @signature = Time.now.to_i / @step
+ if now != @current and current_epoch != last_epoch
+ debug [:new_epoch, current_epoch]
- clear_bucket(epochs_ago(set, @buckets))
+ [(Time.now.to_i / @step - last_epoch).abs, @buckets].min.times do |n|
+ clear_bucket(epochs_ago(set, n))
+ end
+
+ @current = now
+ @db.set("#{set}:epoch", Time.now.to_i / @step)
end
@current
@@ -113,4 +114,4 @@ def clear_bucket(b)
end
def debug(msg); p msg if @debug; end
-end
+end
View
@@ -60,6 +60,25 @@
end
end
+ it "should wipe skipped buckets" do
+ rr.incr("test", "key", 1)
+
+ time_travel_to(Time.now + 20) do
+ rr.incr("test", "key", 1)
+
+ # fast forward & skip to an epoch with data
+ time_travel_to(Time.now + 50) do
+ rr.epoch("test").should match(/test:1/)
+ rr.score("test", "key").should == 1
+ end
+
+ # fast forward & skip to an epoch with no data
+ time_travel_to(Time.now + 150) do
+ rr.score("test", "key").should == 0
+ end
+ end
+ end
+
it "should return top N items from all epochs" do
rr.incr("test", "key1", 1)
rr.incr("test", "key2", 3)
@@ -126,7 +145,4 @@
end
end
- it "should store & verify epoch signatures for each bucket" do
- pending "otherwise, if we skip several buckets, they won't get cleared"
- end
end

0 comments on commit f23ca39

Please sign in to comment.