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

taosdata / TDengine / #5001

24 Mar 2026 01:11AM UTC coverage: 72.254% (-0.05%) from 72.307%
#5001

push

travis-ci

web-flow
fix: possible memory leak in tdb; (#34872)

1 of 20 new or added lines in 2 files covered. (5.0%)

770 existing lines in 143 files now uncovered.

253287 of 350550 relevant lines covered (72.25%)

130300301.72 hits per line

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

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

25
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
2,454,428✔
26
  TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->subKey));
4,908,172✔
27
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->fetchMeta));
4,907,130✔
28
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->consumerId));
4,906,398✔
29
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->snapshotVer));
4,906,023✔
30
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pHandle->epoch));
4,906,023✔
31
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->execHandle.subType));
4,906,443✔
32
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
2,453,431✔
33
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execCol.qmsg));
4,009,521✔
34
    TAOS_CHECK_EXIT(tEncodeSSchemaWrapper(pEncoder, &pHandle->execHandle.execCol.pSW));
4,010,522✔
35
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
448,282✔
36
    int32_t size = taosHashGetSize(pHandle->execHandle.execDb.pFilterOutTbUid);
368,162✔
37
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, size));
368,162✔
38
    void* pIter = NULL;
368,162✔
39
    pIter = taosHashIterate(pHandle->execHandle.execDb.pFilterOutTbUid, pIter);
368,162✔
40
    while (pIter) {
368,162✔
41
      int64_t* tbUid = (int64_t*)taosHashGetKey(pIter, NULL);
×
42
      TAOS_CHECK_EXIT(tEncodeI64(pEncoder, *tbUid));
×
43
      pIter = taosHashIterate(pHandle->execHandle.execDb.pFilterOutTbUid, pIter);
×
44
    }
45
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
79,781✔
46
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->execHandle.execTb.suid));
160,240✔
47
    if (pHandle->execHandle.execTb.qmsg != NULL) {
80,120✔
48
      TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execTb.qmsg));
160,240✔
49
    }
50
  }
51
  tEndEncode(pEncoder);
2,454,070✔
52
_exit:
2,451,044✔
53
  if (code) {
2,451,044✔
54
    return code;
×
55
  } else {
56
    return pEncoder->pos;
2,451,044✔
57
  }
58
}
59

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

67
  TAOS_CHECK_EXIT(tStartDecode(pDecoder));
76,239✔
68
  TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pHandle->subKey));
76,239✔
69
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pHandle->fetchMeta));
152,182✔
70
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->consumerId));
152,182✔
71
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->snapshotVer));
152,182✔
72
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pHandle->epoch));
152,182✔
73
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pHandle->execHandle.subType));
152,182✔
74
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
76,239✔
75
    TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pHandle->execHandle.execCol.qmsg));
101,212✔
76
    if (!tDecodeIsEnd(pDecoder)) {
50,606✔
77
      TAOS_CHECK_EXIT(tDecodeSSchemaWrapper(pDecoder, &pHandle->execHandle.execCol.pSW));
101,212✔
78
    }
79
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
25,337✔
80
    pHandle->execHandle.execDb.pFilterOutTbUid =
17,155✔
81
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
17,155✔
82
    if (pHandle->execHandle.execDb.pFilterOutTbUid == NULL) {
17,155✔
83
      TAOS_CHECK_EXIT(terrno);
×
84
    }
85
    int32_t size = 0;
17,155✔
86
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &size));
17,155✔
87
    for (int32_t i = 0; i < size; i++) {
17,155✔
88
      int64_t tbUid = 0;
×
89
      TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &tbUid));
×
90
      TAOS_CHECK_EXIT(taosHashPut(pHandle->execHandle.execDb.pFilterOutTbUid, &tbUid, sizeof(int64_t), NULL, 0));
×
91
    }
92
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
8,182✔
93
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->execHandle.execTb.suid));
16,660✔
94
    if (!tDecodeIsEnd(pDecoder)) {
8,478✔
95
      TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pHandle->execHandle.execTb.qmsg));
16,364✔
96
    }
97
  }
98
  tEndDecode(pDecoder);
76,535✔
99

100
_exit:
76,239✔
101
  return code;
76,239✔
102
}
103

