Skip to content

Commit 976889f

Browse files
committed
RGW/standalone: refactor RGWPeriod with configstore
Signed-off-by: Ali Masarwa <[email protected]>
1 parent df6c185 commit 976889f

File tree

16 files changed

+220
-351
lines changed

16 files changed

+220
-351
lines changed

src/rgw/driver/dbstore/config/sqlite.cc

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,87 @@ int SQLiteConfigStore::delete_period(const DoutPrefixProvider* dpp,
872872
return 0;
873873
}
874874

875+
int SQLiteConfigStore::read_latest_epoch(const DoutPrefixProvider* dpp, optional_yield y, std::string_view period_id,
876+
uint32_t& epoch, RGWObjVersionTracker* objv, RGWPeriod& info)
877+
{
878+
Prefix prefix{*dpp, "dbconfig:sqlite:read_latest_epoch "}; dpp = &prefix;
879+
880+
if (period_id.empty()) {
881+
ldpp_dout(dpp, 0) << "requires a period id" << dendl;
882+
return -EINVAL;
883+
}
884+
885+
try {
886+
auto conn = impl->get(dpp);
887+
period_select_epoch(dpp, *conn, period_id, epoch, info);
888+
} catch (const buffer::error& e) {
889+
ldpp_dout(dpp, 20) << "period decode failed: " << e.what() << dendl;
890+
return -EIO;
891+
} catch (const sqlite::error& e) {
892+
ldpp_dout(dpp, 20) << "period select failed: " << e.what() << dendl;
893+
if (e.code() == sqlite::errc::done) {
894+
return -ENOENT;
895+
} else if (e.code() == sqlite::errc::busy) {
896+
return -EBUSY;
897+
}
898+
return -EIO;
899+
}
900+
return 0;
901+
}
902+
903+
int SQLiteConfigStore::write_latest_epoch(const DoutPrefixProvider* dpp, optional_yield y, bool exclusive,
904+
std::string_view period_id, uint32_t epoch, RGWObjVersionTracker* objv,
905+
const RGWPeriod& info)
906+
{
907+
Prefix prefix{*dpp, "dbconfig:sqlite:write_latest_epoch "}; dpp = &prefix;
908+
909+
if (info.id.empty()) {
910+
ldpp_dout(dpp, 0) << "period cannot have an empty id" << dendl;
911+
return -EINVAL;
912+
}
913+
914+
bufferlist bl;
915+
encode(info, bl);
916+
const auto data = std::string_view{bl.c_str(), bl.length()};
917+
918+
try {
919+
auto conn = impl->get(dpp);
920+
sqlite::stmt_ptr* stmt = nullptr;
921+
if (exclusive) {
922+
stmt = &conn->statements["period_ins"];
923+
if (!*stmt) {
924+
const std::string sql = fmt::format(schema::period_insert4,
925+
P1, P2, P3, P4);
926+
*stmt = sqlite::prepare_statement(dpp, conn->db.get(), sql);
927+
}
928+
} else {
929+
stmt = &conn->statements["period_ups"];
930+
if (!*stmt) {
931+
const std::string sql = fmt::format(schema::period_upsert4,
932+
P1, P2, P3, P4);
933+
*stmt = sqlite::prepare_statement(dpp, conn->db.get(), sql);
934+
}
935+
}
936+
auto binding = sqlite::stmt_binding{stmt->get()};
937+
sqlite::bind_text(dpp, binding, P1, info.id);
938+
sqlite::bind_int(dpp, binding, P2, info.epoch);
939+
sqlite::bind_text(dpp, binding, P3, info.realm_id);
940+
sqlite::bind_text(dpp, binding, P4, data);
941+
942+
auto reset = sqlite::stmt_execution{stmt->get()};
943+
sqlite::eval0(dpp, reset);
944+
} catch (const sqlite::error& e) {
945+
ldpp_dout(dpp, 20) << "period insert failed: " << e.what() << dendl;
946+
if (e.code() == sqlite::errc::foreign_key_constraint) {
947+
return -EINVAL; // refers to nonexistent RealmID
948+
} else if (e.code() == sqlite::errc::busy) {
949+
return -EBUSY;
950+
}
951+
return -EIO;
952+
}
953+
return 0;
954+
}
955+
875956
int SQLiteConfigStore::list_period_ids(const DoutPrefixProvider* dpp,
876957
optional_yield y,
877958
const std::string& marker,

src/rgw/driver/dbstore/config/sqlite.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ class SQLiteConfigStore : public sal::ConfigStore {
7979
optional_yield y, const std::string& marker,
8080
std::span<std::string> entries,
8181
sal::ListResult<std::string>& result) override;
82+
int read_latest_epoch(const DoutPrefixProvider* dpp, optional_yield y, std::string_view period_id,
83+
uint32_t& epoch, RGWObjVersionTracker* objv, RGWPeriod& info) override;
84+
int write_latest_epoch(const DoutPrefixProvider* dpp, optional_yield y, bool exclusive, std::string_view period_id,
85+
uint32_t epoch, RGWObjVersionTracker* objv, const RGWPeriod& info) override;
8286

8387
int write_default_zonegroup_id(const DoutPrefixProvider* dpp,
8488
optional_yield y, bool exclusive,

src/rgw/driver/immutable_config/store.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ int ImmutableConfigStore::list_period_ids(const DoutPrefixProvider* dpp,
137137
return 0;
138138
}
139139

140+
int ImmutableConfigStore::read_latest_epoch(const DoutPrefixProvider* dpp, optional_yield y, std::string_view period_id,
141+
uint32_t& epoch, RGWObjVersionTracker* objv, RGWPeriod& info)
142+
{
143+
return -ENOENT;
144+
}
145+
146+
int ImmutableConfigStore::write_latest_epoch(const DoutPrefixProvider* dpp, optional_yield y, bool exclusive,
147+
std::string_view period_id, uint32_t epoch, RGWObjVersionTracker* objv,
148+
const RGWPeriod& info)
149+
{
150+
return -EROFS;
151+
}
152+
140153

141154
// ZoneGroup
142155

src/rgw/driver/immutable_config/store.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ class ImmutableConfigStore : public ConfigStore {
7878
optional_yield y, const std::string& marker,
7979
std::span<std::string> entries,
8080
ListResult<std::string>& result) override;
81+
virtual int read_latest_epoch(const DoutPrefixProvider* dpp, optional_yield y, std::string_view period_id,
82+
uint32_t& epoch, RGWObjVersionTracker* objv, RGWPeriod& info) override;
83+
virtual int write_latest_epoch(const DoutPrefixProvider* dpp, optional_yield y, bool exclusive, std::string_view period_id,
84+
uint32_t epoch, RGWObjVersionTracker* objv, const RGWPeriod& info) override;
8185

8286
// ZoneGroup
8387
virtual int write_default_zonegroup_id(const DoutPrefixProvider* dpp,

src/rgw/driver/rados/config/period.cc

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ static std::string latest_epoch_oid(const ceph::common::ConfigProxy& conf,
4444
period_latest_epoch_info_oid));
4545
}
4646

47-
static int read_latest_epoch(const DoutPrefixProvider* dpp, optional_yield y,
48-
ConfigImpl* impl, std::string_view period_id,
49-
uint32_t& epoch, RGWObjVersionTracker* objv)
47+
int RadosConfigStore::read_latest_epoch(const DoutPrefixProvider* dpp, optional_yield y, std::string_view period_id,
48+
uint32_t& epoch, RGWObjVersionTracker* objv, RGWPeriod& info)
5049
{
5150
const auto& pool = impl->period_pool;
5251
const auto latest_oid = latest_epoch_oid(dpp->get_cct()->_conf, period_id);
@@ -58,10 +57,9 @@ static int read_latest_epoch(const DoutPrefixProvider* dpp, optional_yield y,
5857
return r;
5958
}
6059

61-
static int write_latest_epoch(const DoutPrefixProvider* dpp, optional_yield y,
62-
ConfigImpl* impl, bool exclusive,
63-
std::string_view period_id, uint32_t epoch,
64-
RGWObjVersionTracker* objv)
60+
int RadosConfigStore::write_latest_epoch(const DoutPrefixProvider* dpp, optional_yield y, bool exclusive,
61+
std::string_view period_id, uint32_t epoch, RGWObjVersionTracker* objv,
62+
const RGWPeriod& info)
6563
{
6664
const auto& pool = impl->period_pool;
6765
const auto latest_oid = latest_epoch_oid(dpp->get_cct()->_conf, period_id);
@@ -80,8 +78,8 @@ static int delete_latest_epoch(const DoutPrefixProvider* dpp, optional_yield y,
8078
}
8179

8280
static int update_latest_epoch(const DoutPrefixProvider* dpp, optional_yield y,
83-
ConfigImpl* impl, std::string_view period_id,
84-
uint32_t epoch)
81+
RadosConfigStore& rados_config_store, std::string_view period_id,
82+
uint32_t epoch, RGWPeriod& info)
8583
{
8684
static constexpr int MAX_RETRIES = 20;
8785

@@ -91,7 +89,7 @@ static int update_latest_epoch(const DoutPrefixProvider* dpp, optional_yield y,
9189
bool exclusive = false;
9290

9391
// read existing epoch
94-
int r = read_latest_epoch(dpp, y, impl, period_id, existing_epoch, &objv);
92+
int r = rados_config_store.read_latest_epoch(dpp, y, period_id, existing_epoch, &objv, info);
9593
if (r == -ENOENT) {
9694
// use an exclusive create to set the epoch atomically
9795
exclusive = true;
@@ -111,7 +109,7 @@ static int update_latest_epoch(const DoutPrefixProvider* dpp, optional_yield y,
111109
<< " -> " << epoch << " on period=" << period_id << dendl;
112110
}
113111

114-
r = write_latest_epoch(dpp, y, impl, exclusive, period_id, epoch, &objv);
112+
r = rados_config_store.write_latest_epoch(dpp, y, exclusive, period_id, epoch, &objv, info);
115113
if (r == -EEXIST) {
116114
continue; // exclusive create raced with another update, retry
117115
} else if (r == -ECANCELED) {
@@ -149,7 +147,9 @@ int RadosConfigStore::create_period(const DoutPrefixProvider* dpp,
149147
return r;
150148
}
151149

152-
(void) update_latest_epoch(dpp, y, impl.get(), info.get_id(), info.get_epoch());
150+
// non const RGWPeriod
151+
RGWPeriod info_copy = info;
152+
(void) update_latest_epoch(dpp, y, *this, info.get_id(), info.get_epoch(), info_copy);
153153
return 0;
154154
}
155155

@@ -162,7 +162,7 @@ int RadosConfigStore::read_period(const DoutPrefixProvider* dpp,
162162
int r = 0;
163163
if (!epoch) {
164164
epoch = 0;
165-
r = read_latest_epoch(dpp, y, impl.get(), period_id, *epoch, nullptr);
165+
r = read_latest_epoch(dpp, y, period_id, *epoch, nullptr, info);
166166
if (r < 0) {
167167
return r;
168168
}
@@ -182,8 +182,8 @@ int RadosConfigStore::delete_period(const DoutPrefixProvider* dpp,
182182
// read the latest_epoch
183183
uint32_t latest_epoch = 0;
184184
RGWObjVersionTracker latest_objv;
185-
int r = read_latest_epoch(dpp, y, impl.get(), period_id,
186-
latest_epoch, &latest_objv);
185+
RGWPeriod period; // not used in RadosConfigStore, but needed in the API
186+
int r = read_latest_epoch(dpp, y, period_id, latest_epoch, &latest_objv, period);
187187
if (r < 0 && r != -ENOENT) { // just delete epoch=0 on ENOENT
188188
ldpp_dout(dpp, 0) << "failed to read latest epoch for period "
189189
<< period_id << ": " << cpp_strerror(r) << dendl;

src/rgw/driver/rados/config/store.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ class RadosConfigStore : public sal::ConfigStore {
8585
optional_yield y, const std::string& marker,
8686
std::span<std::string> entries,
8787
sal::ListResult<std::string>& result) override;
88+
virtual int read_latest_epoch(const DoutPrefixProvider* dpp, optional_yield y, std::string_view period_id,
89+
uint32_t& epoch, RGWObjVersionTracker* objv, RGWPeriod& info) override;
90+
virtual int write_latest_epoch(const DoutPrefixProvider* dpp, optional_yield y, bool exclusive, std::string_view period_id,
91+
uint32_t epoch, RGWObjVersionTracker* objv, const RGWPeriod& info)override;
8892

8993
// ZoneGroup
9094
virtual int write_default_zonegroup_id(const DoutPrefixProvider* dpp,

0 commit comments

Comments
 (0)