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

taosdata / TDengine / #5048

10 May 2026 03:11AM UTC coverage: 73.222% (+0.07%) from 73.152%
#5048

push

travis-ci

web-flow
merge: from main to 3.0 branch #35290

353 of 452 new or added lines in 9 files covered. (78.1%)

587 existing lines in 140 files now uncovered.

278189 of 379928 relevant lines covered (73.22%)

135206397.85 hits per line

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

57.17
/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 "mndUser.h"
27
#include "mndVgroup.h"
28
#include "tmisce.h"
29
#include "tmsgcb.h"
30

31
#define MND_RETENTION_VER_NUMBER 1
32

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

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

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

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

65
  return sdbSetTable(pMnode->pSdb, table);
515,885✔
66
}
67

68
void mndCleanupRetention(SMnode *pMnode) { mDebug("mnd retention cleanup"); }
515,817✔
69

70
void tFreeRetentionObj(SRetentionObj *pObj) { tFreeCompactObj((SCompactObj *)pObj); }
93,691✔
71

72
int32_t tSerializeSRetentionObj(void *buf, int32_t bufLen, const SRetentionObj *pObj) {
202,074✔
73
  return tSerializeSCompactObj(buf, bufLen, (const SCompactObj *)pObj);
202,074✔
74
}
75

76
int32_t tDeserializeSRetentionObj(void *buf, int32_t bufLen, SRetentionObj *pObj) {
93,691✔
77
  return tDeserializeSCompactObj(buf, bufLen, (SCompactObj *)pObj);
93,691✔
78
}
79

80
SSdbRaw *mndRetentionActionEncode(SRetentionObj *pObj) {
101,037✔
81
  int32_t code = 0;
101,037✔
82
  int32_t lino = 0;
101,037✔
83
  terrno = TSDB_CODE_SUCCESS;
101,037✔
84

85
  void    *buf = NULL;
101,037✔
86
  SSdbRaw *pRaw = NULL;
101,037✔
87

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

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

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

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

113
  int32_t dataPos = 0;
101,037✔
114
  SDB_SET_INT32(pRaw, dataPos, tlen, OVER);
101,037✔
115
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, OVER);
101,037✔
116
  SDB_SET_DATALEN(pRaw, dataPos, OVER);
101,037✔
117

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

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

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

138
  int8_t sver = 0;
93,691✔
139
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) {
93,691✔
140
    goto OVER;
×
141
  }
142

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

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

155
  pObj = sdbGetRowObj(pRow);
93,691✔
156
  if (pObj == NULL) {
93,691✔
157
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
158
    goto OVER;
×
159
  }
160

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

171
  if ((terrno = tDeserializeSRetentionObj(buf, tlen, pObj)) < 0) {
93,691✔
172
    goto OVER;
×
173
  }
174

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

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

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

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

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

201
  return 0;
×
202
}
203

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

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

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

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

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

239
  tstrncpy(pObj->dbname, pDb->name, sizeof(pObj->dbname));
51,567✔
240
  pObj->dbUid = pDb->uid;
51,567✔
241

242
  pObj->startTime = taosGetTimestampMs();
51,567✔
243

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

255
  if ((code = sdbSetRawStatus(pVgRaw, SDB_STATUS_READY)) != 0) {
51,567✔
256
    sdbFreeRaw(pVgRaw);
×
257
    TAOS_RETURN(code);
×
258
  }
259

260
  rsp->id = pObj->id;
51,567✔
261

262
  return 0;
51,567✔
263
}
264