104
int32_t tqMetaDecodeOffsetInfo(STqOffset* info, void* pVal, uint32_t vLen) {
41,457✔
105
  if (info == NULL || pVal == NULL) {
41,457✔
106
    return TSDB_CODE_INVALID_PARA;
×
107
  }
108
  SDecoder decoder = {0};
41,457✔
109
  tDecoderInit(&decoder, (uint8_t*)pVal, vLen);
41,457✔
110
  int32_t code = tDecodeSTqOffset(&decoder, info);
41,743✔
111
  tDecoderClear(&decoder);
41,743✔
112

113
  if (code != 0) {
41,457✔
114
    tDeleteSTqOffset(info);
×
115
    return TSDB_CODE_OUT_OF_MEMORY;
×
116
  }
117
  return code;
41,457✔
118
}
119

120
int32_t tqMetaSaveOffset(STQ* pTq, STqOffset* pOffset) {
219,143✔
121
  if (pTq == NULL || pOffset == NULL) {
219,143✔
122
    return TSDB_CODE_INVALID_PARA;
×
123
  }
124
  void*    buf = NULL;
219,143✔
125
  int32_t  code = TDB_CODE_SUCCESS;
219,143✔
126
  uint32_t  vlen;
127
  SEncoder encoder = {0};
219,143✔
128
  tEncodeSize(tEncodeSTqOffset, pOffset, vlen, code);
219,143✔
129
  if (code < 0) {
219,143✔
130
    goto END;
×
131
  }
132

133
  buf = taosMemoryCalloc(1, vlen);
219,143✔
134
  if (buf == NULL) {
219,143✔
135
    code = terrno;
×
136
    goto END;
×
137
  }
138

139
  tEncoderInit(&encoder, buf, vlen);
219,143✔
140
  code = tEncodeSTqOffset(&encoder, pOffset);
219,143✔
141
  if (code < 0) {
219,143✔
142
    goto END;
×
143
  }
144

145
  taosWLockLatch(&pTq->lock);
219,143✔
146
  code = tqMetaSaveInfo(pTq, pTq->pOffsetStore, pOffset->subKey, strlen(pOffset->subKey), buf, vlen);
219,143✔
147
  taosWUnLockLatch(&pTq->lock);
218,406✔
148
END:
219,143✔
149
  tEncoderClear(&encoder);
219,143✔
150
  taosMemoryFree(buf);
218,406✔
151
  return code;
219,143✔
152
}
153

154
int32_t tqMetaSaveInfo(STQ* pTq, TTB* ttb, const void* key, uint32_t kLen, const void* value, uint32_t vLen) {
1,470,260✔
155
  if (pTq == NULL || ttb == NULL || key == NULL || value == NULL) {
1,470,260✔
156
    return TSDB_CODE_INVALID_PARA;
×
157
  }
158
  int32_t code = TDB_CODE_SUCCESS;
1,470,314✔
159
  TXN*    txn = NULL;
1,470,314✔
160

161
  TQ_ERR_GO_TO_END(
1,470,314✔
162
      tdbBegin(pTq->pMetaDB, &txn, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED));
163
  TQ_ERR_GO_TO_END(tdbTbUpsert(ttb, key, kLen, value, vLen, txn));
1,467,918✔
164
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
1,469,225✔
165
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
1,466,973✔
166

167
  return 0;
1,465,377✔
168

169
END:
×
170
  tdbAbort(pTq->pMetaDB, txn);
×
171
  return code;
×
172
}
173

174
int32_t tqMetaDeleteInfo(STQ* pTq, TTB* ttb, const void* key, uint32_t kLen) {
578,597✔
175
  if (pTq == NULL || ttb == NULL || key == NULL) {
578,597✔
176
    return TSDB_CODE_INVALID_PARA;
×
177
  }
178
  int32_t code = TDB_CODE_SUCCESS;
579,200✔
179
  TXN*    txn = NULL;
579,200✔
180

181
  TQ_ERR_GO_TO_END(
579,200✔
182
      tdbBegin(pTq->pMetaDB, &txn, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED));
183
  TQ_ERR_GO_TO_END(tdbTbDelete(ttb, key, kLen, txn));
575,840✔
184
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
305,457✔
185
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
306,448✔
186

187
  return 0;
305,127✔
188

189
END:
269,517✔
190
  tdbAbort(pTq->pMetaDB, txn);
269,830✔
191
  return code;
270,385✔
192
}
193

