Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DAOS-11217 tools: Fix daos cont list-obj JSON output #9872

Merged
merged 1 commit into from Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 55 additions & 4 deletions src/control/cmd/daos/container.go
Expand Up @@ -638,11 +638,62 @@ func (cmd *containerListObjectsCmd) Execute(_ []string) error {
ap.epc = C.uint64_t(cmd.Epoch)
}

// TODO: Build a Go slice so that we can JSON-format the list.
rc := C.cont_list_objs_hdlr(ap)
snapOpts := uint32(C.DAOS_SNAP_OPT_CR | C.DAOS_SNAP_OPT_OIT)
rc := C.daos_cont_create_snap_opt(ap.cont, &ap.epc, nil, snapOpts, nil)
if err := daosError(rc); err != nil {
return errors.Wrapf(err,
"failed to list objects in container %s", cmd.ContainerID())
return errors.Wrapf(err, "failed to create snapshot for container %s", cmd.ContainerID())
}
defer func() {
rc = C.cont_destroy_snap_hdlr(ap)
if err := daosError(rc); err != nil {
cmd.Errorf("failed to destroy snapshot in cleanup: %v", err)
}
}()

oit := C.daos_handle_t{}
rc = C.daos_oit_open(ap.cont, ap.epc, &oit, nil)
if err := daosError(rc); err != nil {
return errors.Wrapf(err, "failed to open OIT for container %s", cmd.ContainerID())
}
defer func() {
rc = C.daos_oit_close(oit, nil)
if err := daosError(rc); err != nil {
cmd.Errorf("failed to close OIT in cleanup: %v", err)
}
}()

// NB: It is somewhat inefficient to build up a slice of OID strings for
// the JSON output format, but it is simple. If it turns out to be a problem,
// we can implement a custom JSON output handler that streams the OID
// strings directly to output.
var oids []string
var readOids C.uint32_t
oidArr := [C.OID_ARR_SIZE]C.daos_obj_id_t{}
anchor := C.daos_anchor_t{}
for {
if C.daos_anchor_is_eof(&anchor) {
break
}

readOids = C.OID_ARR_SIZE
rc = C.daos_oit_list(oit, &oidArr[0], &readOids, &anchor, nil)
if err := daosError(rc); err != nil {
return errors.Wrapf(err, "failed to list objects for container %s", cmd.ContainerID())
}

for i := C.uint32_t(0); i < readOids; i++ {
oid := fmt.Sprintf("%d.%d", oidArr[i].hi, oidArr[i].lo)

if !cmd.jsonOutputEnabled() {
cmd.Infof("%s", oid)
continue
}
oids = append(oids, oid)
}
}

if cmd.jsonOutputEnabled() {
return cmd.outputJSON(oids, nil)
}

return nil
Expand Down
48 changes: 0 additions & 48 deletions src/utils/daos_hdlr.c
Expand Up @@ -40,8 +40,6 @@

#include "daos_hdlr.h"

#define OID_ARR_SIZE 8

struct file_dfs {
enum {POSIX, DAOS} type;
int fd;
Expand Down Expand Up @@ -3135,52 +3133,6 @@ cont_rollback_hdlr(struct cmd_args_s *ap)
return rc;
}

int
cont_list_objs_hdlr(struct cmd_args_s *ap)
{
daos_obj_id_t oids[OID_ARR_SIZE];
daos_handle_t oit;
daos_anchor_t anchor = {0};
uint32_t oids_nr;
int rc, i;

/* create a snapshot with OIT */
rc = daos_cont_create_snap_opt(ap->cont, &ap->epc, NULL,
DAOS_SNAP_OPT_CR | DAOS_SNAP_OPT_OIT,
NULL);
if (rc != 0)
goto out;

/* open OIT */
rc = daos_oit_open(ap->cont, ap->epc, &oit, NULL);
if (rc != 0) {
fprintf(ap->errstream,
"open of container's OIT failed: "DF_RC"\n", DP_RC(rc));
goto out_snap;
}

while (!daos_anchor_is_eof(&anchor)) {
oids_nr = OID_ARR_SIZE;
rc = daos_oit_list(oit, oids, &oids_nr, &anchor, NULL);
if (rc != 0) {
fprintf(ap->errstream,
"object IDs enumeration failed: "DF_RC"\n",
DP_RC(rc));
D_GOTO(out_close, rc);
}

for (i = 0; i < oids_nr; i++)
D_PRINT(DF_OID"\n", DP_OID(oids[i]));
}

out_close:
daos_oit_close(oit, NULL);
out_snap:
cont_destroy_snap_hdlr(ap);
out:
return rc;
}

int
obj_query_hdlr(struct cmd_args_s *ap)
{
Expand Down
3 changes: 2 additions & 1 deletion src/utils/daos_hdlr.h
Expand Up @@ -9,6 +9,8 @@

#include <daos_fs.h>

#define OID_ARR_SIZE 8

enum fs_op {
FS_COPY,
FS_SET_ATTR,
Expand Down Expand Up @@ -171,7 +173,6 @@ int cont_update_acl_hdlr(struct cmd_args_s *ap);
int cont_delete_acl_hdlr(struct cmd_args_s *ap);
int cont_set_owner_hdlr(struct cmd_args_s *ap);
int cont_rollback_hdlr(struct cmd_args_s *ap);
int cont_list_objs_hdlr(struct cmd_args_s *ap);

/* TODO implement the following container op functions
* all with signatures similar to this:
Expand Down