tsdbSnapshot.c 54.4 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

typedef struct STsdbDataIter2 STsdbDataIter2;
struct STsdbDataIter2 {
  STsdbDataIter2* next;
  SRBTreeNode     rbtn;

  int32_t  type;
  SRowInfo rowInfo;
  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 已提交
62 63 64 65 66 67 68 69
    // TSDB_TOMB_FILE_DATA_ITER
    struct {
      SDelFReader* pReader;
      SArray*      aDelIdx;
      SArray*      aDelData;
      int32_t      iDelIdx;
      int32_t      iDelData;
    } tIter;
H
Hongze Cheng 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  };
};

#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 已提交
97 98 99
  pIter->dIter.iBlockIdx = 0;
  pIter->dIter.iDataBlk = 0;
  pIter->dIter.iRow = 0;
H
Hongze Cheng 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 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

  // 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 已提交
144 145
  pIter->sIter.iSttBlk = 0;
  pIter->sIter.iRow = 0;
H
Hongze Cheng 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

  // 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 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 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
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 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223
/* 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 已提交
224 225 226 227 228 229
static void tsdbCloseTombFileDataIter(STsdbDataIter2* pIter) {
  taosArrayDestroy(pIter->tIter.aDelData);
  taosArrayDestroy(pIter->tIter.aDelIdx);
  taosMemoryFree(pIter);
}

H
Hongze Cheng 已提交
230 231 232 233 234 235 236
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 已提交
237 238
  } else if (pIter->type == TSDB_TOMB_FILE_DATA_ITER) {
    tsdbCloseTombFileDataIter(pIter);
H
Hongze Cheng 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
  } 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 */
