tsdbSnapshot.c 18.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
// STsdbSnapReader ========================================
H
Hongze Cheng 已提交
19
struct STsdbSnapReader {
H
more  
Hongze Cheng 已提交
20 21 22 23
  STsdb*  pTsdb;
  int64_t sver;
  int64_t ever;
  // for data file
H
Hongze Cheng 已提交
24 25
  int8_t        dataDone;
  int32_t       fid;
H
more  
Hongze Cheng 已提交
26
  SDataFReader* pDataFReader;
H
Hongze Cheng 已提交
27
  SArray*       aBlockIdx;  // SArray<SBlockIdx>
H
Hongze Cheng 已提交
28 29
  int32_t       iBlockIdx;
  SBlockIdx*    pBlockIdx;
H
Hongze Cheng 已提交
30
  SMapData      mBlock;  // SMapData<SBlock>
H
Hongze Cheng 已提交
31
  int32_t       iBlock;
H
Hongze Cheng 已提交
32
  SBlockData    blkData;
H
more  
Hongze Cheng 已提交
33
  // for del file
H
Hongze Cheng 已提交
34
  int8_t       delDone;
H
more  
Hongze Cheng 已提交
35
  SDelFReader* pDelFReader;
H
Hongze Cheng 已提交
36 37 38
  int32_t      iDelIdx;
  SArray*      aDelIdx;   // SArray<SDelIdx>
  SArray*      aDelData;  // SArray<SDelData>
H
Hongze Cheng 已提交
39 40
};

H
Hongze Cheng 已提交
41 42 43
static int32_t tsdbSnapReadData(STsdbSnapReader* pReader, uint8_t** ppData) {
  int32_t code = 0;

H
Hongze Cheng 已提交
44 45 46
  while (true) {
    if (pReader->pDataFReader == NULL) {
      SDFileSet* pSet = NULL;
H
Hongze Cheng 已提交
47

H
Hongze Cheng 已提交
48 49 50 51 52
      // search the next data file set to read (todo)
      if (0 /* TODO */) {
        code = TSDB_CODE_VND_READ_END;
        goto _exit;
      }
H
Hongze Cheng 已提交
53

H
Hongze Cheng 已提交
54 55 56 57 58 59 60
      // open
      code = tsdbDataFReaderOpen(&pReader->pDataFReader, pReader->pTsdb, pSet);
      if (code) goto _err;

      // SBlockIdx
      code = tsdbReadBlockIdx(pReader->pDataFReader, pReader->aBlockIdx, NULL);
      if (code) goto _err;
H
Hongze Cheng 已提交
61

H
Hongze Cheng 已提交
62 63 64
      pReader->iBlockIdx = 0;
      pReader->pBlockIdx = NULL;
    }
H
Hongze Cheng 已提交
65

H
Hongze Cheng 已提交
66 67 68 69 70 71
    while (true) {
      if (pReader->pBlockIdx == NULL) {
        if (pReader->iBlockIdx >= taosArrayGetSize(pReader->aBlockIdx)) {
          tsdbDataFReaderClose(&pReader->pDataFReader);
          break;
        }
H
Hongze Cheng 已提交
72

H
Hongze Cheng 已提交
73 74
        pReader->pBlockIdx = (SBlockIdx*)taosArrayGet(pReader->aBlockIdx, pReader->iBlockIdx);
        pReader->iBlockIdx++;
H
Hongze Cheng 已提交
75

H
Hongze Cheng 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        // SBlock
        code = tsdbReadBlock(pReader->pDataFReader, pReader->pBlockIdx, &pReader->mBlock, NULL);
        if (code) goto _err;

        pReader->iBlock = 0;
      }

      while (true) {
        SBlock  block;
        SBlock* pBlock = &block;

        if (pReader->iBlock >= pReader->mBlock.nItem) {
          pReader->pBlockIdx = NULL;
          break;
        }

        tMapDataGetItemByIdx(&pReader->mBlock, pReader->iBlock, pBlock, tGetBlock);
        pReader->iBlock++;
H
Hongze Cheng 已提交
94

H
Hongze Cheng 已提交
95 96 97
        if ((pBlock->minVersion >= pReader->sver && pBlock->minVersion <= pReader->ever) ||
            (pBlock->maxVersion >= pReader->sver && pBlock->maxVersion <= pReader->ever)) {
          // overlap (todo)
H
Hongze Cheng 已提交
98

H
Hongze Cheng 已提交
99 100
          code = tsdbReadBlockData(pReader->pDataFReader, pReader->pBlockIdx, pBlock, &pReader->blkData, NULL, NULL);
          if (code) goto _err;
H
Hongze Cheng 已提交
101

H
Hongze Cheng 已提交
102 103 104
          goto _exit;
        }
      }
H
Hongze Cheng 已提交
105 106 107 108
    }
  }

_exit:
H
Hongze Cheng 已提交
109 110 111 112 113 114 115 116 117 118
  return code;

_err:
  tsdbError("vgId:%d snap read data failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
  return code;
}

static int32_t tsdbSnapReadDel(STsdbSnapReader* pReader, uint8_t** ppData) {
  int32_t   code = 0;
  STsdb*    pTsdb = pReader->pTsdb;
H
Hongze Cheng 已提交
119
  SDelFile* pDelFile = pTsdb->fs->cState->pDelFile;
H
Hongze Cheng 已提交
120 121 122 123 124 125 126 127 128 129 130

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

    // open
    code = tsdbDelFReaderOpen(&pReader->pDelFReader, pDelFile, pTsdb, NULL);
    if (code) goto _err;

H
Hongze Cheng 已提交
131
    // read index
H
Hongze Cheng 已提交
132 133 134 135 136 137 138 139
    code = tsdbReadDelIdx(pReader->pDelFReader, pReader->aDelIdx, NULL);
    if (code) goto _err;

    pReader->iDelIdx = 0;
  }

  while (pReader->iDelIdx < taosArrayGetSize(pReader->aDelIdx)) {
    SDelIdx* pDelIdx = (SDelIdx*)taosArrayGet(pReader->aDelIdx, pReader->iDelIdx);
H
Hongze Cheng 已提交
140
    int32_t  size = 0;
H
Hongze Cheng 已提交
141

H
Hongze Cheng 已提交
142 143
    pReader->iDelIdx++;

H
Hongze Cheng 已提交
144 145 146 147 148 149 150
    code = tsdbReadDelData(pReader->pDelFReader, pDelIdx, pReader->aDelData, NULL);
    if (code) goto _err;

    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 已提交
151
        size += tPutDelData(NULL, pDelData);
H
Hongze Cheng 已提交
152 153 154
      }
    }

