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

taosdata / TDengine / #3608

12 Feb 2025 05:57AM UTC coverage: 63.066% (+1.4%) from 61.715%
#3608

push

travis-ci

web-flow
Merge pull request #29746 from taosdata/merge/mainto3.02

merge: from main to 3.0 branch

140199 of 286257 branches covered (48.98%)

Branch coverage included in aggregate %.

89 of 161 new or added lines in 18 files covered. (55.28%)

3211 existing lines in 190 files now uncovered.

218998 of 283298 relevant lines covered (77.3%)

5949310.66 hits per line

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

29.78
/source/dnode/vnode/src/tsdb/tsdbRetention.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "tcs.h"
17
#include "tsdb.h"
18
#include "tsdbFS2.h"
19
#include "vnd.h"
20

21
typedef struct {
22
  STsdb  *tsdb;
23
  int32_t szPage;
24
  int64_t now;
25
  int64_t cid;
26

27
  STFileSet   *fset;
28
  TFileOpArray fopArr;
29
} SRTNer;
30

31
static int32_t tsdbDoRemoveFileObject(SRTNer *rtner, const STFileObj *fobj) {
×
32
  STFileOp op = {
×
33
      .optype = TSDB_FOP_REMOVE,
34
      .fid = fobj->f->fid,
×
35
      .of = fobj->f[0],
36
  };
37

38
  return TARRAY2_APPEND(&rtner->fopArr, op);
×
39
}
40

41
static int32_t tsdbCopyFileWithLimitedSpeed(TdFilePtr from, TdFilePtr to, int64_t size, uint32_t limitMB) {
30✔
42
  int64_t interval = 1000;  // 1s
30✔
43
  int64_t limit = limitMB ? limitMB * 1024 * 1024 : INT64_MAX;
30!
44
  int64_t offset = 0;
30✔
45
  int64_t remain = size;
30✔
46

47
  while (remain > 0) {
60✔
48
    int64_t n;
49
    int64_t last = taosGetTimestampMs();
30✔
50
    if ((n = taosFSendFile(to, from, &offset, TMIN(limit, remain))) < 0) {
30!
51
      TAOS_CHECK_RETURN(terrno);
×
52
    }
53

54
    remain -= n;
30✔
55

56
    if (remain > 0) {
30!
57
      int64_t elapsed = taosGetTimestampMs() - last;
×
58
      if (elapsed < interval) {
×
59
        taosMsleep(interval - elapsed);
×
60
      }
61
    }
62
  }
63

64
  return 0;
30✔
65
}
66

