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

taosdata / TDengine / #4847

11 Nov 2025 05:50AM UTC coverage: 62.651% (+0.3%) from 62.306%
#4847

push

travis-ci

web-flow
Merge e78cd6509 into 47a2ea7a0

542 of 650 new or added lines in 16 files covered. (83.38%)

1515 existing lines in 91 files now uncovered.

113826 of 181682 relevant lines covered (62.65%)

113230552.12 hits per line

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

72.81
/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,175,805✔
19
  if (pEncoder == NULL || pHandle == NULL) {
1,175,805✔
20
    return TSDB_CODE_INVALID_PARA;
×
21
  }
22
  int32_t code = 0;
1,175,963✔
23
  int32_t lino;
24

25
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
1,175,963✔
26
  TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->subKey));
2,351,904✔
27
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->fetchMeta));
2,351,766✔
28
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->consumerId));
2,351,808✔
29
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->snapshotVer));
2,351,753✔
30
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pHandle->epoch));
2,351,393✔
31
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->execHandle.subType));
2,351,461✔
32
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
1,175,895✔
33
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execCol.qmsg));
2,197,235✔
34
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
77,265✔
35
    int32_t size = taosHashGetSize(pHandle->execHandle.execDb.pFilterOutTbUid);
65,290✔
36
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, size));
65,233✔
37
    void* pIter = NULL;
65,233✔
38
    pIter = taosHashIterate(pHandle->execHandle.execDb.pFilterOutTbUid, pIter);
65,233✔
39
    while (pIter) {
65,233✔
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,032✔
45
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->execHandle.execTb.suid));
24,064✔
46
    if (pHandle->execHandle.execTb.qmsg != NULL) {
12,032✔
47
      TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execTb.qmsg));
24,064✔
48
    }
49
  }
50
  tEndEncode(pEncoder);
1,175,802✔
51
_exit:
1,175,662✔
52
  if (code) {
1,175,662✔
53
    return code;
×
54
  } else {
55
    return pEncoder->pos;
1,175,662✔
56
  }
57
}
58

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

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

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

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

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

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

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

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

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

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

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

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

173
  TQ_ERR_GO_TO_END(
787,510✔
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));
787,060✔
176
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
787,299✔
177
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
786,422✔
178

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

193
  TQ_ERR_GO_TO_END(
174,319✔
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));
173,990✔
196
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
91,853✔
197
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
91,735✔
198

199
  return 0;
91,871✔
200

201
END:
82,166✔
202
  tdbAbort(pTq->pMetaDB, txn);
81,970✔
203
  return code;
82,250✔
204
}
205

206
int32_t tqMetaGetOffset(STQ* pTq, const char* subkey, STqOffset** pOffset) {
765,796✔
207
  if (pTq == NULL || subkey == NULL || pOffset == NULL) {
765,796✔
208
    return TSDB_CODE_INVALID_PARA;
×
209
  }
210
  void* data = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
765,796✔
211
  if (data == NULL) {
765,796✔
212
    int vLen = 0;
259,196✔
213
    taosRLockLatch(&pTq->lock);
259,196✔
214
    if (tdbTbGet(pTq->pOffsetStore, subkey, strlen(subkey), &data, &vLen) < 0) {
259,196✔
215
      taosRUnLockLatch(&pTq->lock);
257,493✔
216
      tdbFree(data);
257,903✔
217
      return TSDB_CODE_OUT_OF_MEMORY;
257,045✔
218
    }
219
    taosRUnLockLatch(&pTq->lock);
1,160✔
220

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

234
    *pOffset = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
1,160✔
235
    if (*pOffset == NULL) {
1,160✔
236
      return TSDB_CODE_OUT_OF_MEMORY;
×
237
    }
238
  } else {
239
    *pOffset = data;
506,600✔
240
  }
241
  return 0;
507,760✔
242
}
243

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

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

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

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

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

274
END:
586,473✔
275
  tEncoderClear(&encoder);
585,757✔
276
  taosMemoryFree(buf);
586,738✔
277
  return code;
585,558✔
278
}
279

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

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

123,218✔
290
  handle->pRef = walOpenRef(pVnode->pWal);
123,196✔
291
  TQ_NULL_GO_TO_END(handle->pRef);
292

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

301
  initStorageAPI(&reader.api);
122,196✔
302

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

3,360✔
345
END:
3,360✔
346
  taosArrayDestroy(tbUidList);
3,360✔
347
  return code;
348
}
123,161✔
349

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

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

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

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

120,512✔
UNCOV
375
  (void)memcpy(handle->subKey, req->subKey, TSDB_SUBSCRIBE_KEY_LEN);
×
376
  handle->consumerId = req->newConsumerId;
377

120,619✔
378
  handle->execHandle.subType = req->subType;
379
  handle->fetchMeta = req->withMeta;
120,619✔
380
  if (req->subType == TOPIC_SUB_TYPE__COLUMN) {
120,596✔
381
    void* tmp = taosStrdup(req->qmsg);
382
    if (tmp == NULL) {
120,583✔
383
      return terrno;
120,583✔
384
    }
120,499✔
385
    handle->execHandle.execCol.qmsg = tmp;
102,599✔
386
  } else if (req->subType == TOPIC_SUB_TYPE__DB) {
102,683✔
UNCOV
387
    handle->execHandle.execDb.pFilterOutTbUid =
×
388
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
389
    if (handle->execHandle.execDb.pFilterOutTbUid == NULL) {
102,683✔
390
      return terrno;
17,900✔
391
    }
14,958✔
392
  } else if (req->subType == TOPIC_SUB_TYPE__TABLE) {
14,922✔
393
    handle->execHandle.execTb.suid = req->suid;
14,958✔
UNCOV
394
    void* tmp = taosStrdup(req->qmsg);
×
395
    if (tmp == NULL) {
396
      return terrno;
2,978✔
397
    }
2,978✔
398
    handle->execHandle.execTb.qmsg = tmp;
2,978✔
399
  }
