• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3558

17 Dec 2024 06:05AM UTC coverage: 59.778% (+1.6%) from 58.204%
#3558

push

travis-ci

web-flow
Merge pull request #29179 from taosdata/merge/mainto3.0

merge: form main to 3.0 branch

132787 of 287595 branches covered (46.17%)

Branch coverage included in aggregate %.

104 of 191 new or added lines in 5 files covered. (54.45%)

6085 existing lines in 168 files now uncovered.

209348 of 284746 relevant lines covered (73.52%)

8164844.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.31
/source/libs/executor/src/exchangeoperator.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "executorInt.h"
17
#include "filter.h"
18
#include "function.h"
19
#include "operator.h"
20
#include "query.h"
21
#include "querytask.h"
22
#include "tdatablock.h"
23
#include "thash.h"
24
#include "tmsg.h"
25
#include "tref.h"
26
#include "trpc.h"
27

28
typedef struct SFetchRspHandleWrapper {
29
  uint32_t exchangeId;
30
  int32_t  sourceIndex;
31
} SFetchRspHandleWrapper;
32

33
typedef struct SSourceDataInfo {
34
  int32_t            index;
35
  SRetrieveTableRsp* pRsp;
36
  uint64_t           totalRows;
37
  int64_t            startTime;
38
  int32_t            code;
39
  EX_SOURCE_STATUS   status;
40
  const char*        taskId;
41
  SArray*            pSrcUidList;
42
  int32_t            srcOpType;
43
  bool               tableSeq;
44
  char*              decompBuf;
45
  int32_t            decompBufSize;
46
} SSourceDataInfo;
47

48
static void destroyExchangeOperatorInfo(void* param);
49
static void freeBlock(void* pParam);
50
static void freeSourceDataInfo(void* param);
51
static void setAllSourcesCompleted(SOperatorInfo* pOperator);
52

53
static int32_t loadRemoteDataCallback(void* param, SDataBuf* pMsg, int32_t code);
54
static int32_t doSendFetchDataRequest(SExchangeInfo* pExchangeInfo, SExecTaskInfo* pTaskInfo, int32_t sourceIndex);
55
static int32_t getCompletedSources(const SArray* pArray, int32_t* pRes);
56
static int32_t prepareConcurrentlyLoad(SOperatorInfo* pOperator);
57
static int32_t seqLoadRemoteData(SOperatorInfo* pOperator);
58
static int32_t prepareLoadRemoteData(SOperatorInfo* pOperator);
59
static int32_t handleLimitOffset(SOperatorInfo* pOperator, SLimitInfo* pLimitInfo, SSDataBlock* pBlock,
60
                                 bool holdDataInBuf);
61
static int32_t doExtractResultBlocks(SExchangeInfo* pExchangeInfo, SSourceDataInfo* pDataInfo);
62

63
static int32_t exchangeWait(SOperatorInfo* pOperator, SExchangeInfo* pExchangeInfo);
64

65
static void concurrentlyLoadRemoteDataImpl(SOperatorInfo* pOperator, SExchangeInfo* pExchangeInfo,
1,886,820✔
66
                                           SExecTaskInfo* pTaskInfo) {
67
  int32_t code = 0;
1,886,820✔
68
  int32_t lino = 0;
1,886,820✔
69
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSourceDataInfo);
1,886,820✔
70
  int32_t completed = 0;
1,886,702✔
71
  code = getCompletedSources(pExchangeInfo->pSourceDataInfo, &completed);
1,886,702✔
72
  if (code != TSDB_CODE_SUCCESS) {
1,886,659!
73
    pTaskInfo->code = code;
×
74
    T_LONG_JMP(pTaskInfo->env, code);
×
75
  }
76
  if (completed == totalSources) {
1,886,659✔
77
    setAllSourcesCompleted(pOperator);
585,374✔
78
    return;
1,887,376✔
79
  }
80

81
  SSourceDataInfo* pDataInfo = NULL;
1,301,285✔
82

83
  while (1) {
110,691✔
84
    qDebug("prepare wait for ready, %p, %s", pExchangeInfo, GET_TASKID(pTaskInfo));
1,411,976✔
85
    code = exchangeWait(pOperator, pExchangeInfo);
1,411,980✔
86

87
    if (code != TSDB_CODE_SUCCESS || isTaskKilled(pTaskInfo)) {
1,412,902!
UNCOV
88
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
89
    }
90

91
    for (int32_t i = 0; i < totalSources; ++i) {
2,646,528✔
92
      pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, i);
2,644,992✔
93
      QUERY_CHECK_NULL(pDataInfo, code, lino, _error, terrno);
2,644,938!
94
      if (pDataInfo->status == EX_SOURCE_DATA_EXHAUSTED) {
2,645,762✔
95
        continue;
1,015,227✔
96
      }
97

98
      if (pDataInfo->status != EX_SOURCE_DATA_READY) {
1,630,535✔
99
        continue;
218,453✔
100
      }
101

102
      if (pDataInfo->code != TSDB_CODE_SUCCESS) {
1,412,082!
UNCOV
103
        code = pDataInfo->code;
×
UNCOV
104
        goto _error;
×
105
      }
106

107
      tmemory_barrier();
1,412,082✔
108
      SRetrieveTableRsp*     pRsp = pDataInfo->pRsp;
1,412,082✔
109
      SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, pDataInfo->index);
1,412,082✔
110
      QUERY_CHECK_NULL(pSource, code, lino, _error, terrno);
1,412,788✔
111

112
      // todo
113
      SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
1,412,777✔
114
      if (pRsp->numOfRows == 0) {
1,412,777✔
115
        if (NULL != pDataInfo->pSrcUidList) {
286,575!
116
          pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
×
117
          code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, i);
×
118
          if (code != TSDB_CODE_SUCCESS) {
×
119
            taosMemoryFreeClear(pDataInfo->pRsp);
×
120
            goto _error;
×
121
          }
122
        } else {
123
          pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
286,575✔
124
          qDebug("%s vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
286,575✔
125
                 " execId:%d index:%d completed, rowsOfSource:%" PRIu64 ", totalRows:%" PRIu64 ", try next %d/%" PRIzu,
126
                 GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId, i,
127
                 pDataInfo->totalRows, pExchangeInfo->loadInfo.totalRows, i + 1, totalSources);
128
          taosMemoryFreeClear(pDataInfo->pRsp);
286,573!
129
        }
130
        break;
286,572✔
131
      }
132

133
      code = doExtractResultBlocks(pExchangeInfo, pDataInfo);
1,126,202✔
134
      if (code != TSDB_CODE_SUCCESS) {
1,125,602!
135
        goto _error;
×
136
      }
137

138
      SRetrieveTableRsp* pRetrieveRsp = pDataInfo->pRsp;
1,125,602✔
139
      updateLoadRemoteInfo(pLoadInfo, pRetrieveRsp->numOfRows, pRetrieveRsp->compLen, pDataInfo->startTime, pOperator);
1,125,602✔
140
      pDataInfo->totalRows += pRetrieveRsp->numOfRows;
1,125,657✔
141

142
      if (pRsp->completed == 1) {
1,125,657✔
143
        pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
1,081,429✔
144
        qDebug("%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
1,081,429✔
145
               " execId:%d index:%d completed, blocks:%d, numOfRows:%" PRId64 ", rowsOfSource:%" PRIu64
146
               ", totalRows:%" PRIu64 ", total:%.2f Kb, try next %d/%" PRIzu,
147
               GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId, i,
148
               pRsp->numOfBlocks, pRsp->numOfRows, pDataInfo->totalRows, pLoadInfo->totalRows,
149
               pLoadInfo->totalSize / 1024.0, i + 1, totalSources);
150
      } else {
151
        qDebug("%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
44,228✔
152
               " execId:%d blocks:%d, numOfRows:%" PRId64 ", totalRows:%" PRIu64 ", total:%.2f Kb",
153
               GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
154
               pRsp->numOfBlocks, pRsp->numOfRows, pLoadInfo->totalRows, pLoadInfo->totalSize / 1024.0);
155
      }
156

157
      taosMemoryFreeClear(pDataInfo->pRsp);
1,125,660!
158

