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

taosdata / TDengine / #5059

17 May 2026 01:15AM UTC coverage: 73.443% (+0.06%) from 73.387%
#5059

push

travis-ci

web-flow
feat (TDgpt): Dynamic Model Synchronization Enhancements (#35344)

* refactor: do some internal refactor.

* fix: fix multiprocess sync issue.

* feat: add dynamic anomaly detection and forecasting services

* fix: log error message for undeploying model in exception handling

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix: handle undeploy when model exists only on disk

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/286aafa0-c3ce-4c27-b803-2707571e9dc1

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: guard dynamic registry concurrent access

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: tighten service list locking scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: restore prophet support and update tests per review feedback

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: improve test name and move copy inside lock scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* Potential fix for pull request finding

Co-au... (continued)

281870 of 383795 relevant lines covered (73.44%)

135516561.93 hits per line

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

73.22
/source/dnode/vnode/src/tq/tqMeta.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
#include "tdbInt.h"
16
#include "tq.h"
17

18
int32_t tqEncodeSTqHandle(SEncoder* pEncoder, const STqHandle* pHandle) {
2,799,098✔
19
  if (pEncoder == NULL || pHandle == NULL) {
2,799,098✔
20
    return TSDB_CODE_INVALID_PARA;
×
21
  }
22
  int32_t code = 0;
2,799,859✔
23
  int32_t lino;
24

25
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
2,799,859✔
26
  TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->subKey));
5,599,990✔
27
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->fetchMeta));
5,599,208✔
28
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->consumerId));
5,599,586✔
29
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->snapshotVer));
5,599,198✔
30
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pHandle->epoch));
5,598,028✔
31
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->execHandle.subType));
5,598,820✔
32
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
2,799,806✔
33
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execCol.qmsg));
4,538,033✔
34
    TAOS_CHECK_EXIT(tEncodeSSchemaWrapper(pEncoder, &pHandle->execHandle.execCol.pSW));
4,538,411✔
35
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
530,188✔
36
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, 0));
434,368✔
37
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
96,224✔
38
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->execHandle.execTb.suid));
192,076✔
39
    if (pHandle->execHandle.execTb.qmsg != NULL) {
96,224✔
40
      TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execTb.qmsg));
192,051✔
41
    }
42
  }
43
  tEndEncode(pEncoder);
2,800,953✔
44
_exit:
2,797,436✔
45
  if (code) {
2,797,436✔
46
    return code;
×
47
  } else {
48
    return pEncoder->pos;
2,797,436✔
49
  }
50
}
51

52
int32_t tqDecodeSTqHandle(SDecoder* pDecoder, STqHandle* pHandle) {
81,863✔
53
  if (pDecoder == NULL || pHandle == NULL) {
81,863✔
54
    return TSDB_CODE_INVALID_PARA;
×
55
  }
56
  int32_t code = 0;
81,863✔
57
  int32_t lino;
58

59
  TAOS_CHECK_EXIT(tStartDecode(pDecoder));
81,863✔
60
  TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pHandle->subKey));
81,863✔
61
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pHandle->fetchMeta));
163,726✔
62
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->consumerId));
163,726✔
63
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->snapshotVer));
163,726✔
64
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pHandle->epoch));
163,726✔
65
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pHandle->execHandle.subType));
163,726✔
66
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
81,863✔
67
    TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pHandle->execHandle.execCol.qmsg));
109,426✔
68
    if (!tDecodeIsEnd(pDecoder)) {
54,713✔
69
      TAOS_CHECK_EXIT(tDecodeSSchemaWrapper(pDecoder, &pHandle->execHandle.execCol.pSW));
109,426✔
70
    }
71
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
27,150✔
72
    int32_t size = 0;
19,172✔
73
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &size));
19,172✔
74
    for (int32_t i = 0; i < size; i++) {
19,172✔
75
      int64_t tbUid = 0;
×
76
      TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &tbUid));
×
77
    }
78
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
7,978✔
79
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->execHandle.execTb.suid));
15,956✔
80
    if (!tDecodeIsEnd(pDecoder)) {
7,978✔
81
      TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pHandle->execHandle.execTb.qmsg));
