/
worker.go
183 lines (145 loc) · 4.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
// Package client contains the runtime client worker.
package client
import (
"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/grpc"
"github.com/oasisprotocol/oasis-core/go/common/logging"
"github.com/oasisprotocol/oasis-core/go/runtime/client/api"
runtimeRegistry "github.com/oasisprotocol/oasis-core/go/runtime/registry"
"github.com/oasisprotocol/oasis-core/go/worker/client/committee"
workerCommon "github.com/oasisprotocol/oasis-core/go/worker/common"
committeeCommon "github.com/oasisprotocol/oasis-core/go/worker/common/committee"
)
// Worker is a runtime client worker handling many runtimes.
type Worker struct {
enabled bool
commonWorker *workerCommon.Worker
runtimes map[common.Namespace]*committee.Node
quitCh chan struct{}
initCh chan struct{}
logger *logging.Logger
}
// Name returns the service name.
func (w *Worker) Name() string {
return "client worker"
}
// Start starts the service.
func (w *Worker) Start() error {
if !w.enabled {
w.logger.Debug("not starting client worker as it is disabled")
// In case the worker is not enabled, close the init channel immediately.
close(w.initCh)
return nil
}
// Wait for all runtimes to terminate.
go func() {
defer close(w.quitCh)
for _, rt := range w.runtimes {
<-rt.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()
}
}
// 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()
}
}
// Initialized returns a channel that will be closed when the client worker
// is initialized and ready to service requests.
func (w *Worker) Initialized() <-chan struct{} {
return w.initCh
}
func (w *Worker) registerRuntime(commonNode *committeeCommon.Node) error {
id := commonNode.Runtime.ID()
w.logger.Info("registering new runtime",
"runtime_id", id,
)
// Create committee node for the given runtime.
node, err := committee.NewNode(commonNode)
if err != nil {
return err
}
// If we are running in stateless client mode, register remote storage.
if w.commonWorker.RuntimeRegistry.Mode() == runtimeRegistry.RuntimeModeClientStateless {
commonNode.Runtime.RegisterStorage(NewStatelessStorage(commonNode.P2P, id))
}
commonNode.AddHooks(node)
w.runtimes[id] = node
w.logger.Info("new runtime registered",
"runtime_id", id,
)
return nil
}
// New creates a new runtime client worker.
func New(grpcInternal *grpc.Server, commonWorker *workerCommon.Worker) (*Worker, error) {
var enabled bool
switch commonWorker.RuntimeRegistry.Mode() {
case runtimeRegistry.RuntimeModeNone, runtimeRegistry.RuntimeModeKeymanager:
enabled = false
default:
// When configured in one of the runtime modes, enable the client worker.
enabled = true
}
w := &Worker{
enabled: enabled,
commonWorker: commonWorker,
runtimes: make(map[common.Namespace]*committee.Node),
quitCh: make(chan struct{}),
initCh: make(chan struct{}),
logger: logging.GetLogger("worker/client"),
}
if !enabled {
return w, nil
}
// Register all configured runtimes.
for _, rt := range commonWorker.GetRuntimes() {
if err := w.registerRuntime(rt); err != nil {
return nil, err
}
}
// Attach the runtime client worker's internal GRPC interface.
api.RegisterService(grpcInternal.Server(), &service{w: w})
return w, nil
}