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

taosdata / TDengine / #3676

22 Mar 2025 04:46PM UTC coverage: 25.147% (-36.8%) from 61.952%
#3676

push

travis-ci

web-flow
fix: userOperTest in linux (#30363)

Co-authored-by: taos-support <it@taosdata.com>

55963 of 304767 branches covered (18.36%)

Branch coverage included in aggregate %.

96374 of 301020 relevant lines covered (32.02%)

582640.8 hits per line

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

0.0
/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) {
×
42
  int64_t interval = 1000;  // 1s
×
43
  int64_t limit = limitMB ? limitMB * 1024 * 1024 : INT64_MAX;
×
44
  int64_t offset = 0;
×
45
  int64_t remain = size;
×
46

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

54
    remain -= n;
×
55

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

64
  return 0;
×
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) {
×
112
  int32_t code = 0;
×
113
  int32_t lino = 0;
×
114

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

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

121
  fdFrom = taosOpenFile(from->fname, TD_FILE_READ);
×
122
  if (fdFrom == NULL) {
×
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);
×
127

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

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

138
_exit:
×
139
  if (code) {
×
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) {
×
144
    tsdbTrace("vgId:%d, failed to close file", TD_VID(rtner->tsdb->pVnode));
×
145
  }
146
  if (taosCloseFile(&fdTo) != 0) {
×
147
    tsdbTrace("vgId:%d, failed to close file", TD_VID(rtner->tsdb->pVnode));
×
148
  }
149
  return code;
×
150
}
151

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

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

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

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

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

190
  // do copy the file
191

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

198
_exit:
×
199
  if (code) {
×
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;
×
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) {
×
214
  int32_t code = 0;
×
215
  int32_t lino = 0;
×
216

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

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

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

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

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

233
_exit:
×
234
  if (code) {
×
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__);
×
239
  }
240
  return code;
×
241
}
242

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

247
  if (fobj == NULL) {
×
248
    return code;
×
249
  }
250

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

260
      code = tsdbAllocateDiskAtLevel(rtner->tsdb, expLevel, tsdbFTypeLabel(fobj->f->type), &diskId);
×
261
      if (code) {
×
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));
264
        code = 0;
×
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),
×
268
                 fobj->fname, fobj->f->did.level, diskId.level, fobj->f->size);
269

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

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

280
_exit:
×
281
  if (code) {
×
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;
×
286
}
287

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

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

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

310
_exit:
×
311
  if (code) {
×
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;
×
316
}
317

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

320
static int32_t tsdbDoS3Migrate(SRTNer *rtner);
321

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

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

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

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

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

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

356
  // do retention
357
  if (rtner.fset) {
×
358
    if (rtnArg->s3Migrate) {
×
359
#ifdef USE_S3
360
      TAOS_CHECK_GOTO(tsdbDoS3Migrate(&rtner), &lino, _exit);
×
361
#endif
362
    } else {
363
      TAOS_CHECK_GOTO(tsdbDoRetention(&rtner), &lino, _exit);
×
364
    }
365

366
    TAOS_CHECK_GOTO(tsdbDoRetentionEnd(&rtner), &lino, _exit);
×
367
  }
368

