]> git.proxmox.com Git - ceph.git/blame - 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
CommitLineData
20effc67
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 "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"
1e59de90 16#include "trace_replay/io_tracer.h"
20effc67
TL
17#include "util/hash.h"
18
19namespace ROCKSDB_NAMESPACE {
20
21BlobFileCache::BlobFileCache(Cache* cache,
1e59de90 22 const ImmutableOptions* immutable_options,
20effc67
TL
23 const FileOptions* file_options,
24 uint32_t column_family_id,
1e59de90
TL
25 HistogramImpl* blob_file_read_hist,
26 const std::shared_ptr<IOTracer>& io_tracer)
20effc67 27 : cache_(cache),
1e59de90
TL
28 mutex_(kNumberOfMutexStripes, kGetSliceNPHash64UnseededFnPtr),
29 immutable_options_(immutable_options),
20effc67
TL
30 file_options_(file_options),
31 column_family_id_(column_family_id),
1e59de90
TL
32 blob_file_read_hist_(blob_file_read_hist),
33 io_tracer_(io_tracer) {
20effc67 34 assert(cache_);
1e59de90 35 assert(immutable_options_);
20effc67
TL
36 assert(file_options_);
37}
38
39Status 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
1e59de90
TL
66 assert(immutable_options_);
67 Statistics* const statistics = immutable_options_->stats;
20effc67
TL
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(
1e59de90
TL
76 *immutable_options_, *file_options_, column_family_id_,
77 blob_file_read_hist_, blob_file_number, io_tracer_, &reader);
20effc67
TL
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