0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
OperationInitialize.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 
25 #include "OperationCreateTable.h"
26 #include "OperationInitialize.h"
27 #include "Utility.h"
28 
29 #include <Hypertable/Lib/Key.h>
31 
32 #include <Common/Error.h>
33 #include <Common/FailureInducer.h>
34 #include <Common/ScopeGuard.h>
35 #include <Common/Serialization.h>
36 #include <Common/System.h>
37 
38 using namespace Hypertable;
39 using namespace Hyperspace;
40 using namespace std;
41 
43  : Operation(context, MetaLog::EntityType::OPERATION_INITIALIZE) {
46 }
47 
49  const MetaLog::EntityHeader &header_)
50  : Operation(context, header_) {
51 }
52 
53 
55  String filename, schema;
56  uint64_t handle = 0;
57  RangeSpec range;
58  int32_t state = get_state();
59 
60  HT_INFOF("Entering Initialize-%lld state=%s",
62 
63  switch (state) {
64 
66 
67  // Initialize Hyperspace DIRECTORIES and FILES
68  {
69  handle = 0;
71  m_context->hyperspace->mkdirs(m_context->toplevel_dir + "/servers");
72  m_context->hyperspace->mkdirs(m_context->toplevel_dir + "/tables");
73  handle = m_context->hyperspace->open(m_context->toplevel_dir + "/master",
75  m_context->hyperspace->close(handle);
76  handle = m_context->hyperspace->open(m_context->toplevel_dir + "/root",
78  }
79 
80  stage_subop(make_shared<OperationCreateNamespace>(m_context, "/sys", 0));
82  record_state();
83  HT_MAYBE_FAIL("initialize-INITIAL");
84  break;
85 
87  if (!validate_subops())
88  break;
89  filename = System::install_dir + "/conf/METADATA.xml";
90  schema = FileUtils::file_to_string(filename);
91  Utility::create_table_in_hyperspace(m_context, "/sys/METADATA", schema, &m_table);
92  HT_MAYBE_FAIL("initialize-STARTED");
93  {
94  lock_guard<mutex> lock(m_mutex);
95  m_dependencies.clear();
98  }
99  m_context->mml_writer->record_state(shared_from_this());
100  m_root_range_name = format("%s[%s..%s]", m_table.id, "", Key::END_ROOT_ROW);
102 
106  break;
107  {
108  lock_guard<mutex> lock(m_mutex);
109  m_dependencies.clear();
115  }
116  m_context->mml_writer->record_state(shared_from_this());
117  HT_MAYBE_FAIL("initialize-ASSIGN_METADATA_RANGES");
118 
120  try {
121  range.start_row = 0;
122  range.end_row = Key::END_ROOT_ROW;
124  m_table, range, false);
126  m_table, range);
127  }
128  catch (Exception &e) {
129  HT_FATAL_OUT << "Problem loading ROOT range - " << e << HT_END;
130  }
131  HT_MAYBE_FAIL("initialize-LOAD_ROOT_METADATA_RANGE");
132  {
133  lock_guard<mutex> lock(m_mutex);
134  m_dependencies.clear();
138  }
139  m_context->mml_writer->record_state(shared_from_this());
140 
142  try {
146  m_table, range, false);
148  m_table, range);
149  }
150  catch (Exception &e) {
151  HT_FATAL_OUT << "Problem loading ROOT range - " << e << HT_END;
152  }
153  HT_MAYBE_FAIL("initialize-LOAD_SECOND_METADATA_RANGE");
154  {
155  lock_guard<mutex> lock(m_mutex);
156  m_dependencies.clear();
158  }
159  m_context->mml_writer->record_state(shared_from_this());
160 
162  // Write METADATA entry for 2nd level METADATA range
163  m_context->metadata_table = m_context->new_table(TableIdentifier::METADATA_NAME);
165  {
166  String tablefile = m_context->toplevel_dir + "/tables/" + m_table.id;
167  m_context->hyperspace->attr_set(tablefile, "x", "", 0);
168  }
169  HT_MAYBE_FAIL("initialize-WRITE_METADATA");
170  {
171  m_dependencies.clear();
173  }
174  m_context->mml_writer->record_state(shared_from_this());
175  break;
176 
178  if (!m_context->metadata_table)
179  m_context->metadata_table = m_context->new_table(TableIdentifier::METADATA_NAME);
180  filename = System::install_dir + "/conf/RS_METRICS.xml";
181  schema = FileUtils::file_to_string(filename);
182  stage_subop(make_shared<OperationCreateTable>(m_context, "/sys/RS_METRICS", schema,
184  stage_subop(make_shared<OperationCreateNamespace>(m_context, "/tmp", 0));
186  record_state();
187  HT_MAYBE_FAIL("initialize-CREATE_RS_METRICS");
188  break;
189 
191 
192  if (!validate_subops())
193  break;
194 
195  if (!m_context->metadata_table)
196  m_context->metadata_table = m_context->new_table(TableIdentifier::METADATA_NAME);
197  if (!m_context->rs_metrics_table)
198  m_context->rs_metrics_table = m_context->new_table("sys/RS_METRICS");
199 
200  complete_ok();
201  break;
202 
203  default:
204  HT_FATALF("Unrecognized state %d", state);
205  }
206 
207  HT_INFOF("Leaving Initialize-%lld state=%s", (Lld)header.id,
209 
210 }
211 
212 
213 void OperationInitialize::display_state(std::ostream &os) {
214  os << " root-location='" << m_metadata_root_location;
215  os << "' 2nd-metadata-location='" << m_metadata_secondlevel_location << "' ";
216  os << m_table << " ";
217 }
218 
220  return 1;
221 }
222 
227 }
228 
229 void OperationInitialize::encode_state(uint8_t **bufp) const {
232  m_table.encode(bufp);
233 }
234 
235 void OperationInitialize::decode_state(uint8_t version, const uint8_t **bufp, size_t *remainp) {
239  m_table.decode(bufp, remainp);
240  if (m_table.id && *m_table.id != 0) {
241  m_root_range_name = format("%s[%s..%s]", m_table.id, "", Key::END_ROOT_ROW);
243  }
244 }
245 
246 void OperationInitialize::decode_state_old(uint8_t version, const uint8_t **bufp, size_t *remainp) {
250  legacy_decode(bufp, remainp, &m_table);
251  if (m_table.id && *m_table.id != 0) {
252  m_root_range_name = format("%s[%s..%s]", m_table.id, "", Key::END_ROOT_ROW);
254  }
255 }
256 
257 void OperationInitialize::decode_result(const uint8_t **bufp, size_t *remainp) {
259  // We need to do this here because we don't know the
260  // state until we're decoding and if the state is COMPLETE,
261  // this method is called instead of decode_state
262  if (m_context) {
263  m_context->metadata_table = m_context->new_table(TableIdentifier::METADATA_NAME);
264  }
265  Operation::decode_result(bufp, remainp);
266 }
267 
268 
270  return "OperationInitialize";
271 }
272 
274  return String("Initialize");
275 }
276 
Retrieves system information (hardware, installation directory, etc)
char * decode_vstr(const uint8_t **bufp, size_t *remainp)
Decode a vstr (vint64, data, null).
bool next_available_server(ContextPtr &context, String &location, bool urgent)
Gets name of next available server.
Definition: Utility.cc:304
uint8_t encoding_version_state() const override
Returns version of encoding format of state.
The FailureInducer simulates errors.
ContextPtr m_context
Pointer to Master context.
Definition: Operation.h:553
static String filename
Definition: Config.cc:48
Range specification.
Definition: RangeSpec.h:40
void encode_state(uint8_t **bufp) const override
Encode operation state.
void decode_state(uint8_t version, const uint8_t **bufp, size_t *remainp) override
Decode operation state.
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
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
const char * SERVERS
Definition: Operation.cc:46
bool validate_subops()
Handles the results of sub operations.
Definition: Operation.cc:512
virtual size_t encoded_length() const
Returns serialized object length.
Definition: Serializable.cc:37
int64_t id
Unique ID of entity.
static const char * METADATA_NAME
virtual void decode_result(const uint8_t **bufp, size_t *remainp)
Decode operation result.
Definition: Operation.cc:356
Declarations for OperationCreateTable.
std::mutex m_mutex
Mutex for serializing access to members
void create_table_load_range(ContextPtr &context, const String &location, TableIdentifier &table, RangeSpec &range, bool needs_compaction)
Loads a table's initial range.
Definition: Utility.cc:313
void create_table_acknowledge_range(ContextPtr &context, const String &location, TableIdentifier &table, RangeSpec &range)
Calls RangeServer::acknowledge_load for a range.
Definition: Utility.cc:350
STL namespace.
EntityHeader header
Entity header
size_t encoded_length_vstr(size_t len)
Computes the encoded length of vstr (vint64, data, null)
#define HT_ON_SCOPE_EXIT(...)
Definition: ScopeGuard.h:301
const char * get_text(int32_t state)
Definition: Operation.cc:609
Represents a set of table parts (sub-tables).
Definition: TableParts.h:47
OperationInitialize(ContextPtr &context)
const String label() override
Human readable label for operation.
std::shared_ptr< Context > ContextPtr
Smart pointer to Context.
Definition: Context.h:265
Hyperspace definitions
const char * end_row
Definition: RangeSpec.h:60
void execute() override
Executes (carries out) the operation.
Open file for writing.
Definition: Session.h:73
virtual void encode(uint8_t **bufp) const
Writes serialized representation of object to a buffer.
Definition: Serializable.cc:64
void set_state(int32_t state)
Definition: Operation.h:473
const char * INIT
Definition: Operation.cc:45
void close_handle_ptr(SessionPtr hyperspace, uint64_t *handlep)
Definition: Session.cc:1400
Compatibility Macros for C/C++.
void stage_subop(std::shared_ptr< Operation > operation)
Stages a sub operation for execution.
Definition: Operation.cc:536
#define HT_END
Definition: Logger.h:220
void set_remove_approval_mask(uint16_t mask)
Sets the remove approvals bit mask.
Definition: Operation.h:345
Functions to serialize/deserialize primitives to/from a memory buffer.
void record_state()
Records operation state to the MML.
Definition: Operation.h:401
const String name() override
Name of operation used for exclusivity.
virtual void decode(const uint8_t **bufp, size_t *remainp)
Reads serialized representation of object from a buffer.
Definition: Serializable.cc:70
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
#define HT_FATALF(msg,...)
Definition: Logger.h:343
long long int Lld
Shortcut for printf formats.
Definition: String.h:53
DependencySet m_obstructions
Set of obstructions.
Definition: Operation.h:598
Declarations for general-purpose utility functions.
void legacy_decode(const uint8_t **bufp, size_t *remainp, BalancePlan *plan)
const char * start_row
Definition: RangeSpec.h:59
size_t encoded_length_state() const override
Encoded length of operation state.
void create_table_in_hyperspace(ContextPtr &context, const String &name, const String &schema_str, TableIdentifierManaged *table)
Creates a table in Hyperspace.
Definition: Utility.cc:182
DependencySet m_dependencies
Set of dependencies.
Definition: Operation.h:595
#define HT_INFOF(msg,...)
Definition: Logger.h:272
void create_table_write_metadata(ContextPtr &context, TableIdentifier *table)
Creates initial METADATA table entry for table.
Definition: Utility.cc:275
Abstract base class for master operations.
Definition: Operation.h:124
void decode_result(const uint8_t **bufp, size_t *remainp) override
Decode operation result.
static String install_dir
The installation directory.
Definition: System.h:114
This is a generic exception class for Hypertable.
Definition: Error.h:314
void decode_state_old(uint8_t version, const uint8_t **bufp, size_t *remainp) override
static const char * END_ROOT_ROW
Definition: Key.h:50
void display_state(std::ostream &os) override
Write human readable operation state to output stream.
Create file if it does not exist.
Definition: Session.h:77
#define HT_MAYBE_FAIL(_label_)
void complete_ok(std::vector< MetaLog::EntityPtr > &additional)
Definition: Operation.cc:436
Open file for reading.
Definition: Session.h:71
Error codes, Exception handling, error logging.
static const char * END_ROW_MARKER
Definition: Key.h:49
static String file_to_string(const String &fname)
Reads a full file into a String.
Definition: FileUtils.cc:333
#define HT_FATAL_OUT
Definition: Logger.h:347
Executes user-defined functions when leaving the current scope.