tsdbSnapshot.c 58.2 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 19 20 21 22 23
extern int32_t tsdbReadDataBlockEx(SDataFReader* pReader, SDataBlk* pDataBlk, SBlockData* pBlockData);

// 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 已提交
24
#define TSDB_TOMB_FILE_DATA_ITER 3
H
Hongze Cheng 已提交
25 26 27 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

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

#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 已提交
94 95 96
  pIter->dIter.iBlockIdx = 0;
  pIter->dIter.iDataBlk = 0;
  pIter->dIter.iRow = 0;
H
Hongze Cheng 已提交
97 98 99 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

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

  // 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 已提交
164 165 166 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
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 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
/* 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);
}

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);
  } 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 已提交
248
    if (pIter->dIter.iRow < pIter->dIter.bData.nRow) {
H
Hongze Cheng 已提交
249 250 251
      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 已提交
252
      pIter->dIter.iRow++;
H
Hongze Cheng 已提交
253 254 255 256
      break;
    }

    for (;;) {
H
Hongze Cheng 已提交
257
      if (pIter->dIter.iDataBlk < pIter->dIter.mDataBlk.nItem) {
H
Hongze Cheng 已提交
258 259 260 261 262 263
        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 已提交
264 265
        pIter->dIter.iDataBlk++;
        pIter->dIter.iRow = 0;
H
Hongze Cheng 已提交
266 267 268 269 270

        break;
      }

      for (;;) {
H
Hongze Cheng 已提交
271
        if (pIter->dIter.iBlockIdx < taosArrayGetSize(pIter->dIter.aBlockIdx)) {
H
Hongze Cheng 已提交
272 273 274 275 276
          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 已提交
277 278
          pIter->dIter.iBlockIdx++;
          pIter->dIter.iDataBlk = 0;
H
Hongze Cheng 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

          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 已提交
301
    if (pIter->sIter.iRow < pIter->sIter.bData.nRow) {
H
Hongze Cheng 已提交
302 303 304
      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 已提交
305
      pIter->sIter.iRow++;
H
Hongze Cheng 已提交
306 307 308
      break;
    }

H
Hongze Cheng 已提交
309
    if (pIter->sIter.iSttBlk < taosArrayGetSize(pIter->sIter.aSttBlk)) {
H
Hongze Cheng 已提交
310 311 312 313 314
      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 已提交
315 316 317
      pIter->sIter.iSttBlk++;

      pIter->sIter.iRow = 0;
H
Hongze Cheng 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
    } 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 已提交
349
// STsdbSnapReader ========================================
H
Hongze Cheng 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
typedef enum { SNAP_DATA_FILE_ITER = 0, SNAP_STT_FILE_ITER } EFIterT;
typedef struct {
  SRBTreeNode n;
  SRowInfo    rInfo;
  EFIterT     type;
  union {
    struct {
      SArray*    aBlockIdx;
      int32_t    iBlockIdx;
      SBlockIdx* pBlockIdx;
      SMapData   mBlock;
      int32_t    iBlock;
    };  // .data file
    struct {
      int32_t iStt;
      SArray* aSttBlk;
      int32_t iSttBlk;
    };  // .stt file
  };
  SBlockData bData;
  int32_t    iRow;
} SFDataIter;

H
Hongze Cheng 已提交
373
struct STsdbSnapReader {
H
more  
Hongze Cheng 已提交
374 375 376
  STsdb*  pTsdb;
  int64_t sver;
  int64_t ever;
H
Hongze Cheng 已提交
377
  STsdbFS fs;
C
Cary Xu 已提交
378
  int8_t  type;
H
more  
Hongze Cheng 已提交
379
  // for data file
H
Hongze Cheng 已提交
380 381
  int8_t        dataDone;
  int32_t       fid;
H
more  
Hongze Cheng 已提交
382
  SDataFReader* pDataFReader;
H
Hongze Cheng 已提交
383 384
  SFDataIter*   pIter;
  SRBTree       rbt;
H
Hongze Cheng 已提交
385
  SFDataIter    aFDataIter[TSDB_MAX_STT_TRIGGER + 1];
H
Hongze Cheng 已提交
386 387
  SBlockData    bData;
  SSkmInfo      skmTable;
H
more  
Hongze Cheng 已提交
388
  // for del file
H
Hongze Cheng 已提交
389
  int8_t       delDone;
H
more  
Hongze Cheng 已提交
390
  SDelFReader* pDelFReader;
H
Hongze Cheng 已提交
391
  SArray*      aDelIdx;  // SArray<SDelIdx>
H
Hongze Cheng 已提交
392 393
  int32_t      iDelIdx;
  SArray*      aDelData;  // SArray<SDelData>
H
Hongze Cheng 已提交
394
  uint8_t*     aBuf[5];
H
Hongze Cheng 已提交
395 396
};

H
Hongze Cheng 已提交
397 398
extern int32_t tsdbUpdateTableSchema(SMeta* pMeta, int64_t suid, int64_t uid, SSkmInfo* pSkmInfo);

H
Hongze Cheng 已提交
399 400 401 402 403 404 405
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 已提交
406
static int32_t tsdbSnapReadOpenFile(STsdbSnapReader* pReader) {
H
Hongze Cheng 已提交
407
  int32_t code = 0;
H
add log  
Hongze Cheng 已提交
408
  int32_t lino = 0;
H
Hongze Cheng 已提交
409

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

H
Hongze Cheng 已提交
414 415
  pReader->fid = pSet->fid;
  code = tsdbDataFReaderOpen(&pReader->pDataFReader, pReader->pTsdb, pSet);
H
add log  
Hongze Cheng 已提交
416
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
417 418

  pReader->pIter = NULL;
H
Hongze Cheng 已提交
419
  tRBTreeCreate(&pReader->rbt, tFDataIterCmprFn);
H
Hongze Cheng 已提交
420 421 422 423 424 425

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

  code = tsdbReadBlockIdx(pReader->pDataFReader, pIter->aBlockIdx);
H
add log  
Hongze Cheng 已提交
426
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
427 428 429 430

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

H
Hongze Cheng 已提交
431
    code = tsdbReadDataBlk(pReader->pDataFReader, pIter->pBlockIdx, &pIter->mBlock);
H
add log  
Hongze Cheng 已提交
432
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
433 434 435 436 437 438

    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 已提交
439

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

443 444
      ASSERT(pIter->pBlockIdx->suid == pIter->bData.suid);
      ASSERT(pIter->pBlockIdx->uid == pIter->bData.uid);
H
Hongze Cheng 已提交
445

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

H
Hongze Cheng 已提交
449 450 451 452 453 454
        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 已提交
455
      }
H
Hongze Cheng 已提交
456
    }
H
Hongze Cheng 已提交
457

H
Hongze Cheng 已提交
458
    continue;
H
Hongze Cheng 已提交
459

H
Hongze Cheng 已提交
460 461 462 463
  _add_iter_and_break:
    tRBTreePut(&pReader->rbt, (SRBTreeNode*)pIter);
    break;
  }
H
Hongze Cheng 已提交
464

H
Hongze Cheng 已提交
465 466 467 468 469
  // .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 已提交
470

H
Hongze Cheng 已提交
471
    code = tsdbReadSttBlk(pReader->pDataFReader, iStt, pIter->aSttBlk);
H
add log  
Hongze Cheng 已提交
472
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
473 474 475 476 477 478 479

    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 已提交
480
      code = tsdbReadSttBlockEx(pReader->pDataFReader, iStt, pSttBlk, &pIter->bData);
H
add log  
Hongze Cheng 已提交
481
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
482 483 484 485 486 487

      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 已提交
488
          pIter->rInfo.uid = pIter->bData.uid ? pIter->bData.uid : pIter->bData.aUid[pIter->iRow];
H
Hongze Cheng 已提交
489 490 491 492
          pIter->rInfo.row = tsdbRowFromBlockData(&pIter->bData, pIter->iRow);
          goto _add_iter;
        }
      }
H
Hongze Cheng 已提交
493
    }
H
Hongze Cheng 已提交
494

H
Hongze Cheng 已提交
495
    continue;
H
Hongze Cheng 已提交
496

H
Hongze Cheng 已提交
497 498 499 500
  _add_iter:
    tRBTreePut(&pReader->rbt, (SRBTreeNode*)pIter);
    pIter++;
  }
H
Hongze Cheng 已提交
501

H
add log  
Hongze Cheng 已提交
502 503 504 505 506 507 508
_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 已提交
509 510 511 512 513 514 515
  return code;
}

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

  if (pReader->pIter) {
H
Hongze Cheng 已提交
516
    SFDataIter* pIter = NULL;
H
Hongze Cheng 已提交
517 518
    while (true) {
    _find_row:
H
Hongze Cheng 已提交
519
      pIter = pReader->pIter;
H
Hongze Cheng 已提交
520 521 522 523
      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 已提交
524
          pIter->rInfo.suid = pIter->bData.suid;
H
Hongze Cheng 已提交
525 526 527
          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 已提交
528
        }
H
Hongze Cheng 已提交
529
      }
H
Hongze Cheng 已提交
530

H
Hongze Cheng 已提交
531 532 533 534 535
      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 已提交
536

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

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

H
Hongze Cheng 已提交
542 543
            pIter->iRow = -1;
            goto _find_row;
H
Hongze Cheng 已提交
544
          }
H
Hongze Cheng 已提交
545 546 547 548 549

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

          pIter->pBlockIdx = (SBlockIdx*)taosArrayGet(pIter->aBlockIdx, pIter->iBlockIdx);
H
Hongze Cheng 已提交
550
          code = tsdbReadDataBlk(pReader->pDataFReader, pIter->pBlockIdx, &pIter->mBlock);
H
Hongze Cheng 已提交
551 552
          if (code) goto _err;
          pIter->iBlock = -1;
H
Hongze Cheng 已提交
553
        }
H
Hongze Cheng 已提交
554

H
Hongze Cheng 已提交
555
        pReader->pIter = NULL;
H
Hongze Cheng 已提交
556
        break;
H
Hongze Cheng 已提交
557 558 559
      } 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 已提交
560

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

H
Hongze Cheng 已提交
563
          code = tsdbReadSttBlockEx(pReader->pDataFReader, pIter->iStt, pSttBlk, &pIter->bData);
H
Hongze Cheng 已提交
564 565 566 567
          if (code) goto _err;

          pIter->iRow = -1;
          goto _find_row;
H
Hongze Cheng 已提交
568
        }
H
Hongze Cheng 已提交
569 570

        pReader->pIter = NULL;
H
Hongze Cheng 已提交
571
        break;
H
Hongze Cheng 已提交
572
      } else {
573
        ASSERT(0);
H
Hongze Cheng 已提交
574 575 576 577 578 579 580 581 582 583 584
      }
    }

  _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 {
585
        ASSERT(c);
H
Hongze Cheng 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
      }
    }
  }

  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 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616
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 已提交
617 618 619
static int32_t tsdbSnapCmprData(STsdbSnapReader* pReader, uint8_t** ppData) {
  int32_t code = 0;

620
  ASSERT(pReader->bData.nRow);
H
Hongze Cheng 已提交
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651

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

static int32_t tsdbSnapReadData(STsdbSnapReader* pReader, uint8_t** ppData) {
  int32_t code = 0;
H
add log  
Hongze Cheng 已提交
652 653 654
  int32_t lino = 0;

  STsdb* pTsdb = pReader->pTsdb;
H
Hongze Cheng 已提交
655 656 657 658

  while (true) {
    if (pReader->pDataFReader == NULL) {
      code = tsdbSnapReadOpenFile(pReader);
H
add log  
Hongze Cheng 已提交
659
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
660 661 662 663 664 665 666 667 668 669 670 671 672 673
    }

    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 已提交
674
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
675

H
Hongze Cheng 已提交
676
    code = tBlockDataInit(pBlockData, &id, pReader->skmTable.pTSchema, NULL, 0);
H
add log  
Hongze Cheng 已提交
677
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
678 679 680

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

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

      pRowInfo = tsdbSnapGetRow(pReader);
      if (pRowInfo == NULL) {
H
Hongze Cheng 已提交
688 689
        tsdbDataFReaderClose(&pReader->pDataFReader);
        break;
H
Hongze Cheng 已提交
690
      }
H
Hongze Cheng 已提交
691 692

      if (pBlockData->nRow >= 4096) break;
H
Hongze Cheng 已提交
693
    }
H
Hongze Cheng 已提交
694 695

    code = tsdbSnapCmprData(pReader, ppData);
H
add log  
Hongze Cheng 已提交
696
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
697 698

    break;
H
Hongze Cheng 已提交
699 700
  }

H
add log  
Hongze Cheng 已提交
701 702 703 704
_exit:
  if (code) {
    tsdbError("vgId:%d, %s failed since %s, path:%s", TD_VID(pTsdb->pVnode), __func__, tstrerror(code), pTsdb->path);
  }
H
Hongze Cheng 已提交
705 706 707 708
  return code;
}

static int32_t tsdbSnapReadDel(STsdbSnapReader* pReader, uint8_t** ppData) {
H
add log  
Hongze Cheng 已提交
709 710 711
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
712
  STsdb*    pTsdb = pReader->pTsdb;
H
Hongze Cheng 已提交
713
  SDelFile* pDelFile = pReader->fs.pDelFile;
H
Hongze Cheng 已提交
714 715 716 717 718 719 720

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

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

H
Hongze Cheng 已提交
724
    // read index
H
Hongze Cheng 已提交
725
    code = tsdbReadDelIdx(pReader->pDelFReader, pReader->aDelIdx);
H
add log  
Hongze Cheng 已提交
726
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
727 728 729 730

    pReader->iDelIdx = 0;
  }

H
Hongze Cheng 已提交
731 732 733 734 735 736
  while (true) {
    if (pReader->iDelIdx >= taosArrayGetSize(pReader->aDelIdx)) {
      tsdbDelFReaderClose(&pReader->pDelFReader);
      break;
    }

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

H
Hongze Cheng 已提交
739 740
    pReader->iDelIdx++;

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

H
Hongze Cheng 已提交
744
    int32_t size = 0;
H
Hongze Cheng 已提交
745 746 747 748
    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 已提交
749
        size += tPutDelData(NULL, pDelData);
H
Hongze Cheng 已提交
750 751
      }
    }
H
Hongze Cheng 已提交
752
    if (size == 0) continue;
H
Hongze Cheng 已提交
753

H
Hongze Cheng 已提交
754 755 756 757 758
    // 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 已提交
759
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
760
    }
H
Hongze Cheng 已提交
761

H
Hongze Cheng 已提交
762
    SSnapDataHdr* pHdr = (SSnapDataHdr*)(*ppData);
C
Cary Xu 已提交
763
    pHdr->type = SNAP_DATA_DEL;
H
Hongze Cheng 已提交
764
    pHdr->size = size;
H
Hongze Cheng 已提交
765

H
Hongze Cheng 已提交
766 767 768 769 770 771
    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 已提交
772

H
Hongze Cheng 已提交
773 774
      if (pDelData->version < pReader->sver) continue;
      if (pDelData->version > pReader->ever) continue;
H
Hongze Cheng 已提交
775

H
Hongze Cheng 已提交
776
      n += tPutDelData((*ppData) + n, pDelData);
H
Hongze Cheng 已提交
777 778
    }

H
Hongze Cheng 已提交
779
    tsdbInfo("vgId:%d, vnode snapshot tsdb read del data for %s, suid:%" PRId64 " uid:%" PRId64 " size:%d",
C
Cary Xu 已提交
780
             TD_VID(pTsdb->pVnode), pTsdb->path, pDelIdx->suid, pDelIdx->uid, size);
H
Hongze Cheng 已提交
781 782 783

    break;
  }
H
Hongze Cheng 已提交
784 785

_exit:
H
add log  
Hongze Cheng 已提交
786 787 788
  if (code) {
    tsdbError("vgId:%d, %s failed since %s, path:%s", TD_VID(pTsdb->pVnode), __func__, tstrerror(code), pTsdb->path);
  }
H
Hongze Cheng 已提交
789 790
  return code;
}
H
more  
Hongze Cheng 已提交
791

C
Cary Xu 已提交
792
int32_t tsdbSnapReaderOpen(STsdb* pTsdb, int64_t sver, int64_t ever, int8_t type, STsdbSnapReader** ppReader) {
H
Hongze Cheng 已提交
793
  int32_t          code = 0;
H
Hongze Cheng 已提交
794
  int32_t          lino = 0;
H
Hongze Cheng 已提交
795
  STsdbSnapReader* pReader = NULL;
H
Hongze Cheng 已提交
796

H
more  
Hongze Cheng 已提交
797
  // alloc
H
Hongze Cheng 已提交
798
  pReader = (STsdbSnapReader*)taosMemoryCalloc(1, sizeof(*pReader));
H
more  
Hongze Cheng 已提交
799 800
  if (pReader == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
801
    TSDB_CHECK_CODE(code, lino, _exit);
H
more  
Hongze Cheng 已提交
802 803 804 805
  }
  pReader->pTsdb = pTsdb;
  pReader->sver = sver;
  pReader->ever = ever;
C
Cary Xu 已提交
806
  pReader->type = type;
H
more  
Hongze Cheng 已提交
807

H
Hongze Cheng 已提交
808 809 810
  code = taosThreadRwlockRdlock(&pTsdb->rwLock);
  if (code) {
    code = TAOS_SYSTEM_ERROR(code);
H
Hongze Cheng 已提交
811
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
812 813 814 815 816
  }

  code = tsdbFSRef(pTsdb, &pReader->fs);
  if (code) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
817
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
818 819 820 821 822
  }

  code = taosThreadRwlockUnlock(&pTsdb->rwLock);
  if (code) {
    code = TAOS_SYSTEM_ERROR(code);
H
Hongze Cheng 已提交
823
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
824 825
  }

H
Hongze Cheng 已提交
826
  // data
H
Hongze Cheng 已提交
827
  pReader->fid = INT32_MIN;
H
Hongze Cheng 已提交
828 829 830 831 832 833 834
  for (int32_t iIter = 0; iIter < sizeof(pReader->aFDataIter) / sizeof(pReader->aFDataIter[0]); iIter++) {
    SFDataIter* pIter = &pReader->aFDataIter[iIter];

    if (iIter == 0) {
      pIter->aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
      if (pIter->aBlockIdx == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
835
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
836 837 838 839 840
      }
    } else {
      pIter->aSttBlk = taosArrayInit(0, sizeof(SSttBlk));
      if (pIter->aSttBlk == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
841
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
842 843 844 845
      }
    }

    code = tBlockDataCreate(&pIter->bData);
H
Hongze Cheng 已提交
846
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
847
  }
H
Hongze Cheng 已提交
848 849

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

H
Hongze Cheng 已提交
852
  // del
H
Hongze Cheng 已提交
853 854 855
  pReader->aDelIdx = taosArrayInit(0, sizeof(SDelIdx));
  if (pReader->aDelIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
856
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
857 858 859 860
  }
  pReader->aDelData = taosArrayInit(0, sizeof(SDelData));
  if (pReader->aDelData == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
861
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
862 863
  }

H
Hongze Cheng 已提交
864 865
_exit:
  if (code) {
S
Shengliang Guan 已提交
866
    tsdbError("vgId:%d, %s failed at line %d since %s, TSDB path: %s", TD_VID(pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
867
              tstrerror(code), pTsdb->path);
H
Hongze Cheng 已提交
868 869 870 871 872 873 874 875 876 877 878 879 880
    *ppReader = NULL;

    if (pReader) {
      taosArrayDestroy(pReader->aDelData);
      taosArrayDestroy(pReader->aDelIdx);
      tBlockDataDestroy(&pReader->bData, 1);
      tsdbFSDestroy(&pReader->fs);
      taosMemoryFree(pReader);
    }
  } else {
    *ppReader = pReader;
    tsdbInfo("vgId:%d, vnode snapshot tsdb reader opened for %s", TD_VID(pTsdb->pVnode), pTsdb->path);
  }
H
more  
Hongze Cheng 已提交
881
  return code;
H
Hongze Cheng 已提交
882 883
}

H
Hongze Cheng 已提交
884
int32_t tsdbSnapReaderClose(STsdbSnapReader** ppReader) {
H
Hongze Cheng 已提交
885
  int32_t          code = 0;
H
Hongze Cheng 已提交
886 887
  STsdbSnapReader* pReader = *ppReader;

H
Hongze Cheng 已提交
888 889 890 891 892 893 894 895 896 897 898 899 900
  // data
  if (pReader->pDataFReader) tsdbDataFReaderClose(&pReader->pDataFReader);
  for (int32_t iIter = 0; iIter < sizeof(pReader->aFDataIter) / sizeof(pReader->aFDataIter[0]); iIter++) {
    SFDataIter* pIter = &pReader->aFDataIter[iIter];

    if (iIter == 0) {
      taosArrayDestroy(pIter->aBlockIdx);
      tMapDataClear(&pIter->mBlock);
    } else {
      taosArrayDestroy(pIter->aSttBlk);
    }

    tBlockDataDestroy(&pIter->bData, 1);
H
Hongze Cheng 已提交
901
  }
H
Hongze Cheng 已提交
902 903

  tBlockDataDestroy(&pReader->bData, 1);
904
  tDestroyTSchema(pReader->skmTable.pTSchema);
H
Hongze Cheng 已提交
905 906 907

  // del
  if (pReader->pDelFReader) tsdbDelFReaderClose(&pReader->pDelFReader);
H
Hongze Cheng 已提交
908 909 910
  taosArrayDestroy(pReader->aDelIdx);
  taosArrayDestroy(pReader->aDelData);

H
Hongze Cheng 已提交
911 912
  tsdbFSUnref(pReader->pTsdb, &pReader->fs);

S
Shengliang Guan 已提交
913
  tsdbInfo("vgId:%d, vnode snapshot tsdb reader closed for %s", TD_VID(pReader->pTsdb->pVnode), pReader->pTsdb->path);
H
Hongze Cheng 已提交
914

H
Hongze Cheng 已提交
915 916 917 918
  for (int32_t iBuf = 0; iBuf < sizeof(pReader->aBuf) / sizeof(pReader->aBuf[0]); iBuf++) {
    tFree(pReader->aBuf[iBuf]);
  }

H
Hongze Cheng 已提交
919 920
  taosMemoryFree(pReader);
  *ppReader = NULL;
H
Hongze Cheng 已提交
921 922 923 924
  return code;
}

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

H
Hongze Cheng 已提交
928 929
  *ppData = NULL;

H
Hongze Cheng 已提交
930
  // read data file
H
Hongze Cheng 已提交
931 932
  if (!pReader->dataDone) {
    code = tsdbSnapReadData(pReader, ppData);
H
add log  
Hongze Cheng 已提交
933 934 935
    TSDB_CHECK_CODE(code, lino, _exit);
    if (*ppData) {
      goto _exit;
H
Hongze Cheng 已提交
936
    } else {
H
add log  
Hongze Cheng 已提交
937
      pReader->dataDone = 1;
H
Hongze Cheng 已提交
938 939
    }
  }
H
Hongze Cheng 已提交
940 941

  // read del file
H
Hongze Cheng 已提交
942 943
  if (!pReader->delDone) {
    code = tsdbSnapReadDel(pReader, ppData);
H
add log  
Hongze Cheng 已提交
944 945 946
    TSDB_CHECK_CODE(code, lino, _exit);
    if (*ppData) {
      goto _exit;
H
Hongze Cheng 已提交
947
    } else {
H
add log  
Hongze Cheng 已提交
948
      pReader->delDone = 1;
H
Hongze Cheng 已提交
949 950
    }
  }
H
Hongze Cheng 已提交
951

H
Hongze Cheng 已提交
952
_exit:
H
add log  
Hongze Cheng 已提交
953 954 955 956 957 958
  if (code) {
    tsdbError("vgId:%d, %s failed since %s, path:%s", TD_VID(pReader->pTsdb->pVnode), __func__, tstrerror(code),
              pReader->pTsdb->path);
  } else {
    tsdbDebug("vgId:%d, %s done, path:%s", TD_VID(pReader->pTsdb->pVnode), __func__, pReader->pTsdb->path);
  }
H
more  
Hongze Cheng 已提交
959 960 961
  return code;
}

H
Hongze Cheng 已提交
962
// STsdbSnapWriter ========================================
H
Hongze Cheng 已提交
963
struct STsdbSnapWriter {
H
Hongze Cheng 已提交
964 965 966
  STsdb*   pTsdb;
  int64_t  sver;
  int64_t  ever;
H
Hongze Cheng 已提交
967 968 969 970 971 972
  int32_t  minutes;
  int8_t   precision;
  int32_t  minRow;
  int32_t  maxRow;
  int8_t   cmprAlg;
  int64_t  commitID;
H
Hongze Cheng 已提交
973
  uint8_t* aBuf[5];
H
Hongze Cheng 已提交
974

H
Hongze Cheng 已提交
975
  STsdbFS fs;
H
Hongze Cheng 已提交
976
  TABLEID tbid;
H
Hongze Cheng 已提交
977

H
Hongze Cheng 已提交
978 979 980 981 982 983 984 985 986 987
  // time-series data
  SBlockData inData;

  int32_t  fid;
  SSkmInfo skmTable;

  /* reader */
  SDataFReader*   pDataFReader;
  STsdbDataIter2* iterList;
  STsdbDataIter2* pDIter;
