-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopology.go
274 lines (233 loc) · 6.09 KB
/
topology.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
package gostream
import (
"log"
"math/rand"
"os"
"os/signal"
"sync"
"syscall"
)
const (
GROUPING_SHUFFLE = 1
GROUPING_KEY = 2
)
type Spout struct {
cc *ComponentCommon
ispout ISpout
}
type Bolt struct {
cc *ComponentCommon
ibolt IBolt
}
type TaskInfo struct {
cc *ComponentCommon
componentId string
index int //本component内的索引
dependentCnt int //依赖messages的上游发送者的数目
messages chan Message
}
type StreamInfo struct {
groupingType int
tasks []*TaskInfo //下游Task
}
type ComponentCommon struct {
id string
tb *TopologyBuilder
parallelism int
tasks []*TaskInfo
//inputs map[string]string //输入map[componentId]streamId
streams map[string]*StreamInfo //输出
}
type TopologyBuilder struct {
statInterval int64 //统计reset周期,单位秒
mu sync.RWMutex
spouts map[string]*Spout
bolts map[string]*Bolt
commons map[string]*ComponentCommon
}
type IOutputCollector interface {
Emit(message Message)
EmitTo(message Message, streamid string)
}
type TopologyContext interface {
GetThisComponentId() string
}
func (t *TaskInfo) GetThisComponentId() string {
return t.componentId
}
func (t *TaskInfo) Emit(message Message) {
var messages chan Message
cc := t.cc
//todo此处可并发
for _, streamInfo := range cc.streams {
l := len(streamInfo.tasks)
if l > 1 {
switch streamInfo.groupingType {
case GROUPING_SHUFFLE:
messages = streamInfo.tasks[rand.Intn(l)].messages
case GROUPING_KEY:
hashid := message.GetHashKey(cc.parallelism, t.index, l)
messages = streamInfo.tasks[hashid].messages
default:
log.Fatalf("unknown groupingType:%d\n", streamInfo.groupingType)
return
}
} else {
messages = streamInfo.tasks[0].messages
}
messages <- message
}
}
func (t *TaskInfo) EmitTo(message Message, streamid string) {
var messages chan Message
cc := t.cc
if streamInfo, ok := t.cc.streams[streamid]; ok {
l := len(streamInfo.tasks)
if l > 1 {
switch streamInfo.groupingType {
case GROUPING_SHUFFLE:
messages = streamInfo.tasks[rand.Intn(l)].messages
case GROUPING_KEY:
hashid := message.GetHashKey(cc.parallelism, t.index, l)
messages = streamInfo.tasks[hashid].messages
default:
log.Fatalf("unknown groupingType:%d\n", streamInfo.groupingType)
return
}
} else {
messages = streamInfo.tasks[0].messages
}
messages <- message
}
}
//关闭下游
func (t *ComponentCommon) closeDownstream() {
for _, streamInfo := range t.streams {
for _, taskInfo := range streamInfo.tasks {
t.tb.mu.Lock()
taskInfo.dependentCnt--
if taskInfo.dependentCnt == 0 {
//log.Printf("close channel,componentId:%s,taskid:%d\n", taskInfo.componentId, taskInfo.taskid)
close(taskInfo.messages)
}
t.tb.mu.Unlock()
}
}
}
func (t *Bolt) grouping(componentId string, streamId string, groupingType int) {
if t.cc.tb.commons[componentId].streams == nil {
t.cc.tb.commons[componentId].streams = make(map[string]*StreamInfo)
}
streamInfo, ok := t.cc.tb.commons[componentId].streams[streamId]
if !ok {
streamInfo = &StreamInfo{}
streamInfo.groupingType = groupingType
streamInfo.tasks = make([]*TaskInfo, 0, t.cc.parallelism)
t.cc.tb.commons[componentId].streams[streamId] = streamInfo
}
for i := 0; i < t.cc.parallelism; i++ {
t.cc.tasks[i].dependentCnt += t.cc.tb.commons[componentId].parallelism
streamInfo.tasks = append(streamInfo.tasks, t.cc.tasks[i])
}
}
func (t *Bolt) ShuffleGrouping(componentId string, streamId string) {
t.grouping(componentId, streamId, GROUPING_SHUFFLE)
}
func (t *Bolt) KeyGrouping(componentId string, streamId string) {
t.grouping(componentId, streamId, GROUPING_KEY)
}
func NewTopologyBuilder() *TopologyBuilder {
tb := &TopologyBuilder{}
tb.commons = make(map[string]*ComponentCommon)
return tb
}
func (t *TopologyBuilder) SetSpout(id string, ispout ISpout, parallelism int) *Spout {
if _, ok := t.commons[id]; ok {
panic("SetSpout,id exist")
}
if t.spouts == nil {
t.spouts = make(map[string]*Spout)
}
//先不考虑重复的问题
cc := &ComponentCommon{}
cc.id = id
cc.parallelism = parallelism
cc.tb = t
cc.tasks = make([]*TaskInfo, 0, parallelism)
for i := 0; i < parallelism; i++ {
task := &TaskInfo{}
task.componentId = id
task.index = i
task.cc = cc
cc.tasks = append(cc.tasks, task)
}
t.commons[id] = cc
spout := &Spout{}
spout.cc = cc
spout.ispout = ispout
t.spouts[id] = spout
return spout
}
func (t *TopologyBuilder) SetBolt(id string, ibolt IBolt, parallelism int, bufSize int) *Bolt {
if t.bolts == nil {
t.bolts = make(map[string]*Bolt)
}
cc := &ComponentCommon{}
cc.id = id
cc.parallelism = parallelism
cc.tb = t
cc.tasks = make([]*TaskInfo, 0, parallelism)
for i := 0; i < parallelism; i++ {
task := &TaskInfo{}
task.componentId = id
task.index = i
task.cc = cc
task.messages = make(chan Message, bufSize) //缓冲设置
cc.tasks = append(cc.tasks, task)
}
t.commons[id] = cc
bolt := &Bolt{}
bolt.cc = cc
bolt.ibolt = ibolt
t.bolts[id] = bolt
return bolt
}
//设置数据统计周期,即多少秒输出一次统计数据(0表示不周期输出)
func (t *TopologyBuilder) SetStatistics(statInterval int64) {
t.statInterval = statInterval
}
func goSignalListen(stop chan bool) {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT)
select {
case <-c:
close(stop)
log.Println("SignalListen closed stop chan")
}
}
func (t *TopologyBuilder) startSpouts(wg *sync.WaitGroup, stop chan bool) {
for id := range t.spouts {
for i := 0; i < t.commons[id].parallelism; i++ {
wg.Add(1)
go t.goSpout(wg, stop, id, i)
}
}
}
func (t *TopologyBuilder) startBolts(wg *sync.WaitGroup) {
for id := range t.bolts {
for i := 0; i < t.commons[id].parallelism; i++ {
wg.Add(1)
go t.goBolt(wg, id, i)
}
}
}
func (t *TopologyBuilder) Run() {
var wg sync.WaitGroup
stop := make(chan bool)
t.startSpouts(&wg, stop)
t.startBolts(&wg)
//监听退出信号
go goSignalListen(stop)
wg.Wait()
log.Println("all finished")
}