15,956✔
82
    }
83
  }
84
  tEndDecode(pDecoder);
81,863✔
85

86
_exit:
81,863✔
87
  return code;
81,863✔
88
}
89

90
void tqDestroySTqHandle(void* data) {
551,555✔
91
  if (data == NULL) return;
551,555✔
92
  STqHandle* pData = (STqHandle*)data;
551,555✔
93
  qDestroyTask(pData->execHandle.task);
551,555✔
94

95
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
549,278✔
96
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
398,452✔
97
    taosMemoryFreeClear(pData->execHandle.execCol.pSW.pSchema);
397,413✔
98
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
152,343✔
99
    tqReaderClose(pData->execHandle.pTqReader);
122,096✔
100
    walCloseReader(pData->pWalReader);
122,096✔
101
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
30,267✔
102
    walCloseReader(pData->pWalReader);
30,267✔
103
    tqReaderClose(pData->execHandle.pTqReader);
30,612✔
104
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
30,612✔
105
    nodesDestroyNode(pData->execHandle.execTb.node);
30,612✔
106
  }
107
  if (pData->msg != NULL) {
549,748✔
108
    rpcFreeCont(pData->msg->pCont);
×
109
    taosMemoryFree(pData->msg);
×
110
    pData->msg = NULL;
×
111
  }
112
  if (pData->block != NULL) {
551,223✔
113
    blockDataDestroy(pData->block);
×
114
  }
115
  if (pData->pRef) {
551,160✔
116
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
536,321✔
117
  }
118
  taosHashCleanup(pData->tableCreateTimeHash);
550,426✔
119
}
120

121
int32_t tqMetaDecodeOffsetInfo(STqOffset* info, void* pVal, uint32_t vLen) {
80,275✔
122
  if (info == NULL || pVal == NULL) {
80,275✔
123
    return TSDB_CODE_INVALID_PARA;
×
124
  }
125
  SDecoder decoder = {0};
80,275✔
126
  tDecoderInit(&decoder, (uint8_t*)pVal, vLen);
80,275✔
127
  int32_t code = tDecodeSTqOffset(&decoder, info);
80,275✔
128
  tDecoderClear(&decoder);
80,275✔
129

130
  if (code != 0) {
80,275✔
131
    tDeleteSTqOffset(info);
×
132
    return TSDB_CODE_OUT_OF_MEMORY;
×
133
  }
134
  return code;
80,275✔
135
}
136

137
int32_t tqMetaSaveOffset(STQ* pTq, STqOffset* pOffset) {
272,361✔
138
  if (pTq == NULL || pOffset == NULL) {
272,361✔
139
    return TSDB_CODE_INVALID_PARA;
×
140
  }
141
  void*    buf = NULL;
272,361✔
142
  int32_t  code = TDB_CODE_SUCCESS;
272,361✔
143
  uint32_t  vlen;
144
  SEncoder encoder = {0};
272,361✔
145
  tEncodeSize(tEncodeSTqOffset, pOffset, vlen, code);
272,361✔
146
  if (code < 0) {
272,361✔
147
    goto END;
×
148
  }
149

150
  buf = taosMemoryCalloc(1, vlen);
272,361✔
151
  if (buf == NULL) {
272,361✔
152
    code = terrno;
×
153
    goto END;
×
154
  }
155

156
  tEncoderInit(&encoder, buf, vlen);
272,361✔
157
  code = tEncodeSTqOffset(&encoder, pOffset);
272,361✔
158
  if (code < 0) {
272,361✔
159
    goto END;
×
160
  }
161

162
  taosWLockLatch(&pTq->lock);
272,361✔
163
  code = tqMetaSaveInfo(pTq, pTq->pOffsetStore, pOffset->subKey, strlen(pOffset->subKey), buf, vlen);
272,361✔
164
  taosWUnLockLatch(&pTq->lock);
272,361✔
165
END:
272,361✔
166
  tEncoderClear(&encoder);
272,361✔
167
  taosMemoryFree(buf);
272,361✔
168
  return code;
271,165✔
169
}
170

