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

#include "tsdb.h"

H
Hongze Cheng 已提交
18
static bool tsdbShouldDoRetention(STsdb *pTsdb, int64_t now) {
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  if (taosArrayGetSize(pTsdb->fs.aDFileSet) == 0) {
    return false;
  }

  SDFileSet *pSet = (SDFileSet *)taosArrayGet(pTsdb->fs.aDFileSet, 0);
  if (tsdbFidLevel(pSet->fid, &pTsdb->keepCfg, now) < 0) {
    return true;
  }

  if (tfsGetLevel(pTsdb->pVnode->pTfs) < 2) {
    return false;
  }

  STsdbKeepCfg *keepCfg = &pTsdb->keepCfg;
  if (keepCfg->keep0 == keepCfg->keep1 && keepCfg->keep1 == keepCfg->keep2) {
    return false;
  }

H
Hongze Cheng 已提交
37
  for (int32_t iSet = 0; iSet < taosArrayGetSize(pTsdb->fs.aDFileSet); iSet++) {
38 39 40
    pSet = (SDFileSet *)taosArrayGet(pTsdb->fs.aDFileSet, iSet);
    int32_t expLevel = tsdbFidLevel(pSet->fid, keepCfg, now);
    SDiskID did;
H
Hongze Cheng 已提交
41

H
Hongze Cheng 已提交
42
    if (expLevel == pSet->diskId.level) continue;
H
Hongze Cheng 已提交
43 44

    if (expLevel < 0) {
H
Hongze Cheng 已提交
45
      return true;
H
Hongze Cheng 已提交
46 47
    } else {
      if (tfsAllocDisk(pTsdb->pVnode->pTfs, expLevel, &did) < 0) {
H
Hongze Cheng 已提交
48
        return false;
H
Hongze Cheng 已提交
49 50
      }

H
Hongze Cheng 已提交
51
      if (did.level == pSet->diskId.level) continue;
H
Hongze Cheng 已提交
52

H
Hongze Cheng 已提交
53
      return true;
H
Hongze Cheng 已提交
54
    }
H
Hongze Cheng 已提交
55 56
  }

H
Hongze Cheng 已提交
57
  return false;
H
Hongze Cheng 已提交
58 59 60 61 62
}

int32_t tsdbDoRetention(STsdb *pTsdb, int64_t now) {
  int32_t code = 0;

H
Hongze Cheng 已提交
63 64 65
  if (!tsdbShouldDoRetention(pTsdb, now)) {
    return code;
  }
H
Hongze Cheng 已提交
66 67

  // do retention
H
Hongze Cheng 已提交
68 69 70
  STsdbFS fs;

  code = tsdbFSCopy(pTsdb, &fs);
H
Hongze Cheng 已提交
71 72
  if (code) goto _err;

H
Hongze Cheng 已提交
73
  for (int32_t iSet = 0; iSet < taosArrayGetSize(fs.aDFileSet); iSet++) {
74
    SDFileSet *pSet = (SDFileSet *)taosArrayGet(fs.aDFileSet, iSet);
H
Hongze Cheng 已提交
75 76 77 78 79 80
    int32_t    expLevel = tsdbFidLevel(pSet->fid, &pTsdb->keepCfg, now);
    SDiskID    did;

    if (expLevel < 0) {
      taosMemoryFree(pSet->pHeadF);
      taosMemoryFree(pSet->pDataF);
H
Hongze Cheng 已提交
81
      taosMemoryFree(pSet->aSttF[0]);
H
Hongze Cheng 已提交
82 83 84 85
      taosMemoryFree(pSet->pSmaF);
      taosArrayRemove(fs.aDFileSet, iSet);
      iSet--;
    } else {
86
      if (expLevel == 0) continue;
H
Hongze Cheng 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
      if (tfsAllocDisk(pTsdb->pVnode->pTfs, expLevel, &did) < 0) {
        code = terrno;
        goto _exit;
      }

      if (did.level == pSet->diskId.level) continue;

      // copy file to new disk (todo)
      SDFileSet fSet = *pSet;
      fSet.diskId = did;

      code = tsdbDFileSetCopy(pTsdb, pSet, &fSet);
      if (code) goto _err;

      code = tsdbFSUpsertFSet(&fs, &fSet);
      if (code) goto _err;
    }
  }

  // do change fs
  code = tsdbFSCommit1(pTsdb, &fs);
H
Hongze Cheng 已提交
108 109
  if (code) goto _err;

H
Hongze Cheng 已提交
110 111 112 113 114 115 116 117 118 119 120 121
  taosThreadRwlockWrlock(&pTsdb->rwLock);

  code = tsdbFSCommit2(pTsdb, &fs);
  if (code) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
    goto _err;
  }

  taosThreadRwlockUnlock(&pTsdb->rwLock);

  tsdbFSDestroy(&fs);

H
Hongze Cheng 已提交
122 123 124 125
_exit:
  return code;

_err:
S
Shengliang Guan 已提交
126
  tsdbError("vgId:%d, tsdb do retention failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
127 128
  ASSERT(0);
  // tsdbFSRollback(pTsdb->pFS);
H
Hongze Cheng 已提交
129 130
  return code;
}