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

taosdata / TDengine / #3562

20 Dec 2024 09:57AM UTC coverage: 26.655% (-32.2%) from 58.812%
#3562

push

travis-ci

web-flow
Merge pull request #29229 from taosdata/enh/TS-5749-3.0

enh: seperate tsdb async tasks to different thread pools

21498 of 109421 branches covered (19.65%)

Branch coverage included in aggregate %.

66 of 96 new or added lines in 7 files covered. (68.75%)

39441 existing lines in 157 files now uncovered.

35007 of 102566 relevant lines covered (34.13%)

53922.97 hits per line

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

10.49
/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

UNCOV
18
int32_t tEncodeSTqHandle(SEncoder* pEncoder, const STqHandle* pHandle) {
×
UNCOV
19
  if (pEncoder == NULL || pHandle == NULL) {
×
20
    return TSDB_CODE_INVALID_PARA;
×
21
  }
UNCOV
22
  int32_t code = 0;
×
23
  int32_t lino;
24

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
178
  return 0;
×
179

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

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

UNCOV
192
  TQ_ERR_GO_TO_END(
×
193
      tdbBegin(pTq->pMetaDB, &txn, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED));
UNCOV
194
  TQ_ERR_GO_TO_END(tdbTbDelete(ttb, key, kLen, txn));
×
UNCOV
195
  TQ_ERR_GO_TO_END(tdbCommit(pTq->pMetaDB, txn));
×
UNCOV
196
  TQ_ERR_GO_TO_END(tdbPostCommit(pTq->pMetaDB, txn));
×
197

UNCOV
198
  return 0;
×
199

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

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

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

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

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

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

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

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

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

UNCOV
268
  TQ_ERR_GO_TO_END(tqMetaSaveInfo(pTq, pTq->pExecStore, key, strlen(key), buf, vlen));
×
269

UNCOV
270
END:
×
UNCOV
271
  tEncoderClear(&encoder);
×
UNCOV
272
  taosMemoryFree(buf);
×
UNCOV
273
  return code;
×
274
}
275

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

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

UNCOV
285
  handle->pRef = walOpenRef(pVnode->pWal);
×
286

UNCOV
287
  TQ_NULL_GO_TO_END(handle->pRef);
×
UNCOV
288
  TQ_ERR_GO_TO_END(walSetRefVer(handle->pRef, handle->snapshotVer));
×
289

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

UNCOV
297
  initStorageAPI(&reader.api);
×
298

UNCOV
299
  if (handle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
×
UNCOV
300
    handle->execHandle.task = qCreateQueueExecTaskInfo(handle->execHandle.execCol.qmsg, &reader, vgId,
×
UNCOV
301
                                                       &handle->execHandle.numOfCols, handle->consumerId);
×
UNCOV
302
    TQ_NULL_GO_TO_END(handle->execHandle.task);
×
UNCOV
303
    void* scanner = NULL;
×
UNCOV
304
    qExtractStreamScanner(handle->execHandle.task, &scanner);
×
UNCOV
305
    TQ_NULL_GO_TO_END(scanner);
×
UNCOV
306
    handle->execHandle.pTqReader = qExtractReaderFromStreamScanner(scanner);
×
UNCOV
307
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
×
UNCOV
308
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
×
UNCOV
309
    handle->pWalReader = walOpenReader(pVnode->pWal, NULL, 0);
×
UNCOV
310
    TQ_NULL_GO_TO_END(handle->pWalReader);
×
UNCOV
311
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
×
UNCOV
312
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
×
UNCOV
313
    TQ_ERR_GO_TO_END(buildSnapContext(reader.vnode, reader.version, 0, handle->execHandle.subType, handle->fetchMeta,
×
314
                                      (SSnapContext**)(&reader.sContext)));
UNCOV
315
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, NULL, handle->consumerId);
×
UNCOV
316
    TQ_NULL_GO_TO_END(handle->execHandle.task);
×
UNCOV
317
  } else if (handle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
×
UNCOV
318
    handle->pWalReader = walOpenReader(pVnode->pWal, NULL, 0);
×
UNCOV
319
    TQ_NULL_GO_TO_END(handle->pWalReader);
×
UNCOV
320
    if (handle->execHandle.execTb.qmsg != NULL && strcmp(handle->execHandle.execTb.qmsg, "") != 0) {
×
UNCOV
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
    }
UNCOV
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)));
UNCOV
329
    handle->execHandle.task = qCreateQueueExecTaskInfo(NULL, &reader, vgId, NULL, handle->consumerId);
×
UNCOV
330
    TQ_NULL_GO_TO_END(handle->execHandle.task);
×
UNCOV
331
    SArray* tbUidList = NULL;
×
UNCOV
332
    int     ret = qGetTableList(handle->execHandle.execTb.suid, pVnode, handle->execHandle.execTb.node, &tbUidList,
×
333
                                handle->execHandle.task);
UNCOV
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
    }
UNCOV
339
    tqInfo("vgId:%d, tq try to get ctb for stb subscribe, suid:%" PRId64, pVnode->config.vgId,
×
340
           handle->execHandle.execTb.suid);
UNCOV
341
    handle->execHandle.pTqReader = tqReaderOpen(pVnode);
×
UNCOV
342
    TQ_NULL_GO_TO_END(handle->execHandle.pTqReader);
×
UNCOV
343
    tqReaderSetTbUidList(handle->execHandle.pTqReader, tbUidList, NULL);
×
UNCOV
344
    taosArrayDestroy(tbUidList);
×
345
  }
346

347
END:
×
UNCOV
348
  return code;
×
349
}
350

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

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

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

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

UNCOV
376
  (void)memcpy(handle->subKey, req->subKey, TSDB_SUBSCRIBE_KEY_LEN);
×
UNCOV
377
  handle->consumerId = req->newConsumerId;
×
378

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

UNCOV
402
  handle->snapshotVer = walGetCommittedVer(pTq->pVnode->pWal);
×
403

UNCOV
404
  int32_t code = tqMetaInitHandle(pTq, handle);
×
UNCOV
405
  if (code != 0) {
×
406
    return code;
×
407
  }
UNCOV
408
  tqInfo("tqMetaCreateHandle %s consumer 0x%" PRIx64 " vgId:%d, snapshotVer:%" PRId64, handle->subKey,
×
409
         handle->consumerId, vgId, handle->snapshotVer);
UNCOV
410
  return taosHashPut(pTq->pHandle, handle->subKey, strlen(handle->subKey), handle, sizeof(STqHandle));
×
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

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

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

483
END:
553✔
484
  return code;
553✔
485
}
486

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

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

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

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

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

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

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