This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapreduce.go
135 lines (108 loc) · 3.75 KB
/
mapreduce.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
// Package mapreduce is client to launching operations over YT.
package mapreduce
import (
"bytes"
"encoding/gob"
"fmt"
"io"
"github.com/go-faster/errors"
"github.com/go-faster/yt/mapreduce/spec"
"github.com/go-faster/yt/yt"
)
func jobCommand(job Job, outputPipes int) string {
return fmt.Sprintf("./go-binary -job %s -output-pipes %d", jobName(job), outputPipes)
}
func encodeJob(job *jobState) (b bytes.Buffer, err error) {
enc := gob.NewEncoder(&b)
err = enc.Encode(&job)
return
}
func decodeJob(r io.Reader) (state *jobState, err error) {
dec := gob.NewDecoder(r)
err = dec.Decode(&state)
return
}
func (mr *client) prepare(spec *spec.Spec) *prepare {
return &prepare{
mr: mr,
spec: spec.Clone(),
ctx: mr.ctx,
}
}
func (mr *client) Map(mapper Job, s *spec.Spec, opts ...OperationOption) (Operation, error) {
p := mr.prepare(s)
p.mapperState = new(jobState)
p.addJobCommand(mapper, &p.spec.Mapper, p.mapperState, len(p.spec.OutputTablePaths))
return p.start(opts)
}
func (mr *client) Reduce(reducer Job, s *spec.Spec, opts ...OperationOption) (Operation, error) {
p := mr.prepare(s)
p.reducerState = new(jobState)
p.addJobCommand(reducer, &p.spec.Reducer, p.reducerState, len(p.spec.OutputTablePaths))
return p.start(opts)
}
func (mr *client) JoinReduce(reducer Job, s *spec.Spec, opts ...OperationOption) (Operation, error) {
p := mr.prepare(s)
p.reducerState = new(jobState)
p.addJobCommand(reducer, &p.spec.Reducer, p.reducerState, len(p.spec.OutputTablePaths))
return p.start(opts)
}
func (mr *client) MapReduce(mapper, reducer Job, s *spec.Spec, opts ...OperationOption) (Operation, error) {
p := mr.prepare(s)
if mapper != nil {
p.mapperState = new(jobState)
p.addJobCommand(mapper, &p.spec.Mapper, p.mapperState, 1+p.spec.MapperOutputTableCount)
}
p.reducerState = new(jobState)
p.addJobCommand(reducer, &p.spec.Reducer, p.reducerState, len(p.spec.OutputTablePaths)-p.spec.MapperOutputTableCount)
return p.start(opts)
}
func (mr *client) MapCombineReduce(mapper, combiner, reducer Job, s *spec.Spec, opts ...OperationOption) (Operation, error) {
p := mr.prepare(s)
p.mapperState = new(jobState)
p.addJobCommand(mapper, &p.spec.Mapper, p.mapperState, 1+p.spec.MapperOutputTableCount)
p.combinerState = new(jobState)
p.addJobCommand(mapper, &p.spec.ReduceCombiner, p.combinerState, 1)
p.reducerState = new(jobState)
p.addJobCommand(reducer, &p.spec.Reducer, p.reducerState, len(p.spec.OutputTablePaths)-p.spec.MapperOutputTableCount)
return p.start(opts)
}
func (mr *client) Sort(s *spec.Spec, opts ...OperationOption) (Operation, error) {
return mr.prepare(s).start(opts)
}
func (mr *client) Merge(s *spec.Spec, opts ...OperationOption) (Operation, error) {
return mr.prepare(s).start(opts)
}
func (mr *client) Erase(s *spec.Spec, opts ...OperationOption) (Operation, error) {
return mr.prepare(s).start(opts)
}
func (mr *client) RemoteCopy(s *spec.Spec, opts ...OperationOption) (Operation, error) {
return mr.prepare(s).start(opts)
}
func (mr *client) Vanilla(s *spec.Spec, jobs map[string]Job, opts ...OperationOption) (Operation, error) {
p := mr.prepare(s)
p.tasksState = map[string]*jobState{}
for name, job := range jobs {
us, ok := p.spec.Tasks[name]
if !ok {
return nil, errors.Errorf("yt: task %q is not specified in spec.tasks", name)
}
state := new(jobState)
p.tasksState[name] = state
p.addJobCommand(job, &us, state, len(us.OutputTablePaths))
}
return p.start(opts)
}
func (mr *client) Track(opID yt.OperationID) (Operation, error) {
return &operation{yc: mr.yc, ctx: mr.ctx, opID: opID}, nil
}
func (mr *client) WithTx(tx yt.Tx) Client {
return &client{
ctx: mr.ctx,
yc: mr.yc,
tx: tx,
config: mr.config,
jobStateKey: mr.jobStateKey,
aead: mr.aead,
}
}