1 /* 2 * Copyright (C) 2018 Codership Oy <info@codership.com> 3 * 4 * This file is part of wsrep-lib. 5 * 6 * Wsrep-lib is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * Wsrep-lib is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with wsrep-lib. If not, see <https://www.gnu.org/licenses/>. 18 */ 19 20 /** @file view.hpp 21 * 22 * 23 */ 24 25 26 #ifndef WSREP_VIEW_HPP 27 #define WSREP_VIEW_HPP 28 29 #include "id.hpp" 30 #include "seqno.hpp" 31 #include "gtid.hpp" 32 #include <vector> 33 #include <iostream> 34 35 namespace wsrep 36 { 37 class view 38 { 39 public: 40 enum status 41 { 42 primary, 43 non_primary, 44 disconnected 45 }; 46 class member 47 { 48 public: member(const wsrep::id & id,const std::string & name,const std::string & incoming)49 member(const wsrep::id& id, 50 const std::string& name, 51 const std::string& incoming) 52 : id_(id) 53 , name_(name) 54 , incoming_(incoming) 55 { 56 } id() const57 const wsrep::id& id() const { return id_; } name() const58 const std::string& name() const { return name_; } incoming() const59 const std::string& incoming() const { return incoming_; } 60 private: 61 wsrep::id id_; 62 std::string name_; 63 std::string incoming_; 64 }; 65 view()66 view() 67 : state_id_() 68 , view_seqno_() 69 , status_(disconnected) 70 , capabilities_() 71 , own_index_(-1) 72 , protocol_version_(0) 73 , members_() 74 { } view(const wsrep::gtid & state_id,wsrep::seqno view_seqno,enum wsrep::view::status status,int capabilities,ssize_t own_index,int protocol_version,const std::vector<wsrep::view::member> & members)75 view(const wsrep::gtid& state_id, 76 wsrep::seqno view_seqno, 77 enum wsrep::view::status status, 78 int capabilities, 79 ssize_t own_index, 80 int protocol_version, 81 const std::vector<wsrep::view::member>& members) 82 : state_id_(state_id) 83 , view_seqno_(view_seqno) 84 , status_(status) 85 , capabilities_(capabilities) 86 , own_index_(own_index) 87 , protocol_version_(protocol_version) 88 , members_(members) 89 { } 90 state_id() const91 wsrep::gtid state_id() const 92 { return state_id_; } 93 view_seqno() const94 wsrep::seqno view_seqno() const 95 { return view_seqno_; } 96 status() const97 wsrep::view::status status() const 98 { return status_; } 99 capabilities() const100 int capabilities() const 101 { return capabilities_; } 102 own_index() const103 ssize_t own_index() const 104 { return own_index_; } 105 106 /** 107 * Return true if the two views have the same membership 108 */ 109 bool equal_membership(const wsrep::view& other) const; 110 protocol_version() const111 int protocol_version() const 112 { return protocol_version_; } 113 members() const114 const std::vector<member>& members() const 115 { return members_; } 116 117 /** 118 * Return true if the view is final 119 */ final() const120 bool final() const 121 { 122 return (members_.empty() && own_index_ == -1); 123 } 124 125 /** 126 * Return member index in the view. 127 * 128 * @return Member index if found, -1 if member is not present 129 * in the view. 130 */ 131 int member_index(const wsrep::id& member_id) const; 132 133 /** 134 * Return true if id is member of this view 135 */ is_member(const wsrep::id & id) const136 bool is_member(const wsrep::id& id) const 137 { 138 return member_index(id) != -1; 139 } 140 141 void print(std::ostream& os) const; 142 143 private: 144 wsrep::gtid state_id_; 145 wsrep::seqno view_seqno_; 146 enum wsrep::view::status status_; 147 int capabilities_; 148 ssize_t own_index_; 149 int protocol_version_; 150 std::vector<wsrep::view::member> members_; 151 }; 152 153 static inline operator <<(std::ostream & os,const wsrep::view & v)154 std::ostream& operator<<(std::ostream& os, const wsrep::view& v) 155 { 156 v.print(os); return os; 157 } 158 to_c_string(enum wsrep::view::status status)159 static inline const char* to_c_string(enum wsrep::view::status status) 160 { 161 switch(status) 162 { 163 case wsrep::view::primary: return "primary"; 164 case wsrep::view::non_primary: return "non-primary"; 165 case wsrep::view::disconnected: return "disconnected"; 166 } 167 return "invalid status"; 168 } 169 } 170 171 #endif // WSREP_VIEW 172