-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcontext.go
191 lines (158 loc) · 4.07 KB
/
context.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
package gopark
import (
"encoding/gob"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"runtime"
"syscall"
"time"
)
type KeyValue struct {
Key interface{}
Value interface{}
}
func (kv *KeyValue) String() string {
return fmt.Sprintf("%v:%v", kv.Key, kv.Value)
}
type KeyGroups struct {
Key interface{}
Groups [][]interface{}
}
type KeyLessFunc func(x, y interface{}) bool
type ParkSorter struct {
values []interface{}
fn KeyLessFunc
}
func (s *ParkSorter) Len() int {
return len(s.values)
}
func (s *ParkSorter) Swap(i, j int) {
s.values[i], s.values[j] = s.values[j], s.values[i]
}
func (s *ParkSorter) Less(i, j int) bool {
return s.fn(s.values[i], s.values[j])
}
func NewParkSorter(values []interface{}, fn KeyLessFunc) *ParkSorter {
return &ParkSorter{values, fn}
}
type Yielder chan interface{}
type ReducerFn func(yield Yielder, partition int) interface{}
type Context struct {
jobName string
scheduler Scheduler
initialzed bool
started bool
startTime time.Time
}
func (c *Context) String() string {
return fmt.Sprintf("Context-[%s]", c.jobName)
}
func (c *Context) init() {
if c.initialzed {
return
}
c.scheduler = newLocalScheduler()
c.initialzed = true
log.Printf("Gpark Context [%s] initialzed.", c.jobName)
}
func (c *Context) start() {
if c.started {
return
}
c.init()
env.start()
c.scheduler.start()
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGABRT)
go func() {
s := <-signalChan
parklog("Captured the signal %v\n", s)
c.Stop()
os.Exit(2)
}()
c.started = true
c.startTime = time.Now()
log.Printf("Context [%s] is started.", c.jobName)
}
func (c *Context) Stop() {
if !c.started {
return
}
env.stop()
c.scheduler.stop()
c.started = false
log.Printf("Context [%s] is stopped, duration = %s.", c.jobName, (time.Since(c.startTime)))
}
func (c *Context) runRoutine(rdd RDD, partitions []int, rn ReducerFn) []Yielder {
if partitions == nil {
partitions = make([]int, rdd.len())
for i := 0; i < rdd.len(); i++ {
partitions[i] = i
}
}
if len(partitions) == 0 {
return nil
}
c.start()
return c.scheduler.runRoutine(rdd, partitions, rn)
}
func (c *Context) TextFile(pathname string) RDD {
absPathname, err := filepath.Abs(pathname)
if err != nil {
panic(err)
}
if fStat, err := os.Stat(absPathname); err != nil {
panic(err)
} else {
if !fStat.IsDir() {
return newTextFileRDD(c, absPathname)
}
pathNames := make([]string, 0)
err = filepath.Walk(absPathname, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
pathNames = append(pathNames, path)
}
return nil
})
if err != nil {
panic(err)
}
rdds := make([]RDD, len(pathNames))
for i := range pathNames {
rdds[i] = newTextFileRDD(c, pathNames[i])
}
return c.Union(rdds)
}
}
func (c *Context) Union(rdds []RDD) RDD {
return newUnionRDD(c, rdds)
}
func (c *Context) Data(d []interface{}) RDD {
return newDataRDD(c, d)
}
func (c *Context) Data_N(d []interface{}, numPartitions int) RDD {
return newDataRDD_N(c, d, numPartitions)
}
func (c *Context) Accumulator(initValue int) Accumulator {
return newIntAccumulator(initValue)
}
func (c *Context) AccumulatorWithParam(initValue interface{}, param AccumulatorParam) Accumulator {
return newAccumulator(initValue, param)
}
func NewContext(jobName string) *Context {
return &Context{
jobName: jobName,
initialzed: false,
started: false,
}
}
func init() {
log.SetFlags(log.LstdFlags)
runtime.GOMAXPROCS(runtime.NumCPU())
gob.Register(new(KeyValue))
gob.Register(new(KeyGroups))
}
var _ = fmt.Println