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

taosdata / TDengine / #4986

15 Mar 2026 08:32AM UTC coverage: 37.305% (-31.3%) from 68.601%
#4986

push

travis-ci

tomchon
test: keep docs and unit test

125478 of 336361 relevant lines covered (37.3%)

1134847.06 hits per line

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

2.49
/source/dnode/mnode/impl/src/mndScanDetail.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 "mndScanDetail.h"
16
#include "mndDb.h"
17
#include "mndShow.h"
18
#include "mndTrans.h"
19

20
#define MND_SCAN_VER_NUMBER 1
21

22
static int32_t  mndRetrieveScanDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
23
static int32_t  mndScanDetailActionInsert(SSdb *pSdb, SScanDetailObj *pScan);
24
static int32_t  mndScanDetailActionUpdate(SSdb *pSdb, SScanDetailObj *pOldScan, SScanDetailObj *pNewScan);
25
static int32_t  mndScanDetailActionDelete(SSdb *pSdb, SScanDetailObj *pScan);
26

27
int32_t mndInitScanDetail(SMnode *pMnode) {
16✔
28
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_SCAN_DETAIL, mndRetrieveScanDetail);
16✔
29

30
  SSdbTable table = {
16✔
31
      .sdbType = SDB_SCAN_DETAIL,
32
      .keyType = SDB_KEY_INT64,
33
      .encodeFp = (SdbEncodeFp)mndScanDetailActionEncode,
34
      .decodeFp = (SdbDecodeFp)mndScanDetailActionDecode,
35
      .insertFp = (SdbInsertFp)mndScanDetailActionInsert,
36
      .updateFp = (SdbUpdateFp)mndScanDetailActionUpdate,
37
      .deleteFp = (SdbDeleteFp)mndScanDetailActionDelete,
38
  };
39

40
  return sdbSetTable(pMnode->pSdb, table);
16✔
41
}
42

43
void mndCleanupScanDetail(SMnode *pMnode) { mDebug("mnd scan detail cleanup"); }
16✔
44

45
static int32_t mndRetrieveScanDetail(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
×
46
  SMnode         *pMnode = pReq->info.node;
×
47
  SSdb           *pSdb = pMnode->pSdb;
×
48
  int32_t         numOfRows = 0;
×
49
  SScanDetailObj *pScanDetail = NULL;
×
50
  char           *sep = NULL;
×
51
  SDbObj         *pDb = NULL;
×
52

53
  mInfo("retrieve scan detail");
×
54

55
  if (strlen(pShow->db) > 0) {
×
56
    sep = strchr(pShow->db, '.');
×
57
    if (sep &&
×
58
        ((0 == strcmp(sep + 1, TSDB_INFORMATION_SCHEMA_DB) || (0 == strcmp(sep + 1, TSDB_PERFORMANCE_SCHEMA_DB))))) {
×
59
      sep++;
×
60
    } else {
61
      pDb = mndAcquireDb(pMnode, pShow->db);
×
62
      if (pDb == NULL) return terrno;
×
63
    }
64
  }
65

66
  while (numOfRows < rows) {
×
67
    pShow->pIter = sdbFetch(pSdb, SDB_SCAN_DETAIL, pShow->pIter, (void **)&pScanDetail);
×
68
    if (pShow->pIter == NULL) break;
×
69

70
    SColumnInfoData *pColInfo;
71
    SName            n;
72
    int32_t          cols = 0;
×
73

74
    char tmpBuf[TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE] = {0};
×
75

76
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
77
    TAOS_CHECK_RETURN_WITH_RELEASE(colDataSetVal(pColInfo, numOfRows, (const char *)&pScanDetail->scanId, false), pSdb,
×
78
                                   pScanDetail);
79

80
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
81
    TAOS_CHECK_RETURN_WITH_RELEASE(colDataSetVal(pColInfo, numOfRows, (const char *)&pScanDetail->vgId, false), pSdb,
×
82
                                   pScanDetail);
83

84
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
85
    TAOS_CHECK_RETURN_WITH_RELEASE(colDataSetVal(pColInfo, numOfRows, (const char *)&pScanDetail->dnodeId, false), pSdb,
×
86
                                   pScanDetail);
87

88
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
89
    TAOS_CHECK_RETURN_WITH_RELEASE(colDataSetVal(pColInfo, numOfRows, (const char *)&pScanDetail->numberFileset, false),
×
90
                                   pSdb, pScanDetail);
91

92
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
93
    TAOS_CHECK_RETURN_WITH_RELEASE(colDataSetVal(pColInfo, numOfRows, (const char *)&pScanDetail->finished, false),
×
94
                                   pSdb, pScanDetail);
95

96
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
97
    TAOS_CHECK_RETURN_WITH_RELEASE(colDataSetVal(pColInfo, numOfRows, (const char *)&pScanDetail->startTime, false),
×
98
                                   pSdb, pScanDetail);
99

100
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
101
    TAOS_CHECK_RETURN_WITH_RELEASE(colDataSetVal(pColInfo, numOfRows, (const char *)&pScanDetail->progress, false),
×
102
                                   pSdb, pScanDetail);
103

104
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
105
    TAOS_CHECK_RETURN_WITH_RELEASE(colDataSetVal(pColInfo, numOfRows, (const char *)&pScanDetail->remainingTime, false),
×
106
                                   pSdb, pScanDetail);
107

108
    numOfRows++;
×
109
    sdbRelease(pSdb, pScanDetail);
×
110
  }
111

112
  pShow->numOfRows += numOfRows;
×
113
  mndReleaseDb(pMnode, pDb);
×
114
  return numOfRows;
×
115
}
116