265
static int32_t mndRetrieveRetention(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
42,050✔
266
  SMnode        *pMnode = pReq->info.node;
42,050✔
267
  SSdb          *pSdb = pMnode->pSdb;
42,050✔
268
  int32_t        numOfRows = 0;
42,050✔
269
  SRetentionObj *pObj = NULL;
42,050✔
270
  char          *sep = NULL;
42,050✔
271
  SDbObj        *pDb = NULL;
42,050✔
272
  int32_t        code = 0, lino = 0;
42,050✔
273
  char           tmpBuf[TSDB_DB_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
42,050✔
274
  SUserObj      *pUser = NULL;
42,050✔
275
  SDbObj        *pIterDb = NULL;
42,050✔
276
  char           objFName[TSDB_OBJ_FNAME_LEN + 1] = {0};
42,050✔
277
  bool           showAll = false, showIter = false;
42,050✔
278
  int64_t        dbUid = 0;
42,050✔
279

280
  if ((pShow->db[0] != 0) && (sep = strchr(pShow->db, '.')) && (*(++sep) != 0)) {
42,050✔
281
    if (IS_SYS_DBNAME(sep)) {
42,050✔
282
      goto _OVER;
×
283
    } else if (!(pDb = mndAcquireDb(pMnode, pShow->db))) {
42,050✔
284
      return terrno;
×
285
    }
286
  }
287

288
  MND_SHOW_CHECK_OBJ_PRIVILEGE_ALL(RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), PRIV_SHOW_RETENTIONS, PRIV_OBJ_DB, 0,
42,050✔
289
                                   _OVER);
290

291
  while (numOfRows < rows) {
71,102✔
292
    pShow->pIter = sdbFetch(pSdb, SDB_RETENTION, pShow->pIter, (void **)&pObj);
71,102✔
293
    if (pShow->pIter == NULL) break;
71,102✔
294

295
    MND_SHOW_CHECK_DB_PRIVILEGE(pDb, pObj->dbname, pObj, RPC_MSG_TOKEN(pReq), MND_OPER_SHOW_RETENTIONS, _OVER);
29,052✔
296

297
    SColumnInfoData *pColInfo;
298
    int32_t          cols = 0;
29,052✔
299

300
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
29,052✔
301
    COL_DATA_SET_VAL_GOTO((const char *)&pObj->id, false, pObj, pShow->pIter, _OVER);
29,052✔
302

303
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
29,052✔
304
    if (pDb != NULL && strcmp(pDb->name, pObj->dbname) != 0) {
29,052✔
305
      sdbRelease(pSdb, pObj);
×
306
      continue;
×
307
    }
308
    SName name = {0};
29,052✔
309
    if ((code = tNameFromString(&name, pObj->dbname, T_NAME_ACCT | T_NAME_DB)) != 0) {
29,052✔
310
      sdbRelease(pSdb, pObj);
×
311
      TAOS_CHECK_GOTO(code, &lino, _OVER);
×
312
    }
313
    (void)tNameGetDbName(&name, varDataVal(tmpBuf));
29,052✔
314
    varDataSetLen(tmpBuf, strlen(varDataVal(tmpBuf)));
29,052✔
315
    COL_DATA_SET_VAL_GOTO((const char *)tmpBuf, false, pObj, pShow->pIter, _OVER);
29,052✔
316

317
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
29,052✔
318
    COL_DATA_SET_VAL_GOTO((const char *)&pObj->startTime, false, pObj, pShow->pIter, _OVER);
29,052✔
319

320
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
29,052✔
321
    tstrncpy(varDataVal(tmpBuf), pObj->triggerType == TSDB_TRIGGER_MANUAL ? "manual" : "auto",
29,052✔
322
             sizeof(tmpBuf) - VARSTR_HEADER_SIZE);
323
    varDataSetLen(tmpBuf, strlen(varDataVal(tmpBuf)));
29,052✔
324
    COL_DATA_SET_VAL_GOTO((const char *)tmpBuf, false, pObj, pShow->pIter, _OVER);
29,052✔
325

326
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
29,052✔
327
    char *optr = "trim";
29,052✔
328
    if (pObj->optrType == TSDB_OPTR_SSMIGRATE) {
29,052✔
329
      optr = "ssmigrate";
×
330
    } else if (pObj->optrType == TSDB_OPTR_ROLLUP) {
29,052✔
331
      optr = "rollup";
24,210✔
332
    }
333
    tstrncpy(varDataVal(tmpBuf), optr, sizeof(tmpBuf) - VARSTR_HEADER_SIZE);
29,052✔
334
    varDataSetLen(tmpBuf, strlen(varDataVal(tmpBuf)));
29,052✔
335
    COL_DATA_SET_VAL_GOTO((const char *)tmpBuf, false, pObj, pShow->pIter, _OVER);
29,052✔
336

337
    sdbRelease(pSdb, pObj);
29,052✔
338
    ++numOfRows;
29,052✔
339
  }
340

341
_OVER:
42,050✔
342
  if (pUser) mndReleaseUser(pMnode, pUser);
42,050✔
343
  mndReleaseDb(pMnode, pDb);
42,050✔
344
  if (code != 0) {
42,050✔
345
    mError("failed to retrieve retention at line %d since %s", lino, tstrerror(code));
×
346
    TAOS_RETURN(code);
×
347
  }
348
  pShow->numOfRows += numOfRows;
42,050✔
349
  return numOfRows;
42,050✔
350
}
351

352
static void mndCancelRetrieveRetention(SMnode *pMnode, void *pIter) {
×
353
  SSdb *pSdb = pMnode->pSdb;
×
354
  sdbCancelFetchByType(pSdb, pIter, SDB_RETENTION);
×
355
}
×
356

357
static void *mndBuildKillRetentionReq(SMnode *pMnode, SVgObj *pVgroup, int32_t *pContLen, int32_t id, int32_t dnodeId) {
×
358
  SVKillRetentionReq req = {0};
×
359
  req.taskId = id;
×
360
  req.vgId = pVgroup->vgId;
×
361
  req.dnodeId = dnodeId;
×
362
  terrno = 0;
×
363

364
  mInfo("vgId:%d, build kill retention req", pVgroup->vgId);
×
365
  int32_t contLen = tSerializeSVKillCompactReq(NULL, 0, &req);
×
366
  if (contLen < 0) {
×
367
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
368
    return NULL;
×
369
  }
370
  contLen += sizeof(SMsgHead);
×
371

372
  void *pReq = taosMemoryMalloc(contLen);
×
373
  if (pReq == NULL) {
×
374
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
375
    return NULL;
×
376
  }
377

378
  SMsgHead *pHead = pReq;
×
379
  pHead->contLen = htonl(contLen);
×
380
  pHead->vgId = htonl(pVgroup->vgId);
×
381

382
  int32_t ret = 0;
×
383
  if ((ret = tSerializeSVKillCompactReq((char *)pReq + sizeof(SMsgHead), contLen, &req)) < 0) {
×
384
    taosMemoryFreeClear(pReq);
×
385
    terrno = ret;
×
386
    return NULL;
×
387
  }
388
  *pContLen = contLen;
×
389
  return pReq;
×
390
}
391

