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

taosdata / TDengine / #3608

12 Feb 2025 05:57AM UTC coverage: 63.066% (+1.4%) from 61.715%
#3608

push

travis-ci

web-flow
Merge pull request #29746 from taosdata/merge/mainto3.02

merge: from main to 3.0 branch

140199 of 286257 branches covered (48.98%)

Branch coverage included in aggregate %.

89 of 161 new or added lines in 18 files covered. (55.28%)

3211 existing lines in 190 files now uncovered.

218998 of 283298 relevant lines covered (77.3%)

5949310.66 hits per line

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

58.05
/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,463,488✔
66
                                           SExecTaskInfo* pTaskInfo) {
67
  int32_t code = 0;
1,463,488✔
68
  int32_t lino = 0;
1,463,488✔
69
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSourceDataInfo);
1,463,488✔
70
  int32_t completed = 0;
1,463,530✔
71
  code = getCompletedSources(pExchangeInfo->pSourceDataInfo, &completed);
1,463,530✔
72
  if (code != TSDB_CODE_SUCCESS) {
1,463,535!
73
    pTaskInfo->code = code;
×
74
    T_LONG_JMP(pTaskInfo->env, code);
×
75
  }
76
  if (completed == totalSources) {
1,463,535✔
77
    setAllSourcesCompleted(pOperator);
489,662✔
78
    return;
1,463,571✔
79
  }
80

81
  SSourceDataInfo* pDataInfo = NULL;
973,873✔
82

83
  while (1) {
104,880✔
84
    qDebug("prepare wait for ready, %p, %s", pExchangeInfo, GET_TASKID(pTaskInfo));
1,078,753✔
85
    code = exchangeWait(pOperator, pExchangeInfo);
1,078,754✔
86

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

91
    for (int32_t i = 0; i < totalSources; ++i) {
1,445,888✔
92
      pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, i);
1,445,857✔
93
      QUERY_CHECK_NULL(pDataInfo, code, lino, _error, terrno);
1,445,859!
94
      if (pDataInfo->status == EX_SOURCE_DATA_EXHAUSTED) {
1,445,859✔
95
        continue;
290,588✔
96
      }
97

98
      if (pDataInfo->status != EX_SOURCE_DATA_READY) {
1,155,271✔
99
        continue;
76,502✔
100
      }
101

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

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

112
      // todo
113
      SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
1,078,790✔
114
      if (pRsp->numOfRows == 0) {
1,078,790✔
115
        if (NULL != pDataInfo->pSrcUidList) {
276,736!
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;
276,736✔
124
          qDebug("%s vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
276,736✔
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);
276,738!
129
        }
130
        break;
276,737✔
131
      }
132

133
      code = doExtractResultBlocks(pExchangeInfo, pDataInfo);
802,054✔
134
      if (code != TSDB_CODE_SUCCESS) {
802,015!
135
        goto _error;
×
136
      }
137

138
      SRetrieveTableRsp* pRetrieveRsp = pDataInfo->pRsp;
802,015✔
139
      updateLoadRemoteInfo(pLoadInfo, pRetrieveRsp->numOfRows, pRetrieveRsp->compLen, pDataInfo->startTime, pOperator);
802,015✔
140
      pDataInfo->totalRows += pRetrieveRsp->numOfRows;
802,042✔
141

142
      if (pRsp->completed == 1) {
802,042✔
143
        pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
754,622✔
144
        qDebug("%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
754,622✔
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
47,420✔
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);
802,043!
158

159
      if (pDataInfo->status != EX_SOURCE_DATA_EXHAUSTED || NULL != pDataInfo->pSrcUidList) {
802,068!
160
        pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
47,435✔
161
        code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, i);
47,435✔
162
        if (code != TSDB_CODE_SUCCESS) {
47,419!
163
          taosMemoryFreeClear(pDataInfo->pRsp);
×
164
          goto _error;
×
165
        }
166
      }
167
      return;
973,906✔
168
    }  // end loop
169

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

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

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

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

193
  SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
