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

taosdata / TDengine / #4829

30 Oct 2025 09:25AM UTC coverage: 49.734% (-11.3%) from 61.071%
#4829

push

travis-ci

web-flow
Merge pull request #33435 from taosdata/3.0

merge 3.0

123072 of 323930 branches covered (37.99%)

Branch coverage included in aggregate %.

7 of 25 new or added lines in 3 files covered. (28.0%)

35232 existing lines in 327 files now uncovered.

172062 of 269495 relevant lines covered (63.85%)

70709785.06 hits per line

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

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

25
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
168,088!
26
  TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->subKey));
336,176!
27
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->fetchMeta));
336,176!
28
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->consumerId));
336,176!
29
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->snapshotVer));
336,176!
30
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pHandle->epoch));
335,816!
31
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->execHandle.subType));
335,269!
32
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
167,541✔
33
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execCol.qmsg));
278,235!
34
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
28,788✔
35
    int32_t size = taosHashGetSize(pHandle->execHandle.execDb.pFilterOutTbUid);
24,228✔
36
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, size));
24,228!
37
    void* pIter = NULL;
24,228✔
38
    pIter = taosHashIterate(pHandle->execHandle.execDb.pFilterOutTbUid, pIter);
24,228✔
39
    while (pIter) {
24,228!
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) {
4,560!
45
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->execHandle.execTb.suid));
9,120!
46
    if (pHandle->execHandle.execTb.qmsg != NULL) {
4,560!
47
      TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execTb.qmsg));
9,120!
48
    }
49
  }
50
  tEndEncode(pEncoder);
168,088✔
51
_exit:
167,910✔
52
  if (code) {
167,910!
53
    return code;
×
54
  } else {
55
    return pEncoder->pos;
167,910✔
56
  }
57
}
58

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

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

96
_exit:
1,450✔
97
  return code;
1,450✔
98
}
99

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

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

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

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

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

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

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

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

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

173
  TQ_ERR_GO_TO_END(
87,476!
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));
87,654!
176
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
87,654!
177
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
86,930!
178

179
  return 0;
86,580✔
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) {
61,641✔
187
  if (pTq == NULL || ttb == NULL || key == NULL) {
61,641!
188
    return TSDB_CODE_INVALID_PARA;
×
189
  }
190
  int32_t code = TDB_CODE_SUCCESS;
61,797✔
191
  TXN*    txn = NULL;
61,797✔
192

193
  TQ_ERR_GO_TO_END(
61,797!
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));
61,285✔
196
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
31,264!
197
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
31,435!
198

199
  return 0;
31,620✔
200

201
END:
29,486✔
202
  tdbAbort(pTq->pMetaDB, txn);
29,668✔
203
  return code;
29,312✔
204
}
205

206
int32_t tqMetaGetOffset(STQ* pTq, const char* subkey, STqOffset** pOffset) {
506,925✔
207
  if (pTq == NULL || subkey == NULL || pOffset == NULL) {
506,925!
208
    return TSDB_CODE_INVALID_PARA;
×
209
  }
210
  void* data = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
506,925!
211
  if (data == NULL) {
506,925✔
212
    int vLen = 0;
87,727✔
213
    taosRLockLatch(&pTq->lock);
87,727✔
214
    if (tdbTbGet(pTq->pOffsetStore, subkey, strlen(subkey), &data, &vLen) < 0) {
87,727!
215
      taosRUnLockLatch(&pTq->lock);
87,103✔
216
      tdbFree(data);
87,361✔
217
      return TSDB_CODE_OUT_OF_MEMORY;
86,734✔
218
    }
219
    taosRUnLockLatch(&pTq->lock);
366✔
220

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

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

234
    *pOffset = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
366!
235
    if (*pOffset == NULL) {
366!
236
      return TSDB_CODE_OUT_OF_MEMORY;
×
237
    }
238
  } else {
239
    *pOffset = data;
419,198✔
240
  }
241
  return 0;
419,564✔
242
}
243

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

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

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

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

272
  TQ_ERR_GO_TO_END(tqMetaSaveInfo(pTq, pTq->pExecStore, key, strlen(key), buf, vlen));
83,862!
273

274
END:
82,814✔
275
  tEncoderClear(&encoder);
82,294✔
276
  taosMemoryFree(buf);
83,699!
277
  return code;
82,138✔
278
}
279

