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

taosdata / TDengine / #4876

10 Dec 2025 05:56AM UTC coverage: 64.632% (+0.2%) from 64.472%
#4876

push

travis-ci

guanshengliang
test: fix idmp case with checkDataMemLoop checked (#33862)

4 of 9 new or added lines in 3 files covered. (44.44%)

380 existing lines in 104 files now uncovered.

162866 of 251990 relevant lines covered (64.63%)

107950382.52 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 tEncodeSTqHandle(SEncoder* pEncoder, const STqHandle* pHandle) {
855,914✔
19
  if (pEncoder == NULL || pHandle == NULL) {
855,914✔
UNCOV
20
    return TSDB_CODE_INVALID_PARA;
×
21
  }
22
  int32_t code = 0;
855,914✔
23
  int32_t lino;
24

25
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
855,914✔
26
  TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->subKey));
1,711,737✔
27
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->fetchMeta));
1,711,737✔
28
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->consumerId));
1,711,703✔
29
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->snapshotVer));
1,711,579✔
30
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pHandle->epoch));
1,711,523✔
31
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->execHandle.subType));
1,711,556✔
32
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
855,823✔
33
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execCol.qmsg));
1,530,847✔
34
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
90,410✔
35
    int32_t size = taosHashGetSize(pHandle->execHandle.execDb.pFilterOutTbUid);
78,126✔
36
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, size));
78,126✔
37
    void* pIter = NULL;
78,126✔
38
    pIter = taosHashIterate(pHandle->execHandle.execDb.pFilterOutTbUid, pIter);
78,126✔
39
    while (pIter) {
78,036✔
40
      int64_t* tbUid = (int64_t*)taosHashGetKey(pIter, NULL);
×
41
      TAOS_CHECK_EXIT(tEncodeI64(pEncoder, *tbUid));
×
42
      pIter = taosHashIterate(pHandle->execHandle.execDb.pFilterOutTbUid, pIter);
×
43
    }
44
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
12,284✔
45
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->execHandle.execTb.suid));
24,568✔
46
    if (pHandle->execHandle.execTb.qmsg != NULL) {
12,284✔
47
      TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execTb.qmsg));
24,568✔
48
    }
49
  }
50
  tEndEncode(pEncoder);
855,697✔
51
_exit:
855,710✔
52
  if (code) {
855,710✔
53
    return code;
×
54
  } else {
55
    return pEncoder->pos;
855,710✔
56
  }
57
}
58

59
int32_t tDecodeSTqHandle(SDecoder* pDecoder, STqHandle* pHandle) {
3,841✔
60
  if (pDecoder == NULL || pHandle == NULL) {
3,841✔
61
    return TSDB_CODE_INVALID_PARA;
×
62
  }
63
  int32_t code = 0;
3,841✔
64
  int32_t lino;
65

66
  TAOS_CHECK_EXIT(tStartDecode(pDecoder));
3,841✔
67
  TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pHandle->subKey));
3,841✔
68
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pHandle->fetchMeta));
7,682✔
69
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->consumerId));
7,682✔
70
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->snapshotVer));
7,682✔
71
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pHandle->epoch));
7,682✔
72
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pHandle->execHandle.subType));
7,682✔
73
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
3,841✔
74
    TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pHandle->execHandle.execCol.qmsg));
5,828✔
75
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
927✔
76
    pHandle->execHandle.execDb.pFilterOutTbUid =
424✔
77
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
424✔
78
    if (pHandle->execHandle.execDb.pFilterOutTbUid == NULL) {
424✔
79
      TAOS_CHECK_EXIT(terrno);
×
80
    }
81
    int32_t size = 0;
424✔
82
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &size));
424✔
83
    for (int32_t i = 0; i < size; i++) {
424✔
84
      int64_t tbUid = 0;
×
85
      TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &tbUid));
×
86
      TAOS_CHECK_EXIT(taosHashPut(pHandle->execHandle.execDb.pFilterOutTbUid, &tbUid, sizeof(int64_t), NULL, 0));
×
87
    }
88
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
503✔
89
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->execHandle.execTb.suid));
1,006✔
90
    if (!tDecodeIsEnd(pDecoder)) {
503✔
91
      TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pHandle->execHandle.execTb.qmsg));
