tsdbSnapshot.c 42.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
// STsdbSnapReader ========================================
H
Hongze Cheng 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
typedef enum { SNAP_DATA_FILE_ITER = 0, SNAP_STT_FILE_ITER } EFIterT;
typedef struct {
  SRBTreeNode n;
  SRowInfo    rInfo;
  EFIterT     type;
  union {
    struct {
      SArray*    aBlockIdx;
      int32_t    iBlockIdx;
      SBlockIdx* pBlockIdx;
      SMapData   mBlock;
      int32_t    iBlock;
    };  // .data file
    struct {
      int32_t iStt;
      SArray* aSttBlk;
      int32_t iSttBlk;
    };  // .stt file
  };
  SBlockData bData;
  int32_t    iRow;
} SFDataIter;

H
Hongze Cheng 已提交
42
struct STsdbSnapReader {
H
more  
Hongze Cheng 已提交
43 44 45
  STsdb*  pTsdb;
  int64_t sver;
  int64_t ever;
H
Hongze Cheng 已提交
46
  STsdbFS fs;
C
Cary Xu 已提交
47
  int8_t  type;
H
more  
Hongze Cheng 已提交
48
  // for data file
H
Hongze Cheng 已提交
49 50
  int8_t        dataDone;
  int32_t       fid;
H
more  
Hongze Cheng 已提交
51
  SDataFReader* pDataFReader;
H
Hongze Cheng 已提交
52 53
  SFDataIter*   pIter;
  SRBTree       rbt;
H
Hongze Cheng 已提交
54
  SFDataIter    aFDataIter[TSDB_MAX_STT_TRIGGER + 1];
H
Hongze Cheng 已提交
55 56
  SBlockData    bData;
  SSkmInfo      skmTable;
H
more  
Hongze Cheng 已提交
57
  // for del file
H
Hongze Cheng 已提交
58
  int8_t       delDone;
H
more  
Hongze Cheng 已提交
59
  SDelFReader* pDelFReader;
H
Hongze Cheng 已提交
60
  SArray*      aDelIdx;  // SArray<SDelIdx>
H
Hongze Cheng 已提交
61 62
  int32_t      iDelIdx;
  SArray*      aDelData;  // SArray<SDelData>
H
Hongze Cheng 已提交
63
  uint8_t*     aBuf[5];
H
Hongze Cheng 已提交
64 65
};

H
Hongze Cheng 已提交
66 67 68 69
extern int32_t tRowInfoCmprFn(const void* p1, const void* p2);
extern int32_t tsdbReadDataBlockEx(SDataFReader* pReader, SDataBlk* pDataBlk, SBlockData* pBlockData);
extern int32_t tsdbUpdateTableSchema(SMeta* pMeta, int64_t suid, int64_t uid, SSkmInfo* pSkmInfo);

H
Hongze Cheng 已提交
70 71 72 73 74 75 76
static int32_t tFDataIterCmprFn(const SRBTreeNode* pNode1, const SRBTreeNode* pNode2) {
  SFDataIter* pIter1 = (SFDataIter*)(((uint8_t*)pNode1) - offsetof(SFDataIter, n));
  SFDataIter* pIter2 = (SFDataIter*)(((uint8_t*)pNode2) - offsetof(SFDataIter, n));

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

H
Hongze Cheng 已提交
77
static int32_t tsdbSnapReadOpenFile(STsdbSnapReader* pReader) {
H
Hongze Cheng 已提交
78 79
  int32_t code = 0;

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

H
Hongze Cheng 已提交
84 85 86 87 88
  pReader->fid = pSet->fid;
  code = tsdbDataFReaderOpen(&pReader->pDataFReader, pReader->pTsdb, pSet);
  if (code) goto _err;

  pReader->pIter = NULL;
H
Hongze Cheng 已提交
89
  tRBTreeCreate(&pReader->rbt, tFDataIterCmprFn);
H
Hongze Cheng 已提交
90 91 92 93 94 95 96 97 98 99 100

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

  code = tsdbReadBlockIdx(pReader->pDataFReader, pIter->aBlockIdx);
  if (code) goto _err;

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

H
Hongze Cheng 已提交
101
    code = tsdbReadDataBlk(pReader->pDataFReader, pIter->pBlockIdx, &pIter->mBlock);
H
Hongze Cheng 已提交
102 103 104 105 106 107 108
    if (code) goto _err;

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

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

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

H
Hongze Cheng 已提交
113 114
      ASSERT(pIter->pBlockIdx->suid == pIter->bData.suid);
      ASSERT(pIter->pBlockIdx->uid == pIter->bData.uid);
H
Hongze Cheng 已提交
115

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

H
Hongze Cheng 已提交
119 120 121 122 123 124
        if (rowVer >= pReader->sver && rowVer <= pReader->ever) {
          pIter->rInfo.suid = pIter->pBlockIdx->suid;
          pIter->rInfo.uid = pIter->pBlockIdx->uid;
          pIter->rInfo.row = tsdbRowFromBlockData(&pIter->bData, pIter->iRow);
          goto _add_iter_and_break;
        }
H
Hongze Cheng 已提交
125
      }
H
Hongze Cheng 已提交
126
    }
H
Hongze Cheng 已提交
127

H
Hongze Cheng 已提交
128
    continue;
H
Hongze Cheng 已提交
129

H
Hongze Cheng 已提交
130 131 132 133
  _add_iter_and_break:
    tRBTreePut(&pReader->rbt, (SRBTreeNode*)pIter);
    break;
  }
H
Hongze Cheng 已提交
134

H
Hongze Cheng 已提交
135 136 137 138 139
  // .stt file
  pIter = &pReader->aFDataIter[1];
  for (int32_t iStt = 0; iStt < pSet->nSttF; iStt++) {
    pIter->type = SNAP_STT_FILE_ITER;
    pIter->iStt = iStt;
H
Hongze Cheng 已提交
140

H
Hongze Cheng 已提交
141 142 143 144 145 146 147 148 149
    code = tsdbReadSttBlk(pReader->pDataFReader, iStt, pIter->aSttBlk);
    if (code) goto _err;

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

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

H
Hongze Cheng 已提交
150
      code = tsdbReadSttBlockEx(pReader->pDataFReader, iStt, pSttBlk, &pIter->bData);
H
Hongze Cheng 已提交
151 152 153 154 155 156 157 158 159 160 161 162
      if (code) goto _err;

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

        if (rowVer >= pReader->sver && rowVer <= pReader->ever) {
          pIter->rInfo.suid = pIter->bData.suid;
          pIter->rInfo.uid = pIter->bData.uid;
          pIter->rInfo.row = tsdbRowFromBlockData(&pIter->bData, pIter->iRow);
          goto _add_iter;
        }
      }
H
Hongze Cheng 已提交
163
    }
H
Hongze Cheng 已提交
164

H
Hongze Cheng 已提交
165
    continue;
H
Hongze Cheng 已提交
166

H
Hongze Cheng 已提交
167 168 169 170
  _add_iter:
    tRBTreePut(&pReader->rbt, (SRBTreeNode*)pIter);
    pIter++;
  }
H
Hongze Cheng 已提交
171

H
Hongze Cheng 已提交
172 173 174 175 176
  tsdbInfo("vgId:%d, vnode snapshot tsdb open data file to read for %s, fid:%d", TD_VID(pReader->pTsdb->pVnode),
           pReader->pTsdb->path, pReader->fid);
  return code;

_err:
S
Shengliang Guan 已提交
177
  tsdbError("vgId:%d, vnode snapshot tsdb snap read open file failed since %s", TD_VID(pReader->pTsdb->pVnode),
H
Hongze Cheng 已提交
178 179 180 181 182 183 184 185 186 187 188
            tstrerror(code));
  return code;
}

