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

taosdata / TDengine / #4860

22 Nov 2025 07:23AM UTC coverage: 64.335% (+0.06%) from 64.272%
#4860

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

0 of 79 new or added lines in 2 files covered. (0.0%)

559 existing lines in 108 files now uncovered.

154614 of 240326 relevant lines covered (64.34%)

112619890.95 hits per line

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

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

25
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
933,612✔
26
  TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->subKey));
1,866,698✔
27
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->fetchMeta));
1,866,772✔
28
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->consumerId));
1,866,876✔
29
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->snapshotVer));
1,866,676✔
30
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pHandle->epoch));
1,866,857✔
31
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->execHandle.subType));
1,865,774✔
32
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
932,329✔
33
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execCol.qmsg));
1,708,397✔
34
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
79,046✔
35
    int32_t size = taosHashGetSize(pHandle->execHandle.execDb.pFilterOutTbUid);
67,088✔
36
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, size));
67,088✔
37
    void* pIter = NULL;
67,088✔
38
    pIter = taosHashIterate(pHandle->execHandle.execDb.pFilterOutTbUid, pIter);
67,088✔
39
    while (pIter) {
67,088✔
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) {
11,958✔
45
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->execHandle.execTb.suid));
23,916✔
46
    if (pHandle->execHandle.execTb.qmsg != NULL) {
11,958✔
47
      TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execTb.qmsg));
23,916✔
48
    }
49
  }
50
  tEndEncode(pEncoder);
933,429✔
51
_exit:
933,420✔
52
  if (code) {
933,420✔
53
    return code;
×
54
  } else {
55
    return pEncoder->pos;
933,420✔
56
  }
57
}
58

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

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

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

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

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

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

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

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

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

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

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

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

173
  TQ_ERR_GO_TO_END(
602,256✔
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));
602,078✔
176
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
602,119✔
177
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
600,531✔
178

179
  return 0;
600,773✔
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) {
174,913✔
187
  if (pTq == NULL || ttb == NULL || key == NULL) {
174,913✔
UNCOV
188
    return TSDB_CODE_INVALID_PARA;
×
189
  }
190
  int32_t code = TDB_CODE_SUCCESS;
175,402✔
191
  TXN*    txn = NULL;
175,402✔
192

193
  TQ_ERR_GO_TO_END(
175,402✔
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));
175,311✔
196
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
91,758✔
197
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
91,777✔
198

199
  return 0;
91,742✔
200

201
END:
83,468✔
202
  tdbAbort(pTq->pMetaDB, txn);
83,468✔
203
  return code;
83,402✔
204
}
205

206
int32_t tqMetaGetOffset(STQ* pTq, const char* subkey, STqOffset** pOffset) {
723,040✔
207
  if (pTq == NULL || subkey == NULL || pOffset == NULL) {
723,040✔
208
    return TSDB_CODE_INVALID_PARA;
×
209
  }
210
  void* data = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
723,040✔
211
  if (data == NULL) {
723,040✔
212
    int vLen = 0;
263,479✔
213
    taosRLockLatch(&pTq->lock);
263,479✔
214
    if (tdbTbGet(pTq->pOffsetStore, subkey, strlen(subkey), &data, &vLen) < 0) {
263,569✔
215
      taosRUnLockLatch(&pTq->lock);
261,870✔
216
      tdbFree(data);
261,886✔
217
      return TSDB_CODE_OUT_OF_MEMORY;
261,120✔
218
    }
219
    taosRUnLockLatch(&pTq->lock);
1,546✔
220

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

234
    *pOffset = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
1,546✔
235
    if (*pOffset == NULL) {
1,546✔
236
      return TSDB_CODE_OUT_OF_MEMORY;
×
237
    }
238
  } else {
239
    *pOffset = data;
459,561✔
240
  }
241
  return 0;
461,107✔
242
}
243

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

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

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

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

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

274
END:
466,129✔
275
  tEncoderClear(&encoder);
465,270✔
276
  taosMemoryFree(buf);
466,457✔
277
  return code;
464,891✔
278
}
279

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

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

290
  handle->pRef = walOpenRef(pVnode->pWal);
125,342✔
291
  TQ_NULL_GO_TO_END(handle->pRef);
125,169✔
292

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

301
  initStorageAPI(&reader.api);
124,781✔
302

