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

taosdata / TDengine / #5050

12 May 2026 05:36AM UTC coverage: 73.398% (+0.09%) from 73.313%
#5050

push

travis-ci

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

90 of 101 new or added lines in 2 files covered. (89.11%)

489 existing lines in 125 files now uncovered.

281602 of 383662 relevant lines covered (73.4%)

138099127.08 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,781,726✔
19
  if (pEncoder == NULL || pHandle == NULL) {
2,781,726✔
20
    return TSDB_CODE_INVALID_PARA;
×
21
  }
22
  int32_t code = 0;
2,782,630✔
23
  int32_t lino;
24

25
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
2,782,630✔
26
  TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->subKey));
5,564,044✔
27
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->fetchMeta));
5,563,305✔
28
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->consumerId));
5,562,970✔
29
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->snapshotVer));
5,561,811✔
30
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pHandle->epoch));
5,560,491✔
31
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->execHandle.subType));
5,561,205✔
32
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
2,781,040✔
33
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execCol.qmsg));
4,429,428✔
34
    TAOS_CHECK_EXIT(tEncodeSSchemaWrapper(pEncoder, &pHandle->execHandle.execCol.pSW));
4,429,840✔
35
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
565,998✔
36
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, 0));
471,132✔
37
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
95,288✔
38
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->execHandle.execTb.suid));
190,181✔
39
    if (pHandle->execHandle.execTb.qmsg != NULL) {
94,893✔
40
      TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execTb.qmsg));
189,794✔
41
    }
42
  }
43
  tEndEncode(pEncoder);
2,782,297✔
44
_exit:
2,773,188✔
45
  if (code) {
2,773,188✔
46
    return code;
×
47
  } else {
48
    return pEncoder->pos;
2,773,188✔
49
  }
50
}
51

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

59
  TAOS_CHECK_EXIT(tStartDecode(pDecoder));
79,961✔
60
  TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pHandle->subKey));
79,961✔
61
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pHandle->fetchMeta));
159,922✔
62
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->consumerId));
159,922✔
63
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->snapshotVer));
159,922✔
64
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pHandle->epoch));
159,922✔
65
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pHandle->execHandle.subType));
159,922✔
66
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
79,961✔
67
    TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pHandle->execHandle.execCol.qmsg));
104,870✔
68
    if (!tDecodeIsEnd(pDecoder)) {
52,435✔
69
      TAOS_CHECK_EXIT(tDecodeSSchemaWrapper(pDecoder, &pHandle->execHandle.execCol.pSW));
104,870✔
70
    }
71
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
27,526✔
72
    int32_t size = 0;
19,305✔
73
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &size));
19,305✔
74
    for (int32_t i = 0; i < size; i++) {
19,305✔
75
      int64_t tbUid = 0;
×
76
      TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &tbUid));
×
77
    }
78
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
8,221✔
79
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->execHandle.execTb.suid));
16,442✔
80
    if (!tDecodeIsEnd(pDecoder)) {
8,221✔
81
      TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pHandle->execHandle.execTb.qmsg));
16,442✔
82
    }
83
  }
84
  tEndDecode(pDecoder);
79,961✔
85

86
_exit:
79,961✔
87
  return code;
79,961✔
88
}
89

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

95
  if (pData->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
547,572✔
96
    taosMemoryFreeClear(pData->execHandle.execCol.qmsg);
394,530✔
97
    taosMemoryFreeClear(pData->execHandle.execCol.pSW.pSchema);
394,598✔
98
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__DB) {
153,015✔
99
    tqReaderClose(pData->execHandle.pTqReader);
122,372✔
100
    walCloseReader(pData->pWalReader);
122,372✔
101
  } else if (pData->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
30,643✔
102
    walCloseReader(pData->pWalReader);
30,643✔
103
    tqReaderClose(pData->execHandle.pTqReader);
30,643✔
104
    taosMemoryFreeClear(pData->execHandle.execTb.qmsg);
30,643✔
105
    nodesDestroyNode(pData->execHandle.execTb.node);
30,643✔
106
  }
107
  if (pData->msg != NULL) {
545,014✔
108
    rpcFreeCont(pData->msg->pCont);
×
109
    taosMemoryFree(pData->msg);
×
110
    pData->msg = NULL;
×
111
  }
112
  if (pData->block != NULL) {
546,471✔
113
    blockDataDestroy(pData->block);
×
114
  }
115
  if (pData->pRef) {
547,158✔
116
    walCloseRef(pData->pRef->pWal, pData->pRef->refId);
532,835✔
117
  }
118
  taosHashCleanup(pData->tableCreateTimeHash);
549,060✔
119
}
120

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

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

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

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

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

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

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

