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

taosdata / TDengine / #4881

14 Dec 2025 03:48AM UTC coverage: 60.617% (+0.5%) from 60.092%
#4881

push

travis-ci

web-flow
test: update coverage workflow time (#33918)

156854 of 258761 relevant lines covered (60.62%)

75258957.81 hits per line

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

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

25
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
551,076✔
26
  TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->subKey));
1,102,152✔
27
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->fetchMeta));
1,102,152✔
28
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->consumerId));
1,101,938✔
29
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->snapshotVer));
1,101,938✔
30
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pHandle->epoch));
1,102,152✔
31
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->execHandle.subType));
1,102,152✔
32
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
551,076✔
33
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execCol.qmsg));
1,098,808✔
34
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
1,672✔
35
    int32_t size = taosHashGetSize(pHandle->execHandle.execDb.pFilterOutTbUid);
1,672✔
36
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, size));
1,672✔
37
    void* pIter = NULL;
1,672✔
38
    pIter = taosHashIterate(pHandle->execHandle.execDb.pFilterOutTbUid, pIter);
1,672✔
39
    while (pIter) {
1,672✔
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) {
×
45
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->execHandle.execTb.suid));
×
46
    if (pHandle->execHandle.execTb.qmsg != NULL) {
×
47
      TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execTb.qmsg));
×
48
    }
49
  }
50
  tEndEncode(pEncoder);
551,076✔
51
_exit:
551,076✔
52
  if (code) {
551,076✔
53
    return code;
×
54
  } else {
55
    return pEncoder->pos;
551,076✔
56
  }
57
}
58

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

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

96
_exit:
×
97
  return code;
×
98
}
99

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

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

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

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

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

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

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

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

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

173
  TQ_ERR_GO_TO_END(
396,328✔
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));
396,328✔
176
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
395,794✔
177
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
395,644✔
178

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

193
  TQ_ERR_GO_TO_END(
43,496✔
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));
43,496✔
196
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
23,816✔
197
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
23,816✔
198

199
  return 0;
23,816✔
200

201
END:
19,680✔
202
  tdbAbort(pTq->pMetaDB, txn);
19,680✔
203
  return code;
19,680✔
204
}
205

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

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

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

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

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

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

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

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

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

274
END:
275,538✔
275
  tEncoderClear(&encoder);
273,936✔
276
  taosMemoryFree(buf);
275,313✔
277
  return code;
275,004✔
278
}
279

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

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

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

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

301
  initStorageAPI(&reader.api);
32,150✔
302

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

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

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

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

365
END:
×
366
  tDecoderClear(&decoder);
×
367
  return code;
×
368
}
369

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

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

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

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

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

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
  }
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

424
  int32_t code = TDB_CODE_SUCCESS;
×
425

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

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

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

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) {
456,343✔
446
  if (pTq == NULL || key == NULL || pHandle == NULL) {
456,343✔
447
    return TSDB_CODE_INVALID_PARA;
×
448
  }
449
  void* data = taosHashGet(pTq->pHandle, key, strlen(key));
456,343✔
450
  if (data == NULL) {
456,568✔
451
    int vLen = 0;
32,150✔
452
    if (tdbTbGet(pTq->pExecStore, key, (int)strlen(key), &data, &vLen) < 0) {
32,150✔
453
      tdbFree(data);
32,150✔
454
      return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
31,722✔
455
    }
456
    STqHandle handle = {0};
×
457
    int32_t code = tqMetaRestoreHandle(pTq, data, vLen >= 0 ? vLen : 0, &handle);
×
458
    if (code != 0) {
×
459
      tdbFree(data);
×
460
      tqDestroyTqHandle(&handle);
×
461
      return code;
×
462
    }
463
    tdbFree(data);
×
464
    *pHandle = taosHashGet(pTq->pHandle, key, strlen(key));
×
465
    if (*pHandle == NULL) {
×
466
      return terrno;
×
467
    }
468
  } else {
469
    *pHandle = data;
424,418✔
470
  }
471
  return TDB_CODE_SUCCESS;
424,418✔
472
}
473

