0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
LoadMetricsRange.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.
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 "Global.h"
25 #include "LoadMetricsRange.h"
26 
27 #include <Common/String.h>
28 
29 #include <ctime>
30 
31 using namespace Hypertable;
32 using namespace std;
33 
34 LoadMetricsRange::LoadMetricsRange(const String &table_id, const String &start_row, const String &end_row)
35  : m_new_rows(false), m_timestamp(time(0)) {
36  initialize(table_id, start_row, end_row);
37 }
38 
39 
49  LoadFactors &load_factors,
50  uint64_t disk_used, uint64_t memory_used,
51  double compression_ratio) {
52  bool update_start_row = false;
53  String old_start_row, old_end_row;
54 
55  if (m_new_rows) {
56  lock_guard<mutex> lock(m_mutex);
57  uint8_t *oldbuf = m_buffer.release();
58  update_start_row = true;
59  old_start_row = m_start_row;
60  old_end_row = m_end_row;
62  m_new_rows = false;
63  delete [] oldbuf;
64  }
65 
66  if ((now - m_timestamp) <= 0)
67  return;
68 
69  time_t rounded_time = (now+(Global::metrics_interval/2)) - ((now+(Global::metrics_interval/2))%Global::metrics_interval);
70  double time_interval = (double)(now - m_timestamp);
71  double scan_rate = (double)(load_factors.scans-m_load_factors.scans) / time_interval;
72  double update_rate = (double)(load_factors.updates-m_load_factors.updates) / time_interval;
73  double cell_read_rate = (double)(load_factors.cells_scanned-m_load_factors.cells_scanned) / time_interval;
74  double cell_write_rate = (double)(load_factors.cells_written-m_load_factors.cells_written) / time_interval;
75  double byte_read_rate = (double)(load_factors.bytes_scanned-m_load_factors.bytes_scanned) / time_interval;
76  double byte_write_rate = (double)(load_factors.bytes_written-m_load_factors.bytes_written) / time_interval;
77  double disk_byte_read_rate = (double)(load_factors.disk_bytes_read-m_load_factors.disk_bytes_read) / time_interval;
78 
79  String value = format("3:%ld,%llu,%llu,%.6f,%.6f,%.6f,%.6f,%.6f,%.6f,%.6f,%.6f",
80  rounded_time, (Llu)disk_used, (Llu)memory_used,
81  compression_ratio, disk_byte_read_rate, byte_write_rate,
82  byte_read_rate, update_rate, scan_rate, cell_write_rate,
83  cell_read_rate);
84 
85  KeySpec key;
86  String row = Global::location_initializer->get() + ":" + m_table_id;
87 
88  key.row = row.c_str();
89  key.row_len = row.length();
90 
91  if (update_start_row) {
92  try {
93  // delete old entries
94  key.flag = FLAG_DELETE_CELL;
95  key.column_qualifier = old_end_row.c_str();
96  key.column_qualifier_len = old_end_row.size();
97  // delete old start row
98  key.column_family = "range_start_row";
99  mutator->set_delete(key);
100  // delete old range metrics
101  key.column_family = "range";
102  mutator->set_delete(key);
103  // delete old move
104  key.column_family = "range_move";
105  mutator->set_delete(key);
106 
107  // set new qualifier
109  key.column_qualifier_len = strlen(m_end_row);
110  // insert new start row
111  key.column_family = "range_start_row";
112  mutator->set(key, (uint8_t *)m_start_row, strlen(m_start_row));
113  }
114  catch (Exception &e) {
115  HT_ERROR_OUT << "Problem updating sys/RS_METRICS - " << e << HT_END;
116  }
117  }
118  else {
120  key.column_qualifier_len = strlen(m_end_row);
121  }
122 
123 
124  key.column_family = "range";
125  try {
126  mutator->set(key, (uint8_t *)value.c_str(), value.length());
127  }
128  catch (Exception &e) {
129  HT_ERROR_OUT << "Problem updating sys/RS_METRICS - " << e << HT_END;
130  }
131 
132  m_timestamp = now;
133  m_load_factors = load_factors;
134 }
135 
136 
137 void LoadMetricsRange::initialize(const String &table_id, const String &start_row,
138  const String &end_row) {
139 
140  m_buffer.reserve(table_id.length() + 1 + start_row.length() + 1 + end_row.length() + 1);
141 
142  // save table ID
143  m_table_id = (const char *)m_buffer.ptr;
144  m_buffer.add(table_id.c_str(), table_id.length()+1);
145 
146  // save start row
147  m_start_row = (const char *)m_buffer.ptr;
148  m_buffer.add(start_row.c_str(), start_row.length()+1);
149 
150  // save end row
151  m_end_row = (const char *)m_buffer.ptr;
152  m_buffer.add(end_row.c_str(), end_row.length()+1);
153 }
static LocationInitializerPtr location_initializer
Definition: Global.h:83
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
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 void set_delete(const KeySpec &key)
Deletes an entire row, a column family in a particular row, or a specific cell within a row...
long long unsigned int Llu
Shortcut for printf formats.
Definition: String.h:50
const char * column_qualifier
Definition: KeySpec.h:128
static const uint32_t FLAG_DELETE_CELL
Definition: KeySpec.h:42
STL namespace.
size_t column_qualifier_len
Definition: KeySpec.h:129
const void * row
Definition: KeySpec.h:125
uint8_t * ptr
Pointer to the end of the used part of the buffer.
virtual void set(const KeySpec &key, const void *value, uint32_t value_len)
Inserts a cell into the table.
Definition: TableMutator.cc:86
Provides the ability to mutate a table in the form of adding and deleting rows and cells...
Definition: TableMutator.h:55
uint8_t * add(const void *data, size_t len)
Adds more data WITH boundary checks; if required the buffer is resized and existing data is preserved...
Compatibility Macros for C/C++.
#define HT_END
Definition: Logger.h:220
#define HT_ERROR_OUT
Definition: Logger.h:301
Hypertable definitions
LoadMetricsRange(const String &table_id, const String &start_row, const String &end_row)
static int32_t metrics_interval
Definition: Global.h:109
This is a generic exception class for Hypertable.
Definition: Error.h:314
A String class based on std::string.
uint8_t * release(size_t *lenp=0)
Moves ownership of the buffer to the caller.
void compute_and_store(TableMutator *mutator, time_t now, LoadFactors &load_factors, uint64_t disk_used, uint64_t memory_used, double compression_ratio)
Value format for version 1:
const char * column_family
Definition: KeySpec.h:127
void initialize(const String &table_id, const String &start_row, const String &end_row)
void reserve(size_t len, bool nocopy=false)
Reserve space for additional data Will grow the space to exactly what's needed.
Definition: DynamicBuffer.h:95