1,006✔
92
    }
93
  }
94
  tEndDecode(pDecoder);
3,841✔
95

96
_exit:
3,825✔
97
  return code;
3,825✔
98
}
99

100
int32_t tqMetaDecodeCheckInfo(STqCheckInfo* info, void* pVal, uint32_t vLen) {
13,373✔
101
  if (info == NULL || pVal == NULL) {
13,373✔
102
    return TSDB_CODE_INVALID_PARA;
×
103
  }
104
  SDecoder decoder = {0};
13,373✔
105
  tDecoderInit(&decoder, (uint8_t*)pVal, vLen);
13,373✔
106
  int32_t code = tDecodeSTqCheckInfo(&decoder, info);
13,373✔
107
  tDecoderClear(&decoder);
13,373✔
108

109
  if (code != 0) {
13,373✔
110
    tDeleteSTqCheckInfo(info);
×
111
    return TSDB_CODE_OUT_OF_MEMORY;
×
112
  }
113
  return code;
13,373✔
114
}
115

116
int32_t tqMetaDecodeOffsetInfo(STqOffset* info, void* pVal, uint32_t vLen) {
1,966✔
117
  if (info == NULL || pVal == NULL) {
1,966✔
118
    return TSDB_CODE_INVALID_PARA;
×
119
  }
120
  SDecoder decoder = {0};
1,966✔
121
  tDecoderInit(&decoder, (uint8_t*)pVal, vLen);
1,966✔
122
  int32_t code = tDecodeSTqOffset(&decoder, info);
1,966✔
123
  tDecoderClear(&decoder);
1,966✔
124

125
  if (code != 0) {
1,966✔
126
    tDeleteSTqOffset(info);
×
127
    return TSDB_CODE_OUT_OF_MEMORY;
×
128
  }
129
  return code;
1,966✔
130
}
131

132
int32_t tqMetaSaveOffset(STQ* pTq, STqOffset* pOffset) {
101,116✔
133
  if (pTq == NULL || pOffset == NULL) {
101,116✔
134
    return TSDB_CODE_INVALID_PARA;
×
135
  }
136
  void*    buf = NULL;
101,116✔
137
  int32_t  code = TDB_CODE_SUCCESS;
101,116✔
138
  uint32_t  vlen;
139
  SEncoder encoder = {0};
101,116✔
140
  tEncodeSize(tEncodeSTqOffset, pOffset, vlen, code);
101,116✔
141
  if (code < 0) {
101,116✔
142
    goto END;
×
143
  }
144

145
  buf = taosMemoryCalloc(1, vlen);
101,116✔
146
  if (buf == NULL) {
101,116✔
147
    code = terrno;
×
148
    goto END;
×
149
  }
150

151
  tEncoderInit(&encoder, buf, vlen);
101,116✔
152
  code = tEncodeSTqOffset(&encoder, pOffset);
101,116✔
153
  if (code < 0) {
101,116✔
154
    goto END;
×
155
  }
156

157
  taosWLockLatch(&pTq->lock);
101,116✔
158
  code = tqMetaSaveInfo(pTq, pTq->pOffsetStore, pOffset->subKey, strlen(pOffset->subKey), buf, vlen);
101,116✔
159
  taosWUnLockLatch(&pTq->lock);
101,116✔
160
END:
101,116✔
161
  tEncoderClear(&encoder);
101,116✔
162
  taosMemoryFree(buf);
101,116✔
163
  return code;
101,116✔
164
}
165

166
int32_t tqMetaSaveInfo(STQ* pTq, TTB* ttb, const void* key, uint32_t kLen, const void* value, uint32_t vLen) {
539,622✔
167
  if (pTq == NULL || ttb == NULL || key == NULL || value == NULL) {
539,622✔
168
    return TSDB_CODE_INVALID_PARA;
×
169
  }
170
  int32_t code = TDB_CODE_SUCCESS;
539,655✔
171
  TXN*    txn = NULL;
539,655✔
172

173
  TQ_ERR_GO_TO_END(
539,712✔
174
      tdbBegin(pTq->pMetaDB, &txn, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED));
175
  TQ_ERR_GO_TO_END(tdbTbUpsert(ttb, key, kLen, value, vLen, txn));
539,530✔
176
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
539,608✔
177
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
539,647✔
178

179
  return 0;
539,397✔
180

181
END:
×
182
  tdbAbort(pTq->pMetaDB, txn);
×
183
  return code;
×
184
}
185

