0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
OperationSetState.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 
28 #include <Common/Compat.h>
29 
31 #include "OperationSetState.h"
32 #include "Utility.h"
33 
34 #include <Hypertable/Lib/Key.h>
35 
36 #include <Hyperspace/Session.h>
37 
38 #include <Common/Error.h>
39 #include <Common/FailureInducer.h>
40 #include <Common/ScopeGuard.h>
41 #include <Common/Serialization.h>
42 
43 #include <boost/algorithm/string.hpp>
44 
45 using namespace Hypertable;
46 using namespace Hypertable::Lib;
47 
49  : Operation(context, MetaLog::EntityType::OPERATION_SET) {
50  m_exclusivities.insert("SET VARIABLES");
51 }
52 
54  const MetaLog::EntityHeader &header_)
55  : Operation(context, header_) {
56 }
57 
59  : Operation(context, event, MetaLog::EntityType::OPERATION_SET) {
60  const uint8_t *ptr = event->payload;
61  size_t remaining = event->payload_len;
62  m_params.decode(&ptr, &remaining);
63  m_context->system_state->admin_set(m_params.specs());
64  m_exclusivities.insert("SET VARIABLES");
65 }
66 
94  int32_t state = get_state();
96 
97  HT_INFOF("Entering SetState-%lld() state=%s",
99 
100  switch (state) {
101 
103 
104  // Check for and send notifications
105  {
106  std::vector<NotificationMessage> notifications;
107  if (m_context->system_state->get_notifications(notifications)) {
108  for (auto &msg : notifications)
109  m_context->notification_hook(msg.subject, msg.body);
110  }
111  }
112 
113  m_context->system_state->get(m_specs, &m_generation);
114 
115  HT_INFOF("specs = %s", SystemVariable::specs_to_string(m_specs).c_str());
116 
118  {
119  std::vector<MetaLog::EntityPtr> entities;
120  entities.push_back(shared_from_this());
121  entities.push_back(m_context->system_state);
122  m_context->mml_writer->record_state(entities);
123  }
124 
126 
127  {
128  StringSet locations;
129  std::vector<RangeServerConnectionPtr> servers;
130  m_context->rsc_manager->get_servers(servers);
131  if (servers.empty()) {
132  complete_ok();
133  break;
134  }
135  for (auto &rsc : servers)
136  locations.insert(rsc->location());
137  dispatch_handler.initialize();
138  dispatch_handler.DispatchHandlerOperation::start(locations);
139  }
140 
141  if (!dispatch_handler.wait_for_completion()) {
142  std::set<DispatchHandlerOperation::Result> results;
143  dispatch_handler.get_results(results);
144  for (auto &result : results) {
145  if (result.error != Error::OK)
146  HT_WARNF("Problem setting state variables at %s - %s",
147  result.location.c_str(), Error::get_text(result.error));
148  }
149  }
150 
151  complete_ok();
152  break;
153 
154  default:
155  HT_FATALF("Unrecognized state %d", state);
156  }
157 
158  HT_INFOF("Leaving SetState-%lld", (Lld)header.id);
159 }
160 
161 
162 void OperationSetState::display_state(std::ostream &os) {
163  bool first = true;
164  for (auto &spec : m_specs) {
165  if (!first)
166  os << ",";
167  os << SystemVariable::code_to_string(spec.code) << "=" << (spec.value ? "true" : "false");
168  first = false;
169  }
170  os << " ";
171 }
172 
174  return 1;
175 }
176 
178  size_t length = m_params.encoded_length() + 12;
179  for (auto &spec : m_specs)
180  length += spec.encoded_length();
181  return length;
182 }
183 
184 void OperationSetState::encode_state(uint8_t **bufp) const {
185  m_params.encode(bufp);
187  Serialization::encode_i32(bufp, m_specs.size());
188  for (auto &spec : m_specs)
189  spec.encode(bufp);
190 }
191 
192 void OperationSetState::decode_state(uint8_t version, const uint8_t **bufp, size_t *remainp) {
193  m_params.decode(bufp, remainp);
194  m_generation = Serialization::decode_i64(bufp, remainp);
195  size_t count = (size_t)Serialization::decode_i32(bufp, remainp);
196  for (size_t i=0; i<count; i++) {
198  spec.decode(bufp, remainp);
199  m_specs.push_back(spec);
200  }
201 }
202 
203 void OperationSetState::decode_state_old(uint8_t version, const uint8_t **bufp, size_t *remainp) {
205  m_specs.clear();
206  if (version == 0)
207  Serialization::decode_i32(bufp, remainp); // skip old version
208  m_generation = Serialization::decode_i64(bufp, remainp);
209  int32_t count = Serialization::decode_i32(bufp, remainp);
210  for (int32_t i=0; i<count; i++) {
211  spec.code = Serialization::decode_i32(bufp, remainp);
212  spec.value = Serialization::decode_bool(bufp, remainp);
213  m_specs.push_back(spec);
214  }
215 }
216 
218  return "OperationSetState";
219 }
220 
222  return String("SetState ") + SystemVariable::specs_to_string(m_specs);
223 }
224 
std::set< String > StringSet
STL Set managing Strings.
Definition: StringExt.h:42
#define HT_WARNF(msg,...)
Definition: Logger.h:290
The FailureInducer simulates errors.
ContextPtr m_context
Pointer to Master context.
Definition: Operation.h:553
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
virtual size_t encoded_length() const
Returns serialized object length.
Definition: Serializable.cc:37
int64_t id
Unique ID of entity.
int32_t code
Variable code.
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
std::string specs_to_string(const std::vector< Spec > &specs)
Returns a textual representation of variable specifications.
const std::vector< SystemVariable::Spec > & specs()
Gets system variable specifications.
Definition: SetState.h:64
EntityHeader header
Entity header
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
size_t encoded_length_state() const override
Returns length of encoded state.
uint64_t decode_i64(const uint8_t **bufp, size_t *remainp)
Decode a 64-bit integer in little-endian order.
virtual void encode(uint8_t **bufp) const
Writes serialized representation of object to a buffer.
Definition: Serializable.cc:64
uint8_t encoding_version_state() const override
Returns version of encoding format of state.
void set_state(int32_t state)
Definition: Operation.h:473
bool decode_bool(const uint8_t **bufp, size_t *remainp)
Decodes a boolean value from the given buffer.
Definition: Serialization.h:96
const char * get_text(int error)
Returns a descriptive error message.
Definition: Error.cc:330
void decode_state(uint8_t version, const uint8_t **bufp, size_t *remainp) override
Decodes operation state.
void encode_i32(uint8_t **bufp, uint32_t val)
Encode a 32-bit integer in little-endian order.
Compatibility Macros for C/C++.
OperationSetState(ContextPtr &context)
Constructor.
std::vector< SystemVariable::Spec > m_specs
Current system state variables.
void encode_i64(uint8_t **bufp, uint64_t val)
Encode a 64-bit integer in little-endian order.
Functions to serialize/deserialize primitives to/from a memory buffer.
const String label() override
Returns textual string describing operation plus state.
Holds a variable code and boolean value.
Declarations for DispatchHandlerOperationSetState.
virtual void decode(const uint8_t **bufp, size_t *remainp)
Reads serialized representation of object from a buffer.
Definition: Serializable.cc:70
bool wait_for_completion()
Waits for requests to complete.
const String name() override
Returns operation name (OperationSetState)
Hypertable library.
Definition: CellInterval.h:30
Hypertable definitions
#define HT_FATALF(msg,...)
Definition: Logger.h:343
long long int Lld
Shortcut for printf formats.
Definition: String.h:53
uint64_t m_generation
Generation number of system state variables (m_specs)a.
void get_results(std::set< Result > &results)
Returns the Result set.
Declarations for general-purpose utility functions.
#define HT_INFOF(msg,...)
Definition: Logger.h:272
Declarations for OperationSetState.
void execute() override
Executes "set state" operation.
Abstract base class for master operations.
Definition: Operation.h:124
Lib::Master::Request::Parameters::SetState m_params
Request parmaeters.
DependencySet m_exclusivities
Set of exclusivities.
Definition: Operation.h:592
void display_state(std::ostream &os) override
Displays textual representation of object state.
void decode_state_old(uint8_t version, const uint8_t **bufp, size_t *remainp) override
void encode_state(uint8_t **bufp) const override
Encodes operation state.
void complete_ok(std::vector< MetaLog::EntityPtr > &additional)
Definition: Operation.cc:436
Error codes, Exception handling, error logging.
const char * code_to_string(int var_code)
Converts variable code to variable string.
Executes user-defined functions when leaving the current scope.
DispatchHandler class for gathering responses to a set of RangeServer::set_state() requests...