171
int32_t tqMetaSaveInfo(STQ* pTq, TTB* ttb, const void* key, uint32_t kLen, const void* value, uint32_t vLen) {
1,699,992✔
172
  if (pTq == NULL || ttb == NULL || key == NULL || value == NULL) {
1,699,992✔
173
    return TSDB_CODE_INVALID_PARA;
×
174
  }
175
  int32_t code = TDB_CODE_SUCCESS;
1,700,387✔
176
  TXN*    txn = NULL;
1,700,387✔
177

178
  TQ_ERR_GO_TO_END(
1,700,769✔
179
      tdbBegin(pTq->pMetaDB, &txn, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED));
180
  TQ_ERR_GO_TO_END(tdbTbUpsert(ttb, key, kLen, value, vLen, txn));
1,698,239✔
181
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
1,696,881✔
182
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
1,690,294✔
183

184
  return 0;
1,697,237✔
185

186
END:
×
187
  if (txn != NULL) {
×
188
    tdbAbort(pTq->pMetaDB, txn);
×
189
  }
190
  return code;
×
191
}
192

193
int32_t tqMetaDeleteInfo(STQ* pTq, TTB* ttb, const void* key, uint32_t kLen) {
693,966✔
194
  if (pTq == NULL || ttb == NULL || key == NULL) {
693,966✔
195
    return TSDB_CODE_INVALID_PARA;
×
196
  }
197
  int32_t code = TDB_CODE_SUCCESS;
694,706✔
198
  TXN*    txn = NULL;
694,706✔
199

200
  TQ_ERR_GO_TO_END(
693,966✔
201
      tdbBegin(pTq->pMetaDB, &txn, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED));
202
  TQ_ERR_GO_TO_END(tdbTbDelete(ttb, key, kLen, txn));
690,144✔
203
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
367,230✔
204
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
365,257✔
205

206
  return 0;
366,277✔
207

208
END:
321,121✔
209
  if (txn != NULL) {
323,060✔
210
    tdbAbort(pTq->pMetaDB, txn);
322,264✔
211
  }
212
  return code;
323,670✔
213
}
214

215
int32_t tqMetaGetOffset(STQ* pTq, const char* subkey, STqOffset** pOffset) {
17,353,928✔
216
  if (pTq == NULL || subkey == NULL || pOffset == NULL) {
17,353,928✔
217
    return TSDB_CODE_INVALID_PARA;
×
218
  }
219
  void* data = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
17,353,928✔
220
  if (data == NULL) {
17,353,571✔
221
    int vLen = 0;
4,446,847✔
222
    taosRLockLatch(&pTq->lock);
4,446,847✔
223
    if (tdbTbGet(pTq->pOffsetStore, subkey, strlen(subkey), &data, &vLen) < 0) {
4,446,838✔
224
      taosRUnLockLatch(&pTq->lock);
4,375,322✔
225
      tdbFree(data);
4,378,522✔
226
      return TSDB_CODE_OUT_OF_MEMORY;
4,365,461✔
227
    }
228
    taosRUnLockLatch(&pTq->lock);
66,017✔
229

230
    STqOffset offset = {0};
66,017✔
231
    if (tqMetaDecodeOffsetInfo(&offset, data, vLen >= 0 ? vLen : 0) != TDB_CODE_SUCCESS) {
66,017✔
232
      tdbFree(data);
×
233
      return TSDB_CODE_OUT_OF_MEMORY;
×
234
    }
235

236
    if (taosHashPut(pTq->pOffset, subkey, strlen(subkey), &offset, sizeof(STqOffset)) != 0) {
66,017✔
237
      tDeleteSTqOffset(&offset);
×
238
      tdbFree(data);
×
239
      return terrno;
×
240
    }
241
    tdbFree(data);
66,017✔
242

243
    *pOffset = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
66,017✔
244
    if (*pOffset == NULL) {
66,017✔
245
      return TSDB_CODE_OUT_OF_MEMORY;
×
246
    }
247
  } else {
248
    *pOffset = data;
12,906,724✔
249
  }
250
  return 0;
12,972,741✔
251
}
252

