smaCommit.c 14.2 KB
Newer Older
C
Cary Xu 已提交
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 "sma.h"

C
Cary Xu 已提交
18 19 20
static int32_t tdProcessRSmaSyncPreCommitImpl(SSma *pSma);
static int32_t tdProcessRSmaSyncCommitImpl(SSma *pSma);
static int32_t tdProcessRSmaSyncPostCommitImpl(SSma *pSma);
C
Cary Xu 已提交
21
static int32_t tdProcessRSmaAsyncPreCommitImpl(SSma *pSma);
C
Cary Xu 已提交
22
static int32_t tdProcessRSmaAsyncCommitImpl(SSma *pSma);
C
Cary Xu 已提交
23
static int32_t tdProcessRSmaAsyncPostCommitImpl(SSma *pSma);
C
Cary Xu 已提交
24
static int32_t tdCleanupQTaskInfoFiles(SSma *pSma, SRSmaStat *pRSmaStat);
C
Cary Xu 已提交
25 26 27 28 29 30 31

/**
 * @brief Only applicable to Rollup SMA
 *
 * @param pSma
 * @return int32_t
 */
C
Cary Xu 已提交
32
int32_t smaSyncPreCommit(SSma *pSma) { return tdProcessRSmaSyncPreCommitImpl(pSma); }
C
Cary Xu 已提交
33 34 35 36 37 38 39

/**
 * @brief Only applicable to Rollup SMA
 *
 * @param pSma
 * @return int32_t
 */
C
Cary Xu 已提交
40
int32_t smaSyncCommit(SSma *pSma) { return tdProcessRSmaSyncCommitImpl(pSma); }
C
Cary Xu 已提交
41 42 43 44 45 46 47

/**
 * @brief Only applicable to Rollup SMA
 *
 * @param pSma
 * @return int32_t
 */
C
Cary Xu 已提交
48
int32_t smaSyncPostCommit(SSma *pSma) { return tdProcessRSmaSyncPostCommitImpl(pSma); }
C
Cary Xu 已提交
49

C
Cary Xu 已提交
50 51 52 53 54 55
/**
 * @brief Only applicable to Rollup SMA
 *
 * @param pSma
 * @return int32_t
 */
C
Cary Xu 已提交
56 57
int32_t smaAsyncPreCommit(SSma *pSma) { return tdProcessRSmaAsyncPreCommitImpl(pSma); }

C
Cary Xu 已提交
58 59 60 61 62 63 64 65
/**
 * @brief Only applicable to Rollup SMA
 *
 * @param pSma
 * @return int32_t
 */
int32_t smaAsyncCommit(SSma *pSma) { return tdProcessRSmaAsyncCommitImpl(pSma); }

C
Cary Xu 已提交
66 67 68 69 70 71 72
/**
 * @brief Only applicable to Rollup SMA
 *
 * @param pSma
 * @return int32_t
 */
int32_t smaAsyncPostCommit(SSma *pSma) { return tdProcessRSmaAsyncPostCommitImpl(pSma); }
C
Cary Xu 已提交
73

C
Cary Xu 已提交
74 75 76 77 78 79 80 81 82 83 84 85
/**
 * @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;
  }

C
Cary Xu 已提交
86
  SRSmaStat *pRSmaStat = (SRSmaStat *)SMA_ENV_STAT(pSmaEnv);
C
Cary Xu 已提交
87 88 89 90 91

  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: {
C
Cary Xu 已提交
92
      smaDebug("vgId:%d, rsma trigger stat from paused to active", SMA_VID(pSma));
C
Cary Xu 已提交
93 94 95 96
      break;
    }
    case TASK_TRIGGER_STAT_INIT: {
      atomic_store_8(RSMA_TRIGGER_STAT(pRSmaStat), TASK_TRIGGER_STAT_ACTIVE);
C
Cary Xu 已提交
97
      smaDebug("vgId:%d, rsma trigger stat from init to active", SMA_VID(pSma));
C
Cary Xu 已提交
98 99 100 101
      break;
    }
    default: {
      atomic_store_8(RSMA_TRIGGER_STAT(pRSmaStat), TASK_TRIGGER_STAT_ACTIVE);
C
Cary Xu 已提交
102
      smaError("vgId:%d, rsma trigger stat %" PRIi8 " is unexpected", SMA_VID(pSma), rsmaTriggerStat);
C
Cary Xu 已提交
103 104 105 106 107 108
      break;
    }
  }
  return TSDB_CODE_SUCCESS;
}

C
Cary Xu 已提交
109
/**
C
Cary Xu 已提交
110
 * @brief pre-commit for rollup sma(sync commit).
C
Cary Xu 已提交
111
 *  1) set trigger stat of rsma timer TASK_TRIGGER_STAT_PAUSED.
C
Cary Xu 已提交
112
 *  2) wait for all triggered fetch tasks to finish
113
 *  3) perform persist task for qTaskInfo
C
Cary Xu 已提交
114 115 116 117
 *
 * @param pSma
 * @return int32_t
 */
