YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/table/flush_block_policy.cc
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
21
#include "yb/rocksdb/options.h"
22
#include "yb/rocksdb/flush_block_policy.h"
23
#include "yb/rocksdb/table/block_builder.h"
24
#include "yb/util/slice.h"
25
26
namespace rocksdb {
27
28
// Flush block by size
29
class FlushBlockBySizePolicy : public FlushBlockPolicy {
30
 public:
31
  // @params block_size:           Approximate size of user data packed per
32
  //                               block.
33
  // @params block_size_deviation: This is used to close a block before it
34
  //                               reaches the configured
35
  FlushBlockBySizePolicy(const uint64_t block_size,
36
                         const uint64_t block_size_deviation,
37
                         const size_t min_keys_per_block,
38
                         const BlockBuilder& data_block_builder) :
39
      block_size_(block_size),
40
      block_size_deviation_(block_size_deviation),
41
      min_keys_per_block_(min_keys_per_block),
42
137k
      data_block_builder_(data_block_builder) {
43
137k
  }
44
45
  virtual bool Update(const Slice& key,
46
94.3M
                      const Slice& value) override {
47
    // it makes no sense to flush when the data block is empty
48
94.3M
    if (data_block_builder_.empty()) {
49
61.3k
      return false;
50
61.3k
    }
51
52
94.2M
    auto curr_size = data_block_builder_.CurrentSizeEstimate();
53
54
    // Do flush if one of the below two conditions is true and we already have the required minimum
55
    // number of keys in block:
56
    // 1) if the current estimated size already exceeds the block size,
57
    // 2) block_size_deviation is set and the estimated size after appending
58
    // the kv will exceed the block size and the current size is under the
59
    // the deviation.
60
94.2M
    return (curr_size >= block_size_ || BlockAlmostFull(key, value))
61
2.52M
        && data_block_builder_.NumKeys() >= min_keys_per_block_;
62
94.2M
  }
63
64
 private:
65
92.9M
  bool BlockAlmostFull(const Slice& key, const Slice& value) const {
66
92.9M
    const auto curr_size = data_block_builder_.CurrentSizeEstimate();
67
92.9M
    const auto estimated_size_after =
68
92.9M
      data_block_builder_.EstimateSizeAfterKV(key, value);
69
70
92.9M
    return
71
92.9M
      estimated_size_after > block_size_ &&
72
2.17M
      block_size_deviation_ > 0 &&
73
2.16M
      curr_size * 100 > block_size_ * (100 - block_size_deviation_);
74
92.9M
  }
75
76
  const uint64_t block_size_;
77
  const uint64_t block_size_deviation_;
78
  const size_t min_keys_per_block_;
79
  const BlockBuilder& data_block_builder_;
80
};
81
82
FlushBlockPolicy* FlushBlockBySizePolicyFactory::NewFlushBlockPolicy(
83
    const BlockBasedTableOptions& table_options,
84
61.4k
    const BlockBuilder& data_block_builder) const {
85
61.4k
  return new FlushBlockBySizePolicy(
86
61.4k
      table_options.block_size, table_options.block_size_deviation, 1 /* min_keys_per_block */,
87
61.4k
      data_block_builder);
88
61.4k
}
89
90
std::unique_ptr<FlushBlockPolicy> FlushBlockBySizePolicyFactory::NewFlushBlockPolicy(
91
    const uint64_t size, const int deviation, const size_t min_keys_per_block,
92
75.6k
    const BlockBuilder& data_block_builder) {
93
75.6k
  return std::make_unique<FlushBlockBySizePolicy>(
94
75.6k
      size, deviation, min_keys_per_block, data_block_builder);
95
75.6k
}
96
97
}  // namespace rocksdb