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

taosdata / TDengine / #4858

17 Nov 2025 09:53AM UTC coverage: 64.048% (-0.09%) from 64.135%
#4858

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

218 of 311 new or added lines in 32 files covered. (70.1%)

4856 existing lines in 134 files now uncovered.

151096 of 235910 relevant lines covered (64.05%)

115767925.69 hits per line

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

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

25
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
1,098,541✔
26
  TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->subKey));
2,196,975✔
27
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->fetchMeta));
2,196,816✔
28
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->consumerId));
2,196,914✔
29
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->snapshotVer));
2,196,982✔
30
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pHandle->epoch));
2,196,989✔
31
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->execHandle.subType));
2,196,805✔
32
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
1,098,357✔
33
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execCol.qmsg));
2,034,450✔
34
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
81,048✔
35
    int32_t size = taosHashGetSize(pHandle->execHandle.execDb.pFilterOutTbUid);
68,436✔
36
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, size));
68,468✔
37
    void* pIter = NULL;
68,468✔
38
    pIter = taosHashIterate(pHandle->execHandle.execDb.pFilterOutTbUid, pIter);
68,468✔
39
    while (pIter) {
68,436✔
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,703✔
45
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->execHandle.execTb.suid));
25,475✔
46
    if (pHandle->execHandle.execTb.qmsg != NULL) {
12,772✔
47
      TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execTb.qmsg));
25,475✔
48
    }
49
  }
50
  tEndEncode(pEncoder);
1,098,440✔
51
_exit:
1,098,441✔
52
  if (code) {
1,098,441✔
53
    return code;
×
54
  } else {
55
    return pEncoder->pos;
1,098,441✔
56
  }
57
}
58

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

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

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

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

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

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

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

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

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

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

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

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

173
  TQ_ERR_GO_TO_END(
722,623✔
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));
722,570✔
176
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
722,495✔
177
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
720,516✔
178

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

193
  TQ_ERR_GO_TO_END(
178,770✔
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));
178,235✔
196
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
92,703✔
197
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
92,803✔
198

199
  return 0;
92,739✔
200

201
END:
85,536✔
202
  tdbAbort(pTq->pMetaDB, txn);
85,560✔
203
  return code;
85,677✔
204
}
205

206
int32_t tqMetaGetOffset(STQ* pTq, const char* subkey, STqOffset** pOffset) {
785,464✔
207
  if (pTq == NULL || subkey == NULL || pOffset == NULL) {
785,464✔
UNCOV
208
    return TSDB_CODE_INVALID_PARA;
×
209
  }
210
  void* data = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
785,464✔
211
  if (data == NULL) {
785,464✔
212
    int vLen = 0;
268,934✔
213
    taosRLockLatch(&pTq->lock);
268,934✔
214
    if (tdbTbGet(pTq->pOffsetStore, subkey, strlen(subkey), &data, &vLen) < 0) {
268,934✔
215
      taosRUnLockLatch(&pTq->lock);
266,970✔
216
      tdbFree(data);
267,156✔
217
      return TSDB_CODE_OUT_OF_MEMORY;
266,159✔
218
    }
219
    taosRUnLockLatch(&pTq->lock);
1,300✔
220

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

234
    *pOffset = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
1,300✔
235
    if (*pOffset == NULL) {
1,300✔
236
      return TSDB_CODE_OUT_OF_MEMORY;
×
237
    }
238
  } else {
239
    *pOffset = data;
516,530✔
240
  }
241
  return 0;
517,830✔
242
}
243

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

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

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

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

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

274
END:
547,692✔
275
  tEncoderClear(&encoder);
546,287✔
276
  taosMemoryFree(buf);
547,877✔
277
  return code;
546,010✔
278
}
279

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

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

290
  handle->pRef = walOpenRef(pVnode->pWal);
128,029✔
291
  TQ_NULL_GO_TO_END(handle->pRef);
127,907✔
292

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

301
  initStorageAPI(&reader.api);
127,805✔
302

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

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

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

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

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

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

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

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

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

403
  int32_t code = tqMetaInitHandle(pTq, handle);
125,086✔
404
  if (code != 0) {
125,086✔
405
    return code;
×
406
  }
407
  tqInfo("tqMetaCreateHandle %s consumer 0x%" PRIx64 " vgId:%d, snapshotVer:%" PRId64, handle->subKey,
125,086✔
408
         handle->consumerId, vgId, handle->snapshotVer);
409
  return taosHashPut(pTq->pHandle, handle->subKey, strlen(handle->subKey), handle, sizeof(STqHandle));
125,086✔
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,221,052✔
445
  if (pTq == NULL || key == NULL || pHandle == NULL) {
5,221,052✔
UNCOV
446
    return TSDB_CODE_INVALID_PARA;
×
447
  }
448
  void* data = taosHashGet(pTq->pHandle, key, strlen(key));
5,222,637✔
449
  if (data == NULL) {
5,221,565✔
450
    int vLen = 0;
128,029✔
451
    if (tdbTbGet(pTq->pExecStore, key, (int)strlen(key), &data, &vLen) < 0) {
128,029✔
452
      tdbFree(data);
124,963✔
453
      return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
124,794✔
454
    }
455
    STqHandle handle = {0};
2,943✔
456
    int32_t code = tqMetaRestoreHandle(pTq, data, vLen >= 0 ? vLen : 0, &handle);
2,943✔
457
    if (code != 0) {
2,943✔
458
      tdbFree(data);
×
459
      tqDestroyTqHandle(&handle);
×
460
      return code;
×
461
    }
462
    tdbFree(data);
2,943✔
463
    *pHandle = taosHashGet(pTq->pHandle, key, strlen(key));
2,943✔
464
    if (*pHandle == NULL) {
2,943✔
465
      return terrno;
×
466
    }
467
  } else {
468
    *pHandle = data;
5,093,536✔
469
  }
470
  return TDB_CODE_SUCCESS;
5,097,204✔
471
}
472

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

483
END:
4,079,787✔
484
  return code;
4,079,787✔
485
}
486

487
static int32_t replaceTqPath(char** path) {
4,068,508✔
488
  if (path == NULL || *path == NULL) {
4,068,508✔
489
    return TSDB_CODE_INVALID_PARA;
×
490
  }
491
  char*   tpath = NULL;
4,069,078✔
492
  int32_t code = tqBuildFName(&tpath, *path, TQ_SUBSCRIBE_NAME);
4,079,239✔
493
  if (code != 0) {
4,079,441✔
494
    return code;
×
495
  }
496
  taosMemoryFree(*path);
4,079,441✔
497
  *path = tpath;
4,077,248✔
498
  return TDB_CODE_SUCCESS;
4,077,603✔
499
}
500

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

513
  TQ_ERR_GO_TO_END(tdbTbcOpen(pTq->pCheckStore, &pCur, NULL));
4,079,877✔
514
  TQ_ERR_GO_TO_END(tdbTbcMoveToFirst(pCur));
4,078,646✔
515

516
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
4,082,573✔
517
    TQ_ERR_GO_TO_END(tqMetaDecodeCheckInfo(&info, pVal, vLen >= 0 ? vLen : 0));
3,679✔
518
    TQ_ERR_GO_TO_END(taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo)));
3,679✔
519
  }
520
  info.colIdList = NULL;
4,079,508✔
521

522
END:
4,079,508✔
523
  tdbFree(pKey);
4,079,263✔
524
  tdbFree(pVal);
4,077,399✔
525
  tdbTbcClose(pCur);
4,078,522✔
526
  tDeleteSTqCheckInfo(&info);
4,078,767✔
527
  return code;
4,079,500✔
528
}
529

530
int32_t tqMetaOpen(STQ* pTq) {
4,074,466✔
531
  if (pTq == NULL) {
4,074,466✔
532
    return TSDB_CODE_INVALID_PARA;
×
533
  }
534
  char*   maindb = NULL;
4,074,466✔
535
  char*   offsetNew = NULL;
4,079,118✔
536
  int32_t code = TDB_CODE_SUCCESS;
4,079,877✔
537
  TQ_ERR_GO_TO_END(tqBuildFName(&maindb, pTq->path, TDB_MAINDB_NAME));
4,079,877✔
538
  if (!taosCheckExistFile(maindb)) {
4,078,845✔
539
    TQ_ERR_GO_TO_END(replaceTqPath(&pTq->path));
4,078,929✔
540
    TQ_ERR_GO_TO_END(tqMetaOpenTdb(pTq));
4,077,745✔
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,079,787✔
547
  if (taosCheckExistFile(offsetNew)) {
4,077,897✔
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,079,877✔
553

554
END:
4,079,787✔
555
  taosMemoryFree(maindb);
4,079,598✔
556
  taosMemoryFree(offsetNew);
4,078,857✔
557
  return code;
4,078,036✔
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,079,877✔
603
  if (pTq == NULL) {
4,079,877✔
604
    return;
×
605
  }
606
  int32_t ret = 0;
4,079,877✔
607
  if (pTq->pExecStore) {
4,079,877✔
608
    tdbTbClose(pTq->pExecStore);
4,079,877✔
609
  }
610
  if (pTq->pCheckStore) {
4,079,877✔
611
    tdbTbClose(pTq->pCheckStore);
4,079,877✔
612
  }
613
  if (pTq->pOffsetStore) {
4,079,877✔
614
    tdbTbClose(pTq->pOffsetStore);
4,079,877✔
615
  }
616
  tdbClose(pTq->pMetaDB);
4,079,683✔
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