From c8277536ead17ac73c65e87a009c05039a9392f5 Mon Sep 17 00:00:00 2001 From: Laurent Goujon Date: Tue, 24 Jan 2017 18:47:47 -0800 Subject: [PATCH] DRILL-5221: Send cancel message as soon as possible in C++ connector In C++ connector, try to send cancel request to the server as soon as possible, which means when receiving the queryId or when requested by the user if queryId has already been received. --- .../client/src/clientlib/drillClientImpl.cpp | 48 ++++++++++--------- .../client/src/clientlib/drillClientImpl.hpp | 2 +- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/contrib/native/client/src/clientlib/drillClientImpl.cpp b/contrib/native/client/src/clientlib/drillClientImpl.cpp index 05171e5c879..2a500df162a 100644 --- a/contrib/native/client/src/clientlib/drillClientImpl.cpp +++ b/contrib/native/client/src/clientlib/drillClientImpl.cpp @@ -862,7 +862,7 @@ status_t DrillClientImpl::processQueryData(AllocatedBufferPtr allocatedBuffer, c DRILL_MT_LOG(DRILL_LOG(LOG_DEBUG) << "Processing Query cancellation " << std::endl;) delete qr; delete allocatedBuffer; - ret = QRY_CANCEL; + ret = QRY_CANCELED; } else { //Validate the RPC message std::string valErr; @@ -1493,7 +1493,6 @@ status_t DrillClientImpl::handleQryCancellation(status_t status, DrillClientQuer pQueryHandle->setIsQueryPending(false); DRILL_MT_LOG(DRILL_LOG(LOG_DEBUG) << "Client app cancelled query." << std::endl;) pQueryHandle->setQueryStatus(status); - removeQueryResult(pQueryHandle); removeQueryHandle(pQueryHandle); return status; } @@ -1536,25 +1535,23 @@ status_t DrillClientImpl::handleTerminatedQryState( void DrillClientImpl::removeQueryHandle(DrillClientQueryHandle* pQueryHandle){ boost::lock_guard lock(m_dcMutex); - if(!m_queryHandles.empty()){ - for(std::map::const_iterator iter=m_queryHandles.begin(); iter!=m_queryHandles.end(); iter++) { - if(pQueryHandle==(DrillClientQueryHandle*)iter->second){ - m_queryHandles.erase(iter->first); - break; - } - } + // Removing first the base handle + for(std::map::const_iterator iter=m_queryHandles.begin(); iter!=m_queryHandles.end(); iter++) { + if(pQueryHandle==(DrillClientQueryHandle*)iter->second){ + m_queryHandles.erase(iter->first); + break; + } } -} -void DrillClientImpl::removeQueryResult(DrillClientQueryResult* pQueryResult){ - boost::lock_guard lock(m_dcMutex); - if(!m_queryResults.empty()){ - for(std::map::const_iterator it=m_queryResults.begin(); it!=m_queryResults.end(); it++) { - if(pQueryResult==(DrillClientQueryResult*)it->second){ - m_queryResults.erase(it->first); - break; - } - } + // if the query handle is a result handle, m_queryResults also need to be cleaned. + DrillClientQueryResult* pQueryResult = dynamic_cast(pQueryHandle); + if (pQueryResult) { + for(std::map::const_iterator it=m_queryResults.begin(); it!=m_queryResults.end(); it++) { + if(pQueryResult==(DrillClientQueryResult*)it->second){ + m_queryResults.erase(it->first); + break; + } + } } } @@ -1690,6 +1687,16 @@ RecordBatch* DrillClientQueryResult::peekNext(){ return pRecordBatch; } +void DrillClientQueryResult::cancel() { + // Calling parent class + DrillClientBaseHandle::cancel(); + + // If queryId has already been received, don't wait to send the + // cancellation message + if (this->m_pQueryId) { + this->client().handleQryCancellation(QRY_CANCELED, this); + } +} RecordBatch* DrillClientQueryResult::getNext() { RecordBatch* pRecordBatch=NULL; boost::unique_lock cvLock(this->m_cvMutex); @@ -1814,9 +1821,6 @@ void DrillClientQueryResult::clearAndDestroy(){ DRILL_MT_LOG(DRILL_LOG(LOG_TRACE) << "Clearing state for Query Id - " << debugPrintQid(*this->m_pQueryId) << std::endl;) } - //Tell the parent to remove this from its lists - this->client().removeQueryResult(this); - //clear query id map entries. if(this->m_pQueryId!=NULL){ delete this->m_pQueryId; this->m_pQueryId=NULL; diff --git a/contrib/native/client/src/clientlib/drillClientImpl.hpp b/contrib/native/client/src/clientlib/drillClientImpl.hpp index 22e34af7d13..4724ae9625d 100644 --- a/contrib/native/client/src/clientlib/drillClientImpl.hpp +++ b/contrib/native/client/src/clientlib/drillClientImpl.hpp @@ -209,6 +209,7 @@ class DrillClientQueryResult: public DrillClientBaseHandle