392
static int32_t mndAddKillRetentionAction(SMnode *pMnode, STrans *pTrans, SVgObj *pVgroup, int32_t id, int32_t dnodeId) {
×
393
  int32_t      code = 0;
×
394
  STransAction action = {0};
×
395

396
  SDnodeObj *pDnode = mndAcquireDnode(pMnode, dnodeId);
×
397
  if (pDnode == NULL) {
×
398
    code = TSDB_CODE_SDB_OBJ_NOT_THERE;
×
399
    if (terrno != 0) code = terrno;
×
400
    TAOS_RETURN(code);
×
401
  }
402
  action.epSet = mndGetDnodeEpset(pDnode);
×
403
  mndReleaseDnode(pMnode, pDnode);
×
404

405
  int32_t contLen = 0;
×
406
  void   *pReq = mndBuildKillRetentionReq(pMnode, pVgroup, &contLen, id, dnodeId);
×
407
  if (pReq == NULL) {
×
408
    code = TSDB_CODE_SDB_OBJ_NOT_THERE;
×
409
    if (terrno != 0) code = terrno;
×
410
    TAOS_RETURN(code);
×
411
  }
412

413
  action.pCont = pReq;
×
414
  action.contLen = contLen;
×
415
  action.msgType = TDMT_VND_KILL_TRIM;
×
416

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

419
  if ((code = mndTransAppendRedoAction(pTrans, &action)) != 0) {
×
420
    taosMemoryFree(pReq);
×
421
    TAOS_RETURN(code);
×
422
  }
423

424
  return 0;
×
425
}
426

427
static int32_t mndKillRetention(SMnode *pMnode, SRpcMsg *pReq, SRetentionObj *pObj) {
×
428
  int32_t code = 0;
×
429
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_DB, pReq, "kill-retention");
×
430
  if (pTrans == NULL) {
×
431
    mError("retention:%" PRId32 ", failed to drop since %s", pObj->id, terrstr());
×
432
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
433
    if (terrno != 0) code = terrno;
×
434
    TAOS_RETURN(code);
×
435
  }
436
  mInfo("trans:%d, used to kill retention:%" PRId32, pTrans->id, pObj->id);
×
437

438
  mndTransSetDbName(pTrans, pObj->dbname, NULL);
×
439

440
  SSdbRaw *pCommitRaw = mndRetentionActionEncode(pObj);
×
441
  if (pCommitRaw == NULL) {
×
442
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
443
    if (terrno != 0) code = terrno;
×
444
    mndTransDrop(pTrans);
×
445
    TAOS_RETURN(code);
×
446
  }
447
  if ((code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
×
448
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
×
449
    mndTransDrop(pTrans);
×
450
    TAOS_RETURN(code);
×
451
  }
452
  if ((code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY)) != 0) {
×
453
    mndTransDrop(pTrans);
×
454
    TAOS_RETURN(code);
×
455
  }
456

457
  void *pIter = NULL;
×
458
  while (1) {
×
459
    SCompactDetailObj *pDetail = NULL;
×
460
    pIter = sdbFetch(pMnode->pSdb, SDB_RETENTION_DETAIL, pIter, (void **)&pDetail);
×
461
    if (pIter == NULL) break;
×
462

463
    if (pDetail->id == pObj->id) {
×
464
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, pDetail->vgId);
×
465
      if (pVgroup == NULL) {
×
466
        mError("trans:%d, failed to append redo action since %s", pTrans->id, terrstr());
×
467
        sdbCancelFetch(pMnode->pSdb, pIter);
×
468
        sdbRelease(pMnode->pSdb, pDetail);
×
469
        mndTransDrop(pTrans);
×
470
        code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
471
        if (terrno != 0) code = terrno;
×
472
        TAOS_RETURN(code);
×
473
      }
474

475
      if ((code = mndAddKillRetentionAction(pMnode, pTrans, pVgroup, pObj->id, pDetail->dnodeId)) != 0) {
×
476
        mError("trans:%d, failed to append redo action since %s", pTrans->id, terrstr());
×
477
        sdbCancelFetch(pMnode->pSdb, pIter);
×
478
        sdbRelease(pMnode->pSdb, pDetail);
×
479
        mndTransDrop(pTrans);
×
480
        TAOS_RETURN(code);
×
481
      }
482

483
      mndReleaseVgroup(pMnode, pVgroup);
×
484
    }
485

486
    sdbRelease(pMnode->pSdb, pDetail);
×
487
  }
488

489
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) {
×
490
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
×
491
    mndTransDrop(pTrans);
×
492
    TAOS_RETURN(code);
×
493
  }
494

495
  mndTransDrop(pTrans);
×
496
  return 0;
×
497
}
498

