tsdbSnapshot.c 56.9 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "tsdb.h"

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

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

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

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

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
113 114 115
  pIter->dIter.iBlockIdx = 0;
  pIter->dIter.iDataBlk = 0;
  pIter->dIter.iRow = 0;
H
Hongze Cheng 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/* seek */

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

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

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

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

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

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

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

        break;
      }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/* get */

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

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

  // timeseries data
  int8_t  dataDone;
  int32_t fid;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

_exit:
  return code;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    break;
  }
H
Hongze Cheng 已提交
733

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
927
  STsdbSnapReader* pReader = *ppReader;
H
Hongze Cheng 已提交
928
  STsdb*           pTsdb = pReader->pTsdb;
H
Hongze Cheng 已提交
929

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

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

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

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

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

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

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

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

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

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

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

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

  int32_t  fid;
  SSkmInfo skmTable;

  /* reader */
  SDataFReader*   pDataFReader;
  STsdbDataIter2* iterList;
  STsdbDataIter2* pDIter;
H
Hongze Cheng 已提交
1032
  STsdbDataIter2* pSIter;
H
Hongze Cheng 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
  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 已提交
1044 1045 1046 1047 1048
  /* reader */
  SDelFReader*    pDelFReader;
  STsdbDataIter2* pTIter;

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
1080
      int32_t c = tTABLEIDCmprFn(pBlockIdx, &pWriter->tbid);
H
Hongze Cheng 已提交
1081 1082 1083 1084 1085 1086
      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 已提交
1087
          code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1088 1089 1090 1091 1092 1093
          TSDB_CHECK_CODE(code, lino, _exit);
        }

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
1123 1124 1125 1126 1127 1128
  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 已提交
1129 1130 1131 1132 1133
    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 已提交
1134
  }
H
Hongze Cheng 已提交
1135

H
Hongze Cheng 已提交
1136 1137 1138
_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 已提交
1139
  } else {
H
Hongze Cheng 已提交
1140 1141
    tsdbTrace("vgId:%d %s done, suid:%" PRId64 " uid:%" PRId64, TD_VID(pWriter->pTsdb->pVnode), __func__,
              pWriter->tbid.suid, pWriter->tbid.uid);
H
Hongze Cheng 已提交
1142
  }
H
Hongze Cheng 已提交
1143 1144 1145
  return code;
}

H
Hongze Cheng 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
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);
          }

1208
          tMapDataPutItem(&pWriter->mDataBlk, &dataBlk, tPutDataBlk);
H
Hongze Cheng 已提交
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
          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 已提交
1235 1236
static int32_t tsdbSnapWriteTableDataEnd(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1237
  int32_t lino = 0;
H
Hongze Cheng 已提交
1238

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

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

H
Hongze Cheng 已提交
1251 1252 1253 1254
        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 已提交
1255 1256
      }

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

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

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

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

H
Hongze Cheng 已提交
1278 1279 1280 1281
_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 已提交
1282 1283 1284
  return code;
}

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

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

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

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

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

H
Hongze Cheng 已提交
1307 1308 1309 1310 1311 1312
    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 已提交
1313 1314

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

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

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

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

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

  // open writer
  SDiskID diskId;
  if (pSet) {
    diskId = pSet->diskId;
H
Hongze Cheng 已提交
1338
  } else {
H
Hongze Cheng 已提交
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
    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 已提交
1357
  }
H
Hongze Cheng 已提交
1358

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

H
Hongze Cheng 已提交
1361 1362 1363 1364 1365 1366
  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 已提交
1367

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

_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 已提交
1378 1379 1380
  return code;
}

H
Hongze Cheng 已提交
1381 1382 1383 1384 1385 1386
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 已提交
1387
    if (pWriter->tbid.uid) {
H
Hongze Cheng 已提交
1388 1389 1390 1391 1392 1393 1394 1395
      code = tsdbSnapWriteTableDataEnd(pWriter);
      TSDB_CHECK_CODE(code, lino, _exit);
    }

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
1448 1449 1450
_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 已提交
1451
  }
H
Hongze Cheng 已提交
1452 1453 1454 1455 1456 1457 1458
  return code;
}

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

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

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

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

H
Hongze Cheng 已提交
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
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 已提交
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
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 已提交
1544
      code = tsdbSnapWriteFileDataEnd(pWriter);
H
Hongze Cheng 已提交
1545 1546 1547
      TSDB_CHECK_CODE(code, lino, _exit);
    }

H
Hongze Cheng 已提交
1548
    code = tsdbSnapWriteFileDataStart(pWriter, fid);
H
Hongze Cheng 已提交
1549 1550 1551 1552 1553 1554 1555
    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 已提交
1556
  for (int32_t iRow = 0; iRow < pWriter->inData.nRow; ++iRow) {
H
Hongze Cheng 已提交
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
    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 已提交
1592 1593 1594
  return code;
}

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

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

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

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

H
Hongze Cheng 已提交
1612 1613 1614 1615
      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 已提交
