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

taosdata / TDengine / #4861

22 Nov 2025 07:23AM UTC coverage: 64.274% (-0.06%) from 64.335%
#4861

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%)

820 existing lines in 129 files now uncovered.

154466 of 240326 relevant lines covered (64.27%)

112821527.61 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,153,128✔
19
  if (pEncoder == NULL || pHandle == NULL) {
1,153,128✔
20
    return TSDB_CODE_INVALID_PARA;
×
21
  }
22
  int32_t code = 0;
1,153,245✔
23
  int32_t lino;
24

25
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
1,153,245✔
26
  TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->subKey));
2,306,454✔
27
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->fetchMeta));
2,306,515✔
28
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->consumerId));
2,306,495✔
29
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->snapshotVer));
2,306,475✔
30
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pHandle->epoch));
2,306,423✔
31
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->execHandle.subType));
2,306,342✔
32
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
1,153,205✔
33
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execCol.qmsg));
2,141,515✔
34
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
82,043✔
35
    int32_t size = taosHashGetSize(pHandle->execHandle.execDb.pFilterOutTbUid);
69,500✔
36
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, size));
69,480✔
37
    void* pIter = NULL;
69,480✔
38
    pIter = taosHashIterate(pHandle->execHandle.execDb.pFilterOutTbUid, pIter);
69,480✔
39
    while (pIter) {
69,419✔
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,644✔
45
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->execHandle.execTb.suid));
25,288✔
46
    if (pHandle->execHandle.execTb.qmsg != NULL) {
12,644✔
47
      TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execTb.qmsg));
25,288✔
48
    }
49
  }
50
  tEndEncode(pEncoder);
1,152,664✔
51
_exit:
1,152,666✔
52
  if (code) {
1,152,666✔
53
    return code;
×
54
  } else {
55
    return pEncoder->pos;
1,152,666✔
56
  }
57
}
58

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

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

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

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

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

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

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

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

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

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

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

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

173
  TQ_ERR_GO_TO_END(
763,940✔
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));
763,173✔
176
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
763,706✔
177
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
763,314✔
178

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

193
  TQ_ERR_GO_TO_END(
181,612✔
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));
181,247✔
196
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
94,999✔
197
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
95,020✔
198

199
  return 0;
95,100✔
200

201
END:
86,282✔
202
  tdbAbort(pTq->pMetaDB, txn);
86,315✔
203
  return code;
86,451✔
204
}
205

206
int32_t tqMetaGetOffset(STQ* pTq, const char* subkey, STqOffset** pOffset) {
794,068✔
207
  if (pTq == NULL || subkey == NULL || pOffset == NULL) {
794,068✔
208
    return TSDB_CODE_INVALID_PARA;
84✔
209
  }
210
  void* data = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
793,984✔
211
  if (data == NULL) {
794,068✔
212
    int vLen = 0;
272,265✔
213
    taosRLockLatch(&pTq->lock);
272,265✔
214
    if (tdbTbGet(pTq->pOffsetStore, subkey, strlen(subkey), &data, &vLen) < 0) {
272,229✔
215
      taosRUnLockLatch(&pTq->lock);
270,546✔
216
      tdbFree(data);
270,368✔
217
      return TSDB_CODE_OUT_OF_MEMORY;
269,600✔
218
    }
219
    taosRUnLockLatch(&pTq->lock);
1,353✔
220

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

234
    *pOffset = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
1,353✔
235
    if (*pOffset == NULL) {
1,353✔
236
      return TSDB_CODE_OUT_OF_MEMORY;
×
237
    }
238
  } else {
239
    *pOffset = data;
521,803✔
240
  }
241
  return 0;
523,156✔
242
}
243

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

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

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

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

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

274
END:
575,621✔
275
  tEncoderClear(&encoder);
576,050✔
276
  taosMemoryFree(buf);
576,123✔
277
  return code;
575,272✔
278
}
279

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

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

290
  handle->pRef = walOpenRef(pVnode->pWal);
128,956✔
291
  TQ_NULL_GO_TO_END(handle->pRef);
128,871✔
292

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

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