280
static int tqMetaInitHandle(STQ* pTq, STqHandle* handle) {
39,271✔
281
  if (pTq == NULL || handle == NULL) {
39,271!
282
    return TSDB_CODE_INVALID_PARA;
×
283
  }
284
  int32_t code = TDB_CODE_SUCCESS;
39,271✔
285

286
  SVnode* pVnode = pTq->pVnode;
39,271✔
287
  int32_t vgId = TD_VID(pVnode);
39,271✔
288

289
  handle->pRef = walOpenRef(pVnode->pWal);
39,271✔
290
  TQ_NULL_GO_TO_END(handle->pRef);
39,271!
291

292
  SReadHandle reader = {
39,271✔
293
      .vnode = pVnode,
294
      .initTableReader = true,
295
      .initTqReader = true,
296
      .version = handle->snapshotVer,
39,271✔
297
  };
298

299
  initStorageAPI(&reader.api);
39,271✔
300

301
  if (handle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
39,082✔
302
    handle->execHandle.task = qCreateQueueExecTaskInfo(handle->execHandle.execCol.qmsg, &reader, vgId,
31,337✔
303
                                                       &handle->execHandle.numOfCols, handle->consumerId);
31,337✔
304
    TQ_NULL_GO_TO_END(handle->execHandle.task);
31,337!
305
    void* scanner = NULL;
31,337✔
306
    qExtractTmqScanner(handle->execHandle.task, &scanner);
31,337✔
307
    TQ_NULL_GO_TO_END(scanner);
31,159!
308
    handle->execHandle.pTqReader = qExtractReaderFromTmqScanner(scanner);
31,159✔
309
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
31,337!
310
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
7,745✔
311
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
6,422✔
312
    TQ_NULL_GO_TO_END(handle->pWalReader);
6,422!
313
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
6,422✔
314
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
6,422!
315
    TQ_ERR_GO_TO_END(buildSnapContext(reader.vnode, reader.version, 0, handle->execHandle.subType, handle->fetchMeta,
6,422!
316
                                      (SSnapContext**)(&reader.sContext)));
317
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, NULL, handle->consumerId);
6,422✔
318
    TQ_NULL_GO_TO_END(handle->execHandle.task);
6,422!
319
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
1,323!
320
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
1,512✔
321
    TQ_NULL_GO_TO_END(handle->pWalReader);
1,512!
322
    if (handle->execHandle.execTb.qmsg != NULL && strcmp(handle->execHandle.execTb.qmsg, "") != 0) {
1,512!
UNCOV
323
      if (nodesStringToNode(handle->execHandle.execTb.qmsg, &handle->execHandle.execTb.node) != 0) {
×
324
        tqError("nodesStringToNode error in sub stable, since %s", terrstr());
×
325
        return TSDB_CODE_SCH_INTERNAL_ERROR;
×
326
      }
327
    }
328
    TQ_ERR_GO_TO_END(buildSnapContext(reader.vnode, reader.version, handle->execHandle.execTb.suid,
1,512!
329
                                      handle->execHandle.subType, handle->fetchMeta,
330
                                      (SSnapContext**)(&reader.sContext)));
331
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, NULL, handle->consumerId);
1,512✔
332
    TQ_NULL_GO_TO_END(handle->execHandle.task);
1,512!
333
    SArray* tbUidList = NULL;
1,512✔
334
    int     ret = qGetTableList(handle->execHandle.execTb.suid, pVnode, handle->execHandle.execTb.node, &tbUidList,
1,512✔
335
                                handle->execHandle.task);
336
    if (ret != TDB_CODE_SUCCESS) {
1,512!
337
      tqError("qGetTableList error:%d handle %s consumer:0x%" PRIx64, ret, handle->subKey, handle->consumerId);
×
338
      taosArrayDestroy(tbUidList);
×
339
      return TSDB_CODE_SCH_INTERNAL_ERROR;
×
340
    }
341
    tqInfo("vgId:%d, tq try to get ctb for stb subscribe, suid:%" PRId64, pVnode->config.vgId,
1,512!
342
           handle->execHandle.execTb.suid);
343
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
1,512✔
344
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
1,512!
345
    tqReaderSetTbUidList(handle->execHandle.pTqReader, tbUidList, NULL);
1,512✔
346
    taosArrayDestroy(tbUidList);
1,512✔
347
  }
348
  handle->tableCreateTimeHash = (SHashObj*)taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
38,904✔
349

350
END:
39,271✔
351
  return code;
39,271✔
352
}
353