253
int32_t tqMetaSaveHandle(STQ* pTq, const char* key, const STqHandle* pHandle) {
1,400,092✔
254
  if (pTq == NULL || key == NULL || pHandle == NULL) {
1,400,092✔
255
    return TSDB_CODE_INVALID_PARA;
×
256
  }
257
  int32_t  code = TDB_CODE_SUCCESS;
1,400,092✔
258
  uint32_t  vlen;
259
  void*    buf = NULL;
1,400,092✔
260
  SEncoder encoder = {0};
1,400,092✔
261
  tEncodeSize(tqEncodeSTqHandle, pHandle, vlen, code);
1,400,092✔
262
  if (code < 0) {
1,399,714✔
263
    goto END;
×
264
  }
265

266
  tqDebug("tq save %s(%d) handle consumer:0x%" PRIx64 " epoch:%d vgId:%d", pHandle->subKey,
1,399,714✔
267
          (int32_t)strlen(pHandle->subKey), pHandle->consumerId, pHandle->epoch, TD_VID(pTq->pVnode));
268

269
  buf = taosMemoryCalloc(1, vlen);
1,400,092✔
270
  if (buf == NULL) {
1,400,092✔
271
    code = terrno;
×
272
    goto END;
×
273
  }
274

275
  tEncoderInit(&encoder, buf, vlen);
1,400,092✔
276
  code = tqEncodeSTqHandle(&encoder, pHandle);
1,400,092✔
277
  if (code < 0) {
1,399,256✔
278
    goto END;
×
279
  }
280

281
  TQ_ERR_GO_TO_END(tqMetaSaveInfo(pTq, pTq->pExecStore, key, strlen(key), buf, vlen));
1,399,256✔
282

283
END:
1,393,851✔
284
  tEncoderClear(&encoder);
1,393,837✔
285
  taosMemoryFree(buf);
1,394,205✔
286
  return code;
1,385,748✔
287
}
288

289
static int tqMetaInitHandle(STQ* pTq, STqHandle* handle) {
537,008✔
290
  if (pTq == NULL || handle == NULL) {
537,008✔
291
    return TSDB_CODE_INVALID_PARA;
×
292
  }
293
  int32_t code = TDB_CODE_SUCCESS;
537,397✔
294
  SArray* tbUidList = NULL;
537,397✔
295

296
  SVnode* pVnode = pTq->pVnode;
537,397✔
297
  int32_t vgId = TD_VID(pVnode);
537,008✔
298

299
  handle->pRef = walOpenRef(pVnode->pWal);
537,008✔
300
  TQ_NULL_GO_TO_END(handle->pRef);
537,397✔
301

302
  SReadHandle reader = {0};
537,008✔
303
  reader = (SReadHandle){
537,008✔
304
      .vnode = pVnode,
305
      .initTableReader = true,
306
      .initTqReader = true,
307
      .version = handle->snapshotVer,
537,008✔
308
  };
309

310
  initStorageAPI(&reader.api);
537,008✔
311

312
  if (handle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
536,629✔
313
    handle->execHandle.task = qCreateQueueExecTaskInfo(handle->execHandle.execCol.qmsg, &reader, vgId, handle->consumerId);
390,240✔
314
    TQ_NULL_GO_TO_END(handle->execHandle.task);
390,240✔
315
    void* scanner = NULL;
390,240✔
316
    qExtractTmqScanner(handle->execHandle.task, &scanner);
390,240✔
317
    TQ_NULL_GO_TO_END(scanner);
390,240✔
318
    handle->execHandle.pTqReader = qExtractReaderFromTmqScanner(scanner);
390,240✔
319
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
390,240✔
320
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
146,832✔
321
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
119,301✔
322
    TQ_NULL_GO_TO_END(handle->pWalReader);
119,301✔
323
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
119,301✔
324
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
119,301✔
325
    TQ_ERR_GO_TO_END(buildSnapContext(reader.vnode, reader.version, 0, handle->execHandle.subType, handle->fetchMeta,
119,301✔
326
                                      (SSnapContext**)(&reader.sContext)));
327
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, handle->consumerId);
119,301✔
328
    TQ_NULL_GO_TO_END(handle->execHandle.task);
119,301✔
329
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
27,856✔
330
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
27,856✔
331
    TQ_NULL_GO_TO_END(handle->pWalReader);
27,856✔
332
    if (handle->execHandle.execTb.qmsg != NULL && strcmp(handle->execHandle.execTb.qmsg, "") != 0) {
27,856✔
333
      if (nodesStringToNode(handle->execHandle.execTb.qmsg, &handle->execHandle.execTb.node) != 0) {
10,911✔
334
        tqError("nodesStringToNode error in sub stable, since %s", terrstr());
×
335
        return TSDB_CODE_SCH_INTERNAL_ERROR;
×
336
      }
337
    }
338
    TQ_ERR_GO_TO_END(buildSnapContext(reader.vnode, reader.version, handle->execHandle.execTb.suid,
27,856✔
339
                                      handle->execHandle.subType, handle->fetchMeta,
340
                                      (SSnapContext**)(&reader.sContext)));
341
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, handle->consumerId);
27,856✔
342
    TQ_NULL_GO_TO_END(handle->execHandle.task);
