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

taosdata / TDengine / #5034

24 Apr 2026 11:25AM UTC coverage: 73.058%. Remained the same
#5034

push

travis-ci

web-flow
merge: from main to 3.0 branch #35224

merge: from main to 3.0 branch[manual-only]

1336 of 1975 new or added lines in 48 files covered. (67.65%)

14149 existing lines in 164 files now uncovered.

275896 of 377640 relevant lines covered (73.06%)

132944440.29 hits per line

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

73.46
/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,497,841✔
19
  if (pEncoder == NULL || pHandle == NULL) {
2,497,841✔
20
    return TSDB_CODE_INVALID_PARA;
×
21
  }
22
  int32_t code = 0;
2,497,841✔
23
  int32_t lino;
24

25
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
2,497,841✔
26
  TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->subKey));
4,996,356✔
27
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->fetchMeta));
4,995,662✔
28
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->consumerId));
4,995,998✔
29
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->snapshotVer));
4,996,692✔
30
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pHandle->epoch));
4,996,019✔
31
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->execHandle.subType));
4,996,019✔
32
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
2,498,178✔
33
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execCol.qmsg));
3,996,113✔
34
    TAOS_CHECK_EXIT(tEncodeSSchemaWrapper(pEncoder, &pHandle->execHandle.execCol.pSW));
3,995,765✔
35
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
499,410✔
36
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, 0));
411,782✔
37
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
87,628✔
38
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->execHandle.execTb.suid));
175,256✔
39
    if (pHandle->execHandle.execTb.qmsg != NULL) {
87,628✔
40
      TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execTb.qmsg));
175,256✔
41
    }
42
  }
43
  tEndEncode(pEncoder);
2,497,473✔
44
_exit:
2,496,329✔
45
  if (code) {
2,496,329✔
46
    return code;
×
47
  } else {
48
    return pEncoder->pos;
2,496,329✔
49
  }
50
}
51

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

59
  TAOS_CHECK_EXIT(tStartDecode(pDecoder));
72,060✔
60
  TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pHandle->subKey));
72,060✔
61
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pHandle->fetchMeta));
144,120✔
62
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->consumerId));
144,120✔
63
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->snapshotVer));
144,120✔
64
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pHandle->epoch));
144,120✔
65
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pHandle->execHandle.subType));
144,120✔
66
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
72,060✔
67
    TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pHandle->execHandle.execCol.qmsg));
95,176✔
68
    if (!tDecodeIsEnd(pDecoder)) {
47,588✔
69
      TAOS_CHECK_EXIT(tDecodeSSchemaWrapper(pDecoder, &pHandle->execHandle.execCol.pSW));
95,176✔
70
    }
71
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
24,472✔
72
    int32_t size = 0;
16,882✔
73
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &size));
16,882✔
74
    for (int32_t i = 0; i < size; i++) {
16,882✔
75
      int64_t tbUid = 0;
×
76
      TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &tbUid));
×
77
    }
78
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
7,590✔
79
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->execHandle.execTb.suid));
15,180✔
80
    if (!tDecodeIsEnd(pDecoder)) {
7,590✔
81
      TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pHandle->execHandle.execTb.qmsg));
15,180✔
82
    }
83
  }
84
  tEndDecode(pDecoder);
72,060✔
85

86
_exit:
72,060✔
87
  return code;
72,060✔
88
}
89

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

95
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
502,680✔
96
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
362,620✔
97
    taosMemoryFreeClear(pData->execHandle.execCol.pSW.pSchema);
362,292✔
98
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
140,388✔
99
    tqReaderClose(pData->execHandle.pTqReader);
111,887✔
100
    walCloseReader(pData->pWalReader);
112,187✔
101
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
28,201✔
102
    walCloseReader(pData->pWalReader);
28,201✔
103
    tqReaderClose(pData->execHandle.pTqReader);
28,201✔
104
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
28,201✔
105
    nodesDestroyNode(pData->execHandle.execTb.node);
28,201✔
106
  }
