1
0
mirror of https://github.com/mariadb-corporation/mariadb-columnstore-engine.git synced 2025-04-23 07:05:36 +03:00
Gagan Goel 973e5024d8 MCOL-4957 Fix performance slowdown for processing TIMESTAMP columns.
Part 1:
 As part of MCOL-3776 to address synchronization issue while accessing
 the fTimeZone member of the Func class, mutex locks were added to the
 accessor and mutator methods. However, this slows down processing
 of TIMESTAMP columns in PrimProc significantly as all threads across
 all concurrently running queries would serialize on the mutex. This
 is because PrimProc only has a single global object for the functor
 class (class derived from Func in utils/funcexp/functor.h) for a given
 function name. To fix this problem:

   (1) We remove the fTimeZone as a member of the Func derived classes
   (hence removing the mutexes) and instead use the fOperationType
   member of the FunctionColumn class to propagate the timezone values
   down to the individual functor processing functions such as
   FunctionColumn::getStrVal(), FunctionColumn::getIntVal(), etc.

   (2) To achieve (1), a timezone member is added to the
   execplan::CalpontSystemCatalog::ColType class.

Part 2:
 Several functors in the Funcexp code call dataconvert::gmtSecToMySQLTime()
 and dataconvert::mySQLTimeToGmtSec() functions for conversion between seconds
 since unix epoch and broken-down representation. These functions in turn call
 the C library function localtime_r() which currently has a known bug of holding
 a global lock via a call to __tz_convert. This significantly reduces performance
 in multi-threaded applications where multiple threads concurrently call
 localtime_r(). More details on the bug:
   https://sourceware.org/bugzilla/show_bug.cgi?id=16145

 This bug in localtime_r() caused processing of the Functors in PrimProc to
 slowdown significantly since a query execution causes Functors code to be
 processed in a multi-threaded manner.

 As a fix, we remove the calls to localtime_r() from gmtSecToMySQLTime()
 and mySQLTimeToGmtSec() by performing the timezone-to-offset conversion
 (done in dataconvert::timeZoneToOffset()) during the execution plan
 creation in the plugin. Note that localtime_r() is only called when the
 time_zone system variable is set to "SYSTEM".

 This fix also required changing the timezone type from a std::string to
 a long across the system.
2022-02-14 14:12:27 -05:00

202 lines
6.7 KiB
C++

