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

taosdata / TDengine / #4903

27 Dec 2025 02:36PM UTC coverage: 65.73% (+0.09%) from 65.642%
#4903

push

travis-ci

web-flow
fix: ci errors (#34079)

192966 of 293572 relevant lines covered (65.73%)

118504973.46 hits per line

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

53.03
/source/dnode/mnode/impl/src/mndRetention.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 "mndRetention.h"
16
#include "audit.h"
17
#include "mndCompact.h"
18
#include "mndCompactDetail.h"
19
#include "mndDb.h"
20
#include "mndDef.h"
21
#include "mndDnode.h"
22
#include "mndPrivilege.h"
23
#include "mndRetentionDetail.h"
24
#include "mndShow.h"
25
#include "mndTrans.h"
26
#include "mndVgroup.h"
27
#include "tmisce.h"
28
#include "tmsgcb.h"
29

30
#define MND_RETENTION_VER_NUMBER 1
31

32
static int32_t mndProcessTrimDbTimer(SRpcMsg *pReq);
33
static int32_t mndProcessQueryRetentionTimer(SRpcMsg *pReq);
34
static int32_t mndRetrieveRetention(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
35
static void    mndCancelRetrieveRetention(SMnode *pMnode, void *pIter);
36

37
/**
38
 * @brief mndInitRetention
39
 *  init retention module.
40
 *  - trim is equivalent to retention
41
 * @param pMnode
42
 * @return
43
 */
44

45
int32_t mndInitRetention(SMnode *pMnode) {
395,339✔
46
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_RETENTION, mndRetrieveRetention);
395,339✔
47
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_RETENTION, mndCancelRetrieveRetention);
395,339✔
48
  mndSetMsgHandle(pMnode, TDMT_MND_TRIM_DB_TIMER, mndProcessTrimDbTimer);
395,339✔
49
  mndSetMsgHandle(pMnode, TDMT_MND_KILL_TRIM, mndProcessKillRetentionReq);  // trim is equivalent to retention
395,339✔
50
  mndSetMsgHandle(pMnode, TDMT_VND_QUERY_TRIM_PROGRESS_RSP, mndProcessQueryRetentionRsp);
395,339✔
51
  mndSetMsgHandle(pMnode, TDMT_MND_QUERY_TRIM_TIMER, mndProcessQueryRetentionTimer);
395,339✔
52
  mndSetMsgHandle(pMnode, TDMT_VND_KILL_TRIM_RSP, mndTransProcessRsp);
395,339✔
53

54
  SSdbTable table = {
395,339✔
55
      .sdbType = SDB_RETENTION,
56
      .keyType = SDB_KEY_INT32,
57
      .encodeFp = (SdbEncodeFp)mndRetentionActionEncode,
58
      .decodeFp = (SdbDecodeFp)mndRetentionActionDecode,
59
      .insertFp = (SdbInsertFp)mndRetentionActionInsert,
60
      .updateFp = (SdbUpdateFp)mndRetentionActionUpdate,
61
      .deleteFp = (SdbDeleteFp)mndRetentionActionDelete,
62
  };
63

64
  return sdbSetTable(pMnode->pSdb, table);
395,339✔
65
}
66

67
void mndCleanupRetention(SMnode *pMnode) { mDebug("mnd retention cleanup"); }
395,276✔
68

69
void tFreeRetentionObj(SRetentionObj *pObj) { tFreeCompactObj((SCompactObj *)pObj); }
27,519✔
70

71
int32_t tSerializeSRetentionObj(void *buf, int32_t bufLen, const SRetentionObj *pObj) {
62,640✔
72
  return tSerializeSCompactObj(buf, bufLen, (const SCompactObj *)pObj);
62,640✔
73
}
74

75
int32_t tDeserializeSRetentionObj(void *buf, int32_t bufLen, SRetentionObj *pObj) {
27,519✔
76
  return tDeserializeSCompactObj(buf, bufLen, (SCompactObj *)pObj);
27,519✔
77
}
78

79
SSdbRaw *mndRetentionActionEncode(SRetentionObj *pObj) {
31,320✔
80
  int32_t code = 0;
31,320✔
81
  int32_t lino = 0;
31,320✔
82
  terrno = TSDB_CODE_SUCCESS;
31,320✔
83

84
  void    *buf = NULL;
31,320✔
85
  SSdbRaw *pRaw = NULL;
31,320✔
86

87
  int32_t tlen = tSerializeSRetentionObj(NULL, 0, pObj);
31,320✔
88
  if (tlen < 0) {
31,320✔
89
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
90
    goto OVER;
×
91
  }
92

93
  int32_t size = sizeof(int32_t) + tlen;
31,320✔
94
  pRaw = sdbAllocRaw(SDB_RETENTION, MND_RETENTION_VER_NUMBER, size);
31,320✔
95
  if (pRaw == NULL) {
31,320✔
96
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
97
    goto OVER;
×
98
  }
99

100
  buf = taosMemoryMalloc(tlen);
31,320✔
101
  if (buf == NULL) {
31,320✔
102
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
103
    goto OVER;
×
104
  }
105

106
  tlen = tSerializeSRetentionObj(buf, tlen, pObj);
31,320✔
107
  if (tlen < 0) {
31,320✔
108
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
109
    goto OVER;
×
110
  }
111

112
  int32_t dataPos = 0;
31,320✔
113
  SDB_SET_INT32(pRaw, dataPos, tlen, OVER);
31,320✔
114
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, OVER);
31,320✔
115
  SDB_SET_DATALEN(pRaw, dataPos, OVER);
31,320✔
116

117
OVER:
31,320✔
118
  taosMemoryFreeClear(buf);
31,320✔
119
  if (terrno != TSDB_CODE_SUCCESS) {
31,320✔
120
    mError("retention:%" PRId32 ", failed to encode to raw:%p since %s", pObj->id, pRaw, terrstr());
×
121
    sdbFreeRaw(pRaw);
×
122
    return NULL;
×
123
  }
124

125
  mTrace("retention:%" PRId32 ", encode to raw:%p, row:%p", pObj->id, pRaw, pObj);
31,320✔
126
  return pRaw;
31,320✔
127
}
128