107
  if (pData->msg != NULL) {
502,680✔
108
    rpcFreeCont(pData->msg->pCont);
×
109
    taosMemoryFree(pData->msg);
×
110
    pData->msg = NULL;
×
111
  }
112
  if (pData->block != NULL) {
502,680✔
113
    blockDataDestroy(pData->block);
×
114
  }
115
  if (pData->pRef) {
502,680✔
116
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
490,016✔
117
  }
118
  taosHashCleanup(pData->tableCreateTimeHash);
502,380✔
119
}
120

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

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

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

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

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

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

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

178
  TQ_ERR_GO_TO_END(
1,502,942✔
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,502,764✔
181
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
1,503,153✔
182
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
1,501,413✔
183

184
  return 0;
1,501,170✔
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) {
627,924✔
194
  if (pTq == NULL || ttb == NULL || key == NULL) {
627,924✔
UNCOV
195
    return TSDB_CODE_INVALID_PARA;
×
196
  }
197
  int32_t code = TDB_CODE_SUCCESS;
627,924✔
198
  TXN*    txn = NULL;
627,924✔
199

200
  TQ_ERR_GO_TO_END(
627,924✔
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));
627,209✔
203
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
334,720✔
204
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
334,722✔
205

206
  return 0;
334,355✔
207

208
END:
291,451✔
209
  if (txn != NULL) {
292,471✔
210
    tdbAbort(pTq->pMetaDB, txn);
292,471✔
211
  }
212
  return code;
292,122✔
213
}
214

215
int32_t tqMetaGetOffset(STQ* pTq, const char* subkey, STqOffset** pOffset) {
18,431,571✔
216
  if (pTq == NULL || subkey == NULL || pOffset == NULL) {
18,431,571✔
UNCOV
217
    return TSDB_CODE_INVALID_PARA;
×
218
  }
219
  void* data = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
18,431,571✔
220
  if (data == NULL) {
18,431,571✔
221
    int vLen = 0;
4,821,382✔
222
    taosRLockLatch(&pTq->lock);
4,821,382✔
223
    if (tdbTbGet(pTq->pOffsetStore, subkey, strlen(subkey), &data, &vLen) < 0) {
4,821,382✔
224
      taosRUnLockLatch(&pTq->lock);
4,764,284✔
225
      tdbFree(data);
4,766,187✔
226
      return TSDB_CODE_OUT_OF_MEMORY;
4,758,825✔
227
    }
228
    taosRUnLockLatch(&pTq->lock);
53,190✔
229

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

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

243
    *pOffset = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
53,190✔
244
    if (*pOffset == NULL) {
53,190✔
245
      return TSDB_CODE_OUT_OF_MEMORY;
×
246
    }
247
  } else {
248
    *pOffset = data;
13,610,189✔
249
  }
250
  return 0;
13,663,379✔
251
}
252

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

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

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

275
  tEncoderInit(&encoder, buf, vlen);
1,249,257✔
276
  code = tqEncodeSTqHandle(&encoder, pHandle);
1,249,257✔
277
  if (code < 0) {
1,248,164✔
278
    goto END;
×
279
  }
280

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

283
END:
1,245,736✔
284
  tEncoderClear(&encoder);
1,245,736✔
285
  taosMemoryFree(buf);
1,247,240✔
286
  return code;
1,243,514✔
287
}
288

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

296
  SVnode* pVnode = pTq->pVnode;
490,316✔
297
  int32_t vgId = TD_VID(pVnode);
490,316✔
298

299
  handle->pRef = walOpenRef(pVnode->pWal);
490,316✔
300
  TQ_NULL_GO_TO_END(handle->pRef);
490,316✔
301

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

310
  initStorageAPI(&reader.api);
489,298✔
311

