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

taosdata / TDengine / #4815

17 Oct 2025 06:47AM UTC coverage: 61.177% (-0.03%) from 61.206%
#4815

push

travis-ci

web-flow
Merge pull request #33289 from taosdata/3.0

enh: Code Optimization (#33283)

155629 of 324369 branches covered (47.98%)

Branch coverage included in aggregate %.

207706 of 269535 relevant lines covered (77.06%)

127615938.53 hits per line

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

55.22
/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
  int64_t  seqId;
32
} SFetchRspHandleWrapper;
33

34
typedef struct SSourceDataInfo {
35
  int32_t            index;
36
  int64_t            seqId;
37
  SRWLatch           lock;
38
  SRetrieveTableRsp* pRsp;
39
  uint64_t           totalRows;
40
  int64_t            startTime;
41
  int32_t            code;
42
  EX_SOURCE_STATUS   status;
43
  const char*        taskId;
44
  SArray*            pSrcUidList;
45
  int32_t            srcOpType;
46
  bool               tableSeq;
47
  char*              decompBuf;
48
  int32_t            decompBufSize;
49
  SOrgTbInfo*        colMap;
50
  bool               isVtbRefScan;
51
  bool               isVtbTagScan;
52
  STimeWindow        window;
53
  bool               fetchSent; // need reset
54
} SSourceDataInfo;
55

56
static void destroyExchangeOperatorInfo(void* param);
57
static void freeBlock(void* pParam);
58
static void freeSourceDataInfo(void* param);
59
static void setAllSourcesCompleted(SOperatorInfo* pOperator);
60

61
static int32_t loadRemoteDataCallback(void* param, SDataBuf* pMsg, int32_t code);
62
static int32_t doSendFetchDataRequest(SExchangeInfo* pExchangeInfo, SExecTaskInfo* pTaskInfo, int32_t sourceIndex);
63
static int32_t getCompletedSources(const SArray* pArray, int32_t* pRes);
64
static int32_t prepareConcurrentlyLoad(SOperatorInfo* pOperator);
65
static int32_t seqLoadRemoteData(SOperatorInfo* pOperator);
66
static int32_t prepareLoadRemoteData(SOperatorInfo* pOperator);
67
static int32_t handleLimitOffset(SOperatorInfo* pOperator, SLimitInfo* pLimitInfo, SSDataBlock* pBlock,
68
                                 bool holdDataInBuf);
69
static int32_t doExtractResultBlocks(SExchangeInfo* pExchangeInfo, SSourceDataInfo* pDataInfo);
70

71
static int32_t exchangeWait(SOperatorInfo* pOperator, SExchangeInfo* pExchangeInfo);
72

73

74
static void streamConcurrentlyLoadRemoteData(SOperatorInfo* pOperator, SExchangeInfo* pExchangeInfo,
16,278,128✔
75
                                           SExecTaskInfo* pTaskInfo) {
76
  int32_t code = 0;
16,278,128✔
77
  int32_t lino = 0;
16,286,948✔
78
  int64_t startTs = taosGetTimestampUs();  
16,278,128✔
79
  int32_t  totalSources = (int32_t)taosArrayGetSize(pExchangeInfo->pSourceDataInfo);
16,278,128✔
80
  int32_t completed = 0;
16,278,128✔
81
  code = getCompletedSources(pExchangeInfo->pSourceDataInfo, &completed);
16,278,128✔
82
  if (code != TSDB_CODE_SUCCESS) {
16,278,128!
83
    pTaskInfo->code = code;
×
84
    T_LONG_JMP(pTaskInfo->env, code);
×
85
  }
86
  if (completed == totalSources) {
16,278,128✔
87
    qDebug("%s no load since all sources completed, completed:%d, totalSources:%d", pTaskInfo->id.str, completed, totalSources);
4,173,463✔
88
    setAllSourcesCompleted(pOperator);
4,173,463✔
89
    return;
4,179,121✔
90
  }
91

92
  SSourceDataInfo* pDataInfo = NULL;
12,104,665✔
93

94
  while (1) {
6,783,641✔
95
    if (pExchangeInfo->current < 0) {
18,888,306✔
96
      qDebug("current %d and all sources complted, totalSources:%d", pExchangeInfo->current, totalSources);
27,359✔
97
      setAllSourcesCompleted(pOperator);
27,359✔
98
      return;
27,359✔
99
    }
100
    
101
    if (pExchangeInfo->current >= totalSources) {
18,860,947✔
102
      completed = 0;
7,758,686✔
103
      code = getCompletedSources(pExchangeInfo->pSourceDataInfo, &completed);
7,758,686✔
104
      if (code != TSDB_CODE_SUCCESS) {
7,758,686!
105
        pTaskInfo->code = code;
×
106
        T_LONG_JMP(pTaskInfo->env, code);
×
107
      }
108
      if (completed == totalSources) {
7,758,686✔
109
        qDebug("stop to load since all sources complted, completed:%d, totalSources:%d", completed, totalSources);
6,629,827✔
110
        setAllSourcesCompleted(pOperator);
6,629,827✔
111
        return;
6,629,827✔
112
      }
113
      
114
      pExchangeInfo->current = 0;
1,128,859✔
115
    }
116

117
    qDebug("%s start stream exchange %p idx:%d fetch", GET_TASKID(pTaskInfo), pExchangeInfo, pExchangeInfo->current);
12,231,120✔
118

119
    SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, pExchangeInfo->current);
12,231,120✔
120
    if (!pDataInfo) {
12,231,120!
121
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
122
      pTaskInfo->code = terrno;
×
123
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
124
    }
125

126
    if (pDataInfo->status == EX_SOURCE_DATA_EXHAUSTED) {
12,231,120✔
127
      pExchangeInfo->current++;
2,425✔
128
      continue;
2,425✔
129
    }
130

131
    pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
12,228,695✔
132

133
    code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, pExchangeInfo->current);
12,228,695✔
134
    if (code != TSDB_CODE_SUCCESS) {
12,228,695!
135
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
136
      pTaskInfo->code = code;
×
137
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
138
    }
139

140
    while (true) {
9,557✔
141
      code = exchangeWait(pOperator, pExchangeInfo);
12,238,252✔
142
      if (code != TSDB_CODE_SUCCESS || isTaskKilled(pTaskInfo)) {
12,238,252!
143
        T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
10,070!
144
      }
145

146
      int64_t currSeqId = atomic_load_64(&pExchangeInfo->seqId);
12,228,182✔
147
      if (pDataInfo->seqId != currSeqId) {
12,228,182✔
148
        qDebug("%s seq rsp reqId %" PRId64 " mismatch with exchange %p curr seqId %" PRId64 ", ignore it", 
9,557✔
149
            GET_TASKID(pTaskInfo), pDataInfo->seqId, pExchangeInfo, currSeqId);
150
        taosMemoryFreeClear(pDataInfo->pRsp);
9,557!
151
        continue;
9,557✔
152
      }
153

154
      break;
12,218,625✔
155
    }
156

157
    SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, pExchangeInfo->current);
12,218,625✔
158
    if (!pSource) {
12,218,625!
159
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
160
      pTaskInfo->code = terrno;
×
161
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
162
    }
163

164
    if (pDataInfo->code != TSDB_CODE_SUCCESS) {
12,218,625✔
165
      qError("%s vgId:%d, clientId:0x%" PRIx64 " taskID:0x%" PRIx64 " execId:%d error happens, code:%s",
6,548!
166
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
167
             tstrerror(pDataInfo->code));
168
      pTaskInfo->code = pDataInfo->code;
6,548✔
169
      T_LONG_JMP(pTaskInfo->env, code);
6,548!
170
    }
171

172
    SRetrieveTableRsp*   pRsp = pDataInfo->pRsp;
12,212,077✔
173
    SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
12,212,077✔
174

175
    if (pRsp->numOfRows == 0) {
12,212,077✔
176
      qDebug("exhausted %p,%s vgId:%d, clientId:0x%" PRIx64 " taskID:0x%" PRIx64
6,781,216✔
177
             " execId:%d idx %d of total completed, rowsOfSource:%" PRIu64 ", totalRows:%" PRIu64 " try next", pDataInfo,
178
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
179
             pExchangeInfo->current + 1, pDataInfo->totalRows, pLoadInfo->totalRows);
180

181
      pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
6,781,216✔
182
      if (pDataInfo->isVtbRefScan || pDataInfo->isVtbTagScan) {
6,781,216!
183
        pExchangeInfo->current = -1;
27,359✔
184
      } else {
185
        pExchangeInfo->current += 1;
6,753,857✔
186
      }
187
      taosMemoryFreeClear(pDataInfo->pRsp);
6,781,216!
188
      continue;
6,781,216✔
189
    }
190

191
    code = doExtractResultBlocks(pExchangeInfo, pDataInfo);
5,430,861✔
192
    TAOS_CHECK_EXIT(code);
5,430,861!
193

194
    SRetrieveTableRsp* pRetrieveRsp = pDataInfo->pRsp;
5,430,861✔
195
    if (pRsp->completed == 1) {
5,430,861✔
196
      qDebug("exhausted %p,%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64 " execId:%d numOfRows:%" PRId64
4,266,398✔
197
             ", rowsOfSource:%" PRIu64 ", totalRows:%" PRIu64 ", totalBytes:%" PRIu64 " try next %d/%d", pDataInfo,
198
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
199
             pRetrieveRsp->numOfRows, pDataInfo->totalRows, pLoadInfo->totalRows, pLoadInfo->totalSize,
200
             pExchangeInfo->current + 1, totalSources);
201

202
      pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
4,266,398✔
203
      if (pDataInfo->isVtbRefScan) {
4,266,398!
204
        pExchangeInfo->current = -1;
×
205
        taosMemoryFreeClear(pDataInfo->pRsp);
×
206
        continue;
×
207
      }
208
    } else {
209
      qDebug("%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64 " execId:%d idx:%d numOfRows:%" PRId64
1,164,463✔
210
             ", totalRows:%" PRIu64 ", totalBytes:%" PRIu64,
211
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
212
             pExchangeInfo->current, pRetrieveRsp->numOfRows, pLoadInfo->totalRows, pLoadInfo->totalSize);
213
    }
214

215
    updateLoadRemoteInfo(pLoadInfo, pRetrieveRsp->numOfRows, pRetrieveRsp->compLen, startTs, pOperator);
5,430,861✔
216
    pDataInfo->totalRows += pRetrieveRsp->numOfRows;
5,430,861✔
217

218
    pExchangeInfo->current++;
5,430,861✔
219

220
    taosMemoryFreeClear(pDataInfo->pRsp);
5,430,861!
221
    return;
5,430,861✔
222
  }
223

224
_exit:
×
225

226
  if (code) {
×
227
    pTaskInfo->code = code;
×
228
    qError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
×
229
  }
230
}
231

232

233
static void concurrentlyLoadRemoteDataImpl(SOperatorInfo* pOperator, SExchangeInfo* pExchangeInfo,
153,511,635✔
234
                                           SExecTaskInfo* pTaskInfo) {
235
  int32_t code = 0;
153,511,635✔
236
  int32_t lino = 0;
153,511,635✔
237
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSourceDataInfo);
153,511,635✔
238
  int32_t completed = 0;
153,511,123✔
239
  code = getCompletedSources(pExchangeInfo->pSourceDataInfo, &completed);
153,511,123✔
240
  if (code != TSDB_CODE_SUCCESS) {
153,511,635!
241
    pTaskInfo->code = code;
×
242
    T_LONG_JMP(pTaskInfo->env, code);
×
243
  }
244
  if (completed == totalSources) {
153,511,635✔
245
    setAllSourcesCompleted(pOperator);
47,548,070✔
246
    return;
47,548,470✔
247
  }
248

249
  SSourceDataInfo* pDataInfo = NULL;
105,963,565✔
250

251
  while (1) {
11,881,144✔
252
    qDebug("prepare wait for ready, %p, %s", pExchangeInfo, GET_TASKID(pTaskInfo));
117,844,709✔
253
    code = exchangeWait(pOperator, pExchangeInfo);
117,844,709✔
254

255
    if (code != TSDB_CODE_SUCCESS || isTaskKilled(pTaskInfo)) {
117,844,475!
256
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
234!
257
    }
258

259
    for (int32_t i = 0; i < totalSources; ++i) {
175,535,356!
260
      pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, i);
175,535,356✔
261
      QUERY_CHECK_NULL(pDataInfo, code, lino, _exit, terrno);
175,535,129!
262
      if (pDataInfo->status == EX_SOURCE_DATA_EXHAUSTED) {
175,535,129✔
263
        continue;
42,325,154✔
264
      }
265

266
      if (pDataInfo->status != EX_SOURCE_DATA_READY) {
133,210,436✔
267
        continue;
15,365,727✔
268
      }
269

270
      int64_t currSeqId = atomic_load_64(&pExchangeInfo->seqId);
117,844,475✔
271
      if (pDataInfo->seqId != currSeqId) {
117,844,709!
272
        qDebug("concurrent rsp reqId %" PRId64 " mismatch with exchange %p curr seqId %" PRId64 ", ignore it", pDataInfo->seqId, pExchangeInfo, currSeqId);
×
273
        taosMemoryFreeClear(pDataInfo->pRsp);
×
274
        break;
×
275
      }
276

277
      if (pDataInfo->code != TSDB_CODE_SUCCESS) {
117,844,475✔
278
        code = pDataInfo->code;
804✔
279
        TAOS_CHECK_EXIT(code);
804!
280
      }
281

282
      tmemory_barrier();
117,843,905✔
283
      SRetrieveTableRsp*     pRsp = pDataInfo->pRsp;
117,843,905✔
284
      SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, pDataInfo->index);
