smaCommit.c 6.9 KB
Newer Older
C
Cary Xu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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"

static int32_t tdProcessRSmaPreCommitImpl(SSma *pSma);
static int32_t tdProcessRSmaCommitImpl(SSma *pSma);
static int32_t tdProcessRSmaPostCommitImpl(SSma *pSma);
C
Cary Xu 已提交
21
static int32_t tdProcessRSmaAsyncCommitImpl(SSma *pSma);
C
Cary Xu 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

/**
 * @brief Only applicable to Rollup SMA
 *
 * @param pSma
 * @return int32_t
 */
int32_t smaPreCommit(SSma *pSma) { return tdProcessRSmaPreCommitImpl(pSma); }

/**
 * @brief Only applicable to Rollup SMA
 *
 * @param pSma
 * @return int32_t
 */
int32_t smaCommit(SSma *pSma) { return tdProcessRSmaCommitImpl(pSma); }

/**
 * @brief Only applicable to Rollup SMA
 *
 * @param pSma
 * @return int32_t
 */
int32_t smaPostCommit(SSma *pSma) { return tdProcessRSmaPostCommitImpl(pSma); }

C
Cary Xu 已提交
47 48 49 50 51 52 53 54
/**
 * @brief Only applicable to Rollup SMA
 *
 * @param pSma
 * @return int32_t
 */
int32_t smaAsyncCommit(SSma *pSma) { return tdProcessRSmaAsyncCommitImpl(pSma); }

C
Cary Xu 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/**
 * @brief set rsma trigger stat active
 *
 * @param pSma
 * @return int32_t
 */
int32_t smaBegin(SSma *pSma) {
  SSmaEnv *pSmaEnv = SMA_RSMA_ENV(pSma);
  if (!pSmaEnv) {
    return TSDB_CODE_SUCCESS;
  }

  SSmaStat  *pStat = SMA_ENV_STAT(pSmaEnv);
  SRSmaStat *pRSmaStat = SMA_RSMA_STAT(pStat);

  int8_t rsmaTriggerStat =
      atomic_val_compare_exchange_8(RSMA_TRIGGER_STAT(pRSmaStat), TASK_TRIGGER_STAT_PAUSED, TASK_TRIGGER_STAT_ACTIVE);
  switch (rsmaTriggerStat) {
    case TASK_TRIGGER_STAT_PAUSED: {
      smaDebug("vgId:%d rsma trigger stat from paused to active", SMA_VID(pSma));
      break;
    }
    case TASK_TRIGGER_STAT_INIT: {
      atomic_store_8(RSMA_TRIGGER_STAT(pRSmaStat), TASK_TRIGGER_STAT_ACTIVE);
      smaDebug("vgId:%d rsma trigger stat from init to active", SMA_VID(pSma));
      break;
    }
    default: {
      atomic_store_8(RSMA_TRIGGER_STAT(pRSmaStat), TASK_TRIGGER_STAT_ACTIVE);
      smaWarn("vgId:%d rsma trigger stat %" PRIi8 " is unexpected", SMA_VID(pSma), rsmaTriggerStat);
      ASSERT(0);
      break;
    }
  }
  return TSDB_CODE_SUCCESS;
}

C
Cary Xu 已提交
92 93 94
/**
 * @brief pre-commit for rollup sma.
 *  1) set trigger stat of rsma timer TASK_TRIGGER_STAT_PAUSED.
95 96
 *  2) wait all triggered fetch tasks finished
 *  3) perform persist task for qTaskInfo
C
Cary Xu 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109
 *
 * @param pSma
 * @return int32_t
 */
static int32_t tdProcessRSmaPreCommitImpl(SSma *pSma) {
  SSmaEnv *pSmaEnv = SMA_RSMA_ENV(pSma);
  if (!pSmaEnv) {
    return TSDB_CODE_SUCCESS;
  }

  SSmaStat  *pStat = SMA_ENV_STAT(pSmaEnv);
  SRSmaStat *pRSmaStat = SMA_RSMA_STAT(pStat);

C
Cary Xu 已提交
110 111

  // step 1: set persistence task paused
C
Cary Xu 已提交
112 113
  atomic_store_8(RSMA_TRIGGER_STAT(pRSmaStat), TASK_TRIGGER_STAT_PAUSED);

114
  // step 2: wait all triggered fetch tasks finished
C
Cary Xu 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128
  int32_t nLoops = 0;
  while (1) {
    if (T_REF_VAL_GET(pStat) == 0) {
      smaDebug("vgId:%d, rsma fetch tasks all finished", SMA_VID(pSma));
      break;
    } else {
      smaDebug("vgId:%d, rsma fetch tasks not all finished yet", SMA_VID(pSma));
    }
    ++nLoops;
    if (nLoops > 1000) {
      sched_yield();
      nLoops = 0;
    }
  }
C
Cary Xu 已提交
129

130 131 132
  // step 3: perform persist task for qTaskInfo
  tdRSmaPersistExecImpl(pRSmaStat);

C
Cary Xu 已提交
133
  smaDebug("vgId:%d, rsma pre commit success", SMA_VID(pSma));
C
Cary Xu 已提交
134

C
Cary Xu 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  return TSDB_CODE_SUCCESS;
}

/**
 * @brief commit for rollup sma
 *
 * @param pSma
 * @return int32_t
 */
static int32_t tdProcessRSmaCommitImpl(SSma *pSma) {
  SSmaEnv *pSmaEnv = SMA_RSMA_ENV(pSma);
  if (!pSmaEnv) {
    return TSDB_CODE_SUCCESS;
  }
  return TSDB_CODE_SUCCESS;
}

