YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/rocksdb/listener.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2014 The LevelDB Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file. See the AUTHORS file for names of contributors.
4
//
5
// The following only applies to changes made to this file as part of YugaByte development.
6
//
7
// Portions Copyright (c) YugaByte, Inc.
8
//
9
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
10
// in compliance with the License.  You may obtain a copy of the License at
11
//
12
// http://www.apache.org/licenses/LICENSE-2.0
13
//
14
// Unless required by applicable law or agreed to in writing, software distributed under the License
15
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
16
// or implied.  See the License for the specific language governing permissions and limitations
17
// under the License.
18
//
19
#ifndef YB_ROCKSDB_LISTENER_H
20
#define YB_ROCKSDB_LISTENER_H
21
22
#pragma once
23
24
#include <memory>
25
#include <string>
26
#include <unordered_map>
27
#include <vector>
28
#include "yb/rocksdb/compaction_job_stats.h"
29
#include "yb/rocksdb/status.h"
30
#include "yb/rocksdb/table_properties.h"
31
32
namespace rocksdb {
33
34
typedef std::unordered_map<std::string, std::shared_ptr<const TableProperties>>
35
    TablePropertiesCollection;
36
37
class DB;
38
struct CompactionJobStats;
39
40
struct TableFileCreationInfo {
41
32.2k
  TableFileCreationInfo() = default;
42
  explicit TableFileCreationInfo(TableProperties&& prop) :
43
22.8k
      table_properties(prop) {}
44
  // the name of the database where the file was created
45
  std::string db_name;
46
  // the name of the column family where the file was created.
47
  std::string cf_name;
48
  // the path to the created file.
49
  std::string file_path;
50
  // the size of the file.
51
  uint64_t file_size = 0;
52
  // the id of the job (which could be flush or compaction) that
53
  // created the file.
54
  int job_id = 0;
55
  // Detailed properties of the created file.
56
  TableProperties table_properties;
57
};
58
59
enum class CompactionReason {
60
  kUnknown,
61
  // [Level] number of L0 files > level0_file_num_compaction_trigger
62
  kLevelL0FilesNum,
63
  // [Level] total size of level > MaxBytesForLevel()
64
  kLevelMaxLevelSize,
65
  // [Universal] Compacting for size amplification
66
  kUniversalSizeAmplification,
67
  // [Universal] Compacting for size ratio
68
  kUniversalSizeRatio,
69
  // [Universal] number of sorted runs > level0_file_num_compaction_trigger
70
  kUniversalSortedRunNum,
71
  // [Universal] files have been marked for direct deletion
72
  kUniversalDirectDeletion,
73
  // [FIFO] total size > max_table_files_size
74
  kFIFOMaxSize,
75
  // Manual compaction
76
  kManualCompaction,
77
  // DB::SuggestCompactRange() marked files for compaction
78
  kFilesMarkedForCompaction,
79
};
80
81
#ifndef ROCKSDB_LITE
82
83
struct TableFileDeletionInfo {
84
  // The name of the database where the file was deleted.
85
  std::string db_name;
86
  // The path to the deleted file.
87
  std::string file_path;
88
  // The id of the job which deleted the file.
89
  int job_id;
90
  // The status indicating whether the deletion was successful or not.
91
  Status status;
92
};
93
94
struct FlushJobInfo {
95
  // the name of the column family
96
  std::string cf_name;
97
  // the path to the newly created file
98
  std::string file_path;
99
  // the id of the thread that completed this flush job.
100
  uint64_t thread_id;
101
  // the job id, which is unique in the same thread.
102
  int job_id;
103
  // If true, then rocksdb is currently slowing-down all writes to prevent
104
  // creating too many Level 0 files as compaction seems not able to
105
  // catch up the write request speed.  This indicates that there are
106
  // too many files in Level 0.
107
  bool triggered_writes_slowdown;
108
  // If true, then rocksdb is currently blocking any writes to prevent
109
  // creating more L0 files.  This indicates that there are too many
110
  // files in level 0.  Compactions should try to compact L0 files down
111
  // to lower levels as soon as possible.
112
  bool triggered_writes_stop;
113
  // The smallest sequence number in the newly created file
114
  SequenceNumber smallest_seqno;
115
  // The largest sequence number in the newly created file
116
  SequenceNumber largest_seqno;
117
  // Table properties of the table being flushed
118
  TableProperties table_properties;
119
};
120
121
struct CompactionJobInfo {
122
688
  CompactionJobInfo() = default;
123
  explicit CompactionJobInfo(const CompactionJobStats& _stats) :
124
0
      stats(_stats) {}
125
126
  // the name of the column family where the compaction happened.
127
  std::string cf_name;
128
  // the status indicating whether the compaction was successful or not.
129
  Status status;
130
  // the id of the thread that completed this compaction job.
131
  uint64_t thread_id = 0;
132
  // the job id, which is unique in the same thread.
133
  int job_id = 0;
134
  // the smallest input level of the compaction.
135
  int base_input_level = 0;
136
  // the output level of the compaction.
137
  int output_level = 0;
138
  // the names of the compaction input files.
139
  std::vector<std::string> input_files;
140
141
  // the names of the compaction output files.
142
  std::vector<std::string> output_files;
143
  // Table properties for input and output tables.
144
  // The map is keyed by values from input_files and output_files.
145
  TablePropertiesCollection table_properties;
146
147
  // Reason to run the compaction
148
  CompactionReason compaction_reason = CompactionReason::kUnknown;
149
150
  bool is_full_compaction = false;
151
152
  // If non-null, this variable stores detailed information
153
  // about this compaction.
154
  CompactionJobStats stats;
155
};
156
157
// EventListener class contains a set of call-back functions that will
158
// be called when specific RocksDB event happens such as flush.  It can
159
// be used as a building block for developing custom features such as
160
// stats-collector or external compaction algorithm.
161
//
162
// Note that call-back functions should not run for an extended period of
163
// time before the function returns, otherwise RocksDB may be blocked.
164
// For example, it is not suggested to do DB::CompactFiles() (as it may
165
// run for a long while) or issue many of DB::Put() (as Put may be blocked
166
// in certain cases) in the same thread in the EventListener callback.
167
// However, doing DB::CompactFiles() and DB::Put() in another thread is
168
// considered safe.
169
//
170
// [Threading] All EventListener callback will be called using the
171
// actual thread that involves in that specific event.   For example, it
172
// is the RocksDB background flush thread that does the actual flush to
173
// call EventListener::OnFlushCompleted().
174
//
175
// [Locking] All EventListener callbacks are designed to be called without
176
// the current thread holding any DB mutex. This is to prevent potential
177
// deadlock and performance issue when using EventListener callback
178
// in a complex way. However, all EventListener call-back functions
179
// should not run for an extended period of time before the function
180
// returns, otherwise RocksDB may be blocked. For example, it is not
181
// suggested to do DB::CompactFiles() (as it may run for a long while)
182
// or issue many of DB::Put() (as Put may be blocked in certain cases)
183
// in the same thread in the EventListener callback. However, doing
184
// DB::CompactFiles() and DB::Put() in a thread other than the
185
// EventListener callback thread is considered safe.
186
class EventListener {
187
 public:
188
189
  // A call-back function which will be called whenever a RocksDB
190
  // memstore flush is scheduled.
191
  //
192
  // Note that the this function must be implemented in a way such that
193
  // it should not run for an extended period of time before the function
194
  // returns.  Otherwise, RocksDB may be blocked.
195
4.40k
  virtual void OnFlushScheduled(DB* db) {}
196
197
  // A call-back function to RocksDB which will be called whenever a
198
  // registered RocksDB flushes a file.  The default implementation is
199
  // no-op.
200
  //
201
  // Note that the this function must be implemented in a way such that
202
  // it should not run for an extended period of time before the function
203
  // returns.  Otherwise, RocksDB may be blocked.
204
  virtual void OnFlushCompleted(DB* /*db*/,
205
3.37k
                                const FlushJobInfo& /*flush_job_info*/) {}
206
207
  // A call-back function for RocksDB which will be called whenever
208
  // a SST file is deleted.  Different from OnCompactionCompleted and
209
  // OnFlushCompleted, this call-back is designed for external logging
210
  // service and thus only provide string parameters instead
211
  // of a pointer to DB.  Applications that build logic basic based
212
  // on file creations and deletions is suggested to implement
213
  // OnFlushCompleted and OnCompactionCompleted.
214
  //
215
  // Note that if applications would like to use the passed reference
216
  // outside this function call, they should make copies from the
217
  // returned value.
218
2.48k
  virtual void OnTableFileDeleted(const TableFileDeletionInfo& /*info*/) {}
219
220
  // A call-back function for RocksDB which will be called whenever a compaction has been started.
221
701
  virtual void OnCompactionStarted() {}
222
223
  // A call-back function for RocksDB which will be called whenever
224
  // a registered RocksDB compacts a file. The default implementation
225
  // is a no-op.
226
  //
227
  // Note that this function must be implemented in a way such that
228
  // it should not run for an extended period of time before the function
229
  // returns. Otherwise, RocksDB may be blocked.
230
  //
231
  // @param db a pointer to the rocksdb instance which just compacted
232
  //   a file.
233
  // @param ci a reference to a CompactionJobInfo struct. 'ci' is released
234
  //  after this function is returned, and must be copied if it is needed
235
  //  outside of this function.
236
  virtual void OnCompactionCompleted(DB* /*db*/,
237
121
                                     const CompactionJobInfo& /*ci*/) {}
238
239
  // A call-back function for RocksDB which will be called whenever
240
  // a SST file is created.  Different from OnCompactionCompleted and
241
  // OnFlushCompleted, this call-back is designed for external logging
242
  // service and thus only provide string parameters instead
243
  // of a pointer to DB.  Applications that build logic basic based
244
  // on file creations and deletions is suggested to implement
245
  // OnFlushCompleted and OnCompactionCompleted.
246
  //
247
  // Note that if applications would like to use the passed reference
248
  // outside this function call, they should make copies from these
249
  // returned value.
250
4.74k
  virtual void OnTableFileCreated(const TableFileCreationInfo& /*info*/) {}
251
252
249k
  virtual ~EventListener() {}
253
};
254
255
#else
256
257
class EventListener {
258
};
259
260
#endif  // ROCKSDB_LITE
261
262
}  // namespace rocksdb
263
264
#endif // YB_ROCKSDB_LISTENER_H