tsdbSnapshot.c 56.9 KB
Newer Older
H
Hongze Cheng 已提交
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"

H
Hongze Cheng 已提交
18
extern int32_t tsdbReadDataBlockEx(SDataFReader* pReader, SDataBlk* pDataBlk, SBlockData* pBlockData);
H
Hongze Cheng 已提交
19 20 21
extern int32_t tsdbUpdateTableSchema(SMeta* pMeta, int64_t suid, int64_t uid, SSkmInfo* pSkmInfo);
extern int32_t tsdbWriteDataBlock(SDataFWriter* pWriter, SBlockData* pBlockData, SMapData* mDataBlk, int8_t cmprAlg);
extern int32_t tsdbWriteSttBlock(SDataFWriter* pWriter, SBlockData* pBlockData, SArray* aSttBlk, int8_t cmprAlg);
H
Hongze Cheng 已提交
22 23 24 25 26

// STsdbDataIter2 ========================================
#define TSDB_MEM_TABLE_DATA_ITER 0
#define TSDB_DATA_FILE_DATA_ITER 1
#define TSDB_STT_FILE_DATA_ITER  2
H
Hongze Cheng 已提交
27
#define TSDB_TOMB_FILE_DATA_ITER 3
H
Hongze Cheng 已提交
28

H
Hongze Cheng 已提交
29 30 31
typedef struct STsdbDataIter2  STsdbDataIter2;
typedef struct STsdbFilterInfo STsdbFilterInfo;

H
Hongze Cheng 已提交
32 33 34 35 36 37
typedef struct {
  int64_t  suid;
  int64_t  uid;
  SDelData delData;
} SDelInfo;

H
Hongze Cheng 已提交
38 39 40 41 42 43
struct STsdbDataIter2 {
  STsdbDataIter2* next;
  SRBTreeNode     rbtn;

  int32_t  type;
  SRowInfo rowInfo;
H
Hongze Cheng 已提交
44
  SDelInfo delInfo;
H
Hongze Cheng 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  union {
    // TSDB_MEM_TABLE_DATA_ITER
    struct {
      SMemTable* pMemTable;
    } mIter;

    // TSDB_DATA_FILE_DATA_ITER
    struct {
      SDataFReader* pReader;
      SArray*       aBlockIdx;  // SArray<SBlockIdx>
      SMapData      mDataBlk;
      SBlockData    bData;
      int32_t       iBlockIdx;
      int32_t       iDataBlk;
      int32_t       iRow;
    } dIter;

    // TSDB_STT_FILE_DATA_ITER
    struct {
      SDataFReader* pReader;
      int32_t       iStt;
      SArray*       aSttBlk;
      SBlockData    bData;
      int32_t       iSttBlk;
      int32_t       iRow;
    } sIter;
H
Hongze Cheng 已提交
71 72 73 74 75 76 77 78
    // TSDB_TOMB_FILE_DATA_ITER
    struct {
      SDelFReader* pReader;
      SArray*      aDelIdx;
      SArray*      aDelData;
      int32_t      iDelIdx;
      int32_t      iDelData;
    } tIter;
H
Hongze Cheng 已提交
79 80 81
  };
};

H
Hongze Cheng 已提交
82 83 84 85 86 87 88
#define TSDB_FILTER_FLAG_BY_VERSION 0x1
struct STsdbFilterInfo {
  int32_t flag;
  int64_t sver;
  int64_t ever;
};

H
Hongze Cheng 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
#define TSDB_RBTN_TO_DATA_ITER(pNode) ((STsdbDataIter2*)(((char*)pNode) - offsetof(STsdbDataIter2, rbtn)))