159
      if (pDataInfo->status != EX_SOURCE_DATA_EXHAUSTED || NULL != pDataInfo->pSrcUidList) {
1,126,168!
160
        pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
44,258✔
161
        code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, i);
44,258✔
162
        if (code != TSDB_CODE_SUCCESS) {
44,211!
163
          taosMemoryFreeClear(pDataInfo->pRsp);
×
164
          goto _error;
×
165
        }
166
      }
167
      return;
1,301,993✔
168
    }  // end loop
169

170
    int32_t complete1 = 0;
288,108✔
171
    code = getCompletedSources(pExchangeInfo->pSourceDataInfo, &complete1);
288,108✔
172
    if (code != TSDB_CODE_SUCCESS) {
286,561!
173
      pTaskInfo->code = code;
×
174
      T_LONG_JMP(pTaskInfo->env, code);
×
175
    }
176
    if (complete1 == totalSources) {
286,563✔
177
      qDebug("all sources are completed, %s", GET_TASKID(pTaskInfo));
175,872✔
178
      return;
175,872✔
179
    }
180
  }
181

UNCOV
182
_error:
×
UNCOV
183
  pTaskInfo->code = code;
×
184
}
185

186
static SSDataBlock* doLoadRemoteDataImpl(SOperatorInfo* pOperator) {
3,976,619✔
187
  int32_t        code = TSDB_CODE_SUCCESS;
3,976,619✔
188
  SExchangeInfo* pExchangeInfo = pOperator->info;
3,976,619✔
189
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,976,619✔
190

191
  size_t totalSources = taosArrayGetSize(pExchangeInfo->pSources);
3,976,619✔
192

193
  SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
3,976,619✔
194
  if (pOperator->status == OP_EXEC_DONE) {
3,976,619!
195
    qDebug("%s all %" PRIzu " source(s) are exhausted, total rows:%" PRIu64 " bytes:%" PRIu64 ", elapsed:%.2f ms",
×
196
           GET_TASKID(pTaskInfo), totalSources, pLoadInfo->totalRows, pLoadInfo->totalSize,
197
           pLoadInfo->totalElapsed / 1000.0);
198
    return NULL;
×
199
  }
200

201
  // we have buffered retrieved datablock, return it directly
202
  SSDataBlock* p = NULL;
3,976,619✔
203
  if (taosArrayGetSize(pExchangeInfo->pResultBlockList) > 0) {
3,976,619✔
204
    p = taosArrayGetP(pExchangeInfo->pResultBlockList, 0);
2,087,700✔
205
    taosArrayRemove(pExchangeInfo->pResultBlockList, 0);
2,087,689✔
206
  }
207

208
  if (p != NULL) {
3,976,631✔
209
    void* tmp = taosArrayPush(pExchangeInfo->pRecycledBlocks, &p);
2,087,735✔
210
    if (!tmp) {
2,087,702!
211
      code = terrno;
×
212
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
213
      pTaskInfo->code = code;
×
214
      T_LONG_JMP(pTaskInfo->env, code);
×
215
    }
216
    return p;
2,087,702✔
217
  } else {
218
    if (pExchangeInfo->seqLoadData) {
1,888,896✔
219
      code = seqLoadRemoteData(pOperator);
2,033✔
220
      if (code != TSDB_CODE_SUCCESS) {
2,033!
221
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
222
        pTaskInfo->code = code;
×
223
        T_LONG_JMP(pTaskInfo->env, code);
×
224
      }
225
    } else {
226
      concurrentlyLoadRemoteDataImpl(pOperator, pExchangeInfo, pTaskInfo);
1,886,863✔
227
    }
228
    if (TSDB_CODE_SUCCESS != pOperator->pTaskInfo->code) {
1,889,341!
UNCOV
229
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(pOperator->pTaskInfo->code));
×
UNCOV
230
      T_LONG_JMP(pTaskInfo->env, pOperator->pTaskInfo->code);
×
231
    }
232
    if (taosArrayGetSize(pExchangeInfo->pResultBlockList) == 0) {
1,889,341✔
233
      return NULL;
762,048✔
234
    } else {
235
      p = taosArrayGetP(pExchangeInfo->pResultBlockList, 0);
1,127,274✔
236
      taosArrayRemove(pExchangeInfo->pResultBlockList, 0);
1,127,190✔
237
      void* tmp = taosArrayPush(pExchangeInfo->pRecycledBlocks, &p);
1,127,078✔
238
      if (!tmp) {
1,126,930!
239
        code = terrno;
×
240
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
241
        pTaskInfo->code = code;
×
242
        T_LONG_JMP(pTaskInfo->env, code);
×
243
      }
244
      return p;
1,126,930✔
245
    }
246
  }
247
}
248

249
static int32_t loadRemoteDataNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
3,968,324✔
250
  int32_t        code = TSDB_CODE_SUCCESS;
3,968,324✔
251
  int32_t        lino = 0;
3,968,324✔
252
  SExchangeInfo* pExchangeInfo = pOperator->info;
3,968,324✔
253
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,968,324✔
254

255
  code = pOperator->fpSet._openFn(pOperator);
3,968,324✔
256
  QUERY_CHECK_CODE(code, lino, _end);
3,968,539!
257

258
  if (pOperator->status == OP_EXEC_DONE) {
3,968,539✔
259
    (*ppRes) = NULL;
60✔
260
    return code;
60✔
261
  }
262

263
  while (1) {
8,186✔
264
    SSDataBlock* pBlock = doLoadRemoteDataImpl(pOperator);
3,976,665✔
265
    if (pBlock == NULL) {
3,976,519✔
266
      (*ppRes) = NULL;
762,045✔
267
      return code;
762,045✔
268
    }
269

270
    code = doFilter(pBlock, pOperator->exprSupp.pFilterInfo, NULL);
3,214,474✔
271
    QUERY_CHECK_CODE(code, lino, _end);
3,214,490!
272

273
    if (blockDataGetNumOfRows(pBlock) == 0) {
3,214,490✔
274
      continue;
2✔
275
    }
276

277
    SLimitInfo* pLimitInfo = &pExchangeInfo->limitInfo;
3,214,459✔
278
    if (hasLimitOffsetInfo(pLimitInfo)) {
3,214,459✔
279
      int32_t status = handleLimitOffset(pOperator, pLimitInfo, pBlock, false);
21,144✔
280
      if (status == PROJECT_RETRIEVE_CONTINUE) {
21,140✔
281
        continue;
8,184✔
282
      } else if (status == PROJECT_RETRIEVE_DONE) {
12,956!
283
        if (pBlock->info.rows == 0) {
12,956!
284
          setOperatorCompleted(pOperator);
×
285
          (*ppRes) = NULL;
×
286
          return code;
×
287
        } else {
288
          (*ppRes) = pBlock;
12,956✔
289
          return code;
12,956✔
290
        }
291
      }
292
    } else {
293
      (*ppRes) = pBlock;
3,193,319✔
294
      return code;
3,193,319✔
295
    }
296
  }
297

298
_end:
×
299
  if (code != TSDB_CODE_SUCCESS) {
×
300
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
301
    pTaskInfo->code = code;
×
302
    T_LONG_JMP(pTaskInfo->env, code);
×
303
  }
304
  (*ppRes) = NULL;
×
305
  return code;
×
306
}
307

308
static int32_t initDataSource(int32_t numOfSources, SExchangeInfo* pInfo, const char* id) {
833,467✔
309
  pInfo->pSourceDataInfo = taosArrayInit(numOfSources, sizeof(SSourceDataInfo));
833,467✔
310
  if (pInfo->pSourceDataInfo == NULL) {
833,466✔
311
    return terrno;
7✔
312
  }
313

314
  if (pInfo->dynamicOp) {
833,459✔
315
    return TSDB_CODE_SUCCESS;
20,964✔
316
  }
317

318
  int32_t len = strlen(id) + 1;
812,495✔
319
  pInfo->pTaskId = taosMemoryCalloc(1, len);
812,495!
320
  if (!pInfo->pTaskId) {
812,621!
321
    return terrno;
×
322
  }
323
  tstrncpy(pInfo->pTaskId, id, len);
812,621✔
324
  for (int32_t i = 0; i < numOfSources; ++i) {
2,183,425✔
325
    SSourceDataInfo dataInfo = {0};
1,370,707✔
326
    dataInfo.status = EX_SOURCE_DATA_NOT_READY;
1,370,707✔
327
    dataInfo.taskId = pInfo->pTaskId;
1,370,707✔
328
    dataInfo.index = i;
1,370,707✔
329
    SSourceDataInfo* pDs = taosArrayPush(pInfo->pSourceDataInfo, &dataInfo);
1,370,707✔
330
    if (pDs == NULL) {
1,370,804!
331
      taosArrayDestroyEx(pInfo->pSourceDataInfo, freeSourceDataInfo);
×
332
      return terrno;
×
333
    }
334
  }
335

336
  return TSDB_CODE_SUCCESS;
812,718✔
337
}
338