354
static int32_t tqMetaRestoreHandle(STQ* pTq, void* pVal, uint32_t vLen, STqHandle* handle) {
1,450✔
355
  if (pTq == NULL || pVal == NULL || handle == NULL) {
1,450!
356
    return TSDB_CODE_INVALID_PARA;
×
357
  }
358
  int32_t  vgId = TD_VID(pTq->pVnode);
1,450✔
359
  SDecoder decoder = {0};
1,450✔
360
  int32_t  code = TDB_CODE_SUCCESS;
1,450✔
361

362
  tDecoderInit(&decoder, (uint8_t*)pVal, vLen);
1,450✔
363
  TQ_ERR_GO_TO_END(tDecodeSTqHandle(&decoder, handle));
1,450!
364
  TQ_ERR_GO_TO_END(tqMetaInitHandle(pTq, handle));
1,450!
365
  tqInfo("tqMetaRestoreHandle %s consumer 0x%" PRIx64 " vgId:%d", handle->subKey, handle->consumerId, vgId);
1,450!
366
  code = taosHashPut(pTq->pHandle, handle->subKey, strlen(handle->subKey), handle, sizeof(STqHandle));
1,450!
367

368
END:
1,450✔
369
  tDecoderClear(&decoder);
1,450✔
370
  return code;
1,450✔
371
}
372

373
int32_t tqMetaCreateHandle(STQ* pTq, SMqRebVgReq* req, STqHandle* handle) {
37,821✔
374
  if (pTq == NULL || req == NULL || handle == NULL) {
37,821!
375
    return TSDB_CODE_INVALID_PARA;
×
376
  }
377
  int32_t vgId = TD_VID(pTq->pVnode);
37,821✔
378

379
  (void)memcpy(handle->subKey, req->subKey, TSDB_SUBSCRIBE_KEY_LEN);
37,821!
380
  handle->consumerId = req->newConsumerId;
37,821✔
381

382
  handle->execHandle.subType = req->subType;
37,821✔
383
  handle->fetchMeta = req->withMeta;
37,821✔
384
  if (req->subType == TOPIC_SUB_TYPE__COLUMN) {
37,821✔
385
    void* tmp = taosStrdup(req->qmsg);
30,617!
386
    if (tmp == NULL) {
30,617!
387
      return terrno;
×
388
    }
389
    handle->execHandle.execCol.qmsg = tmp;
30,617✔
390
  } else if (req->subType == TOPIC_SUB_TYPE__DB) {
7,204✔
391
    handle->execHandle.execDb.pFilterOutTbUid =
6,058✔
392
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
6,058✔
393
    if (handle->execHandle.execDb.pFilterOutTbUid == NULL) {
6,058!
394
      return terrno;
×
395
    }
396
  } else if (req->subType == TOPIC_SUB_TYPE__TABLE) {
1,146!
397
    handle->execHandle.execTb.suid = req->suid;
1,146✔
398
    void* tmp = taosStrdup(req->qmsg);
1,146!
399
    if (tmp == NULL) {
1,146!
400
      return terrno;
×
401
    }
402
    handle->execHandle.execTb.qmsg = tmp;
1,146✔
403
  }
404

405
  handle->snapshotVer = walGetCommittedVer(pTq->pVnode->pWal);
37,821✔
406

407
  int32_t code = tqMetaInitHandle(pTq, handle);
37,821✔
408
  if (code != 0) {
37,821!
409
    return code;
×
410
  }
411
  tqInfo("tqMetaCreateHandle %s consumer 0x%" PRIx64 " vgId:%d, snapshotVer:%" PRId64, handle->subKey,
37,821!
412
         handle->consumerId, vgId, handle->snapshotVer);
413
  return taosHashPut(pTq->pHandle, handle->subKey, strlen(handle->subKey), handle, sizeof(STqHandle));
37,821!
414
}
415

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

427
  int32_t code = TDB_CODE_SUCCESS;
×
428

429
  TQ_ERR_GO_TO_END(tdbTbcOpen(pOld, &pCur, NULL));
×
430
  TQ_ERR_GO_TO_END(
×
431
      tdbBegin(pMetaDB, &txn, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED));
432

433
  TQ_ERR_GO_TO_END(tdbTbcMoveToFirst(pCur));
×
434
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
×
435
    TQ_ERR_GO_TO_END(tdbTbUpsert(pNew, pKey, kLen, pVal, vLen, txn));
×
436
  }
437

438
  TQ_ERR_GO_TO_END(tdbCommit(pMetaDB, txn));
×
439
  TQ_ERR_GO_TO_END(tdbPostCommit(pMetaDB, txn));
×
440

441
END:
×
442
  tdbFree(pKey);
×
443
  tdbFree(pVal);
×
444
  tdbTbcClose(pCur);