static SRowInfo* tsdbSnapGetRow(STsdbSnapReader* pReader) { return pReader->pIter ? &pReader->pIter->rInfo : NULL; }

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

  if (pReader->pIter) {
    SFDataIter* pIter = pReader->pIter;
189

H
Hongze Cheng 已提交
190 191 192 193 194 195 196 197 198
    while (true) {
    _find_row:
      for (pIter->iRow++; pIter->iRow < pIter->bData.nRow; pIter->iRow++) {
        int64_t rowVer = pIter->bData.aVersion[pIter->iRow];

        if (rowVer >= pReader->sver && rowVer <= pReader->ever) {
          pIter->rInfo.uid = pIter->bData.uid ? pIter->bData.uid : pIter->bData.aUid[pIter->iRow];
          pIter->rInfo.row = tsdbRowFromBlockData(&pIter->bData, pIter->iRow);
          goto _out;
H
Hongze Cheng 已提交
199
        }
H
Hongze Cheng 已提交
200
      }
H
Hongze Cheng 已提交
201

H
Hongze Cheng 已提交
202 203 204 205 206
      if (pIter->type == SNAP_DATA_FILE_ITER) {
        while (true) {
          for (pIter->iBlock++; pIter->iBlock < pIter->mBlock.nItem; pIter->iBlock++) {
            SDataBlk dataBlk;
            tMapDataGetItemByIdx(&pIter->mBlock, pIter->iBlock, &dataBlk, tGetDataBlk);
H
Hongze Cheng 已提交
207

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

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

H
Hongze Cheng 已提交
213 214
            pIter->iRow = -1;
            goto _find_row;
H
Hongze Cheng 已提交
215
          }
H
Hongze Cheng 已提交
216 217 218 219 220

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

          pIter->pBlockIdx = (SBlockIdx*)taosArrayGet(pIter->aBlockIdx, pIter->iBlockIdx);
H
Hongze Cheng 已提交
221
          code = tsdbReadDataBlk(pReader->pDataFReader, pIter->pBlockIdx, &pIter->mBlock);
H
Hongze Cheng 已提交
222 223
          if (code) goto _err;
          pIter->iBlock = -1;
H
Hongze Cheng 已提交
224
        }
H
Hongze Cheng 已提交
225

H
Hongze Cheng 已提交
226 227 228 229
        pReader->pIter = NULL;
      } else if (pIter->type == SNAP_STT_FILE_ITER) {
        for (pIter->iSttBlk++; pIter->iSttBlk < taosArrayGetSize(pIter->aSttBlk); pIter->iSttBlk++) {
          SSttBlk* pSttBlk = (SSttBlk*)taosArrayGet(pIter->aSttBlk, pIter->iSttBlk);
H
Hongze Cheng 已提交
230

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

H
Hongze Cheng 已提交
233
          code = tsdbReadSttBlockEx(pReader->pDataFReader, pIter->iStt, pSttBlk, &pIter->bData);
H
Hongze Cheng 已提交
234 235 236 237
          if (code) goto _err;

          pIter->iRow = -1;
          goto _find_row;
H
Hongze Cheng 已提交
238
        }
H
Hongze Cheng 已提交
239 240

        pReader->pIter = NULL;
H
Hongze Cheng 已提交
241
      } else {
H
Hongze Cheng 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
        ASSERT(0);
      }
    }

  _out:
    pIter = (SFDataIter*)tRBTreeMin(&pReader->rbt);
    if (pReader->pIter && pIter) {
      int32_t c = tRowInfoCmprFn(&pReader->pIter->rInfo, &pIter->rInfo);
      if (c > 0) {
        tRBTreePut(&pReader->rbt, (SRBTreeNode*)pReader->pIter);
        pReader->pIter = NULL;
      } else {
        ASSERT(c);
      }
    }
  }

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

  return code;

_err:
  return code;
}

static int32_t tsdbSnapCmprData(STsdbSnapReader* pReader, uint8_t** ppData) {
  int32_t code = 0;

  ASSERT(pReader->bData.nRow);

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

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

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

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

_exit:
  return code;
}

static int32_t tsdbSnapReadData(STsdbSnapReader* pReader, uint8_t** ppData) {
  int32_t code = 0;
  STsdb*  pTsdb = pReader->pTsdb;

  while (true) {
    if (pReader->pDataFReader == NULL) {
      code = tsdbSnapReadOpenFile(pReader);
      if (code) goto _err;
    }

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

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

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

    code = tsdbUpdateTableSchema(pTsdb->pVnode->pMeta, id.suid, id.uid, &pReader->skmTable);
    if (code) goto _err;

H
Hongze Cheng 已提交
329
    code = tBlockDataInit(pBlockData, &id, pReader->skmTable.pTSchema, NULL, 0);
H
Hongze Cheng 已提交
330 331 332 333 334 335 336 337 338 339 340
    if (code) goto _err;

    while (pRowInfo->suid == id.suid && pRowInfo->uid == id.uid) {
      code = tBlockDataAppendRow(pBlockData, &pRowInfo->row, NULL, pRowInfo->uid);
      if (code) goto _err;

      code = tsdbSnapNextRow(pReader);
      if (code) goto _err;

      pRowInfo = tsdbSnapGetRow(pReader);
      if (pRowInfo == NULL) {
H
Hongze Cheng 已提交
341 342
        tsdbDataFReaderClose(&pReader->pDataFReader);
        break;
H
Hongze Cheng 已提交
343
      }
H
Hongze Cheng 已提交
344 345

      if (pBlockData->nRow >= 4096) break;
H
Hongze Cheng 已提交
346
    }
H
Hongze Cheng 已提交
347 348 349 350 351

    code = tsdbSnapCmprData(pReader, ppData);
    if (code) goto _err;

    break;
H
Hongze Cheng 已提交
352 353
  }

H
Hongze Cheng 已提交
354 355 356
  return code;

_err:
S
Shengliang Guan 已提交
357
  tsdbError("vgId:%d, vnode snapshot tsdb read data for %s failed since %s", TD_VID(pTsdb->pVnode), pTsdb->path,
C
Cary Xu 已提交
358
            tstrerror(code));
H
Hongze Cheng 已提交
359 360 361 362 363 364
  return code;
}

static int32_t tsdbSnapReadDel(STsdbSnapReader* pReader, uint8_t** ppData) {
  int32_t   code = 0;
  STsdb*    pTsdb = pReader->pTsdb;
H
Hongze Cheng 已提交
365
  SDelFile* pDelFile = pReader->fs.pDelFile;
H
Hongze Cheng 已提交
366 367 368 369 370 371 372

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

    // open
H
Hongze Cheng 已提交
373
    code = tsdbDelFReaderOpen(&pReader->pDelFReader, pDelFile, pTsdb);
H
Hongze Cheng 已提交
374 375
    if (code) goto _err;

H
Hongze Cheng 已提交
376
    // read index
H
Hongze Cheng 已提交
377
    code = tsdbReadDelIdx(pReader->pDelFReader, pReader->aDelIdx);
H
Hongze Cheng 已提交
378 379 380 381 382
    if (code) goto _err;

    pReader->iDelIdx = 0;
  }

H
Hongze Cheng 已提交
383 384 385 386 387 388
  while (true) {
    if (pReader->iDelIdx >= taosArrayGetSize(pReader->aDelIdx)) {
      tsdbDelFReaderClose(&pReader->pDelFReader);
      break;
    }

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

H
Hongze Cheng 已提交
391 392
    pReader->iDelIdx++;

H
Hongze Cheng 已提交
393
    code = tsdbReadDelData(pReader->pDelFReader, pDelIdx, pReader->aDelData);
H
Hongze Cheng 已提交
394 395
    if (code) goto _err;

H
Hongze Cheng 已提交
396
    int32_t size = 0;
H
Hongze Cheng 已提交
397 398 399 400
    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 已提交
401
        size += tPutDelData(NULL, pDelData);
H
Hongze Cheng 已提交
402 403
      }
    }
H
Hongze Cheng 已提交
404
    if (size == 0) continue;
H
Hongze Cheng 已提交
405

H
Hongze Cheng 已提交
406 407 408 409 410 411 412
    // 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 已提交
413

H
Hongze Cheng 已提交
414
    SSnapDataHdr* pHdr = (SSnapDataHdr*)(*ppData);
C
Cary Xu 已提交
415
    pHdr->type = SNAP_DATA_DEL;
H
Hongze Cheng 已提交
416
    pHdr->size = size;
H
Hongze Cheng 已提交
417

H
Hongze Cheng 已提交
418 419 420 421 422 423
    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 已提交
424

H
Hongze Cheng 已提交
425 426
      if (pDelData->version < pReader->sver) continue;
      if (pDelData->version > pReader->ever) continue;
H
Hongze Cheng 已提交
427

H
Hongze Cheng 已提交
428
      n += tPutDelData((*ppData) + n, pDelData);
H
Hongze Cheng 已提交
429 430
    }

H
Hongze Cheng 已提交
431
    tsdbInfo("vgId:%d, vnode snapshot tsdb read del data for %s, suid:%" PRId64 " uid:%" PRId64 " size:%d",
C
Cary Xu 已提交
432
             TD_VID(pTsdb->pVnode), pTsdb->path, pDelIdx->suid, pDelIdx->uid, size);
H
Hongze Cheng 已提交
433 434 435

    break;
  }
H
Hongze Cheng 已提交
436 437 438 439 440

_exit:
  return code;

_err:
H
Hongze Cheng 已提交
441
  tsdbError("vgId:%d, vnode snapshot tsdb read del for %s failed since %s", TD_VID(pTsdb->pVnode), pTsdb->path,
C
Cary Xu 已提交
442
            tstrerror(code));
H
Hongze Cheng 已提交
443 444
  return code;
}
H
more  
Hongze Cheng 已提交
445

