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

taosdata / TDengine / #4800

16 Oct 2025 09:19AM UTC coverage: 53.935% (-7.1%) from 61.083%
#4800

push

travis-ci

web-flow
Merge b32e3a393 into a190048d5

134724 of 323629 branches covered (41.63%)

Branch coverage included in aggregate %.

184803 of 268802 relevant lines covered (68.75%)

69058627.2 hits per line

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

21.41
/source/dnode/vnode/src/tq/tqSnapshot.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

16
#include "meta.h"
17
#include "tdbInt.h"
18
#include "tq.h"
19

20
// STqSnapReader ========================================
21
struct STqSnapReader {
22
  STQ*    pTq;
23
  int64_t sver;
24
  int64_t ever;
25
  TBC*    pCur;
26
  int8_t  type;
27
};
28

29
int32_t tqSnapReaderOpen(STQ* pTq, int64_t sver, int64_t ever, int8_t type, STqSnapReader** ppReader) {
2,490✔
30
  int      code  = TSDB_CODE_SUCCESS;
2,490✔
31
  int32_t  lino  = 0;
2,490✔
32
  STqSnapReader* pReader = NULL;
2,490✔
33
  TSDB_CHECK_NULL(pTq, code, lino, end, TSDB_CODE_INVALID_MSG);
2,490!
34
  TSDB_CHECK_NULL(ppReader, code, lino, end, TSDB_CODE_INVALID_MSG);
2,490!
35

36
  // alloc
37
  pReader = (STqSnapReader*)taosMemoryCalloc(1, sizeof(STqSnapReader));
2,490!
38
  TSDB_CHECK_NULL(pReader, code, lino, end, terrno);
2,490!
39

40
  pReader->pTq = pTq;
2,490✔
41
  pReader->sver = sver;
2,490✔
42
  pReader->ever = ever;
2,490✔
43
  pReader->type = type;
2,490✔
44

45
  // impl
46
  TTB* pTb = NULL;
2,490✔
47
  if (type == SNAP_DATA_TQ_CHECKINFO) {
2,490✔
48
    pTb = pTq->pCheckStore;
830✔
49
  } else if (type == SNAP_DATA_TQ_HANDLE) {
1,660✔
50
    pTb = pTq->pExecStore;
830✔
51
  } else if (type == SNAP_DATA_TQ_OFFSET) {
830!
52
    pTb = pTq->pOffsetStore;
830✔
53
  } else {
54
    code = TSDB_CODE_INVALID_MSG;
×
55
    goto end;
×
56
  }
57
  code = tdbTbcOpen(pTb, &pReader->pCur, NULL);
2,490✔
58
  TSDB_CHECK_CODE(code, lino, end);
2,490!
59
  code = tdbTbcMoveToFirst(pReader->pCur);
2,490✔
60
  TSDB_CHECK_CODE(code, lino, end);
2,490!
61
  tqInfo("vgId:%d, vnode tq snapshot reader open success", TD_VID(pTq->pVnode));
2,490!
62
  *ppReader = pReader;
2,490✔
63

64
end:
2,490✔
65
  if (code != 0){
2,490!
66
    tqError("%s failed at %d, vnode tq snapshot reader open failed since %s", __func__, lino, tstrerror(code));
×
67
    taosMemoryFreeClear(pReader);
×
68
  }
69

70
  return code;
2,490✔
71
}
72

73
void tqSnapReaderClose(STqSnapReader** ppReader) {
2,490✔
74
  if (ppReader == NULL || *ppReader == NULL) {
2,490!
75
    return;
×
76
  }
77
  tdbTbcClose((*ppReader)->pCur);
2,490✔
78
  taosMemoryFreeClear(*ppReader);
2,490!
79
}
80

81
int32_t tqSnapRead(STqSnapReader* pReader, uint8_t** ppData) {
2,490✔
82
  int     code  = TSDB_CODE_SUCCESS;
2,490✔
83
  int32_t lino  = 0;
2,490✔
84
  void*   pKey  = NULL;
2,490✔
85
  void*   pVal  = NULL;
2,490✔
86
  int32_t kLen  = 0;
2,490✔
87
  int32_t vLen  = 0;
2,490✔
88
  TSDB_CHECK_NULL(pReader, code, lino, end, TSDB_CODE_INVALID_MSG);
2,490!
89
  TSDB_CHECK_NULL(ppData, code, lino, end, TSDB_CODE_INVALID_MSG);
2,490!
90

91
  code = tdbTbcNext(pReader->pCur, &pKey, &kLen, &pVal, &vLen);
2,490✔
92
  TSDB_CHECK_CONDITION(code == 0, code, lino, end, TDB_CODE_SUCCESS);
2,490!
93

94
  *ppData = taosMemoryMalloc(sizeof(SSnapDataHdr) + vLen);
×
95
  TSDB_CHECK_NULL(*ppData, code, lino, end, terrno);
×
96

97
  SSnapDataHdr* pHdr = (SSnapDataHdr*)(*ppData);
×
98
  pHdr->type = pReader->type;
×
99
  pHdr->size = vLen;
×
100
  (void)memcpy(pHdr->data, pVal, vLen);
×
101
  tqInfo("vgId:%d, vnode tq snapshot read data, vLen:%d", TD_VID(pReader->pTq->pVnode), vLen);
×
102

103
end:
2,490✔
104
  if (code != 0) {
2,490!
105
    tqError("%s failed at %d, vnode tq snapshot read data failed since %s", __func__, lino, tstrerror(code));
×
106
  }
107
  tdbFree(pKey);
2,490✔
108
  tdbFree(pVal);
2,490✔
109
  return code;
2,490✔
110
}
111