194
int32_t tqMetaGetOffset(STQ* pTq, const char* subkey, STqOffset** pOffset) {
9,782,299✔
195
  if (pTq == NULL || subkey == NULL || pOffset == NULL) {
9,782,299✔
UNCOV
196
    return TSDB_CODE_INVALID_PARA;
×
197
  }
198
  void* data = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
9,782,630✔
199
  if (data == NULL) {
9,782,920✔
200
    int vLen = 0;
822,475✔
201
    taosRLockLatch(&pTq->lock);
822,475✔
202
    if (tdbTbGet(pTq->pOffsetStore, subkey, strlen(subkey), &data, &vLen) < 0) {
822,144✔
203
      taosRUnLockLatch(&pTq->lock);
791,921✔
204
      tdbFree(data);
793,243✔
205
      return TSDB_CODE_OUT_OF_MEMORY;
790,854✔
206
    }
207
    taosRUnLockLatch(&pTq->lock);
28,946✔
208

209
    STqOffset offset = {0};
29,232✔
210
    if (tqMetaDecodeOffsetInfo(&offset, data, vLen >= 0 ? vLen : 0) != TDB_CODE_SUCCESS) {
29,232✔
211
      tdbFree(data);
×
212
      return TSDB_CODE_OUT_OF_MEMORY;
×
213
    }
214

215
    if (taosHashPut(pTq->pOffset, subkey, strlen(subkey), &offset, sizeof(STqOffset)) != 0) {
28,946✔
216
      tDeleteSTqOffset(&offset);
×
217
      tdbFree(data);
×
218
      return terrno;
×
219
    }
220
    tdbFree(data);
29,232✔
221

222
    *pOffset = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
29,232✔
223
    if (*pOffset == NULL) {
29,232✔
224
      return TSDB_CODE_OUT_OF_MEMORY;
×
225
    }
226
  } else {
227
    *pOffset = data;
8,960,445✔
228
  }
229
  return 0;
8,989,324✔
230
}
231

232
int32_t tqMetaSaveHandle(STQ* pTq, const char* key, const STqHandle* pHandle) {
1,227,214✔
233
  if (pTq == NULL || key == NULL || pHandle == NULL) {
1,227,214✔
234
    return TSDB_CODE_INVALID_PARA;
×
235
  }
236
  int32_t  code = TDB_CODE_SUCCESS;
1,227,214✔
237
  uint32_t  vlen;
238
  void*    buf = NULL;
1,227,214✔
239
  SEncoder encoder = {0};
1,227,214✔
240
  tEncodeSize(tEncodeSTqHandle, pHandle, vlen, code);
1,227,214✔
241
  if (code < 0) {
1,226,476✔
242
    goto END;
×
243
  }
244

245
  tqDebug("tq save %s(%d) handle consumer:0x%" PRIx64 " epoch:%d vgId:%d", pHandle->subKey,
1,226,476✔
246
          (int32_t)strlen(pHandle->subKey), pHandle->consumerId, pHandle->epoch, TD_VID(pTq->pVnode));
247

248
  buf = taosMemoryCalloc(1, vlen);
1,227,214✔
249
  if (buf == NULL) {
1,227,214✔
250
    code = terrno;
×
251
    goto END;
×
252
  }
253

254
  tEncoderInit(&encoder, buf, vlen);
1,227,214✔
255
  code = tEncodeSTqHandle(&encoder, pHandle);
1,227,214✔
256
  if (code < 0) {
1,226,874✔
257
    goto END;
×
258
  }
259

260
  TQ_ERR_GO_TO_END(tqMetaSaveInfo(pTq, pTq->pExecStore, key, strlen(key), buf, vlen));
1,226,874✔
261

262
END:
1,221,976✔
263
  tEncoderClear(&encoder);
1,221,637✔
264
  taosMemoryFree(buf);
1,223,787✔
265
  return code;
1,220,267✔
266
}
267

