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

taosdata / TDengine / #4788

14 Oct 2025 11:21AM UTC coverage: 60.992% (-2.3%) from 63.264%
#4788

push

travis-ci

web-flow
Merge 7ca9b50f9 into 19574fe21

154868 of 324306 branches covered (47.75%)

Branch coverage included in aggregate %.

207304 of 269498 relevant lines covered (76.92%)

125773493.22 hits per line

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

55.15
/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,
20,502,923✔
75
                                           SExecTaskInfo* pTaskInfo) {
76
  int32_t code = 0;
20,502,923✔
77
  int32_t lino = 0;
20,512,542✔
78
  int64_t startTs = taosGetTimestampUs();  
20,504,539✔
79
  int32_t  totalSources = (int32_t)taosArrayGetSize(pExchangeInfo->pSourceDataInfo);
20,504,539✔
80
  int32_t completed = 0;
20,503,988✔
81
  code = getCompletedSources(pExchangeInfo->pSourceDataInfo, &completed);
20,504,539✔
82
  if (code != TSDB_CODE_SUCCESS) {
20,502,923!
83
    pTaskInfo->code = code;
×
84
    T_LONG_JMP(pTaskInfo->env, code);
×
85
  }
86
  if (completed == totalSources) {
20,502,923✔
87
    qDebug("%s no load since all sources completed, completed:%d, totalSources:%d", pTaskInfo->id.str, completed, totalSources);
5,113,050✔
88
    setAllSourcesCompleted(pOperator);
5,113,050✔
89
    return;
5,119,518✔
90
  }
91

92
  SSourceDataInfo* pDataInfo = NULL;
15,389,873✔
93

94
  while (1) {
8,559,592✔
95
    if (pExchangeInfo->current < 0) {
23,949,465✔
96
      qDebug("current %d and all sources complted, totalSources:%d", pExchangeInfo->current, totalSources);
208,664✔
97
      setAllSourcesCompleted(pOperator);
208,664✔
98
      return;
208,664✔
99
    }
100
    
101
    if (pExchangeInfo->current >= totalSources) {
23,740,261✔
102
      completed = 0;
9,598,103✔
103
      code = getCompletedSources(pExchangeInfo->pSourceDataInfo, &completed);
9,598,103✔
104
      if (code != TSDB_CODE_SUCCESS) {
9,597,563!
105
        pTaskInfo->code = code;
×
106
        T_LONG_JMP(pTaskInfo->env, code);
×
107
      }
108
      if (completed == totalSources) {
9,597,563✔
109
        qDebug("stop to load since all sources complted, completed:%d, totalSources:%d", completed, totalSources);
7,507,152✔
110
        setAllSourcesCompleted(pOperator);
7,507,152✔
111
        return;
7,506,037✔
112
      }
113
      
114
      pExchangeInfo->current = 0;
2,090,411✔
115
    }
116

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

119
    SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, pExchangeInfo->current);
16,234,729✔
120
    if (!pDataInfo) {
16,234,189!
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) {
16,234,189!
127
      pExchangeInfo->current++;
×
128
      continue;
×
129
    }
130

131
    pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
16,234,189✔
132

133
    code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, pExchangeInfo->current);
16,235,269✔
134
    if (code != TSDB_CODE_SUCCESS) {
16,235,820!
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) {
7,863✔
141
      code = exchangeWait(pOperator, pExchangeInfo);
16,243,683✔
142
      if (code != TSDB_CODE_SUCCESS || isTaskKilled(pTaskInfo)) {
16,244,223!
143
        T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
7,863!
144
      }
145

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

154
      break;
16,227,946✔
155
    }
156

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

172
    SRetrieveTableRsp*   pRsp = pDataInfo->pRsp;
16,220,705✔
173
    SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
16,220,705✔
174