C
Cary Xu 已提交
446
int32_t tsdbSnapReaderOpen(STsdb* pTsdb, int64_t sver, int64_t ever, int8_t type, STsdbSnapReader** ppReader) {
H
Hongze Cheng 已提交
447
  int32_t          code = 0;
H
Hongze Cheng 已提交
448
  int32_t          lino = 0;
H
Hongze Cheng 已提交
449
  STsdbSnapReader* pReader = NULL;
H
Hongze Cheng 已提交
450

H
more  
Hongze Cheng 已提交
451
  // alloc
H
Hongze Cheng 已提交
452
  pReader = (STsdbSnapReader*)taosMemoryCalloc(1, sizeof(*pReader));
H
more  
Hongze Cheng 已提交
453 454
  if (pReader == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
455
    TSDB_CHECK_CODE(code, lino, _exit);
H
more  
Hongze Cheng 已提交
456 457 458 459
  }
  pReader->pTsdb = pTsdb;
  pReader->sver = sver;
  pReader->ever = ever;
C
Cary Xu 已提交
460
  pReader->type = type;
H
more  
Hongze Cheng 已提交
461

H
Hongze Cheng 已提交
462 463 464
  code = taosThreadRwlockRdlock(&pTsdb->rwLock);
  if (code) {
    code = TAOS_SYSTEM_ERROR(code);
H
Hongze Cheng 已提交
465
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
466 467 468 469 470
  }

  code = tsdbFSRef(pTsdb, &pReader->fs);
  if (code) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
471
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
472 473 474 475 476
  }

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

H
Hongze Cheng 已提交
480
  // data
H
Hongze Cheng 已提交
481
  pReader->fid = INT32_MIN;
H
Hongze Cheng 已提交
482 483 484 485 486 487 488
  for (int32_t iIter = 0; iIter < sizeof(pReader->aFDataIter) / sizeof(pReader->aFDataIter[0]); iIter++) {
    SFDataIter* pIter = &pReader->aFDataIter[iIter];

    if (iIter == 0) {
      pIter->aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
      if (pIter->aBlockIdx == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
489
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
490 491 492 493 494
      }
    } else {
      pIter->aSttBlk = taosArrayInit(0, sizeof(SSttBlk));
      if (pIter->aSttBlk == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
495
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
496 497 498 499
      }
    }

    code = tBlockDataCreate(&pIter->bData);
H
Hongze Cheng 已提交
500
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
501
  }
H
Hongze Cheng 已提交
502 503

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

H
Hongze Cheng 已提交
506
  // del
H
Hongze Cheng 已提交
507 508 509
  pReader->aDelIdx = taosArrayInit(0, sizeof(SDelIdx));
  if (pReader->aDelIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
510
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
511 512 513 514
  }
  pReader->aDelData = taosArrayInit(0, sizeof(SDelData));
  if (pReader->aDelData == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
515
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
516 517
  }

H
Hongze Cheng 已提交
518 519
_exit:
  if (code) {
S
Shengliang Guan 已提交
520
    tsdbError("vgId:%d, %s failed at line %d since %s, TSDB path: %s", TD_VID(pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
521
              tstrerror(code), pTsdb->path);
H
Hongze Cheng 已提交
522 523 524 525 526
    *ppReader = NULL;

    if (pReader) {
      taosArrayDestroy(pReader->aDelData);
      taosArrayDestroy(pReader->aDelIdx);
H
Hongze Cheng 已提交
527
      tBlockDataDestroy(&pReader->bData);
H
Hongze Cheng 已提交
528 529 530 531 532 533 534
      tsdbFSDestroy(&pReader->fs);
      taosMemoryFree(pReader);
    }
  } else {
    *ppReader = pReader;
    tsdbInfo("vgId:%d, vnode snapshot tsdb reader opened for %s", TD_VID(pTsdb->pVnode), pTsdb->path);
  }
H
more  
Hongze Cheng 已提交
535
  return code;
H
Hongze Cheng 已提交
536 537
}

H
Hongze Cheng 已提交
538
int32_t tsdbSnapReaderClose(STsdbSnapReader** ppReader) {
H
Hongze Cheng 已提交
539
  int32_t          code = 0;
H
Hongze Cheng 已提交
540 541
  STsdbSnapReader* pReader = *ppReader;

H
Hongze Cheng 已提交
542 543 544 545 546 547 548 549 550 551 552 553
  // data
  if (pReader->pDataFReader) tsdbDataFReaderClose(&pReader->pDataFReader);
  for (int32_t iIter = 0; iIter < sizeof(pReader->aFDataIter) / sizeof(pReader->aFDataIter[0]); iIter++) {
    SFDataIter* pIter = &pReader->aFDataIter[iIter];

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

H
Hongze Cheng 已提交
554
    tBlockDataDestroy(&pIter->bData);
H
Hongze Cheng 已提交
555
  }
H
Hongze Cheng 已提交
556

H
Hongze Cheng 已提交
557
  tBlockDataDestroy(&pReader->bData);
H
Hongze Cheng 已提交
558
  tDestroyTSchema(pReader->skmTable.pTSchema);
H
Hongze Cheng 已提交
559 560 561

  // del
  if (pReader->pDelFReader) tsdbDelFReaderClose(&pReader->pDelFReader);
H
Hongze Cheng 已提交
562 563 564
  taosArrayDestroy(pReader->aDelIdx);
  taosArrayDestroy(pReader->aDelData);

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

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

H
Hongze Cheng 已提交
569 570 571 572
  for (int32_t iBuf = 0; iBuf < sizeof(pReader->aBuf) / sizeof(pReader->aBuf[0]); iBuf++) {
    tFree(pReader->aBuf[iBuf]);
  }

H
Hongze Cheng 已提交
573 574
  taosMemoryFree(pReader);
  *ppReader = NULL;
H
Hongze Cheng 已提交
575 576 577 578
  return code;
}

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

H
Hongze Cheng 已提交
581 582
  *ppData = NULL;

H
Hongze Cheng 已提交
583
  // read data file
H
Hongze Cheng 已提交
584 585 586
  if (!pReader->dataDone) {
    code = tsdbSnapReadData(pReader, ppData);
    if (code) {
H
Hongze Cheng 已提交
587 588 589 590
      goto _err;
    } else {
      if (*ppData) {
        goto _exit;
H
Hongze Cheng 已提交
591
      } else {
H
Hongze Cheng 已提交
592
        pReader->dataDone = 1;
H
Hongze Cheng 已提交
593 594 595
      }
    }
  }
H
Hongze Cheng 已提交
596 597

  // read del file
H
Hongze Cheng 已提交
598 599 600
  if (!pReader->delDone) {
    code = tsdbSnapReadDel(pReader, ppData);
    if (code) {
H
Hongze Cheng 已提交
601 602 603 604
      goto _err;
    } else {
      if (*ppData) {
        goto _exit;
H
Hongze Cheng 已提交
605
      } else {
H
Hongze Cheng 已提交
606
        pReader->delDone = 1;
H
Hongze Cheng 已提交
607 608 609
      }
    }
  }
H
Hongze Cheng 已提交
610

H
Hongze Cheng 已提交
611
_exit:
S
Shengliang Guan 已提交
612
  tsdbDebug("vgId:%d, vnode snapshot tsdb read for %s", TD_VID(pReader->pTsdb->pVnode), pReader->pTsdb->path);
H
more  
Hongze Cheng 已提交
613 614 615
  return code;

_err:
S
Shengliang Guan 已提交
616
  tsdbError("vgId:%d, vnode snapshot tsdb read for %s failed since %s", TD_VID(pReader->pTsdb->pVnode),
C
Cary Xu 已提交
617
            pReader->pTsdb->path, tstrerror(code));
H
more  
Hongze Cheng 已提交
618 619 620
  return code;
}

H
Hongze Cheng 已提交
621
// STsdbSnapWriter ========================================
H
Hongze Cheng 已提交
622 623 624 625
struct STsdbSnapWriter {
  STsdb*  pTsdb;
  int64_t sver;
  int64_t ever;
H
Hongze Cheng 已提交
626
  STsdbFS fs;
H
Hongze Cheng 已提交
627

H
Hongze Cheng 已提交
628
  // config
H
Hongze Cheng 已提交
629 630 631 632 633 634
  int32_t  minutes;
  int8_t   precision;
  int32_t  minRow;
  int32_t  maxRow;
  int8_t   cmprAlg;
  int64_t  commitID;
H
Hongze Cheng 已提交
635
  uint8_t* aBuf[5];
H
Hongze Cheng 已提交
636

H
Hongze Cheng 已提交
637
  // for data file
H
Hongze Cheng 已提交
638
  SBlockData bData;
H
Hongze Cheng 已提交
639 640 641
  int32_t    fid;
  TABLEID    id;
  SSkmInfo   skmTable;
H
Hongze Cheng 已提交
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
  struct {
    SDataFReader* pReader;
    SArray*       aBlockIdx;
    int32_t       iBlockIdx;
    SBlockIdx*    pBlockIdx;
    SMapData      mDataBlk;
    int32_t       iDataBlk;
    SBlockData    bData;
    int32_t       iRow;
  } dReader;
  struct {
    SDataFWriter* pWriter;
    SArray*       aBlockIdx;
    SMapData      mDataBlk;
    SArray*       aSttBlk;
    SBlockData    bData;
    SBlockData    sData;
  } dWriter;
H
Hongze Cheng 已提交
660

H
Hongze Cheng 已提交
661
  // for del file
H
Hongze Cheng 已提交
662
  SDelFReader* pDelFReader;
H
Hongze Cheng 已提交
663
  SDelFWriter* pDelFWriter;
H
Hongze Cheng 已提交
664
  int32_t      iDelIdx;
H
Hongze Cheng 已提交
665
  SArray*      aDelIdxR;
H
Hongze Cheng 已提交
666
  SArray*      aDelData;
H
Hongze Cheng 已提交
667
  SArray*      aDelIdxW;
H
Hongze Cheng 已提交
668 669
};

