Skip to content

Commit

Permalink
Merge branch 'master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
hit9 committed Jan 26, 2016
2 parents 3533ac1 + 0f2b6ed commit 704c61f
Show file tree
Hide file tree
Showing 31 changed files with 471 additions and 1,050 deletions.
28 changes: 2 additions & 26 deletions alerter/alerter.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,7 @@ func (al *Alerter) work() {
metric := <-al.In
// Check interval.
v, ok := al.m.Get(metric.Name)
if ok {
if v.(uint32)%2 == 1 {
//last time alert, check interval
if metric.Stamp-(v.(uint32)-1)/2 < al.cfg.Alerter.Interval {
//last time in interval, don't alert
continue
} else {
//don't alert set stamp+0
al.m.Set(metric.Name, metric.Stamp*2+0)
continue
}
} else {
//last time did not alert, check interval
if metric.Stamp-v.(uint32)/2 < 60 {
//in interval,alert
} else {
//not in interval, don't alert set stamp+0
al.m.Set(metric.Name, metric.Stamp*2+0)
continue
}

}
} else {
//first time don't alert, set stamp+0
al.m.Set(metric.Name, metric.Stamp*2+0)
if ok && metric.Stamp-v.(uint32) < al.cfg.Alerter.Interval {
continue
}
// Check alert times in one day
Expand Down Expand Up @@ -160,7 +136,7 @@ func (al *Alerter) work() {
log.Info("send message to %s with %s ok", user.Name, metric.Name)
}
if len(users) != 0 {
al.m.Set(metric.Name, metric.Stamp*2+1)
al.m.Set(metric.Name, metric.Stamp)
}
}
}
Expand Down
41 changes: 9 additions & 32 deletions cleaner/cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,23 @@
package cleaner

import (
"github.com/eleme/banshee/config"
"github.com/eleme/banshee/storage"
"github.com/eleme/banshee/util/log"
"time"
)

// The time interval to check outdated data to clean is:
// period / periodNumToInterval. If the period is 1 day, this interval should
// be 4 hours.
const periodNumToInterval uint32 = 6

// The time expiration for metrics is:
// period * metricExpirationNumToPeriod. If the period is 1 day, this
// expiration should be 7 days.
const metricExpirationNumToPeriod uint32 = 7

// The time expiration for metric/index/state is:
// period * expirationNumToPeriod. If the period is 1 day, this expiration
// should be 3 days.
const expirationNumToPeriod uint32 = 3

// Cleaner is to clean outdated data.
type Cleaner struct {
// Config
cfg *config.Config
// Storage
db *storage.DB
// Expiration
metricExpiration uint32
expiration uint32
// Interval
interval uint32
}