186
int32_t tqMetaDeleteInfo(STQ* pTq, TTB* ttb, const void* key, uint32_t kLen) {
184,639✔
187
  if (pTq == NULL || ttb == NULL || key == NULL) {
184,639✔
188
    return TSDB_CODE_INVALID_PARA;
×
189
  }
190
  int32_t code = TDB_CODE_SUCCESS;
185,079✔
191
  TXN*    txn = NULL;
185,079✔
192

193
  TQ_ERR_GO_TO_END(
185,079✔
194
      tdbBegin(pTq->pMetaDB, &txn, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED));
195
  TQ_ERR_GO_TO_END(tdbTbDelete(ttb, key, kLen, txn));
184,686✔
196
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
96,911✔
197
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
96,911✔
198

199
  return 0;
96,820✔
200

201
END:
88,148✔
202
  tdbAbort(pTq->pMetaDB, txn);
88,148✔
203
  return code;
87,775✔
204
}
205

206
int32_t tqMetaGetOffset(STQ* pTq, const char* subkey, STqOffset** pOffset) {
823,582✔
207
  if (pTq == NULL || subkey == NULL || pOffset == NULL) {
823,582✔
208
    return TSDB_CODE_INVALID_PARA;
×
209
  }
210
  void* data = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
823,627✔
211
  if (data == NULL) {
823,699✔
212
    int vLen = 0;
271,118✔
213
    taosRLockLatch(&pTq->lock);
271,118✔
214
    if (tdbTbGet(pTq->pOffsetStore, subkey, strlen(subkey), &data, &vLen) < 0) {
271,118✔
215
      taosRUnLockLatch(&pTq->lock);
269,556✔
216
      tdbFree(data);
269,719✔
217
      return TSDB_CODE_OUT_OF_MEMORY;
269,349✔
218
    }
219
    taosRUnLockLatch(&pTq->lock);
1,327✔
220

221
    STqOffset offset = {0};
1,327✔
222
    if (tqMetaDecodeOffsetInfo(&offset, data, vLen >= 0 ? vLen : 0) != TDB_CODE_SUCCESS) {
1,327✔
223
      tdbFree(data);
×
224
      return TSDB_CODE_OUT_OF_MEMORY;
×
225
    }
226

227
    if (taosHashPut(pTq->pOffset, subkey, strlen(subkey), &offset, sizeof(STqOffset)) != 0) {
1,327✔
228
      tDeleteSTqOffset(&offset);
×
229
      tdbFree(data);
×
230
      return terrno;
×
231
    }
232
    tdbFree(data);
1,327✔
233

234
    *pOffset = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
1,327✔
235
    if (*pOffset == NULL) {
1,327✔
236
      return TSDB_CODE_OUT_OF_MEMORY;
×
237
    }
238
  } else {
239
    *pOffset = data;
552,581✔
240
  }
241
  return 0;
553,908✔
242
}
243

