Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small utility to clean up search data for a metric. #1632

Merged
merged 1 commit into from
Apr 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}