GPUdb C++ API  Version 7.0.19.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 #endif
68 
69  std::string getUsername() const;
70  std::string getPassword() const;
73  std::string getPrimaryUrl() const;
74  bool getUseSnappy() const;
75  size_t getThreadCount() const;
76  avro::ExecutorPtr getExecutor() const;
77 
80  std::map<std::string, std::string>& getHttpHeaders();
81  const std::map<std::string, std::string>& getHttpHeaders() const;
82 
83  size_t getTimeout() const;
84  uint16_t getHostManagerPort() const;
85 
86 #ifndef GPUDB_NO_HTTPS
87  Options& setSslContext(boost::asio::ssl::context* value);
88 #endif
89 
90  Options& setUsername(const std::string& value);
91  Options& setPassword(const std::string& value);
93  Options& setPrimaryUrl(const std::string& value);
94  Options& setUseSnappy(const bool value);
95  Options& setThreadCount(const size_t value);
96  Options& setExecutor(const avro::ExecutorPtr value);
97  Options& setHttpHeaders(const std::map<std::string, std::string>& value);
98  Options& addHttpHeader(const std::string& header, const std::string& value);
99  Options& setTimeout(const size_t value);
100  Options& setHostManagerPort(const uint16_t value);
101 
102  private:
103 
104 #ifndef GPUDB_NO_HTTPS
105  boost::asio::ssl::context* m_sslContext;
106 #endif
107 
108  std::string m_username;
109  std::string m_password;
110  std::string m_primaryUrl;
111  bool m_useSnappy;
112  size_t m_threadCount;
113  avro::ExecutorPtr m_executor;
114  std::map<std::string, std::string> m_httpHeaders;
115  size_t m_timeout;
116  uint16_t m_hmPort;
117  };
118 
119 
129  // No override; defer to the HA process for synchronizing
130  // endpoints (which has different logic for different endpoints)
131  DEFAULT = 0, // maps to 'none'
132  // Synchronize all endpoint calls
133  SYNCHRONOUS, // maps to 'sync'
134  // Do NOT synchronize any endpoint call
135  ASYNCHRONOUS // maps to 'async'
136  };
137 
138 
139 
140  static const int64_t END_OF_SET = -9999;
141 
144  static const std::string DB_CONNECTION_RESET_ERROR_MESSAGE;
145  static const std::string DB_CONNECTION_REFUSED_ERROR_MESSAGE;
146  static const std::string DB_EXITING_ERROR_MESSAGE;
147  static const std::string DB_OFFLINE_ERROR_MESSAGE;
148  static const std::string DB_SYSTEM_LIMITED_ERROR_MESSAGE;
149  static const std::string DB_HM_OFFLINE_ERROR_MESSAGE;
150 
153  static const std::string HEADER_AUTHORIZATION;
154  static const std::string HEADER_CONTENT_TYPE;
155  static const std::string HEADER_CONTENT_LENGTH;
156  static const std::string HEADER_HA_SYNC_MODE;
157 
158 
159  static inline std::string getApiVersion() { return GPUdb::API_VERSION; }
160 
174  GPUdb(const HttpUrl& url, const Options& options = Options());
175 
193  GPUdb(const std::string& url, const Options& options = Options());
194 
208  GPUdb(const std::vector<HttpUrl>& urls, const Options& options = Options());
209 
224  GPUdb(const std::vector<std::string>& urls, const Options& options = Options());
225 
227  ~GPUdb();
228 
231  const HttpUrl& getUrl() const;
232  const std::vector<HttpUrl>& getUrls() const;
233  const HttpUrl& getHmUrl() const;
234  const std::vector<HttpUrl>& getHmUrls() const;
235 
236 #ifndef GPUDB_NO_HTTPS
237  boost::asio::ssl::context* getSslContext() const;
238 #endif
239 
240  const std::string& getUsername() const;
241  const std::string& getPassword() const;
242 
245  const std::string& getPrimaryURL() const;
246 
247  bool getUseSnappy() const;
248  size_t getThreadCount() const;
249  avro::ExecutorPtr getExecutor() const;
250  const std::map<std::string, std::string>& getHttpHeaders() const;
252  size_t getTimeout() const;
253 
256 
261  void setHASyncMode( HASynchronicityMode mode );
262 
281  void addHttpHeader( const std::string& header,
282  const std::string& value );
283 
300  void removeHttpHeader( const std::string& header );
301 
302 
304  void updateHostManagerPort();
305 
306  template<typename TRequest, typename TResponse>
307  TResponse& submitRequest(const HttpUrl& url,
308  const TRequest& request,
309  TResponse& response,
310  const bool enableCompression = false) const
311  {
312  std::vector<uint8_t> requestBytes;
313  try // encoding the request
314  {
315  avro::encode(requestBytes, request);
316  }
317  catch ( const std::exception& ex )
318  {
319  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while encoding request: "
320  << ex.what() );
321  throw GPUdbException( message );
322  }
323 
324  // Submit the request
325  RawGpudbResponse gpudbResponse;
326  submitRequestRaw(url, requestBytes, gpudbResponse, enableCompression);
327 
328  try // decoding the request
329  {
330  avro::decode(response, gpudbResponse.data);
331  }
332  catch ( const std::exception& ex )
333  {
334  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while decoding response: '"
335  << ex.what()
336  << "'; please ensure that the client API matches the server API version." );
337  throw GPUdbException( message );
338  }
339  return response;
340  }
341 
342  template<typename TRequest, typename TResponse>
343  TResponse& submitRequest(const std::string& endpoint,
344  const TRequest& request,
345  TResponse& response,
346  const bool enableCompression = false) const
347  {
348  std::vector<uint8_t> requestBytes;
349  try // encoding the request
350  {
351  avro::encode(requestBytes, request);
352  }
353  catch ( const std::exception& ex )
354  {
355  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while encoding request: "
356  << ex.what() );
357  throw GPUdbException( message );
358  }
359 
360  // Submit the request
361  RawGpudbResponse gpudbResponse;
362  submitRequestRaw(endpoint, requestBytes, gpudbResponse, enableCompression);
363 
364  try // decoding the request
365  {
366  avro::decode(response, gpudbResponse.data);
367  }
368  catch ( const std::exception& ex )
369  {
370  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while decoding response: '"
371  << ex.what()
372  << "'; please ensure that the client API matches the server API version." );
373  throw GPUdbException( message );
374  }
375 
376  return response;
377  }
378 
379  template<typename TRequest, typename TResponse>
380  TResponse& submitRequest(const char* endpoint,
381  const TRequest& request,
382  TResponse& response,
383  const bool enableCompression = false) const
384  {
385  submitRequest( (std::string) endpoint, request, response, enableCompression );
386  return response;
387  }
388 
389 
403  template<typename TRequest, typename TResponse>
404  TResponse& submitRequestToHostManager(const std::string& endpoint,
405  const TRequest& request,
406  TResponse& response,
407  const bool enableCompression = false) const
408  {
409  // Handle host manager stuff here
410  std::vector<uint8_t> requestBytes;
411  try // encoding the request
412  {
413  avro::encode(requestBytes, request);
414  }
415  catch ( const std::exception& ex )
416  {
417  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while encoding request: "
418  << ex.what() );
419  throw GPUdbException( message );
420  }
421 
422  // Submit the request
423  RawGpudbResponse gpudbResponse;
424  submitRequestToHostManagerRaw(endpoint, requestBytes, gpudbResponse, enableCompression);
425 
426  try // decoding the request
427  {
428  avro::decode(response, gpudbResponse.data);
429  }
430  catch ( const std::exception& ex )
431  {
432  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while decoding response: '"
433  << ex.what()
434  << "'; please ensure that the client API matches the server API version." );
435  throw GPUdbException( message );
436  }
437  return response;
438  } // end submitRequestToHostManager
439 
440 
454  template<typename TRequest, typename TResponse>
455  TResponse& submitRequestToHostManager(const char* endpoint,
456  const TRequest& request,
457  TResponse& response,
458  const bool enableCompression = false) const
459  {
460  submitRequestToHostManager( (std::string) endpoint, request, response, enableCompression );
461  return response;
462  } // end submitRequestToHostManager
463 
464 
465 #include "gpudb/GPUdbFunctions.hpp"
466 
467  void addKnownType(const std::string& typeId, const avro::DecoderPtr& decoder);
468 
469  template<typename T>
470  void addKnownType(const std::string& typeId)
471  {
472  addKnownType(typeId, avro::createDecoder<T>());
473  }
474 
475  template<typename T>
476  void addKnownType(const std::string& typeId, const std::string& schemaString)
477  {
478  addKnownType(typeId, avro::createDecoder<T>(schemaString));
479  }
480 
481  template<typename T>
482  void addKnownType(const std::string& typeId, const ::avro::ValidSchema& schema)
483  {
484  addKnownType(typeId, avro::createDecoder<T>(schema));
485  }
486 
487  void addKnownTypeFromTable(const std::string& tableName, const avro::DecoderPtr& decoder);
488 
489  template<typename T>
490  void addKnownTypeFromTable(const std::string& tableName)
491  {
492  addKnownTypeFromTable(tableName, avro::createDecoder<T>());
493  }
494 
495  template<typename T>
496  void addKnownTypeFromTable(const std::string& tableName, const std::string& schemaString)
497  {
498  addKnownTypeFromTable(tableName, avro::createDecoder<T>(schemaString));
499  }
500 
501  template<typename T>
502  void addKnownTypeFromTable(const std::string& tableName, const ::avro::ValidSchema& schema)
503  {
504  addKnownTypeFromTable(tableName, avro::createDecoder<T>(schema));
505  }
506 
507 private:
508 
509  static const std::string API_VERSION;
510 
511  static const std::string FAILOVER_TRIGGER_MESSAGES[];
512  static const std::string PROTECTED_HEADERS[];
513  static const std::string HA_SYNCHRONICITY_MODE_VALUES[];
514 
515  static const size_t NUM_TRIGGER_MESSAGES;
516 
517  mutable std::vector<HttpUrl> m_urls;
518  mutable std::vector<HttpUrl> m_hmUrls;
519  std::string m_primaryUrlStr;
520  HttpUrl* m_primaryUrlPtr;
521  mutable std::vector<size_t> m_urlIndices;
522  mutable boost::mutex m_urlMutex;
523  mutable size_t m_currentUrl;
524 
525 #ifndef GPUDB_NO_HTTPS
526  boost::asio::ssl::context* m_sslContext;
527 #endif
528 
529  std::string m_username;
530  std::string m_password;
531  std::string m_authorization;
532  bool m_useSnappy;
533  size_t m_threadCount;
534  avro::ExecutorPtr m_executor;
535  std::map<std::string, std::string> m_httpHeaders;
536  size_t m_timeout;
537  mutable HASynchronicityMode m_haSyncMode;
538  Options m_options;
539 
540  mutable std::map<std::string, avro::DecoderPtr> m_knownTypes;
541  mutable boost::mutex m_knownTypesMutex;
542 
543 
547  void init();
548 
549  // Handle the primary host URL, if any is given via options
550  void handlePrimaryURL();
551 
552  // Update the URLs with the available HA ring information
553  void getHAringHeadNodeAdresses();
554 
556  void updateHostManagerUrls();
557  void setHostManagerPort(uint16_t value);
558 
564  void randomizeURLs() const;
565 
568  const std::string& getHASynchronicityModeValue( HASynchronicityMode syncMode ) const;
569 
572  const HttpUrl* getUrlPointer() const;
573  const HttpUrl* getHmUrlPointer() const;
574  const HttpUrl* switchUrl(const HttpUrl* oldUrl) const;
575  const HttpUrl* switchHmUrl(const HttpUrl* oldUrl) const;
576 
577 
580  void initHttpRequest(HttpRequest& httpRequest) const;
581  void submitRequestRaw(const std::string& endpoint,
582  const std::vector<uint8_t>& request,
583  RawGpudbResponse& response,
584  const bool enableCompression) const;
585  void submitRequestToHostManagerRaw(const std::string& endpoint,
586  const std::vector<uint8_t>& request,
587  RawGpudbResponse& response,
588  const bool enableCompression) const;
589  void submitRequestRaw(const HttpUrl& url,
590  const std::vector<uint8_t>& request,
591  RawGpudbResponse& response,
592  const bool enableCompression,
593  const bool throwOnError = true) const;
594 
596  avro::DecoderPtr getDecoder(const std::string& typeId) const;
597  void setDecoderIfMissing(const std::string& typeId,
598  const std::string& label,
599  const std::string& schemaString,
600  const std::map<std::string, std::vector<std::string> >& properties) const;
601 
602 };
603 
604 #include "gpudb/GPUdbTemplates.hpp"
605 
606 } // end namespace gpudb
607 
608 #endif
const HttpUrl & getHmUrl() 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:502
Options & setHttpHeaders(const std::map< std::string, std::string > &value)
HASynchronicityMode
A enumeration of high-availability synchronicity override modes.
Definition: GPUdb.hpp:128
static const std::string DB_OFFLINE_ERROR_MESSAGE
Definition: GPUdb.hpp:147
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.
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:153
void addKnownType(const std::string &typeId)
Definition: GPUdb.hpp:470
void addKnownTypeFromTable(const std::string &tableName)
Definition: GPUdb.hpp:490
TResponse & submitRequest(const HttpUrl &url, const TRequest &request, TResponse &response, const bool enableCompression=false) const
Definition: GPUdb.hpp:307
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:404
const std::vector< HttpUrl > & getUrls() const
static const std::string HEADER_CONTENT_LENGTH
Definition: GPUdb.hpp:155
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:144
static const std::string HEADER_HA_SYNC_MODE
Definition: GPUdb.hpp:156
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:455
~GPUdb()
Destructor.
Options & setUsername(const std::string &value)
static const std::string DB_EXITING_ERROR_MESSAGE
Definition: GPUdb.hpp:146
const std::map< std::string, std::string > & getHttpHeaders() const
size_t getThreadCount() const
void addKnownTypeFromTable(const std::string &tableName, const avro::DecoderPtr &decoder)
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:343
const std::string & getUsername() const
void addKnownType(const std::string &typeId, const ::avro::ValidSchema &schema)
Definition: GPUdb.hpp:482
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:149
static std::string getApiVersion()
Definition: GPUdb.hpp:159
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:140
boost::asio::ssl::context * getSslContext() const
TResponse & submitRequest(const char *endpoint, const TRequest &request, TResponse &response, const bool enableCompression=false) const
Definition: GPUdb.hpp:380
Options & setTimeout(const size_t value)
avro::ExecutorPtr getExecutor() const
static const std::string HEADER_CONTENT_TYPE
Definition: GPUdb.hpp:154
const HttpUrl & getUrl() const
Some getters
A set of output parameters forendpoint /gpudbresponse}.
Definition: gpudb_wrapper.h:17
Options & setThreadCount(const size_t value)
void addKnownTypeFromTable(const std::string &tableName, const std::string &schemaString)
Definition: GPUdb.hpp:496
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:145
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:148
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:476
bool getUseSnappy() const