0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TableMutator.h
Go to the documentation of this file.
1 /* -*- c++ -*-
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 #ifndef Hypertable_Lib_TableMutator_h
23 #define Hypertable_Lib_TableMutator_h
24 
25 #include "Cells.h"
26 #include "ClientObject.h"
27 #include "KeySpec.h"
28 #include "Table.h"
29 #include "TableMutatorAsync.h"
30 #include "TableMutatorQueue.h"
31 #include "TableCallback.h"
32 #include "RangeLocator.h"
33 #include "Schema.h"
34 #include "TableIdentifier.h"
35 
37 
38 #include <Common/Properties.h>
39 #include <Common/StringExt.h>
40 #include <Common/Timer.h>
41 
42 #include <condition_variable>
43 #include <iostream>
44 #include <mutex>
45 
46 namespace Hypertable {
47 
55  class TableMutator : public ClientObject {
56 
57  public:
58  enum {
61  };
62 
74  TableMutator(PropertiesPtr &props, Comm *comm, Table *table,
75  RangeLocatorPtr &range_locator, uint32_t timeout_ms,
76  uint32_t flags = 0);
77 
82  virtual ~TableMutator();
83 
91  virtual void set(const KeySpec &key, const void *value, uint32_t value_len);
92 
96  void set(const KeySpec &key, const char *value) {
97  if (value)
98  set(key, value, strlen(value));
99  else
100  set(key, 0, 0);
101  }
102 
106  void set(const KeySpec &key, const std::string &value) {
107  set(key, value.data(), value.length());
108  }
109 
116  virtual void set_delete(const KeySpec &key);
117 
124  virtual void set_cells(const Cells &cells) {
125  set_cells(cells.begin(), cells.end());
126  }
127 
131  virtual void flush();
132 
139  virtual bool retry(uint32_t timeout_ms=0);
140 
145  virtual bool need_retry() {
146  std::lock_guard<std::mutex> lock(m_mutex);
147  return (m_failed_mutations.size() > 0);
148  }
149 
155  virtual uint64_t memory_used() { return m_mutator->memory_used(); }
156 
166  uint64_t get_resend_count() { return m_mutator->get_resend_count(); }
167 
173  virtual void get_failed(FailedMutations &failed_mutations) {
174  std::lock_guard<std::mutex> lock(m_mutex);
175  failed_mutations = m_failed_mutations;
176  }
177 
179  std::ostream &show_failed(const Exception &, std::ostream & = std::cout);
180 
181  void update_ok();
182  void update_error(int error, FailedMutations &failures);
183 
184  int32_t get_last_error() {
185  std::lock_guard<std::mutex> lock(m_mutex);
186  return m_last_error;
187  }
188 
189  private:
190 
191  void auto_flush();
192 
193  friend class TableMutatorAsync;
195 
196  void set_last_error(int32_t error) {
197  std::lock_guard<std::mutex> lock(m_mutex);
198  m_last_error = error;
199  }
200 
201  enum Operation {
202  SET = 1,
206  };
207 
208  void save_last(const KeySpec &key, const void *value, size_t value_len) {
209  m_last_key = key;
210  m_last_value = value;
211  m_last_value_len = value_len;
212  }
213 
214  void save_last(Cells::const_iterator it, Cells::const_iterator end) {
215  m_last_cells_it = it;
216  m_last_cells_end = end;
217  }
218 
219  void set_cells(Cells::const_iterator start, Cells::const_iterator end);
220 
221  void handle_exceptions();
222 
223  void retry_flush();
224 
227  std::condition_variable m_cond;
234  uint64_t m_memory_used;
235  uint64_t m_max_memory;
239  uint32_t m_timeout_ms;
240  uint32_t m_flags;
241  uint32_t m_flush_delay;
242  int32_t m_last_error;
245  const void *m_last_value;
247  Cells::const_iterator m_last_cells_it;
248  Cells::const_iterator m_last_cells_end;
249  const static uint32_t ms_max_sync_retries = 5;
254  };
255 
257  typedef std::shared_ptr<TableMutator> TableMutatorPtr;
258 
259 }
260 
261 #endif // Hypertable_Lib_TableMutator_h
virtual uint64_t memory_used()
Returns the amount of memory used by the collected mutations.
Definition: TableMutator.h:155
static std::mutex mutex
Definition: Logger.cc:43
void wait_for_flush_completion(TableMutatorAsync *mutator)
FailedMutations m_failed_mutations
Definition: TableMutator.h:252
void set_last_error(int32_t error)
Definition: TableMutator.h:196
std::vector< Cell, CellAlloc > Cells
Definition: Cells.h:37
CellsBuilderPtr m_failed_cells
Definition: TableMutator.h:253
std::shared_ptr< TableMutatorQueue > TableMutatorQueuePtr
Shared smart pointer to TableMutatorQueue.
std::ostream & show_failed(const Exception &, std::ostream &=std::cout)
Show failed mutations.
std::shared_ptr< RangeLocator > RangeLocatorPtr
Smart pointer to RangeLocator.
Definition: RangeLocator.h:198
void save_last(const KeySpec &key, const void *value, size_t value_len)
Definition: TableMutator.h:208
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...
Declarations for TableIdentifier and TableIdentifierManaged.
Program options handling.
TableIdentifierManaged m_table_identifier
Definition: TableMutator.h:233
static const uint32_t ms_max_sync_retries
Definition: TableMutator.h:249
Declarations for Schema.
virtual void set(const KeySpec &key, const void *value, uint32_t value_len)
Inserts a cell into the table.
Definition: TableMutator.cc:86
void set(const KeySpec &key, const char *value)
Convenient helper for null-terminated values.
Definition: TableMutator.h:96
Provides the ability to mutate a table in the form of adding and deleting rows and cells...
std::shared_ptr< TableMutatorAsync > TableMutatorAsyncPtr
Wrapper for TableIdentifier providing member storage.
Cells::const_iterator m_last_cells_it
Definition: TableMutator.h:247
Represents an open table.
Definition: Table.h:58
virtual void set_cells(const Cells &cells)
Insert a bunch of cells into the table (atomically if cells are in the same range/row) ...
Definition: TableMutator.h:124
Represents an open table.
Definition: TableCallback.h:34
std::shared_ptr< TableMutator > TableMutatorPtr
Smart pointer to TableMutator.
Definition: TableMutator.h:257
Provides the ability to mutate a table in the form of adding and deleting rows and cells...
Definition: TableMutator.h:55
virtual ~TableMutator()
Destructor for TableMutator object Make sure buffers are flushed and unsynced rangeservers get synced...
Definition: TableMutator.cc:74
RangeLocatorPtr m_range_locator
Definition: TableMutator.h:232
void set(const KeySpec &key, const std::string &value)
Convenient helper for std::string values.
Definition: TableMutator.h:106
std::shared_ptr< Properties > PropertiesPtr
Definition: Properties.h:447
A timer class to keep timeout states across AsyncComm related calls.
Declarations for ClientObject.
virtual void get_failed(FailedMutations &failed_mutations)
Returns the failed mutations.
Definition: TableMutator.h:173
virtual bool retry(uint32_t timeout_ms=0)
Retries the last operation.
Base class for Hypertable client objects.
Definition: ClientObject.h:44
std::shared_ptr< CellsBuilder > CellsBuilderPtr
Smart pointer to CellsBuilder.
Definition: Cells.h:137
void save_last(Cells::const_iterator it, Cells::const_iterator end)
Definition: TableMutator.h:214
Hypertable definitions
Entry point to AsyncComm service.
Definition: Comm.h:61
Declarations for ConnectionManager.
uint64_t get_resend_count()
There are certain circumstances when mutations get flushed to the wrong range server due to stale ran...
Definition: TableMutator.h:166
TableMutatorAsyncPtr m_mutator
Definition: TableMutator.h:238
This is a generic exception class for Hypertable.
Definition: Error.h:314
TableCallback m_callback
Definition: TableMutator.h:236
std::condition_variable m_cond
Definition: TableMutator.h:227
TableMutator(PropertiesPtr &props, Comm *comm, Table *table, RangeLocatorPtr &range_locator, uint32_t timeout_ms, uint32_t flags=0)
Constructs the TableMutator object.
Definition: TableMutator.cc:59
std::shared_ptr< Schema > SchemaPtr
Smart pointer to Schema.
Definition: Schema.h:465
virtual void flush()
Flushes the accumulated mutations to their respective range servers.
void update_error(int error, FailedMutations &failures)
TableMutatorQueuePtr m_queue
Definition: TableMutator.h:237
String extensions and helpers: sets, maps, append operators etc.
virtual bool need_retry()
Checks for failed mutations.
Definition: TableMutator.h:145
std::shared_ptr< Table > TablePtr
Definition: Table.h:53
std::vector< FailedMutation > FailedMutations
Definition: Cells.h:39
Cells::const_iterator m_last_cells_end
Definition: TableMutator.h:248