YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/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
94
  virtual ~StateWithTablets() = default;
44
45
  StateWithTablets(const StateWithTablets&) = delete;
46
  void operator=(const StateWithTablets&) = delete;
47
48
71
  SnapshotCoordinatorContext& context() const {
49
71
    return context_;
50
71
  }
51
52
423
  SysSnapshotEntryPB::State initial_state() const {
53
423
    return initial_state_;
54
423
  }
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
57
  void InitTabletIds(const TabletIds& tablet_ids, SysSnapshotEntryPB::State state) {
77
57
    tablets_.clear();
78
81
    for (const auto& id : tablet_ids) {
79
81
      tablets_.emplace(id, state);
80
81
    }
81
57
    num_tablets_in_initial_state_ = state == initial_state_ ? tablet_ids.size() : 
00
;
82
57
    CheckCompleteness();
83
57
  }
void yb::master::StateWithTablets::InitTabletIds<std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, yb::master::SysSnapshotEntryPB_State)
Line
Count
Source
76
6
  void InitTabletIds(const TabletIds& tablet_ids, SysSnapshotEntryPB::State state) {
77
6
    tablets_.clear();
78
17
    for (const auto& id : tablet_ids) {
79
17
      tablets_.emplace(id, state);
80
17
    }
81
6
    num_tablets_in_initial_state_ = state == initial_state_ ? tablet_ids.size() : 
00
;
82
6
    CheckCompleteness();
83
6
  }
void yb::master::StateWithTablets::InitTabletIds<google::protobuf::RepeatedPtrField<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >(google::protobuf::RepeatedPtrField<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > const&, yb::master::SysSnapshotEntryPB_State)
Line
Count
Source
76
51
  void InitTabletIds(const TabletIds& tablet_ids, SysSnapshotEntryPB::State state) {
77
51
    tablets_.clear();
78
64
    for (const auto& id : tablet_ids) {
79
64
      tablets_.emplace(id, state);
80
64
    }
81
51
    num_tablets_in_initial_state_ = state == initial_state_ ? tablet_ids.size() : 
00
;
82
51
    CheckCompleteness();
83
51
  }
84
85
  // Initialize tablet states using tablet ids, i.e. put all tablets in initial state.
86
  template <class TabletIds>
87
6
  void InitTabletIds(const TabletIds& tablet_ids) {
88
6
    InitTabletIds(tablet_ids, initial_state_);
89
6
  }
90
91
  template <class PB>
92
98
  void TabletsToPB(google::protobuf::RepeatedPtrField<PB>* out) {
93
98
    out->Reserve(narrow_cast<int>(tablets_.size()));
94
145
    for (const auto& tablet : tablets_) {
95
145
      auto* tablet_state = out->Add();
96
145
      tablet_state->set_id(tablet.id);
97
145
      tablet_state->set_state(tablet.state);
98
145
    }
99
98
  }
void yb::master::StateWithTablets::TabletsToPB<yb::master::TabletRestorationPB>(google::protobuf::RepeatedPtrField<yb::master::TabletRestorationPB>*)
Line
Count
Source
92
5
  void TabletsToPB(google::protobuf::RepeatedPtrField<PB>* out) {
93
5
    out->Reserve(narrow_cast<int>(tablets_.size()));
94
22
    for (const auto& tablet : tablets_) {
95
22
      auto* tablet_state = out->Add();
96
22
      tablet_state->set_id(tablet.id);
97
22
      tablet_state->set_state(tablet.state);
98
22
    }
99
5
  }
void yb::master::StateWithTablets::TabletsToPB<yb::master::SysSnapshotEntryPB_TabletSnapshotPB>(google::protobuf::RepeatedPtrField<yb::master::SysSnapshotEntryPB_TabletSnapshotPB>*)
Line
Count
Source
92
93
  void TabletsToPB(google::protobuf::RepeatedPtrField<PB>* out) {
93
93
    out->Reserve(narrow_cast<int>(tablets_.size()));
94
123
    for (const auto& tablet : tablets_) {
95
123
      auto* tablet_state = out->Add();
96
123
      tablet_state->set_id(tablet.id);
97
123
      tablet_state->set_state(tablet.state);
98
123
    }
99
93
  }
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
273
  void DoPrepareOperations(const Functor& functor) {
105
273
    auto& running_index = tablets_.get<RunningTag>();
106
491
    for (auto it = running_index.begin(); it != running_index.end();) {
107
236
      if (it->running) {
108
        // Could exit here, because we have already iterated over all non-running operations.
109
18
        break;
110
18
      }
111
218
      bool should_run = it->state == initial_state_ && 
functor(*it)45
;
112
218
      if (should_run) {
113
38
        VLOG
(4) << "Prepare operation for " << it->ToString()0
;
114
115
        // Here we modify indexed value, so iterator could be advanced to the next element.
116
        // Taking next before modify.
117
38
        auto new_it = it;
118
38
        ++new_it;
119
38
        running_index.modify(it, [](TabletData& data) { data.running = true; });
restoration_state.cc:void yb::master::StateWithTablets::DoPrepareOperations<yb::master::RestorationState::PrepareOperations()::$_0>(yb::master::RestorationState::PrepareOperations()::$_0 const&)::'lambda'(yb::master::StateWithTablets::TabletData&)::operator()(yb::master::StateWithTablets::TabletData&) const
Line
Count
Source
119
10
        running_index.modify(it, [](TabletData& data) { data.running = true; });
snapshot_state.cc:void yb::master::StateWithTablets::DoPrepareOperations<yb::master::SnapshotState::PrepareOperations(std::__1::vector<yb::master::TabletSnapshotOperation, std::__1::allocator<yb::master::TabletSnapshotOperation> >*)::$_0>(yb::master::SnapshotState::PrepareOperations(std::__1::vector<yb::master::TabletSnapshotOperation, std::__1::allocator<yb::master::TabletSnapshotOperation> >*)::$_0 const&)::'lambda'(yb::master::StateWithTablets::TabletData&)::operator()(yb::master::StateWithTablets::TabletData&) const
Line
Count
Source
119
28
        running_index.modify(it, [](TabletData& data) { data.running = true; });
120
38
        it = new_it;
121
180
      } else {
122
180
        ++it;
123
180
      }
124
218
    }
125
273
  }