117,843,671✔
285
      QUERY_CHECK_NULL(pSource, code, lino, _exit, terrno);
117,843,627!
286

287
      // todo
288
      SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
117,843,627✔
289
      if (pRsp->numOfRows == 0) {
117,843,393✔
290
        if (NULL != pDataInfo->pSrcUidList && (!pDataInfo->isVtbRefScan)) {
28,270,843!
291
          pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
×
292
          code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, i);
×
293
          if (code != TSDB_CODE_SUCCESS) {
×
294
            taosMemoryFreeClear(pDataInfo->pRsp);
×
295
            TAOS_CHECK_EXIT(code);
×
296
          }
297
        } else {
298
          pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
28,270,843✔
299
          qDebug("exhausted %p,%s vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
28,270,609✔
300
                 " execId:%d index:%d completed, rowsOfSource:%" PRIu64 ", totalRows:%" PRIu64 ", try next %d/%" PRIzu, pDataInfo,
301
                 GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId, i,
302
                 pDataInfo->totalRows, pExchangeInfo->loadInfo.totalRows, i + 1, totalSources);
303
          taosMemoryFreeClear(pDataInfo->pRsp);
28,270,843!
304
        }
305
        break;
28,270,843✔
306
      }
307

308
      TAOS_CHECK_EXIT(doExtractResultBlocks(pExchangeInfo, pDataInfo));
89,573,062!
309

310
      SRetrieveTableRsp* pRetrieveRsp = pDataInfo->pRsp;
89,573,062✔
311
      updateLoadRemoteInfo(pLoadInfo, pRetrieveRsp->numOfRows, pRetrieveRsp->compLen, pDataInfo->startTime, pOperator);
89,573,062✔
312
      pDataInfo->totalRows += pRetrieveRsp->numOfRows;
89,573,062✔
313

314
      if (pRsp->completed == 1) {
89,573,062✔
315
        pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
75,407,530✔
316
        qDebug("exhausted %p,%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
75,407,530✔
317
               " execId:%d index:%d completed, blocks:%d, numOfRows:%" PRId64 ", rowsOfSource:%" PRIu64
318
               ", totalRows:%" PRIu64 ", total:%.2f Kb, try next %d/%" PRIzu, pDataInfo,
319
               GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId, i,
320
               pRsp->numOfBlocks, pRsp->numOfRows, pDataInfo->totalRows, pLoadInfo->totalRows,
321
               pLoadInfo->totalSize / 1024.0, i + 1, totalSources);
322
      } else {
323
        qDebug("%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
14,165,532!
324
               " execId:%d blocks:%d, numOfRows:%" PRId64 ", totalRows:%" PRIu64 ", total:%.2f Kb",
325
               GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
326
               pRsp->numOfBlocks, pRsp->numOfRows, pLoadInfo->totalRows, pLoadInfo->totalSize / 1024.0);
327
      }
328

329
      taosMemoryFreeClear(pDataInfo->pRsp);
89,573,062!
330

331
      if ((pDataInfo->status != EX_SOURCE_DATA_EXHAUSTED || NULL != pDataInfo->pSrcUidList) && !pDataInfo->isVtbRefScan && !pDataInfo->isVtbTagScan) {
89,573,062!
332
        pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
14,165,532✔
333
        code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, i);
14,165,532✔
334
        if (code != TSDB_CODE_SUCCESS) {
14,165,532!
335
          taosMemoryFreeClear(pDataInfo->pRsp);
×
336
          TAOS_CHECK_EXIT(code);
×
337
        }
338
      }
339
      
340
      return;
89,573,062✔
341
    }  // end loop
342

343
    int32_t complete1 = 0;
28,270,843✔
344
    code = getCompletedSources(pExchangeInfo->pSourceDataInfo, &complete1);
28,270,843✔
345
    if (code != TSDB_CODE_SUCCESS) {
28,270,843!
346
      pTaskInfo->code = code;
×
347
      T_LONG_JMP(pTaskInfo->env, code);
×
348
    }
349
    if (complete1 == totalSources) {
28,270,843✔
350
      qDebug("all sources are completed, %s", GET_TASKID(pTaskInfo));
16,389,699✔
351
      return;
16,389,699✔
352
    }
353
  }
354

355
_exit:
804✔
356

357
  if (code) {
804!
358
    pTaskInfo->code = code;
804✔
359
    qError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
804!
360
  }
361
}
362

363
static SSDataBlock* doLoadRemoteDataImpl(SOperatorInfo* pOperator) {
433,856,222✔
364
  int32_t        code = TSDB_CODE_SUCCESS;
433,856,222✔
365
  SExchangeInfo* pExchangeInfo = pOperator->info;
433,856,222✔
366
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
433,855,143✔
367

368
  size_t totalSources = taosArrayGetSize(pExchangeInfo->pSources);
433,855,688✔
369

370
  SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
433,856,202✔
371
  if (pOperator->status == OP_EXEC_DONE) {
433,856,767!
372
    qDebug("%s all %" PRIzu " source(s) are exhausted, total rows:%" PRIu64 " bytes:%" PRIu64 ", elapsed:%.2f ms",
×
373
           GET_TASKID(pTaskInfo), totalSources, pLoadInfo->totalRows, pLoadInfo->totalSize,
374
           pLoadInfo->totalElapsed / 1000.0);
375
    return NULL;
×
376
  }
377

378
  // we have buffered retrieved datablock, return it directly
379
  SSDataBlock* p = NULL;
433,856,533✔
380
  if (taosArrayGetSize(pExchangeInfo->pResultBlockList) > 0) {
433,856,767✔
381
    p = taosArrayGetP(pExchangeInfo->pResultBlockList, 0);
224,567,415✔
382
    taosArrayRemove(pExchangeInfo->pResultBlockList, 0);
224,566,870✔
383
  }
384

385
  if (p != NULL) {
433,857,015✔
386
    void* tmp = taosArrayPush(pExchangeInfo->pRecycledBlocks, &p);
224,567,663✔
387
    if (!tmp) {
224,567,117!
388
      code = terrno;
×
389
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
390
      pTaskInfo->code = code;
×
391
      T_LONG_JMP(pTaskInfo->env, code);
×
392
    }
393
    return p;
224,567,117✔
394
  } else {
395
    if (pExchangeInfo->seqLoadData) {
209,289,352!
396
      code = seqLoadRemoteData(pOperator);
39,499,589✔
397
      if (code != TSDB_CODE_SUCCESS) {
39,498,959!
398
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
399
        pTaskInfo->code = code;
×
400
        T_LONG_JMP(pTaskInfo->env, code);
×
401
      }
402
    } else if (IS_STREAM_MODE(pOperator->pTaskInfo))   {
169,788,962✔
403
      streamConcurrentlyLoadRemoteData(pOperator, pExchangeInfo, pTaskInfo);
16,278,128✔
404
    } else {
405
      concurrentlyLoadRemoteDataImpl(pOperator, pExchangeInfo, pTaskInfo);
153,511,401✔
406
    }
407
    if (TSDB_CODE_SUCCESS != pOperator->pTaskInfo->code) {
209,271,553✔
408
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(pOperator->pTaskInfo->code));
804!
409
      T_LONG_JMP(pTaskInfo->env, pOperator->pTaskInfo->code);
804!
410
    }
411
    
412
    if (taosArrayGetSize(pExchangeInfo->pResultBlockList) == 0) {
209,271,300✔
413
      qDebug("empty resultBlockList");
83,855,189✔
414
      return NULL;
83,855,393✔
415
    } else {
416
      p = taosArrayGetP(pExchangeInfo->pResultBlockList, 0);
125,415,355✔
417
      taosArrayRemove(pExchangeInfo->pResultBlockList, 0);
125,415,355✔
418
      void* tmp = taosArrayPush(pExchangeInfo->pRecycledBlocks, &p);
125,415,355✔
419
      if (!tmp) {
125,414,802!
420
        code = terrno;
×
421
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
422
        pTaskInfo->code = code;
×
423
        T_LONG_JMP(pTaskInfo->env, code);
×
424
      }
425

426
      qDebug("block with rows:%" PRId64 " loaded", p->info.rows);
125,414,802✔
427
      return p;
125,414,802✔
428
    }
429
}
430
}
431

432
static int32_t loadRemoteDataNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
432,595,715✔
433
  int32_t        code = TSDB_CODE_SUCCESS;
432,595,715✔
434
  int32_t        lino = 0;
432,595,715✔
435
  SExchangeInfo* pExchangeInfo = pOperator->info;
432,595,715✔
436
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
432,595,858✔
437

438
  qDebug("%s start to load from exchange %p", pTaskInfo->id.str, pExchangeInfo);
432,595,057✔
439

440
  code = pOperator->fpSet._openFn(pOperator);
432,596,887✔
441
  QUERY_CHECK_CODE(code, lino, _end);
432,596,103!
442

443
  if (pOperator->status == OP_EXEC_DONE) {
432,596,103✔
444
    (*ppRes) = NULL;
24,222✔
445
    return code;
24,222✔
446
  }
447

448
  while (1) {
1,284,341✔
449
    SSDataBlock* pBlock = doLoadRemoteDataImpl(pOperator);
433,856,222✔
450
    if (pBlock == NULL) {
433,837,858✔
451
      (*ppRes) = NULL;
83,855,393✔
452
      return code;
83,855,393✔
453
    }
454

455
    code = doFilter(pBlock, pOperator->exprSupp.pFilterInfo, NULL, NULL);
349,982,465✔
456
    QUERY_CHECK_CODE(code, lino, _end);
349,982,212!
457

458
    if (blockDataGetNumOfRows(pBlock) == 0) {
349,982,212✔
459
      qDebug("rows 0 block got, continue next load");
1,428!
460
      continue;
1,428✔
461
    }
462

463
    SLimitInfo* pLimitInfo = &pExchangeInfo->limitInfo;
349,980,784✔
464
    if (hasLimitOffsetInfo(pLimitInfo)) {
349,981,330✔
465
      int32_t status = handleLimitOffset(pOperator, pLimitInfo, pBlock, false);
2,718,402✔
466
      if (status == PROJECT_RETRIEVE_CONTINUE) {
2,718,402✔
467
        qDebug("limit retrieve continue");
1,282,913✔
468
        continue;
1,282,913✔
469
      } else if (status == PROJECT_RETRIEVE_DONE) {
1,435,489!
470
        if (pBlock->info.rows == 0) {
1,435,489!
471
          setOperatorCompleted(pOperator);
×
472
          (*ppRes) = NULL;
×
473
          return code;
×
474
        } else {
475
          (*ppRes) = pBlock;
1,435,489✔
476
          return code;
1,435,489✔
477
        }
478
      }
479
    } else {
480
      (*ppRes) = pBlock;
347,262,928✔
481
      qDebug("block with rows %" PRId64 " returned in exechange", pBlock->info.rows);
347,262,928✔
482
      return code;
347,258,592✔
483
    }
484
  }
485

486
_end:
×
487

488
  if (code != TSDB_CODE_SUCCESS) {
×
489
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
490
    pTaskInfo->code = code;
×
491
    T_LONG_JMP(pTaskInfo->env, code);
×
492
  } else {
493
    qDebug("empty block returned in exchange");
×
494
  }
495
  
496
  (*ppRes) = NULL;
×
497
  return code;
×
498
}
499