/**
 * @brief post-commit for rollup sma
 *  1) clean up the outdated qtaskinfo files
 *
 * @param pSma
 * @return int32_t
 */
static int32_t tdProcessRSmaPostCommitImpl(SSma *pSma) {
  SVnode *pVnode = pSma->pVnode;

  if (!VND_IS_RSMA(pVnode)) {
    return TSDB_CODE_SUCCESS;
  }

  int64_t       committed = pVnode->state.committed;
  TdDirPtr      pDir = NULL;
  TdDirEntryPtr pDirEntry = NULL;
  char          dir[TSDB_FILENAME_LEN];
C
Cary Xu 已提交
170
  const char   *pattern = "v[0-9]+qtaskinfo\\.ver([0-9]+)?$";
C
Cary Xu 已提交
171
  regex_t       regex;
C
Cary Xu 已提交
172
  int           code = 0;
C
Cary Xu 已提交
173

C
Cary Xu 已提交
174
  tdGetVndDirName(TD_VID(pVnode), tfsGetPrimaryPath(pVnode->pTfs), VNODE_RSMA_DIR, true, dir);
C
Cary Xu 已提交
175 176

  // Resource allocation and init
C
Cary Xu 已提交
177 178 179 180 181 182
  if ((code = regcomp(&regex, pattern, REG_EXTENDED)) != 0) {
    char errbuf[128];
    regerror(code, &regex, errbuf, sizeof(errbuf));
    smaWarn("vgId:%d, rsma post commit, regcomp for %s failed since %s", TD_VID(pVnode), dir, errbuf);
    return TSDB_CODE_FAILED;
  }
C
Cary Xu 已提交
183 184

  if ((pDir = taosOpenDir(dir)) == NULL) {
C
Cary Xu 已提交
185
    regfree(&regex);
C
Cary Xu 已提交
186
    terrno = TAOS_SYSTEM_ERROR(errno);
C
Cary Xu 已提交
187
    smaWarn("vgId:%d, rsma post commit, open dir %s failed since %s", TD_VID(pVnode), dir, terrstr());
C
Cary Xu 已提交
188 189 190
    return TSDB_CODE_FAILED;
  }

C
Cary Xu 已提交
191 192
  int32_t    dirLen = strlen(dir);
  char      *dirEnd = POINTER_SHIFT(dir, dirLen);
C
Cary Xu 已提交
193 194 195 196 197 198
  regmatch_t regMatch[2];
  while ((pDirEntry = taosReadDir(pDir)) != NULL) {
    char *entryName = taosGetDirEntryName(pDirEntry);
    if (!entryName) {
      continue;
    }
C
Cary Xu 已提交
199 200

    code = regexec(&regex, entryName, 2, regMatch, 0);
C
Cary Xu 已提交
201 202 203

    if (code == 0) {
      // match
C
Cary Xu 已提交
204
      int64_t version = -1;
C
Cary Xu 已提交
205
      sscanf((const char *)POINTER_SHIFT(entryName, regMatch[1].rm_so), "%" PRIi64, &version);
C
Cary Xu 已提交
206
      if ((version < committed) && (version > -1)) {
C
Cary Xu 已提交
207 208
        strncpy(dirEnd, entryName, TSDB_FILENAME_LEN - dirLen);
        if (taosRemoveFile(dir) != 0) {
C
Cary Xu 已提交
209 210
          terrno = TAOS_SYSTEM_ERROR(errno);
          smaWarn("vgId:%d, committed version:%" PRIi64 ", failed to remove %s since %s", TD_VID(pVnode), committed,
C
Cary Xu 已提交
211
                  dir, terrstr());
C
Cary Xu 已提交
212
        } else {
C
Cary Xu 已提交
213
          smaDebug("vgId:%d, committed version:%" PRIi64 ", success to remove %s", TD_VID(pVnode), committed, dir);
C
Cary Xu 已提交
214 215
        }
      }
C
Cary Xu 已提交
216 217
    } else if (code == REG_NOMATCH) {
      // not match
C
Cary Xu 已提交
218
      smaTrace("vgId:%d, rsma post commit, not match %s", TD_VID(pVnode), entryName);
C
Cary Xu 已提交
219 220 221
      continue;
    } else {
      // has other error
C
Cary Xu 已提交
222 223 224
      char errbuf[128];
      regerror(code, &regex, errbuf, sizeof(errbuf));
      smaWarn("vgId:%d, rsma post commit, regexec failed since %s", TD_VID(pVnode), errbuf);
C
Cary Xu 已提交
225 226 227 228 229 230

      taosCloseDir(&pDir);
      regfree(&regex);
      return TSDB_CODE_FAILED;
    }
  }
C
Cary Xu 已提交
231

C
Cary Xu 已提交
232
  taosCloseDir(&pDir);
C
Cary Xu 已提交
233
  regfree(&regex);
C
Cary Xu 已提交
234 235
  return TSDB_CODE_SUCCESS;
}
C
Cary Xu 已提交
236 237 238 239 240 241 242 243 244 245 246 247

/**
 * @brief Rsma async commit implementation
 *  1) Wait all running fetch task finish to fetch and put submitMsg into level 2/3 wQueue(blocking level 1 write)
 *
 * @param pSma
 * @return int32_t
 */
static int32_t tdProcessRSmaAsyncCommitImpl(SSma *pSma) { 
  // TODO
  return TSDB_CODE_SUCCESS;
}