474
int32_t tqMetaOpenTdb(STQ* pTq) {
2,196,020✔
475
  if (pTq == NULL) {
2,196,020✔
476
    return TSDB_CODE_INVALID_PARA;
×
477
  }
478
  int32_t code = TDB_CODE_SUCCESS;
2,196,020✔
479
  TQ_ERR_GO_TO_END(tdbOpen(pTq->path, 16 * 1024, 1, &pTq->pMetaDB, 0, NULL));
2,196,020✔
480
  TQ_ERR_GO_TO_END(tdbTbOpen("tq.db", -1, -1, NULL, pTq->pMetaDB, &pTq->pExecStore, 0));
2,196,908✔
481
  TQ_ERR_GO_TO_END(tdbTbOpen("tq.check.db", -1, -1, NULL, pTq->pMetaDB, &pTq->pCheckStore, 0));
2,196,311✔
482
  TQ_ERR_GO_TO_END(tdbTbOpen("tq.offset.db", -1, -1, NULL, pTq->pMetaDB, &pTq->pOffsetStore, 0));
2,196,084✔
483

484
END:
2,196,403✔
485
  return code;
2,196,403✔
486
}
487

488
static int32_t replaceTqPath(char** path) {
2,196,058✔
489
  if (path == NULL || *path == NULL) {
2,196,058✔
490
    return TSDB_CODE_INVALID_PARA;
×
491
  }
492
  char*   tpath = NULL;
2,196,058✔
493
  int32_t code = tqBuildFName(&tpath, *path, TQ_SUBSCRIBE_NAME);
2,196,058✔
494
  if (code != 0) {
2,197,061✔
495
    return code;
×
496
  }
497
  taosMemoryFree(*path);
2,197,061✔
498
  *path = tpath;
2,196,033✔
499
  return TDB_CODE_SUCCESS;
2,196,033✔
500
}
501

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

514
  TQ_ERR_GO_TO_END(tdbTbcOpen(pTq->pCheckStore, &pCur, NULL));
2,196,617✔
515
  TQ_ERR_GO_TO_END(tdbTbcMoveToFirst(pCur));
2,197,061✔
516

517
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
2,198,051✔
518
    TQ_ERR_GO_TO_END(tqMetaDecodeCheckInfo(&info, pVal, vLen >= 0 ? vLen : 0));
935✔
519
    TQ_ERR_GO_TO_END(taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo)));
1,359✔
520
  }
521
  info.colIdList = NULL;
2,197,061✔
522

523
END:
2,197,061✔
524
  tdbFree(pKey);
2,197,061✔
525
  tdbFree(pVal);
2,196,847✔
526
  tdbTbcClose(pCur);
2,196,941✔
527
  tDeleteSTqCheckInfo(&info);
2,196,809✔
528
  return code;
2,195,848✔
529
}
530

531
int32_t tqMetaOpen(STQ* pTq) {
2,194,351✔
532
  if (pTq == NULL) {
2,194,351✔
533
    return TSDB_CODE_INVALID_PARA;
×
534
  }
535
  char*   maindb = NULL;
2,194,351✔
536
  char*   offsetNew = NULL;
2,196,528✔
537
  int32_t code = TDB_CODE_SUCCESS;
2,197,061✔
538
  TQ_ERR_GO_TO_END(tqBuildFName(&maindb, pTq->path, TDB_MAINDB_NAME));
2,197,061✔
539
  if (!taosCheckExistFile(maindb)) {
2,195,972✔
540
    TQ_ERR_GO_TO_END(replaceTqPath(&pTq->path));
2,197,061✔
541
    TQ_ERR_GO_TO_END(tqMetaOpenTdb(pTq));
2,195,525✔
542
  } else {
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));
2,196,403✔
548
  if (taosCheckExistFile(offsetNew)) {
2,196,151✔
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));
2,197,061✔
554

555
END:
2,196,590✔
556
  taosMemoryFree(maindb);
2,196,590✔
557
  taosMemoryFree(offsetNew);
2,195,880✔
558
  return code;
2,196,094✔
559
}
560

561
int32_t tqMetaTransform(STQ* pTq) {
×
562
  if (pTq == NULL) {
×
563
    return TSDB_CODE_INVALID_PARA;
×
564
  }
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

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

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

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

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

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

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

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

603
void tqMetaClose(STQ* pTq) {
2,196,222✔
604
  if (pTq == NULL) {
2,196,222✔
605
    return;
×
606
  }
607
  int32_t ret = 0;
2,196,222✔
608
  if (pTq->pExecStore) {
2,196,222✔
609
    tdbTbClose(pTq->pExecStore);
2,197,061✔
610
  }
611
  if (pTq->pCheckStore) {
2,197,061✔
612
    tdbTbClose(pTq->pCheckStore);
2,197,061✔
613
  }
614
  if (pTq->pOffsetStore) {
2,197,061✔
615
    tdbTbClose(pTq->pOffsetStore);
2,197,061✔
616
  }
617
  tdbClose(pTq->pMetaDB);
2,197,061✔
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