×
445
  return code;
×
446
}
447

448
int32_t tqMetaGetHandle(STQ* pTq, const char* key, STqHandle** pHandle) {
2,191,168✔
449
  if (pTq == NULL || key == NULL || pHandle == NULL) {
2,191,168!
450
    return TSDB_CODE_INVALID_PARA;
×
451
  }
452
  void* data = taosHashGet(pTq->pHandle, key, strlen(key));
2,191,739!
453
  if (data == NULL) {
2,191,362✔
454
    int vLen = 0;
39,271✔
455
    if (tdbTbGet(pTq->pExecStore, key, (int)strlen(key), &data, &vLen) < 0) {
39,271!
456
      tdbFree(data);
37,821✔
457
      return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
37,821✔
458
    }
459
    STqHandle handle = {0};
1,450✔
460
    int32_t code = tqMetaRestoreHandle(pTq, data, vLen >= 0 ? vLen : 0, &handle);
1,450✔
461
    if (code != 0) {
1,450!
462
      tdbFree(data);
×
463
      tqDestroyTqHandle(&handle);
×
464
      return code;
×
465
    }
466
    tdbFree(data);
1,450✔
467
    *pHandle = taosHashGet(pTq->pHandle, key, strlen(key));
1,450!
468
    if (*pHandle == NULL) {
1,450!
469
      return terrno;
×
470
    }
471
  } else {
472
    *pHandle = data;
2,152,091✔
473
  }
474
  return TDB_CODE_SUCCESS;
2,153,929✔
475
}
476

477
int32_t tqMetaOpenTdb(STQ* pTq) {
699,043✔
478
  if (pTq == NULL) {
699,043!
479
    return TSDB_CODE_INVALID_PARA;
×
480
  }
481
  int32_t code = TDB_CODE_SUCCESS;
699,043✔
482
  TQ_ERR_GO_TO_END(tdbOpen(pTq->path, 16 * 1024, 1, &pTq->pMetaDB, 0, 0, NULL));
699,043!
483
  TQ_ERR_GO_TO_END(tdbTbOpen("tq.db", -1, -1, NULL, pTq->pMetaDB, &pTq->pExecStore, 0));
698,254!
484
  TQ_ERR_GO_TO_END(tdbTbOpen("tq.check.db", -1, -1, NULL, pTq->pMetaDB, &pTq->pCheckStore, 0));
698,108!
485
  TQ_ERR_GO_TO_END(tdbTbOpen("tq.offset.db", -1, -1, NULL, pTq->pMetaDB, &pTq->pOffsetStore, 0));
698,263!
486

487
END:
698,713✔
488
  return code;
698,713✔
489
}
490

491
static int32_t replaceTqPath(char** path) {
699,043✔
492
  if (path == NULL || *path == NULL) {
699,043!
493
    return TSDB_CODE_INVALID_PARA;
×
494
  }
495
  char*   tpath = NULL;
699,043✔
496
  int32_t code = tqBuildFName(&tpath, *path, TQ_SUBSCRIBE_NAME);
699,043✔
497
  if (code != 0) {
699,043!
498
    return code;
×
499
  }
500
  taosMemoryFree(*path);
699,043!
501
  *path = tpath;
698,643✔
502
  return TDB_CODE_SUCCESS;
698,643✔
503
}
504

505
static int32_t tqMetaRestoreCheckInfo(STQ* pTq) {
698,887✔
506
  if (pTq == NULL) {
698,887!
507
    return TSDB_CODE_INVALID_PARA;
×
508
  }
509
  TBC*         pCur = NULL;
698,887✔
510
  void*        pKey = NULL;
698,887✔
511
  int          kLen = 0;
698,887✔
512
  void*        pVal = NULL;
698,887✔
513
  int          vLen = 0;
699,043✔
514
  int32_t      code = 0;
699,043✔
515
  STqCheckInfo info = {0};
699,043✔
516

517
  TQ_ERR_GO_TO_END(tdbTbcOpen(pTq->pCheckStore, &pCur, NULL));
699,043!
518
  TQ_ERR_GO_TO_END(tdbTbcMoveToFirst(pCur));
698,887!
519

520
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
700,663✔
521
    TQ_ERR_GO_TO_END(tqMetaDecodeCheckInfo(&info, pVal, vLen >= 0 ? vLen : 0));
1,939!
522
    TQ_ERR_GO_TO_END(taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo)));
1,950!
523
  }
524
  info.colIdList = NULL;
698,363✔
525

526
END:
698,363✔
527
  tdbFree(pKey);
