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 }
21 
22 #include "gpudb/Avro.hpp"
23 #include "gpudb/GenericRecord.hpp"
24 #include "gpudb/GPUdbException.hpp"
25 #include "gpudb/Http.hpp"
26 #include "gpudb/Type.hpp"
28 
29 
56 namespace gpudb
57 {
58 class GPUdb : private boost::noncopyable
59 {
60 public:
61 
62  class Options
63  {
64  public:
65  Options();
66 
67 #ifndef GPUDB_NO_HTTPS
68  boost::asio::ssl::context* getSslContext() const;
69  bool getBypassSslCertCheck() const;
70 #endif
71 
72  std::string getUsername() const;
73  std::string getPassword() const;
74  std::string getOauthToken() const;
77  std::string getPrimaryUrl() const;
78  bool getUseSnappy() const;
79  size_t getThreadCount() const;
80  avro::ExecutorPtr getExecutor() const;
81 
84  std::map<std::string, std::string>& getHttpHeaders();
85  const std::map<std::string, std::string>& getHttpHeaders() const;
86 
87  size_t getTimeout() const;
88  uint16_t getHostManagerPort() const;
89  bool getDisableFailover() const;
90  bool getDisableAutoDiscovery() const;
91 
92 #ifndef GPUDB_NO_HTTPS
93  Options& setSslContext(boost::asio::ssl::context* value);
94  Options& setBypassSslCertCheck(const bool value);
95 #endif
96 
97  Options& setUsername(const std::string& value);
98  Options& setPassword(const std::string& value);
99  Options& setOauthToken(const std::string& value);
101  Options& setPrimaryUrl(const std::string& value);
102  Options& setUseSnappy(const bool value);
103  Options& setThreadCount(const size_t value);
104  Options& setExecutor(const avro::ExecutorPtr value);
105  Options& setHttpHeaders(const std::map<std::string, std::string>& value);
106  Options& addHttpHeader(const std::string& header, const std::string& value);
107  Options& setTimeout(const size_t value);
108  Options& setHostManagerPort(const uint16_t value);
109  Options& setDisableFailover(const bool value);
110  Options& setDisableAutoDiscovery(const bool value);
111 
112  private:
113 
114 #ifndef GPUDB_NO_HTTPS
115  boost::asio::ssl::context* m_sslContext;
116  bool m_bypassSslCertCheck;
117 #endif
118 
119  std::string m_username;
120  std::string m_password;
121  std::string m_oauthToken;
122  std::string m_primaryUrl;
123  bool m_useSnappy;
124  bool m_disableFailover;
125  bool m_disableAutoDiscovery;
126  size_t m_threadCount;
127  avro::ExecutorPtr m_executor;
128  std::map<std::string, std::string> m_httpHeaders;
129  size_t m_timeout;
130  uint16_t m_hmPort;
131  };
132 
133 
142  DEFAULT = 0, // maps to 'default'
144  NONE, // maps to 'REPL_NONE'
146  SYNCHRONOUS, // maps to 'REPL_SYNC'
149  SYNCHRONOUS_PARALLEL, // maps to 'REPL_SYNC_PARALLEL'
151  ASYNCHRONOUS, // maps to 'REPL_ASYNC'
153  ASYNCHRONOUS_PARALLEL // maps to 'REPL_ASYNC_PARALLEL'
154  };
155 
156 
157 
158  static const int64_t END_OF_SET = -9999;
159 
162  static const std::string DB_CONNECTION_RESET_ERROR_MESSAGE;
163  static const std::string DB_CONNECTION_REFUSED_ERROR_MESSAGE;
164  static const std::string DB_EXITING_ERROR_MESSAGE;
165  static const std::string DB_OFFLINE_ERROR_MESSAGE;
166  static const std::string DB_SYSTEM_LIMITED_ERROR_MESSAGE;
167  static const std::string DB_HM_OFFLINE_ERROR_MESSAGE;
168 
171  static const std::string HEADER_AUTHORIZATION;
172  static const std::string HEADER_CONTENT_TYPE;
173  static const std::string HEADER_CONTENT_LENGTH;
174  static const std::string HEADER_HA_SYNC_MODE;
175 
176 
177  static inline std::string getApiVersion() { return GPUdb::API_VERSION; }
178 
190  GPUdb(const HttpUrl& url, const Options& options = Options());
191 
207  GPUdb(const std::string& url, const Options& options = Options());
208 
220  GPUdb(const std::vector<HttpUrl>& urls, const Options& options = Options());
221 
234  GPUdb(const std::vector<std::string>& urls, const Options& options = Options());
235 
237  ~GPUdb();
238 
239  // Some getters
240  // ------------
241  const HttpUrl& getUrl() const;
242  const std::vector<HttpUrl>& getUrls() const;
243  const HttpUrl& getHmUrl() const;
244  const std::vector<HttpUrl>& getHmUrls() const;
245 
246 #ifndef GPUDB_NO_HTTPS
247  boost::asio::ssl::context* getSslContext() const;
248  bool getBypassSslCertCheck() const;
249 #endif
250 
251  const std::string& getUsername() const;
252  const std::string& getPassword() const;
253  const std::string& getOauthToken() 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 
274  long execute(const std::string& sql);
275 
285  long execute(const std::string& sql, const std::string& parameters);
286 
297  long execute(const std::string& sql, const std::string& parameters,
298  const std::map<std::string, std::string>& options);
299 
300  template<typename T> long execute(const std::string& sql, const std::vector<T>& parameters);
301  template<typename T> long execute(const std::string& sql, const std::vector<T>& parameters,
302  const std::map<std::string, std::string>& options);
303 
304  // Some setters
305  // ------------
306 
311  void setHASyncMode( HASynchronicityMode mode );
312 
331  void addHttpHeader( const std::string& header,
332  const std::string& value );
333 
350  void removeHttpHeader( const std::string& header );
351 
352 
354  void updateHostManagerPort();
355 
356  template<typename TRequest, typename TResponse>
357  TResponse& submitRequest(const HttpUrl& url,
358  const TRequest& request,
359  TResponse& response,
360  const bool enableCompression = false) const
361  {
362  std::vector<uint8_t> requestBytes;
363  try // encoding the request
364  {
365  avro::encode(requestBytes, request);
366  }
367  catch ( const std::exception& ex )
368  {
369  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while encoding request: "
370  << ex.what() );
371  throw GPUdbException( message );
372  }
373 
374  // Submit the request
375  RawGpudbResponse gpudbResponse;
376  submitRequestRaw(url, requestBytes, gpudbResponse, enableCompression);
377 
378  try // decoding the request
379  {
380  avro::decode(response, gpudbResponse.data);
381  }
382  catch ( const std::exception& ex )
383  {
384  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while decoding response: '"
385  << ex.what()
386  << "'; please ensure that the client API matches the server API version." );
387  throw GPUdbException( message );
388  }
389  return response;
390  }
391 
392  template<typename TRequest, typename TResponse>
393  TResponse& submitRequest(const std::string& endpoint,
394  const TRequest& request,
395  TResponse& response,
396  const bool enableCompression = false) const
397  {
398  std::vector<uint8_t> requestBytes;
399  try // encoding the request
400  {
401  avro::encode(requestBytes, request);
402  }
403  catch ( const std::exception& ex )
404  {
405  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while encoding request: "
406  << ex.what() );
407  throw GPUdbException( message );
408  }
409 
410  // Submit the request
411  RawGpudbResponse gpudbResponse;
412  submitRequestRaw(endpoint, requestBytes, gpudbResponse, enableCompression);
413 
414  try // decoding the request
415  {
416  avro::decode(response, gpudbResponse.data);
417  }
418  catch ( const std::exception& ex )
419  {
420  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while decoding response: '"
421  << ex.what()
422  << "'; please ensure that the client API matches the server API version." );
423  throw GPUdbException( message );
424  }
425 
426  return response;
427  }
428 
429  template<typename TRequest, typename TResponse>
430  TResponse& submitRequest(const char* endpoint,
431  const TRequest& request,
432  TResponse& response,
433  const bool enableCompression = false) const
434  {
435  submitRequest( (std::string) endpoint, request, response, enableCompression );
436  return response;
437  }
438 
439 
453  template<typename TRequest, typename TResponse>
454  TResponse& submitRequestToHostManager(const std::string& endpoint,
455  const TRequest& request,
456  TResponse& response,
457  const bool enableCompression = false) const
458  {
459  // Handle host manager stuff here
460  std::vector<uint8_t> requestBytes;
461  try // encoding the request
462  {
463  avro::encode(requestBytes, request);
464  }
465  catch ( const std::exception& ex )
466  {
467  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while encoding request: "
468  << ex.what() );
469  throw GPUdbException( message );
470  }
471 
472  // Submit the request
473  RawGpudbResponse gpudbResponse;
474  submitRequestToHostManagerRaw(endpoint, requestBytes, gpudbResponse, enableCompression);
475 
476  try // decoding the request
477  {
478  avro::decode(response, gpudbResponse.data);
479  }
480  catch ( const std::exception& ex )
481  {
482  std::string message = GPUDB_STREAM_TO_STRING( "Error encountered while decoding response: '"
483  << ex.what()
484  << "'; please ensure that the client API matches the server API version." );
485  throw GPUdbException( message );
486  }
487  return response;
488  } // end submitRequestToHostManager
489 
490 
504  template<typename TRequest, typename TResponse>
505  TResponse& submitRequestToHostManager(const char* endpoint,
506  const TRequest& request,
507  TResponse& response,
508  const bool enableCompression = false) const
509  {
510  submitRequestToHostManager( (std::string) endpoint, request, response, enableCompression );
511  return response;
512  } // end submitRequestToHostManager
513 
514 
515 #include "gpudb/GPUdbFunctions.hpp"
516 
517  void addKnownType(const std::string& typeId, const avro::DecoderPtr& decoder);
518 
519  template<typename T>
520  void addKnownType(const std::string& typeId)
521  {
522  addKnownType(typeId, avro::createDecoder<T>());
523  }
524 
525  template<typename T>
526  void addKnownType(const std::string& typeId, const std::string& schemaString)
527  {
528  addKnownType(typeId, avro::createDecoder<T>(schemaString));
529  }
530 
531  template<typename T>
532  void addKnownType(const std::string& typeId, const ::avro::ValidSchema& schema)
533  {
534  addKnownType(typeId, avro::createDecoder<T>(schema));
535  }
536 
537  void addKnownTypeFromTable(const std::string& tableName, const avro::DecoderPtr& decoder);
538 
539  template<typename T>
540  void addKnownTypeFromTable(const std::string& tableName)
541  {
542  addKnownTypeFromTable(tableName, avro::createDecoder<T>());
543  }
544 
545  template<typename T>
546  void addKnownTypeFromTable(const std::string& tableName, const std::string& schemaString)
547  {
548  addKnownTypeFromTable(tableName, avro::createDecoder<T>(schemaString));
549  }
550 
551  template<typename T>
552  void addKnownTypeFromTable(const std::string& tableName, const ::avro::ValidSchema& schema)
553  {
554  addKnownTypeFromTable(tableName, avro::createDecoder<T>(schema));
555  }
556 
557 private:
558 
559  static const std::string API_VERSION;
560 
561  static const std::string FAILOVER_TRIGGER_MESSAGES[];
562  static const std::string PROTECTED_HEADERS[];
563  static const std::string HA_SYNCHRONICITY_MODE_VALUES[];
564 
565  static const size_t NUM_TRIGGER_MESSAGES;
566 
567  mutable std::vector<HttpUrl> m_urls;
568  mutable std::vector<HttpUrl> m_hmUrls;
569  std::string m_primaryUrlStr;
570  HttpUrl* m_primaryUrlPtr;
571  mutable std::vector<size_t> m_urlIndices;
572  mutable boost::mutex m_urlMutex;
573  mutable size_t m_currentUrl;
574 
575 #ifndef GPUDB_NO_HTTPS
576  boost::asio::ssl::context* m_sslContext;
577 #endif
578 
579  std::string m_username;
580  std::string m_password;
581  std::string m_oauthToken;
582  std::string m_authorization;
583  bool m_useSnappy;
584  bool m_disableFailover;
585  bool m_bypassSslCertCheck;
586  bool m_disableAutoDiscovery;
587  size_t m_threadCount;
588  avro::ExecutorPtr m_executor;
589  std::map<std::string, std::string> m_httpHeaders;
590  size_t m_timeout;
591  mutable HASynchronicityMode m_haSyncMode;
592  Options m_options;
593 
594  mutable std::map<std::string, avro::DecoderPtr> m_knownTypes;
595  mutable boost::mutex m_knownTypesMutex;
596 
597 
598  // Helper functions
599  // ----------------
600 
601  // Initialize the GPUdb object
602  void init();
603 
604  void removeProtectedHttpHeaders();
605 
606  // Handle the primary host URL, if any is given via options
607  void handlePrimaryURL();
608 
609  const std::string createAuthorizationHeader() const;
610 
611  void getAuthorizationFromHttpHeaders();
612 
613  gpudb::GPUdb::HASynchronicityMode createHASyncModeHeader() const;
614  static HASynchronicityMode findHASyncModeByValue(const std::vector<std::string>& values, const std::string value);
615 
616  // Update the URLs with the available HA ring information
617  void getHAringHeadNodeAddresses();
618 
620  void updateHostManagerUrls();
621  void setHostManagerPort(uint16_t value);
622 
628  void randomizeURLs() const;
629 
632  const std::string& getHASynchronicityModeValue( HASynchronicityMode syncMode ) const;
633 
634  // Some getters
635  // ------------
636  const HttpUrl* getUrlPointer() const;
637  const HttpUrl* getHmUrlPointer() const;
638  const HttpUrl* switchUrl(const HttpUrl* oldUrl) const;
639  const HttpUrl* switchHmUrl(const HttpUrl* oldUrl) const;
640 
641 
642  // Request related methods
643  // -----------------------
644  void initHttpRequest(HttpRequest& httpRequest) const;
645  void submitRequestRaw(const std::string& endpoint,
646  const std::vector<uint8_t>& request,
647  RawGpudbResponse& response,
648  const bool enableCompression) const;
649  void submitRequestToHostManagerRaw(const std::string& endpoint,
650  const std::vector<uint8_t>& request,
651  RawGpudbResponse& response,
652  const bool enableCompression) const;
653  void submitRequestRaw(const HttpUrl& url,
654  const std::vector<uint8_t>& request,
655  RawGpudbResponse& response,
656  const bool enableCompression,
657  const bool throwOnError = true) const;
658 
660  avro::DecoderPtr getDecoder(const std::string& typeId) const;
661  void setDecoderIfMissing(const std::string& typeId,
662  const std::string& label,
663  const std::string& schemaString,
664  const std::map<std::string, std::vector<std::string> >& properties) const;
665 
666 };
667 
668 #include "gpudb/GPUdbTemplates.hpp"
669 
670 } // end namespace gpudb
671 
672 #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:151
void addKnownTypeFromTable(const std::string &tableName, const ::avro::ValidSchema &schema)
Definition: GPUdb.hpp:552
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:357
HASynchronicityMode
A enumeration of high-availability synchronicity override modes.
Definition: GPUdb.hpp:139
static const std::string DB_OFFLINE_ERROR_MESSAGE
Definition: GPUdb.hpp:165
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:171
No override; defer to the HA process for synchronizing endpoints (which has different logic for diffe...
Definition: GPUdb.hpp:142
void addKnownType(const std::string &typeId)
Definition: GPUdb.hpp:520
bool getBypassSslCertCheck() const
void addKnownTypeFromTable(const std::string &tableName)
Definition: GPUdb.hpp:540
size_t getThreadCount() const
TResponse & submitRequest(const std::string &endpoint, const TRequest &request, TResponse &response, const bool enableCompression=false) const
Definition: GPUdb.hpp:393
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:173
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:149
const std::map< std::string, std::string > & getHttpHeaders() const
size_t getThreadCount() const
Explicitly do NOT replicate across the HA cluster.
Definition: GPUdb.hpp:144
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:162
static const std::string HEADER_HA_SYNC_MODE
Definition: GPUdb.hpp:174
size_t getTimeout() const
Queues a request to RMQ for each cluster, executes the query locally, and returns to the user.
Definition: GPUdb.hpp:153
~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:164
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:532
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:167
static std::string getApiVersion()
Definition: GPUdb.hpp:177
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:505
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:158
Options & setTimeout(const size_t value)
const std::string & getOauthToken() const
static const std::string HEADER_CONTENT_TYPE
Definition: GPUdb.hpp:172
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:546
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:163
Synchronize all endpoint calls.
Definition: GPUdb.hpp:146
Options & addHttpHeader(const std::string &header, const std::string &value)
static const std::string DB_SYSTEM_LIMITED_ERROR_MESSAGE
Definition: GPUdb.hpp:166
Options & setUseSnappy(const bool value)
TResponse & submitRequest(const char *endpoint, const TRequest &request, TResponse &response, const bool enableCompression=false) const
Definition: GPUdb.hpp:430
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:454
void addKnownType(const std::string &typeId, const std::string &schemaString)
Definition: GPUdb.hpp:526
const std::string & getUsername() const
avro::ExecutorPtr getExecutor() const