0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
BalanceAlgorithmOffload.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 
25 #include "Utility.h"
26 
27 #include <Hypertable/Lib/Client.h>
28 
29 #include <Common/Error.h>
30 
31 #include <boost/algorithm/string.hpp>
32 
33 #include <algorithm>
34 
35 using namespace Hypertable;
36 using namespace std;
37 
39  std::vector<RangeServerStatistics> &statistics, String arguments)
40  : m_context(context), m_statistics(statistics) {
41  boost::trim(arguments);
42  boost::split(m_offload_servers, arguments, boost::is_any_of(", \t"));
43 }
44 
45 
47  std::vector<RangeServerConnectionPtr> &balanced) {
48  StringSet locations;
49  StringSet::iterator locations_it;
51 
52  for (const auto &stats : m_statistics) {
53  if (m_offload_servers.find(stats.location) == m_offload_servers.end()) {
54  if (!stats.stats || !stats.stats->live || !m_context->can_accept_ranges(stats))
55  continue;
56  locations.insert(stats.location);
57  }
58  }
59 
60  if (locations.empty())
62  "No destination servers found with enough room");
63 
64  locations_it = locations.begin();
65 
66  ScanSpec scan_spec;
67  Cell cell;
68  String last_key, last_location, last_start_row;
69  bool read_start_row = false;
70  String location("Location"), start_row("StartRow");
71 
72  // check if we need to move root range
73  if (m_offload_servers.find(root_location) != m_offload_servers.end()) {
74  String new_location = *locations_it;
75  locations_it++;
76  if (locations_it == locations.end())
77  locations_it = locations.begin();
78 
79  RangeMoveSpecPtr move = make_shared<RangeMoveSpec>(root_location.c_str(),
80  new_location.c_str(), TableIdentifier::METADATA_ID,
81  "", Key::END_ROOT_ROW);
82  plan->moves.push_back(move);
83 
85  << Key::END_ROOT_ROW << "] " << root_location << "->"
86  << new_location << HT_END;
87  }
88 
89  scan_spec.columns.push_back(location.c_str());
90  scan_spec.columns.push_back(start_row.c_str());
91  scan_spec.max_versions = 1;
92 
93  TableScannerPtr scanner(m_context->metadata_table->create_scanner(scan_spec));
94  while (scanner->next(cell)) {
95  if (last_key == cell.row_key) {
96  if (location == cell.column_family)
97  last_location = String((const char *)cell.value, cell.value_len);
98  else {
99  read_start_row = true;
100  last_start_row = String((const char *)cell.value, cell.value_len);
101  }
102  }
103  else {
104  last_key = cell.row_key;
105  if (location == cell.column_family) {
106  last_start_row.clear();
107  read_start_row = false;
108  last_location = String((const char *)cell.value, cell.value_len);
109  }
110  else {
111  last_location.clear();
112  read_start_row = true;
113  last_start_row = String((const char *)cell.value, cell.value_len);
114  }
115  }
116 
117  HT_DEBUG_OUT << "last_key=" << last_key << ", last_location="
118  << last_location << ", last_start_row=" << last_start_row << HT_END;
119 
120  // check if this range is on one of the offload servers
121  if (last_location.size() > 0 && read_start_row &&
122  m_offload_servers.find(last_location) != m_offload_servers.end()) {
123  size_t pos = last_key.find(':');
124  HT_ASSERT(pos != string::npos);
125  String table(last_key, 0, pos);
126  String end_row(last_key, pos+1);
127  String new_location = *locations_it;
128  locations_it++;
129  if (locations_it == locations.end())
130  locations_it = locations.begin();
131 
132  RangeMoveSpecPtr move = make_shared<RangeMoveSpec>(last_location.c_str(),
133  new_location.c_str(),
134  table.c_str(), last_start_row.c_str(),
135  end_row.c_str());
136  plan->moves.push_back(move);
137 
138  HT_DEBUG_OUT << table << "[" << last_start_row << ".." << end_row << "] "
139  << last_location << "->" << new_location << HT_END;
140  }
141  }
142 }
143 
std::set< String > StringSet
STL Set managing Strings.
Definition: StringExt.h:42
BalanceAlgorithmOffload(ContextPtr &context, std::vector< RangeServerStatistics > &statistics, String arguments)
static const char * METADATA_ID
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
STL namespace.
std::shared_ptr< TableScanner > TableScannerPtr
Smart pointer to TableScanner.
Definition: TableScanner.h:124
std::shared_ptr< Context > ContextPtr
Smart pointer to Context.
Definition: Context.h:265
#define HT_ASSERT(_e_)
Definition: Logger.h:396
Scan predicate and control specification.
Definition: ScanSpec.h:56
Compatibility Macros for C/C++.
virtual void compute_plan(BalancePlanPtr &plan, std::vector< RangeServerConnectionPtr > &balanced)
const char * row_key
Definition: Cell.h:66
#define HT_END
Definition: Logger.h:220
Hypertable definitions
Declarations for general-purpose utility functions.
std::vector< RangeServerStatistics > m_statistics
const char * column_family
Definition: Cell.h:67
String root_range_location(ContextPtr &context)
Returns location of root METADATA range.
Definition: Utility.cc:389
bool split(int flags)
Tests the SPLIT bit of flags
static const char * END_ROOT_ROW
Definition: Key.h:50
uint32_t value_len
Definition: Cell.h:72
std::shared_ptr< RangeMoveSpec > RangeMoveSpecPtr
Encapsulates decomposed key and value.
Definition: Cell.h:32
Error codes, Exception handling, error logging.
#define HT_THROW(_code_, _msg_)
Definition: Error.h:478
#define HT_DEBUG_OUT
Definition: Logger.h:261
const uint8_t * value
Definition: Cell.h:71