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

taosdata / TDengine / #4811

16 Oct 2025 11:40AM UTC coverage: 58.693% (+0.2%) from 58.518%
#4811

push

travis-ci

web-flow
fix(tref): increase TSDB_REF_OBJECTS from 100 to 2000 for improved reference handling (#33281)

139835 of 303532 branches covered (46.07%)

Branch coverage included in aggregate %.

211576 of 295200 relevant lines covered (71.67%)

16841075.92 hits per line

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

58.9
/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,
37,650✔
75
                                           SExecTaskInfo* pTaskInfo) {
76
  int32_t code = 0;
37,650✔
77
  int32_t lino = 0;
75,300✔
78
  int64_t startTs = taosGetTimestampUs();  
37,651✔
79
  int32_t  totalSources = (int32_t)taosArrayGetSize(pExchangeInfo->pSourceDataInfo);
37,651✔
80
  int32_t completed = 0;
37,651✔
81
  code = getCompletedSources(pExchangeInfo->pSourceDataInfo, &completed);
37,651✔
82
  if (code != TSDB_CODE_SUCCESS) {
37,649!
83
    pTaskInfo->code = code;
×
84
    T_LONG_JMP(pTaskInfo->env, code);
×
85
  }
86
  if (completed == totalSources) {
37,649✔
87
    qDebug("%s no load since all sources completed, completed:%d, totalSources:%d", pTaskInfo->id.str, completed, totalSources);
9,468✔
88
    setAllSourcesCompleted(pOperator);
9,468✔
89
    return;
37,651✔
90
  }
91

92
  SSourceDataInfo* pDataInfo = NULL;
28,181✔
93

94
  while (1) {
15,889✔
95
    if (pExchangeInfo->current < 0) {
44,070✔
96
      qDebug("current %d and all sources complted, totalSources:%d", pExchangeInfo->current, totalSources);
376✔
97
      setAllSourcesCompleted(pOperator);
376✔
98
      return;
376✔
99
    }
100
    
101
    if (pExchangeInfo->current >= totalSources) {
43,694✔
102
      completed = 0;
17,499✔
103
      code = getCompletedSources(pExchangeInfo->pSourceDataInfo, &completed);
17,499✔
104
      if (code != TSDB_CODE_SUCCESS) {
17,497!
105
        pTaskInfo->code = code;
×
106
        T_LONG_JMP(pTaskInfo->env, code);
×
107
      }
108
      if (completed == totalSources) {
17,497✔
109
        qDebug("stop to load since all sources complted, completed:%d, totalSources:%d", completed, totalSources);
13,961✔
110
        setAllSourcesCompleted(pOperator);
13,961✔
111
        return;
13,961✔
112
      }
113
      
114
      pExchangeInfo->current = 0;
3,536✔
115
    }
116

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

119
    SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, pExchangeInfo->current);
29,731✔
120
    if (!pDataInfo) {
29,731!
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) {
29,732✔
127
      pExchangeInfo->current++;
23✔
128
      continue;
23✔
129
    }
130

131
    pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
29,709✔
132

133
    code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, pExchangeInfo->current);
29,709✔
134
    if (code != TSDB_CODE_SUCCESS) {
29,713!
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) {
×
141
      code = exchangeWait(pOperator, pExchangeInfo);
29,713✔
142
      if (code != TSDB_CODE_SUCCESS || isTaskKilled(pTaskInfo)) {
29,713!
143
        T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
144
      }
145

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

154
      break;
29,713✔
155
    }
156

157
    SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, pExchangeInfo->current);
29,713✔
158
    if (!pSource) {
29,710!
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) {
29,712!
165
      qError("%s vgId:%d, clientId:0x%" PRIx64 " taskID:0x%" PRIx64 " execId:%d error happens, code:%s",
×
166
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
167
             tstrerror(pDataInfo->code));
168
      pTaskInfo->code = pDataInfo->code;
×
169
      T_LONG_JMP(pTaskInfo->env, code);
×
170
    }
171

172
    SRetrieveTableRsp*   pRsp = pDataInfo->pRsp;
29,712✔
173
    SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
29,712✔
174

175
    if (pRsp->numOfRows == 0) {
29,712✔
176
      qDebug("exhausted %p,%s vgId:%d, clientId:0x%" PRIx64 " taskID:0x%" PRIx64
15,867✔
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;
15,867✔
182
      if (pDataInfo->isVtbRefScan || pDataInfo->isVtbTagScan) {
15,867!
183
        pExchangeInfo->current = -1;
376✔
184
      } else {
185
        pExchangeInfo->current += 1;
15,491✔
186
      }
187
      taosMemoryFreeClear(pDataInfo->pRsp);
15,867!
188
      continue;
15,867✔
189
    }
190

191
    code = doExtractResultBlocks(pExchangeInfo, pDataInfo);
13,845✔
192
    TAOS_CHECK_EXIT(code);
13,845!
193

194
    SRetrieveTableRsp* pRetrieveRsp = pDataInfo->pRsp;
13,845✔
195
    if (pRsp->completed == 1) {
13,845✔
196
      qDebug("exhausted %p,%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64 " execId:%d numOfRows:%" PRId64
9,964✔
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;
9,964✔
203
      if (pDataInfo->isVtbRefScan) {
9,964!
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
3,881✔
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);
13,845✔
216
    pDataInfo->totalRows += pRetrieveRsp->numOfRows;
13,845✔
217

218
    pExchangeInfo->current++;
13,845✔
219

220
    taosMemoryFreeClear(pDataInfo->pRsp);
13,845!
221
    return;
13,846✔
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,
6,148,181✔
234
                                           SExecTaskInfo* pTaskInfo) {
235
  int32_t code = 0;
6,148,181✔
236
  int32_t lino = 0;
6,148,181✔
237
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSourceDataInfo);
6,148,181✔
238
  int32_t completed = 0;
6,147,707✔
239
  code = getCompletedSources(pExchangeInfo->pSourceDataInfo, &completed);
6,147,707✔
240
  if (code != TSDB_CODE_SUCCESS) {
6,146,637!
241
    pTaskInfo->code = code;
×
242
    T_LONG_JMP(pTaskInfo->env, code);
×
243
  }
244
  if (completed == totalSources) {
6,146,637✔
245
    setAllSourcesCompleted(pOperator);
1,587,535✔
246
    return;
6,148,856✔
247
  }
248

249
  SSourceDataInfo* pDataInfo = NULL;
4,559,102✔
250

251
  while (1) {
195,757✔
252
    qDebug("prepare wait for ready, %p, %s", pExchangeInfo, GET_TASKID(pTaskInfo));
4,754,859✔
253
    code = exchangeWait(pOperator, pExchangeInfo);
4,754,859✔
254

255
    if (code != TSDB_CODE_SUCCESS || isTaskKilled(pTaskInfo)) {
4,757,583!
256
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
257
    }
258

259
    for (int32_t i = 0; i < totalSources; ++i) {
28,267,076✔
260
      pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, i);
28,239,844✔
261
      QUERY_CHECK_NULL(pDataInfo, code, lino, _exit, terrno);
28,217,761!
262
      if (pDataInfo->status == EX_SOURCE_DATA_EXHAUSTED) {
28,261,180✔
263
        continue;
22,655,247✔
264
      }
265

266
      if (pDataInfo->status != EX_SOURCE_DATA_READY) {
5,605,933✔
267
        continue;
854,270✔
268
      }
269

270
      int64_t currSeqId = atomic_load_64(&pExchangeInfo->seqId);
4,751,663✔
271
      if (pDataInfo->seqId != currSeqId) {
4,754,441!
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) {
4,754,441✔
278
        code = pDataInfo->code;
11✔
279
        TAOS_CHECK_EXIT(code);
11!
280
      }
281

282
      tmemory_barrier();
4,754,430✔
283
      SRetrieveTableRsp*     pRsp = pDataInfo->pRsp;
4,754,430✔
284
      SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, pDataInfo->index);
4,754,430✔
285
      QUERY_CHECK_NULL(pSource, code, lino, _exit, terrno);
4,754,884!
286

287
      // todo
288
      SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
4,755,446✔
289
      if (pRsp->numOfRows == 0) {
4,755,446✔
290
        if (NULL != pDataInfo->pSrcUidList && (!pDataInfo->isVtbRefScan)) {
1,701,552!
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;
1,701,552✔
299
          qDebug("exhausted %p,%s vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
1,701,552✔
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);
1,701,546!
304
        }
305
        break;
1,701,680✔
306
      }
307

308
      TAOS_CHECK_EXIT(doExtractResultBlocks(pExchangeInfo, pDataInfo));
3,053,894!
309

310
      SRetrieveTableRsp* pRetrieveRsp = pDataInfo->pRsp;
3,054,349✔
311
      updateLoadRemoteInfo(pLoadInfo, pRetrieveRsp->numOfRows, pRetrieveRsp->compLen, pDataInfo->startTime, pOperator);
3,054,349✔
312
      pDataInfo->totalRows += pRetrieveRsp->numOfRows;
3,054,914✔
313

314
      if (pRsp->completed == 1) {
3,054,914✔
315
        pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
2,756,213✔
316
        qDebug("exhausted %p,%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
2,756,213✔
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
298,701✔
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);
3,054,852!
330

331
      if ((pDataInfo->status != EX_SOURCE_DATA_EXHAUSTED || NULL != pDataInfo->pSrcUidList) && !pDataInfo->isVtbRefScan && !pDataInfo->isVtbTagScan) {
3,055,488!
332
        pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
298,654✔
333
        code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, i);
298,654✔
334
        if (code != TSDB_CODE_SUCCESS) {
298,654!
335
          taosMemoryFreeClear(pDataInfo->pRsp);
×
336
          TAOS_CHECK_EXIT(code);
×
337
        }
338
      }
339
      
340
      return;
4,561,244✔
341
    }  // end loop
342

343
    int32_t complete1 = 0;
1,728,912✔
344
    code = getCompletedSources(pExchangeInfo->pSourceDataInfo, &complete1);
1,728,912✔
345
    if (code != TSDB_CODE_SUCCESS) {
1,701,473!
346
      pTaskInfo->code = code;
×
347
      T_LONG_JMP(pTaskInfo->env, code);
×
348
    }
349
    if (complete1 == totalSources) {
1,701,511✔
350
      qDebug("all sources are completed, %s", GET_TASKID(pTaskInfo));
1,505,754✔
351
      return;
1,505,756✔
352
    }
353
  }
