forked from daos-stack/daos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance.go
494 lines (420 loc) · 14.1 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//
// (C) Copyright 2019 Intel Corporation.
//
// 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.
//
// GOVERNMENT LICENSE RIGHTS-OPEN SOURCE SOFTWARE
// The Government's rights to use, modify, reproduce, release, perform, display,
// or disclose this software are subject to the terms of the Apache License as
// provided in Contract No. 8F-30005.
// Any reproduction of computer software, computer software documentation, or
// portions thereof marked with this legend must also reproduce the markings.
//
package server
import (
"context"
"net"
"os"
"sync"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
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/logging"
"github.com/daos-stack/daos/src/control/server/ioserver"
"github.com/daos-stack/daos/src/control/server/storage"
"github.com/daos-stack/daos/src/control/server/storage/bdev"
"github.com/daos-stack/daos/src/control/server/storage/scm"
"github.com/daos-stack/daos/src/control/system"
)
// IOServerRunner defines an interface for starting and stopping the
// daos_io_server.
type IOServerRunner interface {
Start(context.Context, chan<- error) error
IsRunning() bool
Stop(bool) error
GetConfig() *ioserver.Config
}
// IOServerInstance encapsulates control-plane specific configuration
// and functionality for managed I/O server instances. The distinction
// between this structure and what's in the ioserver package is that the
// ioserver package is only concerned with configuring and executing
// a single daos_io_server instance. IOServerInstance is intended to
// be used with IOServerHarness to manage and monitor multiple instances
// per node.
type IOServerInstance struct {
log logging.Logger
runner IOServerRunner
bdevClassProvider *bdev.ClassProvider
scmProvider *scm.Provider
msClient *mgmtSvcClient
instanceReady chan *srvpb.NotifyReadyReq
storageReady chan struct{}
fsRoot string
sync.RWMutex
// these must be protected by a mutex in order to
// avoid racy access.
_drpcClient drpc.DomainSocketClient
_scmStorageOk bool // cache positive result of NeedsStorageFormat()
_superblock *Superblock
}
// NewIOServerInstance returns an *IOServerInstance initialized with
// its dependencies.
func NewIOServerInstance(log logging.Logger,
bcp *bdev.ClassProvider, sp *scm.Provider,
msc *mgmtSvcClient, r IOServerRunner) *IOServerInstance {
return &IOServerInstance{
log: log,
runner: r,
bdevClassProvider: bcp,
scmProvider: sp,
msClient: msc,
instanceReady: make(chan *srvpb.NotifyReadyReq),
storageReady: make(chan struct{}),
}
}
// scmConfig returns the scm configuration assigned to this instance.
func (srv *IOServerInstance) scmConfig() storage.ScmConfig {
return srv.runner.GetConfig().Storage.SCM
}
// bdevConfig returns the block device configuration assigned to this instance.
func (srv *IOServerInstance) bdevConfig() storage.BdevConfig {
return srv.runner.GetConfig().Storage.Bdev
}
func (srv *IOServerInstance) setDrpcClient(c drpc.DomainSocketClient) {
srv.Lock()
defer srv.Unlock()
srv._drpcClient = c
}
func (srv *IOServerInstance) getDrpcClient() (drpc.DomainSocketClient, error) {
srv.RLock()
defer srv.RUnlock()
if srv._drpcClient == nil {
return nil, errors.New("no dRPC client set (data plane not started?)")
}
return srv._drpcClient, nil
}
// SetIndex sets the server index assigned by the harness.
func (srv *IOServerInstance) SetIndex(idx uint32) {
srv.runner.GetConfig().Index = idx
}
// Index returns the server index assigned by the harness.
func (srv *IOServerInstance) Index() uint32 {
return srv.runner.GetConfig().Index
}
// MountScmDevice mounts the configured SCM device (DCPM or ramdisk emulation)
// at the mountpoint specified in the configuration. If the device is already
// mounted, the function returns nil, indicating success.
func (srv *IOServerInstance) MountScmDevice() error {
scmCfg := srv.scmConfig()
isMount, err := srv.scmProvider.IsMounted(scmCfg.MountPoint)
if err != nil && !os.IsNotExist(errors.Cause(err)) {
return errors.WithMessage(err, "failed to check SCM mount")
}
if isMount {
return nil
}
srv.log.Debugf("attempting to mount existing SCM dir %s\n", scmCfg.MountPoint)
var res *scm.MountResponse
switch scmCfg.Class {
case storage.ScmClassRAM:
res, err = srv.scmProvider.MountRamdisk(scmCfg.MountPoint, uint(scmCfg.RamdiskSize))
case storage.ScmClassDCPM:
if len(scmCfg.DeviceList) != 1 {
err = scm.FaultFormatInvalidDeviceCount
break
}
res, err = srv.scmProvider.MountDcpm(scmCfg.DeviceList[0], scmCfg.MountPoint)
default:
err = errors.New(scm.MsgScmClassNotSupported)
}
if err != nil {
return errors.WithMessage(err, "mounting existing scm dir")
}
srv.log.Debugf("%s mounted: %t", res.Target, res.Mounted)
return nil
}
// NeedsScmFormat probes the configured instance storage and determines whether
// or not it requires a format operation before it can be used.
func (srv *IOServerInstance) NeedsScmFormat() (bool, error) {
srv.RLock()
if srv._scmStorageOk {
srv.RUnlock()
return false, nil
}
srv.RUnlock()
scmCfg := srv.scmConfig()
srv.log.Debugf("%s: checking formatting", scmCfg.MountPoint)
// take a lock here to ensure that we can safely set _scmStorageOk
// as well as avoiding racy access to stuff in srv.ext.
srv.Lock()
defer srv.Unlock()
req, err := scm.CreateFormatRequest(scmCfg, false)
if err != nil {
return false, err
}
res, err := srv.scmProvider.CheckFormat(*req)
if err != nil {
return false, err
}
needsFormat := !res.Mounted && !res.Mountable
srv.log.Debugf("%s (%s) needs format: %t", scmCfg.MountPoint, scmCfg.Class, needsFormat)
return needsFormat, nil
}
// Start checks to make sure that the instance has a valid superblock before
// performing any required NVMe preparation steps and launching a managed
// daos_io_server instance.
func (srv *IOServerInstance) Start(ctx context.Context, errChan chan<- error) error {
if !srv.hasSuperblock() {
if err := srv.ReadSuperblock(); err != nil {
return errors.Wrap(err, "start failed; no superblock")
}
}
if err := srv.bdevClassProvider.PrepareDevices(); err != nil {
return errors.Wrap(err, "start failed; unable to prepare NVMe device(s)")
}
if err := srv.bdevClassProvider.GenConfigFile(); err != nil {
return errors.Wrap(err, "start failed; unable to generate NVMe configuration for SPDK")
}
if err := srv.logScmStorage(); err != nil {
srv.log.Errorf("unable to log SCM storage stats: %s", err)
}
return srv.runner.Start(ctx, errChan)
}
func (srv *IOServerInstance) Stop(force bool) error {
return srv.runner.Stop(force)
}
func (srv *IOServerInstance) IsStarted() bool {
return srv.runner.IsRunning()
}
// NotifyReady receives a ready message from the running IOServer
// instance.
func (srv *IOServerInstance) NotifyReady(msg *srvpb.NotifyReadyReq) {
srv.log.Debugf("%s instance %d ready: %v", DataPlaneName, srv.Index(), msg)
// Activate the dRPC client connection to this iosrv
srv.setDrpcClient(drpc.NewClientConnection(msg.DrpcListenerSock))
go func() {
srv.instanceReady <- msg
}()
}
// AwaitReady returns a channel which receives a ready message
// when the started IOServer instance indicates that it is
// ready to receive dRPC messages.
func (srv *IOServerInstance) AwaitReady() chan *srvpb.NotifyReadyReq {
return srv.instanceReady
}
// NotifyStorageReady releases any blocks on AwaitStorageReady().
func (srv *IOServerInstance) NotifyStorageReady() {
srv.log.Debugf("%s instance %d notifying storage ready", DataPlaneName, srv.Index())
go func() {
close(srv.storageReady)
}()
}
// AwaitStorageReady blocks until the IOServer's storage is ready.
func (srv *IOServerInstance) AwaitStorageReady(ctx context.Context) {
select {
case <-ctx.Done():
srv.log.Infof("%s instance %d storage not ready: %s", DataPlaneName, srv.Index(), ctx.Err())
case <-srv.storageReady:
srv.log.Infof("%s instance %d storage ready", DataPlaneName, srv.Index())
}
}
// SetRank determines the instance rank and sends a SetRank dRPC request
// to the IOServer.
func (srv *IOServerInstance) SetRank(ctx context.Context, ready *srvpb.NotifyReadyReq) error {
superblock := srv.getSuperblock()
if superblock == nil {
return errors.New("nil superblock in SetRank()")
}
r := ioserver.NilRank
if superblock.Rank != nil {
r = *superblock.Rank
}
if !superblock.ValidRank || !superblock.MS {
resp, err := srv.msClient.Join(ctx, &mgmtpb.JoinReq{
Uuid: superblock.UUID,
Rank: r.Uint32(),
Uri: ready.Uri,
Nctxs: ready.Nctxs,
// Addr member populated in msClient
})
if err != nil {
return err
} else if resp.State == mgmtpb.JoinResp_OUT {
return errors.Errorf("rank %d excluded", resp.Rank)
}
r = ioserver.Rank(resp.Rank)
if !superblock.ValidRank {
superblock.Rank = new(ioserver.Rank)
*superblock.Rank = r
superblock.ValidRank = true
srv.setSuperblock(superblock)
if err := srv.WriteSuperblock(); err != nil {
return err
}
}
}
if err := srv.callSetRank(r); err != nil {
return err
}
return nil
}
func (srv *IOServerInstance) callSetRank(rank ioserver.Rank) error {
dresp, err := srv.CallDrpc(drpc.ModuleMgmt, drpc.MethodSetRank, &mgmtpb.SetRankReq{Rank: rank.Uint32()})
if err != nil {
return err
}
resp := &mgmtpb.DaosResp{}
if err = proto.Unmarshal(dresp.Body, resp); err != nil {
return errors.Wrap(err, "unmarshall SetRank response")
}
if resp.Status != 0 {
return errors.Errorf("SetRank: %d\n", resp.Status)
}
return nil
}
// SetTargetCount updates target count in ioserver config.
func (srv *IOServerInstance) SetTargetCount(numTargets int) {
srv.runner.GetConfig().TargetCount = numTargets
}
// StartManagementService starts the DAOS management service replica associated
// with this instance. If no replica is associated with this instance, this
// function is a no-op.
func (srv *IOServerInstance) StartManagementService() error {
superblock := srv.getSuperblock()
// should have been loaded by now
if superblock == nil {
return errors.Errorf("%s instance %d: nil superblock", DataPlaneName, srv.Index())
}
if superblock.CreateMS {
srv.log.Debugf("create MS (bootstrap=%t)", superblock.BootstrapMS)
if err := srv.callCreateMS(superblock); err != nil {
return err
}
superblock.CreateMS = false
superblock.BootstrapMS = false
srv.setSuperblock(superblock)
if err := srv.WriteSuperblock(); err != nil {
return err
}
}
if superblock.MS {
srv.log.Debug("start MS")
if err := srv.callStartMS(); err != nil {
return err
}
msInfo, err := getMgmtInfo(srv)
if err != nil {
return err
}
if msInfo.isReplica {
msg := "Management Service access point started"
if msInfo.shouldBootstrap {
msg += " (bootstrapped)"
}
srv.log.Info(msg)
}
}
return nil
}
// LoadModules initiates the I/O server startup sequence.
func (srv *IOServerInstance) LoadModules() error {
return srv.callSetUp()
}
func (srv *IOServerInstance) callCreateMS(superblock *Superblock) error {
msAddr, err := srv.msClient.LeaderAddress()
if err != nil {
return err
}
req := &mgmtpb.CreateMsReq{}
if superblock.BootstrapMS {
req.Bootstrap = true
req.Uuid = superblock.UUID
req.Addr = msAddr
}
dresp, err := srv.CallDrpc(drpc.ModuleMgmt, drpc.MethodCreateMS, req)
if err != nil {
return err
}
resp := &mgmtpb.DaosResp{}
if err := proto.Unmarshal(dresp.Body, resp); err != nil {
return errors.Wrap(err, "unmarshal CreateMS response")
}
if resp.Status != 0 {
return errors.Errorf("CreateMS: %d\n", resp.Status)
}
return nil
}
func (srv *IOServerInstance) callStartMS() error {
dresp, err := srv.CallDrpc(drpc.ModuleMgmt, drpc.MethodStartMS, nil)
if err != nil {
return err
}
resp := &mgmtpb.DaosResp{}
if err := proto.Unmarshal(dresp.Body, resp); err != nil {
return errors.Wrap(err, "unmarshal StartMS response")
}
if resp.Status != 0 {
return errors.Errorf("StartMS: %d\n", resp.Status)
}
return nil
}
func (srv *IOServerInstance) callSetUp() error {
dresp, err := srv.CallDrpc(drpc.ModuleMgmt, 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
}
// IsMSReplica indicates whether or not this instance is a management service replica.
func (srv *IOServerInstance) IsMSReplica() bool {
return srv.hasSuperblock() && srv.getSuperblock().MS
}
// CallDrpc makes the supplied dRPC call via this instance's dRPC client.
func (srv *IOServerInstance) CallDrpc(module, method int32, body proto.Message) (*drpc.Response, error) {
dc, err := srv.getDrpcClient()
if err != nil {
return nil, err
}
return makeDrpcCall(dc, module, method, body)
}
func (srv *IOServerInstance) BioErrorNotify(bio *srvpb.BioErrorReq) {
srv.log.Errorf("I/O server instance %d (target %d) has detected blob I/O error! %v",
srv.Index(), bio.TgtId, bio)
}
// newMember returns reference to a new member struct if one can be retrieved
// from superblock, error otherwise. Member populated with local reply address.
func (srv *IOServerInstance) newMember() (*system.Member, error) {
if !srv.hasSuperblock() {
return nil, errors.New("missing superblock")
}
sb := srv.getSuperblock()
msAddr, err := srv.msClient.LeaderAddress()
if err != nil {
return nil, err
}
addr, err := net.ResolveTCPAddr("tcp", msAddr)
if err != nil {
return nil, err
}
return system.NewMember(sb.Rank.Uint32(), sb.UUID, addr,
system.MemberStateStarted), nil
}