312
  if (handle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
489,979✔
313
    handle->execHandle.task = qCreateQueueExecTaskInfo(handle->execHandle.execCol.qmsg, &reader, vgId, handle->consumerId);
354,555✔
314
    TQ_NULL_GO_TO_END(handle->execHandle.task);
354,892✔
315
    void* scanner = NULL;
354,892✔
316
    qExtractTmqScanner(handle->execHandle.task, &scanner);
354,892✔
317
    TQ_NULL_GO_TO_END(scanner);
354,892✔
318
    handle->execHandle.pTqReader = qExtractReaderFromTmqScanner(scanner);
354,892✔
319
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
354,892✔
320
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
135,057✔
321
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
109,735✔
322
    TQ_NULL_GO_TO_END(handle->pWalReader);
109,735✔
323
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
109,735✔
324
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
109,735✔
325
    TQ_ERR_GO_TO_END(buildSnapContext(reader.vnode, reader.version, 0, handle->execHandle.subType, handle->fetchMeta,
109,735✔
326
                                      (SSnapContext**)(&reader.sContext)));
327
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, handle->consumerId);
109,735✔
328
    TQ_NULL_GO_TO_END(handle->execHandle.task);
109,735✔
329
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
25,689✔
330
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
25,689✔
331
    TQ_NULL_GO_TO_END(handle->pWalReader);
25,689✔
332
    if (handle->execHandle.execTb.qmsg != NULL && strcmp(handle->execHandle.execTb.qmsg, "") != 0) {
25,689✔
333
      if (nodesStringToNode(handle->execHandle.execTb.qmsg, &handle->execHandle.execTb.node) != 0) {
9,920✔
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,
25,689✔
339
                                      handle->execHandle.subType, handle->fetchMeta,
340
                                      (SSnapContext**)(&reader.sContext)));
341
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, handle->consumerId);
25,689✔
342
    TQ_NULL_GO_TO_END(handle->execHandle.task);
25,689✔
343
    TQ_ERR_GO_TO_END(qGetTableList(handle->execHandle.execTb.suid, pVnode, handle->execHandle.execTb.node, &tbUidList,
25,689✔
344
                                handle->execHandle.task));
345
    tqInfo("vgId:%d, tq try to get ctb for stb subscribe, suid:%" PRId64, pVnode->config.vgId,
25,689✔
346
           handle->execHandle.execTb.suid);
347
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
25,689✔
348
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
25,689✔
349
    TQ_ERR_GO_TO_END(tqReaderSetTbUidList(handle->execHandle.pTqReader, tbUidList, NULL));
25,689✔
350
  }
351
  handle->tableCreateTimeHash = (SHashObj*)taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
490,013✔
352

353
END:
490,282✔
354
  reader.api.snapshotFn.destroySnapshot(reader.sContext);
490,316✔
355
  taosArrayDestroy(tbUidList);
489,979✔
356
  return code;
490,316✔
357
}
358

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

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

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

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

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

387
  handle->execHandle.subType = req->subType;
430,948✔
388
  handle->fetchMeta = req->withMeta;
430,948✔
389
  if (req->subType == TOPIC_SUB_TYPE__COLUMN) {
430,948✔
390
    void* tmp = taosStrdup(req->qmsg);
315,032✔
391
    if (tmp == NULL) {
315,032✔
392
      return terrno;
×
393
    }
394
    handle->execHandle.execCol.qmsg = tmp;
315,032✔
395
    handle->execHandle.execCol.pSW = req->schema;
315,032✔
396
    req->schema.pSchema = NULL;
315,032✔
397
  } else if (req->subType == TOPIC_SUB_TYPE__TABLE) {
115,916✔
398
    handle->execHandle.execTb.suid = req->suid;
20,611✔
399
    void* tmp = taosStrdup(req->qmsg);
20,611✔
400
    if (tmp == NULL) {
20,611✔
401
      return terrno;
×
402
    }
403
    handle->execHandle.execTb.qmsg = tmp;
20,611✔
404
  }
405

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

408
  int32_t code = tqMetaInitHandle(pTq, handle);
430,948✔
409
  if (code != 0) {
430,785✔
410
    return code;
×
411
  }
412
  tqInfo("tqMetaCreateHandle %s consumer 0x%" PRIx64 " vgId:%d, snapshotVer:%" PRId64, handle->subKey,
430,785✔
413
         handle->consumerId, vgId, handle->snapshotVer);