H
Hongze Cheng 已提交
988
  STsdbDataIter2* pSIter;
H
Hongze Cheng 已提交
989 990 991 992 993 994 995 996 997 998 999
  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 已提交
1000 1001 1002 1003 1004
  /* reader */
  SDelFReader*    pDelFReader;
  STsdbDataIter2* pTIter;

  /* writer */
H
Hongze Cheng 已提交
1005
  SDelFWriter* pDelFWriter;
H
Hongze Cheng 已提交
1006
  SArray*      aDelIdx;
H
Hongze Cheng 已提交
1007
  SArray*      aDelData;
H
Hongze Cheng 已提交
1008 1009
};

H
Hongze Cheng 已提交
1010
// SNAP_DATA_TSDB
H
Hongze Cheng 已提交
1011 1012 1013
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 已提交
1014
static int32_t tsdbSnapNextTableData(STsdbSnapWriter* pWriter) {
H
Hongze Cheng 已提交
1015
  int32_t code = 0;
H
Hongze Cheng 已提交
1016
  int32_t lino = 0;
H
Hongze Cheng 已提交
1017

H
Hongze Cheng 已提交
1018
#if 0
1019
  ASSERT(pWriter->dReader.iRow >= pWriter->dReader.bData.nRow);
H
Hongze Cheng 已提交
1020

H
Hongze Cheng 已提交
1021 1022 1023 1024 1025 1026
  if (pWriter->dReader.iBlockIdx < taosArrayGetSize(pWriter->dReader.aBlockIdx)) {
    pWriter->dReader.pBlockIdx = (SBlockIdx*)taosArrayGet(pWriter->dReader.aBlockIdx, pWriter->dReader.iBlockIdx);

    code = tsdbReadDataBlk(pWriter->dReader.pReader, pWriter->dReader.pBlockIdx, &pWriter->dReader.mDataBlk);
    if (code) goto _exit;

H
Hongze Cheng 已提交
1027
    pWriter->dReader.iBlockIdx++;
H
Hongze Cheng 已提交
1028 1029
  } else {
    pWriter->dReader.pBlockIdx = NULL;
H
Hongze Cheng 已提交
1030
    tMapDataReset(&pWriter->dReader.mDataBlk);
H
Hongze Cheng 已提交
1031
  }
H
Hongze Cheng 已提交
1032 1033 1034
  pWriter->dReader.iDataBlk = 0;  // point to the next one
  tBlockDataReset(&pWriter->dReader.bData);
  pWriter->dReader.iRow = 0;
H
Hongze Cheng 已提交
1035
#endif
H
Hongze Cheng 已提交
1036 1037

_exit:
H
Hongze Cheng 已提交
1038 1039 1040
  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 1042 1043 1044 1045
  return code;
}

