]> git.proxmox.com Git - ceph.git/blame - ceph/src/rocksdb/utilities/fault_injection_secondary_cache.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rocksdb / utilities / fault_injection_secondary_cache.h
CommitLineData
1e59de90
TL
1// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2// This source code is licensed under both the GPLv2 (found in the
3// COPYING file in the root directory) and Apache 2.0 License
4// (found in the LICENSE.Apache file in the root directory).
5
6#include "rocksdb/secondary_cache.h"
7#include "util/random.h"
8#include "util/thread_local.h"
9
10namespace ROCKSDB_NAMESPACE {
11
12// This class implements a custom SecondaryCache that randomly injects an
13// error status into Inserts/Lookups based on a specified probability.
14// Its used by db_stress to verify correctness in the presence of
15// secondary cache errors.
16//
17class FaultInjectionSecondaryCache : public SecondaryCache {
18 public:
19 explicit FaultInjectionSecondaryCache(
20 const std::shared_ptr<SecondaryCache>& base, uint32_t seed, int prob)
21 : base_(base),
22 seed_(seed),
23 prob_(prob),
24 thread_local_error_(new ThreadLocalPtr(DeleteThreadLocalErrorContext)) {
25 if (std::strcmp(base_->Name(), "CompressedSecondaryCache") == 0) {
26 base_is_compressed_sec_cache_ = true;
27 }
28 }
29
30 virtual ~FaultInjectionSecondaryCache() override {}
31
32 const char* Name() const override { return "FaultInjectionSecondaryCache"; }
33
34 Status Insert(const Slice& key, void* value,
35 const Cache::CacheItemHelper* helper) override;
36
37 std::unique_ptr<SecondaryCacheResultHandle> Lookup(
38 const Slice& key, const Cache::CreateCallback& create_cb, bool wait,
39 bool advise_erase, bool& is_in_sec_cache) override;
40
41 bool SupportForceErase() const override { return base_->SupportForceErase(); }
42
43 void Erase(const Slice& key) override;
44
45 void WaitAll(std::vector<SecondaryCacheResultHandle*> handles) override;
46
47 Status SetCapacity(size_t capacity) override {
48 return base_->SetCapacity(capacity);
49 }
50
51 Status GetCapacity(size_t& capacity) override {
52 return base_->GetCapacity(capacity);
53 }
54
55 std::string GetPrintableOptions() const override {
56 return base_->GetPrintableOptions();
57 }
58
59 private:
60 class ResultHandle : public SecondaryCacheResultHandle {
61 public:
62 ResultHandle(FaultInjectionSecondaryCache* cache,
63 std::unique_ptr<SecondaryCacheResultHandle>&& base)
64 : cache_(cache), base_(std::move(base)), value_(nullptr), size_(0) {}
65
66 ~ResultHandle() override {}
67
68 bool IsReady() override;
69
70 void Wait() override;
71
72 void* Value() override;
73
74 size_t Size() override;
75
76 static void WaitAll(FaultInjectionSecondaryCache* cache,
77 std::vector<SecondaryCacheResultHandle*> handles);
78
79 private:
80 static void UpdateHandleValue(ResultHandle* handle);
81
82 FaultInjectionSecondaryCache* cache_;
83 std::unique_ptr<SecondaryCacheResultHandle> base_;
84 void* value_;
85 size_t size_;
86 };
87
88 static void DeleteThreadLocalErrorContext(void* p) {
89 ErrorContext* ctx = static_cast<ErrorContext*>(p);
90 delete ctx;
91 }
92
93 const std::shared_ptr<SecondaryCache> base_;
94 uint32_t seed_;
95 int prob_;
96 bool base_is_compressed_sec_cache_{false};
97
98 struct ErrorContext {
99 Random rand;
100
101 explicit ErrorContext(uint32_t seed) : rand(seed) {}
102 };
103 std::unique_ptr<ThreadLocalPtr> thread_local_error_;
104
105 ErrorContext* GetErrorContext();
106};
107
108} // namespace ROCKSDB_NAMESPACE