303
  if (handle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
124,975✔
304
    handle->execHandle.task = qCreateQueueExecTaskInfo(handle->execHandle.execCol.qmsg, &reader, vgId,
106,286✔
305
                                                       &handle->execHandle.numOfCols, handle->consumerId);
106,036✔
306
    TQ_NULL_GO_TO_END(handle->execHandle.task);
106,243✔
307
    void* scanner = NULL;
106,243✔
308
    qExtractTmqScanner(handle->execHandle.task, &scanner);
106,243✔
309
    TQ_NULL_GO_TO_END(scanner);
106,243✔
310
    handle->execHandle.pTqReader = qExtractReaderFromTmqScanner(scanner);
106,243✔
311
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
106,243✔
312
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
18,980✔
313
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
15,778✔
314
    TQ_NULL_GO_TO_END(handle->pWalReader);
15,804✔
315
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
15,752✔
316
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
15,859✔
317
    TQ_ERR_GO_TO_END(buildSnapContext(reader.vnode, reader.version, 0, handle->execHandle.subType, handle->fetchMeta,
15,859✔
318
                                      (SSnapContext**)(&reader.sContext)));
319
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, NULL, handle->consumerId);
15,859✔
320
    TQ_NULL_GO_TO_END(handle->execHandle.task);
15,859✔
321
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
3,240✔
322
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
3,240✔
323
    TQ_NULL_GO_TO_END(handle->pWalReader);
3,240✔
324
    if (handle->execHandle.execTb.qmsg != NULL && strcmp(handle->execHandle.execTb.qmsg, "") != 0) {
3,170✔
325
      if (nodesStringToNode(handle->execHandle.execTb.qmsg, &handle->execHandle.execTb.node) != 0) {
1,744✔
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,310✔
331
                                      handle->execHandle.subType, handle->fetchMeta,
332
                                      (SSnapContext**)(&reader.sContext)));
333
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, NULL, handle->consumerId);
3,240✔
334
    TQ_NULL_GO_TO_END(handle->execHandle.task);
3,240✔
335
    TQ_ERR_GO_TO_END(qGetTableList(handle->execHandle.execTb.suid, pVnode, handle->execHandle.execTb.node, &tbUidList,
3,240✔
336
                                handle->execHandle.task));
337
    tqInfo("vgId:%d, tq try to get ctb for stb subscribe, suid:%" PRId64, pVnode->config.vgId,
3,240✔
338
           handle->execHandle.execTb.suid);
339
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
3,240✔
340
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
3,240✔
341
    TQ_ERR_GO_TO_END(tqReaderSetTbUidList(handle->execHandle.pTqReader, tbUidList, NULL));
3,240✔
342
  }
343
  handle->tableCreateTimeHash = (SHashObj*)taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
125,272✔
344

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

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

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

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

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

375
  (void)memcpy(handle->subKey, req->subKey, TSDB_SUBSCRIBE_KEY_LEN);
122,705✔
376
  handle->consumerId = req->newConsumerId;
122,757✔
377

378
  handle->execHandle.subType = req->subType;
122,757✔
379
  handle->fetchMeta = req->withMeta;
122,705✔
380
  if (req->subType == TOPIC_SUB_TYPE__COLUMN) {
122,705✔
381
    void* tmp = taosStrdup(req->qmsg);
104,266✔
382
    if (tmp == NULL) {
104,266✔
383
      return terrno;
×
384
    }
385
    handle->execHandle.execCol.qmsg = tmp;
104,266✔
386
  } else if (req->subType == TOPIC_SUB_TYPE__DB) {
18,491✔
387
    handle->execHandle.execDb.pFilterOutTbUid =
15,500✔
388
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
15,448✔
389
    if (handle->execHandle.execDb.pFilterOutTbUid == NULL) {
15,500✔
390
      return terrno;
×
391
    }
392
  } else if (req->subType == TOPIC_SUB_TYPE__TABLE) {
2,991✔
393
    handle->execHandle.execTb.suid = req->suid;
2,991✔
394
    void* tmp = taosStrdup(req->qmsg);
2,991✔
395
    if (tmp == NULL) {
2,991✔
396
      return terrno;
×
397
    }
398
    handle->execHandle.execTb.qmsg = tmp;
2,991✔
399
  }
400

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

403
  int32_t code = tqMetaInitHandle(pTq, handle);
122,672✔
404
  if (code != 0) {
122,757✔
405
    return code;
×
406
  }
407
  tqInfo("tqMetaCreateHandle %s consumer 0x%" PRIx64 " vgId:%d, snapshotVer:%" PRId64, handle->subKey,
122,757✔
408
         handle->consumerId, vgId, handle->snapshotVer);
409
  return taosHashPut(pTq->pHandle, handle->subKey, strlen(handle->subKey), handle, sizeof(STqHandle));