static int32_t tsdbSnapWriteCopyData(STsdbSnapWriter* pWriter, TABLEID* pId) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1046
  int32_t lino = 0;
H
Hongze Cheng 已提交
1047

H
Hongze Cheng 已提交
1048
#if 0
H
Hongze Cheng 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
  while (true) {
    if (pWriter->dReader.pBlockIdx == NULL) break;
    if (tTABLEIDCmprFn(pWriter->dReader.pBlockIdx, pId) >= 0) break;

    SBlockIdx blkIdx = *pWriter->dReader.pBlockIdx;
    code = tsdbWriteDataBlk(pWriter->dWriter.pWriter, &pWriter->dReader.mDataBlk, &blkIdx);
    if (code) goto _exit;

    if (taosArrayPush(pWriter->dWriter.aBlockIdx, &blkIdx) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }

    code = tsdbSnapNextTableData(pWriter);
    if (code) goto _exit;
H
Hongze Cheng 已提交
1064
  }
H
Hongze Cheng 已提交
1065
#endif
H
Hongze Cheng 已提交
1066

H
Hongze Cheng 已提交
1067
_exit:
H
Hongze Cheng 已提交
1068 1069 1070
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
H
Hongze Cheng 已提交
1071 1072 1073
  return code;
}

H
Hongze Cheng 已提交
1074 1075
static int32_t tsdbSnapWriteTableDataStart(STsdbSnapWriter* pWriter, TABLEID* pId) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1076 1077
  int32_t lino = 0;

