-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
bucket_client.go
319 lines (264 loc) · 9.72 KB
/
bucket_client.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
package bucketclient
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"io"
"strings"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
"github.com/thanos-io/objstore"
"golang.org/x/sync/errgroup"
"github.com/grafana/loki/v3/pkg/ruler/rulespb"
"github.com/grafana/loki/v3/pkg/ruler/rulestore"
"github.com/grafana/loki/v3/pkg/storage/bucket"
)
const (
// The bucket prefix under which all tenants rule groups are stored.
rulesPrefix = "rules"
loadConcurrency = 10
)
var (
errInvalidRuleGroupKey = errors.New("invalid rule group object key")
errEmptyUser = errors.New("empty user")
errEmptyNamespace = errors.New("empty namespace")
errEmptyGroupName = errors.New("empty group name")
)
// BucketRuleStore is used to support the RuleStore interface against an object storage backend. It is implemented
// using the Thanos objstore.Bucket interface
type BucketRuleStore struct {
bucket objstore.Bucket
cfgProvider bucket.SSEConfigProvider
logger log.Logger
}
func NewBucketRuleStore(bkt objstore.Bucket, cfgProvider bucket.SSEConfigProvider, logger log.Logger) *BucketRuleStore {
return &BucketRuleStore{
bucket: bucket.NewPrefixedBucketClient(bkt, rulesPrefix),
cfgProvider: cfgProvider,
logger: logger,
}
}
// getRuleGroup loads and return a rules group. If existing rule group is supplied, it is Reset and reused. If nil, new RuleGroupDesc is allocated.
func (b *BucketRuleStore) getRuleGroup(ctx context.Context, userID, namespace, groupName string, rg *rulespb.RuleGroupDesc) (*rulespb.RuleGroupDesc, error) {
userBucket := bucket.NewUserBucketClient(userID, b.bucket, b.cfgProvider)
objectKey := getRuleGroupObjectKey(namespace, groupName)
reader, err := userBucket.Get(ctx, objectKey)
if userBucket.IsObjNotFoundErr(err) {
level.Debug(b.logger).Log("msg", "rule group does not exist", "user", userID, "key", objectKey)
return nil, rulestore.ErrGroupNotFound
}
if err != nil {
return nil, errors.Wrapf(err, "failed to get rule group %s", objectKey)
}
defer func() { _ = reader.Close() }()
buf, err := io.ReadAll(reader)
if err != nil {
return nil, errors.Wrapf(err, "failed to read rule group %s", objectKey)
}
if rg == nil {
rg = &rulespb.RuleGroupDesc{}
} else {
rg.Reset()
}
err = proto.Unmarshal(buf, rg)
if err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal rule group %s", objectKey)
}
return rg, nil
}
// ListAllUsers implements rules.RuleStore.
func (b *BucketRuleStore) ListAllUsers(ctx context.Context) ([]string, error) {
var users []string
err := b.bucket.Iter(ctx, "", func(user string) error {
users = append(users, strings.TrimSuffix(user, objstore.DirDelim))
return nil
})
if err != nil {
return nil, fmt.Errorf("unable to list users in rule store bucket: %w", err)
}
return users, nil
}
// ListAllRuleGroups implements rules.RuleStore.
func (b *BucketRuleStore) ListAllRuleGroups(ctx context.Context) (map[string]rulespb.RuleGroupList, error) {
out := map[string]rulespb.RuleGroupList{}
// List rule groups for all tenants.
err := b.bucket.Iter(ctx, "", func(key string) error {
userID, namespace, group, err := parseRuleGroupObjectKeyWithUser(key)
if err != nil {
level.Warn(b.logger).Log("msg", "invalid rule group object key found while listing rule groups", "key", key, "err", err)
// Do not fail just because of a spurious item in the bucket.
return nil
}
out[userID] = append(out[userID], &rulespb.RuleGroupDesc{
User: userID,
Namespace: namespace,
Name: group,
})
return nil
}, objstore.WithRecursiveIter)
if err != nil {
return nil, err
}
return out, nil
}
// ListRuleGroupsForUserAndNamespace implements rules.RuleStore.
func (b *BucketRuleStore) ListRuleGroupsForUserAndNamespace(ctx context.Context, userID string, namespace string) (rulespb.RuleGroupList, error) {
userBucket := bucket.NewUserBucketClient(userID, b.bucket, b.cfgProvider)
groupList := rulespb.RuleGroupList{}
// The prefix to list objects depends on whether the namespace has been
// specified in the request.
prefix := ""
if namespace != "" {
prefix = getNamespacePrefix(namespace)
}
err := userBucket.Iter(ctx, prefix, func(key string) error {
namespace, group, err := parseRuleGroupObjectKey(key)
if err != nil {
level.Warn(b.logger).Log("msg", "invalid rule group object key found while listing rule groups", "user", userID, "key", key, "err", err)
// Do not fail just because of a spurious item in the bucket.
return nil
}
groupList = append(groupList, &rulespb.RuleGroupDesc{
User: userID,
Namespace: namespace,
Name: group,
})
return nil
}, objstore.WithRecursiveIter)
if err != nil {
return nil, err
}
return groupList, nil
}
// LoadRuleGroups implements rules.RuleStore.
func (b *BucketRuleStore) LoadRuleGroups(ctx context.Context, groupsToLoad map[string]rulespb.RuleGroupList) error {
ch := make(chan *rulespb.RuleGroupDesc)
// Given we store one file per rule group. With this, we create a pool of workers that will
// download all rule groups in parallel. We limit the number of workers to avoid a
// particular user having too many rule groups rate limiting us with the object storage.
g, gCtx := errgroup.WithContext(ctx)
for i := 0; i < loadConcurrency; i++ {
g.Go(func() error {
for gr := range ch {
user, namespace, group := gr.GetUser(), gr.GetNamespace(), gr.GetName()
if user == "" || namespace == "" || group == "" {
return fmt.Errorf("invalid rule group: user=%q, namespace=%q, group=%q", user, namespace, group)
}
gr, err := b.getRuleGroup(gCtx, user, namespace, group, gr) // reuse group pointer from the map.
if err != nil {
return errors.Wrapf(err, "get rule group user=%q, namespace=%q, name=%q", user, namespace, group)
}
if user != gr.User || namespace != gr.Namespace || group != gr.Name {
return fmt.Errorf("mismatch between requested rule group and loaded rule group, requested: user=%q, namespace=%q, group=%q, loaded: user=%q, namespace=%q, group=%q", user, namespace, group, gr.User, gr.Namespace, gr.Name)
}
}
return nil
})
}
outer:
for _, gs := range groupsToLoad {
for _, g := range gs {
if g == nil {
continue
}
select {
case <-gCtx.Done():
break outer
case ch <- g:
// ok
}
}
}
close(ch)
return g.Wait()
}
// GetRuleGroup implements rules.RuleStore.
func (b *BucketRuleStore) GetRuleGroup(ctx context.Context, userID string, namespace string, group string) (*rulespb.RuleGroupDesc, error) {
return b.getRuleGroup(ctx, userID, namespace, group, nil)
}
// SetRuleGroup implements rules.RuleStore.
func (b *BucketRuleStore) SetRuleGroup(ctx context.Context, userID string, namespace string, group *rulespb.RuleGroupDesc) error {
userBucket := bucket.NewUserBucketClient(userID, b.bucket, b.cfgProvider)
data, err := proto.Marshal(group)
if err != nil {
return err
}
return userBucket.Upload(ctx, getRuleGroupObjectKey(namespace, group.Name), bytes.NewBuffer(data))
}
// DeleteRuleGroup implements rules.RuleStore.
func (b *BucketRuleStore) DeleteRuleGroup(ctx context.Context, userID string, namespace string, group string) error {
userBucket := bucket.NewUserBucketClient(userID, b.bucket, b.cfgProvider)
err := userBucket.Delete(ctx, getRuleGroupObjectKey(namespace, group))
if b.bucket.IsObjNotFoundErr(err) {
return rulestore.ErrGroupNotFound
}
return err
}
// DeleteNamespace implements rules.RuleStore.
func (b *BucketRuleStore) DeleteNamespace(ctx context.Context, userID string, namespace string) error {
ruleGroupList, err := b.ListRuleGroupsForUserAndNamespace(ctx, userID, namespace)
if err != nil {
return err
}
if len(ruleGroupList) == 0 {
return rulestore.ErrGroupNamespaceNotFound
}
userBucket := bucket.NewUserBucketClient(userID, b.bucket, b.cfgProvider)
for _, rg := range ruleGroupList {
if err := ctx.Err(); err != nil {
return err
}
objectKey := getRuleGroupObjectKey(rg.Namespace, rg.Name)
level.Debug(b.logger).Log("msg", "deleting rule group", "user", userID, "namespace", namespace, "key", objectKey)
err = userBucket.Delete(ctx, objectKey)
if err != nil {
level.Error(b.logger).Log("msg", "unable to delete rule group from namespace", "user", userID, "namespace", namespace, "key", objectKey, "err", err)
return err
}
}
return nil
}
func getNamespacePrefix(namespace string) string {
return base64.URLEncoding.EncodeToString([]byte(namespace)) + objstore.DirDelim
}
func getRuleGroupObjectKey(namespace, group string) string {
return getNamespacePrefix(namespace) + base64.URLEncoding.EncodeToString([]byte(group))
}
// parseRuleGroupObjectKeyWithUser parses a bucket object key in the format "<user>/<namespace>/<rules group>".
func parseRuleGroupObjectKeyWithUser(key string) (user, namespace, group string, err error) {
parts := strings.SplitN(key, objstore.DirDelim, 2)
if len(parts) != 2 {
return "", "", "", errInvalidRuleGroupKey
}
user = parts[0]
if user == "" {
return "", "", "", errEmptyUser
}
namespace, group, err = parseRuleGroupObjectKey(parts[1])
return
}
// parseRuleGroupObjectKey parses a bucket object key in the format "<namespace>/<rules group>".
func parseRuleGroupObjectKey(key string) (namespace, group string, _ error) {
parts := strings.Split(key, objstore.DirDelim)
if len(parts) != 2 {
return "", "", errInvalidRuleGroupKey
}
decodedNamespace, err := base64.URLEncoding.DecodeString(parts[0])
if err != nil {
return "", "", err
}
if len(decodedNamespace) == 0 {
return "", "", errEmptyNamespace
}
decodedGroup, err := base64.URLEncoding.DecodeString(parts[1])
if err != nil {
return "", "", err
}
if len(decodedGroup) == 0 {
return "", "", errEmptyGroupName
}
return string(decodedNamespace), string(decodedGroup), nil
}