129
SSdbRow *mndRetentionActionDecode(SSdbRaw *pRaw) {
27,519✔
130
  int32_t        code = 0;
27,519✔
131
  int32_t        lino = 0;
27,519✔
132
  SSdbRow       *pRow = NULL;
27,519✔
133
  SRetentionObj *pObj = NULL;
27,519✔
134
  void          *buf = NULL;
27,519✔
135
  terrno = TSDB_CODE_SUCCESS;
27,519✔
136

137
  int8_t sver = 0;
27,519✔
138
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
27,519✔
139
    goto OVER;
×
140
  }
141

142
  if (sver != MND_RETENTION_VER_NUMBER) {
27,519✔
143
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
144
    mError("retention read invalid ver, data ver: %d, curr ver: %d", sver, MND_RETENTION_VER_NUMBER);
×
145
    goto OVER;
×
146
  }
147

148
  pRow = sdbAllocRow(sizeof(SRetentionObj));
27,519✔
149
  if (pRow == NULL) {
27,519✔
150
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
151
    goto OVER;
×
152
  }
153

154
  pObj = sdbGetRowObj(pRow);
27,519✔
155
  if (pObj == NULL) {
27,519✔
156
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
157
    goto OVER;
×
158
  }
159

160
  int32_t tlen;
27,519✔
161
  int32_t dataPos = 0;
27,519✔
162
  SDB_GET_INT32(pRaw, dataPos, &tlen, OVER);
27,519✔
163
  buf = taosMemoryMalloc(tlen + 1);
27,519✔
164
  if (buf == NULL) {
27,519✔
165
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
166
    goto OVER;
×
167
  }
168
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, OVER);
27,519✔
169

170
  if ((terrno = tDeserializeSRetentionObj(buf, tlen, pObj)) < 0) {
27,519✔
171
    goto OVER;
×
172
  }
173

174
OVER:
27,519✔
175
  taosMemoryFreeClear(buf);
27,519✔
176
  if (terrno != TSDB_CODE_SUCCESS) {
27,519✔
177
    mError("retention:%" PRId32 ", failed to decode from raw:%p since %s", pObj->id, pRaw, terrstr());
×
178
    taosMemoryFreeClear(pRow);
×
179
    return NULL;
×
180
  }
181

182
  mTrace("retention:%" PRId32 ", decode from raw:%p, row:%p", pObj->id, pRaw, pObj);
27,519✔
183
  return pRow;
27,519✔
184
}
185

186
int32_t mndRetentionActionInsert(SSdb *pSdb, SRetentionObj *pObj) {
14,930✔
187
  mTrace("retention:%" PRId32 ", perform insert action", pObj->id);
14,930✔
188
  return 0;
14,930✔
189
}
190

191
int32_t mndRetentionActionDelete(SSdb *pSdb, SRetentionObj *pObj) {
27,519✔
192
  mTrace("retention:%" PRId32 ", perform delete action", pObj->id);
27,519✔
193
  tFreeRetentionObj(pObj);
27,519✔
194
  return 0;
27,519✔
195
}
196

197
int32_t mndRetentionActionUpdate(SSdb *pSdb, SRetentionObj *pOldObj, SRetentionObj *pNewObj) {
×
198
  mTrace("retention:%" PRId32 ", perform update action, old row:%p new row:%p", pOldObj->id, pOldObj, pNewObj);
×
199

200
  return 0;
×
201
}
202

203
SRetentionObj *mndAcquireRetention(SMnode *pMnode, int32_t id) {
65,979✔
204
  SSdb          *pSdb = pMnode->pSdb;
65,979✔
205
  SRetentionObj *pObj = sdbAcquire(pSdb, SDB_RETENTION, &id);
65,979✔
206
  if (pObj == NULL && (terrno != TSDB_CODE_SDB_OBJ_NOT_THERE && terrno != TSDB_CODE_SDB_OBJ_CREATING &&
65,979✔
207
                       terrno != TSDB_CODE_SDB_OBJ_DROPPING)) {
×
208
    terrno = TSDB_CODE_APP_ERROR;
×
209
    mError("retention:%" PRId32 ", failed to acquire retention since %s", id, terrstr());
×
210
  }
211
  return pObj;
65,979✔
212
}
213

214
void mndReleaseRetention(SMnode *pMnode, SRetentionObj *pObj) {
65,979✔
215
  SSdb *pSdb = pMnode->pSdb;
65,979✔
216
  sdbRelease(pSdb, pObj);
65,979✔
217
}
65,979✔
218

219
static int32_t mndRetentionGetDbInfo(SMnode *pMnode, int32_t id, char *dbname, int32_t len, int64_t *dbUid) {
26,695✔
220
  int32_t        code = 0;
26,695✔
221
  SRetentionObj *pObj = mndAcquireRetention(pMnode, id);
26,695✔
222
  if (pObj == NULL) {
26,695✔
223
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
224
    if (terrno != 0) code = terrno;
×
225
    TAOS_RETURN(code);
×
226
  }
227

228
  tstrncpy(dbname, pObj->dbname, len);
26,695✔
229
  if (dbUid) *dbUid = pObj->dbUid;
26,695✔
230
  mndReleaseRetention(pMnode, pObj);
26,695✔
231
  TAOS_RETURN(code);
26,695✔
232
}
233

234
int32_t mndAddRetentionToTrans(SMnode *pMnode, STrans *pTrans, SRetentionObj *pObj, SDbObj *pDb, STrimDbRsp *rsp) {
17,063✔
235
  int32_t code = 0;
17,063✔
236
  pObj->id = tGenIdPI32();
17,063✔
237

238
  tstrncpy(pObj->dbname, pDb->name, sizeof(pObj->dbname));
17,063✔
239
  pObj->dbUid = pDb->uid;
17,063✔
240

241
  pObj->startTime = taosGetTimestampMs();
17,063✔
242

243
  SSdbRaw *pVgRaw = mndRetentionActionEncode(pObj);
17,063✔
244
  if (pVgRaw == NULL) {
17,063✔
245
    code = TSDB_CODE_SDB_OBJ_NOT_THERE;
×
246
    if (terrno != 0) code = terrno;
×
247
    TAOS_RETURN(code);
×
248
  }
249
  if ((code = mndTransAppendPrepareLog(pTrans, pVgRaw)) != 0) {
17,063✔
250
    sdbFreeRaw(pVgRaw);
×
251
    TAOS_RETURN(code);
×
252
  }
253

254
  if ((code = sdbSetRawStatus(pVgRaw, SDB_STATUS_READY)) != 0) {
17,063✔
255
    sdbFreeRaw(pVgRaw);
×
256
    TAOS_RETURN(code);
×
257
  }
258

259
  rsp->id = pObj->id;
17,063✔
260

261
  return 0;
17,063✔
262
}
263

