0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ht_balance_plan_generator.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; either version 3
9  * of the 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 
26 #include <Hypertable/Lib/Config.h>
27 #include <Hypertable/Lib/Client.h>
29 
30 #include <Hyperspace/Session.h>
31 
32 #include <Common/Init.h>
33 #include <Common/Error.h>
34 #include <Common/System.h>
35 
36 #include <iostream>
37 #include <fstream>
38 #include <cstdio>
39 #include <cstdlib>
40 #include <cmath>
41 
42 extern "C" {
43 #include <poll.h>
44 #include <stdio.h>
45 }
46 
47 using namespace Hypertable;
48 using namespace Hypertable::Config;
49 using namespace std;
50 using namespace boost;
51 
52 namespace {
53 
54  const char *usage =
55  "\n"
56  "Usage: ht_balance_plan_generator [options] <rs_metrics_file>\n\n"
57  "Description:\n"
58  " This program is used to generate a load balancing plan.\n"
59  " The <rs_metrics_file> argument indicates the location of the file containing the dump\n"
60  " of the 'sys/RS_METRICS' table.\n\n"
61  "Options";
62 
63  struct AppPolicy : Config::Policy {
64  static void init_options() {
66  cmdline_desc(usage).add_options()
67  ("help,h", "Show this help message and exit")
68  ("help-config", "Show help message for config properties")
69  ("namespace", str()->default_value("/"),
70  "Namespace in which to create the dummy RS_METRICS table if needed")
71  ("table", str()->default_value("DUMMY_RS_METRICS"),
72  "Name of the table in which to load the rs_metrics_file")
73  ("rs-metrics-loaded", boo()->zero_tokens()->default_value(false),
74  "If true then assume RS_METRICS is already loaded in namespace/table")
75  ("load-balancer", str()->default_value("basic-distribute-load"),
76  "Type of load balancer to be used.")
77  ("verbose,v", boo()->zero_tokens()->default_value(false),
78  "Show more verbose output")
79  ("balance-plan-file,b", str()->default_value(""),
80  "File in which to dump balance plan.")
81  ("rs-metrics-dump", str(), "File containing dump of 'sys/RS_METRICS' table.");
82  }
83  };
84  bool verbose=false;
85 }
86 
87 
88 typedef Meta::list<AppPolicy, DefaultCommPolicy> Policies;
89 
90 void generate_balance_plan(PropertiesPtr &props, const String &load_balancer,
91  ContextPtr &context, BalancePlanPtr &plan);
92 void create_table(String &ns, String &tablename, String &rs_metrics_file);
93 
94 int main(int argc, char **argv) {
95  String ns_str, table_str, load_balancer, rs_metrics_file;
96  String balance_plan_file;
98 
99  try {
100  init_with_policies<Policies>(argc, argv);
101 
102  if (has("verbose")) {
103  verbose = get_bool("verbose");
104  }
105 
106  table_str = get_str("table");
107  ns_str = get_str("namespace");
108  balance_plan_file = get_str("balance-plan-file");
109  load_balancer = get_str("load-balancer");
110  bool loaded = get_bool("rs-metrics-loaded");
111  if (has("rs-metrics-dump")) {
112  if (!loaded) {
113  rs_metrics_file = get_str("rs-metrics-dump");
114  create_table(ns_str, table_str, rs_metrics_file);
115  }
116  }
117  else if (!loaded) {
118  ns_str = "sys";
119  table_str = "RS_METRICS";
120  }
121 
122  ClientPtr client = make_shared<Hypertable::Client>(System::install_dir);
123  NamespacePtr ns = client->open_namespace(ns_str);
124  TablePtr rs_metrics = ns->open_table(table_str);
125  BalancePlanPtr plan = make_shared<BalancePlan>();
126  ContextPtr context = make_shared<Context>(properties);
127  context->rsc_manager.reset();
128  context->rs_metrics_table = rs_metrics;
129  generate_balance_plan(context->props, load_balancer, context, plan);
130  ostream *oo;
131 
132  if (balance_plan_file.size() == 0)
133  oo = &cout;
134  else
135  oo = new ofstream(balance_plan_file.c_str(), ios_base::app);
136 
137  size_t ii;
138 
139  *oo << "BalancePlan: { ";
140  if (plan->moves.size()>0) {
141  for(ii=0; ii<plan->moves.size()-1; ++ii) {
142  *oo << "(" << plan->moves[ii]->table.id << "[" << plan->moves[ii]->range.start_row
143  << ".." << plan->moves[ii]->range.end_row << "], " << plan->moves[ii]->source_location
144  << ", " << plan->moves[ii]->dest_location << "), ";
145  }
146 
147  *oo << "(" << plan->moves[ii]->table.id << "[" << plan->moves[ii]->range.start_row
148  << ".." << plan->moves[ii]->range.end_row << "], " << plan->moves[ii]->source_location
149  << ", " << plan->moves[ii]->dest_location << ")";
150  }
151  *oo << " }" << endl;
152  }
153  catch (Exception &e) {
154  HT_ERROR_OUT << e << HT_END;
155  exit(EXIT_FAILURE);
156  }
157 
158  fflush(stdout);
159  quick_exit(EXIT_SUCCESS); // don't bother with static objects
160 }
161 
162 void generate_balance_plan(PropertiesPtr &props, const String &load_balancer,
163  ContextPtr &context, BalancePlanPtr &plan) {
164 
165  if (load_balancer != "basic-distribute-load")
167  (String)"Only 'basic-distribute-load' balancer is supported. '" + load_balancer
168  + "' balancer not supported.");
169 
170  std::vector<RangeServerStatistics> range_server_stats;
171  // TODO fill this vector; otherwise disk usage is not taken into account
172 
173  BalanceAlgorithmLoad balancer(context, range_server_stats);
174  std::vector<RangeServerConnectionPtr> balanced;
175  balancer.compute_plan(plan, balanced);
176 }
177 
178 
179 void create_table(String &ns, String &table, String &rs_metrics_file) {
180 
181  String hql = (String) " USE \'" + ns + "\';\n" +
182  " DROP TABLE IF EXISTS " + table + ";\n" +
183  " CREATE TABLE " + table + "(\n" +
184  " server MAX_VERSIONS=336,\n" +
185  " range MAX_VERSIONS=24,\n" +
186  " \'range_start_row\' MAX_VERSIONS=1,\n" +
187  " \'range_move\' MAX_VERSIONS=1,\n" +
188  " ACCESS GROUP server (server),\n" +
189  " ACCESS GROUP range (range, \'range_start_row\', \'range_move\')\n" +
190  " );\n" +
191  " LOAD DATA INFILE \'" + rs_metrics_file + "\' INTO TABLE " + table + ";";
192 ;
193  // load from file
194  String install_dir = System::install_dir;
195  String cmd_str = install_dir + (String)"/bin/ht_hypertable --test-mode --exec \""+ hql + "\"";
196  if (verbose)
197  HT_INFOF("Running command: %s", cmd_str.c_str());
198  HT_EXPECT(::system(cmd_str.c_str()) == 0, Error::FAILED_EXPECTATION);
199 }
Retrieves system information (hardware, installation directory, etc)
Interface and base of config policy.
Definition: Config.h:149
int main(int argc, char **argv)
Boost library.
Definition: Properties.cc:39
PropertiesPtr properties
This singleton map stores all options.
Definition: Config.cc:47
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
std::shared_ptr< BalancePlan > BalancePlanPtr
Definition: BalancePlan.h:81
Meta::list< AppPolicy, DefaultCommPolicy > Policies
static void initialize(const String &install_directory=String())
Initializes the static class members; checks header version vs.
Definition: System.h:72
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
STL namespace.
virtual void compute_plan(BalancePlanPtr &plan, std::vector< RangeServerConnectionPtr > &balanced)
Desc & cmdline_desc(const char *usage)
A macro which definds global functions like get_bool(), get_str(), get_i16() etc. ...
Definition: Config.cc:72
bool has(const String &name)
Check existence of a configuration value.
Definition: Config.h:57
std::shared_ptr< Context > ContextPtr
Smart pointer to Context.
Definition: Context.h:265
#define HT_EXPECT(_e_, _code_)
Definition: Logger.h:388
std::shared_ptr< Namespace > NamespacePtr
Shared smart pointer to Namespace.
Definition: Namespace.h:333
std::shared_ptr< Client > ClientPtr
Definition: Client.h:156
void create_table(String &ns, String &tablename, String &rs_metrics_file)
std::shared_ptr< Properties > PropertiesPtr
Definition: Properties.h:447
Compatibility Macros for C/C++.
Po::typed_value< bool > * boo(bool *v=0)
Definition: Properties.h:162
Initialization helper for applications.
#define HT_END
Definition: Logger.h:220
#define HT_ERROR_OUT
Definition: Logger.h:301
bool allow_unregistered_options(bool choice)
Toggle allow unregistered options.
Definition: Config.cc:654
void generate_balance_plan(PropertiesPtr &props, const String &load_balancer, ContextPtr &context, BalancePlanPtr &plan)
Hypertable definitions
static String locate_install_dir(const char *argv0)
Returns the installation directory.
Definition: System.cc:50
#define HT_INFOF(msg,...)
Definition: Logger.h:272
static String install_dir
The installation directory.
Definition: System.h:114
This is a generic exception class for Hypertable.
Definition: Error.h:314
Error codes, Exception handling, error logging.
#define HT_THROW(_code_, _msg_)
Definition: Error.h:478
std::shared_ptr< Table > TablePtr
Definition: Table.h:53