ceph-bench/radosutil.cpp

111 lines
2.5 KiB
C++
Raw Permalink Normal View History

2022-01-18 01:13:03 +03:00
#include "radosutil.h"
2019-01-20 15:35:09 +03:00
#include <json/json.h>
2022-01-18 01:13:03 +03:00
#include <librados.hpp>
#include <string>
2019-01-20 15:35:09 +03:00
using namespace std;
using namespace librados;
RadosUtils::RadosUtils(Rados *rados_)
: rados(rados_), /* */
json_reader(new Json::Reader(Json::Features::strictMode())), /* */
json_writer(new Json::FastWriter()) /* */
2022-01-18 01:13:03 +03:00
{
}
2019-01-20 15:35:09 +03:00
2019-01-20 19:29:30 +03:00
// RadosUtils::~RadosUtils() {}
2022-01-18 01:13:03 +03:00
unsigned int RadosUtils::get_obj_acting_primary(const string &name, const string &pool)
{
Json::Value cmd(Json::objectValue);
cmd["prefix"] = "osd map";
cmd["object"] = name;
cmd["pool"] = pool;
2019-01-20 15:35:09 +03:00
2022-01-18 01:13:03 +03:00
auto &&location = do_mon_command(cmd);
2019-01-20 15:35:09 +03:00
2022-01-18 01:13:03 +03:00
const auto &acting_primary = location["acting_primary"];
if (!acting_primary.isNumeric())
throw "Failed to get acting_primary";
2019-01-20 15:35:09 +03:00
2022-01-18 01:13:03 +03:00
return acting_primary.asUInt();
2019-01-20 15:35:09 +03:00
}
2022-01-18 01:13:03 +03:00
map<string, string> RadosUtils::get_osd_location(unsigned int osd)
{
Json::Value cmd(Json::objectValue);
cmd["prefix"] = "osd find";
cmd["id"] = osd;
2019-01-20 15:35:09 +03:00
2022-01-18 01:13:03 +03:00
auto &&location = do_mon_command(cmd);
const auto &crush = location["crush_location"];
2019-01-20 15:35:09 +03:00
2022-01-18 01:13:03 +03:00
map<string, string> result;
2019-01-20 15:35:09 +03:00
2022-01-18 01:13:03 +03:00
for (auto &&it = crush.begin(); it != crush.end(); ++it)
{
result[it.name()] = it->asString();
}
2019-01-20 15:35:09 +03:00
2022-01-18 01:13:03 +03:00
result["osd"] = "osd." + to_string(osd);
2019-01-20 15:35:09 +03:00
2022-01-18 01:13:03 +03:00
return result;
2019-01-20 15:35:09 +03:00
}
2022-01-18 01:13:03 +03:00
set<unsigned int> RadosUtils::get_osds(const string &pool)
{
Json::Value cmd(Json::objectValue);
cmd["prefix"] = "pg ls-by-pool";
cmd["poolstr"] = pool;
auto pgs = do_mon_command(cmd);
set<unsigned int> osds;
if (pgs.isMember("pg_stats"))
{
pgs = pgs["pg_stats"];
}
for (const auto &pg : pgs)
{
const auto &primary = pg["acting_primary"];
if (!primary.isNumeric())
throw "Failed to get acting_primary";
osds.insert(primary.asUInt());
}
return osds;
2019-01-20 15:35:09 +03:00
}
2022-01-18 01:13:03 +03:00
unsigned int RadosUtils::get_pool_size(const string &pool)
{
Json::Value cmd(Json::objectValue);
cmd["prefix"] = "osd pool get";
cmd["pool"] = pool;
cmd["var"] = "size";
2019-01-20 15:35:09 +03:00
2022-01-18 01:13:03 +03:00
const auto &&v = do_mon_command(cmd);
2019-01-20 15:35:09 +03:00
2022-01-18 01:13:03 +03:00
return v["size"].asUInt();
2019-01-20 15:35:09 +03:00
}
2022-01-18 01:13:03 +03:00
Json::Value RadosUtils::do_mon_command(Json::Value &cmd)
{
int err;
bufferlist outbl;
string outs;
cmd["format"] = "json";
bufferlist inbl;
2019-01-20 15:35:09 +03:00
2022-01-18 01:13:03 +03:00
if ((err = rados->mon_command(json_writer->write(cmd), inbl, &outbl, &outs)) < 0)
throw MyRadosException(err, outs);
2019-01-20 15:35:09 +03:00
2022-01-18 01:13:03 +03:00
Json::Value root;
if (!json_reader->parse(outbl.to_str(), root))
throw "JSON parse error";
2019-01-20 15:35:09 +03:00
2022-01-18 01:13:03 +03:00
return root;
2019-01-20 15:35:09 +03:00
}