0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
OperationGatherStatistics.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 
32 #include "OperationSetState.h"
33 #include "OperationProcessor.h"
34 #include "RangeServerStatistics.h"
35 #include "LoadBalancer.h"
36 
37 #include <Common/Path.h>
38 #include <Common/StringExt.h>
39 
40 #include <cstdlib>
41 
42 using namespace Hypertable;
43 using namespace std;
44 
46  : OperationEphemeral(context, MetaLog::EntityType::OPERATION_GATHER_STATISTICS) {
50 }
51 
53  StringSet locations;
54  std::vector<RangeServerConnectionPtr> servers;
55  std::vector<RangeServerStatistics> results;
56  int32_t state = get_state();
58  Path data_dir;
59  String monitoring_dir, graphviz_str;
60  String filename, filename_tmp;
61  String dot_cmd;
62 
63  HT_INFOF("Entering GatherStatistics-%lld state=%s",
65 
66  switch (state) {
67 
69  m_context->rsc_manager->get_servers(servers);
70  if (servers.empty()) {
71  complete_ok();
72  break;
73  }
74  results.resize(servers.size());
75  for (size_t i=0; i<servers.size(); i++) {
76  results[i].addr = servers[i]->local_addr();
77  results[i].location = servers[i]->location();
78  locations.insert(results[i].location);
79  }
80  dispatch_handler.initialize(results);
81  dispatch_handler.DispatchHandlerOperation::start(locations);
82 
83  // Write MOP "graphviz" files
84  m_context->op->graphviz_output(graphviz_str);
85  data_dir = Path(m_context->props->get_str("Hypertable.DataDirectory"));
86  monitoring_dir = (data_dir /= "/run/monitoring").string();
87  filename = monitoring_dir + "/mop.dot";
88  filename_tmp = monitoring_dir + "/mop.tmp.dot";
89  if (FileUtils::write(filename_tmp, graphviz_str) != -1)
90  FileUtils::rename(filename_tmp, filename);
91  dot_cmd = format("dot -Tjpg -Gcharset=latin1 -o%s/mop.tmp.jpg %s/mop.dot",
92  monitoring_dir.c_str(), monitoring_dir.c_str());
93  if (system(dot_cmd.c_str()) != -1) {
94  filename = monitoring_dir + "/mop.jpg";
95  filename_tmp = monitoring_dir + "/mop.tmp.jpg";
96  FileUtils::rename(filename_tmp, filename);
97  }
98 
99  dispatch_handler.wait_for_completion();
100 
101  {
102  double numerator, denominator;
103  double numerator_total = 0.0, denominator_total = 0.0;
104  std::vector<RangeServerState> rs_states;
105  RangeServerState rs_state;
106  for (auto &rs_stats : results) {
107  if (rs_stats.fetch_error == Error::OK) {
108  numerator = denominator = 0.0;
109  for (size_t i=0; i<rs_stats.stats->system.fs_stat.size(); i++) {
110  numerator += rs_stats.stats->system.fs_stat[i].total
111  - rs_stats.stats->system.fs_stat[i].avail;
112  denominator += rs_stats.stats->system.fs_stat[i].total;
113  }
114  rs_state.location = rs_stats.location;
115  if (denominator > 0.0)
116  rs_state.disk_usage = (numerator / denominator) * 100.0;
117  else
118  rs_state.disk_usage = 0.0;
119  /*
120  HT_INFOF("%s disk_usage (%f/%f) = %0.2f", rs_state.location.c_str(),
121  numerator, denominator, rs_state.disk_usage);
122  */
123  rs_states.push_back(rs_state);
124  numerator_total += numerator;
125  denominator_total += denominator;
126  }
127  }
128  m_context->rsc_manager->set_range_server_state(rs_states);
129 
130  HT_INFOF("Aggregate disk usage = %0.2f",
131  denominator_total == 0.0 ? 0.0 : ((numerator_total / denominator_total)*100.0));
132 
133  int32_t aggregate_disk_usage = 0;
134  if (denominator_total >= 0.0)
135  aggregate_disk_usage = (int32_t)((numerator_total / denominator_total)*100.0);
136  String message = format("because aggreggate disk usage is %d%%",
137  aggregate_disk_usage);
138  bool readonly_mode = aggregate_disk_usage >= m_context->disk_threshold;
139 
140  if (m_context->system_state->auto_set(SystemVariable::READONLY,
141  readonly_mode, message))
142  m_context->op->add_operation(make_shared<OperationSetState>(m_context));
143  else {
144  // This isn't necessary in above block because OperationSetState does it
145  std::vector<NotificationMessage> notifications;
146  if (m_context->system_state->get_notifications(notifications)) {
147  for (auto &msg : notifications)
148  m_context->notification_hook(msg.subject, msg.body);
149  }
150  }
151  }
152 
153  m_context->monitoring->add(results);
154  m_context->balancer->transfer_monitoring_data(results);
155  complete_ok();
156  break;
157 
158  default:
159  HT_FATALF("Unrecognized state %d", state);
160  }
161 
162  HT_INFOF("Leaving GatherStatistics-%lld", (Lld)header.id);
163 }
164 
166  return "OperationGatherStatistics";
167 }
168 
170  return "GatherStatistics";
171 }
172 
std::set< String > StringSet
STL Set managing Strings.
Definition: StringExt.h:42
virtual const String label()
Human readable label for operation.
ContextPtr m_context
Pointer to Master context.
Definition: Operation.h:553
static String filename
Definition: Config.cc:48
Abstract base class for ephemeral operations.
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
Compatibility class for boost::filesystem::path.
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
int64_t id
Unique ID of entity.
Declarations for OperationProcessor.
static ssize_t write(const String &fname, const std::string &contents)
Writes a String buffer to a file; the file is overwritten if it already exists.
Definition: FileUtils.cc:124
STL namespace.
void initialize(std::vector< RangeServerStatistics > &results)
EntityHeader header
Entity header
const char * get_text(int32_t state)
Definition: Operation.cc:609
std::shared_ptr< Context > ContextPtr
Smart pointer to Context.
Definition: Context.h:265
Compatibility class for boost::filesystem::path.
Definition: Path.h:45
const char * INIT
Definition: Operation.cc:45
Compatibility Macros for C/C++.
const char * METADATA
Definition: Operation.cc:48
bool wait_for_completion()
Waits for requests to complete.
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
Declarations for OperationSetState.
Declarations for OperationGatherStatistics.
OperationGatherStatistics(ContextPtr &context)
Constructor.
const char * SYSTEM
Definition: Operation.cc:49
virtual void execute()
Carries out "gather statistics operation.
void complete_ok(std::vector< MetaLog::EntityPtr > &additional)
Definition: Operation.cc:436
static bool rename(const String &oldpath, const String &newpath)
Renames a file or directory.
Definition: FileUtils.cc:438
String extensions and helpers: sets, maps, append operators etc.
virtual const String name()
Name of operation used for exclusivity.