C
Cary Xu 已提交
118
static int32_t tdProcessRSmaSyncPreCommitImpl(SSma *pSma) {
C
Cary Xu 已提交
119 120 121 122 123
  SSmaEnv *pSmaEnv = SMA_RSMA_ENV(pSma);
  if (!pSmaEnv) {
    return TSDB_CODE_SUCCESS;
  }

C
Cary Xu 已提交
124
  SSmaStat  *pStat = SMA_ENV_STAT(pSmaEnv);
C
Cary Xu 已提交
125
  SRSmaStat *pRSmaStat = SMA_STAT_RSMA(pStat);
C
Cary Xu 已提交
126

C
Cary Xu 已提交
127
  // step 1: set rsma stat paused
C
Cary Xu 已提交
128 129
  atomic_store_8(RSMA_TRIGGER_STAT(pRSmaStat), TASK_TRIGGER_STAT_PAUSED);

C
Cary Xu 已提交
130
  // step 2: wait for all triggered fetch tasks to finish
C
Cary Xu 已提交
131 132 133
  int32_t nLoops = 0;
  while (1) {
    if (T_REF_VAL_GET(pStat) == 0) {
C
Cary Xu 已提交
134
      smaDebug("vgId:%d, rsma fetch tasks are all finished", SMA_VID(pSma));
C
Cary Xu 已提交
135 136
      break;
    } else {
C
Cary Xu 已提交
137
      smaDebug("vgId:%d, rsma fetch tasks are not all finished yet", SMA_VID(pSma));
C
Cary Xu 已提交
138 139 140 141 142 143 144
    }
    ++nLoops;
    if (nLoops > 1000) {
      sched_yield();
      nLoops = 0;
    }
  }
C
Cary Xu 已提交
145

146
  // step 3: perform persist task for qTaskInfo
C
Cary Xu 已提交
147 148
  pRSmaStat->commitAppliedVer = pSma->pVnode->state.applied;
  tdRSmaPersistExecImpl(pRSmaStat, RSMA_INFO_HASH(pRSmaStat));
149

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

C
Cary Xu 已提交
152 153 154 155 156 157 158 159 160
  return TSDB_CODE_SUCCESS;
}

/**
 * @brief commit for rollup sma
 *
 * @param pSma
 * @return int32_t
 */
C
Cary Xu 已提交
161
static int32_t tdProcessRSmaSyncCommitImpl(SSma *pSma) {
C
Cary Xu 已提交
162 163 164 165 166 167 168
  SSmaEnv *pSmaEnv = SMA_RSMA_ENV(pSma);
  if (!pSmaEnv) {
    return TSDB_CODE_SUCCESS;
  }
  return TSDB_CODE_SUCCESS;
}

