forked from ShiningRush/fastflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastflow.go
264 lines (226 loc) · 6.07 KB
/
fastflow.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
package fastflow
import (
"context"
"errors"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"strings"
"sync"
"syscall"
"time"
"github.com/mingmingshiliyu/fastflow/pkg/actions"
"github.com/mingmingshiliyu/fastflow/pkg/entity"
"github.com/mingmingshiliyu/fastflow/pkg/entity/run"
"github.com/mingmingshiliyu/fastflow/pkg/event"
"github.com/mingmingshiliyu/fastflow/pkg/mod"
"github.com/mingmingshiliyu/fastflow/pkg/utils"
"github.com/mingmingshiliyu/fastflow/pkg/utils/data"
"github.com/shiningrush/goevent"
)
var closers []mod.Closer
// RegisterAction you need register all used action to it
func RegisterAction(acts []run.Action) {
for i := range acts {
mod.ActionMap[acts[i].Name()] = acts[i]
}
}
// GetAction
func GetAction(name string) (run.Action, bool) {
act, ok := mod.ActionMap[name]
return act, ok
}
// InitialOption
type InitialOption struct {
Keeper mod.Keeper
Store mod.Store
// ParserWorkersCnt default 100
ParserWorkersCnt int
// ExecutorWorkerCnt default 1000
ExecutorWorkerCnt int
// ExecutorTimeout default 30s
ExecutorTimeout time.Duration
// ExecutorTimeout default 15s
DagScheduleTimeout time.Duration
// Read dag define from directory
// each file will be pared to a dag, so you CAN'T define all dag in one file
ReadDagFromDir string
}
// Start will block until accept system signal, if you don't want block, plz check "Init"
func Start(opt *InitialOption, afterInit ...func() error) error {
if err := Init(opt); err != nil {
return err
}
for i := range afterInit {
if err := afterInit[i](); err != nil {
return err
}
}
log.Println("fastflow start success")
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
sig := <-c
log.Println(fmt.Sprintf("get sig: %s, ready to close component", sig))
Close()
log.Println("close completed")
return nil
}
// Init will not block, but you need to close fastflow after application closing
func Init(opt *InitialOption) error {
if err := checkOption(opt); err != nil {
return err
}
initCommonComponent(opt)
initLeaderChangedHandler(opt)
RegisterAction([]run.Action{
&actions.Waiting{},
})
if opt.ReadDagFromDir != "" {
return readDagFromDir(opt.ReadDagFromDir)
}
return nil
}
func initLeaderChangedHandler(opt *InitialOption) {
h := &LeaderChangedHandler{
opt: opt,
}
if err := goevent.Subscribe(h); err != nil {
log.Fatalln(err)
}
closers = append(closers, h)
// when application init, leader election should completed, so we need trigger it
if opt.Keeper.IsLeader() {
h.Handle(context.Background(), &event.LeaderChanged{
IsLeader: true,
WorkerKey: opt.Keeper.WorkerKey(),
})
}
return
}
// SetDagInstanceLifecycleHook set hook handler for fastflow
// IMPORTANT: you MUST set hook before you call Init or Start to avoid lost changes.
// (because component will work immediately after you call Init or Start)
func SetDagInstanceLifecycleHook(hook entity.DagInstanceLifecycleHook) {
entity.HookDagInstance = hook
}
// LeaderChangedHandler used to handle leader chaged event
type LeaderChangedHandler struct {
opt *InitialOption
leaderCloser []mod.Closer
mutex sync.Mutex
}
// Topic
func (l *LeaderChangedHandler) Topic() []string {
return []string{event.KeyLeaderChanged}
}
// Handle
func (l *LeaderChangedHandler) Handle(cxt context.Context, e goevent.Event) {
lcEvent := e.(*event.LeaderChanged)
// changed to leader
if lcEvent.IsLeader && len(l.leaderCloser) == 0 {
wg := mod.NewDefWatchDog(l.opt.DagScheduleTimeout)
wg.Init()
l.leaderCloser = append(l.leaderCloser, wg)
dis := mod.NewDefDispatcher()
dis.Init()
l.leaderCloser = append(l.leaderCloser, dis)
log.Println("leader initial")
}
// continue leader failed
if !lcEvent.IsLeader && len(l.leaderCloser) == 0 {
l.Close()
}
}
// Close leader component
func (l *LeaderChangedHandler) Close() {
for i := range l.leaderCloser {
l.leaderCloser[i].Close()
}
l.leaderCloser = []mod.Closer{}
}
// Close all closer
func Close() {
for i := range closers {
closers[i].Close()
}
goevent.Close()
}
func checkOption(opt *InitialOption) error {
if opt.Keeper == nil {
return fmt.Errorf("keeper cannot be nil")
}
if opt.Store == nil {
return fmt.Errorf("store cannot be nil")
}
if opt.ExecutorTimeout == 0 {
opt.ExecutorTimeout = 30 * time.Second
}
if opt.DagScheduleTimeout == 0 {
opt.DagScheduleTimeout = 15 * time.Second
}
if opt.ExecutorWorkerCnt == 0 {
opt.ExecutorWorkerCnt = 1000
}
if opt.ParserWorkersCnt == 0 {
opt.ParserWorkersCnt = 100
}
return nil
}
func initCommonComponent(opt *InitialOption) {
mod.SetKeeper(opt.Keeper)
mod.SetStore(opt.Store)
entity.StoreMarshal = opt.Store.Marshal
entity.StoreUnmarshal = opt.Store.Unmarshal
// Executor must init before parse otherwise will cause a error
exe := mod.NewDefExecutor(opt.ExecutorTimeout, opt.ExecutorWorkerCnt)
mod.SetExecutor(exe)
p := mod.NewDefParser(opt.ParserWorkersCnt, opt.ExecutorTimeout)
mod.SetParser(p)
exe.Init()
closers = append(closers, exe)
p.Init()
closers = append(closers, p)
comm := &mod.DefCommander{}
mod.SetCommander(comm)
// keeper and store must close latest
closers = append(closers, opt.Store)
closers = append(closers, opt.Keeper)
}
func readDagFromDir(dir string) error {
paths, err := utils.DefaultReader.ReadPathsFromDir(dir)
if err != nil {
return err
}
for _, path := range paths {
bs, err := utils.DefaultReader.ReadDag(path)
if err != nil {
return fmt.Errorf("read %s failed: %w", path, err)
}
dag := entity.Dag{
Status: entity.DagStatusNormal,
}
err = yaml.Unmarshal(bs, &dag)
if err != nil {
return fmt.Errorf("unmarshal %s failed: %w", path, err)
}
if dag.ID == "" {
dag.ID = strings.TrimSuffix(strings.TrimSuffix(filepath.Base(path), ".yaml"), ".yml")
}
if err := ensureDagLatest(&dag); err != nil {
return err
}
}
return nil
}
func ensureDagLatest(dag *entity.Dag) error {
oDag, err := mod.GetStore().GetDag(dag.ID)
if err != nil && !errors.Is(err, data.ErrDataNotFound) {
return err
}
if oDag != nil {
return mod.GetStore().UpdateDag(dag)
}
return mod.GetStore().CreateDag(dag)
}