static int32_t tsdbDataFileDataIterNext(STsdbDataIter2* pIter) {
  int32_t code = 0;
  int32_t lino = 0;

  for (;;) {
H
Hongze Cheng 已提交
259
    if (pIter->dIter.iRow < pIter->dIter.bData.nRow) {
H
Hongze Cheng 已提交
260 261 262
      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 已提交
263
      pIter->dIter.iRow++;
H
Hongze Cheng 已提交
264 265 266 267
      break;
    }

    for (;;) {
H
Hongze Cheng 已提交
268
      if (pIter->dIter.iDataBlk < pIter->dIter.mDataBlk.nItem) {
H
Hongze Cheng 已提交
269 270 271 272 273 274
        SDataBlk dataBlk;
        tMapDataGetItemByIdx(&pIter->dIter.mDataBlk, pIter->dIter.iDataBlk, &dataBlk, tGetDataBlk);

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

H
Hongze Cheng 已提交
275 276
        pIter->dIter.iDataBlk++;
        pIter->dIter.iRow = 0;
H
Hongze Cheng 已提交
277 278 279 280 281

        break;
      }

      for (;;) {
H
Hongze Cheng 已提交
282
        if (pIter->dIter.iBlockIdx < taosArrayGetSize(pIter->dIter.aBlockIdx)) {
H
Hongze Cheng 已提交
283 284 285 286 287
          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 已提交
288 289
          pIter->dIter.iBlockIdx++;
          pIter->dIter.iDataBlk = 0;
H
Hongze Cheng 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

          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;
}

static int32_t tsdbSttFileDataIterNext(STsdbDataIter2* pIter) {
  int32_t code = 0;
  int32_t lino = 0;

  for (;;) {
H
Hongze Cheng 已提交
312
    if (pIter->sIter.iRow < pIter->sIter.bData.nRow) {
H
Hongze Cheng 已提交
313 314 315
      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 已提交
316
      pIter->sIter.iRow++;
H
Hongze Cheng 已提交
317 318 319
      break;
    }

H
Hongze Cheng 已提交
320
    if (pIter->sIter.iSttBlk < taosArrayGetSize(pIter->sIter.aSttBlk)) {
H
Hongze Cheng 已提交
321 322 323 324 325
      SSttBlk* pSttBlk = taosArrayGet(pIter->sIter.aSttBlk, pIter->sIter.iSttBlk);

      code = tsdbReadSttBlockEx(pIter->sIter.pReader, pIter->sIter.iStt, pSttBlk, &pIter->sIter.bData);
      TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
326 327 328
      pIter->sIter.iSttBlk++;

      pIter->sIter.iRow = 0;
H
Hongze Cheng 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    } else {
      pIter->rowInfo = (SRowInfo){0};
      break;
    }
  }

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

static int32_t tsdbDataIterNext2(STsdbDataIter2* pIter) {
  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) {
    return tsdbDataFileDataIterNext(pIter);
  } else if (pIter->type == TSDB_STT_FILE_DATA_ITER) {
    return tsdbSttFileDataIterNext(pIter);
  } else {
    ASSERT(0);
    return code;
  }
}

/* get */

H
Hongze Cheng 已提交
360
// STsdbSnapReader ========================================
H
Hongze Cheng 已提交
361
struct STsdbSnapReader {
H
Hongze Cheng 已提交
362 363 364 365 366
  STsdb*   pTsdb;
  int64_t  sver;
  int64_t  ever;
  int8_t   type;
  uint8_t* aBuf[5];
H
Hongze Cheng 已提交
367

H
Hongze Cheng 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
  STsdbFS  fs;
  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 已提交
386

H
Hongze Cheng 已提交
387 388 389 390 391 392 393
static int32_t tFDataIterCmprFn(const SRBTreeNode* pNode1, const SRBTreeNode* pNode2) {
  SFDataIter* pIter1 = (SFDataIter*)(((uint8_t*)pNode1) - offsetof(SFDataIter, n));
  SFDataIter* pIter2 = (SFDataIter*)(((uint8_t*)pNode2) - offsetof(SFDataIter, n));

  return tRowInfoCmprFn(&pIter1->rInfo, &pIter2->rInfo);
}

H
Hongze Cheng 已提交
394
static int32_t tsdbSnapReadOpenFile(STsdbSnapReader* pReader) {
H
Hongze Cheng 已提交
395
  int32_t code = 0;
H
add log  
Hongze Cheng 已提交
396
  int32_t lino = 0;
H
Hongze Cheng 已提交
397

H
Hongze Cheng 已提交
398 399 400
  SDFileSet  dFileSet = {.fid = pReader->fid};
  SDFileSet* pSet = taosArraySearch(pReader->fs.aDFileSet, &dFileSet, tDFileSetCmprFn, TD_GT);
  if (pSet == NULL) return code;
H
Hongze Cheng 已提交
401

H
Hongze Cheng 已提交
402 403
  pReader->fid = pSet->fid;
  code = tsdbDataFReaderOpen(&pReader->pDataFReader, pReader->pTsdb, pSet);
H
add log  
Hongze Cheng 已提交
404
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
405 406

  pReader->pIter = NULL;
H
Hongze Cheng 已提交
407
  tRBTreeCreate(&pReader->rbt, tFDataIterCmprFn);
H
Hongze Cheng 已提交
408 409 410 411 412 413

  // .data file
  SFDataIter* pIter = &pReader->aFDataIter[0];
  pIter->type = SNAP_DATA_FILE_ITER;

  code = tsdbReadBlockIdx(pReader->pDataFReader, pIter->aBlockIdx);
H
add log  
Hongze Cheng 已提交
414
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
415 416 417 418

  for (pIter->iBlockIdx = 0; pIter->iBlockIdx < taosArrayGetSize(pIter->aBlockIdx); pIter->iBlockIdx++) {
    pIter->pBlockIdx = (SBlockIdx*)taosArrayGet(pIter->aBlockIdx, pIter->iBlockIdx);

H
Hongze Cheng 已提交
419
    code = tsdbReadDataBlk(pReader->pDataFReader, pIter->pBlockIdx, &pIter->mBlock);
H
add log  
Hongze Cheng 已提交
420
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
421 422 423 424 425 426

    for (pIter->iBlock = 0; pIter->iBlock < pIter->mBlock.nItem; pIter->iBlock++) {
      SDataBlk dataBlk;
      tMapDataGetItemByIdx(&pIter->mBlock, pIter->iBlock, &dataBlk, tGetDataBlk);

      if (dataBlk.minVer > pReader->ever || dataBlk.maxVer < pReader->sver) continue;
H
Hongze Cheng 已提交
427

H
Hongze Cheng 已提交
428
      code = tsdbReadDataBlockEx(pReader->pDataFReader, &dataBlk, &pIter->bData);
H
add log  
Hongze Cheng 已提交
429
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
430

431 432
      ASSERT(pIter->pBlockIdx->suid == pIter->bData.suid);
      ASSERT(pIter->pBlockIdx->uid == pIter->bData.uid);
H
Hongze Cheng 已提交
433

H
Hongze Cheng 已提交
434 435
      for (pIter->iRow = 0; pIter->iRow < pIter->bData.nRow; pIter->iRow++) {
        int64_t rowVer = pIter->bData.aVersion[pIter->iRow];
H
Hongze Cheng 已提交
436

H
Hongze Cheng 已提交
437 438 439 440 441 442
        if (rowVer >= pReader->sver && rowVer <= pReader->ever) {
          pIter->rInfo.suid = pIter->pBlockIdx->suid;
          pIter->rInfo.uid = pIter->pBlockIdx->uid;
          pIter->rInfo.row = tsdbRowFromBlockData(&pIter->bData, pIter->iRow);
          goto _add_iter_and_break;
        }
H
Hongze Cheng 已提交
443
      }
H
Hongze Cheng 已提交
444
    }
H
Hongze Cheng 已提交
445

H
Hongze Cheng 已提交
446
    continue;
H
Hongze Cheng 已提交
447

H
Hongze Cheng 已提交
448 449 450 451
  _add_iter_and_break:
    tRBTreePut(&pReader->rbt, (SRBTreeNode*)pIter);
    break;
  }
H
Hongze Cheng 已提交
452

H
Hongze Cheng 已提交
453 454 455 456 457
  // .stt file
  pIter = &pReader->aFDataIter[1];
  for (int32_t iStt = 0; iStt < pSet->nSttF; iStt++) {
    pIter->type = SNAP_STT_FILE_ITER;
    pIter->iStt = iStt;
H
Hongze Cheng 已提交
458

H
Hongze Cheng 已提交
459
    code = tsdbReadSttBlk(pReader->pDataFReader, iStt, pIter->aSttBlk);
H
add log  
Hongze Cheng 已提交
460
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
461 462 463 464 465 466 467

    for (pIter->iSttBlk = 0; pIter->iSttBlk < taosArrayGetSize(pIter->aSttBlk); pIter->iSttBlk++) {
      SSttBlk* pSttBlk = (SSttBlk*)taosArrayGet(pIter->aSttBlk, pIter->iSttBlk);

      if (pSttBlk->minVer > pReader->ever) continue;
      if (pSttBlk->maxVer < pReader->sver) continue;

H
Hongze Cheng 已提交
468
      code = tsdbReadSttBlockEx(pReader->pDataFReader, iStt, pSttBlk, &pIter->bData);
H
add log  
Hongze Cheng 已提交
469
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
470 471 472 473 474 475

      for (pIter->iRow = 0; pIter->iRow < pIter->bData.nRow; pIter->iRow++) {
        int64_t rowVer = pIter->bData.aVersion[pIter->iRow];

        if (rowVer >= pReader->sver && rowVer <= pReader->ever) {
          pIter->rInfo.suid = pIter->bData.suid;
H
Hongze Cheng 已提交
476
          pIter->rInfo.uid = pIter->bData.uid ? pIter->bData.uid : pIter->bData.aUid[pIter->iRow];
H
Hongze Cheng 已提交
477 478 479 480
          pIter->rInfo.row = tsdbRowFromBlockData(&pIter->bData, pIter->iRow);
          goto _add_iter;
        }
      }
H
Hongze Cheng 已提交
481
    }
H
Hongze Cheng 已提交
482

H
Hongze Cheng 已提交
483
    continue;
H
Hongze Cheng 已提交
484

H
Hongze Cheng 已提交
485 486 487 488
  _add_iter:
    tRBTreePut(&pReader->rbt, (SRBTreeNode*)pIter);
    pIter++;
  }
H
Hongze Cheng 已提交
489

H
add log  
Hongze Cheng 已提交
490 491 492 493 494 495 496
_exit:
  if (code) {
    tsdbError("vgId:%d, %s failed since %s", TD_VID(pReader->pTsdb->pVnode), __func__, tstrerror(code));
  } else {
    tsdbInfo("vgId:%d, %s done, path:%s, fid:%d", TD_VID(pReader->pTsdb->pVnode), __func__, pReader->pTsdb->path,
             pReader->fid);
  }
H
Hongze Cheng 已提交
497 498 499 500 501 502 503
  return code;
}

static int32_t tsdbSnapNextRow(STsdbSnapReader* pReader) {
  int32_t code = 0;

  if (pReader->pIter) {
H
Hongze Cheng 已提交
504
    SFDataIter* pIter = NULL;
H
Hongze Cheng 已提交
505 506
    while (true) {
    _find_row:
H
Hongze Cheng 已提交
507
      pIter = pReader->pIter;
H
Hongze Cheng 已提交
508 509 510 511
      for (pIter->iRow++; pIter->iRow < pIter->bData.nRow; pIter->iRow++) {
        int64_t rowVer = pIter->bData.aVersion[pIter->iRow];

        if (rowVer >= pReader->sver && rowVer <= pReader->ever) {
H
Hongze Cheng 已提交
512
          pIter->rInfo.suid = pIter->bData.suid;
H
Hongze Cheng 已提交
513 514 515
          pIter->rInfo.uid = pIter->bData.uid ? pIter->bData.uid : pIter->bData.aUid[pIter->iRow];
          pIter->rInfo.row = tsdbRowFromBlockData(&pIter->bData, pIter->iRow);
          goto _out;
H
Hongze Cheng 已提交
516
        }
H
Hongze Cheng 已提交
517
      }
H
Hongze Cheng 已提交
518

H
Hongze Cheng 已提交
519 520 521 522 523
      if (pIter->type == SNAP_DATA_FILE_ITER) {
        while (true) {
          for (pIter->iBlock++; pIter->iBlock < pIter->mBlock.nItem; pIter->iBlock++) {
            SDataBlk dataBlk;
            tMapDataGetItemByIdx(&pIter->mBlock, pIter->iBlock, &dataBlk, tGetDataBlk);
H
Hongze Cheng 已提交
524

H
Hongze Cheng 已提交
525 526 527
            if (dataBlk.minVer > pReader->ever || dataBlk.maxVer < pReader->sver) continue;

            code = tsdbReadDataBlockEx(pReader->pDataFReader, &dataBlk, &pIter->bData);
H
Hongze Cheng 已提交
528 529
            if (code) goto _err;

H
Hongze Cheng 已提交
530 531
            pIter->iRow = -1;
            goto _find_row;
H
Hongze Cheng 已提交
532
          }
H
Hongze Cheng 已提交
533 534 535 536 537

          pIter->iBlockIdx++;
          if (pIter->iBlockIdx >= taosArrayGetSize(pIter->aBlockIdx)) break;

          pIter->pBlockIdx = (SBlockIdx*)taosArrayGet(pIter->aBlockIdx, pIter->iBlockIdx);
H
Hongze Cheng 已提交
538
          code = tsdbReadDataBlk(pReader->pDataFReader, pIter->pBlockIdx, &pIter->mBlock);
H
Hongze Cheng 已提交
539 540
          if (code) goto _err;
          pIter->iBlock = -1;
H
Hongze Cheng 已提交
541
        }
H
Hongze Cheng 已提交
542

H
Hongze Cheng 已提交
543
        pReader->pIter = NULL;
H
Hongze Cheng 已提交
544
        break;
H
Hongze Cheng 已提交
545 546 547
      } else if (pIter->type == SNAP_STT_FILE_ITER) {
        for (pIter->iSttBlk++; pIter->iSttBlk < taosArrayGetSize(pIter->aSttBlk); pIter->iSttBlk++) {
          SSttBlk* pSttBlk = (SSttBlk*)taosArrayGet(pIter->aSttBlk, pIter->iSttBlk);
H
Hongze Cheng 已提交
548

H
Hongze Cheng 已提交
549
          if (pSttBlk->minVer > pReader->ever || pSttBlk->maxVer < pReader->sver) continue;
H
Hongze Cheng 已提交
550

H
Hongze Cheng 已提交
551
          code = tsdbReadSttBlockEx(pReader->pDataFReader, pIter->iStt, pSttBlk, &pIter->bData);
H
Hongze Cheng 已提交
552 553 554 555
          if (code) goto _err;

          pIter->iRow = -1;
          goto _find_row;
H
Hongze Cheng 已提交
556
        }
H
Hongze Cheng 已提交
557 558

        pReader->pIter = NULL;
H
Hongze Cheng 已提交
559
        break;
H
Hongze Cheng 已提交
560
      } else {
561
        ASSERT(0);
H
Hongze Cheng 已提交
562 563 564 565 566 567 568 569 570 571 572
      }
    }

  _out:
    pIter = (SFDataIter*)tRBTreeMin(&pReader->rbt);
    if (pReader->pIter && pIter) {
      int32_t c = tRowInfoCmprFn(&pReader->pIter->rInfo, &pIter->rInfo);
      if (c > 0) {
        tRBTreePut(&pReader->rbt, (SRBTreeNode*)pReader->pIter);
        pReader->pIter = NULL;
      } else {
573
        ASSERT(c);
H
Hongze Cheng 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
      }
    }
  }

  if (pReader->pIter == NULL) {
    pReader->pIter = (SFDataIter*)tRBTreeMin(&pReader->rbt);
    if (pReader->pIter) {
      tRBTreeDrop(&pReader->rbt, (SRBTreeNode*)pReader->pIter);
    }
  }

  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604
static SRowInfo* tsdbSnapGetRow(STsdbSnapReader* pReader) {
  if (pReader->pIter) {
    return &pReader->pIter->rInfo;
  } else {
    tsdbSnapNextRow(pReader);

    if (pReader->pIter) {
      return &pReader->pIter->rInfo;
    } else {
      return NULL;
    }
  }
}

H
Hongze Cheng 已提交
605 606 607
static int32_t tsdbSnapCmprData(STsdbSnapReader* pReader, uint8_t** ppData) {
  int32_t code = 0;

608
  ASSERT(pReader->bData.nRow);
H
Hongze Cheng 已提交
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637

  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 已提交
638
static int32_t tsdbSnapReadTimeSeriesData(STsdbSnapReader* pReader, uint8_t** ppData) {
H
Hongze Cheng 已提交
639
  int32_t code = 0;
H
add log  
Hongze Cheng 已提交
640 641 642
  int32_t lino = 0;

  STsdb* pTsdb = pReader->pTsdb;
H
Hongze Cheng 已提交
643 644 645 646

  while (true) {
    if (pReader->pDataFReader == NULL) {
      code = tsdbSnapReadOpenFile(pReader);
H
add log  
Hongze Cheng 已提交
647
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661
    }

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

    SRowInfo* pRowInfo = tsdbSnapGetRow(pReader);
    if (pRowInfo == NULL) {
      tsdbDataFReaderClose(&pReader->pDataFReader);
      continue;
    }

    TABLEID     id = {.suid = pRowInfo->suid, .uid = pRowInfo->uid};
    SBlockData* pBlockData = &pReader->bData;

    code = tsdbUpdateTableSchema(pTsdb->pVnode->pMeta, id.suid, id.uid, &pReader->skmTable);
H
add log  
Hongze Cheng 已提交
662
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
663

H
Hongze Cheng 已提交
664
    code = tBlockDataInit(pBlockData, &id, pReader->skmTable.pTSchema, NULL, 0);
H
add log  
Hongze Cheng 已提交
665
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
666 667 668

    while (pRowInfo->suid == id.suid && pRowInfo->uid == id.uid) {
      code = tBlockDataAppendRow(pBlockData, &pRowInfo->row, NULL, pRowInfo->uid);
H
add log  
Hongze Cheng 已提交
669
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
670 671

      code = tsdbSnapNextRow(pReader);
H
add log  
Hongze Cheng 已提交
672
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
673 674 675

      pRowInfo = tsdbSnapGetRow(pReader);
      if (pRowInfo == NULL) {
H
Hongze Cheng 已提交
676 677
        tsdbDataFReaderClose(&pReader->pDataFReader);
        break;
H
Hongze Cheng 已提交
678
      }
H
Hongze Cheng 已提交
679 680

      if (pBlockData->nRow >= 4096) break;
H
Hongze Cheng 已提交
681
    }
H
Hongze Cheng 已提交
682 683

    code = tsdbSnapCmprData(pReader, ppData);
H
add log  
Hongze Cheng 已提交
684
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
685 686

    break;
H
Hongze Cheng 已提交
687 688
  }

H
add log  
Hongze Cheng 已提交
689 690 691 692
_exit:
  if (code) {
    tsdbError("vgId:%d, %s failed since %s, path:%s", TD_VID(pTsdb->pVnode), __func__, tstrerror(code), pTsdb->path);
  }
H
Hongze Cheng 已提交
693 694 695
  return code;
}

H
Hongze Cheng 已提交
696
static int32_t tsdbSnapReadTombstoneData(STsdbSnapReader* pReader, uint8_t** ppData) {
H
add log  
Hongze Cheng 已提交
697 698 699
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
700
  STsdb*    pTsdb = pReader->pTsdb;
H
Hongze Cheng 已提交
701
  SDelFile* pDelFile = pReader->fs.pDelFile;
H
Hongze Cheng 已提交
702 703 704 705 706 707 708

  if (pReader->pDelFReader == NULL) {
    if (pDelFile == NULL) {
      goto _exit;
    }

    // open
H
Hongze Cheng 已提交
709
    code = tsdbDelFReaderOpen(&pReader->pDelFReader, pDelFile, pTsdb);
H
add log  
Hongze Cheng 已提交
710
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
711

H
Hongze Cheng 已提交
712
    // read index
H
Hongze Cheng 已提交
713
    code = tsdbReadDelIdx(pReader->pDelFReader, pReader->aDelIdx);
H
add log  
Hongze Cheng 已提交
714
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
715 716 717 718

    pReader->iDelIdx = 0;
  }

H
Hongze Cheng 已提交
719 720 721 722 723 724
  while (true) {
    if (pReader->iDelIdx >= taosArrayGetSize(pReader->aDelIdx)) {
      tsdbDelFReaderClose(&pReader->pDelFReader);
      break;
    }

H
Hongze Cheng 已提交
725 726
    SDelIdx* pDelIdx = (SDelIdx*)taosArrayGet(pReader->aDelIdx, pReader->iDelIdx);

H
Hongze Cheng 已提交
727 728
    pReader->iDelIdx++;

H
Hongze Cheng 已提交
729
    code = tsdbReadDelData(pReader->pDelFReader, pDelIdx, pReader->aDelData);
H
add log  
Hongze Cheng 已提交
730
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
731

H
Hongze Cheng 已提交
732
    int32_t size = 0;
H
Hongze Cheng 已提交
733 734 735 736
    for (int32_t iDelData = 0; iDelData < taosArrayGetSize(pReader->aDelData); iDelData++) {
      SDelData* pDelData = (SDelData*)taosArrayGet(pReader->aDelData, iDelData);

      if (pDelData->version >= pReader->sver && pDelData->version <= pReader->ever) {
H
Hongze Cheng 已提交
737
        size += tPutDelData(NULL, pDelData);
H
Hongze Cheng 已提交
738 739
      }
    }
H
Hongze Cheng 已提交
740
    if (size == 0) continue;
H
Hongze Cheng 已提交
741

H
Hongze Cheng 已提交
742 743 744 745 746
    // org data
    size = sizeof(TABLEID) + size;
    *ppData = taosMemoryMalloc(sizeof(SSnapDataHdr) + size);
    if (*ppData == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
H
add log  
Hongze Cheng 已提交
747
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
748
    }
H
Hongze Cheng 已提交
749

H
Hongze Cheng 已提交
750
    SSnapDataHdr* pHdr = (SSnapDataHdr*)(*ppData);
C
Cary Xu 已提交
751
    pHdr->type = SNAP_DATA_DEL;
H
Hongze Cheng 已提交
752
    pHdr->size = size;
H
Hongze Cheng 已提交
753

H
Hongze Cheng 已提交
754 755 756 757 758 759
    TABLEID* pId = (TABLEID*)(&pHdr[1]);
    pId->suid = pDelIdx->suid;
    pId->uid = pDelIdx->uid;
    int32_t n = sizeof(SSnapDataHdr) + sizeof(TABLEID);
    for (int32_t iDelData = 0; iDelData < taosArrayGetSize(pReader->aDelData); iDelData++) {
      SDelData* pDelData = (SDelData*)taosArrayGet(pReader->aDelData, iDelData);
H
Hongze Cheng 已提交
760

H
Hongze Cheng 已提交
761 762
      if (pDelData->version < pReader->sver) continue;
      if (pDelData->version > pReader->ever) continue;
H
Hongze Cheng 已提交
763

H
Hongze Cheng 已提交
764
      n += tPutDelData((*ppData) + n, pDelData);
H
Hongze Cheng 已提交
765 766
    }

H
Hongze Cheng 已提交
767
    tsdbInfo("vgId:%d, vnode snapshot tsdb read del data for %s, suid:%" PRId64 " uid:%" PRId64 " size:%d",
C
Cary Xu 已提交
768
             TD_VID(pTsdb->pVnode), pTsdb->path, pDelIdx->suid, pDelIdx->uid, size);
H
Hongze Cheng 已提交
769 770 771

    break;
  }
H
Hongze Cheng 已提交
772 773

_exit:
H
add log  
Hongze Cheng 已提交
774 775 776
  if (code) {
    tsdbError("vgId:%d, %s failed since %s, path:%s", TD_VID(pTsdb->pVnode), __func__, tstrerror(code), pTsdb->path);
  }
H
Hongze Cheng 已提交
777 778
  return code;
}
H
more  
Hongze Cheng 已提交
779

C
Cary Xu 已提交
780
int32_t tsdbSnapReaderOpen(STsdb* pTsdb, int64_t sver, int64_t ever, int8_t type, STsdbSnapReader** ppReader) {
H
Hongze Cheng 已提交
781 782
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
783

H
more  
Hongze Cheng 已提交
784
  // alloc
H
Hongze Cheng 已提交
785
  STsdbSnapReader* pReader = (STsdbSnapReader*)taosMemoryCalloc(1, sizeof(*pReader));
H
more  
Hongze Cheng 已提交
786 787
  if (pReader == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
788
    TSDB_CHECK_CODE(code, lino, _exit);
H
more  
Hongze Cheng 已提交
789 790 791 792
  }
  pReader->pTsdb = pTsdb;
  pReader->sver = sver;
  pReader->ever = ever;
C
Cary Xu 已提交
793
  pReader->type = type;
H
more  
Hongze Cheng 已提交
794

H
Hongze Cheng 已提交
795
  taosThreadRwlockRdlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
796 797 798
  code = tsdbFSRef(pTsdb, &pReader->fs);
  if (code) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
799
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
800
  }
H
Hongze Cheng 已提交
801
  taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
802

H
Hongze Cheng 已提交
803
  // init
H
Hongze Cheng 已提交
804
  pReader->fid = INT32_MIN;
H
Hongze Cheng 已提交
805 806

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

H
Hongze Cheng 已提交
809 810
_exit:
  if (code) {
H
Hongze Cheng 已提交
811 812
    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 已提交
813 814
    if (pReader) {
      tBlockDataDestroy(&pReader->bData, 1);
H
Hongze Cheng 已提交
815
      tsdbFSUnref(pTsdb, &pReader->fs);
H
Hongze Cheng 已提交
816
      taosMemoryFree(pReader);
H
Hongze Cheng 已提交
817
      pReader = NULL;
H
Hongze Cheng 已提交
818 819
    }
  } else {
H
Hongze Cheng 已提交
820 821
    tsdbInfo("vgId:%d %s done, sver:%" PRId64 " ever:%" PRId64 " type:%d", TD_VID(pTsdb->pVnode), __func__, sver, ever,
             type);
H
Hongze Cheng 已提交
822
  }
H
Hongze Cheng 已提交
823
  *ppReader = pReader;
H
more  
Hongze Cheng 已提交
824
  return code;
H
Hongze Cheng 已提交
825 826
}

H
Hongze Cheng 已提交
827
int32_t tsdbSnapReaderClose(STsdbSnapReader** ppReader) {
H
Hongze Cheng 已提交
828 829
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
830

H
Hongze Cheng 已提交
831
  STsdbSnapReader* pReader = *ppReader;
H
Hongze Cheng 已提交
832

H
Hongze Cheng 已提交
833 834 835 836 837 838 839
  // tombstone
  if (pReader->pTIter) {
    tsdbCloseDataIter2(pReader->pTIter);
    pReader->pTIter = NULL;
  }
  if (pReader->pDelFReader) {
    tsdbDelFReaderClose(&pReader->pDelFReader);
H
Hongze Cheng 已提交
840
  }
H
Hongze Cheng 已提交
841

H
Hongze Cheng 已提交
842
  // timeseries
H
Hongze Cheng 已提交
843
  tBlockDataDestroy(&pReader->bData, 1);
H
Hongze Cheng 已提交
844 845 846 847 848 849 850 851
  while (pReader->iterList) {
    STsdbDataIter2* pIter = pReader->iterList;
    pReader->iterList = pIter->next;
    tsdbCloseDataIter2(pIter);
  }
  if (pReader->pDataFReader) {
    tsdbDataFReaderClose(&pReader->pDataFReader);
  }
H
Hongze Cheng 已提交
852

H
Hongze Cheng 已提交
853 854
  // other
  tDestroyTSchema(pReader->skmTable.pTSchema);
H
Hongze Cheng 已提交
855
  tsdbFSUnref(pReader->pTsdb, &pReader->fs);
H
Hongze Cheng 已提交
856 857 858
  for (int32_t iBuf = 0; iBuf < sizeof(pReader->aBuf) / sizeof(pReader->aBuf[0]); iBuf++) {
    tFree(pReader->aBuf[iBuf]);
  }
H
Hongze Cheng 已提交
859
  taosMemoryFree(pReader);
H
Hongze Cheng 已提交
860 861 862 863 864 865 866

_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 已提交
867
  *ppReader = NULL;
H
Hongze Cheng 已提交
868 869 870 871
  return code;
}

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

H
Hongze Cheng 已提交
875 876
  *ppData = NULL;

H
Hongze Cheng 已提交
877
  // read data file
H
Hongze Cheng 已提交
878
  if (!pReader->dataDone) {
H
Hongze Cheng 已提交
879
    code = tsdbSnapReadTimeSeriesData(pReader, ppData);
H
add log  
Hongze Cheng 已提交
880 881 882
    TSDB_CHECK_CODE(code, lino, _exit);
    if (*ppData) {
      goto _exit;
H
Hongze Cheng 已提交
883
    } else {
H
add log  
Hongze Cheng 已提交
884
      pReader->dataDone = 1;
H
Hongze Cheng 已提交
885 886
    }
  }
H
Hongze Cheng 已提交
887 888

  // read del file
H
Hongze Cheng 已提交
889
  if (!pReader->delDone) {
H
Hongze Cheng 已提交
890
    code = tsdbSnapReadTombstoneData(pReader, ppData);
H
add log  
Hongze Cheng 已提交
891 892 893
    TSDB_CHECK_CODE(code, lino, _exit);
    if (*ppData) {
      goto _exit;
H
Hongze Cheng 已提交
894
    } else {
H
add log  
Hongze Cheng 已提交
895
      pReader->delDone = 1;
H
Hongze Cheng 已提交
896 897
    }
  }
H
Hongze Cheng 已提交
898

H
Hongze Cheng 已提交
899
_exit:
H
add log  
Hongze Cheng 已提交
900
  if (code) {
H
Hongze Cheng 已提交
901
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pReader->pTsdb->pVnode), __func__, lino, tstrerror(code));
H
add log  
Hongze Cheng 已提交
902
  } else {
H
Hongze Cheng 已提交
903
    tsdbDebug("vgId:%d %s done", TD_VID(pReader->pTsdb->pVnode), __func__);
H
add log  
Hongze Cheng 已提交
904
  }
H
more  
Hongze Cheng 已提交
905 906 907
  return code;
}

H
Hongze Cheng 已提交
908
// STsdbSnapWriter ========================================
H
Hongze Cheng 已提交
909
struct STsdbSnapWriter {
H
Hongze Cheng 已提交
910 911 912
  STsdb*   pTsdb;
  int64_t  sver;
  int64_t  ever;
H
Hongze Cheng 已提交
913 914 915 916 917 918
  int32_t  minutes;
  int8_t   precision;
  int32_t  minRow;
  int32_t  maxRow;
  int8_t   cmprAlg;
  int64_t  commitID;
H
Hongze Cheng 已提交
919
  uint8_t* aBuf[5];
H
Hongze Cheng 已提交
920

H
Hongze Cheng 已提交
921
  STsdbFS fs;
H
Hongze Cheng 已提交
922
  TABLEID tbid;
H
Hongze Cheng 已提交
923

H
Hongze Cheng 已提交
924 925 926 927 928 929 930 931 932 933
  // time-series data
  SBlockData inData;

  int32_t  fid;
  SSkmInfo skmTable;

  /* reader */
  SDataFReader*   pDataFReader;
  STsdbDataIter2* iterList;
  STsdbDataIter2* pDIter;
H
Hongze Cheng 已提交
934
  STsdbDataIter2* pSIter;
H
Hongze Cheng 已提交
935 936 937 938 939 940 941 942 943 944 945
  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 已提交
946 947 948 949 950
  /* reader */
  SDelFReader*    pDelFReader;
  STsdbDataIter2* pTIter;

  /* writer */
H
Hongze Cheng 已提交
951
  SDelFWriter* pDelFWriter;
H
Hongze Cheng 已提交
952
  SArray*      aDelIdx;
H
Hongze Cheng 已提交
953
  SArray*      aDelData;
H
Hongze Cheng 已提交
954 955
};

H
Hongze Cheng 已提交
956
// SNAP_DATA_TSDB
H
Hongze Cheng 已提交
957 958
static int32_t tsdbSnapWriteTableDataStart(STsdbSnapWriter* pWriter, TABLEID* pId) {
  int32_t code = 0;
H
Hongze Cheng 已提交
959 960
  int32_t lino = 0;

H
Hongze Cheng 已提交
961 962 963 964 965 966
  if (pId) {
    pWriter->tbid = *pId;
  } else {
    pWriter->tbid = (TABLEID){INT64_MAX, INT64_MAX};
  }

H
Hongze Cheng 已提交
967 968
  if (pWriter->pDIter) {
    STsdbDataIter2* pIter = pWriter->pDIter;
H
Hongze Cheng 已提交
969

H
Hongze Cheng 已提交
970
    // assert last table data end
H
Hongze Cheng 已提交
971 972 973
    ASSERT(pIter->dIter.iRow >= pIter->dIter.bData.nRow);
    ASSERT(pIter->dIter.iDataBlk >= pIter->dIter.mDataBlk.nItem);

H
Hongze Cheng 已提交
974
    for (;;) {
H
Hongze Cheng 已提交
975
      if (pIter->dIter.iBlockIdx >= taosArrayGetSize(pIter->dIter.aBlockIdx)) {
H
Hongze Cheng 已提交
976 977 978 979
        pWriter->pDIter = NULL;
        break;
      }

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

H
Hongze Cheng 已提交
982
      int32_t c = tTABLEIDCmprFn(pBlockIdx, &pWriter->tbid);
H
Hongze Cheng 已提交
983 984 985 986 987 988
      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 已提交
989
          code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
990 991 992 993 994 995
          TSDB_CHECK_CODE(code, lino, _exit);
        }

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

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

H
Hongze Cheng 已提交
999 1000
        pIter->dIter.iBlockIdx++;
      } else if (c == 0) {
H
Hongze Cheng 已提交
1001 1002 1003
        code = tsdbReadDataBlk(pIter->dIter.pReader, pBlockIdx, &pIter->dIter.mDataBlk);
        TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1004
        pIter->dIter.iDataBlk = 0;
H
Hongze Cheng 已提交
1005
        pIter->dIter.iBlockIdx++;
H
Hongze Cheng 已提交
1006 1007 1008

        break;
      } else {
H
Hongze Cheng 已提交
1009
        pIter->dIter.iDataBlk = pIter->dIter.mDataBlk.nItem;
H
Hongze Cheng 已提交
1010 1011 1012 1013
        break;
      }
    }
  }
H
Hongze Cheng 已提交
1014

H
Hongze Cheng 已提交
1015
  if (pId) {
H
Hongze Cheng 已提交
1016 1017
    code = tsdbUpdateTableSchema(pWriter->pTsdb->pVnode->pMeta, pId->suid, pId->uid, &pWriter->skmTable);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1018

H
Hongze Cheng 已提交
1019
    tMapDataReset(&pWriter->mDataBlk);
H
Hongze Cheng 已提交
1020

H
Hongze Cheng 已提交
1021 1022
    code = tBlockDataInit(&pWriter->bData, pId, pWriter->skmTable.pTSchema, NULL, 0);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1023
  }
H
Hongze Cheng 已提交
1024

H
Hongze Cheng 已提交
1025 1026 1027 1028 1029 1030
  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 已提交
1031 1032 1033 1034 1035
    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 已提交
1036
  }
H
Hongze Cheng 已提交
1037

H
Hongze Cheng 已提交
1038 1039 1040
_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 已提交
1041
  } else {
H
Hongze Cheng 已提交
1042 1043
    tsdbTrace("vgId:%d %s done, suid:%" PRId64 " uid:%" PRId64, TD_VID(pWriter->pTsdb->pVnode), __func__,
              pWriter->tbid.suid, pWriter->tbid.uid);
H
Hongze Cheng 已提交
1044
  }
