Skip to content

Commit

Permalink
cmd/bosun: serving temporary configs from redis.
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Peterson committed Feb 12, 2016
1 parent a70b059 commit 0fd695e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 46 deletions.
47 changes: 47 additions & 0 deletions cmd/bosun/database/config_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package database

import (
"crypto/md5"
"encoding/base64"

"bosun.org/_third_party/github.com/garyburd/redigo/redis"

"bosun.org/collect"
"bosun.org/opentsdb"
)

type ConfigDataAccess interface {
SaveTempConfig(text string) (hash string, err error)
GetTempConfig(hash string) (text string, err error)
}

func (d *dataAccess) Configs() ConfigDataAccess {
return d
}

const configLifetime = 60 * 24 * 14 // 2 weeks

func (d *dataAccess) SaveTempConfig(text string) (string, error) {
defer collect.StartTimer("redis", opentsdb.TagSet{"op": "SaveTempConfig"})()
conn := d.GetConnection()
defer conn.Close()

sig := md5.Sum([]byte(text))
b64 := base64.StdEncoding.EncodeToString(sig[0:8])
_, err := conn.Do("SET", "tempConfig:"+b64, text, "EX", configLifetime)
return b64, err
}

func (d *dataAccess) GetTempConfig(hash string) (string, error) {
defer collect.StartTimer("redis", opentsdb.TagSet{"op": "GetTempConfig"})()
conn := d.GetConnection()
defer conn.Close()

key := "tempConfig:" + hash
dat, err := redis.String(conn.Do("GET", key))
if err != nil {
return "", err
}
_, err = conn.Do("EXPIRE", key, configLifetime)
return dat, err
}
1 change: 1 addition & 0 deletions cmd/bosun/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
// Core data access interface for everything sched needs
type DataAccess interface {
Metadata() MetadataDataAccess
Configs() ConfigDataAccess
Search() SearchDataAccess
Errors() ErrorDataAccess
State() StateDataAccess
Expand Down
45 changes: 1 addition & 44 deletions cmd/bosun/sched/bolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package sched
import (
"bytes"
"compress/gzip"
"crypto/md5"
"encoding/base64"
"encoding/gob"
"encoding/json"
"fmt"
"time"

Expand Down Expand Up @@ -74,47 +71,6 @@ type storedConfig struct {
LastUsed time.Time
}

// Saves the provided config text in state file for later access.
// Returns a hash of the file to be used as a retreival key.
func (s *Schedule) SaveTempConfig(text string) (hash string, err error) {
sig := md5.Sum([]byte(text))
b64 := base64.StdEncoding.EncodeToString(sig[0:5])
data := storedConfig{Text: text, LastUsed: utcNow()}
bindata, err := json.Marshal(data)
if err != nil {
return "", err
}
err = s.db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte(dbConfigTextBucket))
if err != nil {
return err
}
return b.Put([]byte(b64), bindata)
})
if err != nil {
return "", err
}
return b64, nil
}

// Retreive the specified config text from state file.
func (s *Schedule) LoadTempConfig(hash string) (text string, err error) {
config := storedConfig{}
err = s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(dbConfigTextBucket))
data := b.Get([]byte(hash))
if data == nil || len(data) == 0 {
return fmt.Errorf("Config text '%s' not found", hash)
}
return json.Unmarshal(data, &config)
})
if err != nil {
return "", err
}
go s.SaveTempConfig(config.Text) //refresh timestamp.
return config.Text, nil
}

func migrateOldDataToRedis(db *bolt.DB, data database.DataAccess, s *Schedule) error {
if err := migrateMetricMetadata(db, data); err != nil {
return err
Expand All @@ -136,6 +92,7 @@ func migrateOldDataToRedis(db *bolt.DB, data database.DataAccess, s *Schedule) e
}
return nil
}

func migrateNotifications(db *bolt.DB, s *Schedule) error {
migrated, err := isMigrated(db, "notifications")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/bosun/web/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ func buildConfig(r *http.Request) (c *conf.Conf, a *conf.Alert, hash string, err
}
c.StateFile = ""

hash, err = sched.DefaultSched.SaveTempConfig(string(config))
hash, err = sched.DefaultSched.DataAccess.Configs().SaveTempConfig(string(config))
if err != nil {
return nil, nil, "", err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/bosun/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ func Config(t miniprofiler.Timer, w http.ResponseWriter, r *http.Request) {
var text string
var err error
if hash := r.FormValue("hash"); hash != "" {
text, err = schedule.LoadTempConfig(hash)
text, err = schedule.DataAccess.Configs().GetTempConfig(hash)
if err != nil {
serveError(w, err)
return
Expand Down

0 comments on commit 0fd695e

Please sign in to comment.