414
  return taosHashPut(pTq->pHandle, handle->subKey, strlen(handle->subKey), handle, sizeof(STqHandle));
430,785✔
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) {
321,351,944✔
450
  if (pTq == NULL || key == NULL || pHandle == NULL) {
321,351,944✔
451
    return TSDB_CODE_INVALID_PARA;
×
452
  }
453
  void* data = taosHashGet(pTq->pHandle, key, strlen(key));
321,386,145✔
454
  if (data == NULL) {
321,343,205✔
455
    int vLen = 0;
487,416✔
456
    if (tdbTbGet(pTq->pExecStore, key, (int)strlen(key), &data, &vLen) < 0) {
487,416✔
457
      tdbFree(data);
427,670✔
458
      return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
427,670✔
459
    }
460
    STqHandle handle = {0};
59,368✔
461
    int32_t code = tqMetaRestoreHandle(pTq, data, vLen >= 0 ? vLen : 0, &handle);
59,368✔
462
    if (code != 0) {
59,368✔
463
      tdbFree(data);
×
464
      tqDestroySTqHandle(&handle);
×
465
      return code;
×
466
    }
467
    tdbFree(data);
59,368✔
468
    *pHandle = taosHashGet(pTq->pHandle, key, strlen(key));
59,368✔
469
    if (*pHandle == NULL) {
59,368✔
470
      return terrno;
×
471
    }
472
  } else {
473
    *pHandle = data;
320,855,789✔
474
  }
475
  return TDB_CODE_SUCCESS;
320,949,375✔
476
}
477

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

487
END:
4,500,160✔
488
  if (code != TDB_CODE_SUCCESS) {
4,500,160✔
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,500,160✔
501
}
502

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

515
  *data = fname;
13,499,614✔
516
  fname = NULL;
13,499,648✔
517

518
END:
13,499,648✔
519
  if (code != 0) {
13,499,648✔
520
    tqError("%s failed at %d since %s", __func__, lino, tstrerror(code));
×
521
  }
522
  taosMemoryFree(fname);
13,499,122✔
523
  return code;
13,499,761✔
524
}
525

526
static int32_t tqReplacePath(char** path) {
4,500,160✔
527
  if (path == NULL || *path == NULL) {
4,500,160✔
528
    return TSDB_CODE_INVALID_PARA;
×
529
  }
530
  char*   tpath = NULL;
4,500,160✔
531
  int32_t code = tqBuildFName(&tpath, *path, TQ_SUBSCRIBE_NAME);
4,500,160✔
532
  if (code != 0) {
4,499,967✔
533
    return code;
×
534
  }
535
  taosMemoryFree(*path);
4,499,967✔
536
  *path = tpath;
4,499,369✔
537
  return TDB_CODE_SUCCESS;
4,499,562✔
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,498,517✔
579
  if (pTq == NULL) {
4,498,517✔
580
    return TSDB_CODE_INVALID_PARA;
×
581
  }
582
  char*   maindb = NULL;
4,498,517✔
583
  char*   offsetNew = NULL;
4,500,160✔
584
  int32_t code = TDB_CODE_SUCCESS;
4,500,160✔
585
  TQ_ERR_GO_TO_END(tqBuildFName(&maindb, pTq->path, TDB_MAINDB_NAME));
4,500,160✔
586
  if (!taosCheckExistFile(maindb)) {
4,499,967✔
587
    TQ_ERR_GO_TO_END(tqReplacePath(&pTq->path));
4,500,160✔
588
    TQ_ERR_GO_TO_END(tqMetaOpenTdb(pTq));
4,499,562✔
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,500,160✔
595
  if (taosCheckExistFile(offsetNew)) {
4,499,827✔
596
    TQ_ERR_GO_TO_END(tqOffsetRestoreFromFile(pTq, offsetNew));
×
597
    TQ_ERR_GO_TO_END(taosRemoveFile(offsetNew));
×
598
  }
599

600
END:
4,500,160✔
601
  taosMemoryFree(maindb);
4,500,160✔
602
  taosMemoryFree(offsetNew);
4,500,160✔
603
  return code;
4,499,188✔
604
}
605

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