Skip to content

Commit

Permalink
Add is_serverless member variable to avoid repeatedly determining it.
Browse files Browse the repository at this point in the history
Signed-off-by: currantw <[email protected]>
  • Loading branch information
currantw committed Nov 15, 2024
1 parent 929088e commit aa76d77
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
41 changes: 20 additions & 21 deletions src/sqlodbc/opensearch_communication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ OpenSearchCommunication::IssueRequest(
CREDENTIALS_PROFILE.c_str());

const std::string& service_name =
isServerless()
is_serverless
? SERVICE_NAME_SERVERLESS
: SERVICE_NAME_DEFAULT;

Expand Down Expand Up @@ -491,7 +491,7 @@ bool OpenSearchCommunication::IsSQLPluginEnabled(std::shared_ptr< ErrorDetails >

/**
* @brief Queries server to determine SQL plugin availability.
*
*
* @return true : Successfully queried server for SQL plugin
* @return false : Failed to query server, no plugin available, exception was caught
*/
Expand Down Expand Up @@ -572,10 +572,8 @@ bool OpenSearchCommunication::EstablishConnection() {
InitializeConnection();
}

// Check if the endpoint can be determined.
if (sql_endpoint.empty()) {
SetSqlEndpoint();
}
SetIsServerless();
SetSqlEndpoint();

if (sql_endpoint == SQL_ENDPOINT_ERROR) {
LogMsg(OPENSEARCH_ERROR, m_error_message.c_str());
Expand Down Expand Up @@ -952,7 +950,7 @@ std::string OpenSearchCommunication::GetServerVersion() {
/**
* @brief Queries supplied URL to validate Server Distribution. Maintains
* backwards compatibility with opendistro distribution.
*
*
* @return std::string : Server distribution name, returns "" on error
*/
std::string OpenSearchCommunication::GetServerDistribution() {
Expand Down Expand Up @@ -1069,14 +1067,14 @@ std::string OpenSearchCommunication::GetClusterName() {
}

/**
* @brief Sets URL endpoint for SQL plugin. On failure to
* determine appropriate endpoint, value is set to SQL_ENDPOINT_ERROR_STR
*
* @brief Sets URL endpoint for the SQL plugin.
* Sets it to SQL_ENDPOINT_ERROR if an appropriate
* endpoint could not be determined.
*/
void OpenSearchCommunication::SetSqlEndpoint() {

// Serverless Elasticsearch is not supported.
if (isServerless()) {
if (is_serverless) {
sql_endpoint = SQL_ENDPOINT_OPENSEARCH;
return;
}
Expand All @@ -1092,17 +1090,18 @@ void OpenSearchCommunication::SetSqlEndpoint() {
}

/**
* Returns whether this is connecting to an OpenSearch Serverless cluster.
* @brief Sets flag indicating whether this is
* connecting to an OpenSearch Serverless cluster.
*/
bool OpenSearchCommunication::isServerless() {

// Specified in DSN configuration.
const std::string& is_serverless = m_rt_opts.conn.is_serverless;
void OpenSearchCommunication::SetIsServerless() {

if(!is_serverless.empty()) {
return std::stoi(is_serverless);
}
// If it is not specified in the DSN configuration,
// determine whether this is connected to a serverless
// cluster by parsing the server URL.
const std::string& is_serverless_config_value = m_rt_opts.conn.is_serverless;

// Parsed from server URL.
return m_rt_opts.conn.server.find("aoss.amazonaws.com") != std::string::npos;
is_serverless =
is_serverless_config_value.empty()
? (m_rt_opts.conn.server.find("aoss.amazonaws.com") != std::string::npos)
: std::stoi(is_serverless_config_value);
}
6 changes: 4 additions & 2 deletions src/sqlodbc/opensearch_communication.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ class OpenSearchCommunication {
void StopResultRetrieval();
std::vector< std::string > GetColumnsWithSelectQuery(
const std::string table_name);
void SetSqlEndpoint();

// the endpoint is set according to distribution (ES/OpenSearch)
std::string sql_endpoint;
Expand All @@ -81,7 +80,9 @@ class OpenSearchCommunication {
void SetErrorDetails(std::string reason, std::string message,
ConnErrorType error_type);
void SetErrorDetails(ErrorDetails details);
bool isServerless();

void SetIsServerless();
void SetSqlEndpoint();

// TODO #35 - Go through and add error messages on exit conditions
std::string m_error_message;
Expand All @@ -90,6 +91,7 @@ class OpenSearchCommunication {
ConnStatusType m_status;
ConnErrorType m_error_type;
std::shared_ptr< ErrorDetails > m_error_details;
bool is_serverless;
bool m_valid_connection_options;
bool m_is_retrieving;
OpenSearchResultQueue m_result_queue;
Expand Down

0 comments on commit aa76d77

Please sign in to comment.