YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/db/table_cache.h
Line
Count
Source
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under the BSD-style license found in the
3
//  LICENSE file in the root directory of this source tree. An additional grant
4
//  of patent rights can be found in the PATENTS file in the same directory.
5
//
6
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
21
// Use of this source code is governed by a BSD-style license that can be
22
// found in the LICENSE file. See the AUTHORS file for names of contributors.
23
//
24
// Thread-safe (provides internal synchronization)
25
#ifndef YB_ROCKSDB_DB_TABLE_CACHE_H
26
#define YB_ROCKSDB_DB_TABLE_CACHE_H
27
28
#pragma once
29
30
#include <stdint.h>
31
32
#include <string>
33
#include <vector>
34
35
#include "yb/rocksdb/cache.h"
36
#include "yb/rocksdb/env.h"
37
#include "yb/rocksdb/immutable_options.h"
38
#include "yb/rocksdb/options.h"
39
40
namespace rocksdb {
41
42
class Env;
43
class Arena;
44
struct FileDescriptor;
45
class GetContext;
46
class HistogramImpl;
47
class InternalIterator;
48
49
class TableCache {
50
 public:
51
  TableCache(const ImmutableCFOptions& ioptions,
52
             const EnvOptions& storage_options, Cache* cache);
53
  ~TableCache();
54
55
  struct TableReaderWithHandle {
56
    TableReader* table_reader = nullptr;
57
    Cache::Handle* handle = nullptr;
58
    Cache* cache = nullptr;
59
    bool created_new = false;
60
61
12.8M
    TableReaderWithHandle() = default;
62
    TableReaderWithHandle(TableReaderWithHandle&& rhs);
63
    TableReaderWithHandle& operator=(TableReaderWithHandle&& rhs);
64
    ~TableReaderWithHandle();
65
66
    void Reset();
67
    void Release();
68
  };
69
70
  // Return an iterator for the specified file number (the corresponding
71
  // file length must be exactly "total_file_size" bytes).  If "tableptr" is
72
  // non-nullptr, also sets "*tableptr" to point to the Table object
73
  // underlying the returned iterator, or nullptr if no Table object underlies
74
  // the returned iterator.  The returned "*tableptr" object is owned by
75
  // the cache and should not be deleted, and is valid for as long as the
76
  // returned iterator is live.
77
  // If ioptions_.iterator_replacer is specified - it will receive `filter` as an argument.
78
  // @param skip_filters Disables loading/accessing the filter block
79
  InternalIterator* NewIterator(
80
      const ReadOptions& options, const EnvOptions& toptions,
81
      const InternalKeyComparatorPtr& internal_comparator,
82
      const FileDescriptor& file_fd, const Slice& filter, TableReader** table_reader_ptr = nullptr,
83
      HistogramImpl* file_read_hist = nullptr, bool for_compaction = false,
84
      Arena* arena = nullptr, bool skip_filters = false);
85
86
  // Return table reader wrapped in internal structure for future use to create iterator.
87
  // Parameters meaning is the same as for NewIterator function above.
88
  Status GetTableReaderForIterator(
89
      const ReadOptions& options, const EnvOptions& toptions,
90
      const InternalKeyComparatorPtr& internal_comparator,
91
      const FileDescriptor& file_fd, TableReaderWithHandle* trwh,
92
      HistogramImpl* file_read_hist = nullptr, bool for_compaction = false,
93
      bool skip_filters = false);
94
95
  // Version of NewIterator which uses provided table reader instead of getting it by
96
  // itself.
97
  InternalIterator* NewIterator(
98
      const ReadOptions& options, TableReaderWithHandle* trwh, const Slice& filter,
99
      bool for_compaction = false, Arena* arena = nullptr, bool skip_filters = false);
100
101
  // If a seek to internal key "k" in specified file finds an entry,
102
  // call (*handle_result)(arg, found_key, found_value) repeatedly until
103
  // it returns false.
104
  // @param skip_filters Disables loading/accessing the filter block
105
  Status Get(const ReadOptions& options,
106
             const InternalKeyComparatorPtr& internal_comparator,
107
             const FileDescriptor& file_fd, const Slice& k,
108
             GetContext* get_context, HistogramImpl* file_read_hist = nullptr,
109
             bool skip_filters = false);
110
111
  // Evict any entry for the specified file number
112
  static void Evict(Cache* cache, uint64_t file_number);
113
114
  // Returns table reader, tries to get it in following order:
115
  // - From fd.table_reader
116
  // - From table cache
117
  // - Load from disk if no_io is false.
118
  // Will return STATUS(Incomplete) if table is not yet loaded and no_io is true.
119
  // See NewIterator for other parameters description.
120
  // NOTE: read stats will be recorded by table reader into ioptions_.statistics the same was as
121
  // for usual (not compaction-intended) TableIterator.
122
  yb::Result<TableReaderWithHandle> GetTableReader(
123
      const EnvOptions& toptions, const InternalKeyComparatorPtr& internal_comparator,
124
      const FileDescriptor& fd, QueryId query_id, bool no_io, HistogramImpl* file_read_hist,
125
      bool skip_filters);
126
127
  // Find table reader
128
  // @param skip_filters Disables loading/accessing the filter block
129
  Status FindTable(const EnvOptions& toptions,
130
                   const InternalKeyComparatorPtr& internal_comparator,
131
                   const FileDescriptor& file_fd, Cache::Handle**,
132
                   const QueryId query_id,
133
                   const bool no_io = false, bool record_read_stats = true,
134
                   HistogramImpl* file_read_hist = nullptr,
135
                   bool skip_filters = false);
136
137
  // Get TableReader from a cache handle.
138
  TableReader* GetTableReaderFromHandle(Cache::Handle* handle);
139
140
  // Get the table properties of a given table.
141
  // @no_io: indicates if we should load table to the cache if it is not present
142
  //         in table cache yet.
143
  // WARNING: no_io == false should be used carefully, because if GetTableProperties is called
144
  // with no_io == false it will create TableReader with file_read_hist == nullptr which
145
  // could be reused by GetTableReaderForIterator from a concurrent thread and since
146
  // file_read_hist was empty the TableIterator using this TableReader won't update file_read_hist
147
  // passed to GetTableReaderForIterator.
148
  // @returns: `properties` will be reset on success. Please note that we will
149
  //            return STATUS(Incomplete, ) if table is not present in cache and
150
  //            we set `no_io` to be true.
151
  Status GetTableProperties(const EnvOptions& toptions,
152
                            const InternalKeyComparatorPtr& internal_comparator,
153
                            const FileDescriptor& file_meta,
154
                            std::shared_ptr<const TableProperties>* properties,
155
                            bool no_io);
156
157
  // Return total memory usage of the table reader of the file.
158
  // 0 if table reader of the file is not loaded.
159
  size_t GetMemoryUsageByTableReader(
160
      const EnvOptions& toptions,
161
      const InternalKeyComparatorPtr& internal_comparator,
162
      const FileDescriptor& fd);
163
164
  // Release the handle from a cache
165
  void ReleaseHandle(Cache::Handle* handle);
166
167
 private:
168
  // Build a table reader
169
  Status DoGetTableReader(
170
      const EnvOptions& env_options, const InternalKeyComparatorPtr& internal_comparator,
171
      const FileDescriptor& fd, bool sequential_mode, bool record_read_stats,
172
      HistogramImpl* file_read_hist, unique_ptr<TableReader>* table_reader,
173
      bool skip_filters = false);
174
175
  // Versions of corresponding public functions, but without performance metrics.
176
  Status DoGetTableReaderForIterator(
177
      const ReadOptions& options, const EnvOptions& toptions,
178
      const InternalKeyComparatorPtr& internal_comparator,
179
      const FileDescriptor& file_fd, TableReaderWithHandle* trwh,
180
      HistogramImpl* file_read_hist = nullptr, bool for_compaction = false,
181
      bool skip_filters = false);
182
183
  InternalIterator* DoNewIterator(
184
      const ReadOptions& options, TableReaderWithHandle* trwh, const Slice& filter,
185
      bool for_compaction = false, Arena* arena = nullptr, bool skip_filters = false);
186
187
  const ImmutableCFOptions& ioptions_;
188
  const EnvOptions& env_options_;
189
  Cache* const cache_;
190
  std::string row_cache_id_;
191
};
192
193
}  // namespace rocksdb
194
195
#endif // YB_ROCKSDB_DB_TABLE_CACHE_H