-
Notifications
You must be signed in to change notification settings - Fork 567
/
ppsdb.go
220 lines (195 loc) · 6.58 KB
/
ppsdb.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
// Package ppsdb contains the database schema that PPS uses.
package ppsdb
import (
"fmt"
"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/pachsql"
"github.com/pachyderm/pachyderm/v2/src/internal/uuid"
"github.com/pachyderm/pachyderm/v2/src/pfs"
"github.com/pachyderm/pachyderm/v2/src/pps"
)
const (
pipelinesCollectionName = "pipelines"
jobsCollectionName = "jobs"
clusterDefaultsCollectionName = "cluster_defaults"
projectDefaultsCollectionName = "project_defaults"
)
// PipelinesVersionIndex records the version numbers of pipelines
var PipelinesVersionIndex = &col.Index{
Name: "version",
Extract: func(val proto.Message) string {
info := val.(*pps.PipelineInfo)
return VersionKey(info.Pipeline, info.Version)
},
}
// VersionKey return a unique key for the given project, pipeline & version. If
// the project is the empty string it will return an old-style key without a
// project; otherwise the key will include the project. The version is
// zero-padded in order to facilitate sorting.
func VersionKey(p *pps.Pipeline, version uint64) string {
// zero pad in case we want to sort
return fmt.Sprintf("%s@%08d", p, version)
}
// PipelinesNameKey returns the key used by PipelinesNameIndex to index a
// PipelineInfo.
func PipelinesNameKey(p *pps.Pipeline) string {
return p.String()
}
// PipelinesNameIndex records the name of pipelines
var PipelinesNameIndex = &col.Index{
Name: "name",
Extract: func(val proto.Message) string {
return PipelinesNameKey(val.(*pps.PipelineInfo).Pipeline)
},
}
var pipelinesIndexes = []*col.Index{
PipelinesVersionIndex,
PipelinesNameIndex,
}
// ParsePipelineKey expects keys to either be of the form <pipeline>@<id> or
// <project>/<pipeline>@<id>.
func ParsePipelineKey(key string) (projectName, pipelineName, id string, err error) {
parts := strings.Split(key, "@")
if len(parts) != 2 || !uuid.IsUUIDWithoutDashes(parts[1]) {
return "", "", "", errors.Errorf("key %s is not of form [<project>/]<pipeline>@<id>", key)
}
id = parts[1]
parts = strings.Split(parts[0], "/")
if len(parts) == 0 {
return "", "", "", errors.Errorf("key %s is not of form [<project>/]<pipeline>@<id>")
}
pipelineName = parts[len(parts)-1]
if len(parts) == 1 {
return
}
projectName = strings.Join(parts[0:len(parts)-1], "/")
return
}
func pipelineCommitKey(commit *pfs.Commit) (string, error) {
if commit.Repo.Type != pfs.SpecRepoType {
return "", errors.Errorf("commit %s is not from a spec repo", commit)
}
if projectName := commit.Repo.Project.GetName(); projectName != "" {
return fmt.Sprintf("%s/%s@%s", projectName, commit.Repo.Name, commit.Id), nil
}
return fmt.Sprintf("%s@%s", commit.Repo.Name, commit.Id), nil
}
// Pipelines returns a PostgresCollection of pipelines
func Pipelines(db *pachsql.DB, listener col.PostgresListener) col.PostgresCollection {
return col.NewPostgresCollection(
pipelinesCollectionName,
db,
listener,
&pps.PipelineInfo{},
pipelinesIndexes,
col.WithKeyGen(func(key interface{}) (string, error) {
if commit, ok := key.(*pfs.Commit); ok {
return pipelineCommitKey(commit)
}
return "", errors.New("must provide a spec commit")
}),
col.WithKeyCheck(func(key string) error {
_, _, _, err := ParsePipelineKey(key)
return err
}),
)
}
func JobsPipelineKey(p *pps.Pipeline) string {
return p.String()
}
// JobsPipelineIndex maps pipeline to Jobs started by the pipeline
var JobsPipelineIndex = &col.Index{
Name: "pipeline",
Extract: func(val proto.Message) string {
return JobsPipelineKey(val.(*pps.JobInfo).Job.Pipeline)
},
}
func JobsTerminalKey(pipeline *pps.Pipeline, isTerminal bool) string {
return fmt.Sprintf("%s_%v", pipeline, isTerminal)
}
var JobsTerminalIndex = &col.Index{
Name: "job_state",
Extract: func(val proto.Message) string {
jobInfo := val.(*pps.JobInfo)
return JobsTerminalKey(jobInfo.Job.Pipeline, pps.IsTerminal(jobInfo.State))
},
}
var JobsJobSetIndex = &col.Index{
Name: "jobset",
Extract: func(val proto.Message) string {
return val.(*pps.JobInfo).Job.Id
},
}
var jobsIndexes = []*col.Index{JobsPipelineIndex, JobsTerminalIndex, JobsJobSetIndex}
// JobKey is a string representation of a Job suitable for use as an indexing
// key. It will include the project if the project name is not the empty
// string.
func JobKey(j *pps.Job) string {
return fmt.Sprintf("%s@%s", j.Pipeline, j.Id)
}
// Jobs returns a PostgresCollection of Jobs
func Jobs(db *pachsql.DB, listener col.PostgresListener) col.PostgresCollection {
return col.NewPostgresCollection(
jobsCollectionName,
db,
listener,
&pps.JobInfo{},
jobsIndexes,
)
}
// ClusterDefaults returns a PostgresCollection of cluster defaults. Note that
// this is a singleton table.
func ClusterDefaults(db *pachsql.DB, listener col.PostgresListener) col.PostgresCollection {
return col.NewPostgresCollection(
clusterDefaultsCollectionName,
db,
listener,
&ClusterDefaultsWrapper{},
nil,
)
}
// ProjectDefaults returns a PostgresCollection of project defaults. Note that
// this is a singleton table.
func ProjectDefaults(db *pachsql.DB, listener col.PostgresListener) col.PostgresCollection {
return col.NewPostgresCollection(
projectDefaultsCollectionName,
db,
listener,
&ProjectDefaultsWrapper{},
nil,
)
}
// CollectionsV0 returns a list of all the PPS API collections for
// postgres-initialization purposes. These collections are not usable for
// querying.
// DO NOT MODIFY THIS FUNCTION
// IT HAS BEEN USED IN A RELEASED MIGRATION
func CollectionsV0() []col.PostgresCollection {
return []col.PostgresCollection{
col.NewPostgresCollection(pipelinesCollectionName, nil, nil, nil, pipelinesIndexes),
col.NewPostgresCollection(jobsCollectionName, nil, nil, nil, jobsIndexes),
}
}
// CollectionsV2_7_0 returns a list of collections for postgres-initialization
// purposes. These collections are not usable for querying.
//
// DO NOT MODIFY THIS FUNCTION
// IT HAS BEEN USED IN A RELEASED MIGRATION
func CollectionsV2_7_0() []col.PostgresCollection {
return []col.PostgresCollection{
col.NewPostgresCollection(clusterDefaultsCollectionName, nil, nil, nil, nil),
}
}
// CollectionsV2_8_0 returns a list of collections for postgres-initialization
// purposes. These collections are not usable for querying.
//
// DO NOT MODIFY THIS FUNCTION
// IT HAS BEEN USED IN A RELEASED MIGRATION
func CollectionsV2_8_0() []col.PostgresCollection {
return []col.PostgresCollection{
col.NewPostgresCollection(projectDefaultsCollectionName, nil, nil, nil, nil),
}
}