244
int32_t tqMetaSaveHandle(STQ* pTq, const char* key, const STqHandle* pHandle) {
427,957✔
245
  if (pTq == NULL || key == NULL || pHandle == NULL) {
427,957✔
246
    return TSDB_CODE_INVALID_PARA;
×
247
  }
248
  int32_t  code = TDB_CODE_SUCCESS;
427,957✔
249
  uint32_t  vlen;
250
  void*    buf = NULL;
427,957✔
251
  SEncoder encoder = {0};
427,957✔
252
  tEncodeSize(tEncodeSTqHandle, pHandle, vlen, code);
427,957✔
253
  if (code < 0) {
427,957✔
254
    goto END;
×
255
  }
256

257
  tqDebug("tq save %s(%d) handle consumer:0x%" PRIx64 " epoch:%d vgId:%d", pHandle->subKey,
427,957✔
258
          (int32_t)strlen(pHandle->subKey), pHandle->consumerId, pHandle->epoch, TD_VID(pTq->pVnode));
259

260
  buf = taosMemoryCalloc(1, vlen);
427,957✔
261
  if (buf == NULL) {
427,957✔
262
    code = terrno;
×
263
    goto END;
×
264
  }
265

266
  tEncoderInit(&encoder, buf, vlen);
427,957✔
267
  code = tEncodeSTqHandle(&encoder, pHandle);
427,957✔
268
  if (code < 0) {
427,733✔
269
    goto END;
×
270
  }
271

272
  TQ_ERR_GO_TO_END(tqMetaSaveInfo(pTq, pTq->pExecStore, key, strlen(key), buf, vlen));
427,733✔
273

274
END:
427,551✔
275
  tEncoderClear(&encoder);
427,496✔
276
  taosMemoryFree(buf);
427,777✔
277
  return code;
427,246✔
278
}
279

280
static int tqMetaInitHandle(STQ* pTq, STqHandle* handle) {
130,197✔
281
  if (pTq == NULL || handle == NULL) {
130,197✔
282
    return TSDB_CODE_INVALID_PARA;
×
283
  }
284
  int32_t code = TDB_CODE_SUCCESS;
130,197✔
285
  SArray* tbUidList = NULL;
130,197✔
286

287
  SVnode* pVnode = pTq->pVnode;
130,197✔
288
  int32_t vgId = TD_VID(pVnode);
130,197✔
289

290
  handle->pRef = walOpenRef(pVnode->pWal);
130,181✔
291
  TQ_NULL_GO_TO_END(handle->pRef);
130,197✔
292

293
  SReadHandle reader = {0};
130,197✔
294
  reader = (SReadHandle){
130,197✔
295
      .vnode = pVnode,
296
      .initTableReader = true,
297
      .initTqReader = true,
298
      .version = handle->snapshotVer,
130,197✔
299
  };
300

301
  initStorageAPI(&reader.api);
130,197✔
302

303
  if (handle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
130,005✔
304
    handle->execHandle.task = qCreateQueueExecTaskInfo(handle->execHandle.execCol.qmsg, &reader, vgId,
109,220✔
305
                                                       &handle->execHandle.numOfCols, handle->consumerId);
108,845✔
306
    TQ_NULL_GO_TO_END(handle->execHandle.task);
109,037✔
307
    void* scanner = NULL;
109,037✔
308
    qExtractTmqScanner(handle->execHandle.task, &scanner);
109,037✔
309
    TQ_NULL_GO_TO_END(scanner);
109,037✔
310
    handle->execHandle.pTqReader = qExtractReaderFromTmqScanner(scanner);
109,037✔
311
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
109,037✔
312
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
21,160✔
313
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
17,746✔
314
    TQ_NULL_GO_TO_END(handle->pWalReader);
17,746✔
315
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
17,701✔
316
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
17,746✔
317
    TQ_ERR_GO_TO_END(buildSnapContext(reader.vnode, reader.version, 0, handle->execHandle.subType, handle->fetchMeta,
17,746✔
318
                                      (SSnapContext**)(&reader.sContext)));
319
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, NULL, handle->consumerId);
17,746✔
320
    TQ_NULL_GO_TO_END(handle->execHandle.task);
17,746✔
321
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
3,398✔
322
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
3,414✔
323
    TQ_NULL_GO_TO_END(handle->pWalReader);
3,414✔
324
    if (handle->execHandle.execTb.qmsg != NULL && strcmp(handle->execHandle.execTb.qmsg, "") != 0) {
3,398✔
325
      if (nodesStringToNode(handle->execHandle.execTb.qmsg, &handle->execHandle.execTb.node) != 0) {
1,753✔
326
        tqError("nodesStringToNode error in sub stable, since %s", terrstr());
×
327
        return TSDB_CODE_SCH_INTERNAL_ERROR;
×
328
      }
329
    }
330
    TQ_ERR_GO_TO_END(buildSnapContext(reader.vnode, reader.version, handle->execHandle.execTb.suid,
3,414✔
331
                                      handle->execHandle.subType, handle->fetchMeta,
332
                                      (SSnapContext**)(&reader.sContext)));
333
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, NULL, handle->consumerId);
3,414✔
334
    TQ_NULL_GO_TO_END(handle->execHandle.task);