27,856✔
343
    TQ_ERR_GO_TO_END(qGetTableList(handle->execHandle.execTb.suid, pVnode, handle->execHandle.execTb.node, &tbUidList,
27,856✔
344
                                handle->execHandle.task));
345
    tqInfo("vgId:%d, tq try to get ctb for stb subscribe, suid:%" PRId64, pVnode->config.vgId,
27,856✔
346
           handle->execHandle.execTb.suid);
347
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
27,856✔
348
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
27,856✔
349
    TQ_ERR_GO_TO_END(tqReaderSetTbUidList(handle->execHandle.pTqReader, tbUidList, NULL));
27,856✔
350
  }
351
  handle->tableCreateTimeHash = (SHashObj*)taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
537,397✔
352

353
END:
537,397✔
354
  reader.api.snapshotFn.destroySnapshot(reader.sContext);
537,397✔
355
  taosArrayDestroy(tbUidList);
537,397✔
356
  return code;
537,397✔
357
}
358

359
static int32_t tqMetaRestoreHandle(STQ* pTq, void* pVal, uint32_t vLen, STqHandle* handle) {
67,705✔
360
  if (pTq == NULL || pVal == NULL || handle == NULL) {
67,705✔
361
    return TSDB_CODE_INVALID_PARA;
×
362
  }
363
  int32_t  vgId = TD_VID(pTq->pVnode);
67,705✔
364
  SDecoder decoder = {0};
67,705✔
365
  int32_t  code = TDB_CODE_SUCCESS;
67,705✔
366

367
  tDecoderInit(&decoder, (uint8_t*)pVal, vLen);
67,705✔
368
  TQ_ERR_GO_TO_END(tqDecodeSTqHandle(&decoder, handle));
67,705✔
369
  TQ_ERR_GO_TO_END(tqMetaInitHandle(pTq, handle));
67,705✔
370
  tqInfo("tqMetaRestoreHandle %s consumer 0x%" PRIx64 " vgId:%d", handle->subKey, handle->consumerId, vgId);
67,705✔
371
  code = taosHashPut(pTq->pHandle, handle->subKey, strlen(handle->subKey), handle, sizeof(STqHandle));
67,705✔
372

373
END:
67,705✔
374
  tDecoderClear(&decoder);
67,705✔
375
  return code;
67,705✔
376
}
377