H
Hongze Cheng 已提交
1078 1079 1080 1081 1082 1083
  if (pId) {
    pWriter->tbid = *pId;
  } else {
    pWriter->tbid = (TABLEID){INT64_MAX, INT64_MAX};
  }

H
Hongze Cheng 已提交
1084 1085
  if (pWriter->pDIter) {
    STsdbDataIter2* pIter = pWriter->pDIter;
H
Hongze Cheng 已提交
1086

H
Hongze Cheng 已提交
1087
    // assert last table data end
H
Hongze Cheng 已提交
1088 1089 1090
    ASSERT(pIter->dIter.iRow >= pIter->dIter.bData.nRow);
    ASSERT(pIter->dIter.iDataBlk >= pIter->dIter.mDataBlk.nItem);

H
Hongze Cheng 已提交
1091
    for (;;) {
H
Hongze Cheng 已提交
1092
      if (pIter->dIter.iBlockIdx >= taosArrayGetSize(pIter->dIter.aBlockIdx)) {
H
Hongze Cheng 已提交
1093 1094 1095 1096
        pWriter->pDIter = NULL;
        break;
      }

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

H
Hongze Cheng 已提交
1099
      int32_t c = tTABLEIDCmprFn(pBlockIdx, &pWriter->tbid);
H
Hongze Cheng 已提交
1100 1101 1102 1103 1104 1105
      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 已提交
1106
          code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1107 1108 1109 1110 1111 1112
          TSDB_CHECK_CODE(code, lino, _exit);
        }

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

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

H
Hongze Cheng 已提交
1116 1117
        pIter->dIter.iBlockIdx++;
      } else if (c == 0) {
H
Hongze Cheng 已提交
1118 1119 1120
        code = tsdbReadDataBlk(pIter->dIter.pReader, pBlockIdx, &pIter->dIter.mDataBlk);
        TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1121
        pIter->dIter.iDataBlk = 0;
H
Hongze Cheng 已提交
1122
        pIter->dIter.iBlockIdx++;
H
Hongze Cheng 已提交
1123 1124 1125

        break;
      } else {
H
Hongze Cheng 已提交
1126
        pIter->dIter.iDataBlk = pIter->dIter.mDataBlk.nItem;
H
Hongze Cheng 已提交
1127 1128 1129 1130
        break;
      }
    }
  }
