forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pfs.go
626 lines (579 loc) · 21 KB
/
pfs.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
package client
import (
"io"
"math"
"github.com/pachyderm/pachyderm/src/client/pfs"
"go.pedge.io/proto/stream"
"golang.org/x/net/context"
)
func NewRepo(repoName string) *pfs.Repo {
return &pfs.Repo{Name: repoName}
}
func NewCommit(repoName string, commitID string) *pfs.Commit {
return &pfs.Commit{
Repo: NewRepo(repoName),
ID: commitID,
}
}
func NewFile(repoName string, commitID string, path string) *pfs.File {
return &pfs.File{
Commit: NewCommit(repoName, commitID),
Path: path,
}
}
func NewBlock(hash string) *pfs.Block {
return &pfs.Block{
Hash: hash,
}
}
func NewDiff(repoName string, commitID string, shard uint64) *pfs.Diff {
return &pfs.Diff{
Commit: NewCommit(repoName, commitID),
Shard: shard,
}
}
const (
CommitTypeNone = pfs.CommitType_COMMIT_TYPE_NONE
CommitTypeRead = pfs.CommitType_COMMIT_TYPE_READ
CommitTypeWrite = pfs.CommitType_COMMIT_TYPE_WRITE
)
// CreateRepo creates a new Repo object in pfs with the given name. Repos are
// the top level data object in pfs and should be used to store data of a
// similar type. For example rather than having a single Repo for an entire
// project you might have seperate Repos for logs, metrics, database dumps etc.
func (c APIClient) CreateRepo(repoName string) error {
_, err := c.PfsAPIClient.CreateRepo(
context.Background(),
&pfs.CreateRepoRequest{
Repo: NewRepo(repoName),
},
)
return sanitizeErr(err)
}
// InspectRepo returns info about a specific Repo.
func (c APIClient) InspectRepo(repoName string) (*pfs.RepoInfo, error) {
repoInfo, err := c.PfsAPIClient.InspectRepo(
context.Background(),
&pfs.InspectRepoRequest{
Repo: NewRepo(repoName),
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return repoInfo, nil
}
// ListRepo returns info about all Repos.
// provenance specifies a set of provenance repos, only repos which have ALL of
// the specified repos as provenance will be returned unless provenance is nil
// in which case it is ignored.
func (c APIClient) ListRepo(provenance []string) ([]*pfs.RepoInfo, error) {
request := &pfs.ListRepoRequest{}
for _, repoName := range provenance {
request.Provenance = append(request.Provenance, NewRepo(repoName))
}
repoInfos, err := c.PfsAPIClient.ListRepo(
context.Background(),
request,
)
if err != nil {
return nil, sanitizeErr(err)
}
return repoInfos.RepoInfo, nil
}
// DeleteRepo deletes a repo and reclaims the storage space it was using. Note
// that as of 1.0 we do not reclaim the blocks that the Repo was referencing,
// this is because they may also be referenced by other Repos and deleting them
// would make those Repos inaccessible. This will be resolved in later
// versions.
func (c APIClient) DeleteRepo(repoName string) error {
_, err := c.PfsAPIClient.DeleteRepo(
context.Background(),
&pfs.DeleteRepoRequest{
Repo: NewRepo(repoName),
},
)
return err
}
// StartCommit begins the process of committing data to a Repo. Once started
// you can write to the Commit with PutFile and when all the data has been
// written you must finish the Commit with FinishCommit. NOTE, data is not
// persisted until FinishCommit is called.
// parentCommit specifies the parent Commit, upon creation the new Commit will
// appear identical to the parent Commit, data can safely be added to the new
// commit without affecting the contents of the parent Commit. You may pass ""
// as parentCommit in which case the new Commit will have no parent and will
// initially appear empty.
// branch is a more convenient way to build linear chains of commits. When a
// commit is started with a non empty branch the value of branch becomes an
// alias for the created Commit. This enables a more intuitive access pattern.
// When the commit is started on a branch the previous head of the branch is
// used as the parent of the commit.
func (c APIClient) StartCommit(repoName string, parentCommit string, branch string) (*pfs.Commit, error) {
commit, err := c.PfsAPIClient.StartCommit(
context.Background(),
&pfs.StartCommitRequest{
Repo: NewRepo(repoName),
ParentID: parentCommit,
Branch: branch,
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return commit, nil
}
// FinishCommit ends the process of committing data to a Repo and persists the
// Commit. Once a Commit is finished the data becomes immutable and future
// attempts to write to it with PutFile will error.
func (c APIClient) FinishCommit(repoName string, commitID string) error {
_, err := c.PfsAPIClient.FinishCommit(
context.Background(),
&pfs.FinishCommitRequest{
Commit: NewCommit(repoName, commitID),
},
)
return sanitizeErr(err)
}
// CancelCommit ends the process of committing data to a repo. It differs from
// FinishCommit in that the Commit will not be used as a source for downstream
// pipelines. CancelCommit is used primarily by PPS for the output commits of
// errant jobs.
func (c APIClient) CancelCommit(repoName string, commitID string) error {
_, err := c.PfsAPIClient.FinishCommit(
context.Background(),
&pfs.FinishCommitRequest{
Commit: NewCommit(repoName, commitID),
Cancel: true,
},
)
return sanitizeErr(err)
}
// InspectCommit returns info about a specific Commit.
func (c APIClient) InspectCommit(repoName string, commitID string) (*pfs.CommitInfo, error) {
commitInfo, err := c.PfsAPIClient.InspectCommit(
context.Background(),
&pfs.InspectCommitRequest{
Commit: NewCommit(repoName, commitID),
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return commitInfo, nil
}
// ListCommit returns info about multiple commits.
// repoNames defines a set of Repos to consider commits from, if repoNames is left
// nil or empty then the result will be empty.
// fromCommitIDs lets you get info about Commits that occurred after this
// set of commits.
// commitType specifies the type of commit you want returned, normally CommitTypeRead is the most useful option
// block, when set to true, will cause ListCommit to block until at least 1 new CommitInfo is available.
// Using fromCommitIDs and block you can get subscription semantics from ListCommit.
// all, when set to true, will cause ListCommit to return cancelled commits as well.
// provenance specifies a set of provenance commits, only commits which have
// ALL of the specified commits as provenance will be returned unless
// provenance is nil in which case it is ignored.
func (c APIClient) ListCommit(repoNames []string, fromCommitIDs []string,
commitType pfs.CommitType, block bool, all bool, provenance []*pfs.Commit) ([]*pfs.CommitInfo, error) {
var repos []*pfs.Repo
for _, repoName := range repoNames {
repos = append(repos, &pfs.Repo{Name: repoName})
}
var fromCommits []*pfs.Commit
for i, fromCommitID := range fromCommitIDs {
fromCommits = append(fromCommits, &pfs.Commit{
Repo: repos[i],
ID: fromCommitID,
})
}
commitInfos, err := c.PfsAPIClient.ListCommit(
context.Background(),
&pfs.ListCommitRequest{
Repo: repos,
FromCommit: fromCommits,
Block: block,
All: all,
Provenance: provenance,
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return commitInfos.CommitInfo, nil
}
// ListBranch lists the active branches on a Repo.
func (c APIClient) ListBranch(repoName string) ([]*pfs.CommitInfo, error) {
commitInfos, err := c.PfsAPIClient.ListBranch(
context.Background(),
&pfs.ListBranchRequest{
Repo: NewRepo(repoName),
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return commitInfos.CommitInfo, nil
}
// DeleteCommit deletes a commit.
// Note it is currently not implemented.
func (c APIClient) DeleteCommit(repoName string, commitID string) error {
_, err := c.PfsAPIClient.DeleteCommit(
context.Background(),
&pfs.DeleteCommitRequest{
Commit: NewCommit(repoName, commitID),
},
)
return sanitizeErr(err)
}
// FlushCommit blocks until all of the commits which have a set of commits as
// provenance have finished. For commits to be considered they must have all of
// the specified commits as provenance. This in effect waits for all of the
// jobs that are triggered by a set of commits to complete.
// It returns an error if any of the commits it's waiting on are cancelled due
// to one of the jobs encountering an error during runtime.
// If toRepos is not nil then only the commits up to and including those repos
// will be considered, otherwise all repos are considered.
// Note that it's never necessary to call FlushCommit to run jobs, they'll run
// no matter what, FlushCommit just allows you to wait for them to complete and
// see their output once they do.
func (c APIClient) FlushCommit(commits []*pfs.Commit, toRepos []*pfs.Repo) ([]*pfs.CommitInfo, error) {
commitInfos, err := c.PfsAPIClient.FlushCommit(
context.Background(),
&pfs.FlushCommitRequest{
Commit: commits,
ToRepo: toRepos,
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return commitInfos.CommitInfo, nil
}
// PutBlock takes a reader and splits the data in it into blocks.
// Blocks are guaranteed to be new line delimited.
// Blocks are content addressed and are thus identified by hashes of the content.
// NOTE: this is lower level function that's used internally and might not be
// useful to users.
func (c APIClient) PutBlock(delimiter pfs.Delimiter, reader io.Reader) (blockRefs *pfs.BlockRefs, retErr error) {
writer, err := c.newPutBlockWriteCloser(delimiter)
if err != nil {
return nil, sanitizeErr(err)
}
defer func() {
err := writer.Close()
if err != nil && retErr == nil {
retErr = err
}
if retErr == nil {
blockRefs = writer.blockRefs
}
}()
_, retErr = io.Copy(writer, reader)
return blockRefs, retErr
}
// GetBlock returns the content of a block using it's hash.
// offset specifies a number of bytes that should be skipped in the beginning of the block.
// size limits the total amount of data returned, note you will get fewer bytes
// than size if you pass a value larger than the size of the block.
// If size is set to 0 then all of the data will be returned.
// NOTE: this is lower level function that's used internally and might not be
// useful to users.
func (c APIClient) GetBlock(hash string, offset uint64, size uint64) (io.Reader, error) {
apiGetBlockClient, err := c.BlockAPIClient.GetBlock(
context.Background(),
&pfs.GetBlockRequest{
Block: NewBlock(hash),
OffsetBytes: offset,
SizeBytes: size,
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return protostream.NewStreamingBytesReader(apiGetBlockClient), nil
}
// DeleteBlock deletes a block from the block store.
// NOTE: this is lower level function that's used internally and might not be
// useful to users.
func (c APIClient) DeleteBlock(block *pfs.Block) error {
_, err := c.BlockAPIClient.DeleteBlock(
context.Background(),
&pfs.DeleteBlockRequest{
Block: block,
},
)
return sanitizeErr(err)
}
// InspectBlock returns info about a specific Block.
func (c APIClient) InspectBlock(hash string) (*pfs.BlockInfo, error) {
blockInfo, err := c.BlockAPIClient.InspectBlock(
context.Background(),
&pfs.InspectBlockRequest{
Block: NewBlock(hash),
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return blockInfo, nil
}
// ListBlock returns info about all Blocks.
func (c APIClient) ListBlock() ([]*pfs.BlockInfo, error) {
blockInfos, err := c.BlockAPIClient.ListBlock(
context.Background(),
&pfs.ListBlockRequest{},
)
if err != nil {
return nil, sanitizeErr(err)
}
return blockInfos.BlockInfo, nil
}
// PutFileWriter writes a file to PFS.
// handle is used to perform multiple writes that are guaranteed to wind up
// contiguous in the final file. It may be safely left empty and likely won't
// be needed in most use cases.
// NOTE: PutFileWriter returns an io.WriteCloser you must call Close on it when
// you are done writing.
func (c APIClient) PutFileWriter(repoName string, commitID string, path string, delimiter pfs.Delimiter, handle string) (io.WriteCloser, error) {
return c.newPutFileWriteCloser(repoName, commitID, path, delimiter, handle)
}
// PutFile writes a file to PFS from a reader.
func (c APIClient) PutFile(repoName string, commitID string, path string, reader io.Reader) (_ int, retErr error) {
return c.PutFileWithDelimiter(repoName, commitID, path, pfs.Delimiter_LINE, reader)
}
//PutFileWithDelimiter writes a file to PFS from a reader
// delimiter is used to tell PFS how to break the input into blocks
func (c APIClient) PutFileWithDelimiter(repoName string, commitID string, path string, delimiter pfs.Delimiter, reader io.Reader) (_ int, retErr error) {
writer, err := c.PutFileWriter(repoName, commitID, path, delimiter, "")
if err != nil {
return 0, sanitizeErr(err)
}
defer func() {
if err := writer.Close(); err != nil && retErr == nil {
retErr = err
}
}()
written, err := io.Copy(writer, reader)
return int(written), err
}
// GetFile returns the contents of a file at a specific Commit.
// offset specifies a number of bytes that should be skipped in the beginning of the file.
// size limits the total amount of data returned, note you will get fewer bytes
// than size if you pass a value larger than the size of the file.
// If size is set to 0 then all of the data will be returned.
// fromCommitID lets you get only the data which was added after this Commit.
// shard allows you to downsample the data, returning only a subset of the
// blocks in the file. shard may be left nil in which case the entire file will be returned
func (c APIClient) GetFile(repoName string, commitID string, path string, offset int64,
size int64, fromCommitID string, shard *pfs.Shard, writer io.Writer) error {
return c.getFile(repoName, commitID, path, offset, size, fromCommitID, shard, false, "", writer)
}
func (c APIClient) GetFileUnsafe(repoName string, commitID string, path string, offset int64,
size int64, fromCommitID string, shard *pfs.Shard, handle string, writer io.Writer) error {
return c.getFile(repoName, commitID, path, offset, size, fromCommitID, shard, true, handle, writer)
}
func (c APIClient) getFile(repoName string, commitID string, path string, offset int64,
size int64, fromCommitID string, shard *pfs.Shard, unsafe bool, handle string, writer io.Writer) error {
if size == 0 {
size = math.MaxInt64
}
apiGetFileClient, err := c.PfsAPIClient.GetFile(
context.Background(),
&pfs.GetFileRequest{
File: NewFile(repoName, commitID, path),
Shard: shard,
OffsetBytes: offset,
SizeBytes: size,
FromCommit: newFromCommit(repoName, fromCommitID),
Unsafe: unsafe,
Handle: handle,
},
)
if err != nil {
return sanitizeErr(err)
}
if err := protostream.WriteFromStreamingBytesClient(apiGetFileClient, writer); err != nil {
return sanitizeErr(err)
}
return nil
}
// InspectFile returns info about a specific file. fromCommitID lets you get
// only info which was added after this Commit. shard allows you to downsample
// the data, returning info about only a subset of the blocks in the file.
// shard may be left nil in which case info about the entire file will be
// returned
func (c APIClient) InspectFile(repoName string, commitID string, path string,
fromCommitID string, shard *pfs.Shard) (*pfs.FileInfo, error) {
return c.inspectFile(repoName, commitID, path, fromCommitID, shard, false, "")
}
func (c APIClient) InspectFileUnsafe(repoName string, commitID string, path string,
fromCommitID string, shard *pfs.Shard, handle string) (*pfs.FileInfo, error) {
return c.inspectFile(repoName, commitID, path, fromCommitID, shard, true, handle)
}
func (c APIClient) inspectFile(repoName string, commitID string, path string,
fromCommitID string, shard *pfs.Shard, unsafe bool, handle string) (*pfs.FileInfo, error) {
fileInfo, err := c.PfsAPIClient.InspectFile(
context.Background(),
&pfs.InspectFileRequest{
File: NewFile(repoName, commitID, path),
Shard: shard,
FromCommit: newFromCommit(repoName, fromCommitID),
Unsafe: unsafe,
Handle: handle,
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return fileInfo, nil
}
// ListFile returns info about all files in a Commit.
// fromCommitID lets you get only info which was added after this Commit.
// shard allows you to downsample the data, returning info about only a subset
// of the blocks in the files or only a subset of files. shard may be left nil
// in which case info about all the files and all the blocks in those files
// will be returned.
// recurse causes ListFile to accurately report the size of data stored in directories, it makes the call more expensive
func (c APIClient) ListFile(repoName string, commitID string, path string, fromCommitID string,
shard *pfs.Shard, recurse bool) ([]*pfs.FileInfo, error) {
return c.listFile(repoName, commitID, path, fromCommitID, shard, recurse, false, "")
}
// ListFileUnsafe is identical to ListFile except that it will consider files in unfinished commits.
// handle can be used to specify a specific set of dirty writes that you're interested in.
func (c APIClient) ListFileUnsafe(repoName string, commitID string, path string, fromCommitID string,
shard *pfs.Shard, recurse bool, handle string) ([]*pfs.FileInfo, error) {
return c.listFile(repoName, commitID, path, fromCommitID, shard, recurse, true, handle)
}
func (c APIClient) listFile(repoName string, commitID string, path string, fromCommitID string,
shard *pfs.Shard, recurse bool, unsafe bool, handle string) ([]*pfs.FileInfo, error) {
fileInfos, err := c.PfsAPIClient.ListFile(
context.Background(),
&pfs.ListFileRequest{
File: NewFile(repoName, commitID, path),
Shard: shard,
FromCommit: newFromCommit(repoName, fromCommitID),
Recurse: recurse,
Unsafe: unsafe,
Handle: handle,
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return fileInfos.FileInfo, nil
}
// DeleteFile deletes a file from a Commit.
// DeleteFile leaves a tombstone in the Commit, assuming the file isn't written
// to later attempting to get the file from the finished commit will result in
// not found error.
// The file will of course remain intact in the Commit's parent.
func (c APIClient) DeleteFile(repoName string, commitID string, path string, unsafe bool, handle string) error {
_, err := c.PfsAPIClient.DeleteFile(
context.Background(),
&pfs.DeleteFileRequest{
File: NewFile(repoName, commitID, path),
Unsafe: unsafe,
Handle: handle,
},
)
return err
}
// MakeDirectory creates a directory in PFS.
// Note directories are created implicitly by PutFile, so you technically never
// need this function unless you want to create an empty directory.
func (c APIClient) MakeDirectory(repoName string, commitID string, path string) (retErr error) {
putFileClient, err := c.PfsAPIClient.PutFile(context.Background())
if err != nil {
return sanitizeErr(err)
}
defer func() {
if _, err := putFileClient.CloseAndRecv(); err != nil && retErr == nil {
retErr = sanitizeErr(err)
}
}()
return sanitizeErr(putFileClient.Send(
&pfs.PutFileRequest{
File: NewFile(repoName, commitID, path),
FileType: pfs.FileType_FILE_TYPE_DIR,
},
))
}
type putFileWriteCloser struct {
request *pfs.PutFileRequest
putFileClient pfs.API_PutFileClient
sent bool
}
func (c APIClient) newPutFileWriteCloser(repoName string, commitID string, path string, delimiter pfs.Delimiter, handle string) (*putFileWriteCloser, error) {
putFileClient, err := c.PfsAPIClient.PutFile(context.Background())
if err != nil {
return nil, err
}
return &putFileWriteCloser{
request: &pfs.PutFileRequest{
File: NewFile(repoName, commitID, path),
FileType: pfs.FileType_FILE_TYPE_REGULAR,
Handle: handle,
Delimiter: delimiter,
},
putFileClient: putFileClient,
}, nil
}
func (w *putFileWriteCloser) Write(p []byte) (int, error) {
w.request.Value = p
if err := w.putFileClient.Send(w.request); err != nil {
return 0, sanitizeErr(err)
}
w.sent = true
w.request.Value = nil
// File is only needed on the first request
w.request.File = nil
return len(p), nil
}
func (w *putFileWriteCloser) Close() error {
// we always send at least one request, otherwise it's impossible to create
// an empty file
if !w.sent {
if err := w.putFileClient.Send(w.request); err != nil {
return err
}
}
_, err := w.putFileClient.CloseAndRecv()
return sanitizeErr(err)
}
type putBlockWriteCloser struct {
request *pfs.PutBlockRequest
putBlockClient pfs.BlockAPI_PutBlockClient
blockRefs *pfs.BlockRefs
}
func (c APIClient) newPutBlockWriteCloser(delimiter pfs.Delimiter) (*putBlockWriteCloser, error) {
putBlockClient, err := c.BlockAPIClient.PutBlock(context.Background())
if err != nil {
return nil, err
}
return &putBlockWriteCloser{
request: &pfs.PutBlockRequest{
Delimiter: delimiter,
},
putBlockClient: putBlockClient,
blockRefs: &pfs.BlockRefs{},
}, nil
}
func (w *putBlockWriteCloser) Write(p []byte) (int, error) {
w.request.Value = p
if err := w.putBlockClient.Send(w.request); err != nil {
return 0, sanitizeErr(err)
}
return len(p), nil
}
func (w *putBlockWriteCloser) Close() error {
var err error
w.blockRefs, err = w.putBlockClient.CloseAndRecv()
return sanitizeErr(err)
}
func newFromCommit(repoName string, fromCommitID string) *pfs.Commit {
if fromCommitID != "" {
return NewCommit(repoName, fromCommitID)
}
return nil
}