354

355
_exit:
11✔
356

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

363
static SSDataBlock* doLoadRemoteDataImpl(SOperatorInfo* pOperator) {
13,642,977✔
364
  int32_t        code = TSDB_CODE_SUCCESS;
13,642,977✔
365
  SExchangeInfo* pExchangeInfo = pOperator->info;
13,642,977✔
366
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
13,642,977✔
367

368
  size_t totalSources = taosArrayGetSize(pExchangeInfo->pSources);
13,642,977✔
369

370
  SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
13,642,522✔
371
  if (pOperator->status == OP_EXEC_DONE) {
13,642,522!
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;
13,642,522✔
380
  if (taosArrayGetSize(pExchangeInfo->pResultBlockList) > 0) {
13,642,522✔
381
    p = taosArrayGetP(pExchangeInfo->pResultBlockList, 0);
7,426,465✔
382
    taosArrayRemove(pExchangeInfo->pResultBlockList, 0);
7,426,099✔
383
  }
384

385
  if (p != NULL) {
13,642,552✔
386
    void* tmp = taosArrayPush(pExchangeInfo->pRecycledBlocks, &p);
7,426,205✔
387
    if (!tmp) {
7,426,110!
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;
7,426,110✔
394
  } else {
395
    if (pExchangeInfo->seqLoadData) {
6,216,347✔
396
      code = seqLoadRemoteData(pOperator);
30,558✔
397
      if (code != TSDB_CODE_SUCCESS) {
30,555!
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))   {
6,185,789✔
403
      streamConcurrentlyLoadRemoteData(pOperator, pExchangeInfo, pTaskInfo);
37,650✔
404
    } else {
405
      concurrentlyLoadRemoteDataImpl(pOperator, pExchangeInfo, pTaskInfo);
6,148,139✔
406
    }
407
    if (TSDB_CODE_SUCCESS != pOperator->pTaskInfo->code) {
6,216,448✔
408
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(pOperator->pTaskInfo->code));
11!
409
      T_LONG_JMP(pTaskInfo->env, pOperator->pTaskInfo->code);
11!
410
    }
411
    
412
    if (taosArrayGetSize(pExchangeInfo->pResultBlockList) == 0) {
6,216,437✔
413
      qDebug("empty resultBlockList");
3,124,073✔
414
      return NULL;
3,124,077✔
415
    } else {
416
      p = taosArrayGetP(pExchangeInfo->pResultBlockList, 0);
3,092,060✔
417
      taosArrayRemove(pExchangeInfo->pResultBlockList, 0);
3,090,204✔
418
      void* tmp = taosArrayPush(pExchangeInfo->pRecycledBlocks, &p);
3,090,067✔
419
      if (!tmp) {
3,090,909!
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);
3,090,909✔
427
      return p;
3,091,149✔
428
    }
429
}
430
}
431

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

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

440
  code = pOperator->fpSet._openFn(pOperator);
13,639,412✔
441
  QUERY_CHECK_CODE(code, lino, _end);
13,639,575!
442

443
  if (pOperator->status == OP_EXEC_DONE) {
13,639,575✔
444
    (*ppRes) = NULL;
110✔
445
    return code;
110✔
446
  }
447

448
  while (1) {
3,593✔
449
    SSDataBlock* pBlock = doLoadRemoteDataImpl(pOperator);
13,643,058✔
450
    if (pBlock == NULL) {
13,639,579✔
451
      (*ppRes) = NULL;
3,124,159✔
452
      return code;
3,124,159✔
453
    }
454

455
    code = doFilter(pBlock, pOperator->exprSupp.pFilterInfo, NULL, NULL);
10,515,420✔
456
    QUERY_CHECK_CODE(code, lino, _end);
10,517,142!
457

458
    if (blockDataGetNumOfRows(pBlock) == 0) {
10,517,142✔
459
      qDebug("rows 0 block got, continue next load");
2!
460
      continue;
2✔
461
    }
462

463
    SLimitInfo* pLimitInfo = &pExchangeInfo->limitInfo;
10,517,197✔
464
    if (hasLimitOffsetInfo(pLimitInfo)) {
10,517,197✔
465
      int32_t status = handleLimitOffset(pOperator, pLimitInfo, pBlock, false);
7,903✔
466
      if (status == PROJECT_RETRIEVE_CONTINUE) {
7,903✔
467
        qDebug("limit retrieve continue");
3,591✔
468
        continue;
3,591✔
469
      } else if (status == PROJECT_RETRIEVE_DONE) {
4,312!
470
        if (pBlock->info.rows == 0) {
4,312!
471
          setOperatorCompleted(pOperator);
×
472
          (*ppRes) = NULL;
×
473
          return code;
×
474
        } else {
475
          (*ppRes) = pBlock;
4,312✔
476
          return code;
4,312✔
477
        }
478
      }
479
    } else {
480
      (*ppRes) = pBlock;
10,509,552✔
481
      qDebug("block with rows %" PRId64 " returned in exechange", pBlock->info.rows);
10,509,552✔
482
      return code;
10,509,525✔
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) {
3,117,175✔
501
  pInfo->pSourceDataInfo = taosArrayInit(numOfSources, sizeof(SSourceDataInfo));
3,117,175✔
502
  if (pInfo->pSourceDataInfo == NULL) {
3,117,036✔
503
    return terrno;
6✔
504
  }
505

506
  if (pInfo->dynamicOp) {
3,117,030✔
507
    return TSDB_CODE_SUCCESS;
6,532✔
508
  }
509

510
  int32_t len = strlen(id) + 1;
3,110,498✔
511
  pInfo->pTaskId = taosMemoryCalloc(1, len);
3,110,498!
512
  if (!pInfo->pTaskId) {
3,110,721!
513
    return terrno;
×
514
  }
515
  tstrncpy(pInfo->pTaskId, id, len);
3,110,721✔
516
  for (int32_t i = 0; i < numOfSources; ++i) {
7,573,019✔
517
    SSourceDataInfo dataInfo = {0};
4,462,548✔
518
    dataInfo.status = EX_SOURCE_DATA_NOT_READY;
4,462,548✔
519
    dataInfo.taskId = pInfo->pTaskId;
4,462,548✔
520
    dataInfo.index = i;
4,462,548✔
521
    SSourceDataInfo* pDs = taosArrayPush(pInfo->pSourceDataInfo, &dataInfo);
4,462,548✔
522
    if (pDs == NULL) {
4,462,324!
523
      taosArrayDestroyEx(pInfo->pSourceDataInfo, freeSourceDataInfo);
×
524
      return terrno;
×
525
    }
526
    qDebug("init source data info %d, pDs:%p, status:%d", i, pDs, pDs->status);
4,462,324✔
527
  }
528

529
  return TSDB_CODE_SUCCESS;
3,110,471✔
530
}
531

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

535
  if (numOfSources == 0) {
3,117,129!
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));
3,117,129✔
540
  if (!pInfo->pFetchRpcHandles) {
3,117,234!
541
    return terrno;
×
542
  }
543
  void* ret = taosArrayReserve(pInfo->pFetchRpcHandles, numOfSources);
3,117,234✔
544
  if (!ret) {
3,117,052!
545
    return terrno;
×
546
  }
547

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

554
  if (pExNode->node.dynamicOp) {
3,117,009✔
555
    pInfo->pHashSources = tSimpleHashInit(numOfSources * 2, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
6,532✔
556
    if (NULL == pInfo->pHashSources) {
6,547!
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) {
7,593,901✔
563
    SDownstreamSourceNode* pNode = (SDownstreamSourceNode*)nodesListGetNode((SNodeList*)pExNode->pSrcEndPoints, i);
4,476,882✔
564
    if (!pNode) {
4,477,225!
565
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
566
      return terrno;
×
567
    }
568
    void* tmp = taosArrayPush(pInfo->pSources, pNode);
4,477,225✔
569
    if (!tmp) {
4,476,605!
570
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
571
      return terrno;
×
572
    }
573
    SExchangeSrcIndex idx = {.srcIdx = i, .inUseIdx = -1};
4,476,605✔
574
    int32_t           code =
575
        tSimpleHashPut(pInfo->pHashSources, &pNode->addr.nodeId, sizeof(pNode->addr.nodeId), &idx, sizeof(idx));
4,476,605✔
576
    if (pInfo->pHashSources && code != TSDB_CODE_SUCCESS) {
4,476,877!
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);
3,117,019✔
583
  int64_t refId = taosAddRef(exchangeObjRefPool, pInfo);
3,117,122✔
584
  if (refId < 0) {
3,117,224!
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;
3,117,224✔
590
  }
591

592
  return initDataSource(numOfSources, pInfo, id);
3,117,224✔
593
}
594

595
int32_t resetExchangeOperState(SOperatorInfo* pOper) {
59,603✔
596
  SExchangeInfo* pInfo = pOper->info;
59,603✔
597
  SExchangePhysiNode* pPhynode = (SExchangePhysiNode*)pOper->pPhyNode;
59,603✔
598

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

601
  (void)atomic_add_fetch_64(&pInfo->seqId, 1);
59,608✔
602
  pOper->status = OP_NOT_OPENED;
59,607✔
603
  pInfo->current = 0;
59,607✔
604
  pInfo->loadInfo.totalElapsed = 0;
59,607✔
605
  pInfo->loadInfo.totalRows = 0;
59,607✔
606
  pInfo->loadInfo.totalSize = 0;
59,607✔
607
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pSourceDataInfo); ++i) {
193,549✔
608
    SSourceDataInfo* pDataInfo = taosArrayGet(pInfo->pSourceDataInfo, i);
133,941✔
609
    taosWLockLatch(&pDataInfo->lock);
133,941✔
610
    taosMemoryFreeClear(pDataInfo->decompBuf);
133,942!
611
    taosMemoryFreeClear(pDataInfo->pRsp);
133,942!
612

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

620
  if (pInfo->dynamicOp) {
59,605✔
621
    taosArrayClearEx(pInfo->pSourceDataInfo, freeSourceDataInfo);
17,892✔
622
  } 
623

624
  taosArrayClearEx(pInfo->pResultBlockList, freeBlock);
59,605✔
625
  taosArrayClearEx(pInfo->pRecycledBlocks, freeBlock);
59,606✔
626

627
  blockDataCleanup(pInfo->pDummyBlock);
59,607✔
628

629
  void   *data = NULL;
59,606✔
630
  int32_t iter = 0;
59,606✔
631
  while ((data = tSimpleHashIterate(pInfo->pHashSources, data, &iter))) {
91,573✔
632
    ((SExchangeSrcIndex *)data)->inUseIdx = -1;
31,967✔
633
  }
634
  
635
  pInfo->limitInfo = (SLimitInfo){0};
59,605✔
636
  initLimitInfo(pPhynode->node.pLimit, pPhynode->node.pSlimit, &pInfo->limitInfo);
59,605✔
637

638
  return 0;
59,605✔
639
}
640

641
int32_t createExchangeOperatorInfo(void* pTransporter, SExchangePhysiNode* pExNode, SExecTaskInfo* pTaskInfo,
3,117,179✔
642
                                   SOperatorInfo** pOptrInfo) {
643
  QRY_PARAM_CHECK(pOptrInfo);
3,117,179!
644

645
  int32_t        code = 0;
3,117,179✔
646
  int32_t        lino = 0;
3,117,179✔
647
  SExchangeInfo* pInfo = taosMemoryCalloc(1, sizeof(SExchangeInfo));
3,117,179!
648
  SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
3,117,134!
649
  if (pInfo == NULL || pOperator == NULL) {
3,117,115!
650
    code = terrno;
×
651
    goto _error;
×
652
  }
653

654
  pOperator->pPhyNode = pExNode;
3,117,125✔
655
  pInfo->dynamicOp = pExNode->node.dynamicOp;
3,117,125✔
656
  code = initExchangeOperator(pExNode, pInfo, GET_TASKID(pTaskInfo));
3,117,125✔
657
  QUERY_CHECK_CODE(code, lino, _error);
3,116,982!
658

659
  code = tsem_init(&pInfo->ready, 0, 0);
3,116,982✔
660
  QUERY_CHECK_CODE(code, lino, _error);
3,116,981!
661

662
  pInfo->pDummyBlock = createDataBlockFromDescNode(pExNode->node.pOutputDataBlockDesc);
3,116,981✔
663
  QUERY_CHECK_NULL(pInfo->pDummyBlock, code, lino, _error, terrno);
3,117,058!
664

665
  pInfo->pResultBlockList = taosArrayInit(64, POINTER_BYTES);
3,117,058✔
666
  QUERY_CHECK_NULL(pInfo->pResultBlockList, code, lino, _error, terrno);
3,117,084!
667
  pInfo->pRecycledBlocks = taosArrayInit(64, POINTER_BYTES);
3,117,084✔
668
  QUERY_CHECK_NULL(pInfo->pRecycledBlocks, code, lino, _error, terrno);
3,117,094!
669

670
  SExchangeOpStopInfo stopInfo = {QUERY_NODE_PHYSICAL_PLAN_EXCHANGE, pInfo->self};
3,117,094✔
671
  code = qAppendTaskStopInfo(pTaskInfo, &stopInfo);
3,117,094✔
672
  QUERY_CHECK_CODE(code, lino, _error);
3,117,236!
673

674
  pInfo->seqLoadData = pExNode->seqRecvData;
3,117,236✔
675
  pInfo->dynTbname = pExNode->dynTbname;
3,117,236✔
676
  if (pInfo->dynTbname) {
3,117,236✔
677
    pInfo->seqLoadData = true;
36✔
678
  }
679
  pInfo->pTransporter = pTransporter;
3,117,236✔
680

681
  setOperatorInfo(pOperator, "ExchangeOperator", QUERY_NODE_PHYSICAL_PLAN_EXCHANGE, false, OP_NOT_OPENED, pInfo,
3,117,236✔
682
                  pTaskInfo);
683
  pOperator->exprSupp.numOfExprs = taosArrayGetSize(pInfo->pDummyBlock->pDataBlock);
3,117,192✔
684

685
  code = filterInitFromNode((SNode*)pExNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
3,117,137✔
686
                            pTaskInfo->pStreamRuntimeInfo);
3,117,137✔
687
  QUERY_CHECK_CODE(code, lino, _error);
3,117,116!
688
  qTrace("%s exchange op:%p", __func__, pOperator);
3,117,116!
689
  pOperator->fpSet = createOperatorFpSet(prepareLoadRemoteData, loadRemoteDataNext, NULL, destroyExchangeOperatorInfo,
3,117,116✔
690
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
691
  setOperatorResetStateFn(pOperator, resetExchangeOperState);
3,117,189✔
692
  *pOptrInfo = pOperator;
3,117,164✔
693
  return TSDB_CODE_SUCCESS;
3,117,164✔
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) {
3,117,198✔
712
  SExchangeInfo* pExInfo = (SExchangeInfo*)param;
3,117,198✔
713
  int32_t        code = taosRemoveRef(exchangeObjRefPool, pExInfo->self);
3,117,198✔
714
  if (code != TSDB_CODE_SUCCESS) {
3,117,269!
715
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
716
  }
717
}
3,117,269✔
718

719
void freeBlock(void* pParam) {
4,675,170✔
720
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
4,675,170✔
721
  blockDataDestroy(pBlock);
4,675,170✔
722
}
4,675,224✔
723

724
void freeSourceDataInfo(void* p) {
4,465,555✔
725
  SSourceDataInfo* pInfo = (SSourceDataInfo*)p;
4,465,555✔
726
  taosMemoryFreeClear(pInfo->decompBuf);
4,465,555!
727
  taosMemoryFreeClear(pInfo->pRsp);
4,465,555!
728

729
  pInfo->decompBufSize = 0;
4,465,555✔
730
}
4,465,555✔
731

732
void doDestroyExchangeOperatorInfo(void* param) {
3,117,244✔
733
  if (param == NULL) {
3,117,244!
734
    return;
×
735
  }
736
  SExchangeInfo* pExInfo = (SExchangeInfo*)param;
3,117,244✔
737
  if (pExInfo->pFetchRpcHandles) {
3,117,244!
738
    for (int32_t i = 0; i < pExInfo->pFetchRpcHandles->size; ++i) {
7,593,500✔
739
      int64_t* pRpcHandle = taosArrayGet(pExInfo->pFetchRpcHandles, i);
4,476,708✔
740
      if (*pRpcHandle > 0) {
4,476,245✔
741
        SDownstreamSourceNode* pSource = taosArrayGet(pExInfo->pSources, i);
6,834✔
742
        (void)asyncFreeConnById(pExInfo->pTransporter, *pRpcHandle);
6,834✔
743
      }
744
    }
745
    taosArrayDestroy(pExInfo->pFetchRpcHandles);
3,116,792✔
746
  }
747

748
  taosArrayDestroy(pExInfo->pSources);
3,117,127✔
749
  taosArrayDestroyEx(pExInfo->pSourceDataInfo, freeSourceDataInfo);
3,117,193✔
750

751
  taosArrayDestroyEx(pExInfo->pResultBlockList, freeBlock);
3,117,147✔
752
  taosArrayDestroyEx(pExInfo->pRecycledBlocks, freeBlock);
3,117,187✔
753

754
  blockDataDestroy(pExInfo->pDummyBlock);
3,117,219✔
755
  tSimpleHashCleanup(pExInfo->pHashSources);
3,117,237✔
756

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

763
  taosMemoryFreeClear(param);
3,117,212!
764
}
765

766
int32_t loadRemoteDataCallback(void* param, SDataBuf* pMsg, int32_t code) {
4,815,904✔
767
  SFetchRspHandleWrapper* pWrapper = (SFetchRspHandleWrapper*)param;
4,815,904✔
768

769
  taosMemoryFreeClear(pMsg->pEpSet);
4,815,904!
770
  SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pWrapper->exchangeId);
4,815,904✔
771
  if (pExchangeInfo == NULL) {
4,817,065✔
772
    qWarn("failed to acquire exchange operator, since it may have been released, %p", pExchangeInfo);
95!
773
    taosMemoryFree(pMsg->pData);
95!
774
    return TSDB_CODE_SUCCESS;
95✔
775
  }
776

777
  int64_t currSeqId = atomic_load_64(&pExchangeInfo->seqId);
4,816,970✔
778
  if (pWrapper->seqId != currSeqId) {
4,814,103!
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;
4,814,103✔
789

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

792
  int64_t* pRpcHandle = taosArrayGet(pExchangeInfo->pFetchRpcHandles, index);
4,814,104✔
793
  if (pRpcHandle != NULL) {
4,792,581✔
794
    int32_t ret = asyncFreeConnById(pExchangeInfo->pTransporter, *pRpcHandle);
4,792,411✔
795
    if (ret != 0) {
4,816,545✔
796
      qDebug("failed to free rpc handle, code:%s, %p", tstrerror(ret), pExchangeInfo);
4,671!
797
    }
798
    *pRpcHandle = -1;
4,816,255✔
799
  }
800

801
  SSourceDataInfo* pSourceDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, index);
4,816,425✔
802
  if (!pSourceDataInfo) {
4,794,743!
803
    return terrno;
×
804
  }
805

806
  if (0 == code && NULL == pMsg->pData) {
4,794,743!
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);
4,794,743✔
812
  if (code == TSDB_CODE_SUCCESS) {
4,814,986!
813
    pSourceDataInfo->seqId = pWrapper->seqId;
4,815,085✔
814
    pSourceDataInfo->pRsp = pMsg->pData;
4,815,085✔
815

816
    SRetrieveTableRsp* pRsp = pSourceDataInfo->pRsp;
4,815,085✔
817
    pRsp->numOfRows = htobe64(pRsp->numOfRows);
4,815,085✔
818
    pRsp->compLen = htonl(pRsp->compLen);
4,810,910✔
819
    pRsp->payloadLen = htonl(pRsp->payloadLen);
4,810,910✔
820
    pRsp->numOfCols = htonl(pRsp->numOfCols);
4,810,910✔
821
    pRsp->useconds = htobe64(pRsp->useconds);
4,810,910✔
822
    pRsp->numOfBlocks = htonl(pRsp->numOfBlocks);
4,813,988✔
823

824
    qDebug("%s fetch rsp received, index:%d, blocks:%d, rows:%" PRId64 ", %p", pSourceDataInfo->taskId, index,
4,813,988✔
825
           pRsp->numOfBlocks, pRsp->numOfRows, pExchangeInfo);
826
  } else {
827
    taosMemoryFree(pMsg->pData);
×
828
    pSourceDataInfo->code = rpcCvtErrCode(code);
22✔
829
    if (pSourceDataInfo->code != code) {
22!
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),
22!
834
             pExchangeInfo);
835
    }
836
  }
837

838
  tmemory_barrier();
4,814,019✔
839
  pSourceDataInfo->status = EX_SOURCE_DATA_READY;
4,814,019✔
840
  taosWUnLockLatch(&pSourceDataInfo->lock);
4,814,019✔
841
  
842
  code = tsem_post(&pExchangeInfo->ready);
4,815,468✔
843
  if (code != TSDB_CODE_SUCCESS) {
4,816,541!
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);
4,816,541✔
849
  if (code != TSDB_CODE_SUCCESS) {
4,817,466!
850
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
851
  }
852
  return code;
4,817,557✔
853
}
854

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

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

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

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