C
Cary Xu 已提交
169
static int32_t tdCleanupQTaskInfoFiles(SSma *pSma, SRSmaStat *pRSmaStat) {
C
Cary Xu 已提交
170 171
  SVnode       *pVnode = pSma->pVnode;
  int64_t       committed = pRSmaStat->commitAppliedVer;
C
Cary Xu 已提交
172 173 174
  TdDirPtr      pDir = NULL;
  TdDirEntryPtr pDirEntry = NULL;
  char          dir[TSDB_FILENAME_LEN];
C
Cary Xu 已提交
175
  const char   *pattern = "v[0-9]+qtaskinfo\\.ver([0-9]+)?$";
C
Cary Xu 已提交
176
  regex_t       regex;
C
Cary Xu 已提交
177
  int           code = 0;
C
Cary Xu 已提交
178

C
Cary Xu 已提交
179
  tdGetVndDirName(TD_VID(pVnode), tfsGetPrimaryPath(pVnode->pTfs), VNODE_RSMA_DIR, true, dir);
C
Cary Xu 已提交
180 181

  // Resource allocation and init
C
Cary Xu 已提交
182 183 184 185 186 187
  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 已提交
188 189

  if ((pDir = taosOpenDir(dir)) == NULL) {
C
Cary Xu 已提交
190
    regfree(&regex);
C
Cary Xu 已提交
191
    terrno = TAOS_SYSTEM_ERROR(errno);
C
Cary Xu 已提交
192
    smaDebug("vgId:%d, rsma post commit, open dir %s failed since %s", TD_VID(pVnode), dir, terrstr());
C
Cary Xu 已提交
193 194 195
    return TSDB_CODE_FAILED;
  }

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

    code = regexec(&regex, entryName, 2, regMatch, 0);
C
Cary Xu 已提交
206 207 208

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

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

C
Cary Xu 已提交
237
  taosCloseDir(&pDir);
C
Cary Xu 已提交
238
  regfree(&regex);
C
Cary Xu 已提交
239 240 241 242

  return TSDB_CODE_SUCCESS;
}

C
Cary Xu 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
// SQTaskFile ======================================================
// int32_t tCmprQTaskFile(void const *lhs, void const *rhs) {
//   int64_t    *lCommitted = *(int64_t *)lhs;
//   SQTaskFile *rQTaskF = (SQTaskFile *)rhs;

//   if (lCommitted < rQTaskF->commitID) {
//     return -1;
//   } else if (lCommitted > rQTaskF->commitID) {
//     return 1;
//   }

//   return 0;
// }

#if 0
/**
 * @brief At most time, there is only one qtaskinfo file committed latest in aTaskFile. Sometimes, there would be
 * multiple qtaskinfo files supporting snapshot replication.
 *
 * @param pSma
 * @param pRSmaStat
 * @return int32_t
 */
static int32_t tdCleanupQTaskInfoFiles(SSma *pSma, SRSmaStat *pRSmaStat) {
  SVnode *pVnode = pSma->pVnode;
  int64_t committed = pRSmaStat->commitAppliedVer;
  SArray *aTaskFile = pRSmaStat->aTaskFile;

  void *qTaskFile = taosArraySearch(aTaskFile, committed, tCmprQTaskFile, TD_LE);
  

  return TSDB_CODE_SUCCESS;
}
#endif

C
Cary Xu 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290
/**
 * @brief post-commit for rollup sma
 *  1) clean up the outdated qtaskinfo files
 *
 * @param pSma
 * @return int32_t
 */
static int32_t tdProcessRSmaSyncPostCommitImpl(SSma *pSma) {
  SVnode *pVnode = pSma->pVnode;
  if (!VND_IS_RSMA(pVnode)) {
    return TSDB_CODE_SUCCESS;
  }

C
Cary Xu 已提交
291
  SRSmaStat *pRSmaStat = SMA_RSMA_STAT(pSma);
C
Cary Xu 已提交
292 293

  // cleanup outdated qtaskinfo files
C
Cary Xu 已提交
294
  tdCleanupQTaskInfoFiles(pSma, pRSmaStat);
C
Cary Xu 已提交
295

C
Cary Xu 已提交
296 297
  return TSDB_CODE_SUCCESS;
}
C
Cary Xu 已提交
298 299

/**
C
Cary Xu 已提交
300
 * @brief Rsma async commit implementation(only do some necessary light weighted task)
C
Cary Xu 已提交
301 302
 *  1) set rsma stat TASK_TRIGGER_STAT_PAUSED
 *  2) Wait all running fetch task finish to fetch and put submitMsg into level 2/3 wQueue(blocking level 1 write)
C
Cary Xu 已提交
303 304 305 306
 *
 * @param pSma
 * @return int32_t
 */