H
Hongze Cheng 已提交
1131

H
Hongze Cheng 已提交
1132
  if (pId) {
H
Hongze Cheng 已提交
1133 1134
    code = tsdbUpdateTableSchema(pWriter->pTsdb->pVnode->pMeta, pId->suid, pId->uid, &pWriter->skmTable);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1135

H
Hongze Cheng 已提交
1136
    tMapDataReset(&pWriter->mDataBlk);
H
Hongze Cheng 已提交
1137

H
Hongze Cheng 已提交
1138 1139
    code = tBlockDataInit(&pWriter->bData, pId, pWriter->skmTable.pTSchema, NULL, 0);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1140
  }
H
Hongze Cheng 已提交
1141

H
Hongze Cheng 已提交
1142 1143 1144 1145 1146 1147
  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 已提交
1148 1149 1150 1151 1152
    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 已提交
1153
  }
H
Hongze Cheng 已提交
1154

H
Hongze Cheng 已提交
1155 1156 1157
_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 已提交
1158
  } else {
H
Hongze Cheng 已提交
1159 1160
    tsdbTrace("vgId:%d %s done, suid:%" PRId64 " uid:%" PRId64, TD_VID(pWriter->pTsdb->pVnode), __func__,
              pWriter->tbid.suid, pWriter->tbid.uid);
H
Hongze Cheng 已提交
1161
  }
H
Hongze Cheng 已提交
1162 1163 1164
  return code;
}

H
Hongze Cheng 已提交
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 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
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 已提交
1254 1255
static int32_t tsdbSnapWriteTableDataEnd(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1256
  int32_t lino = 0;
H
Hongze Cheng 已提交
1257

H
Hongze Cheng 已提交
1258 1259 1260
  // write a NULL row to end current table data write
  code = tsdbSnapWriteTableRow(pWriter, NULL);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1261

H
Hongze Cheng 已提交
1262 1263
  if (pWriter->bData.nRow > 0) {
    if (pWriter->bData.nRow < pWriter->minRow) {
H
Hongze Cheng 已提交
1264
      ASSERT(TABLE_SAME_SCHEMA(pWriter->sData.suid, pWriter->sData.uid, pWriter->tbid.suid, pWriter->tbid.uid));
H
Hongze Cheng 已提交
1265
      for (int32_t iRow = 0; iRow < pWriter->bData.nRow; iRow++) {
H
Hongze Cheng 已提交
1266 1267
        code =
            tBlockDataAppendRow(&pWriter->sData, &tsdbRowFromBlockData(&pWriter->bData, iRow), NULL, pWriter->tbid.uid);
H
Hongze Cheng 已提交
1268
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1269

H
Hongze Cheng 已提交
1270 1271 1272 1273
        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 已提交
1274 1275
      }

H
Hongze Cheng 已提交
1276
      tBlockDataClear(&pWriter->bData);
H
Hongze Cheng 已提交
1277 1278 1279
    } else {
      code = tsdbWriteDataBlock(pWriter->pDataFWriter, &pWriter->bData, &pWriter->mDataBlk, pWriter->cmprAlg);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1280 1281 1282
    }
  }

H
Hongze Cheng 已提交
1283 1284 1285
  if (pWriter->mDataBlk.nItem) {
    SBlockIdx* pBlockIdx = taosArrayReserve(pWriter->aBlockIdx, 1);
    if (pBlockIdx == NULL) {
H
Hongze Cheng 已提交
1286
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1287
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1288 1289
    }

H
Hongze Cheng 已提交
1290 1291 1292
    pBlockIdx->suid = pWriter->tbid.suid;
    pBlockIdx->uid = pWriter->tbid.uid;

H
Hongze Cheng 已提交
1293 1294 1295
    code = tsdbWriteDataBlk(pWriter->pDataFWriter, &pWriter->mDataBlk, pBlockIdx);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
1296

H
Hongze Cheng 已提交
1297 1298 1299 1300
_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 已提交
1301 1302 1303
  return code;
}

H
Hongze Cheng 已提交
1304
static int32_t tsdbSnapWriteFileDataStart(STsdbSnapWriter* pWriter, int32_t fid) {
H
Hongze Cheng 已提交
1305
  int32_t code = 0;
H
Hongze Cheng 已提交
1306 1307 1308
  int32_t lino = 0;

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

H
Hongze Cheng 已提交
1310
  STsdb* pTsdb = pWriter->pTsdb;
H
Hongze Cheng 已提交
1311 1312

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

H
Hongze Cheng 已提交
1316 1317 1318 1319
  // open reader
  pWriter->pDataFReader = NULL;
  pWriter->iterList = NULL;
  pWriter->pDIter = NULL;
H
Hongze Cheng 已提交
1320
  pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
1321
  tRBTreeCreate(&pWriter->rbt, tsdbDataIterCmprFn);
H
Hongze Cheng 已提交
1322
  if (pSet) {
H
Hongze Cheng 已提交
1323 1324
    code = tsdbDataFReaderOpen(&pWriter->pDataFReader, pTsdb, pSet);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1325

H
Hongze Cheng 已提交
1326 1327 1328 1329 1330 1331
    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 已提交
1332 1333

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

H
Hongze Cheng 已提交
1337 1338
      if (pWriter->pSIter) {
        code = tsdbSttFileDataIterNext(pWriter->pSIter);
H
Hongze Cheng 已提交
1339 1340 1341
        TSDB_CHECK_CODE(code, lino, _exit);

        // add to tree
H
Hongze Cheng 已提交
1342
        tRBTreePut(&pWriter->rbt, &pWriter->pSIter->rbtn);
H
Hongze Cheng 已提交
1343 1344

        // add to list
H
Hongze Cheng 已提交
1345 1346
        pWriter->pSIter->next = pWriter->iterList;
        pWriter->iterList = pWriter->pSIter;
H
Hongze Cheng 已提交
1347
      }
H
Hongze Cheng 已提交
1348
    }
H
Hongze Cheng 已提交
1349

H
Hongze Cheng 已提交
1350
    pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
1351 1352 1353 1354 1355 1356
  }

  // open writer
  SDiskID diskId;
  if (pSet) {
    diskId = pSet->diskId;
H
Hongze Cheng 已提交
1357
  } else {
H
Hongze Cheng 已提交
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
    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 已提交
1376
  }
H
Hongze Cheng 已提交
1377

H
Hongze Cheng 已提交
1378
  tMapDataReset(&pWriter->mDataBlk);
H
Hongze Cheng 已提交
1379

H
Hongze Cheng 已提交
1380 1381 1382 1383 1384 1385
  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 已提交
1386

H
Hongze Cheng 已提交
1387
  tBlockDataReset(&pWriter->bData);
H
Hongze Cheng 已提交
1388
  tBlockDataReset(&pWriter->sData);
H
Hongze Cheng 已提交
1389 1390 1391 1392 1393 1394 1395 1396

_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 已提交
1397 1398 1399
  return code;
}

