0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ClusterId.cc
Go to the documentation of this file.
1 /* -*- c++ -*-
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 
28 #include <Common/Compat.h>
29 #include "ClusterId.h"
30 
31 #include <Common/Config.h>
32 #include <Common/DynamicBuffer.h>
33 #include <Common/InetAddr.h>
34 #include <Common/ScopeGuard.h>
35 #include <Common/SystemInfo.h>
36 #include <Common/md5.h>
37 
38 #include <chrono>
39 #include <thread>
40 
41 using namespace Hyperspace;
42 using namespace Hypertable;
43 using namespace Hypertable::Config;
44 using namespace std;
45 
46 uint64_t ClusterId::id;
47 
49  bool generate_if_not_found) {
50 
51  string toplevel_dir = properties->get_str("Hypertable.Directory");
52  uint64_t handle = 0;
53 
54  HT_ON_SCOPE_EXIT(&Hyperspace::close_handle_ptr, hyperspace, &handle);
55 
56  // open the "master" file in hyperspace
57  handle = hyperspace->open(toplevel_dir + "/master",
59 
60  size_t retry_count = 12;
61 
62  while (true) {
63  try {
64  // get the "cluster_id" attribute
65  DynamicBuffer buf;
66  hyperspace->attr_get(handle, "cluster_id", buf);
67  string cluster_id((const char *)buf.base, buf.fill());
68  id = (uint64_t)strtoull(cluster_id.c_str(), 0, 0);
69  return;
70  }
71  catch (Exception &e) {
73  if (generate_if_not_found)
74  break;
75  if (--retry_count) {
76  HT_INFO("Problem reading cluster ID from Hyperspace, will retry in 10 seconds ...");
77  this_thread::sleep_for(chrono::milliseconds(10000));
78  continue;
79  }
80  }
81  HT_THROW2(e.code(), e, "Problem reading cluster ID from Hyperspace");
82  }
83  }
84 
85  // Generate new cluster ID
86  uint16_t port = properties->get_i16("Hypertable.Master.Port");
87  InetAddr addr(System::net_info().primary_addr, port);
88  string tmp = addr.format() + Hypertable::format("%u", (unsigned)time(0));
89  id = (uint64_t)md5_hash(tmp.c_str());
90  tmp = Hypertable::format("%llu", (Llu)id);
91 
92  HT_INFOF("Newly generated cluster ID = %llu", (Llu)id);
93 
94  // Store newly generated cluster Id to Hyperspace
95  try {
96  hyperspace->attr_set(handle, "cluster_id", tmp.c_str(), tmp.length());
97  }
98  catch (Exception &e) {
99  HT_FATALF("Problem writing newly generated cluster ID to Hyperspace - %s"
100  "(%s)", Error::get_text(e.code()), e.what());
101  }
102 }
int64_t md5_hash(const char *input)
Returns a 64-bit hash checksum of a null terminated input buffer.
Definition: md5.cc:388
PropertiesPtr properties
This singleton map stores all options.
Definition: Config.cc:47
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
ClusterId(Hyperspace::SessionPtr &hyperspace, bool generate_if_not_found=false)
Constructor.
Definition: ClusterId.cc:48
long long unsigned int Llu
Shortcut for printf formats.
Definition: String.h:50
#define HT_INFO(msg)
Definition: Logger.h:271
STL namespace.
#define HT_ON_SCOPE_EXIT(...)
Definition: ScopeGuard.h:301
A dynamic, resizable and reference counted memory buffer.
Definition: DynamicBuffer.h:42
Hyperspace definitions
A dynamic, resizable memory buffer.
Open file for writing.
Definition: Session.h:73
std::shared_ptr< Session > SessionPtr
Definition: Session.h:734
const char * get_text(int error)
Returns a descriptive error message.
Definition: Error.cc:330
Encapsulate an internet address.
Definition: InetAddr.h:66
static uint64_t id
Cluster ID.
Definition: ClusterId.h:90
void close_handle_ptr(SessionPtr hyperspace, uint64_t *handlep)
Definition: Session.cc:1400
Compatibility Macros for C/C++.
String format(int sep= ':') const
Returns a string with a dotted notation ("127.0.0.1:8080") including the port.
Definition: InetAddr.h:132
Hypertable definitions
#define HT_FATALF(msg,...)
Definition: Logger.h:343
#define HT_INFOF(msg,...)
Definition: Logger.h:272
uint8_t * base
Pointer to the allocated memory buffer.
size_t fill() const
Returns the size of the used portion.
Definition: DynamicBuffer.h:70
Internet address wrapper classes and utility functions.
This is a generic exception class for Hypertable.
Definition: Error.h:314
Configuration settings.
Open file for reading.
Definition: Session.h:71
System information and statistics based on libsigar.
md5 digest routines.
static const NetInfo & net_info()
Retrieves updated Network information (see SystemInfo.h)
Definition: SystemInfo.cc:360
Declarations for ClusterId.
int code() const
Returns the error code.
Definition: Error.h:391
#define HT_THROW2(_code_, _ex_, _msg_)
Definition: Error.h:484
Executes user-defined functions when leaving the current scope.