H
Hongze Cheng 已提交
1045 1046 1047
  return code;
}

H
Hongze Cheng 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
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 已提交
1137 1138
static int32_t tsdbSnapWriteTableDataEnd(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1139
  int32_t lino = 0;
H
Hongze Cheng 已提交
1140

H
Hongze Cheng 已提交
1141 1142 1143
  // write a NULL row to end current table data write
  code = tsdbSnapWriteTableRow(pWriter, NULL);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1144

H
Hongze Cheng 已提交
1145 1146
  if (pWriter->bData.nRow > 0) {
    if (pWriter->bData.nRow < pWriter->minRow) {
H
Hongze Cheng 已提交
1147
      ASSERT(TABLE_SAME_SCHEMA(pWriter->sData.suid, pWriter->sData.uid, pWriter->tbid.suid, pWriter->tbid.uid));
H
Hongze Cheng 已提交
1148
      for (int32_t iRow = 0; iRow < pWriter->bData.nRow; iRow++) {
H
Hongze Cheng 已提交
1149 1150
        code =
            tBlockDataAppendRow(&pWriter->sData, &tsdbRowFromBlockData(&pWriter->bData, iRow), NULL, pWriter->tbid.uid);
H
Hongze Cheng 已提交
1151
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1152

H
Hongze Cheng 已提交
1153 1154 1155 1156
        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 已提交
1157 1158
      }

H
Hongze Cheng 已提交
1159
      tBlockDataClear(&pWriter->bData);
H
Hongze Cheng 已提交
1160 1161 1162
    } else {
      code = tsdbWriteDataBlock(pWriter->pDataFWriter, &pWriter->bData, &pWriter->mDataBlk, pWriter->cmprAlg);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1163 1164 1165
    }
  }

H
Hongze Cheng 已提交
1166 1167 1168
  if (pWriter->mDataBlk.nItem) {
    SBlockIdx* pBlockIdx = taosArrayReserve(pWriter->aBlockIdx, 1);
    if (pBlockIdx == NULL) {
H
Hongze Cheng 已提交
1169
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1170
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1171 1172
    }

H
Hongze Cheng 已提交
1173 1174 1175
    pBlockIdx->suid = pWriter->tbid.suid;
    pBlockIdx->uid = pWriter->tbid.uid;

H
Hongze Cheng 已提交
1176 1177 1178
    code = tsdbWriteDataBlk(pWriter->pDataFWriter, &pWriter->mDataBlk, pBlockIdx);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
1179

H
Hongze Cheng 已提交
1180 1181 1182 1183
_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 已提交
1184 1185 1186
  return code;
}

H
Hongze Cheng 已提交
1187
static int32_t tsdbSnapWriteFileDataStart(STsdbSnapWriter* pWriter, int32_t fid) {
H
Hongze Cheng 已提交
1188
  int32_t code = 0;
H
Hongze Cheng 已提交
1189 1190 1191
  int32_t lino = 0;

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

H
Hongze Cheng 已提交
1193
  STsdb* pTsdb = pWriter->pTsdb;
H
Hongze Cheng 已提交
1194 1195

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

H
Hongze Cheng 已提交
1199 1200 1201 1202
  // open reader
  pWriter->pDataFReader = NULL;
  pWriter->iterList = NULL;
  pWriter->pDIter = NULL;
H
Hongze Cheng 已提交
1203
  pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
1204
  tRBTreeCreate(&pWriter->rbt, tsdbDataIterCmprFn);
H
Hongze Cheng 已提交
1205
  if (pSet) {
H
Hongze Cheng 已提交
1206 1207
    code = tsdbDataFReaderOpen(&pWriter->pDataFReader, pTsdb, pSet);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1208

H
Hongze Cheng 已提交
1209 1210 1211 1212 1213 1214
    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 已提交
1215 1216

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

H
Hongze Cheng 已提交
1220 1221
      if (pWriter->pSIter) {
        code = tsdbSttFileDataIterNext(pWriter->pSIter);
H
Hongze Cheng 已提交
1222 1223 1224
        TSDB_CHECK_CODE(code, lino, _exit);

        // add to tree
H
Hongze Cheng 已提交
1225
        tRBTreePut(&pWriter->rbt, &pWriter->pSIter->rbtn);
H
Hongze Cheng 已提交
1226 1227

        // add to list
H
Hongze Cheng 已提交
1228 1229
        pWriter->pSIter->next = pWriter->iterList;
        pWriter->iterList = pWriter->pSIter;
H
Hongze Cheng 已提交
1230
      }
H
Hongze Cheng 已提交
1231
    }
H
Hongze Cheng 已提交
1232

H
Hongze Cheng 已提交
1233
    pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
1234 1235 1236 1237 1238 1239
  }

  // open writer
  SDiskID diskId;
  if (pSet) {
    diskId = pSet->diskId;
H
Hongze Cheng 已提交
1240
  } else {
H
Hongze Cheng 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
    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 已提交
1259
  }
H
Hongze Cheng 已提交
1260

H
Hongze Cheng 已提交
1261
  tMapDataReset(&pWriter->mDataBlk);
H
Hongze Cheng 已提交
1262

H
Hongze Cheng 已提交
1263 1264 1265 1266 1267 1268
  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 已提交
1269

H
Hongze Cheng 已提交
1270
  tBlockDataReset(&pWriter->bData);
H
Hongze Cheng 已提交
1271
  tBlockDataReset(&pWriter->sData);
H
Hongze Cheng 已提交
1272 1273 1274 1275 1276 1277 1278 1279

_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 已提交
1280 1281 1282
  return code;
}