175
    if (pRsp->numOfRows == 0) {
16,220,705✔
176
      qDebug("exhausted %p,%s vgId:%d, clientId:0x%" PRIx64 " taskID:0x%" PRIx64
8,559,592✔
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;
8,559,592✔
182
      if (pDataInfo->isVtbRefScan || pDataInfo->isVtbTagScan) {
8,559,592!
183
        pExchangeInfo->current = -1;
209,204✔
184
      } else {
185
        pExchangeInfo->current += 1;
8,350,388✔
186
      }
187
      taosMemoryFreeClear(pDataInfo->pRsp);
8,559,592!
188
      continue;
8,559,592✔
189
    }
190

191
    code = doExtractResultBlocks(pExchangeInfo, pDataInfo);
7,661,113✔
192
    TAOS_CHECK_EXIT(code);
7,661,113!
193

194
    SRetrieveTableRsp* pRetrieveRsp = pDataInfo->pRsp;
7,661,113✔
195
    if (pRsp->completed == 1) {
7,661,113✔
196
      qDebug("exhausted %p,%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64 " execId:%d numOfRows:%" PRId64
5,382,018✔
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;
5,382,018✔
203
      if (pDataInfo->isVtbRefScan) {
5,382,018!
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
2,279,095✔
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);
7,661,113✔
216
    pDataInfo->totalRows += pRetrieveRsp->numOfRows;
7,661,113✔
217

218
    pExchangeInfo->current++;
7,661,113✔
219

220
    taosMemoryFreeClear(pDataInfo->pRsp);
7,661,113!
221
    return;
7,660,573✔
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,
150,158,778✔
234
                                           SExecTaskInfo* pTaskInfo) {
235
  int32_t code = 0;
150,158,778✔
236
  int32_t lino = 0;
150,158,778✔
237
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSourceDataInfo);
150,158,778✔
238
  int32_t completed = 0;
150,158,838✔
239
  code = getCompletedSources(pExchangeInfo->pSourceDataInfo, &completed);
150,158,838✔
240
  if (code != TSDB_CODE_SUCCESS) {
150,158,489!
241
    pTaskInfo->code = code;
×
242
    T_LONG_JMP(pTaskInfo->env, code);
×
243
  }
244
  if (completed == totalSources) {
150,158,489✔
245
    setAllSourcesCompleted(pOperator);
46,238,541✔
246
    return;
46,238,973✔
247
  }
248

249
  SSourceDataInfo* pDataInfo = NULL;
103,919,948✔
250

251
  while (1) {
11,589,255✔
252
    qDebug("prepare wait for ready, %p, %s", pExchangeInfo, GET_TASKID(pTaskInfo));
115,509,203✔
253
    code = exchangeWait(pOperator, pExchangeInfo);
115,509,203✔
254

255
    if (code != TSDB_CODE_SUCCESS || isTaskKilled(pTaskInfo)) {
115,509,768!
256
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
280!
257
    }
258

259
    for (int32_t i = 0; i < totalSources; ++i) {
172,220,194!
260
      pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, i);
172,220,194✔
261
      QUERY_CHECK_NULL(pDataInfo, code, lino, _exit, terrno);
172,220,184!
262
      if (pDataInfo->status == EX_SOURCE_DATA_EXHAUSTED) {
172,220,184✔
263
        continue;
40,125,424✔
264
      }
265

266
      if (pDataInfo->status != EX_SOURCE_DATA_READY) {
132,094,834✔
267
        continue;
16,585,002✔
268
      }
269

270
      int64_t currSeqId = atomic_load_64(&pExchangeInfo->seqId);
115,509,552✔
271
      if (pDataInfo->seqId != currSeqId) {
115,509,622!
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) {
115,509,552✔
278
        code = pDataInfo->code;
410✔
279
        TAOS_CHECK_EXIT(code);
410!
280
      }
281

282
      tmemory_barrier();
115,509,422✔
283
      SRetrieveTableRsp*     pRsp = pDataInfo->pRsp;
115,509,422✔
284
      SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, pDataInfo->index);
115,509,358✔
285
      QUERY_CHECK_NULL(pSource, code, lino, _exit, terrno);
115,509,638!
286

287
      // todo
288
      SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
115,509,638✔
289
      if (pRsp->numOfRows == 0) {
115,509,428✔
290
        if (NULL != pDataInfo->pSrcUidList && (!pDataInfo->isVtbRefScan)) {
28,741,569!
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,741,569✔
299
          qDebug("exhausted %p,%s vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
28,741,289✔
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,741,289!
304
        }
305
        break;
28,741,569✔
306
      }
307

308
      TAOS_CHECK_EXIT(doExtractResultBlocks(pExchangeInfo, pDataInfo));
86,767,146!
309

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

314
      if (pRsp->completed == 1) {
86,768,069✔
315
        pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
73,791,423✔
316
        qDebug("exhausted %p,%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
73,791,423✔
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
12,976,646!
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);
86,768,069!
330

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

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

355
_exit:
410✔
356

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

363
static SSDataBlock* doLoadRemoteDataImpl(SOperatorInfo* pOperator) {
400,030,142✔
364
  int32_t        code = TSDB_CODE_SUCCESS;
400,030,142✔
365
  SExchangeInfo* pExchangeInfo = pOperator->info;
400,030,142✔
366
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
400,031,773✔
367

368
  size_t totalSources = taosArrayGetSize(pExchangeInfo->pSources);
400,031,781✔
369

370
  SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
400,032,861✔
371
  if (pOperator->status == OP_EXEC_DONE) {
400,032,585!
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;
400,031,770✔
380
  if (taosArrayGetSize(pExchangeInfo->pResultBlockList) > 0) {
400,031,554✔
381
    p = taosArrayGetP(pExchangeInfo->pResultBlockList, 0);
196,913,975✔
382
    taosArrayRemove(pExchangeInfo->pResultBlockList, 0);
196,913,975✔
383
  }
384

385
  if (p != NULL) {
400,030,978✔
386
    void* tmp = taosArrayPush(pExchangeInfo->pRecycledBlocks, &p);
196,913,975✔
387
    if (!tmp) {
196,912,883!
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;
196,912,883✔
394
  } else {
395
    if (pExchangeInfo->seqLoadData) {
203,117,003!
396
      code = seqLoadRemoteData(pOperator);
32,455,014✔
397
      if (code != TSDB_CODE_SUCCESS) {
32,454,360!
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))   {
170,662,360✔
403
      streamConcurrentlyLoadRemoteData(pOperator, pExchangeInfo, pTaskInfo);
20,505,094✔
404
    } else {
405
      concurrentlyLoadRemoteDataImpl(pOperator, pExchangeInfo, pTaskInfo);
150,158,778✔
406
    }
407
    if (TSDB_CODE_SUCCESS != pOperator->pTaskInfo->code) {
203,102,313✔
408
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(pOperator->pTaskInfo->code));
410!
409
      T_LONG_JMP(pTaskInfo->env, pOperator->pTaskInfo->code);
410!
410
    }
411
    
412
    if (taosArrayGetSize(pExchangeInfo->pResultBlockList) == 0) {
203,101,903✔
413
      qDebug("empty resultBlockList");
83,752,698✔
414
      return NULL;
83,752,698✔
415
    } else {
416
      p = taosArrayGetP(pExchangeInfo->pResultBlockList, 0);
119,349,572✔
417
      taosArrayRemove(pExchangeInfo->pResultBlockList, 0);
119,350,285✔
418
      void* tmp = taosArrayPush(pExchangeInfo->pRecycledBlocks, &p);
119,349,745✔
419
      if (!tmp) {
119,350,285!
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);
119,350,285✔
427
      return p;
119,349,745✔
428
    }
429
}
430
}
431

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

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

440
  code = pOperator->fpSet._openFn(pOperator);
398,735,614✔
441
  QUERY_CHECK_CODE(code, lino, _end);
398,733,841!
442

443
  if (pOperator->status == OP_EXEC_DONE) {
398,733,841✔
444
    (*ppRes) = NULL;
57,696✔
445
    return code;
57,696✔
446
  }
447

448
  while (1) {
1,354,880✔
449
    SSDataBlock* pBlock = doLoadRemoteDataImpl(pOperator);
400,031,025✔
450
    if (pBlock == NULL) {
400,014,770✔
451
      (*ppRes) = NULL;
83,752,698✔
452
      return code;
83,752,698✔
453
    }
454

455
    code = doFilter(pBlock, pOperator->exprSupp.pFilterInfo, NULL, NULL);
316,262,072✔
456
    QUERY_CHECK_CODE(code, lino, _end);
316,262,628!
457

458
    if (blockDataGetNumOfRows(pBlock) == 0) {
316,262,628✔
459
      qDebug("rows 0 block got, continue next load");
1,426!
460
      continue;
1,426✔
461
    }
462

463
    SLimitInfo* pLimitInfo = &pExchangeInfo->limitInfo;
316,262,834✔
464
    if (hasLimitOffsetInfo(pLimitInfo)) {
316,262,294✔
465
      int32_t status = handleLimitOffset(pOperator, pLimitInfo, pBlock, false);
2,848,502✔
466
      if (status == PROJECT_RETRIEVE_CONTINUE) {
2,848,502✔
467
        qDebug("limit retrieve continue");
1,353,454✔
468
        continue;
1,353,454✔
469
      } else if (status == PROJECT_RETRIEVE_DONE) {
1,495,048!
470
        if (pBlock->info.rows == 0) {
1,495,048!
471
          setOperatorCompleted(pOperator);
×
472
          (*ppRes) = NULL;
×
473
          return code;
×
474
        } else {
475
          (*ppRes) = pBlock;
1,495,048✔
476
          return code;
1,495,048✔
477
        }
478
      }
479
    } else {
480
      (*ppRes) = pBlock;
313,413,587✔
481
      qDebug("block with rows %" PRId64 " returned in exechange", pBlock->info.rows);
313,413,792✔
482
      return code;
313,414,127✔
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) {
72,665,945✔
501
  pInfo->pSourceDataInfo = taosArrayInit(numOfSources, sizeof(SSourceDataInfo));
72,665,945✔
502
  if (pInfo->pSourceDataInfo == NULL) {
72,666,725!
503
    return terrno;
×
504
  }
505

506
  if (pInfo->dynamicOp) {
72,666,924!
507
    return TSDB_CODE_SUCCESS;
2,757,458✔
508
  }
509

510
  int32_t len = strlen(id) + 1;
69,909,019!
511
  pInfo->pTaskId = taosMemoryCalloc(1, len);
69,909,019✔
512
  if (!pInfo->pTaskId) {
69,911,557!
513
    return terrno;
×
514
  }
515
  tstrncpy(pInfo->pTaskId, id, len);
69,909,188!
516
  for (int32_t i = 0; i < numOfSources; ++i) {
175,316,145✔
517
    SSourceDataInfo dataInfo = {0};
105,403,700✔
518
    dataInfo.status = EX_SOURCE_DATA_NOT_READY;
105,403,700✔
519
    dataInfo.taskId = pInfo->pTaskId;
105,403,700✔
520
    dataInfo.index = i;
105,404,673✔
521
    SSourceDataInfo* pDs = taosArrayPush(pInfo->pSourceDataInfo, &dataInfo);
105,404,673✔
522
    if (pDs == NULL) {
105,404,961!
523
      taosArrayDestroyEx(pInfo->pSourceDataInfo, freeSourceDataInfo);
×
524
      return terrno;
×
525
    }
526
    qDebug("init source data info %d, pDs:%p, status:%d", i, pDs, pDs->status);
105,404,961✔
527
  }
528

529
  return TSDB_CODE_SUCCESS;
69,912,445✔
530
}
531

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

535
  if (numOfSources == 0) {
72,666,833!
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));
72,666,833✔
540
  if (!pInfo->pFetchRpcHandles) {
72,668,184!
541
    return terrno;
×
542
  }
543
  void* ret = taosArrayReserve(pInfo->pFetchRpcHandles, numOfSources);
72,667,427✔
544
  if (!ret) {
72,667,697!
545
    return terrno;
×
546
  }
547

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

554
  if (pExNode->node.dynamicOp) {
72,669,061!
555
    pInfo->pHashSources = tSimpleHashInit(numOfSources * 2, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
2,757,458✔
556
    if (NULL == pInfo->pHashSources) {
2,757,458!
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) {
185,949,327✔
563
    SDownstreamSourceNode* pNode = (SDownstreamSourceNode*)nodesListGetNode((SNodeList*)pExNode->pSrcEndPoints, i);
113,280,463✔
564
    if (!pNode) {
113,279,849!
565
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
566
      return terrno;
×
567
    }
568
    void* tmp = taosArrayPush(pInfo->pSources, pNode);
113,279,849✔
569
    if (!tmp) {
113,282,962!
570
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
571
      return terrno;
×
572
    }
573
    SExchangeSrcIndex idx = {.srcIdx = i, .inUseIdx = -1};
113,282,962✔
574
    int32_t           code =
575
        tSimpleHashPut(pInfo->pHashSources, &pNode->addr.nodeId, sizeof(pNode->addr.nodeId), &idx, sizeof(idx));
113,280,906✔
576
    if (pInfo->pHashSources && code != TSDB_CODE_SUCCESS) {
113,279,089!
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);
72,668,864✔
583
  int64_t refId = taosAddRef(exchangeObjRefPool, pInfo);
72,667,671✔
584
  if (refId < 0) {
72,664,127!
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;
72,664,127✔
590
  }
591

592
  return initDataSource(numOfSources, pInfo, id);
72,664,784✔
593
}
594

595
int32_t resetExchangeOperState(SOperatorInfo* pOper) {
24,388,636✔
596
  SExchangeInfo* pInfo = pOper->info;
24,388,636✔
597
  SExchangePhysiNode* pPhynode = (SExchangePhysiNode*)pOper->pPhyNode;
24,392,008✔
598

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

601
  (void)atomic_add_fetch_64(&pInfo->seqId, 1);
24,392,552✔
602
  pOper->status = OP_NOT_OPENED;
24,393,057✔
603
  pInfo->current = 0;
24,393,057✔
604
  pInfo->loadInfo.totalElapsed = 0;
24,392,517✔
605
  pInfo->loadInfo.totalRows = 0;
24,393,057✔
606
  pInfo->loadInfo.totalSize = 0;
24,391,973✔
607
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pSourceDataInfo); ++i) {
71,063,260✔
608
    SSourceDataInfo* pDataInfo = taosArrayGet(pInfo->pSourceDataInfo, i);
46,670,199✔
609
    taosWLockLatch(&pDataInfo->lock);
46,669,655✔
610
    taosMemoryFreeClear(pDataInfo->decompBuf);
46,671,287!
611
    taosMemoryFreeClear(pDataInfo->pRsp);
46,671,287!
612

613
    pDataInfo->totalRows = 0;
46,671,287✔
614
    pDataInfo->code = 0;
46,670,732✔
615
    pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
46,670,747✔
616
    pDataInfo->fetchSent = false;
46,669,652✔
617
    taosWUnLockLatch(&pDataInfo->lock);
46,671,287✔
618
  }
619

620
  if (pInfo->dynamicOp) {
24,391,973!
621
    taosArrayClearEx(pInfo->pSourceDataInfo, freeSourceDataInfo);
5,799,200✔
622
  } 
623

624
  taosArrayClearEx(pInfo->pResultBlockList, freeBlock);
24,391,973✔
625
  taosArrayClearEx(pInfo->pRecycledBlocks, freeBlock);
24,391,418✔
626

627
  blockDataCleanup(pInfo->pDummyBlock);
24,391,973✔
628

629
  void   *data = NULL;
24,391,433✔
630
  int32_t iter = 0;
24,391,433✔
631
  while ((data = tSimpleHashIterate(pInfo->pHashSources, data, &iter))) {
32,570,089✔
632
    ((SExchangeSrcIndex *)data)->inUseIdx = -1;
8,178,656✔
633
  }
634
  
635
  pInfo->limitInfo = (SLimitInfo){0};
24,393,057✔
636
  initLimitInfo(pPhynode->node.pLimit, pPhynode->node.pSlimit, &pInfo->limitInfo);
24,393,057✔
637

638
  return 0;
24,392,513✔
639
}
640

641
int32_t createExchangeOperatorInfo(void* pTransporter, SExchangePhysiNode* pExNode, SExecTaskInfo* pTaskInfo,
72,668,480✔
642
                                   SOperatorInfo** pOptrInfo) {
643
  QRY_PARAM_CHECK(pOptrInfo);
72,668,480!
644

645
  int32_t        code = 0;
72,668,756✔
646
  int32_t        lino = 0;
72,668,756✔
647
  SExchangeInfo* pInfo = taosMemoryCalloc(1, sizeof(SExchangeInfo));
72,668,756✔
648
  SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
72,667,583✔
649
  if (pInfo == NULL || pOperator == NULL) {
72,664,473!
650
    code = terrno;
×
651
    goto _error;
×
652
  }
653

654
  pOperator->pPhyNode = pExNode;
72,665,878✔
655
  pInfo->dynamicOp = pExNode->node.dynamicOp;
72,666,298!
656
  code = initExchangeOperator(pExNode, pInfo, GET_TASKID(pTaskInfo));
72,667,157✔
657
  QUERY_CHECK_CODE(code, lino, _error);
72,669,627!
658

659
  code = tsem_init(&pInfo->ready, 0, 0);
72,669,627✔
660
  QUERY_CHECK_CODE(code, lino, _error);
72,668,791!
661

662
  pInfo->pDummyBlock = createDataBlockFromDescNode(pExNode->node.pOutputDataBlockDesc);
72,668,791✔
663
  QUERY_CHECK_NULL(pInfo->pDummyBlock, code, lino, _error, terrno);
72,670,183!
664

665
  pInfo->pResultBlockList = taosArrayInit(64, POINTER_BYTES);
72,669,627✔
666
  QUERY_CHECK_NULL(pInfo->pResultBlockList, code, lino, _error, terrno);
72,669,751!
667
  pInfo->pRecycledBlocks = taosArrayInit(64, POINTER_BYTES);
72,669,411✔
668
  QUERY_CHECK_NULL(pInfo->pRecycledBlocks, code, lino, _error, terrno);
72,669,206!
669

670
  SExchangeOpStopInfo stopInfo = {QUERY_NODE_PHYSICAL_PLAN_EXCHANGE, pInfo->self};
72,669,486✔
671
  code = qAppendTaskStopInfo(pTaskInfo, &stopInfo);
72,669,486✔
672
  QUERY_CHECK_CODE(code, lino, _error);
72,669,398!
673

674
  pInfo->seqLoadData = pExNode->seqRecvData;
72,669,398!
675
  pInfo->dynTbname = pExNode->dynTbname;
72,669,967!
676
  if (pInfo->dynTbname) {
72,669,627!
677
    pInfo->seqLoadData = true;
17,430✔
678
  }
679
  pInfo->pTransporter = pTransporter;
72,669,627✔
680

681
  setOperatorInfo(pOperator, "ExchangeOperator", QUERY_NODE_PHYSICAL_PLAN_EXCHANGE, false, OP_NOT_OPENED, pInfo,
72,669,347✔
682
                  pTaskInfo);
683
  pOperator->exprSupp.numOfExprs = taosArrayGetSize(pInfo->pDummyBlock->pDataBlock);
72,669,903✔
684

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

719
void freeBlock(void* pParam) {
159,393,556✔
720
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
159,393,556✔
721
  blockDataDestroy(pBlock);
159,393,346✔
722
}
159,393,073✔
723

724
void freeSourceDataInfo(void* p) {
106,660,935✔
725
  SSourceDataInfo* pInfo = (SSourceDataInfo*)p;
106,660,935✔
726
  taosMemoryFreeClear(pInfo->decompBuf);
106,660,935!
727
  taosMemoryFreeClear(pInfo->pRsp);
106,661,732!
728

729
  pInfo->decompBufSize = 0;
106,661,516✔
730
}
106,661,516✔
731

732
void doDestroyExchangeOperatorInfo(void* param) {
72,669,386✔
733
  if (param == NULL) {
72,669,386!
734
    return;
×
735
  }
736
  SExchangeInfo* pExInfo = (SExchangeInfo*)param;
72,669,386✔
737
  if (pExInfo->pFetchRpcHandles) {
72,669,386✔
738
    for (int32_t i = 0; i < pExInfo->pFetchRpcHandles->size; ++i) {
185,951,662✔
739
      int64_t* pRpcHandle = taosArrayGet(pExInfo->pFetchRpcHandles, i);
113,283,629✔
740
      if (*pRpcHandle > 0) {
113,283,629✔
741
        SDownstreamSourceNode* pSource = taosArrayGet(pExInfo->pSources, i);
6,198,687✔
742
        (void)asyncFreeConnById(pExInfo->pTransporter, *pRpcHandle);
6,198,687✔
743
      }
744
    }
745
    taosArrayDestroy(pExInfo->pFetchRpcHandles);
72,669,417✔
746
  }
747

748
  taosArrayDestroy(pExInfo->pSources);
72,670,183✔
749
  taosArrayDestroyEx(pExInfo->pSourceDataInfo, freeSourceDataInfo);
72,669,967✔
750

751
  taosArrayDestroyEx(pExInfo->pResultBlockList, freeBlock);
72,669,249✔
752
  taosArrayDestroyEx(pExInfo->pRecycledBlocks, freeBlock);
72,669,967✔
753

754
  blockDataDestroy(pExInfo->pDummyBlock);
72,668,733✔
755
  tSimpleHashCleanup(pExInfo->pHashSources);
72,669,244✔
756

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

763
  taosMemoryFreeClear(param);
72,667,846!
764
}
765

766
int32_t loadRemoteDataCallback(void* param, SDataBuf* pMsg, int32_t code) {
166,154,478✔
767
  SFetchRspHandleWrapper* pWrapper = (SFetchRspHandleWrapper*)param;
166,154,478✔
768

769
  taosMemoryFreeClear(pMsg->pEpSet);
166,154,478!
770
  SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pWrapper->exchangeId);
166,164,918✔
771
  if (pExchangeInfo == NULL) {
166,164,181✔
772
    qWarn("failed to acquire exchange operator, since it may have been released, %p", pExchangeInfo);
29,012!
773
    taosMemoryFree(pMsg->pData);
29,012!
774
    return TSDB_CODE_SUCCESS;
29,012✔
775
  }
776

777
  int64_t currSeqId = atomic_load_64(&pExchangeInfo->seqId);
166,135,169✔
778
  if (pWrapper->seqId != currSeqId) {
166,135,860!
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;
166,126,013✔
789

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

792
  int64_t* pRpcHandle = taosArrayGet(pExchangeInfo->pFetchRpcHandles, index);
166,133,521✔
793
  if (pRpcHandle != NULL) {
166,137,538✔
794
    int32_t ret = asyncFreeConnById(pExchangeInfo->pTransporter, *pRpcHandle);
166,130,997✔
795
    if (ret != 0) {
166,139,069✔
796
      qDebug("failed to free rpc handle, code:%s, %p", tstrerror(ret), pExchangeInfo);
8,313,037✔
797
    }
798
    *pRpcHandle = -1;
166,139,069✔
799
  }
800

801
  SSourceDataInfo* pSourceDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, index);
166,135,347✔
802
  if (!pSourceDataInfo) {
166,135,281!
803
    return terrno;
×
804
  }
805

806
  if (0 == code && NULL == pMsg->pData) {
166,135,281!
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);
166,140,619✔
812
  if (code == TSDB_CODE_SUCCESS) {
166,139,069✔
813
    pSourceDataInfo->seqId = pWrapper->seqId;
166,121,530✔
814
    pSourceDataInfo->pRsp = pMsg->pData;
166,115,077✔
815

816
    SRetrieveTableRsp* pRsp = pSourceDataInfo->pRsp;
166,105,429✔
817
    pRsp->numOfRows = htobe64(pRsp->numOfRows);
166,103,906✔
818
    pRsp->compLen = htonl(pRsp->compLen);
166,112,388✔
819
    pRsp->payloadLen = htonl(pRsp->payloadLen);
166,114,002✔
820
    pRsp->numOfCols = htonl(pRsp->numOfCols);
166,098,323✔
821
    pRsp->useconds = htobe64(pRsp->useconds);
166,111,889✔
822
    pRsp->numOfBlocks = htonl(pRsp->numOfBlocks);
166,092,417✔
823

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

838
  tmemory_barrier();
166,121,721✔
839
  pSourceDataInfo->status = EX_SOURCE_DATA_READY;
166,121,721✔
840
  taosWUnLockLatch(&pSourceDataInfo->lock);
166,120,433✔
841
  
842
  code = tsem_post(&pExchangeInfo->ready);
166,124,137✔
843
  if (code != TSDB_CODE_SUCCESS) {
166,133,743!
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);
166,133,743✔
849
  if (code != TSDB_CODE_SUCCESS) {
166,137,686!
850
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
851
  }
852
  return code;
166,138,193✔
853
}
854

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

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

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

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

884
  return TSDB_CODE_SUCCESS;
361,729✔
885
}
886

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

892
  *ppRes = taosMemoryMalloc(sizeof(SOperatorParam));
30,265,378!
893
  QUERY_CHECK_NULL(*ppRes, code, lino, _return, terrno);
30,265,378!
894

895
  pScan = taosMemoryMalloc(sizeof(STableScanOperatorParam));
30,265,378!
896
  QUERY_CHECK_NULL(pScan, code, lino, _return, terrno);
30,265,378!
897

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

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

904
  pScan->pOrgTbInfo->vgId = pMap->vgId;
30,265,378✔
905
  tstrncpy(pScan->pOrgTbInfo->tbName, pMap->tbName, TSDB_TABLE_FNAME_LEN);
30,265,378!
906

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

910
  pScan->tableSeq = tableSeq;
30,265,378✔
911
  pScan->window.skey = window->skey;
30,265,378✔
912
  pScan->window.ekey = window->ekey;
30,265,378✔
913

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

920
  return code;
30,265,378✔
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,242,950✔
936
  int32_t                  code = TSDB_CODE_SUCCESS;
2,242,950✔
937
  int32_t                  lino = 0;
2,242,950✔
938
  STagScanOperatorParam*   pScan = NULL;
2,242,950✔
939

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

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

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

953
  return code;
2,242,950✔
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) {
6,755,375✔
964
  if (!pRuntimeInfo || !pTimeRange) {
6,755,375!
965
    return TSDB_CODE_INTERNAL_ERROR;
×
966
  }
967

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

973
  switch (pRuntimeInfo->triggerType) {
6,756,466✔
974
    case STREAM_TRIGGER_SLIDING:
5,679,541✔
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
5,679,541✔
978
      pTimeRange->ekey = pParam->wend;    // is equal to wend
5,679,541✔
979
      break;
5,679,541✔
980
    case STREAM_TRIGGER_PERIOD:
84,771✔
981
      pTimeRange->skey = pParam->prevLocalTime;
84,771✔
982
      pTimeRange->ekey = pParam->triggerTime;
85,311✔
983
      break;
85,311✔
984
    default:
991,063✔
985
      pTimeRange->skey = pParam->wstart;
991,063✔
986
      pTimeRange->ekey = pParam->wend;
991,614✔
987
      break;
991,063✔
988
  }
989

990
  return TSDB_CODE_SUCCESS;
6,755,915✔
991
}
992

993
int32_t doSendFetchDataRequest(SExchangeInfo* pExchangeInfo, SExecTaskInfo* pTaskInfo, int32_t sourceIndex) {
166,342,894✔
994
  int32_t          code = TSDB_CODE_SUCCESS;
166,342,894✔
995
  int32_t          lino = 0;
166,342,894✔
996
  SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, sourceIndex);
166,342,894✔
997
  if (!pDataInfo) {
166,343,453!
998
    return terrno;
×
999
  }
1000

1001
  if (EX_SOURCE_DATA_NOT_READY != pDataInfo->status) {
166,343,453!
1002
    return TSDB_CODE_SUCCESS;
×
1003
  }
1004

1005
  pDataInfo->status = EX_SOURCE_DATA_STARTED;
166,344,088✔
1006
  SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, pDataInfo->index);
166,344,366✔
1007
  if (!pSource) {
166,343,810!
1008
    return terrno;
×
1009
  }
1010

1011
  pDataInfo->startTime = taosGetTimestampUs();
166,344,090✔
1012
  size_t totalSources = taosArrayGetSize(pExchangeInfo->pSources);
166,344,646✔
1013

1014
  SFetchRspHandleWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SFetchRspHandleWrapper));
166,341,401✔
1015
  QUERY_CHECK_NULL(pWrapper, code, lino, _end, terrno);
166,344,496!
1016
  pWrapper->exchangeId = pExchangeInfo->self;
166,344,496✔
1017
  pWrapper->sourceIndex = sourceIndex;
166,344,785✔
1018
  pWrapper->seqId = pExchangeInfo->seqId;
166,345,053✔
1019

1020
  if (pSource->localExec) {
166,342,511!
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;
166,344,273✔
1030
    SResFetchReq req = {0};
166,344,273✔
1031
    req.header.vgId = pSource->addr.nodeId;
166,343,692✔
1032
    req.sId = pSource->sId;
166,344,589✔
1033
    req.clientId = pSource->clientId;
166,343,453✔
1034
    req.taskId = pSource->taskId;
166,344,232✔
1035
    req.queryId = pTaskInfo->id.queryId;
166,345,115✔
1036
    req.execId = pSource->execId;
166,343,545✔
1037
    if (pTaskInfo->pStreamRuntimeInfo) {
166,343,549✔
1038
      req.dynTbname = pExchangeInfo->dynTbname;
16,328,336!
1039
      req.execId = pTaskInfo->pStreamRuntimeInfo->execId;
16,327,755✔
1040
      req.pStRtFuncInfo = &pTaskInfo->pStreamRuntimeInfo->funcInfo;
16,326,724✔
1041

1042
      if (pSource->fetchMsgType == TDMT_STREAM_FETCH_FROM_RUNNER) {
16,328,336✔
1043
        qDebug("%s stream fetch from runner, execId:%d, %p", GET_TASKID(pTaskInfo), req.execId, pTaskInfo->pStreamRuntimeInfo);
6,039!
1044
      } else if (pSource->fetchMsgType == TDMT_STREAM_FETCH_FROM_CACHE) {
16,318,357✔
1045
        code = getCurrentWinCalcTimeRange(req.pStRtFuncInfo, &req.pStRtFuncInfo->curWindow);
6,755,915✔
1046
        QUERY_CHECK_CODE(code, lino, _end);
6,755,915!
1047
        needStreamPesudoFuncVals = false;
6,755,915✔
1048
        qDebug("%s stream fetch from cache, execId:%d, curWinIdx:%d, time range:[%" PRId64 ", %" PRId64 "]",
6,755,915✔
1049
               GET_TASKID(pTaskInfo), req.execId, req.pStRtFuncInfo->curIdx, req.pStRtFuncInfo->curWindow.skey,
1050
               req.pStRtFuncInfo->curWindow.ekey);
1051
      }
1052
      if (!pDataInfo->fetchSent) {
16,325,519✔
1053
        req.reset = pDataInfo->fetchSent = true;
14,235,678!
1054
      }
1055
    }
1056
    if (pDataInfo->isVtbRefScan) {
166,340,660!
1057
      code = buildTableScanOperatorParamEx(&req.pOpParam, pDataInfo->pSrcUidList, pDataInfo->srcOpType, pDataInfo->colMap, pDataInfo->tableSeq, &pDataInfo->window);
30,265,378!
1058
      taosArrayDestroy(pDataInfo->colMap->colMap);
30,265,378✔
1059
      taosMemoryFreeClear(pDataInfo->colMap);
30,265,378!
1060
      taosArrayDestroy(pDataInfo->pSrcUidList);
30,265,378✔
1061
      pDataInfo->pSrcUidList = NULL;
30,265,378✔
1062
      if (TSDB_CODE_SUCCESS != code) {
30,265,378!
1063
        pTaskInfo->code = code;
×
1064
        taosMemoryFree(pWrapper);
×
1065
        return pTaskInfo->code;
×
1066
      }
1067
    } else if (pDataInfo->isVtbTagScan) {
136,077,247!
1068
      code = buildTagScanOperatorParam(&req.pOpParam, pDataInfo->pSrcUidList, pDataInfo->srcOpType);
2,242,950✔
1069
      taosArrayDestroy(pDataInfo->pSrcUidList);
2,242,950✔
1070
      pDataInfo->pSrcUidList = NULL;
2,242,950✔
1071
      if (TSDB_CODE_SUCCESS != code) {
2,242,950!
1072
        pTaskInfo->code = code;
×
1073
        taosMemoryFree(pWrapper);
×
1074
        return pTaskInfo->code;
×
1075
      }
1076
    } else {
1077
      if (pDataInfo->pSrcUidList) {
133,834,562✔
1078
        code = buildTableScanOperatorParam(&req.pOpParam, pDataInfo->pSrcUidList, pDataInfo->srcOpType, pDataInfo->tableSeq);
346,173!
1079
        taosArrayDestroy(pDataInfo->pSrcUidList);
346,173✔
1080
        pDataInfo->pSrcUidList = NULL;
346,173✔
1081
        if (TSDB_CODE_SUCCESS != code) {
346,173!
1082
          pTaskInfo->code = code;
×
1083
          taosMemoryFree(pWrapper);
×
1084
          return pTaskInfo->code;
×
1085
        }
1086
      }
1087
    }
1088

1089
    int32_t msgSize = tSerializeSResFetchReq(NULL, 0, &req, needStreamPesudoFuncVals);
166,341,797✔
1090
    if (msgSize < 0) {
166,338,514!
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);
166,338,514✔
1098
    if (NULL == msg) {
166,337,431!
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);
166,337,431✔
1106
    if (msgSize < 0) {
166,337,779!
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);
166,337,779✔
1115

1116
    qDebug("%s build fetch msg and send to vgId:%d, ep:%s, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
166,336,920✔
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));
166,341,002✔
1123
    if (NULL == pMsgSendInfo) {
166,342,929!
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;
166,342,929✔
1132
    pMsgSendInfo->paramFreeFp = taosAutoMemoryFree;
166,343,480✔
1133
    pMsgSendInfo->msgInfo.pData = msg;
166,343,216✔
1134
    pMsgSendInfo->msgInfo.len = msgSize;
166,343,205✔
1135
    pMsgSendInfo->msgType = pSource->fetchMsgType;
166,343,469✔
1136
    pMsgSendInfo->fp = loadRemoteDataCallback;
166,343,694✔
1137
    pMsgSendInfo->requestId = pTaskInfo->id.queryId;
166,343,962✔
1138

1139
    int64_t transporterId = 0;
166,344,769✔
1140
    void* poolHandle = NULL;
166,343,127✔
1141
    code = asyncSendMsgToServer(pExchangeInfo->pTransporter, &pSource->addr.epSet, &transporterId, pMsgSendInfo);
166,343,127✔
1142
    QUERY_CHECK_CODE(code, lino, _end);
166,346,161!
1143
    int64_t* pRpcHandle = taosArrayGet(pExchangeInfo->pFetchRpcHandles, sourceIndex);
166,346,161✔
1144
    *pRpcHandle = transporterId;
166,345,872✔
1145
  }
1146

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

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

1162
int32_t extractDataBlockFromFetchRsp(SSDataBlock* pRes, char* pData, SArray* pColList, char** pNextStart) {
332,317,675✔
1163
  int32_t      code = TSDB_CODE_SUCCESS;
332,317,675✔
1164
  int32_t      lino = 0;
332,317,675✔
1165
  SSDataBlock* pBlock = NULL;
332,317,675✔
1166
  if (pColList == NULL) {  // data from other sources
332,318,771✔
1167
    blockDataCleanup(pRes);
327,441,824✔
1168
    code = blockDecodeInternal(pRes, pData, (const char**)pNextStart);
327,441,490✔
1169
    if (code) {
327,442,022!
1170
      return code;
×
1171
    }
1172
  } else {  // extract data according to pColList
1173
    char* pStart = pData;
4,876,947✔
1174

1175
    int32_t numOfCols = htonl(*(int32_t*)pStart);
4,876,947✔
1176
    pStart += sizeof(int32_t);
4,877,285✔
1177

1178
    // todo refactor:extract method
1179
    SSysTableSchema* pSchema = (SSysTableSchema*)pStart;
4,877,285✔
1180
    for (int32_t i = 0; i < numOfCols; ++i) {
70,308,168✔
1181
      SSysTableSchema* p = (SSysTableSchema*)pStart;
65,430,883✔
1182

1183
      p->colId = htons(p->colId);
65,430,883✔
1184
      p->bytes = htonl(p->bytes);
65,430,883✔
1185
      pStart += sizeof(SSysTableSchema);
65,430,883✔
1186
    }
1187

1188
    pBlock = NULL;
4,877,285✔
1189
    code = createDataBlock(&pBlock);
4,877,285✔
1190
    QUERY_CHECK_CODE(code, lino, _end);
4,877,285!
1191

1192
    for (int32_t i = 0; i < numOfCols; ++i) {
70,308,168✔
1193
      SColumnInfoData idata = createColumnInfoData(pSchema[i].type, pSchema[i].bytes, pSchema[i].colId);
65,430,883✔
1194
      code = blockDataAppendColInfo(pBlock, &idata);
65,430,883✔
1195
      QUERY_CHECK_CODE(code, lino, _end);
65,430,883!
1196
    }
1197

1198
    code = blockDecodeInternal(pBlock, pStart, NULL);
4,877,285✔
1199
    QUERY_CHECK_CODE(code, lino, _end);
4,877,285!
1200

1201
    code = blockDataEnsureCapacity(pRes, pBlock->info.rows);
4,877,285✔
1202
    QUERY_CHECK_CODE(code, lino, _end);
4,877,285!
1203

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

1211
    blockDataDestroy(pBlock);
4,877,285✔
1212
    pBlock = NULL;
4,877,285✔
1213
  }
1214

1215
_end:
332,319,307✔
1216
  if (code != TSDB_CODE_SUCCESS) {
332,318,219!
1217
    blockDataDestroy(pBlock);
×
1218
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1219
  }
1220
  return code;
332,318,763✔
1221
}
1222

1223
void setAllSourcesCompleted(SOperatorInfo* pOperator) {
66,600,664✔
1224
  SExchangeInfo* pExchangeInfo = pOperator->info;
66,600,664✔
1225
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
66,600,664✔
1226

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

1233
  setOperatorCompleted(pOperator);
66,600,664✔
1234
}
66,599,549✔
1235

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

1241
  int32_t completed = 0;
209,002,799✔
1242
  for (int32_t k = 0; k < total; ++k) {
570,319,054✔
1243
    SSourceDataInfo* p = taosArrayGet(pArray, k);
361,317,011✔
1244
    QUERY_CHECK_NULL(p, code, lino, _end, terrno);
361,318,642!
1245
    if (p->status == EX_SOURCE_DATA_EXHAUSTED) {
361,318,642✔
1246
      qDebug("source %d is completed, info:%p %p", k, pArray, p);
169,656,344✔
1247
      completed += 1;
169,656,344✔
1248
    }
1249
  }
1250

1251
  *pRes = completed;
209,002,043✔
1252
_end:
209,003,048✔
1253
  if (code != TSDB_CODE_SUCCESS) {
209,003,048!
1254
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1255
  }
1256
  return code;
209,002,437✔
1257
}
1258