122,757✔
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) {
4,977,776✔
445
  if (pTq == NULL || key == NULL || pHandle == NULL) {
4,977,776✔
UNCOV
446
    return TSDB_CODE_INVALID_PARA;
×
447
  }
448
  void* data = taosHashGet(pTq->pHandle, key, strlen(key));
4,979,730✔
449
  if (data == NULL) {
4,979,181✔
450
    int vLen = 0;
125,428✔
451
    if (tdbTbGet(pTq->pExecStore, key, (int)strlen(key), &data, &vLen) < 0) {
125,428✔
452
      tdbFree(data);
123,258✔
453
      return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
123,215✔
454
    }
455
    STqHandle handle = {0};
2,585✔
456
    int32_t code = tqMetaRestoreHandle(pTq, data, vLen >= 0 ? vLen : 0, &handle);
2,585✔
457
    if (code != 0) {
2,585✔
458
      tdbFree(data);
×
459
      tqDestroyTqHandle(&handle);
×
460
      return code;
×
461
    }
462
    tdbFree(data);
2,585✔
463
    *pHandle = taosHashGet(pTq->pHandle, key, strlen(key));
2,585✔
464
    if (*pHandle == NULL) {
2,585✔
465
      return terrno;
×
466
    }
467
  } else {
468
    *pHandle = data;
4,853,753✔
469
  }
470
  return TDB_CODE_SUCCESS;
4,856,400✔
471
}
472

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

483
END:
4,066,107✔
484
  return code;
4,066,107✔
485
}
486

487
static int32_t replaceTqPath(char** path) {
4,066,905✔
488
  if (path == NULL || *path == NULL) {
4,066,905✔
489
    return TSDB_CODE_INVALID_PARA;
×
490
  }
491
  char*   tpath = NULL;
4,066,905✔
492
  int32_t code = tqBuildFName(&tpath, *path, TQ_SUBSCRIBE_NAME);
4,067,446✔
493
  if (code != 0) {
4,068,222✔
494
    return code;
×
495
  }
496
  taosMemoryFree(*path);
4,068,222✔
497
  *path = tpath;
4,068,139✔
498
  return TDB_CODE_SUCCESS;
4,068,139✔
499
}
500

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

513
  TQ_ERR_GO_TO_END(tdbTbcOpen(pTq->pCheckStore, &pCur, NULL));
4,067,520✔
514
  TQ_ERR_GO_TO_END(tdbTbcMoveToFirst(pCur));
4,066,701✔
515

516
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
4,070,809✔
517
    TQ_ERR_GO_TO_END(tqMetaDecodeCheckInfo(&info, pVal, vLen >= 0 ? vLen : 0));
3,391✔
518
    TQ_ERR_GO_TO_END(taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo)));
3,752✔
519
  }
520
  info.colIdList = NULL;
4,066,665✔
521

522
END:
4,066,665✔
523
  tdbFree(pKey);
4,067,646✔
524
  tdbFree(pVal);
4,065,868✔
525
  tdbTbcClose(pCur);
4,065,829✔
526
  tDeleteSTqCheckInfo(&info);
4,067,080✔
527
  return code;
4,067,541✔
528
}
529

530
int32_t tqMetaOpen(STQ* pTq) {
4,061,966✔
531
  if (pTq == NULL) {
4,061,966✔
532
    return TSDB_CODE_INVALID_PARA;
×
533
  }
534
  char*   maindb = NULL;
4,061,966✔
535
  char*   offsetNew = NULL;
4,067,935✔
536
  int32_t code = TDB_CODE_SUCCESS;
4,068,222✔
537
  TQ_ERR_GO_TO_END(tqBuildFName(&maindb, pTq->path, TDB_MAINDB_NAME));
4,068,222✔
538
  if (!taosCheckExistFile(maindb)) {
4,067,555✔
539
    TQ_ERR_GO_TO_END(replaceTqPath(&pTq->path));
4,067,759✔
540
    TQ_ERR_GO_TO_END(tqMetaOpenTdb(pTq));
4,068,139✔
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,064,704✔
547
  if (taosCheckExistFile(offsetNew)) {
4,066,517✔
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,067,178✔
553

554
END:
4,066,043✔
555
  taosMemoryFree(maindb);
4,066,686✔
556
  taosMemoryFree(offsetNew);
4,067,049✔
557
  return code;
4,063,580✔
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,067,934✔
603
  if (pTq == NULL) {
4,067,934✔
604
    return;
×
605
  }
606
  int32_t ret = 0;
4,067,934✔
607
  if (pTq->pExecStore) {
4,067,934✔
608
    tdbTbClose(pTq->pExecStore);
4,067,934✔
609
  }
610
  if (pTq->pCheckStore) {
4,067,934✔
611
    tdbTbClose(pTq->pCheckStore);
4,067,934✔
612
  }
613
  if (pTq->pOffsetStore) {
4,067,313✔
614
    tdbTbClose(pTq->pOffsetStore);
4,067,934✔
615
  }
616
  tdbClose(pTq->pMetaDB);
4,066,692✔
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