112
// STqSnapWriter ========================================
113
struct STqSnapWriter {
114
  STQ*    pTq;
115
  int64_t sver;
116
  int64_t ever;
117
  TXN*    txn;
118
};
119

120
int32_t tqSnapWriterOpen(STQ* pTq, int64_t sver, int64_t ever, STqSnapWriter** ppWriter) {
×
121
  int     code  = TSDB_CODE_SUCCESS;
×
122
  int32_t lino  = 0;
×
123
  STqSnapWriter* pWriter = NULL;
×
124

125
  TSDB_CHECK_NULL(pTq, code, lino, end, TSDB_CODE_INVALID_MSG);
×
126
  TSDB_CHECK_NULL(ppWriter, code, lino, end, TSDB_CODE_INVALID_MSG);
×
127

128
  // alloc
129
  pWriter = (STqSnapWriter*)taosMemoryCalloc(1, sizeof(*pWriter));
×
130
  TSDB_CHECK_NULL(pWriter, code, lino, end, terrno);
×
131
  pWriter->pTq = pTq;
×
132
  pWriter->sver = sver;
×
133
  pWriter->ever = ever;
×
134

135
  code = tdbBegin(pTq->pMetaDB, &pWriter->txn, tdbDefaultMalloc, tdbDefaultFree, NULL, 0);
×
136
  TSDB_CHECK_CODE(code, lino, end);
×
137
  tqInfo("vgId:%d, tq snapshot writer opene success", TD_VID(pTq->pVnode));
×
138
  *ppWriter = pWriter;
×
139

140
end:
×
141
  if (code != 0){
×
142
    tqError("%s failed at %d tq snapshot writer open failed since %s", __func__, lino, tstrerror(code));
×
143
    taosMemoryFreeClear(pWriter);
×
144
  }
145
  return code;
×
146
}
147

148
int32_t tqSnapWriterClose(STqSnapWriter** ppWriter, int8_t rollback) {
×
149
  int     code  = TSDB_CODE_SUCCESS;
×
150
  int32_t lino  = 0;
×
151
  STqSnapWriter* pWriter = NULL;
×
152

153
  TSDB_CHECK_NULL(ppWriter, code, lino, end, TSDB_CODE_INVALID_MSG);
×
154
  TSDB_CHECK_NULL(*ppWriter, code, lino, end, TSDB_CODE_INVALID_MSG);
×
155
  pWriter = *ppWriter;
×
156

157
  if (rollback) {
×
158
    tdbAbort(pWriter->pTq->pMetaDB, pWriter->txn);
×
159
  } else {
160
    code = tdbCommit(pWriter->pTq->pMetaDB, pWriter->txn);
×
161
    TSDB_CHECK_CODE(code, lino, end);
×
162

163
    code = tdbPostCommit(pWriter->pTq->pMetaDB, pWriter->txn);
×
164
    TSDB_CHECK_CODE(code, lino, end);
×
165
  }
166
  tqInfo("vgId:%d, tq snapshot writer close success", TD_VID(pWriter->pTq->pVnode));
×
167
  taosMemoryFreeClear(*ppWriter);
×
168

169
end:
×
170
  if (code != 0){
×
171
    tqError("%s failed at %d, tq snapshot writer close failed since %s", __func__, lino, tstrerror(code));
×
172
  }
173
  return code;
×
174
}
175