178
  TQ_ERR_GO_TO_END(
1,679,681✔
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,676,899✔
181
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
1,675,841✔
182
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
1,673,268✔
183

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

200
  TQ_ERR_GO_TO_END(
679,775✔
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));
676,506✔
203
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
361,794✔
204
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
360,554✔
205

206
  return 0;
360,135✔
207

208
END:
310,071✔
209
  if (txn != NULL) {
308,678✔
210
    tdbAbort(pTq->pMetaDB, txn);
311,650✔
211
  }
212
  return code;
316,905✔
213
}
214

215
int32_t tqMetaGetOffset(STQ* pTq, const char* subkey, STqOffset** pOffset) {
16,729,287✔
216
  if (pTq == NULL || subkey == NULL || pOffset == NULL) {
16,729,287✔
217
    return TSDB_CODE_INVALID_PARA;
×
218
  }
219
  void* data = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
16,729,302✔
220
  if (data == NULL) {
16,729,647✔
221
    int vLen = 0;
5,383,210✔
222
    taosRLockLatch(&pTq->lock);
5,383,210✔
223
    if (tdbTbGet(pTq->pOffsetStore, subkey, strlen(subkey), &data, &vLen) < 0) {
5,383,210✔
224
      taosRUnLockLatch(&pTq->lock);
5,304,167✔
225
      tdbFree(data);
5,312,176✔
226
      return TSDB_CODE_OUT_OF_MEMORY;
5,293,132✔
227
    }
228
    taosRUnLockLatch(&pTq->lock);
64,905✔
229

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

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

243
    *pOffset = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
64,905✔
244
    if (*pOffset == NULL) {
64,905✔
245
      return TSDB_CODE_OUT_OF_MEMORY;
×
246
    }
247
  } else {
248
    *pOffset = data;
11,346,437✔
249
  }
250
  return 0;
11,411,717✔
251
}
252

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

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

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

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

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

283
END:
1,384,767✔
284
  tEncoderClear(&encoder);
1,384,309✔
285
  taosMemoryFree(buf);
1,382,607✔
286
  return code;
1,381,430✔
287
}
288

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

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

299
  handle->pRef = walOpenRef(pVnode->pWal);
534,658✔
300
  TQ_NULL_GO_TO_END(handle->pRef);
534,336✔
301

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

310
  initStorageAPI(&reader.api);
533,949✔
311

312
  if (handle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
532,600✔
313
    handle->execHandle.task = qCreateQueueExecTaskInfo(handle->execHandle.execCol.qmsg, &reader, vgId, handle->consumerId);
386,512✔
314
    TQ_NULL_GO_TO_END(handle->execHandle.task);
387,485✔
315
    void* scanner = NULL;
387,485✔
316
    qExtractTmqScanner(handle->execHandle.task, &scanner);
387,485✔
317
    TQ_NULL_GO_TO_END(scanner);
387,485✔
318
    handle->execHandle.pTqReader = qExtractReaderFromTmqScanner(scanner);
387,485✔
319
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
387,485✔
320
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
147,171✔
321
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
119,244✔
322
    TQ_NULL_GO_TO_END(handle->pWalReader);
119,620✔
323
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
119,620✔
324
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
119,620✔
325
    TQ_ERR_GO_TO_END(buildSnapContext(reader.vnode, reader.version, 0, handle->execHandle.subType, handle->fetchMeta,
119,620✔
326
                                      (SSnapContext**)(&reader.sContext)));
327
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, handle->consumerId);
119,620✔
328
    TQ_NULL_GO_TO_END(handle->execHandle.task);
119,620✔
329
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
27,927✔
330
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
27,927✔
331
    TQ_NULL_GO_TO_END(handle->pWalReader);
27,927✔
332
    if (handle->execHandle.execTb.qmsg != NULL && strcmp(handle->execHandle.execTb.qmsg, "") != 0) {
27,927✔
333
      if (nodesStringToNode(handle->execHandle.execTb.qmsg, &handle->execHandle.execTb.node) != 0) {
10,752✔
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,927✔
339
                                      handle->execHandle.subType, handle->fetchMeta,
340
                                      (SSnapContext**)(&reader.sContext)));
341
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, handle->consumerId);
27,927✔
342
    TQ_NULL_GO_TO_END(handle->execHandle.task);
27,927✔
343
    TQ_ERR_GO_TO_END(qGetTableList(handle->execHandle.execTb.suid, pVnode, handle->execHandle.execTb.node, &tbUidList,
27,927✔
344
                                handle->execHandle.task));
345
    tqInfo("vgId:%d, tq try to get ctb for stb subscribe, suid:%" PRId64, pVnode->config.vgId,
27,927✔
346
           handle->execHandle.execTb.suid);
347
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
27,927✔
348
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
27,927✔
349
    TQ_ERR_GO_TO_END(tqReaderSetTbUidList(handle->execHandle.pTqReader, tbUidList, NULL));
27,927✔
350
  }