1259
int32_t prepareConcurrentlyLoad(SOperatorInfo* pOperator) {
69,378,187✔
1260
  SExchangeInfo* pExchangeInfo = pOperator->info;
69,378,187✔
1261
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
69,378,187✔
1262

1263
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSourceDataInfo);
69,377,706✔
1264
  int64_t startTs = taosGetTimestampUs();
69,376,526✔
1265

1266
  // Asynchronously send all fetch requests to all sources.
1267
  for (int32_t i = 0; i < totalSources; ++i) {
174,082,015✔
1268
    int32_t code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, i);
104,703,971✔
1269
    if (code != TSDB_CODE_SUCCESS) {
104,705,769✔
1270
      pTaskInfo->code = code;
280✔
1271
      return code;
×
1272
    }
1273
  }
1274

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

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

1285
  return TSDB_CODE_SUCCESS;
69,378,187✔
1286
}
1287

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

1294
  char* pNextStart = pRetrieveRsp->data;
119,350,285✔
1295
  char* pStart = pNextStart;
119,349,865✔
1296

1297
  int32_t index = 0;
119,349,865✔
1298

1299
  if (pRetrieveRsp->compressed) {  // decompress the data
119,349,865!
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) {
446,794,874✔
1317
    pStart = pNextStart;
327,444,923✔
1318

1319
    if (taosArrayGetSize(pExchangeInfo->pRecycledBlocks) > 0) {
327,444,923✔
1320
      pb = *(SSDataBlock**)taosArrayPop(pExchangeInfo->pRecycledBlocks);
168,049,396✔
1321
      blockDataCleanup(pb);
168,049,396✔
1322
    } else {
1323
      code = createOneDataBlock(pExchangeInfo->pDummyBlock, false, &pb);
159,395,906✔
1324
      QUERY_CHECK_NULL(pb, code, lino, _end, code);
159,392,630!
1325
    }
1326

1327
    int32_t compLen = *(int32_t*)pStart;
327,442,026✔
1328
    pStart += sizeof(int32_t);
327,443,118✔
1329

1330
    int32_t rawLen = *(int32_t*)pStart;
327,444,206✔
1331
    pStart += sizeof(int32_t);
327,444,206✔
1332
    QUERY_CHECK_CONDITION((compLen <= rawLen && compLen != 0), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
327,443,662!
1333

1334
    pNextStart = pStart + compLen;
327,443,662✔
1335
    if (pRetrieveRsp->compressed && (compLen < rawLen)) {
327,442,022!
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);
327,443,114✔
1342
    if (code != 0) {
327,442,026!
1343
      taosMemoryFreeClear(pDataInfo->pRsp);
×
1344
      goto _end;
×
1345
    }
1346

1347
    void* tmp = taosArrayPush(pExchangeInfo->pResultBlockList, &pb);
327,442,026✔
1348
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
327,444,765!
1349
    qDebug("%dth block added to resultBlockList, rows:%" PRId64, index, pb->info.rows);
327,444,765✔
1350
    pb = NULL;
327,445,302✔
1351
  }
1352

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

1361
int32_t seqLoadRemoteData(SOperatorInfo* pOperator) {
32,454,493✔
1362
  SExchangeInfo* pExchangeInfo = pOperator->info;
32,454,493✔
1363
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
32,455,014✔
1364

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

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

1381
  while (1) {
7,540,975✔
1382
    if (pExchangeInfo->current >= totalSources) {
39,995,989✔
1383
      setAllSourcesCompleted(pOperator);
7,533,257✔
1384
      return TSDB_CODE_SUCCESS;
7,533,257✔
1385
    }
1386

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

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

1407
    code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, pExchangeInfo->current);
32,427,386✔
1408
    if (code != TSDB_CODE_SUCCESS) {
32,427,386!
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) {
654✔
1415
      code = exchangeWait(pOperator, pExchangeInfo);
32,428,040✔
1416
      if (code != TSDB_CODE_SUCCESS || isTaskKilled(pTaskInfo)) {
32,428,040!
1417
        T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
654!
1418
      }
1419

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

1427
      break;
32,426,732✔
1428
    }
1429

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

1441
    if (pRsp->numOfRows == 0) {
32,426,732✔
1442
      qDebug("exhausted %p,%s vgId:%d, clientId:0x%" PRIx64 " taskID:0x%" PRIx64
7,505,629✔
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,505,629✔
1448
      if (pDataInfo->isVtbRefScan || pDataInfo->isVtbTagScan) {
7,505,629!
1449
        pExchangeInfo->current = totalSources;
7,453,954✔
1450
      } else {
1451
        pExchangeInfo->current += 1;
51,675✔
1452
      }
1453
      taosMemoryFreeClear(pDataInfo->pRsp);
7,505,629!
1454
      continue;
7,505,629✔
1455
    }
1456

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

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

1488
    taosMemoryFreeClear(pDataInfo->pRsp);
24,921,103!
1489
    return TSDB_CODE_SUCCESS;
24,921,103✔
1490
  }
1491

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

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

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

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

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

1559
    dataInfo.isVtbRefScan = pBasicParam->isVtbRefScan;
30,265,378!
1560
    dataInfo.isVtbTagScan = pBasicParam->isVtbTagScan;
30,265,378!
1561
    dataInfo.srcOpType = pBasicParam->srcOpType;
30,265,378✔
1562
    dataInfo.tableSeq = pBasicParam->tableSeq;
30,265,378!
1563

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

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

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

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

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

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

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

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

1655
  return TSDB_CODE_SUCCESS;
32,854,501✔
1656
}
1657

1658
int32_t addDynamicExchangeSource(SOperatorInfo* pOperator) {
32,701,582✔
1659
  SExchangeInfo*               pExchangeInfo = pOperator->info;
32,701,582✔
1660
  int32_t                      code = TSDB_CODE_SUCCESS;
32,701,582✔
1661
  SExchangeOperatorBasicParam* pBasicParam = NULL;
32,701,582✔
1662
  SExchangeOperatorParam*      pParam = (SExchangeOperatorParam*)pOperator->pOperatorGetParam->value;
32,701,582✔
1663
  if (pParam->multiParams) {
32,701,582!
1664
    SExchangeOperatorBatchParam* pBatch = (SExchangeOperatorBatchParam*)pOperator->pOperatorGetParam->value;
189,570✔
1665
    int32_t                      iter = 0;
189,570✔
1666
    while (NULL != (pBasicParam = tSimpleHashIterate(pBatch->pBatchs, pBasicParam, &iter))) {
532,059✔
1667
      code = addSingleExchangeSource(pOperator, pBasicParam);
342,489✔
1668
      if (code) {
342,489!
1669
        return code;
×
1670
      }
1671
    }
1672
  } else {
1673
    pBasicParam = &pParam->basic;
32,512,012✔
1674
    code = addSingleExchangeSource(pOperator, pBasicParam);
32,512,012✔
1675
  }
1676

1677
  freeOperatorParam(pOperator->pOperatorGetParam, OP_GET_PARAM);
32,701,582✔
1678
  pOperator->pOperatorGetParam = NULL;
32,701,582✔
1679

1680
  return code;
32,701,582✔
1681
}
1682

1683
int32_t prepareLoadRemoteData(SOperatorInfo* pOperator) {
432,985,902✔
1684
  SExchangeInfo* pExchangeInfo = pOperator->info;
432,985,902✔
1685
  int32_t        code = TSDB_CODE_SUCCESS;
432,988,505✔
1686
  int32_t        lino = 0;
432,988,505✔
1687
  
1688
  if ((OPTR_IS_OPENED(pOperator) && !pExchangeInfo->dynamicOp) ||
432,988,505!
1689
      (pExchangeInfo->dynamicOp && NULL == pOperator->pOperatorGetParam)) {
115,199,210!
1690
    qDebug("skip prepare, opened:%d, dynamicOp:%d, getParam:%p", OPTR_IS_OPENED(pOperator), pExchangeInfo->dynamicOp, pOperator->pOperatorGetParam);
318,371,435!
1691
    return TSDB_CODE_SUCCESS;
318,371,435✔
1692
  }
1693

1694
  if (pExchangeInfo->dynamicOp) {
114,615,703!
1695
    code = addDynamicExchangeSource(pOperator);
32,701,582✔
1696
    QUERY_CHECK_CODE(code, lino, _end);
32,701,582!
1697
  }
1698

1699
  if (pOperator->status == OP_NOT_OPENED && (pExchangeInfo->dynamicOp && pExchangeInfo->seqLoadData) || IS_STREAM_MODE(pOperator->pTaskInfo)) {
114,615,234!
1700
    pExchangeInfo->current = 0;
43,511,959✔
1701
  }
1702

1703
  int64_t st = taosGetTimestampUs();
114,615,560✔
1704

1705
  if (!IS_STREAM_MODE(pOperator->pTaskInfo) && !pExchangeInfo->seqLoadData) {
114,615,560✔
1706
    code = prepareConcurrentlyLoad(pOperator);
69,375,668✔
1707
    QUERY_CHECK_CODE(code, lino, _end);
69,378,187!
1708
    pExchangeInfo->openedTs = taosGetTimestampUs();
69,378,187✔
1709
  }
1710

1711
  OPTR_SET_OPENED(pOperator);
114,618,585✔
1712
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
114,615,762✔
1713

1714
  qDebug("%s prepare load complete", pOperator->pTaskInfo->id.str);
114,616,370✔
1715

1716
_end:
8,162,137✔
1717
  if (code != TSDB_CODE_SUCCESS) {
114,614,174!
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;
114,614,174✔
1723
}
1724

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

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

1771
  bool limitReached = applyLimitOffset(pLimitInfo, pBlock, pTaskInfo);
2,737,354✔
1772
  if (pBlock->info.rows == 0) {
2,737,354✔
1773
    return PROJECT_RETRIEVE_CONTINUE;
1,353,454✔
1774
  } else {
1775
    if (limitReached && (pLimitInfo->slimit.limit > 0 && pLimitInfo->slimit.limit <= pLimitInfo->numOfOutputGroups)) {
1,383,900!
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,383,900!
1785
    return PROJECT_RETRIEVE_DONE;
1,383,900✔
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) {
164,180,926✔
1792
  SExecTaskInfo* pTask = pOperator->pTaskInfo;
164,180,926✔
1793
  int32_t        code = TSDB_CODE_SUCCESS;
164,181,755✔
1794
  if (pTask->pWorkerCb) {
164,181,755!
1795
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
164,182,311✔
1796
    if (code != TSDB_CODE_SUCCESS) {
164,181,484!
1797
      pTask->code = code;
×
1798
      return pTask->code;
×
1799
    }
1800
  }
1801

1802
  code = tsem_wait(&pExchangeInfo->ready);
164,180,919✔
1803
  if (code != TSDB_CODE_SUCCESS) {
164,182,031!
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) {
164,182,031!
1810
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
164,182,311✔
1811
    if (code != TSDB_CODE_SUCCESS) {
164,182,591!
1812
      pTask->code = code;
×
1813
      return pTask->code;
×
1814
    }
1815
  }
1816
  return TSDB_CODE_SUCCESS;
164,182,311✔
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