-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulk_deleted.go
374 lines (321 loc) · 9.6 KB
/
bulk_deleted.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package index
import (
"context"
"errors"
"sync"
"github.com/bleemeo/squirreldb/types"
"github.com/gocql/gocql"
"github.com/pilosa/pilosa/v2/roaring"
"github.com/prometheus/prometheus/model/labels"
)
type deleter struct {
c *CassandraIndex
labelToPostingUpdates map[labels.Label]int
deleteLabels []string
deleteIDs []uint64
unshardedPostingUpdates []postingUpdateRequest
invalidateKey []postingsCacheKey
}
func newBulkDeleter(c *CassandraIndex) *deleter {
return &deleter{
labelToPostingUpdates: make(map[labels.Label]int),
c: c,
}
}
// PrepareDelete add a metric to be deleted. No read is performed at this point and no lock is required
// sortedLabels may be nil if labels are unknown (so only ID is deleted from all postings).
// Skipping labels2id is used by index fix. It shouldn't be used in normal condition.
func (d *deleter) PrepareDelete(id types.MetricID, sortedLabels labels.Labels, skipLabels2Id bool) {
d.deleteIDs = append(d.deleteIDs, uint64(id))
if sortedLabels != nil && !skipLabels2Id {
sortedLabelsString := sortedLabels.String()
d.deleteLabels = append(d.deleteLabels, sortedLabelsString)
}
for _, lbl := range sortedLabels {
d.invalidateKey = append(d.invalidateKey, postingsCacheKey{
Shard: globalShardNumber,
Name: lbl.Name,
Value: lbl.Value,
})
}
labelsList := make(labels.Labels, 0, len(sortedLabels)*2)
labelsList = append(labelsList, sortedLabels...)
for _, label := range sortedLabels {
labelsList = append(labelsList, labels.Label{
Name: postinglabelName,
Value: label.Name,
})
}
for _, label := range labelsList {
idx, ok := d.labelToPostingUpdates[label]
if !ok {
idx = len(d.unshardedPostingUpdates)
d.unshardedPostingUpdates = append(d.unshardedPostingUpdates, postingUpdateRequest{
Label: label,
})
d.labelToPostingUpdates[label] = idx
}
d.unshardedPostingUpdates[idx].RemoveIDs = append(d.unshardedPostingUpdates[idx].RemoveIDs, uint64(id))
}
}
// Delete perform the deletion and REQUIRE the newMetricGlobalLock.
//
// The method should be called only once, a new deleter should be created to reuse it.
//
// # This method will also remove IDs local in-memory cache
//
// Deletion is performed in reverse order of the creation of metrics:
// - First we delete from labels2id (this cause any new write to create a new metrics)
// - drop id from every sharded postings (TODO: during those deletes, Search() may wrongly
// return the metric ID in cause of negative search.)
// - drop the id2labels (at this points, Search() can elimitate wrong metric ID)
// - the remote the id from all postings, making the ID free
//
// Note: it's not this function which clear the expiration table, this is done elsewhere.
func (d *deleter) Delete(ctx context.Context) error {
if len(d.deleteIDs) == 0 {
return nil
}
idsCopy := make([]uint64, len(d.deleteIDs))
copy(idsCopy, d.deleteIDs)
d.c.logger.Debug().Msgf("deleting metric ID: %v", truncatedSliceIDList(d.deleteIDs, 50))
// Delete metrics from cache *before* processing to Cassandra.
// Doing this ensure that if a write for a metric that in being delete will
// trigger the creation of a new metrics (which will wait for complet delete since we hold the lock).
// Note: in case of multiple SquirrelDB, this race-condition may still happen, but the only consequence
// is leaving an orphaned id2labels entry that will not be used and will eventually be purged when its
// expiration is reached.
d.c.deleteIDsFromCache(idsCopy)
err := d.c.concurrentTasks(
ctx,
concurrentDelete,
func(ctx context.Context, work chan<- func() error) error {
for _, sortedLabelsString := range d.deleteLabels {
task := func() error {
err := d.c.store.DeleteLabels2ID(ctx, sortedLabelsString)
if err != nil && !errors.Is(err, gocql.ErrNotFound) {
return err
}
return nil
}
select {
case work <- task:
case <-ctx.Done():
return ctx.Err()
}
}
return nil
},
)
if err != nil {
return err
}
shards, err := d.c.postings(ctx, []int32{globalShardNumber}, existingShardsLabel, existingShardsLabel, false)
if err != nil {
return err
}
maybePresent, err := d.c.getMaybePresent(ctx, shards.Slice())
if err != nil {
return err
}
shardDeleter := newPostingsInShardDeleter(d.c)
allDeleteIDs := roaring.NewBitmap(d.deleteIDs...)
for _, shard := range shards.Slice() {
shard := int32(shard)
it := maybePresent[shard]
if it == nil || !it.Any() {
// The shard exists in existingShardsLabel but is fully empty.
// This usually occur if a Cassandra error happen after deleting from maybePresence and before
// deleting it from existingShardsLabel.
// Cleanup entry in existingShardsLabel
shardDeleter.ShardsListUpdate.RemoveIDs = append(shardDeleter.ShardsListUpdate.RemoveIDs, uint64(shard))
continue
}
if allDeleteIDs.IntersectionCount(it) == 0 {
continue
}
shardDeleter.PresenceUpdates = append(shardDeleter.PresenceUpdates, postingUpdateRequest{
Shard: shard,
Label: labels.Label{
Name: allPostingLabel,
Value: allPostingLabel,
},
RemoveIDs: d.deleteIDs,
})
for _, req := range d.unshardedPostingUpdates {
ids := roaring.NewBitmap(req.RemoveIDs...)
if ids.IntersectionCount(it) == 0 {
continue
}
req.Shard = shard
shardDeleter.PostingUpdates = append(shardDeleter.PostingUpdates, req)
}
shardDeleter.MaybePresenceUpdates = append(shardDeleter.MaybePresenceUpdates, postingUpdateRequest{
Shard: shard,
Label: labels.Label{
Name: maybePostingLabel,
Value: maybePostingLabel,
},
RemoveIDs: d.deleteIDs,
})
}
d.c.invalidatePostings(ctx, d.invalidateKey)
err = shardDeleter.Delete(ctx)
if err != nil {
return err
}
err = d.c.concurrentTasks(
ctx,
concurrentDelete,
func(ctx context.Context, work chan<- func() error) error {
for _, id := range d.deleteIDs {
id := types.MetricID(id)
task := func() error {
err := d.c.store.DeleteID2Labels(ctx, id)
if err != nil && !errors.Is(err, gocql.ErrNotFound) {
return err
}
return nil
}
select {
case work <- task:
case <-ctx.Done():
return ctx.Err()
}
}
return nil
},
)
if err != nil {
return err
}
_, err = d.c.postingUpdate(ctx, postingUpdateRequest{
Shard: globalShardNumber,
Label: labels.Label{Name: globalAllPostingLabel, Value: globalAllPostingLabel},
RemoveIDs: d.deleteIDs,
})
if err != nil && !errors.Is(err, errBitmapEmpty) && !errors.Is(err, gocql.ErrNotFound) {
return err
}
return nil
}
type postingsInShardDeleter struct {
l sync.Mutex
c *CassandraIndex
PostingUpdates []postingUpdateRequest
PresenceUpdates []postingUpdateRequest
MaybePresenceUpdates []postingUpdateRequest
ShardsListUpdate postingUpdateRequest
}
func newPostingsInShardDeleter(c *CassandraIndex) *postingsInShardDeleter {
return &postingsInShardDeleter{
c: c,
ShardsListUpdate: postingUpdateRequest{
Shard: globalShardNumber,
Label: labels.Label{
Name: existingShardsLabel,
Value: existingShardsLabel,
},
},
}
}
// Delete apply the deletion requested in all Update fields.
// Delete isn't thread safe and shouldn't be called twice.
// ShardsListUpdate fields is mutated.
func (d *postingsInShardDeleter) Delete(ctx context.Context) error {
err := d.c.concurrentTasks(
ctx,
concurrentInsert,
func(ctx context.Context, work chan<- func() error) error {
for _, req := range d.PresenceUpdates {
task := func() error {
bitmap, err := d.c.postingUpdate(ctx, req)
if err != nil && !errors.Is(err, gocql.ErrNotFound) && !errors.Is(err, errBitmapEmpty) {
return err
}
d.c.lookupIDMutex.Lock()
if bitmap == nil || errors.Is(err, errBitmapEmpty) {
delete(d.c.idInShard, req.Shard)
} else {
d.c.idInShard[req.Shard] = bitmap
}
d.c.lookupIDMutex.Unlock()
return nil
}
select {
case work <- task:
case <-ctx.Done():
return ctx.Err()
}
}
return nil
},
)
if err != nil {
return err
}
err = d.c.concurrentTasks(
ctx,
concurrentInsert,
func(ctx context.Context, work chan<- func() error) error {
for _, req := range d.PostingUpdates {
task := func() error {
_, err := d.c.postingUpdate(ctx, req)
if err != nil && !errors.Is(err, gocql.ErrNotFound) && !errors.Is(err, errBitmapEmpty) {
return err
}
return nil
}
select {
case work <- task:
case <-ctx.Done():
return ctx.Err()
}
}
return nil
},
)
if err != nil {
return err
}
if ctx.Err() != nil {
return ctx.Err()
}
err = d.c.concurrentTasks(
ctx,
concurrentInsert,
func(ctx context.Context, work chan<- func() error) error {
for _, req := range d.MaybePresenceUpdates {
task := func() error {
it, err := d.c.postingUpdate(ctx, req)
if err != nil && !errors.Is(err, gocql.ErrNotFound) && !errors.Is(err, errBitmapEmpty) {
return err
}
// it is nil iff it's empty (or an error occure)
if it == nil || errors.Is(err, errBitmapEmpty) {
d.l.Lock()
d.ShardsListUpdate.RemoveIDs = append(d.ShardsListUpdate.RemoveIDs, uint64(req.Shard))
d.l.Unlock()
}
return nil
}
select {
case work <- task:
case <-ctx.Done():
return ctx.Err()
}
}
return nil
},
)
if err != nil {
return err
}
if len(d.ShardsListUpdate.RemoveIDs) > 0 {
_, err := d.c.postingUpdate(ctx, d.ShardsListUpdate)
if err != nil && !errors.Is(err, gocql.ErrNotFound) && !errors.Is(err, errBitmapEmpty) {
return err
}
}
return nil
}