339
static int32_t initExchangeOperator(SExchangePhysiNode* pExNode, SExchangeInfo* pInfo, const char* id) {
833,462✔
340
  size_t numOfSources = LIST_LENGTH(pExNode->pSrcEndPoints);
833,462!
341

342
  if (numOfSources == 0) {
833,462!
343
    qError("%s invalid number: %d of sources in exchange operator", id, (int32_t)numOfSources);
×
344
    return TSDB_CODE_INVALID_PARA;
×
345
  }
346
  pInfo->pFetchRpcHandles = taosArrayInit(numOfSources, sizeof(int64_t));
833,462✔
347
  if (!pInfo->pFetchRpcHandles) {
833,600!
348
    return terrno;
×
349
  }
350
  void* ret = taosArrayReserve(pInfo->pFetchRpcHandles, numOfSources);
833,600✔
351
  if (!ret) {
833,534!
352
    return terrno;
×
353
  }
354

355
  pInfo->pSources = taosArrayInit(numOfSources, sizeof(SDownstreamSourceNode));
833,534✔
356
  if (pInfo->pSources == NULL) {
833,435!
357
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
358
    return terrno;
×
359
  }
360

361
  if (pExNode->node.dynamicOp) {
833,435✔
362
    pInfo->pHashSources = tSimpleHashInit(numOfSources * 2, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
20,964✔
363
    if (NULL == pInfo->pHashSources) {
20,965!
364
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
365
      return terrno;
×
366
    }
367
  }
368

369
  for (int32_t i = 0; i < numOfSources; ++i) {
2,246,250✔
370
    SDownstreamSourceNode* pNode = (SDownstreamSourceNode*)nodesListGetNode((SNodeList*)pExNode->pSrcEndPoints, i);
1,412,731✔
371
    if (!pNode) {
1,412,793!
372
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
373
      return terrno;
×
374
    }
375
    void* tmp = taosArrayPush(pInfo->pSources, pNode);
1,412,793✔
376
    if (!tmp) {
1,412,844!
377
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
378
      return terrno;
×
379
    }
380
    SExchangeSrcIndex idx = {.srcIdx = i, .inUseIdx = -1};
1,412,844✔
381
    int32_t           code =
382
        tSimpleHashPut(pInfo->pHashSources, &pNode->addr.nodeId, sizeof(pNode->addr.nodeId), &idx, sizeof(idx));
1,412,844✔
383
    if (pInfo->pHashSources && code != TSDB_CODE_SUCCESS) {
1,412,814!
384
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
385
      return code;
×
386
    }
387
  }
388

389
  initLimitInfo(pExNode->node.pLimit, pExNode->node.pSlimit, &pInfo->limitInfo);
833,519✔
390
  int64_t refId = taosAddRef(exchangeObjRefPool, pInfo);
833,542✔
391
  if (refId < 0) {
833,564!
392
    int32_t code = terrno;
×
393
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
394
    return code;
×
395
  } else {
396
    pInfo->self = refId;
833,564✔
397
  }
398

399
  return initDataSource(numOfSources, pInfo, id);
833,564✔
400
}
401

402
int32_t createExchangeOperatorInfo(void* pTransporter, SExchangePhysiNode* pExNode, SExecTaskInfo* pTaskInfo,
833,434✔
403
                                   SOperatorInfo** pOptrInfo) {
404
  QRY_PARAM_CHECK(pOptrInfo);
833,434!
405

406
  int32_t        code = 0;
833,434✔
407
  int32_t        lino = 0;
833,434✔
408
  SExchangeInfo* pInfo = taosMemoryCalloc(1, sizeof(SExchangeInfo));
833,434!
409
  SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
833,471!
410
  if (pInfo == NULL || pOperator == NULL) {
833,471!
411
    code = terrno;
×
412
    goto _error;
×
413
  }
414

415
  pInfo->dynamicOp = pExNode->node.dynamicOp;
833,481✔
416
  code = initExchangeOperator(pExNode, pInfo, GET_TASKID(pTaskInfo));
833,481✔
417
  QUERY_CHECK_CODE(code, lino, _error);
833,528!
418

419
  code = tsem_init(&pInfo->ready, 0, 0);
833,528✔
420
  QUERY_CHECK_CODE(code, lino, _error);
833,540!
421

422
  pInfo->pDummyBlock = createDataBlockFromDescNode(pExNode->node.pOutputDataBlockDesc);
833,540✔
423
  QUERY_CHECK_NULL(pInfo->pDummyBlock, code, lino, _error, terrno);
833,550!
424

425
  pInfo->pResultBlockList = taosArrayInit(64, POINTER_BYTES);
833,550✔
426
  QUERY_CHECK_NULL(pInfo->pResultBlockList, code, lino, _error, terrno);
833,493!
427
  pInfo->pRecycledBlocks = taosArrayInit(64, POINTER_BYTES);
833,493✔
428
  QUERY_CHECK_NULL(pInfo->pRecycledBlocks, code, lino, _error, terrno);
833,470!
429

430
  SExchangeOpStopInfo stopInfo = {QUERY_NODE_PHYSICAL_PLAN_EXCHANGE, pInfo->self};
833,470✔
431
  code = qAppendTaskStopInfo(pTaskInfo, &stopInfo);
833,470✔
432
  QUERY_CHECK_CODE(code, lino, _error);
833,569!
433

434
  pInfo->seqLoadData = pExNode->seqRecvData;
833,569✔
435
  pInfo->pTransporter = pTransporter;
833,569✔
436

437
  setOperatorInfo(pOperator, "ExchangeOperator", QUERY_NODE_PHYSICAL_PLAN_EXCHANGE, false, OP_NOT_OPENED, pInfo,
833,569✔
438
                  pTaskInfo);
439
  pOperator->exprSupp.numOfExprs = taosArrayGetSize(pInfo->pDummyBlock->pDataBlock);
833,510✔
440

441
  code = filterInitFromNode((SNode*)pExNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0);
833,514✔
442
  QUERY_CHECK_CODE(code, lino, _error);
833,498!
443

444
  pOperator->fpSet = createOperatorFpSet(prepareLoadRemoteData, loadRemoteDataNext, NULL, destroyExchangeOperatorInfo,
833,498✔
445
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
446
  *pOptrInfo = pOperator;
833,430✔
447
  return TSDB_CODE_SUCCESS;
833,430✔
448

449
_error:
×
450
  if (code != TSDB_CODE_SUCCESS) {
×
451
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
452
    pTaskInfo->code = code;
×
453
  }
454
  if (pInfo != NULL) {
×
455
    doDestroyExchangeOperatorInfo(pInfo);
×
456
  }
457

458
  if (pOperator != NULL) {
×
459
    pOperator->info = NULL;
×
460
    destroyOperator(pOperator);
×
461
  }
462
  return code;
×
463
}
464

465
void destroyExchangeOperatorInfo(void* param) {
833,551✔
466
  SExchangeInfo* pExInfo = (SExchangeInfo*)param;
833,551✔
467
  int32_t        code = taosRemoveRef(exchangeObjRefPool, pExInfo->self);
833,551✔
468
  if (code != TSDB_CODE_SUCCESS) {
833,632!
469
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
470
  }
471
}
833,632✔
472

473
void freeBlock(void* pParam) {
1,896,431✔
474
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
1,896,431✔
475
  blockDataDestroy(pBlock);
1,896,431✔
476
}
1,896,483✔
477

478
void freeSourceDataInfo(void* p) {
1,371,918✔
479
  SSourceDataInfo* pInfo = (SSourceDataInfo*)p;
1,371,918✔
480
  taosMemoryFreeClear(pInfo->decompBuf);
1,371,918!
481
  taosMemoryFreeClear(pInfo->pRsp);
1,371,918!
482

483
  pInfo->decompBufSize = 0;
1,371,918✔
484
}
1,371,918✔
485

486
void doDestroyExchangeOperatorInfo(void* param) {
833,578✔
487
  if (param == NULL) {
833,578!
488
    return;
×
489
  }
490
  SExchangeInfo* pExInfo = (SExchangeInfo*)param;
833,578✔
491
  if (pExInfo->pFetchRpcHandles) {
833,578!
492
    for (int32_t i = 0; i < pExInfo->pFetchRpcHandles->size; ++i) {
2,246,740✔
493
      int64_t* pRpcHandle = taosArrayGet(pExInfo->pFetchRpcHandles, i);
1,413,120✔
494
      if (*pRpcHandle > 0) {
1,413,115✔
495
        SDownstreamSourceNode* pSource = taosArrayGet(pExInfo->pSources, i);
509✔
496
        (void)asyncFreeConnById(pExInfo->pTransporter, *pRpcHandle);
509✔
497
      }
498
    }
499
    taosArrayDestroy(pExInfo->pFetchRpcHandles);
833,620✔
500
  }
501

502
  taosArrayDestroy(pExInfo->pSources);
833,523✔
503
  taosArrayDestroyEx(pExInfo->pSourceDataInfo, freeSourceDataInfo);
833,617✔
504

505
  taosArrayDestroyEx(pExInfo->pResultBlockList, freeBlock);
833,564✔
506
  taosArrayDestroyEx(pExInfo->pRecycledBlocks, freeBlock);
833,616✔
507

508
  blockDataDestroy(pExInfo->pDummyBlock);
833,625✔
509
  tSimpleHashCleanup(pExInfo->pHashSources);
833,628✔
510

511
  int32_t code = tsem_destroy(&pExInfo->ready);
833,602✔
512
  if (code != TSDB_CODE_SUCCESS) {
833,585!
513
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
514
  }
515
  taosMemoryFreeClear(pExInfo->pTaskId);
833,585!
516

517
  taosMemoryFreeClear(param);
833,594!
518
}
519

520
int32_t loadRemoteDataCallback(void* param, SDataBuf* pMsg, int32_t code) {
1,413,683✔
521
  SFetchRspHandleWrapper* pWrapper = (SFetchRspHandleWrapper*)param;
1,413,683✔
522

523
  taosMemoryFreeClear(pMsg->pEpSet);
1,413,683!
524
  SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pWrapper->exchangeId);
1,413,683✔
525
  if (pExchangeInfo == NULL) {
1,415,192✔
526
    qWarn("failed to acquire exchange operator, since it may have been released, %p", pExchangeInfo);
82!
527
    taosMemoryFree(pMsg->pData);
82!
528
    return TSDB_CODE_SUCCESS;
82✔
529
  }
530

531
  int32_t          index = pWrapper->sourceIndex;
1,415,110✔
532
  SSourceDataInfo* pSourceDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, index);
1,415,110✔
533

534
  int64_t* pRpcHandle = taosArrayGet(pExchangeInfo->pFetchRpcHandles, index);
1,414,260✔
535
  if (pRpcHandle != NULL) {
1,414,139✔
536
    int32_t ret = asyncFreeConnById(pExchangeInfo->pTransporter, *pRpcHandle);
1,414,054✔
537
    if (ret != 0) {
1,415,205✔
538
      qDebug("failed to free rpc handle, code:%s, %p", tstrerror(ret), pExchangeInfo);
16,042✔
539
    }
540
    *pRpcHandle = -1;
1,414,506✔
541
  }
542

543
  if (!pSourceDataInfo) {
1,414,591!
544
    return terrno;
×
545
  }
546

547
  if (code == TSDB_CODE_SUCCESS) {
1,414,591!
548
    pSourceDataInfo->pRsp = pMsg->pData;
1,414,868✔
549

550
    SRetrieveTableRsp* pRsp = pSourceDataInfo->pRsp;
1,414,868✔
551
    pRsp->numOfRows = htobe64(pRsp->numOfRows);
1,414,868✔
552
    pRsp->compLen = htonl(pRsp->compLen);
1,414,035✔
553
    pRsp->payloadLen = htonl(pRsp->payloadLen);
1,414,035✔
554
    pRsp->numOfCols = htonl(pRsp->numOfCols);
1,414,035✔
555
    pRsp->useconds = htobe64(pRsp->useconds);
1,414,035✔
556
    pRsp->numOfBlocks = htonl(pRsp->numOfBlocks);
1,414,733✔
557

558
    qDebug("%s fetch rsp received, index:%d, blocks:%d, rows:%" PRId64 ", %p", pSourceDataInfo->taskId, index,
1,414,733✔
559
           pRsp->numOfBlocks, pRsp->numOfRows, pExchangeInfo);
560
  } else {
UNCOV
561
    taosMemoryFree(pMsg->pData);
×
UNCOV
562
    pSourceDataInfo->code = rpcCvtErrCode(code);
×
UNCOV
563
    if (pSourceDataInfo->code != code) {
×
564
      qError("%s fetch rsp received, index:%d, error:%s, cvted error: %s, %p", pSourceDataInfo->taskId, index,
×
565
             tstrerror(code), tstrerror(pSourceDataInfo->code), pExchangeInfo);
566
    } else {
UNCOV
567
      qError("%s fetch rsp received, index:%d, error:%s, %p", pSourceDataInfo->taskId, index, tstrerror(code),
×
568
             pExchangeInfo);
569
    }
570
  }
571

572
  tmemory_barrier();
1,414,750✔
573
  pSourceDataInfo->status = EX_SOURCE_DATA_READY;
1,414,750✔
574
  code = tsem_post(&pExchangeInfo->ready);
1,414,750✔
575
  if (code != TSDB_CODE_SUCCESS) {
1,414,088!
576
    qError("failed to invoke post when fetch rsp is ready, code:%s, %p", tstrerror(code), pExchangeInfo);
×
577
    return code;
×
578
  }
579

580
  code = taosReleaseRef(exchangeObjRefPool, pWrapper->exchangeId);
1,414,088✔
581
  if (code != TSDB_CODE_SUCCESS) {
1,415,296!
582
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
583
  }
584
  return code;
1,415,209✔
585
}
586

