]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_bucket_sync_cache.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rgw / rgw_bucket_sync_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 /*
5 * Ceph - scalable distributed file system
6 *
7 * Copyright (C) 2020 Red Hat, Inc
8 *
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.
13 */
14
15 #pragma once
16
17 #include <boost/smart_ptr/intrusive_ref_counter.hpp>
18 #include "common/intrusive_lru.h"
19 #include "rgw_data_sync.h"
20
21 namespace rgw::bucket_sync {
22
23 // per bucket-shard state cached by DataSyncShardCR
24 struct State {
25 // the source bucket shard to sync
26 std::pair<rgw_bucket_shard, std::optional<uint64_t>> key;
27 // current sync obligation being processed by DataSyncSingleEntry
28 std::optional<rgw_data_sync_obligation> obligation;
29 // incremented with each new obligation
30 uint32_t counter = 0;
31 // highest timestamp applied by all sources
32 ceph::real_time progress_timestamp;
33
34 State(const std::pair<rgw_bucket_shard, std::optional<uint64_t>>& key ) noexcept
35 : key(key) {}
36 State(const rgw_bucket_shard& shard, std::optional<uint64_t> gen) noexcept
37 : key(shard, gen) {}
38 };
39
40 struct Entry;
41 struct EntryToKey;
42 class Handle;
43
44 using lru_config = ceph::common::intrusive_lru_config<
45 std::pair<rgw_bucket_shard, std::optional<uint64_t>>, Entry, EntryToKey>;
46
47 // a recyclable cache entry
48 struct Entry : State, ceph::common::intrusive_lru_base<lru_config> {
49 using State::State;
50 };
51
52 struct EntryToKey {
53 using type = std::pair<rgw_bucket_shard, std::optional<uint64_t>>;
54 const type& operator()(const Entry& e) { return e.key; }
55 };
56
57 // use a non-atomic reference count since these aren't shared across threads
58 template <typename T>
59 using thread_unsafe_ref_counter = boost::intrusive_ref_counter<
60 T, boost::thread_unsafe_counter>;
61
62 // a state cache for entries within a single datalog shard
63 class Cache : public thread_unsafe_ref_counter<Cache> {
64 ceph::common::intrusive_lru<lru_config> cache;
65 protected:
66 // protected ctor to enforce the use of factory function create()
67 explicit Cache(size_t target_size) {
68 cache.set_target_size(target_size);
69 }
70 public:
71 static boost::intrusive_ptr<Cache> create(size_t target_size) {
72 return new Cache(target_size);
73 }
74
75 // find or create a cache entry for the given key, and return a Handle that
76 // keeps it lru-pinned until destruction
77 Handle get(const rgw_bucket_shard& shard, std::optional<uint64_t> gen);
78 };
79
80 // a State handle that keeps the Cache referenced
81 class Handle {
82 boost::intrusive_ptr<Cache> cache;
83 boost::intrusive_ptr<Entry> entry;
84 public:
85 Handle() noexcept = default;
86 ~Handle() = default;
87 Handle(boost::intrusive_ptr<Cache> cache,
88 boost::intrusive_ptr<Entry> entry) noexcept
89 : cache(std::move(cache)), entry(std::move(entry)) {}
90 Handle(Handle&&) = default;
91 Handle(const Handle&) = default;
92 Handle& operator=(Handle&& o) noexcept {
93 // move the entry first so that its cache stays referenced over destruction
94 entry = std::move(o.entry);
95 cache = std::move(o.cache);
96 return *this;
97 }
98 Handle& operator=(const Handle& o) noexcept {
99 // copy the entry first so that its cache stays referenced over destruction
100 entry = o.entry;
101 cache = o.cache;
102 return *this;
103 }
104
105 explicit operator bool() const noexcept { return static_cast<bool>(entry); }
106 State& operator*() const noexcept { return *entry; }
107 State* operator->() const noexcept { return entry.get(); }
108 };
109
110 inline Handle Cache::get(const rgw_bucket_shard& shard, std::optional<uint64_t> gen)
111 {
112 auto result = cache.get_or_create({ shard, gen });
113 return {this, std::move(result.first)};
114 }
115
116 } // namespace rgw::bucket_sync