osd: don't let a timed-out PG state report overwrite a newer one #183

Open
koolkhel wants to merge 2 commits from koolkhel/vitastor:pg-state-stale-txn into master
4 changed files with 120 additions and 5 deletions

View File

@@ -150,6 +150,7 @@ void etcd_mock_t::etcd_call(const std::string & api, json11::Json payload, std::
{ "key", base64_encode(it->first) },
{ "value", base64_encode(it->second.value) },
{ "mod_revision", it->second.mod_revision },
{ "lease", std::to_string(it->second.lease_id) },
});
}
responses.push_back(json11::Json::object {

View File

@@ -1042,6 +1042,10 @@ struct reporting_pg_t
{
pool_pg_num_t pool_pg_num;
bool history_changed;
// The failure branch reads the state key back
bool state_read;
// 0 = the state key must be absent, else ours with mod_revision below this
uint64_t state_mod_less;
};
void osd_t::report_pg_states()
@@ -1066,7 +1070,7 @@ void osd_t::report_pg_states()
continue;
}
auto & pg = pg_it->second;
reporting_pgs.push_back((reporting_pg_t){ *it, pg.history_changed });
reporting_pgs.push_back((reporting_pg_t){ *it, pg.history_changed, false, 0 });
std::string state_key_base64 = base64_encode(st_cli->etcd_prefix+"/pg/state/"+std::to_string(pg.pool_id)+"/"+std::to_string(pg.pg_num));
bool pg_state_exists = false;
if (pg.state != PG_STARTING)
@@ -1111,7 +1115,17 @@ void osd_t::report_pg_states()
{ "lease", etcd_lease_id },
{ "key", state_key_base64 },
});
// ...and wasn't changed after our last write: a report that timed out may still
// be applied by etcd after a newer one and must not overwrite or delete it then
reporting_pgs.back().state_mod_less = std::max(st_cli->etcd_watch_revision_pg, pg.reported_state_rev)+1;
checks.push_back(json11::Json::object {
{ "target", "MOD" },
{ "key", state_key_base64 },
{ "result", "LESS" },
{ "mod_revision", reporting_pgs.back().state_mod_less },
});
}
reporting_pgs.back().state_read = true;
if (pg.state == PG_OFFLINE)
{
success.push_back(json11::Json::object {
@@ -1189,6 +1203,31 @@ void osd_t::report_pg_states()
rpgnames += (rpgnames.size() ? ", " : "")+std::to_string(pp.pool_pg_num.pool_id)+"/"+std::to_string(pp.pool_pg_num.pg_num);
}
printf("Error reporting PG %s states, will repeat the attempt: %s\n", rpgnames.c_str(), err.c_str());
if (err == "")
{
// A compare failed: print the state keys that failed it
bool found = false;
size_t resp_idx = 0;
for (auto & pp: reporting_pgs)
{
if (!pp.state_read)
continue;
auto & res = data["responses"][resp_idx++]["response_range"]["kvs"][0];
bool ok = res.is_null() ? !pp.state_mod_less : (pp.state_mod_less &&
res["lease"].string_value() == etcd_lease_id && res["mod_revision"].uint64_value() < pp.state_mod_less);
if (!ok)
{
found = true;
printf("PG %u/%u state in etcd: %s (lease %s, mod_revision %ju), expected %s\n",
pp.pool_pg_num.pool_id, pp.pool_pg_num.pg_num,
res.is_null() ? "none" : st_cli->parse_etcd_kv(res).value.dump().c_str(),
res["lease"].string_value().c_str(), res["mod_revision"].uint64_value(),
(pp.state_mod_less ? "our lease and mod_revision < "+std::to_string(pp.state_mod_less) : std::string("none")).c_str());
}
}
if (!found)
printf("PG state keys are as expected, a /pg/history or takeover check failed\n");
}
// One of PG state updates failed, put dirty flags back
for (auto pp: reporting_pgs)
{
@@ -1239,6 +1278,10 @@ void osd_t::report_pg_states()
for (auto pp: reporting_pgs)
{
auto pg_it = this->pgs.find(pp.pool_pg_num);
if (pg_it != this->pgs.end())
{
pg_it->second.reported_state_rev = data["header"]["revision"].uint64_value();
}
if (pg_it != this->pgs.end() &&
pg_state_dirty.find(pp.pool_pg_num) == pg_state_dirty.end())
{

View File

@@ -106,6 +106,8 @@ struct pg_t
bool disable_pg_locks = false;
// epoch number - should increase with each non-clean activation of the PG
uint64_t epoch = 0, reported_epoch = 0, reporting_epoch = 0;
// etcd revision of our last successful write of the PG state key
uint64_t reported_state_rev = 0;
// target history and all potential peers
std::vector<std::vector<osd_num_t>> target_history;
std::vector<osd_num_t> all_peers;

View File

@@ -4443,13 +4443,18 @@ static void start_ec_all(osd_test_fixture_t & f, ec_scheme_t ec, osd_num_t prima
});
}
static json11::Json etcd_pg_history(osd_test_fixture_t & f)
static json11::Json etcd_json(osd_test_fixture_t & f, const std::string & key)
{
auto hist_it = f.etcd_store.data.find("/vitastor/pg/history/1/1");
if (hist_it == f.etcd_store.data.end())
auto it = f.etcd_store.data.find(key);
if (it == f.etcd_store.data.end())
return json11::Json();
std::string json_err;
return json11::Json::parse(hist_it->second.value, json_err);
return json11::Json::parse(it->second.value, json_err);
}
static json11::Json etcd_pg_history(osd_test_fixture_t & f)
{
return etcd_json(f, "/vitastor/pg/history/1/1");
}
static bool has_history_set(const json11::Json & history, json11::Json::array set)
@@ -5445,6 +5450,69 @@ void test_chained_read_through_incomplete_parent()
printf("test_chained_read_through_incomplete_parent passed\n");
}
static json11::Json etcd_pg_state(osd_test_fixture_t & f)
{
return etcd_json(f, "/vitastor/pg/state/1/1");
}
// A PG state report that timed out on the client may still be applied by etcd after
// the newer report sent by the retry. Its compare must fail then instead of putting
// the old state back (or deleting the key, for an "offline" report)
void test_stale_pg_state_report()
{
printf("test_stale_pg_state_report\n");
osd_test_fixture_t f;
f.configure_replicated_pool(/*pool_id*/ 1, /*pg_size*/ 2, /*pg_minsize*/ 1, /*pg_count*/ 1,
{ { 1, 2 } });
f.start(json11::Json::object {
{ "osd_num", 1 },
{ "etcd_address", "127.0.0.1:2379" },
{ "immediate_commit", "all" },
{ "block_size", 131072 },
{ "bitmap_granularity", 4096 },
});
f.connect_peer(2);
f.complete_peering_empty();
f.pump();
assert(f.pg(1, 1).state == PG_ACTIVE);
assert(etcd_pg_state(f)["state"] == json11::Json::array { "active" });
// Re-peer. etcd stalls on the "peering" report and the client times out
f.st_cli->pause();
f.repeer_pgs(2);
assert(f.pg(1, 1).state == PG_PEERING);
assert(f.st_cli->queue.size() == 1);
auto stale = f.st_cli->queue[0];
f.st_cli->queue.clear();
f.st_cli->resume();
stale.callback("HTTP request timed out", json11::Json());
// The PG gets active again and the retry reports it
f.complete_peering_empty();
f.pump();
assert(f.pg(1, 1).state == PG_ACTIVE);
assert(etcd_pg_state(f)["state"] == json11::Json::array { "active" });
// Now etcd applies the old report
f.etcd_store.etcd_call(stale.api, stale.payload, [](std::string err, json11::Json res)
{
assert(err == "" && !res["succeeded"].bool_value());
});
f.pump();
assert(etcd_pg_state(f)["state"] == json11::Json::array { "active" });
// A write the watch hasn't delivered yet fails the next report's compare;
// the OSD prints the state key and keeps the PG dirty
f.etcd_store.data["/vitastor/pg/state/1/1"].mod_revision = ++f.etcd_store.mod_revision;
f.repeer_pgs(2);
f.pump();
assert(f.pg(1, 1).state == PG_PEERING);
assert(etcd_pg_state(f)["state"] == json11::Json::array { "active" });
printf("test_stale_pg_state_report passed\n");
}
int main(int narg, char *args[])
{
test_load_global_config();
@@ -5525,5 +5593,6 @@ int main(int narg, char *args[])
test_tombstone_replicated_roles();
test_tombstone_inline_misplaced();
test_tombstone_forget_counters();
test_stale_pg_state_report();
return 0;
}