/* Copyright (C) 2014 InfiniDB, Inc.
Copyright (C) 2016 MariaDB Corporation
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. */
/***********************************************************************
* $Id: ha_view.cpp 9642 2013-06-24 14:57:42Z rdempsey $
*
*
***********************************************************************/
#define PREFER_MY_CONFIG_H
#include <my_config.h>
#include "idb_mysql.h"
#include <string>
using namespace std;
#include <boost/algorithm/string/case_conv.hpp>
using namespace boost;
#include "errorids.h"
using namespace logging;
#include "parsetree.h"
#include "simplefilter.h"
#include "calpontsystemcatalog.h"
using namespace execplan;
#include "ha_subquery.h"
#include "ha_view.h"
namespace cal_impl_if
{
extern uint32_t buildJoin(gp_walk_info& gwi, List<TABLE_LIST>& join_list,
std::stack<execplan::ParseTree*>& outerJoinStack);
extern string getViewName(TABLE_LIST* table_ptr);
CalpontSystemCatalog::TableAliasName& View::viewName()
{
return fViewName;
}
void View::viewName(execplan::CalpontSystemCatalog::TableAliasName& viewName)
{
fViewName = viewName;
}
void View::transform()
{
CalpontSelectExecutionPlan* csep = new CalpontSelectExecutionPlan();
csep->sessionID(fParentGwip->sessionid);
// gwi for the sub query
gp_walk_info gwi(fParentGwip->timeZone);
gwi.thd = fParentGwip->thd;
uint32_t sessionID = csep->sessionID();
gwi.sessionid = sessionID;
boost::shared_ptr<CalpontSystemCatalog> csc = CalpontSystemCatalog::makeCalpontSystemCatalog(sessionID);
csc->identity(CalpontSystemCatalog::FE);
gwi.csc = csc;
// traverse the table list of the view
TABLE_LIST* table_ptr = fSelect.get_table_list();
CalpontSelectExecutionPlan::SelectList derivedTbList;
// @bug 1796. Remember table order on the FROM list.
gwi.clauseType = FROM;
try
{
for (; table_ptr; table_ptr = table_ptr->next_local)
{
// mysql put vtable here for from sub. we ignore it
if (string(table_ptr->table_name.str).find("$vtable") != string::npos)
continue;
string viewName = getViewName(table_ptr);
if (lower_case_table_names)
{
boost::algorithm::to_lower(viewName);
}
if (table_ptr->derived)
{
SELECT_LEX* select_cursor = table_ptr->derived->first_select();
FromSubQuery* fromSub = new FromSubQuery(gwi, select_cursor);
string alias(table_ptr->alias.str);
if (lower_case_table_names)
{
boost::algorithm::to_lower(alias);
}
gwi.viewName =
make_aliasview("", alias, table_ptr->belong_to_view->alias.str, "", true, lower_case_table_names);
fromSub->alias(alias);
gwi.derivedTbList.push_back(SCSEP(fromSub->transform()));
// set alias to both table name and alias name of the derived table
CalpontSystemCatalog::TableAliasName tn = make_aliasview("", "", alias, viewName);
gwi.tbList.push_back(tn);
gwi.tableMap[tn] = make_pair(0, table_ptr);
// TODO MCOL-2178 isUnion member only assigned, never used
// MIGR::infinidb_vtable.isUnion = true; //by-pass the 2nd pass of rnd_init
}
else if (table_ptr->view)
{
// for nested view, the view name is vout.vin... format
CalpontSystemCatalog::TableAliasName tn =
make_aliasview(table_ptr->db.str, table_ptr->table_name.str, table_ptr->alias.str, viewName, true,
lower_case_table_names);
gwi.viewName = make_aliastable(table_ptr->db.str, table_ptr->table_name.str, viewName, true,
lower_case_table_names);
View* view = new View(*table_ptr->view->first_select_lex(), &gwi);
view->viewName(gwi.viewName);
gwi.viewList.push_back(view);
view->transform();
}
else
{
// check foreign engine tables
bool columnStore = (table_ptr->table ? isMCSTable(table_ptr->table) : true);
// trigger system catalog cache
if (columnStore)
csc->columnRIDs(make_table(table_ptr->db.str, table_ptr->table_name.str, lower_case_table_names),
true);
CalpontSystemCatalog::TableAliasName tn =
make_aliasview(table_ptr->db.str, table_ptr->table_name.str, table_ptr->alias.str, viewName,
columnStore, lower_case_table_names);
gwi.tbList.push_back(tn);
gwi.tableMap[tn] = make_pair(0, table_ptr);
fParentGwip->tableMap[tn] = make_pair(0, table_ptr);
}
}
if (gwi.fatalParseError)
{
setError(gwi.thd, ER_INTERNAL_ERROR, gwi.parseErrorText);
return;
}
}
catch (IDBExcept& ie)
{
setError(gwi.thd, ER_INTERNAL_ERROR, ie.what());
CalpontSystemCatalog::removeCalpontSystemCatalog(sessionID);
return;
}
catch (...)
{
string emsg = IDBErrorInfo::instance()->errorMsg(ERR_LOST_CONN_EXEMGR);
setError(gwi.thd, ER_INTERNAL_ERROR, emsg);
CalpontSystemCatalog::removeCalpontSystemCatalog(sessionID);
return;
}
// merge table list to parent select
fParentGwip->tbList.insert(fParentGwip->tbList.end(), gwi.tbList.begin(), gwi.tbList.end());
fParentGwip->derivedTbList.insert(fParentGwip->derivedTbList.end(), gwi.derivedTbList.begin(),
gwi.derivedTbList.end());
fParentGwip->correlatedTbNameVec.insert(fParentGwip->correlatedTbNameVec.end(),
gwi.correlatedTbNameVec.begin(), gwi.correlatedTbNameVec.end());
// merge view list to parent
fParentGwip->viewList.insert(fParentGwip->viewList.end(), gwi.viewList.begin(), gwi.viewList.end());
// merge non-collapsed outer join to parent select
stack<ParseTree*> tmpstack;
while (!gwi.ptWorkStack.empty())
{
tmpstack.push(gwi.ptWorkStack.top());
gwi.ptWorkStack.pop();
}
while (!tmpstack.empty())
{
fParentGwip->ptWorkStack.push(tmpstack.top());
tmpstack.pop();
}
}
uint32_t View::processJoin(gp_walk_info& gwi, std::stack<execplan::ParseTree*>& outerJoinStack)
{
return buildJoin(gwi, fSelect.top_join_list, outerJoinStack);
}
} // namespace cal_impl_if