264
static int32_t mndRetrieveRetention(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
41,238✔
265
  SMnode        *pMnode = pReq->info.node;
41,238✔
266
  SSdb          *pSdb = pMnode->pSdb;
41,238✔
267
  int32_t        numOfRows = 0;
41,238✔
268
  SRetentionObj *pObj = NULL;
41,238✔
269
  char          *sep = NULL;
41,238✔
270
  SDbObj        *pDb = NULL;
41,238✔
271
  int32_t        code = 0, lino = 0;
41,238✔
272
  char           tmpBuf[TSDB_DB_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
41,238✔
273

274
  if ((pShow->db[0] != 0) && (sep = strchr(pShow->db, '.')) && (*(++sep) != 0)) {
41,238✔
275
    if (IS_SYS_DBNAME(sep)) {
41,238✔
276
      goto _OVER;
×
277
    } else if (!(pDb = mndAcquireDb(pMnode, pShow->db))) {
41,238✔
278
      return terrno;
×
279
    }
280
  }
281

282
  while (numOfRows < rows) {
71,100✔
283
    pShow->pIter = sdbFetch(pSdb, SDB_RETENTION, pShow->pIter, (void **)&pObj);
71,100✔
284
    if (pShow->pIter == NULL) break;
71,100✔
285

286
    SColumnInfoData *pColInfo;
287
    int32_t          cols = 0;
29,862✔
288

289
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
29,862✔
290
    COL_DATA_SET_VAL_GOTO((const char *)&pObj->id, false, pObj, pShow->pIter, _OVER);
29,862✔
291

292
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
29,862✔
293
    if (pDb != NULL && strcmp(pDb->name, pObj->dbname) != 0) {
29,862✔
294
      sdbRelease(pSdb, pObj);
×
295
      continue;
×
296
    }
297
    SName name = {0};
29,862✔
298
    if ((code = tNameFromString(&name, pObj->dbname, T_NAME_ACCT | T_NAME_DB)) != 0) {
29,862✔
299
      sdbRelease(pSdb, pObj);
×
300
      sdbCancelFetch(pSdb, pShow->pIter);
×
301
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
302
    }
303
    (void)tNameGetDbName(&name, varDataVal(tmpBuf));
29,862✔
304
    varDataSetLen(tmpBuf, strlen(varDataVal(tmpBuf)));
29,862✔
305
    COL_DATA_SET_VAL_GOTO((const char *)tmpBuf, false, pObj, pShow->pIter, _OVER);
29,862✔
306

307
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
29,862✔
308
    COL_DATA_SET_VAL_GOTO((const char *)&pObj->startTime, false, pObj, pShow->pIter, _OVER);
29,862✔
309

310
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
29,862✔
311
    tstrncpy(varDataVal(tmpBuf), pObj->triggerType == TSDB_TRIGGER_MANUAL ? "manual" : "auto",
29,862✔
312
             sizeof(tmpBuf) - VARSTR_HEADER_SIZE);
313
    varDataSetLen(tmpBuf, strlen(varDataVal(tmpBuf)));
29,862✔
314
    COL_DATA_SET_VAL_GOTO((const char *)tmpBuf, false, pObj, pShow->pIter, _OVER);
29,862✔
315

316
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
29,862✔
317
    char *optr = "trim";
29,862✔
318
    if (pObj->optrType == TSDB_OPTR_SSMIGRATE) {
29,862✔
319
      optr = "ssmigrate";
×
320
    } else if (pObj->optrType == TSDB_OPTR_ROLLUP) {
29,862✔
321
      optr = "rollup";
23,463✔
322
    }
323
    tstrncpy(varDataVal(tmpBuf), optr, sizeof(tmpBuf) - VARSTR_HEADER_SIZE);
29,862✔
324
    varDataSetLen(tmpBuf, strlen(varDataVal(tmpBuf)));
29,862✔
325
    COL_DATA_SET_VAL_GOTO((const char *)tmpBuf, false, pObj, pShow->pIter, _OVER);
29,862✔
326

327
    sdbRelease(pSdb, pObj);
29,862✔
328
    ++numOfRows;
29,862✔
329
  }
330

331
_OVER:
41,238✔
332
  mndReleaseDb(pMnode, pDb);
41,238✔
333
  if (code != 0) {
41,238✔
334
    mError("failed to retrieve retention at line %d since %s", lino, tstrerror(code));
×
335
    TAOS_RETURN(code);
×
336
  }
337
  pShow->numOfRows += numOfRows;
41,238✔
338
  return numOfRows;
41,238✔
339
}
340

341
static void mndCancelRetrieveRetention(SMnode *pMnode, void *pIter) {
×
342
  SSdb *pSdb = pMnode->pSdb;
×
343
  sdbCancelFetchByType(pSdb, pIter, SDB_RETENTION);
×
344
}
×
345

346
static void *mndBuildKillRetentionReq(SMnode *pMnode, SVgObj *pVgroup, int32_t *pContLen, int32_t id, int32_t dnodeId) {
×
347
  SVKillRetentionReq req = {0};
×
348
  req.taskId = id;
×
349
  req.vgId = pVgroup->vgId;
×
350
  req.dnodeId = dnodeId;
×
351
  terrno = 0;
×
352

353
  mInfo("vgId:%d, build kill retention req", pVgroup->vgId);
×
354
  int32_t contLen = tSerializeSVKillCompactReq(NULL, 0, &req);
×
355
  if (contLen < 0) {
×
356
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
357
    return NULL;
×
358
  }
359
  contLen += sizeof(SMsgHead);
×
360

361
  void *pReq = taosMemoryMalloc(contLen);
×
362
  if (pReq == NULL) {
×
363
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
364
    return NULL;
×
365
  }
366

367
  SMsgHead *pHead = pReq;
×
368
  pHead->contLen = htonl(contLen);
×
369
  pHead->vgId = htonl(pVgroup->vgId);
×
370

371
  int32_t ret = 0;
×
372
  if ((ret = tSerializeSVKillCompactReq((char *)pReq + sizeof(SMsgHead), contLen, &req)) < 0) {
×
373
    taosMemoryFreeClear(pReq);
×
374
    terrno = ret;
×
375
    return NULL;
×
376
  }
377
  *pContLen = contLen;
×
378
  return pReq;
×
379
}
380

381
static int32_t mndAddKillRetentionAction(SMnode *pMnode, STrans *pTrans, SVgObj *pVgroup, int32_t id, int32_t dnodeId) {
×
382
  int32_t      code = 0;
×
383
  STransAction action = {0};
×
384

385
  SDnodeObj *pDnode = mndAcquireDnode(pMnode, dnodeId);
×
386
  if (pDnode == NULL) {
×
387
    code = TSDB_CODE_SDB_OBJ_NOT_THERE;
×
388
    if (terrno != 0) code = terrno;
×
389
    TAOS_RETURN(code);
×
390
  }
391
  action.epSet = mndGetDnodeEpset(pDnode);
×
392
  mndReleaseDnode(pMnode, pDnode);
×
393

394
  int32_t contLen = 0;
×
395
  void   *pReq = mndBuildKillRetentionReq(pMnode, pVgroup, &contLen, id, dnodeId);
×
396
  if (pReq == NULL) {
×
397
    code = TSDB_CODE_SDB_OBJ_NOT_THERE;
×
398
    if (terrno != 0) code = terrno;
×
399
    TAOS_RETURN(code);
×
400
  }
401

402
  action.pCont = pReq;
×
403
  action.contLen = contLen;
×
404
  action.msgType = TDMT_VND_KILL_TRIM;
×
405

406
  mTrace("trans:%d, kill retention msg len:%d", pTrans->id, contLen);
×
407

408
  if ((code = mndTransAppendRedoAction(pTrans, &action)) != 0) {
×
409
    taosMemoryFree(pReq);
×
410
    TAOS_RETURN(code);
×
411
  }
412

413
  return 0;
×
414
}
415

416
static int32_t mndKillRetention(SMnode *pMnode, SRpcMsg *pReq, SRetentionObj *pObj) {
×
417
  int32_t code = 0;
×
418
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_DB, pReq, "kill-retention");
×
419
  if (pTrans == NULL) {
×
420
    mError("retention:%" PRId32 ", failed to drop since %s", pObj->id, terrstr());
×
421
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
422
    if (terrno != 0) code = terrno;
×
423
    TAOS_RETURN(code);
×
424
  }
425
  mInfo("trans:%d, used to kill retention:%" PRId32, pTrans->id, pObj->id);
×
426

427
  mndTransSetDbName(pTrans, pObj->dbname, NULL);
×
428

429
  SSdbRaw *pCommitRaw = mndRetentionActionEncode(pObj);
×
430
  if (pCommitRaw == NULL) {
×
431
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
432
    if (terrno != 0) code = terrno;
×
433
    mndTransDrop(pTrans);
×
434
    TAOS_RETURN(code);
×
435
  }
436
  if ((code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
×
437
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
438
    mndTransDrop(pTrans);
×
439
    TAOS_RETURN(code);
×
440
  }
441
  if ((code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY)) != 0) {
×
442
    mndTransDrop(pTrans);
×
443
    TAOS_RETURN(code);
×
444
  }
445

446
  void *pIter = NULL;
×
447
  while (1) {
×
448
    SCompactDetailObj *pDetail = NULL;
×
449
    pIter = sdbFetch(pMnode->pSdb, SDB_RETENTION_DETAIL, pIter, (void **)&pDetail);
×
450
    if (pIter == NULL) break;
×
451

452
    if (pDetail->id == pObj->id) {
×
453
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, pDetail->vgId);
×
454
      if (pVgroup == NULL) {
×
455
        mError("trans:%d, failed to append redo action since %s", pTrans->id, terrstr());
×
456
        sdbCancelFetch(pMnode->pSdb, pIter);
×
457
        sdbRelease(pMnode->pSdb, pDetail);
×
458
        mndTransDrop(pTrans);
×
459
        code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
460
        if (terrno != 0) code = terrno;
×
461
        TAOS_RETURN(code);
×
462
      }
463

464
      if ((code = mndAddKillRetentionAction(pMnode, pTrans, pVgroup, pObj->id, pDetail->dnodeId)) != 0) {
×
465
        mError("trans:%d, failed to append redo action since %s", pTrans->id, terrstr());
×
466
        sdbCancelFetch(pMnode->pSdb, pIter);
×
467
        sdbRelease(pMnode->pSdb, pDetail);
×
468
        mndTransDrop(pTrans);
×
469
        TAOS_RETURN(code);
×
470
      }
471

472
      mndReleaseVgroup(pMnode, pVgroup);
×
473
    }
474

475
    sdbRelease(pMnode->pSdb, pDetail);
×
476
  }