C
Cary Xu 已提交
307
static int32_t tdProcessRSmaAsyncPreCommitImpl(SSma *pSma) {
C
Cary Xu 已提交
308 309
  SSmaEnv *pEnv = SMA_RSMA_ENV(pSma);
  if (!pEnv) {
C
Cary Xu 已提交
310 311 312
    return TSDB_CODE_SUCCESS;
  }

C
Cary Xu 已提交
313
  SSmaStat  *pStat = SMA_ENV_STAT(pEnv);
C
Cary Xu 已提交
314
  SRSmaStat *pRSmaStat = SMA_STAT_RSMA(pStat);
C
Cary Xu 已提交
315

C
Cary Xu 已提交
316
  // step 1: set rsma stat
C
Cary Xu 已提交
317
  atomic_store_8(RSMA_TRIGGER_STAT(pRSmaStat), TASK_TRIGGER_STAT_PAUSED);
C
Cary Xu 已提交
318
  atomic_store_8(RSMA_COMMIT_STAT(pRSmaStat), 1);
C
Cary Xu 已提交
319 320
  pRSmaStat->commitAppliedVer = pSma->pVnode->state.applied;
  ASSERT(pRSmaStat->commitAppliedVer > 0);
C
Cary Xu 已提交
321

C
Cary Xu 已提交
322
  // step 2: wait for all triggered fetch tasks to finish
C
Cary Xu 已提交
323 324 325
  int32_t nLoops = 0;
  while (1) {
    if (T_REF_VAL_GET(pStat) == 0) {
C
Cary Xu 已提交
326
      smaDebug("vgId:%d, rsma commit, fetch tasks are all finished", SMA_VID(pSma));
C
Cary Xu 已提交
327 328
      break;
    } else {
C
Cary Xu 已提交
329
      smaDebug("vgId:%d, rsma commit, fetch tasks are not all finished yet", SMA_VID(pSma));
C
Cary Xu 已提交
330 331 332 333 334 335 336 337
    }
    ++nLoops;
    if (nLoops > 1000) {
      sched_yield();
      nLoops = 0;
    }
  }

C
Cary Xu 已提交
338 339 340 341 342
  /**
   * @brief step 3: consume the SubmitReq in buffer
   *  1) This is high cost task and should not put in asyncPreCommit originally.
   *  2) But, if put in asyncCommit, would trigger taskInfo cloning frequently.
   */
C
Cary Xu 已提交
343 344 345
  if (tdRSmaProcessExecImpl(pSma, RSMA_EXEC_COMMIT) < 0) {
    return TSDB_CODE_FAILED;
  }
C
Cary Xu 已提交
346

C
Cary Xu 已提交
347 348 349 350 351
  smaInfo("vgId:%d, rsma commit, wait for all items to be consumed, TID:%p", SMA_VID(pSma), (void*)taosGetSelfPthreadId());
  nLoops = 0;
  while (atomic_load_64(&pRSmaStat->nBufItems) > 0) {
    ++nLoops;
    if (nLoops > 1000) {
C
Cary Xu 已提交
352 353 354 355
      sched_yield();
      nLoops = 0;
    }
  }
C
Cary Xu 已提交
356 357 358 359 360
  smaInfo("vgId:%d, rsma commit, all items are consumed, TID:%p", SMA_VID(pSma), (void *)taosGetSelfPthreadId());
  if (tdRSmaPersistExecImpl(pRSmaStat, RSMA_INFO_HASH(pRSmaStat)) < 0) {
    return TSDB_CODE_FAILED;
  }
  smaInfo("vgId:%d, rsma commit, operator state commited, TID:%p", SMA_VID(pSma), (void *)taosGetSelfPthreadId());
C
Cary Xu 已提交
361

C
Cary Xu 已提交
362
#if 0 // consuming task of qTaskInfo clone 
C
Cary Xu 已提交
363
  // step 4:  swap queue/qall and iQueue/iQall
C
Cary Xu 已提交
364
  // lock
C
Cary Xu 已提交
365
  // taosWLockLatch(SMA_ENV_LOCK(pEnv));
C
Cary Xu 已提交
366

C
Cary Xu 已提交
367 368
  ASSERT(RSMA_INFO_HASH(pRSmaStat));

C
Cary Xu 已提交
369
  void *pIter = taosHashIterate(RSMA_INFO_HASH(pRSmaStat), NULL);
C
Cary Xu 已提交
370

C
Cary Xu 已提交
371 372 373 374 375 376 377
  while (pIter) {
    SRSmaInfo *pInfo = *(SRSmaInfo **)pIter;
    TSWAP(pInfo->iQall, pInfo->qall);
    TSWAP(pInfo->iQueue, pInfo->queue);
    TSWAP(pInfo->iTaskInfo[0], pInfo->taskInfo[0]);
    TSWAP(pInfo->iTaskInfo[1], pInfo->taskInfo[1]);
    pIter = taosHashIterate(RSMA_INFO_HASH(pRSmaStat), pIter);
C
Cary Xu 已提交
378 379
  }

C
Cary Xu 已提交
380
  // unlock
C
Cary Xu 已提交
381 382
  // taosWUnLockLatch(SMA_ENV_LOCK(pEnv));
#endif
C
Cary Xu 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397

  return TSDB_CODE_SUCCESS;
}

