YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/consensus/state_change_context.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_CONSENSUS_STATE_CHANGE_CONTEXT_H
15
#define YB_CONSENSUS_STATE_CHANGE_CONTEXT_H
16
17
namespace yb {
18
namespace consensus {
19
20
// Context provided for callback on master/tablet-server peer state change for post processing
21
// e.g., update in-memory contents.
22
struct StateChangeContext {
23
  explicit StateChangeContext(StateChangeReason in_reason)
24
119k
      : reason(in_reason) {
25
119k
  }
26
27
  StateChangeContext(StateChangeReason in_reason, bool is_locked)
28
      : reason(in_reason),
29
300k
        is_config_locked_(is_locked) {
30
300k
  }
31
32
  StateChangeContext(StateChangeReason in_reason, string uuid)
33
      : reason(in_reason),
34
182k
        new_leader_uuid(uuid) {
35
182k
  }
36
37
  StateChangeContext(StateChangeReason in_reason,
38
                     ChangeConfigRecordPB change_rec,
39
                     string remove = "")
40
      : reason(in_reason),
41
        change_record(change_rec),
42
19.8k
        remove_uuid(remove) {
43
19.8k
  }
44
45
28.3k
  bool is_config_locked() const {
46
28.3k
    return is_config_locked_;
47
28.3k
  }
48
49
622k
  ~StateChangeContext() {}
50
51
28.9k
  std::string ToString() const {
52
28.9k
    switch (reason) {
53
7.97k
      case StateChangeReason::TABLET_PEER_STARTED:
54
7.97k
        return "Started TabletPeer";
55
7.97k
      case StateChangeReason::CONSENSUS_STARTED:
56
7.97k
        return "RaftConsensus started";
57
7.51k
      case StateChangeReason::NEW_LEADER_ELECTED:
58
7.51k
        return strings::Substitute("New leader $0 elected", new_leader_uuid);
59
4.43k
      case StateChangeReason::FOLLOWER_NO_OP_COMPLETE:
60
4.43k
        return "Replicate of NO_OP complete on follower";
61
328
      case StateChangeReason::LEADER_CONFIG_CHANGE_COMPLETE:
62
328
        return strings::Substitute("Replicated change config $0 round complete on leader",
63
328
          change_record.ShortDebugString());
64
688
      case StateChangeReason::FOLLOWER_CONFIG_CHANGE_COMPLETE:
65
688
        return strings::Substitute("Config change $0 complete on follower",
66
688
          change_record.ShortDebugString());
67
0
      case StateChangeReason::INVALID_REASON: FALLTHROUGH_INTENDED;
68
0
      default:
69
0
        return "INVALID REASON";
70
28.9k
    }
71
28.9k
  }
72
73
  const StateChangeReason reason;
74
75
  // Auxiliary info for some of the reasons above.
76
  // Value is filled when the change reason is NEW_LEADER_ELECTED.
77
  const string new_leader_uuid;
78
79
  // Value is filled when the change reason is LEADER/FOLLOWER_CONFIG_CHANGE_COMPLETE.
80
  const ChangeConfigRecordPB change_record;
81
82
  // Value is filled when the change reason is LEADER_CONFIG_CHANGE_COMPLETE
83
  // and it is a REMOVE_SERVER, then that server's uuid is saved here by the master leader.
84
  const string remove_uuid;
85
86
  // If this is true, the call-stack above has taken the lock for the raft consensus state. Needed
87
  // in SysCatalogStateChanged for master to not re-get the lock. Not used for tserver callback.
88
  // Note that the state changes using the UpdateConsensus() mechanism always hold the lock, so
89
  // defaulting to true as they are majority. For ones that do not hold the lock, setting
90
  // it to false in their constructor suffices currently, so marking it const.
91
  const bool is_config_locked_ = true;
92
};
93
94
} // namespace consensus
95
} // namespace yb
96
97
#endif // YB_CONSENSUS_STATE_CHANGE_CONTEXT_H