-
Notifications
You must be signed in to change notification settings - Fork 351
/
manager.go
416 lines (379 loc) · 14.5 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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package ref
import (
"context"
"errors"
"fmt"
"time"
"github.com/treeverse/lakefs/pkg/batch"
"github.com/treeverse/lakefs/pkg/db"
"github.com/treeverse/lakefs/pkg/graveler"
"github.com/treeverse/lakefs/pkg/ident"
)
// IteratorPrefetchSize is the amount of records to maybeFetch from PG
const IteratorPrefetchSize = 1000
// MaxBatchDelay - 3ms was chosen as a max delay time for critical path queries.
// It trades off amount of queries per second (and thus effectiveness of the batching mechanism) with added latency.
// Since reducing # of expensive operations is only beneficial when there are a lot of concurrent requests,
// the sweet spot is probably between 1-5 milliseconds (representing 200-1000 requests/second to the data store).
// 3ms of delay with ~300 requests/second per resource sounds like a reasonable tradeoff.
const MaxBatchDelay = time.Millisecond * 3
type Manager struct {
db db.Database
addressProvider ident.AddressProvider
batchExecutor batch.Batcher
}
func NewPGRefManager(executor batch.Batcher, db db.Database, addressProvider ident.AddressProvider) *Manager {
return &Manager{
db: db,
addressProvider: addressProvider,
batchExecutor: executor,
}
}
func (m *Manager) GetRepository(ctx context.Context, repositoryID graveler.RepositoryID) (*graveler.Repository, error) {
key := fmt.Sprintf("GetRepository:%s", repositoryID)
repository, err := m.batchExecutor.BatchFor(key, MaxBatchDelay, batch.BatchFn(func() (interface{}, error) {
return m.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
repository := &graveler.Repository{}
err := tx.Get(repository,
`SELECT storage_namespace, creation_date, default_branch FROM graveler_repositories WHERE id = $1`,
repositoryID)
if err != nil {
return nil, err
}
return repository, nil
}, db.ReadOnly())
}))
if errors.Is(err, db.ErrNotFound) {
return nil, graveler.ErrRepositoryNotFound
}
if err != nil {
return nil, err
}
return repository.(*graveler.Repository), nil
}
func createBareRepository(tx db.Tx, repositoryID graveler.RepositoryID, repository graveler.Repository) error {
_, err := tx.Exec(
`INSERT INTO graveler_repositories (id, storage_namespace, creation_date, default_branch) VALUES ($1, $2, $3, $4)`,
repositoryID, repository.StorageNamespace, repository.CreationDate, repository.DefaultBranchID)
if errors.Is(err, db.ErrAlreadyExists) {
return graveler.ErrNotUnique
}
return nil
}
func (m *Manager) CreateRepository(ctx context.Context, repositoryID graveler.RepositoryID, repository graveler.Repository, token graveler.StagingToken) error {
firstCommit := graveler.NewCommit()
firstCommit.Message = graveler.FirstCommitMsg
firstCommit.Generation = 1
commitID := m.addressProvider.ContentAddress(firstCommit)
_, err := m.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
// create an bare repository first
err := createBareRepository(tx, repositoryID, repository)
if err != nil {
return nil, err
}
// Create the default branch with its staging token
_, err = tx.Exec(`
INSERT INTO graveler_branches (repository_id, id, staging_token, commit_id)
VALUES ($1, $2, $3, $4)`,
repositoryID, repository.DefaultBranchID, token, commitID)
if err != nil {
if errors.Is(err, db.ErrAlreadyExists) {
return nil, graveler.ErrNotUnique
}
return nil, err
}
// Add a first empty commit to allow branching off the default branch immediately after repository creation
return nil, m.addCommit(tx, repositoryID, commitID, firstCommit)
})
return err
}
func (m *Manager) CreateBareRepository(ctx context.Context, repositoryID graveler.RepositoryID, repository graveler.Repository) error {
_, err := m.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
return nil, createBareRepository(tx, repositoryID, repository)
})
return err
}
func (m *Manager) ListRepositories(ctx context.Context) (graveler.RepositoryIterator, error) {
return NewRepositoryIterator(ctx, m.db, IteratorPrefetchSize), nil
}
func (m *Manager) DeleteRepository(ctx context.Context, repositoryID graveler.RepositoryID) error {
_, err := m.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
var err error
_, err = tx.Exec(`DELETE FROM graveler_branches WHERE repository_id = $1`, repositoryID)
if err != nil {
return nil, err
}
_, err = tx.Exec(`DELETE FROM graveler_tags WHERE repository_id = $1`, repositoryID)
if err != nil {
return nil, err
}
_, err = tx.Exec(`DELETE FROM graveler_commits WHERE repository_id = $1`, repositoryID)
if err != nil {
return nil, err
}
r, err := tx.Exec(`DELETE FROM graveler_repositories WHERE id = $1`, repositoryID)
if err != nil {
return nil, err
}
if r.RowsAffected() == 0 {
return nil, db.ErrNotFound
}
return nil, nil
})
if errors.Is(err, db.ErrNotFound) {
return graveler.ErrRepositoryNotFound
}
return err
}
func (m *Manager) ParseRef(ref graveler.Ref) (graveler.RawRef, error) {
return ParseRef(ref)
}
func (m *Manager) ResolveRawRef(ctx context.Context, repositoryID graveler.RepositoryID, raw graveler.RawRef) (*graveler.ResolvedRef, error) {
return ResolveRawRef(ctx, m, m.addressProvider, repositoryID, raw)
}
func (m *Manager) GetBranch(ctx context.Context, repositoryID graveler.RepositoryID, branchID graveler.BranchID) (*graveler.Branch, error) {
key := fmt.Sprintf("GetBranch:%s:%s", repositoryID, branchID)
branch, err := m.batchExecutor.BatchFor(key, MaxBatchDelay, batch.BatchFn(func() (interface{}, error) {
return m.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
var rec branchRecord
err := tx.Get(&rec, `SELECT commit_id, staging_token FROM graveler_branches WHERE repository_id = $1 AND id = $2`,
repositoryID, branchID)
if err != nil {
return nil, err
}
return &graveler.Branch{
CommitID: rec.CommitID,
StagingToken: rec.StagingToken,
}, nil
}, db.ReadOnly())
}))
if errors.Is(err, db.ErrNotFound) {
return nil, graveler.ErrBranchNotFound
}
if err != nil {
return nil, err
}
return branch.(*graveler.Branch), nil
}
func (m *Manager) SetBranch(ctx context.Context, repositoryID graveler.RepositoryID, branchID graveler.BranchID, branch graveler.Branch) error {
_, err := m.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
_, err := tx.Exec(`
INSERT INTO graveler_branches (repository_id, id, staging_token, commit_id)
VALUES ($1, $2, $3, $4)
ON CONFLICT (repository_id, id)
DO UPDATE SET staging_token = $3, commit_id = $4`,
repositoryID, branchID, branch.StagingToken, branch.CommitID)
return nil, err
})
return err
}
func (m *Manager) DeleteBranch(ctx context.Context, repositoryID graveler.RepositoryID, branchID graveler.BranchID) error {
_, err := m.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
r, err := tx.Exec(
`DELETE FROM graveler_branches WHERE repository_id = $1 AND id = $2`,
repositoryID, branchID)
if err != nil {
return nil, err
}
if r.RowsAffected() == 0 {
return nil, graveler.ErrNotFound
}
return nil, nil
})
if errors.Is(err, db.ErrNotFound) {
return graveler.ErrBranchNotFound
}
return err
}
func (m *Manager) ListBranches(ctx context.Context, repositoryID graveler.RepositoryID) (graveler.BranchIterator, error) {
_, err := m.GetRepository(ctx, repositoryID)
if err != nil {
return nil, err
}
return NewBranchIterator(ctx, m.db, repositoryID, IteratorPrefetchSize), nil
}
func (m *Manager) GetTag(ctx context.Context, repositoryID graveler.RepositoryID, tagID graveler.TagID) (*graveler.CommitID, error) {
key := fmt.Sprintf("GetTag:%s:%s", repositoryID, tagID)
commitID, err := m.batchExecutor.BatchFor(key, MaxBatchDelay, batch.BatchFn(func() (interface{}, error) {
return m.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
var commitID graveler.CommitID
err := tx.Get(&commitID, `SELECT commit_id FROM graveler_tags WHERE repository_id = $1 AND id = $2`,
repositoryID, tagID)
if err != nil {
return nil, err
}
return &commitID, nil
}, db.ReadOnly())
}))
if errors.Is(err, db.ErrNotFound) {
return nil, graveler.ErrTagNotFound
}
if err != nil {
return nil, err
}
return commitID.(*graveler.CommitID), nil
}
func (m *Manager) CreateTag(ctx context.Context, repositoryID graveler.RepositoryID, tagID graveler.TagID, commitID graveler.CommitID) error {
_, err := m.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
res, err := tx.Exec(`INSERT INTO graveler_tags (repository_id, id, commit_id) VALUES ($1, $2, $3)
ON CONFLICT DO NOTHING`,
repositoryID, tagID, commitID)
if err != nil {
return nil, err
}
if res.RowsAffected() == 0 {
return nil, graveler.ErrTagAlreadyExists
}
return nil, nil
})
return err
}
func (m *Manager) DeleteTag(ctx context.Context, repositoryID graveler.RepositoryID, tagID graveler.TagID) error {
_, err := m.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
r, err := tx.Exec(
`DELETE FROM graveler_tags WHERE repository_id = $1 AND id = $2`,
repositoryID, tagID)
if err != nil {
return nil, err
}
if r.RowsAffected() == 0 {
return nil, graveler.ErrNotFound
}
return nil, nil
})
if errors.Is(err, db.ErrNotFound) {
return graveler.ErrTagNotFound
}
return err
}
func (m *Manager) ListTags(ctx context.Context, repositoryID graveler.RepositoryID) (graveler.TagIterator, error) {
_, err := m.GetRepository(ctx, repositoryID)
if err != nil {
return nil, err
}
return NewTagIterator(ctx, m.db, repositoryID, IteratorPrefetchSize), nil
}
func (m *Manager) GetCommitByPrefix(ctx context.Context, repositoryID graveler.RepositoryID, prefix graveler.CommitID) (*graveler.Commit, error) {
key := fmt.Sprintf("GetCommitByPrefix:%s:%s", repositoryID, prefix)
commit, err := m.batchExecutor.BatchFor(key, MaxBatchDelay, batch.BatchFn(func() (interface{}, error) {
return m.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
records := make([]*commitRecord, 0)
// LIMIT 2 is used to test if a truncated commit ID resolves to *one* commit.
// if we get 2 results that start with the truncated ID, that's enough to determine this prefix is not unique
err := tx.Select(&records, `
SELECT id, committer, message, creation_date, parents, meta_range_id, metadata, version, generation
FROM graveler_commits
WHERE repository_id = $1 AND id LIKE $2 || '%'
LIMIT 2`,
repositoryID, prefix)
if errors.Is(err, db.ErrNotFound) {
return nil, graveler.ErrNotFound
}
if err != nil {
return nil, err
}
if len(records) == 0 {
return "", graveler.ErrNotFound
}
if len(records) > 1 {
return "", graveler.ErrRefAmbiguous // more than 1 commit starts with the ID prefix
}
return records[0].toGravelerCommit(), nil
}, db.ReadOnly())
}))
if errors.Is(err, db.ErrNotFound) {
return nil, graveler.ErrCommitNotFound
}
if err != nil {
return nil, err
}
return commit.(*graveler.Commit), nil
}
func (m *Manager) GetCommit(ctx context.Context, repositoryID graveler.RepositoryID, commitID graveler.CommitID) (*graveler.Commit, error) {
key := fmt.Sprintf("GetCommit:%s:%s", repositoryID, commitID)
commit, err := m.batchExecutor.BatchFor(key, MaxBatchDelay, batch.BatchFn(func() (interface{}, error) {
return m.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
var rec commitRecord
err := tx.Get(&rec, `
SELECT committer, message, creation_date, parents, meta_range_id, metadata, version, generation
FROM graveler_commits WHERE repository_id = $1 AND id = $2`,
repositoryID, commitID)
if err != nil {
return nil, err
}
return rec.toGravelerCommit(), nil
}, db.ReadOnly())
}))
if errors.Is(err, db.ErrNotFound) {
return nil, graveler.ErrCommitNotFound
}
if err != nil {
return nil, err
}
return commit.(*graveler.Commit), nil
}
func (m *Manager) AddCommit(ctx context.Context, repositoryID graveler.RepositoryID, commit graveler.Commit) (graveler.CommitID, error) {
commitID := m.addressProvider.ContentAddress(commit)
_, err := m.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
return nil, m.addCommit(tx, repositoryID, commitID, commit)
})
if err != nil {
return "", err
}
return graveler.CommitID(commitID), err
}
func (m *Manager) addCommit(tx db.Tx, repositoryID graveler.RepositoryID, commitID string, commit graveler.Commit) error {
// convert parents to slice of strings
var parents []string
for _, parent := range commit.Parents {
parents = append(parents, string(parent))
}
// commits are written based on their content hash, if we insert the same ID again,
// it will necessarily have the same attributes as the existing one, so no need to overwrite it
_, err := tx.Exec(`
INSERT INTO graveler_commits
(repository_id, id, committer, message, creation_date, parents, meta_range_id, metadata, version, generation)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
ON CONFLICT DO NOTHING`,
repositoryID, commitID, commit.Committer, commit.Message,
commit.CreationDate.UTC(), parents, commit.MetaRangeID, commit.Metadata, commit.Version, commit.Generation)
return err
}
func (m *Manager) FindMergeBase(ctx context.Context, repositoryID graveler.RepositoryID, commitIDs ...graveler.CommitID) (*graveler.Commit, error) {
const allowedCommitsToCompare = 2
if len(commitIDs) != allowedCommitsToCompare {
return nil, graveler.ErrInvalidMergeBase
}
return FindMergeBase(ctx, m, repositoryID, commitIDs[0], commitIDs[1])
}
func (m *Manager) Log(ctx context.Context, repositoryID graveler.RepositoryID, from graveler.CommitID) (graveler.CommitIterator, error) {
_, err := m.GetRepository(ctx, repositoryID)
if err != nil {
return nil, err
}
return NewCommitIterator(ctx, m.db, repositoryID, from), nil
}
func (m *Manager) ListCommits(ctx context.Context, repositoryID graveler.RepositoryID) (graveler.CommitIterator, error) {
_, err := m.GetRepository(ctx, repositoryID)
if err != nil {
return nil, err
}
return NewOrderedCommitIterator(ctx, m.db, repositoryID, IteratorPrefetchSize), nil
}
func (m *Manager) FillGenerations(ctx context.Context, repositoryID graveler.RepositoryID) error {
_, err := m.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
return tx.Exec(`WITH RECURSIVE cte AS
(
SELECT id, 1 AS generation,creation_date
FROM graveler_commits
WHERE repository_id = $1 AND parents IS NULL OR array_length(parents,1) = 0
UNION ALL
SELECT DISTINCT(c.id), cte.generation+1 AS generation,c.creation_date
FROM graveler_commits c INNER JOIN cte
ON cte.id = ANY(c.parents) WHERE repository_id = $1
)
UPDATE graveler_commits u SET generation = cte.generation FROM cte WHERE u.id = cte.id;`,
repositoryID)
})
return err
}