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

taosdata / TDengine / #3951

28 Apr 2025 05:42AM UTC coverage: 62.445% (-0.4%) from 62.853%
#3951

push

travis-ci

web-flow
fix: mnode-status-case (#30871)

155256 of 317429 branches covered (48.91%)

Branch coverage included in aggregate %.

240626 of 316539 relevant lines covered (76.02%)

6221630.89 hits per line

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

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

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

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

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

96
_exit:
227✔
97
  return code;
227✔
98
}
99

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

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

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

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

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

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

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

157
  TQ_ERR_GO_TO_END(tqMetaSaveInfo(pTq, pTq->pOffsetStore, pOffset->subKey, strlen(pOffset->subKey), buf, vlen));
1!
158

159
END:
1✔
160
  tEncoderClear(&encoder);
1✔
161
  taosMemoryFree(buf);
1!
162
  return code;
1✔
163
}
164

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

172
  TQ_ERR_GO_TO_END(
12,316!
173
      tdbBegin(pTq->pMetaDB, &txn, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED));
174
  TQ_ERR_GO_TO_END(tdbTbUpsert(ttb, key, kLen, value, vLen, txn));
12,297!
175
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
12,299!
176
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
12,315!
177

178
  return 0;
12,307✔
179

180
END:
×
181
  tdbAbort(pTq->pMetaDB, txn);
×
182
  return code;
×
183
}
184

185
int32_t tqMetaDeleteInfo(STQ* pTq, TTB* ttb, const void* key, uint32_t kLen) {
1,880✔
186
  if (pTq == NULL || ttb == NULL || key == NULL) {
1,880!
187
    return TSDB_CODE_INVALID_PARA;
×
188
  }
189
  int32_t code = TDB_CODE_SUCCESS;
1,880✔
190
  TXN*    txn = NULL;
1,880✔
191

192
  TQ_ERR_GO_TO_END(
1,880!
193
      tdbBegin(pTq->pMetaDB, &txn, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED));
194
  TQ_ERR_GO_TO_END(tdbTbDelete(ttb, key, kLen, txn));
1,877✔
195
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
1,607!
196
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
1,607!
197

198
  return 0;
1,607✔
199

200
END:
272✔
201
  tdbAbort(pTq->pMetaDB, txn);
272✔
202
  return code;
273✔
203
}
204

205
int32_t tqMetaGetOffset(STQ* pTq, const char* subkey, STqOffset** pOffset) {
398,997✔
206
  if (pTq == NULL || subkey == NULL || pOffset == NULL) {
398,997!
207
    return TSDB_CODE_INVALID_PARA;
×
208
  }
209
  void* data = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
398,998✔
210
  if (data == NULL) {
398,991✔
211
    int vLen = 0;
390,894✔
212
    if (tdbTbGet(pTq->pOffsetStore, subkey, strlen(subkey), &data, &vLen) < 0) {
390,894✔
213
      tdbFree(data);
390,730✔
214
      return TSDB_CODE_OUT_OF_MEMORY;
390,718✔
215
    }
216

217
    STqOffset offset = {0};
163✔
218
    if (tqMetaDecodeOffsetInfo(&offset, data, vLen >= 0 ? vLen : 0) != TDB_CODE_SUCCESS) {
163!
219
      tdbFree(data);
×
220
      return TSDB_CODE_OUT_OF_MEMORY;
×
221
    }
222

223
    if (taosHashPut(pTq->pOffset, subkey, strlen(subkey), &offset, sizeof(STqOffset)) != 0) {
166!
224
      tDeleteSTqOffset(&offset);
×
225
      tdbFree(data);
×
226
      return terrno;
×
227
    }
228
    tdbFree(data);
166✔
229

230
    *pOffset = taosHashGet(pTq->pOffset, subkey, strlen(subkey));
166✔
231
    if (*pOffset == NULL) {
166!
232
      return TSDB_CODE_OUT_OF_MEMORY;
×
233
    }
234
  } else {
235
    *pOffset = data;
8,097✔
236
  }
237
  return 0;
8,263✔
238
}
239

240
int32_t tqMetaSaveHandle(STQ* pTq, const char* key, const STqHandle* pHandle) {
3,018✔
241
  if (pTq == NULL || key == NULL || pHandle == NULL) {
3,018!
242
    return TSDB_CODE_INVALID_PARA;
×
243
  }
244
  int32_t  code = TDB_CODE_SUCCESS;
3,018✔
245
  uint32_t  vlen;
246
  void*    buf = NULL;
3,018✔
247
  SEncoder encoder = {0};
3,018✔
248
  tEncodeSize(tEncodeSTqHandle, pHandle, vlen, code);
3,018!
249
  if (code < 0) {
3,013!
250
    goto END;
×
251
  }
252

253
  tqDebug("tq save %s(%d) handle consumer:0x%" PRIx64 " epoch:%d vgId:%d", pHandle->subKey,
3,013!
254
          (int32_t)strlen(pHandle->subKey), pHandle->consumerId, pHandle->epoch, TD_VID(pTq->pVnode));
255

256
  buf = taosMemoryCalloc(1, vlen);
3,015!
257
  if (buf == NULL) {
3,016!
258
    code = terrno;
×
259
    goto END;
×
260
  }
261

262
  tEncoderInit(&encoder, buf, vlen);
3,016✔
263
  code = tEncodeSTqHandle(&encoder, pHandle);
3,017✔
264
  if (code < 0) {
3,019!
265
    goto END;
×
266
  }
267

268
  TQ_ERR_GO_TO_END(tqMetaSaveInfo(pTq, pTq->pExecStore, key, strlen(key), buf, vlen));
3,019!
269

270
END:
3,013✔
271
  tEncoderClear(&encoder);
3,013✔
272
  taosMemoryFree(buf);
3,018!
273
  return code;
3,019✔
274
}
275