378
int32_t tqMetaCreateHandle(STQ* pTq, SMqRebVgReq* req, STqHandle* handle) {
469,692✔
379
  if (pTq == NULL || req == NULL || handle == NULL) {
469,692✔
380
    return TSDB_CODE_INVALID_PARA;
×
381
  }
382
  int32_t vgId = TD_VID(pTq->pVnode);
469,692✔
383

384
  (void)memcpy(handle->subKey, req->subKey, TSDB_SUBSCRIBE_KEY_LEN);
469,692✔
385
  handle->consumerId = req->newConsumerId;
469,692✔
386

387
  handle->execHandle.subType = req->subType;
469,692✔
388
  handle->fetchMeta = req->withMeta;
469,692✔
389
  if (req->subType == TOPIC_SUB_TYPE__COLUMN) {
469,295✔
390
    void* tmp = taosStrdup(req->qmsg);
344,134✔
391
    if (tmp == NULL) {
343,762✔
392
      return terrno;
×
393
    }
394
    handle->execHandle.execCol.qmsg = tmp;
343,762✔
395
    handle->execHandle.execCol.pSW = req->schema;
344,134✔
396
    req->schema.pSchema = NULL;
344,134✔
397
  } else if (req->subType == TOPIC_SUB_TYPE__TABLE) {
125,161✔
398
    handle->execHandle.execTb.suid = req->suid;
22,237✔
399
    void* tmp = taosStrdup(req->qmsg);
21,836✔
400
    if (tmp == NULL) {
22,634✔
401
      return terrno;
×
402
    }
403
    handle->execHandle.execTb.qmsg = tmp;
22,634✔
404
  }
405

406
  handle->snapshotVer = walGetCommittedVer(pTq->pVnode->pWal);
469,303✔
407

408
  int32_t code = tqMetaInitHandle(pTq, handle);
469,692✔
409
  if (code != 0) {
469,692✔
410
    return code;
×
411
  }
412
  tqInfo("tqMetaCreateHandle %s consumer 0x%" PRIx64 " vgId:%d, snapshotVer:%" PRId64, handle->subKey,
469,692✔
413
         handle->consumerId, vgId, handle->snapshotVer);
414
  return taosHashPut(pTq->pHandle, handle->subKey, strlen(handle->subKey), handle, sizeof(STqHandle));
469,692✔
415
}
416

417
static int32_t tqMetaTransformInfo(TDB* pMetaDB, TTB* pOld, TTB* pNew) {
×
418
  if (pMetaDB == NULL || pOld == NULL || pNew == NULL) {
×
419
    return TSDB_CODE_INVALID_PARA;
×
420
  }
421
  TBC*  pCur = NULL;
×
422
  void* pKey = NULL;
×
423
  int   kLen = 0;
×
424
  void* pVal = NULL;
×
425
  int   vLen = 0;
×
426
  TXN*  txn = NULL;
×
427

428
  int32_t code = TDB_CODE_SUCCESS;
×
429

430
  TQ_ERR_GO_TO_END(tdbTbcOpen(pOld, &pCur, NULL));
×
431
  TQ_ERR_GO_TO_END(
×
432
      tdbBegin(pMetaDB, &txn, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED));
433

434
  TQ_ERR_GO_TO_END(tdbTbcMoveToFirst(pCur));
×
435
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
×
436
    TQ_ERR_GO_TO_END(tdbTbUpsert(pNew, pKey, kLen, pVal, vLen, txn));
×
437
  }
438

439
  TQ_ERR_GO_TO_END(tdbCommit(pMetaDB, txn));
×
440
  TQ_ERR_GO_TO_END(tdbPostCommit(pMetaDB, txn));
×
441

442
END:
×
443
  tdbFree(pKey);
×
444
  tdbFree(pVal);
×
445
  tdbTbcClose(pCur);
×
446
  return code;
×
447
}
448

449
int32_t tqMetaGetHandle(STQ* pTq, const char* key, STqHandle** pHandle) {
301,099,309✔
450
  if (pTq == NULL || key == NULL || pHandle == NULL) {
301,099,309✔
451
    return TSDB_CODE_INVALID_PARA;
×
452
  }
453
  void* data = taosHashGet(pTq->pHandle, key, strlen(key));
301,165,564✔
454
  if (data == NULL) {
301,084,651✔
455
    int vLen = 0;
537,934✔
456
    if (tdbTbGet(pTq->pExecStore, key, (int)strlen(key), &data, &vLen) < 0) {
537,934✔
457
      tdbFree(data);
469,904✔
458
      return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
469,547✔
459
    }
460
    STqHandle handle = {0};
67,705✔
461
    int32_t code = tqMetaRestoreHandle(pTq, data, vLen >= 0 ? vLen : 0, &handle);
67,705✔
462
    if (code != 0) {
67,705✔
463
      tdbFree(data);
×
464
      tqDestroySTqHandle(&handle);
×
465
      return code;
×
466
    }
467
    tdbFree(data);
67,705✔
468
    *pHandle = taosHashGet(pTq->pHandle, key, strlen(key));
67,705✔
469
    if (*pHandle == NULL) {
67,705✔
470
      return terrno;
×
471
    }
472
  } else {
473
    *pHandle = data;
300,546,717✔
474
  }
475
  return TDB_CODE_SUCCESS;
300,678,217✔
476
}
477