3,414✔
335
    TQ_ERR_GO_TO_END(qGetTableList(handle->execHandle.execTb.suid, pVnode, handle->execHandle.execTb.node, &tbUidList,
3,414✔
336
                                handle->execHandle.task));
337
    tqInfo("vgId:%d, tq try to get ctb for stb subscribe, suid:%" PRId64, pVnode->config.vgId,
3,414✔
338
           handle->execHandle.execTb.suid);
339
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
3,414✔
340
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
3,414✔
341
    TQ_ERR_GO_TO_END(tqReaderSetTbUidList(handle->execHandle.pTqReader, tbUidList, NULL));
3,414✔
342
  }
343
  handle->tableCreateTimeHash = (SHashObj*)taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
130,197✔
344

345
END:
129,752✔
346
  taosArrayDestroy(tbUidList);
129,752✔
347
  return code;
130,197✔
348
}
349

350
static int32_t tqMetaRestoreHandle(STQ* pTq, void* pVal, uint32_t vLen, STqHandle* handle) {
3,202✔
351
  if (pTq == NULL || pVal == NULL || handle == NULL) {
3,202✔
352
    return TSDB_CODE_INVALID_PARA;
×
353
  }
354
  int32_t  vgId = TD_VID(pTq->pVnode);
3,202✔
355
  SDecoder decoder = {0};
3,202✔
356
  int32_t  code = TDB_CODE_SUCCESS;
3,202✔
357

358
  tDecoderInit(&decoder, (uint8_t*)pVal, vLen);
3,202✔
359
  TQ_ERR_GO_TO_END(tDecodeSTqHandle(&decoder, handle));
3,202✔
360
  TQ_ERR_GO_TO_END(tqMetaInitHandle(pTq, handle));
3,186✔
361
  tqInfo("tqMetaRestoreHandle %s consumer 0x%" PRIx64 " vgId:%d", handle->subKey, handle->consumerId, vgId);
3,202✔
362
  code = taosHashPut(pTq->pHandle, handle->subKey, strlen(handle->subKey), handle, sizeof(STqHandle));
3,202✔
363

364
END:
3,202✔
365
  tDecoderClear(&decoder);
3,202✔
366
  return code;
3,202✔
367
}
368

369
int32_t tqMetaCreateHandle(STQ* pTq, SMqRebVgReq* req, STqHandle* handle) {
126,995✔
370
  if (pTq == NULL || req == NULL || handle == NULL) {
126,995✔
371
    return TSDB_CODE_INVALID_PARA;
×
372
  }
373
  int32_t vgId = TD_VID(pTq->pVnode);
126,995✔
374

375
  (void)memcpy(handle->subKey, req->subKey, TSDB_SUBSCRIBE_KEY_LEN);
126,995✔
376
  handle->consumerId = req->newConsumerId;
126,995✔
377

378
  handle->execHandle.subType = req->subType;
126,995✔
379
  handle->fetchMeta = req->withMeta;
126,995✔
380
  if (req->subType == TOPIC_SUB_TYPE__COLUMN) {
126,995✔
381
    void* tmp = taosStrdup(req->qmsg);
106,537✔
382
    if (tmp == NULL) {
106,537✔
383
      return terrno;
×
384
    }
385
    handle->execHandle.execCol.qmsg = tmp;
106,537✔
386
  } else if (req->subType == TOPIC_SUB_TYPE__DB) {
20,458✔
387
    handle->execHandle.execDb.pFilterOutTbUid =
17,423✔
388
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
17,423✔
389
    if (handle->execHandle.execDb.pFilterOutTbUid == NULL) {
17,423✔
390
      return terrno;
×
391
    }
392
  } else if (req->subType == TOPIC_SUB_TYPE__TABLE) {
3,035✔
393
    handle->execHandle.execTb.suid = req->suid;
3,035✔
394
    void* tmp = taosStrdup(req->qmsg);
3,035✔
395
    if (tmp == NULL) {
3,035✔
396
      return terrno;
×
397
    }
398
    handle->execHandle.execTb.qmsg = tmp;
3,035✔
399
  }
400

401
  handle->snapshotVer = walGetCommittedVer(pTq->pVnode->pWal);
126,995✔
402

403
  int32_t code = tqMetaInitHandle(pTq, handle);
126,995✔
404
  if (code != 0) {
126,550✔
405
    return code;
×
406
  }
407
  tqInfo("tqMetaCreateHandle %s consumer 0x%" PRIx64 " vgId:%d, snapshotVer:%" PRId64, handle->subKey,
126,550✔
408
         handle->consumerId, vgId, handle->snapshotVer);
409
  return taosHashPut(pTq->pHandle, handle->subKey, strlen(handle->subKey), handle, sizeof(STqHandle));
126,995✔
410
}
411