587
int32_t buildTableScanOperatorParam(SOperatorParam** ppRes, SArray* pUidList, int32_t srcOpType, bool tableSeq) {
904✔
588
  *ppRes = taosMemoryMalloc(sizeof(SOperatorParam));
904!
589
  if (NULL == *ppRes) {
904!
590
    return terrno;
×
591
  }
592

593
  STableScanOperatorParam* pScan = taosMemoryMalloc(sizeof(STableScanOperatorParam));
904!
594
  if (NULL == pScan) {
904!
595
    taosMemoryFreeClear(*ppRes);
×
596
    return terrno;
×
597
  }
598

599
  pScan->pUidList = taosArrayDup(pUidList, NULL);
904✔
600
  if (NULL == pScan->pUidList) {
904!
601
    taosMemoryFree(pScan);
×
602
    taosMemoryFreeClear(*ppRes);
×
603
    return terrno;
×
604
  }
605
  pScan->tableSeq = tableSeq;
904✔
606

607
  (*ppRes)->opType = srcOpType;
904✔
608
  (*ppRes)->downstreamIdx = 0;
904✔
609
  (*ppRes)->value = pScan;
904✔
610
  (*ppRes)->pChildren = NULL;
904✔
611

612
  return TSDB_CODE_SUCCESS;
904✔
613
}
614

615
int32_t doSendFetchDataRequest(SExchangeInfo* pExchangeInfo, SExecTaskInfo* pTaskInfo, int32_t sourceIndex) {
1,415,667✔
616
  int32_t          code = TSDB_CODE_SUCCESS;
1,415,667✔
617
  int32_t          lino = 0;
1,415,667✔
618
  SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, sourceIndex);
1,415,667✔
619
  if (!pDataInfo) {
1,415,675!
620
    return terrno;
×
621
  }
622

623
  if (EX_SOURCE_DATA_NOT_READY != pDataInfo->status) {
1,415,751!
624
    return TSDB_CODE_SUCCESS;
×
625
  }