268
static int tqMetaInitHandle(STQ* pTq, STqHandle* handle) {
461,358✔
269
  if (pTq == NULL || handle == NULL) {
461,358✔
270
    return TSDB_CODE_INVALID_PARA;
×
271
  }
272
  int32_t code = TDB_CODE_SUCCESS;
461,358✔
273
  SArray* tbUidList = NULL;
461,358✔
274

275
  SVnode* pVnode = pTq->pVnode;
461,358✔
276
  int32_t vgId = TD_VID(pVnode);
461,358✔
277

278
  handle->pRef = walOpenRef(pVnode->pWal);
461,358✔
279
  TQ_NULL_GO_TO_END(handle->pRef);
460,984✔
280

281
  SReadHandle reader = {0};
460,984✔
282
  reader = (SReadHandle){
460,984✔
283
      .vnode = pVnode,
284
      .initTableReader = true,
285
      .initTqReader = true,
286
      .version = handle->snapshotVer,
460,984✔
287
  };
288

289
  initStorageAPI(&reader.api);
460,984✔
290

291
  if (handle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
460,704✔
292
    handle->execHandle.task = qCreateQueueExecTaskInfo(handle->execHandle.execCol.qmsg, &reader, vgId, handle->consumerId);
342,133✔
293
    TQ_NULL_GO_TO_END(handle->execHandle.task);
342,491✔
294
    void* scanner = NULL;
342,491✔
295
    qExtractTmqScanner(handle->execHandle.task, &scanner);
342,491✔
296
    TQ_NULL_GO_TO_END(scanner);
342,491✔
297
    handle->execHandle.pTqReader = qExtractReaderFromTmqScanner(scanner);
342,491✔
298
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
342,491✔
299
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
118,571✔
300
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
93,820✔
301
    TQ_NULL_GO_TO_END(handle->pWalReader);
93,485✔
302
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
93,485✔
303
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
93,820✔
304
    TQ_ERR_GO_TO_END(buildSnapContext(reader.vnode, reader.version, 0, handle->execHandle.subType, handle->fetchMeta,
93,820✔
305
                                      (SSnapContext**)(&reader.sContext)));
306
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, handle->consumerId);
93,820✔
307
    TQ_NULL_GO_TO_END(handle->execHandle.task);
93,820✔
308
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
24,751✔
309
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
25,047✔
310
    TQ_NULL_GO_TO_END(handle->pWalReader);
25,047✔
311
    if (handle->execHandle.execTb.qmsg != NULL && strcmp(handle->execHandle.execTb.qmsg, "") != 0) {
25,047✔
312
      if (nodesStringToNode(handle->execHandle.execTb.qmsg, &handle->execHandle.execTb.node) != 0) {
9,255✔
313
        tqError("nodesStringToNode error in sub stable, since %s", terrstr());
×
314
        return TSDB_CODE_SCH_INTERNAL_ERROR;
×
315
      }
316
    }
317
    TQ_ERR_GO_TO_END(buildSnapContext(reader.vnode, reader.version, handle->execHandle.execTb.suid,
25,047✔
318
                                      handle->execHandle.subType, handle->fetchMeta,
319
                                      (SSnapContext**)(&reader.sContext)));
320
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, handle->consumerId);
25,047✔
321
    TQ_NULL_GO_TO_END(handle->execHandle.task);
25,047✔
322
    TQ_ERR_GO_TO_END(qGetTableList(handle->execHandle.execTb.suid, pVnode, handle->execHandle.execTb.node, &tbUidList,
25,047✔
323
                                handle->execHandle.task));
324
    tqInfo("vgId:%d, tq try to get ctb for stb subscribe, suid:%" PRId64, pVnode->config.vgId,
25,047✔
325
           handle->execHandle.execTb.suid);
326
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
25,047✔
327
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
25,047✔
328
    TQ_ERR_GO_TO_END(tqReaderSetTbUidList(handle->execHandle.pTqReader, tbUidList, NULL));
25,047✔
329
  }
330
  handle->tableCreateTimeHash = (SHashObj*)taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
461,358✔
331

332
END:
461,018✔
333
  reader.api.snapshotFn.destroySnapshot(reader.sContext);
461,358✔
334
  taosArrayDestroy(tbUidList);
461,018✔
335
  return code;
461,018✔
336
}
337

