-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrunner.go
197 lines (168 loc) · 4.35 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
// Runs a job (or part of a job). There are three primary types of runners
//
// 1. LocalRunner - Used for simulating a job locally. The sorting and
// combining functions of Hadoop will be emulated as best as possible, though
// no guarantees are made
// 2. TaskPhaseRunner - Used inter-step during a Hadoop job. This runs a single
// phase of a task
// 3. JobRunner - Submits a multi-task Job to hadoop, organizing temporary
// files and forking the necessary processes.
package gossamr
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"reflect"
)
// Given the arguments, figure out which runner should be used.
func GetRunner(args []string) (Runner, error) {
if argsContain(args, "-task") {
return TaskPhaseRunnerFromArgs(args)
}
return new(LocalRunner), nil
}
func argsContain(args []string, s string) bool {
for _, arg := range args {
if arg == s {
return true
}
}
return false
}
type Runner interface {
Run(job *Job) error
}
// LocalRunner
type LocalRunner struct {
root string
}
func (lr *LocalRunner) Run(j *Job) (err error) {
if lr.root, err = ioutil.TempDir("", "gossamr-"); err != nil {
return
}
log.Printf("Working in %s", lr.root)
defer os.RemoveAll(lr.root)
return lr.runJob(j)
}
func (lr *LocalRunner) runJob(j *Job) (err error) {
input := NewLineReader(os.Stdin)
var fname string
var output *os.File
for i, task := range j.tasks {
if fname, err = lr.runTask(i, task, input); err != nil {
return
}
}
if fname == "" {
return nil
}
if output, err = os.Open(fname); err != nil {
return
}
reader := NewPairReader(output)
writer := NewStringWriter(os.Stdout)
return Copy(reader, writer)
}
func (lr *LocalRunner) runTask(i int, t *Task, in Reader) (output string, err error) {
var f *os.File
mapper, hasMapper := t.mapper()
combiner, hasCombiner := t.combiner()
reducer, hasReducer := t.reducer()
// A task must have a mapper
if !hasMapper {
return "", fmt.Errorf("Task[%d] has no mapper", i)
}
mapOutput, err := lr.open(i, "mapper")
if err != nil {
return "", err
}
output = mapOutput.Name()
if hasCombiner || hasReducer {
if err = lr.execSorted(t, mapper, in, mapOutput); err != nil {
return
}
} else {
if err = lr.exec(t, mapper, in, mapOutput); err != nil {
return
}
}
if hasCombiner {
if f, err = os.Open(output); err != nil {
return output, err
}
in = NewGroupedReader(NewPairReader(f))
combineOutput, err := lr.open(i, "combiner")
if err != nil {
return "", err
}
output = combineOutput.Name()
if err = lr.execSorted(t, combiner, in, combineOutput); err != nil {
return output, err
}
}
if hasReducer {
if f, err = os.Open(output); err != nil {
return output, err
}
in = NewGroupedReader(NewPairReader(f))
reduceOutput, err := lr.open(i, "reducer")
if err != nil {
return "", err
}
output = reduceOutput.Name()
if err = lr.execSorted(t, reducer, in, reduceOutput); err != nil {
return output, err
}
}
return
}
func (lr *LocalRunner) execSorted(t *Task, f reflect.Value, r Reader, out *os.File) error {
w, err := NewSortWriter(out, 1024*1024)
if err != nil {
return err
}
return t.run(f, r, w)
}
func (lr *LocalRunner) exec(t *Task, f reflect.Value, r Reader, out *os.File) error {
w := NewPairWriter(out)
return t.run(f, r, w)
}
func (lr *LocalRunner) open(i int, name string) (f *os.File, err error) {
fname := path.Join(lr.root, fmt.Sprintf("%03d-%s", i, name))
return os.OpenFile(fname, os.O_RDWR|os.O_CREATE, 0644)
}
// TaskPhaseRunner
// Runs a single phase of a task forked from Hadoop. It is assumed that all
// input and output will be typed bytes at this point.
type TaskPhaseRunner struct {
taskNo int
phase Phase
}
func TaskPhaseRunnerFromArgs(args []string) (tpr *TaskPhaseRunner, err error) {
fs := flag.NewFlagSet(args[0], flag.ContinueOnError)
taskNo := fs.Int("task", 0, "task # to run")
phaseName := fs.String("phase", "", "phase of task to run")
if err = fs.Parse(args[1:]); err != nil {
return
}
phase, err := GetPhase(*phaseName)
if err != nil {
return nil, err
}
tpr = &TaskPhaseRunner{
taskNo: *taskNo,
phase: phase,
}
return
}
func (tpr *TaskPhaseRunner) Run(j *Job) error {
if tpr.taskNo > len(j.tasks)-1 {
return fmt.Errorf("No task %d", tpr.taskNo)
}
task := j.tasks[tpr.taskNo]
log.Printf("Running phase %d with TaskPhaseRunner", tpr.phase)
return task.Run(tpr.phase, os.Stdin, os.Stdout)
}