H
Hongze Cheng 已提交
670
// SNAP_DATA_TSDB
H
Hongze Cheng 已提交
671 672 673
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 已提交
674
static int32_t tsdbSnapNextTableData(STsdbSnapWriter* pWriter) {
H
Hongze Cheng 已提交
675
  int32_t code = 0;
H
Hongze Cheng 已提交
676

H
Hongze Cheng 已提交
677 678
  ASSERT(pWriter->dReader.iRow >= pWriter->dReader.bData.nRow);

H
Hongze Cheng 已提交
679 680 681 682 683 684
  if (pWriter->dReader.iBlockIdx < taosArrayGetSize(pWriter->dReader.aBlockIdx)) {
    pWriter->dReader.pBlockIdx = (SBlockIdx*)taosArrayGet(pWriter->dReader.aBlockIdx, pWriter->dReader.iBlockIdx);

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

H
Hongze Cheng 已提交
685
    pWriter->dReader.iBlockIdx++;
H
Hongze Cheng 已提交
686 687
  } else {
    pWriter->dReader.pBlockIdx = NULL;
H
Hongze Cheng 已提交
688
    tMapDataReset(&pWriter->dReader.mDataBlk);
H
Hongze Cheng 已提交
689
  }
H
Hongze Cheng 已提交
690 691 692
  pWriter->dReader.iDataBlk = 0;  // point to the next one
  tBlockDataReset(&pWriter->dReader.bData);
  pWriter->dReader.iRow = 0;
H
Hongze Cheng 已提交
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715

_exit:
  return code;
}

static int32_t tsdbSnapWriteCopyData(STsdbSnapWriter* pWriter, TABLEID* pId) {
  int32_t code = 0;

  while (true) {
    if (pWriter->dReader.pBlockIdx == NULL) break;
    if (tTABLEIDCmprFn(pWriter->dReader.pBlockIdx, pId) >= 0) break;

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

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

    code = tsdbSnapNextTableData(pWriter);
    if (code) goto _exit;
H
Hongze Cheng 已提交
716 717
  }

H
Hongze Cheng 已提交
718 719 720 721
_exit:
  return code;
}

