GPUdb C++ API  Version 7.1.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GPUdb.hpp
Go to the documentation of this file.
1 #ifndef __GPUDB_HPP__
2 #define __GPUDB_HPP__
3 
5 #include "gpudb/utils/Utils.h"
6 
7 #ifndef GPUDB_NO_HTTPS
8 #include <boost/asio/ssl.hpp>
9 #endif
10 
11 #include <stdint.h>
12 
13 #include <boost/thread/mutex.hpp>
14 
15 namespace gpudb
16 {
17  class GPUdb;
18 }
19 
20 #include "gpudb/Avro.hpp"
21 #include "gpudb/GenericRecord.hpp"
22 #include "gpudb/GPUdbException.hpp"
23 #include "gpudb/Http.hpp"
24 #include "gpudb/Type.hpp"
26 
27 
54 namespace gpudb
55 {
56 class GPUdb : private boost::noncopyable
57 {
58 public:
59 
60  class Options
61  {
62  public:
63  Options();
64 
65 #ifndef GPUDB_NO_HTTPS
66  boost::asio::ssl::context* getSslContext() const;
67  bool getBypassSslCertCheck() const;
68 #endif
69 
70  std::string getUsername() const;
71  std::string getPassword() const;
74  std::string getPrimaryUrl() const;
75  bool getUseSnappy() const;
76  size_t getThreadCount() const;
77  avro::ExecutorPtr getExecutor() const;
78 
81  std::map<std::string, std::string>& getHttpHeaders();
82  const std::map<std::string, std::string>& getHttpHeaders() const;
83 
84  size_t getTimeout() const;
85  uint16_t getHostManagerPort() const;
86  bool getDisableFailover() const;
87  bool getDisableAutoDiscovery() const;
88 
89 #ifndef GPUDB_NO_HTTPS
90  Options& setSslContext(boost::asio::ssl::context* value);
91  Options& setBypassSslCertCheck(const bool value);
92 #endif
93 
94  Options& setUsername(const std::string& value);
95  Options& setPassword(const std::string& value);
97  Options& setPrimaryUrl(const std::string& value);
98  Options& setUseSnappy(const bool value);
99  Options& setThreadCount(const size_t value);
100  Options& setExecutor(const avro::ExecutorPtr value);
101  Options& setHttpHeaders(const std::map<std::string, std::string>& value);
102  Options& addHttpHeader(const std::string& header, const std::string& value);
103  Options& setTimeout(const size_t value);
104  Options& setHostManagerPort(const uint16_t value);
105  Options& setDisableFailover(const bool value);
106  Options& setDisableAutoDiscovery(const bool value);
107 
108  private:
109 
110 #ifndef GPUDB_NO_HTTPS
111  boost::asio::ssl::context* m_sslContext;
112  bool m_bypassSslCertCheck;
113 #endif
114 
115  std::string m_username;
116  std::string m_password;
117  std::string m_primaryUrl;
118  bool m_useSnappy;
119  bool m_disableFailover;
120  bool m_disableAutoDiscovery;
121  size_t m_threadCount;
122  avro::ExecutorPtr m_executor;
123  std::map<std::string, std::string> m_httpHeaders;
124  size_t m_timeout;
125  uint16_t m_hmPort;
126  };
127 
128 
138  // No override; defer to the HA process for synchronizing
139  // endpoints (which has different logic for different endpoints)
140  DEFAULT = 0, // maps to 'default'
141  // Explicitly do NOT replicate across the HA cluster
142  NONE, // maps to 'REPL_NONE'
143  // Synchronize all endpoint calls
144  SYNCHRONOUS, // maps to 'REPL_SYNC'
145  // Do NOT synchronize any endpoint call
146  ASYNCHRONOUS // maps to 'REPL_ASYNC'
147  };
148 
149 
150 
151  static const int64_t END_OF_SET = -9999;
152 
155  static const std::string DB_CONNECTION_RESET_ERROR_MESSAGE;
156  static const std::string DB_CONNECTION_REFUSED_ERROR_MESSAGE;
157  static const std::string DB_EXITING_ERROR_MESSAGE;
158  static const std::string DB_OFFLINE_ERROR_MESSAGE;
159  static const std::string DB_SYSTEM_LIMITED_ERROR_MESSAGE;
160  static const std::string DB_HM_OFFLINE_ERROR_MESSAGE;
161 
164  static const std::string HEADER_AUTHORIZATION;
165  static const std::string HEADER_CONTENT_TYPE;
166  static const std::string HEADER_CONTENT_LENGTH;
167  static const std::string HEADER_HA_SYNC_MODE;
168 
169 
170  static inline std::string getApiVersion() { return GPUdb::API_VERSION; }
171 
185  GPUdb(const HttpUrl& url, const Options& options = Options());
186 
204  GPUdb(const std::string& url, const Options& options = Options());
205 
219  GPUdb(const std::vector<HttpUrl>& urls, const Options& options = Options());
220 
235  GPUdb(const std::vector<std::string>& urls, const Options& options = Options());
236 
238  ~GPUdb();
239 
242  const HttpUrl& getUrl() const;
243  const std::vector<HttpUrl>& getUrls() const;
244  const HttpUrl& getHmUrl() const;
245  const std::vector<HttpUrl>& getHmUrls() const;
246 
247 #ifndef GPUDB_NO_HTTPS
248  boost::asio::ssl::context* getSslContext() const;
249  bool getBypassSslCertCheck() const;
250 #endif
251 
252  const std::string& getUsername() const;
253  const std::string& getPassword() const;
254 
257  const std::string& getPrimaryURL() const;
258 
259  bool getUseSnappy() const;
260  size_t getThreadCount() const;
261  avro::ExecutorPtr getExecutor() const;
262  const std::map<std::string, std::string>& getHttpHeaders() const;
264  size_t getTimeout() const;
265 
268 
273  void setHASyncMode( HASynchronicityMode mode );
274 
293  void addHttpHeader( const std::string& header,
294  const std::string& value );
295 
312  void removeHttpHeader( const std::string& header );
313 
314 
316  void updateHostManagerPort();
317 
318  template<typename TRequest, typename TResponse>
319  TResponse& submitRequest(const HttpUrl& url,
320  const TRequest& request,
321  TResponse& response,
322  const bool enableCompression = false) const
323  {
324  std::vector<uint8_t> requestBytes;
325  try // encoding the request
326  {
327  avro::encode(requestBytes, request);
328  }
329  catch ( const std::exception& ex )
330  {
331  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while encoding request: "
332  << ex.what() );
333  throw GPUdbException( message );
334  }
335 
336  // Submit the request
337  RawGpudbResponse gpudbResponse;
338  submitRequestRaw(url, requestBytes, gpudbResponse, enableCompression);
339 
340  try // decoding the request
341  {
342  avro::decode(response, gpudbResponse.data);
343  }
344  catch ( const std::exception& ex )
345  {
346  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while decoding response: '"
347  << ex.what()
348  << "'; please ensure that the client API matches the server API version." );
349  throw GPUdbException( message );
350  }
351  return response;
352  }
353 
354  template<typename TRequest, typename TResponse>
355  TResponse& submitRequest(const std::string& endpoint,
356  const TRequest& request,
357  TResponse& response,
358  const bool enableCompression = false) const
359  {
360  std::vector<uint8_t> requestBytes;
361  try // encoding the request
362  {
363  avro::encode(requestBytes, request);
364  }
365  catch ( const std::exception& ex )
366  {
367  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while encoding request: "
368  << ex.what() );
369  throw GPUdbException( message );
370  }
371 
372  // Submit the request
373  RawGpudbResponse gpudbResponse;
374  submitRequestRaw(endpoint, requestBytes, gpudbResponse, enableCompression);
375 
376  try // decoding the request
377  {
378  avro::decode(response, gpudbResponse.data);
379  }
380  catch ( const std::exception& ex )
381  {
382  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while decoding response: '"
383  << ex.what()
384  << "'; please ensure that the client API matches the server API version." );
385  throw GPUdbException( message );
386  }
387 
388  return response;
389  }
390 
391  template<typename TRequest, typename TResponse>
392  TResponse& submitRequest(const char* endpoint,
393  const TRequest& request,
394  TResponse& response,
395  const bool enableCompression = false) const
396  {
397  submitRequest( (std::string) endpoint, request, response, enableCompression );
398  return response;
399  }
400 
401 
415  template<typename TRequest, typename TResponse>
416  TResponse& submitRequestToHostManager(const std::string& endpoint,
417  const TRequest& request,
418  TResponse& response,
419  const bool enableCompression = false) const
420  {
421  // Handle host manager stuff here
422  std::vector<uint8_t> requestBytes;
423  try // encoding the request
424  {
425  avro::encode(requestBytes, request);
426  }
427  catch ( const std::exception& ex )
428  {
429  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while encoding request: "
430  << ex.what() );
431  throw GPUdbException( message );
432  }
433 
434  // Submit the request
435  RawGpudbResponse gpudbResponse;
436  submitRequestToHostManagerRaw(endpoint, requestBytes, gpudbResponse, enableCompression);
437 
438  try // decoding the request
439  {
440  avro::decode(response, gpudbResponse.data);
441  }
442  catch ( const std::exception& ex )
443  {
444  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while decoding response: '"
445  << ex.what()
446  << "'; please ensure that the client API matches the server API version." );
447  throw GPUdbException( message );
448  }
449  return response;
450  } // end submitRequestToHostManager
451 
452 
466  template<typename TRequest, typename TResponse>
467  TResponse& submitRequestToHostManager(const char* endpoint,
468  const TRequest& request,
469  TResponse& response,
470  const bool enableCompression = false) const
471  {
472  submitRequestToHostManager( (std::string) endpoint, request, response, enableCompression );
473  return response;
474  } // end submitRequestToHostManager
475 
476 
477 #include "gpudb/GPUdbFunctions.hpp"
478 
479  void addKnownType(const std::string& typeId, const avro::DecoderPtr& decoder);
480 
481  template<typename T>
482  void addKnownType(const std::string& typeId)
483  {
484  addKnownType(typeId, avro::createDecoder<T>());
485  }
486 
487  template<typename T>
488  void addKnownType(const std::string& typeId, const std::string& schemaString)
489  {
490  addKnownType(typeId, avro::createDecoder<T>(schemaString));
491  }
492 
493  template<typename T>
494  void addKnownType(const std::string& typeId, const ::avro::ValidSchema& schema)
495  {
496  addKnownType(typeId, avro::createDecoder<T>(schema));
497  }
498 
499  void addKnownTypeFromTable(const std::string& tableName, const avro::DecoderPtr& decoder);
500 
501  template<typename T>
502  void addKnownTypeFromTable(const std::string& tableName)
503  {
504  addKnownTypeFromTable(tableName, avro::createDecoder<T>());
505  }
506 
507  template<typename T>
508  void addKnownTypeFromTable(const std::string& tableName, const std::string& schemaString)
509  {
510  addKnownTypeFromTable(tableName, avro::createDecoder<T>(schemaString));
511  }
512 
513  template<typename T>
514  void addKnownTypeFromTable(const std::string& tableName, const ::avro::ValidSchema& schema)
515  {
516  addKnownTypeFromTable(tableName, avro::createDecoder<T>(schema));
517  }
518 
519 private:
520 
521  static const std::string API_VERSION;
522 
523  static const std::string FAILOVER_TRIGGER_MESSAGES[];
524  static const std::string PROTECTED_HEADERS[];
525  static const std::string HA_SYNCHRONICITY_MODE_VALUES[];
526 
527  static const size_t NUM_TRIGGER_MESSAGES;
528 
529  mutable std::vector<HttpUrl> m_urls;
530  mutable std::vector<HttpUrl> m_hmUrls;
531  std::string m_primaryUrlStr;
532  HttpUrl* m_primaryUrlPtr;
533  mutable std::vector<size_t> m_urlIndices;
534  mutable boost::mutex m_urlMutex;
535  mutable size_t m_currentUrl;
536 
537 #ifndef GPUDB_NO_HTTPS
538  boost::asio::ssl::context* m_sslContext;
539 #endif
540 
541  std::string m_username;
542  std::string m_password;
543  std::string m_authorization;
544  bool m_useSnappy;
545  bool m_disableFailover;
546  bool m_bypassSslCertCheck;
547  bool m_disableAutoDiscovery;
548  size_t m_threadCount;
549  avro::ExecutorPtr m_executor;
550  std::map<std::string, std::string> m_httpHeaders;
551  size_t m_timeout;
552  mutable HASynchronicityMode m_haSyncMode;
553  Options m_options;
554 
555  mutable std::map<std::string, avro::DecoderPtr> m_knownTypes;
556  mutable boost::mutex m_knownTypesMutex;
557 
558 
562  void init();
563 
564  // Handle the primary host URL, if any is given via options
565  void handlePrimaryURL();
566 
567  // Update the URLs with the available HA ring information
568  void getHAringHeadNodeAddresses();
569 
571  void updateHostManagerUrls();
572  void setHostManagerPort(uint16_t value);
573 
579  void randomizeURLs() const;
580 
583  const std::string& getHASynchronicityModeValue( HASynchronicityMode syncMode ) const;
584 
587  const HttpUrl* getUrlPointer() const;
588  const HttpUrl* getHmUrlPointer() const;
589  const HttpUrl* switchUrl(const HttpUrl* oldUrl) const;
590  const HttpUrl* switchHmUrl(const HttpUrl* oldUrl) const;
591 
592 
595  void initHttpRequest(HttpRequest& httpRequest) const;
596  void submitRequestRaw(const std::string& endpoint,
597  const std::vector<uint8_t>& request,
598  RawGpudbResponse& response,
599  const bool enableCompression) const;
600  void submitRequestToHostManagerRaw(const std::string& endpoint,
601  const std::vector<uint8_t>& request,
602  RawGpudbResponse& response,
603  const bool enableCompression) const;
604  void submitRequestRaw(const HttpUrl& url,
605  const std::vector<uint8_t>& request,
606  RawGpudbResponse& response,
607  const bool enableCompression,
608  const bool throwOnError = true) const;
609 
611  avro::DecoderPtr getDecoder(const std::string& typeId) const;
612  void setDecoderIfMissing(const std::string& typeId,
613  const std::string& label,
614  const std::string& schemaString,
615  const std::map<std::string, std::vector<std::string> >& properties) const;
616 
617 };
618 
619 #include "gpudb/GPUdbTemplates.hpp"
620 
621 } // end namespace gpudb
622 
623 #endif
const HttpUrl & getHmUrl() const
bool getBypassSslCertCheck() const
Options & setSslContext(boost::asio::ssl::context *value)
void addKnownType(const std::string &typeId, const avro::DecoderPtr &decoder)
void addKnownTypeFromTable(const std::string &tableName, const ::avro::ValidSchema &schema)
Definition: GPUdb.hpp:514
Options & setHttpHeaders(const std::map< std::string, std::string > &value)
HASynchronicityMode
A enumeration of high-availability synchronicity override modes.
Definition: GPUdb.hpp:137
static const std::string DB_OFFLINE_ERROR_MESSAGE
Definition: GPUdb.hpp:158
GPUdb(const HttpUrl &url, const Options &options=Options())
Pass a single HttpURL and options to instantiate a GPUdb object.
void updateHostManagerPort()
Update the host manager port by inquiring the server.
bool getBypassSslCertCheck() const
std::string getPrimaryUrl() const
Return the URL of the primary cluster, if any (empty string delineates that none was set) ...
#define GPUDB_STREAM_TO_STRING(...)
Definition: Utils.h:19
static const std::string HEADER_AUTHORIZATION
Headers used internally; MUST add each of them to PROTECTED_HEADERS in the .cpp file.
Definition: GPUdb.hpp:164
void addKnownType(const std::string &typeId)
Definition: GPUdb.hpp:482
void addKnownTypeFromTable(const std::string &tableName)
Definition: GPUdb.hpp:502
TResponse & submitRequest(const HttpUrl &url, const TRequest &request, TResponse &response, const bool enableCompression=false) const
Definition: GPUdb.hpp:319
const std::string & getPassword() const
TResponse & submitRequestToHostManager(const std::string &endpoint, const TRequest &request, TResponse &response, const bool enableCompression=false) const
Submit an HTTP request to the host manager.
Definition: GPUdb.hpp:416
const std::vector< HttpUrl > & getUrls() const
bool getDisableAutoDiscovery() const
static const std::string HEADER_CONTENT_LENGTH
Definition: GPUdb.hpp:166
Options & setHostManagerPort(const uint16_t value)
const std::vector< HttpUrl > & getHmUrls() const
avro::ExecutorPtr getExecutor() const
static const std::string DB_CONNECTION_RESET_ERROR_MESSAGE
Special error messages indicating that a connection failure happened (generally should trigger a high...
Definition: GPUdb.hpp:155
static const std::string HEADER_HA_SYNC_MODE
Definition: GPUdb.hpp:167
std::string getPassword() const
TResponse & submitRequestToHostManager(const char *endpoint, const TRequest &request, TResponse &response, const bool enableCompression=false) const
Submit an HTTP request to the host manager.
Definition: GPUdb.hpp:467
~GPUdb()
Destructor.
Options & setUsername(const std::string &value)
static const std::string DB_EXITING_ERROR_MESSAGE
Definition: GPUdb.hpp:157
const std::map< std::string, std::string > & getHttpHeaders() const
Options & setDisableFailover(const bool value)
size_t getThreadCount() const
void addKnownTypeFromTable(const std::string &tableName, const avro::DecoderPtr &decoder)
bool getDisableFailover() const
void addHttpHeader(const std::string &header, const std::string &value)
Adds an HTTP header to the map of additional HTTP headers to send to GPUdb with each request...
TResponse & submitRequest(const std::string &endpoint, const TRequest &request, TResponse &response, const bool enableCompression=false) const
Definition: GPUdb.hpp:355
const std::string & getUsername() const
void addKnownType(const std::string &typeId, const ::avro::ValidSchema &schema)
Definition: GPUdb.hpp:494
uint16_t getHostManagerPort() const
size_t getThreadCount() const
Options & setPrimaryUrl(const std::string &value)
Set the URL for the primary cluster.
boost::asio::ssl::context * getSslContext() const
static const std::string DB_HM_OFFLINE_ERROR_MESSAGE
Definition: GPUdb.hpp:160
static std::string getApiVersion()
Definition: GPUdb.hpp:170
Options & setBypassSslCertCheck(const bool value)
Options & setExecutor(const avro::ExecutorPtr value)
size_t getTimeout() const
void setHASyncMode(HASynchronicityMode mode)
Some setters
std::map< std::string, std::string > & getHttpHeaders()
Get the HTTP headers (will include the high-availability synchronicity override header) ...
std::string getUsername() const
HASynchronicityMode getHASyncMode() const
static const int64_t END_OF_SET
Definition: GPUdb.hpp:151
boost::asio::ssl::context * getSslContext() const
TResponse & submitRequest(const char *endpoint, const TRequest &request, TResponse &response, const bool enableCompression=false) const
Definition: GPUdb.hpp:392
Options & setTimeout(const size_t value)
avro::ExecutorPtr getExecutor() const
static const std::string HEADER_CONTENT_TYPE
Definition: GPUdb.hpp:165
const HttpUrl & getUrl() const
Some getters
A set of output parameters forendpoint /gpudbresponse}.
Definition: gpudb_wrapper.h:17
Options & setThreadCount(const size_t value)
Options & setDisableAutoDiscovery(const bool value)
void addKnownTypeFromTable(const std::string &tableName, const std::string &schemaString)
Definition: GPUdb.hpp:508
bool getUseSnappy() const
std::vector< uint8_t > data
Definition: gpudb_wrapper.h:35
static const std::string DB_CONNECTION_REFUSED_ERROR_MESSAGE
Definition: GPUdb.hpp:156
const std::string & getPrimaryURL() const
Return a string containing the URL for the primary cluster; empty string otherwise.
Options & addHttpHeader(const std::string &header, const std::string &value)
static const std::string DB_SYSTEM_LIMITED_ERROR_MESSAGE
Definition: GPUdb.hpp:159
Options & setUseSnappy(const bool value)
size_t getTimeout() const
void removeHttpHeader(const std::string &header)
Removes the given HTTP header from the map of additional HTTP headers to send to GPUdb with each requ...
Options & setPassword(const std::string &value)
void addKnownType(const std::string &typeId, const std::string &schemaString)
Definition: GPUdb.hpp:488
bool getUseSnappy() const