0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
RangeServerConnection.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3 of the
9  * License, or any later version.
10  *
11  * Hypertable 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 this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
22 #include "Common/Compat.h"
23 #include "Common/Serialization.h"
24 
25 #include "MetaLogEntityTypes.h"
26 #include "RangeServerConnection.h"
27 
28 using namespace Hypertable;
29 using namespace std;
30 
32  const String &hostname,
33  InetAddr public_addr)
34  : MetaLog::Entity(MetaLog::EntityType::RANGE_SERVER_CONNECTION),
35  m_handle(0), m_hyperspace_callback(0), m_location(location),
36  m_hostname(hostname), m_state(RangeServerConnectionFlags::INIT),
37  m_public_addr(public_addr), m_disk_fill_percentage(0.0),
38  m_connected(false), m_recovering(false) {
40 }
41 
43  : MetaLog::Entity(header_), m_handle(0), m_hyperspace_callback(0),
44  m_disk_fill_percentage(0.0), m_connected(false), m_recovering(false) {
46 }
47 
48 
49 bool RangeServerConnection::connect(const String &hostname,
50  InetAddr local_addr, InetAddr public_addr) {
51  lock_guard<mutex> lock(m_mutex);
52 
62  if (!m_connected) {
63  m_connected = true;
64  return true;
65  }
66  return false;
67 }
68 
70  lock_guard<mutex> lock(m_mutex);
71  if (m_connected) {
72  m_connected = false;
73  return true;
74  }
75  return false;
76 }
77 
78 
80  lock_guard<mutex> lock(m_mutex);
81  return m_connected;
82 }
83 
84 
86  lock_guard<mutex> lock(m_mutex);
88  return;
89  m_connected = false;
91 }
92 
94  lock_guard<mutex> lock(m_mutex);
96 }
97 
99  lock_guard<mutex> lock(m_mutex);
101  return false;
103  return true;
104 }
105 
107  lock_guard<mutex> lock(m_mutex);
109 }
110 
112  lock_guard<mutex> lock(m_mutex);
113  return m_recovering;
114 }
115 
117  lock_guard<mutex> lock(m_mutex);
118  m_recovering = b;
119 }
120 
123  lock_guard<mutex> lock(m_mutex);
124  m_handle = handle;
126 }
127 
130  lock_guard<mutex> lock(m_mutex);
131  *handle = m_handle;
132  *cb = m_hyperspace_callback;
133  return m_hyperspace_callback;
134  return m_handle;
135 }
136 
137 
138 
140  lock_guard<mutex> lock(m_mutex);
141  return m_comm_addr;
142 }
143 
145  string str("{RangeServerConnection ");
146  str.append(m_location);
147  str.append(" ");
148  str.append(m_hostname);
149  str.append(" ");
150  str.append(m_public_addr.format());
151  str.append(" (");
152  str.append(m_local_addr.format());
153  str.append(")");
154  if (m_state) {
155  str.append(" ");
156  bool continuation = false;
158  str.append("REMOVED");
159  continuation = true;
160  }
162  if (continuation)
163  str.append("|");
164  str.append("BALANCED");
165  continuation = true;
166  }
167  }
168  str.append("}");
169  return str;
170 }
171 
172 
173 void RangeServerConnection::display(std::ostream &os) {
174  os << " " << m_location << " (" << m_hostname << ") state=";
175  if (m_state) {
176  bool continuation = false;
178  os << "REMOVED";
179  continuation = true;
180  }
182  if (continuation)
183  os << "|";
184  os << "BALANCED";
185  continuation = true;
186  }
187  }
188  else
189  os << "0";
190  os << " ";
191 }
192 
193 void RangeServerConnection::decode(const uint8_t **bufp, size_t *remainp,
194  uint16_t definition_version) {
195  if (definition_version <= 3) {
196  if (definition_version >= 2)
197  Serialization::decode_i16(bufp, remainp); // currently not used
198  decode_old(bufp, remainp);
199  return;
200  }
201  Entity::decode(bufp, remainp);
202 }
203 
204 
206  return 1;
207 }
208 
210  return 4 + m_public_addr.encoded_length() +
213 }
214 
215 void RangeServerConnection::encode_internal(uint8_t **bufp) const {
217  m_public_addr.encode(bufp);
220 }
221 
222 void RangeServerConnection::decode_internal(uint8_t version, const uint8_t **bufp,
223  size_t *remainp) {
224  m_state = Serialization::decode_i32(bufp, remainp);
225  m_public_addr.decode(bufp, remainp);
226  m_location = Serialization::decode_vstr(bufp, remainp);
227  m_hostname = Serialization::decode_vstr(bufp, remainp);
229 }
230 
231 void RangeServerConnection::decode_old(const uint8_t **bufp, size_t *remainp) {
232  m_state = Serialization::decode_i32(bufp, remainp);
233  // was removal_time but not used ...
234  Serialization::decode_i32(bufp, remainp);
235  m_public_addr.legacy_decode(bufp, remainp);
236  m_location = Serialization::decode_vstr(bufp, remainp);
237  m_hostname = Serialization::decode_vstr(bufp, remainp);
239 }
char * decode_vstr(const uint8_t **bufp, size_t *remainp)
Decode a vstr (vint64, data, null).
Master MetaLog entity type constants.
void decode(const uint8_t **bufp, size_t *remainp, uint16_t definition_version) override
Decodes serialized RangeServerConnection object.
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
size_t encoded_length_internal() const override
Returns internal serialized length.
void decode(const uint8_t **bufp, size_t *remainp)
Reads serialized representation of object from a buffer.
Definition: InetAddr.cc:261
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
std::mutex m_mutex
Mutex for serializing access to members
STL namespace.
void decode_old(const uint8_t **bufp, size_t *remainp)
size_t encoded_length_vstr(size_t len)
Computes the encoded length of vstr (vint64, data, null)
uint32_t decode_i32(const uint8_t **bufp, size_t *remainp)
Decode a 32-bit integer in little-endian order.
Callback class for range server lock files.
RangeServerHyperspaceCallback * m_hyperspace_callback
Encapsulate an internet address.
Definition: InetAddr.h:66
uint16_t decode_i16(const uint8_t **bufp, size_t *remainp)
Decode a 16-bit integer in little-endian order.
const char * INIT
Definition: Operation.cc:45
void set_proxy(const String &str)
Sets address type to CommAddress::PROXY and proxy name to p.
Definition: CommAddress.h:76
void set_hyperspace_handle(uint64_t handle, RangeServerHyperspaceCallback *cb)
void encode_i32(uint8_t **bufp, uint32_t val)
Encode a 32-bit integer in little-endian order.
Compatibility Macros for C/C++.
Functions to serialize/deserialize primitives to/from a memory buffer.
void encode(uint8_t **bufp) const
Writes serialized representation of object to a buffer.
Definition: InetAddr.cc:255
void display(std::ostream &os) override
Prints a textual representation of the entity state to an ostream.
String format(int sep= ':') const
Returns a string with a dotted notation ("127.0.0.1:8080") including the port.
Definition: InetAddr.h:132
void decode_internal(uint8_t version, const uint8_t **bufp, size_t *remainp) override
Reads serialized representation of object from a buffer.
void encode_internal(uint8_t **bufp) const override
Writes serialized representation of object to a buffer.
void lock()
Locks the entity's mutex.
Definition: MetaLogEntity.h:97
void encode_vstr(uint8_t **bufp, const void *buf, size_t len)
Encode a buffer as variable length string (vint64, data, null)
Hypertable definitions
uint8_t encoding_version() const override
Returns encoding version.
size_t encoded_length() const
Returns serialized object length.
Definition: InetAddr.cc:228
void legacy_decode(const uint8_t **bufp, size_t *remainp)
Deserializes object from legacy serialization format.
Definition: InetAddr.cc:277
bool connect(const String &hostname, InetAddr local_addr, InetAddr public_addr)
bool get_hyperspace_handle(uint64_t *handle, RangeServerHyperspaceCallback **cb)
RangeServerConnection(const String &location, const String &hostname, InetAddr public_addr)
Address abstraction to hold either proxy name or IPv4:port address.
Definition: CommAddress.h:52