500
static int32_t initDataSource(int32_t numOfSources, SExchangeInfo* pInfo, const char* id) {
73,674,646✔
501
  pInfo->pSourceDataInfo = taosArrayInit(numOfSources, sizeof(SSourceDataInfo));
73,674,646✔
502
  if (pInfo->pSourceDataInfo == NULL) {
73,673,759!
503
    return terrno;
×
504
  }
505

506
  if (pInfo->dynamicOp) {
73,673,534!
507
    return TSDB_CODE_SUCCESS;
3,170,877✔
508
  }
509

510
  int32_t len = strlen(id) + 1;
70,502,716!
511
  pInfo->pTaskId = taosMemoryCalloc(1, len);
70,502,716✔
512
  if (!pInfo->pTaskId) {
70,504,756!
513
    return terrno;
×
514
  }
515
  tstrncpy(pInfo->pTaskId, id, len);
70,501,892!
516
  for (int32_t i = 0; i < numOfSources; ++i) {
177,172,905✔
517
    SSourceDataInfo dataInfo = {0};
106,666,822✔
518
    dataInfo.status = EX_SOURCE_DATA_NOT_READY;
106,666,607✔
519
    dataInfo.taskId = pInfo->pTaskId;
106,666,607✔
520
    dataInfo.index = i;
106,666,837✔
521
    SSourceDataInfo* pDs = taosArrayPush(pInfo->pSourceDataInfo, &dataInfo);
106,666,837✔
522
    if (pDs == NULL) {
106,666,277!
523
      taosArrayDestroyEx(pInfo->pSourceDataInfo, freeSourceDataInfo);
×
524
      return terrno;
×
525
    }
526
    qDebug("init source data info %d, pDs:%p, status:%d", i, pDs, pDs->status);
106,666,277✔
527
  }
528

529
  return TSDB_CODE_SUCCESS;
70,506,083✔
530
}
531

532
static int32_t initExchangeOperator(SExchangePhysiNode* pExNode, SExchangeInfo* pInfo, const char* id) {
73,676,139✔
533
  size_t numOfSources = LIST_LENGTH(pExNode->pSrcEndPoints);
73,676,139!
534

535
  if (numOfSources == 0) {
73,673,823!
536
    qError("%s invalid number: %d of sources in exchange operator", id, (int32_t)numOfSources);
×
537
    return TSDB_CODE_INVALID_PARA;
×
538
  }
539
  pInfo->pFetchRpcHandles = taosArrayInit(numOfSources, sizeof(int64_t));
73,673,823✔
540
  if (!pInfo->pFetchRpcHandles) {
73,675,786!
541
    return terrno;
×
542
  }
543
  void* ret = taosArrayReserve(pInfo->pFetchRpcHandles, numOfSources);
73,674,906✔
544
  if (!ret) {
73,674,907!
545
    return terrno;
×
546
  }
547

548
  pInfo->pSources = taosArrayInit(numOfSources, sizeof(SDownstreamSourceNode));
73,674,907✔
549
  if (pInfo->pSources == NULL) {
73,674,680!
550
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
551
    return terrno;
×
552
  }
553

554
  if (pExNode->node.dynamicOp) {
73,675,083!
555
    pInfo->pHashSources = tSimpleHashInit(numOfSources * 2, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
3,170,877✔
556
    if (NULL == pInfo->pHashSources) {
3,170,877!
557
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
558
      return terrno;
×
559
    }
560
  }
561

562
  for (int32_t i = 0; i < numOfSources; ++i) {
189,087,958✔
563
    SDownstreamSourceNode* pNode = (SDownstreamSourceNode*)nodesListGetNode((SNodeList*)pExNode->pSrcEndPoints, i);
115,413,803✔
564
    if (!pNode) {
115,412,185!
565
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
566
      return terrno;
×
567
    }
568
    void* tmp = taosArrayPush(pInfo->pSources, pNode);
115,412,185✔
569
    if (!tmp) {
115,416,155!
570
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
571
      return terrno;
×
572
    }
573
    SExchangeSrcIndex idx = {.srcIdx = i, .inUseIdx = -1};
115,416,155✔
574
    int32_t           code =
575
        tSimpleHashPut(pInfo->pHashSources, &pNode->addr.nodeId, sizeof(pNode->addr.nodeId), &idx, sizeof(idx));
115,415,682✔
576
    if (pInfo->pHashSources && code != TSDB_CODE_SUCCESS) {
115,413,112!
577
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
578
      return code;
×
579
    }
580
  }
581

582
  initLimitInfo(pExNode->node.pLimit, pExNode->node.pSlimit, &pInfo->limitInfo);
73,674,155✔
583
  int64_t refId = taosAddRef(exchangeObjRefPool, pInfo);
73,672,644✔
584
  if (refId < 0) {
73,672,323!
585
    int32_t code = terrno;
×
586
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
587
    return code;
×
588
  } else {
589
    pInfo->self = refId;
73,672,323✔
590
  }
591

592
  return initDataSource(numOfSources, pInfo, id);
73,672,981✔
593
}
594

595
int32_t resetExchangeOperState(SOperatorInfo* pOper) {
22,652,700✔
596
  SExchangeInfo* pInfo = pOper->info;
22,652,700✔
597
  SExchangePhysiNode* pPhynode = (SExchangePhysiNode*)pOper->pPhyNode;
22,656,599✔
598

599
  qDebug("%s reset exchange op:%p info:%p", pOper->pTaskInfo->id.str, pOper, pInfo);
22,656,018✔
600

601
  (void)atomic_add_fetch_64(&pInfo->seqId, 1);
22,657,108✔
602
  pOper->status = OP_NOT_OPENED;
22,657,689✔
603
  pInfo->current = 0;
22,657,689✔
604
  pInfo->loadInfo.totalElapsed = 0;
22,657,689✔
605
  pInfo->loadInfo.totalRows = 0;
22,657,689✔
606
  pInfo->loadInfo.totalSize = 0;
22,657,689✔
607
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pSourceDataInfo); ++i) {
70,941,969✔
608
    SSourceDataInfo* pDataInfo = taosArrayGet(pInfo->pSourceDataInfo, i);
48,283,735✔
609
    taosWLockLatch(&pDataInfo->lock);
48,283,735✔
610
    taosMemoryFreeClear(pDataInfo->decompBuf);
48,283,190!
611
    taosMemoryFreeClear(pDataInfo->pRsp);
48,283,190!
612

613
    pDataInfo->totalRows = 0;
48,283,190✔
614
    pDataInfo->code = 0;
48,283,190✔
615
    pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
48,283,190✔
616
    pDataInfo->fetchSent = false;
48,283,190✔
617
    taosWUnLockLatch(&pDataInfo->lock);
48,283,190✔
618
  }
619

620
  if (pInfo->dynamicOp) {
22,657,689!
621
    taosArrayClearEx(pInfo->pSourceDataInfo, freeSourceDataInfo);
5,806,760✔
622
  } 
623

624
  taosArrayClearEx(pInfo->pResultBlockList, freeBlock);
22,657,689✔
625
  taosArrayClearEx(pInfo->pRecycledBlocks, freeBlock);
22,657,144✔
626

627
  blockDataCleanup(pInfo->pDummyBlock);
22,657,144✔
628

629
  void   *data = NULL;
22,657,144✔
630
  int32_t iter = 0;
22,657,144✔
631
  while ((data = tSimpleHashIterate(pInfo->pHashSources, data, &iter))) {
32,454,394✔
632
    ((SExchangeSrcIndex *)data)->inUseIdx = -1;
9,797,250✔
633
  }
634
  
635
  pInfo->limitInfo = (SLimitInfo){0};
22,657,144✔
636
  initLimitInfo(pPhynode->node.pLimit, pPhynode->node.pSlimit, &pInfo->limitInfo);
22,657,144✔
637

638
  return 0;
22,657,144✔
639
}
640

641
int32_t createExchangeOperatorInfo(void* pTransporter, SExchangePhysiNode* pExNode, SExecTaskInfo* pTaskInfo,
73,674,545✔
642
                                   SOperatorInfo** pOptrInfo) {
643
  QRY_PARAM_CHECK(pOptrInfo);
73,674,545!
644

645
  int32_t        code = 0;
73,677,038✔
646
  int32_t        lino = 0;
73,677,038✔
647
  SExchangeInfo* pInfo = taosMemoryCalloc(1, sizeof(SExchangeInfo));
73,677,038✔
648
  SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
73,672,186✔
649
  if (pInfo == NULL || pOperator == NULL) {
73,670,568!
650
    code = terrno;
×
651
    goto _error;
×
652
  }
653

654
  pOperator->pPhyNode = pExNode;
73,671,270✔
655
  pInfo->dynamicOp = pExNode->node.dynamicOp;
73,670,802!
656
  code = initExchangeOperator(pExNode, pInfo, GET_TASKID(pTaskInfo));
73,672,220✔
657
  QUERY_CHECK_CODE(code, lino, _error);
73,676,960!
658

659
  code = tsem_init(&pInfo->ready, 0, 0);
73,676,960✔
660
  QUERY_CHECK_CODE(code, lino, _error);
73,677,253!
661

662
  pInfo->pDummyBlock = createDataBlockFromDescNode(pExNode->node.pOutputDataBlockDesc);
73,677,253✔
663
  QUERY_CHECK_NULL(pInfo->pDummyBlock, code, lino, _error, terrno);
73,676,799!
664

665
  pInfo->pResultBlockList = taosArrayInit(64, POINTER_BYTES);
73,677,026✔
666
  QUERY_CHECK_NULL(pInfo->pResultBlockList, code, lino, _error, terrno);
73,676,726!
667
  pInfo->pRecycledBlocks = taosArrayInit(64, POINTER_BYTES);
73,676,726✔
668
  QUERY_CHECK_NULL(pInfo->pRecycledBlocks, code, lino, _error, terrno);
73,676,565!
669

670
  SExchangeOpStopInfo stopInfo = {QUERY_NODE_PHYSICAL_PLAN_EXCHANGE, pInfo->self};
73,677,026✔
671
  code = qAppendTaskStopInfo(pTaskInfo, &stopInfo);
73,676,799✔
672
  QUERY_CHECK_CODE(code, lino, _error);
73,676,565!
673

674
  pInfo->seqLoadData = pExNode->seqRecvData;
73,676,565!
675
  pInfo->dynTbname = pExNode->dynTbname;
73,676,104!
676
  if (pInfo->dynTbname) {
73,677,026!
677
    pInfo->seqLoadData = true;
16,296✔
678
  }
679
  pInfo->pTransporter = pTransporter;
73,676,792✔
680

681
  setOperatorInfo(pOperator, "ExchangeOperator", QUERY_NODE_PHYSICAL_PLAN_EXCHANGE, false, OP_NOT_OPENED, pInfo,
73,676,565✔
682
                  pTaskInfo);
683
  pOperator->exprSupp.numOfExprs = taosArrayGetSize(pInfo->pDummyBlock->pDataBlock);
73,675,299✔
684

685
  code = filterInitFromNode((SNode*)pExNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
73,677,253✔
686
                            pTaskInfo->pStreamRuntimeInfo);
73,676,565✔
687
  QUERY_CHECK_CODE(code, lino, _error);
73,676,038!
688
  qTrace("%s exchange op:%p", __func__, pOperator);
73,676,038!
689
  pOperator->fpSet = createOperatorFpSet(prepareLoadRemoteData, loadRemoteDataNext, NULL, destroyExchangeOperatorInfo,
73,676,038✔
690
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
691
  setOperatorResetStateFn(pOperator, resetExchangeOperState);
73,674,937✔
692
  *pOptrInfo = pOperator;
73,675,443✔
693
  return TSDB_CODE_SUCCESS;
73,675,670✔
694

695
_error:
×
696
  if (code != TSDB_CODE_SUCCESS) {
×
697
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
698
    pTaskInfo->code = code;
×
699
  }
700
  if (pInfo != NULL) {
×
701
    doDestroyExchangeOperatorInfo(pInfo);
×
702
  }
703

704
  if (pOperator != NULL) {
×
705
    pOperator->info = NULL;
×
706
    destroyOperator(pOperator);
×
707
  }
708
  return code;
×
709
}
710

711
void destroyExchangeOperatorInfo(void* param) {
73,677,253✔
712
  SExchangeInfo* pExInfo = (SExchangeInfo*)param;
73,677,253✔
713
  int32_t        code = taosRemoveRef(exchangeObjRefPool, pExInfo->self);
73,677,253✔
714
  if (code != TSDB_CODE_SUCCESS) {
73,676,665!
715
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
716
  }
717
}
73,676,665✔
718

719
void freeBlock(void* pParam) {
176,980,046✔
720
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
176,980,046✔
721
  blockDataDestroy(pBlock);
176,980,634✔
722
}
176,980,964✔
723

724
void freeSourceDataInfo(void* p) {
107,985,347✔
725
  SSourceDataInfo* pInfo = (SSourceDataInfo*)p;
107,985,347✔
726
  taosMemoryFreeClear(pInfo->decompBuf);
107,985,347!
727
  taosMemoryFreeClear(pInfo->pRsp);
107,985,935!
728

729
  pInfo->decompBufSize = 0;
107,984,481✔
730
}
107,985,581✔
731

732
void doDestroyExchangeOperatorInfo(void* param) {
73,677,253✔
733
  if (param == NULL) {
73,677,253!
734
    return;
×
735
  }
736
  SExchangeInfo* pExInfo = (SExchangeInfo*)param;
73,677,253✔
737
  if (pExInfo->pFetchRpcHandles) {
73,677,253✔
738
    for (int32_t i = 0; i < pExInfo->pFetchRpcHandles->size; ++i) {
189,093,078✔
739
      int64_t* pRpcHandle = taosArrayGet(pExInfo->pFetchRpcHandles, i);
115,416,864✔
740
      if (*pRpcHandle > 0) {
115,416,104✔
741
        SDownstreamSourceNode* pSource = taosArrayGet(pExInfo->pSources, i);
4,554,180✔
742
        (void)asyncFreeConnById(pExInfo->pTransporter, *pRpcHandle);
4,554,384✔
743
      }
744
    }
745
    taosArrayDestroy(pExInfo->pFetchRpcHandles);
73,676,697✔
746
  }
747

748
  taosArrayDestroy(pExInfo->pSources);
73,676,269✔
749
  taosArrayDestroyEx(pExInfo->pSourceDataInfo, freeSourceDataInfo);
73,676,108✔
750

751
  taosArrayDestroyEx(pExInfo->pResultBlockList, freeBlock);
73,674,964✔
752
  taosArrayDestroyEx(pExInfo->pRecycledBlocks, freeBlock);
73,676,431✔
753

754
  blockDataDestroy(pExInfo->pDummyBlock);
73,675,609✔
755
  tSimpleHashCleanup(pExInfo->pHashSources);
73,676,492✔
756

757
  int32_t code = tsem_destroy(&pExInfo->ready);
73,676,138✔
758
  if (code != TSDB_CODE_SUCCESS) {
73,676,665!
759
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
760
  }
761
  taosMemoryFreeClear(pExInfo->pTaskId);
73,676,665✔
762

763
  taosMemoryFreeClear(param);
73,675,384✔
764
}
765

766
int32_t loadRemoteDataCallback(void* param, SDataBuf* pMsg, int32_t code) {
171,606,129✔
767
  SFetchRspHandleWrapper* pWrapper = (SFetchRspHandleWrapper*)param;
171,606,129✔
768

769
  taosMemoryFreeClear(pMsg->pEpSet);
171,606,129!
770
  SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pWrapper->exchangeId);
171,613,986✔
771
  if (pExchangeInfo == NULL) {
171,614,276✔
772
    qWarn("failed to acquire exchange operator, since it may have been released, %p", pExchangeInfo);
38,412!
773
    taosMemoryFree(pMsg->pData);
38,412!
774
    return TSDB_CODE_SUCCESS;
38,412✔
775
  }
776

777
  int64_t currSeqId = atomic_load_64(&pExchangeInfo->seqId);
171,575,864✔
778
  if (pWrapper->seqId != currSeqId) {
171,577,861!
779
    qDebug("rsp reqId %" PRId64 " mismatch with exchange %p curr seqId %" PRId64 ", ignore it", pWrapper->seqId, pExchangeInfo, currSeqId);
×
780
    taosMemoryFree(pMsg->pData);
×
781
    code = taosReleaseRef(exchangeObjRefPool, pWrapper->exchangeId);
×
782
    if (code != TSDB_CODE_SUCCESS) {
×
783
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
784
    }
785
    return TSDB_CODE_SUCCESS;
×
786
  }
787

788
  int32_t          index = pWrapper->sourceIndex;
171,570,209✔
789

790
  qDebug("%s exchange %p %dth source got rsp, code:%d, rsp:%p", pExchangeInfo->pTaskId, pExchangeInfo, index, code, pMsg->pData);
171,574,034✔
791

792
  int64_t* pRpcHandle = taosArrayGet(pExchangeInfo->pFetchRpcHandles, index);
171,575,075✔
793
  if (pRpcHandle != NULL) {
171,577,703✔
794
    int32_t ret = asyncFreeConnById(pExchangeInfo->pTransporter, *pRpcHandle);
171,569,667✔
795
    if (ret != 0) {
171,580,426✔
796
      qDebug("failed to free rpc handle, code:%s, %p", tstrerror(ret), pExchangeInfo);
5,516,169✔
797
    }
798
    *pRpcHandle = -1;
171,580,426✔
799
  }
800

801
  SSourceDataInfo* pSourceDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, index);
