forked from grafana/metrictank
-
Notifications
You must be signed in to change notification settings - Fork 7
/
chunk.go
72 lines (64 loc) · 2.5 KB
/
chunk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"context"
"fmt"
"time"
"github.com/grafana/metrictank/store/cassandra"
log "github.com/sirupsen/logrus"
)
// printChunkSummary prints a summary of chunks in the store matching the given conditions, grouped in buckets of groupTTL size by their TTL
func printChunkSummary(ctx context.Context, store *cassandra.CassandraStore, tables []cassandra.Table, metrics []Metric, groupTTL string) error {
now := uint32(time.Now().Unix())
endMonth := now / cassandra.Month_sec
for _, tbl := range tables {
// per store.FindExistingTables(), actual TTL may be up to 2x what's in tablename.
// we query up to 4x so that we also include data that should have been dropped already but still sticks around for whatever reason.
start := now - uint32(4*tbl.TTL*3600)
startMonth := start / cassandra.Month_sec
fmt.Println("## Table", tbl.Name)
if len(metrics) == 0 {
query := fmt.Sprintf("select key, ttl(data) from %s", tbl.Name)
iter := store.Session.Query(query).Iter()
showKeyTTL(iter, groupTTL)
} else {
for _, metric := range metrics {
for num := startMonth; num <= endMonth; num += 1 {
row_key := fmt.Sprintf("%s_%d", metric.AMKey.String(), num)
query := fmt.Sprintf("select key, ttl(data) from %s where key=?", tbl.Name)
iter := store.Session.Query(query, row_key).Iter()
showKeyTTL(iter, groupTTL)
}
}
}
}
return nil
}
func printChunkCsv(ctx context.Context, store *cassandra.CassandraStore, table cassandra.Table, metrics []Metric, start, end uint32) {
// see CassandraStore.SearchTable for more information
startMonth := start / cassandra.Month_sec // starting row has to be at, or before, requested start
endMonth := (end - 1) / cassandra.Month_sec // ending row has to include the last point we might need (end-1)
rowKeys := make([]string, endMonth-startMonth+1)
query := fmt.Sprintf("SELECT key, ts, data FROM %s WHERE key IN ? AND ts < ?", table.Name)
for _, metric := range metrics {
i := 0
for num := startMonth; num <= endMonth; num += 1 {
rowKeys[i] = fmt.Sprintf("%s_%d", metric.AMKey.String(), num)
i++
}
params := []interface{}{rowKeys, end}
iter := store.Session.Query(query, params...).WithContext(ctx).Iter()
var key string
var ts int
var b []byte
for iter.Scan(&key, &ts, &b) {
fmt.Printf("%s,%d,0x%x\n", key, ts, b)
}
err := iter.Close()
if err != nil {
if err == context.Canceled || err == context.DeadlineExceeded {
log.Fatal("query was aborted")
}
log.Fatalf("query failure: %v", err)
}
}
}