351
  handle->tableCreateTimeHash = (SHashObj*)taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
535,032✔
352

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

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

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

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

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

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

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

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

408
  int32_t code = tqMetaInitHandle(pTq, handle);
468,352✔
409
  if (code != 0) {
468,693✔
410
    return code;
×
411
  }
412
  tqInfo("tqMetaCreateHandle %s consumer 0x%" PRIx64 " vgId:%d, snapshotVer:%" PRId64, handle->subKey,
468,693✔
413
         handle->consumerId, vgId, handle->snapshotVer);
414
  return taosHashPut(pTq->pHandle, handle->subKey, strlen(handle->subKey), handle, sizeof(STqHandle));
469,099✔
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) {
320,994,420✔
450
  if (pTq == NULL || key == NULL || pHandle == NULL) {
320,994,420✔
451
    return TSDB_CODE_INVALID_PARA;
×
452
  }
453
  void* data = taosHashGet(pTq->pHandle, key, strlen(key));
321,070,351✔
454
  if (data == NULL) {
320,998,522✔
455
    int vLen = 0;
539,350✔
456
    if (tdbTbGet(pTq->pExecStore, key, (int)strlen(key), &data, &vLen) < 0) {
539,350✔
457
      tdbFree(data);
472,721✔
458
      return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
473,417✔
459
    }
460
    STqHandle handle = {0};
65,933✔
461
    int32_t code = tqMetaRestoreHandle(pTq, data, vLen >= 0 ? vLen : 0, &handle);
65,933✔
462
    if (code != 0) {
65,933✔
463
      tdbFree(data);
×
464
      tqDestroySTqHandle(&handle);
×
465
      return code;
×
466
    }
467
    tdbFree(data);
65,933✔
468
    *pHandle = taosHashGet(pTq->pHandle, key, strlen(key));
65,933✔
469
    if (*pHandle == NULL) {
65,933✔
470
      return terrno;
×
471
    }
472
  } else {
473
    *pHandle = data;
320,459,172✔
474
  }
475
  return TDB_CODE_SUCCESS;
320,576,706✔
476
}
477

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

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

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

515
  *data = fname;
14,611,232✔
516
  fname = NULL;
14,617,700✔
517

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

526
static int32_t tqReplacePath(char** path) {
4,867,201✔
527
  if (path == NULL || *path == NULL) {
4,867,201✔
528
    return TSDB_CODE_INVALID_PARA;
×
529
  }
530
  char*   tpath = NULL;
4,868,621✔
531
  int32_t code = tqBuildFName(&tpath, *path, TQ_SUBSCRIBE_NAME);
4,870,839✔
532
  if (code != 0) {
4,873,023✔
533
    return code;
×
534
  }
535
  taosMemoryFree(*path);
4,873,023✔
536
  *path = tpath;
4,868,522✔
537
  return TDB_CODE_SUCCESS;
4,869,249✔
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,858,990✔
579
  if (pTq == NULL) {
4,858,990✔
580
    return TSDB_CODE_INVALID_PARA;
×
581
  }
582
  char*   maindb = NULL;
4,858,990✔
583
  char*   offsetNew = NULL;
4,873,115✔
584
  int32_t code = TDB_CODE_SUCCESS;
4,873,781✔
585
  TQ_ERR_GO_TO_END(tqBuildFName(&maindb, pTq->path, TDB_MAINDB_NAME));
4,873,781✔
586
  if (!taosCheckExistFile(maindb)) {
4,871,112✔
587
    TQ_ERR_GO_TO_END(tqReplacePath(&pTq->path));
4,872,241✔
588
    TQ_ERR_GO_TO_END(tqMetaOpenTdb(pTq));
4,868,840✔
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,869,649✔
595
  if (taosCheckExistFile(offsetNew)) {
4,868,189✔
596
    TQ_ERR_GO_TO_END(tqOffsetRestoreFromFile(pTq, offsetNew));
×
597
    TQ_ERR_GO_TO_END(taosRemoveFile(offsetNew));
×
598
  }
599

600
END:
4,873,617✔
601
  taosMemoryFree(maindb);
4,872,965✔
602
  taosMemoryFree(offsetNew);
4,869,910✔
603
  return code;
4,868,085✔
604
}
605

606
void tqMetaClose(STQ* pTq) {
4,872,539✔
607
  if (pTq == NULL) {
4,872,539✔
608
    return;
×
609
  }
610
  int32_t ret = 0;
4,872,539✔
611
  if (pTq->pExecStore) {
4,872,539✔
612
    tdbTbClose(pTq->pExecStore);
4,872,943✔
613
  }
614
  if (pTq->pOffsetStore) {
4,874,795✔
615
    tdbTbClose(pTq->pOffsetStore);
4,873,919✔
616
  }
617
  tdbClose(pTq->pMetaDB);
4,873,126✔
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