-
Notifications
You must be signed in to change notification settings - Fork 41
/
driver.go
262 lines (220 loc) · 6.5 KB
/
driver.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
package corral
import (
"context"
"fmt"
"os"
"runtime"
"runtime/pprof"
"sync"
"time"
"github.com/dustin/go-humanize"
"github.com/spf13/viper"
"golang.org/x/sync/semaphore"
log "github.com/sirupsen/logrus"
pb "gopkg.in/cheggaaa/pb.v1"
"github.com/aws/aws-lambda-go/lambda"
"github.com/bcongdon/corral/internal/pkg/corfs"
flag "github.com/spf13/pflag"
)
// Driver controls the execution of a MapReduce Job
type Driver struct {
jobs []*Job
config *config
executor executor
}
// config configures a Driver's execution of jobs
type config struct {
Inputs []string
SplitSize int64
MapBinSize int64
ReduceBinSize int64
MaxConcurrency int
WorkingLocation string
Cleanup bool
}
func newConfig() *config {
loadConfig() // Load viper config from settings file(s) and environment
// Register command line flags
flag.Parse()
viper.BindPFlags(flag.CommandLine)
return &config{
Inputs: []string{},
SplitSize: viper.GetInt64("splitSize"),
MapBinSize: viper.GetInt64("mapBinSize"),
ReduceBinSize: viper.GetInt64("reduceBinSize"),
MaxConcurrency: viper.GetInt("maxConcurrency"),
WorkingLocation: viper.GetString("workingLocation"),
Cleanup: viper.GetBool("cleanup"),
}
}
// Option allows configuration of a Driver
type Option func(*config)
// NewDriver creates a new Driver with the provided job and optional configuration
func NewDriver(job *Job, options ...Option) *Driver {
d := &Driver{
jobs: []*Job{job},
executor: localExecutor{},
}
c := newConfig()
for _, f := range options {
f(c)
}
if c.SplitSize > c.MapBinSize {
log.Warn("Configured Split Size is larger than Map Bin size")
c.SplitSize = c.MapBinSize
}
d.config = c
log.Debugf("Loaded config: %#v", c)
return d
}
// NewMultiStageDriver creates a new Driver with the provided jobs and optional configuration
func NewMultiStageDriver(jobs []*Job, options ...Option) *Driver {
driver := NewDriver(nil, options...)
driver.jobs = jobs
return driver
}
// WithSplitSize sets the SplitSize of the Driver
func WithSplitSize(s int64) Option {
return func(c *config) {
c.SplitSize = s
}
}
// WithMapBinSize sets the MapBinSize of the Driver
func WithMapBinSize(s int64) Option {
return func(c *config) {
c.MapBinSize = s
}
}
// WithReduceBinSize sets the ReduceBinSize of the Driver
func WithReduceBinSize(s int64) Option {
return func(c *config) {
c.ReduceBinSize = s
}
}
// WithWorkingLocation sets the location and filesystem backend of the Driver
func WithWorkingLocation(location string) Option {
return func(c *config) {
c.WorkingLocation = location
}
}
// WithInputs specifies job inputs (i.e. input files/directories)
func WithInputs(inputs ...string) Option {
return func(c *config) {
c.Inputs = append(c.Inputs, inputs...)
}
}
func (d *Driver) runMapPhase(job *Job, jobNumber int, inputs []string) {
inputSplits := job.inputSplits(inputs, d.config.SplitSize)
if len(inputSplits) == 0 {
log.Warnf("No input splits")
return
}
log.Debugf("Number of job input splits: %d", len(inputSplits))
inputBins := packInputSplits(inputSplits, d.config.MapBinSize)
log.Debugf("Number of job input bins: %d", len(inputBins))
bar := pb.New(len(inputBins)).Prefix("Map").Start()
var wg sync.WaitGroup
sem := semaphore.NewWeighted(int64(d.config.MaxConcurrency))
for binID, bin := range inputBins {
sem.Acquire(context.Background(), 1)
wg.Add(1)
go func(bID uint, b []inputSplit) {
defer wg.Done()
defer sem.Release(1)
defer bar.Increment()
err := d.executor.RunMapper(job, jobNumber, bID, b)
if err != nil {
log.Errorf("Error when running mapper %d: %s", bID, err)
}
}(uint(binID), bin)
}
wg.Wait()
bar.Finish()
}
func (d *Driver) runReducePhase(job *Job, jobNumber int) {
var wg sync.WaitGroup
bar := pb.New(int(job.intermediateBins)).Prefix("Reduce").Start()
for binID := uint(0); binID < job.intermediateBins; binID++ {
wg.Add(1)
go func(bID uint) {
defer wg.Done()
defer bar.Increment()
err := d.executor.RunReducer(job, jobNumber, bID)
if err != nil {
log.Errorf("Error when running reducer %d: %s", bID, err)
}
}(binID)
}
wg.Wait()
bar.Finish()
}
// run starts the Driver
func (d *Driver) run() {
if runningInLambda() {
lambdaDriver = d
lambda.Start(handleRequest)
}
if lBackend, ok := d.executor.(*lambdaExecutor); ok {
lBackend.Deploy()
}
if len(d.config.Inputs) == 0 {
log.Error("No inputs!")
return
}
inputs := d.config.Inputs
for idx, job := range d.jobs {
// Initialize job filesystem
job.fileSystem = corfs.InferFilesystem(inputs[0])
jobWorkingLoc := d.config.WorkingLocation
log.Infof("Starting job%d (%d/%d)", idx, idx+1, len(d.jobs))
if len(d.jobs) > 1 {
jobWorkingLoc = job.fileSystem.Join(jobWorkingLoc, fmt.Sprintf("job%d", idx))
}
job.outputPath = jobWorkingLoc
*job.config = *d.config
d.runMapPhase(job, idx, inputs)
d.runReducePhase(job, idx)
// Set inputs of next job to be outputs of current job
inputs = []string{job.fileSystem.Join(jobWorkingLoc, "output-*")}
log.Infof("Job %d - Total Bytes Read:\t%s", idx, humanize.Bytes(uint64(job.bytesRead)))
log.Infof("Job %d - Total Bytes Written:\t%s", idx, humanize.Bytes(uint64(job.bytesWritten)))
}
}
var lambdaFlag = flag.Bool("lambda", false, "Use lambda backend")
var outputDir = flag.StringP("out", "o", "", "Output `directory` (can be local or in S3)")
var memprofile = flag.String("memprofile", "", "Write memory profile to `file`")
var verbose = flag.BoolP("verbose", "v", false, "Output verbose logs")
var undeploy = flag.Bool("undeploy", false, "Undeploy the Lambda function and IAM permissions without running the driver")
// Main starts the Driver, running the submitted jobs.
func (d *Driver) Main() {
if viper.GetBool("verbose") {
log.SetLevel(log.DebugLevel)
}
if *undeploy {
lambda := newLambdaExecutor(viper.GetString("lambdaFunctionName"))
lambda.Undeploy()
return
}
d.config.Inputs = append(d.config.Inputs, flag.Args()...)
if *lambdaFlag {
d.executor = newLambdaExecutor(viper.GetString("lambdaFunctionName"))
}
if *outputDir != "" {
d.config.WorkingLocation = *outputDir
}
start := time.Now()
d.run()
end := time.Now()
fmt.Printf("Job Execution Time: %s\n", end.Sub(start))
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
f.Close()
}
}