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.
112 113
 *  2) wait all triggered fetch tasks finished
 *  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);

130
  // step 2: wait all triggered fetch tasks finished
C
Cary Xu 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144
  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 已提交
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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

  // step 2: wait all triggered fetch tasks finished
  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 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
  /**
   * @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.
   */
  nLoops = 0;
  smaInfo("vgId:%d, start to wait for rsma qtask free, TID:%p", SMA_VID(pSma), (void *)taosGetSelfPthreadId());

  int8_t old;
  while (1) {
    old = atomic_val_compare_exchange_8(&pRSmaStat->execStat, 0, 1);
    if (old == 0) break;
    if (++nLoops > 1000) {
      sched_yield();
      nLoops = 0;
      smaDebug("vgId:%d, wait for rsma qtask free, TID:%p", SMA_VID(pSma), (void *)taosGetSelfPthreadId());
    }
  }

  smaInfo("vgId:%d, end to wait for rsma qtask free, TID:%p", SMA_VID(pSma), (void *)taosGetSelfPthreadId());

  if (tdRSmaProcessExecImpl(pSma, RSMA_EXEC_COMMIT) < 0) {
    atomic_store_8(&pRSmaStat->execStat, 0);
    return TSDB_CODE_FAILED;
  }

  // step 4:  swap queue/qall and iQueue/iQall
C
Cary Xu 已提交
363 364 365
  // lock
  taosWLockLatch(SMA_ENV_LOCK(pEnv));

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

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

C
Cary Xu 已提交
370 371 372 373 374 375 376
  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 已提交
377 378
  }

C
Cary Xu 已提交
379
  atomic_store_64(&pRSmaStat->qBufSize, 0);
C
Cary Xu 已提交
380
  atomic_store_8(&pRSmaStat->execStat, 0);
C
Cary Xu 已提交
381 382 383
  // unlock
  taosWUnLockLatch(SMA_ENV_LOCK(pEnv));

C
Cary Xu 已提交
384
  // step 5: others
C
Cary Xu 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
  pRSmaStat->commitAppliedVer = pSma->pVnode->state.applied;

  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 已提交
402
  SRSmaStat *pRSmaStat = (SRSmaStat *)SMA_ENV_STAT(pSmaEnv);
C
Cary Xu 已提交
403

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

C
Cary Xu 已提交
409 410 411 412
  return TSDB_CODE_SUCCESS;
}

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

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

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

  void *pIter = taosHashIterate(RSMA_INFO_HASH(pRSmaStat), NULL);
C
Cary Xu 已提交
432
  while (pIter) {
C
Cary Xu 已提交
433
    tb_uid_t  *pSuid = (tb_uid_t *)taosHashGetKey(pIter, NULL);
C
Cary Xu 已提交
434 435 436 437
    SRSmaInfo *pRSmaInfo = *(SRSmaInfo **)pIter;
    if (RSMA_INFO_IS_DEL(pRSmaInfo)) {
      int32_t refVal = T_REF_VAL_GET(pRSmaInfo);
      if (refVal == 0) {
C
Cary Xu 已提交
438 439
        if (!rsmaDeleted) {
          if ((rsmaDeleted = taosArrayInit(1, sizeof(tb_uid_t)))) {
C
Cary Xu 已提交
440 441
            taosArrayPush(rsmaDeleted, pSuid);
          }
C
Cary Xu 已提交
442
        }
C
Cary Xu 已提交
443 444 445 446 447 448 449 450 451 452
      } 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);
      }

      pIter = taosHashIterate(RSMA_INFO_HASH(pRSmaStat), pIter);
      continue;
    }
C
Cary Xu 已提交
453

C
Cary Xu 已提交
454 455 456 457 458
    if (pRSmaInfo->taskInfo[0]) {
      if (pRSmaInfo->iTaskInfo[0]) {
        SRSmaInfo *pRSmaInfo = *(SRSmaInfo **)pRSmaInfo->iTaskInfo[0];
        tdFreeRSmaInfo(pSma, pRSmaInfo, true);
        pRSmaInfo->iTaskInfo[0] = NULL;
C
Cary Xu 已提交
459 460
      }
    } else {
C
Cary Xu 已提交
461
      TSWAP(pRSmaInfo->taskInfo[0], pRSmaInfo->iTaskInfo[0]);
C
Cary Xu 已提交
462
    }
C
Cary Xu 已提交
463

C
Cary Xu 已提交
464 465 466 467
    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);

    pIter = taosHashIterate(RSMA_INFO_HASH(pRSmaStat), pIter);
C
Cary Xu 已提交
468 469
  }

C
Cary Xu 已提交
470 471 472 473 474 475 476 477 478
  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 已提交
479
    }
C
Cary Xu 已提交
480
    taosHashRemove(RSMA_INFO_HASH(pRSmaStat), pSuid, sizeof(tb_uid_t));
C
Cary Xu 已提交
481
  }
C
Cary Xu 已提交
482 483
  taosArrayDestroy(rsmaDeleted);
  // TODO: remove suid in files?
C
Cary Xu 已提交
484

C
Cary Xu 已提交
485 486
  // unlock
  taosWUnLockLatch(SMA_ENV_LOCK(pEnv));
C
Cary Xu 已提交
487 488

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

C
Cary Xu 已提交
491 492
  atomic_store_8(RSMA_COMMIT_STAT(pRSmaStat), 0);

C
Cary Xu 已提交
493 494
  return TSDB_CODE_SUCCESS;
}