smaOpen.c 6.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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 "sma.h"
#include "tsdb.h"

19 20
static int32_t smaEvalDays(SVnode *pVnode, SRetention *r, int8_t level, int8_t precision, int32_t duration);
static int32_t smaSetKeepCfg(SVnode *pVnode, STsdbKeepCfg *pKeepCfg, STsdbCfg *pCfg, int type);
C
Cary Xu 已提交
21
static int32_t rsmaRestore(SSma *pSma);
22

23
#define SMA_SET_KEEP_CFG(v, l)                                                                    \
24 25 26 27 28
  do {                                                                                            \
    SRetention *r = &pCfg->retentions[l];                                                         \
    pKeepCfg->keep2 = convertTimeFromPrecisionToUnit(r->keep, pCfg->precision, TIME_UNIT_MINUTE); \
    pKeepCfg->keep0 = pKeepCfg->keep2;                                                            \
    pKeepCfg->keep1 = pKeepCfg->keep2;                                                            \
29
    pKeepCfg->days = smaEvalDays(v, pCfg->retentions, l, pCfg->precision, pCfg->days);            \
30 31 32 33 34 35 36 37 38 39 40
  } while (0)

#define SMA_OPEN_RSMA_IMPL(v, l)                                                   \
  do {                                                                             \
    SRetention *r = (SRetention *)VND_RETENTIONS(v) + l;                           \
    if (!RETENTION_VALID(r)) {                                                     \
      if (l == 0) {                                                                \
        goto _err;                                                                 \
      }                                                                            \
      break;                                                                       \
    }                                                                              \
41
    smaSetKeepCfg(v, &keepCfg, pCfg, TSDB_TYPE_RSMA_L##l);                         \
42 43 44 45 46
    if (tsdbOpen(v, &SMA_RSMA_TSDB##l(pSma), VNODE_RSMA##l##_DIR, &keepCfg) < 0) { \
      goto _err;                                                                   \
    }                                                                              \
  } while (0)

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
/**
 * @brief Evaluate days(duration) for rsma level 1/2/3.
 *  1) level 1: duration from "create database"
 *  2) level 2/3: duration * (freq/freqL1)
 * @param pVnode
 * @param r
 * @param level
 * @param precision
 * @param duration
 * @return int32_t
 */
static int32_t smaEvalDays(SVnode *pVnode, SRetention *r, int8_t level, int8_t precision, int32_t duration) {
  int32_t freqDuration = convertTimeFromPrecisionToUnit((r + TSDB_RETENTION_L0)->freq, precision, TIME_UNIT_MINUTE);
  int32_t keepDuration = convertTimeFromPrecisionToUnit((r + TSDB_RETENTION_L0)->keep, precision, TIME_UNIT_MINUTE);
  int32_t days = duration;  // min
62

63 64 65
  if (days < freqDuration) {
    days = freqDuration;
  }
66

67 68 69 70 71 72
  if (days > keepDuration) {
    days = keepDuration;
  }

  if (level == TSDB_RETENTION_L0) {
    goto end;
73
  }
74 75 76 77 78 79 80 81 82 83 84 85 86

  ASSERT(level >= TSDB_RETENTION_L1 && level <= TSDB_RETENTION_L2);
  
  freqDuration = convertTimeFromPrecisionToUnit((r + level)->freq, precision, TIME_UNIT_MINUTE);
  keepDuration = convertTimeFromPrecisionToUnit((r + level)->keep, precision, TIME_UNIT_MINUTE);

  int32_t nFreqTimes = (r + level)->freq / (r + TSDB_RETENTION_L0)->freq;
  days *= (nFreqTimes > 1 ? nFreqTimes : 1);

  if (days > keepDuration) {
    days = keepDuration;
  }

C
Cary Xu 已提交
87 88
  if (days > TSDB_MAX_DURATION_PER_FILE) {
    days = TSDB_MAX_DURATION_PER_FILE;
89 90 91 92 93 94 95 96
  }

  if (days < freqDuration) {
    days = freqDuration;
  }
end:
  smaInfo("vgId:%d, evaluated duration for level %" PRIi8 " is %d, raw val:%d", TD_VID(pVnode), level + 1, days, duration);
  return days;
97 98
}

99
int smaSetKeepCfg(SVnode *pVnode, STsdbKeepCfg *pKeepCfg, STsdbCfg *pCfg, int type) {
100 101 102 103 104 105
  pKeepCfg->precision = pCfg->precision;
  switch (type) {
    case TSDB_TYPE_TSMA:
      ASSERT(0);
      break;
    case TSDB_TYPE_RSMA_L0:
106
      SMA_SET_KEEP_CFG(pVnode, 0);
107 108
      break;
    case TSDB_TYPE_RSMA_L1:
109
      SMA_SET_KEEP_CFG(pVnode, 1);
110 111
      break;
    case TSDB_TYPE_RSMA_L2:
112
      SMA_SET_KEEP_CFG(pVnode, 2);
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
      break;
    default:
      ASSERT(0);
      break;
  }
  return 0;
}

int32_t smaOpen(SVnode *pVnode) {
  STsdbCfg *pCfg = &pVnode->config.tsdbCfg;

  ASSERT(!pVnode->pSma);

  SSma *pSma = taosMemoryCalloc(1, sizeof(SSma));
  if (!pSma) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
C
Cary Xu 已提交
131 132 133

  pVnode->pSma = pSma;

134 135 136 137
  pSma->pVnode = pVnode;
  taosThreadMutexInit(&pSma->mutex, NULL);
  pSma->locked = false;

C
Cary Xu 已提交
138
  if (VND_IS_RSMA(pVnode)) {
139 140 141 142 143 144 145 146 147 148 149 150 151
    STsdbKeepCfg keepCfg = {0};
    for (int i = 0; i < TSDB_RETENTION_MAX; ++i) {
      if (i == TSDB_RETENTION_L0) {
        SMA_OPEN_RSMA_IMPL(pVnode, 0);
      } else if (i == TSDB_RETENTION_L1) {
        SMA_OPEN_RSMA_IMPL(pVnode, 1);
      } else if (i == TSDB_RETENTION_L2) {
        SMA_OPEN_RSMA_IMPL(pVnode, 2);
      } else {
        ASSERT(0);
      }
    }

C
Cary Xu 已提交
152
    // restore the rsma
153
    if (tdRsmaRestore(pSma, RSMA_RESTORE_REBOOT, pVnode->state.committed) < 0) {
C
Cary Xu 已提交
154 155
      goto _err;
    }
C
Cary Xu 已提交
156 157
  }

158 159 160 161 162
  return 0;
_err:
  return -1;
}

C
Cary Xu 已提交
163
int32_t smaClose(SSma *pSma) {
C
Cary Xu 已提交
164
  if (pSma) {
C
Cary Xu 已提交
165
    taosThreadMutexDestroy(&pSma->mutex);
C
Cary Xu 已提交
166 167
    SMA_TSMA_ENV(pSma) = tdFreeSmaEnv(SMA_TSMA_ENV(pSma));
    SMA_RSMA_ENV(pSma) = tdFreeSmaEnv(SMA_RSMA_ENV(pSma));
168 169 170
    if SMA_RSMA_TSDB0 (pSma) tsdbClose(&SMA_RSMA_TSDB0(pSma));
    if SMA_RSMA_TSDB1 (pSma) tsdbClose(&SMA_RSMA_TSDB1(pSma));
    if SMA_RSMA_TSDB2 (pSma) tsdbClose(&SMA_RSMA_TSDB2(pSma));
C
Cary Xu 已提交
171
    taosMemoryFreeClear(pSma);
172 173
  }
  return 0;
C
Cary Xu 已提交
174 175 176 177
}

/**
 * @brief rsma env restore
178 179 180 181 182
 *
 * @param pSma
 * @param type
 * @param committedVer
 * @return int32_t
C
Cary Xu 已提交
183
 */
184
int32_t tdRsmaRestore(SSma *pSma, int8_t type, int64_t committedVer) {
C
Cary Xu 已提交
185 186
  ASSERT(VND_IS_RSMA(pSma->pVnode));

187
  return tdProcessRSmaRestoreImpl(pSma, type, committedVer);
188
}