H
Hongze Cheng 已提交
1283 1284 1285 1286 1287 1288
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 已提交
1289
    if (pWriter->tbid.uid) {
H
Hongze Cheng 已提交
1290 1291 1292 1293 1294 1295 1296 1297
      code = tsdbSnapWriteTableDataEnd(pWriter);
      TSDB_CHECK_CODE(code, lino, _exit);
    }

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

H
Hongze Cheng 已提交
1298 1299 1300 1301
  if (pRowInfo == NULL) goto _exit;

  code = tsdbSnapWriteTableRow(pWriter, &pRowInfo->row);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1302 1303 1304 1305 1306 1307 1308 1309

_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 已提交
1310
static int32_t tsdbSnapWriteNextRow(STsdbSnapWriter* pWriter, SRowInfo** ppRowInfo) {
H
Hongze Cheng 已提交
1311
  int32_t code = 0;
H
Hongze Cheng 已提交
1312
  int32_t lino = 0;
H
Hongze Cheng 已提交
1313

H
Hongze Cheng 已提交
1314 1315
  if (pWriter->pSIter) {
    code = tsdbDataIterNext2(pWriter->pSIter);
H
Hongze Cheng 已提交
1316
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1317

H
Hongze Cheng 已提交
1318 1319
    if (pWriter->pSIter->rowInfo.suid == 0 && pWriter->pSIter->rowInfo.uid == 0) {
      pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
1320 1321 1322
    } else {
      SRBTreeNode* pNode = tRBTreeMin(&pWriter->rbt);
      if (pNode) {
H
Hongze Cheng 已提交
1323
        int32_t c = tsdbDataIterCmprFn(&pWriter->pSIter->rbtn, pNode);
H
Hongze Cheng 已提交
1324
        if (c > 0) {
H
Hongze Cheng 已提交
1325 1326
          tRBTreePut(&pWriter->rbt, &pWriter->pSIter->rbtn);
          pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
1327 1328 1329 1330 1331
        } else if (c == 0) {
          ASSERT(0);
        }
      }
    }
H
Hongze Cheng 已提交
1332 1333
  }

H
Hongze Cheng 已提交
1334
  if (pWriter->pSIter == NULL) {
H
Hongze Cheng 已提交
1335 1336 1337
    SRBTreeNode* pNode = tRBTreeMin(&pWriter->rbt);
    if (pNode) {
      tRBTreeDrop(&pWriter->rbt, pNode);
H
Hongze Cheng 已提交
1338
      pWriter->pSIter = TSDB_RBTN_TO_DATA_ITER(pNode);
H
Hongze Cheng 已提交
1339
    }
H
Hongze Cheng 已提交
1340 1341
  }

H
Hongze Cheng 已提交
1342
  if (ppRowInfo) {
H
Hongze Cheng 已提交
1343 1344
    if (pWriter->pSIter) {
      *ppRowInfo = &pWriter->pSIter->rowInfo;
H
Hongze Cheng 已提交
1345 1346 1347
    } else {
      *ppRowInfo = NULL;
    }
H
Hongze Cheng 已提交
1348
  }
H
Hongze Cheng 已提交
1349

H
Hongze Cheng 已提交
1350 1351 1352
_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 已提交
1353
  }