2,978✔
UNCOV
400

×
401
  handle->snapshotVer = walGetCommittedVer(pTq->pVnode->pWal);
402

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

120,619✔
412
static int32_t tqMetaTransformInfo(TDB* pMetaDB, TTB* pOld, TTB* pNew) {
413
  if (pMetaDB == NULL || pOld == NULL || pNew == NULL) {
120,619✔
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;
×
UNCOV
422

×
423
  int32_t code = TDB_CODE_SUCCESS;
×
UNCOV
424

×
425
  TQ_ERR_GO_TO_END(tdbTbcOpen(pOld, &pCur, NULL));
×
426
  TQ_ERR_GO_TO_END(
UNCOV
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
  }
UNCOV
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;
×
UNCOV
442
}
×
UNCOV
443

×
UNCOV
444
int32_t tqMetaGetHandle(STQ* pTq, const char* key, STqHandle** pHandle) {
×
UNCOV
445
  if (pTq == NULL || key == NULL || pHandle == NULL) {
×
446
    return TSDB_CODE_INVALID_PARA;
447
  }
448
  void* data = taosHashGet(pTq->pHandle, key, strlen(key));
4,989,517✔
449
  if (data == NULL) {
4,989,517✔
450
    int vLen = 0;
344✔
451
    if (tdbTbGet(pTq->pExecStore, key, (int)strlen(key), &data, &vLen) < 0) {
452
      tdbFree(data);
4,990,152✔
453
      return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
4,989,544✔
454
    }
123,654✔
455
    STqHandle handle = {0};
123,654✔
456
    int32_t code = tqMetaRestoreHandle(pTq, data, vLen >= 0 ? vLen : 0, &handle);
120,951✔
457
    if (code != 0) {
120,786✔
458
      tdbFree(data);
459
      tqDestroyTqHandle(&handle);
2,599✔
460
      return code;
2,599✔
461
    }
2,599✔
UNCOV
462
    tdbFree(data);
×
UNCOV
463
    *pHandle = taosHashGet(pTq->pHandle, key, strlen(key));
×
UNCOV
464
    if (*pHandle == NULL) {
×
465
      return terrno;
466
    }
2,599✔
467
  } else {
2,599✔
468
    *pHandle = data;
2,599✔
UNCOV
469
  }
×
470
  return TDB_CODE_SUCCESS;
471
}
472

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

4,001,428✔
483
END:
4,003,295✔
484
  return code;
4,003,321✔
485
}
4,003,762✔
486

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

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

4,002,755✔
513
  TQ_ERR_GO_TO_END(tdbTbcOpen(pTq->pCheckStore, &pCur, NULL));
4,001,673✔
514
  TQ_ERR_GO_TO_END(tdbTbcMoveToFirst(pCur));
4,002,754✔
515

4,002,754✔
516
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
517
    TQ_ERR_GO_TO_END(tqMetaDecodeCheckInfo(&info, pVal, vLen >= 0 ? vLen : 0));
4,003,505✔
518
    TQ_ERR_GO_TO_END(taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo)));
4,002,755✔
519
  }
520
  info.colIdList = NULL;
4,005,303✔
521

3,967✔
522
END:
3,490✔
523
  tdbFree(pKey);
524
  tdbFree(pVal);
4,002,278✔
525
  tdbTbcClose(pCur);
526
  tDeleteSTqCheckInfo(&info);
4,002,278✔
527
  return code;
4,003,028✔
528
}
4,002,208✔
529

4,001,789✔
530
int32_t tqMetaOpen(STQ* pTq) {
4,003,033✔
531
  if (pTq == NULL) {
4,002,368✔
532
    return TSDB_CODE_INVALID_PARA;
533
  }
534
  char*   maindb = NULL;
3,999,567✔
535
  char*   offsetNew = NULL;
3,999,567✔
UNCOV
536
  int32_t code = TDB_CODE_SUCCESS;
×
537
  TQ_ERR_GO_TO_END(tqBuildFName(&maindb, pTq->path, TDB_MAINDB_NAME));
538
  if (!taosCheckExistFile(maindb)) {
3,999,567✔
539
    TQ_ERR_GO_TO_END(replaceTqPath(&pTq->path));
4,003,762✔
540
    TQ_ERR_GO_TO_END(tqMetaOpenTdb(pTq));
4,003,762✔
541
  } else {
4,003,762✔
542
    TQ_ERR_GO_TO_END(tqMetaTransform(pTq));
4,003,703✔
543
    TQ_ERR_GO_TO_END(taosRemoveFile(maindb));
4,003,069✔
544
  }
4,002,764✔
545

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

4,003,762✔
UNCOV
552
  TQ_ERR_GO_TO_END(tqMetaRestoreCheckInfo(pTq));
×
UNCOV
553

×
554
END:
555
  taosMemoryFree(maindb);
556
  taosMemoryFree(offsetNew);
4,003,762✔
557
  return code;
558
}
4,003,505✔
559

4,003,505✔
560
int32_t tqMetaTransform(STQ* pTq) {
4,003,448✔
561
  if (pTq == NULL) {
4,001,074✔
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));
×
UNCOV
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));
×
UNCOV
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));
×
UNCOV
581

×
582
  TQ_ERR_GO_TO_END(tqBuildFName(&offsetNew, pTq->path, TQ_OFFSET_NAME));
UNCOV
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));
×
UNCOV
589
    }
×
UNCOV
590
  }
×
591

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

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

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