4545#include  " http/Client.h" 
4646#include  " http/ClientConnPool.h" 
4747#include  " http/Method.h" 
48- #include  " mysql/CsvBuffer .h" 
48+ #include  " mysql/CsvMemDisk .h" 
4949#include  " qdisp/CzarStats.h" 
5050#include  " qdisp/Executive.h" 
5151#include  " qdisp/JobQuery.h" 
@@ -244,13 +244,8 @@ qdisp::MergeEndStatus MergingHandler::_mergeHttp(qdisp::UberJob::Ptr const& uber
244244    }
245245
246246    if  (fileSize == 0 ) return  qdisp::MergeEndStatus (true );
247- 
248-     //  Read from the http stream and push records into the CSV stream in a separate thread.
249-     //  Note the fixed capacity of the stream which allows up to 2 records to be buffered
250-     //  in the stream. This is enough to hide the latency of the HTTP connection and
251-     //  the time needed to read the file.
252-     auto  csvStream = mysql::CsvStream::create (2 );
253-     _csvStream = csvStream;
247+     auto  csvMemDisk = mysql::CsvMemDisk::create (fileSize, uberJob->getQueryId (), uberJob->getUjId ());
248+     _csvMemDisk = csvMemDisk;
254249
255250    //  This must be after setting _csvStream to avoid cancelFileMerge()
256251    //  race issues, and it needs to be before the thread starts.
@@ -260,46 +255,46 @@ qdisp::MergeEndStatus MergingHandler::_mergeHttp(qdisp::UberJob::Ptr const& uber
260255    }
261256
262257    string fileReadErrorMsg;
263-     thread  csvThread ([uberJob, csvStream, fileUrl, fileSize, &fileReadErrorMsg ]() {
258+     auto  transferFunc = [& ]() {
264259        size_t  bytesRead = 0 ;
265260        fileReadErrorMsg = ::readHttpFileAndMerge (
266261                uberJob, fileUrl, fileSize,
267-                 [uberJob, csvStream, fileSize, &bytesRead ](char  const * buf, uint32_t  size) {
262+                 [& ](char  const * buf, uint32_t  size) {
268263                    bool  last = false ;
269264                    if  (buf == nullptr  || size == 0 ) {
270265                        last = true ;
271266                    } else  {
272-                         csvStream ->push (buf, size);
267+                         csvMemDisk ->push (buf, size);
273268                        bytesRead += size;
274269                        last = bytesRead >= fileSize;
275270                    }
276271                    if  (last) {
277-                         csvStream ->push (nullptr , 0 );
272+                         csvMemDisk ->push (nullptr , 0 );
278273                    }
279274                },
280275                MergingHandler::_getHttpConnPool ());
281276        //  Push the stream terminator to indicate the end of the stream.
282277        //  It may be needed to unblock the table merger which may be still attempting to read
283278        //  from the CSV stream.
284279        if  (!fileReadErrorMsg.empty ()) {
285-             csvStream ->push (nullptr , 0 );
280+             csvMemDisk ->push (nullptr , 0 );
286281        }
287-     });
282+     };
283+     csvMemDisk->transferDataFromWorker (transferFunc);
288284
289285    //  Attempt the actual merge.
290-     bool  fileMergeSuccess = _infileMerger->mergeHttp (uberJob, fileSize, csvStream );
286+     bool  fileMergeSuccess = _infileMerger->mergeHttp (uberJob, fileSize, csvMemDisk );
291287    if  (!fileMergeSuccess) {
292288        LOGS (_log, LOG_LVL_WARN, __func__ << "  merge failed"  );
293289        util::Error const & err = _infileMerger->getError ();
294290        _setError (ccontrol::MSG_RESULT_ERROR, err.getMsg (), util::ErrorCode::RESULT_IMPORT);
295291    }
296-     if  (csvStream ->getContaminated ()) {
292+     if  (csvMemDisk ->getContaminated ()) {
297293        LOGS (_log, LOG_LVL_ERROR, __func__ << "  merge stream contaminated"  );
298294        fileMergeSuccess = false ;
299295        _setError (ccontrol::MSG_RESULT_ERROR, " merge stream contaminated"  , util::ErrorCode::RESULT_IMPORT);
300296    }
301297
302-     csvThread.join ();
303298    if  (!fileReadErrorMsg.empty ()) {
304299        LOGS (_log, LOG_LVL_WARN, __func__ << "  result file read failed"  );
305300        _setError (ccontrol::MSG_HTTP_RESULT, fileReadErrorMsg, util::ErrorCode::RESULT_IMPORT);
@@ -310,14 +305,14 @@ qdisp::MergeEndStatus MergingHandler::_mergeHttp(qdisp::UberJob::Ptr const& uber
310305    if  (!mergeEStatus.success ) {
311306        //  This error check needs to come after the csvThread.join() to ensure writing
312307        //  is finished. If any bytes were written, the result table is ruined.
313-         mergeEStatus.contaminated  = csvStream-> getBytesWritten () > 0 ;
308+         mergeEStatus.contaminated  = csvMemDisk-> getBytesFetched () > 0 ;
314309    }
315310
316311    return  mergeEStatus;
317312}
318313
319314void  MergingHandler::cancelFileMerge () {
320-     auto  csvStrm = _csvStream .lock ();
315+     auto  csvStrm = _csvMemDisk .lock ();
321316    if  (csvStrm != nullptr ) {
322317        csvStrm->cancel ();
323318    }
@@ -343,9 +338,6 @@ qdisp::MergeEndStatus MergingHandler::flushHttp(string const& fileUrl, uint64_t
343338         " MergingHandler::"   << __func__ << "  uberJob="   << uberJob->getIdStr () << "  fileUrl="   << fileUrl);
344339
345340    qdisp::MergeEndStatus mergeStatus = _mergeHttp (uberJob, fileUrl, fileSize);
346-     if  (mergeStatus.success ) {
347-         _infileMerger->mergeCompleteFor (uberJob->getUjId ());
348-     }
349341    return  mergeStatus;
350342}
351343
0 commit comments