-
Notifications
You must be signed in to change notification settings - Fork 342
/
manager.go
217 lines (193 loc) · 6.64 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
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
package staging
import (
"context"
"errors"
"fmt"
"path"
"strings"
"time"
"github.com/treeverse/lakefs/pkg/batch"
"github.com/treeverse/lakefs/pkg/graveler"
"github.com/treeverse/lakefs/pkg/kv"
"github.com/treeverse/lakefs/pkg/logging"
)
type Manager struct {
kvStore kv.Store
kvStoreLimited kv.Store
wakeup chan asyncEvent
batchExecutor batch.Batcher
batchDBIOTransactionMarkers bool
// cleanupCallback is being called with every successful cleanup cycle
cleanupCallback func()
}
// asyncEvent is a type of event to be handled in the async loop
type asyncEvent string
// cleanTokens is async cleaning of deleted staging tokens
const (
MaxBatchDelay = 3 * time.Millisecond
cleanTokens = asyncEvent("clean_tokens")
)
func NewManager(ctx context.Context, store, storeLimited kv.Store, batchDBIOTransactionMarkers bool, executor batch.Batcher) *Manager {
const wakeupChanCapacity = 100
m := &Manager{
kvStore: store,
kvStoreLimited: storeLimited,
wakeup: make(chan asyncEvent, wakeupChanCapacity),
batchExecutor: executor,
batchDBIOTransactionMarkers: batchDBIOTransactionMarkers,
}
go m.asyncLoop(ctx)
return m
}
func (m *Manager) log(ctx context.Context) logging.Logger {
return logging.FromContext(ctx).WithField("service_name", "staging_manager")
}
func (m *Manager) OnCleanup(cleanupCallback func()) {
m.cleanupCallback = cleanupCallback
}
func (m *Manager) getBatchedEntryData(ctx context.Context, st graveler.StagingToken, key graveler.Key) (*graveler.StagedEntryData, error) {
batchKey := fmt.Sprintf("StagingGet:%s:%s", st, key)
dt, err := m.batchExecutor.BatchFor(ctx, batchKey, MaxBatchDelay, batch.ExecuterFunc(func() (interface{}, error) {
dt := &graveler.StagedEntryData{}
_, err := kv.GetMsg(ctx, m.kvStore, graveler.StagingTokenPartition(st), key, dt)
return dt, err
}))
if err != nil {
return nil, err
}
return dt.(*graveler.StagedEntryData), nil
}
// isDBIOTransactionalMarkerObject returns true if the key is a DBIO transactional marker object (_started or _committed
func isDBIOTransactionalMarkerObject(key graveler.Key) bool {
_, f := path.Split(key.String())
return strings.HasPrefix(f, "_started_") || strings.HasPrefix(f, "_committed_")
}
func (m *Manager) Get(ctx context.Context, st graveler.StagingToken, key graveler.Key) (*graveler.Value, error) {
var err error
data := &graveler.StagedEntryData{}
// workaround for batching DBIO markers objects
if m.batchDBIOTransactionMarkers && isDBIOTransactionalMarkerObject(key) {
data, err = m.getBatchedEntryData(ctx, st, key)
} else {
_, err = kv.GetMsg(ctx, m.kvStore, graveler.StagingTokenPartition(st), key, data)
}
if err != nil {
if errors.Is(err, kv.ErrNotFound) {
err = graveler.ErrNotFound
}
return nil, err
}
// Tombstone handling
if data.Identity == nil {
return nil, nil
}
return graveler.StagedEntryFromProto(data), nil
}
func (m *Manager) Set(ctx context.Context, st graveler.StagingToken, key graveler.Key, value *graveler.Value, requireExists bool) error {
// Tombstone handling
if value == nil {
value = new(graveler.Value)
} else if value.Identity == nil {
return graveler.ErrInvalidValue
}
pb := graveler.ProtoFromStagedEntry(key, value)
stPartition := graveler.StagingTokenPartition(st)
if requireExists {
return kv.SetMsgIf(ctx, m.kvStore, stPartition, key, pb, kv.PrecondConditionalExists)
}
return kv.SetMsg(ctx, m.kvStore, stPartition, key, pb)
}
func (m *Manager) Update(ctx context.Context, st graveler.StagingToken, key graveler.Key, updateFunc graveler.ValueUpdateFunc) error {
oldValueProto := &graveler.StagedEntryData{}
var oldValue *graveler.Value
pred, err := kv.GetMsg(ctx, m.kvStore, graveler.StagingTokenPartition(st), key, oldValueProto)
if err != nil {
if errors.Is(err, kv.ErrNotFound) {
oldValue = nil
} else {
return err
}
} else {
oldValue = graveler.StagedEntryFromProto(oldValueProto)
}
updatedValue, err := updateFunc(oldValue)
if err != nil {
// report error or skip if ErrSkipValueUpdate
if errors.Is(err, graveler.ErrSkipValueUpdate) {
return nil
}
return err
}
return kv.SetMsgIf(ctx, m.kvStore, graveler.StagingTokenPartition(st), key, graveler.ProtoFromStagedEntry(key, updatedValue), pred)
}
func (m *Manager) DropKey(ctx context.Context, st graveler.StagingToken, key graveler.Key) error {
return m.kvStore.Delete(ctx, []byte(graveler.StagingTokenPartition(st)), key)
}
// List returns an iterator of staged values on the staging token st
func (m *Manager) List(ctx context.Context, st graveler.StagingToken, batchSize int) graveler.ValueIterator {
return NewStagingIterator(ctx, m.kvStore, st, batchSize)
}
func (m *Manager) Drop(ctx context.Context, st graveler.StagingToken) error {
return m.DropByPrefix(ctx, st, []byte(""))
}
func (m *Manager) DropAsync(ctx context.Context, st graveler.StagingToken) error {
err := m.kvStore.Set(ctx, []byte(graveler.CleanupTokensPartition()), []byte(st), []byte("stub-value"))
select {
case m.wakeup <- cleanTokens:
default:
m.log(ctx).Debug("wakeup channel is full, skipping wakeup")
}
return err
}
func (m *Manager) DropByPrefix(ctx context.Context, st graveler.StagingToken, prefix graveler.Key) error {
itr, err := kv.ScanPrefix(ctx, m.kvStore, []byte(graveler.StagingTokenPartition(st)), prefix, []byte(""))
if err != nil {
return err
}
defer itr.Close()
for itr.Next() {
err = m.kvStore.Delete(ctx, []byte(graveler.StagingTokenPartition(st)), itr.Entry().Key)
if err != nil {
return err
}
}
return nil
}
func (m *Manager) asyncLoop(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case event := <-m.wakeup:
switch event {
case cleanTokens:
err := m.findAndDrop(ctx)
if err != nil {
m.log(ctx).WithError(err).Error("Dropping tokens failed")
} else if m.cleanupCallback != nil {
m.cleanupCallback()
}
default:
panic(fmt.Sprintf("unknown event: %s", event))
}
}
}
}
// findAndDrop lookup for staging tokens to delete and drop keys by prefix. Uses store limited to rate limit the access.
// it assumes we are processing the data in the background.
func (m *Manager) findAndDrop(ctx context.Context) error {
it, err := m.kvStoreLimited.Scan(ctx, []byte(graveler.CleanupTokensPartition()), kv.ScanOptions{})
if err != nil {
return err
}
defer it.Close()
for it.Next() {
if err := m.DropByPrefix(ctx, graveler.StagingToken(it.Entry().Key), []byte("")); err != nil {
return err
}
if err := m.kvStoreLimited.Delete(ctx, []byte(graveler.CleanupTokensPartition()), it.Entry().Key); err != nil {
return err
}
}
return it.Err()
}