626

627
  pDataInfo->status = EX_SOURCE_DATA_STARTED;
1,415,751✔
628
  SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, pDataInfo->index);
1,415,751✔
629
  if (!pSource) {
1,415,695!
630
    return terrno;
×
631
  }
632

633
  pDataInfo->startTime = taosGetTimestampUs();
1,415,681✔
634
  size_t totalSources = taosArrayGetSize(pExchangeInfo->pSources);
1,415,681✔
635

636
  SFetchRspHandleWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SFetchRspHandleWrapper));
1,415,708!
637
  QUERY_CHECK_NULL(pWrapper, code, lino, _end, terrno);
1,416,029!
638
  pWrapper->exchangeId = pExchangeInfo->self;
1,416,029✔
639
  pWrapper->sourceIndex = sourceIndex;
1,416,029✔
640

641
  if (pSource->localExec) {
1,416,029✔
642
    SDataBuf pBuf = {0};
16,037✔
643
    int32_t  code = (*pTaskInfo->localFetch.fp)(pTaskInfo->localFetch.handle, pSource->sId, pTaskInfo->id.queryId,
16,037✔
644
                                               pSource->clientId, pSource->taskId, 0, pSource->execId, &pBuf.pData,
645
                                               pTaskInfo->localFetch.explainRes);
646
    code = loadRemoteDataCallback(pWrapper, &pBuf, code);
16,037✔
647
    taosMemoryFree(pWrapper);
16,037!
648
    QUERY_CHECK_CODE(code, lino, _end);
16,037!
649
  } else {
650
    SResFetchReq req = {0};
1,399,992✔
651
    req.header.vgId = pSource->addr.nodeId;
1,399,992✔
652
    req.sId = pSource->sId;
1,399,992✔
653
    req.clientId = pSource->clientId;
1,399,992✔
654
    req.taskId = pSource->taskId;
1,399,992✔
655
    req.queryId = pTaskInfo->id.queryId;
1,399,992✔
656
    req.execId = pSource->execId;
1,399,992✔
657
    if (pDataInfo->pSrcUidList) {
1,399,992✔
658
      int32_t code =
659
          buildTableScanOperatorParam(&req.pOpParam, pDataInfo->pSrcUidList, pDataInfo->srcOpType, pDataInfo->tableSeq);
902✔
660
      taosArrayDestroy(pDataInfo->pSrcUidList);
902✔
661
      pDataInfo->pSrcUidList = NULL;
902✔
662
      if (TSDB_CODE_SUCCESS != code) {
902!
663
        pTaskInfo->code = code;
×
664
        taosMemoryFree(pWrapper);
×
665
        return pTaskInfo->code;
×
666
      }
667
    }
668

669
    int32_t msgSize = tSerializeSResFetchReq(NULL, 0, &req);
1,399,992✔
670
    if (msgSize < 0) {
1,399,513!
671
      pTaskInfo->code = msgSize;
×
672
      taosMemoryFree(pWrapper);
×
673
      freeOperatorParam(req.pOpParam, OP_GET_PARAM);
×
674
      return pTaskInfo->code;
×
675
    }
676

677
    void* msg = taosMemoryCalloc(1, msgSize);
1,399,513!
678
    if (NULL == msg) {
1,399,894!
679
      pTaskInfo->code = terrno;
×
680
      taosMemoryFree(pWrapper);
×
681
      freeOperatorParam(req.pOpParam, OP_GET_PARAM);
×
682
      return pTaskInfo->code;
×
683
    }
684

685
    msgSize = tSerializeSResFetchReq(msg, msgSize, &req);
1,399,894✔
686
    if (msgSize < 0) {
1,399,438!
687
      pTaskInfo->code = msgSize;
×
688
      taosMemoryFree(pWrapper);
×
689
      taosMemoryFree(msg);
×
690
      freeOperatorParam(req.pOpParam, OP_GET_PARAM);
×
691
      return pTaskInfo->code;
×
692
    }
693

694
    freeOperatorParam(req.pOpParam, OP_GET_PARAM);
1,399,438✔
695

696
    qDebug("%s build fetch msg and send to vgId:%d, ep:%s, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
1,399,367✔
697
           ", execId:%d, %p, %d/%" PRIzu,
698
           GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->addr.epSet.eps[0].fqdn, pSource->clientId,
699
           pSource->taskId, pSource->execId, pExchangeInfo, sourceIndex, totalSources);
700

701
    // send the fetch remote task result reques
702
    SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
1,399,372!
703
    if (NULL == pMsgSendInfo) {
1,399,611!
704
      taosMemoryFreeClear(msg);
×
705
      taosMemoryFree(pWrapper);
×
706
      qError("%s prepare message %d failed", GET_TASKID(pTaskInfo), (int32_t)sizeof(SMsgSendInfo));
×
707
      pTaskInfo->code = terrno;
×
708
      return pTaskInfo->code;
×
709
    }
710

711
    pMsgSendInfo->param = pWrapper;
1,399,611✔
712
    pMsgSendInfo->paramFreeFp = taosAutoMemoryFree;
1,399,611✔
713
    pMsgSendInfo->msgInfo.pData = msg;
1,399,611✔
714
    pMsgSendInfo->msgInfo.len = msgSize;
1,399,611✔
715
    pMsgSendInfo->msgType = pSource->fetchMsgType;
1,399,611✔
716
    pMsgSendInfo->fp = loadRemoteDataCallback;
1,399,611✔
717

718
    int64_t transporterId = 0;
1,399,611✔
719
    void* poolHandle = NULL;
1,399,611✔
720
    code = asyncSendMsgToServer(pExchangeInfo->pTransporter, &pSource->addr.epSet, &transporterId, pMsgSendInfo);
1,399,611✔
721
    QUERY_CHECK_CODE(code, lino, _end);
1,400,007!
722
    int64_t* pRpcHandle = taosArrayGet(pExchangeInfo->pFetchRpcHandles, sourceIndex);
1,400,007✔
723
    *pRpcHandle = transporterId;
1,399,957✔
724
  }
725

726
_end:
1,415,994✔
727
  if (code != TSDB_CODE_SUCCESS) {
1,415,994!
728
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
729
  }
730
  return code;
1,415,990✔
731
}
732

733
void updateLoadRemoteInfo(SLoadRemoteDataInfo* pInfo, int64_t numOfRows, int32_t dataLen, int64_t startTs,
1,297,176✔
734
                          SOperatorInfo* pOperator) {
735
  pInfo->totalRows += numOfRows;
1,297,176✔
736
  pInfo->totalSize += dataLen;
1,297,176✔
737
  pInfo->totalElapsed += (taosGetTimestampUs() - startTs);
1,297,244✔
738
  pOperator->resultInfo.totalRows += numOfRows;
1,297,244✔
739
}
1,297,244✔
740