H
Hongze Cheng 已提交
1400 1401 1402 1403 1404 1405
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 已提交
1406
    if (pWriter->tbid.uid) {
H
Hongze Cheng 已提交
1407 1408 1409 1410 1411 1412 1413 1414
      code = tsdbSnapWriteTableDataEnd(pWriter);
      TSDB_CHECK_CODE(code, lino, _exit);
    }

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

H
Hongze Cheng 已提交
1415 1416 1417 1418
  if (pRowInfo == NULL) goto _exit;

  code = tsdbSnapWriteTableRow(pWriter, &pRowInfo->row);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1419 1420 1421 1422 1423 1424 1425 1426

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

H
Hongze Cheng 已提交
1431 1432
  if (pWriter->pSIter) {
    code = tsdbDataIterNext2(pWriter->pSIter);
H
Hongze Cheng 已提交
1433
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1434

H
Hongze Cheng 已提交
1435 1436
    if (pWriter->pSIter->rowInfo.suid == 0 && pWriter->pSIter->rowInfo.uid == 0) {
      pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
1437 1438 1439
    } else {
      SRBTreeNode* pNode = tRBTreeMin(&pWriter->rbt);
      if (pNode) {
H
Hongze Cheng 已提交
1440
        int32_t c = tsdbDataIterCmprFn(&pWriter->pSIter->rbtn, pNode);
H
Hongze Cheng 已提交
1441
        if (c > 0) {
H
Hongze Cheng 已提交
1442 1443
          tRBTreePut(&pWriter->rbt, &pWriter->pSIter->rbtn);
          pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
1444 1445 1446 1447 1448
        } else if (c == 0) {
          ASSERT(0);
        }
      }
    }
H
Hongze Cheng 已提交
1449 1450
  }

H
Hongze Cheng 已提交
1451
  if (pWriter->pSIter == NULL) {
H
Hongze Cheng 已提交
1452 1453 1454
    SRBTreeNode* pNode = tRBTreeMin(&pWriter->rbt);
    if (pNode) {
      tRBTreeDrop(&pWriter->rbt, pNode);
H
Hongze Cheng 已提交
1455
      pWriter->pSIter = TSDB_RBTN_TO_DATA_ITER(pNode);
H
Hongze Cheng 已提交
1456
    }
H
Hongze Cheng 已提交
1457 1458
  }

H
Hongze Cheng 已提交
1459
  if (ppRowInfo) {
H
Hongze Cheng 已提交
1460 1461
    if (pWriter->pSIter) {
      *ppRowInfo = &pWriter->pSIter->rowInfo;
H
Hongze Cheng 已提交
1462 1463 1464
    } else {
      *ppRowInfo = NULL;
    }
H
Hongze Cheng 已提交
1465
  }
H
Hongze Cheng 已提交
1466

H
Hongze Cheng 已提交
1467 1468 1469
_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 已提交
1470
  }
H
Hongze Cheng 已提交
1471 1472 1473 1474 1475 1476 1477
  return code;
}

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

H
Hongze Cheng 已提交
1478 1479
  if (pWriter->pSIter) {
    *ppRowInfo = &pWriter->pSIter->rowInfo;
H
Hongze Cheng 已提交
1480 1481 1482 1483 1484
    goto _exit;
  }

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

H
Hongze Cheng 已提交
1486
_exit:
H
Hongze Cheng 已提交
1487 1488 1489
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
H
Hongze Cheng 已提交
1490
  return code;
H
Hongze Cheng 已提交
1491
}
H
Hongze Cheng 已提交
1492

H
Hongze Cheng 已提交
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 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
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 已提交
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
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 已提交
1563
      code = tsdbSnapWriteFileDataEnd(pWriter);
H
Hongze Cheng 已提交
1564 1565 1566
      TSDB_CHECK_CODE(code, lino, _exit);
    }

H
Hongze Cheng 已提交
1567
    code = tsdbSnapWriteFileDataStart(pWriter, fid);
H
Hongze Cheng 已提交
1568 1569 1570 1571 1572 1573 1574
    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 已提交
1575
  for (int32_t iRow = 0; iRow < pWriter->inData.nRow; ++iRow) {
H
Hongze Cheng 已提交
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
    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 已提交
1611 1612 1613
  return code;
}