117
void tFreeScanDetailObj(SScanDetailObj *pScan) {}
×
118

119
static int32_t tSerializeSScanDetailObj(void *buf, int32_t bufLen, const SScanDetailObj *pObj) {
×
120
  SEncoder encoder = {0};
×
121
  int32_t  code = 0;
×
122
  int32_t  lino;
123
  int32_t  tlen;
124
  tEncoderInit(&encoder, buf, bufLen);
×
125

126
  TAOS_CHECK_EXIT(tStartEncode(&encoder));
×
127
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pObj->scanDetailId));
×
128
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pObj->scanId));
×
129
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pObj->vgId));
×
130
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pObj->dnodeId));
×
131
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pObj->numberFileset));
×
132
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pObj->finished));
×
133
  TAOS_CHECK_EXIT(tEncodeI64(&encoder, pObj->startTime));
×
134
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pObj->newNumberFileset));
×
135
  TAOS_CHECK_EXIT(tEncodeI32(&encoder, pObj->newFinished));
×
136
  // 1. add progress and remaining time
137
  TAOS_CHECK_EXIT(tEncodeI32v(&encoder, pObj->progress));
×
138
  TAOS_CHECK_EXIT(tEncodeI64v(&encoder, pObj->remainingTime));
×
139

140
  tEndEncode(&encoder);
×
141

142
_exit:
×
143
  if (code) {
×
144
    tlen = code;
×
145
  } else {
146
    tlen = encoder.pos;
×
147
  }
148
  tEncoderClear(&encoder);
×
149
  return tlen;
×
150
}
151

152
static int32_t tDeserializeSScanDetailObj(void *buf, int32_t bufLen, SScanDetailObj *pObj) {
×
153
  int32_t  code = 0;
×
154
  int32_t  lino;
155
  SDecoder decoder = {0};
×
156
  tDecoderInit(&decoder, buf, bufLen);
×
157

158
  TAOS_CHECK_EXIT(tStartDecode(&decoder));
×
159
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pObj->scanDetailId));
×
160
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pObj->scanId));
×
161
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pObj->vgId));
×
162
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pObj->dnodeId));
×
163
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pObj->numberFileset));
×
164
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pObj->finished));
×
165
  TAOS_CHECK_EXIT(tDecodeI64(&decoder, &pObj->startTime));