369
_exit:
×
370
  if (rtner.fset) {
×
371
    (void)taosThreadMutexLock(&pTsdb->mutex);
×
372
    tsdbFinishTaskOnFileSet(pTsdb, rtnArg->fid, EVA_TASK_RETENTION);
×
373
    (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
374
  }
375

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

386
static int32_t tsdbAsyncRetentionImpl(STsdb *tsdb, int64_t now, bool s3Migrate) {
×
387
  int32_t code = 0;
×
388
  int32_t lino = 0;
×
389

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

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

403
    arg->tsdb = tsdb;
×
404
    arg->now = now;
×
405
    arg->fid = fset->fid;
×
406
    arg->s3Migrate = s3Migrate;
×
407

408
    code = vnodeAsync(RETENTION_TASK_ASYNC, EVA_PRIORITY_LOW, tsdbRetention, tsdbRetentionCancel, arg,
×
409
                      &fset->retentionTask);
410
    if (code) {
×
411
      taosMemoryFree(arg);
×
412
      TSDB_CHECK_CODE(code, lino, _exit);
×
413
    }
414
  }
415

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

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

431
#ifdef USE_S3
432
static int32_t tsdbS3FidLevel(int32_t fid, STsdbKeepCfg *pKeepCfg, int32_t s3KeepLocal, int64_t nowSec) {
×
433
  int32_t localFid;
434
  TSKEY   key;
435

436
  if (pKeepCfg->precision == TSDB_TIME_PRECISION_MILLI) {
×
437
    nowSec = nowSec * 1000;
×
438
  } else if (pKeepCfg->precision == TSDB_TIME_PRECISION_MICRO) {
×
439
    nowSec = nowSec * 1000000l;
×
440
  } else if (pKeepCfg->precision == TSDB_TIME_PRECISION_NANO) {
×
441
    nowSec = nowSec * 1000000000l;
×
442
  }
443

444
  nowSec = nowSec - pKeepCfg->keepTimeOffset * tsTickPerHour[pKeepCfg->precision];
×
445

446
  key = nowSec - s3KeepLocal * tsTickPerMin[pKeepCfg->precision];
×
447
  localFid = tsdbKeyFid(key, pKeepCfg->days, pKeepCfg->precision);
×
448

449
  if (fid >= localFid) {
×
450
    return 0;
×
451
  } else {
452
    return 1;
×
453
  }
454
}
455

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

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

470
  TAOS_CHECK_GOTO(TARRAY2_APPEND(&rtner->fopArr, op), &lino, _exit);
×
471

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

493
  TAOS_CHECK_GOTO(TARRAY2_APPEND(&rtner->fopArr, op), &lino, _exit);
×
494

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

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

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

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

520
    TAOS_CHECK_GOTO(tcsPutObjectFromFileOffset(fname, object_name_prefix, c_offset, chunksize), &lino, _exit);
×
521
  }
522

523
  // copy last chunk
524
  int64_t lc_offset = chunksize * (lcn - fobj->f->lcn);
×
525
  int64_t lc_size = size - lc_offset;
×
526

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

529
  fdFrom = taosOpenFile(fname, TD_FILE_READ);
×
530
  if (fdFrom == NULL) {
×
531
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
532
  }
533

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

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

542
  int64_t n = taosFSendFile(fdTo, fdFrom, &lc_offset, lc_size);
×
543
  if (n < 0) {
×
544
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
545
  }
546

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

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

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

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

576
  TAOS_CHECK_GOTO(TARRAY2_APPEND(&rtner->fopArr, op), &lino, _exit);
×
577

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

599
  TAOS_CHECK_GOTO(TARRAY2_APPEND(&rtner->fopArr, op), &lino, _exit);
×
600

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

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

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

619
    TAOS_CHECK_GOTO(tcsPutObjectFromFileOffset(fobj->fname, object_name_prefix, c_offset, chunksize), &lino, _exit);
×
620
  }
621

622
  // copy last chunk
623
  int64_t lc_offset = (int64_t)(lcn - 1) * chunksize;
×
624
  int64_t lc_size = size - lc_offset;
×
625

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

633
  fdFrom = taosOpenFile(fobj->fname, TD_FILE_READ);
×
634
  if (fdFrom == NULL) {
×
635
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
636
  }
637

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

640
  fdTo = taosOpenFile(fname, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
×
641
  if (fdTo == NULL) {
×
642
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
643
  }
644

645
  int64_t n = taosFSendFile(fdTo, fdFrom, &lc_offset, lc_size);
×
646
  if (n < 0) {
×
647
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
648
  }
649

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

664
static int32_t tsdbDoS3Migrate(SRTNer *rtner) {
×
665
  int32_t code = 0;
×
666
  int32_t lino = 0;
×
667

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

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

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

686
  int64_t chunksize = (int64_t)pCfg->tsdbPageSize * pCfg->s3ChunkSize;
×
687
  int32_t lcn = fobj->f->lcn;
×
688

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

697
        STimeWindow win = {0};
×
698
        tsdbFidKeyRange(fset->fid, rtner->tsdb->keepCfg.days, rtner->tsdb->keepCfg.precision, &win.skey, &win.ekey);
×
699

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

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

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

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

739
int32_t tsdbAsyncS3Migrate(STsdb *tsdb, int64_t now) {
×
740
  int32_t code = 0;
×
741

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

750
  if (!tsS3Enabled) {
×
751
    return 0;
×
752
  }
753

754
  (void)taosThreadMutexLock(&tsdb->mutex);
×
755
  code = tsdbAsyncRetentionImpl(tsdb, now, true);
×
756
  (void)taosThreadMutexUnlock(&tsdb->mutex);
×
757

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

764
#endif
765

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