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

taosdata / TDengine / #4864

26 Nov 2025 05:46AM UTC coverage: 64.548% (+0.009%) from 64.539%
#4864

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

769 of 945 new or added lines in 33 files covered. (81.38%)

3006 existing lines in 116 files now uncovered.

158227 of 245129 relevant lines covered (64.55%)

111826500.07 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 tEncodeSTqHandle(SEncoder* pEncoder, const STqHandle* pHandle) {
1,002,115✔
19
  if (pEncoder == NULL || pHandle == NULL) {
1,002,115✔
20
    return TSDB_CODE_INVALID_PARA;
4✔
21
  }
22
  int32_t code = 0;
1,002,111✔
23
  int32_t lino;
24

25
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
1,002,111✔
26
  TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->subKey));
2,004,200✔
27
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->fetchMeta));
2,004,105✔
28
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->consumerId));
2,003,671✔
29
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->snapshotVer));
2,003,264✔
30
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pHandle->epoch));
2,003,541✔
31
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->execHandle.subType));
2,003,870✔
32
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
1,001,938✔
33
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execCol.qmsg));
1,813,807✔
34
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
95,013✔
35
    int32_t size = taosHashGetSize(pHandle->execHandle.execDb.pFilterOutTbUid);
80,513✔
36
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, size));
80,500✔
37
    void* pIter = NULL;
80,500✔
38
    pIter = taosHashIterate(pHandle->execHandle.execDb.pFilterOutTbUid, pIter);
80,500✔
39
    while (pIter) {
80,359✔
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) {
14,516✔
45
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->execHandle.execTb.suid));
29,032✔
46
    if (pHandle->execHandle.execTb.qmsg != NULL) {
14,516✔
47
      TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execTb.qmsg));
28,840✔
48
    }
49
  }
50
  tEndEncode(pEncoder);
1,001,629✔
51
_exit:
1,001,944✔
52
  if (code) {
1,001,944✔
53
    return code;
×
54
  } else {
55
    return pEncoder->pos;
1,001,944✔
56
  }
57
}
58

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

66
  TAOS_CHECK_EXIT(tStartDecode(pDecoder));
4,805✔
67
  TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pHandle->subKey));
4,805✔
68
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pHandle->fetchMeta));
9,610✔
69
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->consumerId));
9,610✔
70
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->snapshotVer));
9,610✔
71
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pHandle->epoch));
9,610✔
72
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pHandle->execHandle.subType));
9,610✔
73
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
4,805✔
74
    TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pHandle->execHandle.execCol.qmsg));
7,210✔
75
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
1,200✔
76
    pHandle->execHandle.execDb.pFilterOutTbUid =
428✔
77
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
428✔
78
    if (pHandle->execHandle.execDb.pFilterOutTbUid == NULL) {
428✔
79
      TAOS_CHECK_EXIT(terrno);
×
80
    }
81
    int32_t size = 0;
428✔
82
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &size));
428✔
83
    for (int32_t i = 0; i < size; i++) {
428✔
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) {
772✔
89
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->execHandle.execTb.suid));
1,544✔
90
    if (!tDecodeIsEnd(pDecoder)) {
772✔
91
      TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pHandle->execHandle.execTb.qmsg));
1,544✔
92
    }
93
  }
94
  tEndDecode(pDecoder);
4,805✔
95

96
_exit:
4,805✔
97
  return code;
4,805✔
98
}
99

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

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

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

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

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

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

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

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

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

173
  TQ_ERR_GO_TO_END(
639,269✔
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));
638,425✔
176
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
639,043✔
177
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
638,515✔
178

179
  return 0;
637,315✔
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) {
194,814✔
187
  if (pTq == NULL || ttb == NULL || key == NULL) {
194,814✔
188
    return TSDB_CODE_INVALID_PARA;
×
189
  }
190
  int32_t code = TDB_CODE_SUCCESS;
194,814✔
191
  TXN*    txn = NULL;
194,814✔
192

193
  TQ_ERR_GO_TO_END(
194,814✔
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));
194,683✔
196
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
101,807✔
197
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
101,748✔
198