/**
 * @brief commit for rollup sma
 *
 * @param pSma
 * @return int32_t
 */
static int32_t tdProcessRSmaAsyncCommitImpl(SSma *pSma) {
  SSmaEnv *pSmaEnv = SMA_RSMA_ENV(pSma);
  if (!pSmaEnv) {
    return TSDB_CODE_SUCCESS;
  }
C
Cary Xu 已提交
398
#if 0
C
Cary Xu 已提交
399
  SRSmaStat *pRSmaStat = (SRSmaStat *)SMA_ENV_STAT(pSmaEnv);
C
Cary Xu 已提交
400

C
Cary Xu 已提交
401
  // perform persist task for qTaskInfo operator
C
Cary Xu 已提交
402 403 404
  if (tdRSmaPersistExecImpl(pRSmaStat, RSMA_INFO_HASH(pRSmaStat)) < 0) {
    return TSDB_CODE_FAILED;
  }
C
Cary Xu 已提交
405
#endif
C
Cary Xu 已提交
406

C
Cary Xu 已提交
407 408 409 410
  return TSDB_CODE_SUCCESS;
}

/**
C
Cary Xu 已提交
411
 * @brief Migrate rsmaInfo from iRsmaInfo to rsmaInfo if rsma infoHash not empty.
C
Cary Xu 已提交
412 413 414
 *
 * @param pSma
 * @return int32_t
C
Cary Xu 已提交
415 416
 */