741
int32_t extractDataBlockFromFetchRsp(SSDataBlock* pRes, char* pData, SArray* pColList, char** pNextStart) {
3,492,024✔
742
  int32_t      code = TSDB_CODE_SUCCESS;
3,492,024✔
743
  int32_t      lino = 0;
3,492,024✔
744
  SSDataBlock* pBlock = NULL;
3,492,024✔
745
  if (pColList == NULL) {  // data from other sources
3,492,024✔
746
    blockDataCleanup(pRes);
3,321,384✔
747
    code = blockDecode(pRes, pData, (const char**)pNextStart);
3,321,171✔
748
    if (code) {
3,320,856!
749
      return code;
×
750
    }
751
  } else {  // extract data according to pColList
752
    char* pStart = pData;
170,640✔
753

754
    int32_t numOfCols = htonl(*(int32_t*)pStart);
170,640✔
755
    pStart += sizeof(int32_t);
170,640✔
756

757
    // todo refactor:extract method
758
    SSysTableSchema* pSchema = (SSysTableSchema*)pStart;
170,640✔
759
    for (int32_t i = 0; i < numOfCols; ++i) {
2,748,538✔
760
      SSysTableSchema* p = (SSysTableSchema*)pStart;
2,577,898✔
761

762
      p->colId = htons(p->colId);
2,577,898✔
763
      p->bytes = htonl(p->bytes);
2,577,898✔
764
      pStart += sizeof(SSysTableSchema);
2,577,898✔
765
    }
766

767
    pBlock = NULL;
170,640✔
768
    code = createDataBlock(&pBlock);
170,640✔
769
    QUERY_CHECK_CODE(code, lino, _end);
170,778!
770

771
    for (int32_t i = 0; i < numOfCols; ++i) {
2,748,182✔
772
      SColumnInfoData idata = createColumnInfoData(pSchema[i].type, pSchema[i].bytes, pSchema[i].colId);
2,577,368✔
773
      code = blockDataAppendColInfo(pBlock, &idata);
2,584,119✔
774
      QUERY_CHECK_CODE(code, lino, _end);
2,577,404!
775
    }
776

777
    const char* pDummy = NULL;
170,814✔
778
    code = blockDecode(pBlock, pStart, &pDummy);
170,814✔
779
    QUERY_CHECK_CODE(code, lino, _end);
170,498!
780

781
    code = blockDataEnsureCapacity(pRes, pBlock->info.rows);
170,498✔
782
    QUERY_CHECK_CODE(code, lino, _end);
170,616!
783

784
    // data from mnode
785
    pRes->info.dataLoad = 1;
170,616✔
786
    pRes->info.rows = pBlock->info.rows;
170,616✔
787
    pRes->info.scanFlag = MAIN_SCAN;
170,616✔
788
    code = relocateColumnData(pRes, pColList, pBlock->pDataBlock, false);
170,616✔
789
    QUERY_CHECK_CODE(code, lino, _end);
170,675!
790

791
    blockDataDestroy(pBlock);
170,675✔
792
    pBlock = NULL;
170,818✔
793
  }
794

795
_end:
3,491,674✔
796
  if (code != TSDB_CODE_SUCCESS) {
3,491,674!
797
    blockDataDestroy(pBlock);
×
798
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
799
  }
800
  return code;
3,491,615✔
801
}
802

803
void setAllSourcesCompleted(SOperatorInfo* pOperator) {
586,168✔
804
  SExchangeInfo* pExchangeInfo = pOperator->info;
586,168✔
805
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
586,168✔
806

807
  SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
586,168✔
808
  size_t               totalSources = taosArrayGetSize(pExchangeInfo->pSources);
586,168✔
809
  qDebug("%s all %" PRIzu " sources are exhausted, total rows: %" PRIu64 ", %.2f Kb, elapsed:%.2f ms",
586,174✔
810
         GET_TASKID(pTaskInfo), totalSources, pLoadInfo->totalRows, pLoadInfo->totalSize / 1024.0,
811
         pLoadInfo->totalElapsed / 1000.0);
812

813
  setOperatorCompleted(pOperator);
586,174✔
814
}
586,177✔
815

816
int32_t getCompletedSources(const SArray* pArray, int32_t* pRes) {
2,173,267✔
817
  int32_t code = TSDB_CODE_SUCCESS;
2,173,267✔
818
  int32_t lino = 0;
2,173,267✔
819
  size_t  total = taosArrayGetSize(pArray);
2,173,267✔
820

821
  int32_t completed = 0;
2,172,991✔
822
  for (int32_t k = 0; k < total; ++k) {
7,360,667✔
823
    SSourceDataInfo* p = taosArrayGet(pArray, k);
5,187,851✔
824
    QUERY_CHECK_NULL(p, code, lino, _end, terrno);
5,187,699✔
825
    if (p->status == EX_SOURCE_DATA_EXHAUSTED) {
5,187,676✔
826
      completed += 1;
2,543,717✔
827
    }
828
  }
829

830
  *pRes = completed;
2,172,816✔
831
_end:
2,172,816✔
832
  if (code != TSDB_CODE_SUCCESS) {
2,172,816!
833
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
834
  }
835
  return code;
2,173,059✔
836
}
837

838
int32_t prepareConcurrentlyLoad(SOperatorInfo* pOperator) {
811,706✔
839
  SExchangeInfo* pExchangeInfo = pOperator->info;
811,706✔
840
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
811,706✔
841

842
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSourceDataInfo);
811,706✔
843
  int64_t startTs = taosGetTimestampUs();
811,915✔
844

845
  // Asynchronously send all fetch requests to all sources.
846
  for (int32_t i = 0; i < totalSources; ++i) {
2,181,656✔
847
    int32_t code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, i);
1,369,695✔
848
    if (code != TSDB_CODE_SUCCESS) {
1,369,741!
849
      pTaskInfo->code = code;
×
850
      return code;
×
851
    }
852
  }
853

854
  int64_t endTs = taosGetTimestampUs();
811,963✔
855
  qDebug("%s send all fetch requests to %" PRIzu " sources completed, elapsed:%.2fms", GET_TASKID(pTaskInfo),
811,963✔
856
         totalSources, (endTs - startTs) / 1000.0);
857

858
  pOperator->status = OP_RES_TO_RETURN;
811,966✔
859
  pOperator->cost.openCost = taosGetTimestampUs() - startTs;
811,961✔
860
  if (isTaskKilled(pTaskInfo)) {
811,961!
UNCOV
861
    T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
862
  }
863

864
  return TSDB_CODE_SUCCESS;
811,968✔
865
}
866

867
int32_t doExtractResultBlocks(SExchangeInfo* pExchangeInfo, SSourceDataInfo* pDataInfo) {
1,127,419✔
868
  int32_t            code = TSDB_CODE_SUCCESS;
1,127,419✔
869
  int32_t            lino = 0;
1,127,419✔
870
  SRetrieveTableRsp* pRetrieveRsp = pDataInfo->pRsp;
1,127,419✔
871
  SSDataBlock*       pb = NULL;
1,127,419✔
872

873
  char* pNextStart = pRetrieveRsp->data;
1,127,419✔
874
  char* pStart = pNextStart;
1,127,419✔
875

876
  int32_t index = 0;
1,127,419✔
877

878
  if (pRetrieveRsp->compressed) {  // decompress the data
1,127,419!
879
    if (pDataInfo->decompBuf == NULL) {
×
880
      pDataInfo->decompBuf = taosMemoryMalloc(pRetrieveRsp->payloadLen);
×
881
      QUERY_CHECK_NULL(pDataInfo->decompBuf, code, lino, _end, terrno);
×
882
      pDataInfo->decompBufSize = pRetrieveRsp->payloadLen;
×
883
    } else {
884
      if (pDataInfo->decompBufSize < pRetrieveRsp->payloadLen) {
×
885
        char* p = taosMemoryRealloc(pDataInfo->decompBuf, pRetrieveRsp->payloadLen);
×
886
        QUERY_CHECK_NULL(p, code, lino, _end, terrno);
×
887
        if (p != NULL) {
×
888
          pDataInfo->decompBuf = p;
×
889
          pDataInfo->decompBufSize = pRetrieveRsp->payloadLen;
×
890
        }
891
      }
892
    }
893
  }
894

895
  while (index++ < pRetrieveRsp->numOfBlocks) {
4,448,269✔
896
    pStart = pNextStart;
3,321,481✔
897

898
    if (taosArrayGetSize(pExchangeInfo->pRecycledBlocks) > 0) {
3,321,481✔
899
      pb = *(SSDataBlock**)taosArrayPop(pExchangeInfo->pRecycledBlocks);
1,425,515✔
900
      blockDataCleanup(pb);
1,425,468✔
901
    } else {
902
      code = createOneDataBlock(pExchangeInfo->pDummyBlock, false, &pb);
1,895,956✔
903
      QUERY_CHECK_NULL(pb, code, lino, _end, code);
1,896,109!
904
    }
905

906
    int32_t compLen = *(int32_t*)pStart;
3,321,374✔
907
    pStart += sizeof(int32_t);
3,321,374✔
908

909
    int32_t rawLen = *(int32_t*)pStart;
3,321,374✔
910
    pStart += sizeof(int32_t);
3,321,374✔
911
    QUERY_CHECK_CONDITION((compLen <= rawLen && compLen != 0), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
3,321,374!
912

913
    pNextStart = pStart + compLen;
3,321,374✔
914
    if (pRetrieveRsp->compressed && (compLen < rawLen)) {
3,321,374!
915
      int32_t t = tsDecompressString(pStart, compLen, 1, pDataInfo->decompBuf, rawLen, ONE_STAGE_COMP, NULL, 0);
×
916
      QUERY_CHECK_CONDITION((t == rawLen), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
×
917
      pStart = pDataInfo->decompBuf;
×
918
    }
919

920
    code = extractDataBlockFromFetchRsp(pb, pStart, NULL, &pStart);
3,321,374✔
921
    if (code != 0) {
3,320,882!
922
      taosMemoryFreeClear(pDataInfo->pRsp);
×
923
      goto _end;
×
924
    }
925

926
    void* tmp = taosArrayPush(pExchangeInfo->pResultBlockList, &pb);
3,320,882✔
927
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
3,320,850!
928
    pb = NULL;
3,320,850✔
929
  }
930

931
_end:
1,126,788✔
932
  if (code != TSDB_CODE_SUCCESS) {
1,126,788!
933
    blockDataDestroy(pb);
×
934
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
935
  }
936
  return code;
1,126,848✔
937
}
938

939
int32_t seqLoadRemoteData(SOperatorInfo* pOperator) {
2,033✔
940
  SExchangeInfo* pExchangeInfo = pOperator->info;
2,033✔
941
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
2,033✔
942

943
  int32_t code = 0;
2,033✔
944
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSources);
2,033✔
945
  int64_t startTs = taosGetTimestampUs();
2,033✔
946

947
  while (1) {
787✔
948
    if (pExchangeInfo->current >= totalSources) {
2,820✔
949
      setAllSourcesCompleted(pOperator);
797✔
950
      return TSDB_CODE_SUCCESS;
797✔
951
    }
952

953
    SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, pExchangeInfo->current);
2,023✔
954
    if (!pDataInfo) {
2,023!
955
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
956
      pTaskInfo->code = terrno;
×
957
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
958
    }
959
    pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
2,023✔
960

961
    code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, pExchangeInfo->current);
2,023✔
962
    if (code != TSDB_CODE_SUCCESS) {
2,023!
963
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
964
      pTaskInfo->code = code;
×
965
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
966
    }
967

968
    code = exchangeWait(pOperator, pExchangeInfo);
2,023✔
969
    if (code != TSDB_CODE_SUCCESS || isTaskKilled(pTaskInfo)) {
2,023!
970
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
971
    }
972

973
    SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, pExchangeInfo->current);
