GPUdb C++ API  Version 7.2.2.4
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 // The practice of declaring the Bind placeholders (_1, _2, ...) in the global namespace is deprecated. Please use <boost/bind/bind.hpp> + using namespace boost::placeholders, or define BOOST_BIND_GLOBAL_PLACEHOLDERS to retain the current behavior.
9 #define BOOST_BIND_GLOBAL_PLACEHOLDERS
10 #include <boost/asio/ssl.hpp>
11 #endif
12 
13 #include <stdint.h>
14 
15 #include <boost/thread/mutex.hpp>
16 
17 namespace gpudb
18 {
19  class GPUdb;
20  class FailbackPollerService;
21 }
22 
23 #include "gpudb/Avro.hpp"
24 #include "gpudb/GenericRecord.hpp"
25 #include "gpudb/GPUdbException.hpp"
26 #include "gpudb/Http.hpp"
27 #include "gpudb/Type.hpp"
29 
30 
57 namespace gpudb
58 {
59 class GPUdb : private boost::noncopyable
60 {
61 public:
62 
63  class Options
64  {
65  public:
66  Options();
67 
68 #ifndef GPUDB_NO_HTTPS
69  boost::asio::ssl::context* getSslContext() const;
70  bool getBypassSslCertCheck() const;
71 #endif
72 
73  std::string getUsername() const;
74  std::string getPassword() const;
75  std::string getOauthToken() const;
78  std::string getPrimaryUrl() const;
79  bool getUseSnappy() const;
80  size_t getThreadCount() const;
81  avro::ExecutorPtr getExecutor() const;
82 
85  std::map<std::string, std::string>& getHttpHeaders();
86  const std::map<std::string, std::string>& getHttpHeaders() const;
87 
88  size_t getTimeout() const;
89  uint16_t getHostManagerPort() const;
90  bool getDisableFailover() const;
91  bool getDisableAutoDiscovery() const;
92 
93 #ifndef GPUDB_NO_HTTPS
94  Options& setSslContext(boost::asio::ssl::context* value);
95  Options& setBypassSslCertCheck(const bool value);
96 #endif
97 
98  Options& setUsername(const std::string& value);
99  Options& setPassword(const std::string& value);
100  Options& setOauthToken(const std::string& value);
102  Options& setPrimaryUrl(const std::string& value);
103  Options& setUseSnappy(const bool value);
104  Options& setThreadCount(const size_t value);
105  Options& setExecutor(const avro::ExecutorPtr value);
106  Options& setHttpHeaders(const std::map<std::string, std::string>& value);
107  Options& addHttpHeader(const std::string& header, const std::string& value);
108  Options& setTimeout(const size_t value);
109  Options& setHostManagerPort(const uint16_t value);
110  Options& setDisableFailover(const bool value);
111  Options& setDisableAutoDiscovery(const bool value);
112 
113  private:
114 
115 #ifndef GPUDB_NO_HTTPS
116  boost::asio::ssl::context* m_sslContext;
117  bool m_bypassSslCertCheck;
118 #endif
119 
120  std::string m_username;
121  std::string m_password;
122  std::string m_oauthToken;
123  std::string m_primaryUrl;
124  bool m_useSnappy;
125  bool m_disableFailover;
126  bool m_disableAutoDiscovery;
127  size_t m_threadCount;
128  avro::ExecutorPtr m_executor;
129  std::map<std::string, std::string> m_httpHeaders;
130  size_t m_timeout;
131  uint16_t m_hmPort;
132  };
133 
134 
143  DEFAULT = 0, // maps to 'default'
145  NONE, // maps to 'REPL_NONE'
147  SYNCHRONOUS, // maps to 'REPL_SYNC'
150  SYNCHRONOUS_PARALLEL, // maps to 'REPL_SYNC_PARALLEL'
152  ASYNCHRONOUS, // maps to 'REPL_ASYNC'
154  ASYNCHRONOUS_PARALLEL // maps to 'REPL_ASYNC_PARALLEL'
155  };
156 
157 
158 
159  static const int64_t END_OF_SET = -9999;
160 
163  static const std::string DB_CONNECTION_RESET_ERROR_MESSAGE;
164  static const std::string DB_CONNECTION_REFUSED_ERROR_MESSAGE;
165  static const std::string DB_EXITING_ERROR_MESSAGE;
166  static const std::string DB_OFFLINE_ERROR_MESSAGE;
167  static const std::string DB_SYSTEM_LIMITED_ERROR_MESSAGE;
168  static const std::string DB_HM_OFFLINE_ERROR_MESSAGE;
169 
172  static const std::string HEADER_AUTHORIZATION;
173  static const std::string HEADER_CONTENT_TYPE;
174  static const std::string HEADER_CONTENT_LENGTH;
175  static const std::string HEADER_HA_SYNC_MODE;
176 
177 
178  static inline std::string getApiVersion() { return GPUdb::API_VERSION; }
179 
191  GPUdb(const HttpUrl& url, const Options& options = Options());
192 
208  GPUdb(const std::string& url, const Options& options = Options());
209 
221  GPUdb(const std::vector<HttpUrl>& urls, const Options& options = Options());
222 
235  GPUdb(const std::vector<std::string>& urls, const Options& options = Options());
236 
238  ~GPUdb();
239 
240  // Some getters
241  // ------------
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  const std::string& getOauthToken() const;
255 
258  const std::string& getPrimaryURL() const;
259 
260  bool getUseSnappy() const;
261  size_t getThreadCount() const;
262  avro::ExecutorPtr getExecutor() const;
263  const std::map<std::string, std::string>& getHttpHeaders() const;
265  size_t getTimeout() const;
266 
275  long execute(const std::string& sql);
276 
286  long execute(const std::string& sql, const std::string& parameters);
287 
298  long execute(const std::string& sql, const std::string& parameters,
299  const std::map<std::string, std::string>& options);
300 
301  template<typename T> long execute(const std::string& sql, const std::vector<T>& parameters);
302  template<typename T> long execute(const std::string& sql, const std::vector<T>& parameters,
303  const std::map<std::string, std::string>& options);
304 
305  // Some setters
306  // ------------
307 
312  void setHASyncMode( HASynchronicityMode mode );
313 
332  void addHttpHeader( const std::string& header,
333  const std::string& value );
334 
351  void removeHttpHeader( const std::string& header );
352 
353 
355  void updateHostManagerPort();
356 
357  template<typename TRequest, typename TResponse>
358  TResponse& submitRequest(const HttpUrl& url,
359  const TRequest& request,
360  TResponse& response,
361  const bool enableCompression = false) const
362  {
363  std::vector<uint8_t> requestBytes;
364  try // encoding the request
365  {
366  avro::encode(requestBytes, request);
367  }
368  catch ( const std::exception& ex )
369  {
370  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while encoding request: "
371  << ex.what() );
372  throw GPUdbException( message );
373  }
374 
375  // Submit the request
376  RawGpudbResponse gpudbResponse;
377  submitRequestRaw(url, requestBytes, gpudbResponse, enableCompression);
378 
379  try // decoding the request
380  {
381  avro::decode(response, gpudbResponse.data);
382  }
383  catch ( const std::exception& ex )
384  {
385  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while decoding response: '"
386  << ex.what()
387  << "'; please ensure that the client API matches the server API version." );
388  throw GPUdbException( message );
389  }
390  return response;
391  }
392 
393  template<typename TRequest, typename TResponse>
394  TResponse& submitRequest(const std::string& endpoint,
395  const TRequest& request,
396  TResponse& response,
397  const bool enableCompression = false) const
398  {
399  std::vector<uint8_t> requestBytes;
400  try // encoding the request
401  {
402  avro::encode(requestBytes, request);
403  }
404  catch ( const std::exception& ex )
405  {
406  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while encoding request: "
407  << ex.what() );
408  throw GPUdbException( message );
409  }
410 
411  // Submit the request
412  RawGpudbResponse gpudbResponse;
413  submitRequestRaw(endpoint, requestBytes, gpudbResponse, enableCompression);
414 
415  try // decoding the request
416  {
417  avro::decode(response, gpudbResponse.data);
418  }
419  catch ( const std::exception& ex )
420  {
421  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while decoding response: '"
422  << ex.what()
423  << "'; please ensure that the client API matches the server API version." );
424  throw GPUdbException( message );
425  }
426 
427  return response;
428  }
429 
430  template<typename TRequest, typename TResponse>
431  TResponse& submitRequest(const char* endpoint,
432  const TRequest& request,
433  TResponse& response,
434  const bool enableCompression = false) const
435  {
436  submitRequest( (std::string) endpoint, request, response, enableCompression );
437  return response;
438  }
439 
440 
454  template<typename TRequest, typename TResponse>
455  TResponse& submitRequestToHostManager(const std::string& endpoint,
456  const TRequest& request,
457  TResponse& response,
458  const bool enableCompression = false) const
459  {
460  // Handle host manager stuff here
461  std::vector<uint8_t> requestBytes;
462  try // encoding the request
463  {
464  avro::encode(requestBytes, request);
465  }
466  catch ( const std::exception& ex )
467  {
468  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while encoding request: "
469  << ex.what() );
470  throw GPUdbException( message );
471  }
472 
473  // Submit the request
474  RawGpudbResponse gpudbResponse;
475  submitRequestToHostManagerRaw(endpoint, requestBytes, gpudbResponse, enableCompression);
476 
477  try // decoding the request
478  {
479  avro::decode(response, gpudbResponse.data);
480  }
481  catch ( const std::exception& ex )
482  {
483  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while decoding response: '"
484  << ex.what()
485  << "'; please ensure that the client API matches the server API version." );
486  throw GPUdbException( message );
487  }
488  return response;
489  } // end submitRequestToHostManager
490 
491 
505  template<typename TRequest, typename TResponse>
506  TResponse& submitRequestToHostManager(const char* endpoint,
507  const TRequest& request,
508  TResponse& response,
509  const bool enableCompression = false) const
510  {
511  submitRequestToHostManager( (std::string) endpoint, request, response, enableCompression );
512  return response;
513  } // end submitRequestToHostManager
514 
515 
516 #include "gpudb/GPUdbFunctions.hpp"
517 
518  void addKnownType(const std::string& typeId, const avro::DecoderPtr& decoder);
519 
520  template<typename T>
521  void addKnownType(const std::string& typeId)
522  {
523  addKnownType(typeId, avro::createDecoder<T>());
524  }
525 
526  template<typename T>
527  void addKnownType(const std::string& typeId, const std::string& schemaString)
528  {
529  addKnownType(typeId, avro::createDecoder<T>(schemaString));
530  }
531 
532  template<typename T>
533  void addKnownType(const std::string& typeId, const ::avro::ValidSchema& schema)
534  {
535  addKnownType(typeId, avro::createDecoder<T>(schema));
536  }
537 
538  void addKnownTypeFromTable(const std::string& tableName, const avro::DecoderPtr& decoder);
539 
540  template<typename T>
541  void addKnownTypeFromTable(const std::string& tableName)
542  {
543  addKnownTypeFromTable(tableName, avro::createDecoder<T>());
544  }
545 
546  template<typename T>
547  void addKnownTypeFromTable(const std::string& tableName, const std::string& schemaString)
548  {
549  addKnownTypeFromTable(tableName, avro::createDecoder<T>(schemaString));
550  }
551 
552  template<typename T>
553  void addKnownTypeFromTable(const std::string& tableName, const ::avro::ValidSchema& schema)
554  {
555  addKnownTypeFromTable(tableName, avro::createDecoder<T>(schema));
556  }
557 
558 private:
559 
560  static const std::string API_VERSION;
561 
562  static const std::string FAILOVER_TRIGGER_MESSAGES[];
563  static const std::string PROTECTED_HEADERS[];
564  static const std::string HA_SYNCHRONICITY_MODE_VALUES[];
565 
566  static const size_t NUM_TRIGGER_MESSAGES;
567 
568  mutable std::vector<HttpUrl> m_urls;
569  mutable std::vector<HttpUrl> m_hmUrls;
570  std::string m_primaryUrlStr;
571  HttpUrl* m_primaryUrlPtr;
572  mutable std::vector<size_t> m_urlIndices;
573  mutable boost::mutex m_urlMutex;
574  mutable size_t m_currentUrl;
575 
576 #ifndef GPUDB_NO_HTTPS
577  boost::asio::ssl::context* m_sslContext;
578 #endif
579 
580  std::string m_username;
581  std::string m_password;
582  std::string m_oauthToken;
583  std::string m_authorization;
584  bool m_useSnappy;
585  bool m_disableFailover;
586  bool m_bypassSslCertCheck;
587  bool m_disableAutoDiscovery;
588  size_t m_threadCount;
589  avro::ExecutorPtr m_executor;
590  std::map<std::string, std::string> m_httpHeaders;
591  size_t m_timeout;
592  mutable HASynchronicityMode m_haSyncMode;
593  Options m_options;
594 
595  mutable std::map<std::string, avro::DecoderPtr> m_knownTypes;
596  mutable boost::mutex m_knownTypesMutex;
597 
598  friend class FailbackPollerService;
599  mutable gpudb::FailbackPollerService* pollerService;
600 
604  void init();
605 
606  void removeProtectedHttpHeaders();
607 
608  // Handle the primary host URL, if any is given via options
609  void handlePrimaryURL();
610 
611  const std::string createAuthorizationHeader() const;
612 
613  void getAuthorizationFromHttpHeaders();
614 
615  gpudb::GPUdb::HASynchronicityMode createHASyncModeHeader() const;
616  static HASynchronicityMode findHASyncModeByValue(const std::vector<std::string>& values, const std::string value);
617 
618  // Update the URLs with the available HA ring information
619  void getHAringHeadNodeAddresses();
620  bool isKineticaRunning(const std::string& primaryURL) const;
621 
623  void updateHostManagerUrls();
624  void setHostManagerPort(uint16_t value);
625 
631  void randomizeURLs() const;
632 
635  const std::string& getHASynchronicityModeValue( HASynchronicityMode syncMode ) const;
636 
637  // Some getters
638  // ------------
639  const HttpUrl* getUrlPointer() const;
640  const HttpUrl* getHmUrlPointer() const;
641  const HttpUrl* switchUrl(const HttpUrl* oldUrl) const;
642  const HttpUrl* switchHmUrl(const HttpUrl* oldUrl) const;
643  bool checkFailbackConditions() const;
644 
645 
646  // Request related methods
647  // -----------------------
648  void initHttpRequest(HttpRequest& httpRequest) const;
649  void submitRequestRaw(const std::string& endpoint,
650  const std::vector<uint8_t>& request,
651  RawGpudbResponse& response,
652  const bool enableCompression) const;
653  void submitRequestToHostManagerRaw(const std::string& endpoint,
654  const std::vector<uint8_t>& request,
655  RawGpudbResponse& response,
656  const bool enableCompression) const;
657  void submitRequestRaw(const HttpUrl& url,
658  const std::vector<uint8_t>& request,
659  RawGpudbResponse& response,
660  const bool enableCompression,
661  const bool throwOnError = true) const;
662 
664  avro::DecoderPtr getDecoder(const std::string& typeId) const;
665  void setDecoderIfMissing(const std::string& typeId,
666  const std::string& label,
667  const std::string& schemaString,
668  const std::map<std::string, std::vector<std::string> >& properties) const;
669 
670 };
671 
672 #include "gpudb/GPUdbTemplates.hpp"
673 
674 } // end namespace gpudb
675 
676 #endif
Options & setSslContext(boost::asio::ssl::context *value)
void addKnownType(const std::string &typeId, const avro::DecoderPtr &decoder)
std::string getPassword() const
Do NOT synchronize any endpoint call.
Definition: GPUdb.hpp:152
void addKnownTypeFromTable(const std::string &tableName, const ::avro::ValidSchema &schema)
Definition: GPUdb.hpp:553
Options & setHttpHeaders(const std::map< std::string, std::string > &value)
boost::asio::ssl::context * getSslContext() const
std::string getUsername() const
TResponse & submitRequest(const HttpUrl &url, const TRequest &request, TResponse &response, const bool enableCompression=false) const
Definition: GPUdb.hpp:358
HASynchronicityMode
A enumeration of high-availability synchronicity override modes.
Definition: GPUdb.hpp:140
static const std::string DB_OFFLINE_ERROR_MESSAGE
Definition: GPUdb.hpp:166
GPUdb(const HttpUrl &url, const Options &options=Options())
Pass a single HttpURL and options to instantiate a GPUdb object.
const std::vector< HttpUrl > & getUrls() const
void updateHostManagerPort()
Update the host manager port by inquiring the server.
#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:172
No override; defer to the HA process for synchronizing endpoints (which has different logic for diffe...
Definition: GPUdb.hpp:143
void addKnownType(const std::string &typeId)
Definition: GPUdb.hpp:521
bool getBypassSslCertCheck() const
void addKnownTypeFromTable(const std::string &tableName)
Definition: GPUdb.hpp:541
size_t getThreadCount() const
TResponse & submitRequest(const std::string &endpoint, const TRequest &request, TResponse &response, const bool enableCompression=false) const
Definition: GPUdb.hpp:394
avro::ExecutorPtr getExecutor() const
long execute(const std::string &sql)
This method is used to execute a SQL statement (e.g., DML, DDL).
const std::vector< HttpUrl > & getHmUrls() const
const std::string & getPrimaryURL() const
Return a string containing the URL for the primary cluster; empty string otherwise.
bool getUseSnappy() const
static const std::string HEADER_CONTENT_LENGTH
Definition: GPUdb.hpp:174
bool getDisableAutoDiscovery() const
Options & setHostManagerPort(const uint16_t value)
Sends a http request directly to each cluster, executes the query locally, and waits for the response...
Definition: GPUdb.hpp:150
const std::map< std::string, std::string > & getHttpHeaders() const
size_t getThreadCount() const
Explicitly do NOT replicate across the HA cluster.
Definition: GPUdb.hpp:145
bool getUseSnappy() 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:163
static const std::string HEADER_HA_SYNC_MODE
Definition: GPUdb.hpp:175
size_t getTimeout() const
Queues a request to RMQ for each cluster, executes the query locally, and returns to the user.
Definition: GPUdb.hpp:154
~GPUdb()
Destructor.
std::string getPrimaryUrl() const
Return the URL of the primary cluster, if any (empty string delineates that none was set)
Options & setUsername(const std::string &value)
HASynchronicityMode getHASyncMode() const
static const std::string DB_EXITING_ERROR_MESSAGE
Definition: GPUdb.hpp:165
boost::asio::ssl::context * getSslContext() const
uint16_t getHostManagerPort() const
Options & setDisableFailover(const bool value)
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.
Options & setOauthToken(const std::string &value)
void addKnownType(const std::string &typeId, const ::avro::ValidSchema &schema)
Definition: GPUdb.hpp:533
Options & setPrimaryUrl(const std::string &value)
Set the URL for the primary cluster.
const HttpUrl & getHmUrl() const
static const std::string DB_HM_OFFLINE_ERROR_MESSAGE
Definition: GPUdb.hpp:168
static std::string getApiVersion()
Definition: GPUdb.hpp:178
Options & setBypassSslCertCheck(const bool value)
Options & setExecutor(const avro::ExecutorPtr value)
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:506
void setHASyncMode(HASynchronicityMode mode)
Sets the high-availability synchronization mode which will override the default mode.
std::map< std::string, std::string > & getHttpHeaders()
Get the HTTP headers (will include the high-availability synchronicity override header)
const std::string & getPassword() const
static const int64_t END_OF_SET
Definition: GPUdb.hpp:159
Options & setTimeout(const size_t value)
const std::string & getOauthToken() const
static const std::string HEADER_CONTENT_TYPE
Definition: GPUdb.hpp:173
size_t getTimeout() const
The wrapper used by the GPUdb server for all endpoint responses.
Definition: gpudb_wrapper.h:14
Options & setThreadCount(const size_t value)
Options & setDisableAutoDiscovery(const bool value)
void addKnownTypeFromTable(const std::string &tableName, const std::string &schemaString)
Definition: GPUdb.hpp:547
bool getBypassSslCertCheck() const
std::vector< uint8_t > data
Definition: gpudb_wrapper.h:31
const HttpUrl & getUrl() const
std::string getOauthToken() const
static const std::string DB_CONNECTION_REFUSED_ERROR_MESSAGE
Definition: GPUdb.hpp:164
Synchronize all endpoint calls.
Definition: GPUdb.hpp:147
Options & addHttpHeader(const std::string &header, const std::string &value)
static const std::string DB_SYSTEM_LIMITED_ERROR_MESSAGE
Definition: GPUdb.hpp:167
Options & setUseSnappy(const bool value)
TResponse & submitRequest(const char *endpoint, const TRequest &request, TResponse &response, const bool enableCompression=false) const
Definition: GPUdb.hpp:431
bool getDisableFailover() 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)
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:455
void addKnownType(const std::string &typeId, const std::string &schemaString)
Definition: GPUdb.hpp:527
const std::string & getUsername() const
avro::ExecutorPtr getExecutor() const