H
Hongze Cheng 已提交
1354 1355 1356 1357 1358 1359 1360
  return code;
}

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

H
Hongze Cheng 已提交
1361 1362
  if (pWriter->pSIter) {
    *ppRowInfo = &pWriter->pSIter->rowInfo;
H
Hongze Cheng 已提交
1363 1364 1365 1366 1367
    goto _exit;
  }

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

H
Hongze Cheng 已提交
1369
_exit:
H
Hongze Cheng 已提交
1370 1371 1372
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
H
Hongze Cheng 已提交
1373
  return code;
H
Hongze Cheng 已提交
1374
}
H
Hongze Cheng 已提交
1375

H
Hongze Cheng 已提交
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
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 已提交
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
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 已提交
1446
      code = tsdbSnapWriteFileDataEnd(pWriter);
H
Hongze Cheng 已提交
1447 1448 1449
      TSDB_CHECK_CODE(code, lino, _exit);
    }

H
Hongze Cheng 已提交
1450
    code = tsdbSnapWriteFileDataStart(pWriter, fid);
H
Hongze Cheng 已提交
1451 1452 1453 1454 1455 1456 1457
    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 已提交
1458
  for (int32_t iRow = 0; iRow < pWriter->inData.nRow; ++iRow) {
H
Hongze Cheng 已提交
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
    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 已提交
1494 1495 1496
  return code;
}

