You need to sign in or sign up before continuing.
tsdbSnapshot.c 28.7 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 33
  SBlockData    oBlockData;
  SBlockData    nBlockData;
H
more  
Hongze Cheng 已提交
34
  // for del file
H
Hongze Cheng 已提交
35
  int8_t       delDone;
H
more  
Hongze Cheng 已提交
36
  SDelFReader* pDelFReader;
H
Hongze Cheng 已提交
37
  SArray*      aDelIdx;  // SArray<SDelIdx>
H
Hongze Cheng 已提交
38 39
  int32_t      iDelIdx;
  SArray*      aDelData;  // SArray<SDelData>
H
Hongze Cheng 已提交
40 41
};

H
Hongze Cheng 已提交
42 43
static int32_t tsdbSnapReadData(STsdbSnapReader* pReader, uint8_t** ppData) {
  int32_t code = 0;
H
Hongze Cheng 已提交
44
  STsdb*  pTsdb = pReader->pTsdb;
H
Hongze Cheng 已提交
45

H
Hongze Cheng 已提交
46 47
  while (true) {
    if (pReader->pDataFReader == NULL) {
H
Hongze Cheng 已提交
48
      SDFileSet* pSet = tsdbFSStateGetDFileSet(pTsdb->fs->cState, pReader->fid, TD_GT);
H
Hongze Cheng 已提交
49

H
Hongze Cheng 已提交
50
      if (pSet == NULL) goto _exit;
H
Hongze Cheng 已提交
51

H
Hongze Cheng 已提交
52
      pReader->fid = pSet->fid;
H
Hongze Cheng 已提交
53 54 55 56 57 58
      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 已提交
59

H
Hongze Cheng 已提交
60 61
      pReader->iBlockIdx = 0;
      pReader->pBlockIdx = NULL;
H
Hongze Cheng 已提交
62 63

      tsdbInfo("vgId:%d vnode snapshot tsdb open data file to read, fid:%d", TD_VID(pTsdb->pVnode), pReader->fid);
H
Hongze Cheng 已提交
64
    }
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
        code = tsdbReadBlock(pReader->pDataFReader, pReader->pBlockIdx, &pReader->mBlock, NULL);
        if (code) goto _err;

        pReader->iBlock = 0;
      }

H
Hongze Cheng 已提交
82 83
      SBlock  block;
      SBlock* pBlock = &block;
H
Hongze Cheng 已提交
84 85 86 87 88 89 90 91
      while (true) {
        if (pReader->iBlock >= pReader->mBlock.nItem) {
          pReader->pBlockIdx = NULL;
          break;
        }

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

H
Hongze Cheng 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
        if (pBlock->minVersion > pReader->ever || pBlock->maxVersion < pReader->sver) continue;

        code = tsdbReadBlockData(pReader->pDataFReader, pReader->pBlockIdx, pBlock, &pReader->oBlockData, NULL, NULL);
        if (code) goto _err;

        // filter
        tBlockDataReset(&pReader->nBlockData);
        for (int32_t iColData = 0; iColData < taosArrayGetSize(pReader->oBlockData.aIdx); iColData++) {
          SColData* pColDataO = tBlockDataGetColDataByIdx(&pReader->oBlockData, iColData);
          SColData* pColDataN = NULL;

          code = tBlockDataAddColData(&pReader->nBlockData, taosArrayGetSize(pReader->nBlockData.aIdx), &pColDataN);
          if (code) goto _err;

          tColDataInit(pColDataN, pColDataO->cid, pColDataO->type, pColDataO->smaOn);
        }

        for (int32_t iRow = 0; iRow < pReader->oBlockData.nRow; iRow++) {
          TSDBROW row = tsdbRowFromBlockData(&pReader->oBlockData, iRow);
          int64_t version = TSDBROW_VERSION(&row);

          if (version < pReader->sver || version > pReader->ever) continue;
H
Hongze Cheng 已提交
115

H
Hongze Cheng 已提交
116
          code = tBlockDataAppendRow(&pReader->nBlockData, &row, NULL);
H
Hongze Cheng 已提交
117
          if (code) goto _err;
H
Hongze Cheng 已提交
118
        }
H
Hongze Cheng 已提交
119

H
Hongze Cheng 已提交
120 121
        // org data
        // compress data (todo)
H
Hongze Cheng 已提交
122 123 124 125 126 127
        int32_t size = sizeof(TABLEID) + tPutBlockData(NULL, &pReader->nBlockData);

        *ppData = taosMemoryMalloc(sizeof(SSnapDataHdr) + size);
        if (*ppData == NULL) {
          code = TSDB_CODE_OUT_OF_MEMORY;
          goto _err;
H
Hongze Cheng 已提交
128
        }
H
Hongze Cheng 已提交
129 130 131 132 133 134 135 136 137 138 139 140

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

        TABLEID* pId = (TABLEID*)(&pHdr[1]);
        pId->suid = pReader->pBlockIdx->suid;
        pId->uid = pReader->pBlockIdx->uid;

        tPutBlockData((uint8_t*)(&pId[1]), &pReader->nBlockData);

        tsdbInfo("vgId:%d vnode snapshot read data, fid:%d suid:%" PRId64 " uid:%" PRId64
H
Hongze Cheng 已提交
141
                 " iBlock:%d minVersion:%d maxVersion:%d nRow:%d out of %d size:%d",
H
Hongze Cheng 已提交
142
                 TD_VID(pTsdb->pVnode), pReader->fid, pReader->pBlockIdx->suid, pReader->pBlockIdx->uid,
H
Hongze Cheng 已提交
143 144
                 pReader->iBlock - 1, pBlock->minVersion, pBlock->maxVersion, pReader->nBlockData.nRow, pBlock->nRow,
                 size);
H
Hongze Cheng 已提交
145 146

        goto _exit;
H
Hongze Cheng 已提交
147
      }
H
Hongze Cheng 已提交
148 149 150 151
    }
  }