static int32_t tdProcessRSmaAsyncPostCommitImpl(SSma *pSma) {
C
Cary Xu 已提交
417 418
  SSmaEnv *pEnv = SMA_RSMA_ENV(pSma);
  if (!pEnv) {
C
Cary Xu 已提交
419 420 421
    return TSDB_CODE_SUCCESS;
  }

C
Cary Xu 已提交
422 423
  SRSmaStat *pRSmaStat = (SRSmaStat *)SMA_ENV_STAT(pEnv);
  SArray    *rsmaDeleted = NULL;
C
Cary Xu 已提交
424

C
Cary Xu 已提交
425
  // step 1: merge qTaskInfo and iQTaskInfo
C
Cary Xu 已提交
426
  // lock
C
Cary Xu 已提交
427
  // taosWLockLatch(SMA_ENV_LOCK(pEnv));
C
Cary Xu 已提交
428

C
Cary Xu 已提交
429 430
  void *pIter = NULL;
  while ((pIter = taosHashIterate(RSMA_INFO_HASH(pRSmaStat), pIter))) {
C
Cary Xu 已提交
431
    tb_uid_t  *pSuid = (tb_uid_t *)taosHashGetKey(pIter, NULL);
C
Cary Xu 已提交
432 433 434 435
    SRSmaInfo *pRSmaInfo = *(SRSmaInfo **)pIter;
    if (RSMA_INFO_IS_DEL(pRSmaInfo)) {
      int32_t refVal = T_REF_VAL_GET(pRSmaInfo);
      if (refVal == 0) {
C
Cary Xu 已提交
436 437
        if (!rsmaDeleted) {
          if ((rsmaDeleted = taosArrayInit(1, sizeof(tb_uid_t)))) {
C
Cary Xu 已提交
438 439
            taosArrayPush(rsmaDeleted, pSuid);
          }
C
Cary Xu 已提交
440
        }
C
Cary Xu 已提交
441 442 443 444 445 446 447 448 449
      } else {
        smaDebug(
            "vgId:%d, rsma async post commit, not free rsma info since ref is %d although already deleted for "
            "table:%" PRIi64,
            SMA_VID(pSma), refVal, *pSuid);
      }

      continue;
    }
C
Cary Xu 已提交
450
#if 0
C
Cary Xu 已提交
451 452 453
    if (pRSmaInfo->taskInfo[0]) {
      if (pRSmaInfo->iTaskInfo[0]) {
        SRSmaInfo *pRSmaInfo = *(SRSmaInfo **)pRSmaInfo->iTaskInfo[0];
C
Cary Xu 已提交
454
        tdFreeRSmaInfo(pSma, pRSmaInfo, false);
C
Cary Xu 已提交
455
        pRSmaInfo->iTaskInfo[0] = NULL;
C
Cary Xu 已提交
456 457
      }
    } else {
C
Cary Xu 已提交
458
      TSWAP(pRSmaInfo->taskInfo[0], pRSmaInfo->iTaskInfo[0]);
C
Cary Xu 已提交
459
    }
C
Cary Xu 已提交
460

C
Cary Xu 已提交
461 462
    taosHashPut(RSMA_INFO_HASH(pRSmaStat), pSuid, sizeof(tb_uid_t), pIter, sizeof(pIter));
    smaDebug("vgId:%d, rsma async post commit, migrated from iRsmaInfoHash for table:%" PRIi64, SMA_VID(pSma), *pSuid);
C
Cary Xu 已提交
463
#endif
C
Cary Xu 已提交
464 465
  }

C
Cary Xu 已提交
466 467 468 469 470 471 472 473 474
  for (int32_t i = 0; i < taosArrayGetSize(rsmaDeleted); ++i) {
    tb_uid_t *pSuid = taosArrayGet(rsmaDeleted, i);
    void     *pRSmaInfo = taosHashGet(RSMA_INFO_HASH(pRSmaStat), pSuid, sizeof(tb_uid_t));
    if ((pRSmaInfo = *(SRSmaInfo **)pRSmaInfo)) {
      tdFreeRSmaInfo(pSma, pRSmaInfo, true);
      smaDebug(
          "vgId:%d, rsma async post commit, free rsma info since already deleted and ref is 0 for "
          "table:%" PRIi64,
          SMA_VID(pSma), *pSuid);
C
Cary Xu 已提交
475
    }
C
Cary Xu 已提交
476
    taosHashRemove(RSMA_INFO_HASH(pRSmaStat), pSuid, sizeof(tb_uid_t));
C
Cary Xu 已提交
477
  }
C
Cary Xu 已提交
478
  taosArrayDestroy(rsmaDeleted);
C
Cary Xu 已提交
479

C
Cary Xu 已提交
480
  // unlock
C
Cary Xu 已提交
481
  // taosWUnLockLatch(SMA_ENV_LOCK(pEnv));
C
Cary Xu 已提交
482 483

  // step 2: cleanup outdated qtaskinfo files
C
Cary Xu 已提交
484
  tdCleanupQTaskInfoFiles(pSma, pRSmaStat);
C
Cary Xu 已提交
485

C
Cary Xu 已提交
486 487
  atomic_store_8(RSMA_COMMIT_STAT(pRSmaStat), 0);

C
Cary Xu 已提交
488 489
  return TSDB_CODE_SUCCESS;
}