/
dataset.go
151 lines (135 loc) · 3.59 KB
/
dataset.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
// Copyright 2019 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package runner
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"sync"
"github.com/grailbio/base/errors"
"github.com/grailbio/base/file"
"github.com/grailbio/base/log"
"github.com/grailbio/diviner"
)
// A dataset represents a process that creates a datataset, which
// runs may depend on.
type dataset struct {
diviner.Dataset
donec chan struct{}
mu sync.Mutex
status status
err error
}
// NewDataset creates a new runnable dataset from a diviner dataset
// configuration.
func newDataset(d diviner.Dataset) *dataset {
return &dataset{
Dataset: d,
donec: make(chan struct{}),
}
}
// Do processes the dataset, possibly allocating a worker from the
// provided runner. Upon return, the dataset's status must be done.
func (d *dataset) Do(ctx context.Context, runner *Runner) {
// First check if the dataset already exists.
if url := d.IfNotExist; url != "" {
if stat, err := file.Stat(ctx, url); err == nil {
Logger.Printf("dataset %s: found %s, with modtime %v", d.Name, url, stat.ModTime())
d.setStatus(statusOk)
return
} else if !errors.Is(errors.NotExist, err) {
d.error(errors.E("dataset: ifnotexist", url, err))
return
}
}
Logger.Printf("dataset %s: %s not found, start data generation", d.Name, d.IfNotExist)
w, err := runner.allocate(ctx, d.Systems)
if err != nil {
d.error(errors.E("dataset: allocate", d.Systems, err))
return
}
defer w.Return()
d.setStatus(statusRunning)
// First clean up the workspace.
if err := w.Reset(ctx); err != nil {
d.error(err)
return
}
if err := w.CopyFiles(ctx, d.LocalFiles); err != nil {
d.error(errors.E(fmt.Sprintf("dataset copyfiles %+v: %v", d.LocalFiles, err)))
return
}
out, err := w.Run(ctx, d.Script, nil)
if err != nil {
d.error(errors.E(fmt.Sprintf("dataset: failed to start script '%s'", d.Script), err))
return
}
var writer io.Writer = ioutil.Discard
path := fmt.Sprintf("dataset.%s.log", d.Name)
path = strings.Replace(path, "/", "_", -1)
if f, err := os.Create(path); err == nil {
writer = f
defer f.Close()
} else {
log.Error.Printf("dataset: create %s: %v", path, err)
}
_, err = io.Copy(writer, out)
if e := out.Close(); e != nil && err == nil {
err = e
}
if err == nil {
d.setStatus(statusOk)
} else {
d.error(err)
}
}
// Done returns a channel that is closed when the dataset run
// completes.
func (d *dataset) Done() <-chan struct{} {
return d.donec
}
// Status returns the dataset's current status.
func (d *dataset) Status() status {
d.mu.Lock()
defer d.mu.Unlock()
return d.status
}
func (d *dataset) setStatus(status status) {
d.mu.Lock()
defer d.mu.Unlock()
d.setStatusLocked(status)
}
// SetStatus sets the datasets current status.
func (d *dataset) setStatusLocked(status status) {
done := !d.status.Done() && status.Done()
d.status = status
if done {
close(d.donec)
}
}
// Error sets the dataset's status to statusErr and
// its error to the provided error.
func (d *dataset) error(err error) {
log.Error.Printf("dataset %s: %v", d.Name, err)
d.mu.Lock()
defer d.mu.Unlock()
d.err = err
d.setStatusLocked(statusErr)
}
func (d *dataset) errorf(format string, v ...interface{}) {
d.error(fmt.Errorf(format, v...))
}
// Err returns the dataset's error, if any.
func (d *dataset) Err() error {
d.mu.Lock()
defer d.mu.Unlock()
return d.err
}
// String returns a textual representation of this dataset.
func (d *dataset) String() string {
return fmt.Sprintf("%s (%s)", d.Dataset.Name, d.Status())
}