H
Hongze Cheng 已提交
155
    if (size > 0) {
H
Hongze Cheng 已提交
156 157 158 159
      int64_t n = 0;

      size = size + sizeof(SSnapDataHdr) + sizeof(TABLEID);
      code = tRealloc(ppData, size);
H
Hongze Cheng 已提交
160 161
      if (code) goto _err;

H
Hongze Cheng 已提交
162 163 164 165 166 167 168 169 170 171 172
      // SSnapDataHdr
      SSnapDataHdr* pSnapDataHdr = (SSnapDataHdr*)(*ppData + n);
      pSnapDataHdr->type = 1;
      pSnapDataHdr->size = size;  // TODO: size here may incorrect
      n += sizeof(SSnapDataHdr);

      // TABLEID
      TABLEID* pId = (TABLEID*)(*ppData + n);
      pId->suid = pDelIdx->suid;
      pId->uid = pDelIdx->uid;
      n += sizeof(*pId);
H
Hongze Cheng 已提交
173

H
Hongze Cheng 已提交
174
      // DATA
H
Hongze Cheng 已提交
175 176 177 178
      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 已提交
179
          n += tPutDelData(*ppData + n, pDelData);
H
Hongze Cheng 已提交
180 181 182
        }
      }

H
Hongze Cheng 已提交
183 184 185 186 187
      goto _exit;
    }
  }

  code = TSDB_CODE_VND_READ_END;
H
Hongze Cheng 已提交
188
  tsdbDelFReaderClose(&pReader->pDelFReader);
H
Hongze Cheng 已提交
189 190 191 192 193 194 195 196