H
Hongze Cheng 已提交
722 723
static int32_t tsdbSnapWriteTableDataStart(STsdbSnapWriter* pWriter, TABLEID* pId) {
  int32_t code = 0;
H
Hongze Cheng 已提交
724 725 726 727 728 729 730 731 732 733 734

  code = tsdbSnapWriteCopyData(pWriter, pId);
  if (code) goto _err;

  pWriter->id.suid = pId->suid;
  pWriter->id.uid = pId->uid;

  code = tsdbUpdateTableSchema(pWriter->pTsdb->pVnode->pMeta, pId->suid, pId->uid, &pWriter->skmTable);
  if (code) goto _err;

  tMapDataReset(&pWriter->dWriter.mDataBlk);
H
Hongze Cheng 已提交
735
  code = tBlockDataInit(&pWriter->dWriter.bData, pId, pWriter->skmTable.pTSchema, NULL, 0);
H
Hongze Cheng 已提交
736 737 738 739 740
  if (code) goto _err;

  return code;

_err:
S
Shengliang Guan 已提交
741
  tsdbError("vgId:%d, %s failed since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, tstrerror(code));
H
Hongze Cheng 已提交
742 743 744 745 746 747 748 749
  return code;
}

static int32_t tsdbSnapWriteTableDataEnd(STsdbSnapWriter* pWriter) {
  int32_t code = 0;

  if (pWriter->id.suid == 0 && pWriter->id.uid == 0) return code;

H
Hongze Cheng 已提交
750 751 752 753 754 755 756 757 758
  int32_t c = 1;
  if (pWriter->dReader.pBlockIdx) {
    c = tTABLEIDCmprFn(pWriter->dReader.pBlockIdx, &pWriter->id);
    ASSERT(c >= 0);
  }

  if (c == 0) {
    SBlockData* pBData = &pWriter->dWriter.bData;

H
Hongze Cheng 已提交
759 760
    for (; pWriter->dReader.iRow < pWriter->dReader.bData.nRow; pWriter->dReader.iRow++) {
      TSDBROW row = tsdbRowFromBlockData(&pWriter->dReader.bData, pWriter->dReader.iRow);
H
Hongze Cheng 已提交
761 762

      code = tBlockDataAppendRow(pBData, &row, NULL, pWriter->id.uid);
H
Hongze Cheng 已提交
763 764
      if (code) goto _err;

H
Hongze Cheng 已提交
765 766
      if (pBData->nRow >= pWriter->maxRow) {
        code = tsdbWriteDataBlock(pWriter->dWriter.pWriter, pBData, &pWriter->dWriter.mDataBlk, pWriter->cmprAlg);
H
Hongze Cheng 已提交
767 768 769 770
        if (code) goto _err;
      }
    }

H
Hongze Cheng 已提交
771
    code = tsdbWriteDataBlock(pWriter->dWriter.pWriter, pBData, &pWriter->dWriter.mDataBlk, pWriter->cmprAlg);
H
Hongze Cheng 已提交
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
    if (code) goto _err;

    for (; pWriter->dReader.iDataBlk < pWriter->dReader.mDataBlk.nItem; pWriter->dReader.iDataBlk++) {
      SDataBlk dataBlk;
      tMapDataGetItemByIdx(&pWriter->dReader.mDataBlk, pWriter->dReader.iDataBlk, &dataBlk, tGetDataBlk);

      code = tMapDataPutItem(&pWriter->dWriter.mDataBlk, &dataBlk, tPutDataBlk);
      if (code) goto _err;
    }

    code = tsdbSnapNextTableData(pWriter);
    if (code) goto _err;
  }

  if (pWriter->dWriter.mDataBlk.nItem) {
    SBlockIdx blockIdx = {.suid = pWriter->id.suid, .uid = pWriter->id.uid};
    code = tsdbWriteDataBlk(pWriter->dWriter.pWriter, &pWriter->dWriter.mDataBlk, &blockIdx);

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

H
Hongze Cheng 已提交
796 797 798
  pWriter->id.suid = 0;
  pWriter->id.uid = 0;

H
Hongze Cheng 已提交
799 800 801 802 803 804
  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
805 806 807 808
static int32_t tsdbSnapWriteOpenFile(STsdbSnapWriter* pWriter, int32_t fid) {
  int32_t code = 0;
  STsdb*  pTsdb = pWriter->pTsdb;

H
Hongze Cheng 已提交
809 810 811
  ASSERT(pWriter->dWriter.pWriter == NULL);

  pWriter->fid = fid;
H
Hongze Cheng 已提交
812
  pWriter->id = (TABLEID){0};
H
Hongze Cheng 已提交
813 814
  SDFileSet* pSet = taosArraySearch(pWriter->fs.aDFileSet, &(SDFileSet){.fid = fid}, tDFileSetCmprFn, TD_EQ);

H
Hongze Cheng 已提交
815
  // Reader
H
Hongze Cheng 已提交
816 817 818 819 820 821 822
  if (pSet) {
    code = tsdbDataFReaderOpen(&pWriter->dReader.pReader, pWriter->pTsdb, pSet);
    if (code) goto _err;

    code = tsdbReadBlockIdx(pWriter->dReader.pReader, pWriter->dReader.aBlockIdx);
    if (code) goto _err;
  } else {
H
Hongze Cheng 已提交
823 824
    ASSERT(pWriter->dReader.pReader == NULL);
    taosArrayClear(pWriter->dReader.aBlockIdx);
H
Hongze Cheng 已提交
825
  }
H
Hongze Cheng 已提交
826 827 828
  pWriter->dReader.iBlockIdx = 0;  // point to the next one
  code = tsdbSnapNextTableData(pWriter);
  if (code) goto _err;
H
Hongze Cheng 已提交
829

H
Hongze Cheng 已提交
830
  // Writer
H
Hongze Cheng 已提交
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
  SHeadFile fHead = {.commitID = pWriter->commitID};
  SDataFile fData = {.commitID = pWriter->commitID};
  SSmaFile  fSma = {.commitID = pWriter->commitID};
  SSttFile  fStt = {.commitID = pWriter->commitID};
  SDFileSet wSet = {.fid = pWriter->fid, .pHeadF = &fHead, .pDataF = &fData, .pSmaF = &fSma};
  if (pSet) {
    wSet.diskId = pSet->diskId;
    fData = *pSet->pDataF;
    fSma = *pSet->pSmaF;
    for (int32_t iStt = 0; iStt < pSet->nSttF; iStt++) {
      wSet.aSttF[iStt] = pSet->aSttF[iStt];
    }
    wSet.nSttF = pSet->nSttF + 1;  // TODO: fix pSet->nSttF == pTsdb->maxFile
  } else {
    SDiskID did = {0};
    tfsAllocDisk(pTsdb->pVnode->pTfs, 0, &did);
    tfsMkdirRecurAt(pTsdb->pVnode->pTfs, pTsdb->path, did);
    wSet.diskId = did;
    wSet.nSttF = 1;
  }
  wSet.aSttF[wSet.nSttF - 1] = &fStt;
H
Hongze Cheng 已提交
852

H
Hongze Cheng 已提交
853
  code = tsdbDataFWriterOpen(&pWriter->dWriter.pWriter, pWriter->pTsdb, &wSet);
H
Hongze Cheng 已提交
854
  if (code) goto _err;
H
Hongze Cheng 已提交
855 856 857
  taosArrayClear(pWriter->dWriter.aBlockIdx);
  tMapDataReset(&pWriter->dWriter.mDataBlk);
  taosArrayClear(pWriter->dWriter.aSttBlk);
H
Hongze Cheng 已提交
858 859
  tBlockDataReset(&pWriter->dWriter.bData);
  tBlockDataReset(&pWriter->dWriter.sData);
H
Hongze Cheng 已提交
860 861 862 863 864 865 866 867 868 869

  return code;

_err:
  return code;
}

static int32_t tsdbSnapWriteCloseFile(STsdbSnapWriter* pWriter) {
  int32_t code = 0;

H
Hongze Cheng 已提交
870
  ASSERT(pWriter->dWriter.pWriter);
H
Hongze Cheng 已提交
871

H
Hongze Cheng 已提交
872 873
  code = tsdbSnapWriteTableDataEnd(pWriter);
  if (code) goto _err;
H
Hongze Cheng 已提交
874

H
Hongze Cheng 已提交
875 876 877
  // copy remain table data
  TABLEID id = {.suid = INT64_MAX, .uid = INT64_MAX};
  code = tsdbSnapWriteCopyData(pWriter, &id);
H
Hongze Cheng 已提交
878
  if (code) goto _err;
H
Hongze Cheng 已提交
879

H
Hongze Cheng 已提交
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
  code =
      tsdbWriteSttBlock(pWriter->dWriter.pWriter, &pWriter->dWriter.sData, pWriter->dWriter.aSttBlk, pWriter->cmprAlg);
  if (code) goto _err;

  // Indices
  code = tsdbWriteBlockIdx(pWriter->dWriter.pWriter, pWriter->dWriter.aBlockIdx);
  if (code) goto _err;

  code = tsdbWriteSttBlk(pWriter->dWriter.pWriter, pWriter->dWriter.aSttBlk);
  if (code) goto _err;

  code = tsdbUpdateDFileSetHeader(pWriter->dWriter.pWriter);
  if (code) goto _err;

  code = tsdbFSUpsertFSet(&pWriter->fs, &pWriter->dWriter.pWriter->wSet);
  if (code) goto _err;

  code = tsdbDataFWriterClose(&pWriter->dWriter.pWriter, 1);
  if (code) goto _err;

  if (pWriter->dReader.pReader) {
    code = tsdbDataFReaderClose(&pWriter->dReader.pReader);
    if (code) goto _err;
H
Hongze Cheng 已提交
903 904 905 906
  }

_exit:
  return code;
H
Hongze Cheng 已提交
907 908 909

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

H
Hongze Cheng 已提交
912
static int32_t tsdbSnapWriteToDataFile(STsdbSnapWriter* pWriter, int32_t iRow, int8_t* done) {
H
Hongze Cheng 已提交
913 914
  int32_t code = 0;

H
Hongze Cheng 已提交
915 916 917
  SBlockData* pBData = &pWriter->bData;
  TABLEID     id = {.suid = pBData->suid, .uid = pBData->uid ? pBData->uid : pBData->aUid[iRow]};
  TSDBROW     row = tsdbRowFromBlockData(pBData, iRow);
H
Hongze Cheng 已提交
918 919
  TSDBKEY     key = TSDBROW_KEY(&row);

H
Hongze Cheng 已提交
920 921 922 923
  *done = 0;
  while (pWriter->dReader.iRow < pWriter->dReader.bData.nRow ||
         pWriter->dReader.iDataBlk < pWriter->dReader.mDataBlk.nItem) {
    // Merge row by row
H
Hongze Cheng 已提交
924 925 926
    for (; pWriter->dReader.iRow < pWriter->dReader.bData.nRow; pWriter->dReader.iRow++) {
      TSDBROW trow = tsdbRowFromBlockData(&pWriter->dReader.bData, pWriter->dReader.iRow);
      TSDBKEY tKey = TSDBROW_KEY(&trow);
H
Hongze Cheng 已提交
927

H
Hongze Cheng 已提交
928 929
      ASSERT(pWriter->dReader.bData.suid == id.suid && pWriter->dReader.bData.uid == id.uid);

H
Hongze Cheng 已提交
930 931 932 933 934 935 936 937 938 939
      int32_t c = tsdbKeyCmprFn(&key, &tKey);
      if (c < 0) {
        code = tBlockDataAppendRow(&pWriter->dWriter.bData, &row, NULL, id.uid);
        if (code) goto _err;
      } else if (c > 0) {
        code = tBlockDataAppendRow(&pWriter->dWriter.bData, &trow, NULL, id.uid);
        if (code) goto _err;
      } else {
        ASSERT(0);
      }
H
Hongze Cheng 已提交
940

H
Hongze Cheng 已提交
941
      if (pWriter->dWriter.bData.nRow >= pWriter->maxRow) {
H
Hongze Cheng 已提交
942 943 944
        code = tsdbWriteDataBlock(pWriter->dWriter.pWriter, &pWriter->dWriter.bData, &pWriter->dWriter.mDataBlk,
                                  pWriter->cmprAlg);
        if (code) goto _err;
H
Hongze Cheng 已提交
945 946
      }

H
Hongze Cheng 已提交
947 948 949 950
      if (c < 0) {
        *done = 1;
        goto _exit;
      }
H
Hongze Cheng 已提交
951
    }
H
Hongze Cheng 已提交
952

H
Hongze Cheng 已提交
953
    // Merge row by block
H
Hongze Cheng 已提交
954
    SDataBlk tDataBlk = {.minKey = key, .maxKey = key};
H
Hongze Cheng 已提交
955
    for (; pWriter->dReader.iDataBlk < pWriter->dReader.mDataBlk.nItem; pWriter->dReader.iDataBlk++) {
H
Hongze Cheng 已提交
956
      SDataBlk dataBlk;
H
Hongze Cheng 已提交
957
      tMapDataGetItemByIdx(&pWriter->dReader.mDataBlk, pWriter->dReader.iDataBlk, &dataBlk, tGetDataBlk);
H
Hongze Cheng 已提交
958 959 960

      int32_t c = tDataBlkCmprFn(&dataBlk, &tDataBlk);
      if (c < 0) {
H
Hongze Cheng 已提交
961 962 963 964
        code = tsdbWriteDataBlock(pWriter->dWriter.pWriter, &pWriter->dWriter.bData, &pWriter->dWriter.mDataBlk,
                                  pWriter->cmprAlg);
        if (code) goto _err;

H
Hongze Cheng 已提交
965 966
        code = tMapDataPutItem(&pWriter->dWriter.mDataBlk, &dataBlk, tPutDataBlk);
        if (code) goto _err;
H
Hongze Cheng 已提交
967
      } else if (c > 0) {
H
Hongze Cheng 已提交
968 969 970 971
        code = tBlockDataAppendRow(&pWriter->dWriter.bData, &row, NULL, id.uid);
        if (code) goto _err;

        if (pWriter->dWriter.bData.nRow >= pWriter->maxRow) {
H
Hongze Cheng 已提交
972 973 974
          code = tsdbWriteDataBlock(pWriter->dWriter.pWriter, &pWriter->dWriter.bData, &pWriter->dWriter.mDataBlk,
                                    pWriter->cmprAlg);
          if (code) goto _err;
H
Hongze Cheng 已提交
975
        }
H
Hongze Cheng 已提交
976 977 978

        *done = 1;
        goto _exit;
H
Hongze Cheng 已提交
979 980 981
      } else {
        code = tsdbReadDataBlockEx(pWriter->dReader.pReader, &dataBlk, &pWriter->dReader.bData);
        if (code) goto _err;
H
Hongze Cheng 已提交
982
        pWriter->dReader.iRow = 0;
H
Hongze Cheng 已提交
983

H
Hongze Cheng 已提交
984 985
        pWriter->dReader.iDataBlk++;
        break;
H
Hongze Cheng 已提交
986
      }
H
Hongze Cheng 已提交
987
    }
H
Hongze Cheng 已提交
988
  }
H
Hongze Cheng 已提交
989

H
Hongze Cheng 已提交
990 991 992 993
_exit:
  return code;

_err:
S
Shengliang Guan 已提交
994
  tsdbError("vgId:%d, %s failed since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, tstrerror(code));
H
Hongze Cheng 已提交
995 996 997
  return code;
}

H
Hongze Cheng 已提交
998
static int32_t tsdbSnapWriteToSttFile(STsdbSnapWriter* pWriter, int32_t iRow) {
H
Hongze Cheng 已提交
999 1000
  int32_t code = 0;

H
Hongze Cheng 已提交
1001 1002 1003 1004
  TABLEID     id = {.suid = pWriter->bData.suid,
                    .uid = pWriter->bData.uid ? pWriter->bData.uid : pWriter->bData.aUid[iRow]};
  TSDBROW     row = tsdbRowFromBlockData(&pWriter->bData, iRow);
  SBlockData* pBData = &pWriter->dWriter.sData;
H
Hongze Cheng 已提交
1005

H
Hongze Cheng 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
  if (pBData->suid || pBData->uid) {
    if (!TABLE_SAME_SCHEMA(pBData->suid, pBData->uid, id.suid, id.uid)) {
      code = tsdbWriteSttBlock(pWriter->dWriter.pWriter, pBData, pWriter->dWriter.aSttBlk, pWriter->cmprAlg);
      if (code) goto _err;

      pBData->suid = 0;
      pBData->uid = 0;
    }
  }

  if (pBData->suid == 0 && pBData->uid == 0) {
    code = tsdbUpdateTableSchema(pWriter->pTsdb->pVnode->pMeta, pWriter->id.suid, pWriter->id.uid, &pWriter->skmTable);
    if (code) goto _err;

H
Hongze Cheng 已提交
1020 1021
    TABLEID tid = {.suid = pWriter->id.suid, .uid = pWriter->id.suid ? 0 : pWriter->id.uid};
    code = tBlockDataInit(pBData, &tid, pWriter->skmTable.pTSchema, NULL, 0);
H
Hongze Cheng 已提交
1022 1023 1024 1025
    if (code) goto _err;
  }

  code = tBlockDataAppendRow(pBData, &row, NULL, id.uid);
H
Hongze Cheng 已提交
1026 1027
  if (code) goto _err;

H
Hongze Cheng 已提交
1028 1029
  if (pBData->nRow >= pWriter->maxRow) {
    code = tsdbWriteSttBlock(pWriter->dWriter.pWriter, pBData, pWriter->dWriter.aSttBlk, pWriter->cmprAlg);
H
Hongze Cheng 已提交
1030
    if (code) goto _err;
H
Hongze Cheng 已提交
1031 1032
  }

H
Hongze Cheng 已提交
1033 1034 1035 1036 1037 1038 1039
_exit:
  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
1040 1041 1042 1043 1044 1045 1046
static int32_t tsdbSnapWriteRowData(STsdbSnapWriter* pWriter, int32_t iRow) {
  int32_t code = 0;

  SBlockData* pBlockData = &pWriter->bData;
  TABLEID     id = {.suid = pBlockData->suid, .uid = pBlockData->uid ? pBlockData->uid : pBlockData->aUid[iRow]};

  // End last table data write if need
H
Hongze Cheng 已提交
1047
  if (tTABLEIDCmprFn(&pWriter->id, &id) != 0) {
H
Hongze Cheng 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
    code = tsdbSnapWriteTableDataEnd(pWriter);
    if (code) goto _err;
  }

  // Start new table data write if need
  if (pWriter->id.suid == 0 && pWriter->id.uid == 0) {
    code = tsdbSnapWriteTableDataStart(pWriter, &id);
    if (code) goto _err;
  }

  // Merge with .data file data
  int8_t done = 0;
H
Hongze Cheng 已提交
1060 1061 1062 1063
  if (pWriter->dReader.pBlockIdx && tTABLEIDCmprFn(pWriter->dReader.pBlockIdx, &id) == 0) {
    code = tsdbSnapWriteToDataFile(pWriter, iRow, &done);
    if (code) goto _err;
  }
H
Hongze Cheng 已提交
1064 1065 1066

  // Append to the .stt data block (todo: check if need to set/reload sst block)
  if (!done) {
H
Hongze Cheng 已提交
1067
    code = tsdbSnapWriteToSttFile(pWriter, iRow);
H
Hongze Cheng 已提交
1068 1069
    if (code) goto _err;
  }
H
Hongze Cheng 已提交
1070

H
Hongze Cheng 已提交
1071 1072 1073 1074
_exit:
  return code;

_err:
S
Shengliang Guan 已提交
1075
  tsdbError("vgId:%d, %s failed since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, tstrerror(code));
H
Hongze Cheng 已提交
1076 1077 1078
  return code;
}

H
Hongze Cheng 已提交
1079 1080 1081 1082 1083 1084 1085 1086
static int32_t tsdbSnapWriteData(STsdbSnapWriter* pWriter, uint8_t* pData, uint32_t nData) {
  int32_t     code = 0;
  STsdb*      pTsdb = pWriter->pTsdb;
  SBlockData* pBlockData = &pWriter->bData;

  // Decode data
  SSnapDataHdr* pHdr = (SSnapDataHdr*)pData;
  code = tDecmprBlockData(pHdr->data, pHdr->size, pBlockData, pWriter->aBuf);
H
Hongze Cheng 已提交
1087
  if (code) goto _err;
H
Hongze Cheng 已提交
1088

H
Hongze Cheng 已提交
1089 1090
  ASSERT(pBlockData->nRow > 0);

H
Hongze Cheng 已提交
1091 1092 1093 1094
  // Loop to handle each row
  for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
    TSKEY   ts = pBlockData->aTSKEY[iRow];
    int32_t fid = tsdbKeyFid(ts, pWriter->minutes, pWriter->precision);
H
Hongze Cheng 已提交
1095

H
Hongze Cheng 已提交
1096
    if (pWriter->dWriter.pWriter == NULL || pWriter->fid != fid) {
H
Hongze Cheng 已提交
1097 1098 1099 1100 1101 1102 1103
      if (pWriter->dWriter.pWriter) {
        ASSERT(fid > pWriter->fid);

        code = tsdbSnapWriteCloseFile(pWriter);
        if (code) goto _err;
      }

H
Hongze Cheng 已提交
1104 1105 1106 1107 1108 1109 1110 1111
      code = tsdbSnapWriteOpenFile(pWriter, fid);
      if (code) goto _err;
    }

    code = tsdbSnapWriteRowData(pWriter, iRow);
    if (code) goto _err;
  }

H
Hongze Cheng 已提交
1112 1113 1114
  return code;

_err:
S
Shengliang Guan 已提交
1115
  tsdbError("vgId:%d, vnode snapshot tsdb write data for %s failed since %s", TD_VID(pTsdb->pVnode), pTsdb->path,
C
Cary Xu 已提交
1116
            tstrerror(code));
H
Hongze Cheng 已提交
1117 1118 1119
  return code;
}

H
Hongze Cheng 已提交
1120
// SNAP_DATA_DEL
H
Hongze Cheng 已提交
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
static int32_t tsdbSnapMoveWriteDelData(STsdbSnapWriter* pWriter, TABLEID* pId) {
  int32_t code = 0;

  while (true) {
    if (pWriter->iDelIdx >= taosArrayGetSize(pWriter->aDelIdxR)) break;

    SDelIdx* pDelIdx = (SDelIdx*)taosArrayGet(pWriter->aDelIdxR, pWriter->iDelIdx);

    if (tTABLEIDCmprFn(pDelIdx, pId) >= 0) break;

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

    SDelIdx delIdx = *pDelIdx;
    code = tsdbWriteDelData(pWriter->pDelFWriter, pWriter->aDelData, &delIdx);
    if (code) goto _exit;

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

    pWriter->iDelIdx++;
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
1150 1151
static int32_t tsdbSnapWriteDel(STsdbSnapWriter* pWriter, uint8_t* pData, uint32_t nData) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1152 1153
  STsdb*  pTsdb = pWriter->pTsdb;

H
Hongze Cheng 已提交
1154
  // Open del file if not opened yet
H
Hongze Cheng 已提交
1155
  if (pWriter->pDelFWriter == NULL) {
H
Hongze Cheng 已提交
1156
    SDelFile* pDelFile = pWriter->fs.pDelFile;
H
Hongze Cheng 已提交
1157 1158 1159

    // reader
    if (pDelFile) {
H
Hongze Cheng 已提交
1160
      code = tsdbDelFReaderOpen(&pWriter->pDelFReader, pDelFile, pTsdb);
H
Hongze Cheng 已提交
1161 1162
      if (code) goto _err;

H
Hongze Cheng 已提交
1163
      code = tsdbReadDelIdx(pWriter->pDelFReader, pWriter->aDelIdxR);
H
Hongze Cheng 已提交
1164
      if (code) goto _err;
H
Hongze Cheng 已提交
1165 1166
    } else {
      taosArrayClear(pWriter->aDelIdxR);
H
Hongze Cheng 已提交
1167
    }
H
Hongze Cheng 已提交
1168
    pWriter->iDelIdx = 0;
H
Hongze Cheng 已提交
1169 1170

    // writer
H
Hongze Cheng 已提交
1171
    SDelFile delFile = {.commitID = pWriter->commitID};
H
Hongze Cheng 已提交
1172 1173
    code = tsdbDelFWriterOpen(&pWriter->pDelFWriter, &delFile, pTsdb);
    if (code) goto _err;
H
Hongze Cheng 已提交
1174
    taosArrayClear(pWriter->aDelIdxW);
H
Hongze Cheng 已提交
1175 1176
  }

H
Hongze Cheng 已提交
1177 1178
  SSnapDataHdr* pHdr = (SSnapDataHdr*)pData;
  TABLEID       id = *(TABLEID*)pHdr->data;
H
Hongze Cheng 已提交
1179

H
Hongze Cheng 已提交
1180
  ASSERT(pHdr->size + sizeof(SSnapDataHdr) == nData);
H
Hongze Cheng 已提交
1181

H
Hongze Cheng 已提交
1182 1183 1184
  // Move write data < id
  code = tsdbSnapMoveWriteDelData(pWriter, &id);
  if (code) goto _err;
H
Hongze Cheng 已提交
1185

H
Hongze Cheng 已提交
1186
  // Merge incoming data with current
H
Hongze Cheng 已提交
1187 1188 1189
  if (pWriter->iDelIdx < taosArrayGetSize(pWriter->aDelIdxR) &&
      tTABLEIDCmprFn(taosArrayGet(pWriter->aDelIdxR, pWriter->iDelIdx), &id) == 0) {
    SDelIdx* pDelIdx = (SDelIdx*)taosArrayGet(pWriter->aDelIdxR, pWriter->iDelIdx);
H
Hongze Cheng 已提交
1190

H
Hongze Cheng 已提交
1191
    code = tsdbReadDelData(pWriter->pDelFReader, pDelIdx, pWriter->aDelData);
H
Hongze Cheng 已提交
1192 1193
    if (code) goto _err;

H
Hongze Cheng 已提交
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
    pWriter->iDelIdx++;
  } else {
    taosArrayClear(pWriter->aDelData);
  }

  int64_t n = sizeof(SSnapDataHdr) + sizeof(TABLEID);
  while (n < nData) {
    SDelData delData;

    n += tGetDelData(pData + n, &delData);

    if (taosArrayPush(pWriter->aDelData, &delData) == NULL) {
H
Hongze Cheng 已提交
1206 1207 1208
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
1209
  }
H
Hongze Cheng 已提交
1210

H
Hongze Cheng 已提交
1211 1212 1213 1214 1215 1216 1217
  SDelIdx delIdx = {.suid = id.suid, .uid = id.uid};
  code = tsdbWriteDelData(pWriter->pDelFWriter, pWriter->aDelData, &delIdx);
  if (code) goto _err;

  if (taosArrayPush(pWriter->aDelIdxW, &delIdx) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
H
Hongze Cheng 已提交
1218 1219 1220 1221 1222
  }

  return code;

_err:
S
Shengliang Guan 已提交
1223
  tsdbError("vgId:%d, vnode snapshot tsdb write del for %s failed since %s", TD_VID(pTsdb->pVnode), pTsdb->path,
C
Cary Xu 已提交
1224
            tstrerror(code));
H
Hongze Cheng 已提交
1225 1226 1227
  return code;
}

H
Hongze Cheng 已提交
1228 1229
static int32_t tsdbSnapWriteDelEnd(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1230 1231
  STsdb*  pTsdb = pWriter->pTsdb;

H
Hongze Cheng 已提交
1232
  if (pWriter->pDelFWriter == NULL) return code;
H
Hongze Cheng 已提交
1233

H
Hongze Cheng 已提交
1234 1235 1236
  TABLEID id = {.suid = INT64_MAX, .uid = INT64_MAX};
  code = tsdbSnapMoveWriteDelData(pWriter, &id);
  if (code) goto _err;
H
Hongze Cheng 已提交
1237

H
Hongze Cheng 已提交
1238 1239 1240
  code = tsdbWriteDelIdx(pWriter->pDelFWriter, pWriter->aDelIdxW);
  if (code) goto _err;

H
Hongze Cheng 已提交
1241 1242 1243
  code = tsdbUpdateDelFileHdr(pWriter->pDelFWriter);
  if (code) goto _err;

H
Hongze Cheng 已提交
1244
  code = tsdbFSUpsertDelFile(&pWriter->fs, &pWriter->pDelFWriter->fDel);
H
Hongze Cheng 已提交
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
  if (code) goto _err;

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

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

S
Shengliang Guan 已提交
1255
  tsdbInfo("vgId:%d, vnode snapshot tsdb write del for %s end", TD_VID(pTsdb->pVnode), pTsdb->path);
H
Hongze Cheng 已提交
1256 1257 1258
  return code;

_err:
S
Shengliang Guan 已提交
1259
  tsdbError("vgId:%d, vnode snapshot tsdb write del end for %s failed since %s", TD_VID(pTsdb->pVnode), pTsdb->path,
C
Cary Xu 已提交
1260
            tstrerror(code));
H
Hongze Cheng 已提交
1261 1262 1263
  return code;
}

H
Hongze Cheng 已提交
1264
// APIs
H
Hongze Cheng 已提交
1265
int32_t tsdbSnapWriterOpen(STsdb* pTsdb, int64_t sver, int64_t ever, STsdbSnapWriter** ppWriter) {
H
Hongze Cheng 已提交
1266
  int32_t          code = 0;
H
Hongze Cheng 已提交
1267
  int32_t          lino = 0;
H
Hongze Cheng 已提交
1268 1269 1270 1271 1272 1273
  STsdbSnapWriter* pWriter = NULL;

  // alloc
  pWriter = (STsdbSnapWriter*)taosMemoryCalloc(1, sizeof(*pWriter));
  if (pWriter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1274
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1275 1276 1277 1278 1279
  }
  pWriter->pTsdb = pTsdb;
  pWriter->sver = sver;
  pWriter->ever = ever;

H
Hongze Cheng 已提交
1280
  code = tsdbFSCopy(pTsdb, &pWriter->fs);
H
Hongze Cheng 已提交
1281
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1282

H
Hongze Cheng 已提交
1283
  // config
H
Hongze Cheng 已提交
1284 1285 1286 1287 1288
  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 已提交
1289 1290
  pWriter->commitID = pTsdb->pVnode->state.commitID;

H
Hongze Cheng 已提交
1291
  // SNAP_DATA_TSDB
H
Hongze Cheng 已提交
1292
  code = tBlockDataCreate(&pWriter->bData);
H
Hongze Cheng 已提交
1293
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1294

H
Hongze Cheng 已提交
1295 1296 1297 1298 1299
  pWriter->fid = INT32_MIN;
  pWriter->id = (TABLEID){0};
  // Reader
  pWriter->dReader.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pWriter->dReader.aBlockIdx == NULL) {
H
Hongze Cheng 已提交
1300
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1301
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1302
  }
H
Hongze Cheng 已提交
1303
  code = tBlockDataCreate(&pWriter->dReader.bData);
H
Hongze Cheng 已提交
1304
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1305

H
Hongze Cheng 已提交
1306 1307 1308
  // Writer
  pWriter->dWriter.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pWriter->dWriter.aBlockIdx == NULL) {
H
Hongze Cheng 已提交
1309
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1310
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1311
  }
H
Hongze Cheng 已提交
1312 1313
  pWriter->dWriter.aSttBlk = taosArrayInit(0, sizeof(SSttBlk));
  if (pWriter->dWriter.aSttBlk == NULL) {
H
Hongze Cheng 已提交
1314
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1315
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1316
  }
H
Hongze Cheng 已提交
1317
  code = tBlockDataCreate(&pWriter->dWriter.bData);
H
Hongze Cheng 已提交
1318
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1319
  code = tBlockDataCreate(&pWriter->dWriter.sData);
H
Hongze Cheng 已提交
1320
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1321

H
Hongze Cheng 已提交
1322
  // SNAP_DATA_DEL
H
Hongze Cheng 已提交
1323 1324 1325
  pWriter->aDelIdxR = taosArrayInit(0, sizeof(SDelIdx));
  if (pWriter->aDelIdxR == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1326
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1327 1328 1329 1330
  }
  pWriter->aDelData = taosArrayInit(0, sizeof(SDelData));
  if (pWriter->aDelData == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1331
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1332 1333 1334 1335
  }
  pWriter->aDelIdxW = taosArrayInit(0, sizeof(SDelIdx));
  if (pWriter->aDelIdxW == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1336
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1337
  }
H
Hongze Cheng 已提交
1338

H
Hongze Cheng 已提交
1339 1340
_exit:
  if (code) {
S
Shengliang Guan 已提交
1341
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1342 1343 1344 1345 1346 1347
    *ppWriter = NULL;

    if (pWriter) {
      if (pWriter->aDelIdxW) taosArrayDestroy(pWriter->aDelIdxW);
      if (pWriter->aDelData) taosArrayDestroy(pWriter->aDelData);
      if (pWriter->aDelIdxR) taosArrayDestroy(pWriter->aDelIdxR);
H
Hongze Cheng 已提交
1348 1349
      tBlockDataDestroy(&pWriter->dWriter.sData);
      tBlockDataDestroy(&pWriter->dWriter.bData);
H
Hongze Cheng 已提交
1350 1351
      if (pWriter->dWriter.aSttBlk) taosArrayDestroy(pWriter->dWriter.aSttBlk);
      if (pWriter->dWriter.aBlockIdx) taosArrayDestroy(pWriter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
1352
      tBlockDataDestroy(&pWriter->dReader.bData);
H
Hongze Cheng 已提交
1353
      if (pWriter->dReader.aBlockIdx) taosArrayDestroy(pWriter->dReader.aBlockIdx);
H
Hongze Cheng 已提交
1354
      tBlockDataDestroy(&pWriter->bData);
H
Hongze Cheng 已提交
1355 1356 1357 1358 1359 1360 1361
      tsdbFSDestroy(&pWriter->fs);
      taosMemoryFree(pWriter);
    }
  } else {
    tsdbDebug("vgId:%d, tsdb snapshot writer open for %s succeed", TD_VID(pTsdb->pVnode), pTsdb->path);
    *ppWriter = pWriter;
  }
H
Hongze Cheng 已提交
1362 1363 1364
  return code;
}

H
Hongze Cheng 已提交
1365
int32_t tsdbSnapWriterClose(STsdbSnapWriter** ppWriter, int8_t rollback) {
H
Hongze Cheng 已提交
1366
  int32_t          code = 0;
H
Hongze Cheng 已提交
1367
  STsdbSnapWriter* pWriter = *ppWriter;
H
Hongze Cheng 已提交
1368
  STsdb*           pTsdb = pWriter->pTsdb;
H
Hongze Cheng 已提交
1369 1370

  if (rollback) {
H
Hongze Cheng 已提交
1371 1372 1373
    ASSERT(0);
    // code = tsdbFSRollback(pWriter->pTsdb->pFS);
    // if (code) goto _err;
H
Hongze Cheng 已提交
1374
  } else {
H
Hongze Cheng 已提交
1375 1376 1377 1378
    if (pWriter->dWriter.pWriter) {
      code = tsdbSnapWriteCloseFile(pWriter);
      if (code) goto _err;
    }
H
Hongze Cheng 已提交
1379 1380 1381 1382

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

H
Hongze Cheng 已提交
1383
    code = tsdbFSPrepareCommit(pWriter->pTsdb, &pWriter->fs);
H
Hongze Cheng 已提交
1384 1385
    if (code) goto _err;

H
Hongze Cheng 已提交
1386 1387 1388
    // lock
    taosThreadRwlockWrlock(&pTsdb->rwLock);

H
Hongze Cheng 已提交
1389
    code = tsdbFSCommit(pWriter->pTsdb);
H
Hongze Cheng 已提交
1390 1391 1392 1393 1394 1395 1396
    if (code) {
      taosThreadRwlockUnlock(&pTsdb->rwLock);
      goto _err;
    }

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

H
Hongze Cheng 已提交
1399 1400 1401 1402 1403 1404 1405 1406
  // SNAP_DATA_DEL
  taosArrayDestroy(pWriter->aDelIdxW);
  taosArrayDestroy(pWriter->aDelData);
  taosArrayDestroy(pWriter->aDelIdxR);

  // SNAP_DATA_TSDB

  // Writer
H
Hongze Cheng 已提交
1407 1408
  tBlockDataDestroy(&pWriter->dWriter.sData);
  tBlockDataDestroy(&pWriter->dWriter.bData);
H
Hongze Cheng 已提交
1409 1410 1411 1412 1413
  taosArrayDestroy(pWriter->dWriter.aSttBlk);
  tMapDataClear(&pWriter->dWriter.mDataBlk);
  taosArrayDestroy(pWriter->dWriter.aBlockIdx);

  // Reader
H
Hongze Cheng 已提交
1414
  tBlockDataDestroy(&pWriter->dReader.bData);
H
Hongze Cheng 已提交
1415 1416 1417
  tMapDataClear(&pWriter->dReader.mDataBlk);
  taosArrayDestroy(pWriter->dReader.aBlockIdx);

H
Hongze Cheng 已提交
1418
  tBlockDataDestroy(&pWriter->bData);
H
Hongze Cheng 已提交
1419
  tDestroyTSchema(pWriter->skmTable.pTSchema);
H
Hongze Cheng 已提交
1420

H
Hongze Cheng 已提交
1421 1422 1423
  for (int32_t iBuf = 0; iBuf < sizeof(pWriter->aBuf) / sizeof(uint8_t*); iBuf++) {
    tFree(pWriter->aBuf[iBuf]);
  }
S
Shengliang Guan 已提交
1424
  tsdbInfo("vgId:%d, vnode snapshot tsdb writer close for %s", TD_VID(pWriter->pTsdb->pVnode), pWriter->pTsdb->path);
H
Hongze Cheng 已提交
1425 1426 1427 1428 1429
  taosMemoryFree(pWriter);
  *ppWriter = NULL;
  return code;

_err:
S
Shengliang Guan 已提交
1430
  tsdbError("vgId:%d, vnode snapshot tsdb writer close for %s failed since %s", TD_VID(pWriter->pTsdb->pVnode),
C
Cary Xu 已提交
1431 1432 1433
            pWriter->pTsdb->path, tstrerror(code));
  taosMemoryFree(pWriter);
  *ppWriter = NULL;
H
Hongze Cheng 已提交
1434 1435 1436 1437
  return code;
}

int32_t tsdbSnapWrite(STsdbSnapWriter* pWriter, uint8_t* pData, uint32_t nData) {
H
Hongze Cheng 已提交
1438
  int32_t       code = 0;
H
Hongze Cheng 已提交
1439
  SSnapDataHdr* pHdr = (SSnapDataHdr*)pData;
H
Hongze Cheng 已提交
1440 1441

  // ts data
C
Cary Xu 已提交
1442
  if (pHdr->type == SNAP_DATA_TSDB) {
H
Hongze Cheng 已提交
1443
    code = tsdbSnapWriteData(pWriter, pData, nData);
H
Hongze Cheng 已提交
1444
    if (code) goto _err;
H
Hongze Cheng 已提交
1445 1446

    goto _exit;
H
Hongze Cheng 已提交
1447
  } else {
H
Hongze Cheng 已提交
1448
    if (pWriter->dWriter.pWriter) {
H
Hongze Cheng 已提交
1449
      code = tsdbSnapWriteCloseFile(pWriter);
H
Hongze Cheng 已提交
1450 1451
      if (code) goto _err;
    }
H
Hongze Cheng 已提交
1452 1453 1454
  }

  // del data
C
Cary Xu 已提交
1455
  if (pHdr->type == SNAP_DATA_DEL) {
H
Hongze Cheng 已提交
1456
    code = tsdbSnapWriteDel(pWriter, pData, nData);
H
Hongze Cheng 已提交
1457 1458 1459
    if (code) goto _err;
  }

H
Hongze Cheng 已提交
1460
_exit:
C
Cary Xu 已提交
1461
  tsdbDebug("vgId:%d, tsdb snapshot write for %s succeed", TD_VID(pWriter->pTsdb->pVnode), pWriter->pTsdb->path);
H
Hongze Cheng 已提交
1462 1463 1464
  return code;

_err:
C
Cary Xu 已提交
1465
  tsdbError("vgId:%d, tsdb snapshot write for %s failed since %s", TD_VID(pWriter->pTsdb->pVnode), pWriter->pTsdb->path,
C
Cary Xu 已提交
1466
            tstrerror(code));
H
Hongze Cheng 已提交
1467 1468
  return code;
}