H
Hongze Cheng 已提交
1497
// SNAP_DATA_DEL
H
Hongze Cheng 已提交
1498
static int32_t tsdbSnapWriteDelTableDataStart(STsdbSnapWriter* pWriter, TABLEID* pId) {
H
Hongze Cheng 已提交
1499
  int32_t code = 0;
H
Hongze Cheng 已提交
1500
  int32_t lino = 0;
H
Hongze Cheng 已提交
1501

H
Hongze Cheng 已提交
1502 1503 1504 1505 1506
  if (pId) {
    pWriter->tbid = *pId;
  } else {
    pWriter->tbid = (TABLEID){.suid = INT64_MAX, .uid = INT64_MAX};
  }
H
Hongze Cheng 已提交
1507

H
Hongze Cheng 已提交
1508
  taosArrayClear(pWriter->aDelData);
H
Hongze Cheng 已提交
1509

H
Hongze Cheng 已提交
1510 1511
  if (pWriter->pTIter) {
    while (pWriter->pTIter->tIter.iDelIdx < taosArrayGetSize(pWriter->pTIter->tIter.aDelIdx)) {
H
Hongze Cheng 已提交
1512
      SDelIdx* pDelIdx = taosArrayGet(pWriter->pTIter->tIter.aDelIdx, pWriter->pTIter->tIter.iDelIdx);
H
Hongze Cheng 已提交
1513

H
Hongze Cheng 已提交
1514 1515 1516 1517
      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 已提交
1518

H
Hongze Cheng 已提交
1519
        SDelIdx* pDelIdxNew = taosArrayReserve(pWriter->aDelIdx, 1);
H
Hongze Cheng 已提交
1520 1521 1522 1523 1524 1525 1526
        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 已提交
1527

H
Hongze Cheng 已提交
1528 1529 1530 1531 1532
        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 已提交
1533
        code = tsdbReadDelData(pWriter->pDelFReader, pDelIdx, pWriter->aDelData);
H
Hongze Cheng 已提交
1534 1535 1536 1537 1538 1539 1540 1541
        TSDB_CHECK_CODE(code, lino, _exit);

        pWriter->pTIter->tIter.iDelIdx++;
        break;
      } else {
        break;
      }
    }
H
Hongze Cheng 已提交
1542 1543 1544
  }

