-
Notifications
You must be signed in to change notification settings - Fork 107
/
worker.go
305 lines (260 loc) · 7.1 KB
/
worker.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
package common
import (
"context"
"fmt"
"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/grpc"
policyAPI "github.com/oasisprotocol/oasis-core/go/common/grpc/policy/api"
"github.com/oasisprotocol/oasis-core/go/common/identity"
"github.com/oasisprotocol/oasis-core/go/common/logging"
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
genesis "github.com/oasisprotocol/oasis-core/go/genesis/api"
ias "github.com/oasisprotocol/oasis-core/go/ias/api"
keymanagerApi "github.com/oasisprotocol/oasis-core/go/keymanager/api"
runtimeRegistry "github.com/oasisprotocol/oasis-core/go/runtime/registry"
"github.com/oasisprotocol/oasis-core/go/sentry/policywatcher"
"github.com/oasisprotocol/oasis-core/go/worker/common/committee"
"github.com/oasisprotocol/oasis-core/go/worker/common/p2p"
)
// Worker is a garbage bag with lower level services and common runtime objects.
type Worker struct {
enabled bool
cfg Config
Identity *identity.Identity
Consensus consensus.Backend
Grpc *grpc.Server
GrpcPolicyWatcher policyAPI.PolicyWatcher
P2P *p2p.P2P
IAS ias.Endpoint
KeyManager keymanagerApi.Backend
RuntimeRegistry runtimeRegistry.Registry
GenesisDoc *genesis.Document
runtimes map[common.Namespace]*committee.Node
ctx context.Context
cancelCtx context.CancelFunc
quitCh chan struct{}
initCh chan struct{}
logger *logging.Logger
}
// Name returns the service name.
func (w *Worker) Name() string {
return "common worker"
}
// Start starts the service.
func (w *Worker) Start() error {
if !w.enabled {
w.logger.Info("not starting common worker as it is disabled")
// In case the worker is not enabled, close the init channel immediately.
close(w.initCh)
return nil
}
// Wait for the gRPC server and all runtimes to terminate.
go func() {
defer close(w.quitCh)
for _, rt := range w.runtimes {
<-rt.Quit()
}
<-w.Grpc.Quit()
}()
// Wait for all runtimes to be initialized.
go func() {
for _, rt := range w.runtimes {
<-rt.Initialized()
}
close(w.initCh)
}()
// Start runtime services.
for id, rt := range w.runtimes {
w.logger.Info("starting services for runtime",
"runtime_id", id,
)
if err := rt.Start(); err != nil {
return err
}
}
return nil
}
// Stop halts the service.
func (w *Worker) Stop() {
if !w.enabled {
close(w.quitCh)
return
}
for id, rt := range w.runtimes {
w.logger.Info("stopping services for runtime",
"runtime_id", id,
)
rt.Stop()
}
w.Grpc.Stop()
w.cancelCtx()
}
// Enabled returns if worker is enabled.
func (w *Worker) Enabled() bool {
return w.enabled
}
// Quit returns a channel that will be closed when the service terminates.
func (w *Worker) Quit() <-chan struct{} {
return w.quitCh
}
// Cleanup performs the service specific post-termination cleanup.
func (w *Worker) Cleanup() {
if !w.enabled {
return
}
for _, rt := range w.runtimes {
rt.Cleanup()
}
w.Grpc.Cleanup()
}
// Initialized returns a channel that will be closed when the transaction scheduler is
// initialized and ready to service requests.
func (w *Worker) Initialized() <-chan struct{} {
return w.initCh
}
// GetConfig returns the worker's configuration.
func (w *Worker) GetConfig() Config {
return w.cfg
}
// GetRuntimes returns a map of configured runtimes.
func (w *Worker) GetRuntimes() map[common.Namespace]*committee.Node {
return w.runtimes
}
// GetRuntime returns a common committee node for the given runtime (if available).
//
// In case the runtime with the specified id was not configured for this node it returns nil.
func (w *Worker) GetRuntime(id common.Namespace) *committee.Node {
return w.runtimes[id]
}
// NewUnmanagedCommitteeNode creates a new common committee node that is not
// managed by this worker.
//
// Since the node is unmanaged the caller needs to ensure that the node will
// be properly terminated once started.
//
// Note that this does not instruct the storage backend to watch the given
// runtime.
func (w *Worker) NewUnmanagedCommitteeNode(runtime runtimeRegistry.Runtime, enableP2P bool) (*committee.Node, error) {
var p2p *p2p.P2P
if enableP2P {
// Make sure that there is no other (managed) runtime already registered
// with the same identifier as registering another will overwrite the
// P2P handler.
if w.runtimes[runtime.ID()] != nil {
return nil, fmt.Errorf("worker/common: managed runtime with id %s already exists", runtime.ID())
}
p2p = w.P2P
}
return committee.NewNode(
runtime,
w.Identity,
w.KeyManager,
w.Consensus,
p2p,
)
}
func (w *Worker) registerRuntime(runtime runtimeRegistry.Runtime) error {
id := runtime.ID()
w.logger.Info("registering new runtime",
"runtime_id", id,
)
node, err := w.NewUnmanagedCommitteeNode(runtime, true)
if err != nil {
return err
}
w.runtimes[id] = node
w.logger.Info("new runtime registered",
"runtime_id", id,
)
return nil
}
func newWorker(
ctx context.Context,
cancelCtx context.CancelFunc,
dataDir string,
enabled bool,
identity *identity.Identity,
consensus consensus.Backend,
grpc *grpc.Server,
grpcPolicyWatcher policyAPI.PolicyWatcher,
p2p *p2p.P2P,
ias ias.Endpoint,
keyManager keymanagerApi.Backend,
runtimeRegistry runtimeRegistry.Registry,
cfg Config,
genesisDoc *genesis.Document,
) (*Worker, error) {
w := &Worker{
enabled: enabled,
cfg: cfg,
Identity: identity,
Consensus: consensus,
Grpc: grpc,
GrpcPolicyWatcher: grpcPolicyWatcher,
P2P: p2p,
IAS: ias,
KeyManager: keyManager,
RuntimeRegistry: runtimeRegistry,
GenesisDoc: genesisDoc,
runtimes: make(map[common.Namespace]*committee.Node),
ctx: ctx,
cancelCtx: cancelCtx,
quitCh: make(chan struct{}),
initCh: make(chan struct{}),
logger: logging.GetLogger("worker/common"),
}
if enabled {
for _, rt := range runtimeRegistry.Runtimes() {
// Register all configured runtimes.
if err := w.registerRuntime(rt); err != nil {
return nil, err
}
}
}
return w, nil
}
// New creates a new worker.
func New(
dataDir string,
enabled bool,
identity *identity.Identity,
consensus consensus.Backend,
p2p *p2p.P2P,
ias ias.Endpoint,
keyManager keymanagerApi.Backend,
runtimeRegistry runtimeRegistry.Registry,
genesisDoc *genesis.Document,
) (*Worker, error) {
cfg, err := NewConfig(ias)
if err != nil {
return nil, fmt.Errorf("worker/common: failed to initialize config: %w", err)
}
// Create externally-accessible gRPC server.
serverConfig := &grpc.ServerConfig{
Name: "external",
Port: cfg.ClientPort,
Identity: identity,
}
grpc, err := grpc.NewServer(serverConfig)
if err != nil {
return nil, err
}
ctx, cancelCtx := context.WithCancel(context.Background())
grpcPolicyWatcher := policywatcher.New(ctx, cfg.SentryAddresses, identity)
return newWorker(
ctx,
cancelCtx,
dataDir,
enabled,
identity,
consensus,
grpc,
grpcPolicyWatcher,
p2p,
ias,
keyManager,
runtimeRegistry,
*cfg,
genesisDoc,
)
}