×
166
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pObj->newNumberFileset));
×
167
  TAOS_CHECK_EXIT(tDecodeI32(&decoder, &pObj->newFinished));
×
168
  // 1. add progress and remaining time decode
169
  if (!tDecodeIsEnd(&decoder)) {
×
170
    TAOS_CHECK_EXIT(tDecodeI32v(&decoder, &pObj->progress));
×
171
    TAOS_CHECK_EXIT(tDecodeI64v(&decoder, &pObj->remainingTime));
×
172
  } else {
173
    pObj->progress = 0;
×
174
    pObj->remainingTime = 0;
×
175
  }
176

177
  tEndDecode(&decoder);
×
178

179
_exit:
×
180
  tDecoderClear(&decoder);
×
181
  return code;
×
182
}
183

184
SSdbRaw *mndScanDetailActionEncode(SScanDetailObj *pScan) {
×
185
  int32_t code = 0;
×
186
  int32_t lino = 0;
×
187
  terrno = TSDB_CODE_SUCCESS;
×
188

189
  void    *buf = NULL;
×
190
  SSdbRaw *pRaw = NULL;
×
191

192
  int32_t tlen = tSerializeSScanDetailObj(NULL, 0, pScan);
×
193
  if (tlen < 0) {
×
194
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
195
    goto OVER;
×
196
  }
197

198
  int32_t size = sizeof(int32_t) + tlen;
×
199
  pRaw = sdbAllocRaw(SDB_SCAN_DETAIL, MND_SCAN_VER_NUMBER, size);
×
200
  if (pRaw == NULL) {
×
201
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
202
    goto OVER;
×
203
  }
204

205
  buf = taosMemoryMalloc(tlen);
×
206
  if (buf == NULL) {
×
207
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
208
    goto OVER;
×
209
  }
210

211
  tlen = tSerializeSScanDetailObj(buf, tlen, pScan);
×
212
  if (tlen < 0) {
×
213
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
214
    goto OVER;
×
215
  }
216

217
  int32_t dataPos = 0;
×
218
  SDB_SET_INT32(pRaw, dataPos, tlen, OVER);
×
219
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, OVER);
×
220
  SDB_SET_DATALEN(pRaw, dataPos, OVER);
×
221

222
OVER:
×
223
  taosMemoryFreeClear(buf);
×
224
  if (terrno != TSDB_CODE_SUCCESS) {
×
225
    mError("scan detail:%" PRId32 ", failed to encode to raw:%p since %s", pScan->scanId, pRaw, terrstr());
×
226
    sdbFreeRaw(pRaw);
×
227
    return NULL;
×
228
  }
229

230
  mTrace("scan detail:%" PRId32 ", encode to raw:%p, row:%p", pScan->scanId, pRaw, pScan);
×
231
  return pRaw;
×
232
}
233

234
SSdbRow *mndScanDetailActionDecode(SSdbRaw *pRaw) {
×
235
  int32_t         code = 0;
×
236
  int32_t         lino = 0;
×
237
  SSdbRow        *pRow = NULL;
×
238
  SScanDetailObj *pScan = NULL;
×
239
  void           *buf = NULL;
×
240
  terrno = TSDB_CODE_SUCCESS;
×
241

242
  int8_t sver = 0;
×
243
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
×
244
    goto OVER;
×
245
  }
246

247
  if (sver != MND_SCAN_VER_NUMBER) {
×
248
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
249
    mError("scan detail read invalid ver, data ver: %d, curr ver: %d", sver, MND_SCAN_VER_NUMBER);
×
250
    goto OVER;
×
251
  }
252

253
  pRow = sdbAllocRow(sizeof(SScanObj));
×
254
  if (pRow == NULL) {
×
255
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
256
    goto OVER;
×
257
  }
258

259
  pScan = sdbGetRowObj(pRow);
