-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtruncator.go
139 lines (125 loc) · 3.27 KB
/
truncator.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"bufio"
"fmt"
"github.com/noahhl/Go-Redis"
"github.com/noahhl/go-batsd/gobatsd"
"io"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
const NWORKERS = 4
func main() {
gobatsd.LoadConfig()
index := sort.Search(len(gobatsd.Config.Retentions), func(i int) bool { return gobatsd.Config.Retentions[i].Interval == gobatsd.Config.TargetInterval })
if index == len(gobatsd.Config.Retentions) {
if gobatsd.Config.Retentions[0].Interval == gobatsd.Config.TargetInterval {
index = 0
} else {
panic("You must specify the duration you'd like to truncate")
}
}
retention := gobatsd.Config.Retentions[index]
fmt.Printf("Starting truncation for the %v duration.\n", retention.Interval)
spec := redis.DefaultSpec().Host(gobatsd.Config.RedisHost).Port(gobatsd.Config.RedisPort)
redis, err := redis.NewSynchClientWithSpec(spec)
if err != nil {
panic(err)
}
datapoints := make([]string, 0)
if index == 0 {
rawDatapoints, err := redis.Keys("*")
if err != nil {
panic(err)
}
for _, key := range rawDatapoints {
if m, _ := regexp.MatchString("^timers|^counters", string(key)); m {
datapoints = append(datapoints, string(key))
}
}
} else {
rawDatapoints, err := redis.Smembers("datapoints")
if err != nil {
panic(err)
}
for _, key := range rawDatapoints {
if m, _ := regexp.MatchString("^timers|^counters", string(key)); m {
datapoints = append(datapoints, string(key))
}
}
}
since := float64(time.Now().Unix() - retention.Duration)
fmt.Printf("Truncating %v datapoints since %f.\n", len(datapoints), since)
if index == 0 { //Redis truncation
for _, key := range datapoints {
redis.Zremrangebyscore(key, 0.0, since)
}
} else {
nworkers := NWORKERS
if nworkers > len(datapoints) {
nworkers = len(datapoints)
}
c := make(chan int, nworkers)
for i := 0; i < nworkers; i++ {
go func(datapoints []string, i int, c chan int) {
for _, key := range datapoints {
metricName := key + ":" + strconv.FormatInt(retention.Interval, 10)
if m, _ := regexp.MatchString("^timers", metricName); m {
metricName += ":2"
}
TruncateOnDisk(metricName, since)
}
c <- 1
}(datapoints[i*len(datapoints)/nworkers:(i+1)*len(datapoints)/nworkers-1], i, c)
}
for i := 0; i < nworkers; i++ {
<-c
}
}
}
func TruncateOnDisk(metric string, since float64) {
filePath := gobatsd.CalculateFilename(metric, gobatsd.Config.Root)
file, err := os.Open(filePath)
tmpfile, writeErr := os.Create(filePath + "tmp")
if writeErr != nil {
panic(writeErr)
}
if err == nil && writeErr == nil {
linesWritten := 0
reader := bufio.NewReader(file)
writer := bufio.NewWriter(tmpfile)
for {
line, err := reader.ReadString('\n')
if err != nil && err != io.EOF {
panic(err)
}
if err != nil && err == io.EOF {
break
}
//skip the header in v2 files
if line[0] == 'v' {
writer.WriteString(line)
} else {
parts := strings.Split(strings.TrimSpace(line), " ")
ts, _ := strconv.ParseFloat(parts[0], 64)
if ts >= since {
writer.WriteString(line)
linesWritten++
}
}
}
writer.Flush()
file.Close()
tmpfile.Close()
if linesWritten > 0 {
os.Rename(filePath+"tmp", filePath)
} else {
os.Remove(filePath + "tmp")
os.Remove(filePath)
}
}
}