477

478
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) {
×
479
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
480
    mndTransDrop(pTrans);
×
481
    TAOS_RETURN(code);
×
482
  }
483

484
  mndTransDrop(pTrans);
×
485
  return 0;
×
486
}
487

488
int32_t mndProcessKillRetentionReq(SRpcMsg *pReq) {
×
489
  int32_t           code = 0;
×
490
  int32_t           lino = 0;
×
491
  SKillRetentionReq req = {0};  // reuse SKillCompactReq
×
492
  int64_t           tss = taosGetTimestampMs();
×
493

494
  if ((code = tDeserializeSKillCompactReq(pReq->pCont, pReq->contLen, &req)) != 0) {
×
495
    TAOS_RETURN(code);
×
496
  }
497

498
  mInfo("start to kill retention:%" PRId32, req.id);
×
499

500
  SMnode        *pMnode = pReq->info.node;
×
501
  SRetentionObj *pObj = mndAcquireRetention(pMnode, req.id);
×
502
  if (pObj == NULL) {
×
503
    code = TSDB_CODE_MND_INVALID_RETENTION_ID;
×
504
    tFreeSKillCompactReq(&req);
×
505
    TAOS_RETURN(code);
×
506
  }
507

508
  TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_TRIM_DB), &lino, _OVER);
×
509