303
  if (handle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
128,753✔
304
    handle->execHandle.task = qCreateQueueExecTaskInfo(handle->execHandle.execCol.qmsg, &reader, vgId,
109,201✔
305
                                                       &handle->execHandle.numOfCols, handle->consumerId);
109,012✔
306
    TQ_NULL_GO_TO_END(handle->execHandle.task);
109,081✔
307
    void* scanner = NULL;
109,081✔
308
    qExtractTmqScanner(handle->execHandle.task, &scanner);
109,081✔
309
    TQ_NULL_GO_TO_END(scanner);
109,081✔
310
    handle->execHandle.pTqReader = qExtractReaderFromTmqScanner(scanner);
109,081✔
311
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
109,081✔
312
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
19,631✔
313
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
16,281✔
314
    TQ_NULL_GO_TO_END(handle->pWalReader);
16,395✔
315
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
16,366✔
316
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
16,395✔
317
    TQ_ERR_GO_TO_END(buildSnapContext(reader.vnode, reader.version, 0, handle->execHandle.subType, handle->fetchMeta,
16,395✔
318
                                      (SSnapContext**)(&reader.sContext)));
319
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, NULL, handle->consumerId);
16,395✔
320
    TQ_NULL_GO_TO_END(handle->execHandle.task);
16,375✔
321
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
3,394✔
322
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
3,411✔
323
    TQ_NULL_GO_TO_END(handle->pWalReader);
3,480✔
324
    if (handle->execHandle.execTb.qmsg != NULL && strcmp(handle->execHandle.execTb.qmsg, "") != 0) {
3,411✔
325
      if (nodesStringToNode(handle->execHandle.execTb.qmsg, &handle->execHandle.execTb.node) != 0) {
1,825✔
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,411✔
331
                                      handle->execHandle.subType, handle->fetchMeta,
332
                                      (SSnapContext**)(&reader.sContext)));
333
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, NULL, handle->consumerId);
3,480✔
334
    TQ_NULL_GO_TO_END(handle->execHandle.task);
3,480✔
335
    TQ_ERR_GO_TO_END(qGetTableList(handle->execHandle.execTb.suid, pVnode, handle->execHandle.execTb.node, &tbUidList,
3,480✔
336
                                handle->execHandle.task));
337
    tqInfo("vgId:%d, tq try to get ctb for stb subscribe, suid:%" PRId64, pVnode->config.vgId,
3,480✔
338
           handle->execHandle.execTb.suid);
339
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
3,480✔
340
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
3,480✔
341
    TQ_ERR_GO_TO_END(tqReaderSetTbUidList(handle->execHandle.pTqReader, tbUidList, NULL));
3,480✔
342
  }
343
  handle->tableCreateTimeHash = (SHashObj*)taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
128,870✔
344

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

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

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

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

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

375
  (void)memcpy(handle->subKey, req->subKey, TSDB_SUBSCRIBE_KEY_LEN);
125,773✔
376
  handle->consumerId = req->newConsumerId;
125,834✔
377

378
  handle->execHandle.subType = req->subType;
125,834✔
379
  handle->fetchMeta = req->withMeta;
125,834✔
380
  if (req->subType == TOPIC_SUB_TYPE__COLUMN) {
125,773✔
381
    void* tmp = taosStrdup(req->qmsg);
106,707✔
382
    if (tmp == NULL) {
106,707✔
383
      return terrno;
×
384
    }
385
    handle->execHandle.execCol.qmsg = tmp;
106,707✔
386
  } else if (req->subType == TOPIC_SUB_TYPE__DB) {
19,127✔
387
    handle->execHandle.execDb.pFilterOutTbUid =
15,990✔
388
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
15,990✔
389
    if (handle->execHandle.execDb.pFilterOutTbUid == NULL) {
15,990✔
390
      return terrno;
×
391
    }
392
  } else if (req->subType == TOPIC_SUB_TYPE__TABLE) {
3,137✔
393
    handle->execHandle.execTb.suid = req->suid;
3,137✔
394
    void* tmp = taosStrdup(req->qmsg);
3,137✔
395
    if (tmp == NULL) {
3,137✔
396
      return terrno;
×
397
    }
398
    handle->execHandle.execTb.qmsg = tmp;
3,137✔
399
  }
400

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

403
  int32_t code = tqMetaInitHandle(pTq, handle);
125,834✔
404
  if (code != 0) {
125,805✔
405
    return code;
×
406
  }
407
  tqInfo("tqMetaCreateHandle %s consumer 0x%" PRIx64 " vgId:%d, snapshotVer:%" PRId64, handle->subKey,
125,805✔
408
         handle->consumerId, vgId, handle->snapshotVer);
409
  return taosHashPut(pTq->pHandle, handle->subKey, strlen(handle->subKey), handle, sizeof(STqHandle));
125,834✔
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,300,375✔
445
  if (pTq == NULL || key == NULL || pHandle == NULL) {
5,300,375✔
446
    return TSDB_CODE_INVALID_PARA;
×
447
  }
448
  void* data = taosHashGet(pTq->pHandle, key, strlen(key));
