0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ClusterDefinition.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 
26 
27 #include <Common/Compat.h>
28 
29 #include "ClusterDefinition.h"
30 
32 #include <Common/Logger.h>
34 
35 #include <boost/algorithm/string.hpp>
36 #include <boost/tokenizer.hpp>
37 
38 #include <cerrno>
39 #include <cstring>
40 
41 extern "C" {
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <unistd.h>
45 }
46 
47 using namespace Hypertable;
48 using namespace Hypertable::ClusterDefinitionFile;
49 using namespace std;
50 
51 void
53  vector<string> &members,
54  int64_t *generation){
55  lock_guard<mutex> lock(m_mutex);
56  load_file();
57  auto iter = m_roles.find(role);
58  if (iter == m_roles.end()) {
59  members.clear();
60  if (generation)
61  *generation = 0;
62  return;
63  }
64  members = iter->second;
65  if (generation)
66  *generation = (int64_t)m_last_mtime;
67 }
68 
70  struct stat stats;
71 
72  if (stat(m_fname.c_str(), &stats)) {
73  if (errno == ENOENT) {
74  m_roles.clear();
75  m_last_mtime = 0;
76  return;
77  }
78  HT_FATALF("Unable to access file %s - %s",
79  m_fname.c_str(), strerror(errno));
80  }
81 
82  if (stats.st_mtime > m_last_mtime) {
83  TokenizerPtr tokenizer(new Tokenizer(m_fname));
84  Token token;
85  while (tokenizer->next(token)) {
86  if (token.type == Token::ROLE)
87  add_role(token.text);
88  }
89  m_last_mtime = stats.st_mtime;
90  }
91 
92 }
93 
94 void ClusterDefinition::ClusterDefinition::add_role(const std::string &text) {
95  const char *base = text.c_str();
96  const char *ptr;
97  for (ptr=base; *ptr && !isspace(*ptr); ptr++)
98  ;
99  string token(base, ptr-base);
100  boost::trim(token);
101  if (strcasecmp(token.c_str(), "role:"))
102  HT_THROWF(Error::SYNTAX_ERROR, "Mal-formed role definition: %s", text.c_str());
103  while (*ptr && isspace(*ptr))
104  ptr++;
105  base = ptr;
106  while (*ptr && !isspace(*ptr))
107  ptr++;
108  string name(base, ptr-base);
109  boost::trim(name);
110  if (name.empty())
111  HT_THROWF(Error::SYNTAX_ERROR, "Mal-formed role definition: %s", text.c_str());
112  string input(ptr);
113  string value;
114 
115  boost::char_separator<char> sep(" \t\n\r");
116  boost::tokenizer< boost::char_separator<char> > tokens(input, sep);
117  for (auto &t : tokens)
118  value += t + " ";
119  boost::trim(value);
120 
121  m_roles[name] = HostSpecification(value);
122 }
Declarations for HostSpecification.
void add_role(const std::string &text)
Parses a role definition statement Parses the role definition statement contained in text and adds th...
Converts host specification pattern to list of host names.
STL namespace.
Cluster definition file token.
Definition: Token.h:43
shared_ptr< Tokenizer > TokenizerPtr
Smart pointer to Tokenizer.
Definition: Tokenizer.h:123
Logging routines and macros.
void load_file()
Load cluster definition from file Loads the cluster definition from the file m_fname, if the file was modified since the last time it was loaded.
Compatibility Macros for C/C++.
Splits cluster definition file into tokens.
Definition: Tokenizer.h:43
Declarations for ClusterDefinition.
Hypertable definitions
#define HT_FATALF(msg,...)
Definition: Logger.h:343
#define HT_THROWF(_code_, _fmt_,...)
Definition: Error.h:490
void get_role_members(const std::string &role, std::vector< std::string > &members, int64_t *generation=nullptr)
Get list of members of a role This method first refreshes its view of the cluster definition by reloa...
Declarations for Tokenizer.
Cluster definition file translation definitions.
Definition: Compiler.h:35