884
  return TSDB_CODE_SUCCESS;
653✔
885
}
886

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

892
  *ppRes = taosMemoryMalloc(sizeof(SOperatorParam));
27,928!
893
  QUERY_CHECK_NULL(*ppRes, code, lino, _return, terrno);
27,928!
894

895
  pScan = taosMemoryMalloc(sizeof(STableScanOperatorParam));
27,928!
896
  QUERY_CHECK_NULL(pScan, code, lino, _return, terrno);
27,928!
897

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

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

904
  pScan->pOrgTbInfo->vgId = pMap->vgId;
27,928✔
905
  tstrncpy(pScan->pOrgTbInfo->tbName, pMap->tbName, TSDB_TABLE_FNAME_LEN);
27,928✔
906

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

910
  pScan->tableSeq = tableSeq;
27,928✔
911
  pScan->window.skey = window->skey;
27,928✔
912
  pScan->window.ekey = window->ekey;
27,928✔
913

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

920
  return code;
27,928✔
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) {
2,636✔
936
  int32_t                  code = TSDB_CODE_SUCCESS;
2,636✔
937
  int32_t                  lino = 0;
2,636✔
938
  STagScanOperatorParam*   pScan = NULL;
2,636✔
939

940
  *ppRes = taosMemoryMalloc(sizeof(SOperatorParam));
2,636!
941
  QUERY_CHECK_NULL(*ppRes, code, lino, _return, terrno);
2,636!
942

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

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

953
  return code;
2,636✔
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) {
11,761✔
964
  if (!pRuntimeInfo || !pTimeRange) {
11,761!
965
    return TSDB_CODE_INTERNAL_ERROR;
×
966
  }
967

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

973
  switch (pRuntimeInfo->triggerType) {
11,760✔
974
    case STREAM_TRIGGER_SLIDING:
9,841✔
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
9,841✔
978
      pTimeRange->ekey = pParam->wend;    // is equal to wend
9,841✔
979
      break;
9,841✔
980
    case STREAM_TRIGGER_PERIOD:
129✔
981
      pTimeRange->skey = pParam->prevLocalTime;
129✔
982
      pTimeRange->ekey = pParam->triggerTime;
129✔
983
      break;
129✔
984
    default:
1,790✔
985
      pTimeRange->skey = pParam->wstart;
1,790✔
986
      pTimeRange->ekey = pParam->wend;
1,790✔
987
      break;
1,790✔
988
  }
989

990
  return TSDB_CODE_SUCCESS;
11,760✔
991
}
992

