YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/tablet/tablet_retention_policy.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
#ifndef YB_TABLET_TABLET_RETENTION_POLICY_H_
15
#define YB_TABLET_TABLET_RETENTION_POLICY_H_
16
17
#include "yb/docdb/docdb_compaction_filter.h"
18
#include "yb/server/clock.h"
19
20
#include "yb/tablet/tablet_fwd.h"
21
22
namespace yb {
23
namespace tablet {
24
25
using AllowedHistoryCutoffProvider = std::function<HybridTime(RaftGroupMetadata*)>;
26
27
// History retention policy used by a tablet. It is based on pending reads and a fixed retention
28
// interval configured by the user.
29
class TabletRetentionPolicy : public docdb::HistoryRetentionPolicy {
30
 public:
31
  explicit TabletRetentionPolicy(
32
      server::ClockPtr clock, const AllowedHistoryCutoffProvider& allowed_history_cutoff_provider,
33
      RaftGroupMetadata* metadata);
34
35
  docdb::HistoryRetentionDirective GetRetentionDirective() override;
36
37
  // Tries to update history cutoff to proposed value, not allowing it to decrease.
38
  // Returns new committed history cutoff value.
39
  HybridTime UpdateCommittedHistoryCutoff(HybridTime new_value);
40
41
  // Returns history cutoff for propagation.
42
  // It is used at tablet leader while creating request for peer.
43
  // Invalid hybrid time is returned when history cutoff should not be propagated.
44
  // For instance it could happen if we already have big enough history cutoff or propagated it
45
  // recently.
46
  HybridTime HistoryCutoffToPropagate(HybridTime last_write_ht);
47
48
  // Register/Unregister a read operation, with an associated timestamp, for the purpose of
49
  // tracking the oldest read point.
50
  CHECKED_STATUS RegisterReaderTimestamp(HybridTime timestamp);
51
  void UnregisterReaderTimestamp(HybridTime timestamp);
52
53
  void EnableHistoryCutoffPropagation(bool value);
54
55
 private:
56
  bool ShouldRetainDeleteMarkersInMajorCompaction() const;
57
  HybridTime EffectiveHistoryCutoff() REQUIRES(mutex_);
58
59
  // Check proposed history cutoff against other restrictions (for instance min reading timestamp),
60
  // and returns most close value that satisfies them.
61
  HybridTime SanitizeHistoryCutoff(HybridTime proposed_history_cutoff) REQUIRES(mutex_);
62
63
0
  const std::string& LogPrefix() const {
64
0
    return log_prefix_;
65
0
  }
66
67
  const server::ClockPtr clock_;
68
  const AllowedHistoryCutoffProvider allowed_history_cutoff_provider_;
69
  RaftGroupMetadata& metadata_;
70
  const std::string log_prefix_;
71
72
  mutable std::mutex mutex_;
73
  // Set of active read timestamps.
74
  std::multiset<HybridTime> active_readers_ GUARDED_BY(mutex_);
75
  HybridTime committed_history_cutoff_ GUARDED_BY(mutex_) = HybridTime::kMin;
76
  CoarseTimePoint next_history_cutoff_propagation_ GUARDED_BY(mutex_) = CoarseTimePoint::min();
77
  int disable_counter_ GUARDED_BY(mutex_) = 0;
78
};
79
80
class HistoryCutoffPropagationDisabler {
81
 public:
82
2.01k
  explicit HistoryCutoffPropagationDisabler(TabletRetentionPolicy* policy) : policy_(policy) {
83
2.01k
    policy_->EnableHistoryCutoffPropagation(false);
84
2.01k
  }
85
86
2.01k
  ~HistoryCutoffPropagationDisabler() {
87
2.01k
    policy_->EnableHistoryCutoffPropagation(true);
88
2.01k
  }
89
90
 private:
91
  TabletRetentionPolicy* policy_;
92
};
93
94
}  // namespace tablet
95
}  // namespace yb
96
97
#endif  // YB_TABLET_TABLET_RETENTION_POLICY_H_