-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.go
183 lines (154 loc) · 4.04 KB
/
stream.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
package streams
/*
Copyright 2018 Bruno Moura <brunotm@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"runtime"
"sync"
"github.com/brunotm/streams/types"
)
// Error generated by the stream components
type Error struct {
Node *Node
Error error
Record []Record
}
// Stream represents an unbounded, continuously updating data set.
// It contains a topology defining the data processing to be done.
// A Stream can have multiple concurrent tasks over the same processor topology.
// Stream Sources, Processors and Sinks must be safe for concurrent use.
type Stream struct {
mtx sync.Mutex
name string
config Config
tasks nodeTasks
topology *topology
handler func(Error)
donech chan struct{}
}
// Start initializes the stores, sources, processors and sinks within the
// topology and starts the stream.
func (s *Stream) Start() (err error) {
s.mtx.Lock()
defer s.mtx.Unlock()
// Initialize tasks initializing stream componentes
s.initTasks()
for _, node := range s.topology.stores {
pc := newContext(s)
if err = node.init(pc); err != nil {
return err
}
}
for _, node := range s.topology.nodes {
if node.typ == types.Source {
continue
}
pc := newContext(s)
if err = node.init(pc); err != nil {
return err
}
}
for _, node := range s.topology.roots {
pc := newContext(s)
if err = node.init(pc); err != nil {
return err
}
// start streaming
node.pc.activate()
go node.processor.(Source).Consume(pc)
}
return nil
}
// Close the stream.
// Closes all stream sources and its tasks in parallel, close all processors
// sequentially if their context is deactivated, close all sink processors and
// finally all stores.
func (s *Stream) Close() (err error) {
// first close all sources
for _, node := range s.topology.roots {
if closer, ok := node.processor.(Closer); ok {
if err = closer.Close(); err != nil {
return err
}
}
// close all source tasks
s.tasks.setScale(node, 0, 0)
}
// Close all processors
for _, node := range s.topology.nodes {
if node.typ != types.Processor {
continue
}
if closer, ok := node.processor.(Closer); ok {
for {
if node.pc.IsActive() {
runtime.Gosched()
continue
}
if err = closer.Close(); err != nil {
return err
}
}
}
// close all processor tasks
s.tasks.setScale(node, 0, 0)
}
// Close all sinks
for _, node := range s.topology.nodes {
if node.typ != types.Sink {
continue
}
if closer, ok := node.processor.(Closer); ok {
for {
if node.pc.IsActive() {
runtime.Gosched()
continue
}
if err = closer.Close(); err != nil {
return err
}
}
}
}
// Close all stores
for _, node := range s.topology.stores {
if closer, ok := node.processor.(Closer); ok {
if err = closer.Close(); err != nil {
return err
}
}
}
return nil
}
// Store returns the store with the given name
func (s *Stream) Store(name string) (store ROStore, err error) {
st, exists := s.topology.stores[name]
if !exists {
return nil, ErrStoreNotFound
}
return st.processor.(ROStore), nil
}
// initTasks for all source and processors that have successors.
// Sink nodes are ignored.
func (s *Stream) initTasks() {
s.tasks = make(nodeTasks)
for _, node := range s.topology.nodes {
if len(node.successors) == 0 || node.typ == types.Sink {
continue
}
t := &tasks{}
s.tasks[node] = t
scale := s.config.Get(s.name, node.name, "tasks", "count").Int(0)
buffer := s.config.Get(s.name, node.name, "tasks", "buffer").Int(0)
s.tasks.setScale(node, scale, buffer)
}
}