993
int32_t doSendFetchDataRequest(SExchangeInfo* pExchangeInfo, SExecTaskInfo* pTaskInfo, int32_t sourceIndex) {
4,819,126✔
994
  int32_t          code = TSDB_CODE_SUCCESS;
4,819,126✔
995
  int32_t          lino = 0;
4,819,126✔
996
  SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, sourceIndex);
4,819,126✔
997
  if (!pDataInfo) {
4,816,980!
998
    return terrno;
×
999
  }
1000

1001
  if (EX_SOURCE_DATA_NOT_READY != pDataInfo->status) {
4,817,032!
1002
    return TSDB_CODE_SUCCESS;
×
1003
  }
1004

1005
  pDataInfo->status = EX_SOURCE_DATA_STARTED;
4,817,032✔
1006
  SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, pDataInfo->index);
4,817,032✔
1007
  if (!pSource) {
4,816,224!
1008
    return terrno;
×
1009
  }
1010

1011
  pDataInfo->startTime = taosGetTimestampUs();
4,819,317✔
1012
  size_t totalSources = taosArrayGetSize(pExchangeInfo->pSources);
4,819,317✔
1013

1014
  SFetchRspHandleWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SFetchRspHandleWrapper));
4,820,092!
1015
  QUERY_CHECK_NULL(pWrapper, code, lino, _end, terrno);