/* open */
static int32_t tsdbOpenDataFileDataIter(SDataFReader* pReader, STsdbDataIter2** ppIter) {
  int32_t code = 0;
  int32_t lino = 0;

  // create handle
  STsdbDataIter2* pIter = (STsdbDataIter2*)taosMemoryCalloc(1, sizeof(*pIter));
  if (pIter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  pIter->type = TSDB_DATA_FILE_DATA_ITER;
  pIter->dIter.pReader = pReader;
  if ((pIter->dIter.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  code = tBlockDataCreate(&pIter->dIter.bData);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
113 114 115
  pIter->dIter.iBlockIdx = 0;
  pIter->dIter.iDataBlk = 0;
  pIter->dIter.iRow = 0;
H
Hongze Cheng 已提交
116 117 118 119 120 121 122 123 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 157 158 159

  // read data
  code = tsdbReadBlockIdx(pReader, pIter->dIter.aBlockIdx);
  TSDB_CHECK_CODE(code, lino, _exit);

  if (taosArrayGetSize(pIter->dIter.aBlockIdx) == 0) goto _clear;

_exit:
  if (code) {
    if (pIter) {
    _clear:
      tBlockDataDestroy(&pIter->dIter.bData, 1);
      taosArrayDestroy(pIter->dIter.aBlockIdx);
      taosMemoryFree(pIter);
      pIter = NULL;
    }
  }
  *ppIter = pIter;
  return code;
}

static int32_t tsdbOpenSttFileDataIter(SDataFReader* pReader, int32_t iStt, STsdbDataIter2** ppIter) {
  int32_t code = 0;
  int32_t lino = 0;

  // create handle
  STsdbDataIter2* pIter = (STsdbDataIter2*)taosMemoryCalloc(1, sizeof(*pIter));
  if (pIter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  pIter->type = TSDB_STT_FILE_DATA_ITER;
  pIter->sIter.pReader = pReader;
  pIter->sIter.iStt = iStt;
  pIter->sIter.aSttBlk = taosArrayInit(0, sizeof(SSttBlk));
  if (pIter->sIter.aSttBlk == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  code = tBlockDataCreate(&pIter->sIter.bData);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
160 161
  pIter->sIter.iSttBlk = 0;
  pIter->sIter.iRow = 0;
H
Hongze Cheng 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

  // read data
  code = tsdbReadSttBlk(pReader, iStt, pIter->sIter.aSttBlk);
  TSDB_CHECK_CODE(code, lino, _exit);

  if (taosArrayGetSize(pIter->sIter.aSttBlk) == 0) goto _clear;

_exit:
  if (code) {
    if (pIter) {
    _clear:
      taosArrayDestroy(pIter->sIter.aSttBlk);
      tBlockDataDestroy(&pIter->sIter.bData, 1);
      taosMemoryFree(pIter);
      pIter = NULL;
    }
  }
  *ppIter = pIter;
  return code;
}

H
Hongze Cheng 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
static int32_t tsdbOpenTombFileDataIter(SDelFReader* pReader, STsdbDataIter2** ppIter) {
  int32_t code = 0;
  int32_t lino = 0;

  STsdbDataIter2* pIter = (STsdbDataIter2*)taosMemoryCalloc(1, sizeof(*pIter));
  if (pIter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }
  pIter->type = TSDB_TOMB_FILE_DATA_ITER;

  pIter->tIter.pReader = pReader;
  if ((pIter->tIter.aDelIdx = taosArrayInit(0, sizeof(SDelIdx))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }
  if ((pIter->tIter.aDelData = taosArrayInit(0, sizeof(SDelData))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  code = tsdbReadDelIdx(pReader, pIter->tIter.aDelIdx);
  TSDB_CHECK_CODE(code, lino, _exit);

  if (taosArrayGetSize(pIter->tIter.aDelIdx) == 0) goto _clear;

  pIter->tIter.iDelIdx = 0;
  pIter->tIter.iDelData = 0;

_exit:
  if (code) {
    if (pIter) {
    _clear:
      taosArrayDestroy(pIter->tIter.aDelIdx);
      taosArrayDestroy(pIter->tIter.aDelData);
      taosMemoryFree(pIter);
      pIter = NULL;
    }
  }
  *ppIter = pIter;
  return code;
}

H
Hongze Cheng 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239
/* close */
static void tsdbCloseDataFileDataIter(STsdbDataIter2* pIter) {
  tBlockDataDestroy(&pIter->dIter.bData, 1);
  tMapDataClear(&pIter->dIter.mDataBlk);
  taosArrayDestroy(pIter->dIter.aBlockIdx);
  taosMemoryFree(pIter);
}

static void tsdbCloseSttFileDataIter(STsdbDataIter2* pIter) {
  tBlockDataDestroy(&pIter->sIter.bData, 1);
  taosArrayDestroy(pIter->sIter.aSttBlk);
  taosMemoryFree(pIter);
}

H
Hongze Cheng 已提交
240 241 242 243 244 245
static void tsdbCloseTombFileDataIter(STsdbDataIter2* pIter) {
  taosArrayDestroy(pIter->tIter.aDelData);
  taosArrayDestroy(pIter->tIter.aDelIdx);
  taosMemoryFree(pIter);
}

H
Hongze Cheng 已提交
246 247 248 249 250 251 252
static void tsdbCloseDataIter2(STsdbDataIter2* pIter) {
  if (pIter->type == TSDB_MEM_TABLE_DATA_ITER) {
    ASSERT(0);
  } else if (pIter->type == TSDB_DATA_FILE_DATA_ITER) {
    tsdbCloseDataFileDataIter(pIter);
  } else if (pIter->type == TSDB_STT_FILE_DATA_ITER) {
    tsdbCloseSttFileDataIter(pIter);
H
Hongze Cheng 已提交
253 254
  } else if (pIter->type == TSDB_TOMB_FILE_DATA_ITER) {
    tsdbCloseTombFileDataIter(pIter);
H
Hongze Cheng 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
  } else {
    ASSERT(0);
  }
}

/* cmpr */
static int32_t tsdbDataIterCmprFn(const SRBTreeNode* pNode1, const SRBTreeNode* pNode2) {
  STsdbDataIter2* pIter1 = TSDB_RBTN_TO_DATA_ITER(pNode1);
  STsdbDataIter2* pIter2 = TSDB_RBTN_TO_DATA_ITER(pNode2);
  return tRowInfoCmprFn(&pIter1->rowInfo, &pIter2->rowInfo);
}

/* seek */

/* iter next */
H
Hongze Cheng 已提交
270
static int32_t tsdbDataFileDataIterNext(STsdbDataIter2* pIter, STsdbFilterInfo* pFilterInfo) {
H
Hongze Cheng 已提交
271 272 273 274
  int32_t code = 0;
  int32_t lino = 0;

  for (;;) {
H
Hongze Cheng 已提交
275 276 277 278 279 280 281 282 283 284 285
    while (pIter->dIter.iRow < pIter->dIter.bData.nRow) {
      if (pFilterInfo) {
        if (pFilterInfo->flag & TSDB_FILTER_FLAG_BY_VERSION) {
          if (pIter->dIter.bData.aVersion[pIter->dIter.iRow] < pFilterInfo->sver ||
              pIter->dIter.bData.aVersion[pIter->dIter.iRow] > pFilterInfo->ever) {
            pIter->dIter.iRow++;
            continue;
          }
        }
      }

H
Hongze Cheng 已提交
286 287 288
      pIter->rowInfo.suid = pIter->dIter.bData.suid;
      pIter->rowInfo.uid = pIter->dIter.bData.uid;
      pIter->rowInfo.row = tsdbRowFromBlockData(&pIter->dIter.bData, pIter->dIter.iRow);
H
Hongze Cheng 已提交
289
      pIter->dIter.iRow++;
H
Hongze Cheng 已提交
290
      goto _exit;
H
Hongze Cheng 已提交
291 292 293
    }

    for (;;) {
H
Hongze Cheng 已提交
294
      while (pIter->dIter.iDataBlk < pIter->dIter.mDataBlk.nItem) {
H
Hongze Cheng 已提交
295 296 297
        SDataBlk dataBlk;
        tMapDataGetItemByIdx(&pIter->dIter.mDataBlk, pIter->dIter.iDataBlk, &dataBlk, tGetDataBlk);

H
Hongze Cheng 已提交
298 299 300 301 302 303 304 305 306 307
        // filter
        if (pFilterInfo) {
          if (pFilterInfo->flag & TSDB_FILTER_FLAG_BY_VERSION) {
            if (pFilterInfo->sver > dataBlk.maxVer || pFilterInfo->ever < dataBlk.minVer) {
              pIter->dIter.iDataBlk++;
              continue;
            }
          }
        }

H
Hongze Cheng 已提交
308 309 310
        code = tsdbReadDataBlockEx(pIter->dIter.pReader, &dataBlk, &pIter->dIter.bData);
        TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
311 312
        pIter->dIter.iDataBlk++;
        pIter->dIter.iRow = 0;
H
Hongze Cheng 已提交
313 314 315 316

        break;
      }

H
Hongze Cheng 已提交
317 318
      if (pIter->dIter.iRow < pIter->dIter.bData.nRow) break;

H
Hongze Cheng 已提交
319
      for (;;) {
H
Hongze Cheng 已提交
320
        if (pIter->dIter.iBlockIdx < taosArrayGetSize(pIter->dIter.aBlockIdx)) {
H
Hongze Cheng 已提交
321 322 323 324 325
          SBlockIdx* pBlockIdx = taosArrayGet(pIter->dIter.aBlockIdx, pIter->dIter.iBlockIdx);

          code = tsdbReadDataBlk(pIter->dIter.pReader, pBlockIdx, &pIter->dIter.mDataBlk);
          TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
326 327
          pIter->dIter.iBlockIdx++;
          pIter->dIter.iDataBlk = 0;
H
Hongze Cheng 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

          break;
        } else {
          pIter->rowInfo = (SRowInfo){0};
          goto _exit;
        }
      }
    }
  }

_exit:
  if (code) {
    tsdbError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
  }
  return code;
}

H
Hongze Cheng 已提交
345
static int32_t tsdbSttFileDataIterNext(STsdbDataIter2* pIter, STsdbFilterInfo* pFilterInfo) {
H
Hongze Cheng 已提交
346 347 348 349
  int32_t code = 0;
  int32_t lino = 0;

  for (;;) {
H
Hongze Cheng 已提交
350 351 352 353 354 355 356 357 358 359 360
    while (pIter->sIter.iRow < pIter->sIter.bData.nRow) {
      if (pFilterInfo) {
        if (pFilterInfo->flag & TSDB_FILTER_FLAG_BY_VERSION) {
          if (pFilterInfo->sver > pIter->sIter.bData.aVersion[pIter->sIter.iRow] ||
              pFilterInfo->ever < pIter->sIter.bData.aVersion[pIter->sIter.iRow]) {
            pIter->sIter.iRow++;
            continue;
          }
        }
      }

H
Hongze Cheng 已提交
361 362 363
      pIter->rowInfo.suid = pIter->sIter.bData.suid;
      pIter->rowInfo.uid = pIter->sIter.bData.uid ? pIter->sIter.bData.uid : pIter->sIter.bData.aUid[pIter->sIter.iRow];
      pIter->rowInfo.row = tsdbRowFromBlockData(&pIter->sIter.bData, pIter->sIter.iRow);
H
Hongze Cheng 已提交
364
      pIter->sIter.iRow++;
H
Hongze Cheng 已提交
365
      goto _exit;
H
Hongze Cheng 已提交
366 367
    }

H
Hongze Cheng 已提交
368 369 370 371 372 373 374 375 376 377 378 379
    for (;;) {
      if (pIter->sIter.iSttBlk < taosArrayGetSize(pIter->sIter.aSttBlk)) {
        SSttBlk* pSttBlk = taosArrayGet(pIter->sIter.aSttBlk, pIter->sIter.iSttBlk);

        if (pFilterInfo) {
          if (pFilterInfo->flag & TSDB_FILTER_FLAG_BY_VERSION) {
            if (pFilterInfo->sver > pSttBlk->maxVer || pFilterInfo->ever < pSttBlk->minVer) {
              pIter->sIter.iSttBlk++;
              continue;
            }
          }
        }
H
Hongze Cheng 已提交
380

H
Hongze Cheng 已提交
381 382
        code = tsdbReadSttBlockEx(pIter->sIter.pReader, pIter->sIter.iStt, pSttBlk, &pIter->sIter.bData);
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
383

H
Hongze Cheng 已提交
384 385 386 387 388 389 390
        pIter->sIter.iRow = 0;
        pIter->sIter.iSttBlk++;
        break;
      } else {
        pIter->rowInfo = (SRowInfo){0};
        goto _exit;
      }
H
Hongze Cheng 已提交
391 392 393 394 395 396 397 398 399 400
    }
  }

_exit:
  if (code) {
    tsdbError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
  }
  return code;
}

H
Hongze Cheng 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
static int32_t tsdbTombFileDataIterNext(STsdbDataIter2* pIter, STsdbFilterInfo* pFilterInfo) {
  int32_t code = 0;
  int32_t lino = 0;

  for (;;) {
    while (pIter->tIter.iDelData < taosArrayGetSize(pIter->tIter.aDelData)) {
      SDelData* pDelData = taosArrayGet(pIter->tIter.aDelData, pIter->tIter.iDelData);

      if (pFilterInfo) {
        if (pFilterInfo->flag & TSDB_FILTER_FLAG_BY_VERSION) {
          if (pFilterInfo->sver > pDelData->version || pFilterInfo->ever < pDelData->version) {
            pIter->tIter.iDelData++;
            continue;
          }
        }
      }

      pIter->delInfo.delData = *pDelData;
      pIter->tIter.iDelData++;
      goto _exit;
    }

    for (;;) {
      if (pIter->tIter.iDelIdx < taosArrayGetSize(pIter->tIter.aDelIdx)) {
        SDelIdx* pDelIdx = taosArrayGet(pIter->tIter.aDelIdx, pIter->tIter.iDelIdx);

        code = tsdbReadDelData(pIter->tIter.pReader, pDelIdx, pIter->tIter.aDelData);
        TSDB_CHECK_CODE(code, lino, _exit);

        pIter->delInfo.suid = pDelIdx->suid;
        pIter->delInfo.uid = pDelIdx->uid;
        pIter->tIter.iDelData = 0;
        pIter->tIter.iDelIdx++;
        break;
      } else {
        pIter->delInfo = (SDelInfo){0};
        goto _exit;
      }
    }
  }

_exit:
  if (code) {
    tsdbError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
  }
  return code;
}

H
Hongze Cheng 已提交
449
static int32_t tsdbDataIterNext2(STsdbDataIter2* pIter, STsdbFilterInfo* pFilterInfo) {
H
Hongze Cheng 已提交
450 451 452 453 454 455
  int32_t code = 0;

  if (pIter->type == TSDB_MEM_TABLE_DATA_ITER) {
    ASSERT(0);
    return code;
  } else if (pIter->type == TSDB_DATA_FILE_DATA_ITER) {
H
Hongze Cheng 已提交
456
    return tsdbDataFileDataIterNext(pIter, pFilterInfo);
H
Hongze Cheng 已提交
457
  } else if (pIter->type == TSDB_STT_FILE_DATA_ITER) {
H
Hongze Cheng 已提交
458
    return tsdbSttFileDataIterNext(pIter, pFilterInfo);
H
Hongze Cheng 已提交
459 460
  } else if (pIter->type == TSDB_TOMB_FILE_DATA_ITER) {
    return tsdbTombFileDataIterNext(pIter, pFilterInfo);
H
Hongze Cheng 已提交
461 462 463 464 465 466 467 468
  } else {
    ASSERT(0);
    return code;
  }
}

/* get */

H
Hongze Cheng 已提交
469
// STsdbSnapReader ========================================
H
Hongze Cheng 已提交
470
struct STsdbSnapReader {
H
Hongze Cheng 已提交
471 472 473 474 475
  STsdb*   pTsdb;
  int64_t  sver;
  int64_t  ever;
  int8_t   type;
  uint8_t* aBuf[5];
H
Hongze Cheng 已提交
476

H
Hongze Cheng 已提交
477
  STsdbFS  fs;
H
Hongze Cheng 已提交
478
  TABLEID  tbid;
H
Hongze Cheng 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
  SSkmInfo skmTable;

  // timeseries data
  int8_t  dataDone;
  int32_t fid;

  SDataFReader*   pDataFReader;
  STsdbDataIter2* iterList;
  STsdbDataIter2* pIter;
  SRBTree         rbt;
  SBlockData      bData;

  // tombstone data
  int8_t          delDone;
  SDelFReader*    pDelFReader;
  STsdbDataIter2* pTIter;
H
Hongze Cheng 已提交
495
  SArray*         aDelData;
H
Hongze Cheng 已提交
496
};
H
Hongze Cheng 已提交
497

H
Hongze Cheng 已提交
498
static int32_t tsdbSnapReadFileDataStart(STsdbSnapReader* pReader) {
H
Hongze Cheng 已提交
499
  int32_t code = 0;
H
add log  
Hongze Cheng 已提交
500
  int32_t lino = 0;
H
Hongze Cheng 已提交
501

H
Hongze Cheng 已提交
502 503 504 505 506
  SDFileSet* pSet = taosArraySearch(pReader->fs.aDFileSet, &(SDFileSet){.fid = pReader->fid}, tDFileSetCmprFn, TD_GT);
  if (pSet == NULL) {
    pReader->fid = INT32_MAX;
    goto _exit;
  }
H
Hongze Cheng 已提交
507

H
Hongze Cheng 已提交
508 509
  pReader->fid = pSet->fid;

H
Hongze Cheng 已提交
510
  tRBTreeCreate(&pReader->rbt, tsdbDataIterCmprFn);
H
Hongze Cheng 已提交
511

H
Hongze Cheng 已提交
512
  code = tsdbDataFReaderOpen(&pReader->pDataFReader, pReader->pTsdb, pSet);
H
add log  
Hongze Cheng 已提交
513
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
514

H
Hongze Cheng 已提交
515 516
  code = tsdbOpenDataFileDataIter(pReader->pDataFReader, &pReader->pIter);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
517

H
Hongze Cheng 已提交
518 519 520 521 522 523
  if (pReader->pIter) {
    // iter to next with filter info (sver, ever)
    code = tsdbDataIterNext2(pReader->pIter,
                             &(STsdbFilterInfo){.flag = TSDB_FILTER_FLAG_BY_VERSION,  // flag
                                                .sver = pReader->sver,
                                                .ever = pReader->ever});
H
add log  
Hongze Cheng 已提交
524
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
525

H
Hongze Cheng 已提交
526 527 528
    if (pReader->pIter->rowInfo.suid || pReader->pIter->rowInfo.uid) {
      // add to rbtree
      tRBTreePut(&pReader->rbt, &pReader->pIter->rbtn);
H
Hongze Cheng 已提交
529

H
Hongze Cheng 已提交
530 531 532 533 534
      // add to iterList
      pReader->pIter->next = pReader->iterList;
      pReader->iterList = pReader->pIter;
    } else {
      tsdbCloseDataIter2(pReader->pIter);
H
Hongze Cheng 已提交
535 536
    }
  }
H
Hongze Cheng 已提交
537

H
Hongze Cheng 已提交
538 539
  for (int32_t iStt = 0; iStt < pSet->nSttF; ++iStt) {
    code = tsdbOpenSttFileDataIter(pReader->pDataFReader, iStt, &pReader->pIter);
H
add log  
Hongze Cheng 已提交
540
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
541

H
Hongze Cheng 已提交
542 543 544 545 546 547
    if (pReader->pIter) {
      // iter to valid row
      code = tsdbDataIterNext2(pReader->pIter,
                               &(STsdbFilterInfo){.flag = TSDB_FILTER_FLAG_BY_VERSION,  // flag
                                                  .sver = pReader->sver,
                                                  .ever = pReader->ever});
H
add log  
Hongze Cheng 已提交
548
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
549

H
Hongze Cheng 已提交
550 551 552
      if (pReader->pIter->rowInfo.suid || pReader->pIter->rowInfo.uid) {
        // add to rbtree
        tRBTreePut(&pReader->rbt, &pReader->pIter->rbtn);
H
Hongze Cheng 已提交
553

H
Hongze Cheng 已提交
554 555 556 557 558
        // add to iterList
        pReader->pIter->next = pReader->iterList;
        pReader->iterList = pReader->pIter;
      } else {
        tsdbCloseDataIter2(pReader->pIter);
H
Hongze Cheng 已提交
559
      }
H
Hongze Cheng 已提交
560
    }
H
Hongze Cheng 已提交
561
  }
H
Hongze Cheng 已提交
562

H
Hongze Cheng 已提交
563 564
  pReader->pIter = NULL;

H
add log  
Hongze Cheng 已提交
565 566
_exit:
  if (code) {
H
Hongze Cheng 已提交
567
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pReader->pTsdb->pVnode), __func__, lino, tstrerror(code));
H
add log  
Hongze Cheng 已提交
568
  } else {
H
Hongze Cheng 已提交
569
    tsdbInfo("vgId:%d %s done, fid:%d", TD_VID(pReader->pTsdb->pVnode), __func__, pReader->fid);
H
add log  
Hongze Cheng 已提交
570
  }
H
Hongze Cheng 已提交
571 572 573
  return code;
}

H
Hongze Cheng 已提交
574 575 576 577 578 579
static void tsdbSnapReadFileDataEnd(STsdbSnapReader* pReader) {
  while (pReader->iterList) {
    STsdbDataIter2* pIter = pReader->iterList;
    pReader->iterList = pIter->next;
    tsdbCloseDataIter2(pIter);
  }
H
Hongze Cheng 已提交
580

H
Hongze Cheng 已提交
581 582
  tsdbDataFReaderClose(&pReader->pDataFReader);
}
H
Hongze Cheng 已提交
583

H
Hongze Cheng 已提交
584 585 586
static int32_t tsdbSnapReadNextRow(STsdbSnapReader* pReader, SRowInfo** ppRowInfo) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
587

H
Hongze Cheng 已提交
588 589 590 591 592
  if (pReader->pIter) {
    code = tsdbDataIterNext2(pReader->pIter, &(STsdbFilterInfo){.flag = TSDB_FILTER_FLAG_BY_VERSION,  // flag
                                                                .sver = pReader->sver,
                                                                .ever = pReader->ever});
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
593

H
Hongze Cheng 已提交
594 595 596 597 598 599 600 601 602 603 604
    if (pReader->pIter->rowInfo.suid == 0 && pReader->pIter->rowInfo.uid == 0) {
      pReader->pIter = NULL;
    } else {
      SRBTreeNode* pNode = tRBTreeMin(&pReader->rbt);
      if (pNode) {
        int32_t c = tsdbDataIterCmprFn(&pReader->pIter->rbtn, pNode);
        if (c > 0) {
          tRBTreePut(&pReader->rbt, &pReader->pIter->rbtn);
          pReader->pIter = NULL;
        } else if (c == 0) {
          ASSERT(0);
H
Hongze Cheng 已提交
605
        }
H
Hongze Cheng 已提交
606 607
      }
    }
H
Hongze Cheng 已提交
608
  }
H
Hongze Cheng 已提交
609

H
Hongze Cheng 已提交
610 611 612 613 614
  if (pReader->pIter == NULL) {
    SRBTreeNode* pNode = tRBTreeMin(&pReader->rbt);
    if (pNode) {
      tRBTreeDrop(&pReader->rbt, pNode);
      pReader->pIter = TSDB_RBTN_TO_DATA_ITER(pNode);
H
Hongze Cheng 已提交
615 616 617
    }
  }

H
Hongze Cheng 已提交
618
  if (ppRowInfo) {
H
Hongze Cheng 已提交
619
    if (pReader->pIter) {
H
Hongze Cheng 已提交
620 621 622
      *ppRowInfo = &pReader->pIter->rowInfo;
    } else {
      *ppRowInfo = NULL;
H
Hongze Cheng 已提交
623 624 625
    }
  }

H
Hongze Cheng 已提交
626 627 628 629
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pReader->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
H
Hongze Cheng 已提交
630 631 632
  return code;
}

H
Hongze Cheng 已提交
633
static int32_t tsdbSnapReadGetRow(STsdbSnapReader* pReader, SRowInfo** ppRowInfo) {
H
Hongze Cheng 已提交
634
  if (pReader->pIter) {
H
Hongze Cheng 已提交
635 636
    *ppRowInfo = &pReader->pIter->rowInfo;
    return 0;
H
Hongze Cheng 已提交
637
  }
H
Hongze Cheng 已提交
638 639

  return tsdbSnapReadNextRow(pReader, ppRowInfo);
H
Hongze Cheng 已提交
640 641
}

H
Hongze Cheng 已提交
642 643 644
static int32_t tsdbSnapCmprData(STsdbSnapReader* pReader, uint8_t** ppData) {
  int32_t code = 0;

645
  ASSERT(pReader->bData.nRow);
H
Hongze Cheng 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674

  int32_t aBufN[5] = {0};
  code = tCmprBlockData(&pReader->bData, TWO_STAGE_COMP, NULL, NULL, pReader->aBuf, aBufN);
  if (code) goto _exit;

  int32_t size = aBufN[0] + aBufN[1] + aBufN[2] + aBufN[3];
  *ppData = taosMemoryMalloc(sizeof(SSnapDataHdr) + size);
  if (*ppData == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  SSnapDataHdr* pHdr = (SSnapDataHdr*)*ppData;
  pHdr->type = SNAP_DATA_TSDB;
  pHdr->size = size;

  memcpy(pHdr->data, pReader->aBuf[3], aBufN[3]);
  memcpy(pHdr->data + aBufN[3], pReader->aBuf[2], aBufN[2]);
  if (aBufN[1]) {
    memcpy(pHdr->data + aBufN[3] + aBufN[2], pReader->aBuf[1], aBufN[1]);
  }
  if (aBufN[0]) {
    memcpy(pHdr->data + aBufN[3] + aBufN[2] + aBufN[1], pReader->aBuf[0], aBufN[0]);
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
675
static int32_t tsdbSnapReadTimeSeriesData(STsdbSnapReader* pReader, uint8_t** ppData) {
H
Hongze Cheng 已提交
676
  int32_t code = 0;
H
add log  
Hongze Cheng 已提交
677 678 679
  int32_t lino = 0;

  STsdb* pTsdb = pReader->pTsdb;
H
Hongze Cheng 已提交
680

H
Hongze Cheng 已提交
681
  tBlockDataReset(&pReader->bData);
H
Hongze Cheng 已提交
682 683 684

  for (;;) {
    // start a new file read if need
H
Hongze Cheng 已提交
685
    if (pReader->pDataFReader == NULL) {
H
Hongze Cheng 已提交
686
      code = tsdbSnapReadFileDataStart(pReader);
H
add log  
Hongze Cheng 已提交
687
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
688 689 690 691
    }

    if (pReader->pDataFReader == NULL) break;

H
Hongze Cheng 已提交
692 693 694 695
    SRowInfo* pRowInfo;
    code = tsdbSnapReadGetRow(pReader, &pRowInfo);
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
696
    if (pRowInfo == NULL) {
H
Hongze Cheng 已提交
697
      tsdbSnapReadFileDataEnd(pReader);
H
Hongze Cheng 已提交
698 699 700
      continue;
    }

H
Hongze Cheng 已提交
701
    code = tsdbUpdateTableSchema(pTsdb->pVnode->pMeta, pRowInfo->suid, pRowInfo->uid, &pReader->skmTable);
H
add log  
Hongze Cheng 已提交
702
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
703

H
Hongze Cheng 已提交
704
    code = tBlockDataInit(&pReader->bData, (TABLEID*)pRowInfo, pReader->skmTable.pTSchema, NULL, 0);
H
add log  
Hongze Cheng 已提交
705
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
706

H
Hongze Cheng 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720
    do {
      if (!TABLE_SAME_SCHEMA(pReader->bData.suid, pReader->bData.uid, pRowInfo->suid, pRowInfo->uid)) break;

      if (pReader->bData.uid && pReader->bData.uid != pRowInfo->uid) {
        code = tRealloc((uint8_t**)&pReader->bData.aUid, sizeof(int64_t) * (pReader->bData.nRow + 1));
        TSDB_CHECK_CODE(code, lino, _exit);

        for (int32_t iRow = 0; iRow < pReader->bData.nRow; ++iRow) {
          pReader->bData.aUid[iRow] = pReader->bData.uid;
        }
        pReader->bData.uid = 0;
      }

      code = tBlockDataAppendRow(&pReader->bData, &pRowInfo->row, NULL, pRowInfo->uid);
H
add log  
Hongze Cheng 已提交
721
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
722

H
Hongze Cheng 已提交
723
      code = tsdbSnapReadNextRow(pReader, &pRowInfo);
H
add log  
Hongze Cheng 已提交
724
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
725

H
Hongze Cheng 已提交
726 727
      if (pReader->bData.nRow >= 4096) break;
    } while (pRowInfo);
H
Hongze Cheng 已提交
728

H
Hongze Cheng 已提交
729 730 731 732
    ASSERT(pReader->bData.nRow > 0);

    break;
  }
H
Hongze Cheng 已提交
733

H
Hongze Cheng 已提交
734
  if (pReader->bData.nRow > 0) {
H
Hongze Cheng 已提交
735
    code = tsdbSnapCmprData(pReader, ppData);
H
add log  
Hongze Cheng 已提交
736
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
737 738
  }

H
add log  
Hongze Cheng 已提交
739 740
_exit:
  if (code) {
H
Hongze Cheng 已提交
741
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
add log  
Hongze Cheng 已提交
742
  }
H
Hongze Cheng 已提交
743 744 745
  return code;
}

H
Hongze Cheng 已提交
746
static int32_t tsdbSnapCmprTombData(STsdbSnapReader* pReader, uint8_t** ppData) {
H
add log  
Hongze Cheng 已提交
747 748 749
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
750 751 752 753
  int64_t size = sizeof(TABLEID);
  for (int32_t iDelData = 0; iDelData < taosArrayGetSize(pReader->aDelData); ++iDelData) {
    size += tPutDelData(NULL, taosArrayGet(pReader->aDelData, iDelData));
  }
H
Hongze Cheng 已提交
754

H
Hongze Cheng 已提交
755 756 757
  uint8_t* pData = (uint8_t*)taosMemoryMalloc(sizeof(SSnapDataHdr) + size);
  if (pData == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
add log  
Hongze Cheng 已提交
758
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
759
  }
H
Hongze Cheng 已提交
760

H
Hongze Cheng 已提交
761 762 763 764 765 766
  SSnapDataHdr* pHdr = (SSnapDataHdr*)pData;
  pHdr->type = SNAP_DATA_DEL;
  pHdr->size = size;

  TABLEID* pId = (TABLEID*)(pData + sizeof(SSnapDataHdr));
  *pId = pReader->tbid;
H
Hongze Cheng 已提交
767

H
Hongze Cheng 已提交
768 769 770
  size = sizeof(SSnapDataHdr) + sizeof(TABLEID);
  for (int32_t iDelData = 0; iDelData < taosArrayGetSize(pReader->aDelData); ++iDelData) {
    size += tPutDelData(pData + size, taosArrayGet(pReader->aDelData, iDelData));
H
Hongze Cheng 已提交
771 772
  }

H
Hongze Cheng 已提交
773 774 775 776 777 778
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pReader->pTsdb->pVnode), __func__, lino, tstrerror(code));
    if (pData) {
      taosMemoryFree(pData);
      pData = NULL;
H
Hongze Cheng 已提交
779
    }
H
Hongze Cheng 已提交
780 781 782 783
  }
  *ppData = pData;
  return code;
}
H
Hongze Cheng 已提交
784

H
Hongze Cheng 已提交
785 786 787 788 789
static void tsdbSnapReadGetTombData(STsdbSnapReader* pReader, SDelInfo** ppDelInfo) {
  if (pReader->pTIter == NULL || (pReader->pTIter->delInfo.suid == 0 && pReader->pTIter->delInfo.uid == 0)) {
    *ppDelInfo = NULL;
  } else {
    *ppDelInfo = &pReader->pTIter->delInfo;
H
Hongze Cheng 已提交
790 791
  }
}
H
Hongze Cheng 已提交
792

H
Hongze Cheng 已提交
793
static int32_t tsdbSnapReadNextTombData(STsdbSnapReader* pReader, SDelInfo** ppDelInfo) {
H
Hongze Cheng 已提交
794 795
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
796 797 798 799 800 801 802 803 804 805

  code = tsdbDataIterNext2(
      pReader->pTIter,
      &(STsdbFilterInfo){.flag = TSDB_FILTER_FLAG_BY_VERSION, .sver = pReader->sver, .ever = pReader->ever});
  TSDB_CHECK_CODE(code, lino, _exit);

  if (ppDelInfo) {
    tsdbSnapReadGetTombData(pReader, ppDelInfo);
  }

H
Hongze Cheng 已提交
806 807 808 809 810 811
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pReader->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
  return code;
}
H
Hongze Cheng 已提交
812

H
Hongze Cheng 已提交
813 814 815
static int32_t tsdbSnapReadTombData(STsdbSnapReader* pReader, uint8_t** ppData) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
816

H
Hongze Cheng 已提交
817
  STsdb* pTsdb = pReader->pTsdb;
H
Hongze Cheng 已提交
818

H
Hongze Cheng 已提交
819
  // open tombstone data iter if need
H
Hongze Cheng 已提交
820 821 822 823 824 825 826 827 828
  if (pReader->pDelFReader == NULL) {
    if (pReader->fs.pDelFile == NULL) goto _exit;

    // open
    code = tsdbDelFReaderOpen(&pReader->pDelFReader, pReader->fs.pDelFile, pTsdb);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = tsdbOpenTombFileDataIter(pReader->pDelFReader, &pReader->pTIter);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
829 830 831 832 833

    if (pReader->pTIter) {
      code = tsdbSnapReadNextTombData(pReader, NULL);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
834
  }
H
Hongze Cheng 已提交
835

H
Hongze Cheng 已提交
836 837 838
  // loop to get tombstone data
  SDelInfo* pDelInfo;
  tsdbSnapReadGetTombData(pReader, &pDelInfo);
H
Hongze Cheng 已提交
839

H
Hongze Cheng 已提交
840
  if (pDelInfo == NULL) goto _exit;
H
Hongze Cheng 已提交
841

H
Hongze Cheng 已提交
842
  pReader->tbid = *(TABLEID*)pDelInfo;
H
Hongze Cheng 已提交
843

H
Hongze Cheng 已提交
844 845 846 847 848 849 850 851
  if (pReader->aDelData) {
    taosArrayClear(pReader->aDelData);
  } else if ((pReader->aDelData = taosArrayInit(16, sizeof(SDelData))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  while (pDelInfo && pDelInfo->suid == pReader->tbid.suid && pDelInfo->uid == pReader->tbid.uid) {
H
Hongze Cheng 已提交
852
    if (taosArrayPush(pReader->aDelData, &pDelInfo->delData) < 0) {
H
Hongze Cheng 已提交
853 854
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
855 856
    }

H
Hongze Cheng 已提交
857 858 859
    code = tsdbSnapReadNextTombData(pReader, &pDelInfo);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
860

H
Hongze Cheng 已提交
861
  // encode tombstone data
H
Hongze Cheng 已提交
862 863 864
  if (taosArrayGetSize(pReader->aDelData) > 0) {
    code = tsdbSnapCmprTombData(pReader, ppData);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
865
  }
H
Hongze Cheng 已提交
866 867

_exit:
H
add log  
Hongze Cheng 已提交
868
  if (code) {
H
Hongze Cheng 已提交
869 870 871
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbDebug("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
H
add log  
Hongze Cheng 已提交
872
  }
H
Hongze Cheng 已提交
873 874
  return code;
}
H
more  
Hongze Cheng 已提交
875

C
Cary Xu 已提交
876
int32_t tsdbSnapReaderOpen(STsdb* pTsdb, int64_t sver, int64_t ever, int8_t type, STsdbSnapReader** ppReader) {
H
Hongze Cheng 已提交
877 878
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
879

H
more  
Hongze Cheng 已提交
880
  // alloc
H
Hongze Cheng 已提交
881
  STsdbSnapReader* pReader = (STsdbSnapReader*)taosMemoryCalloc(1, sizeof(*pReader));
H
more  
Hongze Cheng 已提交
882 883
  if (pReader == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
884
    TSDB_CHECK_CODE(code, lino, _exit);
H
more  
Hongze Cheng 已提交
885 886 887 888
  }
  pReader->pTsdb = pTsdb;
  pReader->sver = sver;
  pReader->ever = ever;
C
Cary Xu 已提交
889
  pReader->type = type;
H
more  
Hongze Cheng 已提交
890

H
Hongze Cheng 已提交
891
  taosThreadRwlockRdlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
892 893 894
  code = tsdbFSRef(pTsdb, &pReader->fs);
  if (code) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
895
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
896
  }
H
Hongze Cheng 已提交
897
  taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
898

H
Hongze Cheng 已提交
899
  // init
H
Hongze Cheng 已提交
900
  pReader->fid = INT32_MIN;
H
Hongze Cheng 已提交
901 902

  code = tBlockDataCreate(&pReader->bData);
H
Hongze Cheng 已提交
903
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
904

H
Hongze Cheng 已提交
905 906
_exit:
  if (code) {
H
Hongze Cheng 已提交
907 908
    tsdbError("vgId:%d %s failed at line %d since %s, sver:%" PRId64 " ever:%" PRId64 " type:%d", TD_VID(pTsdb->pVnode),
              __func__, lino, tstrerror(code), sver, ever, type);
H
Hongze Cheng 已提交
909 910
    if (pReader) {
      tBlockDataDestroy(&pReader->bData, 1);
H
Hongze Cheng 已提交
911
      tsdbFSUnref(pTsdb, &pReader->fs);
H
Hongze Cheng 已提交
912
      taosMemoryFree(pReader);
H
Hongze Cheng 已提交
913
      pReader = NULL;
H
Hongze Cheng 已提交
914 915
    }
  } else {
H
Hongze Cheng 已提交
916 917
    tsdbInfo("vgId:%d %s done, sver:%" PRId64 " ever:%" PRId64 " type:%d", TD_VID(pTsdb->pVnode), __func__, sver, ever,
             type);
H
Hongze Cheng 已提交
918
  }
H
Hongze Cheng 已提交
919
  *ppReader = pReader;
H
more  
Hongze Cheng 已提交
920
  return code;
H
Hongze Cheng 已提交
921 922
}

H
Hongze Cheng 已提交
923
int32_t tsdbSnapReaderClose(STsdbSnapReader** ppReader) {
H
Hongze Cheng 已提交
924 925
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
926

H
Hongze Cheng 已提交
927
  STsdbSnapReader* pReader = *ppReader;
H
Hongze Cheng 已提交
928

H
Hongze Cheng 已提交
929 930 931 932 933 934 935
  // tombstone
  if (pReader->pTIter) {
    tsdbCloseDataIter2(pReader->pTIter);
    pReader->pTIter = NULL;
  }
  if (pReader->pDelFReader) {
    tsdbDelFReaderClose(&pReader->pDelFReader);
H
Hongze Cheng 已提交
936
  }
H
Hongze Cheng 已提交
937
  taosArrayDestroy(pReader->aDelData);
H
Hongze Cheng 已提交
938

H
Hongze Cheng 已提交
939 940 941 942 943 944 945 946 947
  // timeseries
  while (pReader->iterList) {
    STsdbDataIter2* pIter = pReader->iterList;
    pReader->iterList = pIter->next;
    tsdbCloseDataIter2(pIter);
  }
  if (pReader->pDataFReader) {
    tsdbDataFReaderClose(&pReader->pDataFReader);
  }
H
Hongze Cheng 已提交
948
  tBlockDataDestroy(&pReader->bData, 1);
H
Hongze Cheng 已提交
949

H
Hongze Cheng 已提交
950 951
  // other
  tDestroyTSchema(pReader->skmTable.pTSchema);
H
Hongze Cheng 已提交
952
  tsdbFSUnref(pReader->pTsdb, &pReader->fs);
H
Hongze Cheng 已提交
953 954 955
  for (int32_t iBuf = 0; iBuf < sizeof(pReader->aBuf) / sizeof(pReader->aBuf[0]); iBuf++) {
    tFree(pReader->aBuf[iBuf]);
  }
H
Hongze Cheng 已提交
956
  taosMemoryFree(pReader);
H
Hongze Cheng 已提交
957 958 959 960 961 962 963

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pReader->pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbDebug("vgId:%d %s done", TD_VID(pReader->pTsdb->pVnode), __func__);
  }
H
Hongze Cheng 已提交
964
  *ppReader = NULL;
H
Hongze Cheng 已提交
965 966 967 968
  return code;
}

int32_t tsdbSnapRead(STsdbSnapReader* pReader, uint8_t** ppData) {
H
more  
Hongze Cheng 已提交
969
  int32_t code = 0;
H
add log  
Hongze Cheng 已提交
970
  int32_t lino = 0;
H
Hongze Cheng 已提交
971

H
Hongze Cheng 已提交
972 973
  *ppData = NULL;

H
Hongze Cheng 已提交
974
  // read data file
H
Hongze Cheng 已提交
975
  if (!pReader->dataDone) {
H
Hongze Cheng 已提交
976
    code = tsdbSnapReadTimeSeriesData(pReader, ppData);
H
add log  
Hongze Cheng 已提交
977 978 979
    TSDB_CHECK_CODE(code, lino, _exit);
    if (*ppData) {
      goto _exit;
H
Hongze Cheng 已提交
980
    } else {
H
add log  
Hongze Cheng 已提交
981
      pReader->dataDone = 1;
H
Hongze Cheng 已提交
982 983
    }
  }
H
Hongze Cheng 已提交
984 985

  // read del file
H
Hongze Cheng 已提交
986
  if (!pReader->delDone) {
H
Hongze Cheng 已提交
987
    code = tsdbSnapReadTombData(pReader, ppData);
H
add log  
Hongze Cheng 已提交
988 989 990
    TSDB_CHECK_CODE(code, lino, _exit);
    if (*ppData) {
      goto _exit;
H
Hongze Cheng 已提交
991
    } else {
H
add log  
Hongze Cheng 已提交
992
      pReader->delDone = 1;
H
Hongze Cheng 已提交
993 994
    }
  }
H
Hongze Cheng 已提交
995

H
Hongze Cheng 已提交
996
_exit:
H
add log  
Hongze Cheng 已提交
997
  if (code) {
H
Hongze Cheng 已提交
998
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pReader->pTsdb->pVnode), __func__, lino, tstrerror(code));
H
add log  
Hongze Cheng 已提交
999
  } else {
H
Hongze Cheng 已提交
1000
    tsdbDebug("vgId:%d %s done", TD_VID(pReader->pTsdb->pVnode), __func__);
H
add log  
Hongze Cheng 已提交
1001
  }
H
more  
Hongze Cheng 已提交
1002 1003 1004
  return code;
}

H
Hongze Cheng 已提交
1005
// STsdbSnapWriter ========================================
H
Hongze Cheng 已提交
1006
struct STsdbSnapWriter {
H
Hongze Cheng 已提交
1007 1008 1009
  STsdb*   pTsdb;
  int64_t  sver;
  int64_t  ever;
H
Hongze Cheng 已提交
1010 1011 1012 1013 1014 1015
  int32_t  minutes;
  int8_t   precision;
  int32_t  minRow;
  int32_t  maxRow;
  int8_t   cmprAlg;
  int64_t  commitID;
H
Hongze Cheng 已提交
1016
  uint8_t* aBuf[5];
H
Hongze Cheng 已提交
1017

H
Hongze Cheng 已提交
1018
  STsdbFS fs;
H
Hongze Cheng 已提交
1019
  TABLEID tbid;
H
Hongze Cheng 已提交
1020

H
Hongze Cheng 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
  // time-series data
  SBlockData inData;

  int32_t  fid;
  SSkmInfo skmTable;

  /* reader */
  SDataFReader*   pDataFReader;
  STsdbDataIter2* iterList;
  STsdbDataIter2* pDIter;
H
Hongze Cheng 已提交
1031
  STsdbDataIter2* pSIter;
H
Hongze Cheng 已提交
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
  SRBTree         rbt;  // SRBTree<STsdbDataIter2>

  /* writer */
  SDataFWriter* pDataFWriter;
  SArray*       aBlockIdx;
  SMapData      mDataBlk;  // SMapData<SDataBlk>
  SArray*       aSttBlk;   // SArray<SSttBlk>
  SBlockData    bData;
  SBlockData    sData;

  // tombstone data
H
Hongze Cheng 已提交
1043 1044 1045 1046 1047
  /* reader */
  SDelFReader*    pDelFReader;
  STsdbDataIter2* pTIter;

  /* writer */
H
Hongze Cheng 已提交
1048
  SDelFWriter* pDelFWriter;
H
Hongze Cheng 已提交
1049
  SArray*      aDelIdx;
H
Hongze Cheng 已提交
1050
  SArray*      aDelData;
H
Hongze Cheng 已提交
1051 1052
};

H
Hongze Cheng 已提交
1053
// SNAP_DATA_TSDB
H
Hongze Cheng 已提交
1054 1055
static int32_t tsdbSnapWriteTableDataStart(STsdbSnapWriter* pWriter, TABLEID* pId) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1056 1057
  int32_t lino = 0;

H
Hongze Cheng 已提交
1058 1059 1060 1061 1062 1063
  if (pId) {
    pWriter->tbid = *pId;
  } else {
    pWriter->tbid = (TABLEID){INT64_MAX, INT64_MAX};
  }

H
Hongze Cheng 已提交
1064 1065
  if (pWriter->pDIter) {
    STsdbDataIter2* pIter = pWriter->pDIter;
H
Hongze Cheng 已提交
1066

H
Hongze Cheng 已提交
1067
    // assert last table data end
H
Hongze Cheng 已提交
1068 1069 1070
    ASSERT(pIter->dIter.iRow >= pIter->dIter.bData.nRow);
    ASSERT(pIter->dIter.iDataBlk >= pIter->dIter.mDataBlk.nItem);

H
Hongze Cheng 已提交
1071
    for (;;) {
H
Hongze Cheng 已提交
1072
      if (pIter->dIter.iBlockIdx >= taosArrayGetSize(pIter->dIter.aBlockIdx)) {
H
Hongze Cheng 已提交
1073 1074 1075 1076
        pWriter->pDIter = NULL;
        break;
      }

H
Hongze Cheng 已提交
1077
      SBlockIdx* pBlockIdx = (SBlockIdx*)taosArrayGet(pIter->dIter.aBlockIdx, pIter->dIter.iBlockIdx);
H
Hongze Cheng 已提交
1078

H
Hongze Cheng 已提交
1079
      int32_t c = tTABLEIDCmprFn(pBlockIdx, &pWriter->tbid);
H
Hongze Cheng 已提交
1080 1081 1082 1083 1084 1085
      if (c < 0) {
        code = tsdbReadDataBlk(pIter->dIter.pReader, pBlockIdx, &pIter->dIter.mDataBlk);
        TSDB_CHECK_CODE(code, lino, _exit);

        SBlockIdx* pNewBlockIdx = taosArrayReserve(pWriter->aBlockIdx, 1);
        if (pNewBlockIdx == NULL) {
H
Hongze Cheng 已提交
1086
          code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1087 1088 1089 1090 1091 1092
          TSDB_CHECK_CODE(code, lino, _exit);
        }

        pNewBlockIdx->suid = pBlockIdx->suid;
        pNewBlockIdx->uid = pBlockIdx->uid;

H
Hongze Cheng 已提交
1093
        code = tsdbWriteDataBlk(pWriter->pDataFWriter, &pIter->dIter.mDataBlk, pNewBlockIdx);
H
Hongze Cheng 已提交
1094 1095
        TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1096 1097
        pIter->dIter.iBlockIdx++;
      } else if (c == 0) {
H
Hongze Cheng 已提交
1098 1099 1100
        code = tsdbReadDataBlk(pIter->dIter.pReader, pBlockIdx, &pIter->dIter.mDataBlk);
        TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1101
        pIter->dIter.iDataBlk = 0;
H
Hongze Cheng 已提交
1102
        pIter->dIter.iBlockIdx++;
H
Hongze Cheng 已提交
1103 1104 1105

        break;
      } else {
H
Hongze Cheng 已提交
1106
        pIter->dIter.iDataBlk = pIter->dIter.mDataBlk.nItem;
H
Hongze Cheng 已提交
1107 1108 1109 1110
        break;
      }
    }
  }
H
Hongze Cheng 已提交
1111

H
Hongze Cheng 已提交
1112
  if (pId) {
H
Hongze Cheng 已提交
1113 1114
    code = tsdbUpdateTableSchema(pWriter->pTsdb->pVnode->pMeta, pId->suid, pId->uid, &pWriter->skmTable);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1115

H
Hongze Cheng 已提交
1116
    tMapDataReset(&pWriter->mDataBlk);
H
Hongze Cheng 已提交
1117

H
Hongze Cheng 已提交
1118 1119
    code = tBlockDataInit(&pWriter->bData, pId, pWriter->skmTable.pTSchema, NULL, 0);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1120
  }
H
Hongze Cheng 已提交
1121

H
Hongze Cheng 已提交
1122 1123 1124 1125 1126 1127
  if (!TABLE_SAME_SCHEMA(pWriter->tbid.suid, pWriter->tbid.uid, pWriter->sData.suid, pWriter->sData.uid)) {
    if ((pWriter->sData.nRow > 0)) {
      code = tsdbWriteSttBlock(pWriter->pDataFWriter, &pWriter->sData, pWriter->aSttBlk, pWriter->cmprAlg);
      TSDB_CHECK_CODE(code, lino, _exit);
    }

H
Hongze Cheng 已提交
1128 1129 1130 1131 1132
    if (pId) {
      TABLEID id = {.suid = pWriter->tbid.suid, .uid = pWriter->tbid.suid ? 0 : pWriter->tbid.uid};
      code = tBlockDataInit(&pWriter->sData, &id, pWriter->skmTable.pTSchema, NULL, 0);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
1133
  }
H
Hongze Cheng 已提交
1134

H
Hongze Cheng 已提交
1135 1136 1137
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1138
  } else {
H
Hongze Cheng 已提交
1139 1140
    tsdbTrace("vgId:%d %s done, suid:%" PRId64 " uid:%" PRId64, TD_VID(pWriter->pTsdb->pVnode), __func__,
              pWriter->tbid.suid, pWriter->tbid.uid);
H
Hongze Cheng 已提交
1141
  }
H
Hongze Cheng 已提交
1142 1143 1144
  return code;
}

H
Hongze Cheng 已提交
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
static int32_t tsdbSnapWriteTableRowImpl(STsdbSnapWriter* pWriter, TSDBROW* pRow) {
  int32_t code = 0;
  int32_t lino = 0;

  code = tBlockDataAppendRow(&pWriter->bData, pRow, pWriter->skmTable.pTSchema, pWriter->tbid.uid);
  TSDB_CHECK_CODE(code, lino, _exit);

  if (pWriter->bData.nRow >= pWriter->maxRow) {
    code = tsdbWriteDataBlock(pWriter->pDataFWriter, &pWriter->bData, &pWriter->mDataBlk, pWriter->cmprAlg);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
  return code;
}

static int32_t tsdbSnapWriteTableRow(STsdbSnapWriter* pWriter, TSDBROW* pRow) {
  int32_t code = 0;
  int32_t lino = 0;

  TSDBKEY inKey = pRow ? TSDBROW_KEY(pRow) : TSDBKEY_MAX;

  if (pWriter->pDIter == NULL || (pWriter->pDIter->dIter.iRow >= pWriter->pDIter->dIter.bData.nRow &&
                                  pWriter->pDIter->dIter.iDataBlk >= pWriter->pDIter->dIter.mDataBlk.nItem)) {
    goto _write_row;
  } else {
    for (;;) {
      while (pWriter->pDIter->dIter.iRow < pWriter->pDIter->dIter.bData.nRow) {
        TSDBROW row = tsdbRowFromBlockData(&pWriter->pDIter->dIter.bData, pWriter->pDIter->dIter.iRow);

        int32_t c = tsdbKeyCmprFn(&inKey, &TSDBROW_KEY(&row));
        if (c < 0) {
          goto _write_row;
        } else if (c > 0) {
          code = tsdbSnapWriteTableRowImpl(pWriter, &row);
          TSDB_CHECK_CODE(code, lino, _exit);

          pWriter->pDIter->dIter.iRow++;
        } else {
          ASSERT(0);
        }
      }

      for (;;) {
        if (pWriter->pDIter->dIter.iDataBlk >= pWriter->pDIter->dIter.mDataBlk.nItem) goto _write_row;

        // FIXME: Here can be slow, use array instead
        SDataBlk dataBlk;
        tMapDataGetItemByIdx(&pWriter->pDIter->dIter.mDataBlk, pWriter->pDIter->dIter.iDataBlk, &dataBlk, tGetDataBlk);

        int32_t c = tDataBlkCmprFn(&dataBlk, &(SDataBlk){.minKey = inKey, .maxKey = inKey});
        if (c > 0) {
          goto _write_row;
        } else if (c < 0) {
          if (pWriter->bData.nRow > 0) {
            code = tsdbWriteDataBlock(pWriter->pDataFWriter, &pWriter->bData, &pWriter->mDataBlk, pWriter->cmprAlg);
            TSDB_CHECK_CODE(code, lino, _exit);
          }

          tMapDataPutItem(&pWriter->pDIter->dIter.mDataBlk, &dataBlk, tPutDataBlk);
          pWriter->pDIter->dIter.iDataBlk++;
        } else {
          code = tsdbReadDataBlockEx(pWriter->pDataFReader, &dataBlk, &pWriter->pDIter->dIter.bData);
          TSDB_CHECK_CODE(code, lino, _exit);

          pWriter->pDIter->dIter.iRow = 0;
          pWriter->pDIter->dIter.iDataBlk++;
          break;
        }
      }
    }
  }

_write_row:
  if (pRow) {
    code = tsdbSnapWriteTableRowImpl(pWriter, pRow);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
  return code;
}

H
Hongze Cheng 已提交
1234 1235
static int32_t tsdbSnapWriteTableDataEnd(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1236
  int32_t lino = 0;
H
Hongze Cheng 已提交
1237

H
Hongze Cheng 已提交
1238 1239 1240
  // write a NULL row to end current table data write
  code = tsdbSnapWriteTableRow(pWriter, NULL);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1241

H
Hongze Cheng 已提交
1242 1243
  if (pWriter->bData.nRow > 0) {
    if (pWriter->bData.nRow < pWriter->minRow) {
H
Hongze Cheng 已提交
1244
      ASSERT(TABLE_SAME_SCHEMA(pWriter->sData.suid, pWriter->sData.uid, pWriter->tbid.suid, pWriter->tbid.uid));
H
Hongze Cheng 已提交
1245
      for (int32_t iRow = 0; iRow < pWriter->bData.nRow; iRow++) {
H
Hongze Cheng 已提交
1246 1247
        code =
            tBlockDataAppendRow(&pWriter->sData, &tsdbRowFromBlockData(&pWriter->bData, iRow), NULL, pWriter->tbid.uid);
H
Hongze Cheng 已提交
1248
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1249

H
Hongze Cheng 已提交
1250 1251 1252 1253
        if (pWriter->sData.nRow >= pWriter->maxRow) {
          code = tsdbWriteSttBlock(pWriter->pDataFWriter, &pWriter->sData, pWriter->aSttBlk, pWriter->cmprAlg);
          TSDB_CHECK_CODE(code, lino, _exit);
        }
H
Hongze Cheng 已提交
1254 1255
      }

H
Hongze Cheng 已提交
1256
      tBlockDataClear(&pWriter->bData);
H
Hongze Cheng 已提交
1257 1258 1259
    } else {
      code = tsdbWriteDataBlock(pWriter->pDataFWriter, &pWriter->bData, &pWriter->mDataBlk, pWriter->cmprAlg);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1260 1261 1262
    }
  }

H
Hongze Cheng 已提交
1263 1264 1265
  if (pWriter->mDataBlk.nItem) {
    SBlockIdx* pBlockIdx = taosArrayReserve(pWriter->aBlockIdx, 1);
    if (pBlockIdx == NULL) {
H
Hongze Cheng 已提交
1266
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1267
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1268 1269
    }

H
Hongze Cheng 已提交
1270 1271 1272
    pBlockIdx->suid = pWriter->tbid.suid;
    pBlockIdx->uid = pWriter->tbid.uid;

H
Hongze Cheng 已提交
1273 1274 1275
    code = tsdbWriteDataBlk(pWriter->pDataFWriter, &pWriter->mDataBlk, pBlockIdx);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
1276

H
Hongze Cheng 已提交
1277 1278 1279 1280
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
H
Hongze Cheng 已提交
1281 1282 1283
  return code;
}

H
Hongze Cheng 已提交
1284
static int32_t tsdbSnapWriteFileDataStart(STsdbSnapWriter* pWriter, int32_t fid) {
H
Hongze Cheng 已提交
1285
  int32_t code = 0;
H
Hongze Cheng 已提交
1286 1287 1288
  int32_t lino = 0;

  ASSERT(pWriter->pDataFWriter == NULL && pWriter->fid < fid);
H
Hongze Cheng 已提交
1289

H
Hongze Cheng 已提交
1290
  STsdb* pTsdb = pWriter->pTsdb;
H
Hongze Cheng 已提交
1291 1292

  pWriter->fid = fid;
H
Hongze Cheng 已提交
1293
  pWriter->tbid = (TABLEID){0};
H
Hongze Cheng 已提交
1294 1295
  SDFileSet* pSet = taosArraySearch(pWriter->fs.aDFileSet, &(SDFileSet){.fid = fid}, tDFileSetCmprFn, TD_EQ);

H
Hongze Cheng 已提交
1296 1297 1298 1299
  // open reader
  pWriter->pDataFReader = NULL;
  pWriter->iterList = NULL;
  pWriter->pDIter = NULL;
H
Hongze Cheng 已提交
1300
  pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
1301
  tRBTreeCreate(&pWriter->rbt, tsdbDataIterCmprFn);
H
Hongze Cheng 已提交
1302
  if (pSet) {
H
Hongze Cheng 已提交
1303 1304
    code = tsdbDataFReaderOpen(&pWriter->pDataFReader, pTsdb, pSet);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1305

H
Hongze Cheng 已提交
1306 1307 1308 1309 1310 1311
    code = tsdbOpenDataFileDataIter(pWriter->pDataFReader, &pWriter->pDIter);
    TSDB_CHECK_CODE(code, lino, _exit);
    if (pWriter->pDIter) {
      pWriter->pDIter->next = pWriter->iterList;
      pWriter->iterList = pWriter->pDIter;
    }
H
Hongze Cheng 已提交
1312 1313

    for (int32_t iStt = 0; iStt < pSet->nSttF; iStt++) {
H
Hongze Cheng 已提交
1314
      code = tsdbOpenSttFileDataIter(pWriter->pDataFReader, iStt, &pWriter->pSIter);
H
Hongze Cheng 已提交
1315 1316
      TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1317
      if (pWriter->pSIter) {
H
Hongze Cheng 已提交
1318
        code = tsdbSttFileDataIterNext(pWriter->pSIter, NULL);
H
Hongze Cheng 已提交
1319 1320 1321
        TSDB_CHECK_CODE(code, lino, _exit);

        // add to tree
H
Hongze Cheng 已提交
1322
        tRBTreePut(&pWriter->rbt, &pWriter->pSIter->rbtn);
H
Hongze Cheng 已提交
1323 1324

        // add to list
H
Hongze Cheng 已提交
1325 1326
        pWriter->pSIter->next = pWriter->iterList;
        pWriter->iterList = pWriter->pSIter;
H
Hongze Cheng 已提交
1327
      }
H
Hongze Cheng 已提交
1328
    }
H
Hongze Cheng 已提交
1329

H
Hongze Cheng 已提交
1330
    pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
1331 1332 1333 1334 1335 1336
  }

  // open writer
  SDiskID diskId;
  if (pSet) {
    diskId = pSet->diskId;
H
Hongze Cheng 已提交
1337
  } else {
H
Hongze Cheng 已提交
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
    tfsAllocDisk(pTsdb->pVnode->pTfs, 0 /*TODO*/, &diskId);
    tfsMkdirRecurAt(pTsdb->pVnode->pTfs, pTsdb->path, diskId);
  }
  SDFileSet wSet = {.diskId = diskId,
                    .fid = fid,
                    .pHeadF = &(SHeadFile){.commitID = pWriter->commitID},
                    .pDataF = (pSet) ? pSet->pDataF : &(SDataFile){.commitID = pWriter->commitID},
                    .pSmaF = (pSet) ? pSet->pSmaF : &(SSmaFile){.commitID = pWriter->commitID},
                    .nSttF = 1,
                    .aSttF = {&(SSttFile){.commitID = pWriter->commitID}}};
  code = tsdbDataFWriterOpen(&pWriter->pDataFWriter, pTsdb, &wSet);
  TSDB_CHECK_CODE(code, lino, _exit);

  if (pWriter->aBlockIdx) {
    taosArrayClear(pWriter->aBlockIdx);
  } else if ((pWriter->aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1356
  }
H
Hongze Cheng 已提交
1357

H
Hongze Cheng 已提交
1358
  tMapDataReset(&pWriter->mDataBlk);
H
Hongze Cheng 已提交
1359

H
Hongze Cheng 已提交
1360 1361 1362 1363 1364 1365
  if (pWriter->aSttBlk) {
    taosArrayClear(pWriter->aSttBlk);
  } else if ((pWriter->aSttBlk = taosArrayInit(0, sizeof(SSttBlk))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
1366

H
Hongze Cheng 已提交
1367
  tBlockDataReset(&pWriter->bData);
H
Hongze Cheng 已提交
1368
  tBlockDataReset(&pWriter->sData);
H
Hongze Cheng 已提交
1369 1370 1371 1372 1373 1374 1375 1376

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s, fid:%d", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code),
              fid);
  } else {
    tsdbDebug("vgId:%d %s done, fid:%d", TD_VID(pTsdb->pVnode), __func__, fid);
  }
H
Hongze Cheng 已提交
1377 1378 1379
  return code;
}

H
Hongze Cheng 已提交
1380 1381 1382 1383 1384 1385
static int32_t tsdbSnapWriteTableData(STsdbSnapWriter* pWriter, SRowInfo* pRowInfo) {
  int32_t code = 0;
  int32_t lino = 0;

  // switch to new table if need
  if (pRowInfo == NULL || pRowInfo->uid != pWriter->tbid.uid) {
H
Hongze Cheng 已提交
1386
    if (pWriter->tbid.uid) {
H
Hongze Cheng 已提交
1387 1388 1389 1390 1391 1392 1393 1394
      code = tsdbSnapWriteTableDataEnd(pWriter);
      TSDB_CHECK_CODE(code, lino, _exit);
    }

    code = tsdbSnapWriteTableDataStart(pWriter, (TABLEID*)pRowInfo);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
1395 1396 1397 1398
  if (pRowInfo == NULL) goto _exit;

  code = tsdbSnapWriteTableRow(pWriter, &pRowInfo->row);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1399 1400 1401 1402 1403 1404 1405 1406

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
  return code;
}

H
Hongze Cheng 已提交
1407
static int32_t tsdbSnapWriteNextRow(STsdbSnapWriter* pWriter, SRowInfo** ppRowInfo) {
H
Hongze Cheng 已提交
1408
  int32_t code = 0;
H
Hongze Cheng 已提交
1409
  int32_t lino = 0;
H
Hongze Cheng 已提交
1410

H
Hongze Cheng 已提交
1411
  if (pWriter->pSIter) {
H
Hongze Cheng 已提交
1412
    code = tsdbDataIterNext2(pWriter->pSIter, NULL);
H
Hongze Cheng 已提交
1413
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1414

H
Hongze Cheng 已提交
1415 1416
    if (pWriter->pSIter->rowInfo.suid == 0 && pWriter->pSIter->rowInfo.uid == 0) {
      pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
1417 1418 1419
    } else {
      SRBTreeNode* pNode = tRBTreeMin(&pWriter->rbt);
      if (pNode) {
H
Hongze Cheng 已提交
1420
        int32_t c = tsdbDataIterCmprFn(&pWriter->pSIter->rbtn, pNode);
H
Hongze Cheng 已提交
1421
        if (c > 0) {
H
Hongze Cheng 已提交
1422 1423
          tRBTreePut(&pWriter->rbt, &pWriter->pSIter->rbtn);
          pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
1424 1425 1426 1427 1428
        } else if (c == 0) {
          ASSERT(0);
        }
      }
    }
H
Hongze Cheng 已提交
1429 1430
  }

H
Hongze Cheng 已提交
1431
  if (pWriter->pSIter == NULL) {
H
Hongze Cheng 已提交
1432 1433 1434
    SRBTreeNode* pNode = tRBTreeMin(&pWriter->rbt);
    if (pNode) {
      tRBTreeDrop(&pWriter->rbt, pNode);
H
Hongze Cheng 已提交
1435
      pWriter->pSIter = TSDB_RBTN_TO_DATA_ITER(pNode);
H
Hongze Cheng 已提交
1436
    }
H
Hongze Cheng 已提交
1437 1438
  }

H
Hongze Cheng 已提交
1439
  if (ppRowInfo) {
H
Hongze Cheng 已提交
1440 1441
    if (pWriter->pSIter) {
      *ppRowInfo = &pWriter->pSIter->rowInfo;
H
Hongze Cheng 已提交
1442 1443 1444
    } else {
      *ppRowInfo = NULL;
    }
H
Hongze Cheng 已提交
1445
  }
H
Hongze Cheng 已提交
1446

H
Hongze Cheng 已提交
1447 1448 1449
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1450
  }
H
Hongze Cheng 已提交
1451 1452 1453 1454 1455 1456 1457
  return code;
}

static int32_t tsdbSnapWriteGetRow(STsdbSnapWriter* pWriter, SRowInfo** ppRowInfo) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1458 1459
  if (pWriter->pSIter) {
    *ppRowInfo = &pWriter->pSIter->rowInfo;
H
Hongze Cheng 已提交
1460 1461 1462 1463 1464
    goto _exit;
  }

  code = tsdbSnapWriteNextRow(pWriter, ppRowInfo);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1465

H
Hongze Cheng 已提交
1466
_exit:
H
Hongze Cheng 已提交
1467 1468 1469
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
H
Hongze Cheng 已提交
1470
  return code;
H
Hongze Cheng 已提交
1471
}
H
Hongze Cheng 已提交
1472

H
Hongze Cheng 已提交
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
static int32_t tsdbSnapWriteFileDataEnd(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
  int32_t lino = 0;

  ASSERT(pWriter->pDataFWriter);

  // consume remain data and end with a NULL table row
  SRowInfo* pRowInfo;
  code = tsdbSnapWriteGetRow(pWriter, &pRowInfo);
  TSDB_CHECK_CODE(code, lino, _exit);
  for (;;) {
    code = tsdbSnapWriteTableData(pWriter, pRowInfo);
    TSDB_CHECK_CODE(code, lino, _exit);

    if (pRowInfo == NULL) break;

    code = tsdbSnapWriteNextRow(pWriter, &pRowInfo);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  // do file-level updates
  code = tsdbWriteSttBlk(pWriter->pDataFWriter, pWriter->aSttBlk);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbWriteBlockIdx(pWriter->pDataFWriter, pWriter->aBlockIdx);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbUpdateDFileSetHeader(pWriter->pDataFWriter);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbFSUpsertFSet(&pWriter->fs, &pWriter->pDataFWriter->wSet);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbDataFWriterClose(&pWriter->pDataFWriter, 1);
  TSDB_CHECK_CODE(code, lino, _exit);

  if (pWriter->pDataFReader) {
    code = tsdbDataFReaderClose(&pWriter->pDataFReader);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  // clear sources
  while (pWriter->iterList) {
    STsdbDataIter2* pIter = pWriter->iterList;
    pWriter->iterList = pIter->next;
    tsdbCloseDataIter2(pIter);
  }

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, tstrerror(code));
  } else {
    tsdbDebug("vgId:%d %s is done", TD_VID(pWriter->pTsdb->pVnode), __func__);
  }
  return code;
}

H
Hongze Cheng 已提交
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
static int32_t tsdbSnapWriteTimeSeriesData(STsdbSnapWriter* pWriter, SSnapDataHdr* pHdr) {
  int32_t code = 0;
  int32_t lino = 0;

  code = tDecmprBlockData(pHdr->data, pHdr->size, &pWriter->inData, pWriter->aBuf);
  TSDB_CHECK_CODE(code, lino, _exit);

  ASSERT(pWriter->inData.nRow > 0);

  // switch to new data file if need
  int32_t fid = tsdbKeyFid(pWriter->inData.aTSKEY[0], pWriter->minutes, pWriter->precision);
  if (pWriter->fid != fid) {
    if (pWriter->pDataFWriter) {
H
Hongze Cheng 已提交
1543
      code = tsdbSnapWriteFileDataEnd(pWriter);
H
Hongze Cheng 已提交
1544 1545 1546
      TSDB_CHECK_CODE(code, lino, _exit);
    }

H
Hongze Cheng 已提交
1547
    code = tsdbSnapWriteFileDataStart(pWriter, fid);
H
Hongze Cheng 已提交
1548 1549 1550 1551 1552 1553 1554
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  // loop write each row
  SRowInfo* pRowInfo;
  code = tsdbSnapWriteGetRow(pWriter, &pRowInfo);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1555
  for (int32_t iRow = 0; iRow < pWriter->inData.nRow; ++iRow) {
H
Hongze Cheng 已提交
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
    SRowInfo rInfo = {.suid = pWriter->inData.suid,
                      .uid = pWriter->inData.uid ? pWriter->inData.uid : pWriter->inData.aUid[iRow],
                      .row = tsdbRowFromBlockData(&pWriter->inData, iRow)};

    for (;;) {
      if (pRowInfo == NULL) {
        code = tsdbSnapWriteTableData(pWriter, &rInfo);
        TSDB_CHECK_CODE(code, lino, _exit);
        break;
      } else {
        int32_t c = tRowInfoCmprFn(&rInfo, pRowInfo);
        if (c < 0) {
          code = tsdbSnapWriteTableData(pWriter, &rInfo);
          TSDB_CHECK_CODE(code, lino, _exit);
          break;
        } else if (c > 0) {
          code = tsdbSnapWriteTableData(pWriter, pRowInfo);
          TSDB_CHECK_CODE(code, lino, _exit);

          code = tsdbSnapWriteNextRow(pWriter, &pRowInfo);
          TSDB_CHECK_CODE(code, lino, _exit);
        } else {
          ASSERT(0);
        }
      }
    }
  }

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbDebug("vgId:%d %s done, suid:%" PRId64 " uid:%" PRId64 " nRow:%d", TD_VID(pWriter->pTsdb->pVnode), __func__,
              pWriter->inData.suid, pWriter->inData.uid, pWriter->inData.nRow);
  }
H
Hongze Cheng 已提交
1591 1592 1593
  return code;
}

H
Hongze Cheng 已提交
1594
// SNAP_DATA_DEL
H
Hongze Cheng 已提交
1595
static int32_t tsdbSnapWriteDelTableDataStart(STsdbSnapWriter* pWriter, TABLEID* pId) {
H
Hongze Cheng 已提交
1596
  int32_t code = 0;
H
Hongze Cheng 已提交
1597
  int32_t lino = 0;
H
Hongze Cheng 已提交
1598

H
Hongze Cheng 已提交
1599 1600 1601 1602 1603
  if (pId) {
    pWriter->tbid = *pId;
  } else {
    pWriter->tbid = (TABLEID){.suid = INT64_MAX, .uid = INT64_MAX};
  }
H
Hongze Cheng 已提交
1604

H
Hongze Cheng 已提交
1605
  taosArrayClear(pWriter->aDelData);
H
Hongze Cheng 已提交
1606

H
Hongze Cheng 已提交
1607 1608
  if (pWriter->pTIter) {
    while (pWriter->pTIter->tIter.iDelIdx < taosArrayGetSize(pWriter->pTIter->tIter.aDelIdx)) {
H
Hongze Cheng 已提交
1609
      SDelIdx* pDelIdx = taosArrayGet(pWriter->pTIter->tIter.aDelIdx, pWriter->pTIter->tIter.iDelIdx);
H
Hongze Cheng 已提交
1610

H
Hongze Cheng 已提交
1611 1612 1613 1614
      int32_t c = tTABLEIDCmprFn(pDelIdx, &pWriter->tbid);
      if (c < 0) {
        code = tsdbReadDelData(pWriter->pDelFReader, pDelIdx, pWriter->pTIter->tIter.aDelData);
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1615

H
Hongze Cheng 已提交
1616
        SDelIdx* pDelIdxNew = taosArrayReserve(pWriter->aDelIdx, 1);
H
Hongze Cheng 已提交
1617 1618 1619 1620 1621 1622 1623
        if (pDelIdxNew == NULL) {
          code = TSDB_CODE_OUT_OF_MEMORY;
          TSDB_CHECK_CODE(code, lino, _exit);
        }

        pDelIdxNew->suid = pDelIdx->suid;
        pDelIdxNew->uid = pDelIdx->uid;
H
Hongze Cheng 已提交
1624

H
Hongze Cheng 已提交
1625 1626 1627 1628 1629
        code = tsdbWriteDelData(pWriter->pDelFWriter, pWriter->pTIter->tIter.aDelData, pDelIdxNew);
        TSDB_CHECK_CODE(code, lino, _exit);

        pWriter->pTIter->tIter.iDelIdx++;
      } else if (c == 0) {
H
Hongze Cheng 已提交
1630
        code = tsdbReadDelData(pWriter->pDelFReader, pDelIdx, pWriter->aDelData);
H
Hongze Cheng 已提交
1631 1632 1633 1634 1635 1636 1637 1638
        TSDB_CHECK_CODE(code, lino, _exit);

        pWriter->pTIter->tIter.iDelIdx++;
        break;
      } else {
        break;
      }
    }
H
Hongze Cheng 已提交
1639 1640 1641
  }

_exit:
H
Hongze Cheng 已提交
1642 1643 1644 1645 1646 1647
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbTrace("vgId:%d %s done, suid:%" PRId64 " uid:%" PRId64, TD_VID(pWriter->pTsdb->pVnode), __func__, pId->suid,
              pId->uid);
  }
H
Hongze Cheng 已提交
1648 1649 1650
  return code;
}

H
Hongze Cheng 已提交
1651
static int32_t tsdbSnapWriteDelTableDataEnd(STsdbSnapWriter* pWriter) {
H
Hongze Cheng 已提交
1652
  int32_t code = 0;
H
Hongze Cheng 已提交
1653
  int32_t lino = 0;
H
Hongze Cheng 已提交
1654

H
Hongze Cheng 已提交
1655 1656 1657 1658 1659 1660
  if (taosArrayGetSize(pWriter->aDelData) > 0) {
    SDelIdx* pDelIdx = taosArrayReserve(pWriter->aDelIdx, 1);
    if (pDelIdx == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
1661

H
Hongze Cheng 已提交
1662 1663
    pDelIdx->suid = pWriter->tbid.suid;
    pDelIdx->uid = pWriter->tbid.uid;
H
Hongze Cheng 已提交
1664

H
Hongze Cheng 已提交
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
    code = tsdbWriteDelData(pWriter->pDelFWriter, pWriter->aDelData, pDelIdx);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbTrace("vgId:%d %s done", TD_VID(pWriter->pTsdb->pVnode), __func__);
  }
  return code;
}

static int32_t tsdbSnapWriteDelTableData(STsdbSnapWriter* pWriter, TABLEID* pId, uint8_t* pData, int64_t size) {
  int32_t code = 0;
  int32_t lino = 0;

  if (pId == NULL || pId->uid != pWriter->tbid.uid) {
    if (pWriter->tbid.uid) {
      code = tsdbSnapWriteDelTableDataEnd(pWriter);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1686 1687
    }

H
Hongze Cheng 已提交
1688 1689
    code = tsdbSnapWriteDelTableDataStart(pWriter, pId);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1690 1691
  }

H
Hongze Cheng 已提交
1692
  if (pId == NULL) goto _exit;
H
Hongze Cheng 已提交
1693

H
Hongze Cheng 已提交
1694 1695 1696 1697
  int64_t n = 0;
  while (n < size) {
    SDelData delData;
    n += tGetDelData(pData + n, &delData);
H
Hongze Cheng 已提交
1698

H
Hongze Cheng 已提交
1699 1700 1701 1702 1703 1704
    if (taosArrayPush(pWriter->aDelData, &delData) < 0) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  }
  ASSERT(n == size);
H
Hongze Cheng 已提交
1705

H
Hongze Cheng 已提交
1706 1707 1708
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1709
  }
H
Hongze Cheng 已提交
1710 1711
  return code;
}
H
Hongze Cheng 已提交
1712

H
Hongze Cheng 已提交
1713 1714 1715
static int32_t tsdbSnapWriteDelDataStart(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1716

H
Hongze Cheng 已提交
1717 1718
  STsdb*    pTsdb = pWriter->pTsdb;
  SDelFile* pDelFile = pWriter->fs.pDelFile;
H
Hongze Cheng 已提交
1719

H
Hongze Cheng 已提交
1720 1721 1722 1723 1724 1725 1726 1727 1728
  pWriter->tbid = (TABLEID){0};

  // reader
  if (pDelFile) {
    code = tsdbDelFReaderOpen(&pWriter->pDelFReader, pDelFile, pTsdb);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = tsdbOpenTombFileDataIter(pWriter->pDelFReader, &pWriter->pTIter);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1729
  }
H
Hongze Cheng 已提交
1730

H
Hongze Cheng 已提交
1731 1732 1733
  // writer
  code = tsdbDelFWriterOpen(&pWriter->pDelFWriter, &(SDelFile){.commitID = pWriter->commitID}, pTsdb);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1734

H
Hongze Cheng 已提交
1735
  if ((pWriter->aDelIdx = taosArrayInit(0, sizeof(SDelIdx))) == NULL) {
H
Hongze Cheng 已提交
1736
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1737 1738 1739 1740 1741
    TSDB_CHECK_CODE(code, lino, _exit);
  }
  if ((pWriter->aDelData = taosArrayInit(0, sizeof(SDelData))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1742 1743
  }

H
Hongze Cheng 已提交
1744 1745 1746 1747 1748 1749
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbDebug("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
  }
H
Hongze Cheng 已提交
1750 1751 1752
  return code;
}

H
Hongze Cheng 已提交
1753
static int32_t tsdbSnapWriteDelDataEnd(STsdbSnapWriter* pWriter) {
H
Hongze Cheng 已提交
1754
  int32_t code = 0;
H
Hongze Cheng 已提交
1755
  int32_t lino = 0;
H
Hongze Cheng 已提交
1756

H
Hongze Cheng 已提交
1757
  STsdb* pTsdb = pWriter->pTsdb;
H
Hongze Cheng 已提交
1758

H
Hongze Cheng 已提交
1759
  // end remaining table with NULL data
H
Hongze Cheng 已提交
1760 1761
  code = tsdbSnapWriteDelTableData(pWriter, NULL, NULL, 0);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1762

H
Hongze Cheng 已提交
1763 1764 1765
  // update file-level info
  code = tsdbWriteDelIdx(pWriter->pDelFWriter, pWriter->aDelIdx);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1766

H
Hongze Cheng 已提交
1767
  code = tsdbUpdateDelFileHdr(pWriter->pDelFWriter);
H
Hongze Cheng 已提交
1768
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1769

H
Hongze Cheng 已提交
1770
  code = tsdbFSUpsertDelFile(&pWriter->fs, &pWriter->pDelFWriter->fDel);
H
Hongze Cheng 已提交
1771
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1772 1773

  code = tsdbDelFWriterClose(&pWriter->pDelFWriter, 1);
H
Hongze Cheng 已提交
1774
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1775 1776 1777

  if (pWriter->pDelFReader) {
    code = tsdbDelFReaderClose(&pWriter->pDelFReader);
H
Hongze Cheng 已提交
1778
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1779 1780
  }

H
Hongze Cheng 已提交
1781 1782 1783 1784 1785
  if (pWriter->pTIter) {
    tsdbCloseDataIter2(pWriter->pTIter);
    pWriter->pTIter = NULL;
  }

H
Hongze Cheng 已提交
1786 1787 1788 1789 1790 1791
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbInfo("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
  }
H
Hongze Cheng 已提交
1792
  return code;
H
Hongze Cheng 已提交
1793
}
H
Hongze Cheng 已提交
1794

H
Hongze Cheng 已提交
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
static int32_t tsdbSnapWriteDelData(STsdbSnapWriter* pWriter, SSnapDataHdr* pHdr) {
  int32_t code = 0;
  int32_t lino = 0;

  STsdb* pTsdb = pWriter->pTsdb;

  // start to write del data if need
  if (pWriter->pDelFWriter == NULL) {
    code = tsdbSnapWriteDelDataStart(pWriter);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
1807
  // do write del data
H
Hongze Cheng 已提交
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
  code = tsdbSnapWriteDelTableData(pWriter, (TABLEID*)pHdr->data, pHdr->data + sizeof(TABLEID),
                                   pHdr->size - sizeof(TABLEID));
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed since %s", TD_VID(pTsdb->pVnode), __func__, tstrerror(code));
  } else {
    tsdbTrace("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
  }
H
Hongze Cheng 已提交
1818 1819 1820
  return code;
}

H
Hongze Cheng 已提交
1821
// APIs
H
Hongze Cheng 已提交
1822
int32_t tsdbSnapWriterOpen(STsdb* pTsdb, int64_t sver, int64_t ever, STsdbSnapWriter** ppWriter) {
H
Hongze Cheng 已提交
1823 1824
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1825 1826

  // alloc
H
Hongze Cheng 已提交
1827
  STsdbSnapWriter* pWriter = (STsdbSnapWriter*)taosMemoryCalloc(1, sizeof(*pWriter));
H
Hongze Cheng 已提交
1828 1829
  if (pWriter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1830
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1831 1832 1833 1834
  }
  pWriter->pTsdb = pTsdb;
  pWriter->sver = sver;
  pWriter->ever = ever;
H
Hongze Cheng 已提交
1835 1836 1837 1838 1839
  pWriter->minutes = pTsdb->keepCfg.days;
  pWriter->precision = pTsdb->keepCfg.precision;
  pWriter->minRow = pTsdb->pVnode->config.tsdbCfg.minRows;
  pWriter->maxRow = pTsdb->pVnode->config.tsdbCfg.maxRows;
  pWriter->cmprAlg = pTsdb->pVnode->config.tsdbCfg.compression;
H
Hongze Cheng 已提交
1840 1841
  pWriter->commitID = pTsdb->pVnode->state.commitID;

H
Hongze Cheng 已提交
1842 1843 1844
  code = tsdbFSCopy(pTsdb, &pWriter->fs);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1845
  // SNAP_DATA_TSDB
H
Hongze Cheng 已提交
1846 1847 1848
  code = tBlockDataCreate(&pWriter->inData);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1849
  pWriter->fid = INT32_MIN;
H
Hongze Cheng 已提交
1850

H
Hongze Cheng 已提交
1851
  code = tBlockDataCreate(&pWriter->bData);
H
Hongze Cheng 已提交
1852
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1853 1854

  code = tBlockDataCreate(&pWriter->sData);
H
Hongze Cheng 已提交
1855
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1856

H
Hongze Cheng 已提交
1857
  // SNAP_DATA_DEL
H
Hongze Cheng 已提交
1858

H
Hongze Cheng 已提交
1859 1860
_exit:
  if (code) {
H
Hongze Cheng 已提交
1861
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1862
    if (pWriter) {
H
Hongze Cheng 已提交
1863
      tBlockDataDestroy(&pWriter->sData, 1);
H
Hongze Cheng 已提交
1864
      tBlockDataDestroy(&pWriter->bData, 1);
H
Hongze Cheng 已提交
1865
      tBlockDataDestroy(&pWriter->inData, 1);
H
Hongze Cheng 已提交
1866
      tsdbFSDestroy(&pWriter->fs);
H
Hongze Cheng 已提交
1867
      pWriter = NULL;
H
Hongze Cheng 已提交
1868 1869
    }
  } else {
H
Hongze Cheng 已提交
1870
    tsdbInfo("vgId:%d %s done, sver:%" PRId64 " ever:%" PRId64, TD_VID(pTsdb->pVnode), __func__, sver, ever);
H
Hongze Cheng 已提交
1871
  }
H
Hongze Cheng 已提交
1872
  *ppWriter = pWriter;
H
Hongze Cheng 已提交
1873 1874 1875
  return code;
}

H
Hongze Cheng 已提交
1876 1877
int32_t tsdbSnapWriterPrepareClose(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1878 1879
  int32_t lino = 0;

H
Hongze Cheng 已提交
1880
  if (pWriter->pDataFWriter) {
H
Hongze Cheng 已提交
1881
    code = tsdbSnapWriteFileDataEnd(pWriter);
H
Hongze Cheng 已提交
1882
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1883 1884
  }

H
Hongze Cheng 已提交
1885 1886 1887 1888
  if (pWriter->pDelFWriter) {
    code = tsdbSnapWriteDelDataEnd(pWriter);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
1889 1890

  code = tsdbFSPrepareCommit(pWriter->pTsdb, &pWriter->fs);
H
Hongze Cheng 已提交
1891
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1892 1893 1894

_exit:
  if (code) {
H
Hongze Cheng 已提交
1895 1896 1897
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbDebug("vgId:%d %s done", TD_VID(pWriter->pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
1898 1899 1900 1901
  }
  return code;
}

H
Hongze Cheng 已提交
1902
int32_t tsdbSnapWriterClose(STsdbSnapWriter** ppWriter, int8_t rollback) {
H
Hongze Cheng 已提交
1903
  int32_t          code = 0;
H
Hongze Cheng 已提交
1904
  STsdbSnapWriter* pWriter = *ppWriter;
H
Hongze Cheng 已提交
1905
  STsdb*           pTsdb = pWriter->pTsdb;
H
Hongze Cheng 已提交
1906 1907

  if (rollback) {
H
Hongze Cheng 已提交
1908
    tsdbRollbackCommit(pWriter->pTsdb);
H
Hongze Cheng 已提交
1909
  } else {
H
Hongze Cheng 已提交
1910 1911 1912
    // lock
    taosThreadRwlockWrlock(&pTsdb->rwLock);

H
Hongze Cheng 已提交
1913
    code = tsdbFSCommit(pWriter->pTsdb);
H
Hongze Cheng 已提交
1914 1915 1916 1917 1918 1919 1920
    if (code) {
      taosThreadRwlockUnlock(&pTsdb->rwLock);
      goto _err;
    }

    // unlock
    taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1921 1922
  }

H
Hongze Cheng 已提交
1923 1924
  // SNAP_DATA_DEL
  taosArrayDestroy(pWriter->aDelData);
H
Hongze Cheng 已提交
1925
  taosArrayDestroy(pWriter->aDelIdx);
H
Hongze Cheng 已提交
1926 1927

  // SNAP_DATA_TSDB
H
Hongze Cheng 已提交
1928
  tBlockDataDestroy(&pWriter->sData, 1);
H
Hongze Cheng 已提交
1929
  tBlockDataDestroy(&pWriter->bData, 1);
H
Hongze Cheng 已提交
1930 1931 1932
  taosArrayDestroy(pWriter->aSttBlk);
  tMapDataClear(&pWriter->mDataBlk);
  taosArrayDestroy(pWriter->aBlockIdx);
1933
  tDestroyTSchema(pWriter->skmTable.pTSchema);
H
Hongze Cheng 已提交
1934
  tBlockDataDestroy(&pWriter->inData, 1);
H
Hongze Cheng 已提交
1935

H
Hongze Cheng 已提交
1936 1937 1938
  for (int32_t iBuf = 0; iBuf < sizeof(pWriter->aBuf) / sizeof(uint8_t*); iBuf++) {
    tFree(pWriter->aBuf[iBuf]);
  }
S
Shengliang Guan 已提交
1939
  tsdbInfo("vgId:%d, %s done", TD_VID(pWriter->pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
1940 1941 1942 1943 1944
  taosMemoryFree(pWriter);
  *ppWriter = NULL;
  return code;

_err:
S
Shengliang Guan 已提交
1945
  tsdbError("vgId:%d, vnode snapshot tsdb writer close for %s failed since %s", TD_VID(pWriter->pTsdb->pVnode),
C
Cary Xu 已提交
1946 1947 1948
            pWriter->pTsdb->path, tstrerror(code));
  taosMemoryFree(pWriter);
  *ppWriter = NULL;
H
Hongze Cheng 已提交
1949 1950 1951
  return code;
}

H
Hongze Cheng 已提交
1952 1953 1954
int32_t tsdbSnapWrite(STsdbSnapWriter* pWriter, SSnapDataHdr* pHdr) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1955

C
Cary Xu 已提交
1956
  if (pHdr->type == SNAP_DATA_TSDB) {
H
Hongze Cheng 已提交
1957 1958
    code = tsdbSnapWriteTimeSeriesData(pWriter, pHdr);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1959
    goto _exit;
H
Hongze Cheng 已提交
1960
  } else if (pWriter->pDataFWriter) {
H
Hongze Cheng 已提交
1961
    code = tsdbSnapWriteFileDataEnd(pWriter);
H
Hongze Cheng 已提交
1962
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1963 1964
  }

C
Cary Xu 已提交
1965
  if (pHdr->type == SNAP_DATA_DEL) {
H
Hongze Cheng 已提交
1966 1967 1968
    code = tsdbSnapWriteDelData(pWriter, pHdr);
    TSDB_CHECK_CODE(code, lino, _exit);
    goto _exit;
H
Hongze Cheng 已提交
1969 1970
  }

H
Hongze Cheng 已提交
1971
_exit:
H
Hongze Cheng 已提交
1972 1973 1974 1975 1976 1977 1978
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s, type:%d index:%" PRId64 " size:%" PRId64,
              TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code), pHdr->type, pHdr->index, pHdr->size);
  } else {
    tsdbDebug("vgId:%d %s done, type:%d index:%" PRId64 " size:%" PRId64, TD_VID(pWriter->pTsdb->pVnode), __func__,
              pHdr->type, pHdr->index, pHdr->size);
  }
H
Hongze Cheng 已提交
1979 1980
  return code;
}