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

taosdata / TDengine / #3583

17 Jan 2025 07:28AM UTC coverage: 63.876% (+0.07%) from 63.803%
#3583

push

travis-ci

web-flow
Merge pull request #29594 from taosdata/fix/insert-when-2-replicas

fix/insert-when-2-replicas

141608 of 284535 branches covered (49.77%)

Branch coverage included in aggregate %.

0 of 2 new or added lines in 1 file covered. (0.0%)

396 existing lines in 105 files now uncovered.

220075 of 281695 relevant lines covered (78.13%)

19864675.58 hits per line

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

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

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

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

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

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

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

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

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

125
  if (code != 0) {
209!
126
    tDeleteSTqOffset(info);
×
127
    return TSDB_CODE_OUT_OF_MEMORY;
×
128
  }
129
  return code;
209✔
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) {
10,081✔
166
  if (pTq == NULL || ttb == NULL || key == NULL || value == NULL) {
10,081!
167
    return TSDB_CODE_INVALID_PARA;
×
168
  }
169
  int32_t code = TDB_CODE_SUCCESS;
10,082✔
170
  TXN*    txn = NULL;
10,082✔
171

172
  TQ_ERR_GO_TO_END(
10,082!
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));
10,049!
175
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
10,065!
176
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
10,085!
177

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

192
  TQ_ERR_GO_TO_END(
1,782!
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,778✔
195
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
1,522!
196
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
1,524!
197

198
  return 0;
1,524✔
199

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

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

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

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

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

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

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

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

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

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

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

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

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

285
  handle->pRef = walOpenRef(pVnode->pWal);
1,639✔
286

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

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

297
  initStorageAPI(&reader.api);
1,557✔
298

299
  if (handle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
1,559✔
300
    handle->execHandle.task = qCreateQueueExecTaskInfo(handle->execHandle.execCol.qmsg, &reader, vgId,
2,469✔
301
                                                       &handle->execHandle.numOfCols, handle->consumerId);
1,235✔
302
    TQ_NULL_GO_TO_END(handle->execHandle.task);
1,234!
303
    void* scanner = NULL;
1,234✔
304
    qExtractStreamScanner(handle->execHandle.task, &scanner);
1,234✔
305
    TQ_NULL_GO_TO_END(scanner);
1,233!
306
    handle->execHandle.pTqReader = qExtractReaderFromStreamScanner(scanner);
1,233✔
307
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
1,232!
308
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
324✔
309
    handle->pWalReader = walOpenReader(pVnode->pWal, NULL, 0);
257✔
310
    TQ_NULL_GO_TO_END(handle->pWalReader);
257!
311
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
257✔
312
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
257!
313
    TQ_ERR_GO_TO_END(buildSnapContext(reader.vnode, reader.version, 0, handle->execHandle.subType, handle->fetchMeta,
257!
314
                                      (SSnapContext**)(&reader.sContext)));
315
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, NULL, handle->consumerId);
257✔
316
    TQ_NULL_GO_TO_END(handle->execHandle.task);
257!
317
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
67!
318
    handle->pWalReader = walOpenReader(pVnode->pWal, NULL, 0);
67✔
319
    TQ_NULL_GO_TO_END(handle->pWalReader);
67!
320
    if (handle->execHandle.execTb.qmsg != NULL && strcmp(handle->execHandle.execTb.qmsg, "") != 0) {
67!
321
      if (nodesStringToNode(handle->execHandle.execTb.qmsg, &handle->execHandle.execTb.node) != 0) {
21!
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,
67!
327
                                      handle->execHandle.subType, handle->fetchMeta,
328
                                      (SSnapContext**)(&reader.sContext)));
329
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, NULL, handle->consumerId);
67✔
330
    TQ_NULL_GO_TO_END(handle->execHandle.task);
67!
331
    SArray* tbUidList = NULL;
67✔
332
    int     ret = qGetTableList(handle->execHandle.execTb.suid, pVnode, handle->execHandle.execTb.node, &tbUidList,
67✔
333
                                handle->execHandle.task);
334
    if (ret != TDB_CODE_SUCCESS) {
67!
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,
67!
340
           handle->execHandle.execTb.suid);
341
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
67✔
342
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
67!
343
    tqReaderSetTbUidList(handle->execHandle.pTqReader, tbUidList, NULL);
67✔
344
    taosArrayDestroy(tbUidList);
67✔
345
  }
346

347
END:
×
348
  return code;
1,638✔
349
}
350

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

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

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

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

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

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

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

404
  int32_t code = tqMetaInitHandle(pTq, handle);
1,444✔
405
  if (code != 0) {
1,443!
406
    return code;
×
407
  }
408
  tqInfo("tqMetaCreateHandle %s consumer 0x%" PRIx64 " vgId:%d, snapshotVer:%" PRId64, handle->subKey,
1,443!
409
         handle->consumerId, vgId, handle->snapshotVer);
410
  return taosHashPut(pTq->pHandle, handle->subKey, strlen(handle->subKey), handle, sizeof(STqHandle));
1,446✔
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) {
63,952✔
446
  if (pTq == NULL || key == NULL || pHandle == NULL) {
63,952!
447
    return TSDB_CODE_INVALID_PARA;
×
448
  }
449
  void* data = taosHashGet(pTq->pHandle, key, strlen(key));
63,970✔
450
  if (data == NULL) {
63,978✔
451
    int vLen = 0;
1,588✔
452
    if (tdbTbGet(pTq->pExecStore, key, (int)strlen(key), &data, &vLen) < 0) {
1,588✔
453
      tdbFree(data);
1,384✔
454
      return TSDB_CODE_MND_SUBSCRIBE_NOT_EXIST;
1,465✔
455
    }
456
    STqHandle handle = {0};
195✔
457
    if (tqMetaRestoreHandle(pTq, data, vLen >= 0 ? vLen : 0, &handle) != 0) {
195✔
458
      tdbFree(data);
82✔
459
      tqDestroyTqHandle(&handle);
82✔
460
      return TSDB_CODE_OUT_OF_MEMORY;
83✔
461
    }
462
    tdbFree(data);
113✔
463
    *pHandle = taosHashGet(pTq->pHandle, key, strlen(key));
113✔
464
    if (*pHandle == NULL) {
113!
465
      return TSDB_CODE_OUT_OF_MEMORY;
×
466
    }
467
  } else {
468
    *pHandle = data;
62,390✔
469
  }
470
  return TDB_CODE_SUCCESS;
62,503✔
471
}
472

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

483
END:
12,354✔
484
  return code;
12,354✔
485
}
486

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

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

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

516
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
12,438✔
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)));
86!
519
  }
520
  info.colIdList = NULL;
12,354✔
521

522
END:
12,354✔
523
  tdbFree(pKey);
12,354✔
524
  tdbFree(pVal);
12,354✔
525
  tdbTbcClose(pCur);
12,354✔
526
  tDeleteSTqCheckInfo(&info);
12,354✔
527
  return code;
12,353✔
528
}
529

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

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