338
static int32_t tqMetaRestoreHandle(STQ* pTq, void* pVal, uint32_t vLen, STqHandle* handle) {
63,923✔
339
  if (pTq == NULL || pVal == NULL || handle == NULL) {
63,923✔
340
    return TSDB_CODE_INVALID_PARA;
×
341
  }
342
  int32_t  vgId = TD_VID(pTq->pVnode);
63,923✔
343
  SDecoder decoder = {0};
63,923✔
344
  int32_t  code = TDB_CODE_SUCCESS;
63,923✔
345

346
  tDecoderInit(&decoder, (uint8_t*)pVal, vLen);
63,923✔
347
  TQ_ERR_GO_TO_END(tDecodeSTqHandle(&decoder, handle));
63,923✔
348
  TQ_ERR_GO_TO_END(tqMetaInitHandle(pTq, handle));
63,923✔
349
  tqInfo("tqMetaRestoreHandle %s consumer 0x%" PRIx64 " vgId:%d", handle->subKey, handle->consumerId, vgId);
63,923✔
350
  code = taosHashPut(pTq->pHandle, handle->subKey, strlen(handle->subKey), handle, sizeof(STqHandle));
63,923✔
351

352
END:
63,923✔
353
  tDecoderClear(&decoder);
63,923✔
354
  return code;
63,923✔
355
}
356

357
int32_t tqMetaCreateHandle(STQ* pTq, SMqRebVgReq* req, STqHandle* handle) {
397,435✔
358
  if (pTq == NULL || req == NULL || handle == NULL) {
397,435✔
359
    return TSDB_CODE_INVALID_PARA;
×
360
  }
361
  int32_t vgId = TD_VID(pTq->pVnode);
397,435✔
362

363
  (void)memcpy(handle->subKey, req->subKey, TSDB_SUBSCRIBE_KEY_LEN);
397,435✔
364
  handle->consumerId = req->newConsumerId;
397,435✔
365

366
  handle->execHandle.subType = req->subType;
397,435✔
367
  handle->fetchMeta = req->withMeta;
397,435✔
368
  if (req->subType == TOPIC_SUB_TYPE__COLUMN) {
397,435✔
369
    void* tmp = taosStrdup(req->qmsg);
299,378✔
370
    if (tmp == NULL) {
299,378✔
371
      return terrno;
×
372
    }
373
    handle->execHandle.execCol.qmsg = tmp;
299,378✔
374
    handle->execHandle.execCol.pSW = req->schema;
299,378✔
375
    req->schema.pSchema = NULL;
299,378✔
376
  } else if (req->subType == TOPIC_SUB_TYPE__DB) {
98,057✔
377
    handle->execHandle.execDb.pFilterOutTbUid =
79,088✔
378
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
79,088✔
379
    if (handle->execHandle.execDb.pFilterOutTbUid == NULL) {
79,088✔
380
      return terrno;
×
381
    }
382
  } else if (req->subType == TOPIC_SUB_TYPE__TABLE) {
18,969✔
383
    handle->execHandle.execTb.suid = req->suid;
18,969✔
384
    void* tmp = taosStrdup(req->qmsg);
18,969✔
385
    if (tmp == NULL) {
18,969✔
386
      return terrno;
×
387
    }
388
    handle->execHandle.execTb.qmsg = tmp;
18,969✔
389
  }
390

391
  handle->snapshotVer = walGetCommittedVer(pTq->pVnode->pWal);
397,435✔
392

393
  int32_t code = tqMetaInitHandle(pTq, handle);
397,435✔
394
  if (code != 0) {
397,095✔
395
    return code;
×
396
  }
397
  tqInfo("tqMetaCreateHandle %s consumer 0x%" PRIx64 " vgId:%d, snapshotVer:%" PRId64, handle->subKey,
397,095✔
398
         handle->consumerId, vgId, handle->snapshotVer);
399
  return taosHashPut(pTq->pHandle, handle->subKey, strlen(handle->subKey), handle, sizeof(STqHandle));
397,435✔
400
}
401

402
static int32_t tqMetaTransformInfo(TDB* pMetaDB, TTB* pOld, TTB* pNew) {
×
403
  if (pMetaDB == NULL || pOld == NULL || pNew == NULL) {
×
404
    return TSDB_CODE_INVALID_PARA;
×
405
  }
406
  TBC*  pCur = NULL;
×
407
  void* pKey = NULL;
×
408
  int   kLen = 0;
×
409
  void* pVal = NULL;
×
410
  int   vLen = 0;
×
411
  TXN*  txn = NULL;
×
412

413
  int32_t code = TDB_CODE_SUCCESS;
×
414

415
  TQ_ERR_GO_TO_END(tdbTbcOpen(pOld, &pCur, NULL));
×
416
  TQ_ERR_GO_TO_END(
×
417
      tdbBegin(pMetaDB, &txn, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED));
418

419
  TQ_ERR_GO_TO_END(tdbTbcMoveToFirst(pCur));
×
420
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
×
421
    TQ_ERR_GO_TO_END(tdbTbUpsert(pNew, pKey, kLen, pVal, vLen, txn));
×
422
  }