176
static int32_t tqWriteCheck(STqSnapWriter* pWriter, uint8_t* pData, uint32_t nData){
×
177
  int       code  = TSDB_CODE_SUCCESS;
×
178
  int32_t   lino  = 0;
×
179
  TSDB_CHECK_NULL(pWriter, code, lino, end, TSDB_CODE_INVALID_MSG);
×
180
  TSDB_CHECK_NULL(pData, code, lino, end, TSDB_CODE_INVALID_MSG);
×
181
  TSDB_CHECK_CONDITION(nData >= sizeof(SSnapDataHdr), code, lino, end, TSDB_CODE_INVALID_MSG);
×
182
end:
×
183
  if (code != 0){
×
184
    tqError("%s failed at %d failed since %s", __func__, lino, tstrerror(code));
×
185
  }
186
  return code;
×
187
}
188
int32_t tqSnapHandleWrite(STqSnapWriter* pWriter, uint8_t* pData, uint32_t nData) {
×
189
  int       code  = TSDB_CODE_SUCCESS;
×
190
  int32_t   lino  = 0;
×
191
  SDecoder  decoder = {0};
×
192
  SDecoder* pDecoder = &decoder;
×
193
  STqHandle handle = {0};
×
194
  code = tqWriteCheck(pWriter, pData, nData);
×
195
  TSDB_CHECK_CODE(code, lino, end);
×
196

197
  STQ*      pTq = pWriter->pTq;
×
198
  tDecoderInit(pDecoder, pData + sizeof(SSnapDataHdr), nData - sizeof(SSnapDataHdr));
×
199
  code = tDecodeSTqHandle(pDecoder, &handle);
×
200
  TSDB_CHECK_CODE(code, lino, end);
×
201

202
  taosWLockLatch(&pTq->lock);
×
203
  code = tqMetaSaveInfo(pTq, pTq->pExecStore, handle.subKey, strlen(handle.subKey), pData + sizeof(SSnapDataHdr),
×
204
                        nData - sizeof(SSnapDataHdr));
205
  taosWUnLockLatch(&pTq->lock);
×
206
  TSDB_CHECK_CODE(code, lino, end);
×
207
  tqInfo("vgId:%d, vnode tq snapshot write success", TD_VID(pTq->pVnode));
×
208

209
end:
×
210
  tDecoderClear(pDecoder);
×
211
  tqDestroyTqHandle(&handle);
×
212
  if (code != 0){
×
213
    tqError("%s failed at %d, vnode tq snapshot write failed since %s", __func__, lino, tstrerror(code));
×
214
  }
215
  return code;
×
216
}
217

218
int32_t tqSnapCheckInfoWrite(STqSnapWriter* pWriter, uint8_t* pData, uint32_t nData) {
×
219
  int       code  = TSDB_CODE_SUCCESS;
×
220
  int32_t   lino  = 0;
×
221

222
  code = tqWriteCheck(pWriter, pData, nData);
×
223
  TSDB_CHECK_CODE(code, lino, end);
×
224

225
  STQ*         pTq = pWriter->pTq;
×
226
  STqCheckInfo info = {0};
×
227
  code = tqMetaDecodeCheckInfo(&info, pData + sizeof(SSnapDataHdr), nData - sizeof(SSnapDataHdr));
×
228
  TSDB_CHECK_CODE(code, lino, end);
×
229

230
  taosWLockLatch(&pTq->lock);
×
231
  code = tqMetaSaveInfo(pTq, pTq->pCheckStore, &info.topic, strlen(info.topic), pData + sizeof(SSnapDataHdr),
×
232
                        nData - sizeof(SSnapDataHdr));
233
  taosWUnLockLatch(&pTq->lock);
×
234

235
  tDeleteSTqCheckInfo(&info);
×
236
  TSDB_CHECK_CODE(code, lino, end);
×
237
  tqInfo("vgId:%d, vnode tq check info  write success", TD_VID(pTq->pVnode));
×
238

239
end:
×
240
  if (code != 0){
×
241
    tqError("%s failed at %d, vnode tq check info write failed since %s", __func__, lino, tstrerror(code));
×
242
  }
243
  return code;
×
244
}
245

246
int32_t tqSnapOffsetWrite(STqSnapWriter* pWriter, uint8_t* pData, uint32_t nData) {
×
247
  int       code  = TSDB_CODE_SUCCESS;
×
248
  int32_t   lino  = 0;
×
249
  code = tqWriteCheck(pWriter, pData, nData);
×
250
  TSDB_CHECK_CODE(code, lino, end);
×
251

252
  STQ*    pTq = pWriter->pTq;
×
253
  STqOffset info = {0};
×
254
  code = tqMetaDecodeOffsetInfo(&info, pData + sizeof(SSnapDataHdr), nData - sizeof(SSnapDataHdr));
×
255
  TSDB_CHECK_CODE(code, lino, end);
×
256

257
  taosWLockLatch(&pTq->lock);
×
258
  code = tqMetaSaveInfo(pTq, pTq->pOffsetStore, info.subKey, strlen(info.subKey), pData + sizeof(SSnapDataHdr),
×
259
                        nData - sizeof(SSnapDataHdr));
260
  taosWUnLockLatch(&pTq->lock);
×
261
  tDeleteSTqOffset(&info);
×
262
  TSDB_CHECK_CODE(code, lino, end);
×
263
  tqInfo("vgId:%d, vnode tq offset write success", TD_VID(pTq->pVnode));
×
264

265
end:
×
266
  if (code != 0){
×
267
    tqError("%s failed at %d, vnode tq offset write failed since %s", __func__, lino, tstrerror(code));
×
268
  }
269
  return code;
×
270
}
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