4,821,263!
1016
  pWrapper->exchangeId = pExchangeInfo->self;
4,821,263✔
1017
  pWrapper->sourceIndex = sourceIndex;
4,821,263✔
1018
  pWrapper->seqId = pExchangeInfo->seqId;
4,821,263✔
1019

1020
  if (pSource->localExec) {
4,821,263!
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;
4,821,263✔
1030
    SResFetchReq req = {0};
4,821,263✔
1031
    req.header.vgId = pSource->addr.nodeId;
4,821,263✔
1032
    req.sId = pSource->sId;
4,821,263✔
1033
    req.clientId = pSource->clientId;
4,821,263✔
1034
    req.taskId = pSource->taskId;
4,821,263✔
1035
    req.queryId = pTaskInfo->id.queryId;
4,821,263✔
1036
    req.execId = pSource->execId;
4,821,263✔
1037
    if (pTaskInfo->pStreamRuntimeInfo) {
4,821,263✔
1038
      req.dynTbname = pExchangeInfo->dynTbname;
29,954✔
1039
      req.execId = pTaskInfo->pStreamRuntimeInfo->execId;
29,954✔
1040
      req.pStRtFuncInfo = &pTaskInfo->pStreamRuntimeInfo->funcInfo;
29,954✔
1041

1042
      if (pSource->fetchMsgType == TDMT_STREAM_FETCH_FROM_RUNNER) {
29,954✔
1043
        qDebug("%s stream fetch from runner, execId:%d, %p", GET_TASKID(pTaskInfo), req.execId, pTaskInfo->pStreamRuntimeInfo);
80!
1044
      } else if (pSource->fetchMsgType == TDMT_STREAM_FETCH_FROM_CACHE) {
29,874✔
1045
        code = getCurrentWinCalcTimeRange(req.pStRtFuncInfo, &req.pStRtFuncInfo->curWindow);
11,761✔
1046
        QUERY_CHECK_CODE(code, lino, _end);
11,760!
1047
        needStreamPesudoFuncVals = false;
11,760✔
1048
        qDebug("%s stream fetch from cache, execId:%d, curWinIdx:%d, time range:[%" PRId64 ", %" PRId64 "]",
11,760✔
1049
               GET_TASKID(pTaskInfo), req.execId, req.pStRtFuncInfo->curIdx, req.pStRtFuncInfo->curWindow.skey,
1050
               req.pStRtFuncInfo->curWindow.ekey);
1051
      }
1052
      if (!pDataInfo->fetchSent) {
30,052✔
1053
        req.reset = pDataInfo->fetchSent = true;
26,416✔
1054
      }
1055
    }
1056
    if (pDataInfo->isVtbRefScan) {
4,821,361✔
1057
      code = buildTableScanOperatorParamEx(&req.pOpParam, pDataInfo->pSrcUidList, pDataInfo->srcOpType, pDataInfo->colMap, pDataInfo->tableSeq, &pDataInfo->window);
27,928✔
1058
      taosArrayDestroy(pDataInfo->colMap->colMap);
27,928✔
1059
      taosMemoryFreeClear(pDataInfo->colMap);
27,928!
1060
      taosArrayDestroy(pDataInfo->pSrcUidList);
27,928✔
1061
      pDataInfo->pSrcUidList = NULL;
27,928✔
1062
      if (TSDB_CODE_SUCCESS != code) {
27,928!
1063
        pTaskInfo->code = code;
×
1064
        taosMemoryFree(pWrapper);
×
1065
        return pTaskInfo->code;
×
1066
      }
1067
    } else if (pDataInfo->isVtbTagScan) {
4,793,433✔
1068
      code = buildTagScanOperatorParam(&req.pOpParam, pDataInfo->pSrcUidList, pDataInfo->srcOpType);
2,636✔
1069
      taosArrayDestroy(pDataInfo->pSrcUidList);
2,636✔
1070
      pDataInfo->pSrcUidList = NULL;
2,636✔
1071
      if (TSDB_CODE_SUCCESS != code) {
2,636!
1072
        pTaskInfo->code = code;
×
1073
        taosMemoryFree(pWrapper);
×
1074
        return pTaskInfo->code;
×
1075
      }
1076
    } else {
1077
      if (pDataInfo->pSrcUidList) {
4,790,797✔
1078
        code = buildTableScanOperatorParam(&req.pOpParam, pDataInfo->pSrcUidList, pDataInfo->srcOpType, pDataInfo->tableSeq);
622✔
1079
        taosArrayDestroy(pDataInfo->pSrcUidList);
622✔
1080
        pDataInfo->pSrcUidList = NULL;
622✔
1081
        if (TSDB_CODE_SUCCESS != code) {
622!
1082
          pTaskInfo->code = code;
×
1083
          taosMemoryFree(pWrapper);
×
1084
          return pTaskInfo->code;
×
1085
        }
1086
      }
1087
    }
1088

1089
    int32_t msgSize = tSerializeSResFetchReq(NULL, 0, &req, needStreamPesudoFuncVals);
4,821,361✔
1090
    if (msgSize < 0) {
4,820,898!
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);
4,820,898!
1098
    if (NULL == msg) {
4,821,144!
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);
4,821,144✔
1106
    if (msgSize < 0) {
4,820,832!
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);
4,820,832✔
1115

1116
    qDebug("%s build fetch msg and send to vgId:%d, ep:%s, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
4,820,838✔
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));
4,820,840✔
1123
    if (NULL == pMsgSendInfo) {
4,820,935!
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;
4,820,935✔
1132
    pMsgSendInfo->paramFreeFp = taosAutoMemoryFree;
4,820,935✔
1133
    pMsgSendInfo->msgInfo.pData = msg;
4,820,935✔
1134
    pMsgSendInfo->msgInfo.len = msgSize;
4,820,935✔
1135
    pMsgSendInfo->msgType = pSource->fetchMsgType;
4,820,935✔
1136
    pMsgSendInfo->fp = loadRemoteDataCallback;
4,820,935✔
1137
    pMsgSendInfo->requestId = pTaskInfo->id.queryId;
4,820,935✔
1138

1139
    int64_t transporterId = 0;
4,820,935✔
1140
    void* poolHandle = NULL;
4,820,935✔
1141
    code = asyncSendMsgToServer(pExchangeInfo->pTransporter, &pSource->addr.epSet, &transporterId, pMsgSendInfo);
4,820,935✔
1142
    QUERY_CHECK_CODE(code, lino, _end);
4,821,263!
1143
    int64_t* pRpcHandle = taosArrayGet(pExchangeInfo->pFetchRpcHandles, sourceIndex);
4,821,263✔
1144
    *pRpcHandle = transporterId;
4,818,763✔
1145
  }
