tsdbRetention2.c 9.8 KB
Newer Older
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"

18
enum { RETENTION_NO = 0, RETENTION_EXPIRED = 1, RETENTION_MIGRATE = 2 };
19

20
#if 1
21 22 23
#define MIGRATE_MIN_FSIZE (1048576 << 9)  // 512 MB
#define MIGRATE_MAX_SPEED (1048576 << 5)  // 32 MB
#define MIGRATE_MIN_COST  (5)             // second
24 25 26 27 28
#else
#define MIGRATE_MIN_FSIZE (1048576 << 5)  // 32 MB
#define MIGRATE_MAX_SPEED (1048576 << 2)  // 4  MB
#define MIGRATE_MIN_COST  (5)             // second
#endif
29 30 31 32

static bool    tsdbShouldDoMigrate(STsdb *pTsdb);
static int32_t tsdbShouldDoRetention(STsdb *pTsdb, int64_t now);
static int32_t tsdbProcessExpire(STsdb *pTsdb, int64_t now, int32_t retention);
33
static int32_t tsdbProcessMigrate(STsdb *pTsdb, int64_t now, int64_t maxSpeed, int32_t retention);
34

35
static bool tsdbShouldDoMigrate(STsdb *pTsdb) {
36 37 38 39 40 41 42 43
  if (tfsGetLevel(pTsdb->pVnode->pTfs) < 2) {
    return false;
  }

  STsdbKeepCfg *keepCfg = &pTsdb->keepCfg;
  if (keepCfg->keep0 == keepCfg->keep1 && keepCfg->keep1 == keepCfg->keep2) {
    return false;
  }
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  return true;
}

static int32_t tsdbShouldDoRetention(STsdb *pTsdb, int64_t now) {
  int32_t retention = RETENTION_NO;
  if (taosArrayGetSize(pTsdb->fs.aDFileSet) == 0) {
    return retention;
  }

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

  if (!tsdbShouldDoMigrate(pTsdb)) {
    return retention;
  }
61

62
  for (int32_t iSet = 0; iSet < taosArrayGetSize(pTsdb->fs.aDFileSet); ++iSet) {
63
    pSet = (SDFileSet *)taosArrayGet(pTsdb->fs.aDFileSet, iSet);
64
    int32_t expLevel = tsdbFidLevel(pSet->fid, &pTsdb->keepCfg, now);
65 66 67

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

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    if (expLevel > 0) {
      retention |= RETENTION_MIGRATE;
      break;
    }
  }

  return retention;
}

static int32_t tsdbProcessExpire(STsdb *pTsdb, int64_t now, int32_t retention) {
  int32_t code = 0;
  int32_t nLoops = 0;
  int32_t maxFid = INT32_MIN;
  STsdbFS fs = {0};
  STsdbFS fsLatest = {0};

  if (!(retention & RETENTION_EXPIRED)) {
    goto _exit;
  }

  code = tsdbFSCopy(pTsdb, &fs);
  if (code) goto _exit;

  int32_t fsSize = taosArrayGetSize(fs.aDFileSet);
  for (int32_t iSet = 0; iSet < fsSize; iSet++) {
    SDFileSet *pSet = (SDFileSet *)taosArrayGet(fs.aDFileSet, iSet);
    int32_t    expLevel = tsdbFidLevel(pSet->fid, &pTsdb->keepCfg, now);
    SDiskID    did;

97
    if (expLevel < 0) {
98 99
      SET_DFSET_EXPIRED(pSet);
      if (pSet->fid > maxFid) maxFid = pSet->fid;
100
    } else {
101 102 103
      break;
    }
  }
104

105
  if (maxFid == INT32_MIN) goto _exit;
106

107 108 109 110 111
_wait_commit_end:
  while (atomic_load_32(&pTsdb->trimHdl.minCommitFid) <= maxFid) {
    if (++nLoops > 1000) {
      nLoops = 0;
      sched_yield();
C
Cary Xu 已提交
112
      // printf("%s:%d wait commit finished\n", __func__, __LINE__);
113 114 115 116 117 118
    }
  }
  if (atomic_val_compare_exchange_8(&pTsdb->trimHdl.state, 0, 1) == 0) {
    if (atomic_load_32(&pTsdb->trimHdl.minCommitFid) <= maxFid) {
      atomic_store_8(&pTsdb->trimHdl.state, 0);
      goto _wait_commit_end;
119
    }
120 121 122 123
    atomic_store_32(&pTsdb->trimHdl.maxRetentFid, maxFid);
    atomic_store_8(&pTsdb->trimHdl.state, 0);
  } else {
    goto _wait_commit_end;
124 125
  }

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
_merge_fs:
  taosThreadRwlockWrlock(&pTsdb->rwLock);
  if ((code = tsdbFSCopy(pTsdb, &fsLatest))) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
    goto _exit;
  }
  // 1) merge tsdbFSNew and pTsdb->fs
  if ((code = tsdbFSUpdDel(pTsdb, &fsLatest, &fs, maxFid))) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
    goto _exit;
  }
  // 2) save CURRENT
  if ((code = tsdbFSCommit1(pTsdb, &fsLatest))) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
    goto _exit;
  }
  // 3) apply the tsdbFS to pTsdb->fs
  if ((code = tsdbFSCommit2(pTsdb, &fsLatest))) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
    goto _exit;
  }
  taosThreadRwlockUnlock(&pTsdb->rwLock);