H
Hongze Cheng 已提交
1614
// SNAP_DATA_DEL
H
Hongze Cheng 已提交
1615
static int32_t tsdbSnapWriteDelTableDataStart(STsdbSnapWriter* pWriter, TABLEID* pId) {
H
Hongze Cheng 已提交
1616
  int32_t code = 0;
H
Hongze Cheng 已提交
1617
  int32_t lino = 0;
H
Hongze Cheng 已提交
1618

H
Hongze Cheng 已提交
1619 1620 1621 1622 1623
  if (pId) {
    pWriter->tbid = *pId;
  } else {
    pWriter->tbid = (TABLEID){.suid = INT64_MAX, .uid = INT64_MAX};
  }
H
Hongze Cheng 已提交
1624

H
Hongze Cheng 已提交
1625 1626
  if (pWriter->pTIter) {
    ASSERT(pWriter->pTIter->tIter.iDelData >= taosArrayGetSize(pWriter->pTIter->tIter.aDelData));
H
Hongze Cheng 已提交
1627

H
Hongze Cheng 已提交
1628 1629 1630 1631
    for (;;) {
      if (pWriter->pTIter->tIter.iDelIdx >= taosArrayGetSize(pWriter->pTIter->tIter.aDelIdx)) {
        break;
      }
H
Hongze Cheng 已提交
1632

H
Hongze Cheng 已提交
1633
      SDelIdx* pDelIdx = taosArrayGet(pWriter->pTIter->tIter.aDelIdx, pWriter->pTIter->tIter.iDelIdx);
H
Hongze Cheng 已提交
1634

H
Hongze Cheng 已提交
1635 1636 1637 1638
      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 已提交
1639

H
Hongze Cheng 已提交
1640 1641 1642 1643 1644 1645 1646 1647
        SDelIdx* pDelIdxNew = taosArrayReserve(pWriter->pTIter->tIter.aDelIdx, 1);
        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 已提交
1648

H
Hongze Cheng 已提交
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
        code = tsdbWriteDelData(pWriter->pDelFWriter, pWriter->pTIter->tIter.aDelData, pDelIdxNew);
        TSDB_CHECK_CODE(code, lino, _exit);

        pWriter->pTIter->tIter.iDelIdx++;
      } else if (c == 0) {
        code = tsdbReadDelData(pWriter->pDelFReader, pDelIdx, pWriter->pTIter->tIter.aDelData);
        TSDB_CHECK_CODE(code, lino, _exit);

        if (taosArrayAddBatch(pWriter->aDelData, pWriter->pTIter->tIter.aDelData->pData,
                              taosArrayGetSize(pWriter->pTIter->tIter.aDelData)) == NULL) {
          code = TSDB_CODE_OUT_OF_MEMORY;
          TSDB_CHECK_CODE(code, lino, _exit);
        }

        pWriter->pTIter->tIter.iDelData = taosArrayGetSize(pWriter->pTIter->tIter.aDelData);
        pWriter->pTIter->tIter.iDelIdx++;
        break;
      } else {
        pWriter->pTIter->tIter.iDelData = taosArrayGetSize(pWriter->pTIter->tIter.aDelData);
        break;
      }
    }
H
Hongze Cheng 已提交
1671 1672
  }

H
Hongze Cheng 已提交
1673 1674
  taosArrayClear(pWriter->aDelData);

H
Hongze Cheng 已提交
1675
_exit:
H
Hongze Cheng 已提交
1676 1677 1678 1679 1680 1681
  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 已提交
1682 1683 1684
  return code;
}

H
Hongze Cheng 已提交
1685
static int32_t tsdbSnapWriteDelTableDataEnd(STsdbSnapWriter* pWriter) {
H
Hongze Cheng 已提交
1686
  int32_t code = 0;
H
Hongze Cheng 已提交
1687
  int32_t lino = 0;
H
Hongze Cheng 已提交
1688

H
Hongze Cheng 已提交
1689 1690 1691 1692 1693 1694
  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 已提交
1695

H
Hongze Cheng 已提交
1696 1697
    pDelIdx->suid = pWriter->tbid.suid;
    pDelIdx->uid = pWriter->tbid.uid;
H
Hongze Cheng 已提交
1698

H
Hongze Cheng 已提交
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
    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 已提交
1720 1721
    }

H
Hongze Cheng 已提交
1722 1723
    code = tsdbSnapWriteDelTableDataStart(pWriter, pId);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1724 1725
  }

H
Hongze Cheng 已提交
1726
  if (pId == NULL) goto _exit;
H
Hongze Cheng 已提交
1727

H
Hongze Cheng 已提交
1728 1729 1730 1731
  int64_t n = 0;
  while (n < size) {
    SDelData delData;
    n += tGetDelData(pData + n, &delData);
H
Hongze Cheng 已提交
1732

H
Hongze Cheng 已提交
1733 1734 1735 1736 1737
    if (taosArrayPush(pWriter->aDelData, &delData) < 0) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  }
H
Hongze Cheng 已提交
1738

H
Hongze Cheng 已提交
1739
  ASSERT(n == size);
H
Hongze Cheng 已提交
1740

H
Hongze Cheng 已提交
1741 1742 1743
_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 已提交
1744
  }
H
Hongze Cheng 已提交
1745 1746
  return code;
}
H
Hongze Cheng 已提交
1747

H
Hongze Cheng 已提交
1748 1749 1750
static int32_t tsdbSnapWriteDelDataStart(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1751

H
Hongze Cheng 已提交
1752 1753
  STsdb*    pTsdb = pWriter->pTsdb;
  SDelFile* pDelFile = pWriter->fs.pDelFile;
H
Hongze Cheng 已提交
1754

H
Hongze Cheng 已提交
1755 1756 1757 1758 1759 1760 1761 1762 1763
  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 已提交
1764
  }
H
Hongze Cheng 已提交
1765

H
Hongze Cheng 已提交
1766 1767 1768
  // writer
  code = tsdbDelFWriterOpen(&pWriter->pDelFWriter, &(SDelFile){.commitID = pWriter->commitID}, pTsdb);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1769

H
Hongze Cheng 已提交
1770
  if ((pWriter->aDelIdx = taosArrayInit(0, sizeof(SDelIdx))) == NULL) {
H
Hongze Cheng 已提交
1771
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1772 1773 1774 1775 1776
    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 已提交
1777 1778
  }

H
Hongze Cheng 已提交
1779 1780 1781 1782 1783 1784
_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 已提交
1785 1786 1787
  return code;
}

H
Hongze Cheng 已提交
1788
static int32_t tsdbSnapWriteDelDataEnd(STsdbSnapWriter* pWriter) {
H
Hongze Cheng 已提交
1789
  int32_t code = 0;
H
Hongze Cheng 已提交
1790
  int32_t lino = 0;
H
Hongze Cheng 已提交
1791

H
Hongze Cheng 已提交
1792
  STsdb* pTsdb = pWriter->pTsdb;
H
Hongze Cheng 已提交
1793

H
Hongze Cheng 已提交
1794 1795 1796
  // end remaining table (TODO)
  code = tsdbSnapWriteDelTableData(pWriter, NULL, NULL, 0);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1797

H
Hongze Cheng 已提交
1798 1799 1800
  // update file-level info
  code = tsdbWriteDelIdx(pWriter->pDelFWriter, pWriter->aDelIdx);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1801

H
Hongze Cheng 已提交
1802
  code = tsdbUpdateDelFileHdr(pWriter->pDelFWriter);
H
Hongze Cheng 已提交
1803
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1804

H
Hongze Cheng 已提交
1805
  code = tsdbFSUpsertDelFile(&pWriter->fs, &pWriter->pDelFWriter->fDel);
H
Hongze Cheng 已提交
1806
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1807 1808

  code = tsdbDelFWriterClose(&pWriter->pDelFWriter, 1);
H
Hongze Cheng 已提交
1809
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1810 1811 1812

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

H
Hongze Cheng 已提交
1816 1817 1818 1819 1820 1821
_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 已提交
1822
  return code;
H
Hongze Cheng 已提交
1823
}
H
Hongze Cheng 已提交
1824

H
Hongze Cheng 已提交
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
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);
  }

  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 已提交
1847 1848 1849
  return code;
}