171,576,315✔
802
  if (!pSourceDataInfo) {
171,577,466!
803
    return terrno;
×
804
  }
805

806
  if (0 == code && NULL == pMsg->pData) {
171,577,466!
807
    qError("invalid rsp msg, msgType:%d, len:%d", pMsg->msgType, pMsg->len);
×
808
    code = TSDB_CODE_QRY_INVALID_MSG;
×
809
  }
810

811
  taosWLockLatch(&pSourceDataInfo->lock);
171,584,241✔
812
  if (code == TSDB_CODE_SUCCESS) {
171,580,037✔
813
    pSourceDataInfo->seqId = pWrapper->seqId;
171,561,493✔
814
    pSourceDataInfo->pRsp = pMsg->pData;
171,558,690✔
815

816
    SRetrieveTableRsp* pRsp = pSourceDataInfo->pRsp;
171,547,844✔
817
    pRsp->numOfRows = htobe64(pRsp->numOfRows);
171,543,639✔
818
    pRsp->compLen = htonl(pRsp->compLen);
171,550,396✔
819
    pRsp->payloadLen = htonl(pRsp->payloadLen);
171,551,684✔
820
    pRsp->numOfCols = htonl(pRsp->numOfCols);
171,535,078✔
821
    pRsp->useconds = htobe64(pRsp->useconds);
171,549,796✔
822
    pRsp->numOfBlocks = htonl(pRsp->numOfBlocks);
171,521,015✔
823

824
    qDebug("%s fetch rsp received, index:%d, blocks:%d, rows:%" PRId64 ", %p", pSourceDataInfo->taskId, index,
171,534,261✔
825
           pRsp->numOfBlocks, pRsp->numOfRows, pExchangeInfo);
826
  } else {
827
    taosMemoryFree(pMsg->pData);
18,544!
828
    pSourceDataInfo->code = rpcCvtErrCode(code);
18,544✔
829
    if (pSourceDataInfo->code != code) {
18,544!
830
      qError("%s fetch rsp received, index:%d, error:%s, cvted error: %s, %p", pSourceDataInfo->taskId, index,
×
831
             tstrerror(code), tstrerror(pSourceDataInfo->code), pExchangeInfo);
832
    } else {
833
      qError("%s fetch rsp received, index:%d, error:%s, %p", pSourceDataInfo->taskId, index, tstrerror(code),
18,544!
834
             pExchangeInfo);
835
    }
836
  }
837

838
  tmemory_barrier();
171,560,062✔
839
  pSourceDataInfo->status = EX_SOURCE_DATA_READY;
171,560,062✔
840
  taosWUnLockLatch(&pSourceDataInfo->lock);
171,571,761✔
841
  
842
  code = tsem_post(&pExchangeInfo->ready);
171,563,500✔
843
  if (code != TSDB_CODE_SUCCESS) {
171,574,563!
844
    qError("failed to invoke post when fetch rsp is ready, code:%s, %p", tstrerror(code), pExchangeInfo);
×
845
    return code;
×
846
  }
847

848
  code = taosReleaseRef(exchangeObjRefPool, pWrapper->exchangeId);
171,574,563✔
849
  if (code != TSDB_CODE_SUCCESS) {
171,577,857!
850
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
851
  }
852
  return code;
171,578,673✔
853
}
854

855
int32_t buildTableScanOperatorParam(SOperatorParam** ppRes, SArray* pUidList, int32_t srcOpType, bool tableSeq) {
361,173✔
856
  *ppRes = taosMemoryMalloc(sizeof(SOperatorParam));
361,173!
857
  if (NULL == *ppRes) {
361,173!
858
    return terrno;
×
859
  }
860

861
  STableScanOperatorParam* pScan = taosMemoryMalloc(sizeof(STableScanOperatorParam));
361,173!
862
  if (NULL == pScan) {
361,173!
863
    taosMemoryFreeClear(*ppRes);
×
864
    return terrno;
×
865
  }
866

867
  pScan->pUidList = taosArrayDup(pUidList, NULL);
361,173✔
868
  if (NULL == pScan->pUidList) {
361,173!
869
    taosMemoryFree(pScan);
×
870
    taosMemoryFreeClear(*ppRes);
×
871
    return terrno;
×
872
  }
873
  pScan->tableSeq = tableSeq;
361,173✔
874
  pScan->pOrgTbInfo = NULL;
361,173✔
875
  pScan->window.skey = INT64_MAX;
361,173✔
876
  pScan->window.ekey = INT64_MIN;
361,173✔
877

878
  (*ppRes)->opType = srcOpType;
361,173✔
879
  (*ppRes)->downstreamIdx = 0;
361,173✔
880
  (*ppRes)->value = pScan;
361,173✔
881
  (*ppRes)->pChildren = NULL;
361,173✔
882
  (*ppRes)->reUse = false;
361,173✔
883

884
  return TSDB_CODE_SUCCESS;
361,173✔
885
}
886

887
int32_t buildTableScanOperatorParamEx(SOperatorParam** ppRes, SArray* pUidList, int32_t srcOpType, SOrgTbInfo *pMap, bool tableSeq, STimeWindow *window) {
36,215,182✔
888
  int32_t                  code = TSDB_CODE_SUCCESS;
36,215,182✔
889
  int32_t                  lino = 0;
36,215,182✔
890
  STableScanOperatorParam* pScan = NULL;
36,215,182✔
891

892
  *ppRes = taosMemoryMalloc(sizeof(SOperatorParam));
36,215,182!
893
  QUERY_CHECK_NULL(*ppRes, code, lino, _return, terrno);
36,215,182!
894

895
  pScan = taosMemoryMalloc(sizeof(STableScanOperatorParam));
36,215,182!
896
  QUERY_CHECK_NULL(pScan, code, lino, _return, terrno);
36,215,182!
897

898
  pScan->pUidList = taosArrayDup(pUidList, NULL);
36,215,182✔
899
  QUERY_CHECK_NULL(pScan->pUidList, code, lino, _return, terrno);
36,215,182!
900

901
  pScan->pOrgTbInfo = taosMemoryMalloc(sizeof(SOrgTbInfo));
36,215,182!
902
  QUERY_CHECK_NULL(pScan->pOrgTbInfo, code, lino, _return, terrno);
36,215,182!
903

904
  pScan->pOrgTbInfo->vgId = pMap->vgId;
36,215,182✔
905
  tstrncpy(pScan->pOrgTbInfo->tbName, pMap->tbName, TSDB_TABLE_FNAME_LEN);
36,215,182!
906

907
  pScan->pOrgTbInfo->colMap = taosArrayDup(pMap->colMap, NULL);
36,215,182✔
908
  QUERY_CHECK_NULL(pScan->pOrgTbInfo->colMap, code, lino, _return, terrno);
36,215,182!
909

910
  pScan->tableSeq = tableSeq;
36,215,182✔
911
  pScan->window.skey = window->skey;
36,215,182✔
912
  pScan->window.ekey = window->ekey;
36,215,182✔
913

914
  (*ppRes)->opType = srcOpType;
36,215,182✔
915
  (*ppRes)->downstreamIdx = 0;
36,215,182✔
916
  (*ppRes)->value = pScan;
36,215,182✔
917
  (*ppRes)->pChildren = NULL;
36,215,182✔
918
  (*ppRes)->reUse = false;
36,215,182✔
919

920
  return code;
36,215,182✔
921
_return:
×
922
  qError("%s failed at %d, failed to build scan operator msg:%s", __FUNCTION__, lino, tstrerror(code));
×
923
  taosMemoryFreeClear(*ppRes);
×
924
  if (pScan) {
×
925
    taosArrayDestroy(pScan->pUidList);
×
926
    if (pScan->pOrgTbInfo) {
×
927
      taosArrayDestroy(pScan->pOrgTbInfo->colMap);
×
928
      taosMemoryFreeClear(pScan->pOrgTbInfo);
×
929
    }
930
    taosMemoryFree(pScan);
×
931
  }
932
  return code;
×
933
}
934

935
int32_t buildTagScanOperatorParam(SOperatorParam** ppRes, SArray* pUidList, int32_t srcOpType) {
3,008,760✔
936
  int32_t                  code = TSDB_CODE_SUCCESS;
3,008,760✔
937
  int32_t                  lino = 0;
3,008,760✔
938
  STagScanOperatorParam*   pScan = NULL;
3,008,760✔
939

940
  *ppRes = taosMemoryMalloc(sizeof(SOperatorParam));
3,008,760!
941
  QUERY_CHECK_NULL(*ppRes, code, lino, _return, terrno);
3,008,760!
942

943
  pScan = taosMemoryMalloc(sizeof(STagScanOperatorParam));
3,008,760!
944
  QUERY_CHECK_NULL(pScan, code, lino, _return, terrno);
3,008,760!
945
  pScan->vcUid = *(tb_uid_t*)taosArrayGet(pUidList, 0);
3,008,760✔
946

947
  (*ppRes)->opType = srcOpType;
3,008,760✔
948
  (*ppRes)->downstreamIdx = 0;
3,008,760✔
949
  (*ppRes)->value = pScan;
3,008,760✔
950
  (*ppRes)->pChildren = NULL;
3,008,760✔
951
  (*ppRes)->reUse = false;
3,008,760✔
952

953
  return code;
3,008,760✔
954
_return:
×
955
  qError("%s failed at %d, failed to build scan operator msg:%s", __FUNCTION__, lino, tstrerror(code));
×
956
  taosMemoryFreeClear(*ppRes);
×
957
  if (pScan) {
×
958
    taosMemoryFree(pScan);
×
959
  }
960
  return code;
×
961
}
962