199
  return 0;
101,563✔
200

201
END:
92,911✔
202
  tdbAbort(pTq->pMetaDB, txn);
92,612✔
203
  return code;
92,948✔
204
}
205

206
int32_t tqMetaGetOffset(STQ* pTq, const char* subkey, STqOffset** pOffset) {
821,666✔
207
  if (pTq == NULL || subkey == NULL || pOffset == NULL) {
821,666✔
208
    return TSDB_CODE_INVALID_PARA;
×
209
  }
210
  void* data = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
821,666✔
211
  if (data == NULL) {
821,666✔
212
    int vLen = 0;
301,165✔
213
    taosRLockLatch(&pTq->lock);
301,165✔
214
    if (tdbTbGet(pTq->pOffsetStore, subkey, strlen(subkey), &data, &vLen) < 0) {
301,165✔
215
      taosRUnLockLatch(&pTq->lock);
298,903✔
216
      tdbFree(data);
299,361✔
217
      return TSDB_CODE_OUT_OF_MEMORY;
298,321✔
218
    }
219
    taosRUnLockLatch(&pTq->lock);
1,616✔
220

221
    STqOffset offset = {0};
1,616✔
222
    if (tqMetaDecodeOffsetInfo(&offset, data, vLen >= 0 ? vLen : 0) != TDB_CODE_SUCCESS) {
1,616✔
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,616✔
228
      tDeleteSTqOffset(&offset);
×
229
      tdbFree(data);
×
230
      return terrno;
×
231
    }
232
    tdbFree(data);
1,616✔
233

234
    *pOffset = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
1,616✔
235
    if (*pOffset == NULL) {
1,616✔
236
      return TSDB_CODE_OUT_OF_MEMORY;
×
237
    }
238
  } else {
239
    *pOffset = data;
520,501✔
240
  }
241
  return 0;
522,117✔
242
}
243

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

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

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

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

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

274
END:
498,727✔
275
  tEncoderClear(&encoder);
497,524✔
276
  taosMemoryFree(buf);
500,059✔
277
  return code;
497,938✔
278
}
279

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

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

290
  handle->pRef = walOpenRef(pVnode->pWal);
141,664✔
291
  TQ_NULL_GO_TO_END(handle->pRef);
141,492✔
292

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

301
  initStorageAPI(&reader.api);
141,495✔
302

303
  if (handle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
141,283✔
304
    handle->execHandle.task = qCreateQueueExecTaskInfo(handle->execHandle.execCol.qmsg, &reader, vgId,
118,668✔
305
                                                       &handle->execHandle.numOfCols, handle->consumerId);
118,481✔
306
    TQ_NULL_GO_TO_END(handle->execHandle.task);
118,760✔
307
    void* scanner = NULL;
118,760✔
308
    qExtractTmqScanner(handle->execHandle.task, &scanner);
118,760✔
309
    TQ_NULL_GO_TO_END(scanner);
118,760✔
310
    handle->execHandle.pTqReader = qExtractReaderFromTmqScanner(scanner);
118,760✔
311
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
118,760✔
312
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
22,956✔
313
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
18,843✔
314
    TQ_NULL_GO_TO_END(handle->pWalReader);
18,843✔
315
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
18,843✔
316
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
18,843✔
317
    TQ_ERR_GO_TO_END(buildSnapContext(reader.vnode, reader.version, 0, handle->execHandle.subType, handle->fetchMeta,
18,843✔
318
                                      (SSnapContext**)(&reader.sContext)));
319
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, NULL, handle->consumerId);
18,843✔
320
    TQ_NULL_GO_TO_END(handle->execHandle.task);
18,843✔
321
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
4,152✔
322
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
4,152✔
323
    TQ_NULL_GO_TO_END(handle->pWalReader);
4,152✔
324
    if (handle->execHandle.execTb.qmsg != NULL && strcmp(handle->execHandle.execTb.qmsg, "") != 0) {
4,152✔
325
      if (nodesStringToNode(handle->execHandle.execTb.qmsg, &handle->execHandle.execTb.node) != 0) {
2,062✔
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,
4,152✔
331
                                      handle->execHandle.subType, handle->fetchMeta,
332
                                      (SSnapContext**)(&reader.sContext)));