// New creates a cleaner.
func New(db *storage.DB, period uint32) *Cleaner {
c := new(Cleaner)
c.db = db
c.metricExpiration = metricExpirationNumToPeriod * period
c.expiration = expirationNumToPeriod * period
c.interval = period / periodNumToInterval
return c
func New(cfg *config.Config, db *storage.DB) *Cleaner {
return &Cleaner{cfg, db}
}

// clean checks all indexes and do cleaning.
Expand All @@ -52,15 +30,14 @@ func (c *Cleaner) clean() {
// Use local server time and uint32 is enough for further 90 years
now := uint32(time.Now().Unix())
for _, idx := range idxs {
if idx.Stamp+c.expiration < now {
if idx.Stamp+c.cfg.Cleaner.Threshold < now {
// Long time no data, clean all.
c.db.State.Delete(idx.Name)
c.db.Index.Delete(idx.Name)
c.db.Metric.DeleteTo(idx.Name, idx.Stamp+1) // DeleteTo is right closed
log.Info("%s fully cleaned", idx.Name)
} else {
// Clean outdated metrics.
n, _ := c.db.Metric.DeleteTo(idx.Name, now-c.metricExpiration)
n, _ := c.db.Metric.DeleteTo(idx.Name, now-c.cfg.Expiration)
if n > 0 {
log.Info("%s %d outdated metrics cleaned", idx.Name, n)
}
Expand All @@ -70,11 +47,11 @@ func (c *Cleaner) clean() {

// Start a time ticker to clean.
func (c *Cleaner) Start() {
log.Info("start cleaner with interval %ds..", c.interval)
log.Info("start cleaner..")
// Clean right now.
c.clean()
// Clean each interval.
ticker := time.NewTicker(time.Duration(c.interval) * time.Second)
ticker := time.NewTicker(time.Duration(c.cfg.Cleaner.Interval) * time.Second)
for {
<-ticker.C
c.clean()
Expand Down
39 changes: 6 additions & 33 deletions cleaner/cleaner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,26 @@
package cleaner

import (
"github.com/eleme/banshee/config"
"github.com/eleme/banshee/models"
"github.com/eleme/banshee/storage"
"github.com/eleme/banshee/storage/indexdb"
"github.com/eleme/banshee/storage/statedb"
"github.com/eleme/banshee/util/assert"
"os"
"testing"
"time"
)

func TestNew(t *testing.T) {
numGrid := uint32(288)
gridLen := uint32(300)
period := numGrid * gridLen
// Open storage
dbFileName := "db-test"
dbOptions := &storage.Options{numGrid, gridLen}
db, _ := storage.Open(dbFileName, dbOptions)
defer os.RemoveAll(dbFileName)
defer db.Close()
// Create cleaner
c := New(db, period)
assert.Ok(t, c.expiration == expirationNumToPeriod*period)
assert.Ok(t, c.metricExpiration == metricExpirationNumToPeriod*period)
assert.Ok(t, c.interval*periodNumToInterval == period)
}

func TestClean(t *testing.T) {
numGrid := uint32(288)
gridLen := uint32(300)
period := numGrid * gridLen
// Config
cfg := config.New()
// Open storage
dbFileName := "db-test"
dbOptions := &storage.Options{numGrid, gridLen}
db, _ := storage.Open(dbFileName, dbOptions)
db, _ := storage.Open(dbFileName)
defer os.RemoveAll(dbFileName)
defer db.Close()
// Create cleaner
c := New(db, period)
c := New(cfg, db)
// Add outdated data.
// Case fully cleaned: 3 days no new data
m1 := &models.Metric{Name: "fully-case", Stamp: uint32(time.Now().Unix() - 3*3600*24 - 1)}
Expand All @@ -55,10 +36,6 @@ func TestClean(t *testing.T) {
db.Metric.Put(m1)
db.Metric.Put(m2)
db.Metric.Put(m3)
// Pit States.
db.State.Put(m1, &models.State{})
db.State.Put(m2, &models.State{})
db.State.Put(m3, &models.State{})
// Put indexes.
db.Index.Put(i1)
db.Index.Put(i2)
Expand All @@ -68,18 +45,14 @@ func TestClean(t *testing.T) {
var err error
_, err = db.Index.Get(m1.Name)
assert.Ok(t, err == indexdb.ErrNotFound)
_, err = db.State.Get(m1)
assert.Ok(t, err == statedb.ErrNotFound)
l, err := db.Metric.Get(m1.Name, 0, uint32(time.Now().Unix()))
assert.Ok(t, len(l) == 0)
// m2 should be cleaned and m3 shouldn't be cleaned
l, err = db.Metric.Get(m2.Name, m2.Stamp, uint32(time.Now().Unix()))
assert.Ok(t, len(l) == 1)
assert.Ok(t, l[0].Name == m2.Name)
assert.Ok(t, l[0].Stamp == m3.Stamp && l[0].Stamp != m2.Stamp)
// m2/m3's state and index shouldn't be cleaned
// m2/m3's index shouldn't be cleaned
i, err := db.Index.Get(m2.Name)
assert.Ok(t, err == nil && i.Name == m2.Name)
s, err := db.State.Get(m2)
assert.Ok(t, err == nil && s != nil)
}
Loading

0 comments on commit 704c61f

Please sign in to comment.