-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlauncher.go
321 lines (259 loc) · 7.78 KB
/
launcher.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2019 dfuse Platform Inc.
//
// 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.
package launcher
import (
"fmt"
"runtime/debug"
"sync"
"time"
"github.com/streamingfast/firehose-core/firehose/info"
"github.com/streamingfast/shutter"
"go.uber.org/atomic"
"go.uber.org/zap"
)
type Launcher struct {
shutter *shutter.Shutter
runtime *Runtime
apps map[string]App
appStatus map[string]AppStatus
appStatusSubscriptions []*subscription
appStatusLock sync.RWMutex
shutdownDoOnce sync.Once
firstShutdownAppID string
hasBeenSignaled *atomic.Bool
logger *zap.Logger
}
func NewLauncher(logger *zap.Logger, absDataDir string, infoServer *info.InfoServer) *Launcher {
l := &Launcher{
shutter: shutter.New(),
apps: make(map[string]App),
appStatus: make(map[string]AppStatus),
hasBeenSignaled: new(atomic.Bool),
logger: logger,
}
l.runtime = &Runtime{
AbsDataDir: absDataDir,
InfoServer: infoServer,
IsPendingShutdown: func() bool {
return l.hasBeenSignaled.Load()
},
}
return l
}
func (l *Launcher) SwitchHasBeenSignaledAtomic(other *atomic.Bool) {
l.hasBeenSignaled = other
}
func (l *Launcher) Close() {
l.shutter.Shutdown(nil)
}
func (l *Launcher) Launch(appNames []string) error {
if len(appNames) == 0 {
return fmt.Errorf("no apps specified")
}
// This is done first as a sanity check so we don't launch anything if something is misconfigured
for _, appID := range appNames {
appDef, found := AppRegistry[appID]
if !found {
return fmt.Errorf("cannot launch un-registered application %q", appID)
}
if appDef.InitFunc != nil {
l.logger.Debug("initialize application", zap.String("app", appID))
err := appDef.InitFunc(l.runtime)
if err != nil {
return fmt.Errorf("unable to initialize app %q: %w", appID, err)
}
}
}
for _, appID := range appNames {
appDef := AppRegistry[appID]
l.StoreAndStreamAppStatus(appID, AppStatusCreated)
l.logger.Debug("creating application", zap.String("app", appID))
// We wrap FactoryFunc inside a func to be able to recover from panic
app, err := func() (app App, err error) {
defer func() {
// If err is not nil, it means FactoryFunc finished, so handle possible recover only if err == nil
if err == nil {
err = recoverToError(recover())
}
}()
return appDef.FactoryFunc(l.runtime)
}()
if err != nil {
return fmt.Errorf("unable to create app %q: %w", appID, err)
}
l.shutter.OnTerminating(func(err error) {
go app.Shutdown(err)
})
l.apps[appDef.ID] = app
}
for appID, app := range l.apps {
if l.shutter.IsTerminating() {
break
}
// run
go func(appID string, app App) {
defer (func() {
// Don't be fooled, this will work only for this very goroutine and its
// execution. If the app launches other goroutines and one of those fails, this
// recovery will not be able to recover them and the whole program will panic
// without ever reaching this point here.
l.shutdownIfRecoveringFromPanic(appID, recover())
})()
l.logger.Debug("launching app", zap.String("app", appID))
err := app.Run()
if err != nil {
l.shutdownDueToApp(appID, err)
}
}(appID, app)
}
for appID, app := range l.apps {
if l.shutter.IsTerminating() {
break
}
// watch for shutdown
go func(appID string, app App) {
select {
case <-app.Terminating():
l.shutdownDueToApp(appID, app.Err())
case <-l.shutter.Terminating():
}
}(appID, app)
}
return nil
}
func (l *Launcher) Terminating() <-chan string {
ch := make(chan string, 1)
go func() {
<-l.shutter.Terminating()
ch <- l.firstShutdownAppID
}()
return ch
}
func (l *Launcher) Err() error {
return l.shutter.Err()
}
// shutdownDueToApp initiates a launcher shutdown process recording the app that initially triggered
// the shutdown and calling the launcher `Shutdown` method with the error. The `err` can be `nil`
// in which case we assume a clean shutdown. Otherwise, we assume a fatal error shutdown and log
// the fatal error.
func (l *Launcher) shutdownDueToApp(appID string, err error) {
l.shutdownDoOnce.Do(func() { // pretty printing of error causing dfuse shutdown
l.firstShutdownAppID = appID
if err != nil {
l.FatalAppError(appID, err)
} else {
l.logger.Info(fmt.Sprintf("app %s triggered clean shutdown", appID))
}
})
l.StoreAndStreamAppStatus(appID, AppStatusStopped)
l.shutter.Shutdown(err)
}
func (l *Launcher) FatalAppError(app string, err error) {
msg := fmt.Sprintf("\n################################################################\n"+
"Fatal error in app %s:\n\n%s"+
"\n################################################################\n", app, err)
l.logger.Error(msg)
}
// shutdownIfRecoveringFromPanic is called with the result of `recover()` call in a `defer`
// to handle any panic and shutdowns down the whole launcher if any recovered error was encountered.
func (l *Launcher) shutdownIfRecoveringFromPanic(appID string, recovered interface{}) (shuttindDown bool) {
if recovered == nil {
return false
}
err := fmt.Errorf("app %q panicked", appID)
switch v := recovered.(type) {
case error:
err = fmt.Errorf("%s: %w\n%s", err.Error(), v, string(debug.Stack()))
default:
err = fmt.Errorf("%s: %s\n%s", err.Error(), v, string(debug.Stack()))
}
l.shutdownDueToApp(appID, err)
return true
}
func recoverToError(recovered interface{}) (err error) {
if recovered == nil {
return nil
}
if v, ok := recovered.(error); ok {
return v
}
return fmt.Errorf("%s", recovered)
}
func (l *Launcher) StoreAndStreamAppStatus(appID string, status AppStatus) {
l.appStatusLock.Lock()
defer l.appStatusLock.Unlock()
l.appStatus[appID] = status
appInfo := &AppInfo{
ID: appID,
Status: status,
}
for _, sub := range l.appStatusSubscriptions {
sub.Push(appInfo)
}
}
func (l *Launcher) GetAppStatus(appID string) AppStatus {
l.appStatusLock.RLock()
defer l.appStatusLock.RUnlock()
if v, found := l.appStatus[appID]; found {
return v
}
return AppStatusNotFound
}
func (l *Launcher) GetAppIDs() (resp []string) {
for appID := range l.apps {
resp = append(resp, string(appID))
}
return resp
}
func (l *Launcher) WaitForTermination() {
l.logger.Info("waiting for all apps termination...")
now := time.Now()
for appID, app := range l.apps {
innerFor:
for {
select {
case <-app.Terminated():
l.logger.Debug("app terminated", zap.String("app_id", appID))
break innerFor
case <-time.After(1500 * time.Millisecond):
l.logger.Info(fmt.Sprintf("still waiting for app %q ... %v", appID, time.Since(now).Round(100*time.Millisecond)))
}
}
}
l.logger.Info("all apps terminated gracefully")
}
func (l *Launcher) SubscribeAppStatus() *subscription {
chanSize := 500
sub := newSubscription(l.logger, chanSize)
l.appStatusLock.Lock()
defer l.appStatusLock.Unlock()
l.appStatusSubscriptions = append(l.appStatusSubscriptions, sub)
l.logger.Debug("app status subscribed")
return sub
}
func (l *Launcher) UnsubscribeAppStatus(sub *subscription) {
if sub == nil {
return
}
l.appStatusLock.Lock()
defer l.appStatusLock.Unlock()
var filtered []*subscription
for _, candidate := range l.appStatusSubscriptions {
// Pointer address comparison
if candidate != sub {
filtered = append(filtered, candidate)
}
}
l.appStatusSubscriptions = filtered
}