0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
OperationDropNamespace.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 
24 #include "OperationDropNamespace.h"
25 
27 
28 #include <Common/Error.h>
29 #include <Common/FailureInducer.h>
30 #include <Common/ScopeGuard.h>
31 #include <Common/Serialization.h>
32 
33 #include <boost/algorithm/string.hpp>
34 
35 using namespace Hypertable;
36 using namespace Hypertable::Lib::Master;
37 
39  const String &name,
40  int32_t flags)
41  : Operation(context, MetaLog::EntityType::OPERATION_DROP_NAMESPACE),
42  m_params(name, flags) {
44 }
45 
47  const MetaLog::EntityHeader &header_)
48  : Operation(context, header_) {
49 }
50 
52  EventPtr &event)
53  : Operation(context, event, MetaLog::EntityType::OPERATION_DROP_NAMESPACE) {
54  const uint8_t *ptr = event->payload;
55  size_t remaining = event->payload_len;
56  m_params.decode(&ptr, &remaining);
58 }
59 
61  m_exclusivities.insert(m_params.name());
63 }
64 
65 
67  bool is_namespace;
68  String hyperspace_dir;
69  int32_t state = get_state();
70 
71  HT_INFOF("Entering DropNamespace-%lld(%s, flags=%s) state=%s",
72  (Lld)header.id, m_params.name().c_str(),
75 
76  switch (state) {
77 
79  // Check to see if namespace exists
80  if(m_context->namemap->name_to_id(m_params.name(), m_id, &is_namespace)) {
81  if (!is_namespace) {
82  complete_error(Error::BAD_NAMESPACE, format("%s is not a namespace", m_params.name().c_str()));
83  return;
84  }
86  m_context->mml_writer->record_state(shared_from_this());
87  }
88  else {
90  complete_ok();
91  else
93  return;
94  }
95  HT_MAYBE_FAIL("drop-namespace-INITIAL");
96 
98  try {
99  m_context->namemap->drop_mapping(m_params.name());
100  HT_MAYBE_FAIL("drop-namespace-STARTED-a");
101  hyperspace_dir = m_context->toplevel_dir + "/tables/" + m_id;
102  m_context->hyperspace->unlink(hyperspace_dir);
103  HT_MAYBE_FAIL("drop-namespace-STARTED-b");
104  }
105  catch (Exception &e) {
106  if (e.code() == Error::INDUCED_FAILURE)
107  throw;
110  HT_ERROR_OUT << e << HT_END;
111  complete_error(e);
112  return;
113  }
114  }
115  complete_ok();
116  break;
117 
118  default:
119  HT_FATALF("Unrecognized state %d", state);
120  }
121 
122  HT_INFOF("Leaving DropNamespace-%lld(%s, flags=%s) state=%s",
123  (Lld)header.id, m_params.name().c_str(),
126 
127 }
128 
129 
131  os << " name=" << m_params.name()
132  << " flags=" << NamespaceFlag::to_str(m_params.flags())
133  << " id=" << m_id << " ";
134 }
135 
137  return 1;
138 }
139 
142 }
143 
144 void OperationDropNamespace::encode_state(uint8_t **bufp) const {
145  m_params.encode(bufp);
147 }
148 
149 void OperationDropNamespace::decode_state(uint8_t version, const uint8_t **bufp, size_t *remainp) {
150  m_params.decode(bufp, remainp);
151  m_id = Serialization::decode_vstr(bufp, remainp);
152 }
153 
154 void OperationDropNamespace::decode_state_old(uint8_t version, const uint8_t **bufp, size_t *remainp) {
155  string name = Serialization::decode_vstr(bufp, remainp);
156  int32_t flags = Serialization::decode_i32(bufp, remainp);
158  m_id = Serialization::decode_vstr(bufp, remainp);
159 }
160 
162  return "OperationDropNamespace";
163 }
164 
166  return String("DropNamespace ") + m_params.name();
167 }
168 
const string & name() const
Gets name of namespace to drop.
Definition: DropNamespace.h:62
char * decode_vstr(const uint8_t **bufp, size_t *remainp)
Decode a vstr (vint64, data, null).
The FailureInducer simulates errors.
void decode_state_old(uint8_t version, const uint8_t **bufp, size_t *remainp) override
ContextPtr m_context
Pointer to Master context.
Definition: Operation.h:553
std::string m_id
Namespace ID path.
void encode_state(uint8_t **bufp) const override
Encode 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
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.
uint8_t encoding_version_state() const override
Returns version of encoding format of state.
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
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.
std::shared_ptr< Context > ContextPtr
Smart pointer to Context.
Definition: Context.h:265
OperationDropNamespace(ContextPtr &context, const std::string &name, int32_t flags)
void decode_state(uint8_t version, const uint8_t **bufp, size_t *remainp) override
Decode operation state.
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 display_state(std::ostream &os) override
Write human readable operation state to output stream.
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.
#define HT_ERROR_OUT
Definition: Logger.h:301
size_t encoded_length_state() const override
Encoded length of operation state.
const std::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 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
Request parameters for drop namespace operation.
Definition: DropNamespace.h:46
Lib::Master::Request::Parameters::DropNamespace m_params
Request parmaeters.
Perform operation if namespace exists.
Definition: NamespaceFlag.h:47
DependencySet m_dependencies
Set of dependencies.
Definition: Operation.h:595
#define HT_INFOF(msg,...)
Definition: Logger.h:272
Abstract base class for master operations.
Definition: Operation.h:124
void execute() override
Executes (carries out) the operation.
This is a generic exception class for Hypertable.
Definition: Error.h:314
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
const std::string label() override
Human readable label for operation.
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
Executes user-defined functions when leaving the current scope.