H
Hongze Cheng 已提交
1850
// APIs
H
Hongze Cheng 已提交
1851
int32_t tsdbSnapWriterOpen(STsdb* pTsdb, int64_t sver, int64_t ever, STsdbSnapWriter** ppWriter) {
H
Hongze Cheng 已提交
1852 1853
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1854 1855

  // alloc
H
Hongze Cheng 已提交
1856
  STsdbSnapWriter* pWriter = (STsdbSnapWriter*)taosMemoryCalloc(1, sizeof(*pWriter));
H
Hongze Cheng 已提交
1857 1858
  if (pWriter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1859
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1860 1861 1862 1863
  }
  pWriter->pTsdb = pTsdb;
  pWriter->sver = sver;
  pWriter->ever = ever;
H
Hongze Cheng 已提交
1864 1865 1866 1867 1868
  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 已提交
1869 1870
  pWriter->commitID = pTsdb->pVnode->state.commitID;

H
Hongze Cheng 已提交
1871 1872 1873
  code = tsdbFSCopy(pTsdb, &pWriter->fs);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1874
  // SNAP_DATA_TSDB
H
Hongze Cheng 已提交
1875 1876 1877
  code = tBlockDataCreate(&pWriter->inData);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1878
  pWriter->fid = INT32_MIN;
H
Hongze Cheng 已提交
1879

H
Hongze Cheng 已提交
1880
  code = tBlockDataCreate(&pWriter->bData);
H
Hongze Cheng 已提交
1881
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1882 1883

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

H
Hongze Cheng 已提交
1886
  // SNAP_DATA_DEL
H
Hongze Cheng 已提交
1887

H
Hongze Cheng 已提交
1888 1889
_exit:
  if (code) {
H
Hongze Cheng 已提交
1890
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1891
    if (pWriter) {
H
Hongze Cheng 已提交
1892
      tBlockDataDestroy(&pWriter->sData, 1);
H
Hongze Cheng 已提交
1893
      tBlockDataDestroy(&pWriter->bData, 1);
H
Hongze Cheng 已提交
1894
      tBlockDataDestroy(&pWriter->inData, 1);
H
Hongze Cheng 已提交
1895
      tsdbFSDestroy(&pWriter->fs);
H
Hongze Cheng 已提交
1896
      pWriter = NULL;
H
Hongze Cheng 已提交
1897 1898
    }
  } else {
H
Hongze Cheng 已提交
1899
    tsdbInfo("vgId:%d %s done, sver:%" PRId64 " ever:%" PRId64, TD_VID(pTsdb->pVnode), __func__, sver, ever);
H
Hongze Cheng 已提交
1900
  }
H
Hongze Cheng 已提交
1901
  *ppWriter = pWriter;
H
Hongze Cheng 已提交
1902 1903 1904
  return code;
}

H
Hongze Cheng 已提交
1905 1906
int32_t tsdbSnapWriterPrepareClose(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1907 1908
  int32_t lino = 0;

H
Hongze Cheng 已提交
1909
  if (pWriter->pDataFWriter) {
H
Hongze Cheng 已提交
1910
    code = tsdbSnapWriteFileDataEnd(pWriter);
H
Hongze Cheng 已提交
1911
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1912 1913
  }

H
Hongze Cheng 已提交
1914 1915 1916 1917
  if (pWriter->pDelFWriter) {
    code = tsdbSnapWriteDelDataEnd(pWriter);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
1918 1919

  code = tsdbFSPrepareCommit(pWriter->pTsdb, &pWriter->fs);
H
Hongze Cheng 已提交
1920
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1921 1922 1923

_exit:
  if (code) {
H
Hongze Cheng 已提交
1924 1925 1926
    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 已提交
1927 1928 1929 1930
  }
  return code;
}

H
Hongze Cheng 已提交
1931
int32_t tsdbSnapWriterClose(STsdbSnapWriter** ppWriter, int8_t rollback) {
H
Hongze Cheng 已提交
1932
  int32_t          code = 0;
H
Hongze Cheng 已提交
1933
  STsdbSnapWriter* pWriter = *ppWriter;
H
Hongze Cheng 已提交
1934
  STsdb*           pTsdb = pWriter->pTsdb;
H
Hongze Cheng 已提交
1935 1936

  if (rollback) {
H
Hongze Cheng 已提交
1937
    tsdbRollbackCommit(pWriter->pTsdb);
H
Hongze Cheng 已提交
1938
  } else {
H
Hongze Cheng 已提交
1939 1940 1941
    // lock
    taosThreadRwlockWrlock(&pTsdb->rwLock);

H
Hongze Cheng 已提交
1942
    code = tsdbFSCommit(pWriter->pTsdb);
H
Hongze Cheng 已提交
1943 1944 1945 1946 1947 1948 1949
    if (code) {
      taosThreadRwlockUnlock(&pTsdb->rwLock);
      goto _err;
    }

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

H
Hongze Cheng 已提交
1952 1953
  // SNAP_DATA_DEL
  taosArrayDestroy(pWriter->aDelData);
H
Hongze Cheng 已提交
1954
  taosArrayDestroy(pWriter->aDelIdx);
H
Hongze Cheng 已提交
1955 1956

  // SNAP_DATA_TSDB
H
Hongze Cheng 已提交
1957
  tBlockDataDestroy(&pWriter->sData, 1);
H
Hongze Cheng 已提交
1958
  tBlockDataDestroy(&pWriter->bData, 1);
H
Hongze Cheng 已提交
1959 1960 1961
  taosArrayDestroy(pWriter->aSttBlk);
  tMapDataClear(&pWriter->mDataBlk);
  taosArrayDestroy(pWriter->aBlockIdx);
1962
  tDestroyTSchema(pWriter->skmTable.pTSchema);
H
Hongze Cheng 已提交
1963
  tBlockDataDestroy(&pWriter->inData, 1);
H
Hongze Cheng 已提交
1964

H
Hongze Cheng 已提交
1965 1966 1967
  for (int32_t iBuf = 0; iBuf < sizeof(pWriter->aBuf) / sizeof(uint8_t*); iBuf++) {
    tFree(pWriter->aBuf[iBuf]);
  }
S
Shengliang Guan 已提交
1968
  tsdbInfo("vgId:%d, %s done", TD_VID(pWriter->pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
1969 1970 1971 1972 1973
  taosMemoryFree(pWriter);
  *ppWriter = NULL;
  return code;

_err:
S
Shengliang Guan 已提交
1974
  tsdbError("vgId:%d, vnode snapshot tsdb writer close for %s failed since %s", TD_VID(pWriter->pTsdb->pVnode),
C
Cary Xu 已提交
1975 1976 1977
            pWriter->pTsdb->path, tstrerror(code));
  taosMemoryFree(pWriter);
  *ppWriter = NULL;
H
Hongze Cheng 已提交
1978 1979 1980
  return code;
}

H
Hongze Cheng 已提交
1981 1982 1983
int32_t tsdbSnapWrite(STsdbSnapWriter* pWriter, SSnapDataHdr* pHdr) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1984

C
Cary Xu 已提交
1985
  if (pHdr->type == SNAP_DATA_TSDB) {
H
Hongze Cheng 已提交
1986 1987
    code = tsdbSnapWriteTimeSeriesData(pWriter, pHdr);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1988
    goto _exit;
H
Hongze Cheng 已提交
1989
  } else if (pWriter->pDataFWriter) {
H
Hongze Cheng 已提交
1990
    code = tsdbSnapWriteFileDataEnd(pWriter);
H
Hongze Cheng 已提交
1991
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1992 1993
  }

C
Cary Xu 已提交
1994
  if (pHdr->type == SNAP_DATA_DEL) {
H
Hongze Cheng 已提交
1995 1996 1997
    code = tsdbSnapWriteDelData(pWriter, pHdr);
    TSDB_CHECK_CODE(code, lino, _exit);
    goto _exit;
H
Hongze Cheng 已提交
1998 1999
  }

H
Hongze Cheng 已提交
2000
_exit:
H
Hongze Cheng 已提交
2001 2002 2003 2004 2005 2006 2007
  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 已提交
2008 2009
  return code;
}