2,023✔
974
    if (!pSource) {
2,023!
975
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
976
      pTaskInfo->code = terrno;
×
977
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
978
    }
979

980
    if (pDataInfo->code != TSDB_CODE_SUCCESS) {
2,023!
981
      qError("%s vgId:%d, clientId:0x%" PRIx64 " taskID:0x%" PRIx64 " execId:%d error happens, code:%s",
×
982
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
983
             tstrerror(pDataInfo->code));
984
      pOperator->pTaskInfo->code = pDataInfo->code;
×
985
      return pOperator->pTaskInfo->code;
×
986
    }
987

988
    SRetrieveTableRsp*   pRsp = pDataInfo->pRsp;
2,023✔
989
    SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
2,023✔
990

991
    if (pRsp->numOfRows == 0) {
2,023✔
992
      qDebug("%s vgId:%d, clientId:0x%" PRIx64 " taskID:0x%" PRIx64
787✔
993
             " execId:%d %d of total completed, rowsOfSource:%" PRIu64 ", totalRows:%" PRIu64 " try next",
994
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
995
             pExchangeInfo->current + 1, pDataInfo->totalRows, pLoadInfo->totalRows);
996

997
      pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
787✔
998
      pExchangeInfo->current += 1;
787✔
999
      taosMemoryFreeClear(pDataInfo->pRsp);
787!
1000
      continue;
787✔
1001
    }
1002

1003
    code = doExtractResultBlocks(pExchangeInfo, pDataInfo);
1,236✔
1004
    if (code != TSDB_CODE_SUCCESS) {
1,236!
1005
      goto _error;
×
1006
    }
1007

1008
    SRetrieveTableRsp* pRetrieveRsp = pDataInfo->pRsp;
1,236✔
1009
    if (pRsp->completed == 1) {
1,236✔
1010
      qDebug("%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64 " execId:%d numOfRows:%" PRId64
1,042✔
1011
             ", rowsOfSource:%" PRIu64 ", totalRows:%" PRIu64 ", totalBytes:%" PRIu64 " try next %d/%" PRIzu,
1012
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
1013
             pRetrieveRsp->numOfRows, pDataInfo->totalRows, pLoadInfo->totalRows, pLoadInfo->totalSize,
1014
             pExchangeInfo->current + 1, totalSources);
1015

1016
      pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
1,042✔
1017
      pExchangeInfo->current += 1;
1,042✔
1018
    } else {
1019
      qDebug("%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64 " execId:%d numOfRows:%" PRId64
194!
1020
             ", totalRows:%" PRIu64 ", totalBytes:%" PRIu64,
1021
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
1022
             pRetrieveRsp->numOfRows, pLoadInfo->totalRows, pLoadInfo->totalSize);
1023
    }
1024

1025
    updateLoadRemoteInfo(pLoadInfo, pRetrieveRsp->numOfRows, pRetrieveRsp->compLen, startTs, pOperator);
1,236✔
1026
    pDataInfo->totalRows += pRetrieveRsp->numOfRows;
1,236✔
1027

1028
    taosMemoryFreeClear(pDataInfo->pRsp);
1,236!
1029
    return TSDB_CODE_SUCCESS;
1,236✔
1030
  }
1031

1032
_error:
×
1033
  pTaskInfo->code = code;
×
1034
  return code;
×
1035
}
1036

1037
int32_t addSingleExchangeSource(SOperatorInfo* pOperator, SExchangeOperatorBasicParam* pBasicParam) {
902✔
1038
  SExchangeInfo*     pExchangeInfo = pOperator->info;
902✔
1039
  SExchangeSrcIndex* pIdx = tSimpleHashGet(pExchangeInfo->pHashSources, &pBasicParam->vgId, sizeof(pBasicParam->vgId));
902✔
1040
  if (NULL == pIdx) {
902!
1041
    qError("No exchange source for vgId: %d", pBasicParam->vgId);
×
1042
    return TSDB_CODE_INVALID_PARA;
×
1043
  }
1044

1045
  if (pIdx->inUseIdx < 0) {
902✔
1046
    SSourceDataInfo dataInfo = {0};
878✔
1047
    dataInfo.status = EX_SOURCE_DATA_NOT_READY;
878✔
1048
    dataInfo.taskId = pExchangeInfo->pTaskId;
878✔
1049
    dataInfo.index = pIdx->srcIdx;
878✔
1050
    dataInfo.pSrcUidList = taosArrayDup(pBasicParam->uidList, NULL);
878✔
1051
    if (dataInfo.pSrcUidList == NULL) {
878!
1052
      return terrno;
×
1053
    }
1054

1055
    dataInfo.srcOpType = pBasicParam->srcOpType;
878✔
1056
    dataInfo.tableSeq = pBasicParam->tableSeq;
878✔
1057

1058
    void* tmp = taosArrayPush(pExchangeInfo->pSourceDataInfo, &dataInfo);
878✔
1059
    if (!tmp) {
878!
1060
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1061
      return terrno;
×
1062
    }
1063
    pIdx->inUseIdx = taosArrayGetSize(pExchangeInfo->pSourceDataInfo) - 1;
878✔
1064
  } else {
1065
    SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, pIdx->inUseIdx);
24✔
1066
    if (!pDataInfo) {
24!
1067
      return terrno;
×
1068
    }
1069
    if (pDataInfo->status == EX_SOURCE_DATA_EXHAUSTED) {
24!
1070
      pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
24✔
1071
    }
1072
    pDataInfo->pSrcUidList = taosArrayDup(pBasicParam->uidList, NULL);
24✔
1073
    if (pDataInfo->pSrcUidList == NULL) {
24!
1074
      return terrno;
×
1075
    }
1076

1077
    pDataInfo->srcOpType = pBasicParam->srcOpType;
24✔
1078
    pDataInfo->tableSeq = pBasicParam->tableSeq;
24✔
1079
  }
1080

1081
  return TSDB_CODE_SUCCESS;
902✔
1082
}
1083

