Move some mysql/mariadb utilities to maxutils

Can be used in system tests later on.
This commit is contained in:
Esa Korhonen 2018-11-08 19:06:19 +02:00
parent a6fe6a0463
commit 0c7e737eb7
13 changed files with 203 additions and 106 deletions

View File

@ -170,6 +170,7 @@ include_directories(server/modules/include)
include_directories(${CMAKE_BINARY_DIR}/include)
include_directories(${CURL_INCLUDE_DIRS})
include_directories(maxutils/maxbase/include)
include_directories(maxutils/maxsql/include)
if (BUILD_CDC)
include_directories(avro)

View File

@ -41,14 +41,6 @@ char* mxs_lestr_consume(uint8_t** c, size_t* size);
*/
MYSQL* mxs_mysql_real_connect(MYSQL* mysql, SERVER* server, const char* user, const char* passwd);
/**
* Check if the MYSQL error number is a connection error.
*
* @param Error code
* @return True if the MYSQL error number is a connection error
*/
bool mxs_mysql_is_net_error(unsigned int errcode);
/**
* Execute a query using global query retry settings.
*
@ -59,17 +51,6 @@ bool mxs_mysql_is_net_error(unsigned int errcode);
*/
int mxs_mysql_query(MYSQL* conn, const char* query);
/**
* Execute a query, manually defining retry limits.
*
* @param conn MySQL connection
* @param query Query to execute
* @param query_retries Maximum number of retries
* @param query_retry_timeout Maximum time to spend retrying, in seconds
* @return return value of mysql_query
*/
int mxs_mysql_query_ex(MYSQL* conn, const char* query, int query_retries, time_t query_retry_timeout);
/**
* Trim MySQL quote characters surrounding a string.
*
@ -142,19 +123,4 @@ mxs_mysql_name_kind_t mxs_mysql_name_to_pcre(char* pcre,
*/
void mxs_mysql_update_server_version(MYSQL* mysql, SERVER* server);
/**
* Enable/disable the logging of all SQL statements MaxScale sends to
* the servers.
*
* @param enable If true, enable, if false, disable.
*/
void mxs_mysql_set_log_statements(bool enable);
/**
* Returns whether SQL statements sent to the servers are logged or not.
*
* @return True, if statements are logged, false otherwise.
*/
bool mxs_mysql_get_log_statements();
MXS_END_DECLS

View File

@ -1 +1,2 @@
add_subdirectory(maxbase)
add_subdirectory(maxsql)

View File

@ -0,0 +1,3 @@
include_directories(include)
add_subdirectory(include/maxsql)
add_subdirectory(src)

View File

@ -0,0 +1,3 @@
install_header(mariadb.hh devel)
install_header(ccdefs.hh devel)

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2018 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2022-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#pragma once
/**
* @file ccdefs.hh
*
* This file should be included first by all maxsql headers.
*/
#if !defined (__cplusplus)
#error This file is only to be included by C++ code.
#endif
#include <maxbase/ccdefs.hh>
/**
* All classes of MaxSql are defined in the namespace @c maxsql.
*/
namespace maxsql
{
}
/**
* Shorthand for the @c maxsql namespace.
*/
namespace mxq = maxsql;

View File

@ -0,0 +1,54 @@
/*
* Copyright (c) 2018 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2022-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#pragma once
#include <maxsql/ccdefs.hh>
#include <string>
#include <mysql.h>
namespace maxsql
{
/**
* Execute a query, manually defining retry limits.
*
* @param conn MySQL connection
* @param query Query to execute
* @param query_retries Maximum number of retries
* @param query_retry_timeout Maximum time to spend retrying, in seconds
* @return return value of mysql_query
*/
int mysql_query_ex(MYSQL* conn, const std::string& query, int query_retries, time_t query_retry_timeout);
/**
* Check if the MYSQL error number is a connection error.
*
* @param Error code
* @return True if the MYSQL error number is a connection error
*/
bool mysql_is_net_error(unsigned int errcode);
/**
* Enable/disable the logging of all SQL statements MaxScale sends to
* the servers.
*
* @param enable If true, enable, if false, disable.
*/
void mysql_set_log_statements(bool enable);
/**
* Returns whether SQL statements sent to the servers are logged or not.
*
* @return True, if statements are logged, false otherwise.
*/
bool mysql_get_log_statements();
}

