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

taosdata / TDengine / #3534

21 Nov 2024 07:36AM UTC coverage: 60.825% (+2.0%) from 58.848%
#3534

push

travis-ci

web-flow
Merge pull request #28810 from taosdata/ehn/add-sync-heartbeat-sent-time-to-log

ehn:add-sync-heartbeat-sent-time-to-log

120023 of 252376 branches covered (47.56%)

Branch coverage included in aggregate %.

43 of 47 new or added lines in 3 files covered. (91.49%)

2254 existing lines in 162 files now uncovered.

200876 of 275203 relevant lines covered (72.99%)

16110754.39 hits per line

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

79.15
/source/libs/executor/src/querytask.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 "filter.h"
17
#include "function.h"
18
#include "functionMgt.h"
19
#include "os.h"
20
#include "querynodes.h"
21
#include "tfill.h"
22
#include "tname.h"
23

24
#include "tdatablock.h"
25
#include "tmsg.h"
26

27
#include "executorInt.h"
28
#include "index.h"
29
#include "operator.h"
30
#include "query.h"
31
#include "querytask.h"
32
#include "storageapi.h"
33
#include "thash.h"
34
#include "ttypes.h"
35

36
#define CLEAR_QUERY_STATUS(q, st) ((q)->status &= (~(st)))
37

38
int32_t doCreateTask(uint64_t queryId, uint64_t taskId, int32_t vgId, EOPTR_EXEC_MODEL model, SStorageAPI* pAPI,
7,252,280✔
39
                     SExecTaskInfo** pTaskInfo) {
40
  if (pTaskInfo == NULL) {
7,252,280!
41
    return TSDB_CODE_SUCCESS;
×
42
  }
43

44
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
7,252,280✔
45
  if (p == NULL) {
7,252,740!
UNCOV
46
    return terrno;
×
47
  }
48

49
  setTaskStatus(p, TASK_NOT_COMPLETED);
7,252,740✔
50
  p->cost.created = taosGetTimestampUs();
7,257,719✔
51

52
  p->execModel = model;
7,257,719✔
53
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
7,257,719✔
54
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
7,259,922✔
55
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
7,253,541!
UNCOV
56
    doDestroyTask(p);
×
UNCOV
57
    return terrno;
×
58
  }
59

60
  p->storageAPI = *pAPI;
7,254,702✔
61
  taosInitRWLatch(&p->lock);
7,254,702✔
62

63
  p->id.vgId = vgId;
7,260,068✔
64
  p->id.queryId = queryId;
7,260,068✔
65
  p->id.taskId = taskId;
7,260,068✔
66
  p->id.str = taosMemoryMalloc(64);
7,260,068✔
67
  if (p->id.str == NULL) {
7,260,265!
UNCOV
68
    doDestroyTask(p);
×
UNCOV
69
    return terrno;
×
70
  }
71

72
  buildTaskId(taskId, queryId, p->id.str);
7,260,265✔
73
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
7,260,162✔
74
  if (p->id.str == NULL || p->schemaInfos == NULL) {
7,257,072!
UNCOV
75
    doDestroyTask(p);
×
UNCOV
76
    return terrno;
×
77
  }
78

79
  *pTaskInfo = p;
7,257,103✔
80
  return TSDB_CODE_SUCCESS;
7,257,103✔
81
}
82

83
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
36,130,385✔
84

85
void setTaskKilled(SExecTaskInfo* pTaskInfo, int32_t rspCode) {
7,633✔
86
  pTaskInfo->code = rspCode;
7,633✔
87
  (void)stopTableScanOperator(pTaskInfo->pRoot, pTaskInfo->id.str, &pTaskInfo->storageAPI);
7,633✔
88
}
7,633✔
89

90
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
22,884,238✔
91
  if (status == TASK_NOT_COMPLETED) {
22,884,238✔
92
    pTaskInfo->status = status;
7,254,636✔
93
  } else {
94
    // QUERY_NOT_COMPLETED is not compatible with any other status, so clear its position first
95
    CLEAR_QUERY_STATUS(pTaskInfo, TASK_NOT_COMPLETED);
15,629,602✔
96
    pTaskInfo->status |= status;
15,629,602✔
97
  }