333
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, NULL, handle->consumerId);
4,152✔
334
    TQ_NULL_GO_TO_END(handle->execHandle.task);
4,152✔
335
    TQ_ERR_GO_TO_END(qGetTableList(handle->execHandle.execTb.suid, pVnode, handle->execHandle.execTb.node, &tbUidList,
4,152✔
336
                                handle->execHandle.task));
337
    tqInfo("vgId:%d, tq try to get ctb for stb subscribe, suid:%" PRId64, pVnode->config.vgId,
4,152✔
338
           handle->execHandle.execTb.suid);
339
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
4,152✔
340
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
4,152✔
341
    TQ_ERR_GO_TO_END(tqReaderSetTbUidList(handle->execHandle.pTqReader, tbUidList, NULL));
4,152✔
342
  }
343
  handle->tableCreateTimeHash = (SHashObj*)taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
141,755✔
344

345
END:
141,755✔
346
  taosArrayDestroy(tbUidList);
141,755✔
347
  return code;
141,755✔
348
}
349

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

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

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

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

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

378
  handle->execHandle.subType = req->subType;
137,826✔
379
  handle->fetchMeta = req->withMeta;
137,826✔
380
  if (req->subType == TOPIC_SUB_TYPE__COLUMN) {
137,826✔
381
    void* tmp = taosStrdup(req->qmsg);
115,743✔
382
    if (tmp == NULL) {
115,743✔
383
      return terrno;
×
384
    }
385
    handle->execHandle.execCol.qmsg = tmp;
115,743✔
386
  } else if (req->subType == TOPIC_SUB_TYPE__DB) {
22,083✔
387
    handle->execHandle.execDb.pFilterOutTbUid =
18,511✔
388
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
18,511✔
389
    if (handle->execHandle.execDb.pFilterOutTbUid == NULL) {
18,511✔
390
      return terrno;
×
391
    }
392
  } else if (req->subType == TOPIC_SUB_TYPE__TABLE) {
3,572✔
393
    handle->execHandle.execTb.suid = req->suid;
3,572✔
394
    void* tmp = taosStrdup(req->qmsg);
3,572✔
395
    if (tmp == NULL) {
3,572✔
396
      return terrno;
×
397
    }
398
    handle->execHandle.execTb.qmsg = tmp;
3,572✔
399
  }
400

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

403
  int32_t code = tqMetaInitHandle(pTq, handle);
137,742✔
404
  if (code != 0) {
137,826✔
405
    return code;
×
406
  }
407
  tqInfo("tqMetaCreateHandle %s consumer 0x%" PRIx64 " vgId:%d, snapshotVer:%" PRId64, handle->subKey,
137,826✔
408
         handle->consumerId, vgId, handle->snapshotVer);
409
  return taosHashPut(pTq->pHandle, handle->subKey, strlen(handle->subKey), handle, sizeof(STqHandle));
137,826✔
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) {
5,887,682✔
445
  if (pTq == NULL || key == NULL || pHandle == NULL) {
5,887,682✔
446
    return TSDB_CODE_INVALID_PARA;
×
447
  }
448
  void* data = taosHashGet(pTq->pHandle, key, strlen(key));
5,889,072✔
449
  if (data == NULL) {
5,888,037✔
450
    int vLen = 0;
141,785✔
451
    if (tdbTbGet(pTq->pExecStore, key, (int)strlen(key), &data, &vLen) < 0) {
141,785✔
452
      tdbFree(data);
137,542✔
453
      return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
137,254✔
454
    }
455
    STqHandle handle = {0};
3,929✔
456
    int32_t code = tqMetaRestoreHandle(pTq, data, vLen >= 0 ? vLen : 0, &handle);
3,929✔
457
    if (code != 0) {
3,929✔
458
      tdbFree(data);
×
459
      tqDestroyTqHandle(&handle);
×
460
      return code;
×
461
    }
462
    tdbFree(data);
3,929✔
463
    *pHandle = taosHashGet(pTq->pHandle, key, strlen(key));
3,929✔
464
    if (*pHandle == NULL) {
3,929✔
465
      return terrno;
×
466
    }
467
  } else {
468
    *pHandle = data;
5,746,252✔
469
  }
470
  return TDB_CODE_SUCCESS;
5,751,182✔
471
}
472

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

