Coverage Report

Created: 2024-07-27 06:53

/src/rocksdb/db/blob/blob_file_cache.cc
Line
Count
Source (jump to first uncovered line)
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),
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
38.8k
      io_tracer_(io_tracer) {
34
38.8k
  assert(cache_);
35
38.8k
  assert(immutable_options_);
36
38.8k
  assert(file_options_);
37
38.8k
}
38
39
Status BlobFileCache::GetBlobFileReader(
40
    const ReadOptions& read_options, uint64_t blob_file_number,
41
0
    CacheHandleGuard<BlobFileReader>* blob_file_reader) {
42
0
  assert(blob_file_reader);
43
0
  assert(blob_file_reader->IsEmpty());
44
45
0
  const Slice key = GetSliceForKey(&blob_file_number);
46
47
0
  assert(cache_);
48
49
0
  TypedHandle* handle = cache_.Lookup(key);
50
0
  if (handle) {
51
0
    *blob_file_reader = cache_.Guard(handle);
52
0
    return Status::OK();
53
0
  }
54
55
0
  TEST_SYNC_POINT("BlobFileCache::GetBlobFileReader:DoubleCheck");
56
57
  // Check again while holding mutex
58
0
  MutexLock lock(&mutex_.Get(key));
59
60
0
  handle = cache_.Lookup(key);
61
0
  if (handle) {
62
0
    *blob_file_reader = cache_.Guard(handle);
63
0
    return Status::OK();
64
0
  }
65
66
0
  assert(immutable_options_);
67
0
  Statistics* const statistics = immutable_options_->stats;
68
69
0
  RecordTick(statistics, NO_FILE_OPENS);
70
71
0
  std::unique_ptr<BlobFileReader> reader;
72
73
0
  {
74
0
    assert(file_options_);
75
0
    const Status s = BlobFileReader::Create(
76
0
        *immutable_options_, read_options, *file_options_, column_family_id_,
77
0
        blob_file_read_hist_, blob_file_number, io_tracer_, &reader);
78
0
    if (!s.ok()) {
79
0
      RecordTick(statistics, NO_FILE_ERRORS);
80
0
      return s;
81
0
    }
82
0
  }
83
84
0
  {
85
0
    constexpr size_t charge = 1;
86
87
0
    const Status s = cache_.Insert(key, reader.get(), charge, &handle);
88
0
    if (!s.ok()) {
89
0
      RecordTick(statistics, NO_FILE_ERRORS);
90
0
      return s;
91
0
    }
92
0
  }
93
94
0
  reader.release();
95
96
0
  *blob_file_reader = cache_.Guard(handle);
97
98
0
  return Status::OK();
99
0
}
100
101
}  // namespace ROCKSDB_NAMESPACE