499
int32_t mndProcessKillRetentionReq(SRpcMsg *pReq) {
×
500
  int32_t           code = 0;
×
501
  int32_t           lino = 0;
×
502
  SKillRetentionReq req = {0};  // reuse SKillCompactReq
×
503
  int64_t           tss = taosGetTimestampMs();
×
504

505
  if ((code = tDeserializeSKillCompactReq(pReq->pCont, pReq->contLen, &req)) != 0) {
×
506
    TAOS_RETURN(code);
×
507
  }
508

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

511
  SMnode        *pMnode = pReq->info.node;
×
512
  SRetentionObj *pObj = mndAcquireRetention(pMnode, req.id);
×
513
  if (pObj == NULL) {
×
514
    code = TSDB_CODE_MND_INVALID_RETENTION_ID;
×
515
    tFreeSKillCompactReq(&req);
×
516
    TAOS_RETURN(code);
×
517
  }
518

519
  TAOS_CHECK_GOTO(mndCheckOperPrivilege(pMnode, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_TRIM_DB), &lino, _OVER);
×
520

521
  TAOS_CHECK_GOTO(mndKillRetention(pMnode, pReq, pObj), &lino, _OVER);
×
522

523
  code = TSDB_CODE_ACTION_IN_PROGRESS;
×
524

525
  if (tsAuditLevel >= AUDIT_LEVEL_CLUSTER) {
×
526
    char    obj[TSDB_INT32_ID_LEN] = {0};
×
527
    int32_t nBytes = snprintf(obj, sizeof(obj), "%d", pObj->id);
×
528
    if ((uint32_t)nBytes < sizeof(obj)) {
×
529
      int64_t tse = taosGetTimestampMs();
×
530
      double  duration = (double)(tse - tss);
×
531
      duration = duration / 1000;
×
532
      auditRecord(pReq, pMnode->clusterId, "killRetention", pObj->dbname, obj, req.sql, req.sqlLen, duration, 0);
×
533
    } else {
534
      mError("retention:%" PRId32 " failed to audit since %s", pObj->id, tstrerror(TSDB_CODE_OUT_OF_RANGE));
×
535
    }
536
  }
537
_OVER:
×
538
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
539
    mError("failed to kill retention %" PRId32 " since %s", req.id, terrstr());
×
540
  }
541

542
  tFreeSKillCompactReq((SKillCompactReq *)&req);
×
543
  mndReleaseRetention(pMnode, pObj);
×
544

545
  TAOS_RETURN(code);
×
546
}
547

548
// update progress
549
static int32_t mndUpdateRetentionProgress(SMnode *pMnode, SRpcMsg *pReq, int32_t id, SQueryRetentionProgressRsp *rsp) {
178,612✔
550
  int32_t code = 0;
178,612✔
551

552
  void *pIter = NULL;
178,612✔
553
  while (1) {
354,330✔
554
    SRetentionDetailObj *pDetail = NULL;
532,942✔
555
    pIter = sdbFetch(pMnode->pSdb, SDB_RETENTION_DETAIL, pIter, (void **)&pDetail);
532,942✔
556
    if (pIter == NULL) break;
532,942✔
557

558
    if (pDetail->id == id && pDetail->vgId == rsp->vgId && pDetail->dnodeId == rsp->dnodeId) {
527,179✔
559
      pDetail->newNumberFileset = rsp->numberFileset;
172,849✔
560
      pDetail->newFinished = rsp->finished;
172,849✔
561
      pDetail->progress = rsp->progress;
172,849✔
562
      pDetail->remainingTime = rsp->remainingTime;
172,849✔
563

564
      sdbCancelFetch(pMnode->pSdb, pIter);
172,849✔
565
      sdbRelease(pMnode->pSdb, pDetail);
172,849✔
566

567
      TAOS_RETURN(code);
172,849✔
568
    }
569

570
    sdbRelease(pMnode->pSdb, pDetail);
354,330✔
571
  }
572

573
  return TSDB_CODE_MND_COMPACT_DETAIL_NOT_EXIST;
5,763✔
574
}
575

576
int32_t mndProcessQueryRetentionRsp(SRpcMsg *pReq) {
180,532✔
577
  int32_t                    code = 0;
180,532✔
578
  SQueryRetentionProgressRsp req = {0};
180,532✔
579
  if (pReq->code != 0) {
180,532✔
580
    mError("received wrong retention response, req code is %s", tstrerror(pReq->code));
1,920✔
581
    TAOS_RETURN(pReq->code);
1,920✔
582
  }
583
  code = tDeserializeSQueryCompactProgressRsp(pReq->pCont, pReq->contLen, &req);
178,612✔
584
  if (code != 0) {
178,612✔
585
    mError("failed to deserialize vnode-query-retention-progress-rsp, ret:%d, pCont:%p, len:%d", code, pReq->pCont,
×
586
           pReq->contLen);
587
    TAOS_RETURN(code);
×
588
  }
589

590
  mDebug("retention:%d, receive query response, vgId:%d, dnodeId:%d, numberFileset:%d, finished:%d", req.id, req.vgId,
178,612✔
591
         req.dnodeId, req.numberFileset, req.finished);
592

593
  SMnode *pMnode = pReq->info.node;
178,612✔
594

595
  code = mndUpdateRetentionProgress(pMnode, pReq, req.id, &req);
178,612✔
596
  if (code != 0) {
178,612✔
597
    mError("retention:%d, failed to update progress, vgId:%d, dnodeId:%d, numberFileset:%d, finished:%d", req.id,
5,763✔
598
           req.vgId, req.dnodeId, req.numberFileset, req.finished);
599
    TAOS_RETURN(code);
5,763✔
600
  }
601

602
  TAOS_RETURN(code);
172,849✔
603
}
604