963
static int32_t getCurrentWinCalcTimeRange(SStreamRuntimeFuncInfo* pRuntimeInfo, STimeWindow* pTimeRange) {
4,708,183✔
964
  if (!pRuntimeInfo || !pTimeRange) {
4,708,183!
965
    return TSDB_CODE_INTERNAL_ERROR;
×
966
  }
967

968
  SSTriggerCalcParam* pParam = taosArrayGet(pRuntimeInfo->pStreamPesudoFuncVals, pRuntimeInfo->curIdx);
4,708,183✔
969
  if (!pParam) {
4,708,183!
970
    return TSDB_CODE_INTERNAL_ERROR;
×
971
  }
972

973
  switch (pRuntimeInfo->triggerType) {
4,708,183✔
974
    case STREAM_TRIGGER_SLIDING:
3,762,385✔
975
      // Unable to distinguish whether there is an interval, all use wstart/wend
976
      // and the results are equal to those of prevTs/currentTs, using the same address of union.
977
      pTimeRange->skey = pParam->wstart;  // is equal to wstart
3,762,385✔
978
      pTimeRange->ekey = pParam->wend;    // is equal to wend
3,762,385✔
979
      break;
3,762,385✔
980
    case STREAM_TRIGGER_PERIOD:
5,776✔
981
      pTimeRange->skey = pParam->prevLocalTime;
5,776✔
982
      pTimeRange->ekey = pParam->triggerTime;
5,776✔
983
      break;
5,776✔
984
    default:
940,022✔
985
      pTimeRange->skey = pParam->wstart;
940,022✔
986
      pTimeRange->ekey = pParam->wend;
940,022✔
987
      break;
940,022✔
988
  }
989

990
  return TSDB_CODE_SUCCESS;
4,708,183✔
991
}
992

993
int32_t doSendFetchDataRequest(SExchangeInfo* pExchangeInfo, SExecTaskInfo* pTaskInfo, int32_t sourceIndex) {
172,060,390✔
994
  int32_t          code = TSDB_CODE_SUCCESS;
172,060,390✔
995
  int32_t          lino = 0;
172,060,390✔
996
  SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, sourceIndex);
172,060,390✔
997
  if (!pDataInfo) {
172,059,216!
998
    return terrno;
×
999
  }
1000

1001
  if (EX_SOURCE_DATA_NOT_READY != pDataInfo->status) {
172,059,216!
1002
    return TSDB_CODE_SUCCESS;
×
1003
  }
1004

1005
  pDataInfo->status = EX_SOURCE_DATA_STARTED;
172,059,896✔
1006
  SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, pDataInfo->index);
172,060,762✔
1007
  if (!pSource) {
172,059,190!
1008
    return terrno;
×
1009
  }
1010

1011
  pDataInfo->startTime = taosGetTimestampUs();
172,058,147✔
1012
  size_t totalSources = taosArrayGetSize(pExchangeInfo->pSources);
172,060,964✔
1013

1014
  SFetchRspHandleWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SFetchRspHandleWrapper));
172,060,100✔
1015
  QUERY_CHECK_NULL(pWrapper, code, lino, _end, terrno);
172,058,414!
1016
  pWrapper->exchangeId = pExchangeInfo->self;
172,058,414✔
1017
  pWrapper->sourceIndex = sourceIndex;
172,060,174✔
1018
  pWrapper->seqId = pExchangeInfo->seqId;
172,060,499✔
1019

1020
  if (pSource->localExec) {
172,058,134!
1021
    SDataBuf pBuf = {0};
×
1022
    int32_t  code = (*pTaskInfo->localFetch.fp)(pTaskInfo->localFetch.handle, pSource->sId, pTaskInfo->id.queryId,
×
1023
                                               pSource->clientId, pSource->taskId, 0, pSource->execId, &pBuf.pData,
1024
                                               pTaskInfo->localFetch.explainRes);
1025
    code = loadRemoteDataCallback(pWrapper, &pBuf, code);
×
1026
    taosMemoryFree(pWrapper);
×
1027
    QUERY_CHECK_CODE(code, lino, _end);
×
1028
  } else {
1029
    bool needStreamPesudoFuncVals = true;
172,058,102✔
1030
    SResFetchReq req = {0};
172,058,102✔
1031
    req.header.vgId = pSource->addr.nodeId;
172,059,573✔
1032
    req.sId = pSource->sId;
172,059,804✔
1033
    req.clientId = pSource->clientId;
172,057,080✔
1034
    req.taskId = pSource->taskId;
172,059,819✔
1035
    req.queryId = pTaskInfo->id.queryId;
172,059,999✔
1036
    req.execId = pSource->execId;
172,056,383✔
1037
    if (pTaskInfo->pStreamRuntimeInfo) {
172,058,365✔
1038
      req.dynTbname = pExchangeInfo->dynTbname;
12,308,583!
1039
      req.execId = pTaskInfo->pStreamRuntimeInfo->execId;
12,308,583✔
1040
      req.pStRtFuncInfo = &pTaskInfo->pStreamRuntimeInfo->funcInfo;
12,308,583✔
1041

1042
      if (pSource->fetchMsgType == TDMT_STREAM_FETCH_FROM_RUNNER) {
12,308,583✔
1043
        qDebug("%s stream fetch from runner, execId:%d, %p", GET_TASKID(pTaskInfo), req.execId, pTaskInfo->pStreamRuntimeInfo);
46,294!
1044
      } else if (pSource->fetchMsgType == TDMT_STREAM_FETCH_FROM_CACHE) {
12,262,289✔
1045
        code = getCurrentWinCalcTimeRange(req.pStRtFuncInfo, &req.pStRtFuncInfo->curWindow);
4,708,183✔
1046
        QUERY_CHECK_CODE(code, lino, _end);
4,708,183!
1047
        needStreamPesudoFuncVals = false;
4,708,183✔
1048
        qDebug("%s stream fetch from cache, execId:%d, curWinIdx:%d, time range:[%" PRId64 ", %" PRId64 "]",
4,708,183✔
1049
               GET_TASKID(pTaskInfo), req.execId, req.pStRtFuncInfo->curIdx, req.pStRtFuncInfo->curWindow.skey,
1050
               req.pStRtFuncInfo->curWindow.ekey);
1051
      }
1052
      if (!pDataInfo->fetchSent) {
12,308,583✔
1053
        req.reset = pDataInfo->fetchSent = true;
11,170,967!
1054
      }
1055
    }
1056
    if (pDataInfo->isVtbRefScan) {
172,058,921!
1057
      code = buildTableScanOperatorParamEx(&req.pOpParam, pDataInfo->pSrcUidList, pDataInfo->srcOpType, pDataInfo->colMap, pDataInfo->tableSeq, &pDataInfo->window);
36,215,182!
1058
      taosArrayDestroy(pDataInfo->colMap->colMap);
36,215,182✔
1059
      taosMemoryFreeClear(pDataInfo->colMap);
36,215,182!
1060
      taosArrayDestroy(pDataInfo->pSrcUidList);
36,215,182✔
1061
      pDataInfo->pSrcUidList = NULL;
36,215,182✔
1062
      if (TSDB_CODE_SUCCESS != code) {
36,215,182!
1063
        pTaskInfo->code = code;
×
1064
        taosMemoryFree(pWrapper);
×
1065
        return pTaskInfo->code;
×
1066
      }
1067
    } else if (pDataInfo->isVtbTagScan) {
135,844,546!
1068
      code = buildTagScanOperatorParam(&req.pOpParam, pDataInfo->pSrcUidList, pDataInfo->srcOpType);
3,008,760✔
1069
      taosArrayDestroy(pDataInfo->pSrcUidList);
3,008,760✔
1070
      pDataInfo->pSrcUidList = NULL;
3,008,760✔
1071
      if (TSDB_CODE_SUCCESS != code) {
3,008,760!
1072
        pTaskInfo->code = code;
×
1073
        taosMemoryFree(pWrapper);
×
1074
        return pTaskInfo->code;
×
1075
      }
1076
    } else {
1077
      if (pDataInfo->pSrcUidList) {
132,835,786✔
1078
        code = buildTableScanOperatorParam(&req.pOpParam, pDataInfo->pSrcUidList, pDataInfo->srcOpType, pDataInfo->tableSeq);
345,612!
1079
        taosArrayDestroy(pDataInfo->pSrcUidList);
345,612✔
1080
        pDataInfo->pSrcUidList = NULL;
345,612✔
1081
        if (TSDB_CODE_SUCCESS != code) {
345,612!
1082
          pTaskInfo->code = code;
×
1083
          taosMemoryFree(pWrapper);
×
1084
          return pTaskInfo->code;
×
1085
        }
1086
      }
1087
    }
1088

1089
    int32_t msgSize = tSerializeSResFetchReq(NULL, 0, &req, needStreamPesudoFuncVals);
172,059,952✔
1090
    if (msgSize < 0) {
172,057,197!
1091
      pTaskInfo->code = msgSize;
×
1092
      taosMemoryFree(pWrapper);
×
1093
      freeOperatorParam(req.pOpParam, OP_GET_PARAM);
×
1094
      return pTaskInfo->code;
×
1095
    }
1096

1097
    void* msg = taosMemoryCalloc(1, msgSize);
172,057,197✔
1098
    if (NULL == msg) {
172,053,100!
1099
      pTaskInfo->code = terrno;
×
1100
      taosMemoryFree(pWrapper);
×
1101
      freeOperatorParam(req.pOpParam, OP_GET_PARAM);
×
1102
      return pTaskInfo->code;
×
1103
    }
1104

1105
    msgSize = tSerializeSResFetchReq(msg, msgSize, &req, needStreamPesudoFuncVals);
172,053,100✔
1106
    if (msgSize < 0) {
172,055,774!
1107
      pTaskInfo->code = msgSize;
×
1108
      taosMemoryFree(pWrapper);
×
1109
      taosMemoryFree(msg);
×
1110
      freeOperatorParam(req.pOpParam, OP_GET_PARAM);
×
1111
      return pTaskInfo->code;
×
1112
    }
1113

1114
    freeOperatorParam(req.pOpParam, OP_GET_PARAM);
172,055,774✔
1115

1116
    qDebug("%s build fetch msg and send to vgId:%d, ep:%s, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
172,056,811✔
1117
           ", seqId:%" PRId64 ", execId:%d, %p, %d/%" PRIzu,
1118
           GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->addr.epSet.eps[0].fqdn, pSource->clientId,
1119
           pSource->taskId, pExchangeInfo->seqId, pSource->execId, pExchangeInfo, sourceIndex, totalSources);
1120

1121
    // send the fetch remote task result reques
1122
    SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
172,061,845✔
1123
    if (NULL == pMsgSendInfo) {
172,059,920!
1124
      taosMemoryFreeClear(msg);
×
1125
      taosMemoryFree(pWrapper);
×
1126
      qError("%s prepare message %d failed", GET_TASKID(pTaskInfo), (int32_t)sizeof(SMsgSendInfo));
×
1127
      pTaskInfo->code = terrno;
×
1128
      return pTaskInfo->code;
×
1129
    }
1130

1131
    pMsgSendInfo->param = pWrapper;
172,059,920✔
1132
    pMsgSendInfo->paramFreeFp = taosAutoMemoryFree;
172,060,721✔
1133
    pMsgSendInfo->msgInfo.pData = msg;
172,060,721✔
1134
    pMsgSendInfo->msgInfo.len = msgSize;
172,061,272✔
1135
    pMsgSendInfo->msgType = pSource->fetchMsgType;
172,059,946✔
1136
    pMsgSendInfo->fp = loadRemoteDataCallback;
172,061,845✔
1137
    pMsgSendInfo->requestId = pTaskInfo->id.queryId;
172,061,274✔
1138

1139
    int64_t transporterId = 0;
172,061,641✔
1140
    void* poolHandle = NULL;
172,061,845✔
1141
    code = asyncSendMsgToServer(pExchangeInfo->pTransporter, &pSource->addr.epSet, &transporterId, pMsgSendInfo);
172,061,845✔
1142
    QUERY_CHECK_CODE(code, lino, _end);
172,061,257!
1143
    int64_t* pRpcHandle = taosArrayGet(pExchangeInfo->pFetchRpcHandles, sourceIndex);
172,061,257✔
1144
    *pRpcHandle = transporterId;
172,061,845✔
1145
  }
1146

1147
_end:
172,061,363✔
1148
  if (code != TSDB_CODE_SUCCESS) {
172,061,363!
1149
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1150
  }