1146

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

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

1162
int32_t extractDataBlockFromFetchRsp(SSDataBlock* pRes, char* pData, SArray* pColList, char** pNextStart) {
10,692,969✔
1163
  int32_t      code = TSDB_CODE_SUCCESS;
10,692,969✔
1164
  int32_t      lino = 0;
10,692,969✔
1165
  SSDataBlock* pBlock = NULL;
10,692,969✔
1166
  if (pColList == NULL) {  // data from other sources
10,692,969✔
1167
    blockDataCleanup(pRes);
10,537,028✔
1168
    code = blockDecode(pRes, pData, (const char**)pNextStart);
10,535,960✔
1169
    if (code) {
10,534,100!
1170
      return code;
×
1171
    }
1172
  } else {  // extract data according to pColList
1173
    char* pStart = pData;
155,941✔
1174

1175
    int32_t numOfCols = htonl(*(int32_t*)pStart);
155,941✔
1176
    pStart += sizeof(int32_t);
155,941✔
1177

1178
    // todo refactor:extract method
1179
    SSysTableSchema* pSchema = (SSysTableSchema*)pStart;
155,941✔
1180
    for (int32_t i = 0; i < numOfCols; ++i) {
2,596,359✔
1181
      SSysTableSchema* p = (SSysTableSchema*)pStart;
2,440,418✔
1182

1183
      p->colId = htons(p->colId);
2,440,418✔
1184
      p->bytes = htonl(p->bytes);
2,440,418✔
1185
      pStart += sizeof(SSysTableSchema);
2,440,418✔
1186
    }
1187

1188
    pBlock = NULL;
155,941✔
1189
    code = createDataBlock(&pBlock);
155,941✔
1190
    QUERY_CHECK_CODE(code, lino, _end);
156,441!
1191

1192
    for (int32_t i = 0; i < numOfCols; ++i) {
2,590,086✔
1193
      SColumnInfoData idata = createColumnInfoData(pSchema[i].type, pSchema[i].bytes, pSchema[i].colId);
2,433,821✔
1194
      code = blockDataAppendColInfo(pBlock, &idata);
2,440,955✔
1195
      QUERY_CHECK_CODE(code, lino, _end);
2,433,645!
1196
    }
1197

1198
    code = blockDecode(pBlock, pStart, NULL);
156,265✔
1199
    QUERY_CHECK_CODE(code, lino, _end);
156,360!
1200

1201
    code = blockDataEnsureCapacity(pRes, pBlock->info.rows);
156,360✔
1202
    QUERY_CHECK_CODE(code, lino, _end);
156,408!
1203

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

1211
    blockDataDestroy(pBlock);
156,419✔
1212
    pBlock = NULL;
156,465✔
1213
  }
1214

1215
_end:
10,690,565✔
1216
  if (code != TSDB_CODE_SUCCESS) {
10,690,565!
1217
    blockDataDestroy(pBlock);
×
1218
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1219
  }
1220
  return code;
10,690,974✔
1221
}
1222

1223
void setAllSourcesCompleted(SOperatorInfo* pOperator) {
1,618,416✔
1224
  SExchangeInfo* pExchangeInfo = pOperator->info;
1,618,416✔
1225
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
1,618,416✔
1226

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

1233
  setOperatorCompleted(pOperator);
1,618,440✔
1234
}
1,618,492✔
1235

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

1241
  int32_t completed = 0;
7,903,769✔
1242
  for (int32_t k = 0; k < total; ++k) {
63,414,308✔
1243
    SSourceDataInfo* p = taosArrayGet(pArray, k);
55,531,493✔
1244
    QUERY_CHECK_NULL(p, code, lino, _end, terrno);
55,500,543!
1245
    if (p->status == EX_SOURCE_DATA_EXHAUSTED) {
55,509,770✔
1246
      qDebug("source %d is completed, info:%p %p", k, pArray, p);
27,854,724✔
1247
      completed += 1;
27,855,493✔
1248
    }
1249
  }
1250

1251
  *pRes = completed;
7,882,815✔
1252
_end:
7,882,815✔
1253
  if (code != TSDB_CODE_SUCCESS) {
7,882,815!
1254
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1255
  }
1256
  return code;
7,901,151✔
1257
}
1258

1259
int32_t prepareConcurrentlyLoad(SOperatorInfo* pOperator) {
3,109,323✔
1260
  SExchangeInfo* pExchangeInfo = pOperator->info;
3,109,323✔
1261
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,109,323✔
1262

1263
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSourceDataInfo);
3,109,323✔
1264
  int64_t startTs = taosGetTimestampUs();
3,109,288✔
1265

1266
  // Asynchronously send all fetch requests to all sources.
1267
  for (int32_t i = 0; i < totalSources; ++i) {
7,569,216✔
1268
    int32_t code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, i);
4,460,278✔
1269
    if (code != TSDB_CODE_SUCCESS) {
4,459,928!
1270
      pTaskInfo->code = code;
×
1271
      return code;
×
1272
    }
1273
  }
1274

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