698,519✔
528
  tdbFree(pVal);
698,360✔
529
  tdbTbcClose(pCur);
698,508✔
530
  tDeleteSTqCheckInfo(&info);
698,887✔
531
  return code;
698,713✔
532
}
533

534
int32_t tqMetaOpen(STQ* pTq) {
698,420✔
535
  if (pTq == NULL) {
698,420!
536
    return TSDB_CODE_INVALID_PARA;
×
537
  }
538
  char*   maindb = NULL;
698,420✔
539
  char*   offsetNew = NULL;
699,043✔
540
  int32_t code = TDB_CODE_SUCCESS;
699,043✔
541
  TQ_ERR_GO_TO_END(tqBuildFName(&maindb, pTq->path, TDB_MAINDB_NAME));
699,043!
542
  if (!taosCheckExistFile(maindb)) {
698,872!
543
    TQ_ERR_GO_TO_END(replaceTqPath(&pTq->path));
699,043!
544
    TQ_ERR_GO_TO_END(tqMetaOpenTdb(pTq));
698,643!
545
  } else {
546
    TQ_ERR_GO_TO_END(tqMetaTransform(pTq));
×
547
    TQ_ERR_GO_TO_END(taosRemoveFile(maindb));
×
548
  }
549

550
  TQ_ERR_GO_TO_END(tqBuildFName(&offsetNew, pTq->path, TQ_OFFSET_NAME));
698,713!
551
  if (taosCheckExistFile(offsetNew)) {
698,887!
552
    TQ_ERR_GO_TO_END(tqOffsetRestoreFromFile(pTq, offsetNew));
×
553
    TQ_ERR_GO_TO_END(taosRemoveFile(offsetNew));
×
554
  }
555

556
  TQ_ERR_GO_TO_END(tqMetaRestoreCheckInfo(pTq));
699,043!
557

558
END:
698,887✔
559
  taosMemoryFree(maindb);
698,887!
560
  taosMemoryFree(offsetNew);
698,539!
561
  return code;
698,698✔
562
}
563

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

576
  TQ_ERR_GO_TO_END(tdbOpen(pTq->path, 16 * 1024, 1, &pMetaDB, 0, 0, NULL));
×
577
  TQ_ERR_GO_TO_END(tdbTbOpen("tq.db", -1, -1, NULL, pMetaDB, &pExecStore, 0));
×
578
  TQ_ERR_GO_TO_END(tdbTbOpen("tq.check.db", -1, -1, NULL, pMetaDB, &pCheckStore, 0));
×
579

580
  TQ_ERR_GO_TO_END(replaceTqPath(&pTq->path));
×
581
  TQ_ERR_GO_TO_END(tqMetaOpenTdb(pTq));
×
582

583
  TQ_ERR_GO_TO_END(tqMetaTransformInfo(pTq->pMetaDB, pExecStore, pTq->pExecStore));
×
584
  TQ_ERR_GO_TO_END(tqMetaTransformInfo(pTq->pMetaDB, pCheckStore, pTq->pCheckStore));
×
585

586
  TQ_ERR_GO_TO_END(tqBuildFName(&offsetNew, pTq->path, TQ_OFFSET_NAME));
×
587

588
  if (taosCheckExistFile(offset)) {
×
589
    if (taosCopyFile(offset, offsetNew) < 0) {
×
590
      tqError("copy offset file error");
×
591
    } else {
592
      TQ_ERR_GO_TO_END(taosRemoveFile(offset));
×
593
    }
594
  }
595

596
END:
×
597
  taosMemoryFree(offset);
×
598
  taosMemoryFree(offsetNew);
×
599

600
  tdbTbClose(pExecStore);
×
601
  tdbTbClose(pCheckStore);
×
602
  tdbClose(pMetaDB);
×
603
  return code;
×
604
}
605

606
void tqMetaClose(STQ* pTq) {
699,043✔
607
  if (pTq == NULL) {
699,043!
608
    return;
×
609
  }
610
  int32_t ret = 0;
699,043✔
611
  if (pTq->pExecStore) {
699,043!
612
    tdbTbClose(pTq->pExecStore);
699,043✔
613
  }
614
  if (pTq->pCheckStore) {
699,043!
615
    tdbTbClose(pTq->pCheckStore);
699,043✔
616
  }
617
  if (pTq->pOffsetStore) {
699,043!
618
    tdbTbClose(pTq->pOffsetStore);
699,043✔
619
  }
620
  tdbClose(pTq->pMetaDB);
699,043✔
621
}
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