/
instance.go
396 lines (334 loc) · 11.5 KB
/
instance.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//
// (C) Copyright 2019-2024 Intel Corporation.
//
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
package server
import (
"context"
"fmt"
"os"
"sync"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
mgmtpb "github.com/daos-stack/daos/src/control/common/proto/mgmt"
srvpb "github.com/daos-stack/daos/src/control/common/proto/srv"
"github.com/daos-stack/daos/src/control/drpc"
"github.com/daos-stack/daos/src/control/lib/atm"
"github.com/daos-stack/daos/src/control/lib/control"
"github.com/daos-stack/daos/src/control/lib/ranklist"
"github.com/daos-stack/daos/src/control/logging"
"github.com/daos-stack/daos/src/control/server/storage"
"github.com/daos-stack/daos/src/control/system"
)
type (
systemJoinFn func(context.Context, *control.SystemJoinReq) (*control.SystemJoinResp, error)
onAwaitFormatFn func(context.Context, uint32, string) error
onStorageReadyFn func(context.Context) error
onReadyFn func(context.Context) error
onInstanceExitFn func(context.Context, uint32, ranklist.Rank, error, int) error
)
// EngineInstance encapsulates control-plane specific configuration
// and functionality for managed I/O Engine instances. The distinction
// between this structure and what's in the engine package is that the
// engine package is only concerned with configuring and executing
// a single daos_engine instance. EngineInstance is intended to
// be used with EngineHarness to manage and monitor multiple instances
// per node.
type EngineInstance struct {
log logging.Logger
runner EngineRunner
storage *storage.Provider
waitFormat atm.Bool
storageReady chan bool
waitDrpc atm.Bool
drpcReady chan *srvpb.NotifyReadyReq
ready atm.Bool
startRequested chan bool
fsRoot string
hostFaultDomain *system.FaultDomain
joinSystem systemJoinFn
onAwaitFormat []onAwaitFormatFn
onStorageReady []onStorageReadyFn
onReady []onReadyFn
onInstanceExit []onInstanceExitFn
sync.RWMutex
// these must be protected by a mutex in order to
// avoid racy access.
_cancelCtx context.CancelFunc
_drpcClient drpc.DomainSocketClient
_superblock *Superblock
_lastErr error // populated when harness receives signal
}
// NewEngineInstance returns an *EngineInstance initialized with
// its dependencies.
func NewEngineInstance(l logging.Logger, p *storage.Provider, jf systemJoinFn, r EngineRunner) *EngineInstance {
return &EngineInstance{
log: l,
runner: r,
storage: p,
joinSystem: jf,
drpcReady: make(chan *srvpb.NotifyReadyReq),
storageReady: make(chan bool),
startRequested: make(chan bool),
}
}
// WithHostFaultDomain adds a fault domain for the host this instance is running
// on.
func (ei *EngineInstance) WithHostFaultDomain(fd *system.FaultDomain) *EngineInstance {
ei.hostFaultDomain = fd
return ei
}
// isAwaitingFormat indicates whether EngineInstance is waiting
// for an administrator action to trigger a format.
func (ei *EngineInstance) isAwaitingFormat() bool {
return ei.waitFormat.Load()
}
// IsStarted indicates whether EngineInstance is in a running state.
func (ei *EngineInstance) IsStarted() bool {
return ei.runner.IsRunning()
}
// IsReady indicates whether the EngineInstance is in a ready state.
//
// If true indicates that the instance is fully setup, distinct from
// drpc and storage ready states, and currently active.
func (ei *EngineInstance) IsReady() bool {
return ei.ready.Load() && ei.IsStarted()
}
// OnAwaitFormat adds a list of callbacks to invoke when the instance
// requires formatting.
func (ei *EngineInstance) OnAwaitFormat(fns ...onAwaitFormatFn) {
ei.onAwaitFormat = append(ei.onAwaitFormat, fns...)
}
// OnStorageReady adds a list of callbacks to invoke when the instance
// storage becomes ready.
func (ei *EngineInstance) OnStorageReady(fns ...onStorageReadyFn) {
ei.onStorageReady = append(ei.onStorageReady, fns...)
}
// OnReady adds a list of callbacks to invoke when the instance
// becomes ready.
func (ei *EngineInstance) OnReady(fns ...onReadyFn) {
ei.onReady = append(ei.onReady, fns...)
}
// OnInstanceExit adds a list of callbacks to invoke when the instance
// runner (process) terminates.
func (ei *EngineInstance) OnInstanceExit(fns ...onInstanceExitFn) {
ei.onInstanceExit = append(ei.onInstanceExit, fns...)
}
// LocalState returns local perspective of the current instance state
// (doesn't consider state info held by the global system membership).
func (ei *EngineInstance) LocalState() system.MemberState {
switch {
case ei.IsReady():
return system.MemberStateReady
case ei.IsStarted():
return system.MemberStateStarting
case ei.isAwaitingFormat():
return system.MemberStateAwaitFormat
default:
return system.MemberStateStopped
}
}
// setIndex sets the server index assigned by the harness.
func (ei *EngineInstance) setIndex(idx uint32) {
ei.runner.GetConfig().Index = idx
}
// Index returns the server index assigned by the harness.
func (ei *EngineInstance) Index() uint32 {
return ei.runner.GetConfig().Index
}
// SetCheckerMode adjusts the engine configuration to enable or disable
// starting the engine in checker mode.
func (ei *EngineInstance) SetCheckerMode(enabled bool) {
ei.runner.GetConfig().CheckerEnabled = enabled
}
// removeSocket removes the socket file used for dRPC communication with
// harness and updates relevant ready states.
func (ei *EngineInstance) removeSocket() error {
fMsg := fmt.Sprintf("removing instance %d socket file", ei.Index())
dc, err := ei.getDrpcClient()
if err != nil {
return errors.Wrap(err, fMsg)
}
engineSock := dc.GetSocketPath()
if err := checkDrpcClientSocketPath(engineSock); err != nil {
return errors.Wrap(err, fMsg)
}
os.Remove(engineSock)
ei.ready.SetFalse()
return nil
}
func (ei *EngineInstance) determineRank(ctx context.Context, ready *srvpb.NotifyReadyReq) (ranklist.Rank, bool, uint32, error) {
superblock := ei.getSuperblock()
if superblock == nil {
return ranklist.NilRank, false, 0, errors.New("nil superblock while determining rank")
}
r := ranklist.NilRank
if superblock.Rank != nil {
r = *superblock.Rank
}
joinReq := &control.SystemJoinReq{
UUID: superblock.UUID,
Rank: r,
URI: ready.GetUri(),
SecondaryURIs: ready.GetSecondaryUris(),
NumContexts: ready.GetNctxs(),
NumSecondaryContexts: ready.GetSecondaryNctxs(),
FaultDomain: ei.hostFaultDomain,
InstanceIdx: ei.Index(),
Incarnation: ready.GetIncarnation(),
CheckMode: ready.GetCheckMode(),
}
resp, err := ei.joinSystem(ctx, joinReq)
if err != nil {
ei.log.Errorf("join failed: %s", err)
return ranklist.NilRank, false, 0, err
}
switch resp.State {
case system.MemberStateAdminExcluded, system.MemberStateExcluded:
return ranklist.NilRank, resp.LocalJoin, 0, errors.Errorf("rank %d excluded", resp.Rank)
case system.MemberStateCheckerStarted:
// If the system is in checker mode but the rank was not started in
// checker mode, we need to restart it in order to get the correct
// modules loaded.
if !ready.GetCheckMode() {
ei.log.Noticef("restarting rank %d in checker mode", resp.Rank)
go ei.requestStart(context.Background())
ei.SetCheckerMode(true)
return ranklist.NilRank, resp.LocalJoin, 0, errors.Errorf("rank %d restarting to enable checker", resp.Rank)
}
}
r = ranklist.Rank(resp.Rank)
if !superblock.ValidRank || ready.Uri != superblock.URI {
ei.log.Noticef("updating rank %d URI to %s", resp.Rank, ready.Uri)
superblock.Rank = new(ranklist.Rank)
*superblock.Rank = r
superblock.ValidRank = true
superblock.URI = ready.Uri
ei.setSuperblock(superblock)
if err := ei.WriteSuperblock(); err != nil {
return ranklist.NilRank, resp.LocalJoin, 0, err
}
}
return r, resp.LocalJoin, resp.MapVersion, nil
}
func (ei *EngineInstance) updateFaultDomainInSuperblock() error {
if ei.hostFaultDomain == nil {
return errors.New("engine instance has a nil fault domain")
}
superblock := ei.getSuperblock()
if superblock == nil {
return errors.New("nil superblock while updating fault domain")
}
newDomainStr := ei.hostFaultDomain.String()
if newDomainStr == superblock.HostFaultDomain {
// No change
return nil
}
ei.log.Infof("instance %d setting host fault domain to %q (previously %q)",
ei.Index(), ei.hostFaultDomain, superblock.HostFaultDomain)
superblock.HostFaultDomain = newDomainStr
ei.setSuperblock(superblock)
if err := ei.WriteSuperblock(); err != nil {
return err
}
return nil
}
// handleReady determines the instance rank and sends a SetRank dRPC request
// to the Engine.
func (ei *EngineInstance) handleReady(ctx context.Context, ready *srvpb.NotifyReadyReq) error {
if err := ei.updateFaultDomainInSuperblock(); err != nil {
ei.log.Error(err.Error()) // nonfatal
}
r, localJoin, mapVersion, err := ei.determineRank(ctx, ready)
if err != nil {
return err
}
// If the join was already processed because it ran on the same server,
// skip the rest of these steps.
if localJoin {
return nil
}
return ei.SetupRank(ctx, r, mapVersion)
}
func (ei *EngineInstance) SetupRank(ctx context.Context, rank ranklist.Rank, map_version uint32) error {
if ei.IsReady() {
ei.log.Debugf("SetupRank called on an already set-up instance %d", ei.Index())
return nil
}
if err := ei.callSetRank(ctx, rank, map_version); err != nil {
return errors.Wrap(err, "SetRank failed")
}
if err := ei.callSetUp(ctx); err != nil {
return errors.Wrap(err, "SetUp failed")
}
ei.ready.SetTrue()
return nil
}
func (ei *EngineInstance) callSetRank(ctx context.Context, rank ranklist.Rank, map_version uint32) error {
dresp, err := ei.callDrpc(ctx, drpc.MethodSetRank, &mgmtpb.SetRankReq{Rank: rank.Uint32(), MapVersion: map_version})
if err != nil {
return err
}
resp := &mgmtpb.DaosResp{}
if err = proto.Unmarshal(dresp.Body, resp); err != nil {
return errors.Wrap(err, "unmarshal SetRank response")
}
if resp.Status != 0 {
return errors.Errorf("SetRank: %d\n", resp.Status)
}
return nil
}
// GetRank returns a valid instance rank or error.
func (ei *EngineInstance) GetRank() (ranklist.Rank, error) {
var err error
sb := ei.getSuperblock()
switch {
case sb == nil:
err = errors.New("nil superblock")
case sb.Rank == nil:
err = errors.New("nil rank in superblock")
}
if err != nil {
return ranklist.NilRank, err
}
return *sb.Rank, nil
}
// setMemSize updates memory size in engine config.
func (ei *EngineInstance) setMemSize(memSizeMb int) {
ei.Lock()
defer ei.Unlock()
ei.runner.GetConfig().MemSize = memSizeMb
}
// setHugepageSz updates hugepage size in engine config.
func (ei *EngineInstance) setHugepageSz(hpSizeMb int) {
ei.Lock()
defer ei.Unlock()
ei.runner.GetConfig().HugepageSz = hpSizeMb
}
// GetTargetCount returns the target count set for this instance.
func (ei *EngineInstance) GetTargetCount() int {
ei.RLock()
defer ei.RUnlock()
return ei.runner.GetConfig().TargetCount
}
func (ei *EngineInstance) callSetUp(ctx context.Context) error {
dresp, err := ei.callDrpc(ctx, drpc.MethodSetUp, nil)
if err != nil {
return err
}
resp := &mgmtpb.DaosResp{}
if err := proto.Unmarshal(dresp.Body, resp); err != nil {
return errors.Wrap(err, "unmarshal SetUp response")
}
if resp.Status != 0 {
return errors.Errorf("SetUp: %d\n", resp.Status)
}
return nil
}
func (ei *EngineInstance) Debugf(format string, args ...interface{}) {
ei.log.Debugf(format, args...)
}
func (ei *EngineInstance) Tracef(format string, args ...interface{}) {
ei.log.Tracef(format, args...)
}