YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/master/state_with_tablets.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_MASTER_STATE_WITH_TABLETS_H
15
#define YB_MASTER_STATE_WITH_TABLETS_H
16
17
#include <boost/iterator/transform_iterator.hpp>
18
#include <boost/multi_index/hashed_index.hpp>
19
#include <boost/multi_index/member.hpp>
20
#include <boost/multi_index/ordered_index.hpp>
21
#include <boost/multi_index_container.hpp>
22
#include <boost/range/iterator_range_core.hpp>
23
#include <glog/logging.h>
24
25
#include "yb/gutil/casts.h"
26
27
#include "yb/master/master_fwd.h"
28
#include "yb/master/catalog_entity_info.pb.h"
29
30
#include "yb/util/monotime.h"
31
#include "yb/util/status.h"
32
#include "yb/util/tostring.h"
33
34
namespace yb {
35
namespace master {
36
37
class StateWithTablets {
38
 public:
39
  StateWithTablets(
40
      SnapshotCoordinatorContext* context, SysSnapshotEntryPB::State initial_state,
41
      std::string log_prefix);
42
43
0
  virtual ~StateWithTablets() = default;
44
45
  StateWithTablets(const StateWithTablets&) = delete;
46
  void operator=(const StateWithTablets&) = delete;
47
48
0
  SnapshotCoordinatorContext& context() const {
49
0
    return context_;
50
0
  }
51
52
0
  SysSnapshotEntryPB::State initial_state() const {
53
0
    return initial_state_;
54
0
  }
55
56
  // If any of tablets failed returns this failure.
57
  // Otherwise if any of tablets is in initial state returns initial state.
58
  // Otherwise all tablets should be in the same state, which is returned.
59
  Result<SysSnapshotEntryPB::State> AggregatedState() const;
60
61
  CHECKED_STATUS AnyFailure() const;
62
  Result<bool> Complete() const;
63
  bool AllTabletsDone() const;
64
  bool PassedSinceCompletion(const MonoDelta& duration) const;
65
  std::vector<TabletId> TabletIdsInState(SysSnapshotEntryPB::State state);
66
  void Done(const TabletId& tablet_id, Status status);
67
  bool AllInState(SysSnapshotEntryPB::State state);
68
  bool HasInState(SysSnapshotEntryPB::State state);
69
  void SetInitialTabletsState(SysSnapshotEntryPB::State state);
70
71
  // Initialize tablet states from serialized data.
72
  void InitTablets(
73
      const google::protobuf::RepeatedPtrField<SysSnapshotEntryPB::TabletSnapshotPB>& tablets);
74
75
  template <class TabletIds>
76
0
  void InitTabletIds(const TabletIds& tablet_ids, SysSnapshotEntryPB::State state) {
77
0
    tablets_.clear();
78
0
    for (const auto& id : tablet_ids) {
79
0
      tablets_.emplace(id, state);
80
0
    }
81
0
    num_tablets_in_initial_state_ = state == initial_state_ ? tablet_ids.size() : 0;
82
0
    CheckCompleteness();
83
0
  }
Unexecuted instantiation: _ZN2yb6master16StateWithTablets13InitTabletIdsINSt3__16vectorINS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEENS8_ISA_EEEEEEvRKT_NS0_24SysSnapshotEntryPB_StateE
Unexecuted instantiation: _ZN2yb6master16StateWithTablets13InitTabletIdsIN6google8protobuf16RepeatedPtrFieldINSt3__112basic_stringIcNS6_11char_traitsIcEENS6_9allocatorIcEEEEEEEEvRKT_NS0_24SysSnapshotEntryPB_StateE
84
85
  // Initialize tablet states using tablet ids, i.e. put all tablets in initial state.
86
  template <class TabletIds>
87
0
  void InitTabletIds(const TabletIds& tablet_ids) {
88
0
    InitTabletIds(tablet_ids, initial_state_);
89
0
  }
90
91
  template <class PB>
92
0
  void TabletsToPB(google::protobuf::RepeatedPtrField<PB>* out) {
93
0
    out->Reserve(narrow_cast<int>(tablets_.size()));
94
0
    for (const auto& tablet : tablets_) {
95
0
      auto* tablet_state = out->Add();
96
0
      tablet_state->set_id(tablet.id);
97
0
      tablet_state->set_state(tablet.state);
98
0
    }
99
0
  }
Unexecuted instantiation: _ZN2yb6master16StateWithTablets11TabletsToPBINS0_19TabletRestorationPBEEEvPN6google8protobuf16RepeatedPtrFieldIT_EE
Unexecuted instantiation: _ZN2yb6master16StateWithTablets11TabletsToPBINS0_35SysSnapshotEntryPB_TabletSnapshotPBEEEvPN6google8protobuf16RepeatedPtrFieldIT_EE
100
101
  // Invoking callback for all operations that are not running and are still in the initial state.
102
  // Marking such operations as running.
103
  template <class Functor>
104
0
  void DoPrepareOperations(const Functor& functor) {
105
0
    auto& running_index = tablets_.get<RunningTag>();
106
0
    for (auto it = running_index.begin(); it != running_index.end();) {
107
0
      if (it->running) {
108
        // Could exit here, because we have already iterated over all non-running operations.
109
0
        break;
110
0
      }
111
0
      bool should_run = it->state == initial_state_ && functor(*it);
112
0
      if (should_run) {
113
0
        VLOG(4) << "Prepare operation for " << it->ToString();
114
115
        // Here we modify indexed value, so iterator could be advanced to the next element.
116
        // Taking next before modify.
117
0
        auto new_it = it;
118
0
        ++new_it;
119
0
        running_index.modify(it, [](TabletData& data) { data.running = true; });
Unexecuted instantiation: restoration_state.cc:_ZZN2yb6master16StateWithTablets19DoPrepareOperationsIZNS0_16RestorationState17PrepareOperationsEvE3$_0EEvRKT_ENKUlRNS1_10TabletDataEE_clES9_
Unexecuted instantiation: snapshot_state.cc:_ZZN2yb6master16StateWithTablets19DoPrepareOperationsIZNS0_13SnapshotState17PrepareOperationsEPNSt3__16vectorINS0_23TabletSnapshotOperationENS4_9allocatorIS6_EEEEE3$_0EEvRKT_ENKUlRNS1_10TabletDataEE_clESG_
120
0
        it = new_it;
121
0
      } else {
122
0
        ++it;
123
0
      }
124
0
    }
125
0
  }
Unexecuted instantiation: restoration_state.cc:_ZN2yb6master16StateWithTablets19DoPrepareOperationsIZNS0_16RestorationState17PrepareOperationsEvE3$_0EEvRKT_
Unexecuted instantiation: snapshot_state.cc:_ZN2yb6master16StateWithTablets19DoPrepareOperationsIZNS0_13SnapshotState17PrepareOperationsEPNSt3__16vectorINS0_23TabletSnapshotOperationENS4_9allocatorIS6_EEEEE3$_0EEvRKT_
126
127
  void RemoveTablets(const std::vector<std::string>& tablet_ids);
128
129
0
  auto tablet_ids() const {
130
0
    auto lambda = [](const TabletData& data) { return data.id; };
131
0
    return boost::make_iterator_range(
132
0
        boost::make_transform_iterator(tablets_.begin(), lambda),
133
0
        boost::make_transform_iterator(tablets_.end(), lambda));
134
0
  }
135
136
  const std::string& LogPrefix() const;
137
138
  virtual bool IsTerminalFailure(const Status& status) = 0;
139
140
0
  virtual CHECKED_STATUS CheckDoneStatus(const Status& status) {
141
0
    return status;
142
0
  }
143
144
0
  bool Empty() {
145
0
    return tablets().empty();
146
0
  }
147
148
 protected:
149
  struct TabletData {
150
    TabletId id;
151
    SysSnapshotEntryPB::State state;
152
    Status last_error;
153
    bool running = false;
154
155
    TabletData(const TabletId& id_, SysSnapshotEntryPB::State state_)
156
0
        : id(id_), state(state_) {
157
0
    }
158
159
0
    std::string ToString() const {
160
0
      return YB_STRUCT_TO_STRING(id, state, last_error, running);
161
0
    }
162
  };
163
164
  const std::string& InitialStateName() const;
165
166
  class RunningTag;
167
168
  typedef boost::multi_index_container<
169
    TabletData,
170
    boost::multi_index::indexed_by<
171
      boost::multi_index::hashed_unique<
172
        boost::multi_index::member<TabletData, TabletId, &TabletData::id>
173
      >,
174
      boost::multi_index::ordered_non_unique<
175
        boost::multi_index::tag<RunningTag>,
176
        boost::multi_index::member<TabletData, bool, &TabletData::running>
177
      >
178
    >
179
  > Tablets;
180
181
0
  const Tablets& tablets() const {
182
0
    return tablets_;
183
0
  }
184
185
 private:
186
  void CheckCompleteness();
187
188
  SnapshotCoordinatorContext& context_;
189
  SysSnapshotEntryPB::State initial_state_;
190
  const std::string log_prefix_;
191
192
  Tablets tablets_;
193
194
  size_t num_tablets_in_initial_state_ = 0;
195
  // Time when last tablet were transferred from initial state.
196
  CoarseTimePoint complete_at_;
197
};
198
199
} // namespace master
200
} // namespace yb
201
202
#endif  // YB_MASTER_STATE_WITH_TABLETS_H