-
Notifications
You must be signed in to change notification settings - Fork 342
/
manager.go
172 lines (158 loc) · 5.84 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
package settings
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"time"
"github.com/treeverse/lakefs/pkg/block"
"github.com/treeverse/lakefs/pkg/block/adapter"
"github.com/treeverse/lakefs/pkg/cache"
"github.com/treeverse/lakefs/pkg/graveler"
"github.com/treeverse/lakefs/pkg/logging"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
const (
settingsRelativeKey = "%s/settings/%s.json" // where the settings are saved relative to the storage namespace
cacheSize = 100_000
defaultCacheExpiry = 1 * 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 {
refManager graveler.RefManager
branchLock graveler.BranchLocker
blockAdapter block.Adapter
committedBlockStoragePrefix string
cache cache.Cache
}
type ManagerOption func(m *Manager)
func WithCache(cache cache.Cache) ManagerOption {
return func(m *Manager) {
m.cache = cache
}
}
func NewManager(refManager graveler.RefManager, branchLock graveler.BranchLocker, blockAdapter block.Adapter, committedBlockStoragePrefix string, options ...ManagerOption) *Manager {
m := &Manager{
refManager: refManager,
branchLock: branchLock,
blockAdapter: blockAdapter,
committedBlockStoragePrefix: committedBlockStoragePrefix,
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.
func (m *Manager) Save(ctx context.Context, repositoryID graveler.RepositoryID, key string, setting proto.Message) error {
repo, err := m.refManager.GetRepository(ctx, repositoryID)
if err != nil {
return err
}
messageBytes, err := proto.Marshal(setting)
if err != nil {
return err
}
err = m.blockAdapter.Put(ctx, block.ObjectPointer{
StorageNamespace: string(repo.StorageNamespace),
Identifier: fmt.Sprintf(settingsRelativeKey, m.committedBlockStoragePrefix, key),
IdentifierType: block.IdentifierTypeRelative,
}, int64(len(messageBytes)), bytes.NewReader(messageBytes), block.PutOpts{})
if err != nil {
return err
}
logSetting(logging.FromContext(ctx), repositoryID, key, setting, "saving repository-level setting")
return nil
}
func (m *Manager) GetLatest(ctx context.Context, repositoryID graveler.RepositoryID, key string, settingTemplate proto.Message) (proto.Message, error) {
messageBytes, err := m.getFromStore(ctx, repositoryID, key)
if err != nil {
return nil, err
}
setting := proto.Clone(settingTemplate)
err = proto.Unmarshal(messageBytes, setting)
if err != nil {
return nil, err
}
return setting, nil
}
// Get fetches the setting under the given repository and key, and returns the result.
// The result is eventually consistent: it is not guaranteed to be the most up-to-date setting. The cache expiry period is 1 second.
// The settingTemplate parameter is used to determine the returned type.
func (m *Manager) Get(ctx context.Context, repositoryID graveler.RepositoryID, key string, settingTemplate proto.Message) (proto.Message, error) {
k := cacheKey{
RepositoryID: repositoryID,
Key: key,
}
setting, err := m.cache.GetOrSet(k, func() (v interface{}, err error) {
return m.GetLatest(ctx, repositoryID, key, settingTemplate)
})
if err != nil {
return nil, err
}
logSetting(logging.FromContext(ctx), repositoryID, key, setting.(proto.Message), "got repository-level setting")
return setting.(proto.Message), nil
}
func (m *Manager) getFromStore(ctx context.Context, repositoryID graveler.RepositoryID, key string) ([]byte, error) {
repo, err := m.refManager.GetRepository(ctx, repositoryID)
if err != nil {
return nil, err
}
objectPointer := block.ObjectPointer{
StorageNamespace: string(repo.StorageNamespace),
Identifier: fmt.Sprintf(settingsRelativeKey, m.committedBlockStoragePrefix, key),
IdentifierType: block.IdentifierTypeRelative,
}
reader, err := m.blockAdapter.Get(ctx, objectPointer, -1)
if errors.Is(err, adapter.ErrDataNotFound) {
return nil, graveler.ErrNotFound
}
if err != nil {
return nil, err
}
defer func() {
_ = reader.Close()
}()
return io.ReadAll(reader)
}
// UpdateWithLock atomically gets a setting, performs the update function, and persists the setting to the store.
// The settingTemplate parameter is used to determine the type passed to the update function.
func (m *Manager) UpdateWithLock(ctx context.Context, repositoryID graveler.RepositoryID, key string, settingTemplate proto.Message, update func(proto.Message) error) error {
repo, err := m.refManager.GetRepository(ctx, repositoryID)
if err != nil {
return err
}
_, err = m.branchLock.MetadataUpdater(ctx, repositoryID, repo.DefaultBranchID, func() (interface{}, error) {
setting, err := m.GetLatest(ctx, repositoryID, key, settingTemplate)
if errors.Is(err, graveler.ErrNotFound) {
setting = proto.Clone(settingTemplate)
} else if err != nil {
return nil, err
}
logSetting(logging.FromContext(ctx), repositoryID, key, setting, "got repository-level setting")
err = update(setting)
if err != nil {
return nil, err
}
return nil, m.Save(ctx, repositoryID, key, setting)
})
return err
}
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)
}
}