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

taosdata / TDengine / #3562

20 Dec 2024 09:57AM UTC coverage: 26.655% (-32.2%) from 58.812%
#3562

push

travis-ci

web-flow
Merge pull request #29229 from taosdata/enh/TS-5749-3.0

enh: seperate tsdb async tasks to different thread pools

21498 of 109421 branches covered (19.65%)

Branch coverage included in aggregate %.

66 of 96 new or added lines in 7 files covered. (68.75%)

39441 existing lines in 157 files now uncovered.

35007 of 102566 relevant lines covered (34.13%)

53922.97 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

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

UNCOV
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

UNCOV
233
_exit:
×
UNCOV
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 {
UNCOV
238
    tsdbDebug("vid:%d, cid:%" PRId64 ", %s done", TD_VID(rtner->tsdb->pVnode), rtner->cid, __func__);
×
239
  }
UNCOV
240
  return code;
×
241
}
242

UNCOV
243
static int32_t tsdbDoRetention(SRTNer *rtner) {
×
UNCOV
244
  int32_t    code = 0;
×
UNCOV
245
  int32_t    lino = 0;
×
UNCOV
246
  STFileObj *fobj = NULL;
×
UNCOV
247
  STFileSet *fset = rtner->fset;
×
UNCOV
248
  int32_t    expLevel = tsdbFidLevel(fset->fid, &rtner->tsdb->keepCfg, rtner->now);
×
249

UNCOV
250
  if (expLevel < 0) {  // remove the fileset
×
251
    for (int32_t ftype = 0; (ftype < TSDB_FTYPE_MAX) && (fobj = fset->farr[ftype], 1); ++ftype) {
×
252
      if (fobj == NULL) continue;
×
253
      TAOS_CHECK_GOTO(tsdbDoRemoveFileObject(rtner, fobj), &lino, _exit);
×
254
    }
255

256
    SSttLvl *lvl;
257
    TARRAY2_FOREACH(fset->lvlArr, lvl) {
×
258
      TARRAY2_FOREACH(lvl->fobjArr, fobj) { TAOS_CHECK_GOTO(tsdbDoRemoveFileObject(rtner, fobj), &lino, _exit); }
×
259
    }
UNCOV
260
  } else if (expLevel == 0) {  // only migrate to upper level
×
UNCOV
261
    return 0;
×
262
  } else {  // migrate
263
    SDiskID did;
264

UNCOV
265
    TAOS_CHECK_GOTO(tfsAllocDisk(rtner->tsdb->pVnode->pTfs, expLevel, &did), &lino, _exit);
×
UNCOV
266
    code = tfsMkdirRecurAt(rtner->tsdb->pVnode->pTfs, rtner->tsdb->path, did);
×
UNCOV
267
    TSDB_CHECK_CODE(code, lino, _exit);
×
268

269
    // data
UNCOV
270
    for (int32_t ftype = 0; ftype < TSDB_FTYPE_MAX && (fobj = fset->farr[ftype], 1); ++ftype) {
×
UNCOV
271
      if (fobj == NULL) continue;
×
272

UNCOV
273
      if (fobj->f->did.level == did.level) {
×
UNCOV
274
        continue;
×
275
      }
276

277
      if (fobj->f->did.level > did.level) {
×
278
        continue;
×
279
      }
280
      tsdbInfo("file:%s size: %" PRId64 " do migrate from %d to %d", fobj->fname, fobj->f->size, fobj->f->did.level,
×
281
               did.level);
282

283
      TAOS_CHECK_GOTO(tsdbDoMigrateFileObj(rtner, fobj, &did), &lino, _exit);
×
284
    }
285

286
    // stt
287
    SSttLvl *lvl;
UNCOV
288
    TARRAY2_FOREACH(fset->lvlArr, lvl) {
×
UNCOV
289
      TARRAY2_FOREACH(lvl->fobjArr, fobj) {
×
UNCOV
290
        if (fobj->f->did.level == did.level) {
×
UNCOV
291
          continue;
×
292
        }
293

294
        TAOS_CHECK_GOTO(tsdbDoMigrateFileObj(rtner, fobj, &did), &lino, _exit);
×
295
      }
296
    }
297
  }
298

UNCOV
299
_exit:
×
UNCOV
300
  if (code) {
×
301
    tsdbError("vgId:%d, %s failed at %s:%d since %s", TD_VID(rtner->tsdb->pVnode), __func__, __FILE__, lino,
×
302
              tstrerror(code));
303
  }
UNCOV
304
  return code;
×
305
}
306

