You've already forked mariadb-columnstore-engine
mirror of
https://github.com/mariadb-corporation/mariadb-columnstore-engine.git
synced 2025-07-30 19:23:07 +03:00
MCOL-497 securing mysql connection with TLS in crossengine and querystats code.
This commit is contained in:
@ -145,7 +145,7 @@ SET (ENGINE_LOCALDIR "${INSTALL_ENGINE}/local")
|
||||
SET (ENGINE_MYSQLDIR "${INSTALL_ENGINE}/mysql")
|
||||
SET (ENGINE_TOOLSDIR "${INSTALL_ENGINE}/tools")
|
||||
|
||||
SET (ENGINE_COMMON_LIBS messageqcpp loggingcpp configcpp idbboot ${Boost_LIBRARIES} xml2 pthread rt)
|
||||
SET (ENGINE_COMMON_LIBS messageqcpp loggingcpp configcpp idbboot ${Boost_LIBRARIES} xml2 pthread rt libmysql_client)
|
||||
SET (ENGINE_OAM_LIBS oamcpp alarmmanager)
|
||||
SET (ENGINE_BRM_LIBS brm idbdatafile cacheutils rwlock ${ENGINE_OAM_LIBS} ${ENGINE_COMMON_LIBS})
|
||||
SET (ENGINE_EXEC_LIBS joblist execplan windowfunction joiner rowgroup funcexp udfsdk dataconvert common compress querystats querytele thrift threadpool ${ENGINE_BRM_LIBS})
|
||||
@ -205,6 +205,7 @@ SET (ENGINE_UTILS_THREADPOOL_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/utils/threadp
|
||||
SET (ENGINE_UTILS_BATCHLDR_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/utils/batchloader")
|
||||
SET (ENGINE_UTILS_DDLCLEANUP_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/utils/ddlcleanup")
|
||||
SET (ENGINE_UTILS_QUERYSTATS_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/utils/querystats")
|
||||
SET (ENGINE_UTILS_LIBMYSQL_CL_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/utils/libmysql_client")
|
||||
SET (ENGINE_WE_CONFIGCPP_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/writeengine/xml")
|
||||
SET (ENGINE_SERVER_SQL_INCLUDE "${SERVER_SOURCE_ROOT_DIR}/sql")
|
||||
SET (ENGINE_SERVER_INCLUDE_INCLUDE "${SERVER_SOURCE_ROOT_DIR}/include")
|
||||
@ -213,7 +214,7 @@ SET (ENGINE_UTILS_UDFSDK_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/utils/udfsdk"
|
||||
|
||||
SET (ENGINE_DEFAULT_INCLUDES ${CMAKE_BINARY_DIR} "." "../" "../../" ${SERVER_BUILD_INCLUDE_DIR})
|
||||
|
||||
SET (ENGINE_COMMON_INCLUDES ${ENGINE_DEFAULT_INCLUDES} ${Boost_INCLUDE_DIR} ${LIBXML2_INCLUDE_DIR} ${ENGINE_UTILS_MESSAGEQCPP_INCLUDE} ${ENGINE_WE_SHARED_INCLUDE} ${ENGINE_UTILS_IDBDATAFILE_INCLUDE} ${ENGINE_UTILS_LOGGINGCPP_INCLUDE} ${ENGINE_UTILS_CONFIGCPP_INCLUDE} ${ENGINE_UTILS_COMPRESS_INCLUDE} ${ENGINE_VERSIONING_BRM_INCLUDE} ${ENGINE_UTILS_ROWGROUP_INCLUDE} ${ENGINE_UTILS_COMMON_INCLUDE} ${ENGINE_UTILS_DATACONVERT_INCLUDE} ${ENGINE_UTILS_RWLOCK_INCLUDE} ${ENGINE_UTILS_FUNCEXP_INCLUDE} ${ENGINE_OAMAPPS_ALARMMANAGER_INCLUDE} ${ENGINE_UTILS_INCLUDE} ${ENGINE_OAM_OAMCPP_INCLUDE} ${ENGINE_DBCON_DDLPKGPROC_INCLUDE} ${ENGINE_DBCON_DDLPKG_INCLUDE} ${ENGINE_DBCON_EXECPLAN_INCLUDE} ${ENGINE_UTILS_STARTUP_INCLUDE} ${ENGINE_DBCON_JOBLIST_INCLUDE} ${ENGINE_WE_WRAPPER_INCLUDE} ${ENGINE_WE_SERVER_INCLUDE} ${ENGINE_DBCON_DMLPKG_INCLUDE} ${ENGINE_WE_CLIENT_INCLUDE} ${ENGINE_DBCON_DMLPKGPROC_INCLUDE} ${ENGINE_UTILS_CACHEUTILS_INCLUDE} ${ENGINE_UTILS_MYSQLCL_INCLUDE} ${ENGINE_UTILS_QUERYTELE_INCLUDE} ${ENGINE_UTILS_THRIFT_INCLUDE} ${ENGINE_UTILS_JOINER_INCLUDE} ${ENGINE_UTILS_THREADPOOL_INCLUDE} ${ENGINE_UTILS_BATCHLDR_INCLUDE} ${ENGINE_UTILS_DDLCLEANUP_INCLUDE} ${ENGINE_UTILS_QUERYSTATS_INCLUDE} ${ENGINE_WE_CONFIGCPP_INCLUDE} ${ENGINE_SERVER_SQL_INCLUDE} ${ENGINE_SERVER_INCLUDE_INCLUDE} ${ENGINE_SERVER_PCRE_INCLUDE} ${ENGINE_UTILS_UDFSDK_INCLUDE})
|
||||
SET (ENGINE_COMMON_INCLUDES ${ENGINE_DEFAULT_INCLUDES} ${Boost_INCLUDE_DIR} ${LIBXML2_INCLUDE_DIR} ${ENGINE_UTILS_MESSAGEQCPP_INCLUDE} ${ENGINE_WE_SHARED_INCLUDE} ${ENGINE_UTILS_IDBDATAFILE_INCLUDE} ${ENGINE_UTILS_LOGGINGCPP_INCLUDE} ${ENGINE_UTILS_CONFIGCPP_INCLUDE} ${ENGINE_UTILS_COMPRESS_INCLUDE} ${ENGINE_VERSIONING_BRM_INCLUDE} ${ENGINE_UTILS_ROWGROUP_INCLUDE} ${ENGINE_UTILS_COMMON_INCLUDE} ${ENGINE_UTILS_DATACONVERT_INCLUDE} ${ENGINE_UTILS_RWLOCK_INCLUDE} ${ENGINE_UTILS_FUNCEXP_INCLUDE} ${ENGINE_OAMAPPS_ALARMMANAGER_INCLUDE} ${ENGINE_UTILS_INCLUDE} ${ENGINE_OAM_OAMCPP_INCLUDE} ${ENGINE_DBCON_DDLPKGPROC_INCLUDE} ${ENGINE_DBCON_DDLPKG_INCLUDE} ${ENGINE_DBCON_EXECPLAN_INCLUDE} ${ENGINE_UTILS_STARTUP_INCLUDE} ${ENGINE_DBCON_JOBLIST_INCLUDE} ${ENGINE_WE_WRAPPER_INCLUDE} ${ENGINE_WE_SERVER_INCLUDE} ${ENGINE_DBCON_DMLPKG_INCLUDE} ${ENGINE_WE_CLIENT_INCLUDE} ${ENGINE_DBCON_DMLPKGPROC_INCLUDE} ${ENGINE_UTILS_CACHEUTILS_INCLUDE} ${ENGINE_UTILS_MYSQLCL_INCLUDE} ${ENGINE_UTILS_QUERYTELE_INCLUDE} ${ENGINE_UTILS_THRIFT_INCLUDE} ${ENGINE_UTILS_JOINER_INCLUDE} ${ENGINE_UTILS_THREADPOOL_INCLUDE} ${ENGINE_UTILS_BATCHLDR_INCLUDE} ${ENGINE_UTILS_DDLCLEANUP_INCLUDE} ${ENGINE_UTILS_QUERYSTATS_INCLUDE} ${ENGINE_WE_CONFIGCPP_INCLUDE} ${ENGINE_SERVER_SQL_INCLUDE} ${ENGINE_SERVER_INCLUDE_INCLUDE} ${ENGINE_SERVER_PCRE_INCLUDE} ${ENGINE_UTILS_UDFSDK_INCLUDE} ${ENGINE_UTILS_LIBMYSQL_CL_INCLUDE})
|
||||
|
||||
ADD_SUBDIRECTORY(utils)
|
||||
ADD_SUBDIRECTORY(oam/oamcpp)
|
||||
|
@ -57,85 +57,11 @@ using namespace querytele;
|
||||
#include "jobstep.h"
|
||||
#include "jlf_common.h"
|
||||
|
||||
#include "libmysql_client.h"
|
||||
|
||||
namespace joblist
|
||||
{
|
||||
|
||||
LibMySQL::LibMySQL() : fCon(NULL), fRes(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
LibMySQL::~LibMySQL()
|
||||
{
|
||||
if (fRes)
|
||||
{
|
||||
mysql_free_result(fRes);
|
||||
}
|
||||
|
||||
fRes = NULL;
|
||||
|
||||
if (fCon)
|
||||
{
|
||||
mysql_close(fCon);
|
||||
}
|
||||
|
||||
fCon = NULL;
|
||||
}
|
||||
|
||||
|
||||
int LibMySQL::init(const char* h, unsigned int p, const char* u, const char* w, const char* d)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
fCon = mysql_init(NULL);
|
||||
|
||||
if (fCon != NULL)
|
||||
{
|
||||
unsigned int tcp_option = MYSQL_PROTOCOL_TCP;
|
||||
mysql_options(fCon, MYSQL_OPT_PROTOCOL, &tcp_option);
|
||||
|
||||
if (mysql_real_connect(fCon, h, u, w, d, p, NULL, 0) == NULL)
|
||||
{
|
||||
fErrStr = "fatal error in mysql_real_connect()";
|
||||
ret = mysql_errno(fCon);
|
||||
}
|
||||
else
|
||||
{
|
||||
mysql_set_character_set(fCon, "utf8");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
fErrStr = "fatal error in mysql_init()";
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int LibMySQL::run(const char* query)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (mysql_real_query(fCon, query, strlen(query)) != 0)
|
||||
{
|
||||
fErrStr = "fatal error reading result from crossengine client lib";
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
fRes = mysql_use_result(fCon);
|
||||
|
||||
if (fRes == NULL)
|
||||
{
|
||||
fErrStr = "fatal error reading result from crossengine client lib";
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
CrossEngineStep::CrossEngineStep(
|
||||
const string& schema,
|
||||
const string& table,
|
||||
@ -158,7 +84,7 @@ CrossEngineStep::CrossEngineStep(
|
||||
fExtendedInfo = "CES: ";
|
||||
getMysqldInfo(jobInfo);
|
||||
fQtc.stepParms().stepType = StepTeleStats::T_CES;
|
||||
mysql = new LibMySQL();
|
||||
mysql = new utils::LibMySQL();
|
||||
}
|
||||
|
||||
|
||||
@ -503,7 +429,7 @@ void CrossEngineStep::execute()
|
||||
ret = mysql->init(fHost.c_str(), fPort, fUser.c_str(), fPasswd.c_str(), fSchema.c_str());
|
||||
|
||||
if (ret != 0)
|
||||
handleMySqlError(mysql->getError().c_str(), ret);
|
||||
mysql->handleMySqlError(mysql->getError().c_str(), ret);
|
||||
|
||||
string query(makeQuery());
|
||||
fLogger->logMessage(logging::LOG_TYPE_INFO, "QUERY to foreign engine: " + query);
|
||||
@ -514,7 +440,7 @@ void CrossEngineStep::execute()
|
||||
ret = mysql->run(query.c_str());
|
||||
|
||||
if (ret != 0)
|
||||
handleMySqlError(mysql->getError().c_str(), ret);
|
||||
mysql->handleMySqlError(mysql->getError().c_str(), ret);
|
||||
|
||||
int num_fields = mysql->getFieldCount();
|
||||
|
||||
@ -818,22 +744,6 @@ string CrossEngineStep::makeQuery()
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
void CrossEngineStep::handleMySqlError(const char* errStr, unsigned int errCode)
|
||||
{
|
||||
ostringstream oss;
|
||||
oss << errStr << "(" << errCode << ")";
|
||||
|
||||
if (errCode == (unsigned int) - 1)
|
||||
oss << "(null pointer)";
|
||||
else
|
||||
oss << "(" << errCode << ")";
|
||||
|
||||
throw IDBExcept(oss.str(), ERR_CROSS_ENGINE_CONNECT);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const RowGroup& CrossEngineStep::getOutputRowGroup() const
|
||||
{
|
||||
return fRowGroupOut;
|
||||
@ -979,5 +889,3 @@ void CrossEngineStep::formatMiniStats()
|
||||
|
||||
}
|
||||
// vim:ts=4 sw=4:
|
||||
|
||||
|
||||
|
@ -32,6 +32,11 @@
|
||||
using namespace std;
|
||||
|
||||
// forward reference
|
||||
namespace utils
|
||||
{
|
||||
class LibMySQL;
|
||||
}
|
||||
|
||||
namespace execplan
|
||||
{
|
||||
class ParseTree;
|
||||
@ -45,55 +50,6 @@ class FuncExp;
|
||||
|
||||
namespace joblist
|
||||
{
|
||||
|
||||
class LibMySQL
|
||||
{
|
||||
public:
|
||||
LibMySQL();
|
||||
~LibMySQL();
|
||||
|
||||
// init: host port username passwd db
|
||||
int init(const char*, unsigned int, const char*, const char*, const char*);
|
||||
|
||||
// run the query
|
||||
int run(const char* q);
|
||||
|
||||
int getFieldCount()
|
||||
{
|
||||
return mysql_num_fields(fRes);
|
||||
}
|
||||
int getRowCount()
|
||||
{
|
||||
return mysql_num_rows(fRes);
|
||||
}
|
||||
char** nextRow()
|
||||
{
|
||||
char** row = mysql_fetch_row(fRes);
|
||||
fieldLengths = mysql_fetch_lengths(fRes);
|
||||
fFields = mysql_fetch_fields(fRes);
|
||||
return row;
|
||||
}
|
||||
long getFieldLength(int field)
|
||||
{
|
||||
return fieldLengths[field];
|
||||
}
|
||||
MYSQL_FIELD* getField(int field)
|
||||
{
|
||||
return &fFields[field];
|
||||
}
|
||||
const std::string& getError()
|
||||
{
|
||||
return fErrStr;
|
||||
}
|
||||
|
||||
private:
|
||||
MYSQL* fCon;
|
||||
MYSQL_RES* fRes;
|
||||
MYSQL_FIELD* fFields;
|
||||
std::string fErrStr;
|
||||
unsigned long* fieldLengths;
|
||||
};
|
||||
|
||||
/** @brief class CrossEngineStep
|
||||
*
|
||||
*/
|
||||
@ -215,7 +171,6 @@ protected:
|
||||
const char*, const execplan::CalpontSystemCatalog::ColType&, int64_t);
|
||||
virtual void formatMiniStats();
|
||||
virtual void printCalTrace();
|
||||
virtual void handleMySqlError(const char*, unsigned int);
|
||||
|
||||
uint64_t fRowsRetrieved;
|
||||
uint64_t fRowsReturned;
|
||||
@ -274,7 +229,7 @@ protected:
|
||||
rowgroup::RowGroup fRowGroupFe3;
|
||||
|
||||
funcexp::FuncExp* fFeInstance;
|
||||
LibMySQL* mysql;
|
||||
utils::LibMySQL* mysql;
|
||||
};
|
||||
|
||||
|
||||
|
@ -532,6 +532,9 @@ void Oam::getSystemConfig(const std::string& module, ModuleConfig& moduleconfig)
|
||||
const string MODULE_DISABLE_STATE = "ModuleDisableState";
|
||||
const string MODULE_DBROOT_COUNT = "ModuleDBRootCount";
|
||||
const string MODULE_DBROOT_ID = "ModuleDBRootID";
|
||||
const string MODULE_TLS_CA = "ModuleTLSCA";
|
||||
const string MODULE_TLS_CL_CERT = "ModuleTLSClientCert";
|
||||
const string MODULE_TLS_CL_KEY = "ModuleTLSClientKey";
|
||||
|
||||
string moduletype = module.substr(0, MAX_MODULE_TYPE_SIZE);
|
||||
int moduleID = atoi(module.substr(MAX_MODULE_TYPE_SIZE, MAX_MODULE_ID_SIZE).c_str());
|
||||
@ -606,6 +609,13 @@ void Oam::getSystemConfig(const std::string& module, ModuleConfig& moduleconfig)
|
||||
|
||||
sort ( moduleconfig.dbrootConfigList.begin(), moduleconfig.dbrootConfigList.end() );
|
||||
|
||||
if ( moduletype == "um" )
|
||||
{
|
||||
moduleconfig.TLSCA = sysConfig->getConfig(Section, MODULE_TLS_CA + itoa(moduleTypeID) );
|
||||
moduleconfig.TLSClientCert = sysConfig->getConfig(Section, MODULE_TLS_CL_CERT + itoa(moduleTypeID) );
|
||||
moduleconfig.TLSClientKey = sysConfig->getConfig(Section, MODULE_TLS_CL_KEY + itoa(moduleTypeID) );
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -2953,8 +2963,6 @@ oamModuleInfo_t Oam::getModuleInfo()
|
||||
// Get Server Type Install ID
|
||||
|
||||
serverTypeInstall = atoi(sysConfig->getConfig("Installation", "ServerTypeInstall").c_str());
|
||||
|
||||
sysConfig;
|
||||
}
|
||||
catch (...) {}
|
||||
|
||||
|
@ -833,6 +833,9 @@ struct ModuleConfig_s
|
||||
std::string ModuleType; //!< Module Type
|
||||
std::string ModuleDesc; //!< Module Description
|
||||
std::string DisableState; //!< Disabled State
|
||||
std::string TLSCA; //!< TLS CA cert or path
|
||||
std::string TLSClientCert; //!< TLS client cert path
|
||||
std::string TLSClientKey; //!< TLS client key path
|
||||
HostConfigList hostConfigList; //!< IP Address and Hostname List
|
||||
DBRootConfigList dbrootConfigList; //!< DBRoot ID list
|
||||
};
|
||||
|
@ -24,3 +24,4 @@ add_subdirectory(winport)
|
||||
add_subdirectory(thrift)
|
||||
add_subdirectory(querytele)
|
||||
add_subdirectory(clusterTester)
|
||||
add_subdirectory(libmysql_client)
|
||||
|
13
utils/libmysql_client/CMakeLists.txt
Normal file
13
utils/libmysql_client/CMakeLists.txt
Normal file
@ -0,0 +1,13 @@
|
||||
|
||||
include_directories( ${ENGINE_COMMON_INCLUDES} )
|
||||
|
||||
########### next target ###############
|
||||
|
||||
set(libmysql_client_LIB_SRCS libmysql_client.cpp)
|
||||
|
||||
add_library(libmysql_client SHARED ${libmysql_client_LIB_SRCS})
|
||||
|
||||
set_target_properties(libmysql_client PROPERTIES VERSION 1.0.0 SOVERSION 1)
|
||||
|
||||
install(TARGETS libmysql_client DESTINATION ${ENGINE_LIBDIR} COMPONENT libs)
|
||||
|
144
utils/libmysql_client/libmysql_client.cpp
Normal file
144
utils/libmysql_client/libmysql_client.cpp
Normal file
@ -0,0 +1,144 @@
|
||||
/* Copyright (C) 2014 InfiniDB, Inc.
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; version 2 of
|
||||
the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
MA 02110-1301, USA. */
|
||||
|
||||
#include <unistd.h>
|
||||
//#define NDEBUG
|
||||
#include <cassert>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
using namespace std;
|
||||
|
||||
#include "idberrorinfo.h"
|
||||
using namespace logging;
|
||||
|
||||
#include "liboamcpp.h"
|
||||
|
||||
#include "libmysql_client.h"
|
||||
|
||||
namespace utils
|
||||
{
|
||||
|
||||
LibMySQL::LibMySQL() : fCon(NULL), fRes(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
LibMySQL::~LibMySQL()
|
||||
{
|
||||
if (fRes)
|
||||
{
|
||||
mysql_free_result(fRes);
|
||||
}
|
||||
|
||||
fRes = NULL;
|
||||
|
||||
if (fCon)
|
||||
{
|
||||
mysql_close(fCon);
|
||||
}
|
||||
|
||||
fCon = NULL;
|
||||
}
|
||||
|
||||
|
||||
int LibMySQL::init(const char* h, unsigned int p, const char* u, const char* w, const char* d)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
fCon = mysql_init(NULL);
|
||||
|
||||
oam::Oam oam;
|
||||
oam::oamModuleInfo_t moduleInfo;
|
||||
moduleInfo = oam.getModuleInfo();
|
||||
string moduleName = boost::get<0>(moduleInfo);
|
||||
int serverTypeInstall = boost::get<5>(moduleInfo);
|
||||
|
||||
// This is single server installation so use um1 instead of pm1.
|
||||
if ( serverTypeInstall == 2 )
|
||||
moduleName.assign("um1");
|
||||
|
||||
oam::ModuleConfig moduleconfig;
|
||||
oam.getSystemConfig(moduleName, moduleconfig);
|
||||
|
||||
if (!(moduleconfig.TLSCA.empty() || moduleconfig.TLSClientCert.empty() || moduleconfig.TLSClientKey.empty()))
|
||||
{
|
||||
mysql_ssl_set(fCon, moduleconfig.TLSClientKey.c_str(), moduleconfig.TLSClientCert.c_str(),
|
||||
moduleconfig.TLSCA.c_str(), NULL, NULL);
|
||||
}
|
||||
|
||||
if (fCon != NULL)
|
||||
{
|
||||
unsigned int tcp_option = MYSQL_PROTOCOL_TCP;
|
||||
mysql_options(fCon, MYSQL_OPT_PROTOCOL, &tcp_option);
|
||||
|
||||
if (mysql_real_connect(fCon, h, u, w, d, p, NULL, 0) == NULL)
|
||||
{
|
||||
fErrStr = "fatal error running mysql_real_connect() in libmysql_client lib";
|
||||
ret = mysql_errno(fCon);
|
||||
}
|
||||
else
|
||||
{
|
||||
mysql_set_character_set(fCon, "utf8");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
fErrStr = "fatal error running mysql_init() in libmysql_client lib";
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int LibMySQL::run(const char* query)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (mysql_real_query(fCon, query, strlen(query)) != 0)
|
||||
{
|
||||
fErrStr = "fatal error runing mysql_real_query() in libmysql_client lib";
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
fRes = mysql_use_result(fCon);
|
||||
|
||||
if (fRes == NULL)
|
||||
{
|
||||
fErrStr = "fatal error running mysql_use_result() or empty result set in libmysql_client lib";
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void LibMySQL::handleMySqlError(const char* errStr, unsigned int errCode)
|
||||
{
|
||||
ostringstream oss;
|
||||
oss << errStr << "(" << errCode << ")";
|
||||
|
||||
if (errCode == (unsigned int) - 1)
|
||||
oss << "(null pointer)";
|
||||
else
|
||||
oss << "(" << errCode << ")";
|
||||
|
||||
throw IDBExcept(oss.str(), ERR_CROSS_ENGINE_CONNECT);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
} // namespace
|
89
utils/libmysql_client/libmysql_client.h
Normal file
89
utils/libmysql_client/libmysql_client.h
Normal file
@ -0,0 +1,89 @@
|
||||
/* Copyright (C) 2014 InfiniDB, Inc.
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; version 2 of
|
||||
the License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
MA 02110-1301, USA. */
|
||||
|
||||
#ifndef UTILS_LIBMYSQL_CL_H
|
||||
#define UTILS LIBMYSQL_CL_H
|
||||
|
||||
#include <my_config.h>
|
||||
#include <mysql.h>
|
||||
|
||||
#include <boost/scoped_array.hpp>
|
||||
|
||||
namespace utils
|
||||
{
|
||||
|
||||
class LibMySQL
|
||||
{
|
||||
public:
|
||||
LibMySQL();
|
||||
~LibMySQL();
|
||||
|
||||
// init: host port username passwd db
|
||||
int init(const char*, unsigned int, const char*, const char*, const char*);
|
||||
|
||||
// run the query
|
||||
int run(const char* q);
|
||||
|
||||
void handleMySqlError(const char*, unsigned int);
|
||||
|
||||
MYSQL* getMySqlCon()
|
||||
{
|
||||
return fCon;
|
||||
}
|
||||
int getFieldCount()
|
||||
{
|
||||
return mysql_num_fields(fRes);
|
||||
}
|
||||
int getRowCount()
|
||||
{
|
||||
return mysql_num_rows(fRes);
|
||||
}
|
||||
char** nextRow()
|
||||
{
|
||||
char** row = mysql_fetch_row(fRes);
|
||||
fieldLengths = mysql_fetch_lengths(fRes);
|
||||
fFields = mysql_fetch_fields(fRes);
|
||||
return row;
|
||||
}
|
||||
long getFieldLength(int field)
|
||||
{
|
||||
return fieldLengths[field];
|
||||
}
|
||||
MYSQL_FIELD* getField(int field)
|
||||
{
|
||||
return &fFields[field];
|
||||
}
|
||||
const std::string& getError()
|
||||
{
|
||||
return fErrStr;
|
||||
}
|
||||
|
||||
private:
|
||||
MYSQL* fCon;
|
||||
MYSQL_RES* fRes;
|
||||
MYSQL_FIELD* fFields;
|
||||
std::string fErrStr;
|
||||
unsigned long* fieldLengths;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
|
||||
#endif // UTILS_LIBMYSQL_CL_H
|
||||
|
||||
// vim:ts=4 sw=4:
|
@ -37,6 +37,8 @@ using namespace joblist;
|
||||
#include "errorids.h"
|
||||
using namespace logging;
|
||||
|
||||
#include "libmysql_client.h"
|
||||
|
||||
#include "querystats.h"
|
||||
|
||||
namespace querystats
|
||||
@ -44,21 +46,6 @@ namespace querystats
|
||||
|
||||
const string SCHEMA = "infinidb_querystats";
|
||||
|
||||
struct IDB_MySQL
|
||||
{
|
||||
IDB_MySQL(): fCon(NULL), fRes(NULL) {}
|
||||
~IDB_MySQL()
|
||||
{
|
||||
if (fRes)
|
||||
mysql_free_result(fRes);
|
||||
|
||||
if (fCon)
|
||||
mysql_close(fCon);
|
||||
}
|
||||
MYSQL* fCon;
|
||||
MYSQL_RES* fRes;
|
||||
};
|
||||
|
||||
QueryStats::QueryStats()
|
||||
{
|
||||
reset();
|
||||
@ -197,6 +184,7 @@ void QueryStats::insert()
|
||||
return;
|
||||
|
||||
// get configure for every query to allow only changing of connect info
|
||||
int ret;
|
||||
string host, user, pwd;
|
||||
uint32_t port;
|
||||
|
||||
@ -205,23 +193,17 @@ void QueryStats::insert()
|
||||
ERR_CROSS_ENGINE_CONFIG);
|
||||
|
||||
// insert stats to querystats table
|
||||
IDB_MySQL mysql;
|
||||
utils::LibMySQL mysql;
|
||||
|
||||
mysql.fCon = mysql_init(NULL);
|
||||
ret = mysql.init(host.c_str(), port, user.c_str(), pwd.c_str(),
|
||||
SCHEMA.c_str());
|
||||
|
||||
if (mysql.fCon == NULL)
|
||||
handleMySqlError("fatal error initializing querystats lib", -1);
|
||||
|
||||
unsigned int tcp_option = MYSQL_PROTOCOL_TCP;
|
||||
mysql_options(mysql.fCon, MYSQL_OPT_PROTOCOL, &tcp_option);
|
||||
|
||||
if (mysql_real_connect(mysql.fCon, host.c_str(), user.c_str(), pwd.c_str(),
|
||||
SCHEMA.c_str(), port, NULL, 0) == NULL)
|
||||
handleMySqlError("fatal error setting up parms in querystats lib", mysql_errno(mysql.fCon));
|
||||
if (ret != 0)
|
||||
mysql.handleMySqlError(mysql.getError().c_str(), ret);
|
||||
|
||||
// escape quote characters
|
||||
boost::scoped_array<char> query(new char[fQuery.length() * 2 + 1]);
|
||||
mysql_real_escape_string(mysql.fCon, query.get(), fQuery.c_str(), fQuery.length());
|
||||
mysql_real_escape_string(mysql.getMySqlCon(), query.get(), fQuery.c_str(), fQuery.length());
|
||||
|
||||
ostringstream insert;
|
||||
insert << "insert into querystats values (0, ";
|
||||
@ -246,19 +228,10 @@ void QueryStats::insert()
|
||||
insert << fNumFiles << ", ";
|
||||
insert << fFileBytes << ")"; // the last 2 fields are not populated yet
|
||||
|
||||
int ret = mysql_real_query(mysql.fCon, insert.str().c_str(), insert.str().length());
|
||||
ret = mysql.run(insert.str().c_str());
|
||||
|
||||
if (ret != 0)
|
||||
handleMySqlError("fatal error executing query in querystats lib", ret);
|
||||
}
|
||||
|
||||
void QueryStats::handleMySqlError(const char* errStr, unsigned int errCode)
|
||||
{
|
||||
ostringstream oss;
|
||||
oss << errStr << " (" << errCode << ")";
|
||||
Message::Args args;
|
||||
args.add(oss.str());
|
||||
throw IDBExcept(ERR_CROSS_ENGINE_CONNECT, args);
|
||||
mysql.handleMySqlError(mysql.getError().c_str(), ret);
|
||||
}
|
||||
|
||||
uint32_t QueryStats::userPriority(string _host, const string _user)
|
||||
@ -288,18 +261,14 @@ uint32_t QueryStats::userPriority(string _host, const string _user)
|
||||
ERR_CROSS_ENGINE_CONFIG);
|
||||
|
||||
// get user priority
|
||||
IDB_MySQL mysql;
|
||||
mysql.fCon = mysql_init(NULL);
|
||||
utils::LibMySQL mysql;
|
||||
|
||||
if (mysql.fCon == NULL)
|
||||
handleMySqlError("fatal error initializing querystats lib", -1);
|
||||
int ret;
|
||||
ret = mysql.init(host.c_str(), port, user.c_str(), pwd.c_str(),
|
||||
SCHEMA.c_str());
|
||||
|
||||
unsigned int tcp_option = MYSQL_PROTOCOL_TCP;
|
||||
mysql_options(mysql.fCon, MYSQL_OPT_PROTOCOL, &tcp_option);
|
||||
|
||||
if (mysql_real_connect(mysql.fCon, host.c_str(), user.c_str(), pwd.c_str(),
|
||||
SCHEMA.c_str(), port, NULL, 0) == NULL)
|
||||
handleMySqlError("fatal error connecting to InfiniDB in querystats lib", mysql_errno(mysql.fCon));
|
||||
if (ret != 0)
|
||||
mysql.handleMySqlError(mysql.getError().c_str(), ret);
|
||||
|
||||
// get the part of host string befor ':' if there is.
|
||||
size_t pos = _host.find(':', 0);
|
||||
@ -318,26 +287,17 @@ uint32_t QueryStats::userPriority(string _host, const string _user)
|
||||
<< _user
|
||||
<< "') and upper(a.priority) = upper(b.priority)";
|
||||
|
||||
int ret = mysql_real_query(mysql.fCon, query.str().c_str(), query.str().length());
|
||||
|
||||
ret = mysql.run(query.str().c_str());
|
||||
if (ret != 0)
|
||||
handleMySqlError("fatal error executing query in querystats lib", ret);
|
||||
mysql.handleMySqlError(mysql.getError().c_str(), ret);
|
||||
|
||||
// Using mysql_store_result here as for mysql_use_result we would need to get every row
|
||||
// Maybe limit 1 on the query in the future?
|
||||
mysql.fRes = mysql_store_result(mysql.fCon);
|
||||
char** rowIn;
|
||||
rowIn = mysql.nextRow();
|
||||
|
||||
if (mysql.fRes == NULL)
|
||||
handleMySqlError("fatal error reading results from InfiniDB in querystats lib", mysql_errno(mysql.fCon));
|
||||
|
||||
// only fetch one row. if duplicate user name in the table, the first one will be got.
|
||||
MYSQL_ROW row;
|
||||
row = mysql_fetch_row(mysql.fRes);
|
||||
|
||||
if (row)
|
||||
if(rowIn)
|
||||
{
|
||||
fPriority = row[0];
|
||||
fPriorityLevel = atoi(row[1]);
|
||||
fPriority = rowIn[0];
|
||||
fPriorityLevel = atoi(rowIn[1]);
|
||||
}
|
||||
|
||||
return fPriorityLevel;
|
||||
|
@ -127,7 +127,6 @@ struct QueryStats
|
||||
modified accordingly.
|
||||
*/
|
||||
void insert();
|
||||
void handleMySqlError(const char*, unsigned int);
|
||||
|
||||
/* User mysql API to query priority table and get this user's assigned priority */
|
||||
uint32_t userPriority(std::string host, const std::string user);
|
||||
|
Reference in New Issue
Block a user