0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
OperationSystemStatus.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 "OperationSystemStatus.h"
25 
28 
29 #include <Common/Error.h>
30 #include <Common/Serialization.h>
31 #include <Common/Status.h>
32 #include <Common/StatusPersister.h>
33 #include <Common/StringExt.h>
34 
35 #include <set>
36 #include <vector>
37 
38 using namespace Hypertable;
39 using namespace Hypertable::Lib::Master;
40 using namespace std;
41 
43  : OperationEphemeral(context, event, MetaLog::EntityType::OPERATION_STATUS) {
44 }
45 
47 
48  HT_INFOF("Entering SystemStatus-%lld state=%s",
50 
53  uint32_t deadline = (uint32_t)((double)m_event->header.timeout_ms * 0.75);
54  Timer timer(deadline, true);
55  Status status;
56 
57  while (Utility::status(m_context, timer, status)) {
58  set<string> locations;
59  vector<RangeServerConnectionPtr> servers;
60  vector<DispatchHandlerOperationSystemStatus::Result> results;
61  DispatchHandlerOperationSystemStatus dispatch_handler(m_context, timer);
62 
63  m_context->rsc_manager->get_servers(servers);
64  if (servers.empty()) {
65  status.set(Status::Code::CRITICAL, "No connected RangeServers");
66  break;
67  }
68  results.reserve(servers.size());
69  for (auto &rsc : servers) {
70  DispatchHandlerOperationSystemStatus::Result result(rsc->location(), rsc->local_addr());
71  results.push_back(result);
72  locations.insert(result.location);
73  }
74  dispatch_handler.initialize(results);
75  dispatch_handler.DispatchHandlerOperation::start(locations);
76  dispatch_handler.wait_for_completion();
77 
78  for (auto &result : results) {
79  Status::Code code;
80  if (result.error != Error::OK) {
81  if (result.error == Error::REQUEST_TIMEOUT) {
82  if (status.get() != Status::Code::OK)
83  continue;
84  code = Status::Code::WARNING;
85  }
86  else
88  status.set(code, status_text_from_result(result));
89  if (code == Status::Code::CRITICAL)
90  break;
91  }
92  else if (result.status.get() != Status::Code::OK) {
93  if (status.get() == Status::Code::OK ||
94  (status.get() == Status::Code::WARNING &&
95  result.status.get() == Status::Code::CRITICAL)) {
96  status.set(result.status.get(),
97  status_text_from_result(result));
98  if (status.get() == Status::Code::CRITICAL)
99  break;
100  }
101  }
102  }
103  break;
104  }
105 
106  if (status.get() == Status::Code::OK && !m_context->quorum_reached)
107  status.set(Status::Code::WARNING,
108  "RangeServer quorum has not been reached");
109 
110  m_params.set_status(status);
111  complete_ok();
112 
113  HT_INFOF("Leaving SystemStatus-%lld %s", (Lld)header.id,
114  status.format_output_line("Hypertable").c_str());
115 }
116 
118  return "OperationSystemStatus";
119 }
120 
122  return String("Status");
123 }
124 
126  return 4 + m_params.encoded_length();
127 }
128 
142 void OperationSystemStatus::encode_result(uint8_t **bufp) const {
144  m_params.encode(bufp);
145 }
146 
147 
149  Status::Code code;
150  string address = result.addr.format_ipaddress();
151  if (result.error != Error::OK) {
152  if (result.error == Error::REQUEST_TIMEOUT)
153  code = Status::Code::WARNING;
154  else
155  code = Status::Code::CRITICAL;
156  if (!result.message.empty())
157  return format("RangeServer %s (%s) %s - %s",
158  result.location.c_str(), address.c_str(),
159  Error::get_text(result.error), result.message.c_str());
160  return format("RangeServer %s (%s) %s", result.location.c_str(),
161  address.c_str(), Error::get_text(result.error));
162  }
163  string text;
164  result.status.get(&code, text);
165  return format("RangeServer %s (%s) %s", result.location.c_str(),
166  address.c_str(), text.c_str());
167 }
String format_ipaddress()
Returns a string with a dotted notation ("127.0.0.1") without! the port.
Definition: InetAddr.h:136
size_t encoded_result_length() const override
Length of encoded operation result.
ContextPtr m_context
Pointer to Master context.
Definition: Operation.h:553
Holds Nagios-style program status information.
Definition: Status.h:42
Abstract base class for ephemeral operations.
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
Declarations for Status.
std::string status_text_from_result(DispatchHandlerOperationSystemStatus::Result &result)
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
virtual size_t encoded_length() const
Returns serialized object length.
Definition: Serializable.cc:37
int64_t id
Unique ID of entity.
Declarations for DispatchHandlerOperationSystemStatus.
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
STL namespace.
Carries out scatter/gather requests for RangeServer status.
EntityHeader header
Entity header
void set_status(const Hypertable::Status &status)
Sets status object.
Definition: SystemStatus.h:64
const char * get_text(int32_t state)
Definition: Operation.cc:609
void set(Code code, const std::string &text)
Sets status code and text.
Definition: Status.h:101
Code
Enumeration for status codes.
Definition: Status.h:47
EventPtr m_event
Pointer to client event (if any) that originated the operation.
Definition: Operation.h:556
std::shared_ptr< Context > ContextPtr
Smart pointer to Context.
Definition: Context.h:265
void execute() override
Executes (carries out) the operation.
virtual void encode(uint8_t **bufp) const
Writes serialized representation of object to a buffer.
Definition: Serializable.cc:64
Response::Parameters::SystemStatus m_params
const char * get_text(int error)
Returns a descriptive error message.
Definition: Error.cc:330
bool status(ContextPtr &context, Timer &timer, Status &status)
Runs a status check on the master.
Definition: Utility.cc:408
void encode_i32(uint8_t **bufp, uint32_t val)
Encode a 32-bit integer in little-endian order.
Compatibility Macros for C/C++.
const String label() override
Human readable label for operation.
Functions to serialize/deserialize primitives to/from a memory buffer.
OperationSystemStatus(ContextPtr &context, EventPtr &event)
bool wait_for_completion()
Waits for requests to complete.
void initialize(std::vector< Result > &results)
Performs initialization.
Hypertable definitions
long long int Lld
Shortcut for printf formats.
Definition: String.h:53
Declarations for general-purpose utility functions.
#define HT_INFOF(msg,...)
Definition: Logger.h:272
void get(Code *code, std::string &text) const
Gets status code and text.
Definition: Status.h:111
const String name() override
Name of operation used for exclusivity.
A timer class to keep timeout states across AsyncComm related calls.
Definition: Timer.h:44
Declarations for StatusPersister.
void complete_ok(std::vector< MetaLog::EntityPtr > &additional)
Definition: Operation.cc:436
std::string format_output_line(const std::string &service)
Formats a Nagios-style output line.
Definition: Status.cc:92
String extensions and helpers: sets, maps, append operators etc.
Error codes, Exception handling, error logging.
void encode_result(uint8_t **bufp) const override
Encode operation result.