-
Notifications
You must be signed in to change notification settings - Fork 342
/
manager.go
173 lines (159 loc) · 5.38 KB
/
manager.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
package settings
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"time"
"github.com/go-openapi/swag"
"github.com/treeverse/lakefs/pkg/cache"
"github.com/treeverse/lakefs/pkg/graveler"
"github.com/treeverse/lakefs/pkg/kv"
"github.com/treeverse/lakefs/pkg/logging"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
const (
cacheSize = 100_000
defaultCacheExpiry = 3 * time.Second
)
type cacheKey struct {
RepositoryID graveler.RepositoryID
Key string
}
// Manager is a key-value store for Graveler repository-level settings.
// Each setting is stored under a key, and can be any proto.Message.
// Fetched settings are cached using cache.Cache with a default expiry time of 1 second. Hence, the store is eventually consistent.
type Manager struct {
store kv.Store
refManager graveler.RefManager
cache cache.Cache
}
type ManagerOption func(m *Manager)
func WithCache(cache cache.Cache) ManagerOption {
return func(m *Manager) {
m.WithCache(cache)
}
}
func (m *Manager) WithCache(cache cache.Cache) {
m.cache = cache
}
func NewManager(refManager graveler.RefManager, store kv.Store, options ...ManagerOption) *Manager {
m := &Manager{
refManager: refManager,
store: store,
cache: cache.NewCache(cacheSize, defaultCacheExpiry, cache.NewJitterFn(defaultCacheExpiry)),
}
for _, o := range options {
o(m)
}
return m
}
// Save persists the given setting under the given repository and key. Overrides settings key in KV Store.
// The setting is persisted only if the current version of the setting matches the given checksum.
// If lastKnownChecksum is the empty string, the setting is persisted only if it does not exist.
// If lastKnownChecksum is nil, the setting is persisted unconditionally.
func (m *Manager) Save(ctx context.Context, repository *graveler.RepositoryRecord, key string, setting proto.Message, lastKnownChecksum *string) error {
logSetting(logging.FromContext(ctx), repository.RepositoryID, key, setting, "saving repository-level setting")
repoPartition := graveler.RepoPartition(repository)
keyPath := []byte(graveler.SettingsPath(key))
if lastKnownChecksum == nil {
return kv.SetMsg(ctx, m.store, repoPartition, keyPath, setting)
}
if *lastKnownChecksum == "" {
err := kv.SetMsgIf(ctx, m.store, repoPartition, keyPath, setting, nil)
if errors.Is(err, kv.ErrPredicateFailed) {
return graveler.ErrPreconditionFailed
}
return err
}
valueWithPredicate, err := m.store.Get(ctx, []byte(repoPartition), keyPath)
if errors.Is(err, kv.ErrNotFound) {
return graveler.ErrPreconditionFailed
}
if err != nil {
return err
}
currentChecksum, err := computeChecksum(valueWithPredicate.Value)
if err != nil {
return err
}
if *currentChecksum != *lastKnownChecksum {
return graveler.ErrPreconditionFailed
}
err = kv.SetMsgIf(ctx, m.store, repoPartition, keyPath, setting, valueWithPredicate.Predicate)
if errors.Is(err, kv.ErrPredicateFailed) {
return graveler.ErrPreconditionFailed
}
return err
}
func computeChecksum(value []byte) (*string, error) {
if len(value) == 0 {
// empty value checksum is the empty string
return swag.String(""), nil
}
h := sha256.New()
_, err := h.Write(value)
if err != nil {
return nil, err
}
return swag.String(hex.EncodeToString(h.Sum(nil))), nil
}
// GetLatest loads the latest setting into dst.
// It returns a checksum representing the version of the setting, which can be passed to SaveIf for conditional updates.
// The checksum of a non-existent setting is the empty string.
func (m *Manager) GetLatest(ctx context.Context, repository *graveler.RepositoryRecord, key string, dst proto.Message) (*string, error) {
settings, err := m.store.Get(ctx, []byte(graveler.RepoPartition(repository)), []byte(graveler.SettingsPath(key)))
if errors.Is(err, kv.ErrNotFound) {
// return empty list and do not consider this an error
proto.Reset(dst)
return swag.String(""), nil
}
if err != nil {
return nil, err
}
checksum, err := computeChecksum(settings.Value)
if err != nil {
return nil, err
}
err = proto.Unmarshal(settings.Value, dst)
if err != nil {
return nil, err
}
logSetting(logging.FromContext(ctx), repository.RepositoryID, key, dst, "got repository-level setting")
return checksum, nil
}
// Get fetches the setting under the given repository and key, and loads it into dst.
// The result is eventually consistent: it is not guaranteed to be the most up-to-date setting. The cache expiry period is 1 second.
func (m *Manager) Get(ctx context.Context, repository *graveler.RepositoryRecord, key string, dst proto.Message) error {
k := cacheKey{
RepositoryID: repository.RepositoryID,
Key: key,
}
tmp := proto.Clone(dst)
setting, err := m.cache.GetOrSet(k, func() (v interface{}, err error) {
_, err = m.GetLatest(ctx, repository, key, tmp)
if errors.Is(err, graveler.ErrNotFound) {
// do not return this error here, so that empty settings are cached
return tmp, nil
}
return tmp, err
})
if err != nil {
return err
}
if setting == nil {
return graveler.ErrNotFound
}
proto.Merge(dst, setting.(proto.Message))
return nil
}
func logSetting(logger logging.Logger, repositoryID graveler.RepositoryID, key string, setting proto.Message, logMsg string) {
if logger.IsTracing() {
logger.
WithField("repo", repositoryID).
WithField("key", key).
WithField("setting", protojson.Format(setting)).
Trace(logMsg)
}
}