1616

H
Hongze Cheng 已提交
1617
        SDelIdx* pDelIdxNew = taosArrayReserve(pWriter->aDelIdx, 1);
H
Hongze Cheng 已提交
1618 1619 1620 1621 1622 1623 1624
        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 已提交
1625

H
Hongze Cheng 已提交
1626 1627 1628 1629 1630
        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 已提交
1631
        code = tsdbReadDelData(pWriter->pDelFReader, pDelIdx, pWriter->aDelData);
H
Hongze Cheng 已提交
1632 1633 1634 1635 1636 1637 1638 1639
        TSDB_CHECK_CODE(code, lino, _exit);

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

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

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

H
Hongze Cheng 已提交
1656 1657 1658 1659 1660 1661
  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 已提交
1662

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

H
Hongze Cheng 已提交
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
    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 已提交
1687 1688
    }

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

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

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

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

H
Hongze Cheng 已提交
1707 1708 1709
_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 已提交
1710
  }
H
Hongze Cheng 已提交
1711 1712
  return code;
}
H
Hongze Cheng 已提交
1713

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

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

H
Hongze Cheng 已提交
1721 1722 1723 1724 1725 1726 1727 1728 1729
  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 已提交
1730
  }
H
Hongze Cheng 已提交
1731

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

H
Hongze Cheng 已提交
1736
  if ((pWriter->aDelIdx = taosArrayInit(0, sizeof(SDelIdx))) == NULL) {
H
Hongze Cheng 已提交
1737
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1738 1739 1740 1741 1742
    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 已提交
1743 1744
  }

H
Hongze Cheng 已提交
1745 1746 1747 1748 1749 1750
_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 已提交
1751 1752 1753
  return code;
}

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
1787 1788 1789 1790 1791 1792
_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 已提交
1793
  return code;
H
Hongze Cheng 已提交
1794
}
H
Hongze Cheng 已提交
1795

H
Hongze Cheng 已提交
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
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 已提交
1808
  // do write del data
H
Hongze Cheng 已提交
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
  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 已提交
1819 1820 1821
  return code;
}

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

  // alloc
H
Hongze Cheng 已提交
1828
  STsdbSnapWriter* pWriter = (STsdbSnapWriter*)taosMemoryCalloc(1, sizeof(*pWriter));
H
Hongze Cheng 已提交
1829 1830
  if (pWriter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1831
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1832 1833 1834 1835
  }
  pWriter->pTsdb = pTsdb;
  pWriter->sver = sver;
  pWriter->ever = ever;
H
Hongze Cheng 已提交
1836 1837 1838 1839 1840
  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 已提交
1841 1842
  pWriter->commitID = pTsdb->pVnode->state.commitID;

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

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

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

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

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

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

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

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

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

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

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

_exit:
  if (code) {
H
Hongze Cheng 已提交
1896 1897 1898
    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 已提交
1899 1900 1901 1902
  }
  return code;
}

H
Hongze Cheng 已提交
1903
int32_t tsdbSnapWriterClose(STsdbSnapWriter** ppWriter, int8_t rollback) {
H
Hongze Cheng 已提交
1904 1905 1906
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1907
  STsdbSnapWriter* pWriter = *ppWriter;
H
Hongze Cheng 已提交
1908
  STsdb*           pTsdb = pWriter->pTsdb;
H
Hongze Cheng 已提交
1909 1910

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

H
Hongze Cheng 已提交
1916
    code = tsdbFSCommit(pWriter->pTsdb);
H
Hongze Cheng 已提交
1917 1918
    if (code) {
      taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1919
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1920 1921 1922 1923
    }

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

H
Hongze Cheng 已提交
1926 1927
  // SNAP_DATA_DEL
  taosArrayDestroy(pWriter->aDelData);
H
Hongze Cheng 已提交
1928
  taosArrayDestroy(pWriter->aDelIdx);
H
Hongze Cheng 已提交
1929 1930

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

H
Hongze Cheng 已提交
1939 1940 1941
  for (int32_t iBuf = 0; iBuf < sizeof(pWriter->aBuf) / sizeof(uint8_t*); iBuf++) {
    tFree(pWriter->aBuf[iBuf]);
  }
H
Hongze Cheng 已提交
1942
  tsdbFSDestroy(&pWriter->fs);
H
Hongze Cheng 已提交
1943 1944 1945
  taosMemoryFree(pWriter);
  *ppWriter = NULL;

H
Hongze Cheng 已提交
1946 1947 1948 1949 1950 1951
_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 已提交
1952 1953 1954
  return code;
}

H
Hongze Cheng 已提交
1955 1956 1957
int32_t tsdbSnapWrite(STsdbSnapWriter* pWriter, SSnapDataHdr* pHdr) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1958

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

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

H
Hongze Cheng 已提交
1974
_exit:
H
Hongze Cheng 已提交
1975 1976 1977 1978 1979 1980 1981
  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 已提交
1982 1983
  return code;
}