1151
  return code;
172,061,641✔
1152
}
1153

1154
void updateLoadRemoteInfo(SLoadRemoteDataInfo* pInfo, int64_t numOfRows, int32_t dataLen, int64_t startTs,
130,449,036✔
1155
                          SOperatorInfo* pOperator) {
1156
  pInfo->totalRows += numOfRows;
130,449,036✔
1157
  pInfo->totalSize += dataLen;
130,449,036✔
1158
  pInfo->totalElapsed += (taosGetTimestampUs() - startTs);
130,449,036✔
1159
  pOperator->resultInfo.totalRows += numOfRows;
130,449,036✔
1160
}
130,449,036✔
1161

1162
int32_t extractDataBlockFromFetchRsp(SSDataBlock* pRes, char* pData, SArray* pColList, char** pNextStart) {
366,481,244✔
1163
  int32_t      code = TSDB_CODE_SUCCESS;
366,481,244✔
1164
  int32_t      lino = 0;
366,481,244✔
1165
  SSDataBlock* pBlock = NULL;
366,481,244✔
1166
  if (pColList == NULL) {  // data from other sources
366,482,094✔
1167
    blockDataCleanup(pRes);
361,448,964✔
1168
    code = blockDecodeInternal(pRes, pData, (const char**)pNextStart);
361,446,748✔
1169
    if (code) {
361,449,202!
1170
      return code;
×
1171
    }
1172
  } else {  // extract data according to pColList
1173
    char* pStart = pData;
5,033,130✔
1174

1175
    int32_t numOfCols = htonl(*(int32_t*)pStart);
5,033,130✔
1176
    pStart += sizeof(int32_t);
5,033,681✔
1177

1178
    // todo refactor:extract method
1179
    SSysTableSchema* pSchema = (SSysTableSchema*)pStart;
5,033,681✔
1180
    for (int32_t i = 0; i < numOfCols; ++i) {
71,942,625✔
1181
      SSysTableSchema* p = (SSysTableSchema*)pStart;
66,908,944✔
1182

1183
      p->colId = htons(p->colId);
66,908,944✔
1184
      p->bytes = htonl(p->bytes);
66,908,944✔
1185
      pStart += sizeof(SSysTableSchema);
66,908,944✔
1186
    }
1187

1188
    pBlock = NULL;
5,033,681✔
1189
    code = createDataBlock(&pBlock);
5,033,681✔
1190
    QUERY_CHECK_CODE(code, lino, _end);
5,033,681!
1191

1192
    for (int32_t i = 0; i < numOfCols; ++i) {
71,942,625✔
1193
      SColumnInfoData idata = createColumnInfoData(pSchema[i].type, pSchema[i].bytes, pSchema[i].colId);
66,908,944✔
1194
      code = blockDataAppendColInfo(pBlock, &idata);
66,908,944✔
1195
      QUERY_CHECK_CODE(code, lino, _end);
66,908,944!
1196
    }
1197

1198
    code = blockDecodeInternal(pBlock, pStart, NULL);
5,033,681✔
1199
    QUERY_CHECK_CODE(code, lino, _end);
5,033,681!
1200

1201
    code = blockDataEnsureCapacity(pRes, pBlock->info.rows);
5,033,681✔
1202
    QUERY_CHECK_CODE(code, lino, _end);
5,033,681!
1203

1204
    // data from mnode
1205
    pRes->info.dataLoad = 1;
5,033,681✔
1206
    pRes->info.rows = pBlock->info.rows;
5,033,681✔
1207
    pRes->info.scanFlag = MAIN_SCAN;
5,033,681✔
1208
    code = relocateColumnData(pRes, pColList, pBlock->pDataBlock, false);
5,033,681✔
1209
    QUERY_CHECK_CODE(code, lino, _end);
5,033,681!
1210

1211
    blockDataDestroy(pBlock);
5,033,681✔
1212
    pBlock = NULL;
5,033,681✔
1213
  }
1214

1215
_end:
366,482,883✔
1216
  if (code != TSDB_CODE_SUCCESS) {
366,481,798!
1217
    blockDataDestroy(pBlock);
×
1218
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1219
  }
1220
  return code;
366,481,256✔
1221
}
1222

1223
void setAllSourcesCompleted(SOperatorInfo* pOperator) {
67,465,734✔
1224
  SExchangeInfo* pExchangeInfo = pOperator->info;
67,465,734✔
1225
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
67,466,246✔
1226

1227
  SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
67,466,246✔
1228
  size_t               totalSources = taosArrayGetSize(pExchangeInfo->pSources);
67,466,246✔
1229
  qDebug("%s all %" PRIzu " sources are exhausted, total rows: %" PRIu64 ", %.2f Kb, elapsed:%.2f ms",
67,465,695✔
1230
         GET_TASKID(pTaskInfo), totalSources, pLoadInfo->totalRows, pLoadInfo->totalSize / 1024.0,
1231
         pLoadInfo->totalElapsed / 1000.0);
1232

1233
  setOperatorCompleted(pOperator);
67,465,695✔
1234
}
67,466,246✔
1235

1236
int32_t getCompletedSources(const SArray* pArray, int32_t* pRes) {
205,818,458✔
1237
  int32_t code = TSDB_CODE_SUCCESS;
205,818,458✔
1238
  int32_t lino = 0;
205,818,458✔
1239
  size_t  total = taosArrayGetSize(pArray);
205,818,458✔
1240

1241
  int32_t completed = 0;
205,819,013✔
1242
  for (int32_t k = 0; k < total; ++k) {
564,127,959✔
1243
    SSourceDataInfo* p = taosArrayGet(pArray, k);
358,309,065✔
1244
    QUERY_CHECK_NULL(p, code, lino, _end, terrno);
358,309,065!
1245
    if (p->status == EX_SOURCE_DATA_EXHAUSTED) {
358,309,065✔
1246
      qDebug("source %d is completed, info:%p %p", k, pArray, p);
168,357,502✔
1247
      completed += 1;
168,357,502✔
1248
    }
1249
  }
1250

1251
  *pRes = completed;
205,818,894✔
1252
_end:
205,818,575✔
1253
  if (code != TSDB_CODE_SUCCESS) {
205,818,575!
1254
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1255
  }
1256
  return code;
205,818,809✔
1257
}
1258

1259
int32_t prepareConcurrentlyLoad(SOperatorInfo* pOperator) {
70,036,344✔
1260
  SExchangeInfo* pExchangeInfo = pOperator->info;
70,036,344✔
1261
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
70,036,023✔
1262

1263
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSourceDataInfo);
70,036,932✔
1264
  int64_t startTs = taosGetTimestampUs();
70,035,556✔
1265

1266
  // Asynchronously send all fetch requests to all sources.
1267
  for (int32_t i = 0; i < totalSources; ++i) {
176,228,446✔
1268
    int32_t code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, i);
106,187,061✔
1269
    if (code != TSDB_CODE_SUCCESS) {
106,192,393!
1270
      pTaskInfo->code = code;
×
1271
      return code;
×
1272
    }
1273
  }
1274

1275
  int64_t endTs = taosGetTimestampUs();
70,037,799✔
1276
  qDebug("%s send all fetch requests to %" PRIzu " sources completed, elapsed:%.2fms", GET_TASKID(pTaskInfo),
70,037,799✔
1277
         totalSources, (endTs - startTs) / 1000.0);
1278

1279
  pOperator->status = OP_RES_TO_RETURN;
70,037,799✔
1280
  pOperator->cost.openCost = taosGetTimestampUs() - startTs;
70,037,799✔
1281
  if (isTaskKilled(pTaskInfo)) {
70,037,799!
1282
    T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
1283
  }
1284

1285
  return TSDB_CODE_SUCCESS;
70,037,396✔
1286
}
1287

1288
int32_t doExtractResultBlocks(SExchangeInfo* pExchangeInfo, SSourceDataInfo* pDataInfo) {
125,415,355✔
1289
  int32_t            code = TSDB_CODE_SUCCESS;
125,415,355✔
1290
  int32_t            lino = 0;
125,415,355✔
1291
  SRetrieveTableRsp* pRetrieveRsp = pDataInfo->pRsp;
125,415,355✔
1292
  SSDataBlock*       pb = NULL;
125,415,355✔
1293

1294
  char* pNextStart = pRetrieveRsp->data;
125,415,355✔
1295
  char* pStart = pNextStart;
125,415,140✔
1296

1297
  int32_t index = 0;
125,415,355✔
1298

1299
  if (pRetrieveRsp->compressed) {  // decompress the data
125,415,355!
1300
    if (pDataInfo->decompBuf == NULL) {
×
1301
      pDataInfo->decompBuf = taosMemoryMalloc(pRetrieveRsp->payloadLen);
×
1302
      QUERY_CHECK_NULL(pDataInfo->decompBuf, code, lino, _end, terrno);
×
1303
      pDataInfo->decompBufSize = pRetrieveRsp->payloadLen;
×
1304
    } else {
1305
      if (pDataInfo->decompBufSize < pRetrieveRsp->payloadLen) {
×
1306
        char* p = taosMemoryRealloc(pDataInfo->decompBuf, pRetrieveRsp->payloadLen);
×
1307
        QUERY_CHECK_NULL(p, code, lino, _end, terrno);
×
1308
        if (p != NULL) {
×
1309
          pDataInfo->decompBuf = p;
×
1310
          pDataInfo->decompBufSize = pRetrieveRsp->payloadLen;
×
1311
        }
1312
      }
1313
    }
1314
  }
1315

1316
  while (index++ < pRetrieveRsp->numOfBlocks) {
486,865,633✔
1317
    pStart = pNextStart;
361,450,051✔
1318

1319
    if (taosArrayGetSize(pExchangeInfo->pRecycledBlocks) > 0) {
361,450,051✔
1320
      pb = *(SSDataBlock**)taosArrayPop(pExchangeInfo->pRecycledBlocks);
184,464,066✔
1321
      blockDataCleanup(pb);
184,464,777✔
1322
    } else {
1323
      code = createOneDataBlock(pExchangeInfo->pDummyBlock, false, &pb);
176,985,086✔
1324
      QUERY_CHECK_NULL(pb, code, lino, _end, code);
176,982,270!
1325
    }
1326

1327
    int32_t compLen = *(int32_t*)pStart;
361,447,047✔
1328
    pStart += sizeof(int32_t);
361,447,592✔
1329

1330
    int32_t rawLen = *(int32_t*)pStart;
361,448,126✔
1331
    pStart += sizeof(int32_t);
361,448,126✔
1332
    QUERY_CHECK_CONDITION((compLen <= rawLen && compLen != 0), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
361,447,892!
1333

1334
    pNextStart = pStart + compLen;
361,447,892✔
1335
    if (pRetrieveRsp->compressed && (compLen < rawLen)) {
361,446,492!
1336
      int32_t t = tsDecompressString(pStart, compLen, 1, pDataInfo->decompBuf, rawLen, ONE_STAGE_COMP, NULL, 0);
×
1337
      QUERY_CHECK_CONDITION((t == rawLen), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
×
1338
      pStart = pDataInfo->decompBuf;
×
1339
    }
1340

1341
    code = extractDataBlockFromFetchRsp(pb, pStart, NULL, &pStart);
361,447,351✔
1342
    if (code != 0) {
361,448,659!
1343
      taosMemoryFreeClear(pDataInfo->pRsp);
×
1344
      goto _end;
×
1345
    }
1346

1347
    void* tmp = taosArrayPush(pExchangeInfo->pResultBlockList, &pb);
361,448,659✔
1348
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
361,446,509!
1349
    qDebug("%dth block added to resultBlockList, rows:%" PRId64, index, pb->info.rows);
361,446,509✔
1350
    pb = NULL;
361,450,278✔
1351
  }
1352

1353
_end:
125,415,355✔
1354
  if (code != TSDB_CODE_SUCCESS) {
125,415,355!
1355
    blockDataDestroy(pb);
×
1356
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1357
  }
1358
  return code;
125,415,355✔
1359
}
1360

1361
int32_t seqLoadRemoteData(SOperatorInfo* pOperator) {
39,499,589✔
1362
  SExchangeInfo* pExchangeInfo = pOperator->info;
39,499,589✔
1363
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
39,499,589✔
1364

1365
  int32_t code = 0;
39,499,589✔
1366
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSources);
39,499,589✔
1367
  int64_t startTs = taosGetTimestampUs();
39,499,589✔
1368

1369
  int32_t vgId = 0;
39,499,589✔
1370
  if (pExchangeInfo->dynTbname) {
39,499,589!
1371
    SArray* vals = pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamPartColVals;
47,268✔
1372
    for (int32_t i = 0; i < taosArrayGetSize(vals); ++i) {
47,268!
1373
      SStreamGroupValue* pValue = taosArrayGet(vals, i);
47,268✔
1374
      if (pValue != NULL && pValue->isTbname) {
47,268!
1375
        vgId = pValue->vgId;
47,268✔
1376
        break;
47,268✔
1377
      }
1378
    }
1379
  }
1380

1381
  while (1) {
9,085,934✔
1382
    if (pExchangeInfo->current >= totalSources) {
48,585,523✔
1383
      setAllSourcesCompleted(pOperator);
9,087,527✔
1384
      return TSDB_CODE_SUCCESS;
9,087,527✔
1385
    }
1386

1387
    SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, pExchangeInfo->current);
39,497,996✔
1388
    if (!pSource) {
39,497,996!
1389
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1390
      pTaskInfo->code = terrno;
×
1391
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
1392
    }
1393

1394
    if (vgId != 0 && pSource->addr.nodeId != vgId){
39,497,996✔
1395
      pExchangeInfo->current += 1;
23,472✔
1396
      continue;
23,472✔
1397
    }
1398

1399
    SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, pExchangeInfo->current);
39,474,524✔
1400
    if (!pDataInfo) {
39,474,524!
1401
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1402
      pTaskInfo->code = terrno;
×
1403
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
1404
    }
1405
    pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
39,474,524✔
1406

1407
    code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, pExchangeInfo->current);
