]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/db/blob/blob_file_cache.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rocksdb / db / blob / blob_file_cache.cc
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 "db/blob/blob_file_cache.h"
7
8 #include <cassert>
9 #include <memory>
10
11 #include "db/blob/blob_file_reader.h"
12 #include "options/cf_options.h"
13 #include "rocksdb/cache.h"
14 #include "rocksdb/slice.h"
15 #include "test_util/sync_point.h"
16 #include "trace_replay/io_tracer.h"
17 #include "util/hash.h"
18
19 namespace ROCKSDB_NAMESPACE {
20
21 BlobFileCache::BlobFileCache(Cache* cache,
22 const ImmutableOptions* immutable_options,
23 const FileOptions* file_options,
24 uint32_t column_family_id,
25 HistogramImpl* blob_file_read_hist,
26 const std::shared_ptr<IOTracer>& io_tracer)
27 : cache_(cache),
28 mutex_(kNumberOfMutexStripes, kGetSliceNPHash64UnseededFnPtr),
29 immutable_options_(immutable_options),
30 file_options_(file_options),
31 column_family_id_(column_family_id),
32 blob_file_read_hist_(blob_file_read_hist),
33 io_tracer_(io_tracer) {
34 assert(cache_);
35 assert(immutable_options_);
36 assert(file_options_);
37 }
38
39 Status BlobFileCache::GetBlobFileReader(
40 uint64_t blob_file_number,
41 CacheHandleGuard<BlobFileReader>* blob_file_reader) {
42 assert(blob_file_reader);
43 assert(blob_file_reader->IsEmpty());
44
45 const Slice key = GetSlice(&blob_file_number);
46
47 assert(cache_);
48
49 Cache::Handle* handle = cache_->Lookup(key);
50 if (handle) {
51 *blob_file_reader = CacheHandleGuard<BlobFileReader>(cache_, handle);
52 return Status::OK();
53 }
54
55 TEST_SYNC_POINT("BlobFileCache::GetBlobFileReader:DoubleCheck");
56
57 // Check again while holding mutex
58 MutexLock lock(mutex_.get(key));
59
60 handle = cache_->Lookup(key);
61 if (handle) {
62 *blob_file_reader = CacheHandleGuard<BlobFileReader>(cache_, handle);
63 return Status::OK();
64 }
65
66 assert(immutable_options_);
67 Statistics* const statistics = immutable_options_->stats;
68
69 RecordTick(statistics, NO_FILE_OPENS);
70
71 std::unique_ptr<BlobFileReader> reader;
72
73 {
74 assert(file_options_);
75 const Status s = BlobFileReader::Create(
76 *immutable_options_, *file_options_, column_family_id_,
77 blob_file_read_hist_, blob_file_number, io_tracer_, &reader);
78 if (!s.ok()) {
79 RecordTick(statistics, NO_FILE_ERRORS);
80 return s;
81 }
82 }
83
84 {
85 constexpr size_t charge = 1;
86
87 const Status s = cache_->Insert(key, reader.get(), charge,
88 &DeleteCacheEntry<BlobFileReader>, &handle);
89 if (!s.ok()) {
90 RecordTick(statistics, NO_FILE_ERRORS);
91 return s;
92 }
93 }
94
95 reader.release();
96
97 *blob_file_reader = CacheHandleGuard<BlobFileReader>(cache_, handle);
98
99 return Status::OK();
100 }
101
102 } // namespace ROCKSDB_NAMESPACE