423

424
  TQ_ERR_GO_TO_END(tdbCommit(pMetaDB, txn));
×
425
  TQ_ERR_GO_TO_END(tdbPostCommit(pMetaDB, txn));
×
426

427
END:
×
428
  tdbFree(pKey);
×
429
  tdbFree(pVal);
×
430
  tdbTbcClose(pCur);
×
431
  return code;
×
432
}
433

434
int32_t tqMetaGetHandle(STQ* pTq, const char* key, STqHandle** pHandle) {
17,147,492✔
435
  if (pTq == NULL || key == NULL || pHandle == NULL) {
17,147,492✔
436
    return TSDB_CODE_INVALID_PARA;
11✔
437
  }
438
  void* data = taosHashGet(pTq->pHandle, key, strlen(key));
17,152,571✔
439
  if (data == NULL) {
17,146,773✔
440
    int vLen = 0;
464,082✔
441
    if (tdbTbGet(pTq->pExecStore, key, (int)strlen(key), &data, &vLen) < 0) {
464,082✔
442
      tdbFree(data);
399,876✔
443
      return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
396,639✔
444
    }
445
    STqHandle handle = {0};
63,923✔
446
    int32_t code = tqMetaRestoreHandle(pTq, data, vLen >= 0 ? vLen : 0, &handle);
63,923✔
447
    if (code != 0) {
63,923✔
448
      tdbFree(data);
×
449
      tqDestroyTqHandle(&handle);
×
450
      return code;
×
451
    }
452
    tdbFree(data);
63,923✔
453
    *pHandle = taosHashGet(pTq->pHandle, key, strlen(key));
63,923✔
454
    if (*pHandle == NULL) {
63,923✔
455
      return terrno;
×
456
    }
457
  } else {
458
    *pHandle = data;
16,682,691✔
459
  }
460
  return TDB_CODE_SUCCESS;
16,754,521✔
461
}
462

463
int32_t tqMetaOpenTdb(STQ* pTq) {
4,325,444✔
464
  if (pTq == NULL) {
4,325,444✔
465
    return TSDB_CODE_INVALID_PARA;
×
466
  }
467
  int32_t code = TDB_CODE_SUCCESS;
4,325,444✔
468
  TQ_ERR_GO_TO_END(tdbOpen(pTq->path, 16 * 1024, 1, &pTq->pMetaDB, 0, NULL));
4,325,444✔
469
  TQ_ERR_GO_TO_END(tdbTbOpen("tq.db", -1, -1, NULL, pTq->pMetaDB, &pTq->pExecStore, 0));
4,326,744✔
470
  TQ_ERR_GO_TO_END(tdbTbOpen("tq.offset.db", -1, -1, NULL, pTq->pMetaDB, &pTq->pOffsetStore, 0));
4,326,111✔
471

472
END:
4,326,868✔
473
  if (code != TDB_CODE_SUCCESS) {
4,326,868✔
NEW
474
    if (pTq->pExecStore) {
×
NEW
475
      tdbTbClose(pTq->pExecStore);
×
NEW
476
      pTq->pExecStore = NULL;
×
477
    }
NEW
478
    if (pTq->pOffsetStore) {
×
NEW
479
      tdbTbClose(pTq->pOffsetStore);
×
NEW
480
      pTq->pOffsetStore = NULL;
×
481
    }
NEW
482
    tdbClose(pTq->pMetaDB);
×
NEW
483
    pTq->pMetaDB = NULL;
×
484
  }
485
  return code;
4,326,411✔
486
}
487

488
static int32_t replaceTqPath(char** path) {
4,326,744✔
489
  if (path == NULL || *path == NULL) {
4,326,744✔
490
    return TSDB_CODE_INVALID_PARA;
×
491
  }
492
  char*   tpath = NULL;
4,327,155✔
493
  int32_t code = tqBuildFName(&tpath, *path, TQ_SUBSCRIBE_NAME);
4,326,338✔
494
  if (code != 0) {
4,326,766✔
495
    return code;
×
496
  }
497
  taosMemoryFree(*path);
4,326,766✔
498
  *path = tpath;
4,326,590✔
499
  return TDB_CODE_SUCCESS;
4,326,590✔
500
}
501