510
  TAOS_CHECK_GOTO(mndKillRetention(pMnode, pReq, pObj), &lino, _OVER);
×
511

512
  code = TSDB_CODE_ACTION_IN_PROGRESS;
×
513

514
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
×
515
    char    obj[TSDB_INT32_ID_LEN] = {0};
×
516
    int32_t nBytes = snprintf(obj, sizeof(obj), "%d", pObj->id);
×
517
    if ((uint32_t)nBytes < sizeof(obj)) {
×
518
      int64_t tse = taosGetTimestampMs();
×
519
      double  duration = (double)(tse - tss);
×
520
      duration = duration / 1000;
×
521
      auditRecord(pReq, pMnode->clusterId, "killRetention", pObj->dbname, obj, req.sql, req.sqlLen, duration, 0);
×
522
    } else {
523
      mError("retention:%" PRId32 " failed to audit since %s", pObj->id, tstrerror(TSDB_CODE_OUT_OF_RANGE));
×
524
    }
525
  }
526
_OVER:
×
527
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
528
    mError("failed to kill retention %" PRId32 " since %s", req.id, terrstr());
×
529
  }
530

531
  tFreeSKillCompactReq((SKillCompactReq *)&req);
×
532
  mndReleaseRetention(pMnode, pObj);
×
533

534
  TAOS_RETURN(code);
×
535
}
536

537
// update progress
538
static int32_t mndUpdateRetentionProgress(SMnode *pMnode, SRpcMsg *pReq, int32_t id, SQueryRetentionProgressRsp *rsp) {
63,610✔
539
  int32_t code = 0;
63,610✔
540

541
  void *pIter = NULL;
63,610✔
542
  while (1) {
81,118✔
543
    SRetentionDetailObj *pDetail = NULL;
144,728✔
544
    pIter = sdbFetch(pMnode->pSdb, SDB_RETENTION_DETAIL, pIter, (void **)&pDetail);
144,728✔
545
    if (pIter == NULL) break;
144,728✔
546

547
    if (pDetail->id == id && pDetail->vgId == rsp->vgId && pDetail->dnodeId == rsp->dnodeId) {
144,728✔
548
      pDetail->newNumberFileset = rsp->numberFileset;
63,610✔
549
      pDetail->newFinished = rsp->finished;
63,610✔
550
      pDetail->progress = rsp->progress;
63,610✔
551
      pDetail->remainingTime = rsp->remainingTime;
63,610✔
552

553
      sdbCancelFetch(pMnode->pSdb, pIter);
63,610✔
554
      sdbRelease(pMnode->pSdb, pDetail);
63,610✔
555

556
      TAOS_RETURN(code);
63,610✔
557
    }
558

559
    sdbRelease(pMnode->pSdb, pDetail);
81,118✔
560
  }
561

562
  return TSDB_CODE_MND_COMPACT_DETAIL_NOT_EXIST;
×
563
}
564

565
int32_t mndProcessQueryRetentionRsp(SRpcMsg *pReq) {
63,610✔
566
  int32_t                    code = 0;
63,610✔
567
  SQueryRetentionProgressRsp req = {0};
63,610✔
568
  if (pReq->code != 0) {
63,610✔
569
    mError("received wrong retention response, req code is %s", tstrerror(pReq->code));
×
570
    TAOS_RETURN(pReq->code);
×
571
  }
572
  code = tDeserializeSQueryCompactProgressRsp(pReq->pCont, pReq->contLen, &req);
63,610✔
573
  if (code != 0) {
63,610✔
574
    mError("failed to deserialize vnode-query-retention-progress-rsp, ret:%d, pCont:%p, len:%d", code, pReq->pCont,
×
575
           pReq->contLen);
576
    TAOS_RETURN(code);
×
577
  }
578

579
  mDebug("retention:%d, receive query response, vgId:%d, dnodeId:%d, numberFileset:%d, finished:%d", req.id, req.vgId,
63,610✔
580
         req.dnodeId, req.numberFileset, req.finished);
581

582
  SMnode *pMnode = pReq->info.node;
63,610✔
583

584
  code = mndUpdateRetentionProgress(pMnode, pReq, req.id, &req);
63,610✔
585
  if (code != 0) {
63,610✔
586
    mError("retention:%d, failed to update progress, vgId:%d, dnodeId:%d, numberFileset:%d, finished:%d", req.id,
×
587
           req.vgId, req.dnodeId, req.numberFileset, req.finished);
588
    TAOS_RETURN(code);
×
589
  }
590

591
  TAOS_RETURN(code);
63,610✔
592
}
593

594
// timer
595
void mndRetentionSendProgressReq(SMnode *pMnode, SRetentionObj *pObj) {
26,695✔
596
  void *pIter = NULL;
26,695✔
597

598
  while (1) {
63,610✔
599
    SRetentionDetailObj *pDetail = NULL;
90,305✔
600
    pIter = sdbFetch(pMnode->pSdb, SDB_RETENTION_DETAIL, pIter, (void **)&pDetail);
90,305✔
601
    if (pIter == NULL) break;
90,305✔
602

603
    if (pDetail->id == pObj->id) {
63,610✔
604
      SEpSet epSet = {0};
63,610✔
605

606
      SDnodeObj *pDnode = mndAcquireDnode(pMnode, pDetail->dnodeId);
63,610✔
607
      if (pDnode == NULL) break;
63,610✔
608
      if (addEpIntoEpSet(&epSet, pDnode->fqdn, pDnode->port) != 0) {
63,610✔
609
        sdbRelease(pMnode->pSdb, pDetail);
×
610
        continue;
×
611
      }
612
      mndReleaseDnode(pMnode, pDnode);
63,610✔
613

614
      SQueryRetentionProgressReq req;
63,610✔
615
      req.id = pDetail->id;
63,610✔
616
      req.vgId = pDetail->vgId;
63,610✔
617
      req.dnodeId = pDetail->dnodeId;
63,610✔
618

619
      int32_t contLen = tSerializeSQueryCompactProgressReq(NULL, 0, &req);
63,610✔
620
      if (contLen < 0) {
63,610✔
621
        sdbRelease(pMnode->pSdb, pDetail);
×
622
        continue;
×
623
      }
624

625
      contLen += sizeof(SMsgHead);
63,610✔
626

627
      SMsgHead *pHead = rpcMallocCont(contLen);
63,610✔
628
      if (pHead == NULL) {
63,610✔
629
        sdbRelease(pMnode->pSdb, pDetail);
×
630
        continue;
×
631
      }
632

633
      pHead->contLen = htonl(contLen);
63,610✔
634
      pHead->vgId = htonl(pDetail->vgId);
63,610✔
635

636
      if (tSerializeSQueryCompactProgressReq((char *)pHead + sizeof(SMsgHead), contLen - sizeof(SMsgHead), &req) <= 0) {
63,610✔
637
        sdbRelease(pMnode->pSdb, pDetail);
×
638
        continue;
×
639
      }
640

641
      SRpcMsg rpcMsg = {.msgType = TDMT_VND_QUERY_TRIM_PROGRESS, .contLen = contLen};
63,610✔
642

643
      rpcMsg.pCont = pHead;
63,610✔
644

645
      char    detail[1024] = {0};
63,610✔
646
      int32_t len = tsnprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d",
127,220✔
647
                              TMSG_INFO(TDMT_VND_QUERY_TRIM_PROGRESS), epSet.numOfEps, epSet.inUse);
127,220✔
648
      for (int32_t i = 0; i < epSet.numOfEps; ++i) {
127,220✔
649
        len += tsnprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
63,610✔
650
      }
651

652
      mDebug("retention:%d, send update progress msg to %s", pDetail->id, detail);
63,610✔
653

654
      if (tmsgSendReq(&epSet, &rpcMsg) < 0) {
63,610✔
655
        sdbRelease(pMnode->pSdb, pDetail);
×
656
        continue;
×
657
      }
658
    }
659

660
    sdbRelease(pMnode->pSdb, pDetail);
63,610✔
661
  }