_exit:
  tsdbFSDestroy(&fs);
  tsdbFSDestroy(&fsLatest);
  if (code != 0) {
    tsdbError("vgId:%d, tsdb do retention(expire) failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
    ASSERT(0);
  }
  return code;
157 158 159
}

/**
160 161
 * @brief
 *
162 163
 * @param pTsdb
 * @param now
164
 * @param retention
165 166
 * @return int32_t
 */
167
static int32_t tsdbProcessMigrate(STsdb *pTsdb, int64_t now, int64_t maxSpeed, int32_t retention) {
168
  int32_t code = 0;
169
  int32_t nBatch = 0;
170 171 172 173 174
  int32_t nLoops = 0;
  int32_t maxFid = INT32_MIN;
  int64_t fSize = 0;
  STsdbFS fs = {0};
  STsdbFS fsLatest = {0};
175

176 177
  if (!(retention & RETENTION_MIGRATE)) {
    goto _exit;
178 179
  }

180 181 182 183
_migrate_loop:
  // reset
  maxFid = INT32_MIN;
  fSize = 0;
184 185
  tsdbFSDestroy(&fs);
  tsdbFSDestroy(&fsLatest);
186

187 188 189 190 191
  if (atomic_load_8(&pTsdb->trimHdl.commitInWait) == 1) {
    atomic_store_32(&pTsdb->trimHdl.maxRetentFid, INT32_MIN);
    taosMsleep(10);
  }

192
  code = tsdbFSCopy(pTsdb, &fs);
193 194 195 196 197 198 199 200
  if (code) goto _exit;

  int32_t fsSize = taosArrayGetSize(fs.aDFileSet);
  for (int32_t iSet = 0; iSet < fsSize; ++iSet) {
    SDFileSet *pSet = (SDFileSet *)taosArrayGet(fs.aDFileSet, iSet);
    int32_t    expLevel = tsdbFidLevel(pSet->fid, &pTsdb->keepCfg, now);
    SDiskID    did;

201 202
    if (pSet->diskId.level == expLevel) continue;

203 204 205 206 207 208 209 210 211 212 213
    if (expLevel > 0) {
      ASSERT(pSet->fid > maxFid);
      maxFid = pSet->fid;
      fSize += (pSet->pDataF->size + pSet->pHeadF->size + pSet->pSmaF->size);
      if (fSize / MIGRATE_MAX_SPEED > MIGRATE_MIN_COST) {
        break;
      }
      for (int32_t iStt = 0; iStt < pSet->nSttF; ++iStt) {
        fSize += pSet->aSttF[iStt]->size;
      }
      if (fSize / MIGRATE_MAX_SPEED > MIGRATE_MIN_COST) {
214
        tsdbInfo("vgId:%d migrate loop[%d] with maxFid:%d", TD_VID(pTsdb->pVnode), nBatch, maxFid);
215 216 217 218
        break;
      }
    }
  }
219

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
  if (maxFid == INT32_MIN) goto _exit;

_wait_commit_end:
  while (atomic_load_32(&pTsdb->trimHdl.minCommitFid) <= maxFid) {
    if (++nLoops > 1000) {
      nLoops = 0;
      sched_yield();
    }
  }
  if (atomic_val_compare_exchange_8(&pTsdb->trimHdl.state, 0, 1) == 0) {
    if (atomic_load_32(&pTsdb->trimHdl.minCommitFid) <= maxFid) {
      atomic_store_8(&pTsdb->trimHdl.state, 0);
      goto _wait_commit_end;
    }
    atomic_store_32(&pTsdb->trimHdl.maxRetentFid, maxFid);
    atomic_store_8(&pTsdb->trimHdl.state, 0);
  } else {
    goto _wait_commit_end;
  }

  // migrate
  for (int32_t iSet = 0; iSet < fsSize; ++iSet) {
242 243 244 245
    SDFileSet *pSet = (SDFileSet *)taosArrayGet(fs.aDFileSet, iSet);
    int32_t    expLevel = tsdbFidLevel(pSet->fid, &pTsdb->keepCfg, now);
    SDiskID    did;

246 247
    if (pSet->fid > maxFid) break;

248 249 250
    tsdbInfo("vgId:%d migrate loop[%d] with maxFid:%d, fid:%d, did:%d, level:%d, expLevel:%d", TD_VID(pTsdb->pVnode),
             nBatch, maxFid, pSet->fid, pSet->diskId.id, pSet->diskId.level, expLevel);

251
    if (expLevel < 0) {
252
      SET_DFSET_EXPIRED(pSet);
253 254 255
    } else {
      if (expLevel == pSet->diskId.level) continue;

256 257 258 259 260 261 262
      if (tfsAllocDisk(pTsdb->pVnode->pTfs, expLevel, &did) < 0) {
        code = terrno;
        goto _exit;
      }

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

263
      // copy file to new disk
264 265 266
      SDFileSet fSet = *pSet;
      fSet.diskId = did;

267
      code = tsdbDFileSetCopy(pTsdb, pSet, &fSet, maxSpeed);
268
      if (code) goto _exit;
269 270

      code = tsdbFSUpsertFSet(&fs, &fSet);
271
      if (code) goto _exit;
272 273 274
    }
  }

275
_merge_fs:
276 277
  taosThreadRwlockWrlock(&pTsdb->rwLock);

278
  if ((code = tsdbFSCopy(pTsdb, &fsLatest))) {
279
    taosThreadRwlockUnlock(&pTsdb->rwLock);
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    goto _exit;
  }
  // 1) merge tsdbFSNew and pTsdb->fs
  if ((code = tsdbFSUpdDel(pTsdb, &fsLatest, &fs, maxFid))) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
    goto _exit;
  }
  // 2) save CURRENT
  if ((code = tsdbFSCommit1(pTsdb, &fsLatest))) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
    goto _exit;
  }
  // 3) apply the tsdbFS to pTsdb->fs
  if ((code = tsdbFSCommit2(pTsdb, &fsLatest))) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
    goto _exit;