412
static int32_t tqMetaTransformInfo(TDB* pMetaDB, TTB* pOld, TTB* pNew) {
×
413
  if (pMetaDB == NULL || pOld == NULL || pNew == NULL) {
×
414
    return TSDB_CODE_INVALID_PARA;
×
415
  }
416
  TBC*  pCur = NULL;
×
417
  void* pKey = NULL;
×
418
  int   kLen = 0;
×
419
  void* pVal = NULL;
×
420
  int   vLen = 0;
×
421
  TXN*  txn = NULL;
×
422

423
  int32_t code = TDB_CODE_SUCCESS;
×
424

425
  TQ_ERR_GO_TO_END(tdbTbcOpen(pOld, &pCur, NULL));
×
426
  TQ_ERR_GO_TO_END(
×
427
      tdbBegin(pMetaDB, &txn, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED));
428

429
  TQ_ERR_GO_TO_END(tdbTbcMoveToFirst(pCur));
×
430
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
×
431
    TQ_ERR_GO_TO_END(tdbTbUpsert(pNew, pKey, kLen, pVal, vLen, txn));
×
432
  }
433

434
  TQ_ERR_GO_TO_END(tdbCommit(pMetaDB, txn));
×
435
  TQ_ERR_GO_TO_END(tdbPostCommit(pMetaDB, txn));
×
436

437
END:
×
438
  tdbFree(pKey);
×
439
  tdbFree(pVal);
×
440
  tdbTbcClose(pCur);
×
441
  return code;
×
442
}
443

444
int32_t tqMetaGetHandle(STQ* pTq, const char* key, STqHandle** pHandle) {
7,146,978✔
445
  if (pTq == NULL || key == NULL || pHandle == NULL) {
7,146,978✔
446
    return TSDB_CODE_INVALID_PARA;
×
447
  }
448
  void* data = taosHashGet(pTq->pHandle, key, strlen(key));
7,147,539✔
449
  if (data == NULL) {
7,147,029✔
450
    int vLen = 0;
130,197✔
451
    if (tdbTbGet(pTq->pExecStore, key, (int)strlen(key), &data, &vLen) < 0) {
130,197✔
452
      tdbFree(data);
126,878✔
453
      return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
126,968✔
454
    }
455
    STqHandle handle = {0};
3,202✔
456
    int32_t code = tqMetaRestoreHandle(pTq, data, vLen >= 0 ? vLen : 0, &handle);
3,202✔
457
    if (code != 0) {
3,202✔
458
      tdbFree(data);
×
459
      tqDestroyTqHandle(&handle);
×
460
      return code;
×
461
    }
462
    tdbFree(data);
3,202✔
463
    *pHandle = taosHashGet(pTq->pHandle, key, strlen(key));
3,202✔
464
    if (*pHandle == NULL) {
3,202✔
465
      return terrno;
×
466
    }
467
  } else {
468
    *pHandle = data;
7,016,832✔
469
  }
470
  return TDB_CODE_SUCCESS;
7,020,995✔
471
}
472