502
int32_t tqMetaOpen(STQ* pTq) {
4,323,585✔
503
  if (pTq == NULL) {
4,323,585✔
504
    return TSDB_CODE_INVALID_PARA;
×
505
  }
506
  char*   maindb = NULL;
4,323,585✔
507
  char*   offsetNew = NULL;
4,327,761✔
508
  int32_t code = TDB_CODE_SUCCESS;
4,327,761✔
509
  TQ_ERR_GO_TO_END(tqBuildFName(&maindb, pTq->path, TDB_MAINDB_NAME));
4,327,761✔
510
  if (!taosCheckExistFile(maindb)) {
4,326,744✔
511
    TQ_ERR_GO_TO_END(replaceTqPath(&pTq->path));
4,327,761✔
512
    TQ_ERR_GO_TO_END(tqMetaOpenTdb(pTq));
4,327,304✔
513
  } else {
514
    TQ_ERR_GO_TO_END(tqMetaTransform(pTq));
×
515
    TQ_ERR_GO_TO_END(taosRemoveFile(maindb));
×
516
  }
517

518
  TQ_ERR_GO_TO_END(tqBuildFName(&offsetNew, pTq->path, TQ_OFFSET_NAME));
4,325,623✔
519
  if (taosCheckExistFile(offsetNew)) {
4,327,149✔
520
    TQ_ERR_GO_TO_END(tqOffsetRestoreFromFile(pTq, offsetNew));
×
521
    TQ_ERR_GO_TO_END(taosRemoveFile(offsetNew));
×
522
  }
523

524
END:
4,326,868✔
525
  taosMemoryFree(maindb);
4,327,055✔
526
  taosMemoryFree(offsetNew);
4,325,602✔
527
  return code;
4,326,462✔
528
}
529

530
int32_t tqMetaTransform(STQ* pTq) {
×
531
  if (pTq == NULL) {
×
532
    return TSDB_CODE_INVALID_PARA;
×
533
  }
534
  int32_t code = TDB_CODE_SUCCESS;
×
535
  TDB*    pMetaDB = NULL;
×
536
  TTB*    pExecStore = NULL;
×
537
  char*   offsetNew = NULL;
×
538
  char*   offset = NULL;
×
539
  TQ_ERR_GO_TO_END(tqBuildFName(&offset, pTq->path, TQ_OFFSET_NAME));
×
540

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

544
  TQ_ERR_GO_TO_END(replaceTqPath(&pTq->path));
×
545
  TQ_ERR_GO_TO_END(tqMetaOpenTdb(pTq));
×
546

547
  TQ_ERR_GO_TO_END(tqMetaTransformInfo(pTq->pMetaDB, pExecStore, pTq->pExecStore));
×
548

549
  TQ_ERR_GO_TO_END(tqBuildFName(&offsetNew, pTq->path, TQ_OFFSET_NAME));
×
550

551
  if (taosCheckExistFile(offset)) {
×
552
    if (taosCopyFile(offset, offsetNew) < 0) {
×
553
      tqError("copy offset file error");
×
554
    } else {
555
      TQ_ERR_GO_TO_END(taosRemoveFile(offset));
×
556
    }
557
  }
558

559
END:
×
560
  taosMemoryFree(offset);
×
561
  taosMemoryFree(offsetNew);
×
562

563
  tdbTbClose(pExecStore);
×
564
  tdbClose(pMetaDB);
×
565
  return code;
×
566
}
567

568
void tqMetaClose(STQ* pTq) {
4,327,389✔
569
  if (pTq == NULL) {
4,327,389✔
570
    return;
×
571
  }
572
  int32_t ret = 0;
4,327,389✔
573
  if (pTq->pExecStore) {
4,327,389✔
574
    tdbTbClose(pTq->pExecStore);
4,327,761✔
575
  }
576
  if (pTq->pOffsetStore) {
4,327,956✔
577
    tdbTbClose(pTq->pOffsetStore);
4,327,956✔
578
  }
579
  tdbClose(pTq->pMetaDB);
4,327,956✔
580
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc