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

taosdata / TDengine / #4878

11 Dec 2025 02:43AM UTC coverage: 64.569% (-0.02%) from 64.586%
#4878

push

travis-ci

guanshengliang
feat(TS-7270): internal dependence

307 of 617 new or added lines in 24 files covered. (49.76%)

3821 existing lines in 123 files now uncovered.

163630 of 253417 relevant lines covered (64.57%)

107598827.89 hits per line

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

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

25
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
1,112,765✔
26
  TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->subKey));
2,224,425✔
27
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->fetchMeta));
2,224,322✔
28
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->consumerId));
2,224,099✔
29
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->snapshotVer));
2,223,915✔
30
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pHandle->epoch));
2,224,416✔
31
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->execHandle.subType));
2,224,567✔
32
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
1,112,142✔
33
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execCol.qmsg));
2,058,104✔
34
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
83,678✔
35
    int32_t size = taosHashGetSize(pHandle->execHandle.execDb.pFilterOutTbUid);
70,626✔
36
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, size));
70,626✔
37
    void* pIter = NULL;
70,626✔
38
    pIter = taosHashIterate(pHandle->execHandle.execDb.pFilterOutTbUid, pIter);
70,626✔
39
    while (pIter) {
70,532✔
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) {
13,052✔
45
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->execHandle.execTb.suid));
26,104✔
46
    if (pHandle->execHandle.execTb.qmsg != NULL) {
13,052✔
47
      TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execTb.qmsg));
25,920✔
48
    }
49
  }
50
  tEndEncode(pEncoder);
1,112,448✔
51
_exit:
1,111,669✔
52
  if (code) {
1,111,669✔
53
    return code;
×
54
  } else {
55
    return pEncoder->pos;
1,111,669✔
56
  }
57
}
58

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

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

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

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

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

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

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

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

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

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

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

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

173
  TQ_ERR_GO_TO_END(
733,490✔
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));
732,611✔
176
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
732,200✔
177
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
731,905✔
178

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

193
  TQ_ERR_GO_TO_END(
183,867✔
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));
183,867✔
196
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
96,027✔
197
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
95,841✔
198

199
  return 0;
95,956✔
200

201
END:
87,540✔
202
  tdbAbort(pTq->pMetaDB, txn);
87,547✔
203
  return code;
87,682✔
204
}
205

206
int32_t tqMetaGetOffset(STQ* pTq, const char* subkey, STqOffset** pOffset) {
817,159✔
207
  if (pTq == NULL || subkey == NULL || pOffset == NULL) {
817,159✔
208
    return TSDB_CODE_INVALID_PARA;
×
209
  }
210
  void* data = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
817,159✔
211
  if (data == NULL) {
817,073✔
212
    int vLen = 0;
265,483✔
213
    taosRLockLatch(&pTq->lock);
265,483✔
214
    if (tdbTbGet(pTq->pOffsetStore, subkey, strlen(subkey), &data, &vLen) < 0) {
265,569✔
215
      taosRUnLockLatch(&pTq->lock);
264,045✔
216
      tdbFree(data);
264,473✔
217
      return TSDB_CODE_OUT_OF_MEMORY;
263,623✔
218
    }
219
    taosRUnLockLatch(&pTq->lock);
1,015✔
220

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

234
    *pOffset = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
1,015✔
235
    if (*pOffset == NULL) {
1,015✔
236
      return TSDB_CODE_OUT_OF_MEMORY;
×
237
    }
238
  } else {
239
    *pOffset = data;
551,590✔
240
  }
241
  return 0;
552,605✔
242
}
243

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

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

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

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

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

274
END:
552,055✔
275
  tEncoderClear(&encoder);
551,289✔
276
  taosMemoryFree(buf);
554,179✔
277
  return code;
549,542✔
278
}
279

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

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

290
  handle->pRef = walOpenRef(pVnode->pWal);
129,789✔
291
  TQ_NULL_GO_TO_END(handle->pRef);
129,789✔
292

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

301
  initStorageAPI(&reader.api);
129,704✔
302

303
  if (handle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
129,789✔
304
    handle->execHandle.task = qCreateQueueExecTaskInfo(handle->execHandle.execCol.qmsg, &reader, vgId,
109,348✔
305
                                                       &handle->execHandle.numOfCols, handle->consumerId);
109,162✔
306
    TQ_NULL_GO_TO_END(handle->execHandle.task);
109,162✔
307
    void* scanner = NULL;
109,162✔
308
    qExtractTmqScanner(handle->execHandle.task, &scanner);
109,071✔
309
    TQ_NULL_GO_TO_END(scanner);
109,071✔
310
    handle->execHandle.pTqReader = qExtractReaderFromTmqScanner(scanner);
109,071✔
311
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
109,071✔
312
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
20,627✔
313
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
17,042✔
314
    TQ_NULL_GO_TO_END(handle->pWalReader);
17,042✔
315
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
17,042✔
316
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
17,042✔
317
    TQ_ERR_GO_TO_END(buildSnapContext(reader.vnode, reader.version, 0, handle->execHandle.subType, handle->fetchMeta,
17,042✔
318
                                      (SSnapContext**)(&reader.sContext)));
319
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, NULL, handle->consumerId);
17,042✔
320
    TQ_NULL_GO_TO_END(handle->execHandle.task);
17,042✔
321
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
3,585✔
322
    handle->pWalReader = walOpenReader(pVnode->pWal, 0);
3,585✔
323
    TQ_NULL_GO_TO_END(handle->pWalReader);
3,585✔
324
    if (handle->execHandle.execTb.qmsg != NULL && strcmp(handle->execHandle.execTb.qmsg, "") != 0) {
3,585✔
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,405✔
331
                                      handle->execHandle.subType, handle->fetchMeta,
332
                                      (SSnapContext**)(&reader.sContext)));
333
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, NULL, handle->consumerId);
3,585✔
334
    TQ_NULL_GO_TO_END(handle->execHandle.task);
3,585✔
335
    TQ_ERR_GO_TO_END(qGetTableList(handle->execHandle.execTb.suid, pVnode, handle->execHandle.execTb.node, &tbUidList,
3,585✔
336
                                handle->execHandle.task));
337
    tqInfo("vgId:%d, tq try to get ctb for stb subscribe, suid:%" PRId64, pVnode->config.vgId,
3,585✔
338
           handle->execHandle.execTb.suid);
339
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
3,585✔
340
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
3,585✔
341
    TQ_ERR_GO_TO_END(tqReaderSetTbUidList(handle->execHandle.pTqReader, tbUidList, NULL));
3,585✔
342
  }
343
  handle->tableCreateTimeHash = (SHashObj*)taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
129,698✔
344

345
END:
129,794✔
346
  reader.api.snapshotFn.destroySnapshot(reader.sContext);
129,789✔
347
  taosArrayDestroy(tbUidList);
129,789✔
348
  return code;
129,698✔
349
}
350

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

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

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

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

376
  (void)memcpy(handle->subKey, req->subKey, TSDB_SUBSCRIBE_KEY_LEN);
126,993✔
377
  handle->consumerId = req->newConsumerId;
126,993✔
378

379
  handle->execHandle.subType = req->subType;
126,993✔
380
  handle->fetchMeta = req->withMeta;
126,993✔
381
  if (req->subType == TOPIC_SUB_TYPE__COLUMN) {
126,993✔
382
    void* tmp = taosStrdup(req->qmsg);
107,023✔
383
    if (tmp == NULL) {
107,023✔
UNCOV
384
      return terrno;
×
385
    }
386
    handle->execHandle.execCol.qmsg = tmp;
107,023✔
387
  } else if (req->subType == TOPIC_SUB_TYPE__DB) {
19,970✔
388
    handle->execHandle.execDb.pFilterOutTbUid =
16,723✔
389
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
16,723✔
390
    if (handle->execHandle.execDb.pFilterOutTbUid == NULL) {
16,723✔
UNCOV
391
      return terrno;
×
392
    }
393
  } else if (req->subType == TOPIC_SUB_TYPE__TABLE) {
3,247✔
394
    handle->execHandle.execTb.suid = req->suid;
3,247✔
395
    void* tmp = taosStrdup(req->qmsg);
3,247✔
396
    if (tmp == NULL) {
3,247✔
UNCOV
397
      return terrno;
×
398
    }
399
    handle->execHandle.execTb.qmsg = tmp;
3,247✔
400
  }
401

402
  handle->snapshotVer = walGetCommittedVer(pTq->pVnode->pWal);
126,993✔
403

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

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

UNCOV
424
  int32_t code = TDB_CODE_SUCCESS;
×
425

UNCOV
426
  TQ_ERR_GO_TO_END(tdbTbcOpen(pOld, &pCur, NULL));
×
427
  TQ_ERR_GO_TO_END(
×
428
      tdbBegin(pMetaDB, &txn, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED));
429

UNCOV
430
  TQ_ERR_GO_TO_END(tdbTbcMoveToFirst(pCur));
×
431
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
×
432
    TQ_ERR_GO_TO_END(tdbTbUpsert(pNew, pKey, kLen, pVal, vLen, txn));
×
433
  }
434

UNCOV
435
  TQ_ERR_GO_TO_END(tdbCommit(pMetaDB, txn));
×
436
  TQ_ERR_GO_TO_END(tdbPostCommit(pMetaDB, txn));
×
437

UNCOV
438
END:
×
439
  tdbFree(pKey);
×
440
  tdbFree(pVal);
×
441
  tdbTbcClose(pCur);
×
442
  return code;
×
443
}
444

445
int32_t tqMetaGetHandle(STQ* pTq, const char* key, STqHandle** pHandle) {
5,302,050✔
446
  if (pTq == NULL || key == NULL || pHandle == NULL) {
5,302,050✔
UNCOV
447
    return TSDB_CODE_INVALID_PARA;
×
448
  }
449
  void* data = taosHashGet(pTq->pHandle, key, strlen(key));
5,302,227✔
450
  if (data == NULL) {
5,301,917✔
451
    int vLen = 0;
130,079✔
452
    if (tdbTbGet(pTq->pExecStore, key, (int)strlen(key), &data, &vLen) < 0) {
130,079✔
453
      tdbFree(data);
127,079✔
454
      return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
127,224✔
455
    }
456
    STqHandle handle = {0};
2,796✔
457
    int32_t code = tqMetaRestoreHandle(pTq, data, vLen >= 0 ? vLen : 0, &handle);
2,796✔
458
    if (code != 0) {
2,796✔
UNCOV
459
      tdbFree(data);
×
460
      tqDestroyTqHandle(&handle);
×
461
      return code;
×
462
    }
463
    tdbFree(data);
2,796✔
464
    *pHandle = taosHashGet(pTq->pHandle, key, strlen(key));
2,796✔
465
    if (*pHandle == NULL) {
2,796✔
UNCOV
466
      return terrno;
×
467
    }
468
  } else {
469
    *pHandle = data;
5,171,838✔
470
  }
471
  return TDB_CODE_SUCCESS;
5,175,040✔
472
}
473

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

484
END:
4,135,685✔
485
  return code;
4,135,685✔
486
}
487

488
static int32_t replaceTqPath(char** path) {
4,134,829✔
489
  if (path == NULL || *path == NULL) {
4,134,829✔
UNCOV
490
    return TSDB_CODE_INVALID_PARA;
×
491
  }
492
  char*   tpath = NULL;
4,136,126✔
493
  int32_t code = tqBuildFName(&tpath, *path, TQ_SUBSCRIBE_NAME);
4,136,126✔
494
  if (code != 0) {
4,136,659✔
UNCOV
495
    return code;
×
496
  }
497
  taosMemoryFree(*path);
4,136,659✔
498
  *path = tpath;
4,136,061✔
499
  return TDB_CODE_SUCCESS;
4,136,061✔
500
}
501

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

514
  TQ_ERR_GO_TO_END(tdbTbcOpen(pTq->pCheckStore, &pCur, NULL));
4,136,157✔
515
  TQ_ERR_GO_TO_END(tdbTbcMoveToFirst(pCur));
4,135,653✔
516

517
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
4,138,484✔
518
    TQ_ERR_GO_TO_END(tqMetaDecodeCheckInfo(&info, pVal, vLen >= 0 ? vLen : 0));
2,950✔
519
    TQ_ERR_GO_TO_END(taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo)));
3,892✔
520
  }
521
  info.colIdList = NULL;
4,135,522✔
522

523
END:
4,135,522✔
524
  tdbFree(pKey);
4,134,543✔
525
  tdbFree(pVal);
4,135,324✔
526
  tdbTbcClose(pCur);
4,134,680✔
527
  tDeleteSTqCheckInfo(&info);
4,135,383✔
528
  return code;
4,133,583✔
529
}
530

531
int32_t tqMetaOpen(STQ* pTq) {
4,132,371✔
532
  if (pTq == NULL) {
4,132,371✔
UNCOV
533
    return TSDB_CODE_INVALID_PARA;
×
534
  }
535
  char*   maindb = NULL;
4,132,371✔
536
  char*   offsetNew = NULL;
4,136,659✔
537
  int32_t code = TDB_CODE_SUCCESS;
4,136,659✔
538
  TQ_ERR_GO_TO_END(tqBuildFName(&maindb, pTq->path, TDB_MAINDB_NAME));
4,136,659✔
539
  if (!taosCheckExistFile(maindb)) {
4,135,694✔
540
    TQ_ERR_GO_TO_END(replaceTqPath(&pTq->path));
4,136,153✔
541
    TQ_ERR_GO_TO_END(tqMetaOpenTdb(pTq));
4,136,567✔
542
  } else {
UNCOV
543
    TQ_ERR_GO_TO_END(tqMetaTransform(pTq));
×
544
    TQ_ERR_GO_TO_END(taosRemoveFile(maindb));
×
545
  }
546

547
  TQ_ERR_GO_TO_END(tqBuildFName(&offsetNew, pTq->path, TQ_OFFSET_NAME));
4,135,685✔
548
  if (taosCheckExistFile(offsetNew)) {
4,133,534✔
UNCOV
549
    TQ_ERR_GO_TO_END(tqOffsetRestoreFromFile(pTq, offsetNew));
×
550
    TQ_ERR_GO_TO_END(taosRemoveFile(offsetNew));
×
551
  }
552

553
  TQ_ERR_GO_TO_END(tqMetaRestoreCheckInfo(pTq));
4,136,659✔
554

555
END:
4,134,990✔
556
  taosMemoryFree(maindb);
4,135,563✔
557
  taosMemoryFree(offsetNew);
4,136,151✔
558
  return code;
4,134,899✔
559
}
560

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

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

UNCOV
577
  TQ_ERR_GO_TO_END(replaceTqPath(&pTq->path));
×
578
  TQ_ERR_GO_TO_END(tqMetaOpenTdb(pTq));
×
579

UNCOV
580
  TQ_ERR_GO_TO_END(tqMetaTransformInfo(pTq->pMetaDB, pExecStore, pTq->pExecStore));
×
581
  TQ_ERR_GO_TO_END(tqMetaTransformInfo(pTq->pMetaDB, pCheckStore, pTq->pCheckStore));
×
582

UNCOV
583
  TQ_ERR_GO_TO_END(tqBuildFName(&offsetNew, pTq->path, TQ_OFFSET_NAME));
×
584

UNCOV
585
  if (taosCheckExistFile(offset)) {
×
586
    if (taosCopyFile(offset, offsetNew) < 0) {
×
587
      tqError("copy offset file error");
×
588
    } else {
UNCOV
589
      TQ_ERR_GO_TO_END(taosRemoveFile(offset));
×
590
    }
591
  }
592

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

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

603
void tqMetaClose(STQ* pTq) {
4,135,511✔
604
  if (pTq == NULL) {
4,135,511✔
UNCOV
605
    return;
×
606
  }
607
  int32_t ret = 0;
4,135,511✔
608
  if (pTq->pExecStore) {
4,135,511✔
609
    tdbTbClose(pTq->pExecStore);
4,136,659✔
610
  }
611
  if (pTq->pCheckStore) {
4,136,659✔
612
    tdbTbClose(pTq->pCheckStore);
4,136,231✔
613
  }
614
  if (pTq->pOffsetStore) {
4,136,659✔
615
    tdbTbClose(pTq->pOffsetStore);
4,135,168✔
616
  }
617
  tdbClose(pTq->pMetaDB);
4,138,150✔
618
}
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