605
// timer
606
void mndRetentionSendProgressReq(SMnode *pMnode, SRetentionObj *pObj) {
106,884✔
607
  void *pIter = NULL;
106,884✔
608

609
  while (1) {
286,951✔
610
    SRetentionDetailObj *pDetail = NULL;
393,835✔
611
    pIter = sdbFetch(pMnode->pSdb, SDB_RETENTION_DETAIL, pIter, (void **)&pDetail);
393,835✔
612
    if (pIter == NULL) break;
393,835✔
613

614
    if (pDetail->id == pObj->id) {
286,951✔
615
      SEpSet epSet = {0};
180,532✔
616

617
      SDnodeObj *pDnode = mndAcquireDnode(pMnode, pDetail->dnodeId);
180,532✔
618
      if (pDnode == NULL) break;
180,532✔
619
      if (addEpIntoEpSet(&epSet, pDnode->fqdn, pDnode->port) != 0) {
180,532✔
620
        sdbRelease(pMnode->pSdb, pDetail);
×
621
        continue;
×
622
      }
623
      mndReleaseDnode(pMnode, pDnode);
180,532✔
624

625
      SQueryRetentionProgressReq req;
180,532✔
626
      req.id = pDetail->id;
180,532✔
627
      req.vgId = pDetail->vgId;
180,532✔
628
      req.dnodeId = pDetail->dnodeId;
180,532✔
629

630
      int32_t contLen = tSerializeSQueryCompactProgressReq(NULL, 0, &req);
180,532✔
631
      if (contLen < 0) {
180,532✔
632
        sdbRelease(pMnode->pSdb, pDetail);
×
633
        continue;
×
634
      }
635

636
      contLen += sizeof(SMsgHead);
180,532✔
637

638
      SMsgHead *pHead = rpcMallocCont(contLen);
180,532✔
639
      if (pHead == NULL) {
180,532✔
640
        sdbRelease(pMnode->pSdb, pDetail);
×
641
        continue;
×
642
      }
643

644
      pHead->contLen = htonl(contLen);
180,532✔
645
      pHead->vgId = htonl(pDetail->vgId);
180,532✔
646

647
      if (tSerializeSQueryCompactProgressReq((char *)pHead + sizeof(SMsgHead), contLen - sizeof(SMsgHead), &req) <= 0) {
180,532✔
648
        sdbRelease(pMnode->pSdb, pDetail);
×
649
        continue;
×
650
      }
651

652
      SRpcMsg rpcMsg = {.msgType = TDMT_VND_QUERY_TRIM_PROGRESS, .contLen = contLen};
180,532✔
653

654
      rpcMsg.pCont = pHead;
180,532✔
655

656
      char    detail[1024] = {0};
180,532✔
657
      int32_t len = snprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d",
361,064✔
658
                             TMSG_INFO(TDMT_VND_QUERY_TRIM_PROGRESS), epSet.numOfEps, epSet.inUse);
361,064✔
659
      for (int32_t i = 0; i < epSet.numOfEps; ++i) {
361,064✔
660
        len += snprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
180,532✔
661
      }
662

663
      mDebug("retention:%d, send update progress msg to %s", pDetail->id, detail);
180,532✔
664

665
      if (tmsgSendReq(&epSet, &rpcMsg) < 0) {
180,532✔
666
        sdbRelease(pMnode->pSdb, pDetail);
×
667
        continue;
×
668
      }
669
    }
670

671
    sdbRelease(pMnode->pSdb, pDetail);
286,951✔
672
  }
673
}
106,884✔
674

675
static int32_t mndSaveRetentionProgress(SMnode *pMnode, int32_t id) {
106,884✔
676
  int32_t code = 0;
106,884✔
677
  bool    needSave = false;
106,884✔
678
  void   *pIter = NULL;
106,884✔
679
  while (1) {
286,951✔
680
    SRetentionDetailObj *pDetail = NULL;
393,835✔
681
    pIter = sdbFetch(pMnode->pSdb, SDB_RETENTION_DETAIL, pIter, (void **)&pDetail);
393,835✔
682
    if (pIter == NULL) break;
393,835✔
683

684
    if (pDetail->id == id) {
286,951✔
685
      mDebug(
180,532✔
686
          "retention:%d, check save progress, vgId:%d, dnodeId:%d, numberFileset:%d, finished:%d, "
687
          "newNumberFileset:%d, newFinished:%d",
688
          pDetail->id, pDetail->vgId, pDetail->dnodeId, pDetail->numberFileset, pDetail->finished,
689
          pDetail->newNumberFileset, pDetail->newFinished);
690

691
      // these 2 number will jump back after dnode restart, so < is not used here
692
      if (pDetail->numberFileset != pDetail->newNumberFileset || pDetail->finished != pDetail->newFinished)
180,532✔
693
        needSave = true;
91,445✔
694
    }
695

696
    sdbRelease(pMnode->pSdb, pDetail);
286,951✔
697
  }
698

699
  char    dbname[TSDB_TABLE_FNAME_LEN] = {0};
106,884✔
700
  int64_t dbUid = 0;
106,884✔
701
  TAOS_CHECK_RETURN(mndRetentionGetDbInfo(pMnode, id, dbname, TSDB_TABLE_FNAME_LEN, &dbUid));
106,884✔
702

703
  if (!mndDbIsExist(pMnode, dbname, dbUid)) {
106,884✔
704
    needSave = true;
642✔
705
    mWarn("retention:%" PRId32 ", no db exist, set needSave:%s", id, dbname);
642✔
706
  }
707

708
  if (!needSave) {
106,884✔
709
    mDebug("retention:%" PRId32 ", no need to save", id);
60,941✔
710
    TAOS_RETURN(code);
60,941✔
711
  }
712

713
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_DB, NULL, "update-retention-progress");
45,943✔
714
  if (pTrans == NULL) {
45,943✔
715
    mError("trans:%" PRId32 ", failed to create since %s", pTrans->id, terrstr());
×
716
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
717
    if (terrno != 0) code = terrno;
×
718
    TAOS_RETURN(code);
×
719
  }
