Skip to content

Commit d3b1a69

Browse files
authored
Merge pull request ceph#62244 from ronen-fr/wip-rf-cast-conf
common, osd: add cmd_getval_cast_or() Reviewed-by: Radoslaw Zarzynski <[email protected]> Reviewed-by: Alex Ainscow <[email protected]>
2 parents 0e6ccd1 + 58cd295 commit d3b1a69

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

src/common/cmdparse.h

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,34 @@ T cmd_getval_or(const cmdmap_t& cmdmap, std::string_view k,
102102
const V& defval)
103103
{
104104
auto found = cmdmap.find(k);
105-
if (found == cmdmap.end()) {
105+
if (found == cmdmap.cend()) {
106106
return T(defval);
107107
}
108108
try {
109-
return boost::get<T>(cmdmap.find(k)->second);
109+
return boost::get<T>(found->second);
110+
} catch (boost::bad_get&) {
111+
throw bad_cmd_get(k, cmdmap);
112+
}
113+
}
114+
115+
/**
116+
* with default, to be used when the parameter type (which matches the type of the
117+
* 'default' object) is not one of the limited set of parameters type.
118+
* Added to support "safe types" - typed wrappers "around" simple types (e.g.
119+
* shard_id_t which is a structure holding one int8_t), without requiring the
120+
* user to specify the default in the 'internal type'.
121+
*
122+
* Throws if the key is found, but the type is not the expected one.
123+
*/
124+
template <typename CMD_TYPE, typename T>
125+
T cmd_getval_cast_or(const cmdmap_t& cmdmap, std::string_view k, T defval)
126+
{
127+
auto found = cmdmap.find(k);
128+
if (found == cmdmap.cend()) {
129+
return defval;
130+
}
131+
try {
132+
return T(boost::get<CMD_TYPE>(found->second));
110133
} catch (boost::bad_get&) {
111134
throw bad_cmd_get(k, cmdmap);
112135
}

src/osd/OSD.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ using ceph::make_mutex;
193193
using namespace ceph::osd::scheduler;
194194
using TOPNSPC::common::cmd_getval;
195195
using TOPNSPC::common::cmd_getval_or;
196+
using TOPNSPC::common::cmd_getval_cast_or;
196197
using namespace std::literals;
197198

198199
static ostream& _prefix(std::ostream* _dout, int whoami, epoch_t epoch) {
@@ -6582,10 +6583,10 @@ void TestOpsSocketHook::test_ops(OSDService *service, ObjectStore *store,
65826583
return;
65836584
}
65846585

6585-
int64_t shardid64 = cmd_getval_or<int64_t>(cmdmap, "shardid", static_cast<int64_t>(shard_id_t::NO_SHARD));
6586-
shard_id_t shardid = shard_id_t(static_cast<int8_t>(shardid64));
6587-
6588-
hobject_t obj(object_t(objname), string(""), CEPH_NOSNAP, rawpg.ps(), pool, nspace);
6586+
shard_id_t shardid =
6587+
cmd_getval_cast_or<int64_t>(cmdmap, "shardid", shard_id_t::NO_SHARD);
6588+
hobject_t obj(
6589+
object_t(objname), ""s, CEPH_NOSNAP, rawpg.ps(), pool, nspace);
65896590
ghobject_t gobj(obj, ghobject_t::NO_GEN, shardid);
65906591
spg_t pgid(curmap->raw_pg_to_pg(rawpg), shardid);
65916592
if (curmap->pg_is_ec(rawpg)) {

src/test/objectstore/test_bluestore_types.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "os/bluestore/Writer.h"
1717
#include "common/pretty_binary.h"
1818

19+
#include <bitset>
1920
#include <sstream>
2021

2122
#define _STR(x) #x

0 commit comments

Comments
 (0)