forked from asonawalla/gazette
/
shards_prune.go
143 lines (119 loc) · 4.11 KB
/
shards_prune.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
package main
import (
"context"
"github.com/LiveRamp/gazette/v2/pkg/client"
"github.com/LiveRamp/gazette/v2/pkg/consumer"
"github.com/LiveRamp/gazette/v2/pkg/fragment"
mbp "github.com/LiveRamp/gazette/v2/pkg/mainboilerplate"
pb "github.com/LiveRamp/gazette/v2/pkg/protocol"
"github.com/LiveRamp/gazette/v2/pkg/recoverylog"
log "github.com/sirupsen/logrus"
)
type cmdShardsPrune struct {
pruneConfig
}
func init() {
_ = mustAddCmd(cmdShards, "prune", "Removes fragments of a hinted recovery log which are no longer needed", `
Recovery logs capture every write which has ever occurred in a Shard DB.
This includes all prior writes of client keys & values, and also RocksDB
compactions, which can significantly inflate the total volume of writes
relative to the data currently represented in a RocksDB.
Prune log examines the provided hints to identify Fragments of the log
which have no intersection with any live files of the DB, and can thus
be safely deleted.
`, &cmdShardsPrune{})
}
func (cmd *cmdShardsPrune) Execute([]string) error {
startup()
var ctx = context.Background()
var m = shardsPruneMetrics{}
for _, shard := range listShards(cmd.Selector).Shards {
m.shardsTotal++
var lastHints = fetchLastHints(ctx, shard.Spec.Id)
if lastHints == nil {
log.Infof("skipping shard %s, there are no backup hints for this shard", shard.Spec.Id)
continue
}
var _, segments, err = lastHints.LiveLogSegments()
if err != nil {
mbp.Must(err, "unable to fetch hint segments")
}
// Zero the LastOffset of the final hinted Segment. This has the effect of implicitly
// intersecting with all fragments having offsets greater than its FirstOffset.
// We want this behavior because playback will continue to read offsets & Fragments
// after reading past the final hinted Segment.
segments[len(segments)-1].LastOffset = 0
for _, f := range fetchFragments(ctx, lastHints.Log) {
var spec = f.Spec
m.fragmentsTotal++
m.bytesTotal += spec.ContentLength()
if len(segments.Intersect(spec.Begin, spec.End)) == 0 {
log.WithFields(log.Fields{
"log": spec.Journal,
"name": spec.ContentName(),
"size": spec.ContentLength(),
"mod": spec.ModTime,
}).Info("pruning fragment")
m.fragmentsPruned++
m.bytesPruned += spec.ContentLength()
if !cmd.DryRun {
err = fragment.Remove(ctx, spec)
if err != nil {
mbp.Must(err, "error removing fragment", "path", spec.ContentPath())
}
}
}
}
logShardsPruneMetrics(m, shard.Spec.Id.String(), "finished pruning log for shard")
}
logShardsPruneMetrics(m, "", "finished pruning log for all shards")
return nil
}
func fetchLastHints(ctx context.Context, id consumer.ShardID) *recoverylog.FSMHints {
var req = &consumer.GetHintsRequest{
Shard: id,
}
var resp, err = consumer.FetchHints(ctx, consumer.NewShardClient(shardsCfg.Consumer.Dial(ctx)), req)
mbp.Must(err, "failed to fetch hints")
if resp.Status != consumer.Status_OK {
log.Panic("failed to fetch hints ", resp.Status.String())
}
for i := len(resp.BackupHints) - 1; i >= 0; i-- {
if resp.BackupHints[i].Hints != nil {
return resp.BackupHints[i].Hints
}
}
return nil
}
func fetchFragments(ctx context.Context, journal pb.Journal) []pb.FragmentsResponse__Fragment {
var err error
var req = pb.FragmentsRequest{
Journal: journal,
}
var brokerClient = journalsCfg.Broker.RoutedJournalClient(ctx)
resp, err := client.ListAllFragments(ctx, brokerClient, req)
mbp.Must(err, "failed to fetch fragments")
return resp.Fragments
}
type shardsPruneMetrics struct {
shardsTotal int64
fragmentsTotal int64
fragmentsPruned int64
bytesTotal int64
bytesPruned int64
}
func logShardsPruneMetrics(m shardsPruneMetrics, shard, message string) {
var fields = log.Fields{
"shardsTotal": m.shardsTotal,
"fragmentsTotal": m.fragmentsTotal,
"fragmentsPruned": m.fragmentsPruned,
"fragmentsKept": m.fragmentsTotal - m.fragmentsPruned,
"bytesTotal": m.bytesTotal,
"bytesPruned": m.bytesPruned,
"bytesKept": m.bytesTotal - m.bytesPruned,
}
if shard != "" {
fields["shard"] = shard
}
log.WithFields(fields).Info(message)
}