1
0
mirror of https://github.com/mariadb-corporation/mariadb-columnstore-engine.git synced 2025-04-18 21:44:02 +03:00
mariadb-columnstore-engine/dbcon/execplan/mcsanalyzetableexecutionplan.cpp
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

159 lines
4.3 KiB
C++

/* Copyright (C) 2021 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. */
#include <iostream>
#include <algorithm>
using namespace std;
#include <boost/uuid/uuid_io.hpp>
#include "bytestream.h"
using namespace messageqcpp;
#include "mcsanalyzetableexecutionplan.h"
#include "objectreader.h"
#include "filter.h"
#include "returnedcolumn.h"
#include "simplecolumn.h"
#include "querystats.h"
#include "querytele.h"
using namespace querytele;
namespace execplan
{
std::string MCSAnalyzeTableExecutionPlan::toString() const
{
std::ostringstream output;
output << ">ANALYZE TABLE " << std::endl;
output << "Shema: " << fSchemaName << std::endl;
output << "Table: " << fTableName << std::endl;
output << ">>Returned Columns" << std::endl;
for (auto& rColumn : fReturnedCols)
output << *rColumn << std::endl;
output << "--- Column Map ---" << std::endl;
CalpontSelectExecutionPlan::ColumnMap::const_iterator iter;
for (iter = columnMap().begin(); iter != columnMap().end(); iter++)
output << (*iter).first << " : " << (*iter).second << endl;
output << "SessionID: " << fSessionID << std::endl;
output << "TxnID: " << fTxnID << std::endl;
output << "VerID: " << fVerID << std::endl;
return output.str();
}
void MCSAnalyzeTableExecutionPlan::serialize(messageqcpp::ByteStream& bs) const
{
bs << static_cast<ObjectReader::id_t>(ObjectReader::MCSANALYZETBLEXECUTIONPLAN);
// Returned columns.
bs << static_cast<uint32_t>(fReturnedCols.size());
for (auto& rColumn : fReturnedCols)
rColumn->serialize(bs);
// Column map.
bs << static_cast<uint32_t>(fColumnMap.size());
for (auto& column : fColumnMap)
{
bs << column.first;
column.second->serialize(bs);
}
bs << static_cast<uint32_t>(frmParms.size());
for (RMParmVec::const_iterator it = frmParms.begin(); it != frmParms.end(); ++it)
{
bs << it->sessionId;
bs << it->id;
bs << it->value;
}
bs << fData;
bs << static_cast<uint32_t>(fSessionID);
bs << static_cast<uint32_t>(fTxnID);
bs << fVerID;
bs << fStatementID;
bs << static_cast<uint64_t>(fStringScanThreshold);
bs << fPriority;
bs << fSchemaName;
bs << fTableName;
bs << fLocalQuery;
messageqcpp::ByteStream::octbyte timeZone = fTimeZone;
bs << timeZone;
bs << fTraceFlags;
}
void MCSAnalyzeTableExecutionPlan::unserialize(messageqcpp::ByteStream& bs)
{
ObjectReader::checkType(bs, ObjectReader::MCSANALYZETBLEXECUTIONPLAN);
fReturnedCols.clear();
fColumnMap.clear();
uint32_t size;
bs >> size;
for (uint32_t i = 0; i < size; ++i)
{
auto* returnedColumn = dynamic_cast<ReturnedColumn*>(ObjectReader::createTreeNode(bs));
SRCP srcp(returnedColumn);
fReturnedCols.push_back(srcp);
}
bs >> size;
for (uint32_t i = 0; i < size; ++i)
{
std::string colName;
bs >> colName;
auto* returnedColumn = dynamic_cast<ReturnedColumn*>(ObjectReader::createTreeNode(bs));
SRCP srcp(returnedColumn);
fColumnMap.insert(ColumnMap::value_type(colName, srcp));
}
bs >> size;
messageqcpp::ByteStream::doublebyte id;
messageqcpp::ByteStream::quadbyte sessionId;
messageqcpp::ByteStream::octbyte memory;
for (uint32_t i = 0; i < size; i++)
{
bs >> sessionId;
bs >> id;
bs >> memory;
frmParms.push_back(RMParam(sessionId, id, memory));
}
bs >> fData;
bs >> reinterpret_cast<uint32_t&>(fSessionID);
bs >> reinterpret_cast<uint32_t&>(fTxnID);
bs >> fVerID;
bs >> fStatementID;
bs >> reinterpret_cast<uint64_t&>(fStringScanThreshold);
bs >> fPriority;
bs >> fSchemaName;
bs >> fTableName;
bs >> fLocalQuery;
messageqcpp::ByteStream::octbyte timeZone;
bs >> timeZone;
fTimeZone = timeZone;
bs >> fTraceFlags;
}
} // namespace execplan