473
int32_t tqMetaOpenTdb(STQ* pTq) {
4,126,722✔
474
  if (pTq == NULL) {
4,126,722✔
475
    return TSDB_CODE_INVALID_PARA;
×
476
  }
477
  int32_t code = TDB_CODE_SUCCESS;
4,126,722✔
478
  TQ_ERR_GO_TO_END(tdbOpen(pTq->path, 16 * 1024, 1, &pTq->pMetaDB, 0, NULL));
4,126,722✔
479
  TQ_ERR_GO_TO_END(tdbTbOpen("tq.db", -1, -1, NULL, pTq->pMetaDB, &pTq->pExecStore, 0));
4,124,824✔
480
  TQ_ERR_GO_TO_END(tdbTbOpen("tq.check.db", -1, -1, NULL, pTq->pMetaDB, &pTq->pCheckStore, 0));
4,126,254✔
481
  TQ_ERR_GO_TO_END(tdbTbOpen("tq.offset.db", -1, -1, NULL, pTq->pMetaDB, &pTq->pOffsetStore, 0));
4,125,275✔
482

483
END:
4,126,149✔
484
  return code;
4,126,149✔
485
}
486

487
static int32_t replaceTqPath(char** path) {
4,118,721✔
488
  if (path == NULL || *path == NULL) {
4,118,721✔
489
    return TSDB_CODE_INVALID_PARA;
×
490
  }
491
  char*   tpath = NULL;
4,118,721✔
492
  int32_t code = tqBuildFName(&tpath, *path, TQ_SUBSCRIBE_NAME);
4,126,722✔
493
  if (code != 0) {
4,126,722✔
494
    return code;
×
495
  }
496
  taosMemoryFree(*path);
4,126,722✔
497
  *path = tpath;
4,125,934✔
498
  return TDB_CODE_SUCCESS;
4,125,934✔
499
}
500

501
static int32_t tqMetaRestoreCheckInfo(STQ* pTq) {
4,125,392✔
502
  if (pTq == NULL) {
4,125,392✔
503
    return TSDB_CODE_INVALID_PARA;
×
504
  }
505
  TBC*         pCur = NULL;
4,125,392✔
506
  void*        pKey = NULL;
4,125,392✔
507
  int          kLen = 0;
4,125,392✔
508
  void*        pVal = NULL;
4,125,392✔
509
  int          vLen = 0;
4,125,392✔
510
  int32_t      code = 0;
4,125,392✔
511
  STqCheckInfo info = {0};
4,125,392✔
512

513
  TQ_ERR_GO_TO_END(tdbTbcOpen(pTq->pCheckStore, &pCur, NULL));
4,125,392✔
514
  TQ_ERR_GO_TO_END(tdbTbcMoveToFirst(pCur));
4,126,149✔
515

516
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
4,128,922✔
517
    TQ_ERR_GO_TO_END(tqMetaDecodeCheckInfo(&info, pVal, vLen >= 0 ? vLen : 0));
3,564✔
518
    TQ_ERR_GO_TO_END(taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo)));
4,012✔
519
  }
520
  info.colIdList = NULL;
4,126,699✔
521

522
END:
4,126,699✔
523
  tdbFree(pKey);
4,126,149✔
524
  tdbFree(pVal);
4,125,366✔
525
  tdbTbcClose(pCur);
4,125,939✔
526
  tDeleteSTqCheckInfo(&info);
4,125,440✔
527
  return code;
4,125,600✔
528
}
529

