/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 | 67.6k | : reason(in_reason) { |
25 | 67.6k | } |
26 | | |
27 | | StateChangeContext(StateChangeReason in_reason, bool is_locked) |
28 | | : reason(in_reason), |
29 | 177k | is_config_locked_(is_locked) { |
30 | 177k | } |
31 | | |
32 | | StateChangeContext(StateChangeReason in_reason, string uuid) |
33 | | : reason(in_reason), |
34 | 103k | new_leader_uuid(uuid) { |
35 | 103k | } |
36 | | |
37 | | StateChangeContext(StateChangeReason in_reason, |
38 | | ChangeConfigRecordPB change_rec, |
39 | | string remove = "") |
40 | | : reason(in_reason), |
41 | | change_record(change_rec), |
42 | 10.2k | remove_uuid(remove) { |
43 | 10.2k | } |
44 | | |
45 | 19.3k | bool is_config_locked() const { |
46 | 19.3k | return is_config_locked_; |
47 | 19.3k | } |
48 | | |
49 | 358k | ~StateChangeContext() {} |
50 | | |
51 | 19.7k | std::string ToString() const { |
52 | 19.7k | switch (reason) { |
53 | 5.36k | case StateChangeReason::TABLET_PEER_STARTED: |
54 | 5.36k | return "Started TabletPeer"; |
55 | 5.36k | case StateChangeReason::CONSENSUS_STARTED: |
56 | 5.36k | return "RaftConsensus started"; |
57 | 5.19k | case StateChangeReason::NEW_LEADER_ELECTED: |
58 | 5.19k | return strings::Substitute("New leader $0 elected", new_leader_uuid); |
59 | 3.14k | case StateChangeReason::FOLLOWER_NO_OP_COMPLETE: |
60 | 3.14k | return "Replicate of NO_OP complete on follower"; |
61 | 232 | case StateChangeReason::LEADER_CONFIG_CHANGE_COMPLETE: |
62 | 232 | return strings::Substitute("Replicated change config $0 round complete on leader", |
63 | 232 | change_record.ShortDebugString()); |
64 | 496 | case StateChangeReason::FOLLOWER_CONFIG_CHANGE_COMPLETE: |
65 | 496 | return strings::Substitute("Config change $0 complete on follower", |
66 | 496 | change_record.ShortDebugString()); |
67 | 0 | case StateChangeReason::INVALID_REASON: FALLTHROUGH_INTENDED; |
68 | 0 | default: |
69 | 0 | return "INVALID REASON"; |
70 | 19.7k | } |
71 | 19.7k | } |
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 |