-
Notifications
You must be signed in to change notification settings - Fork 4
/
runner.go
359 lines (321 loc) · 9.63 KB
/
runner.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
package gomrjob
import (
"context"
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"os/user"
"path/filepath"
"strings"
"time"
"github.com/jehiah/gomrjob/dataproc"
"github.com/jehiah/gomrjob/hdfs"
"github.com/jehiah/gomrjob/internal/gcloud"
"github.com/jehiah/gomrjob/internal/storage"
)
var (
submitJob = flag.Bool("submit-job", false, "submit the job")
// internal flags used by the map-reduce steps
stage = flag.String("stage", "", "map,reduce")
step = flag.Int("step", 0, "the step to execute")
remoteLogger = flag.String("remote-logger", "", "address for remote logger")
// flags for Dataproc support
bucket = flag.String("bucket", "", "Google Storage bucket to use | GS_BUCKET")
project = flag.String("project", "", "Google Cloud Project ID | GS_PROJECT")
cluster = flag.String("cluster", "", "Dataproc cluster | GS_CLUSTER")
region = flag.String("region", "", "Dataproc region | GS_REGION")
serviceAccount = flag.String("service_account", "", "Google Storage Service Account JSON | GOOGLE_APPLICATION_CREDENTIALS")
)
type JobType int8
const (
HDFS JobType = iota
Dataproc
)
const executibleName = "gomrjob_binary" // The filenamename used for the executible when uploaded
type Runner struct {
Name string
Steps []Step
// Inputfiles can be of the format `/pattern/to/files*.gz` or `hdfs:///pattern/to/files*.gz` or `s3://bucket/pattern`
InputFiles []string
Output string // fully qualified
ReducerTasks int
PassThroughOptions []string // CLI arguments to $exe when run as map / reduce tasks
CompressOutput bool
CacheFiles []string // -files
Files []string // -file
Properties map[string]string // -D key=value argumets to mapreduce-streaming.jar
JobType JobType
defaultProto string
tmpPath string
gcloud *http.Client
}
// LoadAndValidateFlags loads flags from env and checks for missing arguments
func LoadAndValidateFlags() {
// bootstrap missing flags from environment (if set)
for env, target := range map[string]*string{
"GOOGLE_APPLICATION_CREDENTIALS": serviceAccount,
"GS_REGION": region,
"GS_PROJECT": project,
"GS_CLUSTER": cluster,
"GS_BUCKET": bucket,
} {
if have := os.Getenv(env); have != "" && *target == "" {
*target = have
}
}
if *serviceAccount == "" {
return
}
switch {
case *project == "":
log.Fatal("missing --project")
case *cluster == "":
log.Fatal("missing --cluster")
case *region == "":
log.Fatal("missing --region")
case *bucket == "":
log.Fatal("missing --bucket")
}
}
func NewRunner() *Runner {
r := &Runner{
ReducerTasks: 30,
Properties: make(map[string]string),
JobType: HDFS,
defaultProto: "hdfs:///",
}
r.setTempPath()
return r
}
func (r *Runner) setTempPath() {
user, err := user.Current()
var username = ""
if err == nil {
username = user.Username
}
now := time.Now().Format("20060102-150405")
r.tmpPath = fmt.Sprintf("user/%s/tmp/%s.%s", username, r.Name, now)
}
func (r *Runner) Cleanup() error {
switch r.JobType {
case HDFS:
return hdfs.RMR(r.tmpPath)
case Dataproc:
return storage.DeletePrefix(context.Background(), r.gcloud, *bucket, r.tmpPath)
}
panic("invalid job type")
}
// submitJob runs a single map/combine/reduce job.
func (r *Runner) submitJob(loggerAddress string, stepNumber int, step Step) error {
if stepNumber >= len(r.Steps) || len(r.Steps) == 0 {
return fmt.Errorf("step %d out of range", stepNumber)
}
var input []string
var output string
if stepNumber == len(r.Steps)-1 && r.Output != "" {
output = r.Output
} else {
if len(r.Steps) == 1 {
output = fmt.Sprintf("%s/output", r.tmpPath)
} else {
output = fmt.Sprintf("%s/step_%d/output", r.tmpPath, stepNumber)
}
}
if stepNumber == 0 {
input = r.InputFiles
} else {
input = append(input, fmt.Sprintf("%s/step_%d/output/part-*", r.tmpPath, stepNumber-1))
}
taskOptions := append([]string{executibleName}, r.PassThroughOptions...)
if loggerAddress != "" {
taskOptions = append(taskOptions, fmt.Sprintf("--remote-logger=%s", loggerAddress))
}
taskOptions = append(taskOptions, fmt.Sprintf("--step=%d", stepNumber))
taskString := strings.Join(taskOptions, " ")
if r.CompressOutput {
r.Properties["mapred.output.compress"] = "true"
r.Properties["mapred.output.compression.codec"] = "org.apache.hadoop.io.compress.GzipCodec"
}
name := r.Name
if len(r.Steps) != 1 {
name = fmt.Sprintf("%s-step_%d", name, stepNumber)
}
// StepReducerTasksCount interface overrides reducer tasks per step
reducerTasks := r.ReducerTasks
if step, ok := step.(StepReducerTasksCount); ok {
reducerTasks = step.NumberReducerTasks()
}
j := hdfs.Job{
Name: name,
ReducerTasks: reducerTasks,
Input: input,
Output: output,
Mapper: fmt.Sprintf("%s --stage=mapper", taskString),
Reducer: fmt.Sprintf("%s --stage=reducer", taskString),
Files: r.Files,
Properties: r.Properties,
CacheFiles: r.CacheFiles,
DefaultProto: r.defaultProto,
}
if _, ok := step.(Combiner); ok {
j.Combiner = fmt.Sprintf("%s --stage=combiner", taskString)
}
switch r.JobType {
case HDFS:
return hdfs.SubmitJob(j)
case Dataproc:
return dataproc.SubmitJob(j, r.gcloud, *project, *region, *cluster)
default:
panic("unknown job type")
}
}
func (r *Runner) copyRunningBinaryToHdfs() error {
// copy the current executible binary to hadoop for use as the map reduce tasks
localExePath, err := filepath.EvalSymlinks("/proc/self/exe")
if err != nil {
return fmt.Errorf("failed locating running executable %s", err)
}
exePath := fmt.Sprintf("%s/%s", r.tmpPath, executibleName)
if err := hdfs.Put(localExePath, exePath); err != nil {
return fmt.Errorf("error copying %s to hdfs %s", exePath, err)
}
r.CacheFiles = append(r.CacheFiles, fmt.Sprintf("%s%s#%s", r.defaultProto, exePath, executibleName))
return nil
}
func (r *Runner) cacheFileInGoogleStorage(ctx context.Context, src, target string) error {
f, err := os.Open(src)
if err != nil {
return err
}
defer f.Close()
cachedFile := fmt.Sprintf("gs://%s/%s", *bucket, target)
log.Printf("uploading %s as %s", src, cachedFile)
var contentType string
err = storage.Insert(ctx, r.gcloud, *bucket, target, contentType, f)
if err != nil {
return err
}
r.CacheFiles = append(r.CacheFiles, cachedFile)
return nil
}
func (r *Runner) copyRunningBinaryToDataproc(ctx context.Context) error {
exePath := fmt.Sprintf("%s/%s", r.tmpPath, executibleName)
return r.cacheFileInGoogleStorage(ctx, "/proc/self/exe", exePath)
}
// return which stage the runner is executing as
func (r *Runner) Stage() string {
switch *stage {
case "mapper", "reducer", "combiner":
return *stage
}
if *submitJob {
return "submit-job"
}
return "unknown"
}
// Run is the program entry point from main()
//
// When executed directly (--stage=”) uploads loads the executibile
// and submits mapreduce jobs for each stage of the program
func (r *Runner) Run() error {
if *step >= len(r.Steps) {
return fmt.Errorf("invalid --step=%d (max %d)", *step, len(r.Steps))
}
if *remoteLogger != "" {
conn, err := dialRemoteLogger(*remoteLogger)
if err != nil {
if *stage == "" {
Status(fmt.Sprintf("error dialing remote logger %s", err))
} else {
log.Printf("failed connecting to remote logger %s", err)
}
} else {
hostname, _ := os.Hostname()
w := newPrefixLogger(fmt.Sprintf("[%s %s:%d] ", hostname, *stage, *step), conn)
log.SetOutput(w)
}
}
s := r.Steps[*step]
if *stage != "" {
log.Printf("starting %s step %d", *stage, *step)
}
var err error
switch *stage {
case "mapper":
s, ok := s.(Mapper)
if !ok {
// if a step does not support mapper, it's the identity mapper of just echo std -> stdout
_, err = io.Copy(os.Stdout, os.Stdin)
} else {
err = s.Mapper(os.Stdin, os.Stdout)
}
case "reducer":
err = s.Reducer(os.Stdin, os.Stdout)
case "combiner":
s, ok := s.(Combiner)
if !ok {
return errors.New("step does not support Combiner interface")
}
err = s.Combiner(os.Stdin, os.Stdout)
}
if *stage != "" {
auditCpuTime("gomrjob", fmt.Sprintf("%s[%d]", *stage, *step))
if err != nil {
log.Printf("Error: %s", err)
os.Exit(1)
} else {
os.Exit(0)
}
return nil
}
if !*submitJob {
return errors.New("missing --submit-job")
}
r.setTempPath()
LoadAndValidateFlags()
if *serviceAccount != "" {
r.gcloud, err = gcloud.LoadFromServiceJSON(*serviceAccount, gcloud.ScopeCloudPlatform, gcloud.ScopeStorageReadWrite)
if err != nil {
log.Fatal(err)
}
r.JobType = Dataproc
r.defaultProto = fmt.Sprintf("gs://%s/", *bucket)
}
switch r.JobType {
case HDFS:
if err := hdfs.FsCmd("-mkdir", "-p", r.defaultProto+r.tmpPath); err != nil {
return err
}
if err := r.copyRunningBinaryToHdfs(); err != nil {
return err
}
case Dataproc:
ctx := context.Background()
if err := r.copyRunningBinaryToDataproc(ctx); err != nil {
return err
}
// since -file on the hadoop-streaming.jar submission doesn't refer to a local file
// we need to upload the files to Google Storage and use CacheFiles
for _, f := range r.Files {
target := fmt.Sprintf("%s/%s", r.tmpPath, filepath.Base(f))
if err := r.cacheFileInGoogleStorage(ctx, f, target); err != nil {
return err
}
}
r.Files = []string{}
}
loggerAddress := startRemoteLogListner()
if r.Output == "" {
r.Output = fmt.Sprintf("%s%s/output", r.defaultProto, r.tmpPath)
}
for stepNumber, step := range r.Steps {
if err := r.submitJob(loggerAddress, stepNumber, step); err != nil {
return fmt.Errorf("failed running Step %d = %s", stepNumber, err)
}
}
return nil
}