276
static int tqMetaInitHandle(STQ* pTq, STqHandle* handle) {
1,586✔
277
  if (pTq == NULL || handle == NULL) {
1,586!
278
    return TSDB_CODE_INVALID_PARA;
×
279
  }
280
  int32_t code = TDB_CODE_SUCCESS;
1,586✔
281

282
  SVnode* pVnode = pTq->pVnode;
1,586✔
283
  int32_t vgId = TD_VID(pVnode);
1,586✔
284

285
  handle->pRef = walOpenRef(pVnode->pWal);
1,586✔
286
  TQ_NULL_GO_TO_END(handle->pRef);
1,587!
287

288
  SReadHandle reader = {
1,587✔
289
      .vnode = pVnode,
290
      .initTableReader = true,
291
      .initTqReader = true,
292
      .version = handle->snapshotVer,
1,587✔
293
  };
294

295
  initStorageAPI(&reader.api);
1,587✔
296

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

346
END:
1,586✔
347
  return code;
1,586✔
348
}
349

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

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

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

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

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

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

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

403
  int32_t code = tqMetaInitHandle(pTq, handle);
1,406✔
404
  if (code != 0) {
1,404!
405
    return code;
×
406
  }
407
  tqInfo("tqMetaCreateHandle %s consumer 0x%" PRIx64 " vgId:%d, snapshotVer:%" PRId64, handle->subKey,
1,404!
408
         handle->consumerId, vgId, handle->snapshotVer);
409
  return taosHashPut(pTq->pHandle, handle->subKey, strlen(handle->subKey), handle, sizeof(STqHandle));
1,406✔
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) {
3,347,600✔
445
  if (pTq == NULL || key == NULL || pHandle == NULL) {
3,347,600!
446
    return TSDB_CODE_INVALID_PARA;
×
447
  }
448
  void* data = taosHashGet(pTq->pHandle, key, strlen(key));
3,349,309✔
449
  if (data == NULL) {
3,349,565✔
450
    int vLen = 0;
4,054✔
451
    if (tdbTbGet(pTq->pExecStore, key, (int)strlen(key), &data, &vLen) < 0) {
4,054✔
452
      tdbFree(data);
1,449✔
453
      return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
1,447✔
454
    }
455
    STqHandle handle = {0};
181✔
456
    int32_t code = tqMetaRestoreHandle(pTq, data, vLen >= 0 ? vLen : 0, &handle);
181✔
457
    if (code != 0) {
181!
458
      tdbFree(data);
×
459
      tqDestroyTqHandle(&handle);
×
460
      return code;
×
461
    }
462
    tdbFree(data);
181✔
463
    *pHandle = taosHashGet(pTq->pHandle, key, strlen(key));
181✔
464
    if (*pHandle == NULL) {
181!
465
      return terrno;
×
466
    }
467
  } else {
468
    *pHandle = data;
3,345,511✔
469
  }
470
  return TDB_CODE_SUCCESS;
3,345,692✔
471
}
472

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

483
END:
11,724✔
484
  return code;
11,724✔
485
}
486

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

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

513
  TQ_ERR_GO_TO_END(tdbTbcOpen(pTq->pCheckStore, &pCur, NULL));
11,724!
514
  TQ_ERR_GO_TO_END(tdbTbcMoveToFirst(pCur));
11,724!
515

516
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
11,808✔
517
    TQ_ERR_GO_TO_END(tqMetaDecodeCheckInfo(&info, pVal, vLen >= 0 ? vLen : 0));
85!
518
    TQ_ERR_GO_TO_END(taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo)));
85!
519
  }
520
  info.colIdList = NULL;
11,723✔
521

522
END:
11,723✔
523
  tdbFree(pKey);
11,723✔
524
  tdbFree(pVal);
11,723✔
525
  tdbTbcClose(pCur);
11,724✔
526
  tDeleteSTqCheckInfo(&info);
11,725✔
527
  return code;
11,723✔
528
}
529

530
int32_t tqMetaOpen(STQ* pTq) {
11,716✔
531
  if (pTq == NULL) {
11,716!
532
    return TSDB_CODE_INVALID_PARA;
×
533
  }
534
  char*   maindb = NULL;
11,716✔
535
  char*   offsetNew = NULL;
11,716✔
536
  int32_t code = TDB_CODE_SUCCESS;
11,716✔
537
  TQ_ERR_GO_TO_END(tqBuildFName(&maindb, pTq->path, TDB_MAINDB_NAME));
11,716!
538
  if (!taosCheckExistFile(maindb)) {
11,722!
539
    TQ_ERR_GO_TO_END(replaceTqPath(&pTq->path));
11,725!
540
    TQ_ERR_GO_TO_END(tqMetaOpenTdb(pTq));
11,725!
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));
11,724!
547
  if (taosCheckExistFile(offsetNew)) {
11,724!
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));
11,724!
553

554
END:
11,723✔
555
  taosMemoryFree(maindb);
11,723!
556
  taosMemoryFree(offsetNew);
11,722!
557
  return code;
11,724✔
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) {
11,725✔
603
  if (pTq == NULL) {
11,725!
604
    return;
×
605
  }
606
  int32_t ret = 0;
11,725✔
607
  if (pTq->pExecStore) {
11,725!
608
    tdbTbClose(pTq->pExecStore);
11,725✔
609
  }
610
  if (pTq->pCheckStore) {
11,725!
611
    tdbTbClose(pTq->pCheckStore);
11,725✔
612
  }
613
  if (pTq->pOffsetStore) {
11,725!
614
    tdbTbClose(pTq->pOffsetStore);
11,726✔
615
  }
616
  tdbClose(pTq->pMetaDB);
11,725✔
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