_exit:
H
Hongze Cheng 已提交
1545 1546 1547 1548 1549 1550
  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 已提交
1551 1552 1553
  return code;
}

H
Hongze Cheng 已提交
1554
static int32_t tsdbSnapWriteDelTableDataEnd(STsdbSnapWriter* pWriter) {
H
Hongze Cheng 已提交
1555
  int32_t code = 0;
H
Hongze Cheng 已提交
1556
  int32_t lino = 0;
H
Hongze Cheng 已提交
1557

H
Hongze Cheng 已提交
1558 1559 1560 1561 1562 1563
  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 已提交
1564

H
Hongze Cheng 已提交
1565 1566
    pDelIdx->suid = pWriter->tbid.suid;
    pDelIdx->uid = pWriter->tbid.uid;
H
Hongze Cheng 已提交
1567

H
Hongze Cheng 已提交
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
    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 已提交
1589 1590
    }

H
Hongze Cheng 已提交
1591 1592
    code = tsdbSnapWriteDelTableDataStart(pWriter, pId);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1593 1594
  }

H
Hongze Cheng 已提交
1595
  if (pId == NULL) goto _exit;
H
Hongze Cheng 已提交
1596

H
Hongze Cheng 已提交
1597 1598 1599 1600
  int64_t n = 0;
  while (n < size) {
    SDelData delData;
    n += tGetDelData(pData + n, &delData);
H
Hongze Cheng 已提交
1601

H
Hongze Cheng 已提交
1602 1603 1604 1605 1606 1607
    if (taosArrayPush(pWriter->aDelData, &delData) < 0) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  }
  ASSERT(n == size);
H
Hongze Cheng 已提交
1608

H
Hongze Cheng 已提交
1609 1610 1611
_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 已提交
1612
  }
H
Hongze Cheng 已提交
1613 1614
  return code;
}
H
Hongze Cheng 已提交
1615

H
Hongze Cheng 已提交
1616 1617 1618
static int32_t tsdbSnapWriteDelDataStart(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1619

H
Hongze Cheng 已提交
1620 1621
  STsdb*    pTsdb = pWriter->pTsdb;
  SDelFile* pDelFile = pWriter->fs.pDelFile;
H
Hongze Cheng 已提交
1622

H
Hongze Cheng 已提交
1623 1624 1625 1626 1627 1628 1629 1630 1631
  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 已提交
1632
  }
H
Hongze Cheng 已提交
1633

H
Hongze Cheng 已提交
1634 1635 1636
  // writer
  code = tsdbDelFWriterOpen(&pWriter->pDelFWriter, &(SDelFile){.commitID = pWriter->commitID}, pTsdb);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1637

H
Hongze Cheng 已提交
1638
  if ((pWriter->aDelIdx = taosArrayInit(0, sizeof(SDelIdx))) == NULL) {
H
Hongze Cheng 已提交
1639
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1640 1641 1642 1643 1644
    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 已提交
1645 1646
  }

H
Hongze Cheng 已提交
1647 1648 1649 1650 1651 1652
_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 已提交
1653 1654 1655
  return code;
}

H
Hongze Cheng 已提交
1656
static int32_t tsdbSnapWriteDelDataEnd(STsdbSnapWriter* pWriter) {
H
Hongze Cheng 已提交
1657
  int32_t code = 0;
H
Hongze Cheng 已提交
1658
  int32_t lino = 0;
H
Hongze Cheng 已提交
1659

H
Hongze Cheng 已提交
1660
  STsdb* pTsdb = pWriter->pTsdb;
H
Hongze Cheng 已提交
1661

H
Hongze Cheng 已提交
1662
  // end remaining table with NULL data
H
Hongze Cheng 已提交
1663 1664
  code = tsdbSnapWriteDelTableData(pWriter, NULL, NULL, 0);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1665

H
Hongze Cheng 已提交
1666 1667 1668
  // update file-level info
  code = tsdbWriteDelIdx(pWriter->pDelFWriter, pWriter->aDelIdx);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1669

H
Hongze Cheng 已提交
1670
  code = tsdbUpdateDelFileHdr(pWriter->pDelFWriter);
H
Hongze Cheng 已提交
1671
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1672

H
Hongze Cheng 已提交
1673
  code = tsdbFSUpsertDelFile(&pWriter->fs, &pWriter->pDelFWriter->fDel);
H
Hongze Cheng 已提交
1674
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1675 1676

  code = tsdbDelFWriterClose(&pWriter->pDelFWriter, 1);
H
Hongze Cheng 已提交
1677
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1678 1679 1680

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

H
Hongze Cheng 已提交
1684 1685 1686 1687 1688
  if (pWriter->pTIter) {
    tsdbCloseDataIter2(pWriter->pTIter);
    pWriter->pTIter = NULL;
  }

H
Hongze Cheng 已提交
1689 1690 1691 1692 1693 1694
_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 已提交
1695
  return code;
H
Hongze Cheng 已提交
1696
}
H
Hongze Cheng 已提交
1697

H
Hongze Cheng 已提交
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
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 已提交
1710
  // do write del data
H
Hongze Cheng 已提交
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
  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 已提交
1721 1722 1723
  return code;
}

