Merge pull request #19041 from dragonylffly/wip-list-objects-in-a-pg

tools/rados: allow list objects in a specific pg in a pool

Reviewed-by: Sage Weil <sage@redhat.com>
Reviewed-by: Kefu Chai <kchai@redhat.com>
This commit is contained in:
Kefu Chai 2018-05-06 11:33:47 +08:00 committed by GitHub
commit 1a87441de1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1946,6 +1946,14 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts,
with_reference = true;
}
i = opts.find("pgid");
boost::optional<pg_t> pgid(i != opts.end(), std::move(pg_t()));
if (pgid && (!pgid->parse(i->second.c_str()) || (pool_name && rados.pool_lookup(pool_name) != pgid->pool()))) {
cerr << "invalid pgid" << std::endl;
ret = -1;
goto out;
}
// open rados
ret = rados.init_with_context(g_ceph_context);
if (ret < 0) {
@ -1975,10 +1983,11 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts,
}
// open io context.
if (pool_name) {
ret = rados.ioctx_create(pool_name, io_ctx);
if (pool_name || pgid) {
ret = pool_name ? rados.ioctx_create(pool_name, io_ctx) : rados.ioctx_create2(pgid->pool(), io_ctx);
if (ret < 0) {
cerr << "error opening pool " << pool_name << ": "
cerr << "error opening pool "
<< (pool_name ? pool_name : std::string("with id ") + std::to_string(pgid->pool())) << ": "
<< cpp_strerror(ret) << std::endl;
goto out;
}
@ -2203,8 +2212,8 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts,
}
else if (strcmp(nargs[0], "ls") == 0) {
if (!pool_name) {
cerr << "pool name was not specified" << std::endl;
if (!pool_name && !pgid) {
cerr << "either pool name or pg id needs to be specified" << std::endl;
ret = -1;
goto out;
}
@ -2227,7 +2236,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts,
if (formatter)
formatter->open_array_section("objects");
try {
librados::NObjectIterator i = io_ctx.nobjects_begin();
librados::NObjectIterator i = pgid ? io_ctx.nobjects_begin(pgid->ps()) : io_ctx.nobjects_begin();
librados::NObjectIterator i_end = io_ctx.nobjects_end();
for (; i != i_end; ++i) {
if (use_striper) {
@ -2238,6 +2247,11 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts,
if (l <= 17 ||
(0 != i->get_oid().compare(l-17, 17,".0000000000000000"))) continue;
}
if (pgid) {
uint32_t ps;
if (io_ctx.get_object_pg_hash_position2(i->get_oid(), &ps) || pgid->ps() != ps)
break;
}
if (!formatter) {
// Only include namespace in output when wildcard specified
if (wildcard)
@ -3928,6 +3942,8 @@ int main(int argc, const char **argv)
opts["omap-key-file"] = val;
} else if (ceph_argparse_flag(args, i, "--with-reference", (char*)NULL)) {
opts["with-reference"] = "true";
} else if (ceph_argparse_witharg(args, i, &val, "--pgid", (char*)NULL)) {
opts["pgid"] = val;
} else {
if (val[0] == '-')
usage_exit();