0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
LocationInitializer.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.
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 
26 
27 #include <Common/Compat.h>
28 #include "LocationInitializer.h"
29 
34 
35 #include <Hyperspace/Session.h>
36 
37 #include <Common/Config.h>
38 #include <Common/FileUtils.h>
39 #include <Common/Logger.h>
40 #include <Common/Serialization.h>
41 #include <Common/StatsSystem.h>
42 #include <Common/Path.h>
43 #include <Common/md5.h>
44 #include <Common/ScopeGuard.h>
45 
46 #include <boost/algorithm/string.hpp>
47 
48 using namespace Hypertable;
49 using namespace Hypertable::Lib;
50 using namespace Serialization;
51 using namespace std;
52 
53 LocationInitializer::LocationInitializer(std::shared_ptr<Context> &context)
54  : m_context(context) {
55 
56  Path data_dir = m_context->props->get_str("Hypertable.DataDirectory");
57  data_dir /= "/run";
58  if (!FileUtils::exists(data_dir.string()))
59  FileUtils::mkdirs(data_dir.string());
60  m_location_file = (data_dir /= "/location").string();
61 
62  // Get location string
63  {
64  m_location = m_context->props->get_str("Hypertable.RangeServer.ProxyName");
65  if (!m_location.empty()) {
66  boost::trim(m_location);
67  if (m_location == "*") {
68  char location_hash[33];
69  uint16_t port = m_context->props->get_i16("Hypertable.RangeServer.Port");
70  md5_string(format("%s:%u", System::net_info().host_name.c_str(), port).c_str(), location_hash);
71  m_location = format("rs-%s", String(location_hash).substr(0, 8).c_str());
72  }
73  }
74  else if (FileUtils::exists(m_location_file)) {
75  if (!FileUtils::read(m_location_file, m_location)) {
76  HT_ERRORF("Problem reading location file '%s'", m_location_file.c_str());
77  quick_exit(EXIT_FAILURE);
78  }
79  boost::trim(m_location);
80  if (!m_location.empty())
81  m_location_persisted = true;
82  }
83  }
84 
85 }
86 
88  bool removed = false;
89  if (!m_location.empty()) {
90  String filename=path + "/" + m_location;
91  uint64_t handle=0;
92  HT_ON_SCOPE_EXIT(&Hyperspace::close_handle_ptr, hyperspace, &handle);
93  uint32_t oflags = Hyperspace::OPEN_FLAG_READ;
94  try {
95  handle = hyperspace->open(filename, oflags);
96  if (hyperspace->attr_exists(handle, "removed"))
97  removed = true;
98  }
99  catch (Exception &e) {
101  HT_FATAL_OUT << e << HT_END;
102  }
103  }
104  return removed;
105 }
106 
108  lock_guard<mutex> lock(m_mutex);
109  StatsSystem stats;
110  const char *base, *ptr;
111  String datadirs = m_context->props->get_str("Hypertable.RangeServer.Monitoring.DataDirectories");
112  uint16_t port = m_context->props->get_i16("Hypertable.RangeServer.Port");
113  String dir;
114  std::vector<String> dirs;
115 
116  base = datadirs.c_str();
117  while ((ptr = strchr(base, ',')) != 0) {
118  dir = String(base, ptr-base);
119  boost::trim(dir);
120  dirs.push_back(dir);
121  base = ptr+1;
122  }
123  dir = String(base);
124  boost::trim(dir);
125  dirs.push_back(dir);
126 
129 
130 
131  CommHeader header(Master::Protocol::COMMAND_REGISTER_SERVER);
133  stats);
134  CommBuf *cbuf = new CommBuf(header, params.encoded_length());
135  params.encode(cbuf->get_data_ptr_address());
136 
137  return cbuf;
138 }
139 
141  int error = Protocol::response_code(event);
142 
143  if (error != Error::OK) {
144  HT_ERROR_OUT << "Problem initializing Master connection - "
146  return false;
147  }
148 
149  bool location_persisted = false;
150 
151  // Decode response
152  const uint8_t *ptr = event->payload + 4;
153  size_t remain = event->payload_len - 4;
155  params.decode(&ptr, &remain);
156 
157  // Update server state
158  m_context->server_state->set(params.generation(), params.variables());
159 
160  {
161  lock_guard<mutex> lock(m_mutex);
162  if (m_location == "")
163  m_location = params.location();
164  else
165  HT_ASSERT(m_location == params.location());
166  location_persisted = m_location_persisted;
167  m_handshake_complete = true;
168  }
169 
170  if (!location_persisted) {
171  if (FileUtils::write(m_location_file, params.location()) < 0) {
172  HT_ERRORF("Unable to write location to file '%s'", m_location_file.c_str());
173  quick_exit(EXIT_FAILURE);
174  }
175  {
176  lock_guard<mutex> lock(m_mutex);
177  m_location_persisted = true;
178  }
179  }
180  m_cond.notify_all();
181  return true;
182 }
183 
185  lock_guard<mutex> lock(m_mutex);
186  return m_location;
187 }
188 
190  unique_lock<mutex> lock(m_mutex);
191  m_cond.wait(lock, [this](){ return m_handshake_complete; });
192 }
LocationInitializer(std::shared_ptr< Context > &context)
Constructor.
Network communication event.
Definition: Event.h:54
static String filename
Definition: Config.cc:48
String m_location_file
Local pathname to location file.
static int32_t response_code(const Event *event)
Returns the response code from an event event generated in response to a request message.
Definition: Protocol.cc:39
static bool read(const String &fname, String &contents)
Reads a whole file into a String.
Definition: FileUtils.cc:59
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
Compatibility class for boost::filesystem::path.
static String string_format_message(const Event *event)
Returns error message decoded standard error MESSAGE generated in response to a request message...
Definition: Protocol.cc:51
String format(const char *fmt,...)
Returns a String using printf like format facilities Vanilla snprintf is about 1.5x faster than this...
Definition: String.cc:37
virtual size_t encoded_length() const
Returns serialized object length.
Definition: Serializable.cc:37
Declarations for SystemVariable.
Declarations for Protocol.
bool process_initialization_response(Event *event) override
Process response to initialization message.
static ssize_t write(const String &fname, const std::string &contents)
Writes a String buffer to a file; the file is overwritten if it already exists.
Definition: FileUtils.cc:124
static bool exists(const String &fname)
Checks if a file or directory exists.
Definition: FileUtils.cc:420
STL namespace.
uint8_t ** get_data_ptr_address()
Returns address of the primary buffer internal data pointer.
Definition: CommBuf.h:160
static bool mkdirs(const String &dirname)
Creates a directory (with all parent directories, if required)
Definition: FileUtils.cc:366
Request parameters for register server operation.
Collecting and (de)serializing system-wide statistics.
#define HT_ON_SCOPE_EXIT(...)
Definition: ScopeGuard.h:301
CommBuf * create_initialization_request() override
Creates a connection initialization message.
std::condition_variable m_cond
Condition variable signalling completion of initialization handshake.
#define HT_ASSERT(_e_)
Definition: Logger.h:396
Compatibility class for boost::filesystem::path.
Definition: Path.h:45
File system utility functions.
virtual void encode(uint8_t **bufp) const
Writes serialized representation of object to a buffer.
Definition: Serializable.cc:64
std::shared_ptr< Session > SessionPtr
Definition: Session.h:734
Logging routines and macros.
void close_handle_ptr(SessionPtr hyperspace, uint64_t *handlep)
Definition: Session.cc:1400
Compatibility Macros for C/C++.
#define HT_END
Definition: Logger.h:220
Functions to serialize/deserialize primitives to/from a memory buffer.
void md5_string(const char *input, char output[33])
Calculates the hex string of MD5 of null terminated input.
Definition: md5.cc:384
String m_location
Assigned location (proxy name)
#define HT_ERROR_OUT
Definition: Logger.h:301
int64_t generation()
Gets system state generation.
virtual void decode(const uint8_t **bufp, size_t *remainp)
Reads serialized representation of object from a buffer.
Definition: Serializable.cc:70
bool m_location_persisted
Flag indicating if assigned location has been written to location file.
Collects, serializes and deserializes system-wide statistics.
Definition: StatsSystem.h:43
Hypertable library.
Definition: CellInterval.h:30
Hypertable definitions
const string & location() const
Gets location (proxy name)
Header for messages transmitted via AsyncComm.
Definition: CommHeader.h:40
virtual bool is_removed(const String &path, Hyperspace::SessionPtr &hyperspace)
Checks if "removed" attribute is set on Hyperspace location file.
void wait_for_handshake()
Waits for completion of initialization handshake.
bool m_lock_held
Flag indicating that Hyperspace lock on location file is held.
std::mutex m_mutex
Mutex for serializing concurrent access.
This is a generic exception class for Hypertable.
Definition: Error.h:314
Message buffer for holding data to be transmitted over a network.
Definition: CommBuf.h:79
void add_categories(int32_t categories)
Adds more categories to the already existing categories.
Definition: StatsSystem.cc:69
Response parameters for register server operation.
#define HT_ERRORF(msg,...)
Definition: Logger.h:300
const vector< SystemVariable::Spec > & variables()
Gets state variables.
Configuration settings.
Declarations for LocationInitializer.
Open file for reading.
Definition: Session.h:71
Declarations for RegisterServer response parameters.
Declarations for RegisterServer request parameters.
bool m_handshake_complete
Flag indicating completion of initialization handshake.
md5 digest routines.
static const NetInfo & net_info()
Retrieves updated Network information (see SystemInfo.h)
Definition: SystemInfo.cc:360
#define HT_FATAL_OUT
Definition: Logger.h:347
int code() const
Returns the error code.
Definition: Error.h:391
Executes user-defined functions when leaving the current scope.
String get()
Gets assigned location (proxy name)
std::shared_ptr< Context > m_context
Range server context