39,474,524✔
1408
    if (code != TSDB_CODE_SUCCESS) {
39,474,524!
1409
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1410
      pTaskInfo->code = code;
×
1411
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
1412
    }
1413

1414
    while (true) {
630✔
1415
      code = exchangeWait(pOperator, pExchangeInfo);
39,475,154✔
1416
      if (code != TSDB_CODE_SUCCESS || isTaskKilled(pTaskInfo)) {
39,475,154!
1417
        T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
630!
1418
      }
1419

1420
      int64_t currSeqId = atomic_load_64(&pExchangeInfo->seqId);
39,474,524✔
1421
      if (pDataInfo->seqId != currSeqId) {
39,474,524✔
1422
        qDebug("seq rsp reqId %" PRId64 " mismatch with exchange %p curr seqId %" PRId64 ", ignore it", pDataInfo->seqId, pExchangeInfo, currSeqId);
630!
1423
        taosMemoryFreeClear(pDataInfo->pRsp);
630!
1424
        continue;
630✔
1425
      }
1426

1427
      break;
39,473,894✔
1428
    }
1429

1430
    if (pDataInfo->code != TSDB_CODE_SUCCESS) {
39,473,894!
1431
      qError("%s vgId:%d, clientId:0x%" PRIx64 " taskID:0x%" PRIx64 " execId:%d error happens, code:%s",
×
1432
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
1433
             tstrerror(pDataInfo->code));
1434
      pOperator->pTaskInfo->code = pDataInfo->code;
×
1435
      return pOperator->pTaskInfo->code;
×
1436
    }
1437

1438
    SRetrieveTableRsp*   pRsp = pDataInfo->pRsp;
39,473,894✔
1439
    SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
39,473,894✔
1440

1441
    if (pRsp->numOfRows == 0) {
39,473,894✔
1442
      qDebug("exhausted %p,%s vgId:%d, clientId:0x%" PRIx64 " taskID:0x%" PRIx64
9,062,462✔
1443
             " execId:%d %d of total completed, rowsOfSource:%" PRIu64 ", totalRows:%" PRIu64 " try next", pDataInfo,
1444
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
1445
             pExchangeInfo->current + 1, pDataInfo->totalRows, pLoadInfo->totalRows);
1446

1447
      pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
9,062,462✔
1448
      if (pDataInfo->isVtbRefScan || pDataInfo->isVtbTagScan) {
9,062,462!
1449
        pExchangeInfo->current = totalSources;
9,020,775✔
1450
      } else {
1451
        pExchangeInfo->current += 1;
41,687✔
1452
      }
1453
      taosMemoryFreeClear(pDataInfo->pRsp);
9,062,462!
1454
      continue;
9,062,462✔
1455
    }
1456

1457
    code = doExtractResultBlocks(pExchangeInfo, pDataInfo);
30,411,432✔
1458
    if (code != TSDB_CODE_SUCCESS) {
30,411,432!
1459
      goto _error;
×
1460
    }
1461

1462
    SRetrieveTableRsp* pRetrieveRsp = pDataInfo->pRsp;
30,411,432✔
1463
    if (pRsp->completed == 1) {
30,411,432✔
1464
      qDebug("exhausted %p,%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64 " execId:%d numOfRows:%" PRId64
99,488!
1465
             ", rowsOfSource:%" PRIu64 ", totalRows:%" PRIu64 ", totalBytes:%" PRIu64 " try next %d/%" PRIzu, pDataInfo,
1466
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
1467
             pRetrieveRsp->numOfRows, pDataInfo->totalRows, pLoadInfo->totalRows, pLoadInfo->totalSize,
1468
             pExchangeInfo->current + 1, totalSources);
1469

1470
      pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
99,488✔
1471
      if (pDataInfo->isVtbRefScan) {
99,488!
1472
        pExchangeInfo->current = totalSources;
×
1473
      } else {
1474
        pExchangeInfo->current += 1;
99,488✔
1475
      }
1476
    } else {
1477
      qDebug("%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64 " execId:%d numOfRows:%" PRId64
30,311,944✔
1478
             ", totalRows:%" PRIu64 ", totalBytes:%" PRIu64,
1479
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
1480
             pRetrieveRsp->numOfRows, pLoadInfo->totalRows, pLoadInfo->totalSize);
1481
    }
1482
    if (pExchangeInfo->dynamicOp && pExchangeInfo->seqLoadData) {
30,411,432!
1483
      taosArrayClear(pExchangeInfo->pSourceDataInfo);
30,148,449✔
1484
    }
1485
    updateLoadRemoteInfo(pLoadInfo, pRetrieveRsp->numOfRows, pRetrieveRsp->compLen, startTs, pOperator);
30,411,432✔
1486
    pDataInfo->totalRows += pRetrieveRsp->numOfRows;
30,411,432✔
1487

1488
    taosMemoryFreeClear(pDataInfo->pRsp);
30,411,432!
1489
    return TSDB_CODE_SUCCESS;
30,411,432✔
1490
  }
1491

1492
_error:
×
1493
  pTaskInfo->code = code;
×
1494
  return code;
×
1495
}
1496

1497
void clearVtbScanDataInfo(void* pItem) {
8,102,860✔
1498
  SSourceDataInfo *pInfo = (SSourceDataInfo *)pItem;
8,102,860✔
1499
  if (pInfo->colMap) {
8,102,860!
1500
    taosArrayDestroy(pInfo->colMap->colMap);
×
1501
    taosMemoryFreeClear(pInfo->colMap);
×
1502
  }
1503
  taosArrayDestroy(pInfo->pSrcUidList);
8,102,860✔
1504
}
8,102,860✔
1505

1506
int32_t addSingleExchangeSource(SOperatorInfo* pOperator, SExchangeOperatorBasicParam* pBasicParam) {
39,569,554✔
1507
  SExchangeInfo*     pExchangeInfo = pOperator->info;
39,569,554✔
1508
  SExchangeSrcIndex* pIdx = tSimpleHashGet(pExchangeInfo->pHashSources, &pBasicParam->vgId, sizeof(pBasicParam->vgId));
39,569,554✔
1509
  if (NULL == pIdx) {
39,569,554✔
1510
    if (pBasicParam->isNewDeployed) {
10,355!
1511
      SDownstreamSourceNode *pNode = NULL;
10,355✔
1512
      int32_t code = nodesCloneNode((SNode*)&pBasicParam->newDeployedSrc, (SNode**)&pNode);
10,355✔
1513
      if (code != TSDB_CODE_SUCCESS) {
10,355!
1514
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1515
        return code;
×
1516
      }
1517

1518
      void* tmp = taosArrayPush(pExchangeInfo->pSources, pNode);
10,355✔
1519
      if (!tmp) {
10,355!
1520
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1521
        return terrno;
×
1522
      }
1523
      SExchangeSrcIndex idx = {.srcIdx = taosArrayGetSize(pExchangeInfo->pSources) - 1, .inUseIdx = -1};
10,355✔
1524
      code =
1525
          tSimpleHashPut(pExchangeInfo->pHashSources, &pNode->addr.nodeId, sizeof(pNode->addr.nodeId), &idx, sizeof(idx));
10,355✔
1526
      if (pExchangeInfo->pHashSources && code != TSDB_CODE_SUCCESS) {
10,355!
1527
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1528
        return code;
×
1529
      }
1530
      pIdx = tSimpleHashGet(pExchangeInfo->pHashSources, &pBasicParam->vgId, sizeof(pBasicParam->vgId));
10,355✔
1531
      if (pIdx == NULL) {
10,355!
1532
        qError("No exchange source for vgId: %d", pBasicParam->vgId);
×
1533
        return TSDB_CODE_INVALID_PARA;
×
1534
      }
1535
    } else {
1536
      qError("No exchange source for vgId: %d", pBasicParam->vgId);
×
1537
      return TSDB_CODE_INVALID_PARA;
×
1538
    }
1539
  }
1540

1541
  qDebug("start to add single exchange source");
39,569,554✔
1542

1543
  if (pBasicParam->isVtbRefScan) {
39,569,554!
1544
    SSourceDataInfo dataInfo = {0};
36,215,182✔
1545
    dataInfo.status = EX_SOURCE_DATA_NOT_READY;
36,215,182✔
1546
    dataInfo.taskId = pExchangeInfo->pTaskId;
36,215,182✔
1547
    dataInfo.index = pIdx->srcIdx;
36,215,182✔
1548
    dataInfo.window = pBasicParam->window;
36,215,182✔
1549
    dataInfo.colMap = taosMemoryMalloc(sizeof(SOrgTbInfo));
36,215,182!
1550
    dataInfo.colMap->vgId = pBasicParam->colMap->vgId;
36,215,182✔
1551
    tstrncpy(dataInfo.colMap->tbName, pBasicParam->colMap->tbName, TSDB_TABLE_FNAME_LEN);
36,215,182!
1552
    dataInfo.colMap->colMap = taosArrayDup(pBasicParam->colMap->colMap, NULL);
36,215,182✔
1553

1554
    dataInfo.pSrcUidList = taosArrayDup(pBasicParam->uidList, NULL);
36,215,182✔
1555
    if (dataInfo.pSrcUidList == NULL) {
36,215,182!
1556
      return terrno;
×
1557
    }
1558

1559
    dataInfo.isVtbRefScan = pBasicParam->isVtbRefScan;
36,215,182!
1560
    dataInfo.isVtbTagScan = pBasicParam->isVtbTagScan;
36,215,182!
1561
    dataInfo.srcOpType = pBasicParam->srcOpType;
36,215,182✔
1562
    dataInfo.tableSeq = pBasicParam->tableSeq;
36,215,182!
1563

1564
    taosArrayClearEx(pExchangeInfo->pSourceDataInfo, clearVtbScanDataInfo);
36,215,182✔
1565
    void* tmp = taosArrayPush(pExchangeInfo->pSourceDataInfo, &dataInfo);
36,215,182✔
1566
    if (!tmp) {
36,215,182!
1567
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1568
      return terrno;
×
1569
    }
1570
  } else if (pBasicParam->isVtbTagScan) {
3,354,372!
1571
    SSourceDataInfo dataInfo = {0};
3,008,760✔
1572
    dataInfo.status = EX_SOURCE_DATA_NOT_READY;
3,008,760✔
1573
    dataInfo.taskId = pExchangeInfo->pTaskId;
3,008,760✔
1574
    dataInfo.index = pIdx->srcIdx;
3,008,760✔
1575
    dataInfo.window = pBasicParam->window;
3,008,760✔
1576
    dataInfo.colMap = NULL;
3,008,760✔
1577

1578
    dataInfo.pSrcUidList = taosArrayDup(pBasicParam->uidList, NULL);
3,008,760✔
1579
    if (dataInfo.pSrcUidList == NULL) {
3,008,760!
1580
      return terrno;
×
1581
    }
1582

1583
    dataInfo.isVtbRefScan = pBasicParam->isVtbRefScan;
3,008,760!
1584
    dataInfo.isVtbTagScan = pBasicParam->isVtbTagScan;
3,008,760!
1585
    dataInfo.srcOpType = pBasicParam->srcOpType;
3,008,760✔
1586
    dataInfo.tableSeq = pBasicParam->tableSeq;
3,008,760!
1587

1588
    taosArrayClearEx(pExchangeInfo->pSourceDataInfo, clearVtbScanDataInfo);
3,008,760✔
1589
    void* tmp = taosArrayPush(pExchangeInfo->pSourceDataInfo, &dataInfo);
3,008,760✔
1590
    if (!tmp) {
3,008,760!
1591
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1592
      return terrno;
×
1593
    }
1594
  } else {
1595
    if (pIdx->inUseIdx < 0) {
345,612✔
1596
      SSourceDataInfo dataInfo = {0};
343,752✔
1597
      dataInfo.status = EX_SOURCE_DATA_NOT_READY;
343,752✔
1598
      dataInfo.taskId = pExchangeInfo->pTaskId;
343,752✔
1599
      dataInfo.index = pIdx->srcIdx;
343,752✔
1600
      if (pBasicParam->isVtbRefScan) {
343,752!
1601
        dataInfo.window = pBasicParam->window;
×
1602
        dataInfo.colMap = taosMemoryMalloc(sizeof(SOrgTbInfo));
×
1603
        dataInfo.colMap->vgId = pBasicParam->colMap->vgId;
×
1604
        tstrncpy(dataInfo.colMap->tbName, pBasicParam->colMap->tbName, TSDB_TABLE_FNAME_LEN);
×
1605
        dataInfo.colMap->colMap = taosArrayDup(pBasicParam->colMap->colMap, NULL);
×
1606
      }
1607

1608
      dataInfo.pSrcUidList = taosArrayDup(pBasicParam->uidList, NULL);
343,752✔
1609
      if (dataInfo.pSrcUidList == NULL) {
343,752!
1610
        return terrno;
×
1611
      }
1612

1613
      dataInfo.isVtbRefScan = pBasicParam->isVtbRefScan;
343,752!
1614
      dataInfo.isVtbTagScan = pBasicParam->isVtbTagScan;
343,752!
1615
      dataInfo.srcOpType = pBasicParam->srcOpType;
343,752✔
1616
      dataInfo.tableSeq = pBasicParam->tableSeq;
343,752!
1617

1618
      void* tmp = taosArrayPush(pExchangeInfo->pSourceDataInfo, &dataInfo);
343,752✔
1619
      if (!tmp) {
343,752!
1620
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1621
        return terrno;
×
1622
      }
1623
      pIdx->inUseIdx = taosArrayGetSize(pExchangeInfo->pSourceDataInfo) - 1;
343,752✔
1624
    } else {
1625
      SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, pIdx->inUseIdx);
1,860✔
1626
      if (!pDataInfo) {
1,860!
1627
        return terrno;
×
1628
      }
1629
      if (pDataInfo->status == EX_SOURCE_DATA_EXHAUSTED) {
1,860!
1630
        pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
1,860✔
1631
      }
1632

1633
      if (pBasicParam->isVtbRefScan) {
1,860!
1634
        pDataInfo->window = pBasicParam->window;
×
1635
        if (!pDataInfo->colMap) {
×
1636
          pDataInfo->colMap = taosMemoryMalloc(sizeof(SOrgTbInfo));
×
1637
        }
1638
        pDataInfo->colMap->vgId = pBasicParam->colMap->vgId;
×
1639
        tstrncpy(pDataInfo->colMap->tbName, pBasicParam->colMap->tbName, TSDB_TABLE_FNAME_LEN);
×
1640
        pDataInfo->colMap->colMap = taosArrayDup(pBasicParam->colMap->colMap, NULL);
×
1641
      }
1642

1643
      pDataInfo->pSrcUidList = taosArrayDup(pBasicParam->uidList, NULL);
1,860✔
1644
      if (pDataInfo->pSrcUidList == NULL) {
1,860!
1645
        return terrno;
×
1646
      }
1647

1648
      pDataInfo->isVtbRefScan = pBasicParam->isVtbRefScan;
1,860!
1649
      pDataInfo->isVtbTagScan = pBasicParam->isVtbTagScan;
1,860!
1650
      pDataInfo->srcOpType = pBasicParam->srcOpType;
1,860✔
1651
      pDataInfo->tableSeq = pBasicParam->tableSeq;
1,860!
1652
    }
1653
  }