View File

@ -0,0 +1,7 @@
add_library(maxsql STATIC
mariadb.cc
)
target_link_libraries(maxsql maxbase ${MARIADB_CONNECTOR_LIBRARIES})
set_target_properties(maxsql PROPERTIES VERSION "1.0.0" LINK_FLAGS -Wl,-z,defs)

View File

@ -0,0 +1,87 @@
/*
* Copyright (c) 2016 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2022-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#include <maxsql/mariadb.hh>
#include <time.h>
#include <errmsg.h>
#include <maxbase/assert.h>
namespace
{
struct THIS_UNIT
{
bool log_statements; // Should all statements sent to server be logged?
};
static THIS_UNIT this_unit =
{
false
};
}
namespace maxsql
{
int mysql_query_ex(MYSQL* conn, const std::string& query, int query_retries, time_t query_retry_timeout)
{
const char* query_cstr = query.c_str();
time_t start = time(NULL);
int rc = mysql_query(conn, query_cstr);
for (int n = 0; rc != 0 && n < query_retries && mysql_is_net_error(mysql_errno(conn))
&& time(NULL) - start < query_retry_timeout; n++)
{
rc = mysql_query(conn, query_cstr);
}
if (this_unit.log_statements)
{
const char* host = "0.0.0.0";
unsigned int port = 0;
MXB_AT_DEBUG(int rc1 = ) mariadb_get_info(conn, MARIADB_CONNECTION_HOST, &host);
MXB_AT_DEBUG(int rc2 = ) mariadb_get_info(conn, MARIADB_CONNECTION_PORT, &port);
mxb_assert(!rc1 && !rc2);
MXB_NOTICE("SQL([%s]:%u): %d, \"%s\"", host, port, rc, query_cstr);
}
return rc;
}
bool mysql_is_net_error(unsigned int errcode)
{
switch (errcode)
{
case CR_SOCKET_CREATE_ERROR:
case CR_CONNECTION_ERROR:
case CR_CONN_HOST_ERROR:
case CR_IPSOCK_ERROR:
case CR_SERVER_GONE_ERROR:
case CR_TCP_CONNECTION:
case CR_SERVER_LOST:
return true;
default:
return false;
}
}
void mysql_set_log_statements(bool enable)
{
this_unit.log_statements = enable;
}
bool mysql_get_log_statements()
{
return this_unit.log_statements;
}
}

View File

@ -51,6 +51,7 @@ add_library(maxscale-common SHARED
target_link_libraries(maxscale-common
maxbase
maxsql
${MARIADB_CONNECTOR_LIBRARIES}
${LZMA_LINK_FLAGS}
${PCRE2_LIBRARIES}

View File

@ -46,6 +46,7 @@
#include <maxbase/maxbase.hh>
#include <maxbase/stacktrace.hh>
#include <maxsql/mariadb.hh>
#include <maxscale/alloc.h>
#include <maxscale/adminusers.h>
#include <maxscale/dcb.h>
@ -3165,12 +3166,12 @@ static void disable_module_unloading(const char* arg)
static void enable_statement_logging(const char* arg)
{
mxs_mysql_set_log_statements(true);
maxsql::mysql_set_log_statements(true);
}
static void disable_statement_logging(const char* arg)
{
mxs_mysql_set_log_statements(false);
maxsql::mysql_set_log_statements(false);
}
static void redirect_output_to_file(const char* arg)

View File

@ -27,25 +27,12 @@
#include <stdbool.h>
#include <errmsg.h>
#include <maxsql/mariadb.hh>
#include <maxscale/alloc.h>
#include <maxscale/config.hh>
#include <maxscale/log.h>
#include <maxbase/atomic.hh>
namespace
{
struct THIS_UNIT
{
bool log_statements; // Should all statements sent to server be logged?
};
static THIS_UNIT this_unit =
{
false
};
}
/**
* @brief Calculate the length of a length-encoded integer in bytes
*
@ -224,53 +211,10 @@ MYSQL* mxs_mysql_real_connect(MYSQL* con, SERVER* server, const char* user, cons
return mysql;
}
bool mxs_mysql_is_net_error(unsigned int errcode)
{
switch (errcode)
{
case CR_SOCKET_CREATE_ERROR:
case CR_CONNECTION_ERROR:
case CR_CONN_HOST_ERROR:
case CR_IPSOCK_ERROR:
case CR_SERVER_GONE_ERROR:
case CR_TCP_CONNECTION:
case CR_SERVER_LOST:
return true;
default:
return false;
}
}
int mxs_mysql_query_ex(MYSQL* conn, const char* query, int query_retries, time_t query_retry_timeout)
{
time_t start = time(NULL);
int rc = mysql_query(conn, query);
for (int n = 0; rc != 0 && n < query_retries
&& mxs_mysql_is_net_error(mysql_errno(conn))
&& time(NULL) - start < query_retry_timeout; n++)
{
rc = mysql_query(conn, query);
}
if (this_unit.log_statements)
{
const char* host = "0.0.0.0";
unsigned int port = 0;
MXB_AT_DEBUG(int rc1 = ) mariadb_get_info(conn, MARIADB_CONNECTION_HOST, &host);
MXB_AT_DEBUG(int rc2 = ) mariadb_get_info(conn, MARIADB_CONNECTION_PORT, &port);
mxb_assert(!rc1 && !rc2);
MXS_NOTICE("SQL([%s]:%u): %d, \"%s\"", host, port, rc, query);
}
return rc;
}
int mxs_mysql_query(MYSQL* conn, const char* query)
{
MXS_CONFIG* cnf = config_get_global_options();
return mxs_mysql_query_ex(conn, query, cnf->query_retries, cnf->query_retry_timeout);
return maxsql::mysql_query_ex(conn, query, cnf->query_retries, cnf->query_retry_timeout);
}
const char* mxs_mysql_get_value(MYSQL_RES* result, MYSQL_ROW row, const char* key)
@ -415,13 +359,3 @@ void mxs_mysql_update_server_version(MYSQL* mysql, SERVER* server)
mxb_assert(version_string != NULL && version_num != 0);
server_set_version(server, version_string, version_num);
}
void mxs_mysql_set_log_statements(bool enable)
{
this_unit.log_statements = enable;
}
bool mxs_mysql_get_log_statements()
{
return this_unit.log_statements;
}

View File

@ -19,6 +19,7 @@
#include <thread>
#include <set>
#include <maxbase/format.hh>
#include <maxsql/mariadb.hh>
#include <maxscale/mysql_utils.h>
@ -114,7 +115,7 @@ bool MariaDBServer::execute_cmd_ex(const string& cmd, QueryRetryMode mode,
}
else
{
query_success = (mxs_mysql_query_ex(conn, cmd.c_str(), 0, 0) == 0);
query_success = (maxsql::mysql_query_ex(conn, cmd, 0, 0) == 0);
}
bool rval = false;
@ -207,7 +208,7 @@ bool MariaDBServer::execute_cmd_time_limit(const std::string& cmd, maxbase::Dura
Duration time_remaining = time_limit - timer.split();
keep_trying = (time_remaining.secs() > 0)
// either a connector-c timeout
&& (mxs_mysql_is_net_error(errornum)
&& (maxsql::mysql_is_net_error(errornum)
// or query was interrupted by max_statement_time.
|| (!cmd_prefix.empty() && errornum == ER_STATEMENT_TIMEOUT));
if (!cmd_success)