Skip to content

Commit

Permalink
Small utility to clean up search data for a metric.
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Peterson committed Apr 6, 2016
1 parent 50185c9 commit 458dc84
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/bosun/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ func (d *dataAccess) SCLEAR() string {
return "SCLEAR"
}

func (d *dataAccess) HCLEAR() string {
if d.isRedis {
return "DEL"
}
return "HCLEAR"
}

func (d *dataAccess) LMCLEAR(key string, value string) (string, []interface{}) {
if d.isRedis {
return "LREM", []interface{}{key, 0, value}
Expand Down
39 changes: 39 additions & 0 deletions cmd/bosun/database/search_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ metric "__all__" is a special key that will hold all values for the tag key, reg
All Metrics:
search:allMetrics -> hash of metric name to timestamp
search:mts:{metric} -> all tag sets for a metric. Hash with time stamps
*/

const Search_All = "__all__"
Expand Down Expand Up @@ -207,3 +209,40 @@ func (d *dataAccess) LoadLastInfos() (map[string]map[string]*LastInfo, error) {
}
return m, nil
}

//This function not exposed on any public interface. See cmd/bosun/database/test/util/purge_search_data.go for usage.
func (d *dataAccess) PurgeSearchData(metric string, noop bool) error {
defer collect.StartTimer("redis", opentsdb.TagSet{"op": "PurgeSearchData"})()
conn := d.GetConnection()
defer conn.Close()

tagKeys, err := d.GetTagKeysForMetric(metric)
if err != nil {
return err
}
fmt.Println("HDEL", searchAllMetricsKey)
if !noop {
_, err = conn.Do("HDEL", searchAllMetricsKey, metric)
if err != nil {
return err
}
}
hashesToDelete := []string{
searchMetricTagSetKey(metric),
searchTagkKey(metric),
}
for tagk := range tagKeys {
hashesToDelete = append(hashesToDelete, searchTagvKey(metric, tagk))
}
cmd := d.HCLEAR()
for _, hash := range hashesToDelete {
fmt.Println(cmd, hash)
if !noop {
_, err = conn.Do(cmd, hash)
if err != nil {
return err
}
}
}
return nil
}
28 changes: 28 additions & 0 deletions cmd/bosun/database/test/util/purge_search_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"flag"
"fmt"

"bosun.org/cmd/bosun/database"
)

// USAGE: go run purge_search_data.go -r redishost:6379 -m metricIWantToClear
// use -n to see what would get deleted, without changing any data

var redis = flag.String("r", "", "redis host:port")
var metric = flag.String("m", "", "metric to purge")
var noop = flag.Bool("n", false, "only print commands, don't run.")

func main() {
flag.Parse()
if *redis == "" || *metric == "" {
flag.PrintDefaults()
return
}
db := database.NewDataAccess(*redis, true, 0, "").(interface {
PurgeSearchData(string, bool) error
})
err := db.PurgeSearchData(*metric, *noop)
fmt.Println(err)
}

0 comments on commit 458dc84

Please sign in to comment.