0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
OperationCreateNamespace.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 
27 
28 #include <Common/Error.h>
29 #include <Common/FailureInducer.h>
30 #include <Common/Serialization.h>
31 
32 using namespace Hypertable;
33 using namespace Hypertable::Lib::Master;
34 using namespace std;
35 
37  const string &name,
38  int32_t flags)
39  : Operation(context, MetaLog::EntityType::OPERATION_CREATE_NAMESPACE),
40  m_params(name, flags) {
42 }
43 
45  const MetaLog::EntityHeader &header_)
46  : Operation(context, header_) {
47 }
48 
50  : Operation(context, event, MetaLog::EntityType::OPERATION_CREATE_NAMESPACE) {
51  const uint8_t *ptr = event->payload;
52  size_t remaining = event->payload_len;
53  m_params.decode(&ptr, &remaining);
55 }
56 
58  m_exclusivities.insert(m_params.name());
59  if (m_params.name() != "/sys" && m_params.name() != "/tmp")
61 }
62 
64  bool is_namespace;
65  int nsflags = NameIdMapper::IS_NAMESPACE;
66  string hyperspace_dir;
67  int32_t state = get_state();
68 
69  HT_INFOF("Entering CreateNamespace-%lld('%s', flags=%s, '%s') state=%s",
70  (Lld)header.id, m_params.name().c_str(),
72  m_id.c_str(), OperationState::get_text(state));
73 
74  switch (state) {
75 
77  // Check to see if namespace exists
78  if (m_context->namemap->exists_mapping(m_params.name(), &is_namespace)) {
80  complete_ok();
81  else if (is_namespace)
83  else
85  return;
86  }
88  m_context->mml_writer->record_state(shared_from_this());
89  HT_MAYBE_FAIL("create-namespace-INITIAL");
90 
94  try {
95  m_context->namemap->add_mapping(m_params.name(), m_id, nsflags, true);
96  }
97  catch (Exception &e) {
98  if (e.code() == Error::INDUCED_FAILURE)
99  throw;
101  HT_ERROR_OUT << e << HT_END;
102  complete_error(e);
103  return;
104  }
105  HT_MAYBE_FAIL("create-namespace-ASSIGN_ID-a");
106  hyperspace_dir = m_context->toplevel_dir + "/tables" + m_id;
107  try {
109  m_context->hyperspace->mkdirs(hyperspace_dir);
110  else
111  m_context->hyperspace->mkdir(hyperspace_dir);
112  }
113  catch (Exception &e) {
115  HT_THROW2F(e.code(), e, "CreateNamespace %s -> %s", m_params.name().c_str(), m_id.c_str());
116  }
117  HT_MAYBE_FAIL("create-namespace-ASSIGN_ID-b");
118  complete_ok();
119  HT_MAYBE_FAIL("create-namespace-ASSIGN_ID-c");
120  break;
121 
122  default:
123  HT_FATALF("Unrecognized state %d", state);
124  }
125 
126  HT_INFOF("Leaving CreateNamespace-%lld('%s', flags=%s, '%s') state=%s",
127  (Lld)header.id, m_params.name().c_str(),
128  NamespaceFlag::to_str(m_params.flags()).c_str(), m_id.c_str(),
130 
131 }
132 
134  os << " name=" << m_params.name() << " flags=" << NamespaceFlag::to_str(m_params.flags());
135  if (m_id != "")
136  os << " (id=" << m_id << ")";
137 }
138 
140  return 1;
141 }
142 
145 }
146 
147 void OperationCreateNamespace::encode_state(uint8_t **bufp) const {
148  m_params.encode(bufp);
150 }
151 
153  const uint8_t **bufp,
154  size_t *remainp) {
155  m_params.decode(bufp, remainp);
156  m_id = Serialization::decode_vstr(bufp, remainp);
157 }
158 
160  const uint8_t **bufp,
161  size_t *remainp) {
162  string name = Serialization::decode_vstr(bufp, remainp);
163  int32_t flags = Serialization::decode_i32(bufp, remainp);
165  m_id = Serialization::decode_vstr(bufp, remainp);
166 }
167 
169  return "OperationCreateNamespace";
170 }
171 
173  return string("CreateNamespace ") + m_params.name();
174 }
175 
#define HT_THROW2F(_code_, _ex_, _fmt_,...)
Definition: Error.h:494
char * decode_vstr(const uint8_t **bufp, size_t *remainp)
Decode a vstr (vint64, data, null).
The FailureInducer simulates errors.
ContextPtr m_context
Pointer to Master context.
Definition: Operation.h:553
static std::string to_str(int flags)
Converts flags to human readable string.
virtual size_t encoded_length() const
Returns serialized object length.
Definition: Serializable.cc:37
int64_t id
Unique ID of entity.
void encode_state(uint8_t **bufp) const override
Encode operation state.
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
STL namespace.
EntityHeader header
Entity header
size_t encoded_length_vstr(size_t len)
Computes the encoded length of vstr (vint64, data, null)
const char * get_text(int32_t state)
Definition: Operation.cc:609
uint32_t decode_i32(const uint8_t **bufp, size_t *remainp)
Decode a 32-bit integer in little-endian order.
Request parameters for create namespace operation.
std::shared_ptr< Context > ContextPtr
Smart pointer to Context.
Definition: Context.h:265
const string & name() const
Gets name of namespace to create.
Lib::Master::Request::Parameters::CreateNamespace m_params
Request parmaeters.
Perform operation if namespace does not exist.
Definition: NamespaceFlag.h:49
OperationCreateNamespace(ContextPtr &context, const std::string &name, int32_t flags)
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
Compatibility Macros for C/C++.
Declarations for NamespaceFlag.
#define HT_END
Definition: Logger.h:220
Functions to serialize/deserialize primitives to/from a memory buffer.
void execute() override
Executes (carries out) the operation.
uint8_t encoding_version_state() const override
Returns version of encoding format of state.
#define HT_ERROR_OUT
Definition: Logger.h:301
virtual void decode(const uint8_t **bufp, size_t *remainp)
Reads serialized representation of object from a buffer.
Definition: Serializable.cc:70
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_dependencies
Set of dependencies.
Definition: Operation.h:595
#define HT_INFOF(msg,...)
Definition: Logger.h:272
void decode_state_old(uint8_t version, const uint8_t **bufp, size_t *remainp) override
Abstract base class for master operations.
Definition: Operation.h:124
const std::string name() override
Name of operation used for exclusivity.
void decode_state(uint8_t version, const uint8_t **bufp, size_t *remainp) override
Decode operation state.
This is a generic exception class for Hypertable.
Definition: Error.h:314
const std::string label() override
Human readable label for operation.
size_t encoded_length_state() const override
Encoded length of operation state.
void display_state(std::ostream &os) override
Write human readable operation state to output stream.
DependencySet m_exclusivities
Set of exclusivities.
Definition: Operation.h:592
#define HT_MAYBE_FAIL(_label_)
void complete_ok(std::vector< MetaLog::EntityPtr > &additional)
Definition: Operation.cc:436
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
int code() const
Returns the error code.
Definition: Error.h:391