662
}
26,695✔
663

664
static int32_t mndSaveRetentionProgress(SMnode *pMnode, int32_t id) {
26,695✔
665
  int32_t code = 0;
26,695✔
666
  bool    needSave = false;
26,695✔
667
  void   *pIter = NULL;
26,695✔
668
  while (1) {
63,610✔
669
    SRetentionDetailObj *pDetail = NULL;
90,305✔
670
    pIter = sdbFetch(pMnode->pSdb, SDB_RETENTION_DETAIL, pIter, (void **)&pDetail);
90,305✔
671
    if (pIter == NULL) break;
90,305✔
672

673
    if (pDetail->id == id) {
63,610✔
674
      mDebug(
63,610✔
675
          "retention:%d, check save progress, vgId:%d, dnodeId:%d, numberFileset:%d, finished:%d, "
676
          "newNumberFileset:%d, newFinished:%d",
677
          pDetail->id, pDetail->vgId, pDetail->dnodeId, pDetail->numberFileset, pDetail->finished,
678
          pDetail->newNumberFileset, pDetail->newFinished);
679

680
      // these 2 number will jump back after dnode restart, so < is not used here
681
      if (pDetail->numberFileset != pDetail->newNumberFileset || pDetail->finished != pDetail->newFinished)
63,610✔
682
        needSave = true;
34,692✔
683
    }
684

685
    sdbRelease(pMnode->pSdb, pDetail);
63,610✔
686
  }
687

688
  char    dbname[TSDB_TABLE_FNAME_LEN] = {0};
26,695✔
689
  int64_t dbUid = 0;
26,695✔
690
  TAOS_CHECK_RETURN(mndRetentionGetDbInfo(pMnode, id, dbname, TSDB_TABLE_FNAME_LEN, &dbUid));
26,695✔
691

692
  if (!mndDbIsExist(pMnode, dbname, dbUid)) {
26,695✔
693
    needSave = true;
×
694
    mWarn("retention:%" PRId32 ", no db exist, set needSave:%s", id, dbname);
×
695
  }
696

697
  if (!needSave) {
26,695✔
698
    mDebug("retention:%" PRId32 ", no need to save", id);
11,096✔
699
    TAOS_RETURN(code);
11,096✔
700
  }
701

702
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_DB, NULL, "update-retention-progress");
15,599✔
703
  if (pTrans == NULL) {
15,599✔
704
    mError("trans:%" PRId32 ", failed to create since %s", pTrans->id, terrstr());
×
705
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
706
    if (terrno != 0) code = terrno;
×
707
    TAOS_RETURN(code);
×
708
  }
709
  mInfo("retention:%d, trans:%d, used to update retention progress.", id, pTrans->id);
15,599✔
710

711
  mndTransSetDbName(pTrans, dbname, NULL);
15,599✔
712

713
  pIter = NULL;
15,599✔
714
  while (1) {
40,740✔
715
    SRetentionDetailObj *pDetail = NULL;
56,339✔
716
    pIter = sdbFetch(pMnode->pSdb, SDB_RETENTION_DETAIL, pIter, (void **)&pDetail);
56,339✔
717
    if (pIter == NULL) break;
56,339✔
718

719
    if (pDetail->id == id) {
40,740✔
720
      mInfo(
40,740✔
721
          "retention:%d, trans:%d, check compact progress, vgId:%d, dnodeId:%d, numberFileset:%d, finished:%d, "
722
          "newNumberFileset:%d, newFinished:%d",
723
          pDetail->id, pTrans->id, pDetail->vgId, pDetail->dnodeId, pDetail->numberFileset, pDetail->finished,
724
          pDetail->newNumberFileset, pDetail->newFinished);
725

726
      pDetail->numberFileset = pDetail->newNumberFileset;
40,740✔
727
      pDetail->finished = pDetail->newFinished;
40,740✔
728

729
      SSdbRaw *pCommitRaw = mndCompactDetailActionEncode(pDetail);
40,740✔
730
      if (pCommitRaw == NULL) {
40,740✔
731
        sdbCancelFetch(pMnode->pSdb, pIter);
×
732
        sdbRelease(pMnode->pSdb, pDetail);
×
733
        mndTransDrop(pTrans);
×
734
        code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
735
        if (terrno != 0) code = terrno;
×
736
        TAOS_RETURN(code);
×
737
      }
738
      if ((code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
40,740✔
739
        mError("retention:%d, trans:%d, failed to append commit log since %s", pDetail->id, pTrans->id, terrstr());
×
740
        sdbCancelFetch(pMnode->pSdb, pIter);
×
741
        sdbRelease(pMnode->pSdb, pDetail);
×
742
        mndTransDrop(pTrans);
×
743
        TAOS_RETURN(code);
×
744
      }
745
      if ((code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY)) != 0) {
40,740✔
746
        sdbCancelFetch(pMnode->pSdb, pIter);
×
747
        sdbRelease(pMnode->pSdb, pDetail);
×
748
        mndTransDrop(pTrans);
×
749
        TAOS_RETURN(code);
×
750
      }
751
    }
752

753
    sdbRelease(pMnode->pSdb, pDetail);
40,740✔
754
  }