3,525,407✔
194
  if (pOperator->status == OP_EXEC_DONE) {
3,525,407!
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,525,407✔
203
  if (taosArrayGetSize(pExchangeInfo->pResultBlockList) > 0) {
3,525,407✔
204
    p = taosArrayGetP(pExchangeInfo->pResultBlockList, 0);
2,059,876✔
205
    taosArrayRemove(pExchangeInfo->pResultBlockList, 0);
2,059,843✔
206
  }
207

208
  if (p != NULL) {
3,525,402✔
209
    void* tmp = taosArrayPush(pExchangeInfo->pRecycledBlocks, &p);
2,059,861✔
210
    if (!tmp) {
2,059,869!
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,059,869✔
217
  } else {
218
    if (pExchangeInfo->seqLoadData) {
1,465,541✔
219
      code = seqLoadRemoteData(pOperator);
2,022✔
220
      if (code != TSDB_CODE_SUCCESS) {
2,022!
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,463,519✔
227
    }
228
    if (TSDB_CODE_SUCCESS != pOperator->pTaskInfo->code) {
1,465,567!
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,465,567✔
233
      return NULL;
662,296✔
234
    } else {
235
      p = taosArrayGetP(pExchangeInfo->pResultBlockList, 0);
803,267✔
236
      taosArrayRemove(pExchangeInfo->pResultBlockList, 0);
803,284✔
237
      void* tmp = taosArrayPush(pExchangeInfo->pRecycledBlocks, &p);
803,277✔
238
      if (!tmp) {
803,284!
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;
803,284✔
245
    }
246
  }
247
}
248

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

255
  code = pOperator->fpSet._openFn(pOperator);
3,516,736✔
256
  QUERY_CHECK_CODE(code, lino, _end);
3,516,874!
257

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

263
  while (1) {
8,611✔
264
    SSDataBlock* pBlock = doLoadRemoteDataImpl(pOperator);
3,525,425✔
265
    if (pBlock == NULL) {
3,525,337✔
266
      (*ppRes) = NULL;
662,295✔
267
      return code;
662,295✔
268
    }
269

270
    code = doFilter(pBlock, pOperator->exprSupp.pFilterInfo, NULL);
2,863,042✔
271
    QUERY_CHECK_CODE(code, lino, _end);
2,863,069!
272

273
    if (blockDataGetNumOfRows(pBlock) == 0) {
2,863,069✔
274
      continue;
2✔
275
    }
276

277
    SLimitInfo* pLimitInfo = &pExchangeInfo->limitInfo;
2,863,066✔
278
    if (hasLimitOffsetInfo(pLimitInfo)) {
2,863,066✔
279
      int32_t status = handleLimitOffset(pOperator, pLimitInfo, pBlock, false);
22,539✔
280
      if (status == PROJECT_RETRIEVE_CONTINUE) {
22,524✔
281
        continue;
8,609✔
282
      } else if (status == PROJECT_RETRIEVE_DONE) {
13,915!
283
        if (pBlock->info.rows == 0) {
13,915!
284
          setOperatorCompleted(pOperator);
×
285
          (*ppRes) = NULL;
×
286
          return code;
×
287
        } else {
288
          (*ppRes) = pBlock;
13,915✔
289
          return code;
13,915✔
290
        }
291
      }
292
    } else {
293
      (*ppRes) = pBlock;
2,840,542✔
294
      return code;
2,840,542✔
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) {
743,180✔
309
  pInfo->pSourceDataInfo = taosArrayInit(numOfSources, sizeof(SSourceDataInfo));
743,180✔
310
  if (pInfo->pSourceDataInfo == NULL) {
743,182✔
311
    return terrno;
4✔
312
  }
313

314
  if (pInfo->dynamicOp) {
743,178✔
315
    return TSDB_CODE_SUCCESS;
22,046✔
316
  }
317

318
  int32_t len = strlen(id) + 1;
721,132✔
319
  pInfo->pTaskId = taosMemoryCalloc(1, len);
721,132!
320
  if (!pInfo->pTaskId) {
721,230!
321
    return terrno;
×
322
  }
323
  tstrncpy(pInfo->pTaskId, id, len);
721,230✔
324
  for (int32_t i = 0; i < numOfSources; ++i) {
1,754,632✔
325
    SSourceDataInfo dataInfo = {0};
1,033,310✔
326
    dataInfo.status = EX_SOURCE_DATA_NOT_READY;
1,033,310✔
327
    dataInfo.taskId = pInfo->pTaskId;
1,033,310✔
328
    dataInfo.index = i;
1,033,310✔
329
    SSourceDataInfo* pDs = taosArrayPush(pInfo->pSourceDataInfo, &dataInfo);
1,033,310✔
330
    if (pDs == NULL) {
1,033,402!
331
      taosArrayDestroyEx(pInfo->pSourceDataInfo, freeSourceDataInfo);
×
332
      return terrno;
×
333
    }
334
  }
335

336
  return TSDB_CODE_SUCCESS;
721,322✔
337
}
338

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

342
  if (numOfSources == 0) {
743,186!
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));
743,186✔
347
  if (!pInfo->pFetchRpcHandles) {
743,269!
348
    return terrno;
×
349
  }
350
  void* ret = taosArrayReserve(pInfo->pFetchRpcHandles, numOfSources);
743,269✔
351
  if (!ret) {
743,230!
352
    return terrno;
×
353
  }
354

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

361
  if (pExNode->node.dynamicOp) {
743,138✔
362
    pInfo->pHashSources = tSimpleHashInit(numOfSources * 2, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
22,046✔
363
    if (NULL == pInfo->pHashSources) {
22,050!
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) {
1,820,704✔
370
    SDownstreamSourceNode* pNode = (SDownstreamSourceNode*)nodesListGetNode((SNodeList*)pExNode->pSrcEndPoints, i);
1,077,515✔
371
    if (!pNode) {
1,077,568!
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,077,568✔
376
    if (!tmp) {
1,077,580!
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,077,580✔
381
    int32_t           code =
382
        tSimpleHashPut(pInfo->pHashSources, &pNode->addr.nodeId, sizeof(pNode->addr.nodeId), &idx, sizeof(idx));
1,077,580✔
383
    if (pInfo->pHashSources && code != TSDB_CODE_SUCCESS) {
1,077,562!
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);
743,189✔
390
  int64_t refId = taosAddRef(exchangeObjRefPool, pInfo);
743,191✔
391
  if (refId < 0) {
743,281!
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;
743,281✔
397
  }
398

399
  return initDataSource(numOfSources, pInfo, id);
743,281✔
400
}
401

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

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

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

419
  code = tsem_init(&pInfo->ready, 0, 0);
743,221✔
420
  QUERY_CHECK_CODE(code, lino, _error);
743,205!
421

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

425
  pInfo->pResultBlockList = taosArrayInit(64, POINTER_BYTES);
743,251✔
426
  QUERY_CHECK_NULL(pInfo->pResultBlockList, code, lino, _error, terrno);
743,167!
427
  pInfo->pRecycledBlocks = taosArrayInit(64, POINTER_BYTES);
743,167✔
428
  QUERY_CHECK_NULL(pInfo->pRecycledBlocks, code, lino, _error, terrno);
743,165!
429

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

434
  pInfo->seqLoadData = pExNode->seqRecvData;
743,251✔
435
  pInfo->pTransporter = pTransporter;
743,251✔
436

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

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

444
  pOperator->fpSet = createOperatorFpSet(prepareLoadRemoteData, loadRemoteDataNext, NULL, destroyExchangeOperatorInfo,
743,209✔
445
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
446
  *pOptrInfo = pOperator;
743,148✔
447
  return TSDB_CODE_SUCCESS;
743,148✔
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) {
743,255✔
466
  SExchangeInfo* pExInfo = (SExchangeInfo*)param;
743,255✔
467
  int32_t        code = taosRemoveRef(exchangeObjRefPool, pExInfo->self);
743,255✔
468
  if (code != TSDB_CODE_SUCCESS) {
743,303!
469
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
470
  }
471
}
743,303✔
472

473
void freeBlock(void* pParam) {
1,735,325✔
474
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
1,735,325✔
475
  blockDataDestroy(pBlock);
1,735,325✔
476
}
1,735,354✔
477

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

483
  pInfo->decompBufSize = 0;
1,034,437✔
484
}
1,034,437✔
485

486
void doDestroyExchangeOperatorInfo(void* param) {
743,281✔
487
  if (param == NULL) {
743,281!
488
    return;
×
489
  }
490
  SExchangeInfo* pExInfo = (SExchangeInfo*)param;
743,281✔
491
  if (pExInfo->pFetchRpcHandles) {
743,281!
492
    for (int32_t i = 0; i < pExInfo->pFetchRpcHandles->size; ++i) {
1,820,990✔
493
      int64_t* pRpcHandle = taosArrayGet(pExInfo->pFetchRpcHandles, i);
1,077,707✔
494
      if (*pRpcHandle > 0) {
1,077,701✔
495
        SDownstreamSourceNode* pSource = taosArrayGet(pExInfo->pSources, i);
331✔
496
        (void)asyncFreeConnById(pExInfo->pTransporter, *pRpcHandle);
331✔
497
      }
498
    }
499
    taosArrayDestroy(pExInfo->pFetchRpcHandles);
743,283✔
500
  }
501

502
  taosArrayDestroy(pExInfo->pSources);
743,243✔
503
  taosArrayDestroyEx(pExInfo->pSourceDataInfo, freeSourceDataInfo);
743,295✔
504

505
  taosArrayDestroyEx(pExInfo->pResultBlockList, freeBlock);
743,266✔
506
  taosArrayDestroyEx(pExInfo->pRecycledBlocks, freeBlock);
743,296✔
507

508
  blockDataDestroy(pExInfo->pDummyBlock);
743,303✔
509
  tSimpleHashCleanup(pExInfo->pHashSources);
743,304✔
510

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

517
  taosMemoryFreeClear(param);
743,277!
518
}
519

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

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

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

534
  int64_t* pRpcHandle = taosArrayGet(pExchangeInfo->pFetchRpcHandles, index);
1,080,900✔
535
  if (pRpcHandle != NULL) {
1,080,926✔
536
    int32_t ret = asyncFreeConnById(pExchangeInfo->pTransporter, *pRpcHandle);
1,080,919✔
537
    if (ret != 0) {
1,081,050✔
538
      qDebug("failed to free rpc handle, code:%s, %p", tstrerror(ret), pExchangeInfo);
16,211✔
539
    }
540
    *pRpcHandle = -1;
1,080,968✔
541
  }
542

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

547
  if (code == TSDB_CODE_SUCCESS) {
1,080,975!
548
    pSourceDataInfo->pRsp = pMsg->pData;
1,081,026✔
549

550
    SRetrieveTableRsp* pRsp = pSourceDataInfo->pRsp;
1,081,026✔
551
    pRsp->numOfRows = htobe64(pRsp->numOfRows);
1,081,026✔
552
    pRsp->compLen = htonl(pRsp->compLen);
1,080,817✔
553
    pRsp->payloadLen = htonl(pRsp->payloadLen);
1,080,817✔
554
    pRsp->numOfCols = htonl(pRsp->numOfCols);
1,080,817✔
555
    pRsp->useconds = htobe64(pRsp->useconds);
1,080,817✔
556
    pRsp->numOfBlocks = htonl(pRsp->numOfBlocks);
1,081,016✔
557

558
    qDebug("%s fetch rsp received, index:%d, blocks:%d, rows:%" PRId64 ", %p", pSourceDataInfo->taskId, index,
1,081,016✔
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,081,034✔
573
  pSourceDataInfo->status = EX_SOURCE_DATA_READY;
1,081,034✔
574
  code = tsem_post(&pExchangeInfo->ready);
1,081,034✔
575
  if (code != TSDB_CODE_SUCCESS) {
1,081,012!
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,081,012✔
581
  if (code != TSDB_CODE_SUCCESS) {
1,081,153!
582
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
583
  }
584
  return code;
1,081,142✔
585
}
586

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

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

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

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

612
  return TSDB_CODE_SUCCESS;
1,085✔
613
}
614

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

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

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

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

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

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

669
    int32_t msgSize = tSerializeSResFetchReq(NULL, 0, &req);
1,065,277✔
670
    if (msgSize < 0) {
1,065,059!
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,065,059!
678
    if (NULL == msg) {
1,065,190!
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,065,190✔
686
    if (msgSize < 0) {
1,065,114!
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,065,114✔
695

696
    qDebug("%s build fetch msg and send to vgId:%d, ep:%s, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
1,065,052✔
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,065,052!
703
    if (NULL == pMsgSendInfo) {
1,065,173!
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,065,173✔
712
    pMsgSendInfo->paramFreeFp = taosAutoMemoryFree;
1,065,173✔
713
    pMsgSendInfo->msgInfo.pData = msg;
1,065,173✔
714
    pMsgSendInfo->msgInfo.len = msgSize;
1,065,173✔
715
    pMsgSendInfo->msgType = pSource->fetchMsgType;
1,065,173✔
716
    pMsgSendInfo->fp = loadRemoteDataCallback;
1,065,173✔
717

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

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

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

741
int32_t extractDataBlockFromFetchRsp(SSDataBlock* pRes, char* pData, SArray* pColList, char** pNextStart) {
2,990,423✔
742
  int32_t      code = TSDB_CODE_SUCCESS;
2,990,423✔
743
  int32_t      lino = 0;
2,990,423✔
744
  SSDataBlock* pBlock = NULL;
2,990,423✔
745
  if (pColList == NULL) {  // data from other sources
2,990,423✔
746
    blockDataCleanup(pRes);
2,976,487✔
747
    code = blockDecode(pRes, pData, (const char**)pNextStart);
2,976,433✔
748
    if (code) {
2,976,086!
749
      return code;
×
750
    }
751
  } else {  // extract data according to pColList
752
    char* pStart = pData;
13,936✔
753

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

757
    // todo refactor:extract method
758
    SSysTableSchema* pSchema = (SSysTableSchema*)pStart;
13,936✔
759
    for (int32_t i = 0; i < numOfCols; ++i) {
210,612✔
760
      SSysTableSchema* p = (SSysTableSchema*)pStart;
196,676✔
761

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

767
    pBlock = NULL;
13,936✔
768
    code = createDataBlock(&pBlock);
13,936✔
769
    QUERY_CHECK_CODE(code, lino, _end);
13,949!
770

771
    for (int32_t i = 0; i < numOfCols; ++i) {
210,627✔
772
      SColumnInfoData idata = createColumnInfoData(pSchema[i].type, pSchema[i].bytes, pSchema[i].colId);
196,678✔
773
      code = blockDataAppendColInfo(pBlock, &idata);
196,681✔
774
      QUERY_CHECK_CODE(code, lino, _end);
196,678!
775
    }
776

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

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

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

791
    blockDataDestroy(pBlock);
13,949✔
792
    pBlock = NULL;
13,949✔
793
  }
794

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

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

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

813
  setOperatorCompleted(pOperator);
490,445✔
814
}
490,448✔
815

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

821
  int32_t completed = 0;
1,740,235✔
822
  for (int32_t k = 0; k < total; ++k) {
4,525,094✔
823
    SSourceDataInfo* p = taosArrayGet(pArray, k);
2,784,851✔
824
    QUERY_CHECK_NULL(p, code, lino, _end, terrno);
2,784,858!
825
    if (p->status == EX_SOURCE_DATA_EXHAUSTED) {
2,784,859✔
826
      completed += 1;
1,334,172✔
827
    }
828
  }
829

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

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

842
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSourceDataInfo);
720,422✔
843
  int64_t startTs = taosGetTimestampUs();
720,544✔
844

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

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

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

864
  return TSDB_CODE_SUCCESS;
720,583✔
865
}
866

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

873
  char* pNextStart = pRetrieveRsp->data;
803,280✔
874
  char* pStart = pNextStart;
803,280✔
875

876
  int32_t index = 0;
803,280✔
877

878
  if (pRetrieveRsp->compressed) {  // decompress the data
803,280!
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) {
3,779,510✔
896
    pStart = pNextStart;
2,976,302✔
897

898
    if (taosArrayGetSize(pExchangeInfo->pRecycledBlocks) > 0) {
2,976,302✔
899
      pb = *(SSDataBlock**)taosArrayPop(pExchangeInfo->pRecycledBlocks);
1,241,278✔
900
      blockDataCleanup(pb);
1,241,276✔
901
    } else {
902
      code = createOneDataBlock(pExchangeInfo->pDummyBlock, false, &pb);
1,735,030✔
903
      QUERY_CHECK_NULL(pb, code, lino, _end, code);
1,735,248!
904
    }
905

906
    int32_t compLen = *(int32_t*)pStart;
2,976,498✔
907
    pStart += sizeof(int32_t);
2,976,498✔
908

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

913
    pNextStart = pStart + compLen;
2,976,498✔
914
    if (pRetrieveRsp->compressed && (compLen < rawLen)) {
2,976,498!
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);
2,976,498✔
921
    if (code != 0) {
2,976,100!
922
      taosMemoryFreeClear(pDataInfo->pRsp);
×
923
      goto _end;
×
924
    }
925

926
    void* tmp = taosArrayPush(pExchangeInfo->pResultBlockList, &pb);
2,976,100✔
927
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
2,976,230!
928
    pb = NULL;
2,976,230✔
929
  }
930

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

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

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

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

953
    SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, pExchangeInfo->current);
1,937✔
954
    if (!pDataInfo) {
1,937!
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;
1,937✔
960

961
    code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, pExchangeInfo->current);
1,937✔
962
    if (code != TSDB_CODE_SUCCESS) {
1,937!
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);
1,937✔
969
    if (code != TSDB_CODE_SUCCESS || isTaskKilled(pTaskInfo)) {
1,937!
970
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
971
    }
972

973
    SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, pExchangeInfo->current);
1,937✔
974
    if (!pSource) {
1,937!
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) {
1,937!
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;
1,937✔
989
    SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
1,937✔
990

991
    if (pRsp->numOfRows == 0) {
1,937✔
992
      qDebug("%s vgId:%d, clientId:0x%" PRIx64 " taskID:0x%" PRIx64
700✔
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;
700✔
998
      pExchangeInfo->current += 1;
700✔
999
      taosMemoryFreeClear(pDataInfo->pRsp);
700!
1000
      continue;
700✔
1001
    }
1002

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

1008
    SRetrieveTableRsp* pRetrieveRsp = pDataInfo->pRsp;
1,237✔
1009
    if (pRsp->completed == 1) {
1,237✔
1010
      qDebug("%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64 " execId:%d numOfRows:%" PRId64
1,064✔
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,064✔
1017
      pExchangeInfo->current += 1;
1,064✔
1018
    } else {
1019
      qDebug("%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64 " execId:%d numOfRows:%" PRId64
173!
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,237✔
1026
    pDataInfo->totalRows += pRetrieveRsp->numOfRows;
1,237✔
1027

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

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

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

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

1055
    dataInfo.srcOpType = pBasicParam->srcOpType;
1,012✔
1056
    dataInfo.tableSeq = pBasicParam->tableSeq;
1,012✔
1057

1058
    void* tmp = taosArrayPush(pExchangeInfo->pSourceDataInfo, &dataInfo);
1,012✔
1059
    if (!tmp) {
1,012!
1060
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1061
      return terrno;
×
1062
    }
1063
    pIdx->inUseIdx = taosArrayGetSize(pExchangeInfo->pSourceDataInfo) - 1;
1,012✔
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;
1,036✔
1082
}
1083

1084
int32_t addDynamicExchangeSource(SOperatorInfo* pOperator) {
639✔
1085
  SExchangeInfo*               pExchangeInfo = pOperator->info;
639✔
1086
  int32_t                      code = TSDB_CODE_SUCCESS;
639✔
1087
  SExchangeOperatorBasicParam* pBasicParam = NULL;
639✔
1088
  SExchangeOperatorParam*      pParam = (SExchangeOperatorParam*)pOperator->pOperatorGetParam->value;
639✔
1089
  if (pParam->multiParams) {
639✔
1090
    SExchangeOperatorBatchParam* pBatch = (SExchangeOperatorBatchParam*)pOperator->pOperatorGetParam->value;
591✔
1091
    int32_t                      iter = 0;
591✔
1092
    while (NULL != (pBasicParam = tSimpleHashIterate(pBatch->pBatchs, pBasicParam, &iter))) {
1,579✔
1093
      code = addSingleExchangeSource(pOperator, pBasicParam);
988✔
1094
      if (code) {
988!
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);
639✔
1104
  pOperator->pOperatorGetParam = NULL;
639✔
1105

1106
  return TSDB_CODE_SUCCESS;
639✔
1107
}
1108

1109
int32_t prepareLoadRemoteData(SOperatorInfo* pOperator) {
3,728,330✔
1110
  SExchangeInfo* pExchangeInfo = pOperator->info;
3,728,330✔
1111
  int32_t        code = TSDB_CODE_SUCCESS;
3,728,330✔
1112
  int32_t        lino = 0;
3,728,330✔
1113
  if ((OPTR_IS_OPENED(pOperator) && !pExchangeInfo->dynamicOp) ||
3,728,330✔
1114
      (pExchangeInfo->dynamicOp && NULL == pOperator->pOperatorGetParam)) {
723,731✔
1115
    return TSDB_CODE_SUCCESS;
3,007,162✔
1116
  }
1117

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

1123
  int64_t st = taosGetTimestampUs();
721,336✔
1124

1125
  if (!pExchangeInfo->seqLoadData) {
721,336✔
1126
    code = prepareConcurrentlyLoad(pOperator);
720,550✔
1127
    QUERY_CHECK_CODE(code, lino, _end);
720,563!
1128
    pExchangeInfo->openedTs = taosGetTimestampUs();
720,570✔
1129
  }
1130

1131
  OPTR_SET_OPENED(pOperator);
721,356✔
1132
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
721,388✔
1133

1134
_end:
721,388✔
1135
  if (code != TSDB_CODE_SUCCESS) {
721,388!
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;
721,388✔
1141
}
1142

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

1146
  if (pLimitInfo->remainGroupOffset > 0) {
22,524!
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) {
22,524✔
1168
    pLimitInfo->numOfOutputGroups += 1;
821✔
1169
    if ((pLimitInfo->slimit.limit > 0) && (pLimitInfo->slimit.limit <= pLimitInfo->numOfOutputGroups)) {
821!
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);
821✔
1178
    // existing rows that belongs to previous group.
1179
    if (pBlock->info.rows > 0) {
821!
1180
      return PROJECT_RETRIEVE_DONE;
821✔
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;
21,703✔
1188

1189
  bool limitReached = applyLimitOffset(pLimitInfo, pBlock, pTaskInfo);
21,703✔
1190
  if (pBlock->info.rows == 0) {
21,703✔
1191
    return PROJECT_RETRIEVE_CONTINUE;
8,609✔
1192
  } else {
1193
    if (limitReached && (pLimitInfo->slimit.limit > 0 && pLimitInfo->slimit.limit <= pLimitInfo->numOfOutputGroups)) {
13,094!
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)) {
13,094!
1203
    return PROJECT_RETRIEVE_DONE;
13,094✔
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,080,650✔
1210
  SExecTaskInfo* pTask = pOperator->pTaskInfo;
1,080,650✔
1211
  int32_t        code = TSDB_CODE_SUCCESS;
1,080,650✔
1212
  if (pTask->pWorkerCb) {
1,080,650!
1213
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
1,080,694✔
1214
    if (code != TSDB_CODE_SUCCESS) {
1,080,739!
1215
      pTask->code = code;
×
1216
      return pTask->code;
×
1217
    }
1218
  }
1219

1220
  code = tsem_wait(&pExchangeInfo->ready);
1,080,695✔
1221
  if (code != TSDB_CODE_SUCCESS) {
1,080,699!
UNCOV
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,080,733!
1228
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
1,080,734✔
1229
    if (code != TSDB_CODE_SUCCESS) {
1,080,741!
1230
      pTask->code = code;
×
1231
      return pTask->code;
×
1232
    }
1233
  }
1234
  return TSDB_CODE_SUCCESS;
1,080,740✔
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