296 297 298
  }
  taosThreadRwlockUnlock(&pTsdb->rwLock);

299 300 301
  ++nBatch;
  goto _migrate_loop;

302
_exit:
303 304
  tsdbFSDestroy(&fs);
  tsdbFSDestroy(&fsLatest);
305 306 307 308
  if (code != 0) {
    tsdbError("vgId:%d, tsdb do retention(migrate) failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
    ASSERT(0);
  }
309
  return code;
310
}
311

312 313 314 315 316 317 318 319 320 321 322 323
/**
 * @brief Data migration between multi-tier storage, including remove expired data.
 *  1) firstly, remove expired DFileSet;
 *  2) partition the tsdbFS by the expLevel and the estimated cost(e.g. 5s) to copy, and migrate
 * DFileSet groups between multi-tier storage;
 *  3) update the tsdbFS and CURRENT in the same transaction;
 *  4) finish the migration
 * @param pTsdb
 * @param now
 * @param maxSpeed
 * @return int32_t
 */
324
int32_t tsdbDoRetention(STsdb *pTsdb, int64_t now, int64_t maxSpeed) {
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
  int32_t code = 0;
  int32_t retention = RETENTION_NO;

  retention = tsdbShouldDoRetention(pTsdb, now);
  if (retention == RETENTION_NO) {
    goto _exit;
  }

  // step 1: process expire
  code = tsdbProcessExpire(pTsdb, now, retention);
  if (code < 0) goto _exit;

  // step 2: process multi-tier migration
  code = tsdbProcessMigrate(pTsdb, now, maxSpeed, retention);
  if (code < 0) goto _exit;

_exit:
  pTsdb->trimHdl.maxRetentFid = INT32_MIN;
  if (code != 0) {
344
    tsdbError("vgId:%d, tsdb do retention:%d failed since %s", TD_VID(pTsdb->pVnode), retention, tstrerror(code));
345 346
    ASSERT(0);
    // tsdbFSRollback(pTsdb->pFS);
347 348
  } else {
    tsdbInfo("vgId:%d, tsdb do retention:%d succeed", TD_VID(pTsdb->pVnode), retention);
349
  }
350 351
  return code;
}