755

756
  bool allFinished = true;
15,599✔
757
  pIter = NULL;
15,599✔
758
  while (1) {
31,782✔
759
    SRetentionDetailObj *pDetail = NULL;
47,381✔
760
    pIter = sdbFetch(pMnode->pSdb, SDB_RETENTION_DETAIL, pIter, (void **)&pDetail);
47,381✔
761
    if (pIter == NULL) break;
47,381✔
762

763
    if (pDetail->id == id) {
34,792✔
764
      mInfo("retention:%d, trans:%d, check compact finished, vgId:%d, dnodeId:%d, numberFileset:%d, finished:%d",
34,792✔
765
            pDetail->id, pTrans->id, pDetail->vgId, pDetail->dnodeId, pDetail->numberFileset, pDetail->finished);
766

767
      if (pDetail->numberFileset == -1 && pDetail->finished == -1) {
34,792✔
768
        allFinished = false;
2,299✔
769
        sdbCancelFetch(pMnode->pSdb, pIter);
2,299✔
770
        sdbRelease(pMnode->pSdb, pDetail);
2,299✔
771
        break;
2,299✔
772
      }
773
      if (pDetail->numberFileset != -1 && pDetail->finished != -1 && pDetail->numberFileset != pDetail->finished) {
32,493✔
774
        allFinished = false;
711✔
775
        sdbCancelFetch(pMnode->pSdb, pIter);
711✔
776
        sdbRelease(pMnode->pSdb, pDetail);
711✔
777
        break;
711✔
778
      }
779
    }
780

781
    sdbRelease(pMnode->pSdb, pDetail);
31,782✔
782
  }
783

784
  if (!mndDbIsExist(pMnode, dbname, dbUid)) {
15,599✔
785
    allFinished = true;
×
786
    mWarn("retention:%" PRId32 ", no db exist, set all finished:%s", id, dbname);
×
787
  }
788

789
  if (allFinished) {
15,599✔
790
    mInfo("retention:%d, all finished", id);
12,589✔
791
    pIter = NULL;
12,589✔
792
    while (1) {
27,775✔
793
      SRetentionDetailObj *pDetail = NULL;
40,364✔
794
      pIter = sdbFetch(pMnode->pSdb, SDB_RETENTION_DETAIL, pIter, (void **)&pDetail);
40,364✔
795
      if (pIter == NULL) break;
40,364✔
796

797
      if (pDetail->id == id) {
27,775✔
798
        SSdbRaw *pCommitRaw = mndRetentionDetailActionEncode(pDetail);
27,775✔
799
        if (pCommitRaw == NULL) {
27,775✔
800
          mndTransDrop(pTrans);
×
801
          code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
802
          if (terrno != 0) code = terrno;
×
803
          TAOS_RETURN(code);
×
804
        }
805
        if ((code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
27,775✔
806
          mError("retention:%d, trans:%d, failed to append commit log since %s", pDetail->id, pTrans->id,
×
807
                 tstrerror(code));
808
          sdbCancelFetch(pMnode->pSdb, pIter);
×
809
          sdbRelease(pMnode->pSdb, pDetail);
×
810
          mndTransDrop(pTrans);
×
811
          TAOS_RETURN(code);
×
812
        }
813
        if ((code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED)) != 0) {
27,775✔
814
          sdbCancelFetch(pMnode->pSdb, pIter);
×
815
          sdbRelease(pMnode->pSdb, pDetail);
×
816
          mndTransDrop(pTrans);
×
817
          TAOS_RETURN(code);
×
818
        }
819
        mInfo("retention:%d, add drop compactdetail action", pDetail->compactDetailId);
27,775✔
820
      }
821

822
      sdbRelease(pMnode->pSdb, pDetail);
27,775✔
823
    }
824

825
    SRetentionObj *pObj = mndAcquireRetention(pMnode, id);
12,589✔
826
    if (pObj == NULL) {
12,589✔
827
      mndTransDrop(pTrans);
×
828
      code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
829
      if (terrno != 0) code = terrno;
×
830
      TAOS_RETURN(code);
×
831
    }
832
    SSdbRaw *pCommitRaw = mndRetentionActionEncode(pObj);
12,589✔
833
    mndReleaseRetention(pMnode, pObj);
12,589✔
834
    if (pCommitRaw == NULL) {
12,589✔
835
      mndTransDrop(pTrans);
×
836
      code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
837
      if (terrno != 0) code = terrno;
×
838
      TAOS_RETURN(code);
×
839
    }
840
    if ((code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
12,589✔
841
      mError("retention:%d, trans:%d, failed to append commit log since %s", id, pTrans->id, tstrerror(code));
×
842
      mndTransDrop(pTrans);
×
843
      TAOS_RETURN(code);
×
844
    }
845
    if ((code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED)) != 0) {
12,589✔
846
      mError("retention:%d, trans:%d, failed to append commit log since %s", id, pTrans->id, tstrerror(code));
×
847
      mndTransDrop(pTrans);
×
848
      TAOS_RETURN(code);
×
849
    }
850
    mInfo("retention:%d, add drop compact action", pObj->id);
12,589✔
851
  }
852

853
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) {
15,599✔
854
    mError("retention:%d, trans:%d, failed to prepare since %s", id, pTrans->id, tstrerror(code));
×
855
    mndTransDrop(pTrans);
×
856
    TAOS_RETURN(code);
×
857
  }
858

859
  mndTransDrop(pTrans);
15,599✔
860
  return 0;
15,599✔
861
}
862

863
static void mndRetentionPullup(SMnode *pMnode) {
2,513,854✔
864
  int32_t code = 0;
2,513,854✔
865
  SSdb   *pSdb = pMnode->pSdb;
2,513,854✔
866
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_RETENTION), sizeof(int32_t));
2,513,854✔
867
  if (pArray == NULL) return;
