YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/docdb/docdb_util.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
// Utilities for docdb operations.
15
16
#ifndef YB_DOCDB_DOCDB_UTIL_H
17
#define YB_DOCDB_DOCDB_UTIL_H
18
19
#include "yb/common/schema.h"
20
21
#include "yb/docdb/docdb_fwd.h"
22
#include "yb/docdb/doc_path.h"
23
#include "yb/docdb/shared_lock_manager_fwd.h"
24
#include "yb/docdb/doc_write_batch.h"
25
#include "yb/docdb/docdb_compaction_filter.h"
26
#include "yb/rocksdb/compaction_filter.h"
27
28
namespace yb {
29
namespace docdb {
30
31
void SetValueFromQLBinaryWrapper(
32
  QLValuePB ql_value,
33
  const int pg_data_type,
34
  DatumMessagePB* cdc_datum_message = NULL);
35
36
struct ExternalIntent {
37
  DocPath doc_path;
38
  std::string value;
39
};
40
41
// A wrapper around a RocksDB instance and provides utility functions on top of it, such as
42
// compacting the history until a certain point. This is used in the bulk load tool. This is also
43
// convenient base class for GTest test classes, because it exposes member functions such as
44
// rocksdb() and write_options().
45
class DocDBRocksDBUtil {
46
47
 public:
48
119
  DocDBRocksDBUtil() {}
49
  explicit DocDBRocksDBUtil(InitMarkerBehavior init_marker_behavior)
50
0
      : init_marker_behavior_(init_marker_behavior) {
51
0
  }
52
53
118
  virtual ~DocDBRocksDBUtil() {}
54
  virtual CHECKED_STATUS InitRocksDBDir() = 0;
55
56
  // Initializes RocksDB options, should be called after constructor, because it uses virtual
57
  // function BlockCacheSize.
58
  virtual CHECKED_STATUS InitRocksDBOptions() = 0;
59
  virtual std::string tablet_id() = 0;
60
61
  // Size of block cache for RocksDB, 0 means don't use block cache.
62
119
  virtual size_t block_cache_size() const { return 16 * 1024 * 1024; }
63
64
  rocksdb::DB* rocksdb();
65
  rocksdb::DB* intents_db();
66
641k
  DocDB doc_db() { return { rocksdb(), intents_db(), &KeyBounds::kNoBounds }; }
67
68
  CHECKED_STATUS InitCommonRocksDBOptionsForTests();
69
  CHECKED_STATUS InitCommonRocksDBOptionsForBulkLoad();
70
71
761k
  const rocksdb::WriteOptions& write_options() const { return write_options_; }
72
73
  const rocksdb::Options& regular_db_options() const { return regular_db_options_; }
74
75
  CHECKED_STATUS OpenRocksDB();
76
77
  void CloseRocksDB();
78
  CHECKED_STATUS ReopenRocksDB();
79
  CHECKED_STATUS DestroyRocksDB();
80
  void ResetMonotonicCounter();
81
82
  // Populates the given RocksDB write batch from the given DocWriteBatch. If a valid hybrid_time is
83
  // specified, it is appended to every key.
84
  CHECKED_STATUS PopulateRocksDBWriteBatch(
85
      const DocWriteBatch& dwb,
86
      rocksdb::WriteBatch *rocksdb_write_batch,
87
      HybridTime hybrid_time = HybridTime::kInvalid,
88
      bool decode_dockey = true,
89
      bool increment_write_id = true,
90
      PartialRangeKeyIntents partial_range_key_intents = PartialRangeKeyIntents::kTrue) const;
91
92
  // Writes the given DocWriteBatch to RocksDB. We substitue the hybrid time, if provided.
93
  CHECKED_STATUS WriteToRocksDB(
94
      const DocWriteBatch& write_batch,
95
      const HybridTime& hybrid_time,
96
      bool decode_dockey = true,
97
      bool increment_write_id = true,
98
      PartialRangeKeyIntents partial_range_key_intents = PartialRangeKeyIntents::kTrue);
99
100
  // The same as WriteToRocksDB but also clears the write batch afterwards.
101
  CHECKED_STATUS WriteToRocksDBAndClear(DocWriteBatch* dwb, const HybridTime& hybrid_time,
102
                                        bool decode_dockey = true, bool increment_write_id = true);
103
104
  // Writes value fully determined by its index using DefaultWriteBatch.
105
  CHECKED_STATUS WriteSimple(int index);
106
107
  void SetHistoryCutoffHybridTime(HybridTime history_cutoff);
108
109
  // Produces a string listing the contents of the entire RocksDB database, with every key and value
110
  // decoded as a DocDB key/value and converted to a human-readable string representation.
111
  std::string DocDBDebugDumpToStr();
112
113
  // ----------------------------------------------------------------------------------------------
114
  // SetPrimitive taking a Value
115
116
  CHECKED_STATUS SetPrimitive(
117
      const DocPath& doc_path,
118
      const ValueRef& value,
119
      HybridTime hybrid_time,
120
2.49k
      const ReadHybridTime& read_ht = ReadHybridTime::Max()) {
121
2.49k
    return SetPrimitive(doc_path, ValueControlFields(), value, hybrid_time, read_ht);
122
2.49k
  }
123
124
  CHECKED_STATUS SetPrimitive(
125
      const DocPath& doc_path,
126
      const ValueControlFields& control_fields,
127
      const ValueRef& value,
128
      HybridTime hybrid_time,
129
      const ReadHybridTime& read_ht = ReadHybridTime::Max());
130
131
  CHECKED_STATUS SetPrimitive(
132
      const DocPath& doc_path,
133
      const QLValuePB& value,
134
      HybridTime hybrid_time,
135
      const ReadHybridTime& read_ht = ReadHybridTime::Max());
136
137
  CHECKED_STATUS AddExternalIntents(
138
      const TransactionId& txn_id,
139
      const std::vector<ExternalIntent>& intents,
140
      const Uuid& involved_tablet,
141
      HybridTime hybrid_time);
142
143
  CHECKED_STATUS InsertSubDocument(
144
      const DocPath& doc_path,
145
      const ValueRef& value,
146
      HybridTime hybrid_time,
147
      MonoDelta ttl = ValueControlFields::kMaxTtl,
148
      const ReadHybridTime& read_ht = ReadHybridTime::Max());
149
150
  CHECKED_STATUS ExtendSubDocument(
151
      const DocPath& doc_path,
152
      const ValueRef& value,
153
      HybridTime hybrid_time,
154
      MonoDelta ttl = ValueControlFields::kMaxTtl,
155
      const ReadHybridTime& read_ht = ReadHybridTime::Max());
156
157
  CHECKED_STATUS ExtendList(
158
      const DocPath& doc_path,
159
      const ValueRef& value,
160
      HybridTime hybrid_time,
161
      const ReadHybridTime& read_ht = ReadHybridTime::Max());
162
163
  CHECKED_STATUS ReplaceInList(
164
      const DocPath &doc_path,
165
      const int target_cql_index,
166
      const ValueRef& value,
167
      const ReadHybridTime& read_ht,
168
      const HybridTime& hybrid_time,
169
      const rocksdb::QueryId query_id,
170
      MonoDelta default_ttl = ValueControlFields::kMaxTtl,
171
      MonoDelta ttl = ValueControlFields::kMaxTtl,
172
      UserTimeMicros user_timestamp = ValueControlFields::kInvalidUserTimestamp);
173
174
  CHECKED_STATUS DeleteSubDoc(
175
      const DocPath& doc_path,
176
      HybridTime hybrid_time,
177
      const ReadHybridTime& read_ht = ReadHybridTime::Max());
178
179
  void DocDBDebugDumpToConsole();
180
181
  CHECKED_STATUS FlushRocksDbAndWait();
182
183
  void SetTableTTL(uint64_t ttl_msec);
184
185
  void SetCurrentTransactionId(const TransactionId& txn_id) {
186
    current_txn_id_ = txn_id;
187
  }
188
189
  void SetTransactionIsolationLevel(IsolationLevel isolation_level) {
190
    txn_isolation_level_ = isolation_level;
191
  }
192
193
  void ResetCurrentTransactionId() {
194
    current_txn_id_.reset();
195
  }
196
197
44
  CHECKED_STATUS DisableCompactions() {
198
44
    regular_db_options_.compaction_style = rocksdb::kCompactionStyleNone;
199
44
    intents_db_options_.compaction_style = rocksdb::kCompactionStyleNone;
200
44
    return ReopenRocksDB();
201
44
  }
202
203
  CHECKED_STATUS ReinitDBOptions();
204
205
0
  std::atomic<int64_t>& monotonic_counter() {
206
0
    return monotonic_counter_;
207
0
  }
208
209
  DocWriteBatch MakeDocWriteBatch();
210
  DocWriteBatch MakeDocWriteBatch(InitMarkerBehavior init_marker_behavior);
211
212
  void SetInitMarkerBehavior(InitMarkerBehavior init_marker_behavior);
213
214
  // Returns DocWriteBatch created with MakeDocWriteBatch.
215
  // Could be used when single DocWriteBatch is enough.
216
  DocWriteBatch& DefaultDocWriteBatch();
217
218
 protected:
219
  std::string IntentsDBDir();
220
221
  std::unique_ptr<rocksdb::DB> regular_db_;
222
  std::unique_ptr<rocksdb::DB> intents_db_;
223
  rocksdb::Options regular_db_options_;
224
  rocksdb::Options intents_db_options_;
225
  std::string rocksdb_dir_;
226
227
  // This is used for auto-assigning op ids to RocksDB write batches to emulate what a tablet would
228
  // do in production.
229
  rocksdb::OpId op_id_;
230
231
  std::shared_ptr<rocksdb::Cache> block_cache_;
232
  std::shared_ptr<ManualHistoryRetentionPolicy> retention_policy_ {
233
      std::make_shared<ManualHistoryRetentionPolicy>() };
234
  std::shared_ptr<rocksdb::CompactionFileFilterFactory> compaction_file_filter_factory_;
235
  std::shared_ptr<std::function<uint64_t()>> max_file_size_for_compaction_;
236
237
  rocksdb::WriteOptions write_options_;
238
  Schema schema_;
239
  boost::optional<TransactionId> current_txn_id_;
240
  mutable IntraTxnWriteId intra_txn_write_id_ = 0;
241
  IsolationLevel txn_isolation_level_ = IsolationLevel::NON_TRANSACTIONAL;
242
  InitMarkerBehavior init_marker_behavior_ = InitMarkerBehavior::kOptional;
243
244
 private:
245
  std::atomic<int64_t> monotonic_counter_{0};
246
  boost::optional<DocWriteBatch> doc_write_batch_;
247
};
248
249
}  // namespace docdb
250
}  // namespace yb
251
252
#endif // YB_DOCDB_DOCDB_UTIL_H