1
0
Fork 0

Compare commits

...

1 Commits

3 changed files with 53 additions and 9 deletions

View File

@ -34,26 +34,22 @@ struct alloc_osd_t
json11::Json::object {
{ "target", "VERSION" },
{ "version", 0 },
{ "key", base64_encode(
parent->cli->st_cli.etcd_prefix+"/osd/stats/"+std::to_string(new_id)
) },
{ "key", "/osd/stats/"+std::to_string(new_id) }
},
} },
{ "success", json11::Json::array {
json11::Json::object {
{ "request_put", json11::Json::object {
{ "key", base64_encode(
parent->cli->st_cli.etcd_prefix+"/osd/stats/"+std::to_string(new_id)
) },
{ "value", base64_encode("{}") },
{ "key", "/osd/stats/"+std::to_string(new_id) }
{ "value", "{}" },
} },
},
} },
{ "failure", json11::Json::array {
json11::Json::object {
{ "request_range", json11::Json::object {
{ "key", base64_encode(parent->cli->st_cli.etcd_prefix+"/osd/stats/") },
{ "range_end", base64_encode(parent->cli->st_cli.etcd_prefix+"/osd/stats0") },
{ "key", "/osd/stats/" },
{ "range_end", "/osd/stats0" },
{ "keys_only", true },
} },
},

View File

@ -54,8 +54,55 @@ etcd_kv_t etcd_state_client_t::parse_etcd_kv(const json11::Json & kv_json)
return kv;
}
json11::Json etcd_state_client_t::etcd_encode_actions(const json11::Json & items)
{
json11::Json::array encoded;
for (auto & v: items.array_items())
{
json11::Json::object act;
for (auto & kv: v.object_items())
{
if (kv.first == "key" || kv.first == "range_end")
act[kv.first] = base64_encode(etcd_prefix+kv.second.string_value());
else if (kv.first == "value")
act[kv.first] = base64_encode(kv.second.is_string() ? kv.second.string_value() : kv.second.dump());
else
act[kv.first] = kv.second;
}
encoded.push_back(act);
}
return encoded;
}
void etcd_state_client_t::etcd_txn(json11::Json txn, int timeout, int retries, int interval, std::function<void(std::string, json11::Json)> callback)
{
// FIXME: json11 is immutable which is very inconvenient for such cases
json11::Json::object encoded;
if (txn["compare"].is_array())
{
json11::Json::array compare;
for (auto & v: txn["compare"].array_items())
{
json11::Json::object cmp;
for (auto & kv: v.object_items())
{
if (kv.first == "key")
cmp[kv.first] = base64_encode(etcd_prefix+kv.second.string_value());
else
cmp[kv.first] = kv.second;
}
compare.push_back(cmp);
}
encoded["compare"] = compare;
}
if (txn["failure"].is_array())
{
encoded["failure"] = etcd_encode_actions(txn["failure"]);
}
if (txn["success"].is_array())
{
encoded["success"] = etcd_encode_actions(txn["success"]);
}
etcd_call("/kv/txn", txn, timeout, retries, interval, callback);
}

View File

@ -93,6 +93,7 @@ protected:
bool rand_initialized = false;
void add_etcd_url(std::string);
void pick_next_etcd();
json11::Json etcd_encode_actions(const json11::Json & items);
public:
int etcd_keepalive_timeout = 30;
int etcd_ws_keepalive_interval = 30;