2,513,854✔
868

869
  void *pIter = NULL;
2,513,854✔
870
  while (1) {
26,695✔
871
    SRetentionObj *pObj = NULL;
2,540,549✔
872
    pIter = sdbFetch(pMnode->pSdb, SDB_RETENTION, pIter, (void **)&pObj);
2,540,549✔
873
    if (pIter == NULL) break;
2,540,549✔
874
    if (taosArrayPush(pArray, &pObj->id) == NULL) {
53,390✔
875
      mError("failed to push retention id:%d into array, but continue pull up", pObj->id);
×
876
    }
877
    sdbRelease(pSdb, pObj);
26,695✔
878
  }
879

880
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
2,540,549✔
881
    int32_t *pId = taosArrayGet(pArray, i);
26,695✔
882
    mInfo("begin to pull up retention:%d", *pId);
26,695✔
883
    SRetentionObj *pObj = mndAcquireRetention(pMnode, *pId);
26,695✔
884
    if (pObj != NULL) {
26,695✔
885
      mInfo("retention:%d, begin to pull up", pObj->id);
26,695✔
886
      mndRetentionSendProgressReq(pMnode, pObj);
26,695✔
887
      if ((code = mndSaveRetentionProgress(pMnode, pObj->id)) != 0) {
26,695✔
888
        mError("retention:%d, failed to save retention progress since %s", pObj->id, tstrerror(code));
×
889
      }
890
      mndReleaseRetention(pMnode, pObj);
26,695✔
891
    }
892
  }
893
  taosArrayDestroy(pArray);
2,513,854✔
894
}
895

896
static int32_t mndTrimDbDispatchAudit(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, STimeWindow *tw) {
×
897
  int64_t tss = taosGetTimestampMs();
×
898
  if (!tsEnableAudit || tsMonitorFqdn[0] == 0 || tsMonitorPort == 0) {
×
899
    return 0;
×
900
  }
901

902
  if (tsAuditLevel < AUDIT_LEVEL_CLUSTER) {
×
903
    return 0;
×
904
  }
905

906
  SName   name = {0};
×
907
  int32_t sqlLen = 0;
×
908
  char    sql[256] = {0};
×
909
  char    skeyStr[40] = {0};
×
910
  char    ekeyStr[40] = {0};
×
911
  char   *pDbName = pDb->name;
×
912

913
  if (tNameFromString(&name, pDb->name, T_NAME_ACCT | T_NAME_DB) == 0) {
×
914
    pDbName = name.dbname;
×
915
  }
916

917
  if (taosFormatUtcTime(skeyStr, sizeof(skeyStr), tw->skey, pDb->cfg.precision) == 0 &&
×
918
      taosFormatUtcTime(ekeyStr, sizeof(ekeyStr), tw->ekey, pDb->cfg.precision) == 0) {
×
919
    sqlLen = tsnprintf(sql, sizeof(sql), "trim db %s start with '%s' end with '%s'", pDbName, skeyStr, ekeyStr);
×
920
  } else {
921
    sqlLen = tsnprintf(sql, sizeof(sql), "trim db %s start with %" PRIi64 " end with %" PRIi64, pDbName, tw->skey,
×
922
                       tw->ekey);
923
  }
924

925
  int64_t tse = taosGetTimestampMs();
×
926
  double  duration = (double)(tse - tss);
×
927
  duration = duration / 1000;
×
928
  auditRecord(NULL, pMnode->clusterId, "autoTrimDB", name.dbname, "", sql, sqlLen, duration, 0);
×
929

930
  return 0;
×
931
}
932

933
extern int32_t mndTrimDb(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, STimeWindow tw, SArray *vgroupIds,
934
                         ETsdbOpType type, ETriggerType triggerType);
935
static int32_t mndTrimDbDispatch(SRpcMsg *pReq) {
51✔
936
  int32_t    code = 0, lino = 0;
51✔
937
  SMnode    *pMnode = pReq->info.node;
51✔
938
  SSdb      *pSdb = pMnode->pSdb;
51✔
939
  int64_t    curSec = taosGetTimestampMs() / 1000;
51✔
940
  STrimDbReq trimReq = {
51✔
941
      .tw.skey = INT64_MIN, .tw.ekey = curSec, .optrType = TSDB_OPTR_NORMAL, .triggerType = TSDB_TRIGGER_AUTO};
942

943
  void   *pIter = NULL;
51✔
944
  SDbObj *pDb = NULL;
51✔
945
  while ((pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb))) {
51✔
946
    if (pDb->cfg.isMount) {
×
947
      sdbRelease(pSdb, pDb);
×
948
      continue;
×
949
    }
950

951
    (void)snprintf(trimReq.db, sizeof(trimReq.db), "%s", pDb->name);
×
952

953
    if ((code = mndTrimDb(pMnode, pReq, pDb, trimReq.tw, trimReq.vgroupIds, trimReq.optrType, trimReq.triggerType)) ==
×
954
        0) {
955
      mInfo("db:%s, start to auto trim, optr:%u, tw:%" PRId64 ",%" PRId64, trimReq.db, trimReq.optrType,
×
956
            trimReq.tw.skey, trimReq.tw.ekey);
957
    } else {
958
      mError("db:%s, failed to auto trim since %s", pDb->name, tstrerror(code));
×
959
      sdbRelease(pSdb, pDb);
×
960
      continue;
×
961
    }
962

963
    TAOS_UNUSED(mndTrimDbDispatchAudit(pMnode, pReq, pDb, &trimReq.tw));
×
964

965
    sdbRelease(pSdb, pDb);
×
966
  }
967
_exit:
51✔
968
  return code;
51✔
969
}
970

971
static int32_t mndProcessQueryRetentionTimer(SRpcMsg *pReq) {
2,513,854✔
972
  mTrace("start to process query trim timer");
2,513,854✔
973
  mndRetentionPullup(pReq->info.node);
2,513,854✔
974
  return 0;
2,513,854✔
975
}
976

977
static int32_t mndProcessTrimDbTimer(SRpcMsg *pReq) {
51✔
978
  mTrace("start to process trim db timer");
51✔
979
  return mndTrimDbDispatch(pReq);
51✔
980
}
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