restoration_state.cc:void yb::master::StateWithTablets::DoPrepareOperations<yb::master::RestorationState::PrepareOperations()::$_0>(yb::master::RestorationState::PrepareOperations()::$_0 const&)
Line
Count
Source
104
3
  void DoPrepareOperations(const Functor& functor) {
105
3
    auto& running_index = tablets_.get<RunningTag>();
106
13
    for (auto it = running_index.begin(); it != running_index.end();) {
107
12
      if (it->running) {
108
        // Could exit here, because we have already iterated over all non-running operations.
109
2
        break;
110
2
      }
111
10
      bool should_run = it->state == initial_state_ && functor(*it);
112
10
      if (should_run) {
113
10
        VLOG
(4) << "Prepare operation for " << it->ToString()0
;
114
115
        // Here we modify indexed value, so iterator could be advanced to the next element.
116
        // Taking next before modify.
117
10
        auto new_it = it;
118
10
        ++new_it;
119
10
        running_index.modify(it, [](TabletData& data) { data.running = true; });
120
10
        it = new_it;
121
10
      } else {
122
0
        ++it;
123
0
      }
124
10
    }
125
3
  }
snapshot_state.cc:void yb::master::StateWithTablets::DoPrepareOperations<yb::master::SnapshotState::PrepareOperations(std::__1::vector<yb::master::TabletSnapshotOperation, std::__1::allocator<yb::master::TabletSnapshotOperation> >*)::$_0>(yb::master::SnapshotState::PrepareOperations(std::__1::vector<yb::master::TabletSnapshotOperation, std::__1::allocator<yb::master::TabletSnapshotOperation> >*)::$_0 const&)
Line
Count
Source
104
270
  void DoPrepareOperations(const Functor& functor) {
105
270
    auto& running_index = tablets_.get<RunningTag>();
106
478
    for (auto it = running_index.begin(); it != running_index.end();) {
107
224
      if (it->running) {
108
        // Could exit here, because we have already iterated over all non-running operations.
109
16
        break;
110
16
      }
111
208
      bool should_run = it->state == initial_state_ && 
functor(*it)35
;
112
208
      if (should_run) {
113
28
        VLOG
(4) << "Prepare operation for " << it->ToString()0
;
114
115
        // Here we modify indexed value, so iterator could be advanced to the next element.
116
        // Taking next before modify.
117
28
        auto new_it = it;
118
28
        ++new_it;
119
28
        running_index.modify(it, [](TabletData& data) { data.running = true; });
120
28
        it = new_it;
121
180
      } else {
122
180
        ++it;
123
180
      }
124
208
    }
125
270
  }
126
127
  void RemoveTablets(const std::vector<std::string>& tablet_ids);
128
129
9
  auto tablet_ids() const {
130
24
    auto lambda = [](const TabletData& data) { return data.id; };
131
9
    return boost::make_iterator_range(
132
9
        boost::make_transform_iterator(tablets_.begin(), lambda),
133
9
        boost::make_transform_iterator(tablets_.end(), lambda));
134
9
  }
135
136
  const std::string& LogPrefix() const;
137
138
  virtual bool IsTerminalFailure(const Status& status) = 0;
139
140
10
  virtual CHECKED_STATUS CheckDoneStatus(const Status& status) {
141
10
    return status;
142
10
  }
143
144
80
  bool Empty() {
145
80
    return tablets().empty();
146
80
  }
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
203
        : id(id_), state(state_) {
157
203
    }
158
159
21
    std::string ToString() const {
160
21
      return YB_STRUCT_TO_STRING(id, state, last_error, running);
161
21
    }
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
89
  const Tablets& tablets() const {
182
89
    return tablets_;
183
89
  }
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