/
worker.go
207 lines (167 loc) · 4.75 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
package executor
import (
"context"
"fmt"
"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/logging"
"github.com/oasisprotocol/oasis-core/go/common/node"
workerCommon "github.com/oasisprotocol/oasis-core/go/worker/common"
committeeCommon "github.com/oasisprotocol/oasis-core/go/worker/common/committee"
"github.com/oasisprotocol/oasis-core/go/worker/compute/executor/committee"
"github.com/oasisprotocol/oasis-core/go/worker/registration"
)
// Worker is an executor worker handling many runtimes.
type Worker struct {
enabled bool
scheduleCheckTxEnabled bool
scheduleMaxQueueSize uint64
scheduleTxCacheSize uint64
commonWorker *workerCommon.Worker
registration *registration.Worker
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 "executor worker"
}
// Start starts the service.
func (w *Worker) Start() error {
if !w.enabled {
w.logger.Info("not starting executor 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 and all proxies to terminate.
go func() {
defer close(w.quitCh)
defer (w.cancelCtx)()
for _, rt := range w.runtimes {
<-rt.Quit()
}
}()
// Wait for all runtimes to be initialized and for the node
// to be registered for the current epoch.
go func() {
for _, rt := range w.runtimes {
<-rt.Initialized()
}
<-w.registration.InitialRegistrationCh()
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 executor worker
// is initialized and ready to service requests.
func (w *Worker) Initialized() <-chan struct{} {
return w.initCh
}
// GetRuntime returns a registered runtime.
//
// In case the runtime with the specified id was not registered it
// returns nil.
func (w *Worker) GetRuntime(id common.Namespace) *committee.Node {
return w.runtimes[id]
}
func (w *Worker) registerRuntime(commonNode *committeeCommon.Node) error {
id := commonNode.Runtime.ID()
w.logger.Info("registering new runtime",
"runtime_id", id,
)
rp, err := w.registration.NewRuntimeRoleProvider(node.RoleComputeWorker, id)
if err != nil {
return fmt.Errorf("failed to create role provider: %w", err)
}
// Create committee node for the given runtime.
node, err := committee.NewNode(commonNode, w.commonWorker.GetConfig(), rp, w.scheduleCheckTxEnabled, w.scheduleMaxQueueSize, w.scheduleTxCacheSize)
if err != nil {
return err
}
commonNode.AddHooks(node)
w.runtimes[id] = node
w.logger.Info("new runtime registered",
"runtime_id", id,
)
return nil
}
func newWorker(
dataDir string,
enabled bool,
commonWorker *workerCommon.Worker,
registration *registration.Worker,
scheduleCheckTxEnabled bool,
scheduleMaxQueueSize uint64,
scheduleTxCacheSize uint64,
) (*Worker, error) {
ctx, cancelCtx := context.WithCancel(context.Background())
w := &Worker{
enabled: enabled,
commonWorker: commonWorker,
scheduleCheckTxEnabled: scheduleCheckTxEnabled,
scheduleMaxQueueSize: scheduleMaxQueueSize,
scheduleTxCacheSize: scheduleTxCacheSize,
registration: registration,
runtimes: make(map[common.Namespace]*committee.Node),
ctx: ctx,
cancelCtx: cancelCtx,
quitCh: make(chan struct{}),
initCh: make(chan struct{}),
logger: logging.GetLogger("worker/executor"),
}
if enabled {
if !w.commonWorker.Enabled() {
panic("common worker should have been enabled for executor worker")
}
// Register all configured runtimes.
for _, rt := range commonWorker.GetRuntimes() {
if err := w.registerRuntime(rt); err != nil {
return nil, err
}
}
}
return w, nil
}