Skip to content

Commit

Permalink
Fix incorrect aws sdk use (#106)
Browse files Browse the repository at this point in the history
* add a helper class to init/shutdown aws api properly

* add helper class

* remove logging in AwsSDKHelper

* Use an atomic reference count

Use an atomic reference counter for SDK usage count.
Delay SDK initialization to later.
Move helper class to TSCommunication's cpp file.

* add a test case to ensure multi connection uses AWS SDK API appropriately

Co-authored-by: James Duong <[email protected]>
  • Loading branch information
yanw-bq and jduo authored Jul 8, 2021
1 parent 92d3d5d commit 54e6def
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
4 changes: 0 additions & 4 deletions src/odfesqlodbc/communication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@
Communication::Communication()
: m_status(ConnStatusType::CONNECTION_BAD),
m_client_encoding(m_supported_client_encodings[0]) {
LogMsg(LOG_DEBUG, "Initializing AWS API.");
Aws::InitAPI(m_sdk_options);
}

Communication::~Communication() {
LogMsg(LOG_DEBUG, "Shutting down AWS API.");
Aws::ShutdownAPI(m_sdk_options);
}

bool Communication::Setup(const runtime_options& options) {
Expand Down
6 changes: 0 additions & 6 deletions src/odfesqlodbc/communication.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#ifndef COMMUNICATION
#define COMMUNICATION

#include <aws/core/Aws.h>

#include <map>
#include <memory>
#include <string>
Expand Down Expand Up @@ -135,10 +133,6 @@ class Communication {
* Current client encoding
*/
std::string m_client_encoding;
/**
* AWS sdk options
*/
Aws::SDKOptions m_sdk_options;
/**
* Map storing prefetch queues
*/
Expand Down
43 changes: 43 additions & 0 deletions src/odfesqlodbc/odbc_communication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
#include "okta_credentials_provider.h"
#include "version.h"
#include "mylog.h"
#include <atomic>
#include <memory>
#include <mutex>
#include <aws/core/Aws.h>
#include <aws/core/auth/AWSCredentials.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/core/client/DefaultRetryStrategy.h>
Expand All @@ -34,6 +37,38 @@
// clang-format on

namespace {
/**
* A helper class to initialize/shutdown AWS API once per DLL load/unload.
*/
class AwsSdkHelper {
public:
AwsSdkHelper() :
m_reference_count(0) {
}

AwsSdkHelper& operator++() {
if (1 == ++m_reference_count) {
std::scoped_lock lock(m_mutex);
Aws::InitAPI(m_sdk_options);
}
return *this;
}

AwsSdkHelper& operator--() {
if (0 == --m_reference_count) {
std::scoped_lock lock(m_mutex);
Aws::ShutdownAPI(m_sdk_options);
}
return *this;
}

Aws::SDKOptions m_sdk_options;
std::atomic<int> m_reference_count;
std::mutex m_mutex;
};

AwsSdkHelper AWS_SDK_HELPER;

const Aws::String UA_ID_PREFIX = Aws::String("ts-odbc.");
const std::string DEFAULT_CREATOR_TYPE = "DEFAULT";

Expand Down Expand Up @@ -93,6 +128,14 @@ namespace {
};
}

TSCommunication::TSCommunication() {
++AWS_SDK_HELPER;
}

TSCommunication::~TSCommunication() {
--AWS_SDK_HELPER;
}

bool TSCommunication::Validate(const runtime_options& options) {
if (options.auth.region.empty() && options.auth.end_point_override.empty()) {
throw std::invalid_argument("Both region and end point cannot be empty.");
Expand Down
4 changes: 4 additions & 0 deletions src/odfesqlodbc/odbc_communication.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
*/
class TSCommunication : public Communication {
public:
TSCommunication();

~TSCommunication();

/**
* Validate options
* @param options const runtime_options&
Expand Down

0 comments on commit 54e6def

Please sign in to comment.