1279
  pOperator->status = OP_RES_TO_RETURN;
3,109,185✔
1280
  pOperator->cost.openCost = taosGetTimestampUs() - startTs;
3,109,249✔
1281
  if (isTaskKilled(pTaskInfo)) {
3,109,249✔
1282
    T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
21!
1283
  }
1284

1285
  return TSDB_CODE_SUCCESS;
3,109,327✔
1286
}
1287

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

1294
  char* pNextStart = pRetrieveRsp->data;
3,091,272✔
1295
  char* pStart = pNextStart;
3,091,272✔
1296

1297
  int32_t index = 0;
3,091,272✔
1298

1299
  if (pRetrieveRsp->compressed) {  // decompress the data
3,091,272!
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) {
13,626,824✔
1317
    pStart = pNextStart;
10,534,940✔
1318

1319
    if (taosArrayGetSize(pExchangeInfo->pRecycledBlocks) > 0) {
10,534,940✔
1320
      pb = *(SSDataBlock**)taosArrayPop(pExchangeInfo->pRecycledBlocks);
5,861,504✔
1321
      blockDataCleanup(pb);
5,861,443✔
1322
    } else {
1323
      code = createOneDataBlock(pExchangeInfo->pDummyBlock, false, &pb);
4,674,351✔
1324
      QUERY_CHECK_NULL(pb, code, lino, _end, code);
4,674,971!
1325
    }
1326

1327
    int32_t compLen = *(int32_t*)pStart;
10,535,800✔
1328
    pStart += sizeof(int32_t);
10,535,800✔
1329

1330
    int32_t rawLen = *(int32_t*)pStart;
10,535,800✔
1331
    pStart += sizeof(int32_t);
10,535,800✔
1332
    QUERY_CHECK_CONDITION((compLen <= rawLen && compLen != 0), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
10,535,800!
1333

1334
    pNextStart = pStart + compLen;
10,535,800✔
1335
    if (pRetrieveRsp->compressed && (compLen < rawLen)) {
10,535,800!
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);
10,535,800✔
1342
    if (code != 0) {
10,535,133!
1343
      taosMemoryFreeClear(pDataInfo->pRsp);
×
1344
      goto _end;
×
1345
    }
1346

1347
    void* tmp = taosArrayPush(pExchangeInfo->pResultBlockList, &pb);
10,535,133✔
1348
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
10,535,547!
1349
    qDebug("%dth block added to resultBlockList, rows:%" PRId64, index, pb->info.rows);
10,535,547✔
1350
    pb = NULL;
10,535,552✔
1351
  }
1352

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

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

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

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

1381
  while (1) {
7,149✔
1382
    if (pExchangeInfo->current >= totalSources) {
37,707✔
1383
      setAllSourcesCompleted(pOperator);
7,077✔
1384
      return TSDB_CODE_SUCCESS;
7,077✔
1385
    }
1386

1387
    SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, pExchangeInfo->current);
30,630✔
1388
    if (!pSource) {
30,630!
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){
30,630✔
1395
      pExchangeInfo->current += 1;
137✔
1396
      continue;
137✔
1397
    }
1398

1399
    SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, pExchangeInfo->current);
30,493✔
1400
    if (!pDataInfo) {
30,493!
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;
30,493✔
1406

1407
    code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, pExchangeInfo->current);
30,493✔
1408
    if (code != TSDB_CODE_SUCCESS) {
30,493!
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) {
3✔
1415
      code = exchangeWait(pOperator, pExchangeInfo);
30,496✔
1416
      if (code != TSDB_CODE_SUCCESS || isTaskKilled(pTaskInfo)) {
30,496!
1417
        T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
3!
1418
      }
1419

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

1427
      break;
30,490✔
1428
    }
1429