98
}
22,884,238✔
99

100
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
7,251,270✔
101
                           int32_t vgId, char* sql, EOPTR_EXEC_MODEL model) {
102
  int32_t code = doCreateTask(pPlan->id.queryId, taskId, vgId, model, &pHandle->api, pTaskInfo);
7,251,270✔
103
  if (*pTaskInfo == NULL || code != 0) {
7,257,429!
UNCOV
104
    nodesDestroyNode((SNode*)pPlan);
×
UNCOV
105
    taosMemoryFree(sql);
×
UNCOV
106
    return code;
×
107
  }
108

109
  if (pHandle) {
7,258,025!
110
    if (pHandle->pStateBackend) {
7,258,224✔
111
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
7,213✔
112
    }
113
  }
114

115
  TSWAP((*pTaskInfo)->sql, sql);
7,258,025✔
116

117
  (*pTaskInfo)->pSubplan = pPlan;
7,258,025✔
118
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
7,258,025✔
119
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
7,258,025✔
120
                        pPlan->dbFName, &((*pTaskInfo)->pRoot));
7,258,025✔
121

122
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
7,249,454!
UNCOV
123
    doDestroyTask(*pTaskInfo);
×
124
    (*pTaskInfo) = NULL;
142✔
125
  }
126
  return code;
7,258,347✔
127
}
128

129
void cleanupQueriedTableScanInfo(void* p) {
5,069,394✔
130
  SSchemaInfo* pSchemaInfo = p;
5,069,394✔
131

132
  taosMemoryFreeClear(pSchemaInfo->dbname);
5,069,394!
133
  taosMemoryFreeClear(pSchemaInfo->tablename);
5,069,529!
134
  tDeleteSchemaWrapper(pSchemaInfo->sw);
5,069,577!
135
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
5,069,803!
136
}
5,069,784✔
137

138
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
5,065,163✔
139
                                   SExecTaskInfo* pTaskInfo) {
140
  SMetaReader mr = {0};
5,065,163✔
141
  if (pHandle == NULL) {
5,065,163!
142
    return TSDB_CODE_INVALID_PARA;
×
143
  }
144

145
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
5,065,163✔
146

147
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
5,065,163✔
148
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
5,069,238✔
149
  if (code != TSDB_CODE_SUCCESS) {
5,061,994✔
150
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
118!
151
           GET_TASKID(pTaskInfo));
152

153
    pAPI->metaReaderFn.clearReader(&mr);
118✔
154
    return code;
118✔
155
  }
156

157
  SSchemaInfo schemaInfo = {0};
5,061,876✔
158

159
  schemaInfo.tablename = taosStrdup(mr.me.name);
5,061,876✔
160
  schemaInfo.dbname = taosStrdup(dbName);
5,062,903✔
161
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
5,067,118!
162
    pAPI->metaReaderFn.clearReader(&mr);
6,748✔
UNCOV
163
    cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
164
    return terrno;
×
165
  }
166

167
  if (mr.me.type == TSDB_SUPER_TABLE) {
5,060,370✔
168
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
4,300,696✔
169
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
4,300,696✔
170
  } else if (mr.me.type == TSDB_CHILD_TABLE) {
766,212✔
171
    tDecoderClear(&mr.coder);
469,602✔
172

173
    tb_uid_t suid = mr.me.ctbEntry.suid;
469,638✔
174
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
469,638✔
175
    if (code != TSDB_CODE_SUCCESS) {
469,603!
UNCOV
176
      pAPI->metaReaderFn.clearReader(&mr);
×
UNCOV
177
      cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
178
      return code;
×
179
    }
180

181
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
469,640✔
182
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
469,640✔
183
  } else {
184
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
298,083✔
185
  }
186

187
  pAPI->metaReaderFn.clearReader(&mr);
5,068,419✔
188

189
  if (schemaInfo.sw == NULL) {
5,066,174!
UNCOV
190
    cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
191
    return terrno;
×
192
  }
193