_exit:
  return code;

_err:
  tsdbError("vgId:%d snap read del failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  return code;
}
H
more  
Hongze Cheng 已提交
197

H
Hongze Cheng 已提交
198
int32_t tsdbSnapReaderOpen(STsdb* pTsdb, int64_t sver, int64_t ever, STsdbSnapReader** ppReader) {
H
Hongze Cheng 已提交
199 200
  int32_t          code = 0;
  STsdbSnapReader* pReader = NULL;
H
Hongze Cheng 已提交
201

H
more  
Hongze Cheng 已提交
202
  // alloc
H
Hongze Cheng 已提交
203
  pReader = (STsdbSnapReader*)taosMemoryCalloc(1, sizeof(*pReader));
H
more  
Hongze Cheng 已提交
204 205 206 207 208 209 210 211
  if (pReader == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pReader->pTsdb = pTsdb;
  pReader->sver = sver;
  pReader->ever = ever;

H
Hongze Cheng 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  pReader->aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pReader->aBlockIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  pReader->mBlock = tMapDataInit();

  code = tBlockDataInit(&pReader->blkData);
  if (code) goto _err;

  pReader->aDelIdx = taosArrayInit(0, sizeof(SDelIdx));
  if (pReader->aDelIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  pReader->aDelData = taosArrayInit(0, sizeof(SDelData));
  if (pReader->aDelData == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

H
more  
Hongze Cheng 已提交
235 236 237 238 239 240 241
  *ppReader = pReader;
  return code;

_err:
  tsdbError("vgId:%d snapshot reader open failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  *ppReader = NULL;
  return code;
H
Hongze Cheng 已提交
242 243
}

H
Hongze Cheng 已提交
244
int32_t tsdbSnapReaderClose(STsdbSnapReader** ppReader) {
H
Hongze Cheng 已提交
245 246 247 248 249 250 251 252 253 254
  int32_t          code = 0;
  STsdbSnapReader* pReader = *ppReader;

  taosArrayDestroy(pReader->aDelData);
  taosArrayDestroy(pReader->aDelIdx);
  if (pReader->pDelFReader) {
    tsdbDelFReaderClose(&pReader->pDelFReader);
  }
  tBlockDataClear(&pReader->blkData);
  tMapDataClear(&pReader->mBlock);
H
Hongze Cheng 已提交
255
  taosArrayDestroy(pReader->aBlockIdx);
H
Hongze Cheng 已提交
256 257 258 259 260 261
  if (pReader->pDataFReader) {
    tsdbDataFReaderClose(&pReader->pDataFReader);
  }
  taosMemoryFree(pReader);
  *ppReader = NULL;

H
Hongze Cheng 已提交
262 263 264 265
  return code;
}

int32_t tsdbSnapRead(STsdbSnapReader* pReader, uint8_t** ppData) {
H
more  
Hongze Cheng 已提交
266
  int32_t code = 0;
H
Hongze Cheng 已提交
267 268

  // read data file
H
Hongze Cheng 已提交
269 270 271 272 273 274 275 276 277 278 279 280
  if (!pReader->dataDone) {
    code = tsdbSnapReadData(pReader, ppData);
    if (code) {
      if (code == TSDB_CODE_VND_READ_END) {
        pReader->dataDone = 1;
      } else {
        goto _err;
      }
    } else {
      goto _exit;
    }
  }
H
Hongze Cheng 已提交
281 282

  // read del file
H
Hongze Cheng 已提交
283 284 285 286 287 288 289 290 291 292 293 294
  if (!pReader->delDone) {
    code = tsdbSnapReadDel(pReader, ppData);
    if (code) {
      if (code == TSDB_CODE_VND_READ_END) {
        pReader->delDone = 1;
      } else {
        goto _err;
      }
    } else {
      goto _exit;
    }
  }
H
Hongze Cheng 已提交
295

H
Hongze Cheng 已提交
296 297 298
  code = TSDB_CODE_VND_READ_END;

_exit:
H
more  
Hongze Cheng 已提交
299 300 301 302 303 304 305
  return code;

_err:
  tsdbError("vgId:%d snapshot read failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
  return code;
}

H
Hongze Cheng 已提交
306
// STsdbSnapWriter ========================================
H
Hongze Cheng 已提交
307 308 309 310
struct STsdbSnapWriter {
  STsdb*  pTsdb;
  int64_t sver;
  int64_t ever;
H
Hongze Cheng 已提交
311

H
Hongze Cheng 已提交
312 313 314
  // config
  int32_t minutes;
  int8_t  precision;
H
Hongze Cheng 已提交
315

H
Hongze Cheng 已提交
316
  // for data file
H
Hongze Cheng 已提交
317
  int32_t       fid;
H
Hongze Cheng 已提交
318 319
  SDataFReader* pDataFReader;
  SArray*       aBlockIdx;
H
Hongze Cheng 已提交
320
  int32_t       iBlockIdx;
H
Hongze Cheng 已提交
321
  SBlockIdx*    pBlockIdx;
H
Hongze Cheng 已提交
322
  SMapData      mBlock;
H
Hongze Cheng 已提交
323 324 325
  int32_t       iBlock;
  SBlockData    blockData;
  int32_t       iRow;
H
Hongze Cheng 已提交
326

H
Hongze Cheng 已提交
327
  SDataFWriter* pDataFWriter;
H
Hongze Cheng 已提交
328
  SArray*       aBlockIdxN;
H
Hongze Cheng 已提交
329
  SBlockIdx     blockIdx;
H
Hongze Cheng 已提交
330
  SMapData      mBlockN;
H
Hongze Cheng 已提交
331
  SBlock        block;
H
Hongze Cheng 已提交
332
  SBlockData    nBlockData;
H
Hongze Cheng 已提交
333

H
Hongze Cheng 已提交
334
  // for del file
H
Hongze Cheng 已提交
335
  SDelFReader* pDelFReader;
H
Hongze Cheng 已提交
336
  SDelFWriter* pDelFWriter;
H
Hongze Cheng 已提交
337 338 339 340
  int32_t      iDelIdx;
  SArray*      aDelIdx;
  SArray*      aDelData;
  SArray*      aDelIdxN;
H
Hongze Cheng 已提交
341 342
};

H
Hongze Cheng 已提交
343
static int32_t tsdbSnapRollback(STsdbSnapWriter* pWriter) {
H
Hongze Cheng 已提交
344 345 346 347 348
  int32_t code = 0;
  // TODO
  return code;
}

H
Hongze Cheng 已提交
349
static int32_t tsdbSnapCommit(STsdbSnapWriter* pWriter) {
H
Hongze Cheng 已提交
350 351 352 353 354
  int32_t code = 0;
  // TODO
  return code;
}

H
Hongze Cheng 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
static int32_t tsdbSnapWriteDataEnd(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
  STsdb*  pTsdb = pWriter->pTsdb;

  if (pWriter->pDataFWriter == NULL) goto _exit;

  // TODO

  code = tsdbDataFWriterClose(&pWriter->pDataFWriter, 0);
  if (code) goto _err;

  if (pWriter->pDataFReader) {
    code = tsdbDataFReaderClose(&pWriter->pDataFReader);
    if (code) goto _err;
  }

_exit:
  return code;

_err:
  tsdbError("vgId:%d tsdb snapshot writer data end failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  return code;
}

H
Hongze Cheng 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
static int32_t tsdbSnapWriteAppendData(STsdbSnapWriter* pWriter, uint8_t* pData, uint32_t nData) {
  int32_t     code = 0;
  int32_t     iRow = 0;           // todo
  int32_t     nRow = 0;           // todo
  SBlockData* pBlockData = NULL;  // todo

  while (iRow < nRow) {
    code = tBlockDataAppendRow(&pWriter->nBlockData, &tsdbRowFromBlockData(pBlockData, iRow), NULL);
    if (code) goto _err;
  }

  return code;

_err:
  tsdbError("vgId:%d tsdb snapshot write append data failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
  return code;
}

H
more  
Hongze Cheng 已提交
397 398 399 400 401 402
static int32_t tsdbSnapWriteTableData(STsdbSnapWriter* pWriter, uint8_t* pData, uint32_t nData) {
  int32_t code = 0;
  // TODO
  return code;
}

H
Hongze Cheng 已提交
403 404 405 406 407 408 409 410 411 412
static int32_t tsdbSnapWriteData(STsdbSnapWriter* pWriter, uint8_t* pData, uint32_t nData) {
  int32_t code = 0;
  STsdb*  pTsdb = pWriter->pTsdb;
  int64_t suid = 0;  // todo
  int64_t uid = 0;   // todo
  int64_t skey;      // todo
  int64_t ekey;      // todo

  int32_t fid = tsdbKeyFid(skey, pWriter->minutes, pWriter->precision);
  ASSERT(fid == tsdbKeyFid(ekey, pWriter->minutes, pWriter->precision));
H
Hongze Cheng 已提交
413

H
Hongze Cheng 已提交
414
  // begin
H
Hongze Cheng 已提交
415
  if (pWriter->pDataFWriter == NULL || pWriter->fid != fid) {
H
Hongze Cheng 已提交
416 417
    code = tsdbSnapWriteDataEnd(pWriter);
    if (code) goto _err;
H
Hongze Cheng 已提交
418 419 420

    pWriter->fid = fid;
    SDFileSet* pSet = tsdbFSStateGetDFileSet(pTsdb->fs->nState, fid);
H
Hongze Cheng 已提交
421
    // reader
H
Hongze Cheng 已提交
422
    if (pSet) {
H
Hongze Cheng 已提交
423
      // open
H
Hongze Cheng 已提交
424 425 426
      code = tsdbDataFReaderOpen(&pWriter->pDataFReader, pTsdb, pSet);
      if (code) goto _err;

H
Hongze Cheng 已提交
427
      // SBlockIdx
H
Hongze Cheng 已提交
428 429 430 431 432 433
      code = tsdbReadBlockIdx(pWriter->pDataFReader, pWriter->aBlockIdx, NULL);
      if (code) goto _err;
    } else {
      taosArrayClear(pWriter->aBlockIdx);
    }
    pWriter->iBlockIdx = 0;
H
Hongze Cheng 已提交
434 435 436

    // writer
    SDFileSet wSet = {0};
H
Hongze Cheng 已提交
437 438 439 440 441 442 443 444
    if (pSet == NULL) {
      wSet = (SDFileSet){0};  // todo
    } else {
      wSet = (SDFileSet){0};  // todo
    }

    code = tsdbDataFWriterOpen(&pWriter->pDataFWriter, pTsdb, &wSet);
    if (code) goto _err;
H
Hongze Cheng 已提交
445 446

    taosArrayClear(pWriter->aBlockIdxN);
H
Hongze Cheng 已提交
447 448
  }

H
more  
Hongze Cheng 已提交
449 450
  code = tsdbSnapWriteTableData(pWriter, pData, nData);
  if (code) goto _err;
H
Hongze Cheng 已提交
451

H
more  
Hongze Cheng 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
  // // process
  // TABLEID id = {0};    // TODO
  // TSKEY   minKey = 0;  // TODO
  // TSKEY   maxKey = 0;  // TODO

  // while (true) {
  //   if (pWriter->pBlockIdx) {
  //     int32_t c = tTABLEIDCmprFn(&id, pWriter->pBlockIdx);

  //     if (c == 0) {
  //     } else if (c < 0) {
  //       // keep merge
  //     } else {
  //       // code = tsdbSnapWriteTableDataEnd(pWriter);
  //       if (code) goto _err;

  //       pWriter->iBlockIdx++;
  //       if (pWriter->iBlockIdx < taosArrayGetSize(pWriter->aBlockIdx)) {
  //         pWriter->pBlockIdx = (SBlockIdx*)taosArrayGet(pWriter->aBlockIdx, pWriter->iBlockIdx);
  //       } else {
  //         pWriter->pBlockIdx = NULL;
  //       }

  //       if (pWriter->pBlockIdx) {
  //         code = tsdbReadBlock(pWriter->pDataFReader, pWriter->pBlockIdx, &pWriter->mBlock, NULL);
  //         if (code) goto _err;
  //       }
  //     }
  //   } else {
  //     int32_t c = tTABLEIDCmprFn(&id, &pWriter->blockIdx);

  //     if (c == 0) {
  //       // merge commit the block data
  //     } else if (c > 0) {
  //       // code = tsdbSnapWriteTableDataEnd(pWriter);
  //       if (code) goto _err;
  //     } else {
  //       ASSERT(0);
  //     }
  //   }
  // }
H
Hongze Cheng 已提交
493

H
Hongze Cheng 已提交
494 495 496 497 498 499 500 501 502
  return code;

_err:
  tsdbError("vgId:%d tsdb snapshot write data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  return code;
}

static int32_t tsdbSnapWriteDel(STsdbSnapWriter* pWriter, uint8_t* pData, uint32_t nData) {
  int32_t code = 0;
H
Hongze Cheng 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
  STsdb*  pTsdb = pWriter->pTsdb;

  if (pWriter->pDelFWriter == NULL) {
    SDelFile* pDelFile = tsdbFSStateGetDelFile(pTsdb->fs->nState);

    // reader
    if (pDelFile) {
      code = tsdbDelFReaderOpen(&pWriter->pDelFReader, pDelFile, pTsdb, NULL);
      if (code) goto _err;

      code = tsdbReadDelIdx(pWriter->pDelFReader, pWriter->aDelIdx, NULL);
      if (code) goto _err;
    }

    // writer
    SDelFile delFile = {.commitID = pTsdb->pVnode->state.commitID, .offset = 0, .size = 0};
    code = tsdbDelFWriterOpen(&pWriter->pDelFWriter, &delFile, pTsdb);
    if (code) goto _err;
  }

  // process the del data
  TABLEID id = {0};  // todo

  while (true) {
    SDelIdx* pDelIdx = NULL;
    int64_t  n = 0;
    SDelData delData;
    SDelIdx  delIdx;
    int8_t   toBreak = 0;

    if (pWriter->iDelIdx < taosArrayGetSize(pWriter->aDelIdx)) {
      pDelIdx = taosArrayGet(pWriter->aDelIdx, pWriter->iDelIdx);
    }

    if (pDelIdx) {
      int32_t c = tTABLEIDCmprFn(&id, pDelIdx);
      if (c < 0) {
        goto _new_del;
      } else {
        code = tsdbReadDelData(pWriter->pDelFReader, pDelIdx, pWriter->aDelData, NULL);
        if (code) goto _err;

        pWriter->iDelIdx++;
        if (c == 0) {
          toBreak = 1;
H
Hongze Cheng 已提交
548
          delIdx = (SDelIdx){.suid = id.suid, .uid = id.uid};
H
Hongze Cheng 已提交
549 550
          goto _merge_del;
        } else {
H
Hongze Cheng 已提交
551
          delIdx = (SDelIdx){.suid = pDelIdx->suid, .uid = pDelIdx->uid};
H
Hongze Cheng 已提交
552 553 554 555 556 557 558
          goto _write_del;
        }
      }
    }

  _new_del:
    toBreak = 1;
H
Hongze Cheng 已提交
559
    delIdx = (SDelIdx){.suid = id.suid, .uid = id.uid};
H
Hongze Cheng 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
    taosArrayClear(pWriter->aDelData);

  _merge_del:
    while (n < nData) {
      n += tGetDelData(pData + n, &delData);
      if (taosArrayPush(pWriter->aDelData, &delData) == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        goto _err;
      }
    }

  _write_del:
    code = tsdbWriteDelData(pWriter->pDelFWriter, pWriter->aDelData, NULL, &delIdx);
    if (code) goto _err;

    if (taosArrayPush(pWriter->aDelIdxN, &delIdx) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }

    if (toBreak) break;
  }

_exit:
  return code;

_err:
  tsdbError("vgId:%d tsdb snapshot write del failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
588 589 590
  return code;
}

H
Hongze Cheng 已提交
591 592
static int32_t tsdbSnapWriteDelEnd(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
  STsdb*  pTsdb = pWriter->pTsdb;

  if (pWriter->pDelFWriter == NULL) goto _exit;
  for (; pWriter->iDelIdx < taosArrayGetSize(pWriter->aDelIdx); pWriter->iDelIdx++) {
    SDelIdx* pDelIdx = (SDelIdx*)taosArrayGet(pWriter->aDelIdx, pWriter->iDelIdx);

    code = tsdbReadDelData(pWriter->pDelFReader, pDelIdx, pWriter->aDelData, NULL);
    if (code) goto _err;

    SDelIdx delIdx = (SDelIdx){.suid = pDelIdx->suid, .uid = pDelIdx->uid};
    code = tsdbWriteDelData(pWriter->pDelFWriter, pWriter->aDelData, NULL, &delIdx);
    if (code) goto _err;

    if (taosArrayPush(pWriter->aDelIdx, &delIdx) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
  }

  code = tsdbUpdateDelFileHdr(pWriter->pDelFWriter);
  if (code) goto _err;

  code = tsdbFSStateUpsertDelFile(pTsdb->fs->nState, &pWriter->pDelFWriter->fDel);
  if (code) goto _err;

  code = tsdbDelFWriterClose(&pWriter->pDelFWriter, 1);
  if (code) goto _err;

  if (pWriter->pDelFReader) {
    code = tsdbDelFReaderClose(&pWriter->pDelFReader);
    if (code) goto _err;
  }

_exit:
  return code;

_err:
  tsdbError("vgId:%d tsdb snapshow write del end failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
631 632 633
  return code;
}

H
Hongze Cheng 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
int32_t tsdbSnapWriterOpen(STsdb* pTsdb, int64_t sver, int64_t ever, STsdbSnapWriter** ppWriter) {
  int32_t          code = 0;
  STsdbSnapWriter* pWriter = NULL;

  // alloc
  pWriter = (STsdbSnapWriter*)taosMemoryCalloc(1, sizeof(*pWriter));
  if (pWriter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pWriter->pTsdb = pTsdb;
  pWriter->sver = sver;
  pWriter->ever = ever;

  *ppWriter = pWriter;
  return code;

_err:
  tsdbError("vgId:%d tsdb snapshot writer open failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  *ppWriter = NULL;
  return code;
}

H
Hongze Cheng 已提交
657
int32_t tsdbSnapWriterClose(STsdbSnapWriter** ppWriter, int8_t rollback) {
H
Hongze Cheng 已提交
658 659 660 661 662 663 664
  int32_t          code = 0;
  STsdbSnapWriter* pWriter = *ppWriter;

  if (rollback) {
    code = tsdbSnapRollback(pWriter);
    if (code) goto _err;
  } else {
H
Hongze Cheng 已提交
665 666 667 668 669 670
    code = tsdbSnapWriteDataEnd(pWriter);
    if (code) goto _err;

    code = tsdbSnapWriteDelEnd(pWriter);
    if (code) goto _err;

H
Hongze Cheng 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
    code = tsdbSnapCommit(pWriter);
    if (code) goto _err;
  }

  taosMemoryFree(pWriter);
  *ppWriter = NULL;

  return code;

_err:
  tsdbError("vgId:%d tsdb snapshot writer close failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
  return code;
}

int32_t tsdbSnapWrite(STsdbSnapWriter* pWriter, uint8_t* pData, uint32_t nData) {
H
Hongze Cheng 已提交
686
  int32_t code = 0;
H
Hongze Cheng 已提交
687 688 689 690 691 692 693
  int8_t  type = pData[0];

  // ts data
  if (type == 0) {
    code = tsdbSnapWriteData(pWriter, pData + 1, nData - 1);
    if (code) goto _err;
  } else {
H
Hongze Cheng 已提交
694 695
    code = tsdbSnapWriteDataEnd(pWriter);
    if (code) goto _err;
H
Hongze Cheng 已提交
696 697 698 699 700 701 702 703 704 705 706 707
  }

  // del data
  if (type == 1) {
    code = tsdbSnapWriteDel(pWriter, pData + 1, nData - 1);
    if (code) goto _err;
  }

  return code;

_err:
  tsdbError("vgId:%d tsdb snapshow write failed since %s", TD_VID(pWriter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
708 709
  return code;
}