forked from asonawalla/gazette
/
main.go
211 lines (180 loc) · 5.58 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// gazretention is used to expose fragments from cloud storage that we no longer
// wish to retain.
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"sync"
"sync/atomic"
"time"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"github.com/LiveRamp/gazette/pkg/cloudstore"
"github.com/LiveRamp/gazette/pkg/envflagfactory"
"github.com/LiveRamp/gazette/pkg/mainboilerplate"
"github.com/LiveRamp/gazette/pkg/metrics"
)
const (
oneMb = 1024 * 1024
)
var (
prefix = flag.String("prefix", "", "The directory prefix to search.")
dur = flag.String("dur", "0s",
"Files found that are older than |dur| from now will be returned.")
config = flag.String("config", "",
"Filepath to JSON config file of an array of prefix and durations.")
nprocs = flag.Int("nprocs", 8,
"How many deletes to run in parallel")
cloudFSUrl = envflagfactory.NewCloudFSURL()
)
type cfsFragment struct {
os.FileInfo
path string
prefix string
}
// findExpiredFragments searches the provided cloudstore filesystem |cfs| under
// the directory specified by |prefix| and returns any files found modified
// after now - |duration|.
func appendExpiredFragments(prefix string, duration time.Duration,
frags []*cfsFragment, cfs cloudstore.FileSystem) ([]*cfsFragment, error) {
var horizon time.Time
// Default of a 0 duration keeps any files written after unix epoch.
if duration == 0 {
horizon = time.Unix(0, 0)
} else {
horizon = time.Now().Add(-duration)
}
// Get all fragments associated with journal older than retention time.
if err := cfs.Walk(prefix, func(fname string, finfo os.FileInfo, err error) error {
var modTime = finfo.ModTime()
var sizeMb = float64(finfo.Size()) / oneMb
if modTime.Before(horizon) {
log.WithFields(log.Fields{
"cfsPath": fname,
"fragment": finfo.Name(),
"sizeMb": sizeMb,
"lastModTime": modTime,
}).Debug("Expired fragment found...")
frags = append(frags, &cfsFragment{finfo, fname, prefix})
} else {
// Stats collection while we're parsing journal fragments anyway.
// Only increment on fragments we plan on keeping.
metrics.GazretentionRetainedFragmentsTotal.WithLabelValues(prefix).Inc()
metrics.GazretentionRetainedBytesTotal.WithLabelValues(prefix).Add(float64(finfo.Size()))
}
return nil
}); err != nil {
return nil, err
}
return frags, nil
}
// deleteExpiredFrags deletes and emits stats on expired fragments |expFrags|
// found on the filesystem |cfs|.
func deleteExpiredFrags(expFrags []*cfsFragment, cfs cloudstore.FileSystem) error {
// Make a channel that accepts fragments.
var ch = make(chan *cfsFragment, 256)
var numErrs int64 = 0
// Make a WaitGroup that tracks the completion of the goroutines.
var wg sync.WaitGroup
wg.Add(*nprocs)
// Create |*nprocs| goroutines that read from the channel and delete files
// in a loop, incrementing a stats every time.
for i := 0; i < *nprocs; i++ {
go func(f <-chan *cfsFragment) {
for frag := range f {
if err := cfs.Remove(frag.path); err != nil {
atomic.AddInt64(&numErrs, 1)
log.WithField("err", err).Error("error deleting file.")
} else {
metrics.GazretentionDeletedFragmentsTotal.
WithLabelValues(frag.prefix).Inc()
metrics.GazretentionDeletedBytesTotal.
WithLabelValues(frag.prefix).Add(float64(frag.Size()))
log.WithField("path", frag.path).Debug("deleted file.")
}
}
wg.Done()
}(ch)
}
// Fill the channel with the content of |expFrags|.
for _, frag := range expFrags {
ch <- frag
}
// Tell the goroutines to die.
close(ch)
// Wait for the goroutines to finish.
wg.Wait()
if numErrs > 0 {
return fmt.Errorf("could not delete %d files.", numErrs)
} else {
return nil
}
}
// readAndParseConf reads a JSON-formatted config file |fp| which contains a map
// of prefix and retention durations.
func readAndParseConf(fp string) (map[string]string, error) {
var err error
var raw []byte
var confMap map[string]string
raw, err = ioutil.ReadFile(fp)
if err != nil {
return confMap, err
}
err = json.Unmarshal(raw, &confMap)
if err != nil {
return confMap, err
}
return confMap, nil
}
func main() {
defer mainboilerplate.LogPanic()
mainboilerplate.Initialize()
prometheus.MustRegister(metrics.GazretentionCollectors()...)
var err error
var cfs cloudstore.FileSystem
var tdur time.Duration
var confMap map[string]string
var expFrags []*cfsFragment
// Get either prefix or filepath to parse.
if *prefix != "" {
confMap[*prefix] = *dur
} else if *config != "" {
confMap, err = readAndParseConf(*config)
if err != nil {
log.WithField("err", err).Fatal("Failed to parse conf file")
}
} else {
log.Fatal("-prefix or -config must be specified.")
}
cfs, err = cloudstore.NewFileSystem(nil, *cloudFSUrl)
if err != nil {
log.WithField("err", err).Fatal("cannot initialize cloudstore.")
}
for pref, duration := range confMap {
log.WithField("prefix", pref).
Info("Gathering expired journal fragments...")
tdur, err = time.ParseDuration(duration)
if err != nil {
log.WithField("err", err).Error("invalid retention duration.")
continue
}
expFrags, err = appendExpiredFragments(pref, tdur, expFrags, cfs)
if err != nil {
log.WithField("err", err).Error("cannot parse filesystem.")
}
}
if len(expFrags) > 0 {
log.Info("Deleting expired fragments...")
err = deleteExpiredFrags(expFrags, cfs)
if err != nil {
log.WithField("err", err).Fatal("Unable to delete expired fragments.")
}
} else {
log.Info("No expired fragments found!")
}
log.Infof("Done! Uncovered and performed delete on %d fragments.",
len(expFrags))
}