478
int32_t tqMetaOpenTdb(STQ* pTq) {
4,900,513✔
479
  if (pTq == NULL) {
4,900,513✔
480
    return TSDB_CODE_INVALID_PARA;
×
481
  }
482
  int32_t code = TDB_CODE_SUCCESS;
4,900,513✔
483
  TQ_ERR_GO_TO_END(tdbOpen(pTq->path, 16 * 1024, 1, &pTq->pMetaDB, 0, NULL));
4,900,513✔
484
  TQ_ERR_GO_TO_END(tdbTbOpen("tq.db", -1, -1, NULL, pTq->pMetaDB, &pTq->pExecStore, 0));
4,899,772✔
485
  TQ_ERR_GO_TO_END(tdbTbOpen("tq.offset.db", -1, -1, NULL, pTq->pMetaDB, &pTq->pOffsetStore, 0));
4,899,987✔
486

487
END:
4,899,732✔
488
  if (code != TDB_CODE_SUCCESS) {
4,899,732✔
489
    if (pTq->pExecStore) {
×
490
      tdbTbClose(pTq->pExecStore);
×
491
      pTq->pExecStore = NULL;
×
492
    }
493
    if (pTq->pOffsetStore) {
×
494
      tdbTbClose(pTq->pOffsetStore);
×
495
      pTq->pOffsetStore = NULL;
×
496
    }
497
    tdbClose(pTq->pMetaDB);
×
498
    pTq->pMetaDB = NULL;
×
499
  }
500
  return code;
4,894,332✔
501
}
502

503
static int32_t tqBuildFName(char** data, const char* path, char* name) {
14,703,551✔
504
  int32_t code = 0;
14,703,551✔
505
  int32_t lino = 0;
14,703,551✔
506
  char*   fname = NULL;
14,703,551✔
507
  TSDB_CHECK_NULL(data, code, lino, END, TSDB_CODE_INVALID_MSG);
14,703,551✔
508
  TSDB_CHECK_NULL(path, code, lino, END, TSDB_CODE_INVALID_MSG);
14,703,551✔
509
  TSDB_CHECK_NULL(name, code, lino, END, TSDB_CODE_INVALID_MSG);
14,703,551✔
510
  int32_t len = strlen(path) + strlen(name) + 2;
14,703,551✔
511
  fname = taosMemoryCalloc(1, len);
14,703,551✔
512
  TSDB_CHECK_NULL(fname, code, lino, END, terrno);
14,700,082✔
513
  (void)snprintf(fname, len, "%s%s%s", path, TD_DIRSEP, name);
14,700,082✔
514

515
  *data = fname;
14,700,082✔
516
  fname = NULL;
14,702,674✔
517

518
END:
14,702,674✔
519
  if (code != 0) {
14,702,674✔
520
    tqError("%s failed at %d since %s", __func__, lino, tstrerror(code));
×
521
  }
522
  taosMemoryFree(fname);
14,700,429✔
523
  return code;
14,702,808✔
524
}
525

526
static int32_t tqReplacePath(char** path) {
4,901,906✔
527
  if (path == NULL || *path == NULL) {
4,901,906✔
528
    return TSDB_CODE_INVALID_PARA;
×
529
  }
530
  char*   tpath = NULL;
4,901,906✔
531
  int32_t code = tqBuildFName(&tpath, *path, TQ_SUBSCRIBE_NAME);
4,901,541✔
532
  if (code != 0) {
4,901,138✔
533
    return code;
×
534
  }
535
  taosMemoryFree(*path);
4,901,138✔
536
  *path = tpath;
4,901,137✔
537
  return TDB_CODE_SUCCESS;
4,901,502✔
538
}
539