67
static int32_t tsdbDoCopyFileLC(SRTNer *rtner, const STFileObj *from, const STFile *to) {
×
68
  int32_t   code = 0;
×
69
  int32_t   lino = 0;
×
70
  TdFilePtr fdFrom = NULL, fdTo = NULL;
×
71
  char      fname_from[TSDB_FILENAME_LEN];
72
  char      fname_to[TSDB_FILENAME_LEN];
73

74
  tsdbTFileLastChunkName(rtner->tsdb, from->f, fname_from);
×
75
  tsdbTFileLastChunkName(rtner->tsdb, to, fname_to);
×
76

77
  fdFrom = taosOpenFile(fname_from, TD_FILE_READ);
×
78
  if (fdFrom == NULL) {
×
79
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
80
  }
81

82
  tsdbInfo("vgId: %d, open tofile: %s size: %" PRId64, TD_VID(rtner->tsdb->pVnode), fname_to, from->f->size);
×
83

84
  fdTo = taosOpenFile(fname_to, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
×
85
  if (fdTo == NULL) {
×
86
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
87
  }
88

89
  SVnodeCfg *pCfg = &rtner->tsdb->pVnode->config;
×
90
  int64_t    chunksize = (int64_t)pCfg->tsdbPageSize * pCfg->s3ChunkSize;
×
91
  int64_t    lc_size = tsdbLogicToFileSize(to->size, rtner->szPage) - chunksize * (to->lcn - 1);
×
92

93
  if (taosFSendFile(fdTo, fdFrom, 0, lc_size) < 0) {
×
94
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
95
  }
96

97
_exit:
×
98
  if (code) {
×
99
    tsdbError("vgId:%d, %s failed at %s:%d since %s", TD_VID(rtner->tsdb->pVnode), __func__, __FILE__, lino,
×
100
              tstrerror(code));
101
  }
102
  if (taosCloseFile(&fdFrom) != 0) {
×
103
    tsdbError("vgId:%d, failed to close file %s", TD_VID(rtner->tsdb->pVnode), fname_from);
×
104
  }
105
  if (taosCloseFile(&fdTo) != 0) {
×
106
    tsdbError("vgId:%d, failed to close file %s", TD_VID(rtner->tsdb->pVnode), fname_to);
×
107
  }
108
  return code;
×
109
}
110

111
static int32_t tsdbDoCopyFile(SRTNer *rtner, const STFileObj *from, const STFile *to) {
30✔
112
  int32_t code = 0;
30✔
113
  int32_t lino = 0;
30✔
114

115
  char      fname[TSDB_FILENAME_LEN];
116
  TdFilePtr fdFrom = NULL;
30✔
117
  TdFilePtr fdTo = NULL;
30✔
118

119
  tsdbTFileName(rtner->tsdb, to, fname);
30✔
120

121
  fdFrom = taosOpenFile(from->fname, TD_FILE_READ);
30✔
122
  if (fdFrom == NULL) {
30!
123
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
124
  }
125

126
  tsdbInfo("vgId: %d, open tofile: %s size: %" PRId64, TD_VID(rtner->tsdb->pVnode), fname, from->f->size);
30!
127

128
  fdTo = taosOpenFile(fname, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
30✔
129
  if (fdTo == NULL) {
30!
130
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
131
  }
132
  TSDB_CHECK_CODE(code, lino, _exit);
30!
133

134
  TAOS_CHECK_GOTO(tsdbCopyFileWithLimitedSpeed(fdFrom, fdTo, tsdbLogicToFileSize(from->f->size, rtner->szPage),
60!
135
                                               tsRetentionSpeedLimitMB),
136
                  &lino, _exit);
137

138
_exit:
30✔
139
  if (code) {
30!
140
    tsdbError("vgId:%d, %s failed at %s:%d since %s", TD_VID(rtner->tsdb->pVnode), __func__, __FILE__, lino,
×
141
              tstrerror(code));
142
  }
143
  if (taosCloseFile(&fdFrom) != 0) {
30!
144
    tsdbTrace("vgId:%d, failed to close file", TD_VID(rtner->tsdb->pVnode));
×
145
  }
146
  if (taosCloseFile(&fdTo) != 0) {
30!
147
    tsdbTrace("vgId:%d, failed to close file", TD_VID(rtner->tsdb->pVnode));
×
148
  }
149
  return code;
30✔
150
}
151

152
static int32_t tsdbDoMigrateFileObj(SRTNer *rtner, const STFileObj *fobj, const SDiskID *did) {
30✔
153
  int32_t  code = 0;
30✔
154
  int32_t  lino = 0;
30✔
155
  STFileOp op = {0};
30✔
156
  int32_t  lcn = fobj->f->lcn;
30✔
157

158
  // remove old
159
  op = (STFileOp){
30✔
160
      .optype = TSDB_FOP_REMOVE,
161
      .fid = fobj->f->fid,
30✔
162
      .of = fobj->f[0],
30✔
163
  };
164

165
  TAOS_CHECK_GOTO(TARRAY2_APPEND(&rtner->fopArr, op), &lino, _exit);
60!
166

167
  // create new
168
  op = (STFileOp){
30✔
169
      .optype = TSDB_FOP_CREATE,
170
      .fid = fobj->f->fid,
30✔
171
      .nf =
172
          {
173
              .type = fobj->f->type,
30✔
174
              .did = did[0],
30✔
175
              .fid = fobj->f->fid,
30✔
176
              .minVer = fobj->f->minVer,
30✔
177
              .maxVer = fobj->f->maxVer,
30✔
178
              .cid = fobj->f->cid,
30✔
179
              .size = fobj->f->size,
30✔
180
              .lcn = lcn,
181
              .stt[0] =
182
                  {
183
                      .level = fobj->f->stt[0].level,
30✔
184
                  },
185
          },
186
  };
187

188
  TAOS_CHECK_GOTO(TARRAY2_APPEND(&rtner->fopArr, op), &lino, _exit);
60!
189

190
  // do copy the file
191

192
  if (lcn < 1) {
30!
193
    TAOS_CHECK_GOTO(tsdbDoCopyFile(rtner, fobj, &op.nf), &lino, _exit);
30!
194
  } else {
195
    TAOS_CHECK_GOTO(tsdbDoCopyFileLC(rtner, fobj, &op.nf), &lino, _exit);
×
196
  }
197

198
_exit:
×
199
  if (code) {
30!
200
    tsdbError("vgId:%d, %s failed at %s:%d since %s", TD_VID(rtner->tsdb->pVnode), __func__, __FILE__, lino,
×
201
              tstrerror(code));
202
  }
203
  return code;
30✔
204
}
205

206
typedef struct {
207
  STsdb  *tsdb;
208
  int64_t now;
209
  int32_t fid;
210
  bool    s3Migrate;
211
} SRtnArg;
212

213
static int32_t tsdbDoRetentionEnd(SRTNer *rtner) {
78✔
214
  int32_t code = 0;
78✔
215
  int32_t lino = 0;
78✔
216

217
  if (TARRAY2_SIZE(&rtner->fopArr) > 0) {
78✔
218
    TAOS_CHECK_GOTO(tsdbFSEditBegin(rtner->tsdb->pFS, &rtner->fopArr, TSDB_FEDIT_RETENTION), &lino, _exit);
12!
219

220
    (void)taosThreadMutexLock(&rtner->tsdb->mutex);
12✔
221

222
    code = tsdbFSEditCommit(rtner->tsdb->pFS);
12✔
223
    if (code) {
12!
224
      (void)taosThreadMutexUnlock(&rtner->tsdb->mutex);
×
225
      TSDB_CHECK_CODE(code, lino, _exit);
×
226
    }
227

228
    (void)taosThreadMutexUnlock(&rtner->tsdb->mutex);
12✔
229

230
    TARRAY2_DESTROY(&rtner->fopArr, NULL);
12!
231
  }
232

233
_exit:
66✔
234
  if (code) {
78!
235
    tsdbError("vgId:%d, %s failed at %s:%d since %s", TD_VID(rtner->tsdb->pVnode), __func__, __FILE__, lino,
×
236
              tstrerror(code));
237
  } else {
238
    tsdbDebug("vid:%d, cid:%" PRId64 ", %s done", TD_VID(rtner->tsdb->pVnode), rtner->cid, __func__);
78!
239
  }
240
  return code;
78✔
241
}
242

243
static int32_t tsdbRemoveOrMoveFileObject(SRTNer *rtner, int32_t expLevel, STFileObj *fobj) {
374✔
244
  int32_t code = 0;
374✔
245
  int32_t lino = 0;
374✔
246

247
  if (fobj == NULL) {
374✔
248
    return code;
210✔
249
  }
250

251
  if (expLevel < 0) {
164!
252
    // remove the file
253
    code = tsdbDoRemoveFileObject(rtner, fobj);
×
UNCOV
254
    TSDB_CHECK_CODE(code, lino, _exit);
×
255
  } else if (expLevel > fobj->f->did.level) {
164✔
256
    // Try to move the file to a new level
257
    for (; expLevel > fobj->f->did.level; expLevel--) {
30!
258
      SDiskID diskId = {0};
30✔
259

260
      code = tsdbAllocateDiskAtLevel(rtner->tsdb, expLevel, tsdbFTypeLabel(fobj->f->type), &diskId);
30✔
261
      if (code) {
30!
UNCOV
262
        tsdbTrace("vgId:%d, cannot allocate disk for file %s, level:%d, reason:%s, skip!", TD_VID(rtner->tsdb->pVnode),
×
263
                  fobj->fname, expLevel, tstrerror(code));
UNCOV
264
        code = 0;
×
UNCOV
265
        continue;
×
266
      } else {
267
        tsdbInfo("vgId:%d start to migrate file %s from level %d to %d, size:%" PRId64, TD_VID(rtner->tsdb->pVnode),
30!
268
                 fobj->fname, fobj->f->did.level, diskId.level, fobj->f->size);
269

270
        code = tsdbDoMigrateFileObj(rtner, fobj, &diskId);
30✔
271
        TSDB_CHECK_CODE(code, lino, _exit);
30!
272

273
        tsdbInfo("vgId:%d end to migrate file %s from level %d to %d, size:%" PRId64, TD_VID(rtner->tsdb->pVnode),
30!
274
                 fobj->fname, fobj->f->did.level, diskId.level, fobj->f->size);
275
        break;
30✔
276
      }
277
    }
278
  }
279

280
_exit:
134✔
281
  if (code) {
164!
UNCOV
282
    tsdbError("vgId:%d, %s failed at %s:%d since %s", TD_VID(rtner->tsdb->pVnode), __func__, __FILE__, lino,
×
283
              tstrerror(code));
284
  }
285
  return code;
164✔
286
}
287

288
static int32_t tsdbDoRetention(SRTNer *rtner) {
78✔
289
  int32_t    code = 0;
78✔
290
  int32_t    lino = 0;
78✔
291
  STFileObj *fobj = NULL;
78✔
292
  STFileSet *fset = rtner->fset;
78✔
293

294
  // handle data file sets
295
  int32_t expLevel = tsdbFidLevel(fset->fid, &rtner->tsdb->keepCfg, rtner->now);
78✔
296
  for (int32_t ftype = 0; ftype < TSDB_FTYPE_MAX; ++ftype) {
390✔
297
    code = tsdbRemoveOrMoveFileObject(rtner, expLevel, fset->farr[ftype]);
312✔
298
    TSDB_CHECK_CODE(code, lino, _exit);
312!
299
  }
300

301
  // handle stt file
302
  SSttLvl *lvl;
303
  TARRAY2_FOREACH(fset->lvlArr, lvl) {
122✔
304
    TARRAY2_FOREACH(lvl->fobjArr, fobj) {
106✔
305
      code = tsdbRemoveOrMoveFileObject(rtner, expLevel, fobj);
62✔
306
      TSDB_CHECK_CODE(code, lino, _exit);
62!
307
    }
308
  }
309

310
_exit:
78✔
311
  if (code) {
78!
UNCOV
312
    tsdbError("vgId:%d, %s failed at %s:%d since %s", TD_VID(rtner->tsdb->pVnode), __func__, __FILE__, lino,
×
313
              tstrerror(code));
314
  }
315
  return code;
78✔
316
}
317

UNCOV
318
static void tsdbRetentionCancel(void *arg) { taosMemoryFree(arg); }
×
319

320
static int32_t tsdbDoS3Migrate(SRTNer *rtner);
321

322
static int32_t tsdbRetention(void *arg) {
78✔
323
  int32_t code = 0;
78✔
324
  int32_t lino = 0;
78✔
325

326
  SRtnArg   *rtnArg = (SRtnArg *)arg;
78✔
327
  STsdb     *pTsdb = rtnArg->tsdb;
78✔
328
  SVnode    *pVnode = pTsdb->pVnode;
78✔
329
  STFileSet *fset = NULL;
78✔
330
  SRTNer     rtner = {
156✔
331
          .tsdb = pTsdb,
332
          .szPage = pVnode->config.tsdbPageSize,
78✔
333
          .now = rtnArg->now,
78✔
334
          .cid = tsdbFSAllocEid(pTsdb->pFS),
78✔
335
  };
336

337
  // begin task
338
  (void)taosThreadMutexLock(&pTsdb->mutex);
78✔
339

340
  // check if background task is disabled
341
  if (pTsdb->bgTaskDisabled) {
78!
UNCOV
342
    tsdbInfo("vgId:%d, background task is disabled, skip retention", TD_VID(pTsdb->pVnode));
×
UNCOV
343
    (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
UNCOV
344
    return 0;
×
345
  }
346

347
  // set flag and copy
348
  tsdbBeginTaskOnFileSet(pTsdb, rtnArg->fid, EVA_TASK_RETENTION, &fset);
78✔
349
  if (fset && (code = tsdbTFileSetInitCopy(pTsdb, fset, &rtner.fset))) {
78!
UNCOV
350
    (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
UNCOV
351
    TSDB_CHECK_CODE(code, lino, _exit);
×
352
  }
353

354
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
78✔
355

356
  // do retention
357
  if (rtner.fset) {
78!
358
    if (rtnArg->s3Migrate) {
78!
UNCOV
359
      TAOS_CHECK_GOTO(tsdbDoS3Migrate(&rtner), &lino, _exit);
×
360
    } else {
361
      TAOS_CHECK_GOTO(tsdbDoRetention(&rtner), &lino, _exit);
78!
362
    }
363

364
    TAOS_CHECK_GOTO(tsdbDoRetentionEnd(&rtner), &lino, _exit);
78!
365
  }
366

367
_exit:
78✔
368
  if (rtner.fset) {
78!
369
    (void)taosThreadMutexLock(&pTsdb->mutex);
78✔
370
    tsdbFinishTaskOnFileSet(pTsdb, rtnArg->fid, EVA_TASK_RETENTION);
78✔
371
    (void)taosThreadMutexUnlock(&pTsdb->mutex);
78✔
372
  }
373

374
  // clear resources
375
  tsdbTFileSetClear(&rtner.fset);
78✔
376
  TARRAY2_DESTROY(&rtner.fopArr, NULL);
78!
377
  taosMemoryFree(arg);
78!
378
  if (code) {
78!
379
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(pTsdb->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
380
  }
381
  return code;
78✔
382
}
383

384
static int32_t tsdbAsyncRetentionImpl(STsdb *tsdb, int64_t now, bool s3Migrate) {
8✔
385
  int32_t code = 0;
8✔
386
  int32_t lino = 0;
8✔
387

388
  // check if background task is disabled
389
  if (tsdb->bgTaskDisabled) {
8!
UNCOV
390
    tsdbInfo("vgId:%d, background task is disabled, skip retention", TD_VID(tsdb->pVnode));
×
UNCOV
391
    return 0;
×
392
  }
393

394
  STFileSet *fset;
395
  TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
86✔
396
    SRtnArg *arg = taosMemoryMalloc(sizeof(*arg));
78!
397
    if (arg == NULL) {
78!
398
      TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
399
    }
400

401
    arg->tsdb = tsdb;
78✔
402
    arg->now = now;
78✔
403
    arg->fid = fset->fid;
78✔
404
    arg->s3Migrate = s3Migrate;
78✔
405

406
    code = vnodeAsync(RETENTION_TASK_ASYNC, EVA_PRIORITY_LOW, tsdbRetention, tsdbRetentionCancel, arg,
78✔
407
                      &fset->retentionTask);
408
    if (code) {
78!
UNCOV
409
      taosMemoryFree(arg);
×
UNCOV
410
      TSDB_CHECK_CODE(code, lino, _exit);
×
411
    }
412
  }
413

414
_exit:
8✔
415
  if (code) {
8!
UNCOV
416
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
417
  }
418
  return code;
8✔
419
}
420

421
int32_t tsdbAsyncRetention(STsdb *tsdb, int64_t now) {
8✔
422
  int32_t code = 0;
8✔
423
  (void)taosThreadMutexLock(&tsdb->mutex);
8✔
424
  code = tsdbAsyncRetentionImpl(tsdb, now, false);
8✔
425
  (void)taosThreadMutexUnlock(&tsdb->mutex);
8✔
426
  return code;
8✔
427
}
428

UNCOV
429
static int32_t tsdbS3FidLevel(int32_t fid, STsdbKeepCfg *pKeepCfg, int32_t s3KeepLocal, int64_t nowSec) {
×
430
  int32_t localFid;
431
  TSKEY   key;
432

433
  if (pKeepCfg->precision == TSDB_TIME_PRECISION_MILLI) {
×
UNCOV
434
    nowSec = nowSec * 1000;
×
435
  } else if (pKeepCfg->precision == TSDB_TIME_PRECISION_MICRO) {
×
436
    nowSec = nowSec * 1000000l;
×
UNCOV
437
  } else if (pKeepCfg->precision == TSDB_TIME_PRECISION_NANO) {
×
438
    nowSec = nowSec * 1000000000l;
×
439
  }
440

UNCOV
441
  nowSec = nowSec - pKeepCfg->keepTimeOffset * tsTickPerHour[pKeepCfg->precision];
×
442

443
  key = nowSec - s3KeepLocal * tsTickPerMin[pKeepCfg->precision];
×
444
  localFid = tsdbKeyFid(key, pKeepCfg->days, pKeepCfg->precision);
×
445

446
  if (fid >= localFid) {
×
447
    return 0;
×
448
  } else {
UNCOV
449
    return 1;
×
450
  }
451
}
452

453
static int32_t tsdbMigrateDataFileLCS3(SRTNer *rtner, const STFileObj *fobj, int64_t size, int64_t chunksize) {
×
UNCOV
454
  int32_t   code = 0;
×
UNCOV
455
  int32_t   lino = 0;
×
456
  STFileOp  op = {0};
×
UNCOV
457
  TdFilePtr fdFrom = NULL, fdTo = NULL;
×
UNCOV
458
  int32_t   lcn = fobj->f->lcn + (size - 1) / chunksize;
×
459

460
  // remove old
461
  op = (STFileOp){
×
462
      .optype = TSDB_FOP_REMOVE,
UNCOV
463
      .fid = fobj->f->fid,
×
464
      .of = fobj->f[0],
×
465
  };
466

467
  TAOS_CHECK_GOTO(TARRAY2_APPEND(&rtner->fopArr, op), &lino, _exit);
×
468

469
  // create new
470
  op = (STFileOp){
×
471
      .optype = TSDB_FOP_CREATE,
UNCOV
472
      .fid = fobj->f->fid,
×
473
      .nf =
474
          {
UNCOV
475
              .type = fobj->f->type,
×
UNCOV
476
              .did = fobj->f->did,
×
UNCOV
477
              .fid = fobj->f->fid,
×
UNCOV
478
              .minVer = fobj->f->minVer,
×
479
              .maxVer = fobj->f->maxVer,
×
UNCOV
480
              .cid = fobj->f->cid,
×
UNCOV
481
              .size = fobj->f->size,
×
482
              .lcn = lcn,
483
              .stt[0] =
484
                  {
485
                      .level = fobj->f->stt[0].level,
×
486
                  },
487
          },
488
  };
489

490
  TAOS_CHECK_GOTO(TARRAY2_APPEND(&rtner->fopArr, op), &lino, _exit);
×
491

492
  char fname[TSDB_FILENAME_LEN];
UNCOV
493
  tsdbTFileName(rtner->tsdb, &op.nf, fname);
×
494
  char   *object_name = taosDirEntryBaseName(fname);
×
495
  char    object_name_prefix[TSDB_FILENAME_LEN];
496
  int32_t node_id = vnodeNodeId(rtner->tsdb->pVnode);
×
497
  snprintf(object_name_prefix, TSDB_FQDN_LEN, "%d/%s", node_id, object_name);
×
498

499
  char *dot = strrchr(object_name_prefix, '.');
×
UNCOV
500
  if (!dot) {
×
UNCOV
501
    tsdbError("vgId:%d, incorrect lcn: %d, %s at line %d", TD_VID(rtner->tsdb->pVnode), lcn, __func__, lino);
×
502
    TAOS_CHECK_GOTO(TSDB_CODE_FAILED, &lino, _exit);
×
503
  }
504

UNCOV
505
  char *dot2 = strchr(object_name, '.');
×
506
  if (!dot) {
×
UNCOV
507
    tsdbError("vgId:%d, incorrect lcn: %d, %s at line %d", TD_VID(rtner->tsdb->pVnode), lcn, __func__, lino);
×
UNCOV
508
    TAOS_CHECK_GOTO(TSDB_CODE_FAILED, &lino, _exit);
×
509
  }
510
  snprintf(dot2 + 1, TSDB_FQDN_LEN - (dot2 + 1 - object_name), "%d.data", fobj->f->lcn);
×
511

512
  // do copy the file
513
  for (int32_t cn = fobj->f->lcn; cn < lcn; ++cn) {
×
UNCOV
514
    snprintf(dot + 1, TSDB_FQDN_LEN - (dot + 1 - object_name_prefix), "%d.data", cn);
×
515
    int64_t c_offset = chunksize * (cn - fobj->f->lcn);
×
516

517
    TAOS_CHECK_GOTO(tcsPutObjectFromFileOffset(fname, object_name_prefix, c_offset, chunksize), &lino, _exit);
×
518
  }
519

520
  // copy last chunk
UNCOV
521
  int64_t lc_offset = chunksize * (lcn - fobj->f->lcn);
×
522
  int64_t lc_size = size - lc_offset;
×
523

524
  snprintf(dot2 + 1, TSDB_FQDN_LEN - (dot2 + 1 - object_name), "%d.data", fobj->f->lcn);
×
525

UNCOV
526
  fdFrom = taosOpenFile(fname, TD_FILE_READ);
×
UNCOV
527
  if (fdFrom == NULL) {
×
528
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
529
  }
530

UNCOV
531
  tsdbInfo("vgId:%d, open lcfile: %s size: %" PRId64, TD_VID(rtner->tsdb->pVnode), fname, lc_size);
×
532

533
  snprintf(dot2 + 1, TSDB_FQDN_LEN - (dot2 + 1 - object_name), "%d.data", lcn);
×
534
  fdTo = taosOpenFile(fname, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
×
535
  if (fdTo == NULL) {
×
UNCOV
536
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
537
  }
538

539
  int64_t n = taosFSendFile(fdTo, fdFrom, &lc_offset, lc_size);
×
UNCOV
540
  if (n < 0) {
×
UNCOV
541
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
542
  }
543

UNCOV
544
_exit:
×
545
  if (code) {
×
UNCOV
546
    tsdbError("vgId:%d %s failed at line %s:%d since %s", TD_VID(rtner->tsdb->pVnode), __func__, __FILE__, lino,
×
547
              tstrerror(code));
548
  }
549
  if (taosCloseFile(&fdFrom) != 0) {
×
550
    tsdbTrace("vgId:%d, failed to close file", TD_VID(rtner->tsdb->pVnode));
×
551
  }
552

553
  if (taosCloseFile(&fdTo) != 0) {
×
UNCOV
554
    tsdbTrace("vgId:%d, failed to close file", TD_VID(rtner->tsdb->pVnode));
×
555
  }
556
  return code;
×
557
}
558

559
static int32_t tsdbMigrateDataFileS3(SRTNer *rtner, const STFileObj *fobj, int64_t size, int64_t chunksize) {
×
UNCOV
560
  int32_t   code = 0;
×
UNCOV
561
  int32_t   lino = 0;
×
562
  STFileOp  op = {0};
×
UNCOV
563
  int32_t   lcn = (size - 1) / chunksize + 1;
×
UNCOV
564
  TdFilePtr fdFrom = NULL, fdTo = NULL;
×
565

566
  // remove old
567
  op = (STFileOp){
×
568
      .optype = TSDB_FOP_REMOVE,
UNCOV
569
      .fid = fobj->f->fid,
×
570
      .of = fobj->f[0],
×
571
  };
572

573
  TAOS_CHECK_GOTO(TARRAY2_APPEND(&rtner->fopArr, op), &lino, _exit);
×
574

575
  // create new
576
  op = (STFileOp){
×
577
      .optype = TSDB_FOP_CREATE,
UNCOV
578
      .fid = fobj->f->fid,
×
579
      .nf =
580
          {
UNCOV
581
              .type = fobj->f->type,
×
UNCOV
582
              .did = fobj->f->did,
×
UNCOV
583
              .fid = fobj->f->fid,
×
UNCOV
584
              .minVer = fobj->f->minVer,
×
585
              .maxVer = fobj->f->maxVer,
×
UNCOV
586
              .cid = fobj->f->cid,
×
UNCOV
587
              .size = fobj->f->size,
×
588
              .lcn = lcn,
589
              .stt[0] =
590
                  {
591
                      .level = fobj->f->stt[0].level,
×
592
                  },
593
          },
594
  };
595

596
  TAOS_CHECK_GOTO(TARRAY2_APPEND(&rtner->fopArr, op), &lino, _exit);
×
597

598
  char fname[TSDB_FILENAME_LEN];
UNCOV
599
  tsdbTFileName(rtner->tsdb, &op.nf, fname);
×
UNCOV
600
  char   *object_name = taosDirEntryBaseName(fname);
×
601
  char    object_name_prefix[TSDB_FILENAME_LEN];
602
  int32_t node_id = vnodeNodeId(rtner->tsdb->pVnode);
×
603
  snprintf(object_name_prefix, TSDB_FQDN_LEN, "%d/%s", node_id, object_name);
×
604

605
  char *dot = strrchr(object_name_prefix, '.');
×
UNCOV
606
  if (!dot) {
×
UNCOV
607
    tsdbError("vgId:%d, incorrect lcn: %d, %s at line %d", TD_VID(rtner->tsdb->pVnode), lcn, __func__, lino);
×
UNCOV
608
    TAOS_CHECK_GOTO(TSDB_CODE_FAILED, &lino, _exit);
×
609
  }
610

611
  // do copy the file
612
  for (int32_t cn = 1; cn < lcn; ++cn) {
×
613
    snprintf(dot + 1, TSDB_FQDN_LEN - (dot + 1 - object_name_prefix), "%d.data", cn);
×
614
    int64_t c_offset = chunksize * (cn - 1);
×
615

UNCOV
616
    TAOS_CHECK_GOTO(tcsPutObjectFromFileOffset(fobj->fname, object_name_prefix, c_offset, chunksize), &lino, _exit);
×
617
  }
618

619
  // copy last chunk
620
  int64_t lc_offset = (int64_t)(lcn - 1) * chunksize;
×
621
  int64_t lc_size = size - lc_offset;
×
622

UNCOV
623
  dot = strchr(object_name, '.');
×
624
  if (!dot) {
×
UNCOV
625
    tsdbError("vgId:%d, incorrect lcn: %d, %s at line %d", TD_VID(rtner->tsdb->pVnode), lcn, __func__, lino);
×
626
    TAOS_CHECK_GOTO(TSDB_CODE_FAILED, &lino, _exit);
×
627
  }
628
  snprintf(dot + 1, TSDB_FQDN_LEN - (dot + 1 - object_name), "%d.data", lcn);
×
629

UNCOV
630
  fdFrom = taosOpenFile(fobj->fname, TD_FILE_READ);
×
631
  if (fdFrom == NULL) {
×
632
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
633
  }
634

UNCOV
635
  tsdbInfo("vgId: %d, open lcfile: %s size: %" PRId64, TD_VID(rtner->tsdb->pVnode), fname, fobj->f->size);
×
636

637
  fdTo = taosOpenFile(fname, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
×
638
  if (fdTo == NULL) {
×
UNCOV
639
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
640
  }
641

642
  int64_t n = taosFSendFile(fdTo, fdFrom, &lc_offset, lc_size);
×
UNCOV
643
  if (n < 0) {
×
644
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
645
  }
646

647
_exit:
×
UNCOV
648
  if (code) {
×
UNCOV
649
    tsdbError("vgId:%d %s failed at line %s:%d since %s", TD_VID(rtner->tsdb->pVnode), __func__, __FILE__, lino,
×
650
              tstrerror(code));
651
  }
652
  if (taosCloseFile(&fdFrom) != 0) {
×
UNCOV
653
    tsdbTrace("vgId:%d, failed to close file", TD_VID(rtner->tsdb->pVnode));
×
654
  }
655
  if (taosCloseFile(&fdTo) != 0) {
×
656
    tsdbTrace("vgId:%d, failed to close file", TD_VID(rtner->tsdb->pVnode));
×
657
  }
UNCOV
658
  return code;
×
659
}
660

661
static int32_t tsdbDoS3Migrate(SRTNer *rtner) {
×
662
  int32_t code = 0;
×
UNCOV
663
  int32_t lino = 0;
×
664

665
  STFileSet *fset = rtner->fset;
×
666
  STFileObj *fobj = fset->farr[TSDB_FTYPE_DATA];
×
667
  if (!fobj) {
×
668
    return 0;
×
669
  }
670

UNCOV
671
  int32_t expLevel = tsdbFidLevel(fset->fid, &rtner->tsdb->keepCfg, rtner->now);
×
672
  if (expLevel < 0) {  // expired
×
673
    return 0;
×
674
  }
675

676
  SVnodeCfg *pCfg = &rtner->tsdb->pVnode->config;
×
677
  int32_t    s3KeepLocal = pCfg->s3KeepLocal;
×
678
  int32_t    s3ExpLevel = tsdbS3FidLevel(fset->fid, &rtner->tsdb->keepCfg, s3KeepLocal, rtner->now);
×
679
  if (s3ExpLevel < 1) {  // keep on local storage
×
680
    return 0;
×
681
  }
682

683
  int64_t chunksize = (int64_t)pCfg->tsdbPageSize * pCfg->s3ChunkSize;
×
684
  int32_t lcn = fobj->f->lcn;
×
685

686
  if (/*lcn < 1 && */ taosCheckExistFile(fobj->fname)) {
×
687
    int64_t mtime = 0;
×
688
    int64_t size = 0;
×
689
    int32_t r = taosStatFile(fobj->fname, &size, &mtime, NULL);
×
UNCOV
690
    if (size > chunksize && mtime < rtner->now - tsS3UploadDelaySec) {
×
UNCOV
691
      if (pCfg->s3Compact && lcn < 0) {
×
692
        extern int32_t tsdbAsyncCompact(STsdb * tsdb, const STimeWindow *tw, bool sync);
693

UNCOV
694
        STimeWindow win = {0};
×
UNCOV
695
        tsdbFidKeyRange(fset->fid, rtner->tsdb->keepCfg.days, rtner->tsdb->keepCfg.precision, &win.skey, &win.ekey);
×
696

697
        tsdbInfo("vgId:%d, async compact begin lcn: %d.", TD_VID(rtner->tsdb->pVnode), lcn);
×
UNCOV
698
        code = tsdbAsyncCompact(rtner->tsdb, &win, pCfg->sttTrigger == 1);
×
UNCOV
699
        tsdbInfo("vgId:%d, async compact end lcn: %d.", TD_VID(rtner->tsdb->pVnode), lcn);
×
700
        goto _exit;
×
701
        return code;
702
      }
703

704
      TAOS_CHECK_GOTO(tsdbMigrateDataFileS3(rtner, fobj, size, chunksize), &lino, _exit);
×
705
    }
706
  } else {
UNCOV
707
    if (lcn <= 1) {
×
708
      TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, &lino, _exit);
×
709
    }
710
    char fname1[TSDB_FILENAME_LEN];
UNCOV
711
    tsdbTFileLastChunkName(rtner->tsdb, fobj->f, fname1);
×
712

713
    if (taosCheckExistFile(fname1)) {
×
UNCOV
714
      int64_t mtime = 0;
×
UNCOV
715
      int64_t size = 0;
×
UNCOV
716
      if (taosStatFile(fname1, &size, &mtime, NULL) != 0) {
×
717
        tsdbError("vgId:%d, %s failed at %s:%d ", TD_VID(rtner->tsdb->pVnode), __func__, __FILE__, __LINE__);
×
718
      }
719
      if (size > chunksize && mtime < rtner->now - tsS3UploadDelaySec) {
×
UNCOV
720
        TAOS_CHECK_GOTO(tsdbMigrateDataFileLCS3(rtner, fobj, size, chunksize), &lino, _exit);
×
721
      }
722
    } else {
UNCOV
723
      tsdbError("vgId:%d, file: %s not found, %s at line %d", TD_VID(rtner->tsdb->pVnode), fname1, __func__, lino);
×
UNCOV
724
      return code;
×
725
    }
726
  }
727

728
_exit:
×
729
  if (code) {
×
730
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(rtner->tsdb->pVnode), __func__, __FILE__, lino,
×
731
              tstrerror(code));
732
  }
733
  return code;
×
734
}
735

736
int32_t tsdbAsyncS3Migrate(STsdb *tsdb, int64_t now) {
×
737
  int32_t code = 0;
×
738

UNCOV
739
  int32_t expired = grantCheck(TSDB_GRANT_OBJECT_STORAGE);
×
740
  if (expired && tsS3Enabled) {
×
741
    tsdbWarn("s3 grant expired: %d", expired);
×
742
    tsS3Enabled = false;
×
UNCOV
743
  } else if (!expired && tsS3EnabledCfg) {
×
744
    tsS3Enabled = true;
×
745
  }
746

747
  if (!tsS3Enabled) {
×
UNCOV
748
    return 0;
×
749
  }
750

UNCOV
751
  (void)taosThreadMutexLock(&tsdb->mutex);
×
UNCOV
752
  code = tsdbAsyncRetentionImpl(tsdb, now, true);
×
UNCOV
753
  (void)taosThreadMutexUnlock(&tsdb->mutex);
×
754

UNCOV
755
  if (code) {
×
UNCOV
756
    tsdbError("vgId:%d, %s failed, reason:%s", TD_VID(tsdb->pVnode), __func__, tstrerror(code));
×
757
  }
UNCOV
758
  return code;
×
759
}
760

761
static int32_t tsdbGetS3SizeImpl(STsdb *tsdb, int64_t *size) {
22✔
762
  int32_t code = 0;
22✔
763

764
  SVnodeCfg *pCfg = &tsdb->pVnode->config;
22✔
765
  int64_t    chunksize = (int64_t)pCfg->tsdbPageSize * pCfg->s3ChunkSize;
22✔
766

767
  STFileSet *fset;
768
  TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
42✔
769
    STFileObj *fobj = fset->farr[TSDB_FTYPE_DATA];
20✔
770
    if (fobj) {
20!
UNCOV
771
      int32_t lcn = fobj->f->lcn;
×
UNCOV
772
      if (lcn > 1) {
×
UNCOV
773
        *size += ((lcn - 1) * chunksize);
×
774
      }
775
    }
776
  }
777

778
  return code;
22✔
779
}
780
int32_t tsdbGetS3Size(STsdb *tsdb, int64_t *size) {
22✔
781
  int32_t code = 0;
22✔
782
  (void)taosThreadMutexLock(&tsdb->mutex);
22✔
783
  code = tsdbGetS3SizeImpl(tsdb, size);
22✔
784
  (void)taosThreadMutexUnlock(&tsdb->mutex);
22✔
785
  return code;
22✔
786
}
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