307
static void tsdbRetentionCancel(void *arg) { taosMemoryFree(arg); }
×
308

309
static int32_t tsdbDoS3Migrate(SRTNer *rtner);
310

UNCOV
311
static int32_t tsdbRetention(void *arg) {
×
UNCOV
312
  int32_t code = 0;
×
UNCOV
313
  int32_t lino = 0;
×
314

UNCOV
315
  SRtnArg   *rtnArg = (SRtnArg *)arg;
×
UNCOV
316
  STsdb     *pTsdb = rtnArg->tsdb;
×
UNCOV
317
  SVnode    *pVnode = pTsdb->pVnode;
×
UNCOV
318
  STFileSet *fset = NULL;
×
UNCOV
319
  SRTNer     rtner = {
×
320
          .tsdb = pTsdb,
UNCOV
321
          .szPage = pVnode->config.tsdbPageSize,
×
UNCOV
322
          .now = rtnArg->now,
×
UNCOV
323
          .cid = tsdbFSAllocEid(pTsdb->pFS),
×
324
  };
325

326
  // begin task
UNCOV
327
  (void)taosThreadMutexLock(&pTsdb->mutex);
×
328

329
  // check if background task is disabled
NEW
330
  if (pTsdb->bgTaskDisabled) {
×
NEW
331
    tsdbInfo("vgId:%d, background task is disabled, skip retention", TD_VID(pTsdb->pVnode));
×
NEW
332
    (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
NEW
333
    return 0;
×
334
  }
335

336
  // set flag and copy
NEW
337
  tsdbBeginTaskOnFileSet(pTsdb, rtnArg->fid, EVA_TASK_RETENTION, &fset);
×
338
  if (fset && (code = tsdbTFileSetInitCopy(pTsdb, fset, &rtner.fset))) {
×
339
    (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
340
    TSDB_CHECK_CODE(code, lino, _exit);
×
341
  }
342

UNCOV
343
  (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
344

345
  // do retention
UNCOV
346
  if (rtner.fset) {
×
UNCOV
347
    if (rtnArg->s3Migrate) {
×
348
      TAOS_CHECK_GOTO(tsdbDoS3Migrate(&rtner), &lino, _exit);
×
349
    } else {
UNCOV
350
      TAOS_CHECK_GOTO(tsdbDoRetention(&rtner), &lino, _exit);
×
351
    }
352

UNCOV
353
    TAOS_CHECK_GOTO(tsdbDoRetentionEnd(&rtner), &lino, _exit);
×
354
  }
355

UNCOV
356
_exit:
×
UNCOV
357
  if (rtner.fset) {
×
UNCOV
358
    (void)taosThreadMutexLock(&pTsdb->mutex);
×
NEW
359
    tsdbFinishTaskOnFileSet(pTsdb, rtnArg->fid, EVA_TASK_RETENTION);
×
UNCOV
360
    (void)taosThreadMutexUnlock(&pTsdb->mutex);
×
361
  }
362

363
  // clear resources
UNCOV
364
  tsdbTFileSetClear(&rtner.fset);
×
UNCOV
365
  TARRAY2_DESTROY(&rtner.fopArr, NULL);
×
UNCOV
366
  taosMemoryFree(arg);
×
UNCOV
367
  if (code) {
×
368
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(pTsdb->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
369
  }
UNCOV
370
  return code;
×
371
}
372

UNCOV
373
static int32_t tsdbAsyncRetentionImpl(STsdb *tsdb, int64_t now, bool s3Migrate) {
×
UNCOV
374
  int32_t code = 0;
×
UNCOV
375
  int32_t lino = 0;
×
376

377
  // check if background task is disabled
NEW
378
  if (tsdb->bgTaskDisabled) {
×
NEW
379
    tsdbInfo("vgId:%d, background task is disabled, skip retention", TD_VID(tsdb->pVnode));
×
NEW
380
    return 0;
×
381
  }
382

383
  STFileSet *fset;
NEW
384
  TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
×
NEW
385
    SRtnArg *arg = taosMemoryMalloc(sizeof(*arg));
×
NEW
386
    if (arg == NULL) {
×
NEW
387
      TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
388
    }
389

NEW
390
    arg->tsdb = tsdb;
×
NEW
391
    arg->now = now;
×
NEW
392
    arg->fid = fset->fid;
×
NEW
393
    arg->s3Migrate = s3Migrate;
×
394

NEW
395
    code = vnodeAsync(RETENTION_TASK_ASYNC, EVA_PRIORITY_LOW, tsdbRetention, tsdbRetentionCancel, arg,
×
396
                      &fset->retentionTask);
NEW
397
    if (code) {
×
NEW
398
      taosMemoryFree(arg);
×
NEW
399
      TSDB_CHECK_CODE(code, lino, _exit);
×
400
    }
401
  }
402

UNCOV
403
_exit:
×
UNCOV
404
  if (code) {
×
405
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, lino, tstrerror(code));
×
406
  }
UNCOV
407
  return code;
×
408
}
409

UNCOV
410
int32_t tsdbAsyncRetention(STsdb *tsdb, int64_t now) {
×
UNCOV
411
  int32_t code = 0;
×
UNCOV
412
  (void)taosThreadMutexLock(&tsdb->mutex);
×
UNCOV
413
  code = tsdbAsyncRetentionImpl(tsdb, now, false);
×
UNCOV
414
  (void)taosThreadMutexUnlock(&tsdb->mutex);
×
UNCOV
415
  return code;
×
416
}
417

418
static int32_t tsdbS3FidLevel(int32_t fid, STsdbKeepCfg *pKeepCfg, int32_t s3KeepLocal, int64_t nowSec) {
×
419
  int32_t localFid;
420
  TSKEY   key;
421

422
  if (pKeepCfg->precision == TSDB_TIME_PRECISION_MILLI) {
×
423
    nowSec = nowSec * 1000;
×
424
  } else if (pKeepCfg->precision == TSDB_TIME_PRECISION_MICRO) {
×
425
    nowSec = nowSec * 1000000l;
×
426
  } else if (pKeepCfg->precision == TSDB_TIME_PRECISION_NANO) {
×
427
    nowSec = nowSec * 1000000000l;
×
428
  }
429

430
  nowSec = nowSec - pKeepCfg->keepTimeOffset * tsTickPerHour[pKeepCfg->precision];
×
431

432
  key = nowSec - s3KeepLocal * tsTickPerMin[pKeepCfg->precision];
×
433
  localFid = tsdbKeyFid(key, pKeepCfg->days, pKeepCfg->precision);
×
434

435
  if (fid >= localFid) {
×
436
    return 0;
×
437
  } else {
438
    return 1;
×
439
  }
440
}
441

442
static int32_t tsdbMigrateDataFileLCS3(SRTNer *rtner, const STFileObj *fobj, int64_t size, int64_t chunksize) {
×
443
  int32_t   code = 0;
×
444
  int32_t   lino = 0;
×
445
  STFileOp  op = {0};
×
446
  TdFilePtr fdFrom = NULL, fdTo = NULL;
×
447
  int32_t   lcn = fobj->f->lcn + (size - 1) / chunksize;
×
448

449
  // remove old
450
  op = (STFileOp){
×
451
      .optype = TSDB_FOP_REMOVE,
452
      .fid = fobj->f->fid,
×
453
      .of = fobj->f[0],
×
454
  };
455

456
  TAOS_CHECK_GOTO(TARRAY2_APPEND(&rtner->fopArr, op), &lino, _exit);
×
457

458
  // create new
459
  op = (STFileOp){
×
460
      .optype = TSDB_FOP_CREATE,
461
      .fid = fobj->f->fid,
×
462
      .nf =
463
          {
464
              .type = fobj->f->type,
×
465
              .did = fobj->f->did,
×
466
              .fid = fobj->f->fid,
×
467
              .minVer = fobj->f->minVer,
×
468
              .maxVer = fobj->f->maxVer,
×
469
              .cid = fobj->f->cid,
×
470
              .size = fobj->f->size,
×
471
              .lcn = lcn,
472
              .stt[0] =
473
                  {
474
                      .level = fobj->f->stt[0].level,
×
475
                  },
476
          },
477
  };
478

479
  TAOS_CHECK_GOTO(TARRAY2_APPEND(&rtner->fopArr, op), &lino, _exit);
×
480

481
  char fname[TSDB_FILENAME_LEN];
482
  tsdbTFileName(rtner->tsdb, &op.nf, fname);
×
483
  char   *object_name = taosDirEntryBaseName(fname);
×
484
  char    object_name_prefix[TSDB_FILENAME_LEN];
485
  int32_t node_id = vnodeNodeId(rtner->tsdb->pVnode);
×
486
  snprintf(object_name_prefix, TSDB_FQDN_LEN, "%d/%s", node_id, object_name);
×
487

488
  char *dot = strrchr(object_name_prefix, '.');
×
489
  if (!dot) {
×
490
    tsdbError("vgId:%d, incorrect lcn: %d, %s at line %d", TD_VID(rtner->tsdb->pVnode), lcn, __func__, lino);
×
491
    TAOS_CHECK_GOTO(TSDB_CODE_FAILED, &lino, _exit);
×
492
  }
493

494
  char *dot2 = strchr(object_name, '.');
×
495
  if (!dot) {
×
496
    tsdbError("vgId:%d, incorrect lcn: %d, %s at line %d", TD_VID(rtner->tsdb->pVnode), lcn, __func__, lino);
×
497
    TAOS_CHECK_GOTO(TSDB_CODE_FAILED, &lino, _exit);
×
498
  }
499
  snprintf(dot2 + 1, TSDB_FQDN_LEN - (dot2 + 1 - object_name), "%d.data", fobj->f->lcn);
×
500

501
  // do copy the file
502
  for (int32_t cn = fobj->f->lcn; cn < lcn; ++cn) {
×
503
    snprintf(dot + 1, TSDB_FQDN_LEN - (dot + 1 - object_name_prefix), "%d.data", cn);
×
504
    int64_t c_offset = chunksize * (cn - fobj->f->lcn);
×
505

506
    TAOS_CHECK_GOTO(tcsPutObjectFromFileOffset(fname, object_name_prefix, c_offset, chunksize), &lino, _exit);
×
507
  }
508

509
  // copy last chunk
510
  int64_t lc_offset = chunksize * (lcn - fobj->f->lcn);
×
511
  int64_t lc_size = size - lc_offset;
×
512

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

515
  fdFrom = taosOpenFile(fname, TD_FILE_READ);
×
516
  if (fdFrom == NULL) {
×
517
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
518
  }
519

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

522
  snprintf(dot2 + 1, TSDB_FQDN_LEN - (dot2 + 1 - object_name), "%d.data", lcn);
×
523
  fdTo = taosOpenFile(fname, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
×
524
  if (fdTo == NULL) {
×
525
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
526
  }
527

528
  int64_t n = taosFSendFile(fdTo, fdFrom, &lc_offset, lc_size);
×
529
  if (n < 0) {
×
530
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
531
  }
532

533
_exit:
×
534
  if (code) {
×
535
    tsdbError("vgId:%d %s failed at line %s:%d since %s", TD_VID(rtner->tsdb->pVnode), __func__, __FILE__, lino,
×
536
              tstrerror(code));
537
  }
538
  if (taosCloseFile(&fdFrom) != 0) {
×
539
    tsdbTrace("vgId:%d, failed to close file", TD_VID(rtner->tsdb->pVnode));
×
540
  }
541

542
  if (taosCloseFile(&fdTo) != 0) {
×
543
    tsdbTrace("vgId:%d, failed to close file", TD_VID(rtner->tsdb->pVnode));
×
544
  }
545
  return code;
×
546
}
547

548
static int32_t tsdbMigrateDataFileS3(SRTNer *rtner, const STFileObj *fobj, int64_t size, int64_t chunksize) {
×
549
  int32_t   code = 0;
×
550
  int32_t   lino = 0;
×
551
  STFileOp  op = {0};
×
552
  int32_t   lcn = (size - 1) / chunksize + 1;
×
553
  TdFilePtr fdFrom = NULL, fdTo = NULL;
×
554

555
  // remove old
556
  op = (STFileOp){
×
557
      .optype = TSDB_FOP_REMOVE,
558
      .fid = fobj->f->fid,
×
559
      .of = fobj->f[0],
×
560
  };
561

562
  TAOS_CHECK_GOTO(TARRAY2_APPEND(&rtner->fopArr, op), &lino, _exit);
×
563

564
  // create new
565
  op = (STFileOp){
×
566
      .optype = TSDB_FOP_CREATE,
567
      .fid = fobj->f->fid,
×
568
      .nf =
569
          {
570
              .type = fobj->f->type,
×
571
              .did = fobj->f->did,
×
572
              .fid = fobj->f->fid,
×
573
              .minVer = fobj->f->minVer,
×
574
              .maxVer = fobj->f->maxVer,
×
575
              .cid = fobj->f->cid,
×
576
              .size = fobj->f->size,
×
577
              .lcn = lcn,
578
              .stt[0] =
579
                  {
580
                      .level = fobj->f->stt[0].level,
×
581
                  },
582
          },
583
  };
584

585
  TAOS_CHECK_GOTO(TARRAY2_APPEND(&rtner->fopArr, op), &lino, _exit);
×
586

587
  char fname[TSDB_FILENAME_LEN];
588
  tsdbTFileName(rtner->tsdb, &op.nf, fname);
×
589
  char   *object_name = taosDirEntryBaseName(fname);
×
590
  char    object_name_prefix[TSDB_FILENAME_LEN];
591
  int32_t node_id = vnodeNodeId(rtner->tsdb->pVnode);
×
592
  snprintf(object_name_prefix, TSDB_FQDN_LEN, "%d/%s", node_id, object_name);
×
593

594
  char *dot = strrchr(object_name_prefix, '.');
×
595
  if (!dot) {
×
596
    tsdbError("vgId:%d, incorrect lcn: %d, %s at line %d", TD_VID(rtner->tsdb->pVnode), lcn, __func__, lino);
×
597
    TAOS_CHECK_GOTO(TSDB_CODE_FAILED, &lino, _exit);
×
598
  }
599

600
  // do copy the file
601
  for (int32_t cn = 1; cn < lcn; ++cn) {
×
602
    snprintf(dot + 1, TSDB_FQDN_LEN - (dot + 1 - object_name_prefix), "%d.data", cn);
×
603
    int64_t c_offset = chunksize * (cn - 1);
×
604

605
    TAOS_CHECK_GOTO(tcsPutObjectFromFileOffset(fobj->fname, object_name_prefix, c_offset, chunksize), &lino, _exit);
×
606
  }
607

608
  // copy last chunk
609
  int64_t lc_offset = (int64_t)(lcn - 1) * chunksize;
×
610
  int64_t lc_size = size - lc_offset;
×
611

612
  dot = strchr(object_name, '.');
×
613
  if (!dot) {
×
614
    tsdbError("vgId:%d, incorrect lcn: %d, %s at line %d", TD_VID(rtner->tsdb->pVnode), lcn, __func__, lino);
×
615
    TAOS_CHECK_GOTO(TSDB_CODE_FAILED, &lino, _exit);
×
616
  }
617
  snprintf(dot + 1, TSDB_FQDN_LEN - (dot + 1 - object_name), "%d.data", lcn);
×
618

619
  fdFrom = taosOpenFile(fobj->fname, TD_FILE_READ);
×
620
  if (fdFrom == NULL) {
×
621
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
622
  }
623

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

626
  fdTo = taosOpenFile(fname, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
×
627
  if (fdTo == NULL) {
×
628
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
629
  }
630

631
  int64_t n = taosFSendFile(fdTo, fdFrom, &lc_offset, lc_size);
×
632
  if (n < 0) {
×
633
    TAOS_CHECK_GOTO(terrno, &lino, _exit);
×
634
  }
635

636
_exit:
×
637
  if (code) {
×
638
    tsdbError("vgId:%d %s failed at line %s:%d since %s", TD_VID(rtner->tsdb->pVnode), __func__, __FILE__, lino,
×
639
              tstrerror(code));
640
  }
641
  if (taosCloseFile(&fdFrom) != 0) {
×
642
    tsdbTrace("vgId:%d, failed to close file", TD_VID(rtner->tsdb->pVnode));
×
643
  }
644
  if (taosCloseFile(&fdTo) != 0) {
×
645
    tsdbTrace("vgId:%d, failed to close file", TD_VID(rtner->tsdb->pVnode));
×
646
  }
647
  return code;
×
648
}
649

650
static int32_t tsdbDoS3Migrate(SRTNer *rtner) {
×
651
  int32_t code = 0;
×
652
  int32_t lino = 0;
×
653

654
  STFileSet *fset = rtner->fset;
×
655
  STFileObj *fobj = fset->farr[TSDB_FTYPE_DATA];
×
656
  if (!fobj) {
×
657
    return 0;
×
658
  }
659

660
  int32_t expLevel = tsdbFidLevel(fset->fid, &rtner->tsdb->keepCfg, rtner->now);
×
661
  if (expLevel < 0) {  // expired
×
662
    return 0;
×
663
  }
664

665
  SVnodeCfg *pCfg = &rtner->tsdb->pVnode->config;
×
666
  int32_t    s3KeepLocal = pCfg->s3KeepLocal;
×
667
  int32_t    s3ExpLevel = tsdbS3FidLevel(fset->fid, &rtner->tsdb->keepCfg, s3KeepLocal, rtner->now);
×
668
  if (s3ExpLevel < 1) {  // keep on local storage
×
669
    return 0;
×
670
  }
671

672
  int64_t chunksize = (int64_t)pCfg->tsdbPageSize * pCfg->s3ChunkSize;
×
673
  int32_t lcn = fobj->f->lcn;
×
674

675
  if (/*lcn < 1 && */ taosCheckExistFile(fobj->fname)) {
×
676
    int64_t mtime = 0;
×
677
    int64_t size = 0;
×
678
    int32_t r = taosStatFile(fobj->fname, &size, &mtime, NULL);
×
679
    if (size > chunksize && mtime < rtner->now - tsS3UploadDelaySec) {
×
680
      if (pCfg->s3Compact && lcn < 0) {
×
681
        extern int32_t tsdbAsyncCompact(STsdb * tsdb, const STimeWindow *tw, bool sync);
682

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

686
        tsdbInfo("vgId:%d, async compact begin lcn: %d.", TD_VID(rtner->tsdb->pVnode), lcn);
×
687
        code = tsdbAsyncCompact(rtner->tsdb, &win, pCfg->sttTrigger == 1);
×
688
        tsdbInfo("vgId:%d, async compact end lcn: %d.", TD_VID(rtner->tsdb->pVnode), lcn);
×
689
        goto _exit;
×
690
        return code;
691
      }
692

693
      TAOS_CHECK_GOTO(tsdbMigrateDataFileS3(rtner, fobj, size, chunksize), &lino, _exit);
×
694
    }
695
  } else {
696
    if (lcn <= 1) {
×
697
      TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, &lino, _exit);
×
698
    }
699
    char fname1[TSDB_FILENAME_LEN];
700
    tsdbTFileLastChunkName(rtner->tsdb, fobj->f, fname1);
×
701

702
    if (taosCheckExistFile(fname1)) {
×
703
      int64_t mtime = 0;
×
704
      int64_t size = 0;
×
705
      if (taosStatFile(fname1, &size, &mtime, NULL) != 0) {
×
706
        tsdbError("vgId:%d, %s failed at %s:%d ", TD_VID(rtner->tsdb->pVnode), __func__, __FILE__, __LINE__);
×
707
      }
708
      if (size > chunksize && mtime < rtner->now - tsS3UploadDelaySec) {
×
709
        TAOS_CHECK_GOTO(tsdbMigrateDataFileLCS3(rtner, fobj, size, chunksize), &lino, _exit);
×
710
      }
711
    } else {
712
      tsdbError("vgId:%d, file: %s not found, %s at line %d", TD_VID(rtner->tsdb->pVnode), fname1, __func__, lino);
×
713
      return code;
×
714
    }
715
  }
716

717
_exit:
×
718
  if (code) {
×
719
    tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(rtner->tsdb->pVnode), __func__, __FILE__, lino,
×
720
              tstrerror(code));
721
  }
722
  return code;
×
723
}
724

725
int32_t tsdbAsyncS3Migrate(STsdb *tsdb, int64_t now) {
×
726
  int32_t code = 0;
×
727

728
  int32_t expired = grantCheck(TSDB_GRANT_OBJECT_STORAGE);
×
729
  if (expired && tsS3Enabled) {
×
730
    tsdbWarn("s3 grant expired: %d", expired);
×
731
    tsS3Enabled = false;
×
732
  } else if (!expired && tsS3EnabledCfg) {
×
733
    tsS3Enabled = true;
×
734
  }
735

736
  if (!tsS3Enabled) {
×
737
    return 0;
×
738
  }
739

740
  (void)taosThreadMutexLock(&tsdb->mutex);
×
741
  code = tsdbAsyncRetentionImpl(tsdb, now, true);
×
742
  (void)taosThreadMutexUnlock(&tsdb->mutex);
×
743

744
  if (code) {
×
745
    tsdbError("vgId:%d, %s failed, reason:%s", TD_VID(tsdb->pVnode), __func__, tstrerror(code));
×
746
  }
747
  return code;
×
748
}
749

UNCOV
750
static int32_t tsdbGetS3SizeImpl(STsdb *tsdb, int64_t *size) {
×
UNCOV
751
  int32_t code = 0;
×
752

UNCOV
753
  SVnodeCfg *pCfg = &tsdb->pVnode->config;
×
UNCOV
754
  int64_t    chunksize = (int64_t)pCfg->tsdbPageSize * pCfg->s3ChunkSize;
×
755

756
  STFileSet *fset;
UNCOV
757
  TARRAY2_FOREACH(tsdb->pFS->fSetArr, fset) {
×
UNCOV
758
    STFileObj *fobj = fset->farr[TSDB_FTYPE_DATA];
×
UNCOV
759
    if (fobj) {
×
760
      int32_t lcn = fobj->f->lcn;
×
761
      if (lcn > 1) {
×
762
        *size += ((lcn - 1) * chunksize);
×
763
      }
764
    }
765
  }
766

UNCOV
767
  return code;
×
768
}
UNCOV
769
int32_t tsdbGetS3Size(STsdb *tsdb, int64_t *size) {
×
UNCOV
770
  int32_t code = 0;
×
UNCOV
771
  (void)taosThreadMutexLock(&tsdb->mutex);
×
UNCOV
772
  code = tsdbGetS3SizeImpl(tsdb, size);
×
UNCOV
773
  (void)taosThreadMutexUnlock(&tsdb->mutex);
×
UNCOV
774
  return code;
×
775
}
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