540
static int32_t tqMetaTransform(STQ* pTq) {
×
541
  if (pTq == NULL) {
×
542
    return TSDB_CODE_INVALID_PARA;
×
543
  }
544
  int32_t code = TDB_CODE_SUCCESS;
×
545
  TDB*    pMetaDB = NULL;
×
546
  TTB*    pExecStore = NULL;
×
547
  char*   offsetNew = NULL;
×
548
  char*   offset = NULL;
×
549
  TQ_ERR_GO_TO_END(tqBuildFName(&offset, pTq->path, TQ_OFFSET_NAME));
×
550

551
  TQ_ERR_GO_TO_END(tdbOpen(pTq->path, 16 * 1024, 1, &pMetaDB, 0, NULL));
×
552
  TQ_ERR_GO_TO_END(tdbTbOpen("tq.db", -1, -1, NULL, pMetaDB, &pExecStore, 0));
×
553

554
  TQ_ERR_GO_TO_END(tqReplacePath(&pTq->path));
×
555
  TQ_ERR_GO_TO_END(tqMetaOpenTdb(pTq));
×
556

557
  TQ_ERR_GO_TO_END(tqMetaTransformInfo(pTq->pMetaDB, pExecStore, pTq->pExecStore));
×
558

559
  TQ_ERR_GO_TO_END(tqBuildFName(&offsetNew, pTq->path, TQ_OFFSET_NAME));
×
560

561
  if (taosCheckExistFile(offset)) {
×
562
    if (taosCopyFile(offset, offsetNew) < 0) {
×
563
      tqError("copy offset file error");
×
564
    } else {
565
      TQ_ERR_GO_TO_END(taosRemoveFile(offset));
×
566
    }
567
  }
568

569
END:
×
570
  taosMemoryFree(offset);
×
571
  taosMemoryFree(offsetNew);
×
572

573
  tdbTbClose(pExecStore);
×
574
  tdbClose(pMetaDB);
×
575
  return code;
×
576
}
577

578
int32_t tqMetaOpen(STQ* pTq) {
4,899,583✔
579
  if (pTq == NULL) {
4,899,583✔
580
    return TSDB_CODE_INVALID_PARA;
×
581
  }
582
  char*   maindb = NULL;
4,899,583✔
583
  char*   offsetNew = NULL;
4,901,906✔
584
  int32_t code = TDB_CODE_SUCCESS;
4,901,906✔
585
  TQ_ERR_GO_TO_END(tqBuildFName(&maindb, pTq->path, TDB_MAINDB_NAME));
4,901,906✔
586
  if (!taosCheckExistFile(maindb)) {
4,901,906✔
587
    TQ_ERR_GO_TO_END(tqReplacePath(&pTq->path));
4,901,906✔
588
    TQ_ERR_GO_TO_END(tqMetaOpenTdb(pTq));
4,901,281✔
589
  } else {
590
    TQ_ERR_GO_TO_END(tqMetaTransform(pTq));
×
591
    TQ_ERR_GO_TO_END(taosRemoveFile(maindb));
×
592
  }
593

594
  TQ_ERR_GO_TO_END(tqBuildFName(&offsetNew, pTq->path, TQ_OFFSET_NAME));
4,898,821✔
595
  if (taosCheckExistFile(offsetNew)) {
4,900,578✔
596
    TQ_ERR_GO_TO_END(tqOffsetRestoreFromFile(pTq, offsetNew));
×
597
    TQ_ERR_GO_TO_END(taosRemoveFile(offsetNew));
×
598
  }
599

600
END:
4,901,092✔
601
  taosMemoryFree(maindb);
4,900,955✔
602
  taosMemoryFree(offsetNew);
4,899,666✔
603
  return code;
4,897,835✔
604
}
605

606
void tqMetaClose(STQ* pTq) {
4,902,006✔
607
  if (pTq == NULL) {
4,902,006✔
608
    return;
×
609
  }
610
  int32_t ret = 0;
4,902,006✔
611
  if (pTq->pExecStore) {
4,902,006✔
612
    tdbTbClose(pTq->pExecStore);
4,901,906✔
613
  }
614
  if (pTq->pOffsetStore) {
4,902,006✔
615
    tdbTbClose(pTq->pOffsetStore);
4,902,006✔
616
  }
617
  tdbClose(pTq->pMetaDB);
4,901,597✔
618
}
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