1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp
#include "svc_mdlog.h"
#include "svc_rados.h"
#include "svc_zone.h"
#include "svc_sys_obj.h"
#include "rgw/rgw_tools.h"
#include "rgw/rgw_mdlog.h"
#include "rgw/rgw_coroutine.h"
#include "rgw/rgw_cr_rados.h"
#include "rgw/rgw_zone.h"
#include "common/errno.h"
#include <boost/asio/yield.hpp>
#define dout_subsys ceph_subsys_rgw
using Svc = RGWSI_MDLog::Svc;
using Cursor = RGWPeriodHistory::Cursor;
RGWSI_MDLog::RGWSI_MDLog(CephContext *cct, bool _run_sync) : RGWServiceInstance(cct), run_sync(_run_sync) {
}
RGWSI_MDLog::~RGWSI_MDLog() {
}
int RGWSI_MDLog::init(RGWSI_RADOS *_rados_svc, RGWSI_Zone *_zone_svc, RGWSI_SysObj *_sysobj_svc, RGWSI_Cls *_cls_svc)
{
svc.zone = _zone_svc;
svc.sysobj = _sysobj_svc;
svc.mdlog = this;
svc.rados = _rados_svc;
svc.cls = _cls_svc;
return 0;
}
int RGWSI_MDLog::do_start(optional_yield y, const DoutPrefixProvider *dpp)
{
auto& current_period = svc.zone->get_current_period();
current_log = get_log(current_period.get_id());
period_puller.reset(new RGWPeriodPuller(svc.zone, svc.sysobj));
period_history.reset(new RGWPeriodHistory(cct, period_puller.get(),
current_period));
if (run_sync &&
svc.zone->need_to_sync()) {
// initialize the log period history
svc.mdlog->init_oldest_log_period(y, dpp);
}
return 0;
}
int RGWSI_MDLog::read_history(RGWMetadataLogHistory *state,
RGWObjVersionTracker *objv_tracker,
optional_yield y,
const DoutPrefixProvider *dpp) const
{
auto obj_ctx = svc.sysobj->init_obj_ctx();
auto& pool = svc.zone->get_zone_params().log_pool;
const auto& oid = RGWMetadataLogHistory::oid;
bufferlist bl;
int ret = rgw_get_system_obj(obj_ctx, pool, oid, bl, objv_tracker, nullptr, y, dpp);
if (ret < 0) {
return ret;
}
if (bl.length() == 0) {
/* bad history object, remove it */
rgw_raw_obj obj(pool, oid);
auto sysobj = obj_ctx.get_obj(obj);
ret = sysobj.wop().remove(dpp, y);
if (ret < 0) {
ldpp_dout(dpp, 0) << "ERROR: meta history is empty, but cannot remove it (" << cpp_strerror(-ret) << ")" << dendl;
return ret;
}
return -ENOENT;
}
try {
auto p = bl.cbegin();
state->decode(p);
} catch (buffer::error& e) {
ldpp_dout(dpp, 1) << "failed to decode the mdlog history: "
<< e.what() << dendl;
return -EIO;
}
return 0;
}
int RGWSI_MDLog::write_history(const DoutPrefixProvider *dpp,
const RGWMetadataLogHistory& state,
RGWObjVersionTracker *objv_tracker,
optional_yield y, bool exclusive)
{
bufferlist bl;
state.encode(bl);
auto& pool = svc.zone->get_zone_params().log_pool;
const auto& oid = RGWMetadataLogHistory::oid;
auto obj_ctx = svc.sysobj->init_obj_ctx();
return rgw_put_system_obj(dpp, obj_ctx, pool, oid, bl,
exclusive, objv_tracker, real_time{}, y);
}
namespace mdlog {
using Cursor = RGWPeriodHistory::Cursor;
/// read the mdlog history and use it to initialize the given cursor
class ReadHistoryCR : public RGWCoroutine {
const DoutPrefixProvider *dpp;
Svc svc;
Cursor *cursor;
RGWObjVersionTracker *objv_tracker;
RGWMetadataLogHistory state;
RGWAsyncRadosProcessor *async_processor;
public:
ReadHistoryCR(const DoutPrefixProvider *dpp,
const Svc& svc,
Cursor *cursor,
RGWObjVersionTracker *objv_tracker)
: RGWCoroutine(svc.zone->ctx()), dpp(dpp), svc(svc),
cursor(cursor),
objv_tracker(objv_tracker),
async_processor(svc.rados->get_async_processor())
{}
int operate(const DoutPrefixProvider *dpp) {
reenter(this) {
yield {
rgw_raw_obj obj{svc.zone->get_zone_params().log_pool,
RGWMetadataLogHistory::oid};
constexpr bool empty_on_enoent = false;
using ReadCR = RGWSimpleRadosReadCR<RGWMetadataLogHistory>;
call(new ReadCR(dpp, async_processor, svc.sysobj, obj,
&state, empty_on_enoent, objv_tracker));
}
if (retcode < 0) {
ldpp_dout(dpp, 1) << "failed to read mdlog history: "
<< cpp_strerror(retcode) << dendl;
return set_cr_error(retcode);
}
*cursor = svc.mdlog->period_history->lookup(state.oldest_realm_epoch);
if (!*cursor) {
return set_cr_error(cursor->get_error());
}
ldpp_dout(dpp, 10) << "read mdlog history with oldest period id="
<< state.oldest_period_id << " realm_epoch="
<< state.oldest_realm_epoch << dendl;
return set_cr_done();
}
return 0;
}
};
/// write the given cursor to the mdlog history
class WriteHistoryCR : public RGWCoroutine {
const DoutPrefixProvider *dpp;
Svc svc;
Cursor cursor;
RGWObjVersionTracker *objv;
RGWMetadataLogHistory state;
RGWAsyncRadosProcessor *async_processor;
public:
WriteHistoryCR(const DoutPrefixProvider *dpp,
Svc& svc,
const Cursor& cursor,
RGWObjVersionTracker *objv)
: RGWCoroutine(svc.zone->ctx()), dpp(dpp), svc(svc),
cursor(cursor), objv(objv),
async_processor(svc.rados->get_async_processor())
{}
int operate(const DoutPrefixProvider *dpp) {
reenter(this) {
state.oldest_period_id = cursor.get_period().get_id();
state.oldest_realm_epoch = cursor.get_epoch();
yield {
rgw_raw_obj obj{svc.zone->get_zone_params().log_pool,
RGWMetadataLogHistory::oid};
using WriteCR = RGWSimpleRadosWriteCR<RGWMetadataLogHistory>;
call(new WriteCR(dpp, async_processor, svc.sysobj, obj, state, objv));
}
if (retcode < 0) {
ldpp_dout(dpp, 1) << "failed to write mdlog history: "
<< cpp_strerror(retcode) << dendl;
return set_cr_error(retcode);
}
ldpp_dout(dpp, 10) << "wrote mdlog history with oldest period id="
<< state.oldest_period_id << " realm_epoch="
<< state.oldest_realm_epoch << dendl;
return set_cr_done();
}
return 0;
}
};
/// update the mdlog history to reflect trimmed logs
class TrimHistoryCR : public RGWCoroutine {
const DoutPrefixProvider *dpp;
Svc svc;
const Cursor cursor; //< cursor to trimmed period
RGWObjVersionTracker *objv; //< to prevent racing updates
Cursor next; //< target cursor for oldest log period
Cursor existing; //< existing cursor read from disk
public:
TrimHistoryCR(const DoutPrefixProvider *dpp, const Svc& svc, Cursor cursor, RGWObjVersionTracker *objv)
: RGWCoroutine(svc.zone->ctx()), dpp(dpp), svc(svc),
cursor(cursor), objv(objv), next(cursor) {
next.next(); // advance past cursor
}
int operate(const DoutPrefixProvider *dpp) {
reenter(this) {
// read an existing history, and write the new history if it's newer
yield call(new ReadHistoryCR(dpp, svc, &existing, objv));
if (retcode < 0) {
return set_cr_error(retcode);
}
// reject older trims with ECANCELED
if (cursor.get_epoch() < existing.get_epoch()) {
ldpp_dout(dpp, 4) << "found oldest log epoch=" << existing.get_epoch()
<< ", rejecting trim at epoch=" << cursor.get_epoch() << dendl;
return set_cr_error(-ECANCELED);
}
// overwrite with updated history
yield call(new WriteHistoryCR(dpp, svc, next, objv));
if (retcode < 0) {
return set_cr_error(retcode);
}
return set_cr_done();
}
return 0;
}
};
} // mdlog namespace
// traverse all the way back to the beginning of the period history, and
// return a cursor to the first period in a fully attached history
Cursor RGWSI_MDLog::find_oldest_period(const DoutPrefixProvider *dpp, optional_yield y)
{
auto cursor = period_history->get_current();
while (cursor) {
// advance to the period's predecessor
if (!cursor.has_prev()) {
auto& predecessor = cursor.get_period().get_predecessor();
if (predecessor.empty()) {
// this is the first period, so our logs must start here
ldpp_dout(dpp, 10) << "find_oldest_period returning first "
"period " << cursor.get_period().get_id() << dendl;
return cursor;
}
// pull the predecessor and add it to our history
RGWPeriod period;
int r = period_puller->pull(dpp, predecessor, period, y);
if (r < 0) {
return cursor;
}
auto prev = period_history->insert(std::move(period));
if (!prev) {
return prev;
}
ldpp_dout(dpp, 20) << "find_oldest_period advancing to "
"predecessor period " << predecessor << dendl;
ceph_assert(cursor.has_prev());
}
cursor.prev();
}
ldpp_dout(dpp, 10) << "find_oldest_period returning empty cursor" << dendl;
return cursor;
}
Cursor RGWSI_MDLog::init_oldest_log_period(optional_yield y, const DoutPrefixProvider *dpp)
{
// read the mdlog history
RGWMetadataLogHistory state;
RGWObjVersionTracker objv;
int ret = read_history(&state, &objv, y, dpp);
if (ret == -ENOENT) {
// initialize the mdlog history and write it
ldpp_dout(dpp, 10) << "initializing mdlog history" << dendl;
auto cursor = find_oldest_period(dpp, y);
if (!cursor) {
return cursor;
}
// write the initial history
state.oldest_realm_epoch = cursor.get_epoch();
state.oldest_period_id = cursor.get_period().get_id();
constexpr bool exclusive = true; // don't overwrite
int ret = write_history(dpp, state, &objv, y, exclusive);
if (ret < 0 && ret != -EEXIST) {
ldpp_dout(dpp, 1) << "failed to write mdlog history: "
<< cpp_strerror(ret) << dendl;
return Cursor{ret};
}
return cursor;
} else if (ret < 0) {
ldpp_dout(dpp, 1) << "failed to read mdlog history: "
<< cpp_strerror(ret) << dendl;
return Cursor{ret};
}
// if it's already in the history, return it
auto cursor = period_history->lookup(state.oldest_realm_epoch);
if (cursor) {
return cursor;
} else {
cursor = find_oldest_period(dpp, y);
state.oldest_realm_epoch = cursor.get_epoch();
state.oldest_period_id = cursor.get_period().get_id();
ldpp_dout(dpp, 10) << "rewriting mdlog history" << dendl;
ret = write_history(dpp, state, &objv, y);
if (ret < 0 && ret != -ECANCELED) {
ldpp_dout(dpp, 1) << "failed to write mdlog history: "
<< cpp_strerror(ret) << dendl;
return Cursor{ret};
}
return cursor;
}
// pull the oldest period by id
RGWPeriod period;
ret = period_puller->pull(dpp, state.oldest_period_id, period, y);
if (ret < 0) {
ldpp_dout(dpp, 1) << "failed to read period id=" << state.oldest_period_id
<< " for mdlog history: " << cpp_strerror(ret) << dendl;
return Cursor{ret};
}
// verify its realm_epoch
if (period.get_realm_epoch() != state.oldest_realm_epoch) {
ldpp_dout(dpp, 1) << "inconsistent mdlog history: read period id="
<< period.get_id() << " with realm_epoch=" << period.get_realm_epoch()
<< ", expected realm_epoch=" << state.oldest_realm_epoch << dendl;
return Cursor{-EINVAL};
}
// attach the period to our history
return period_history->attach(dpp, std::move(period), y);
}
Cursor RGWSI_MDLog::read_oldest_log_period(optional_yield y, const DoutPrefixProvider *dpp) const
{
RGWMetadataLogHistory state;
int ret = read_history(&state, nullptr, y, dpp);
if (ret < 0) {
ldpp_dout(dpp, 1) << "failed to read mdlog history: "
<< cpp_strerror(ret) << dendl;
return Cursor{ret};
}
ldpp_dout(dpp, 10) << "read mdlog history with oldest period id="
<< state.oldest_period_id << " realm_epoch="
<< state.oldest_realm_epoch << dendl;
return period_history->lookup(state.oldest_realm_epoch);
}
RGWCoroutine* RGWSI_MDLog::read_oldest_log_period_cr(const DoutPrefixProvider *dpp,
Cursor *period, RGWObjVersionTracker *objv) const
{
return new mdlog::ReadHistoryCR(dpp, svc, period, objv);
}
RGWCoroutine* RGWSI_MDLog::trim_log_period_cr(const DoutPrefixProvider *dpp,
Cursor period, RGWObjVersionTracker *objv) const
{
return new mdlog::TrimHistoryCR(dpp, svc, period, objv);
}
RGWMetadataLog* RGWSI_MDLog::get_log(const std::string& period)
{
// construct the period's log in place if it doesn't exist
auto insert = md_logs.emplace(std::piecewise_construct,
std::forward_as_tuple(period),
std::forward_as_tuple(cct, svc.zone, svc.cls, period));
return &insert.first->second;
}
int RGWSI_MDLog::add_entry(const DoutPrefixProvider *dpp, const string& hash_key, const string& section, const string& key, bufferlist& bl)
{
ceph_assert(current_log); // must have called init()
return current_log->add_entry(dpp, hash_key, section, key, bl);
}
int RGWSI_MDLog::get_shard_id(const string& hash_key, int *shard_id)
{
ceph_assert(current_log); // must have called init()
return current_log->get_shard_id(hash_key, shard_id);
}
int RGWSI_MDLog::pull_period(const DoutPrefixProvider *dpp, const std::string& period_id, RGWPeriod& period,
optional_yield y)
{
return period_puller->pull(dpp, period_id, period, y);
}
|