]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/services/svc_sys_obj_cache.h
import 15.2.0 Octopus source
[ceph.git] / ceph / src / rgw / services / svc_sys_obj_cache.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
3
4 #pragma once
5
6 #include "rgw/rgw_service.h"
7 #include "rgw/rgw_cache.h"
8
9 #include "svc_sys_obj_core.h"
10
11 class RGWSI_Notify;
12
13 class RGWSI_SysObj_Cache_CB;
14 class RGWSI_SysObj_Cache_ASocketHook;
15
16 class RGWSI_SysObj_Cache : public RGWSI_SysObj_Core
17 {
18 friend class RGWSI_SysObj_Cache_CB;
19 friend class RGWServices_Def;
20 friend class ASocketHandler;
21
22 RGWSI_Notify *notify_svc{nullptr};
23 ObjectCache cache;
24
25 std::shared_ptr<RGWSI_SysObj_Cache_CB> cb;
26
27 void normalize_pool_and_obj(const rgw_pool& src_pool, const string& src_obj, rgw_pool& dst_pool, string& dst_obj);
28 protected:
29 void init(RGWSI_RADOS *_rados_svc,
30 RGWSI_Zone *_zone_svc,
31 RGWSI_Notify *_notify_svc) {
32 core_init(_rados_svc, _zone_svc);
33 notify_svc = _notify_svc;
34 }
35
36 int do_start() override;
37 void shutdown() override;
38
39 int raw_stat(const rgw_raw_obj& obj, uint64_t *psize, real_time *pmtime, uint64_t *epoch,
40 map<string, bufferlist> *attrs, bufferlist *first_chunk,
41 RGWObjVersionTracker *objv_tracker,
42 optional_yield y) override;
43
44 int read(RGWSysObjectCtxBase& obj_ctx,
45 RGWSI_SysObj_Obj_GetObjState& read_state,
46 RGWObjVersionTracker *objv_tracker,
47 const rgw_raw_obj& obj,
48 bufferlist *bl, off_t ofs, off_t end,
49 map<string, bufferlist> *attrs,
50 bool raw_attrs,
51 rgw_cache_entry_info *cache_info,
52 boost::optional<obj_version>,
53 optional_yield y) override;
54
55 int get_attr(const rgw_raw_obj& obj, const char *name, bufferlist *dest,
56 optional_yield y) override;
57
58 int set_attrs(const rgw_raw_obj& obj,
59 map<string, bufferlist>& attrs,
60 map<string, bufferlist> *rmattrs,
61 RGWObjVersionTracker *objv_tracker,
62 optional_yield y);
63
64 int remove(RGWSysObjectCtxBase& obj_ctx,
65 RGWObjVersionTracker *objv_tracker,
66 const rgw_raw_obj& obj,
67 optional_yield y) override;
68
69 int write(const rgw_raw_obj& obj,
70 real_time *pmtime,
71 map<std::string, bufferlist>& attrs,
72 bool exclusive,
73 const bufferlist& data,
74 RGWObjVersionTracker *objv_tracker,
75 real_time set_mtime,
76 optional_yield y) override;
77
78 int write_data(const rgw_raw_obj& obj,
79 const bufferlist& bl,
80 bool exclusive,
81 RGWObjVersionTracker *objv_tracker,
82 optional_yield y);
83
84 int distribute_cache(const string& normal_name, const rgw_raw_obj& obj,
85 ObjectCacheInfo& obj_info, int op,
86 optional_yield y);
87
88 int watch_cb(uint64_t notify_id,
89 uint64_t cookie,
90 uint64_t notifier_id,
91 bufferlist& bl);
92
93 void set_enabled(bool status);
94
95 public:
96 RGWSI_SysObj_Cache(CephContext *cct) : RGWSI_SysObj_Core(cct), asocket(this) {
97 cache.set_ctx(cct);
98 }
99
100 bool chain_cache_entry(std::initializer_list<rgw_cache_entry_info *> cache_info_entries,
101 RGWChainedCache::Entry *chained_entry);
102 void register_chained_cache(RGWChainedCache *cc);
103 void unregister_chained_cache(RGWChainedCache *cc);
104
105 class ASocketHandler {
106 RGWSI_SysObj_Cache *svc;
107
108 std::unique_ptr<RGWSI_SysObj_Cache_ASocketHook> hook;
109
110 public:
111 ASocketHandler(RGWSI_SysObj_Cache *_svc);
112 ~ASocketHandler();
113
114 int start();
115 void shutdown();
116
117 // `call_list` must iterate over all cache entries and call
118 // `cache_list_dump_helper` with the supplied Formatter on any that
119 // include `filter` as a substring.
120 //
121 void call_list(const std::optional<std::string>& filter, Formatter* f);
122
123 // `call_inspect` must look up the requested target and, if found,
124 // dump it to the supplied Formatter and return true. If not found,
125 // it must return false.
126 //
127 int call_inspect(const std::string& target, Formatter* f);
128
129 // `call_erase` must erase the requested target and return true. If
130 // the requested target does not exist, it should return false.
131 int call_erase(const std::string& target);
132
133 // `call_zap` must erase the cache.
134 int call_zap();
135 } asocket;
136 };
137
138 template <class T>
139 class RGWChainedCacheImpl : public RGWChainedCache {
140 RGWSI_SysObj_Cache *svc{nullptr};
141 ceph::timespan expiry;
142 RWLock lock;
143
144 std::unordered_map<std::string, std::pair<T, ceph::coarse_mono_time>> entries;
145
146 public:
147 RGWChainedCacheImpl() : lock("RGWChainedCacheImpl::lock") {}
148 ~RGWChainedCacheImpl() {
149 if (!svc) {
150 return;
151 }
152 svc->unregister_chained_cache(this);
153 }
154
155 void unregistered() override {
156 svc = nullptr;
157 }
158
159 void init(RGWSI_SysObj_Cache *_svc) {
160 if (!_svc) {
161 return;
162 }
163 svc = _svc;
164 svc->register_chained_cache(this);
165 expiry = std::chrono::seconds(svc->ctx()->_conf.get_val<uint64_t>(
166 "rgw_cache_expiry_interval"));
167 }
168
169 boost::optional<T> find(const string& key) {
170 std::shared_lock rl{lock};
171 auto iter = entries.find(key);
172 if (iter == entries.end()) {
173 return boost::none;
174 }
175 if (expiry.count() &&
176 (ceph::coarse_mono_clock::now() - iter->second.second) > expiry) {
177 return boost::none;
178 }
179
180 return iter->second.first;
181 }
182
183 bool put(RGWSI_SysObj_Cache *svc, const string& key, T *entry,
184 std::initializer_list<rgw_cache_entry_info *> cache_info_entries) {
185 if (!svc) {
186 return false;
187 }
188
189 Entry chain_entry(this, key, entry);
190
191 /* we need the svc cache to call us under its lock to maintain lock ordering */
192 return svc->chain_cache_entry(cache_info_entries, &chain_entry);
193 }
194
195 void chain_cb(const string& key, void *data) override {
196 T *entry = static_cast<T *>(data);
197 std::unique_lock wl{lock};
198 entries[key].first = *entry;
199 if (expiry.count() > 0) {
200 entries[key].second = ceph::coarse_mono_clock::now();
201 }
202 }
203
204 void invalidate(const string& key) override {
205 std::unique_lock wl{lock};
206 entries.erase(key);
207 }
208
209 void invalidate_all() override {
210 std::unique_lock wl{lock};
211 entries.clear();
212 }
213 }; /* RGWChainedCacheImpl */