diff --git a/dbcon/joblist/diskjoinstep.cpp b/dbcon/joblist/diskjoinstep.cpp index 1dc255fe1..ee184a061 100644 --- a/dbcon/joblist/diskjoinstep.cpp +++ b/dbcon/joblist/diskjoinstep.cpp @@ -424,8 +424,8 @@ void DiskJoinStep::joinFcn() while (largeData) { l_largeRG.setData(largeData.get()); - thjs->joinOneRG(0, &joinResults, l_largeRG, l_outputRG, l_largeRow, l_joinFERow, - l_outputRow, baseRow, joinMatches, smallRowTemplates, + thjs->joinOneRG(0, joinResults, l_largeRG, l_outputRG, l_largeRow, l_joinFERow, + l_outputRow, baseRow, joinMatches, smallRowTemplates, outputDL.get(), &joiners, &colMappings, &fergMappings, &smallNullMem); for (j = 0; j < (int) joinResults.size(); j++) @@ -434,7 +434,7 @@ void DiskJoinStep::joinFcn() //cout << "got joined output " << l_outputRG.toString() << endl; outputDL->insert(joinResults[j]); } - + thjs->returnMemory(); joinResults.clear(); largeData = in->jp->getNextLargeRGData(); } @@ -443,7 +443,6 @@ void DiskJoinStep::joinFcn() { if (!lastLargeIteration) { - /* TODO: an optimization would be to detect whether any new rows were marked and if not suppress the save operation */ vector unmatched; @@ -454,7 +453,6 @@ void DiskJoinStep::joinFcn() } else { - //cout << "finishing small-outer output" << endl; vector unmatched; RGData rgData(l_outputRG); @@ -484,6 +482,21 @@ void DiskJoinStep::joinFcn() { outputDL->insert(rgData); //cout << "inserting a full RG" << endl; + if (thjs) + { + if (!thjs->getMemory(l_outputRG.getMaxDataSize())) + { + // calculate guess of size required for error message + uint64_t memReqd = (unmatched.size() * outputRG.getDataSize(1)) / 1048576; + Message::Args args; + args.add(memReqd); + args.add(thjs->resourceManager->getConfiguredUMMemLimit() / 1048576); + std::cerr << logging::IDBErrorInfo::instance()->errorMsg(logging::ERR_JOIN_RESULT_TOO_BIG, args) + << " @" << __FILE__ << ":" << __LINE__; + throw logging::IDBExcept(logging::ERR_JOIN_RESULT_TOO_BIG, args); + } + } + rgData.reinit(l_outputRG); l_outputRG.setData(&rgData); l_outputRG.resetRowGroup(0); @@ -498,6 +511,10 @@ void DiskJoinStep::joinFcn() //cout << "inserting an rg with " << l_outputRG.getRowCount() << endl; outputDL->insert(rgData); } + if (thjs) + { + thjs->returnMemory(); + } } } } diff --git a/dbcon/joblist/distributedenginecomm.cpp b/dbcon/joblist/distributedenginecomm.cpp index 01f29e75d..0b91d8b74 100644 --- a/dbcon/joblist/distributedenginecomm.cpp +++ b/dbcon/joblist/distributedenginecomm.cpp @@ -307,7 +307,11 @@ void DistributedEngineComm::Setup() newPmCount--; writeToLog(__FILE__, __LINE__, "Could not connect to PMS" + std::to_string(connectionId) + ": " + ex.what(), LOG_TYPE_ERROR); - cerr << "Could not connect to PMS" << std::to_string(connectionId) << ": " << ex.what() << endl; + if (newPmCount == 0) + { + writeToLog(__FILE__, __LINE__, "No more PMs to try to connect to", LOG_TYPE_ERROR); + break; + } } catch (...) { @@ -315,6 +319,11 @@ void DistributedEngineComm::Setup() newPmCount--; writeToLog(__FILE__, __LINE__, "Could not connect to PMS" + std::to_string(connectionId), LOG_TYPE_ERROR); + if (newPmCount == 0) + { + writeToLog(__FILE__, __LINE__, "No more PMs to try to connect to", LOG_TYPE_ERROR); + break; + } } } diff --git a/dbcon/joblist/groupconcat.cpp b/dbcon/joblist/groupconcat.cpp index 91aa4e3b4..73757734b 100644 --- a/dbcon/joblist/groupconcat.cpp +++ b/dbcon/joblist/groupconcat.cpp @@ -788,7 +788,6 @@ void GroupConcatOrderBy::processRow(const rowgroup::Row& row) fDataQueue.push(fData); uint64_t newSize = fRowsPerRG * fRowGroup.getRowSize(); - fMemSize += newSize; if (!fRm->getMemory(newSize, fSessionMemLimit)) { @@ -796,7 +795,8 @@ void GroupConcatOrderBy::processRow(const rowgroup::Row& row) << " @" << __FILE__ << ":" << __LINE__; throw IDBExcept(fErrorCode); } - + fMemSize += newSize; + fData.reinit(fRowGroup, fRowsPerRG); fRowGroup.setData(&fData); fRowGroup.resetRowGroup(0); @@ -979,7 +979,6 @@ void GroupConcatNoOrder::initialize(const rowgroup::SP_GroupConcat& gcc) fConcatColumns.push_back((*(i++)).second); uint64_t newSize = fRowsPerRG * fRowGroup.getRowSize(); - fMemSize += newSize; if (!fRm->getMemory(newSize, fSessionMemLimit)) { @@ -987,7 +986,8 @@ void GroupConcatNoOrder::initialize(const rowgroup::SP_GroupConcat& gcc) << " @" << __FILE__ << ":" << __LINE__; throw IDBExcept(fErrorCode); } - + fMemSize += newSize; + fData.reinit(fRowGroup, fRowsPerRG); fRowGroup.setData(&fData); fRowGroup.resetRowGroup(0); @@ -1014,15 +1014,14 @@ void GroupConcatNoOrder::processRow(const rowgroup::Row& row) { uint64_t newSize = fRowsPerRG * fRowGroup.getRowSize(); - fMemSize += newSize; - if (!fRm->getMemory(newSize, fSessionMemLimit)) { cerr << IDBErrorInfo::instance()->errorMsg(fErrorCode) << " @" << __FILE__ << ":" << __LINE__; throw IDBExcept(fErrorCode); } - + fMemSize += newSize; + fDataQueue.push(fData); fData.reinit(fRowGroup, fRowsPerRG); fRowGroup.setData(&fData); diff --git a/dbcon/joblist/limitedorderby.cpp b/dbcon/joblist/limitedorderby.cpp index 82cc73957..ab3c1615b 100644 --- a/dbcon/joblist/limitedorderby.cpp +++ b/dbcon/joblist/limitedorderby.cpp @@ -127,13 +127,13 @@ void LimitedOrderBy::processRow(const rowgroup::Row& row) fUncommitedMemory += memSizeInc; if (fUncommitedMemory >= fMaxUncommited) { - fMemSize += fUncommitedMemory; if (!fRm->getMemory(fUncommitedMemory, fSessionMemLimit)) { cerr << IDBErrorInfo::instance()->errorMsg(fErrorCode) << " @" << __FILE__ << ":" << __LINE__; throw IDBExcept(fErrorCode); } + fMemSize += fUncommitedMemory; fUncommitedMemory = 0; } @@ -148,7 +148,6 @@ void LimitedOrderBy::processRow(const rowgroup::Row& row) { fDataQueue.push(fData); uint64_t newSize = fRowGroup.getSizeWithStrings() - fRowGroup.getHeaderSize(); - fMemSize += newSize; if (!fRm->getMemory(newSize, fSessionMemLimit)) { @@ -156,7 +155,8 @@ void LimitedOrderBy::processRow(const rowgroup::Row& row) << " @" << __FILE__ << ":" << __LINE__; throw IDBExcept(fErrorCode); } - + fMemSize += newSize; + fData.reinit(fRowGroup, fRowsPerRG); fRowGroup.setData(&fData); fRowGroup.resetRowGroup(0); @@ -190,13 +190,13 @@ void LimitedOrderBy::finalize() { if (fUncommitedMemory > 0) { - fMemSize += fUncommitedMemory; if (!fRm->getMemory(fUncommitedMemory, fSessionMemLimit)) { cerr << IDBErrorInfo::instance()->errorMsg(fErrorCode) << " @" << __FILE__ << ":" << __LINE__; throw IDBExcept(fErrorCode); } + fMemSize += fUncommitedMemory; fUncommitedMemory = 0; } @@ -209,7 +209,6 @@ void LimitedOrderBy::finalize() // *DRRTUY Very memory intensive. CS needs to account active // memory only and release memory if needed. uint64_t memSizeInc = fRowGroup.getSizeWithStrings() - fRowGroup.getHeaderSize(); - fMemSize += memSizeInc; if (!fRm->getMemory(memSizeInc, fSessionMemLimit)) { @@ -217,6 +216,7 @@ void LimitedOrderBy::finalize() << " @" << __FILE__ << ":" << __LINE__; throw IDBExcept(fErrorCode); } + fMemSize += memSizeInc; uint64_t offset = 0; uint64_t i = 0; @@ -265,7 +265,6 @@ void LimitedOrderBy::finalize() if(offset == (uint64_t)-1) { tempRGDataList.push_front(fData); - fMemSize += memSizeInc; if (!fRm->getMemory(memSizeInc, fSessionMemLimit)) { @@ -273,6 +272,7 @@ void LimitedOrderBy::finalize() << " @" << __FILE__ << ":" << __LINE__; throw IDBExcept(fErrorCode); } + fMemSize += memSizeInc; fData.reinit(fRowGroup, fRowsPerRG); fRowGroup.setData(&fData); diff --git a/dbcon/joblist/primitivestep.h b/dbcon/joblist/primitivestep.h index 0a3cdbdb0..b92e461a5 100644 --- a/dbcon/joblist/primitivestep.h +++ b/dbcon/joblist/primitivestep.h @@ -1366,7 +1366,15 @@ public: { return true; } - + ResourceManager* resourceManager() const + { + return fRm; + } + bool runFEonPM() const + { + return bRunFEonPM; + } + protected: void sendError(uint16_t status); @@ -1454,12 +1462,6 @@ private: void serializeJoiner(); void serializeJoiner(uint32_t connectionNumber); - void generateJoinResultSet(const std::vector>& joinerOutput, - rowgroup::Row& baseRow, const std::vector>& mappings, - const uint32_t depth, rowgroup::RowGroup& outputRG, rowgroup::RGData& rgData, - std::vector* outputData, - const boost::scoped_array& smallRows, rowgroup::Row& joinedRow); - std::vector> tjoiners; bool doJoin, hasPMJoin, hasUMJoin; std::vector joinerMatchesRGs; // parses the small-side matches from joiner @@ -1491,17 +1493,12 @@ private: boost::shared_ptr fe1, fe2; rowgroup::RowGroup fe1Input, fe2Output; boost::shared_array fe2Mapping; - bool runFEonPM; + bool bRunFEonPM; /* for UM F & E 2 processing */ rowgroup::RGData fe2Data; rowgroup::Row fe2InRow, fe2OutRow; - void processFE2(rowgroup::RowGroup& input, rowgroup::RowGroup& output, rowgroup::Row& inRow, rowgroup::Row& outRow, - std::vector* rgData, funcexp::FuncExpWrapper* localFE2); - void processFE2_oneRG(rowgroup::RowGroup& input, rowgroup::RowGroup& output, rowgroup::Row& inRow, - rowgroup::Row& outRow, funcexp::FuncExpWrapper* localFE2); - /* Runtime Casual Partitioning adjustments. The CP code is needlessly complicated; * to avoid making it worse, decided to designate 'scanFlags' as the static * component and this new array as the runtime component. The final CP decision @@ -1515,8 +1512,9 @@ private: boost::shared_ptr deliveryDL; uint32_t deliveryIt; - struct JoinLocalData + class JoinLocalData { + public: JoinLocalData() = delete; JoinLocalData(const JoinLocalData&) = delete; JoinLocalData(JoinLocalData&&) = delete; @@ -1524,12 +1522,21 @@ private: JoinLocalData& operator=(JoinLocalData&&) = delete; ~JoinLocalData() = default; - JoinLocalData(rowgroup::RowGroup& primRowGroup, rowgroup::RowGroup& outputRowGroup, + JoinLocalData(TupleBPS* pTupleBPS, rowgroup::RowGroup& primRowGroup, rowgroup::RowGroup& outputRowGroup, boost::shared_ptr& fe2, rowgroup::RowGroup& fe2Output, std::vector& joinerMatchesRGs, rowgroup::RowGroup& joinFERG, std::vector>& tjoiners, uint32_t smallSideCount, bool doJoin); + friend class TupleBPS; + + private: + uint64_t generateJoinResultSet(const uint32_t depth, + std::vector& outputData, + RowGroupDL* dlp); + void processFE2(vector& rgData); + + TupleBPS* tbps; // Parent rowgroup::RowGroup local_primRG; rowgroup::RowGroup local_outputRG; @@ -1565,7 +1572,7 @@ private: boost::scoped_array largeNullMemory; boost::scoped_array> smallNullMemory; uint32_t matchCount; - + rowgroup::Row postJoinRow; rowgroup::RowGroup local_fe2Output; rowgroup::RGData local_fe2Data; @@ -1591,7 +1598,7 @@ private: for (uint32_t i = 0; i < numThreads; ++i) { joinLocalDataPool.push_back(std::shared_ptr( - new JoinLocalData(primRowGroup, outputRowGroup, fe2, fe2Output, joinerMatchesRGs, joinFERG, + new JoinLocalData(this, primRowGroup, outputRowGroup, fe2, fe2Output, joinerMatchesRGs, joinFERG, tjoiners, smallSideCount, doJoin))); } diff --git a/dbcon/joblist/resourcemanager.cpp b/dbcon/joblist/resourcemanager.cpp index 55b11f1ae..ca4e1c7a7 100644 --- a/dbcon/joblist/resourcemanager.cpp +++ b/dbcon/joblist/resourcemanager.cpp @@ -267,6 +267,9 @@ ResourceManager::ResourceManager(bool runningInExeMgr) : fAllowedDiskAggregation = getBoolVal(fRowAggregationStr, "AllowDiskBasedAggregation", defaultAllowDiskAggregation); + + fMaxBPPSendQueue = getUintVal(fPrimitiveServersStr, "MaxBPPSendQueue", defaultMaxBPPSendQueue); + if (!load_encryption_keys()) { Logger log; @@ -347,7 +350,7 @@ void ResourceManager::emMaxPct() { } void ResourceManager::emPriority() { } void ResourceManager::emExecQueueSize() { } -void ResourceManager::hjNumThreads() { } +void ResourceManager::hjNumThreads() { } void ResourceManager::hjMaxBuckets() { } void ResourceManager::hjMaxElems() { } void ResourceManager::hjFifoSizeLargeSide() { } @@ -418,24 +421,52 @@ bool ResourceManager::userPriorityEnabled() const return "Y" == val; } -bool ResourceManager::getMemory(int64_t amount, boost::shared_ptr sessionLimit, bool patience) +// Counts memory. This funtion doesn't actually malloc, just counts against two limits +// totalUmMemLimit for overall UM counting and (optional) sessionLimit for a single session. +// If both have space, return true. +bool ResourceManager::getMemory(int64_t amount, boost::shared_ptr& sessionLimit, bool patience) { bool ret1 = (atomicops::atomicSub(&totalUmMemLimit, amount) >= 0); - bool ret2 = (atomicops::atomicSub(sessionLimit.get(), amount) >= 0); + bool ret2 = sessionLimit ? (atomicops::atomicSub(sessionLimit.get(), amount) >= 0) : ret1; uint32_t retryCounter = 0, maxRetries = 20; // 10s delay while (patience && !(ret1 && ret2) && retryCounter++ < maxRetries) { atomicops::atomicAdd(&totalUmMemLimit, amount); - atomicops::atomicAdd(sessionLimit.get(), amount); + sessionLimit ? atomicops::atomicAdd(sessionLimit.get(), amount) : 0; usleep(500000); ret1 = (atomicops::atomicSub(&totalUmMemLimit, amount) >= 0); - ret2 = (atomicops::atomicSub(sessionLimit.get(), amount) >= 0); + ret2 = sessionLimit ? (atomicops::atomicSub(sessionLimit.get(), amount) >= 0) : ret1; + } + if (!(ret1 && ret2)) + { + // If we didn't get any memory, restore the counters. + atomicops::atomicAdd(&totalUmMemLimit, amount); + sessionLimit ? atomicops::atomicAdd(sessionLimit.get(), amount) : 0; } - return (ret1 && ret2); } +// Don't care about session memory +bool ResourceManager::getMemory(int64_t amount, bool patience) +{ + bool ret1 = (atomicops::atomicSub(&totalUmMemLimit, amount) >= 0); + + uint32_t retryCounter = 0, maxRetries = 20; // 10s delay + + while (patience && !ret1 && retryCounter++ < maxRetries) + { + atomicops::atomicAdd(&totalUmMemLimit, amount); + usleep(500000); + ret1 = (atomicops::atomicSub(&totalUmMemLimit, amount) >= 0); + } + if (!ret1) + { + // If we didn't get any memory, restore the counters. + atomicops::atomicAdd(&totalUmMemLimit, amount); + } + return ret1; +} } //namespace diff --git a/dbcon/joblist/resourcemanager.h b/dbcon/joblist/resourcemanager.h index 31106b3be..476b57bcc 100644 --- a/dbcon/joblist/resourcemanager.h +++ b/dbcon/joblist/resourcemanager.h @@ -82,6 +82,15 @@ const uint32_t defaultMaxOutstandingRequests = 20; const uint32_t defaultProcessorThreadsPerScan = 16; const uint32_t defaultJoinerChunkSize = 16 * 1024 * 1024; +// I estimate that the average non-cloud columnstore node has 64GB. I've seen from 16GB to 256GB. Cloud can be as low as 4GB +// However, ExeMgr has a targetRecvQueueSize hardcoded to 50,000,000, so some number greater than this makes sense. Seriously greater doesn't make sense, +// so I went with 5x. If there are a number of simultaneous queries that return giant result sets, then 0.25 GB each seems reasonable. +// This is only for the return queue. We still need room for all the processing, and if a single node system, for ExeMgr as well. +// On small systems, I recommend we use a smaller value. +// I believe a larger value will not improve anything since at this point, we're just filling a queue much faster than it can be emptied. +// Even if we make this default larger, giant results will still eventually block. Just with less memory available for other processing. +const uint64_t defaultMaxBPPSendQueue = 250000000; // ~250MB + //bucketreuse const std::string defaultTempDiskPath = "/tmp"; const std::string defaultWorkingDir = "."; //"/tmp"; @@ -380,6 +389,11 @@ public: return getUintVal(fJobListStr, "DECThrottleThreshold", defaultDECThrottleThreshold); } + uint64_t getMaxBPPSendQueue() const + { + return fMaxBPPSendQueue; + } + EXPORT void emServerThreads(); EXPORT void emServerQueueSize(); EXPORT void emSecondsBetweenMemChecks(); @@ -397,11 +411,16 @@ public: /* sessionLimit is a pointer to the var holding the session-scope limit, should be JobInfo.umMemLimit for the query. */ /* Temporary parameter 'patience', will wait for up to 10s to get the memory. */ - EXPORT bool getMemory(int64_t amount, boost::shared_ptr sessionLimit, bool patience = true); - inline void returnMemory(int64_t amount, boost::shared_ptr sessionLimit) + EXPORT bool getMemory(int64_t amount, boost::shared_ptr& sessionLimit, bool patience = true); + EXPORT bool getMemory(int64_t amount, bool patience = true); + inline void returnMemory(int64_t amount) { atomicops::atomicAdd(&totalUmMemLimit, amount); - atomicops::atomicAdd(sessionLimit.get(), amount); + } + inline void returnMemory(int64_t amount, boost::shared_ptr& sessionLimit) + { + atomicops::atomicAdd(&totalUmMemLimit, amount); + sessionLimit ? atomicops::atomicAdd(sessionLimit.get(), amount): 0; } inline int64_t availableMemory() const { @@ -602,7 +621,7 @@ private: /* new HJ/Union/Aggregation support */ volatile int64_t totalUmMemLimit; // mem limit for join, union, and aggregation on the UM - uint64_t configuredUmMemLimit; + int64_t configuredUmMemLimit; uint64_t pmJoinMemLimit; // mem limit on individual PM joins /* multi-thread aggregate */ @@ -618,6 +637,7 @@ private: bool fUseHdfs; bool fAllowedDiskAggregation{false}; uint64_t fDECConnectionsPerQuery; + uint64_t fMaxBPPSendQueue = 250000000; }; @@ -662,3 +682,4 @@ inline bool ResourceManager::getBoolVal(const std::string& section, const std::s #undef EXPORT #endif + diff --git a/dbcon/joblist/tuple-bps.cpp b/dbcon/joblist/tuple-bps.cpp index c34098004..47def09ff 100644 --- a/dbcon/joblist/tuple-bps.cpp +++ b/dbcon/joblist/tuple-bps.cpp @@ -153,15 +153,15 @@ struct TupleBPSAggregators } }; -TupleBPS::JoinLocalData::JoinLocalData(RowGroup& primRowGroup, RowGroup& outputRowGroup, +TupleBPS::JoinLocalData::JoinLocalData(TupleBPS* pTupleBPS, RowGroup& primRowGroup, RowGroup& outputRowGroup, boost::shared_ptr& fe2, rowgroup::RowGroup& fe2Output, std::vector& joinerMatchesRGs, rowgroup::RowGroup& joinFERG, std::vector>& tjoiners, uint32_t smallSideCount, bool doJoin) - : local_primRG(primRowGroup), local_outputRG(outputRowGroup), fe2(fe2), fe2Output(fe2Output), - joinerMatchesRGs(joinerMatchesRGs), joinFERG(joinFERG), tjoiners(tjoiners), + : tbps(pTupleBPS), local_primRG(primRowGroup), local_outputRG(outputRowGroup), fe2(fe2), + fe2Output(fe2Output), joinerMatchesRGs(joinerMatchesRGs), joinFERG(joinFERG), tjoiners(tjoiners), smallSideCount(smallSideCount), doJoin(doJoin) { if (doJoin || fe2) @@ -231,6 +231,132 @@ TupleBPS::JoinLocalData::JoinLocalData(RowGroup& primRowGroup, RowGroup& outputR } } +uint64_t TupleBPS::JoinLocalData::generateJoinResultSet(const uint32_t depth, + std::vector& outputData, + RowGroupDL* dlp) +{ + uint32_t i; + Row& smallRow = smallSideRows[depth]; + uint64_t memSizeForOutputRG = 0; + + if (depth < smallSideCount - 1) + { + for (i = 0; i < joinerOutput[depth].size() && !tbps->cancelled(); i++) + { + smallRow.setPointer(joinerOutput[depth][i]); + applyMapping(smallMappings[depth], smallRow, &joinedBaseRow); + memSizeForOutputRG += generateJoinResultSet(depth + 1, outputData, dlp); + } + } + else + { + local_outputRG.getRow(local_outputRG.getRowCount(), &postJoinRow); + + for (i = 0; i < joinerOutput[depth].size() && !tbps->cancelled(); i++, postJoinRow.nextRow(), + local_outputRG.incRowCount()) + { + smallRow.setPointer(joinerOutput[depth][i]); + + if (UNLIKELY(local_outputRG.getRowCount() == 8192)) + { + uint32_t dbRoot = local_outputRG.getDBRoot(); + uint64_t baseRid = local_outputRG.getBaseRid(); + outputData.push_back(joinedData); + // Don't let the join results buffer get out of control. + if (tbps->resourceManager()->getMemory(local_outputRG.getMaxDataSize(), false)) + { + memSizeForOutputRG += local_outputRG.getMaxDataSize(); + } + else + { + // Don't wait for memory, just send the data on to DL. + RowGroup out(local_outputRG); + if (fe2 && tbps->runFEonPM()) + { + processFE2(outputData); + tbps->rgDataVecToDl(outputData, local_fe2Output, dlp); + } + else + { + tbps->rgDataVecToDl(outputData, out, dlp); + } + tbps->resourceManager()->returnMemory(memSizeForOutputRG); + memSizeForOutputRG = 0; + } + joinedData.reinit(local_outputRG); + local_outputRG.setData(&joinedData); + local_outputRG.resetRowGroup(baseRid); + local_outputRG.setDBRoot(dbRoot); + local_outputRG.getRow(0, &postJoinRow); + } + + applyMapping(smallMappings[depth], smallRow, &joinedBaseRow); + copyRow(joinedBaseRow, &postJoinRow); + } + } + return memSizeForOutputRG; +} + +void TupleBPS::JoinLocalData::processFE2(vector& rgData) +{ + vector results; + RGData result; + uint32_t i, j; + bool ret; + + result = RGData(local_fe2Output); + local_fe2Output.setData(&result); + local_fe2Output.resetRowGroup(-1); + local_fe2Output.getRow(0, &local_fe2OutRow); + + for (i = 0; i < rgData.size(); i++) + { + local_outputRG.setData(&(rgData)[i]); + + if (local_fe2Output.getRowCount() == 0) + { + local_fe2Output.resetRowGroup(local_outputRG.getBaseRid()); + local_fe2Output.setDBRoot(local_outputRG.getDBRoot()); + } + + local_outputRG.getRow(0, &postJoinRow); + + for (j = 0; j < local_outputRG.getRowCount(); j++, postJoinRow.nextRow()) + { + ret = local_fe2.evaluate(&postJoinRow); + + if (ret) + { + applyMapping(tbps->fe2Mapping, postJoinRow, &local_fe2OutRow); + local_fe2OutRow.setRid(postJoinRow.getRelRid()); + local_fe2Output.incRowCount(); + local_fe2OutRow.nextRow(); + + if (local_fe2Output.getRowCount() == 8192 || + local_fe2Output.getDBRoot() != local_outputRG.getDBRoot() || + local_fe2Output.getBaseRid() != local_outputRG.getBaseRid() + ) + { + results.push_back(result); + result = RGData(local_fe2Output); + local_fe2Output.setData(&result); + local_fe2Output.resetRowGroup(local_outputRG.getBaseRid()); + local_fe2Output.setDBRoot(local_outputRG.getDBRoot()); + local_fe2Output.getRow(0, &local_fe2OutRow); + } + } + } + } + + if (local_fe2Output.getRowCount() > 0) + { + results.push_back(result); + } + + rgData.swap(results); +} + + struct ByteStreamProcessor { ByteStreamProcessor(TupleBPS* tbps, vector>& bsv, @@ -379,7 +505,6 @@ TupleBPS::TupleBPS(const pColStep& rhs, const JobInfo& jobInfo) : fExtendedInfo = "TBPS: "; fQtc.stepParms().stepType = StepTeleStats::T_BPS; - hasPCFilter = hasPMFilter = hasRIDFilter = hasSegmentFilter = hasDBRootFilter = hasSegmentDirFilter = hasPartitionFilter = hasMaxFilter = hasMinFilter = hasLBIDFilter = hasExtentIDFilter = false; } @@ -1294,7 +1419,7 @@ void TupleBPS::run() if (fe1) fBPP->setFEGroup1(fe1, fe1Input); - if (fe2 && runFEonPM) + if (fe2 && bRunFEonPM) fBPP->setFEGroup2(fe2, fe2Output); if (fe2) @@ -2006,6 +2131,7 @@ void TupleBPS::processByteStreamVector(vectorlargeMapping, data->largeSideRow, &data->joinedBaseRow); data->joinedBaseRow.setRid(data->largeSideRow.getRelRid()); - generateJoinResultSet(data->joinerOutput, data->joinedBaseRow, data->smallMappings, 0, - data->local_outputRG, data->joinedData, &rgDatav, - data->smallSideRows, data->postJoinRow); - - // Bug 3510: Don't let the join results buffer get out of control. Need - // to refactor this. All post-join processing needs to go here AND below - // for now. - if (rgDatav.size() * data->local_outputRG.getMaxDataSize() > 50000000) - { - RowGroup out(data->local_outputRG); - - if (fe2 && !runFEonPM) - { - processFE2(out, data->local_fe2Output, data->postJoinRow, data->local_fe2OutRow, - &rgDatav, &data->local_fe2); - rgDataVecToDl(rgDatav, data->local_fe2Output, dlp); - } - else - rgDataVecToDl(rgDatav, out, dlp); - } + memAmount += data->generateJoinResultSet( 0, rgDatav, dlp); } } // end of the for-loop in the join code @@ -2204,12 +2311,16 @@ void TupleBPS::processByteStreamVector(vector 0 && !cancelled()) + if (memAmount) { - processFE2(data->local_outputRG, data->local_fe2Output, data->postJoinRow, - data->local_fe2OutRow, &rgDatav, &data->local_fe2); + resourceManager()->returnMemory(memAmount); + memAmount = 0; + } + + // Execute UM F & E group 2 on rgDatav + if (fe2 && !bRunFEonPM && rgDatav.size() > 0 && !cancelled()) + { + data->processFE2(rgDatav); rgDataVecToDl(rgDatav, data->local_fe2Output, dlp); } @@ -2233,7 +2344,7 @@ void TupleBPS::processByteStreamVector(vector 0) { - if (fe2 && runFEonPM) + if (fe2 && bRunFEonPM) rgDataVecToDl(rgDatav, data->local_fe2Output, dlp); else rgDataVecToDl(rgDatav, data->local_outputRG, dlp); @@ -2382,10 +2493,7 @@ void TupleBPS::receiveMultiPrimitiveMessages() start = end; } - // Join threads. - for (uint32_t i = 0, e = fProcessorThreads.size(); i < e; ++i) - jobstepThreadPool.join(fProcessorThreads[i]); - + jobstepThreadPool.join(fProcessorThreads); // Clear all. fProcessorThreads.clear(); bsv.clear(); @@ -2444,7 +2552,7 @@ void TupleBPS::receiveMultiPrimitiveMessages() abort_nolock(); } - // We have on thread here and do not need to notify any waiting producer threads, because we are done of + // We have one thread here and do not need to notify any waiting producer threads, because we are done with // consuming messages from queue. tplLock.unlock(); @@ -2492,9 +2600,8 @@ void TupleBPS::receiveMultiPrimitiveMessages() if (fe2) { rgDatav.push_back(data->joinedData); - processFE2(data->local_outputRG, data->local_fe2Output, data->postJoinRow, - data->local_fe2OutRow, &rgDatav, &data->local_fe2); - + data->processFE2(rgDatav); + if (rgDatav.size() > 0) rgDataToDl(rgDatav[0], data->local_fe2Output, dlp); @@ -2515,9 +2622,8 @@ void TupleBPS::receiveMultiPrimitiveMessages() if (fe2) { rgDatav.push_back(data->joinedData); - processFE2(data->local_outputRG, data->local_fe2Output, data->postJoinRow, - data->local_fe2OutRow, &rgDatav, &data->local_fe2); - + data->processFE2(rgDatav); + if (rgDatav.size() > 0) rgDataToDl(rgDatav[0], data->local_fe2Output, dlp); @@ -2856,52 +2962,6 @@ void TupleBPS::setJoinedResultRG(const rowgroup::RowGroup& rg) fe2Mapping = makeMapping(outputRowGroup, fe2Output); } -/* probably worthwhile to make some of these class vars */ -void TupleBPS::generateJoinResultSet(const vector >& joinerOutput, - Row& baseRow, const vector >& mappings, const uint32_t depth, - RowGroup& outputRG, RGData& rgData, vector* outputData, const scoped_array& smallRows, - Row& joinedRow) -{ - uint32_t i; - Row& smallRow = smallRows[depth]; - - if (depth < smallSideCount - 1) - { - for (i = 0; i < joinerOutput[depth].size(); i++) - { - smallRow.setPointer(joinerOutput[depth][i]); - applyMapping(mappings[depth], smallRow, &baseRow); - generateJoinResultSet(joinerOutput, baseRow, mappings, depth + 1, - outputRG, rgData, outputData, smallRows, joinedRow); - } - } - else - { - outputRG.getRow(outputRG.getRowCount(), &joinedRow); - - for (i = 0; i < joinerOutput[depth].size(); i++, joinedRow.nextRow(), - outputRG.incRowCount()) - { - smallRow.setPointer(joinerOutput[depth][i]); - - if (UNLIKELY(outputRG.getRowCount() == 8192)) - { - uint32_t dbRoot = outputRG.getDBRoot(); - uint64_t baseRid = outputRG.getBaseRid(); - outputData->push_back(rgData); - rgData = RGData(outputRG); - outputRG.setData(&rgData); - outputRG.resetRowGroup(baseRid); - outputRG.setDBRoot(dbRoot); - outputRG.getRow(0, &joinedRow); - } - - applyMapping(mappings[depth], smallRow, &baseRow); - copyRow(baseRow, &joinedRow); - } - } -} - const rowgroup::RowGroup& TupleBPS::getOutputRowGroup() const { return outputRowGroup; @@ -3015,9 +3075,9 @@ void TupleBPS::setFcnExpGroup2(const boost::shared_ptr& fe2Output = rg; checkDupOutputColumns(rg); fe2Mapping = makeMapping(outputRowGroup, fe2Output); - runFEonPM = runFE2onPM; + bRunFEonPM = runFE2onPM; - if (runFEonPM) + if (bRunFEonPM) fBPP->setFEGroup2(fe2, fe2Output); } @@ -3030,7 +3090,7 @@ void TupleBPS::setFcnExpGroup3(const vector& fe) fe2->addReturnedColumn(fe[i]); // if this is called, there's no join, so it can always run on the PM - runFEonPM = true; + bRunFEonPM = true; fBPP->setFEGroup2(fe2, fe2Output); } @@ -3040,95 +3100,10 @@ void TupleBPS::setFE23Output(const rowgroup::RowGroup& feOutput) checkDupOutputColumns(feOutput); fe2Mapping = makeMapping(outputRowGroup, fe2Output); - if (fe2 && runFEonPM) + if (fe2 && bRunFEonPM) fBPP->setFEGroup2(fe2, fe2Output); } -void TupleBPS::processFE2_oneRG(RowGroup& input, RowGroup& output, Row& inRow, - Row& outRow, funcexp::FuncExpWrapper* local_fe) -{ - bool ret; - uint32_t i; - - output.resetRowGroup(input.getBaseRid()); - output.setDBRoot(input.getDBRoot()); - output.getRow(0, &outRow); - input.getRow(0, &inRow); - - for (i = 0; i < input.getRowCount(); i++, inRow.nextRow()) - { - ret = local_fe->evaluate(&inRow); - - if (ret) - { - applyMapping(fe2Mapping, inRow, &outRow); - outRow.setRid(inRow.getRelRid()); - output.incRowCount(); - outRow.nextRow(); - } - } -} - -void TupleBPS::processFE2(RowGroup& input, RowGroup& output, Row& inRow, Row& outRow, - vector* rgData, funcexp::FuncExpWrapper* local_fe) -{ - vector results; - RGData result; - uint32_t i, j; - bool ret; - - result = RGData(output); - output.setData(&result); - output.resetRowGroup(-1); - output.getRow(0, &outRow); - - for (i = 0; i < rgData->size(); i++) - { - input.setData(&(*rgData)[i]); - - if (output.getRowCount() == 0) - { - output.resetRowGroup(input.getBaseRid()); - output.setDBRoot(input.getDBRoot()); - } - - input.getRow(0, &inRow); - - for (j = 0; j < input.getRowCount(); j++, inRow.nextRow()) - { - ret = local_fe->evaluate(&inRow); - - if (ret) - { - applyMapping(fe2Mapping, inRow, &outRow); - outRow.setRid(inRow.getRelRid()); - output.incRowCount(); - outRow.nextRow(); - - if (output.getRowCount() == 8192 || - output.getDBRoot() != input.getDBRoot() || - output.getBaseRid() != input.getBaseRid() - ) - { - results.push_back(result); - result = RGData(output); - output.setData(&result); - output.resetRowGroup(input.getBaseRid()); - output.setDBRoot(input.getDBRoot()); - output.getRow(0, &outRow); - } - } - } - } - - if (output.getRowCount() > 0) - { - results.push_back(result); - } - - rgData->swap(results); -} - const rowgroup::RowGroup& TupleBPS::getDeliveredRowGroup() const { if (fe2) diff --git a/dbcon/joblist/tupleaggregatestep.cpp b/dbcon/joblist/tupleaggregatestep.cpp index 19514b0a0..49b98abb8 100644 --- a/dbcon/joblist/tupleaggregatestep.cpp +++ b/dbcon/joblist/tupleaggregatestep.cpp @@ -5565,13 +5565,10 @@ void TupleAggregateStep::threadedAggregateRowGroups(uint32_t threadID) if (more) { fRowGroupIns[threadID].setData(&rgData); - fMemUsage[threadID] += - fRowGroupIns[threadID].getSizeWithStrings(); bool diskAggAllowed = fRm->getAllowDiskAggregation(); - if (!fRm->getMemory( - fRowGroupIns[threadID].getSizeWithStrings(), - fSessionMemLimit, !diskAggAllowed)) + int64_t memSize = fRowGroupIns[threadID].getSizeWithStrings(); + if (!fRm->getMemory(memSize, fSessionMemLimit, !diskAggAllowed)) { if (!diskAggAllowed) { @@ -5592,6 +5589,7 @@ void TupleAggregateStep::threadedAggregateRowGroups(uint32_t threadID) } break; } + fMemUsage[threadID] += memSize; rgDatas.push_back(rgData); } else diff --git a/dbcon/joblist/tuplehashjoin.cpp b/dbcon/joblist/tuplehashjoin.cpp index 736c0ef55..b18f73b61 100644 --- a/dbcon/joblist/tuplehashjoin.cpp +++ b/dbcon/joblist/tuplehashjoin.cpp @@ -40,7 +40,6 @@ using namespace std; #include "tuplehashjoin.h" #include "calpontsystemcatalog.h" #include "elementcompression.h" -#include "resourcemanager.h" #include "tupleaggregatestep.h" #include "errorids.h" #include "diskjoinstep.h" @@ -76,6 +75,7 @@ TupleHashJoinStep::TupleHashJoinStep(const JobInfo& jobInfo) : fTupleId2(-1), fCorrelatedSide(0), resourceManager(jobInfo.rm), + fMemSizeForOutputRG(0), runRan(false), joinRan(false), largeSideIndex(1), @@ -137,10 +137,12 @@ TupleHashJoinStep::~TupleHashJoinStep() if (memUsedByEachJoin) { for (uint i = 0 ; i < smallDLs.size(); i++) - resourceManager->returnMemory(memUsedByEachJoin[i], sessionMemLimit); + { + if (memUsedByEachJoin[i]) + resourceManager->returnMemory(memUsedByEachJoin[i], sessionMemLimit); + } } - - + returnMemory(); //cout << "deallocated THJS, UM memory available: " << resourceManager.availableMemory() << endl; } @@ -223,11 +225,13 @@ void TupleHashJoinStep::trackMem(uint index) memAfter = joiner->getMemUsage(); if (memAfter != memBefore) { - gotMem = resourceManager->getMemory(memAfter - memBefore, sessionMemLimit, false); - atomicops::atomicAdd(&memUsedByEachJoin[index], memAfter - memBefore); - memBefore = memAfter; - if (!gotMem) + gotMem = resourceManager->getMemory(memAfter - memBefore, sessionMemLimit, true); + if (gotMem) + atomicops::atomicAdd(&memUsedByEachJoin[index], memAfter - memBefore); + else return; + + memBefore = memAfter; } memTrackDone.timed_wait(scoped, boost::posix_time::seconds(1)); } @@ -237,16 +241,21 @@ void TupleHashJoinStep::trackMem(uint index) memAfter = joiner->getMemUsage(); if (memAfter == memBefore) return; - gotMem = resourceManager->getMemory(memAfter - memBefore, sessionMemLimit, false); - atomicops::atomicAdd(&memUsedByEachJoin[index], memAfter - memBefore); - if (!gotMem) + gotMem = resourceManager->getMemory(memAfter - memBefore, sessionMemLimit, true); + if (gotMem) + { + atomicops::atomicAdd(&memUsedByEachJoin[index], memAfter - memBefore); + } + else { if (!joinIsTooBig && (isDML || !allowDJS || (fSessionId & 0x80000000) || (tableOid() < 3000 && tableOid() >= 1000))) { joinIsTooBig = true; - fLogger->logMessage(logging::LOG_TYPE_INFO, logging::ERR_JOIN_TOO_BIG); - errorMessage(logging::IDBErrorInfo::instance()->errorMsg(logging::ERR_JOIN_TOO_BIG)); + ostringstream oss; + oss << "(" << __LINE__ << ") " << logging::IDBErrorInfo::instance()->errorMsg(logging::ERR_JOIN_TOO_BIG); + fLogger->logMessage(logging::LOG_TYPE_INFO, oss.str()); + errorMessage(oss.str()); status(logging::ERR_JOIN_TOO_BIG); cout << "Join is too big, raise the UM join limit for now (monitor thread)" << endl; abort(); @@ -389,7 +398,6 @@ void TupleHashJoinStep::smallRunnerFcn(uint32_t index, uint threadID, uint64_t * smallRG = smallRGs[index]; smallRG.initRow(&r); - try { ssize_t rgSize; @@ -407,9 +415,12 @@ void TupleHashJoinStep::smallRunnerFcn(uint32_t index, uint threadID, uint64_t * utils::releaseSpinlock(rgdLock); rgSize = smallRG.getSizeWithStrings(); - atomicops::atomicAdd(&memUsedByEachJoin[index], rgSize); - gotMem = resourceManager->getMemory(rgSize, sessionMemLimit, false); - if (!gotMem) + gotMem = resourceManager->getMemory(rgSize, sessionMemLimit, true); + if (gotMem) + { + atomicops::atomicAdd(&memUsedByEachJoin[index], rgSize); + } + else { /* Mem went over the limit. If DML or a syscat query, abort. @@ -423,19 +434,20 @@ void TupleHashJoinStep::smallRunnerFcn(uint32_t index, uint threadID, uint64_t * (tableOid() < 3000 && tableOid() >= 1000)) { joinIsTooBig = true; - fLogger->logMessage(logging::LOG_TYPE_INFO, logging::ERR_JOIN_TOO_BIG); - errorMessage(logging::IDBErrorInfo::instance()->errorMsg(logging::ERR_JOIN_TOO_BIG)); + ostringstream oss; + oss << "(" << __LINE__ << ") " << logging::IDBErrorInfo::instance()->errorMsg(logging::ERR_JOIN_TOO_BIG); + fLogger->logMessage(logging::LOG_TYPE_INFO, oss.str()); + errorMessage(oss.str()); status(logging::ERR_JOIN_TOO_BIG); cout << "Join is too big, raise the UM join limit for now (small runner)" << endl; abort(); } else if (allowDJS) joiner->setConvertToDiskJoin(); + return; } - joiner->insertRGData(smallRG, threadID); - if (!joiner->inUM() && (memUsedByEachJoin[index] > pmMemLimit)) { joiner->setInUM(rgData[index]); @@ -457,7 +469,6 @@ next: "TupleHashJoinStep::smallRunnerFcn()"); status(logging::ERR_EXEMGR_MALFUNCTION); } - if (!joiner->inUM()) joiner->setInPM(); } @@ -649,7 +660,7 @@ void TupleHashJoinStep::hjRunner() memUsedByEachJoin.reset(new ssize_t[smallDLs.size()]); for (i = 0; i < smallDLs.size(); i++) - memUsedByEachJoin[i] = 0; + atomicops::atomicZero(&memUsedByEachJoin[i]); try { @@ -747,7 +758,7 @@ void TupleHashJoinStep::hjRunner() { vector empty; resourceManager->returnMemory(memUsedByEachJoin[djsJoinerMap[i]], sessionMemLimit); - memUsedByEachJoin[djsJoinerMap[i]] = 0; + atomicops::atomicZero(&memUsedByEachJoin[i]); djs[i].loadExistingData(rgData[djsJoinerMap[i]]); rgData[djsJoinerMap[i]].swap(empty); } @@ -833,8 +844,10 @@ void TupleHashJoinStep::hjRunner() { if (joinIsTooBig && !status()) { - fLogger->logMessage(logging::LOG_TYPE_INFO, logging::ERR_JOIN_TOO_BIG); - errorMessage(logging::IDBErrorInfo::instance()->errorMsg(logging::ERR_JOIN_TOO_BIG)); + ostringstream oss; + oss << "(" << __LINE__ << ") " << logging::IDBErrorInfo::instance()->errorMsg(logging::ERR_JOIN_TOO_BIG); + fLogger->logMessage(logging::LOG_TYPE_INFO, oss.str()); + errorMessage(oss.str()); status(logging::ERR_JOIN_TOO_BIG); cout << "Join is too big, raise the UM join limit for now" << endl; @@ -847,7 +860,7 @@ void TupleHashJoinStep::hjRunner() for (uint i = 0; i < smallDLs.size(); i++) { resourceManager->returnMemory(memUsedByEachJoin[i], sessionMemLimit); - memUsedByEachJoin[i] = 0; + atomicops::atomicZero(&memUsedByEachJoin[i]); } } } @@ -1027,7 +1040,7 @@ uint32_t TupleHashJoinStep::nextBand(messageqcpp::ByteStream& bs) for (uint i = 0; i < smallDLs.size(); i++) { resourceManager->returnMemory(memUsedByEachJoin[i], sessionMemLimit); - memUsedByEachJoin[i] = 0; + atomicops::atomicZero(&memUsedByEachJoin[i]); } return 0; } @@ -1051,7 +1064,7 @@ uint32_t TupleHashJoinStep::nextBand(messageqcpp::ByteStream& bs) for (uint i = 0; i < smallDLs.size(); i++) { resourceManager->returnMemory(memUsedByEachJoin[i], sessionMemLimit); - memUsedByEachJoin[i] = 0; + atomicops::atomicZero(&memUsedByEachJoin[i]); } return 0; } @@ -1549,8 +1562,8 @@ void TupleHashJoinStep::joinRunnerFcn(uint32_t threadID) if (local_inputRG.getRowCount() == 0) continue; - joinOneRG(threadID, &joinedRowData, local_inputRG, local_outputRG, largeRow, - joinFERow, joinedRow, baseRow, joinMatches, smallRowTemplates); + joinOneRG(threadID, joinedRowData, local_inputRG, local_outputRG, largeRow, + joinFERow, joinedRow, baseRow, joinMatches, smallRowTemplates, outputDL); } if (fe2) @@ -1558,6 +1571,7 @@ void TupleHashJoinStep::joinRunnerFcn(uint32_t threadID) processDupList(threadID, (fe2 ? local_fe2RG : local_outputRG), &joinedRowData); sendResult(joinedRowData); + returnMemory(); joinedRowData.clear(); grabSomeWork(&inputData); } @@ -1689,10 +1703,11 @@ void TupleHashJoinStep::grabSomeWork(vector* work) /* This function is a port of the main join loop in TupleBPS::receiveMultiPrimitiveMessages(). Any * changes made here should also be made there and vice versa. */ -void TupleHashJoinStep::joinOneRG(uint32_t threadID, vector* out, +void TupleHashJoinStep::joinOneRG(uint32_t threadID, vector& out, RowGroup& inputRG, RowGroup& joinOutput, Row& largeSideRow, Row& joinFERow, Row& joinedRow, Row& baseRow, vector >& joinMatches, shared_array& smallRowTemplates, + RowGroupDL* outputDL, // disk-join support vars. This param list is insane; refactor attempt would be nice at some point. vector >* tjoiners, boost::shared_array >* rgMappings, @@ -1700,7 +1715,6 @@ void TupleHashJoinStep::joinOneRG(uint32_t threadID, vector* out, boost::scoped_array >* smallNullMem ) { - /* Disk-join support. These dissociate the fcn from THJS's members & allow this fcn to be called from DiskJoinStep */ @@ -1821,19 +1835,19 @@ void TupleHashJoinStep::joinOneRG(uint32_t threadID, vector* out, applyMapping((*rgMappings)[smallSideCount], largeSideRow, &baseRow); baseRow.setRid(largeSideRow.getRelRid()); generateJoinResultSet(joinMatches, baseRow, *rgMappings, - 0, joinOutput, joinedData, out, smallRowTemplates, joinedRow); + 0, joinOutput, joinedData, out, smallRowTemplates, joinedRow, outputDL); } } if (joinOutput.getRowCount() > 0) - out->push_back(joinedData); + out.push_back(joinedData); } void TupleHashJoinStep::generateJoinResultSet(const vector >& joinerOutput, Row& baseRow, const shared_array >& mappings, const uint32_t depth, RowGroup& l_outputRG, RGData& rgData, - vector* outputData, const shared_array& smallRows, - Row& joinedRow) + vector& outputData, const shared_array& smallRows, + Row& joinedRow, RowGroupDL* dlp) { uint32_t i; Row& smallRow = smallRows[depth]; @@ -1845,9 +1859,8 @@ void TupleHashJoinStep::generateJoinResultSet(const vector { smallRow.setPointer(joinerOutput[depth][i]); applyMapping(mappings[depth], smallRow, &baseRow); -// cout << "depth " << depth << ", size " << joinerOutput[depth].size() << ", row " << i << ": " << smallRow.toString() << endl; generateJoinResultSet(joinerOutput, baseRow, mappings, depth + 1, - l_outputRG, rgData, outputData, smallRows, joinedRow); + l_outputRG, rgData, outputData, smallRows, joinedRow, dlp); } } else @@ -1863,7 +1876,15 @@ void TupleHashJoinStep::generateJoinResultSet(const vector { uint32_t dbRoot = l_outputRG.getDBRoot(); uint64_t baseRid = l_outputRG.getBaseRid(); - outputData->push_back(rgData); + outputData.push_back(rgData); + // Count the memory + if (UNLIKELY(!getMemory(l_outputRG.getMaxDataSize()))) + { + // Don't let the join results buffer get out of control. + sendResult(outputData); + outputData.clear(); + returnMemory(); + } rgData.reinit(l_outputRG); l_outputRG.setData(&rgData); l_outputRG.resetRowGroup(baseRid); @@ -1871,11 +1892,8 @@ void TupleHashJoinStep::generateJoinResultSet(const vector l_outputRG.getRow(0, &joinedRow); } -// cout << "depth " << depth << ", size " << joinerOutput[depth].size() << ", row " << i << ": " << smallRow.toString() << endl; applyMapping(mappings[depth], smallRow, &baseRow); copyRow(baseRow, &joinedRow); - //memcpy(joinedRow.getData(), baseRow.getData(), joinedRow.getSize()); - //cout << "(step " << stepID << ") fully joined row is: " << joinedRow.toString() << endl; } } } diff --git a/dbcon/joblist/tuplehashjoin.h b/dbcon/joblist/tuplehashjoin.h index 884644eba..d6107138f 100644 --- a/dbcon/joblist/tuplehashjoin.h +++ b/dbcon/joblist/tuplehashjoin.h @@ -32,11 +32,12 @@ #include #include #include +#include "resourcemanager.h" +#include "exceptclasses.h" namespace joblist { class BatchPrimitive; -class ResourceManager; class TupleBPS; struct FunctionJoinInfo; class DiskJoinStep; @@ -371,6 +372,21 @@ public: } void abort(); + void returnMemory() + { + if (fMemSizeForOutputRG > 0) + { + resourceManager->returnMemory(fMemSizeForOutputRG); + fMemSizeForOutputRG = 0; + } + } + bool getMemory(uint64_t memSize) + { + bool gotMem = resourceManager->getMemory(memSize); + if (gotMem) + fMemSizeForOutputRG += memSize; + return gotMem; + } private: TupleHashJoinStep(); TupleHashJoinStep(const TupleHashJoinStep&); @@ -427,7 +443,8 @@ private: std::vector > smallSideKeys; ResourceManager* resourceManager; - + uint64_t fMemSizeForOutputRG; + struct JoinerSorter { inline bool operator()(const boost::shared_ptr& j1, @@ -519,18 +536,20 @@ private: void generateJoinResultSet(const std::vector >& joinerOutput, rowgroup::Row& baseRow, const boost::shared_array >& mappings, const uint32_t depth, rowgroup::RowGroup& outputRG, rowgroup::RGData& rgData, - std::vector* outputData, - const boost::shared_array& smallRows, rowgroup::Row& joinedRow); + std::vector& outputData, + const boost::shared_array& smallRows, rowgroup::Row& joinedRow, + RowGroupDL* outputDL); void grabSomeWork(std::vector* work); void sendResult(const std::vector& res); void processFE2(rowgroup::RowGroup& input, rowgroup::RowGroup& output, rowgroup::Row& inRow, rowgroup::Row& outRow, std::vector* rgData, funcexp::FuncExpWrapper* local_fe); - void joinOneRG(uint32_t threadID, std::vector* out, + void joinOneRG(uint32_t threadID, std::vector& out, rowgroup::RowGroup& inputRG, rowgroup::RowGroup& joinOutput, rowgroup::Row& largeSideRow, rowgroup::Row& joinFERow, rowgroup::Row& joinedRow, rowgroup::Row& baseRow, std::vector >& joinMatches, boost::shared_array& smallRowTemplates, + RowGroupDL* outputDL, std::vector >* joiners = NULL, boost::shared_array >* rgMappings = NULL, boost::shared_array >* feMappings = NULL, @@ -575,6 +594,7 @@ private: DJSReader(TupleHashJoinStep* hj, uint32_t i) : HJ(hj), index(i) { } void operator()() { + utils::setThreadName("DJSReader"); HJ->djsReaderFcn(index); } TupleHashJoinStep* HJ; diff --git a/dbcon/joblist/tupleunion.cpp b/dbcon/joblist/tupleunion.cpp index f6a64ef0e..73fe936ba 100644 --- a/dbcon/joblist/tupleunion.cpp +++ b/dbcon/joblist/tupleunion.cpp @@ -245,10 +245,13 @@ void TupleUnion::readInput(uint32_t which) memUsageAfter = allocator.getMemUsage(); memDiff += (memUsageAfter - memUsageBefore); - memUsage += memDiff; } - if (!rm->getMemory(memDiff, sessionMemLimit)) + if (rm->getMemory(memDiff, sessionMemLimit)) + { + memUsage += memDiff; + } + else { fLogger->logMessage(logging::LOG_TYPE_INFO, logging::ERR_UNION_TOO_BIG); diff --git a/dbcon/joblist/windowfunctionstep.cpp b/dbcon/joblist/windowfunctionstep.cpp index ce47e25c0..3aabe6663 100755 --- a/dbcon/joblist/windowfunctionstep.cpp +++ b/dbcon/joblist/windowfunctionstep.cpp @@ -919,11 +919,12 @@ void WindowFunctionStep::execute() { fInRowGroupData.push_back(rgData); uint64_t memAdd = fRowGroupIn.getSizeWithStrings() + rowCnt * sizeof(RowPosition); - fMemUsage += memAdd; if (fRm->getMemory(memAdd, fSessionMemLimit) == false) throw IDBExcept(ERR_WF_DATA_SET_TOO_BIG); - + + fMemUsage += memAdd; + for (uint64_t j = 0; j < rowCnt; ++j) { if (i > 0x0000FFFFFFFFFFFFULL || j > 0x000000000000FFFFULL) @@ -1054,11 +1055,12 @@ void WindowFunctionStep::doFunction() while (((i = nextFunctionIndex()) < fFunctionCount) && !cancelled()) { uint64_t memAdd = fRows.size() * sizeof(RowPosition); - fMemUsage += memAdd; if (fRm->getMemory(memAdd, fSessionMemLimit) == false) throw IDBExcept(ERR_WF_DATA_SET_TOO_BIG); + fMemUsage += memAdd; + fFunctions[i]->setCallback(this, i); (*fFunctions[i].get())(); } diff --git a/dbcon/mysql/ha_mcs_dml.cpp b/dbcon/mysql/ha_mcs_dml.cpp index 7cdb55d0e..0524447c6 100644 --- a/dbcon/mysql/ha_mcs_dml.cpp +++ b/dbcon/mysql/ha_mcs_dml.cpp @@ -755,7 +755,7 @@ int ha_mcs_impl_write_batch_row_(const uchar* buf, TABLE* table, cal_impl_if::ca } } - rc = fprintf(ci.filePtr, "\n"); //@bug 6077 check whether thhe pipe is still open + rc = fprintf(ci.filePtr, "\n"); //@bug 6077 check whether the pipe is still open if ( rc < 0) rc = -1; diff --git a/exemgr/femsghandler.cpp b/exemgr/femsghandler.cpp index 10d1c772a..89b6fbddf 100644 --- a/exemgr/femsghandler.cpp +++ b/exemgr/femsghandler.cpp @@ -19,6 +19,7 @@ #include "iosocket.h" #include "femsghandler.h" +#include "threadnaming.h" using namespace std; using namespace joblist; @@ -35,6 +36,7 @@ public: Runner(FEMsgHandler* f) : target(f) { } void operator()() { + utils::setThreadName("FEMsgHandler"); target->threadFcn(); } FEMsgHandler* target; diff --git a/exemgr/main.cpp b/exemgr/main.cpp index 55b240963..87cb102a4 100644 --- a/exemgr/main.cpp +++ b/exemgr/main.cpp @@ -79,6 +79,7 @@ #include "mariadb_my_sys.h" #include "statistics.h" +#include "threadnaming.h" class Opt { @@ -640,8 +641,6 @@ private: if (jl->status() == 0) { - std::string emsg; - if (jl->putEngineComm(fEc) != 0) throw std::runtime_error(jl->errMsg()); } @@ -738,6 +737,7 @@ private: void operator()() { + utils::setThreadName("SessionThread"); messageqcpp::ByteStream bs, inbs; execplan::CalpontSelectExecutionPlan csep; csep.sessionID(0); diff --git a/primitives/blockcache/iomanager.cpp b/primitives/blockcache/iomanager.cpp index 70c7349bb..c6d8f445a 100644 --- a/primitives/blockcache/iomanager.cpp +++ b/primitives/blockcache/iomanager.cpp @@ -104,6 +104,7 @@ using namespace compress; using namespace idbdatafile; #include "mcsconfig.h" +#include "threadnaming.h" typedef tr1::unordered_set USOID; @@ -394,6 +395,7 @@ static int updateptrs(char* ptr, FdCacheType_t::iterator fdit) void* thr_popper(ioManager* arg) { + utils::setThreadName("thr_popper"); ioManager* iom = arg; FileBufferMgr* fbm; int totalRqst = 0; diff --git a/primitives/primproc/batchprimitiveprocessor.cpp b/primitives/primproc/batchprimitiveprocessor.cpp index b384f2318..2feb62787 100644 --- a/primitives/primproc/batchprimitiveprocessor.cpp +++ b/primitives/primproc/batchprimitiveprocessor.cpp @@ -1151,17 +1151,24 @@ void BatchPrimitiveProcessor::initProcessor() } /* This version does a join on projected rows */ -void BatchPrimitiveProcessor::executeTupleJoin() +// In order to prevent super size result sets in the case of near cartesian joins on three or more joins, +// the startRid start at 0) is used to begin the rid loop and if we cut off processing early because of +// the size of the result set, we return the next rid to start with. If we finish ridCount rids, return 0- +uint32_t BatchPrimitiveProcessor::executeTupleJoin(uint32_t startRid) { uint32_t newRowCount = 0, i, j; vector matches; uint64_t largeKey; - + uint64_t resultCount = 0; + uint32_t newStartRid = startRid; outputRG.getRow(0, &oldRow); outputRG.getRow(0, &newRow); //cout << "before join, RG has " << outputRG.getRowCount() << " BPP ridcount= " << ridCount << endl; - for (i = 0; i < ridCount && !sendThread->aborted(); i++, oldRow.nextRow()) + // ridCount gets modified based on the number of Rids actually processed during this call. + // origRidCount is the number of rids for this thread after filter, which are the total + // number of rids to be processed from all calls to this function during this thread. + for (i = startRid; i < origRidCount && !sendThread->aborted(); i++, oldRow.nextRow()) { /* Decide whether this large-side row belongs in the output. The breaks * in the loop mean that it doesn't. @@ -1270,10 +1277,9 @@ void BatchPrimitiveProcessor::executeTupleJoin() if (j == joinerCount) { + uint32_t matchCount; for (j = 0; j < joinerCount; j++) { - uint32_t matchCount; - /* The result is already known if... * -- anti-join with no fcnexp * -- semi-join with no fcnexp and not scalar @@ -1361,6 +1367,8 @@ void BatchPrimitiveProcessor::executeTupleJoin() tSmallSideMatches[j][newRowCount].push_back(-1); matchCount = 1; } + + resultCount += matchCount; } /* Finally, copy the row into the output */ @@ -1384,8 +1392,18 @@ void BatchPrimitiveProcessor::executeTupleJoin() //else // cout << "j != joinerCount\n"; } + // If we've accumulated more than maxResultCount -- 1048576 (2^20)_ of resultCounts, cut off processing. The caller will + // restart to continue where we left off. + if (resultCount >= maxResultCount) + { + newStartRid += newRowCount; + break; + } } + if (resultCount < maxResultCount) + newStartRid = 0; + ridCount = newRowCount; outputRG.setRowCount(ridCount); @@ -1404,6 +1422,7 @@ void BatchPrimitiveProcessor::executeTupleJoin() } } */ + return newStartRid; } #ifdef PRIMPROC_STOPWATCH @@ -1412,6 +1431,9 @@ void BatchPrimitiveProcessor::execute(StopWatch* stopwatch) void BatchPrimitiveProcessor::execute() #endif { + uint8_t sendCount = 0; +// bool smoreRGs = false; +// uint32_t sStartRid = 0; uint32_t i, j; try @@ -1616,9 +1638,9 @@ void BatchPrimitiveProcessor::execute() } /* 7/7/09 PL: I Changed the projection alg to reduce block touches when there's - a join. The key columns get projected first, the join is executed to further + a join. The key columns get projected first, the join is executed to further reduce the ridlist, then the rest of the columns get projected */ - + if (!doJoin) { for (j = 0; j < projectCount; ++j) @@ -1638,15 +1660,92 @@ void BatchPrimitiveProcessor::execute() // else // cout << " no target found for OID " << projectSteps[j]->getOID() << endl; } + if (fe2) + { + /* functionize this -> processFE2() */ + fe2Output.resetRowGroup ( baseRid ); + fe2Output.getRow ( 0, &fe2Out ); + fe2Input->getRow ( 0, &fe2In ); + + //cerr << "input row: " << fe2In.toString() << endl; + for ( j = 0; j < outputRG.getRowCount(); j++, fe2In.nextRow() ) + { + if ( fe2->evaluate ( &fe2In ) ) + { + applyMapping ( fe2Mapping, fe2In, &fe2Out ); + //cerr << " passed. output row: " << fe2Out.toString() << endl; + fe2Out.setRid ( fe2In.getRelRid() ); + fe2Output.incRowCount(); + fe2Out.nextRow(); + } + } + + if ( !fAggregator ) + { + *serialized << ( uint8_t ) 1; // the "count this msg" var + fe2Output.setDBRoot ( dbRoot ); + fe2Output.serializeRGData ( *serialized ); + //*serialized << fe2Output.getDataSize(); + //serialized->append(fe2Output.getData(), fe2Output.getDataSize()); + } + } + + if (fAggregator) + { + *serialized << ( uint8_t ) 1; // the "count this msg" var + + RowGroup& toAggregate = ( fe2 ? fe2Output : outputRG ); + //toAggregate.convertToInlineDataInPlace(); + + if ( fe2 ) + fe2Output.setDBRoot ( dbRoot ); + else + outputRG.setDBRoot ( dbRoot ); + + fAggregator->addRowGroup ( &toAggregate ); + + if ( ( currentBlockOffset + 1 ) == count ) // @bug4507, 8k + { + fAggregator->loadResult ( *serialized ); // @bug4507, 8k + } // @bug4507, 8k + else if ( utils::MonitorProcMem::isMemAvailable() ) // @bug4507, 8k + { + fAggregator->loadEmptySet ( *serialized ); // @bug4507, 8k + } // @bug4507, 8k + else // @bug4507, 8k + { + fAggregator->loadResult ( *serialized ); // @bug4507, 8k + fAggregator->aggReset(); // @bug4507, 8k + } // @bug4507, 8k + } + + if (!fAggregator && !fe2) + { + *serialized << ( uint8_t ) 1; // the "count this msg" var + outputRG.setDBRoot ( dbRoot ); + //cerr << "serializing " << outputRG.toString() << endl; + outputRG.serializeRGData ( *serialized ); + + //*serialized << outputRG.getDataSize(); + //serialized->append(outputRG.getData(), outputRG.getDataSize()); + } + +#ifdef PRIMPROC_STOPWATCH + stopwatch->stop ( "- if(ot != ROW_GROUP) else" ); +#endif } - else + else // Is doJoin { + uint32_t startRid = 0; + ByteStream preamble = *serialized; + origRidCount = ridCount; // ridCount can get modified by executeTupleJoin(). We need to keep track of the original val. /* project the key columns. If there's the filter IN the join, project everything. Also need to project 'long' strings b/c executeTupleJoin may copy entire rows using copyRow(), which will try to interpret the uninit'd string ptr. Valgrind will legitimately complain about copying uninit'd values for the other types but that is technically safe. */ for (j = 0; j < projectCount; j++) + { if (keyColumnProj[j] || (projectionMap[j] != -1 && (hasJoinFEFilters || oldRow.isLongString(projectionMap[j])))) { @@ -1656,218 +1755,174 @@ void BatchPrimitiveProcessor::execute() stopwatch->stop("-- projectIntoRowGroup"); #else projectSteps[j]->projectIntoRowGroup(outputRG, projectionMap[j]); -#endif - } - - -#ifdef PRIMPROC_STOPWATCH - stopwatch->start("-- executeTupleJoin()"); - executeTupleJoin(); - stopwatch->stop("-- executeTupleJoin()"); -#else - executeTupleJoin(); -#endif - - /* project the non-key columns */ - for (j = 0; j < projectCount; ++j) - { - if (projectionMap[j] != -1 && !keyColumnProj[j] && !hasJoinFEFilters && - !oldRow.isLongString(projectionMap[j])) - { -#ifdef PRIMPROC_STOPWATCH - stopwatch->start("-- projectIntoRowGroup"); - projectSteps[j]->projectIntoRowGroup(outputRG, projectionMap[j]); - stopwatch->stop("-- projectIntoRowGroup"); -#else - projectSteps[j]->projectIntoRowGroup(outputRG, projectionMap[j]); #endif } } - } - /* The RowGroup is fully joined at this point. - Add additional RowGroup processing here. - TODO: Try to clean up all of the switching */ - - if (doJoin && (fe2 || fAggregator)) - { - bool moreRGs = true; - ByteStream preamble = *serialized; - initGJRG(); - - while (moreRGs && !sendThread->aborted()) + do //while (startRid > 0) { - /* - generate 1 rowgroup (8192 rows max) of joined rows - if there's an FE2, run it - -pack results into a new rowgroup - -if there are < 8192 rows in the new RG, continue - if there's an agg, run it - send the result - */ - resetGJRG(); - moreRGs = generateJoinedRowGroup(baseJRow); - *serialized << (uint8_t) !moreRGs; - - if (fe2) +#ifdef PRIMPROC_STOPWATCH + stopwatch->start("-- executeTupleJoin()"); + startRid = executeTupleJoin(startRid); + stopwatch->stop("-- executeTupleJoin()"); +#else + startRid = executeTupleJoin(startRid); +// sStartRid = startRid; +#endif + /* project the non-key columns */ + for (j = 0; j < projectCount; ++j) { - /* functionize this -> processFE2()*/ - fe2Output.resetRowGroup(baseRid); - fe2Output.setDBRoot(dbRoot); - fe2Output.getRow(0, &fe2Out); - fe2Input->getRow(0, &fe2In); - - for (j = 0; j < joinedRG.getRowCount(); j++, fe2In.nextRow()) - if (fe2->evaluate(&fe2In)) - { - applyMapping(fe2Mapping, fe2In, &fe2Out); - fe2Out.setRid(fe2In.getRelRid()); - fe2Output.incRowCount(); - fe2Out.nextRow(); - } + if (projectionMap[j] != -1 && !keyColumnProj[j] && !hasJoinFEFilters && + !oldRow.isLongString(projectionMap[j])) + { +#ifdef PRIMPROC_STOPWATCH + stopwatch->start("-- projectIntoRowGroup"); + projectSteps[j]->projectIntoRowGroup(outputRG, projectionMap[j]); + stopwatch->stop("-- projectIntoRowGroup"); +#else + projectSteps[j]->projectIntoRowGroup(outputRG, projectionMap[j]); +#endif + } } + /* The RowGroup is fully joined at this point. + * Add additional RowGroup processing here. + * TODO: Try to clean up all of the switching */ - RowGroup& nextRG = (fe2 ? fe2Output : joinedRG); - nextRG.setDBRoot(dbRoot); - - if (fAggregator) + if (fe2 || fAggregator) { - fAggregator->addRowGroup(&nextRG); + bool moreRGs = true; + initGJRG(); - if ((currentBlockOffset + 1) == count && moreRGs == false) // @bug4507, 8k + while (moreRGs && !sendThread->aborted()) { - fAggregator->loadResult(*serialized); // @bug4507, 8k - } // @bug4507, 8k - else if (utils::MonitorProcMem::isMemAvailable()) // @bug4507, 8k + /* + * generate 1 rowgroup (8192 rows max) of joined rows + * if there's an FE2, run it + * -pack results into a new rowgroup + * -if there are < 8192 rows in the new RG, continue + * if there's an agg, run it + * send the result + */ + resetGJRG(); + moreRGs = generateJoinedRowGroup(baseJRow); +// smoreRGs = moreRGs; + sendCount = (uint8_t)(!moreRGs && !startRid); +// *serialized << (uint8_t)(!moreRGs && !startRid); // the "count this msg" var + *serialized << sendCount; + if (fe2) + { + /* functionize this -> processFE2()*/ + fe2Output.resetRowGroup(baseRid); + fe2Output.setDBRoot(dbRoot); + fe2Output.getRow(0, &fe2Out); + fe2Input->getRow(0, &fe2In); + + for (j = 0; j < joinedRG.getRowCount(); j++, fe2In.nextRow()) + if (fe2->evaluate(&fe2In)) + { + applyMapping(fe2Mapping, fe2In, &fe2Out); + fe2Out.setRid(fe2In.getRelRid()); + fe2Output.incRowCount(); + fe2Out.nextRow(); + } + } + + RowGroup& nextRG = (fe2 ? fe2Output : joinedRG); + nextRG.setDBRoot(dbRoot); + + if (fAggregator) + { + fAggregator->addRowGroup(&nextRG); + + if ((currentBlockOffset + 1) == count && moreRGs == false && startRid == 0) // @bug4507, 8k + { + fAggregator->loadResult(*serialized); // @bug4507, 8k + } // @bug4507, 8k + else if (utils::MonitorProcMem::isMemAvailable()) // @bug4507, 8k + { + fAggregator->loadEmptySet(*serialized); // @bug4507, 8k + } // @bug4507, 8k + else // @bug4507, 8k + { + fAggregator->loadResult(*serialized); // @bug4507, 8k + fAggregator->aggReset(); // @bug4507, 8k + } // @bug4507, 8k + } + else + { + //cerr <<" * serialzing " << nextRG.toString() << endl; + nextRG.serializeRGData(*serialized); + } + + /* send the msg & reinit the BS */ + if (moreRGs) + { + sendResponse(); + serialized.reset(new ByteStream()); + *serialized = preamble; + } + } + + if ( hasSmallOuterJoin ) { - fAggregator->loadEmptySet(*serialized); // @bug4507, 8k - } // @bug4507, 8k - else // @bug4507, 8k + // Should we happen to finish sending data rows right on the boundary of when moreRGs flips off, then + // we need to start a new buffer. I.e., it needs the count this message byte pushed. + if (serialized->length() == preamble.length()) + *serialized << ( uint8_t )(startRid > 0 ? 0 : 1); // the "count this msg" var + + *serialized << ridCount; + + for ( i = 0; i < joinerCount; i++ ) + { + for ( j = 0; j < ridCount; ++j ) + { + serializeInlineVector ( *serialized, + tSmallSideMatches[i][j] ); + tSmallSideMatches[i][j].clear(); + } + } + } + else { - fAggregator->loadResult(*serialized); // @bug4507, 8k - fAggregator->aggReset(); // @bug4507, 8k - } // @bug4507, 8k + // We hae no more use for this allocation + for ( i = 0; i < joinerCount; i++ ) + for ( j = 0; j < ridCount; ++j ) + tSmallSideMatches[i][j].clear(); + } } else { - //cerr <<" * serialzing " << nextRG.toString() << endl; - nextRG.serializeRGData(*serialized); - } + *serialized << ( uint8_t )(startRid > 0 ? 0 : 1); // the "count this msg" var + outputRG.setDBRoot ( dbRoot ); + //cerr << "serializing " << outputRG.toString() << endl; + outputRG.serializeRGData ( *serialized ); - /* send the msg & reinit the BS */ - if (moreRGs) - { - sendResponse(); - serialized.reset(new ByteStream()); - *serialized = preamble; - } - } - - if (hasSmallOuterJoin) - { - *serialized << ridCount; - - for (i = 0; i < joinerCount; i++) - for (j = 0; j < ridCount; ++j) - serializeInlineVector(*serialized, - tSmallSideMatches[i][j]); - } - } - - if (!doJoin && fe2) - { - /* functionize this -> processFE2() */ - fe2Output.resetRowGroup(baseRid); - fe2Output.getRow(0, &fe2Out); - fe2Input->getRow(0, &fe2In); - - //cerr << "input row: " << fe2In.toString() << endl; - for (j = 0; j < outputRG.getRowCount(); j++, fe2In.nextRow()) - { - if (fe2->evaluate(&fe2In)) - { - applyMapping(fe2Mapping, fe2In, &fe2Out); - //cerr << " passed. output row: " << fe2Out.toString() << endl; - fe2Out.setRid (fe2In.getRelRid()); - fe2Output.incRowCount(); - fe2Out.nextRow(); - } - } - - if (!fAggregator) - { - *serialized << (uint8_t) 1; // the "count this msg" var - fe2Output.setDBRoot(dbRoot); - fe2Output.serializeRGData(*serialized); - //*serialized << fe2Output.getDataSize(); - //serialized->append(fe2Output.getData(), fe2Output.getDataSize()); - } - } - - if (!doJoin && fAggregator) - { - *serialized << (uint8_t) 1; // the "count this msg" var - - RowGroup& toAggregate = (fe2 ? fe2Output : outputRG); - //toAggregate.convertToInlineDataInPlace(); - - if (fe2) - fe2Output.setDBRoot(dbRoot); - else - outputRG.setDBRoot(dbRoot); - - fAggregator->addRowGroup(&toAggregate); - - if ((currentBlockOffset + 1) == count) // @bug4507, 8k - { - fAggregator->loadResult(*serialized); // @bug4507, 8k - } // @bug4507, 8k - else if (utils::MonitorProcMem::isMemAvailable()) // @bug4507, 8k - { - fAggregator->loadEmptySet(*serialized); // @bug4507, 8k - } // @bug4507, 8k - else // @bug4507, 8k - { - fAggregator->loadResult(*serialized); // @bug4507, 8k - fAggregator->aggReset(); // @bug4507, 8k - } // @bug4507, 8k - } - - if (!fAggregator && !fe2) - { - *serialized << (uint8_t) 1; // the "count this msg" var - outputRG.setDBRoot(dbRoot); - //cerr << "serializing " << outputRG.toString() << endl; - outputRG.serializeRGData(*serialized); - - //*serialized << outputRG.getDataSize(); - //serialized->append(outputRG.getData(), outputRG.getDataSize()); - if (doJoin) - { - for (i = 0; i < joinerCount; i++) - { - for (j = 0; j < ridCount; ++j) + //*serialized << outputRG.getDataSize(); + //serialized->append(outputRG.getData(), outputRG.getDataSize()); + for ( i = 0; i < joinerCount; i++ ) { - serializeInlineVector(*serialized, - tSmallSideMatches[i][j]); + for ( j = 0; j < ridCount; ++j ) + { + serializeInlineVector ( *serialized, + tSmallSideMatches[i][j] ); + tSmallSideMatches[i][j].clear(); + } } } - } + if (startRid > 0) + { + sendResponse(); + serialized.reset ( new ByteStream() ); + *serialized = preamble; + } + }while (startRid > 0); } - - // clear small side match vector - if (doJoin) - { - for (i = 0; i < joinerCount; i++) - for (j = 0; j < ridCount; ++j) - tSmallSideMatches[i][j].clear(); - } - #ifdef PRIMPROC_STOPWATCH stopwatch->stop("- if(ot != ROW_GROUP) else"); #endif } - + ridCount = origRidCount; // May not be needed, but just to be safe. +// std::cout << "end of send. startRid=" << sStartRid << " moreRG=" << smoreRGs << " sendCount=" << sendCount << std::endl; if (projectCount > 0 || ot == ROW_GROUP) { *serialized << cachedIO; @@ -2215,8 +2270,9 @@ int BatchPrimitiveProcessor::operator()() if (sendThread->aborted()) break; - if (!sendThread->okToProceed()) + if (sendThread->sizeTooBig()) { + // The send buffer is full of messages yet to be sent, so this thread would block anyway. freeLargeBuffers(); return -1; // the reschedule error code } diff --git a/primitives/primproc/batchprimitiveprocessor.h b/primitives/primproc/batchprimitiveprocessor.h index 916c9dc77..97673efd8 100644 --- a/primitives/primproc/batchprimitiveprocessor.h +++ b/primitives/primproc/batchprimitiveprocessor.h @@ -220,6 +220,7 @@ private: int128_t wide128Values[LOGICAL_BLOCK_RIDS]; boost::scoped_array absRids; boost::scoped_array strValues; + uint16_t origRidCount; uint16_t ridCount; bool needStrValues; uint16_t wideColumnsWidths; @@ -328,7 +329,7 @@ private: boost::shared_array > > tJoiners; typedef std::vector MatchedData[LOGICAL_BLOCK_RIDS]; boost::shared_array tSmallSideMatches; - void executeTupleJoin(); + uint32_t executeTupleJoin(uint32_t startRid); bool getTupleJoinRowGroupData; std::vector smallSideRGs; rowgroup::RowGroup largeSideRG; @@ -425,6 +426,8 @@ private: uint processorThreads; uint ptMask; bool firstInstance; + + static const uint64_t maxResultCount = 1048576; // 2^20 friend class Command; friend class ColumnCommand; diff --git a/primitives/primproc/bppsendthread.cpp b/primitives/primproc/bppsendthread.cpp index 75ab27c38..2ebccae23 100644 --- a/primitives/primproc/bppsendthread.cpp +++ b/primitives/primproc/bppsendthread.cpp @@ -23,59 +23,56 @@ #include #include +#include #include "bppsendthread.h" -using namespace std; -using namespace boost; - -#include "atomicops.h" - namespace primitiveprocessor { - extern uint32_t connectionsPerUM; +extern uint32_t BPPCount; BPPSendThread::BPPSendThread() : die(false), gotException(false), mainThreadWaiting(false), sizeThreshold(100), msgsLeft(-1), waiting(false), sawAllConnections(false), - fcEnabled(false), currentByteSize(0), maxByteSize(25000000) + fcEnabled(false), currentByteSize(0) { + maxByteSize = joblist::ResourceManager::instance()->getMaxBPPSendQueue(); runner = boost::thread(Runner_t(this)); } BPPSendThread::BPPSendThread(uint32_t initMsgsLeft) : die(false), gotException(false), mainThreadWaiting(false), sizeThreshold(100), msgsLeft(initMsgsLeft), waiting(false), - sawAllConnections(false), fcEnabled(false), currentByteSize(0), maxByteSize(25000000) + sawAllConnections(false), fcEnabled(false), currentByteSize(0) { + maxByteSize = joblist::ResourceManager::instance()->getMaxBPPSendQueue(); runner = boost::thread(Runner_t(this)); } BPPSendThread::~BPPSendThread() { - boost::mutex::scoped_lock sl(msgQueueLock); - boost::mutex::scoped_lock sl2(ackLock); - die = true; - queueNotEmpty.notify_one(); - okToSend.notify_one(); - sl.unlock(); - sl2.unlock(); + abort(); runner.join(); } -bool BPPSendThread::okToProceed() -{ - // keep the queue size below the 100 msg threshold & below the 25MB mark, - // but at least 2 msgs so there is always 1 ready to be sent. - return ((msgQueue.size() < sizeThreshold && currentByteSize < maxByteSize) - || msgQueue.size() < 3) && !die; -} - void BPPSendThread::sendResult(const Msg_t& msg, bool newConnection) { + // Wait for the queue to empty out a bit if it's stuffed full + if (sizeTooBig()) + { + std::unique_lock sl1(respondLock); + while (currentByteSize >= maxByteSize && msgQueue.size() > 3 && !die) + { + respondWait = true; + fProcessorPool->incBlockedThreads(); + okToRespond.wait(sl1); + fProcessorPool->decBlockedThreads(); + respondWait = false; + } + } if (die) return; - - boost::mutex::scoped_lock sl(msgQueueLock); - + + std::unique_lock sl(msgQueueLock); + if (gotException) throw runtime_error(exceptionString); @@ -105,11 +102,24 @@ void BPPSendThread::sendResult(const Msg_t& msg, bool newConnection) void BPPSendThread::sendResults(const vector& msgs, bool newConnection) { + // Wait for the queue to empty out a bit if it's stuffed full + if (sizeTooBig()) + { + std::unique_lock sl1(respondLock); + while (currentByteSize >= maxByteSize && msgQueue.size() > 3 && !die) + { + respondWait = true; + fProcessorPool->incBlockedThreads(); + okToRespond.wait(sl1); + fProcessorPool->decBlockedThreads(); + respondWait = false; + } + } if (die) return; - boost::mutex::scoped_lock sl(msgQueueLock); - + std::unique_lock sl(msgQueueLock); + if (gotException) throw runtime_error(exceptionString); @@ -143,7 +153,7 @@ void BPPSendThread::sendResults(const vector& msgs, bool newConnection) void BPPSendThread::sendMore(int num) { - boost::mutex::scoped_lock sl(ackLock); + std::unique_lock sl(ackLock); // cout << "got an ACK for " << num << " msgsLeft=" << msgsLeft << endl; if (num == -1) @@ -156,6 +166,7 @@ void BPPSendThread::sendMore(int num) else (void)atomicops::atomicAdd(&msgsLeft, num); + sl.unlock(); if (waiting) okToSend.notify_one(); } @@ -178,7 +189,7 @@ void BPPSendThread::mainLoop() while (!die) { - boost::mutex::scoped_lock sl(msgQueueLock); + std::unique_lock sl(msgQueueLock); if (msgQueue.empty() && !die) { @@ -209,8 +220,7 @@ void BPPSendThread::mainLoop() if (msgsLeft <= 0 && fcEnabled && !die) { - boost::mutex::scoped_lock sl2(ackLock); - + std::unique_lock sl2(ackLock); while (msgsLeft <= 0 && fcEnabled && !die) { waiting = true; @@ -254,19 +264,26 @@ void BPPSendThread::mainLoop() (void)atomicops::atomicSub(¤tByteSize, bsSize); msg[msgsSent].msg.reset(); } + + if (respondWait && currentByteSize < maxByteSize) + { + okToRespond.notify_one(); + } } } } void BPPSendThread::abort() { - boost::mutex::scoped_lock sl(msgQueueLock); - boost::mutex::scoped_lock sl2(ackLock); - die = true; - queueNotEmpty.notify_one(); - okToSend.notify_one(); - sl.unlock(); - sl2.unlock(); + std::lock_guard sl(msgQueueLock); + std::lock_guard sl2(ackLock); + std::lock_guard sl3(respondLock); + { + die = true; + } + queueNotEmpty.notify_all(); + okToSend.notify_all(); + okToRespond.notify_all(); } } diff --git a/primitives/primproc/bppsendthread.h b/primitives/primproc/bppsendthread.h index 16fdf7e6a..54dd62b76 100644 --- a/primitives/primproc/bppsendthread.h +++ b/primitives/primproc/bppsendthread.h @@ -20,15 +20,16 @@ * * ***********************************************************************/ -/** @file */ #include "batchprimitiveprocessor.h" #include "umsocketselector.h" +#include +#include #include #include #include -#include - +#include "threadnaming.h" +#include "prioritythreadpool.h" #ifndef BPPSENDTHREAD_H #define BPPSENDTHREAD_H @@ -63,7 +64,15 @@ public: msg(m), sock(so), sockLock(sl), sockIndex(si) { } }; - bool okToProceed(); + bool sizeTooBig() + { + // keep the queue size below the 100 msg threshold & below the 250MB mark, + // but at least 3 msgs so there is always 1 ready to be sent. + return ((msgQueue.size() > sizeThreshold) || + (currentByteSize >= maxByteSize && msgQueue.size() > 3)) && !die; + } + + void sendMore(int num); void sendResults(const std::vector& msgs, bool newConnection); void sendResult(const Msg_t& msg, bool newConnection); @@ -74,6 +83,10 @@ public: { return die; } + void setProcessorPool(threadpool::PriorityThreadPool* processorPool) + { + fProcessorPool = processorPool; + } private: BPPSendThread(const BPPSendThread&); @@ -85,22 +98,27 @@ private: Runner_t(BPPSendThread* b) : bppst(b) { } void operator()() { + utils::setThreadName("BPPSendThread"); bppst->mainLoop(); } }; boost::thread runner; std::queue msgQueue; - boost::mutex msgQueueLock; - boost::condition queueNotEmpty; + std::mutex msgQueueLock; + std::condition_variable queueNotEmpty; volatile bool die, gotException, mainThreadWaiting; std::string exceptionString; uint32_t sizeThreshold; volatile int32_t msgsLeft; bool waiting; - boost::mutex ackLock; - boost::condition okToSend; - + std::mutex ackLock; + std::condition_variable okToSend; + // Condition to prevent run away queue + bool respondWait; + std::mutex respondLock; + std::condition_variable okToRespond; + /* Load balancing structures */ struct Connection_t { @@ -125,6 +143,9 @@ private: /* secondary queue size restriction based on byte size */ volatile uint64_t currentByteSize; uint64_t maxByteSize; + // Used to tell the PriorityThreadPool It should consider additional threads because a + // queue full event has happened and a thread has been blocked. + threadpool::PriorityThreadPool* fProcessorPool; }; } diff --git a/primitives/primproc/primitiveserver.cpp b/primitives/primproc/primitiveserver.cpp index 26766a51d..ae5824243 100644 --- a/primitives/primproc/primitiveserver.cpp +++ b/primitives/primproc/primitiveserver.cpp @@ -1453,7 +1453,7 @@ struct BPPHandler SBPPV bppv; // make the new BPP object - bppv.reset(new BPPV()); + bppv.reset(new BPPV(fPrimitiveServerPtr)); bpp.reset(new BatchPrimitiveProcessor(bs, fPrimitiveServerPtr->prefetchThreshold(), bppv->getSendThread(), fPrimitiveServerPtr->ProcessorThreads())); @@ -1913,7 +1913,7 @@ struct ReadThread /* Message format: * ISMPacketHeader * Partition count - 32 bits - * Partition set - sizeof(LogicalPartition) * count + * Partition set - sizeof(LogicalPartition) boost::shared_ptr* count * OID count - 32 bits * OID array - 32 bits * count */ @@ -2004,8 +2004,7 @@ struct ReadThread void operator()() { utils::setThreadName("PPReadThread"); - boost::shared_ptr procPoolPtr = - fPrimitiveServerPtr->getProcessorThreadPool(); + threadpool::PriorityThreadPool* procPoolPtr = fPrimitiveServerPtr->getProcessorThreadPool(); SBS bs; UmSocketSelector* pUmSocketSelector = UmSocketSelector::instance(); @@ -2475,8 +2474,8 @@ PrimitiveServer::PrimitiveServer(int serverThreads, fServerpool.setQueueSize(fServerQueueSize); fServerpool.setName("PrimitiveServer"); - fProcessorPool.reset(new threadpool::PriorityThreadPool(fProcessorWeight, highPriorityThreads, - medPriorityThreads, lowPriorityThreads, 0)); + fProcessorPool = new threadpool::PriorityThreadPool(fProcessorWeight, highPriorityThreads, + medPriorityThreads, lowPriorityThreads, 0); // We're not using either the priority or the job-clustering features, just need a threadpool // that can reschedule jobs, and an unlimited non-blocking queue @@ -2526,9 +2525,10 @@ void PrimitiveServer::start(Service *service) cerr << "PrimitiveServer::start() exiting!" << endl; } -BPPV::BPPV() +BPPV::BPPV(PrimitiveServer* ps) { sendThread.reset(new BPPSendThread()); + sendThread->setProcessorPool(ps->getProcessorThreadPool()); v.reserve(BPPCount); pos = 0; joinDataReceived = false; @@ -2570,7 +2570,7 @@ const vector >& BPPV::get() boost::shared_ptr BPPV::next() { uint32_t size = v.size(); - uint32_t i; + uint32_t i = 0; #if 0 diff --git a/primitives/primproc/primitiveserver.h b/primitives/primproc/primitiveserver.h index 648cda187..af86204f7 100644 --- a/primitives/primproc/primitiveserver.h +++ b/primitives/primproc/primitiveserver.h @@ -55,10 +55,12 @@ extern BRM::DBRM* brm; extern boost::mutex bppLock; extern uint32_t highPriorityThreads, medPriorityThreads, lowPriorityThreads; +class PrimitiveServer; + class BPPV { public: - BPPV(); + BPPV(PrimitiveServer* ps); ~BPPV(); boost::shared_ptr next(); void add(boost::shared_ptr a); @@ -133,7 +135,7 @@ public: /** @brief get a pointer the shared processor thread pool */ - inline boost::shared_ptr getProcessorThreadPool() const + inline threadpool::PriorityThreadPool* getProcessorThreadPool() const { return fProcessorPool; } @@ -170,7 +172,7 @@ private: /** @brief the thread pool used to process * primitive commands */ - boost::shared_ptr fProcessorPool; + threadpool::PriorityThreadPool* fProcessorPool; int fServerThreads; int fServerQueueSize; diff --git a/primitives/primproc/primproc.cpp b/primitives/primproc/primproc.cpp index 644ac1eb7..0464c6f46 100644 --- a/primitives/primproc/primproc.cpp +++ b/primitives/primproc/primproc.cpp @@ -76,7 +76,7 @@ using namespace idbdatafile; #include "mariadb_my_sys.h" #include "service.h" - +#include "threadnaming.h" class Opt { @@ -259,6 +259,7 @@ public: void operator()() { + utils::setThreadName("QszMonThd"); for (;;) { uint32_t qd = fPsp->getProcessorThreadPool()->getWaiting(); @@ -304,6 +305,7 @@ private: #ifdef DUMP_CACHE_CONTENTS void* waitForSIGUSR1(void* p) { + utils::setThreadName("waitForSIGUSR1"); #if defined(__LP64__) || defined(_MSC_VER) ptrdiff_t tmp = reinterpret_cast(p); int cacheCount = static_cast(tmp); diff --git a/utils/common/MonitorProcMem.cpp b/utils/common/MonitorProcMem.cpp index 3258bfb11..320effcc9 100644 --- a/utils/common/MonitorProcMem.cpp +++ b/utils/common/MonitorProcMem.cpp @@ -42,6 +42,7 @@ using namespace std; using namespace logging; #include "MonitorProcMem.h" +#include "threadnaming.h" namespace utils { @@ -57,6 +58,7 @@ int MonitorProcMem::fMemPctCheck = 0; //------------------------------------------------------------------------------ void MonitorProcMem::operator()() const { + utils::setThreadName("MonitorProcMem"); while (1) { if (fMaxPct > 0) diff --git a/utils/common/atomicops.h b/utils/common/atomicops.h index f2ec85fbf..0f77d175e 100644 --- a/utils/common/atomicops.h +++ b/utils/common/atomicops.h @@ -165,6 +165,28 @@ inline bool atomicCAS(volatile T* mem, T comp, T swap) #endif } +// implements a zero out of a variable +template +inline void atomicZero(volatile T* mem) +{ +#ifdef _MSC_VER + + switch (sizeof(T)) + { + case 4: + default: + InterlockedXor(reinterpret_cast(mem),(static_cast(*mem))); + break; + + case 8: + InterlockedXor64(reinterpret_cast(mem),(static_cast(*mem))); + break; + } + #else + __sync_xor_and_fetch(mem, *mem); + #endif +} + //Implements a scheduler yield inline void atomicYield() { diff --git a/utils/joiner/joinpartition.cpp b/utils/joiner/joinpartition.cpp index 4b796979c..8b1badfb4 100644 --- a/utils/joiner/joinpartition.cpp +++ b/utils/joiner/joinpartition.cpp @@ -104,9 +104,6 @@ JoinPartition::JoinPartition(const RowGroup& lRG, buckets.reserve(bucketCount); - for (int i = 0; i < (int) bucketCount; i++) - buckets.push_back(boost::shared_ptr(new JoinPartition(*this, false))); - string compressionType; try { @@ -122,6 +119,11 @@ JoinPartition::JoinPartition(const RowGroup& lRG, { compressor.reset(new compress::CompressInterfaceSnappy()); } + + for (uint32_t i = 0; i < bucketCount; i++) + buckets.push_back(boost::shared_ptr(new JoinPartition(*this, false))); + + } /* Ctor used by JoinPartition on expansion, creates JP's in filemode */ diff --git a/utils/loggingcpp/ErrorMessage.txt b/utils/loggingcpp/ErrorMessage.txt index 4a899fc4c..d918e2ff7 100755 --- a/utils/loggingcpp/ErrorMessage.txt +++ b/utils/loggingcpp/ErrorMessage.txt @@ -103,6 +103,7 @@ 2054 ERR_DISKAGG_ERROR Unknown error while aggregation. 2055 ERR_DISKAGG_TOO_BIG Not enough memory to make disk-based aggregation. Raise TotalUmMemory if possible. 2056 ERR_DISKAGG_FILEIO_ERROR There was an IO error during a disk-based aggregation: %1% +2057 ERR_JOIN_RESULT_TOO_BIG Not enough memory to consolidate join results. Estimated %1% MB needed. TotalUmMemory is %2% MB. # Sub-query errors 3001 ERR_NON_SUPPORT_SUB_QUERY_TYPE This subquery type is not supported yet. diff --git a/utils/rowgroup/rowaggregation.cpp b/utils/rowgroup/rowaggregation.cpp index 41ec61083..1ed114c69 100755 --- a/utils/rowgroup/rowaggregation.cpp +++ b/utils/rowgroup/rowaggregation.cpp @@ -4918,12 +4918,12 @@ RowAggregationMultiDistinct::RowAggregationMultiDistinct(const RowAggregationMul for (uint32_t i = 0; i < rhs.fSubAggregators.size(); i++) { #if 0 - fTotalMemUsage += fSubRowGroups[i].getDataSize(AGG_ROWGROUP_SIZE); - if (!fRm->getMemory(fSubRowGroups[i].getDataSize(AGG_ROWGROUP_SIZE, fSessionMemLimit))) throw logging::IDBExcept(logging::IDBErrorInfo::instance()-> errorMsg(logging::ERR_AGGREGATION_TOO_BIG), logging::ERR_AGGREGATION_TOO_BIG); + fTotalMemUsage += fSubRowGroups[i].getDataSize(AGG_ROWGROUP_SIZE); + #endif data.reset(new RGData(fSubRowGroups[i], RowAggStorage::getMaxRows(fRm ? fRm->getAllowDiskAggregation() : false))); fSubRowData.push_back(data); @@ -4964,12 +4964,11 @@ void RowAggregationMultiDistinct::addSubAggregator(const boost::shared_ptr data; #if 0 - fTotalMemUsage += rg.getDataSize(AGG_ROWGROUP_SIZE); - if (!fRm->getMemory(rg.getDataSize(AGG_ROWGROUP_SIZE), fSessionMemLimit)) throw logging::IDBExcept(logging::IDBErrorInfo::instance()-> errorMsg(logging::ERR_AGGREGATION_TOO_BIG), logging::ERR_AGGREGATION_TOO_BIG); + fTotalMemUsage += rg.getDataSize(AGG_ROWGROUP_SIZE); #endif data.reset(new RGData(rg, RowAggStorage::getMaxRows(fRm ? fRm->getAllowDiskAggregation() : false))); fSubRowData.push_back(data); diff --git a/utils/rowgroup/rowstorage.cpp b/utils/rowgroup/rowstorage.cpp index 09e9b5335..88f0b104f 100644 --- a/utils/rowgroup/rowstorage.cpp +++ b/utils/rowgroup/rowstorage.cpp @@ -325,25 +325,31 @@ public: protected: bool acquireImpl(size_t amount) final { - MemManager::acquireImpl(amount); - if (!fRm->getMemory(amount, fSessLimit, fWait) && fStrict) + if (amount) { - return false; - } - + if (!fRm->getMemory(amount, fSessLimit, fWait) && fStrict) + { + return false; + } + MemManager::acquireImpl(amount); + } return true; } - void releaseImpl(size_t amount) override { - MemManager::releaseImpl(amount); - fRm->returnMemory(amount, fSessLimit); + void releaseImpl(size_t amount) override + { + if (amount) + { + MemManager::releaseImpl(amount); + fRm->returnMemory(amount, fSessLimit); + } } private: joblist::ResourceManager* fRm = nullptr; boost::shared_ptr fSessLimit; const bool fWait; - const bool fStrict; + const bool fStrict; }; class Dumper { @@ -353,7 +359,8 @@ public: , fMM(mm->clone()) {} - int write(const std::string &fname, const char *buf, size_t sz) { + int write(const std::string &fname, const char *buf, size_t sz) + { if (sz == 0) return 0; @@ -368,7 +375,9 @@ public: fCompressor->compress(buf, sz, fTmpBuf.data(), &len); tmpbuf = fTmpBuf.data(); sz = len; - } else { + } + else + { tmpbuf = buf; } diff --git a/utils/threadpool/prioritythreadpool.cpp b/utils/threadpool/prioritythreadpool.cpp index ce1d3c388..3ed4c7d1a 100644 --- a/utils/threadpool/prioritythreadpool.cpp +++ b/utils/threadpool/prioritythreadpool.cpp @@ -28,6 +28,7 @@ using namespace std; #include "messageobj.h" #include "messagelog.h" +#include "threadnaming.h" using namespace logging; #include "prioritythreadpool.h" @@ -40,7 +41,8 @@ namespace threadpool PriorityThreadPool::PriorityThreadPool(uint targetWeightPerRun, uint highThreads, uint midThreads, uint lowThreads, uint ID) : - _stop(false), weightPerRun(targetWeightPerRun), id(ID) + _stop(false), weightPerRun(targetWeightPerRun), id(ID), + blockedThreads(0), extraThreads(0), stopExtra(true) { boost::thread* newThread; for (uint32_t i = 0; i < highThreads; i++) @@ -99,7 +101,22 @@ void PriorityThreadPool::addJob(const Job& job, bool useLock) newThread->detach(); threadCounts[LOW]++; } - + + // If some threads have blocked (because of output queue full) + // Temporarily add some extra worker threads to make up for the blocked threads. + if (blockedThreads > extraThreads) + { + stopExtra = false; + newThread = threads.create_thread(ThreadHelper(this, EXTRA)); + newThread->detach(); + extraThreads++; + } + else if (blockedThreads == 0) + { + // Release the temporary threads -- some threads have become unblocked. + stopExtra = true; + } + if (job.priority > 66) jobQueues[HIGH].push_back(job); else if (job.priority > 33) @@ -127,7 +144,7 @@ void PriorityThreadPool::removeJobs(uint32_t id) PriorityThreadPool::Priority PriorityThreadPool::pickAQueue(Priority preference) { - if (!jobQueues[preference].empty()) + if (preference != EXTRA && !jobQueues[preference].empty()) return preference; else if (!jobQueues[HIGH].empty()) return HIGH; @@ -139,6 +156,10 @@ PriorityThreadPool::Priority PriorityThreadPool::pickAQueue(Priority preference) void PriorityThreadPool::threadFcn(const Priority preferredQueue) throw() { + if (preferredQueue == EXTRA) + utils::setThreadName("Extra"); + else + utils::setThreadName("Idle"); Priority queue = LOW; uint32_t weight, i = 0; vector runList; @@ -158,6 +179,14 @@ void PriorityThreadPool::threadFcn(const Priority preferredQueue) throw() if (jobQueues[queue].empty()) { + // If this is an EXTRA thread due toother threads blocking, and all blockers are unblocked, + // we don't want this one any more. + if (preferredQueue == EXTRA && stopExtra) + { + extraThreads--; + return; + } + newJob.wait(lk); continue; } @@ -194,7 +223,11 @@ void PriorityThreadPool::threadFcn(const Priority preferredQueue) throw() if (reschedule[i]) rescheduleCount++; } - + if (preferredQueue == EXTRA) + utils::setThreadName("Extra (used)"); + else + utils::setThreadName("Idle"); + // no real work was done, prevent intensive busy waiting if (rescheduleCount == runList.size()) usleep(1000); @@ -216,6 +249,7 @@ void PriorityThreadPool::threadFcn(const Priority preferredQueue) throw() } runList.clear(); + } } catch (std::exception& ex) diff --git a/utils/threadpool/prioritythreadpool.h b/utils/threadpool/prioritythreadpool.h index f58af4d8d..a97ca621b 100644 --- a/utils/threadpool/prioritythreadpool.h +++ b/utils/threadpool/prioritythreadpool.h @@ -35,8 +35,10 @@ #include #include #include +#include #include "../winport/winport.h" #include "primitives/primproc/umsocketselector.h" +#include "atomicops.h" namespace threadpool { @@ -73,7 +75,8 @@ public: LOW, MEDIUM, HIGH, - _COUNT + _COUNT, + EXTRA // After _COUNT because _COUNT is for jobQueue size and EXTRA isn't a jobQueue. But we need EXTRA in places where Priority is used. }; /********************************************* @@ -95,7 +98,21 @@ public: /** @brief for use in debugging */ void dump(); - + + // If a job is blocked, we want to temporarily increase the number of threads managed by the pool + // A problem can occur if all threads are running long or blocked for a single query. Other + // queries won't get serviced, even though there are cpu cycles available. + // These calls are currently protected by respondLock in sendThread(). If you call from other + // places, you need to consider atomicity. + void incBlockedThreads() + { + blockedThreads++; + } + void decBlockedThreads() + { + blockedThreads--; + } + protected: private: @@ -127,6 +144,10 @@ private: bool _stop; uint32_t weightPerRun; volatile uint id; // prevent it from being optimized out + + std::atomic blockedThreads; + std::atomic extraThreads; + bool stopExtra; }; } // namespace threadpool diff --git a/utils/threadpool/threadpool.cpp b/utils/threadpool/threadpool.cpp index eb86dad42..1256fcbeb 100644 --- a/utils/threadpool/threadpool.cpp +++ b/utils/threadpool/threadpool.cpp @@ -88,6 +88,7 @@ void ThreadPool::setQueueSize(size_t queueSize) void ThreadPool::pruneThread() { + utils::setThreadName("pruneThread"); boost::unique_lock lock2(fPruneMutex); while(true) diff --git a/utils/windowfunction/idborderby.cpp b/utils/windowfunction/idborderby.cpp index 156cd4def..e5557ae77 100644 --- a/utils/windowfunction/idborderby.cpp +++ b/utils/windowfunction/idborderby.cpp @@ -771,15 +771,13 @@ void IdbOrderBy::initialize(const RowGroup& rg) IdbCompare::initialize(rg); uint64_t newSize = rg.getSizeWithStrings(fRowsPerRG); - fMemSize += newSize; - - if (!fRm->getMemory(newSize, fSessionMemLimit)) + if (fRm && !fRm->getMemory(newSize, fSessionMemLimit)) { cerr << IDBErrorInfo::instance()->errorMsg(fErrorCode) << " @" << __FILE__ << ":" << __LINE__; throw IDBExcept(fErrorCode); } - + fMemSize += newSize; fData.reinit(fRowGroup, fRowsPerRG); fRowGroup.setData(&fData); fRowGroup.resetRowGroup(0);