-
Notifications
You must be signed in to change notification settings - Fork 291
/
pool.go
596 lines (497 loc) · 15.3 KB
/
pool.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//
// (C) Copyright 2021-2024 Intel Corporation.
//
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
package main
import (
"fmt"
"os"
"strings"
"unsafe"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/daos-stack/daos/src/control/cmd/daos/pretty"
"github.com/daos-stack/daos/src/control/common"
"github.com/daos-stack/daos/src/control/lib/daos"
"github.com/daos-stack/daos/src/control/lib/ranklist"
"github.com/daos-stack/daos/src/control/lib/ui"
)
/*
#include "util.h"
*/
import "C"
// argOrID is used to handle a positional argument that can be a label or UUID,
// or a non-ID positional argument to be consumed by the command handler if the
// --path flag is used.
type argOrID struct {
ui.LabelOrUUIDFlag
unparsedArg string
}
func (opt *argOrID) Clear() {
opt.LabelOrUUIDFlag.Clear()
opt.unparsedArg = ""
}
func (opt *argOrID) UnmarshalFlag(val string) error {
if err := opt.LabelOrUUIDFlag.UnmarshalFlag(val); err != nil {
if opt.unparsedArg != "" {
return err
}
opt.unparsedArg = val
return nil
}
return nil
}
func (opt *argOrID) String() string {
if opt.unparsedArg != "" {
return opt.unparsedArg
}
if !opt.LabelOrUUIDFlag.Empty() {
return opt.LabelOrUUIDFlag.String()
}
return ""
}
type PoolID struct {
argOrID
}
type poolBaseCmd struct {
daosCmd
poolUUID uuid.UUID
cPoolHandle C.daos_handle_t
SysName string `long:"sys-name" short:"G" description:"DAOS system name"`
Args struct {
Pool PoolID `positional-arg-name:"pool label or UUID" description:"required if --path is not used"`
} `positional-args:"yes"`
}
func (cmd *poolBaseCmd) poolUUIDPtr() *C.uchar {
if cmd.poolUUID == uuid.Nil {
cmd.Errorf("poolUUIDPtr(): nil UUID")
return nil
}
return (*C.uchar)(unsafe.Pointer(&cmd.poolUUID[0]))
}
func (cmd *poolBaseCmd) PoolID() ui.LabelOrUUIDFlag {
return cmd.Args.Pool.LabelOrUUIDFlag
}
func (cmd *poolBaseCmd) connectPool(flags C.uint) error {
sysName := cmd.SysName
var cSysName *C.char
if sysName != "" {
cSysName := C.CString(sysName)
defer freeString(cSysName)
}
var rc C.int
switch {
case cmd.PoolID().HasLabel():
var poolInfo C.daos_pool_info_t
cLabel := C.CString(cmd.PoolID().Label)
defer freeString(cLabel)
cmd.Debugf("connecting to pool: %s", cmd.PoolID().Label)
rc = C.daos_pool_connect(cLabel, cSysName, flags,
&cmd.cPoolHandle, &poolInfo, nil)
if rc == 0 {
var err error
cmd.poolUUID, err = uuidFromC(poolInfo.pi_uuid)
if err != nil {
cmd.disconnectPool()
return err
}
}
case cmd.PoolID().HasUUID():
cmd.poolUUID = cmd.PoolID().UUID
cmd.Debugf("connecting to pool: %s", cmd.poolUUID)
cUUIDstr := C.CString(cmd.poolUUID.String())
defer freeString(cUUIDstr)
rc = C.daos_pool_connect(cUUIDstr, cSysName, flags,
&cmd.cPoolHandle, nil, nil)
default:
return errors.New("no pool UUID or label supplied")
}
return daosError(rc)
}
func (cmd *poolBaseCmd) disconnectPool() {
cmd.Debugf("disconnecting pool %s", cmd.PoolID())
// Hack for NLT fault injection testing: If the rc
// is -DER_NOMEM, retry once in order to actually
// shut down and release resources.
rc := C.daos_pool_disconnect(cmd.cPoolHandle, nil)
if rc == -C.DER_NOMEM {
rc = C.daos_pool_disconnect(cmd.cPoolHandle, nil)
// DAOS-8866, daos_pool_disconnect() might have failed, but worked anyway.
if rc == -C.DER_NO_HDL {
rc = -C.DER_SUCCESS
}
}
if err := daosError(rc); err != nil {
cmd.Errorf("pool disconnect failed: %s", err)
}
}
func (cmd *poolBaseCmd) resolveAndConnect(flags C.uint, ap *C.struct_cmd_args_s) (func(), error) {
if err := cmd.connectPool(flags); err != nil {
return nil, errors.Wrapf(err,
"failed to connect to pool %s", cmd.PoolID())
}
if ap != nil {
if err := copyUUID(&ap.p_uuid, cmd.poolUUID); err != nil {
return nil, err
}
ap.pool = cmd.cPoolHandle
switch {
case cmd.PoolID().HasLabel():
pLabel := C.CString(cmd.PoolID().Label)
defer freeString(pLabel)
C.strncpy(&ap.pool_str[0], pLabel, C.DAOS_PROP_LABEL_MAX_LEN)
case cmd.PoolID().HasUUID():
pUUIDstr := C.CString(cmd.poolUUID.String())
defer freeString(pUUIDstr)
C.strncpy(&ap.pool_str[0], pUUIDstr, C.DAOS_PROP_LABEL_MAX_LEN)
}
}
return func() {
cmd.disconnectPool()
}, nil
}
func (cmd *poolBaseCmd) getAttr(name string) (*attribute, error) {
return getDaosAttribute(cmd.cPoolHandle, poolAttr, name)
}
type poolCmd struct {
Query poolQueryCmd `command:"query" description:"query pool info"`
QueryTargets poolQueryTargetsCmd `command:"query-targets" description:"query pool target info"`
ListConts containerListCmd `command:"list-containers" alias:"list-cont" description:"list all containers in pool"`
ListAttrs poolListAttrsCmd `command:"list-attr" alias:"list-attrs" alias:"lsattr" description:"list pool user-defined attributes"`
GetAttr poolGetAttrCmd `command:"get-attr" alias:"getattr" description:"get pool user-defined attribute"`
SetAttr poolSetAttrCmd `command:"set-attr" alias:"setattr" description:"set pool user-defined attribute"`
DelAttr poolDelAttrCmd `command:"del-attr" alias:"delattr" description:"delete pool user-defined attribute"`
AutoTest poolAutoTestCmd `command:"autotest" description:"verify setup with smoke tests"`
}
type poolQueryCmd struct {
poolBaseCmd
ShowEnabledRanks bool `short:"e" long:"show-enabled" description:"Show engine unique identifiers (ranks) which are enabled"`
ShowDisabledRanks bool `short:"b" long:"show-disabled" description:"Show engine unique identifiers (ranks) which are disabled"`
}
func convertPoolSpaceInfo(in *C.struct_daos_pool_space, mt C.uint) *daos.StorageUsageStats {
if in == nil {
return nil
}
return &daos.StorageUsageStats{
Total: uint64(in.ps_space.s_total[mt]),
Free: uint64(in.ps_space.s_free[mt]),
Min: uint64(in.ps_free_min[mt]),
Max: uint64(in.ps_free_max[mt]),
Mean: uint64(in.ps_free_mean[mt]),
MediaType: daos.StorageMediaType(mt),
}
}
func convertPoolRebuildStatus(in *C.struct_daos_rebuild_status) *daos.PoolRebuildStatus {
if in == nil {
return nil
}
out := &daos.PoolRebuildStatus{
Status: int32(in.rs_errno),
}
if out.Status == 0 {
out.Objects = uint64(in.rs_obj_nr)
out.Records = uint64(in.rs_rec_nr)
switch {
case in.rs_version == 0:
out.State = daos.PoolRebuildStateIdle
case C.get_rebuild_state(in) == C.DRS_COMPLETED:
out.State = daos.PoolRebuildStateDone
default:
out.State = daos.PoolRebuildStateBusy
}
}
return out
}
func convertPoolInfo(pinfo *C.daos_pool_info_t) (*daos.PoolInfo, error) {
poolInfo := new(daos.PoolInfo)
poolInfo.UUID = uuid.Must(uuidFromC(pinfo.pi_uuid))
poolInfo.TotalTargets = uint32(pinfo.pi_ntargets)
poolInfo.DisabledTargets = uint32(pinfo.pi_ndisabled)
poolInfo.ActiveTargets = uint32(pinfo.pi_space.ps_ntargets)
poolInfo.TotalEngines = uint32(pinfo.pi_nnodes)
poolInfo.ServiceLeader = uint32(pinfo.pi_leader)
poolInfo.Version = uint32(pinfo.pi_map_ver)
poolInfo.State = daos.PoolServiceStateReady
if poolInfo.DisabledTargets > 0 {
poolInfo.State = daos.PoolServiceStateDegraded
}
poolInfo.Rebuild = convertPoolRebuildStatus(&pinfo.pi_rebuild_st)
poolInfo.TierStats = []*daos.StorageUsageStats{
convertPoolSpaceInfo(&pinfo.pi_space, C.DAOS_MEDIA_SCM),
convertPoolSpaceInfo(&pinfo.pi_space, C.DAOS_MEDIA_NVME),
}
return poolInfo, nil
}
const (
dpiQuerySpace = C.DPI_SPACE
dpiQueryRebuild = C.DPI_REBUILD_STATUS
dpiQueryAll = C.uint64_t(^uint64(0)) // DPI_ALL is -1
)
func generateRankSet(ranklist *C.d_rank_list_t) string {
if ranklist.rl_nr == 0 {
return ""
}
ranks := uintptr(unsafe.Pointer(ranklist.rl_ranks))
const size = unsafe.Sizeof(uint32(0))
rankset := "["
for i := 0; i < int(ranklist.rl_nr); i++ {
if i > 0 {
rankset += ","
}
rankset += fmt.Sprint(*(*uint32)(unsafe.Pointer(ranks + uintptr(i)*size)))
}
rankset += "]"
return rankset
}
func (cmd *poolQueryCmd) Execute(_ []string) error {
if cmd.ShowEnabledRanks && cmd.ShowDisabledRanks {
return errors.New("show-enabled and show-disabled can't be used at the same time.")
}
var rlPtr **C.d_rank_list_t = nil
var rl *C.d_rank_list_t = nil
if cmd.ShowEnabledRanks || cmd.ShowDisabledRanks {
rlPtr = &rl
}
cleanup, err := cmd.resolveAndConnect(C.DAOS_PC_RO, nil)
if err != nil {
return err
}
defer cleanup()
cPoolInfo := C.daos_pool_info_t{
pi_bits: dpiQueryAll,
}
if cmd.ShowDisabledRanks {
cPoolInfo.pi_bits &= C.uint64_t(^(uint64(C.DPI_ENGINES_ENABLED)))
}
rc := C.daos_pool_query(cmd.cPoolHandle, rlPtr, &cPoolInfo, nil, nil)
defer C.d_rank_list_free(rl)
if err := daosError(rc); err != nil {
return errors.Wrapf(err,
"failed to query pool %s", cmd.poolUUID)
}
poolInfo, err := convertPoolInfo(&cPoolInfo)
if err != nil {
return err
}
if rlPtr != nil {
if cmd.ShowEnabledRanks {
poolInfo.EnabledRanks = ranklist.MustCreateRankSet(generateRankSet(rl))
}
if cmd.ShowDisabledRanks {
poolInfo.DisabledRanks = ranklist.MustCreateRankSet(generateRankSet(rl))
}
}
if cmd.JSONOutputEnabled() {
return cmd.OutputJSON(poolInfo, nil)
}
var bld strings.Builder
if err := pretty.PrintPoolInfo(poolInfo, &bld); err != nil {
return err
}
cmd.Info(bld.String())
return nil
}
type poolQueryTargetsCmd struct {
poolBaseCmd
Rank uint32 `long:"rank" required:"1" description:"Engine rank of the targets to be queried"`
Targets string `long:"target-idx" description:"Comma-separated list of target idx(s) to be queried"`
}
// For using the pretty printer that dmg uses for this target info.
func convertPoolTargetInfo(ptinfo *C.daos_target_info_t) (*daos.PoolQueryTargetInfo, error) {
pqti := new(daos.PoolQueryTargetInfo)
pqti.Type = daos.PoolQueryTargetType(ptinfo.ta_type)
pqti.State = daos.PoolQueryTargetState(ptinfo.ta_state)
pqti.Space = []*daos.StorageUsageStats{
{
Total: uint64(ptinfo.ta_space.s_total[C.DAOS_MEDIA_SCM]),
Free: uint64(ptinfo.ta_space.s_free[C.DAOS_MEDIA_SCM]),
MediaType: C.DAOS_MEDIA_SCM,
},
{
Total: uint64(ptinfo.ta_space.s_total[C.DAOS_MEDIA_NVME]),
Free: uint64(ptinfo.ta_space.s_free[C.DAOS_MEDIA_NVME]),
MediaType: C.DAOS_MEDIA_NVME,
},
}
return pqti, nil
}
func (cmd *poolQueryTargetsCmd) Execute(_ []string) error {
cleanup, err := cmd.resolveAndConnect(C.DAOS_PC_RO, nil)
if err != nil {
return err
}
defer cleanup()
var idxList []uint32
if err = common.ParseNumberList(cmd.Targets, &idxList); err != nil {
return errors.WithMessage(err, "parsing target list")
}
ptInfo := new(C.daos_target_info_t)
var rc C.int
infos := make([]*daos.PoolQueryTargetInfo, 0, len(idxList))
for tgt := 0; tgt < len(idxList); tgt++ {
rc = C.daos_pool_query_target(cmd.cPoolHandle, C.uint32_t(idxList[tgt]), C.uint32_t(cmd.Rank), ptInfo, nil)
if err := daosError(rc); err != nil {
return errors.Wrapf(err,
"failed to query pool %s rank:target %d:%d", cmd.poolUUID, cmd.Rank, idxList[tgt])
}
tgtInfo, err := convertPoolTargetInfo(ptInfo)
if err != nil {
return err
}
infos = append(infos, tgtInfo)
}
if cmd.JSONOutputEnabled() {
return cmd.OutputJSON(infos, nil)
}
var bld strings.Builder
for _, info := range infos {
if err := pretty.PrintPoolQueryTargetInfo(info, &bld); err != nil {
return err
}
}
cmd.Info(bld.String())
return nil
}
type poolListAttrsCmd struct {
poolBaseCmd
Verbose bool `long:"verbose" short:"V" description:"Include values"`
}
func (cmd *poolListAttrsCmd) Execute(_ []string) error {
cleanup, err := cmd.resolveAndConnect(C.DAOS_PC_RO, nil)
if err != nil {
return err
}
defer cleanup()
attrs, err := listDaosAttributes(cmd.cPoolHandle, poolAttr, cmd.Verbose)
if err != nil {
return errors.Wrapf(err,
"failed to list attributes for pool %s", cmd.poolUUID)
}
if cmd.JSONOutputEnabled() {
if cmd.Verbose {
return cmd.OutputJSON(attrs.asMap(), nil)
}
return cmd.OutputJSON(attrs.asList(), nil)
}
var bld strings.Builder
title := fmt.Sprintf("Attributes for pool %s:", cmd.poolUUID)
printAttributes(&bld, title, attrs...)
cmd.Info(bld.String())
return nil
}
type poolGetAttrCmd struct {
poolBaseCmd
Args struct {
Attrs ui.GetPropertiesFlag `positional-arg-name:"key[,key...]"`
} `positional-args:"yes"`
}
func (cmd *poolGetAttrCmd) Execute(_ []string) error {
cleanup, err := cmd.resolveAndConnect(C.DAOS_PC_RO, nil)
if err != nil {
return err
}
defer cleanup()
var attrs attrList
if len(cmd.Args.Attrs.ParsedProps) == 0 {
attrs, err = listDaosAttributes(cmd.cPoolHandle, poolAttr, true)
} else {
attrs, err = getDaosAttributes(cmd.cPoolHandle, poolAttr, cmd.Args.Attrs.ParsedProps.ToSlice())
}
if err != nil {
return errors.Wrapf(err, "failed to get attributes for pool %s", cmd.PoolID())
}
if cmd.JSONOutputEnabled() {
// Maintain compatibility with older behavior.
if len(cmd.Args.Attrs.ParsedProps) == 1 && len(attrs) == 1 {
return cmd.OutputJSON(attrs[0], nil)
}
return cmd.OutputJSON(attrs, nil)
}
var bld strings.Builder
title := fmt.Sprintf("Attributes for pool %s:", cmd.PoolID())
printAttributes(&bld, title, attrs...)
cmd.Info(bld.String())
return nil
}
type poolSetAttrCmd struct {
poolBaseCmd
Args struct {
Attrs ui.SetPropertiesFlag `positional-arg-name:"key:val[,key:val...]" required:"1"`
} `positional-args:"yes"`
}
func (cmd *poolSetAttrCmd) Execute(_ []string) error {
cleanup, err := cmd.resolveAndConnect(C.DAOS_PC_RW, nil)
if err != nil {
return err
}
defer cleanup()
if len(cmd.Args.Attrs.ParsedProps) == 0 {
return errors.New("attribute name and value are required")
}
attrs := make(attrList, 0, len(cmd.Args.Attrs.ParsedProps))
for key, val := range cmd.Args.Attrs.ParsedProps {
attrs = append(attrs, &attribute{
Name: key,
Value: []byte(val),
})
}
if err := setDaosAttributes(cmd.cPoolHandle, poolAttr, attrs); err != nil {
return errors.Wrapf(err, "failed to set attributes on pool %s", cmd.PoolID())
}
return nil
}
type poolDelAttrCmd struct {
poolBaseCmd
Args struct {
Name string `positional-arg-name:"<attribute name>" required:"1"`
} `positional-args:"yes"`
}
func (cmd *poolDelAttrCmd) Execute(_ []string) error {
cleanup, err := cmd.resolveAndConnect(C.DAOS_PC_RW, nil)
if err != nil {
return err
}
defer cleanup()
if err := delDaosAttribute(cmd.cPoolHandle, poolAttr, cmd.Args.Name); err != nil {
return errors.Wrapf(err,
"failed to delete attribute %q on pool %s",
cmd.Args.Name, cmd.poolUUID)
}
return nil
}
type poolAutoTestCmd struct {
poolBaseCmd
SkipBig C.bool `long:"skip-big" short:"S" description:"skip big tests"`
DeadlineLimit C.int `long:"deadline-limit" short:"D" description:"deadline limit for test (seconds)"`
}
func (cmd *poolAutoTestCmd) Execute(_ []string) error {
ap, deallocCmdArgs, err := allocCmdArgs(cmd.Logger)
if err != nil {
return err
}
defer deallocCmdArgs()
cleanup, err := cmd.resolveAndConnect(C.DAOS_PC_RW, nil)
if err != nil {
return err
}
defer cleanup()
ap.pool = cmd.cPoolHandle
if err := copyUUID(&ap.p_uuid, cmd.poolUUID); err != nil {
return err
}
ap.p_op = C.POOL_AUTOTEST
// Set outstream to stdout; don't try to redirect it.
ap.outstream, err = fd2FILE(os.Stdout.Fd(), "w")
if err != nil {
return err
}
ap.skip_big = C.bool(cmd.SkipBig)
ap.deadline_limit = C.int(cmd.DeadlineLimit)
rc := C.pool_autotest_hdlr(ap)
if err := daosError(rc); err != nil {
return errors.Wrapf(err, "failed to run autotest for pool %s",
cmd.poolUUID)
}
return nil
}