483
END:
4,169,524✔
484
  return code;
4,169,524✔
485
}
486

487
static int32_t replaceTqPath(char** path) {
4,167,606✔
488
  if (path == NULL || *path == NULL) {
4,167,606✔
489
    return TSDB_CODE_INVALID_PARA;
×
490
  }
491
  char*   tpath = NULL;
4,167,704✔
492
  int32_t code = tqBuildFName(&tpath, *path, TQ_SUBSCRIBE_NAME);
4,168,732✔
493
  if (code != 0) {
4,169,398✔
494
    return code;
×
495
  }
496
  taosMemoryFree(*path);
4,169,398✔
497
  *path = tpath;
4,168,771✔
498
  return TDB_CODE_SUCCESS;
4,168,771✔
499
}
500

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

513
  TQ_ERR_GO_TO_END(tdbTbcOpen(pTq->pCheckStore, &pCur, NULL));
4,168,866✔
514
  TQ_ERR_GO_TO_END(tdbTbcMoveToFirst(pCur));
4,167,828✔
515

516
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
4,171,510✔
517
    TQ_ERR_GO_TO_END(tqMetaDecodeCheckInfo(&info, pVal, vLen >= 0 ? vLen : 0));
3,861✔
518
    TQ_ERR_GO_TO_END(taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo)));
4,318✔
519
  }
520
  info.colIdList = NULL;
4,168,657✔
521

522
END:
4,168,657✔
523
  tdbFree(pKey);
4,167,245✔
524
  tdbFree(pVal);
4,168,437✔
525
  tdbTbcClose(pCur);
4,168,692✔
526
  tDeleteSTqCheckInfo(&info);
4,167,857✔
527
  return code;
4,169,265✔
528
}
529

530
int32_t tqMetaOpen(STQ* pTq) {
4,165,858✔
531
  if (pTq == NULL) {
4,165,858✔
532
    return TSDB_CODE_INVALID_PARA;
×
533
  }
534
  char*   maindb = NULL;
4,165,858✔
535
  char*   offsetNew = NULL;
4,169,494✔
536
  int32_t code = TDB_CODE_SUCCESS;
4,169,524✔
537
  TQ_ERR_GO_TO_END(tqBuildFName(&maindb, pTq->path, TDB_MAINDB_NAME));
4,169,524✔
538
  if (!taosCheckExistFile(maindb)) {
4,168,805✔
539
    TQ_ERR_GO_TO_END(replaceTqPath(&pTq->path));
4,169,524✔
540
    TQ_ERR_GO_TO_END(tqMetaOpenTdb(pTq));
4,168,052✔
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,168,866✔
547
  if (taosCheckExistFile(offsetNew)) {
4,167,507✔
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,169,524✔
553

554
END:
4,168,611✔
555
  taosMemoryFree(maindb);
4,169,524✔
556
  taosMemoryFree(offsetNew);
4,169,289✔
557
  return code;
4,169,030✔
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, 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,167,680✔
603
  if (pTq == NULL) {
4,167,680✔
604
    return;
×
605
  }
606
  int32_t ret = 0;
4,167,680✔
607
  if (pTq->pExecStore) {
4,167,680✔
608
    tdbTbClose(pTq->pExecStore);
4,169,524✔
609
  }
610
  if (pTq->pCheckStore) {
4,169,494✔
611
    tdbTbClose(pTq->pCheckStore);
4,169,494✔
612
  }
613
  if (pTq->pOffsetStore) {
4,169,524✔
614
    tdbTbClose(pTq->pOffsetStore);
4,169,524✔
615
  }
616
  tdbClose(pTq->pMetaDB);
4,169,494✔
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