×
260
  if (pScan == NULL) {
×
261
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
262
    goto OVER;
×
263
  }
264

265
  int32_t tlen;
266
  int32_t dataPos = 0;
×
267
  SDB_GET_INT32(pRaw, dataPos, &tlen, OVER);
×
268
  buf = taosMemoryMalloc(tlen + 1);
×
269
  if (buf == NULL) {
×
270
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
271
    goto OVER;
×
272
  }
273
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, OVER);
×
274

275
  if ((terrno = tDeserializeSScanDetailObj(buf, tlen, pScan)) < 0) {
×
276
    goto OVER;
×
277
  }
278

279
OVER:
×
280
  taosMemoryFreeClear(buf);
×
281
  if (terrno != TSDB_CODE_SUCCESS) {
×
282
    mError("scan detail:%" PRId32 ", failed to decode from raw:%p since %s", pScan->scanId, pRaw, terrstr());
×
283
    taosMemoryFreeClear(pRow);
×
284
    return NULL;
×
285
  }
286

287
  mTrace("scan detail:%" PRId32 ", decode from raw:%p, row:%p", pScan->scanId, pRaw, pScan);
×
288
  return pRow;
×
289
}
290

291
static int32_t mndScanDetailActionInsert(SSdb *pSdb, SScanDetailObj *pScan) {
×
292
  mTrace("scan detail:%" PRId32 ", perform insert action", pScan->scanId);
×
293
  return 0;
×
294
}
295

296
static int32_t mndScanDetailActionDelete(SSdb *pSdb, SScanDetailObj *pScan) {
×
297
  mTrace("scan detail:%" PRId32 ", perform insert action", pScan->scanId);
×
298
  tFreeScanDetailObj(pScan);
×
299
  return 0;
×
300
}
301

302
static int32_t mndScanDetailActionUpdate(SSdb *pSdb, SScanDetailObj *pOldScan, SScanDetailObj *pNewScan) {
×
303
  mTrace("scan detail:%" PRId32 ", perform update action, old row:%p new row:%p", pOldScan->scanId, pOldScan, pNewScan);
×
304

305
  pOldScan->numberFileset = pNewScan->numberFileset;
×
306
  pOldScan->finished = pNewScan->finished;
×
307

308
  return 0;
×
309
}
310

311
int32_t mndAddScanDetailToTran(SMnode *pMnode, STrans *pTrans, SScanObj *pScan, SVgObj *pVgroup, SVnodeGid *pVgid,
×
312
                               int32_t index) {
313
  int32_t        code = 0;
×
314
  SScanDetailObj scanDetail = {0};
×
315
  scanDetail.scanDetailId = index;
×
316
  scanDetail.scanId = pScan->scanId;
×
317
  scanDetail.vgId = pVgroup->vgId;
×
318
  scanDetail.dnodeId = pVgid->dnodeId;
×
319
  scanDetail.startTime = taosGetTimestampMs();
×
320
  scanDetail.numberFileset = -1;
×
321
  scanDetail.finished = -1;
×
322
  scanDetail.newNumberFileset = -1;
×
323
  scanDetail.newFinished = -1;
×
324

325
  mInfo("scan:%d, add scan detail to trans, index:%d, vgId:%d, dnodeId:%d", scanDetail.scanId, scanDetail.scanDetailId,
×
326
        scanDetail.vgId, scanDetail.dnodeId);
327

328
  SSdbRaw *pVgRaw = mndScanDetailActionEncode(&scanDetail);
×
329
  if (pVgRaw == NULL) return -1;
×
330
  if (mndTransAppendCommitlog(pTrans, pVgRaw) != 0) {
×
331
    sdbFreeRaw(pVgRaw);
×
332
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
333
    if (terrno != 0) code = terrno;
×
334
    TAOS_RETURN(code);
×
335
  }
336
  code = sdbSetRawStatus(pVgRaw, SDB_STATUS_READY);
×
337

338
  TAOS_RETURN(code);
×
339
}
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