1430
    if (pDataInfo->code != TSDB_CODE_SUCCESS) {
30,490!
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;
30,490✔
1439
    SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
30,490✔
1440

1441
    if (pRsp->numOfRows == 0) {
30,490✔
1442
      qDebug("exhausted %p,%s vgId:%d, clientId:0x%" PRIx64 " taskID:0x%" PRIx64
7,012✔
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;
7,012✔
1448
      if (pDataInfo->isVtbRefScan || pDataInfo->isVtbTagScan) {
7,012!
1449
        pExchangeInfo->current = totalSources;
6,809✔
1450
      } else {
1451
        pExchangeInfo->current += 1;
203✔
1452
      }
1453
      taosMemoryFreeClear(pDataInfo->pRsp);
7,012!
1454
      continue;
7,012✔
1455
    }
1456

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

1462
    SRetrieveTableRsp* pRetrieveRsp = pDataInfo->pRsp;
23,478✔
1463
    if (pRsp->completed == 1) {
23,478✔
1464
      qDebug("exhausted %p,%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64 " execId:%d numOfRows:%" PRId64
246!
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;
246✔
1471
      if (pDataInfo->isVtbRefScan) {
246!
1472
        pExchangeInfo->current = totalSources;
×
1473
      } else {
1474
        pExchangeInfo->current += 1;
246✔
1475
      }
1476
    } else {
1477
      qDebug("%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64 " execId:%d numOfRows:%" PRId64
23,232✔
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) {
23,478!
1483
      taosArrayClear(pExchangeInfo->pSourceDataInfo);
23,035✔
1484
    }
1485
    updateLoadRemoteInfo(pLoadInfo, pRetrieveRsp->numOfRows, pRetrieveRsp->compLen, startTs, pOperator);
23,478✔
1486
    pDataInfo->totalRows += pRetrieveRsp->numOfRows;
23,478✔
1487

1488
    taosMemoryFreeClear(pDataInfo->pRsp);
23,478!
1489
    return TSDB_CODE_SUCCESS;
23,478✔
1490
  }
1491

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

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

1506
int32_t addSingleExchangeSource(SOperatorInfo* pOperator, SExchangeOperatorBasicParam* pBasicParam) {
31,186✔
1507
  SExchangeInfo*     pExchangeInfo = pOperator->info;
31,186✔
1508
  SExchangeSrcIndex* pIdx = tSimpleHashGet(pExchangeInfo->pHashSources, &pBasicParam->vgId, sizeof(pBasicParam->vgId));
31,186✔
1509
  if (NULL == pIdx) {
31,186✔
1510
    if (pBasicParam->isNewDeployed) {
19!
1511
      SDownstreamSourceNode *pNode = NULL;
19✔
1512
      int32_t code = nodesCloneNode((SNode*)&pBasicParam->newDeployedSrc, (SNode**)&pNode);
19✔
1513
      if (code != TSDB_CODE_SUCCESS) {
19!
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);
19✔
1519
      if (!tmp) {
19!
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};
19✔
1524
      code =
1525
          tSimpleHashPut(pExchangeInfo->pHashSources, &pNode->addr.nodeId, sizeof(pNode->addr.nodeId), &idx, sizeof(idx));
19✔
1526
      if (pExchangeInfo->pHashSources && code != TSDB_CODE_SUCCESS) {
19!
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));
19✔
1531
      if (pIdx == NULL) {
19!
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");
31,186✔
1542

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

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

1559
    dataInfo.isVtbRefScan = pBasicParam->isVtbRefScan;
27,928✔
1560
    dataInfo.isVtbTagScan = pBasicParam->isVtbTagScan;
27,928✔
1561
    dataInfo.srcOpType = pBasicParam->srcOpType;
27,928✔
1562
    dataInfo.tableSeq = pBasicParam->tableSeq;
27,928✔
1563

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

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

1583
    dataInfo.isVtbRefScan = pBasicParam->isVtbRefScan;
2,636✔
1584
    dataInfo.isVtbTagScan = pBasicParam->isVtbTagScan;
2,636✔
1585
    dataInfo.srcOpType = pBasicParam->srcOpType;
2,636✔
1586
    dataInfo.tableSeq = pBasicParam->tableSeq;
2,636✔
1587

1588
    taosArrayClearEx(pExchangeInfo->pSourceDataInfo, clearVtbScanDataInfo);
2,636✔
1589
    void* tmp = taosArrayPush(pExchangeInfo->pSourceDataInfo, &dataInfo);
2,636✔
1590
    if (!tmp) {
2,636!
1591
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1592
      return terrno;
×
1593
    }
1594
  } else {
1595
    if (pIdx->inUseIdx < 0) {
622✔
1596
      SSourceDataInfo dataInfo = {0};
616✔
1597
      dataInfo.status = EX_SOURCE_DATA_NOT_READY;
616✔
1598
      dataInfo.taskId = pExchangeInfo->pTaskId;
616✔
1599
      dataInfo.index = pIdx->srcIdx;
616✔
1600
      if (pBasicParam->isVtbRefScan) {
616!
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);
616✔
1609
      if (dataInfo.pSrcUidList == NULL) {
616!
1610
        return terrno;
×
1611
      }
1612

1613
      dataInfo.isVtbRefScan = pBasicParam->isVtbRefScan;
616✔
1614
      dataInfo.isVtbTagScan = pBasicParam->isVtbTagScan;
616✔
1615
      dataInfo.srcOpType = pBasicParam->srcOpType;
616✔
1616
      dataInfo.tableSeq = pBasicParam->tableSeq;
616✔
1617

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

1633
      if (pBasicParam->isVtbRefScan) {
6!
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);
6✔
1644
      if (pDataInfo->pSrcUidList == NULL) {
6!
1645
        return terrno;
×
1646
      }
1647

1648
      pDataInfo->isVtbRefScan = pBasicParam->isVtbRefScan;
6✔
1649
      pDataInfo->isVtbTagScan = pBasicParam->isVtbTagScan;
6✔
1650
      pDataInfo->srcOpType = pBasicParam->srcOpType;
6✔
1651
      pDataInfo->tableSeq = pBasicParam->tableSeq;
6✔
1652
    }
1653
  }
1654

1655
  return TSDB_CODE_SUCCESS;
31,186✔
1656
}
1657

1658
int32_t addDynamicExchangeSource(SOperatorInfo* pOperator) {
30,921✔
1659
  SExchangeInfo*               pExchangeInfo = pOperator->info;
30,921✔
1660
  int32_t                      code = TSDB_CODE_SUCCESS;
30,921✔
1661
  SExchangeOperatorBasicParam* pBasicParam = NULL;
30,921✔
1662
  SExchangeOperatorParam*      pParam = (SExchangeOperatorParam*)pOperator->pOperatorGetParam->value;
30,921✔
1663
  if (pParam->multiParams) {
30,921✔
1664
    SExchangeOperatorBatchParam* pBatch = (SExchangeOperatorBatchParam*)pOperator->pOperatorGetParam->value;
345✔
1665
    int32_t                      iter = 0;
345✔
1666
    while (NULL != (pBasicParam = tSimpleHashIterate(pBatch->pBatchs, pBasicParam, &iter))) {
955✔
1667
      code = addSingleExchangeSource(pOperator, pBasicParam);
610✔
1668
      if (code) {
610!
1669
        return code;
×
1670
      }
1671
    }
1672
  } else {
1673
    pBasicParam = &pParam->basic;
30,576✔
1674
    code = addSingleExchangeSource(pOperator, pBasicParam);
30,576✔
1675
  }
1676

1677
  freeOperatorParam(pOperator->pOperatorGetParam, OP_GET_PARAM);
30,921✔
1678
  pOperator->pOperatorGetParam = NULL;
30,921✔
1679

1680
  return code;
30,921✔
1681
}
1682

1683
int32_t prepareLoadRemoteData(SOperatorInfo* pOperator) {
16,307,078✔
1684
  SExchangeInfo* pExchangeInfo = pOperator->info;
16,307,078✔
1685
  int32_t        code = TSDB_CODE_SUCCESS;
16,307,078✔
1686
  int32_t        lino = 0;
16,307,078✔
1687
  
1688
  if ((OPTR_IS_OPENED(pOperator) && !pExchangeInfo->dynamicOp) ||
16,307,078✔
1689
      (pExchangeInfo->dynamicOp && NULL == pOperator->pOperatorGetParam)) {
3,163,987✔
1690
    qDebug("skip prepare, opened:%d, dynamicOp:%d, getParam:%p", OPTR_IS_OPENED(pOperator), pExchangeInfo->dynamicOp, pOperator->pOperatorGetParam);
13,144,300✔
1691
    return TSDB_CODE_SUCCESS;
13,144,365✔
1692
  }
1693

1694
  if (pExchangeInfo->dynamicOp) {
3,162,778✔
1695
    code = addDynamicExchangeSource(pOperator);
30,921✔
1696
    QUERY_CHECK_CODE(code, lino, _end);
30,921!
1697
  }
1698

1699
  if (pOperator->status == OP_NOT_OPENED && (pExchangeInfo->dynamicOp && pExchangeInfo->seqLoadData) || IS_STREAM_MODE(pOperator->pTaskInfo)) {
3,162,778✔
1700
    pExchangeInfo->current = 0;
52,177✔
1701
  }
1702

1703
  int64_t st = taosGetTimestampUs();
3,163,616✔
1704

1705
  if (!IS_STREAM_MODE(pOperator->pTaskInfo) && !pExchangeInfo->seqLoadData) {
3,163,616✔
1706
    code = prepareConcurrentlyLoad(pOperator);
3,109,328✔
1707
    QUERY_CHECK_CODE(code, lino, _end);
3,109,332!
1708
    pExchangeInfo->openedTs = taosGetTimestampUs();
3,109,350✔
1709
  }
1710

1711
  OPTR_SET_OPENED(pOperator);
3,163,638✔
1712
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
3,163,650✔
1713

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

1716
_end:
2,956,678✔
1717
  if (code != TSDB_CODE_SUCCESS) {
3,163,652!
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;
3,163,652✔
1723
}
1724

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

1728
  if (pLimitInfo->remainGroupOffset > 0) {
7,903!
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) {
7,903✔
1750
    pLimitInfo->numOfOutputGroups += 1;
242✔
1751
    if ((pLimitInfo->slimit.limit > 0) && (pLimitInfo->slimit.limit <= pLimitInfo->numOfOutputGroups)) {
242!
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);
242✔
1760
    // existing rows that belongs to previous group.
1761
    if (pBlock->info.rows > 0) {
242!
1762
      return PROJECT_RETRIEVE_DONE;
242✔
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;
7,661✔
1770

1771
  bool limitReached = applyLimitOffset(pLimitInfo, pBlock, pTaskInfo);
7,661✔
1772
  if (pBlock->info.rows == 0) {
7,661✔
1773
    return PROJECT_RETRIEVE_CONTINUE;
3,591✔
1774
  } else {
1775
    if (limitReached && (pLimitInfo->slimit.limit > 0 && pLimitInfo->slimit.limit <= pLimitInfo->numOfOutputGroups)) {
4,070!
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)) {
4,070!
1785
    return PROJECT_RETRIEVE_DONE;
4,070✔
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) {
4,815,656✔
1792
  SExecTaskInfo* pTask = pOperator->pTaskInfo;
4,815,656✔
1793
  int32_t        code = TSDB_CODE_SUCCESS;
4,815,656✔
1794
  if (pTask->pWorkerCb) {
4,815,656!
1795
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
4,815,724✔
1796
    if (code != TSDB_CODE_SUCCESS) {
4,817,805!
1797
      pTask->code = code;
×
1798
      return pTask->code;
×
1799
    }
1800
  }
1801

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

1809
  if (pTask->pWorkerCb) {
4,817,728!
1810
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
4,817,728✔
1811
    if (code != TSDB_CODE_SUCCESS) {
4,817,818!
1812
      pTask->code = code;
×
1813
      return pTask->code;
×
1814
    }
1815
  }
1816
  return TSDB_CODE_SUCCESS;
4,817,818✔
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