YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/rocksdb/table/meta_blocks.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 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
21
#include "yb/rocksdb/table/meta_blocks.h"
22
23
#include <map>
24
#include <string>
25
26
#include "yb/rocksdb/db/table_properties_collector.h"
27
#include "yb/rocksdb/table.h"
28
#include "yb/rocksdb/table/block.h"
29
#include "yb/rocksdb/table/block_builder.h"
30
#include "yb/rocksdb/table/format.h"
31
#include "yb/rocksdb/table/internal_iterator.h"
32
#include "yb/rocksdb/table/table_properties_internal.h"
33
#include "yb/rocksdb/util/coding.h"
34
#include "yb/rocksdb/util/file_reader_writer.h"
35
36
DEFINE_bool(verify_encrypted_meta_block_checksums, true,
37
            "Whether to verify checksums for meta blocks of encrypted SSTables.");
38
39
namespace rocksdb {
40
41
namespace {
42
43
constexpr auto kMetaIndexBlockRestartInterval = 1;
44
45
// We use kKeyDeltaEncodingSharedPrefix format for property blocks, but since
46
// kPropertyBlockRestartInterval == 1 every key in these blocks will still have zero shared prefix
47
// length and will be stored fully.
48
constexpr auto kPropertyBlockKeyValueEncodingFormat =
49
    KeyValueEncodingFormat::kKeyDeltaEncodingSharedPrefix;
50
constexpr auto kPropertyBlockRestartInterval = 1;
51
52
152k
ReadOptions CreateMetaBlockReadOptions(RandomAccessFileReader* file) {
53
152k
  ReadOptions read_options;
54
55
  // We need to verify checksums for meta blocks in order to recover from the encryption format
56
  // issue described at https://github.com/yugabyte/yugabyte-db/issues/3707.
57
  // However, we only do that for encrypted files in order to prevent lots of RocksDB unit tests
58
  // from failing as described at https://github.com/yugabyte/yugabyte-db/issues/3974.
59
152k
  read_options.verify_checksums = file->file()->IsEncrypted() &&
60
152k
                                  
FLAGS_verify_encrypted_meta_block_checksums11
;
61
152k
  return read_options;
62
152k
}
63
64
}  // namespace
65
66
MetaIndexBuilder::MetaIndexBuilder()
67
    : meta_index_block_(new BlockBuilder(
68
67.5k
          kMetaIndexBlockRestartInterval, kMetaIndexBlockKeyValueEncodingFormat)) {}
69
70
void MetaIndexBuilder::Add(const std::string& key,
71
82.1k
                           const BlockHandle& handle) {
72
82.1k
  std::string handle_encoding;
73
82.1k
  handle.AppendEncodedTo(&handle_encoding);
74
82.1k
  meta_block_handles_.insert({key, handle_encoding});
75
82.1k
}
76
77
67.5k
Slice MetaIndexBuilder::Finish() {
78
82.1k
  for (const auto& metablock : meta_block_handles_) {
79
82.1k
    meta_index_block_->Add(metablock.first, metablock.second);
80
82.1k
  }
81
67.5k
  return meta_index_block_->Finish();
82
67.5k
}
83
84
PropertyBlockBuilder::PropertyBlockBuilder()
85
    : properties_block_(
86
67.5k
          new BlockBuilder(kPropertyBlockRestartInterval, kPropertyBlockKeyValueEncodingFormat)) {}
87
88
void PropertyBlockBuilder::Add(const std::string& name,
89
1.21M
                               const std::string& val) {
90
1.21M
  props_.insert({name, val});
91
1.21M
}
92
93
811k
void PropertyBlockBuilder::Add(const std::string& name, uint64_t val) {
94
811k
  assert(props_.find(name) == props_.end());
95
96
0
  std::string dst;
97
811k
  PutVarint64(&dst, val);
98
99
811k
  Add(name, dst);
100
811k
}
101
102
void PropertyBlockBuilder::Add(
103
131k
    const UserCollectedProperties& user_collected_properties) {
104
395k
  for (const auto& prop : user_collected_properties) {
105
395k
    Add(prop.first, prop.second);
106
395k
  }
107
131k
}
108
109
67.5k
void PropertyBlockBuilder::AddTableProperty(const TableProperties& props) {
110
67.5k
  Add(TablePropertiesNames::kRawKeySize, props.raw_key_size);
111
67.5k
  Add(TablePropertiesNames::kRawValueSize, props.raw_value_size);
112
67.5k
  Add(TablePropertiesNames::kDataSize, props.data_size);
113
67.5k
  Add(TablePropertiesNames::kDataIndexSize, props.data_index_size);
114
67.5k
  Add(TablePropertiesNames::kFilterIndexSize, props.filter_index_size);
115
67.5k
  Add(TablePropertiesNames::kNumEntries, props.num_entries);
116
67.5k
  Add(TablePropertiesNames::kNumDataBlocks, props.num_data_blocks);
117
67.5k
  Add(TablePropertiesNames::kNumFilterBlocks, props.num_filter_blocks);
118
67.5k
  Add(TablePropertiesNames::kNumDataIndexBlocks, props.num_data_index_blocks);
119
67.5k
  Add(TablePropertiesNames::kFilterSize, props.filter_size);
120
67.5k
  Add(TablePropertiesNames::kFormatVersion, props.format_version);
121
67.5k
  Add(TablePropertiesNames::kFixedKeyLen, props.fixed_key_len);
122
123
67.5k
  if (!props.filter_policy_name.empty()) {
124
11.6k
    Add(TablePropertiesNames::kFilterPolicy,
125
11.6k
        props.filter_policy_name);
126
11.6k
  }
127
67.5k
}
128
129
67.5k
Slice PropertyBlockBuilder::Finish() {
130
1.21M
  for (const auto& prop : props_) {
131
1.21M
    properties_block_->Add(prop.first, prop.second);
132
1.21M
  }
133
134
67.5k
  return properties_block_->Finish();
135
67.5k
}
136
137
void LogPropertiesCollectionError(
138
0
    Logger* info_log, const std::string& method, const std::string& name) {
139
0
  assert(method == "Add" || method == "Finish");
140
141
0
  std::string msg =
142
0
    "Encountered error when calling TablePropertiesCollector::" +
143
0
    method + "() with collector name: " + name;
144
0
  RLOG(InfoLogLevel::ERROR_LEVEL, info_log, "%s", msg.c_str());
145
0
}
146
147
bool NotifyCollectTableCollectorsOnAdd(
148
    const Slice& key, const Slice& value, uint64_t file_size,
149
    const std::vector<std::unique_ptr<IntTblPropCollector>>& collectors,
150
179M
    Logger* info_log) {
151
179M
  bool all_succeeded = true;
152
338M
  for (auto& collector : collectors) {
153
338M
    Status s = collector->InternalAdd(key, value, file_size);
154
338M
    all_succeeded = all_succeeded && 
s.ok()338M
;
155
338M
    if (!s.ok()) {
156
0
      LogPropertiesCollectionError(info_log, "Add" /* method */,
157
0
                                   collector->Name());
158
0
    }
159
338M
  }
160
179M
  return all_succeeded;
161
179M
}
162
163
bool NotifyCollectTableCollectorsOnFinish(
164
    const std::vector<std::unique_ptr<IntTblPropCollector>>& collectors,
165
67.5k
    Logger* info_log, PropertyBlockBuilder* builder) {
166
67.5k
  bool all_succeeded = true;
167
129k
  for (auto& collector : collectors) {
168
129k
    UserCollectedProperties user_collected_properties;
169
129k
    Status s = collector->Finish(&user_collected_properties);
170
171
129k
    all_succeeded = all_succeeded && 
s.ok()129k
;
172
129k
    if (!s.ok()) {
173
0
      LogPropertiesCollectionError(info_log, "Finish" /* method */,
174
0
                                   collector->Name());
175
129k
    } else {
176
129k
      builder->Add(user_collected_properties);
177
129k
    }
178
129k
  }
179
180
67.5k
  return all_succeeded;
181
67.5k
}
182
183
Status ReadProperties(const Slice& handle_value, RandomAccessFileReader* file,
184
                      const Footer& footer, Env* env, Logger* logger,
185
112k
                      TableProperties** table_properties) {
186
112k
  assert(table_properties);
187
188
0
  Slice v = handle_value;
189
112k
  BlockHandle handle;
190
112k
  if (!handle.DecodeFrom(&v).ok()) {
191
0
    return STATUS(InvalidArgument, "Failed to decode properties block handle");
192
0
  }
193
194
112k
  BlockContents block_contents;
195
112k
  ReadOptions read_options = CreateMetaBlockReadOptions(file);
196
112k
  Status s = ReadBlockContents(file, footer, read_options, handle, &block_contents,
197
112k
                               env, nullptr /* mem_tracker */, false);
198
199
112k
  if (!s.ok()) {
200
0
    return s;
201
0
  }
202
203
112k
  Block properties_block(std::move(block_contents));
204
112k
  std::unique_ptr<InternalIterator> iter(properties_block.NewIterator(
205
112k
      BytewiseComparator(), kPropertyBlockKeyValueEncodingFormat));
206
207
112k
  auto new_table_properties = new TableProperties();
208
  // All pre-defined properties of type uint64_t
209
112k
  std::unordered_map<std::string, uint64_t*> predefined_uint64_properties = {
210
112k
      {TablePropertiesNames::kDataSize, &new_table_properties->data_size},
211
112k
      {TablePropertiesNames::kDataIndexSize, &new_table_properties->data_index_size},
212
112k
      {TablePropertiesNames::kFilterSize, &new_table_properties->filter_size},
213
112k
      {TablePropertiesNames::kFilterIndexSize, &new_table_properties->filter_index_size},
214
112k
      {TablePropertiesNames::kRawKeySize, &new_table_properties->raw_key_size},
215
112k
      {TablePropertiesNames::kRawValueSize, &new_table_properties->raw_value_size},
216
112k
      {TablePropertiesNames::kNumDataBlocks, &new_table_properties->num_data_blocks},
217
112k
      {TablePropertiesNames::kNumEntries, &new_table_properties->num_entries},
218
112k
      {TablePropertiesNames::kNumFilterBlocks, &new_table_properties->num_filter_blocks},
219
112k
      {TablePropertiesNames::kNumDataIndexBlocks, &new_table_properties->num_data_index_blocks},
220
112k
      {TablePropertiesNames::kFormatVersion, &new_table_properties->format_version},
221
112k
      {TablePropertiesNames::kFixedKeyLen, &new_table_properties->fixed_key_len}, };
222
223
112k
  std::string last_key;
224
2.13M
  for (iter->SeekToFirst(); iter->Valid(); 
iter->Next()2.01M
) {
225
2.01M
    s = iter->status();
226
2.01M
    if (!s.ok()) {
227
0
      break;
228
0
    }
229
230
2.01M
    auto key = iter->key().ToString();
231
    // properties block is strictly sorted with no duplicate key.
232
2.01M
    assert(last_key.empty() ||
233
2.01M
           BytewiseComparator()->Compare(key, last_key) > 0);
234
0
    last_key = key;
235
236
2.01M
    auto raw_val = iter->value();
237
2.01M
    auto pos = predefined_uint64_properties.find(key);
238
239
2.01M
    if (pos != predefined_uint64_properties.end()) {
240
      // handle predefined rocksdb properties
241
1.34M
      uint64_t val;
242
1.34M
      if (!GetVarint64(&raw_val, &val)) {
243
        // skip malformed value
244
0
        auto error_msg =
245
0
          "Detect malformed value in properties meta-block:"
246
0
          "\tkey: " + key + "\tval: " + raw_val.ToString();
247
0
        RLOG(InfoLogLevel::ERROR_LEVEL, logger, "%s", error_msg.c_str());
248
0
        continue;
249
0
      }
250
1.34M
      *(pos->second) = val;
251
1.34M
    } else 
if (673k
key == TablePropertiesNames::kFilterPolicy673k
) {
252
17.5k
      new_table_properties->filter_policy_name = raw_val.ToString();
253
655k
    } else {
254
      // handle user-collected properties
255
655k
      new_table_properties->user_collected_properties.insert(
256
655k
          {key, raw_val.ToString()});
257
655k
    }
258
2.01M
  }
259
112k
  if (
s.ok()112k
) {
260
112k
    *table_properties = new_table_properties;
261
18.4E
  } else {
262
18.4E
    delete new_table_properties;
263
18.4E
  }
264
265
112k
  return s;
266
112k
}
267
268
Status ReadTableProperties(RandomAccessFileReader* file, uint64_t file_size,
269
                           uint64_t table_magic_number, Env* env,
270
34.1k
                           Logger* info_log, TableProperties** properties) {
271
  // -- Read metaindex block
272
34.1k
  Footer footer;
273
34.1k
  auto s = ReadFooterFromFile(file, file_size, &footer, table_magic_number);
274
34.1k
  if (!s.ok()) {
275
47
    return s;
276
47
  }
277
278
34.1k
  auto metaindex_handle = footer.metaindex_handle();
279
34.1k
  BlockContents metaindex_contents;
280
34.1k
  ReadOptions read_options = CreateMetaBlockReadOptions(file);
281
34.1k
  s = ReadBlockContents(file, footer, read_options, metaindex_handle,
282
34.1k
                        &metaindex_contents, env, nullptr /* mem_tracker */, false);
283
34.1k
  if (!s.ok()) {
284
0
    return s;
285
0
  }
286
34.1k
  Block metaindex_block(std::move(metaindex_contents));
287
34.1k
  std::unique_ptr<InternalIterator> meta_iter(metaindex_block.NewIterator(
288
34.1k
      BytewiseComparator(), kMetaIndexBlockKeyValueEncodingFormat));
289
290
  // -- Read property block
291
34.1k
  bool found_properties_block = true;
292
34.1k
  s = SeekToPropertiesBlock(meta_iter.get(), &found_properties_block);
293
34.1k
  if (!s.ok()) {
294
0
    return s;
295
0
  }
296
297
34.1k
  TableProperties table_properties;
298
34.1k
  if (found_properties_block == true) {
299
34.1k
    s = ReadProperties(meta_iter->value(), file, footer, env, info_log,
300
34.1k
                       properties);
301
34.1k
  } else {
302
16
    s = STATUS(NotFound, "");
303
16
  }
304
305
34.1k
  return s;
306
34.1k
}
307
308
Status FindMetaBlock(InternalIterator* meta_index_iter,
309
                     const std::string& meta_block_name,
310
40.7k
                     BlockHandle* block_handle) {
311
40.7k
  meta_index_iter->Seek(meta_block_name);
312
40.7k
  if (meta_index_iter->status().ok() && 
meta_index_iter->Valid()40.7k
&&
313
40.7k
      
meta_index_iter->key() == meta_block_name40.7k
) {
314
16.7k
    Slice v = meta_index_iter->value();
315
16.7k
    return block_handle->DecodeFrom(&v);
316
24.0k
  } else {
317
24.0k
    return STATUS(Corruption, "Cannot find the meta block", meta_block_name);
318
24.0k
  }
319
40.7k
}
320
321
Status FindMetaBlock(RandomAccessFileReader* file, uint64_t file_size,
322
                     uint64_t table_magic_number, Env* env,
323
                     const std::string& meta_block_name,
324
                     const std::shared_ptr<yb::MemTracker>& mem_tracker,
325
96
                     BlockHandle* block_handle) {
326
96
  Footer footer;
327
96
  auto s = ReadFooterFromFile(file, file_size, &footer, table_magic_number);
328
96
  if (!s.ok()) {
329
0
    return s;
330
0
  }
331
332
96
  auto metaindex_handle = footer.metaindex_handle();
333
96
  BlockContents metaindex_contents;
334
96
  ReadOptions read_options = CreateMetaBlockReadOptions(file);
335
96
  s = ReadBlockContents(file, footer, read_options, metaindex_handle,
336
96
                        &metaindex_contents, env, mem_tracker, false);
337
96
  if (!s.ok()) {
338
0
    return s;
339
0
  }
340
96
  Block metaindex_block(std::move(metaindex_contents));
341
342
96
  std::unique_ptr<InternalIterator> meta_iter;
343
96
  meta_iter.reset(
344
96
      metaindex_block.NewIterator(BytewiseComparator(), kMetaIndexBlockKeyValueEncodingFormat));
345
346
96
  return FindMetaBlock(meta_iter.get(), meta_block_name, block_handle);
347
96
}
348
349
Status ReadMetaBlock(RandomAccessFileReader* file, uint64_t file_size,
350
                     uint64_t table_magic_number, Env* env,
351
                     const std::string& meta_block_name,
352
                     const std::shared_ptr<yb::MemTracker>& mem_tracker,
353
6.20k
                     BlockContents* contents) {
354
6.20k
  Status status;
355
6.20k
  Footer footer;
356
6.20k
  status = ReadFooterFromFile(file, file_size, &footer, table_magic_number);
357
6.20k
  if (!status.ok()) {
358
0
    return status;
359
0
  }
360
361
  // Reading metaindex block
362
6.20k
  auto metaindex_handle = footer.metaindex_handle();
363
6.20k
  BlockContents metaindex_contents;
364
6.20k
  ReadOptions read_options = CreateMetaBlockReadOptions(file);
365
6.20k
  status = ReadBlockContents(file, footer, read_options, metaindex_handle,
366
6.20k
                             &metaindex_contents, env, mem_tracker, false);
367
6.20k
  if (!status.ok()) {
368
0
    return status;
369
0
  }
370
371
  // Finding metablock
372
6.20k
  Block metaindex_block(std::move(metaindex_contents));
373
374
6.20k
  std::unique_ptr<InternalIterator> meta_iter;
375
6.20k
  meta_iter.reset(
376
6.20k
      metaindex_block.NewIterator(BytewiseComparator(), kMetaIndexBlockKeyValueEncodingFormat));
377
378
6.20k
  BlockHandle block_handle;
379
6.20k
  status = FindMetaBlock(meta_iter.get(), meta_block_name, &block_handle);
380
381
6.20k
  if (!status.ok()) {
382
6.07k
    return status;
383
6.07k
  }
384
385
  // Reading metablock
386
128
  return ReadBlockContents(
387
128
      file, footer, read_options, block_handle, contents, env, mem_tracker, false);
388
6.20k
}
389
390
}  // namespace rocksdb