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

taosdata / TDengine / #4788

14 Oct 2025 11:21AM UTC coverage: 60.992% (-2.3%) from 63.264%
#4788

push

travis-ci

web-flow
Merge 7ca9b50f9 into 19574fe21

154868 of 324306 branches covered (47.75%)

Branch coverage included in aggregate %.

207304 of 269498 relevant lines covered (76.92%)

125773493.22 hits per line

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

66.86
/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) {
32,595✔
30
  int      code  = TSDB_CODE_SUCCESS;
32,595✔
31
  int32_t  lino  = 0;
32,595✔
32
  STqSnapReader* pReader = NULL;
32,595✔
33
  TSDB_CHECK_NULL(pTq, code, lino, end, TSDB_CODE_INVALID_MSG);
32,595!
34
  TSDB_CHECK_NULL(ppReader, code, lino, end, TSDB_CODE_INVALID_MSG);
32,595!
35

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

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

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

64
end:
32,595✔
65
  if (code != 0){
32,595!
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;
32,595✔
71
}
72

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

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

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

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

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

103
end:
34,195✔
104
  if (code != 0) {
34,195!
105
    tqError("%s failed at %d, vnode tq snapshot read data failed since %s", __func__, lino, tstrerror(code));
×
106
  }
107
  tdbFree(pKey);
34,195✔
108
  tdbFree(pVal);
34,195✔
109
  return code;
34,195✔
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) {
1,622✔
121
  int     code  = TSDB_CODE_SUCCESS;
1,622✔
122
  int32_t lino  = 0;
1,622✔
123
  STqSnapWriter* pWriter = NULL;
1,622✔
124

125
  TSDB_CHECK_NULL(pTq, code, lino, end, TSDB_CODE_INVALID_MSG);
1,622!
126
  TSDB_CHECK_NULL(ppWriter, code, lino, end, TSDB_CODE_INVALID_MSG);
1,622!
127

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

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

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

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

153
  TSDB_CHECK_NULL(ppWriter, code, lino, end, TSDB_CODE_INVALID_MSG);
1,622!
154
  TSDB_CHECK_NULL(*ppWriter, code, lino, end, TSDB_CODE_INVALID_MSG);
1,622!
155
  pWriter = *ppWriter;
1,622✔
156

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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