_exit:
H
Hongze Cheng 已提交
152 153 154
  return code;

_err:
H
Hongze Cheng 已提交
155
  tsdbError("vgId:%d vnode snapshot tsdb read data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
156 157 158 159 160 161
  return code;
}

static int32_t tsdbSnapReadDel(STsdbSnapReader* pReader, uint8_t** ppData) {
  int32_t   code = 0;
  STsdb*    pTsdb = pReader->pTsdb;
H
Hongze Cheng 已提交
162
  SDelFile* pDelFile = pTsdb->fs->cState->pDelFile;
H
Hongze Cheng 已提交
163 164 165 166 167 168 169 170 171 172

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

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

H
Hongze Cheng 已提交
173
    // read index
H
Hongze Cheng 已提交
174 175 176 177 178 179
    code = tsdbReadDelIdx(pReader->pDelFReader, pReader->aDelIdx, NULL);
    if (code) goto _err;

    pReader->iDelIdx = 0;
  }

H
Hongze Cheng 已提交
180 181 182 183 184 185
  while (true) {
    if (pReader->iDelIdx >= taosArrayGetSize(pReader->aDelIdx)) {
      tsdbDelFReaderClose(&pReader->pDelFReader);
      break;
    }

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

H
Hongze Cheng 已提交
188 189
    pReader->iDelIdx++;

H
Hongze Cheng 已提交
190 191 192
    code = tsdbReadDelData(pReader->pDelFReader, pDelIdx, pReader->aDelData, NULL);
    if (code) goto _err;

H
Hongze Cheng 已提交
193
    int32_t size = 0;
H
Hongze Cheng 已提交
194 195 196 197
    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 已提交
198
        size += tPutDelData(NULL, pDelData);
H
Hongze Cheng 已提交
199 200 201
      }
    }

H
Hongze Cheng 已提交
202
    if (size == 0) continue;
H
Hongze Cheng 已提交
203

H
Hongze Cheng 已提交
204 205 206 207 208 209 210
    // org data
    size = sizeof(TABLEID) + size;
    *ppData = taosMemoryMalloc(sizeof(SSnapDataHdr) + size);
    if (*ppData == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
211

H
Hongze Cheng 已提交
212 213 214
    SSnapDataHdr* pHdr = (SSnapDataHdr*)(*ppData);
    pHdr->type = 2;
    pHdr->size = size;
H
Hongze Cheng 已提交
215

H
Hongze Cheng 已提交
216 217 218 219 220 221
    TABLEID* pId = (TABLEID*)(&pHdr[1]);
    pId->suid = pDelIdx->suid;
    pId->uid = pDelIdx->uid;
    int32_t n = sizeof(SSnapDataHdr) + sizeof(TABLEID);
    for (int32_t iDelData = 0; iDelData < taosArrayGetSize(pReader->aDelData); iDelData++) {
      SDelData* pDelData = (SDelData*)taosArrayGet(pReader->aDelData, iDelData);
H
Hongze Cheng 已提交
222

H
Hongze Cheng 已提交
223 224
      if (pDelData->version < pReader->sver) continue;
      if (pDelData->version > pReader->ever) continue;
H
Hongze Cheng 已提交
225

H
Hongze Cheng 已提交
226
      n += tPutDelData((*ppData) + n, pDelData);
H
Hongze Cheng 已提交
227 228
    }

H
Hongze Cheng 已提交
229 230 231 232 233
    tsdbInfo("vgId:%d vnode snapshot tsdb read del data, suid:%" PRId64 " uid:%d" PRId64 " size:%d",
             TD_VID(pTsdb->pVnode), pDelIdx->suid, pDelIdx->uid, size);

    break;
  }
H
Hongze Cheng 已提交
234 235 236 237 238

_exit:
  return code;

_err:
H
Hongze Cheng 已提交
239
  tsdbError("vgId:%d vnode snapshot tsdb read del failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
240 241
  return code;
}
H
more  
Hongze Cheng 已提交
242

