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

taosdata / TDengine / #3831

02 Apr 2025 01:14AM UTC coverage: 34.081% (-0.02%) from 34.097%
#3831

push

travis-ci

happyguoxy
test:alter gcda dir

148596 of 599532 branches covered (24.79%)

Branch coverage included in aggregate %.

222550 of 489473 relevant lines covered (45.47%)

1589752.67 hits per line

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

57.01
/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) {
5,148✔
19
  if (pEncoder == NULL || pHandle == NULL) {
5,148!
20
    return TSDB_CODE_INVALID_PARA;
21
  }
22
  int32_t code = 0;
5,150✔
23
  int32_t lino;
24

25
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
5,150!
26
  TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->subKey));
10,304!
27
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->fetchMeta));
10,304!
28
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->consumerId));
10,304!
29
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pHandle->snapshotVer));
10,304!
30
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pHandle->epoch));
10,304!
31
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pHandle->execHandle.subType));
10,304!
32
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
5,152✔
33
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pHandle->execHandle.execCol.qmsg));
10,288!
34
  } else if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
8!
35
    int32_t size = taosHashGetSize(pHandle->execHandle.execDb.pFilterOutTbUid);
8✔
36
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, size));
8!
37
    void* pIter = NULL;
8✔
38
    pIter = taosHashIterate(pHandle->execHandle.execDb.pFilterOutTbUid, pIter);
8✔
39
    while (pIter) {
8!
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);
5,152✔
51
_exit:
5,152✔
52
  if (code) {
5,152!
53
    return code;
×
54
  } else {
55
    return pEncoder->pos;
5,152✔
56
  }
57
}
58

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

66
  TAOS_CHECK_EXIT(tStartDecode(pDecoder));
2!
67
  TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pHandle->subKey));
2!
68
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pHandle->fetchMeta));
4!
69
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->consumerId));
4!
70
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pHandle->snapshotVer));
4!
71
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pHandle->epoch));
4!
72
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pHandle->execHandle.subType));
4!
73
  if (pHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
2!
74
    TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pHandle->execHandle.execCol.qmsg));
4!
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);
2✔
95

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

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

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

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

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

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

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

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

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

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

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

172
  TQ_ERR_GO_TO_END(
3,690!
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));
3,690!
175
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
3,690!
176
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
3,688!
177

178
  return 0;
3,684✔
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) {
84✔
186
  if (pTq == NULL || ttb == NULL || key == NULL) {
84!
187
    return TSDB_CODE_INVALID_PARA;
×
188
  }
189
  int32_t code = TDB_CODE_SUCCESS;
84✔
190
  TXN*    txn = NULL;
84✔
191

192
  TQ_ERR_GO_TO_END(
84!
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));
84✔
195
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
44!
196
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
44!
197

198
  return 0;
44✔
199

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

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

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

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

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

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

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

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

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

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

270
END:
2,572✔
271
  tEncoderClear(&encoder);
2,572✔
272
  taosMemoryFree(buf);
2,576!
273
  return code;
2,576✔
274
}
275

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

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

285
  handle->pRef = walOpenRef(pVnode->pWal);
120✔
286

287
  TQ_NULL_GO_TO_END(handle->pRef);
120!
288
  TQ_ERR_GO_TO_END(walSetRefVer(handle->pRef, handle->snapshotVer));
120!
289

290
  SReadHandle reader = {
120✔
291
      .vnode = pVnode,
292
      .initTableReader = true,
293
      .initTqReader = true,
294
      .version = handle->snapshotVer,
120✔
295
  };
296

297
  initStorageAPI(&reader.api);
120✔
298

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

348
END:
120✔
349
  return code;
120✔
350
}
351

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

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

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

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

377
  (void)memcpy(handle->subKey, req->subKey, TSDB_SUBSCRIBE_KEY_LEN);
118✔
378
  handle->consumerId = req->newConsumerId;
118✔
379

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

403
  handle->snapshotVer = walGetCommittedVer(pTq->pVnode->pWal);
118✔
404

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

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