720
  mInfo("retention:%d, trans:%d, used to update retention progress.", id, pTrans->id);
45,943✔
721

722
  mndTransSetDbName(pTrans, dbname, NULL);
45,943✔
723

724
  pIter = NULL;
45,943✔
725
  while (1) {
179,185✔
726
    SRetentionDetailObj *pDetail = NULL;
225,128✔
727
    pIter = sdbFetch(pMnode->pSdb, SDB_RETENTION_DETAIL, pIter, (void **)&pDetail);
225,128✔
728
    if (pIter == NULL) break;
225,128✔
729

730
    if (pDetail->id == id) {
179,185✔
731
      mInfo(
116,408✔
732
          "retention:%d, trans:%d, check compact progress, vgId:%d, dnodeId:%d, numberFileset:%d, finished:%d, "
733
          "newNumberFileset:%d, newFinished:%d",
734
          pDetail->id, pTrans->id, pDetail->vgId, pDetail->dnodeId, pDetail->numberFileset, pDetail->finished,
735
          pDetail->newNumberFileset, pDetail->newFinished);
736

737
      pDetail->numberFileset = pDetail->newNumberFileset;
116,408✔
738
      pDetail->finished = pDetail->newFinished;
116,408✔
739

740
      SSdbRaw *pCommitRaw = mndCompactDetailActionEncode(pDetail);
116,408✔
741
      if (pCommitRaw == NULL) {
116,408✔
742
        sdbCancelFetch(pMnode->pSdb, pIter);
×
743
        sdbRelease(pMnode->pSdb, pDetail);
×
744
        mndTransDrop(pTrans);
×
745
        code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
746
        if (terrno != 0) code = terrno;
×
747
        TAOS_RETURN(code);
×
748
      }
749
      if ((code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
116,408✔
750
        mError("retention:%d, trans:%d, failed to append commit log since %s", pDetail->id, pTrans->id, terrstr());
×
751
        sdbCancelFetch(pMnode->pSdb, pIter);
×
752
        sdbRelease(pMnode->pSdb, pDetail);
×
753
        mndTransDrop(pTrans);
×
754
        TAOS_RETURN(code);
×
755
      }
756
      if ((code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY)) != 0) {
116,408✔
757
        sdbCancelFetch(pMnode->pSdb, pIter);
×
758
        sdbRelease(pMnode->pSdb, pDetail);
×
759
        mndTransDrop(pTrans);
×
760
        TAOS_RETURN(code);
×
761
      }
762
    }
763

764
    sdbRelease(pMnode->pSdb, pDetail);
179,185✔
765
  }
766

767
  bool allFinished = true;
45,943✔
768
  pIter = NULL;
45,943✔
769
  while (1) {
165,389✔
770
    SRetentionDetailObj *pDetail = NULL;
211,332✔
771
    pIter = sdbFetch(pMnode->pSdb, SDB_RETENTION_DETAIL, pIter, (void **)&pDetail);
211,332✔
772
    if (pIter == NULL) break;
211,332✔
773

774
    if (pDetail->id == id) {
170,271✔
775
      mInfo("retention:%d, trans:%d, check compact finished, vgId:%d, dnodeId:%d, numberFileset:%d, finished:%d",
111,450✔
776
            pDetail->id, pTrans->id, pDetail->vgId, pDetail->dnodeId, pDetail->numberFileset, pDetail->finished);
777

778
      if (pDetail->numberFileset == -1 && pDetail->finished == -1) {
111,450✔
779
        allFinished = false;
4,796✔
780
        sdbCancelFetch(pMnode->pSdb, pIter);
4,796✔
781
        sdbRelease(pMnode->pSdb, pDetail);
4,796✔
782
        break;
4,796✔
783
      }
784
      if (pDetail->numberFileset != -1 && pDetail->finished != -1 && pDetail->numberFileset != pDetail->finished) {
106,654✔
785
        allFinished = false;
86✔
786
        sdbCancelFetch(pMnode->pSdb, pIter);
86✔
787
        sdbRelease(pMnode->pSdb, pDetail);
86✔
788
        break;
86✔
789
      }
790
    }
791

792
    sdbRelease(pMnode->pSdb, pDetail);
165,389✔
793
  }
794

795
  if (!mndDbIsExist(pMnode, dbname, dbUid)) {
45,943✔
796
    allFinished = true;
642✔
797
    mWarn("retention:%" PRId32 ", no db exist, set all finished:%s", id, dbname);
642✔
798
  }