H
Hongze Cheng 已提交
1724
// APIs
H
Hongze Cheng 已提交
1725
int32_t tsdbSnapWriterOpen(STsdb* pTsdb, int64_t sver, int64_t ever, STsdbSnapWriter** ppWriter) {
H
Hongze Cheng 已提交
1726 1727
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1728 1729

  // alloc
H
Hongze Cheng 已提交
1730
  STsdbSnapWriter* pWriter = (STsdbSnapWriter*)taosMemoryCalloc(1, sizeof(*pWriter));
H
Hongze Cheng 已提交
1731 1732
  if (pWriter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1733
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1734 1735 1736 1737
  }
  pWriter->pTsdb = pTsdb;
  pWriter->sver = sver;
  pWriter->ever = ever;
H
Hongze Cheng 已提交
1738 1739 1740 1741 1742
  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 已提交
1743 1744
  pWriter->commitID = pTsdb->pVnode->state.commitID;

H
Hongze Cheng 已提交
1745 1746 1747
  code = tsdbFSCopy(pTsdb, &pWriter->fs);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1748
  // SNAP_DATA_TSDB
H
Hongze Cheng 已提交
1749 1750 1751
  code = tBlockDataCreate(&pWriter->inData);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1752
  pWriter->fid = INT32_MIN;
H
Hongze Cheng 已提交
1753

H
Hongze Cheng 已提交
1754
  code = tBlockDataCreate(&pWriter->bData);
H
Hongze Cheng 已提交
1755
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1756 1757

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

H
Hongze Cheng 已提交
1760
  // SNAP_DATA_DEL
H
Hongze Cheng 已提交
1761

H
Hongze Cheng 已提交
1762 1763
_exit:
  if (code) {
H
Hongze Cheng 已提交
1764
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1765
    if (pWriter) {
H
Hongze Cheng 已提交
1766
      tBlockDataDestroy(&pWriter->sData, 1);
H
Hongze Cheng 已提交
1767
      tBlockDataDestroy(&pWriter->bData, 1);
H
Hongze Cheng 已提交
1768
      tBlockDataDestroy(&pWriter->inData, 1);
H
Hongze Cheng 已提交
1769
      tsdbFSDestroy(&pWriter->fs);
H
Hongze Cheng 已提交
1770
      pWriter = NULL;
H
Hongze Cheng 已提交
1771 1772
    }
  } else {
H
Hongze Cheng 已提交
1773
    tsdbInfo("vgId:%d %s done, sver:%" PRId64 " ever:%" PRId64, TD_VID(pTsdb->pVnode), __func__, sver, ever);
H
Hongze Cheng 已提交
1774
  }
H
Hongze Cheng 已提交
1775
  *ppWriter = pWriter;
H
Hongze Cheng 已提交
1776 1777 1778
  return code;
}

H
Hongze Cheng 已提交
1779 1780
int32_t tsdbSnapWriterPrepareClose(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1781 1782
  int32_t lino = 0;

H
Hongze Cheng 已提交
1783
  if (pWriter->pDataFWriter) {
H
Hongze Cheng 已提交
1784
    code = tsdbSnapWriteFileDataEnd(pWriter);
H
Hongze Cheng 已提交
1785
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1786 1787
  }

H
Hongze Cheng 已提交
1788 1789 1790 1791
  if (pWriter->pDelFWriter) {
    code = tsdbSnapWriteDelDataEnd(pWriter);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
1792 1793

  code = tsdbFSPrepareCommit(pWriter->pTsdb, &pWriter->fs);
H
Hongze Cheng 已提交
1794
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1795 1796 1797

_exit:
  if (code) {
H
Hongze Cheng 已提交
1798 1799 1800
    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 已提交
1801 1802 1803 1804
  }
  return code;
}

H
Hongze Cheng 已提交
1805
int32_t tsdbSnapWriterClose(STsdbSnapWriter** ppWriter, int8_t rollback) {
H
Hongze Cheng 已提交
1806
  int32_t          code = 0;
H
Hongze Cheng 已提交
1807
  STsdbSnapWriter* pWriter = *ppWriter;
H
Hongze Cheng 已提交
1808
  STsdb*           pTsdb = pWriter->pTsdb;
H
Hongze Cheng 已提交
1809 1810

  if (rollback) {
H
Hongze Cheng 已提交
1811
    tsdbRollbackCommit(pWriter->pTsdb);
H
Hongze Cheng 已提交
1812
  } else {
H
Hongze Cheng 已提交
1813 1814 1815
    // lock
    taosThreadRwlockWrlock(&pTsdb->rwLock);

H
Hongze Cheng 已提交
1816
    code = tsdbFSCommit(pWriter->pTsdb);
H
Hongze Cheng 已提交
1817 1818 1819 1820 1821 1822 1823
    if (code) {
      taosThreadRwlockUnlock(&pTsdb->rwLock);
      goto _err;
    }

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

H
Hongze Cheng 已提交
1826 1827
  // SNAP_DATA_DEL
  taosArrayDestroy(pWriter->aDelData);
H
Hongze Cheng 已提交
1828
  taosArrayDestroy(pWriter->aDelIdx);
H
Hongze Cheng 已提交
1829 1830

  // SNAP_DATA_TSDB
H
Hongze Cheng 已提交
1831
  tBlockDataDestroy(&pWriter->sData, 1);
H
Hongze Cheng 已提交
1832
  tBlockDataDestroy(&pWriter->bData, 1);
H
Hongze Cheng 已提交
1833 1834 1835
  taosArrayDestroy(pWriter->aSttBlk);
  tMapDataClear(&pWriter->mDataBlk);
  taosArrayDestroy(pWriter->aBlockIdx);
1836
  tDestroyTSchema(pWriter->skmTable.pTSchema);
H
Hongze Cheng 已提交
1837
  tBlockDataDestroy(&pWriter->inData, 1);
H
Hongze Cheng 已提交
1838

H
Hongze Cheng 已提交
1839 1840 1841
  for (int32_t iBuf = 0; iBuf < sizeof(pWriter->aBuf) / sizeof(uint8_t*); iBuf++) {
    tFree(pWriter->aBuf[iBuf]);
  }
S
Shengliang Guan 已提交
1842
  tsdbInfo("vgId:%d, %s done", TD_VID(pWriter->pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
1843 1844 1845 1846 1847
  taosMemoryFree(pWriter);
  *ppWriter = NULL;
  return code;

_err:
S
Shengliang Guan 已提交
1848
  tsdbError("vgId:%d, vnode snapshot tsdb writer close for %s failed since %s", TD_VID(pWriter->pTsdb->pVnode),
C
Cary Xu 已提交
1849 1850 1851
            pWriter->pTsdb->path, tstrerror(code));
  taosMemoryFree(pWriter);
  *ppWriter = NULL;
H
Hongze Cheng 已提交
1852 1853 1854
  return code;
}

H
Hongze Cheng 已提交
1855 1856 1857
int32_t tsdbSnapWrite(STsdbSnapWriter* pWriter, SSnapDataHdr* pHdr) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1858

C
Cary Xu 已提交
1859
  if (pHdr->type == SNAP_DATA_TSDB) {
H
Hongze Cheng 已提交
1860 1861
    code = tsdbSnapWriteTimeSeriesData(pWriter, pHdr);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1862
    goto _exit;
H
Hongze Cheng 已提交
1863
  } else if (pWriter->pDataFWriter) {
H
Hongze Cheng 已提交
1864
    code = tsdbSnapWriteFileDataEnd(pWriter);
H
Hongze Cheng 已提交
1865
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1866 1867
  }

C
Cary Xu 已提交
1868
  if (pHdr->type == SNAP_DATA_DEL) {
H
Hongze Cheng 已提交
1869 1870 1871
    code = tsdbSnapWriteDelData(pWriter, pHdr);
    TSDB_CHECK_CODE(code, lino, _exit);
    goto _exit;
H
Hongze Cheng 已提交
1872 1873
  }

H
Hongze Cheng 已提交
1874
_exit:
H
Hongze Cheng 已提交
1875 1876 1877 1878 1879 1880 1881
  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 已提交
1882 1883
  return code;
}