530
int32_t tqMetaOpen(STQ* pTq) {
4,122,216✔
531
  if (pTq == NULL) {
4,122,216✔
532
    return TSDB_CODE_INVALID_PARA;
×
533
  }
534
  char*   maindb = NULL;
4,122,216✔
535
  char*   offsetNew = NULL;
4,126,722✔
536
  int32_t code = TDB_CODE_SUCCESS;
4,126,722✔
537
  TQ_ERR_GO_TO_END(tqBuildFName(&maindb, pTq->path, TDB_MAINDB_NAME));
4,126,722✔
538
  if (!taosCheckExistFile(maindb)) {
4,126,722✔
539
    TQ_ERR_GO_TO_END(replaceTqPath(&pTq->path));
4,126,722✔
540
    TQ_ERR_GO_TO_END(tqMetaOpenTdb(pTq));
4,125,934✔
541
  } else {
542
    TQ_ERR_GO_TO_END(tqMetaTransform(pTq));
×
543
    TQ_ERR_GO_TO_END(taosRemoveFile(maindb));
×
544
  }
545

546
  TQ_ERR_GO_TO_END(tqBuildFName(&offsetNew, pTq->path, TQ_OFFSET_NAME));
4,126,149✔
547
  if (taosCheckExistFile(offsetNew)) {
4,125,392✔
548
    TQ_ERR_GO_TO_END(tqOffsetRestoreFromFile(pTq, offsetNew));
×
549
    TQ_ERR_GO_TO_END(taosRemoveFile(offsetNew));
×
550
  }
551

552
  TQ_ERR_GO_TO_END(tqMetaRestoreCheckInfo(pTq));
4,126,149✔
553

554
END:
4,125,440✔
555
  taosMemoryFree(maindb);
4,125,440✔
556
  taosMemoryFree(offsetNew);
4,126,197✔
557
  return code;
4,125,440✔
558
}
559

560
int32_t tqMetaTransform(STQ* pTq) {
×
561
  if (pTq == NULL) {
×
562
    return TSDB_CODE_INVALID_PARA;
×
563
  }
564
  int32_t code = TDB_CODE_SUCCESS;
×
565
  TDB*    pMetaDB = NULL;
×
566
  TTB*    pExecStore = NULL;
×
567
  TTB*    pCheckStore = NULL;
×
568
  char*   offsetNew = NULL;
×
569
  char*   offset = NULL;
×
570
  TQ_ERR_GO_TO_END(tqBuildFName(&offset, pTq->path, TQ_OFFSET_NAME));
×
571

572
  TQ_ERR_GO_TO_END(tdbOpen(pTq->path, 16 * 1024, 1, &pMetaDB, 0, NULL));
×
573
  TQ_ERR_GO_TO_END(tdbTbOpen("tq.db", -1, -1, NULL, pMetaDB, &pExecStore, 0));
×
574
  TQ_ERR_GO_TO_END(tdbTbOpen("tq.check.db", -1, -1, NULL, pMetaDB, &pCheckStore, 0));
×
575

576
  TQ_ERR_GO_TO_END(replaceTqPath(&pTq->path));
×
577
  TQ_ERR_GO_TO_END(tqMetaOpenTdb(pTq));
×
578

579
  TQ_ERR_GO_TO_END(tqMetaTransformInfo(pTq->pMetaDB, pExecStore, pTq->pExecStore));
×
580
  TQ_ERR_GO_TO_END(tqMetaTransformInfo(pTq->pMetaDB, pCheckStore, pTq->pCheckStore));
×
581

582
  TQ_ERR_GO_TO_END(tqBuildFName(&offsetNew, pTq->path, TQ_OFFSET_NAME));
×
583

584
  if (taosCheckExistFile(offset)) {
×
585
    if (taosCopyFile(offset, offsetNew) < 0) {
×
586
      tqError("copy offset file error");
×
587
    } else {
588
      TQ_ERR_GO_TO_END(taosRemoveFile(offset));
×
589
    }
590
  }
591

592
END:
×
593
  taosMemoryFree(offset);
×
594
  taosMemoryFree(offsetNew);
×
595

596
  tdbTbClose(pExecStore);
×
597
  tdbTbClose(pCheckStore);
×
598
  tdbClose(pMetaDB);
×
599
  return code;
×
600
}
601

602
void tqMetaClose(STQ* pTq) {
4,126,631✔
603
  if (pTq == NULL) {
4,126,631✔
604
    return;
×
605
  }
606
  int32_t ret = 0;
4,126,631✔
607
  if (pTq->pExecStore) {
4,126,631✔
608
    tdbTbClose(pTq->pExecStore);
4,126,722✔
609
  }
610
  if (pTq->pCheckStore) {
4,126,722✔
611
    tdbTbClose(pTq->pCheckStore);
4,126,722✔
612
  }
613
  if (pTq->pOffsetStore) {
4,126,722✔
614
    tdbTbClose(pTq->pOffsetStore);
4,126,722✔
615
  }
616
  tdbClose(pTq->pMetaDB);
4,126,722✔
617
}
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