1654

1655
  return TSDB_CODE_SUCCESS;
39,569,554✔
1656
}
1657

1658
int32_t addDynamicExchangeSource(SOperatorInfo* pOperator) {
39,416,925✔
1659
  SExchangeInfo*               pExchangeInfo = pOperator->info;
39,416,925✔
1660
  int32_t                      code = TSDB_CODE_SUCCESS;
39,416,925✔
1661
  SExchangeOperatorBasicParam* pBasicParam = NULL;
39,416,925✔
1662
  SExchangeOperatorParam*      pParam = (SExchangeOperatorParam*)pOperator->pOperatorGetParam->value;
39,416,925✔
1663
  if (pParam->multiParams) {
39,416,925!
1664
    SExchangeOperatorBatchParam* pBatch = (SExchangeOperatorBatchParam*)pOperator->pOperatorGetParam->value;
189,263✔
1665
    int32_t                      iter = 0;
189,263✔
1666
    while (NULL != (pBasicParam = tSimpleHashIterate(pBatch->pBatchs, pBasicParam, &iter))) {
531,155✔
1667
      code = addSingleExchangeSource(pOperator, pBasicParam);
341,892✔
1668
      if (code) {
341,892!
1669
        return code;
×
1670
      }
1671
    }
1672
  } else {
1673
    pBasicParam = &pParam->basic;
39,227,662✔
1674
    code = addSingleExchangeSource(pOperator, pBasicParam);
39,227,662✔
1675
  }
1676

1677
  freeOperatorParam(pOperator->pOperatorGetParam, OP_GET_PARAM);
39,416,925✔
1678
  pOperator->pOperatorGetParam = NULL;
39,416,925✔
1679

1680
  return code;
39,416,925✔
1681
}
1682

1683
int32_t prepareLoadRemoteData(SOperatorInfo* pOperator) {
466,965,082✔
1684
  SExchangeInfo* pExchangeInfo = pOperator->info;
466,965,082✔
1685
  int32_t        code = TSDB_CODE_SUCCESS;
466,965,466✔
1686
  int32_t        lino = 0;
466,965,466✔
1687
  
1688
  if ((OPTR_IS_OPENED(pOperator) && !pExchangeInfo->dynamicOp) ||
466,965,466!
1689
      (pExchangeInfo->dynamicOp && NULL == pOperator->pOperatorGetParam)) {
120,744,295!
1690
    qDebug("skip prepare, opened:%d, dynamicOp:%d, getParam:%p", OPTR_IS_OPENED(pOperator), pExchangeInfo->dynamicOp, pOperator->pOperatorGetParam);
346,802,441!
1691
    return TSDB_CODE_SUCCESS;
346,802,221✔
1692
  }
1693

1694
  if (pExchangeInfo->dynamicOp) {
120,162,677!
1695
    code = addDynamicExchangeSource(pOperator);
39,416,925✔
1696
    QUERY_CHECK_CODE(code, lino, _end);
39,416,925!
1697
  }
1698

1699
  if (pOperator->status == OP_NOT_OPENED && (pExchangeInfo->dynamicOp && pExchangeInfo->seqLoadData) || IS_STREAM_MODE(pOperator->pTaskInfo)) {
120,162,089!
1700
    pExchangeInfo->current = 0;
47,829,166✔
1701
  }
1702

1703
  int64_t st = taosGetTimestampUs();
120,160,988✔
1704

1705
  if (!IS_STREAM_MODE(pOperator->pTaskInfo) && !pExchangeInfo->seqLoadData) {
120,160,988✔
1706
    code = prepareConcurrentlyLoad(pOperator);
70,035,568✔
1707
    QUERY_CHECK_CODE(code, lino, _end);
70,037,799!
1708
    pExchangeInfo->openedTs = taosGetTimestampUs();
70,037,521✔
1709
  }
1710

1711
  OPTR_SET_OPENED(pOperator);
120,165,923✔
1712
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
120,163,544✔
1713

1714
  qDebug("%s prepare load complete", pOperator->pTaskInfo->id.str);
120,163,265✔
1715

1716
_end:
6,510,259✔
1717
  if (code != TSDB_CODE_SUCCESS) {
120,163,544!
1718
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1719
    pOperator->pTaskInfo->code = code;
×
1720
    T_LONG_JMP(pOperator->pTaskInfo->env, code);
×
1721
  }
1722
  return TSDB_CODE_SUCCESS;
120,163,544✔
1723
}
1724

1725
int32_t handleLimitOffset(SOperatorInfo* pOperator, SLimitInfo* pLimitInfo, SSDataBlock* pBlock, bool holdDataInBuf) {
2,718,402✔
1726
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
2,718,402✔
1727

1728
  if (pLimitInfo->remainGroupOffset > 0) {
2,718,402!
1729
    if (pLimitInfo->currentGroupId == 0) {  // it is the first group
×
1730
      pLimitInfo->currentGroupId = pBlock->info.id.groupId;
×
1731
      blockDataCleanup(pBlock);
×
1732
      return PROJECT_RETRIEVE_CONTINUE;
×
1733
    } else if (pLimitInfo->currentGroupId != pBlock->info.id.groupId) {
×
1734
      // now it is the data from a new group
1735
      pLimitInfo->remainGroupOffset -= 1;
×
1736

1737
      // ignore data block in current group
1738
      if (pLimitInfo->remainGroupOffset > 0) {
×
1739
        blockDataCleanup(pBlock);
×
1740
        return PROJECT_RETRIEVE_CONTINUE;
×
1741
      }
1742
    }
1743

1744
    // set current group id of the project operator
1745
    pLimitInfo->currentGroupId = pBlock->info.id.groupId;
×
1746
  }
1747

1748
  // here check for a new group data, we need to handle the data of the previous group.
1749
  if (pLimitInfo->currentGroupId != 0 && pLimitInfo->currentGroupId != pBlock->info.id.groupId) {
2,718,402✔
1750
    pLimitInfo->numOfOutputGroups += 1;
110,722✔
1751
    if ((pLimitInfo->slimit.limit > 0) && (pLimitInfo->slimit.limit <= pLimitInfo->numOfOutputGroups)) {
110,722!
1752
      pOperator->status = OP_EXEC_DONE;
×
1753
      blockDataCleanup(pBlock);
×
1754

1755
      return PROJECT_RETRIEVE_DONE;
×
1756
    }
1757

1758
    // reset the value for a new group data
1759
    resetLimitInfoForNextGroup(pLimitInfo);
110,722✔
1760
    // existing rows that belongs to previous group.
1761
    if (pBlock->info.rows > 0) {
110,722!
1762
      return PROJECT_RETRIEVE_DONE;
110,722✔
1763
    }
1764
  }
1765

1766
  // here we reach the start position, according to the limit/offset requirements.
1767

1768
  // set current group id
1769
  pLimitInfo->currentGroupId = pBlock->info.id.groupId;
2,607,680✔
1770

1771
  bool limitReached = applyLimitOffset(pLimitInfo, pBlock, pTaskInfo);
2,607,680✔
1772
  if (pBlock->info.rows == 0) {
2,607,680✔
1773
    return PROJECT_RETRIEVE_CONTINUE;
1,282,913✔
1774
  } else {
1775
    if (limitReached && (pLimitInfo->slimit.limit > 0 && pLimitInfo->slimit.limit <= pLimitInfo->numOfOutputGroups)) {
1,324,767!
1776
      setOperatorCompleted(pOperator);
×
1777
      return PROJECT_RETRIEVE_DONE;
×
1778
    }
1779
  }
1780

1781
  // todo optimize performance
1782
  // If there are slimit/soffset value exists, multi-round result can not be packed into one group, since the
1783
  // they may not belong to the same group the limit/offset value is not valid in this case.
1784
  if ((!holdDataInBuf) || (pBlock->info.rows >= pOperator->resultInfo.threshold) || hasSlimitOffsetInfo(pLimitInfo)) {
1,324,767!
1785
    return PROJECT_RETRIEVE_DONE;
1,324,767✔
1786
  } else {  // not full enough, continue to accumulate the output data in the buffer.
1787
    return PROJECT_RETRIEVE_CONTINUE;
×
1788
  }
1789
}
1790

1791
static int32_t exchangeWait(SOperatorInfo* pOperator, SExchangeInfo* pExchangeInfo) {
169,557,534✔
1792
  SExecTaskInfo* pTask = pOperator->pTaskInfo;
169,557,534✔
1793
  int32_t        code = TSDB_CODE_SUCCESS;
169,558,115✔
1794
  if (pTask->pWorkerCb) {
169,558,115!
1795
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
169,558,115✔
1796
    if (code != TSDB_CODE_SUCCESS) {
169,558,115!
1797
      pTask->code = code;
×
1798
      return pTask->code;
×
1799
    }
1800
  }
1801

1802
  code = tsem_wait(&pExchangeInfo->ready);
169,558,115✔
1803
  if (code != TSDB_CODE_SUCCESS) {
169,557,881!
1804
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1805
    pTask->code = code;
×
1806
    return pTask->code;
×
1807
  }
1808

1809
  if (pTask->pWorkerCb) {
169,557,881!
1810
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
169,558,115✔
1811
    if (code != TSDB_CODE_SUCCESS) {
169,557,534!
1812
      pTask->code = code;
×
1813
      return pTask->code;
×
1814
    }
1815
  }
1816
  return TSDB_CODE_SUCCESS;
169,557,300✔
1817
}
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