799

800
  if (allFinished) {
45,943✔
801
    mInfo("retention:%d, all finished", id);
41,385✔
802
    pIter = NULL;
41,385✔
803
    while (1) {
145,423✔
804
      SRetentionDetailObj *pDetail = NULL;
186,808✔
805
      pIter = sdbFetch(pMnode->pSdb, SDB_RETENTION_DETAIL, pIter, (void **)&pDetail);
186,808✔
806
      if (pIter == NULL) break;
186,808✔
807

808
      if (pDetail->id == id) {
145,423✔
809
        SSdbRaw *pCommitRaw = mndRetentionDetailActionEncode(pDetail);
97,548✔
810
        if (pCommitRaw == NULL) {
97,548✔
811
          mndTransDrop(pTrans);
×
812
          code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
813
          if (terrno != 0) code = terrno;
×
814
          TAOS_RETURN(code);
×
815
        }
816
        if ((code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
97,548✔
817
          mError("retention:%d, trans:%d, failed to append commit log since %s", pDetail->id, pTrans->id,
×
818
                 tstrerror(code));
819
          sdbCancelFetch(pMnode->pSdb, pIter);
×
820
          sdbRelease(pMnode->pSdb, pDetail);
×
821
          mndTransDrop(pTrans);
×
822
          TAOS_RETURN(code);
×
823
        }
824
        if ((code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED)) != 0) {
97,548✔
825
          sdbCancelFetch(pMnode->pSdb, pIter);
×
826
          sdbRelease(pMnode->pSdb, pDetail);
×
827
          mndTransDrop(pTrans);
×
828
          TAOS_RETURN(code);
×
829
        }
830
        mInfo("retention:%d, add drop compactdetail action", pDetail->compactDetailId);
97,548✔
831
      }
832

833
      sdbRelease(pMnode->pSdb, pDetail);
145,423✔
834
    }
835

836
    SRetentionObj *pObj = mndAcquireRetention(pMnode, id);
41,385✔
837
    if (pObj == NULL) {
41,385✔
838
      mndTransDrop(pTrans);
×
839
      code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
840
      if (terrno != 0) code = terrno;
×
841
      TAOS_RETURN(code);
×
842
    }
843
    SSdbRaw *pCommitRaw = mndRetentionActionEncode(pObj);
41,385✔
844
    mndReleaseRetention(pMnode, pObj);
41,385✔
845
    if (pCommitRaw == NULL) {
41,385✔
846
      mndTransDrop(pTrans);
×
847
      code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
848
      if (terrno != 0) code = terrno;
×
849
      TAOS_RETURN(code);
×
850
    }
851
    if ((code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
41,385✔
852
      mError("retention:%d, trans:%d, failed to append commit log since %s", id, pTrans->id, tstrerror(code));
×
853
      mndTransDrop(pTrans);
×
854
      TAOS_RETURN(code);
×
855
    }
856
    if ((code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED)) != 0) {
41,385✔
857
      mError("retention:%d, trans:%d, failed to append commit log since %s", id, pTrans->id, tstrerror(code));
×
858
      mndTransDrop(pTrans);
×
859
      TAOS_RETURN(code);
×
860
    }
861
    mInfo("retention:%d, add drop compact action", pObj->id);
41,385✔
862
  }
863

864
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) {
45,943✔
UNCOV
865
    mError("retention:%d, trans:%d, failed to prepare since %s", id, pTrans->id, tstrerror(code));
×
UNCOV
866
    mndTransDrop(pTrans);
×
UNCOV
867
    TAOS_RETURN(code);
×
868
  }
869

870
  mndTransDrop(pTrans);
45,943✔
871
  return 0;
45,943✔
872
}
873

874
static void mndRetentionPullup(SMnode *pMnode) {
3,825,945✔
875
  int32_t code = 0;
3,825,945✔
876
  SSdb   *pSdb = pMnode->pSdb;
3,825,945✔
877
  SArray *pArray = taosArrayInit(sdbGetSize(pSdb, SDB_RETENTION), sizeof(int32_t));
3,825,945✔
878
  if (pArray == NULL) return;
3,825,945✔
879

880
  void *pIter = NULL;
3,825,945✔
881
  while (1) {
106,884✔
882
    SRetentionObj *pObj = NULL;
3,932,829✔
883
    pIter = sdbFetch(pMnode->pSdb, SDB_RETENTION, pIter, (void **)&pObj);
3,932,829✔
884
    if (pIter == NULL) break;
3,932,829✔
885
    if (taosArrayPush(pArray, &pObj->id) == NULL) {
213,768✔
886
      mError("failed to push retention id:%d into array, but continue pull up", pObj->id);
×
887
    }
888
    sdbRelease(pSdb, pObj);
106,884✔
889
  }
890

891
  for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) {
3,932,829✔
892
    int32_t *pId = taosArrayGet(pArray, i);
106,884✔
893
    mInfo("begin to pull up retention:%d", *pId);
106,884✔
894
    SRetentionObj *pObj = mndAcquireRetention(pMnode, *pId);
106,884✔
895
    if (pObj != NULL) {
106,884✔
896
      mInfo("retention:%d, begin to pull up", pObj->id);
106,884✔
897
      mndRetentionSendProgressReq(pMnode, pObj);
106,884✔
898
      if ((code = mndSaveRetentionProgress(pMnode, pObj->id)) != 0) {
106,884✔
UNCOV
899
        mError("retention:%d, failed to save retention progress since %s", pObj->id, tstrerror(code));
×
900
      }
901
      mndReleaseRetention(pMnode, pObj);
106,884✔
902
    }
903
  }
