/
pfsdb.go
268 lines (237 loc) · 8.2 KB
/
pfsdb.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
package v2_5_0
import (
"context"
"strings"
"google.golang.org/protobuf/proto"
col "github.com/pachyderm/pachyderm/v2/src/internal/collection"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/log"
"github.com/pachyderm/pachyderm/v2/src/internal/pachsql"
"github.com/pachyderm/pachyderm/v2/src/internal/uuid"
"github.com/pachyderm/pachyderm/v2/src/pfs"
pfsserver "github.com/pachyderm/pachyderm/v2/src/server/pfs"
)
func projects(db *pachsql.DB, listener col.PostgresListener) col.PostgresCollection {
return col.NewPostgresCollection(
"projects",
db,
listener,
&pfs.ProjectInfo{},
nil,
col.WithKeyGen(func(key interface{}) (string, error) {
if project, ok := key.(*pfs.Project); !ok {
return "", errors.New("key must be a project")
} else {
return project.Name, nil
}
}),
col.WithNotFoundMessage(func(key interface{}) string {
return pfsserver.ErrProjectNotFound{Project: key.(*pfs.Project)}.Error()
}),
col.WithExistsMessage(func(key interface{}) string {
return pfsserver.ErrProjectExists{Project: key.(*pfs.Project)}.Error()
}),
)
}
var reposTypeIndex = &index{
Name: "type",
Extract: func(val proto.Message) string {
return val.(*pfs.RepoInfo).Repo.Type
},
}
func reposNameKey(repo *pfs.Repo) string {
return repo.Project.Name + "/" + repo.Name
}
var reposNameIndex = &index{
Name: "name",
Extract: func(val proto.Message) string {
return reposNameKey(val.(*pfs.RepoInfo).Repo)
},
}
var reposIndexes = []*index{reposNameIndex, reposTypeIndex}
func repoKey(repo *pfs.Repo) string {
return repo.Project.Name + "/" + repo.Name + "." + repo.Type
}
func repoKeyCheck(key string) error {
parts := strings.Split(key, ".")
if len(parts) < 2 || len(parts[1]) == 0 {
return errors.Errorf("repo must have a specified type")
}
return nil
}
var commitsRepoIndex = &index{
Name: "repo",
Extract: func(val proto.Message) string {
return repoKey(val.(*CommitInfo).Commit.Branch.Repo)
},
}
var commitsBranchlessIndex = &index{
Name: "branchless",
Extract: func(val proto.Message) string {
return commitBranchlessKey(val.(*CommitInfo).Commit)
},
}
var commitsCommitSetIndex = &index{
Name: "commitset",
Extract: func(val proto.Message) string {
return val.(*CommitInfo).Commit.Id
},
}
var commitsIndexes = []*index{commitsRepoIndex, commitsBranchlessIndex, commitsCommitSetIndex}
func commitKey(commit *pfs.Commit) string {
return branchKey(commit.Branch) + "=" + commit.Id
}
func commitBranchlessKey(commit *pfs.Commit) string {
return repoKey(commit.Branch.Repo) + "@" + commit.Id
}
var branchesRepoIndex = &index{
Name: "repo",
Extract: func(val proto.Message) string {
return repoKey(val.(*pfs.BranchInfo).Branch.Repo)
},
}
var branchesIndexes = []*index{branchesRepoIndex}
func branchKey(branch *pfs.Branch) string {
return repoKey(branch.Repo) + "@" + branch.Name
}
func pfsCollections() []*postgresCollection {
return []*postgresCollection{
newPostgresCollection("projects", nil),
}
}
func migrateProject(p *pfs.Project) *pfs.Project {
if p == nil || p.Name == "" {
return &pfs.Project{Name: "default"}
}
return p
}
func migrateRepo(r *pfs.Repo) *pfs.Repo {
r.Project = migrateProject(r.Project)
return r
}
func migrateRepoInfo(r *pfs.RepoInfo) *pfs.RepoInfo {
r.Repo = migrateRepo(r.Repo)
for i, b := range r.Branches {
r.Branches[i] = migrateBranch(b)
}
return r
}
func migrateBranch(b *pfs.Branch) *pfs.Branch {
b.Repo = migrateRepo(b.Repo)
return b
}
func migrateBranchInfo(b *pfs.BranchInfo) *pfs.BranchInfo {
b.Branch = migrateBranch(b.Branch)
if b.Head != nil {
b.Head = migrateCommit(b.Head)
}
for i, bb := range b.Provenance {
b.Provenance[i] = migrateBranch(bb)
}
for i, bb := range b.DirectProvenance {
b.DirectProvenance[i] = migrateBranch(bb)
}
for i, bb := range b.Subvenance {
b.Subvenance[i] = migrateBranch(bb)
}
return b
}
func migrateCommit(c *pfs.Commit) *pfs.Commit {
c.Branch = migrateBranch(c.Branch)
return c
}
func migrateCommitInfo(c *CommitInfo) *CommitInfo {
c.Commit = migrateCommit(c.Commit)
if c.ParentCommit != nil {
c.ParentCommit = migrateCommit(c.ParentCommit)
}
for i, cc := range c.ChildCommits {
c.ChildCommits[i] = migrateCommit(cc)
}
for i, bb := range c.DirectProvenance {
c.DirectProvenance[i] = migrateBranch(bb)
}
return c
}
// migratePFSDB migrates PFS to be fully project-aware with a default project.
// It uses some internal knowledge about how cols.PostgresCollection works to do
// so.
func migratePFSDB(ctx context.Context, tx *pachsql.Tx) error {
// A pre-projects commit diff/total has a commit_id value which looks
// like images.user@master=da4016a16f8944cba94038ab5bcc9933; a
// post-projects commit diff/total has a commit_id which looks like
// myproject/images.user@master=da4016a16f8944cba94038ab5bcc9933.
//
// The regexp below is matches only rows with a pre-projects–style
// commit_id and does not touch post-projects–style rows. This is
// because prior to the default project name changing, commits in the
// default project were still identified without the project (e.g. as
// images.user@master=da4016a16f8944cba94038ab5bcc9933 rather than
// /images.user@master=da4016a16f8944cba94038ab5bcc9933).
log.Info(ctx, "Updating commit_diffs to include default project")
if _, err := tx.ExecContext(ctx, `UPDATE pfs.commit_diffs SET commit_id = regexp_replace(commit_id, '^([-a-zA-Z0-9_]+)', 'default/\1') WHERE commit_id ~ '^[-a-zA-Z0-9_]+\.';`); err != nil {
return errors.Wrap(err, "could not update pfs.commit_diffs")
}
log.Info(ctx, "Updating commit_totals to include default project")
if _, err := tx.ExecContext(ctx, `UPDATE pfs.commit_totals SET commit_id = regexp_replace(commit_id, '^([-a-zA-Z0-9_]+)', 'default/\1') WHERE commit_id ~ '^[-a-zA-Z0-9_]+\.';`); err != nil {
return errors.Wrap(err, "could not update pfs.commit_totals")
}
log.Info(ctx, "Updating storage.tracker_objects to include default project")
if _, err := tx.ExecContext(ctx, `UPDATE storage.tracker_objects SET str_id = regexp_replace(str_id, 'commit/([-a-zA-Z0-9_]+)', 'commit/default/\1') WHERE str_id ~ '^commit/[-a-zA-Z0-9_]+\.';`); err != nil {
return errors.Wrapf(err, "could not update storage.tracker_objects")
}
var oldRepo = new(pfs.RepoInfo)
if err := migratePostgreSQLCollection(ctx, tx, "repos", reposIndexes, oldRepo, func(oldKey string) (newKey string, newVal proto.Message, err error) {
oldRepo = migrateRepoInfo(oldRepo)
return repoKey(oldRepo.Repo), oldRepo, nil
},
withKeyCheck(repoKeyCheck),
withKeyGen(func(key interface{}) (string, error) {
if repo, ok := key.(*pfs.Repo); !ok {
return "", errors.New("key must be a repo")
} else {
return repoKey(repo), nil
}
}),
); err != nil {
return errors.Wrap(err, "could not migrate repos")
}
var oldBranch = new(pfs.BranchInfo)
if err := migratePostgreSQLCollection(ctx, tx, "branches", branchesIndexes, oldBranch, func(oldKey string) (newKey string, newVal proto.Message, err error) {
oldBranch = migrateBranchInfo(oldBranch)
return branchKey(oldBranch.Branch), oldBranch, nil
},
withKeyGen(func(key interface{}) (string, error) {
if branch, ok := key.(*pfs.Branch); !ok {
return "", errors.New("key must be a branch")
} else {
return branchKey(branch), nil
}
}),
withKeyCheck(func(key string) error {
keyParts := strings.Split(key, "@")
if len(keyParts) != 2 {
return errors.Errorf("branch key %s isn't valid, use BranchKey to generate it", key)
}
if uuid.IsUUIDWithoutDashes(keyParts[1]) {
return errors.Errorf("branch name cannot be a UUID V4")
}
return repoKeyCheck(keyParts[0])
})); err != nil {
return errors.Wrap(err, "could not migrate branches")
}
var oldCommit = new(CommitInfo)
if err := migratePostgreSQLCollection(ctx, tx, "commits", commitsIndexes, oldCommit, func(oldKey string) (newKey string, newVal proto.Message, err error) {
oldCommit = migrateCommitInfo(oldCommit)
return commitKey(oldCommit.Commit), oldCommit, nil
}, withKeyGen(func(key interface{}) (string, error) {
if commit, ok := key.(*pfs.Commit); !ok {
return "", errors.New("key must be a commit")
} else {
return commitKey(commit), nil
}
})); err != nil {
return errors.Wrap(err, "could not migrate commits")
}
return nil
}