5,300,760✔
449
  if (data == NULL) {
5,301,168✔
450
    int vLen = 0;
129,536✔
451
    if (tdbTbGet(pTq->pExecStore, key, (int)strlen(key), &data, &vLen) < 0) {
129,536✔
452
      tdbFree(data);
125,603✔
453
      return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
126,293✔
454
    }
455
    STqHandle handle = {0};
3,122✔
456
    int32_t code = tqMetaRestoreHandle(pTq, data, vLen >= 0 ? vLen : 0, &handle);
3,122✔
457
    if (code != 0) {
3,122✔
458
      tdbFree(data);
×
459
      tqDestroyTqHandle(&handle);
×
460
      return code;
×
461
    }
462
    tdbFree(data);
3,122✔
463
    *pHandle = taosHashGet(pTq->pHandle, key, strlen(key));
3,122✔
464
    if (*pHandle == NULL) {
3,122✔
465
      return terrno;
×
466
    }
467
  } else {
468
    *pHandle = data;
5,171,632✔
469
  }
470
  return TDB_CODE_SUCCESS;
5,175,048✔
471
}
472

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

483
END:
4,068,097✔
484
  return code;
4,068,097✔
485
}
486

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

501
static int32_t tqMetaRestoreCheckInfo(STQ* pTq) {
4,068,798✔
502
  if (pTq == NULL) {
4,068,798✔
503
    return TSDB_CODE_INVALID_PARA;
×
504
  }
505
  TBC*         pCur = NULL;
4,068,798✔
506
  void*        pKey = NULL;
4,069,374✔
507
  int          kLen = 0;
4,069,270✔
508
  void*        pVal = NULL;
4,068,373✔
509
  int          vLen = 0;
4,070,323✔
510
  int32_t      code = 0;
4,069,510✔
511
  STqCheckInfo info = {0};
4,069,510✔
512

513
  TQ_ERR_GO_TO_END(tdbTbcOpen(pTq->pCheckStore, &pCur, NULL));
4,068,839✔
514
  TQ_ERR_GO_TO_END(tdbTbcMoveToFirst(pCur));
4,068,694✔
515

516
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
4,072,205✔
517
    TQ_ERR_GO_TO_END(tqMetaDecodeCheckInfo(&info, pVal, vLen >= 0 ? vLen : 0));
2,961✔
518
    TQ_ERR_GO_TO_END(taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo)));
3,792✔
519
  }
520
  info.colIdList = NULL;
4,069,502✔
521

522
END:
4,069,502✔
523
  tdbFree(pKey);
4,069,045✔
524
  tdbFree(pVal);
4,068,820✔
525
  tdbTbcClose(pCur);
4,068,704✔
526
  tDeleteSTqCheckInfo(&info);
4,068,820✔
527
  return code;
4,069,095✔
528
}
529

530
int32_t tqMetaOpen(STQ* pTq) {
4,068,010✔
531
  if (pTq == NULL) {
4,068,010✔
532
    return TSDB_CODE_INVALID_PARA;
×
533
  }
534
  char*   maindb = NULL;
4,068,010✔
535
  char*   offsetNew = NULL;
4,070,691✔
536
  int32_t code = TDB_CODE_SUCCESS;
4,070,691✔
537
  TQ_ERR_GO_TO_END(tqBuildFName(&maindb, pTq->path, TDB_MAINDB_NAME));
4,070,691✔
538
  if (!taosCheckExistFile(maindb)) {
4,070,691✔
539
    TQ_ERR_GO_TO_END(replaceTqPath(&pTq->path));
4,070,691✔
540
    TQ_ERR_GO_TO_END(tqMetaOpenTdb(pTq));
4,069,939✔
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,068,097✔
547
  if (taosCheckExistFile(offsetNew)) {
4,066,820✔
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,069,521✔
553

554
END:
4,068,519✔
555
  taosMemoryFree(maindb);
4,068,151✔
556
  taosMemoryFree(offsetNew);
4,068,552✔
557
  return code;
4,066,123✔
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,070,674✔
603
  if (pTq == NULL) {
4,070,674✔
604
    return;
×
605
  }
606
  int32_t ret = 0;
4,070,674✔
607
  if (pTq->pExecStore) {
4,070,674✔
608
    tdbTbClose(pTq->pExecStore);
4,070,674✔
609
  }
610
  if (pTq->pCheckStore) {
4,070,674✔
611
    tdbTbClose(pTq->pCheckStore);
4,070,691✔
612
  }
613
  if (pTq->pOffsetStore) {
4,070,674✔
614
    tdbTbClose(pTq->pOffsetStore);
4,070,674✔
615
  }
616
  tdbClose(pTq->pMetaDB);
4,070,708✔
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