904
  taosArrayDestroy(pArray);
3,825,945✔
905
}
906

907
static int32_t mndTrimDbDispatchAudit(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, STimeWindow *tw) {
31,654✔
908
  int64_t tss = taosGetTimestampMs();
31,654✔
909
  if (!tsEnableAudit || tsMonitorFqdn[0] == 0 || tsMonitorPort == 0) {
31,654✔
910
    return 0;
31,654✔
911
  }
912

913
  if (tsAuditLevel < AUDIT_LEVEL_CLUSTER) {
×
914
    return 0;
×
915
  }
916

917
  SName   name = {0};
×
918
  int32_t sqlLen = 0;
×
919
  char    sql[256] = {0};
×
920
  char    skeyStr[40] = {0};
×
921
  char    ekeyStr[40] = {0};
×
922
  char   *pDbName = pDb->name;
×
923

924
  if (tNameFromString(&name, pDb->name, T_NAME_ACCT | T_NAME_DB) == 0) {
×
925
    pDbName = name.dbname;
×
926
  }
927

928
  if (taosFormatUtcTime(skeyStr, sizeof(skeyStr), tw->skey, pDb->cfg.precision) == 0 &&
×
929
      taosFormatUtcTime(ekeyStr, sizeof(ekeyStr), tw->ekey, pDb->cfg.precision) == 0) {
×
930
    sqlLen = snprintf(sql, sizeof(sql), "trim db %s start with '%s' end with '%s'", pDbName, skeyStr, ekeyStr);
×
931
  } else {
932
    sqlLen =
933
        snprintf(sql, sizeof(sql), "trim db %s start with %" PRIi64 " end with %" PRIi64, pDbName, tw->skey, tw->ekey);
×
934
  }
935

936
  int64_t tse = taosGetTimestampMs();
×
937
  double  duration = (double)(tse - tss);
×
938
  duration = duration / 1000;
×
939
  auditRecord(NULL, pMnode->clusterId, "autoTrimDB", name.dbname, "", sql, sqlLen, duration, 0);
×
940

941
  return 0;
×
942
}
943

944
extern int32_t mndTrimDb(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, STimeWindow tw, SArray *vgroupIds,
945
                         ETsdbOpType type, ETriggerType triggerType);
946
static int32_t mndTrimDbDispatch(SRpcMsg *pReq) {
15,878✔
947
  int32_t    code = 0, lino = 0;
15,878✔
948
  SMnode    *pMnode = pReq->info.node;
15,878✔
949
  SSdb      *pSdb = pMnode->pSdb;
15,878✔
950
  int64_t    curSec = taosGetTimestampMs() / 1000;
15,878✔
951
  STrimDbReq trimReq = {
15,878✔
952
      .tw.skey = INT64_MIN, .tw.ekey = curSec, .optrType = TSDB_OPTR_NORMAL, .triggerType = TSDB_TRIGGER_AUTO};
953

954
  void   *pIter = NULL;
15,878✔
955
  SDbObj *pDb = NULL;
15,878✔
956
  while ((pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb))) {
48,396✔
957
    if (pDb->cfg.isMount) {
32,518✔
958
      sdbRelease(pSdb, pDb);
×
959
      continue;
×
960
    }
961

962
    (void)snprintf(trimReq.db, sizeof(trimReq.db), "%s", pDb->name);
32,518✔
963

964
    if ((code = mndTrimDb(pMnode, pReq, pDb, trimReq.tw, trimReq.vgroupIds, trimReq.optrType, trimReq.triggerType)) ==
32,518✔
965
        0) {
966
      mInfo("db:%s, start to auto trim, optr:%u, tw:%" PRId64 ",%" PRId64, trimReq.db, trimReq.optrType,
31,654✔
967
            trimReq.tw.skey, trimReq.tw.ekey);
968
    } else {
969
      mError("db:%s, failed to auto trim since %s", pDb->name, tstrerror(code));
864✔
970
      sdbRelease(pSdb, pDb);
864✔
971
      continue;
864✔
972
    }
973

974
    TAOS_UNUSED(mndTrimDbDispatchAudit(pMnode, pReq, pDb, &trimReq.tw));
31,654✔
975

976
    sdbRelease(pSdb, pDb);
31,654✔
977
  }
978
_exit:
15,878✔
979
  return code;
15,878✔
980
}
981

982
static int32_t mndProcessQueryRetentionTimer(SRpcMsg *pReq) {
3,825,945✔
983
  mTrace("start to process query trim timer");
3,825,945✔
984
  mndRetentionPullup(pReq->info.node);
3,825,945✔
985
  return 0;
3,825,945✔
986
}
987

988
static int32_t mndProcessTrimDbTimer(SRpcMsg *pReq) {
15,878✔
989
  mTrace("start to process trim db timer");
15,878✔
990
  return mndTrimDbDispatch(pReq);
15,878✔
991
}
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