425
  int32_t code = TDB_CODE_SUCCESS;
44✔
426

427
  TQ_ERR_GO_TO_END(tdbTbcOpen(pOld, &pCur, NULL));
44!
428
  TQ_ERR_GO_TO_END(
44!
429
      tdbBegin(pMetaDB, &txn, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED));
430

431
  TQ_ERR_GO_TO_END(tdbTbcMoveToFirst(pCur));
44!
432
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
46✔
433
    TQ_ERR_GO_TO_END(tdbTbUpsert(pNew, pKey, kLen, pVal, vLen, txn));
2!
434
  }
435

436
  TQ_ERR_GO_TO_END(tdbCommit(pMetaDB, txn));
44!
437
  TQ_ERR_GO_TO_END(tdbPostCommit(pMetaDB, txn));
44!
438

439
END:
44✔
440
  tdbFree(pKey);
44✔
441
  tdbFree(pVal);
44✔
442
  tdbTbcClose(pCur);
44✔
443
  return code;
44✔
444
}
445

446
int32_t tqMetaGetHandle(STQ* pTq, const char* key, STqHandle** pHandle) {
22,242✔
447
  if (pTq == NULL || key == NULL || pHandle == NULL) {
22,242!
448
    return TSDB_CODE_INVALID_PARA;
×
449
  }
450
  void* data = taosHashGet(pTq->pHandle, key, strlen(key));
22,242✔
451
  if (data == NULL) {
22,242✔
452
    int vLen = 0;
120✔
453
    if (tdbTbGet(pTq->pExecStore, key, (int)strlen(key), &data, &vLen) < 0) {
120✔
454
      tdbFree(data);
118✔
455
      return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
118✔
456
    }
457
    STqHandle handle = {0};
2✔
458
    if (tqMetaRestoreHandle(pTq, data, vLen >= 0 ? vLen : 0, &handle) != 0) {
2!
459
      tdbFree(data);
×
460
      tqDestroyTqHandle(&handle);
×
461
      return TSDB_CODE_OUT_OF_MEMORY;
×
462
    }
463
    tdbFree(data);
2✔
464
    *pHandle = taosHashGet(pTq->pHandle, key, strlen(key));
2✔
465
    if (*pHandle == NULL) {
2!
466
      return TSDB_CODE_OUT_OF_MEMORY;
×
467
    }
468
  } else {
469
    *pHandle = data;
22,122✔
470
  }
471
  return TDB_CODE_SUCCESS;
22,124✔
472
}
473

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

484
END:
554✔
485
  return code;
554✔
486
}
487

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

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

514
  TQ_ERR_GO_TO_END(tdbTbcOpen(pTq->pCheckStore, &pCur, NULL));
554!
515
  TQ_ERR_GO_TO_END(tdbTbcMoveToFirst(pCur));
554!
516

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

523
END:
554✔
524
  tdbFree(pKey);
554✔
525
  tdbFree(pVal);
554✔
526
  tdbTbcClose(pCur);
554✔
527
  tDeleteSTqCheckInfo(&info);
554✔
528
  return code;
554✔
529
}
530

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

547
  TQ_ERR_GO_TO_END(tqBuildFName(&offsetNew, pTq->path, TQ_OFFSET_NAME));
554!
548
  if (taosCheckExistFile(offsetNew)) {
554!
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));
554!
554

555
END:
554✔
556
  taosMemoryFree(maindb);
554!
557
  taosMemoryFree(offsetNew);
554!
558
  return code;
554✔
559
}
560

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

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

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

580
  TQ_ERR_GO_TO_END(tqMetaTransformInfo(pTq->pMetaDB, pExecStore, pTq->pExecStore));
22!
581
  TQ_ERR_GO_TO_END(tqMetaTransformInfo(pTq->pMetaDB, pCheckStore, pTq->pCheckStore));
22!
582

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

585
  if (taosCheckExistFile(offset)) {
22!
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:
22✔
594
  taosMemoryFree(offset);
22!
595
  taosMemoryFree(offsetNew);
22!
596

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

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