H
Hongze Cheng 已提交
243
int32_t tsdbSnapReaderOpen(STsdb* pTsdb, int64_t sver, int64_t ever, STsdbSnapReader** ppReader) {
H
Hongze Cheng 已提交
244 245
  int32_t          code = 0;
  STsdbSnapReader* pReader = NULL;
H
Hongze Cheng 已提交
246

H
more  
Hongze Cheng 已提交
247
  // alloc
H
Hongze Cheng 已提交
248
  pReader = (STsdbSnapReader*)taosMemoryCalloc(1, sizeof(*pReader));
H
more  
Hongze Cheng 已提交
249 250 251 252 253 254 255 256
  if (pReader == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pReader->pTsdb = pTsdb;
  pReader->sver = sver;
  pReader->ever = ever;

H
Hongze Cheng 已提交
257
  pReader->fid = INT32_MIN;
H
Hongze Cheng 已提交
258 259 260 261 262 263
  pReader->aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pReader->aBlockIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pReader->mBlock = tMapDataInit();
H
Hongze Cheng 已提交
264 265 266
  code = tBlockDataInit(&pReader->oBlockData);
  if (code) goto _err;
  code = tBlockDataInit(&pReader->nBlockData);
H
Hongze Cheng 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279
  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
Hongze Cheng 已提交
280
  tsdbInfo("vgId:%d vnode snapshot tsdb reader opened", TD_VID(pTsdb->pVnode));
H
more  
Hongze Cheng 已提交
281 282 283 284
  *ppReader = pReader;
  return code;

_err:
H
Hongze Cheng 已提交
285
  tsdbError("vgId:%d vnode snapshot tsdb reader open failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
more  
Hongze Cheng 已提交
286 287
  *ppReader = NULL;
  return code;
H
Hongze Cheng 已提交
288 289
}

H
Hongze Cheng 已提交
290
int32_t tsdbSnapReaderClose(STsdbSnapReader** ppReader) {
H
Hongze Cheng 已提交
291 292 293 294 295 296
  int32_t          code = 0;
  STsdbSnapReader* pReader = *ppReader;

  if (pReader->pDataFReader) {
    tsdbDataFReaderClose(&pReader->pDataFReader);
  }
H
Hongze Cheng 已提交
297 298
  taosArrayDestroy(pReader->aBlockIdx);
  tMapDataClear(&pReader->mBlock);
H
Hongze Cheng 已提交
299 300
  tBlockDataClear(&pReader->oBlockData, 1);
  tBlockDataClear(&pReader->nBlockData, 1);
H
Hongze Cheng 已提交
301 302 303 304 305 306 307 308 309

  if (pReader->pDelFReader) {
    tsdbDelFReaderClose(&pReader->pDelFReader);
  }
  taosArrayDestroy(pReader->aDelIdx);
  taosArrayDestroy(pReader->aDelData);

  tsdbInfo("vgId:%d vnode snapshot tsdb reader closed", TD_VID(pReader->pTsdb->pVnode));

H
Hongze Cheng 已提交
310 311
  taosMemoryFree(pReader);
  *ppReader = NULL;
H
Hongze Cheng 已提交
312 313 314 315
  return code;
}

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

H
Hongze Cheng 已提交
318 319
  *ppData = NULL;

H
Hongze Cheng 已提交
320
  // read data file
H
Hongze Cheng 已提交
321 322 323
  if (!pReader->dataDone) {
    code = tsdbSnapReadData(pReader, ppData);
    if (code) {
H
Hongze Cheng 已提交
324 325 326 327
      goto _err;
    } else {
      if (*ppData) {
        goto _exit;
H
Hongze Cheng 已提交
328
      } else {
H
Hongze Cheng 已提交
329
        pReader->dataDone = 1;
H
Hongze Cheng 已提交
330 331 332
      }
    }
  }
H
Hongze Cheng 已提交
333 334

  // read del file
H
Hongze Cheng 已提交
335 336 337
  if (!pReader->delDone) {
    code = tsdbSnapReadDel(pReader, ppData);
    if (code) {
H
Hongze Cheng 已提交
338 339 340 341
      goto _err;
    } else {
      if (*ppData) {
        goto _exit;
H
Hongze Cheng 已提交
342
      } else {
H
Hongze Cheng 已提交
343
        pReader->delDone = 1;
H
Hongze Cheng 已提交
344 345 346
      }
    }
  }
H
Hongze Cheng 已提交
347

H
Hongze Cheng 已提交
348
_exit:
H
more  
Hongze Cheng 已提交
349 350 351
  return code;

_err:
H
Hongze Cheng 已提交
352
  tsdbError("vgId:%d vnode snapshot tsdb read failed since %s", TD_VID(pReader->pTsdb->pVnode), tstrerror(code));
H
more  
Hongze Cheng 已提交
353 354 355
  return code;
}

H
Hongze Cheng 已提交
356
// STsdbSnapWriter ========================================
H
Hongze Cheng 已提交
357 358 359 360
struct STsdbSnapWriter {
  STsdb*  pTsdb;
  int64_t sver;
  int64_t ever;
H
Hongze Cheng 已提交
361

H
Hongze Cheng 已提交
362 363 364
  // config
  int32_t minutes;
  int8_t  precision;
H
Hongze Cheng 已提交
365 366 367
  int32_t minRow;
  int32_t maxRow;
  int8_t  cmprAlg;
H
Hongze Cheng 已提交
368
  int64_t commitID;
H
Hongze Cheng 已提交
369

H
Hongze Cheng 已提交
370
  // for data file
H
Hongze Cheng 已提交
371 372
  SBlockData bData;

H
Hongze Cheng 已提交
373
  int32_t       fid;
H
Hongze Cheng 已提交
374
  SDataFReader* pDataFReader;
H
Hongze Cheng 已提交
375
  SArray*       aBlockIdx;  // SArray<SBlockIdx>
H
Hongze Cheng 已提交
376
  int32_t       iBlockIdx;
H
Hongze Cheng 已提交
377
  SBlockIdx*    pBlockIdx;
H
Hongze Cheng 已提交
378
  SMapData      mBlock;  // SMapData<SBlock>
H
Hongze Cheng 已提交
379
  int32_t       iBlock;
H
Hongze Cheng 已提交
380 381
  SBlock*       pBlock;
  SBlock        block;
H
Hongze Cheng 已提交
382
  SBlockData    bDataR;
H
Hongze Cheng 已提交
383
  int32_t       iRow;
H
Hongze Cheng 已提交
384

H
Hongze Cheng 已提交
385
  SDataFWriter* pDataFWriter;
H
Hongze Cheng 已提交
386
  SBlockIdx*    pBlockIdxW;  // NULL when no committing table
H
Hongze Cheng 已提交
387 388
  SBlock        blockW;
  SBlockData    bDataW;
H
Hongze Cheng 已提交
389
  SBlockIdx     blockIdxW;
H
Hongze Cheng 已提交
390

H
Hongze Cheng 已提交
391 392 393
  SMapData mBlockW;     // SMapData<SBlock>
  SArray*  aBlockIdxW;  // SArray<SBlockIdx>

H
Hongze Cheng 已提交
394
  // for del file
H
Hongze Cheng 已提交
395
  SDelFReader* pDelFReader;
H
Hongze Cheng 已提交
396
  SDelFWriter* pDelFWriter;
H
Hongze Cheng 已提交
397
  int32_t      iDelIdx;
H
Hongze Cheng 已提交
398
  SArray*      aDelIdxR;
H
Hongze Cheng 已提交
399
  SArray*      aDelData;
H
Hongze Cheng 已提交
400
  SArray*      aDelIdxW;
H
Hongze Cheng 已提交
401 402
};

H
Hongze Cheng 已提交
403
static int32_t tsdbSnapRollback(STsdbSnapWriter* pWriter) {
H
Hongze Cheng 已提交
404 405 406 407 408
  int32_t code = 0;
  // TODO
  return code;
}

H
Hongze Cheng 已提交
409
static int32_t tsdbSnapCommit(STsdbSnapWriter* pWriter) {
H
Hongze Cheng 已提交
410 411 412 413 414
  int32_t code = 0;
  // TODO
  return code;
}

H
Hongze Cheng 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
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 已提交
439 440 441 442 443 444 445
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) {
H
Hongze Cheng 已提交
446
    code = tBlockDataAppendRow(&pWriter->bDataW, &tsdbRowFromBlockData(pBlockData, iRow), NULL);
H
Hongze Cheng 已提交
447 448 449 450 451 452 453 454 455 456
    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
Hongze Cheng 已提交
457
static int32_t tsdbSnapWriteTableDataEnd(STsdbSnapWriter* pWriter) {
H
more  
Hongze Cheng 已提交
458 459
  int32_t code = 0;

H
Hongze Cheng 已提交
460
  ASSERT(pWriter->pBlockIdxW != NULL);
H
Hongze Cheng 已提交
461

H
Hongze Cheng 已提交
462 463 464 465
  // write remain data if has
  if (pWriter->bDataW.nRow > 0) {
    if (pWriter->bDataW.nRow >= pWriter->minRow) {
      pWriter->blockW.last = 0;
H
Hongze Cheng 已提交
466
    } else {
H
Hongze Cheng 已提交
467
      pWriter->blockW.last = 1;
H
Hongze Cheng 已提交
468 469
    }

H
Hongze Cheng 已提交
470 471 472
    code = tsdbWriteBlockData(pWriter->pDataFWriter, &pWriter->bDataW, NULL, NULL, pWriter->pBlockIdxW,
                              &pWriter->blockW, pWriter->cmprAlg);
    if (code) goto _err;
H
Hongze Cheng 已提交
473

H
Hongze Cheng 已提交
474 475 476
    code = tMapDataPutItem(&pWriter->mBlockW, &pWriter->blockW, tPutBlock);
    if (code) goto _err;
  }
H
Hongze Cheng 已提交
477

H
Hongze Cheng 已提交
478 479
  code = tsdbWriteBlock(pWriter->pDataFWriter, &pWriter->mBlockW, NULL, pWriter->pBlockIdxW);
  if (code) goto _err;
H
Hongze Cheng 已提交
480

H
Hongze Cheng 已提交
481 482 483
  if (taosArrayPush(pWriter->aBlockIdxW, pWriter->pBlockIdxW) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
H
Hongze Cheng 已提交
484 485 486 487 488 489 490
  }

  return code;

_err:
  return code;
}
H
Hongze Cheng 已提交
491

H
Hongze Cheng 已提交
492 493 494 495 496 497 498 499 500 501 502 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
static int32_t tsdbSnapWriteTableDataAhead(STsdbSnapWriter* pWriter, TABLEID id) {
  int32_t code = 0;

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

  while (true) {
    if (pWriter->iBlockIdx >= taosArrayGetSize(pWriter->aBlockIdx)) break;

    SBlockIdx* pBlockIdx = (SBlockIdx*)taosArrayGet(pWriter->aBlockIdx, pWriter->iBlockIdx);
    int32_t    c = tTABLEIDCmprFn(pBlockIdx, &id);

    if (c >= 0) break;

    pWriter->iBlockIdx++;

    code = tsdbReadBlock(pWriter->pDataFReader, pBlockIdx, &pWriter->mBlock, NULL);
    if (code) goto _err;

    SBlock block;
    tMapDataReset(&pWriter->mBlockW);
    for (int32_t iBlock = 0; iBlock < pWriter->mBlock.nItem; iBlock++) {
      tMapDataGetItemByIdx(&pWriter->mBlock, iBlock, &block, tGetBlock);

      if (block.last) {
        code = tsdbReadBlockData(pWriter->pDataFReader, pBlockIdx, &block, &pWriter->bDataR, NULL, NULL);
        if (code) goto _err;

        tBlockReset(&block);
        block.last = 1;
        code = tsdbWriteBlockData(pWriter->pDataFWriter, &pWriter->bDataR, NULL, NULL, pBlockIdx, &block,
                                  pWriter->cmprAlg);
        if (code) goto _err;
      }

      code = tMapDataPutItem(&pWriter->mBlockW, &block, tPutBlock);
      if (code) goto _err;
    }

    SBlockIdx blockIdx = {.suid = pBlockIdx->suid, .uid = pBlockIdx->uid};
    code = tsdbWriteBlock(pWriter->pDataFWriter, &pWriter->mBlockW, NULL, &blockIdx);
    if (code) goto _err;

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

_exit:
  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
547 548 549
static int32_t tsdbSnapWriteDataImpl(STsdbSnapWriter* pWriter, TABLEID id) {
  int32_t     code = 0;
  SBlockData* pBlockData = &pWriter->bData;
H
Hongze Cheng 已提交
550 551
  TSDBKEY     keyFirst = tBlockDataFirstKey(pBlockData);
  TSDBKEY     keyLast = tBlockDataLastKey(pBlockData);
H
Hongze Cheng 已提交
552 553 554 555

  if (pWriter->pDataFReader == NULL) {
    // no old data

H
Hongze Cheng 已提交
556
    // end last table write if need
H
Hongze Cheng 已提交
557
    if (pWriter->pBlockIdxW) {
H
Hongze Cheng 已提交
558 559
      int32_t c = tTABLEIDCmprFn(pWriter->pBlockIdxW, &id);

H
Hongze Cheng 已提交
560
      if (c < 0) {
H
Hongze Cheng 已提交
561 562 563 564 565
        // end last table data write
        code = tsdbSnapWriteTableDataEnd(pWriter);
        if (code) goto _err;

        // reset
H
Hongze Cheng 已提交
566 567 568 569 570 571 572 573
        pWriter->pBlockIdxW = NULL;
      } else if (c > 0) {
        ASSERT(0);
      }
    }

    // start a new table data if need
    if (pWriter->pBlockIdxW == NULL) {
H
Hongze Cheng 已提交
574
      pWriter->pBlockIdxW = &pWriter->blockIdxW;
H
Hongze Cheng 已提交
575 576
      pWriter->pBlockIdxW->suid = id.suid;
      pWriter->pBlockIdxW->uid = id.uid;
H
Hongze Cheng 已提交
577

H
Hongze Cheng 已提交
578
      tBlockReset(&pWriter->blockW);
H
Hongze Cheng 已提交
579 580 581 582

      tBlockDataReset(&pWriter->bDataW);

      tMapDataReset(&pWriter->mBlockW);
H
Hongze Cheng 已提交
583 584
    }

H
Hongze Cheng 已提交
585 586 587
    // set block schema
    code = tBlockDataCorrectSchema(&pWriter->bDataW, pBlockData);
    if (code) goto _err;
H
Hongze Cheng 已提交
588 589 590

    // add rows
    for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
H
Hongze Cheng 已提交
591
      TSDBROW row = tsdbRowFromBlockData(pBlockData, iRow);
H
Hongze Cheng 已提交
592

H
Hongze Cheng 已提交
593
      code = tBlockDataAppendRow(&pWriter->bDataW, &row, NULL);
H
Hongze Cheng 已提交
594 595 596
      if (code) goto _err;

      if (pWriter->bDataW.nRow >= pWriter->maxRow * 4 / 5) {
H
Hongze Cheng 已提交
597
        pWriter->blockW.last = 0;
H
Hongze Cheng 已提交
598
        code = tsdbWriteBlockData(pWriter->pDataFWriter, &pWriter->bDataW, NULL, NULL, pWriter->pBlockIdxW,
H
Hongze Cheng 已提交
599
                                  &pWriter->blockW, pWriter->cmprAlg);
H
Hongze Cheng 已提交
600 601
        if (code) goto _err;

H
Hongze Cheng 已提交
602
        code = tMapDataPutItem(&pWriter->mBlockW, &pWriter->blockW, tPutBlock);
H
Hongze Cheng 已提交
603 604 605
        if (code) goto _err;

        // reset
H
Hongze Cheng 已提交
606 607
        tBlockReset(&pWriter->blockW);
        tBlockDataClearData(&pWriter->bDataW);
H
Hongze Cheng 已提交
608 609
      }
    }
H
Hongze Cheng 已提交
610
  } else {
H
Hongze Cheng 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 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
    // has old data

    // TABLE ==================================================
    // end last table data if id not same (todo)
    if (pWriter->pBlockIdxW) {
      int32_t c = tTABLEIDCmprFn(pWriter->pBlockIdxW, &id);
      if (c < 0) {
      } else if (c > 0) {
        ASSERT(0);
      }
    }

    // start new table data if need (todo)
    if (pWriter->pBlockIdxW == NULL) {
      // commit table data ahead
      code = tsdbSnapWriteTableDataAhead(pWriter, id);
      if (code) goto _err;

      // reader
      if (pWriter->iBlockIdx < taosArrayGetSize(pWriter->aBlockIdx)) {
        pWriter->pBlockIdx = (SBlockIdx*)taosArrayGet(pWriter->aBlockIdx, pWriter->iBlockIdx);
        int32_t c = tTABLEIDCmprFn(pWriter->pBlockIdx, &id);
        if (c) {
          pWriter->pBlockIdx = NULL;
        }
      } else {
        pWriter->pBlockIdx = NULL;
      }

      if (pWriter->pBlockIdx) {
        code = tsdbReadBlock(pWriter->pDataFReader, pWriter->pBlockIdx, &pWriter->mBlockW, NULL);
        if (code) goto _err;

        pWriter->iBlock = 0;
      }

      // writer
      pWriter->pBlockIdxW = &pWriter->blockIdxW;
      pWriter->pBlockIdxW->suid = id.suid;
      pWriter->pBlockIdxW->uid = id.uid;

      tBlockReset(&pWriter->blockW);
      tBlockDataReset(&pWriter->bDataW);
      tMapDataReset(&pWriter->mBlockW);
    }

    // BLOCK ==================================================
    // write block ahead
    while (true) {
      if (pWriter->iBlock >= pWriter->mBlock.nItem) break;

      SBlock block;
      tMapDataGetItemByIdx(&pWriter->mBlock, pWriter->iBlock, &block, tGetBlock);

      if (tsdbKeyCmprFn(&block.maxKey, &keyFirst) >= 0) break;

      pWriter->iBlock++;

      code = tMapDataPutItem(&pWriter->mBlockW, &block, tPutBlock);
      if (code) goto _err;
    }
H
Hongze Cheng 已提交
672 673 674
  }

  return code;
H
Hongze Cheng 已提交
675 676 677 678 679

_err:
  tsdbError("vgId:%d vnode snapshot tsdb write data impl failed since %s", TD_VID(pWriter->pTsdb->pVnode),
            tstrerror(code));
  return code;
H
Hongze Cheng 已提交
680
}
H
Hongze Cheng 已提交
681

H
Hongze Cheng 已提交
682
static int32_t tsdbSnapWriteData(STsdbSnapWriter* pWriter, uint8_t* pData, uint32_t nData) {
H
Hongze Cheng 已提交
683 684 685 686 687 688 689
  int32_t       code = 0;
  STsdb*        pTsdb = pWriter->pTsdb;
  SSnapDataHdr* pHdr = (SSnapDataHdr*)pData;
  TABLEID       id = *(TABLEID*)(&pHdr[1]);
  int64_t       n;

  // decode
H
Hongze Cheng 已提交
690
  SBlockData* pBlockData = &pWriter->bData;
H
Hongze Cheng 已提交
691 692
  n = tGetBlockData(pData + sizeof(SSnapDataHdr) + sizeof(TABLEID), pBlockData);
  ASSERT(n + sizeof(SSnapDataHdr) + sizeof(TABLEID) == nData);
H
Hongze Cheng 已提交
693

H
Hongze Cheng 已提交
694 695 696
  // open file
  TSDBKEY keyFirst = tBlockDataFirstKey(pBlockData);
  TSDBKEY keyLast = tBlockDataLastKey(pBlockData);
H
Hongze Cheng 已提交
697

H
Hongze Cheng 已提交
698 699
  int32_t fid = tsdbKeyFid(keyFirst.ts, pWriter->minutes, pWriter->precision);
  ASSERT(fid == tsdbKeyFid(keyLast.ts, pWriter->minutes, pWriter->precision));
H
Hongze Cheng 已提交
700
  if (pWriter->pDataFWriter == NULL || pWriter->fid != fid) {
H
Hongze Cheng 已提交
701
    // end last file data write if need
H
Hongze Cheng 已提交
702
    code = tsdbSnapWriteDataEnd(pWriter);  // todo
H
Hongze Cheng 已提交
703
    if (code) goto _err;
H
Hongze Cheng 已提交
704 705

    pWriter->fid = fid;
H
Hongze Cheng 已提交
706 707 708

    // read
    SDFileSet* pSet = tsdbFSStateGetDFileSet(pTsdb->fs->nState, fid, TD_EQ);  // todo: check nState is valid
H
Hongze Cheng 已提交
709 710 711 712 713 714 715
    if (pSet) {
      code = tsdbDataFReaderOpen(&pWriter->pDataFReader, pTsdb, pSet);
      if (code) goto _err;

      code = tsdbReadBlockIdx(pWriter->pDataFReader, pWriter->aBlockIdx, NULL);
      if (code) goto _err;
    } else {
H
Hongze Cheng 已提交
716
      ASSERT(pWriter->pDataFReader == NULL);
H
Hongze Cheng 已提交
717 718 719
      taosArrayClear(pWriter->aBlockIdx);
    }
    pWriter->iBlockIdx = 0;
H
Hongze Cheng 已提交
720 721 722 723 724 725
    pWriter->pBlockIdx = NULL;
    tMapDataReset(&pWriter->mBlock);
    pWriter->iBlock = 0;
    pWriter->pBlock = NULL;
    tBlockDataReset(&pWriter->bDataR);
    pWriter->iRow = 0;
H
Hongze Cheng 已提交
726

H
Hongze Cheng 已提交
727 728 729 730 731 732 733 734 735 736
    // write
    SDFileSet wSet;

    if (pSet) {
      wSet = (SDFileSet){.diskId = pSet->diskId,
                         .fid = fid,
                         .fHead = {.commitID = pWriter->commitID, .offset = 0, .size = 0},
                         .fData = pSet->fData,
                         .fLast = {.commitID = pWriter->commitID, .size = 0},
                         .fSma = pSet->fSma};
H
Hongze Cheng 已提交
737
    } else {
H
Hongze Cheng 已提交
738 739 740 741 742 743
      wSet = (SDFileSet){.diskId = (SDiskID){.level = 0, .id = 0},
                         .fid = fid,
                         .fHead = {.commitID = pWriter->commitID, .offset = 0, .size = 0},
                         .fData = {.commitID = pWriter->commitID, .size = 0},
                         .fLast = {.commitID = pWriter->commitID, .size = 0},
                         .fSma = {.commitID = pWriter->commitID, .size = 0}};
H
Hongze Cheng 已提交
744 745 746 747
    }

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

H
Hongze Cheng 已提交
749 750 751 752
    taosArrayClear(pWriter->aBlockIdxW);
    pWriter->pBlockIdxW = NULL;
    tMapDataReset(&pWriter->mBlockW);
    tBlockDataReset(&pWriter->bDataW);
H
Hongze Cheng 已提交
753 754
  }

H
Hongze Cheng 已提交
755 756
  code = tsdbSnapWriteDataImpl(pWriter, id);
  if (code) goto _err;
H
Hongze Cheng 已提交
757

H
Hongze Cheng 已提交
758 759
  tsdbInfo("vgId:%d vnode snapshot tsdb write data, fid:%d suid:%" PRId64 " uid:%" PRId64 " nRow:%d",
           TD_VID(pTsdb->pVnode), fid, id.suid, id.suid, pBlockData->nRow);
H
Hongze Cheng 已提交
760 761 762
  return code;

_err:
H
Hongze Cheng 已提交
763
  tsdbError("vgId:%d vnode snapshot tsdb write data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
764 765 766 767 768
  return code;
}

static int32_t tsdbSnapWriteDel(STsdbSnapWriter* pWriter, uint8_t* pData, uint32_t nData) {
  int32_t code = 0;
H
Hongze Cheng 已提交
769 770 771 772 773 774 775 776 777 778
  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;

H
Hongze Cheng 已提交
779
      code = tsdbReadDelIdx(pWriter->pDelFReader, pWriter->aDelIdxR, NULL);
H
Hongze Cheng 已提交
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
      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;

H
Hongze Cheng 已提交
799 800
    if (pWriter->iDelIdx < taosArrayGetSize(pWriter->aDelIdxR)) {
      pDelIdx = taosArrayGet(pWriter->aDelIdxR, pWriter->iDelIdx);
H
Hongze Cheng 已提交
801 802 803 804 805 806 807 808 809 810 811 812 813
    }

    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 已提交
814
          delIdx = (SDelIdx){.suid = id.suid, .uid = id.uid};
H
Hongze Cheng 已提交
815 816
          goto _merge_del;
        } else {
H
Hongze Cheng 已提交
817
          delIdx = (SDelIdx){.suid = pDelIdx->suid, .uid = pDelIdx->uid};
H
Hongze Cheng 已提交
818 819 820 821 822 823 824
          goto _write_del;
        }
      }
    }

  _new_del:
    toBreak = 1;
H
Hongze Cheng 已提交
825
    delIdx = (SDelIdx){.suid = id.suid, .uid = id.uid};
H
Hongze Cheng 已提交
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
    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;

H
Hongze Cheng 已提交
841
    if (taosArrayPush(pWriter->aDelIdxW, &delIdx) == NULL) {
H
Hongze Cheng 已提交
842 843 844 845 846 847 848 849 850 851 852 853
      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 已提交
854 855 856
  return code;
}

H
Hongze Cheng 已提交
857 858
static int32_t tsdbSnapWriteDelEnd(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
859 860 861
  STsdb*  pTsdb = pWriter->pTsdb;

  if (pWriter->pDelFWriter == NULL) goto _exit;
H
Hongze Cheng 已提交
862 863
  for (; pWriter->iDelIdx < taosArrayGetSize(pWriter->aDelIdxR); pWriter->iDelIdx++) {
    SDelIdx* pDelIdx = (SDelIdx*)taosArrayGet(pWriter->aDelIdxR, pWriter->iDelIdx);
H
Hongze Cheng 已提交
864 865 866 867 868 869 870 871

    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;

H
Hongze Cheng 已提交
872
    if (taosArrayPush(pWriter->aDelIdxR, &delIdx) == NULL) {
H
Hongze Cheng 已提交
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
      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 已提交
897 898 899
  return code;
}

H
Hongze Cheng 已提交
900 901 902 903 904 905 906 907 908 909 910 911 912 913
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;

H
Hongze Cheng 已提交
914
  // config
H
Hongze Cheng 已提交
915 916 917 918 919
  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 已提交
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
  pWriter->commitID = pTsdb->pVnode->state.commitID;

  // for data file
  code = tBlockDataInit(&pWriter->bData);

  if (code) goto _err;
  pWriter->aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pWriter->aBlockIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  code = tBlockDataInit(&pWriter->bDataR);
  if (code) goto _err;

  pWriter->aBlockIdxW = taosArrayInit(0, sizeof(SBlockIdx));
  if (pWriter->aBlockIdxW == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  code = tBlockDataInit(&pWriter->bDataW);
  if (code) goto _err;

  // for del file
  pWriter->aDelIdxR = taosArrayInit(0, sizeof(SDelIdx));
  if (pWriter->aDelIdxR == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pWriter->aDelData = taosArrayInit(0, sizeof(SDelData));
  if (pWriter->aDelData == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pWriter->aDelIdxW = taosArrayInit(0, sizeof(SDelIdx));
  if (pWriter->aDelIdxW == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
958

H
Hongze Cheng 已提交
959 960 961 962 963 964 965 966 967
  *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 已提交
968
int32_t tsdbSnapWriterClose(STsdbSnapWriter** ppWriter, int8_t rollback) {
H
Hongze Cheng 已提交
969 970 971 972 973 974 975
  int32_t          code = 0;
  STsdbSnapWriter* pWriter = *ppWriter;

  if (rollback) {
    code = tsdbSnapRollback(pWriter);
    if (code) goto _err;
  } else {
H
Hongze Cheng 已提交
976 977 978 979 980 981
    code = tsdbSnapWriteDataEnd(pWriter);
    if (code) goto _err;

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

H
Hongze Cheng 已提交
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
    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 已提交
997 998
  int32_t       code = 0;
  SSnapDataHdr* pHdr = (SSnapDataHdr*)pData;
H
Hongze Cheng 已提交
999 1000

  // ts data
H
Hongze Cheng 已提交
1001 1002
  if (pHdr->type == 1) {
    code = tsdbSnapWriteData(pWriter, pData, nData);
H
Hongze Cheng 已提交
1003
    if (code) goto _err;
H
Hongze Cheng 已提交
1004 1005

    goto _exit;
H
Hongze Cheng 已提交
1006
  } else {
H
Hongze Cheng 已提交
1007 1008 1009 1010
    if (pWriter->pDataFWriter) {
      code = tsdbSnapWriteDataEnd(pWriter);
      if (code) goto _err;
    }
H
Hongze Cheng 已提交
1011 1012 1013
  }

  // del data
H
Hongze Cheng 已提交
1014
  if (pHdr->type == 2) {
H
Hongze Cheng 已提交
1015 1016 1017 1018
    code = tsdbSnapWriteDel(pWriter, pData + 1, nData - 1);
    if (code) goto _err;
  }

H
Hongze Cheng 已提交
1019
_exit:
H
Hongze Cheng 已提交
1020 1021 1022 1023
  return code;

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