0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
OperationRecreateIndexTables.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>
29 
35 
36 #include <Hypertable/Lib/Key.h>
37 
38 #include <Hyperspace/Session.h>
39 
40 #include <Common/Error.h>
41 #include <Common/FailureInducer.h>
42 #include <Common/ScopeGuard.h>
43 #include <Common/Serialization.h>
44 
45 #include <boost/algorithm/string.hpp>
46 
47 #include <poll.h>
48 
49 using namespace Hypertable;
50 using namespace Hyperspace;
51 using namespace std;
52 
54  std::string name,
55  TableParts parts) :
56  Operation(context, MetaLog::EntityType::OPERATION_RECREATE_INDEX_TABLES),
57  m_params(name, parts) {
58  m_exclusivities.insert(m_params.name());
59 }
60 
61 
63  const MetaLog::EntityHeader &header)
64  : Operation(context, header) {
65 }
66 
68  : Operation(context, event, MetaLog::EntityType::OPERATION_RECREATE_INDEX_TABLES) {
69  const uint8_t *ptr = event->payload;
70  size_t remaining = event->payload_len;
71  m_params.decode(&ptr, &remaining);
72  m_exclusivities.insert(m_params.name());
73 }
74 
151  std::vector<Entity *> entities;
152  int32_t state = get_state();
153 
154  HT_INFOF("Entering RecreateIndexTables-%lld (table=%s, parts=%s) state=%s",
155  (Lld)header.id, m_params.name().c_str(),
156  m_params.parts().to_string().c_str(), OperationState::get_text(state));
157 
158  switch (state) {
159 
161  {
162  string schema_str;
163  if (!fetch_schema(schema_str))
164  break;
165  SchemaPtr schema( Schema::new_instance(schema_str) );
166  uint8_t parts = 0;
167  for (auto cf_spec : schema->get_column_families()) {
168  if (m_params.parts().value_index() && cf_spec->get_value_index())
169  parts |= TableParts::VALUE_INDEX;
170  if (m_params.parts().qualifier_index() && cf_spec->get_qualifier_index())
172  }
173  if (parts == 0) {
175  break;
176  }
177  m_parts = TableParts(parts);
178  }
180  m_context->mml_writer->record_state(shared_from_this());
181  HT_MAYBE_FAIL("recreate-index-tables-INITIAL");
182 
183  // drop through ...
184 
186  stage_subop(make_shared<OperationToggleTableMaintenance>(m_context, m_params.name(),
189  record_state();
190  break;
191 
193  if (!validate_subops())
194  break;
195  stage_subop(make_shared<OperationDropTable>(m_context, m_params.name(), true, m_parts));
197  record_state();
198  break;
199 
201  if (!validate_subops())
202  break;
203 
204  if (m_parts) {
205  string schema;
206  if (!fetch_schema(schema))
207  break;
208  stage_subop(make_shared<OperationCreateTable>(m_context, m_params.name(), schema, m_parts));
209  }
211  record_state();
212  break;
213 
215 
216  if (!validate_subops())
217  break;
218 
219  stage_subop(make_shared<OperationToggleTableMaintenance>(m_context, m_params.name(),
222  HT_MAYBE_FAIL("recreate-index-tables-RESUME_TABLE_MAINTENANCE-a");
223  record_state();
224  HT_MAYBE_FAIL("recreate-index-tables-RESUME_TABLE_MAINTENANCE-b");
225  break;
226 
228  if (!validate_subops())
229  break;
230  complete_ok();
231  break;
232 
233  default:
234  HT_FATALF("Unrecognized state %d", state);
235  }
236 
237  HT_INFOF("Leaving RecreateIndexTables-%lld (table=%s, parts=%s) state=%s",
238  (Lld)header.id, m_params.name().c_str(),
239  m_params.parts().to_string().c_str(),
241 }
242 
244  os << " table_name=" << m_params.name()
245  << " parts=" << m_params.parts().to_string();
246 }
247 
249  return 1;
250 }
251 
254 }
255 
256 void OperationRecreateIndexTables::encode_state(uint8_t **bufp) const {
257  m_params.encode(bufp);
258  m_parts.encode(bufp);
259 }
260 
261 void OperationRecreateIndexTables::decode_state(uint8_t version, const uint8_t **bufp, size_t *remainp) {
262  m_params.decode(bufp, remainp);
263  m_parts.decode(bufp, remainp);
264 }
265 
266 void OperationRecreateIndexTables::decode_state_old(uint8_t version, const uint8_t **bufp, size_t *remainp) {
267  string name = Serialization::decode_vstr(bufp, remainp);
268  int8_t parts = (int8_t)Serialization::decode_byte(bufp, remainp);
269  m_parts = TableParts(parts);
271 }
272 
274  return "OperationRecreateIndexTables";
275 }
276 
278  return format("Recreate Index Tables (table=%s, parts=%s)",
279  m_params.name().c_str(), m_params.parts().to_string().c_str());
280 }
281 
283  string table_id;
284  bool is_namespace;
285  if (m_context->namemap->name_to_id(m_params.name(), table_id, &is_namespace)) {
286  if (is_namespace) {
287  complete_error(Error::TABLE_NOT_FOUND, format("%s is a namespace", m_params.name().c_str()));
288  return false;
289  }
290  }
291  else {
293  return false;
294  }
295  DynamicBuffer value_buf;
296  string filename = m_context->toplevel_dir + "/tables/" + table_id;
297  m_context->hyperspace->attr_get(filename, "schema", value_buf);
298  schema = string((char *)value_buf.base, strlen((char *)value_buf.base));
299  return true;
300 }
301 
void display_state(std::ostream &os) override
Writes human readable representation of object to output stream.
char * decode_vstr(const uint8_t **bufp, size_t *remainp)
Decode a vstr (vint64, data, null).
Declarations for OperationToggleTableMaintenance.
The FailureInducer simulates errors.
ContextPtr m_context
Pointer to Master context.
Definition: Operation.h:553
static String filename
Definition: Config.cc:48
const bool OFF
Constant representing off
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
Declarations for OperationDropTable.
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
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.
Declarations for OperationCreateTable.
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
STL namespace.
Declarations for OperationRecreateIndexTables.
EntityHeader header
Entity header
const char * get_text(int32_t state)
Definition: Operation.cc:609
A dynamic, resizable and reference counted memory buffer.
Definition: DynamicBuffer.h:42
Represents a set of table parts (sub-tables).
Definition: TableParts.h:47
Declarations for ReferenceManager.
std::shared_ptr< Context > ContextPtr
Smart pointer to Context.
Definition: Context.h:265
Hyperspace definitions
const String label() override
Returns descriptive label for operation.
void execute() override
Carries out recreate index tables operation.
const string & name() const
Gets name of table to create.
Request parameters for recreate index tables operation.
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
Lib::Master::Request::Parameters::RecreateIndexTables m_params
Request parmaeters.
Compatibility Macros for C/C++.
void stage_subop(std::shared_ptr< Operation > operation)
Stages a sub operation for execution.
Definition: Operation.cc:536
Functions to serialize/deserialize primitives to/from a memory buffer.
void record_state()
Records operation state to the MML.
Definition: Operation.h:401
void decode_state_old(uint8_t version, const uint8_t **bufp, size_t *remainp) override
uint8_t encoding_version_state() const override
Returns encoding version of serialization format.
virtual void decode(const uint8_t **bufp, size_t *remainp)
Reads serialized representation of object from a buffer.
Definition: Serializable.cc:70
Hypertable definitions
static Schema * new_instance(const std::string &buf)
Creates schema object from XML schema string.
Definition: Schema.cc:202
#define HT_FATALF(msg,...)
Definition: Logger.h:343
const String name() override
Returns name of operation.
long long int Lld
Shortcut for printf formats.
Definition: String.h:53
void encode_state(uint8_t **bufp) const override
Writes serialized encoding of object state.
Declarations for general-purpose utility functions.
const bool ON
Constant representing on
void decode_state(uint8_t version, const uint8_t **bufp, size_t *remainp) override
Reads serialized encoding of object state.
#define HT_INFOF(msg,...)
Definition: Logger.h:272
uint8_t * base
Pointer to the allocated memory buffer.
bool value_index() const
Test if value index is included in set.
Definition: TableParts.h:75
Abstract base class for master operations.
Definition: Operation.h:124
const std::string to_string() const
Returns human readable string describing table parts.
Definition: TableParts.cc:63
size_t encoded_length_state() const override
Returns serialized state length.
DependencySet m_exclusivities
Set of exclusivities.
Definition: Operation.h:592
std::shared_ptr< Schema > SchemaPtr
Smart pointer to Schema.
Definition: Schema.h:465
#define HT_MAYBE_FAIL(_label_)
void complete_ok(std::vector< MetaLog::EntityPtr > &additional)
Definition: Operation.cc:436
uint8_t decode_byte(const uint8_t **bufp, size_t *remainp)
Decodes a single byte from the given buffer.
Definition: Serialization.h:73
OperationRecreateIndexTables(ContextPtr &context, std::string name, TableParts parts)
Constructor.
Error codes, Exception handling, error logging.
void complete_error(int error, const String &msg, std::vector< MetaLog::EntityPtr > &additional)
Completes operation with error.
Definition: Operation.cc:400
Executes user-defined functions when leaving the current scope.
bool qualifier_index() const
Test if qualifier index is included in set.
Definition: TableParts.h:80