1084
int32_t addDynamicExchangeSource(SOperatorInfo* pOperator) {
538✔
1085
  SExchangeInfo*               pExchangeInfo = pOperator->info;
538✔
1086
  int32_t                      code = TSDB_CODE_SUCCESS;
538✔
1087
  SExchangeOperatorBasicParam* pBasicParam = NULL;
538✔
1088
  SExchangeOperatorParam*      pParam = (SExchangeOperatorParam*)pOperator->pOperatorGetParam->value;
538✔
1089
  if (pParam->multiParams) {
538✔
1090
    SExchangeOperatorBatchParam* pBatch = (SExchangeOperatorBatchParam*)pOperator->pOperatorGetParam->value;
490✔
1091
    int32_t                      iter = 0;
490✔
1092
    while (NULL != (pBasicParam = tSimpleHashIterate(pBatch->pBatchs, pBasicParam, &iter))) {
1,344✔
1093
      code = addSingleExchangeSource(pOperator, pBasicParam);
854✔
1094
      if (code) {
854!
1095
        return code;
×
1096
      }
1097
    }
1098
  } else {
1099
    pBasicParam = &pParam->basic;
48✔
1100
    code = addSingleExchangeSource(pOperator, pBasicParam);
48✔
1101
  }
1102

1103
  freeOperatorParam(pOperator->pOperatorGetParam, OP_GET_PARAM);
538✔
1104
  pOperator->pOperatorGetParam = NULL;
538✔
1105

1106
  return TSDB_CODE_SUCCESS;
538✔
1107
}
1108

1109
int32_t prepareLoadRemoteData(SOperatorInfo* pOperator) {
4,196,380✔
1110
  SExchangeInfo* pExchangeInfo = pOperator->info;
4,196,380✔
1111
  int32_t        code = TSDB_CODE_SUCCESS;
4,196,380✔
1112
  int32_t        lino = 0;
4,196,380✔
1113
  if ((OPTR_IS_OPENED(pOperator) && !pExchangeInfo->dynamicOp) ||
4,196,380✔
1114
      (pExchangeInfo->dynamicOp && NULL == pOperator->pOperatorGetParam)) {
814,788✔
1115
    return TSDB_CODE_SUCCESS;
3,383,938✔
1116
  }
1117

1118
  if (pExchangeInfo->dynamicOp) {
812,442✔
1119
    code = addDynamicExchangeSource(pOperator);
538✔
1120
    QUERY_CHECK_CODE(code, lino, _end);
538!
1121
  }
1122

1123
  int64_t st = taosGetTimestampUs();
812,687✔
1124

1125
  if (!pExchangeInfo->seqLoadData) {
812,687✔
1126
    code = prepareConcurrentlyLoad(pOperator);
811,908✔
1127
    QUERY_CHECK_CODE(code, lino, _end);
811,958!
1128
    pExchangeInfo->openedTs = taosGetTimestampUs();
811,955✔
1129
  }
1130

1131
  OPTR_SET_OPENED(pOperator);
812,734✔
1132
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
812,776✔
1133

1134
_end:
812,776✔
1135
  if (code != TSDB_CODE_SUCCESS) {
812,776!
1136
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1137
    pOperator->pTaskInfo->code = code;
×
1138
    T_LONG_JMP(pOperator->pTaskInfo->env, code);
×
1139
  }
1140
  return TSDB_CODE_SUCCESS;
812,776✔
1141
}
1142

1143
int32_t handleLimitOffset(SOperatorInfo* pOperator, SLimitInfo* pLimitInfo, SSDataBlock* pBlock, bool holdDataInBuf) {
21,140✔
1144
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
21,140✔
1145

1146
  if (pLimitInfo->remainGroupOffset > 0) {
21,140!
1147
    if (pLimitInfo->currentGroupId == 0) {  // it is the first group
×
1148
      pLimitInfo->currentGroupId = pBlock->info.id.groupId;
×
1149
      blockDataCleanup(pBlock);
×
1150
      return PROJECT_RETRIEVE_CONTINUE;
×
1151
    } else if (pLimitInfo->currentGroupId != pBlock->info.id.groupId) {
×
1152
      // now it is the data from a new group
1153
      pLimitInfo->remainGroupOffset -= 1;
×
1154

1155
      // ignore data block in current group
1156
      if (pLimitInfo->remainGroupOffset > 0) {
×
1157
        blockDataCleanup(pBlock);
×
1158
        return PROJECT_RETRIEVE_CONTINUE;
×
1159
      }
1160
    }
1161

1162
    // set current group id of the project operator
1163
    pLimitInfo->currentGroupId = pBlock->info.id.groupId;
×
1164
  }
1165

1166
  // here check for a new group data, we need to handle the data of the previous group.
1167
  if (pLimitInfo->currentGroupId != 0 && pLimitInfo->currentGroupId != pBlock->info.id.groupId) {
21,140✔
1168
    pLimitInfo->numOfOutputGroups += 1;
783✔
1169
    if ((pLimitInfo->slimit.limit > 0) && (pLimitInfo->slimit.limit <= pLimitInfo->numOfOutputGroups)) {
783!
1170
      pOperator->status = OP_EXEC_DONE;
×
1171
      blockDataCleanup(pBlock);
×
1172

1173
      return PROJECT_RETRIEVE_DONE;
×
1174
    }
1175

1176
    // reset the value for a new group data
1177
    resetLimitInfoForNextGroup(pLimitInfo);
783✔
1178
    // existing rows that belongs to previous group.
1179
    if (pBlock->info.rows > 0) {
783!
1180
      return PROJECT_RETRIEVE_DONE;
783✔
1181
    }
1182
  }
1183

1184
  // here we reach the start position, according to the limit/offset requirements.
1185

1186
  // set current group id
1187
  pLimitInfo->currentGroupId = pBlock->info.id.groupId;
20,357✔
1188

1189
  bool limitReached = applyLimitOffset(pLimitInfo, pBlock, pTaskInfo);
20,357✔
1190
  if (pBlock->info.rows == 0) {
20,357✔
1191
    return PROJECT_RETRIEVE_CONTINUE;
8,184✔
1192
  } else {
1193
    if (limitReached && (pLimitInfo->slimit.limit > 0 && pLimitInfo->slimit.limit <= pLimitInfo->numOfOutputGroups)) {
12,173!
1194
      setOperatorCompleted(pOperator);
×
1195
      return PROJECT_RETRIEVE_DONE;
×
1196
    }
1197
  }
1198

1199
  // todo optimize performance
1200
  // If there are slimit/soffset value exists, multi-round result can not be packed into one group, since the
1201
  // they may not belong to the same group the limit/offset value is not valid in this case.
1202
  if ((!holdDataInBuf) || (pBlock->info.rows >= pOperator->resultInfo.threshold) || hasSlimitOffsetInfo(pLimitInfo)) {
12,173!
1203
    return PROJECT_RETRIEVE_DONE;
12,173✔
1204
  } else {  // not full enough, continue to accumulate the output data in the buffer.
1205
    return PROJECT_RETRIEVE_CONTINUE;
×
1206
  }
1207
}
1208

1209
static int32_t exchangeWait(SOperatorInfo* pOperator, SExchangeInfo* pExchangeInfo) {
1,413,997✔
1210
  SExecTaskInfo* pTask = pOperator->pTaskInfo;
1,413,997✔
1211
  int32_t        code = TSDB_CODE_SUCCESS;
1,413,997✔
1212
  if (pTask->pWorkerCb) {
1,413,997!
1213
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
1,414,073✔
1214
    if (code != TSDB_CODE_SUCCESS) {
1,414,938!
1215
      pTask->code = code;
×
1216
      return pTask->code;
×
1217
    }
1218
  }
1219

1220
  code = tsem_wait(&pExchangeInfo->ready);
1,414,862✔
1221
  if (code != TSDB_CODE_SUCCESS) {
1,414,810!
1222
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1223
    pTask->code = code;
×
1224
    return pTask->code;
×
1225
  }
1226

1227
  if (pTask->pWorkerCb) {
1,414,837!
1228
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
1,414,845✔
1229
    if (code != TSDB_CODE_SUCCESS) {
1,414,933!
1230
      pTask->code = code;
×
1231
      return pTask->code;
×
1232
    }
1233
  }
1234
  return TSDB_CODE_SUCCESS;
1,414,925✔
1235
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc