1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
5 * Ceph - scalable distributed file system
7 * Copyright (C) 2019 Red Hat, Inc.
9 * This is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License version 2.1, as published by the Free Software
12 * Foundation. See file COPYING.
19 #include "common/RWLock.h"
21 #include "rgw_metadata.h"
22 #include "rgw_mdlog_types.h"
24 #include "services/svc_rados.h"
26 #define META_LOG_OBJ_PREFIX "meta.log."
28 struct RGWMetadataLogInfo
{
30 real_time last_update
;
32 void dump(Formatter
*f
) const;
33 void decode_json(JSONObj
*obj
);
36 class RGWCompletionManager
;
38 class RGWMetadataLogInfoCompletion
: public RefCountedObject
{
40 using info_callback_t
= std::function
<void(int, const cls_log_header
&)>;
42 cls_log_header header
;
43 RGWSI_RADOS::Obj io_obj
;
44 librados::AioCompletion
*completion
;
45 std::mutex mutex
; //< protects callback between cancel/complete
46 boost::optional
<info_callback_t
> callback
; //< cleared on cancel
48 explicit RGWMetadataLogInfoCompletion(info_callback_t callback
);
49 ~RGWMetadataLogInfoCompletion() override
;
51 RGWSI_RADOS::Obj
& get_io_obj() { return io_obj
; }
52 cls_log_header
& get_header() { return header
; }
53 librados::AioCompletion
* get_completion() { return completion
; }
55 void finish(librados::completion_t cb
) {
56 std::lock_guard
<std::mutex
> lock(mutex
);
58 (*callback
)(completion
->get_return_value(), header
);
62 std::lock_guard
<std::mutex
> lock(mutex
);
63 callback
= boost::none
;
67 class RGWMetadataLog
{
69 const std::string prefix
;
72 RGWSI_Zone
*zone
{nullptr};
73 RGWSI_Cls
*cls
{nullptr};
76 static std::string
make_prefix(const std::string
& period
) {
78 return META_LOG_OBJ_PREFIX
;
79 return META_LOG_OBJ_PREFIX
+ period
+ ".";
83 std::set
<int> modified_shards
;
85 void mark_modified(int shard_id
);
87 RGWMetadataLog(CephContext
*_cct
,
88 RGWSI_Zone
*_zone_svc
,
90 const std::string
& period
)
92 prefix(make_prefix(period
)),
93 lock("RGWMetaLog::lock") {
99 void get_shard_oid(int id
, std::string
& oid
) const {
101 snprintf(buf
, sizeof(buf
), "%d", id
);
105 int add_entry(const DoutPrefixProvider
*dpp
, const std::string
& hash_key
, const std::string
& section
, const std::string
& key
, bufferlist
& bl
);
106 int get_shard_id(const std::string
& hash_key
, int *shard_id
);
107 int store_entries_in_shard(const DoutPrefixProvider
*dpp
, std::list
<cls_log_entry
>& entries
, int shard_id
, librados::AioCompletion
*completion
);
119 LogListCtx() : cur_shard(0), done(false) {}
122 void init_list_entries(int shard_id
, const real_time
& from_time
,
123 const real_time
& end_time
, const std::string
& marker
,
125 void complete_list_entries(void *handle
);
126 int list_entries(const DoutPrefixProvider
*dpp
,
129 std::list
<cls_log_entry
>& entries
,
130 std::string
*out_marker
,
133 int trim(const DoutPrefixProvider
*dpp
, int shard_id
, const real_time
& from_time
, const real_time
& end_time
, const std::string
& start_marker
, const std::string
& end_marker
);
134 int get_info(const DoutPrefixProvider
*dpp
, int shard_id
, RGWMetadataLogInfo
*info
);
135 int get_info_async(const DoutPrefixProvider
*dpp
, int shard_id
, RGWMetadataLogInfoCompletion
*completion
);
136 int lock_exclusive(const DoutPrefixProvider
*dpp
, int shard_id
, timespan duration
, std::string
&zone_id
, std::string
& owner_id
);
137 int unlock(const DoutPrefixProvider
*dpp
, int shard_id
, std::string
& zone_id
, std::string
& owner_id
);
139 int update_shards(std::list
<int>& shards
);
141 void read_clear_modified(std::set
<int> &modified
);
144 struct LogStatusDump
{
145 RGWMDLogStatus status
;
147 explicit LogStatusDump(RGWMDLogStatus _status
) : status(_status
) {}
148 void dump(Formatter
*f
) const;
151 struct RGWMetadataLogData
{
152 obj_version read_version
;
153 obj_version write_version
;
154 RGWMDLogStatus status
;
156 RGWMetadataLogData() : status(MDLOG_STATUS_UNKNOWN
) {}
158 void encode(bufferlist
& bl
) const;
159 void decode(bufferlist::const_iterator
& bl
);
160 void dump(Formatter
*f
) const;
161 void decode_json(JSONObj
*obj
);
163 WRITE_CLASS_ENCODER(RGWMetadataLogData
)
165 struct RGWMetadataLogHistory
{
166 epoch_t oldest_realm_epoch
;
167 std::string oldest_period_id
;
169 void encode(bufferlist
& bl
) const {
170 ENCODE_START(1, 1, bl
);
171 encode(oldest_realm_epoch
, bl
);
172 encode(oldest_period_id
, bl
);
175 void decode(bufferlist::const_iterator
& p
) {
177 decode(oldest_realm_epoch
, p
);
178 decode(oldest_period_id
, p
);
182 static const std::string oid
;
184 WRITE_CLASS_ENCODER(RGWMetadataLogHistory
)