MongoDB C++ Driver  legacy-1.1.2
member_state.h
1 /*
2  * Copyright (C) 2010 10gen Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
19 #pragma once
20 
21 #include <string>
22 
23 namespace mongo {
24 namespace repl {
25 
26 
27 /*
28  RS_STARTUP serving still starting up, or still trying to initiate the set
29  RS_PRIMARY this server thinks it is primary
30  RS_SECONDARY this server thinks it is a secondary (slave mode)
31  RS_RECOVERING recovering/resyncing; after recovery usually auto-transitions to secondary
32  RS_STARTUP2 loaded config, still determining who is primary
33 
34  State -> integer mappings are reserved forever. Do not change them or delete them, except
35  to update RS_MAX when introducing new states.
36 */
37 struct MemberState {
38  enum MS {
39  RS_STARTUP = 0,
40  RS_PRIMARY = 1,
41  RS_SECONDARY = 2,
42  RS_RECOVERING = 3,
43  RS_STARTUP2 = 5,
44  RS_UNKNOWN = 6, /* remote node not yet reached */
45  RS_ARBITER = 7,
46  RS_DOWN = 8, /* node not reachable for a report */
47  RS_ROLLBACK = 9,
48  RS_REMOVED = 10, /* node removed from replica set */
49  RS_MAX = 10
50  } s;
51 
52  MemberState(MS ms = RS_UNKNOWN) : s(ms) {}
53  explicit MemberState(int ms) : s((MS)ms) {}
54 
55  bool startup() const {
56  return s == RS_STARTUP;
57  }
58  bool primary() const {
59  return s == RS_PRIMARY;
60  }
61  bool secondary() const {
62  return s == RS_SECONDARY;
63  }
64  bool recovering() const {
65  return s == RS_RECOVERING;
66  }
67  bool startup2() const {
68  return s == RS_STARTUP2;
69  }
70  bool rollback() const {
71  return s == RS_ROLLBACK;
72  }
73  bool readable() const {
74  return s == RS_PRIMARY || s == RS_SECONDARY;
75  }
76  bool removed() const {
77  return s == RS_REMOVED;
78  }
79  bool arbiter() const {
80  return s == RS_ARBITER;
81  }
82 
83  std::string toString() const;
84 
85  bool operator==(const MemberState& r) const {
86  return s == r.s;
87  }
88  bool operator!=(const MemberState& r) const {
89  return s != r.s;
90  }
91 };
92 
93 inline std::string MemberState::toString() const {
94  switch (s) {
95  case RS_STARTUP:
96  return "STARTUP";
97  case RS_PRIMARY:
98  return "PRIMARY";
99  case RS_SECONDARY:
100  return "SECONDARY";
101  case RS_RECOVERING:
102  return "RECOVERING";
103  case RS_STARTUP2:
104  return "STARTUP2";
105  case RS_ARBITER:
106  return "ARBITER";
107  case RS_DOWN:
108  return "DOWN";
109  case RS_ROLLBACK:
110  return "ROLLBACK";
111  case RS_UNKNOWN:
112  return "UNKNOWN";
113  case RS_REMOVED:
114  return "REMOVED";
115  }
116  return "";
117 }
118 
119 } // namespace repl
120 } // namespace mongo
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
Definition: member_state.h:37