194
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
5,066,174✔
195
  if (schemaInfo.qsw == NULL) {
5,060,532!
UNCOV
196
    cleanupQueriedTableScanInfo(&schemaInfo);
×
UNCOV
197
    return terrno;
×
198
  }
199

200
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
5,060,532✔
201
  if (p == NULL) {
5,062,497!
202
    cleanupQueriedTableScanInfo(&schemaInfo);
×
203
    return terrno;
×
204
  }
205

206
  return code;
5,062,497✔
207
}
208

209
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
5,061,101✔
210
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
5,061,101!
211
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
5,061,101✔
212

213
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
5,061,101✔
214
  if (pqSw == NULL) {
5,069,048!
UNCOV
215
    return NULL;
×
216
  }
217

218
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
5,069,048✔
219
  if (pqSw->pSchema == NULL) {
5,062,084!
UNCOV
220
    taosMemoryFree(pqSw);
×
UNCOV
221
    return NULL;
×
222
  }
223

224
  for (int32_t i = 0; i < numOfCols; ++i) {
31,129,824✔
225
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
26,096,374✔
226
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
26,067,185✔
227

228
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
26,067,185✔
229
    pSchema->colId = pColNode->colId;
26,067,185✔
230
    pSchema->type = pColNode->node.resType.type;
26,067,185✔
231
    pSchema->bytes = pColNode->node.resType.bytes;
26,067,185✔
232
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
26,067,185✔
233
  }
234

235
  // this the tags and pseudo function columns, we only keep the tag columns
236
  for (int32_t i = 0; i < numOfTags; ++i) {
12,147,981✔
237
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
7,122,142✔
238

239
    int32_t type = nodeType(pNode->pExpr);
7,114,531✔
240
    if (type == QUERY_NODE_COLUMN) {
7,114,531✔
241
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
6,350,448✔
242

243
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
6,350,448✔
244
      pSchema->colId = pColNode->colId;
6,350,448✔
245
      pSchema->type = pColNode->node.resType.type;
6,350,448✔
246
      pSchema->bytes = pColNode->node.resType.bytes;
6,350,448✔
247
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
6,350,448✔
248
    }
249
  }
250

251
  return pqSw;
5,025,839✔
252
}
253

254
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) {
7,262,426✔
255
  tDeleteSchemaWrapper(pStreamInfo->schema);
7,262,426✔
256
  tOffsetDestroy(&pStreamInfo->currentOffset);
7,262,516✔
257
}
7,262,257✔
258

259
static void freeBlock(void* pParam) {
9,961,762✔
260
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
9,961,762✔
261
  blockDataDestroy(pBlock);
9,961,762✔
262
}
9,962,258✔
263

264
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
7,259,013✔
265
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
7,259,013✔
266
  destroyOperator(pTaskInfo->pRoot);
7,259,014✔
267
  pTaskInfo->pRoot = NULL;
7,262,473✔
268

269
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
7,262,473✔
270
  cleanupStreamInfo(&pTaskInfo->streamInfo);
7,262,545✔
271

272
  if (!pTaskInfo->localFetch.localExec) {
7,262,176✔
273
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
7,190,834✔
274
    pTaskInfo->pSubplan = NULL;
7,191,093✔
275
  }
276

277
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
7,262,435✔
278
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
7,262,556✔
279
  taosMemoryFreeClear(pTaskInfo->sql);
7,262,581✔
280
  taosMemoryFreeClear(pTaskInfo->id.str);
7,262,606!
281
  taosMemoryFreeClear(pTaskInfo);
7,262,535✔
282
}
7,262,574✔
283

284
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst) {
7,331,718✔
285
  char* p = dst;
7,331,718✔
286

287
  int32_t offset = 6;
7,331,718✔
288
  memcpy(p, "TID:0x", offset);
7,331,718✔
289
  offset += tintToHex(taskId, &p[offset]);
7,331,718✔
290

291
  memcpy(&p[offset], " QID:0x", 7);
7,334,190✔
292
  offset += 7;
7,334,190✔
293
  offset += tintToHex(queryId, &p[offset]);
7,334,190✔
294

295
  p[offset] = 0;
7,333,351✔
296
}
7,333,351✔
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

© 2025 Coveralls, Inc