tsdbSnapshot.c 43.5 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
  int32_t code = 0;
H
add log  
Hongze Cheng 已提交
79
  int32_t lino = 0;
H
Hongze Cheng 已提交
80

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

H
Hongze Cheng 已提交
85 86
  pReader->fid = pSet->fid;
  code = tsdbDataFReaderOpen(&pReader->pDataFReader, pReader->pTsdb, pSet);
H
add log  
Hongze Cheng 已提交
87
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
88 89

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

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

  code = tsdbReadBlockIdx(pReader->pDataFReader, pIter->aBlockIdx);
H
add log  
Hongze Cheng 已提交
97
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
98 99 100 101

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

H
Hongze Cheng 已提交
102
    code = tsdbReadDataBlk(pReader->pDataFReader, pIter->pBlockIdx, &pIter->mBlock);
H
add log  
Hongze Cheng 已提交
103
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
104 105 106 107 108 109

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

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

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

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

H
Hongze Cheng 已提交
120 121 122 123 124 125
        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 已提交
126
      }
H
Hongze Cheng 已提交
127
    }
H
Hongze Cheng 已提交
128

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

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

H
Hongze Cheng 已提交
136 137 138 139 140
  // .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 已提交
141

H
Hongze Cheng 已提交
142
    code = tsdbReadSttBlk(pReader->pDataFReader, iStt, pIter->aSttBlk);
H
add log  
Hongze Cheng 已提交
143
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
144 145 146 147 148 149 150

    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 已提交
151
      code = tsdbReadSttBlockEx(pReader->pDataFReader, iStt, pSttBlk, &pIter->bData);
H
add log  
Hongze Cheng 已提交
152
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
153 154 155 156 157 158

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

        if (rowVer >= pReader->sver && rowVer <= pReader->ever) {
          pIter->rInfo.suid = pIter->bData.suid;
H
Hongze Cheng 已提交
159
          pIter->rInfo.uid = pIter->bData.uid ? pIter->bData.uid : pIter->bData.aUid[pIter->iRow];
H
Hongze Cheng 已提交
160 161 162 163
          pIter->rInfo.row = tsdbRowFromBlockData(&pIter->bData, pIter->iRow);
          goto _add_iter;
        }
      }
H
Hongze Cheng 已提交
164
    }
H
Hongze Cheng 已提交
165

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

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

H
add log  
Hongze Cheng 已提交
173 174 175 176 177 178 179
_exit:
  if (code) {
    tsdbError("vgId:%d, %s failed since %s", TD_VID(pReader->pTsdb->pVnode), __func__, tstrerror(code));
  } else {
    tsdbInfo("vgId:%d, %s done, path:%s, fid:%d", TD_VID(pReader->pTsdb->pVnode), __func__, pReader->pTsdb->path,
             pReader->fid);
  }
H
Hongze Cheng 已提交
180 181 182 183 184 185 186
  return code;
}

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

  if (pReader->pIter) {
H
Hongze Cheng 已提交
187
    SFDataIter* pIter = NULL;
H
Hongze Cheng 已提交
188 189
    while (true) {
    _find_row:
H
Hongze Cheng 已提交
190
      pIter = pReader->pIter;
H
Hongze Cheng 已提交
191 192 193 194
      for (pIter->iRow++; pIter->iRow < pIter->bData.nRow; pIter->iRow++) {
        int64_t rowVer = pIter->bData.aVersion[pIter->iRow];

        if (rowVer >= pReader->sver && rowVer <= pReader->ever) {
H
Hongze Cheng 已提交
195
          pIter->rInfo.suid = pIter->bData.suid;
H
Hongze Cheng 已提交
196 197 198
          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
        pReader->pIter = NULL;
H
Hongze Cheng 已提交
227
        break;
H
Hongze Cheng 已提交
228 229 230
      } 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 已提交
231

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

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

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

        pReader->pIter = NULL;
H
Hongze Cheng 已提交
242
        break;
H
Hongze Cheng 已提交
243
      } else {
244
        ASSERT(0);
H
Hongze Cheng 已提交
245 246 247 248 249 250 251 252 253 254 255
      }
    }

  _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 {
256
        ASSERT(c);
H
Hongze Cheng 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
      }
    }
  }

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

  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287
static SRowInfo* tsdbSnapGetRow(STsdbSnapReader* pReader) {
  if (pReader->pIter) {
    return &pReader->pIter->rInfo;
  } else {
    tsdbSnapNextRow(pReader);

    if (pReader->pIter) {
      return &pReader->pIter->rInfo;
    } else {
      return NULL;
    }
  }
}

H
Hongze Cheng 已提交
288 289 290
static int32_t tsdbSnapCmprData(STsdbSnapReader* pReader, uint8_t** ppData) {
  int32_t code = 0;

291
  ASSERT(pReader->bData.nRow);
H
Hongze Cheng 已提交
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

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

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

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

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

_exit:
  return code;
}

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

  STsdb* pTsdb = pReader->pTsdb;
H
Hongze Cheng 已提交
326 327 328 329

  while (true) {
    if (pReader->pDataFReader == NULL) {
      code = tsdbSnapReadOpenFile(pReader);
H
add log  
Hongze Cheng 已提交
330
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344
    }

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

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

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

    code = tsdbUpdateTableSchema(pTsdb->pVnode->pMeta, id.suid, id.uid, &pReader->skmTable);
H
add log  
Hongze Cheng 已提交
345
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
346

H
Hongze Cheng 已提交
347
    code = tBlockDataInit(pBlockData, &id, pReader->skmTable.pTSchema, NULL, 0);
H
add log  
Hongze Cheng 已提交
348
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
349 350 351

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

      code = tsdbSnapNextRow(pReader);
H
add log  
Hongze Cheng 已提交
355
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
356 357 358

      pRowInfo = tsdbSnapGetRow(pReader);
      if (pRowInfo == NULL) {
H
Hongze Cheng 已提交
359 360
        tsdbDataFReaderClose(&pReader->pDataFReader);
        break;
H
Hongze Cheng 已提交
361
      }
H
Hongze Cheng 已提交
362 363

      if (pBlockData->nRow >= 4096) break;
H
Hongze Cheng 已提交
364
    }
H
Hongze Cheng 已提交
365 366

    code = tsdbSnapCmprData(pReader, ppData);
H
add log  
Hongze Cheng 已提交
367
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
368 369

    break;
H
Hongze Cheng 已提交
370 371
  }

H
add log  
Hongze Cheng 已提交
372 373 374 375
_exit:
  if (code) {
    tsdbError("vgId:%d, %s failed since %s, path:%s", TD_VID(pTsdb->pVnode), __func__, tstrerror(code), pTsdb->path);
  }
H
Hongze Cheng 已提交
376 377 378 379
  return code;
}

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

H
Hongze Cheng 已提交
383
  STsdb*    pTsdb = pReader->pTsdb;
H
Hongze Cheng 已提交
384
  SDelFile* pDelFile = pReader->fs.pDelFile;
H
Hongze Cheng 已提交
385 386 387 388 389 390 391

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

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

H
Hongze Cheng 已提交
395
    // read index
H
Hongze Cheng 已提交
396
    code = tsdbReadDelIdx(pReader->pDelFReader, pReader->aDelIdx);
H
add log  
Hongze Cheng 已提交
397
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
398 399 400 401

    pReader->iDelIdx = 0;
  }

H
Hongze Cheng 已提交
402 403 404 405 406 407
  while (true) {
    if (pReader->iDelIdx >= taosArrayGetSize(pReader->aDelIdx)) {
      tsdbDelFReaderClose(&pReader->pDelFReader);
      break;
    }

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

H
Hongze Cheng 已提交
410 411
    pReader->iDelIdx++;

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

H
Hongze Cheng 已提交
415
    int32_t size = 0;
H
Hongze Cheng 已提交
416 417 418 419
    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 已提交
420
        size += tPutDelData(NULL, pDelData);
H
Hongze Cheng 已提交
421 422
      }
    }
H
Hongze Cheng 已提交
423
    if (size == 0) continue;
H
Hongze Cheng 已提交
424

H
Hongze Cheng 已提交
425 426 427 428 429
    // org data
    size = sizeof(TABLEID) + size;
    *ppData = taosMemoryMalloc(sizeof(SSnapDataHdr) + size);
    if (*ppData == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
H
add log  
Hongze Cheng 已提交
430
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
431
    }
H
Hongze Cheng 已提交
432

H
Hongze Cheng 已提交
433
    SSnapDataHdr* pHdr = (SSnapDataHdr*)(*ppData);
C
Cary Xu 已提交
434
    pHdr->type = SNAP_DATA_DEL;
H
Hongze Cheng 已提交
435
    pHdr->size = size;
H
Hongze Cheng 已提交
436

H
Hongze Cheng 已提交
437 438 439 440 441 442
    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 已提交
443

H
Hongze Cheng 已提交
444 445
      if (pDelData->version < pReader->sver) continue;
      if (pDelData->version > pReader->ever) continue;
H
Hongze Cheng 已提交
446

H
Hongze Cheng 已提交
447
      n += tPutDelData((*ppData) + n, pDelData);
H
Hongze Cheng 已提交
448 449
    }

H
Hongze Cheng 已提交
450
    tsdbInfo("vgId:%d, vnode snapshot tsdb read del data for %s, suid:%" PRId64 " uid:%" PRId64 " size:%d",
C
Cary Xu 已提交
451
             TD_VID(pTsdb->pVnode), pTsdb->path, pDelIdx->suid, pDelIdx->uid, size);
H
Hongze Cheng 已提交
452 453 454

    break;
  }
H
Hongze Cheng 已提交
455 456

_exit:
H
add log  
Hongze Cheng 已提交
457 458 459
  if (code) {
    tsdbError("vgId:%d, %s failed since %s, path:%s", TD_VID(pTsdb->pVnode), __func__, tstrerror(code), pTsdb->path);
  }
H
Hongze Cheng 已提交
460 461
  return code;
}
H
more  
Hongze Cheng 已提交
462

C
Cary Xu 已提交
463
int32_t tsdbSnapReaderOpen(STsdb* pTsdb, int64_t sver, int64_t ever, int8_t type, STsdbSnapReader** ppReader) {
H
Hongze Cheng 已提交
464
  int32_t          code = 0;
H
Hongze Cheng 已提交
465
  int32_t          lino = 0;
H
Hongze Cheng 已提交
466
  STsdbSnapReader* pReader = NULL;
H
Hongze Cheng 已提交
467

H
more  
Hongze Cheng 已提交
468
  // alloc
H
Hongze Cheng 已提交
469
  pReader = (STsdbSnapReader*)taosMemoryCalloc(1, sizeof(*pReader));
H
more  
Hongze Cheng 已提交
470 471
  if (pReader == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
472
    TSDB_CHECK_CODE(code, lino, _exit);
H
more  
Hongze Cheng 已提交
473 474 475 476
  }
  pReader->pTsdb = pTsdb;
  pReader->sver = sver;
  pReader->ever = ever;
C
Cary Xu 已提交
477
  pReader->type = type;
H
more  
Hongze Cheng 已提交
478

H
Hongze Cheng 已提交
479 480 481
  code = taosThreadRwlockRdlock(&pTsdb->rwLock);
  if (code) {
    code = TAOS_SYSTEM_ERROR(code);
H
Hongze Cheng 已提交
482
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
483 484 485 486 487
  }

  code = tsdbFSRef(pTsdb, &pReader->fs);
  if (code) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
488
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
489 490 491 492 493
  }

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

H
Hongze Cheng 已提交
497
  // data
H
Hongze Cheng 已提交
498
  pReader->fid = INT32_MIN;
H
Hongze Cheng 已提交
499 500 501 502 503 504 505
  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 已提交
506
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
507 508 509 510 511
      }
    } else {
      pIter->aSttBlk = taosArrayInit(0, sizeof(SSttBlk));
      if (pIter->aSttBlk == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
512
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
513 514 515 516
      }
    }

    code = tBlockDataCreate(&pIter->bData);
H
Hongze Cheng 已提交
517
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
518
  }
H
Hongze Cheng 已提交
519 520

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

H
Hongze Cheng 已提交
523
  // del
H
Hongze Cheng 已提交
524 525 526
  pReader->aDelIdx = taosArrayInit(0, sizeof(SDelIdx));
  if (pReader->aDelIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
527
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
528 529 530 531
  }
  pReader->aDelData = taosArrayInit(0, sizeof(SDelData));
  if (pReader->aDelData == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
532
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
533 534
  }

H
Hongze Cheng 已提交
535 536
_exit:
  if (code) {
S
Shengliang Guan 已提交
537
    tsdbError("vgId:%d, %s failed at line %d since %s, TSDB path: %s", TD_VID(pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
538
              tstrerror(code), pTsdb->path);
H
Hongze Cheng 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551
    *ppReader = NULL;

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

H
Hongze Cheng 已提交
555
int32_t tsdbSnapReaderClose(STsdbSnapReader** ppReader) {
H
Hongze Cheng 已提交
556
  int32_t          code = 0;
H
Hongze Cheng 已提交
557 558
  STsdbSnapReader* pReader = *ppReader;

H
Hongze Cheng 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571
  // data
  if (pReader->pDataFReader) tsdbDataFReaderClose(&pReader->pDataFReader);
  for (int32_t iIter = 0; iIter < sizeof(pReader->aFDataIter) / sizeof(pReader->aFDataIter[0]); iIter++) {
    SFDataIter* pIter = &pReader->aFDataIter[iIter];

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

    tBlockDataDestroy(&pIter->bData, 1);
H
Hongze Cheng 已提交
572
  }
H
Hongze Cheng 已提交
573 574

  tBlockDataDestroy(&pReader->bData, 1);
575
  tDestroyTSchema(pReader->skmTable.pTSchema);
H
Hongze Cheng 已提交
576 577 578

  // del
  if (pReader->pDelFReader) tsdbDelFReaderClose(&pReader->pDelFReader);
H
Hongze Cheng 已提交
579 580 581
  taosArrayDestroy(pReader->aDelIdx);
  taosArrayDestroy(pReader->aDelData);

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

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

H
Hongze Cheng 已提交
586 587 588 589
  for (int32_t iBuf = 0; iBuf < sizeof(pReader->aBuf) / sizeof(pReader->aBuf[0]); iBuf++) {
    tFree(pReader->aBuf[iBuf]);
  }

H
Hongze Cheng 已提交
590 591
  taosMemoryFree(pReader);
  *ppReader = NULL;
H
Hongze Cheng 已提交
592 593 594 595
  return code;
}

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

H
Hongze Cheng 已提交
599 600
  *ppData = NULL;

H
Hongze Cheng 已提交
601
  // read data file
H
Hongze Cheng 已提交
602 603
  if (!pReader->dataDone) {
    code = tsdbSnapReadData(pReader, ppData);
H
add log  
Hongze Cheng 已提交
604 605 606
    TSDB_CHECK_CODE(code, lino, _exit);
    if (*ppData) {
      goto _exit;
H
Hongze Cheng 已提交
607
    } else {
H
add log  
Hongze Cheng 已提交
608
      pReader->dataDone = 1;
H
Hongze Cheng 已提交
609 610
    }
  }
H
Hongze Cheng 已提交
611 612

  // read del file
H
Hongze Cheng 已提交
613 614
  if (!pReader->delDone) {
    code = tsdbSnapReadDel(pReader, ppData);
H
add log  
Hongze Cheng 已提交
615 616 617
    TSDB_CHECK_CODE(code, lino, _exit);
    if (*ppData) {
      goto _exit;
H
Hongze Cheng 已提交
618
    } else {
H
add log  
Hongze Cheng 已提交
619
      pReader->delDone = 1;
H
Hongze Cheng 已提交
620 621
    }
  }
H
Hongze Cheng 已提交
622

H
Hongze Cheng 已提交
623
_exit:
H
add log  
Hongze Cheng 已提交
624 625 626 627 628 629
  if (code) {
    tsdbError("vgId:%d, %s failed since %s, path:%s", TD_VID(pReader->pTsdb->pVnode), __func__, tstrerror(code),
              pReader->pTsdb->path);
  } else {
    tsdbDebug("vgId:%d, %s done, path:%s", TD_VID(pReader->pTsdb->pVnode), __func__, pReader->pTsdb->path);
  }
H
more  
Hongze Cheng 已提交
630 631 632
  return code;
}

H
Hongze Cheng 已提交
633
// STsdbSnapWriter ========================================
H
Hongze Cheng 已提交
634 635 636 637
struct STsdbSnapWriter {
  STsdb*  pTsdb;
  int64_t sver;
  int64_t ever;
H
Hongze Cheng 已提交
638
  STsdbFS fs;
H
Hongze Cheng 已提交
639

H
Hongze Cheng 已提交
640
  // config
H
Hongze Cheng 已提交
641 642 643 644 645 646
  int32_t  minutes;
  int8_t   precision;
  int32_t  minRow;
  int32_t  maxRow;
  int8_t   cmprAlg;
  int64_t  commitID;
H
Hongze Cheng 已提交
647
  uint8_t* aBuf[5];
H
Hongze Cheng 已提交
648

H
Hongze Cheng 已提交
649
  // for data file
H
Hongze Cheng 已提交
650
  SBlockData bData;
H
Hongze Cheng 已提交
651 652 653
  int32_t    fid;
  TABLEID    id;
  SSkmInfo   skmTable;
H
Hongze Cheng 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
  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 已提交
672

H
Hongze Cheng 已提交
673
  // for del file
H
Hongze Cheng 已提交
674
  SDelFReader* pDelFReader;
H
Hongze Cheng 已提交
675
  SDelFWriter* pDelFWriter;
H
Hongze Cheng 已提交
676
  int32_t      iDelIdx;
H
Hongze Cheng 已提交
677
  SArray*      aDelIdxR;
H
Hongze Cheng 已提交
678
  SArray*      aDelData;
H
Hongze Cheng 已提交
679
  SArray*      aDelIdxW;
H
Hongze Cheng 已提交
680 681
};

H
Hongze Cheng 已提交
682
// SNAP_DATA_TSDB
H
Hongze Cheng 已提交
683 684 685
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 已提交
686
static int32_t tsdbSnapNextTableData(STsdbSnapWriter* pWriter) {
H
Hongze Cheng 已提交
687
  int32_t code = 0;
H
Hongze Cheng 已提交
688

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

H
Hongze Cheng 已提交
691 692 693 694 695 696
  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 已提交
697
    pWriter->dReader.iBlockIdx++;
H
Hongze Cheng 已提交
698 699
  } else {
    pWriter->dReader.pBlockIdx = NULL;
H
Hongze Cheng 已提交
700
    tMapDataReset(&pWriter->dReader.mDataBlk);
H
Hongze Cheng 已提交
701
  }
H
Hongze Cheng 已提交
702 703 704
  pWriter->dReader.iDataBlk = 0;  // point to the next one
  tBlockDataReset(&pWriter->dReader.bData);
  pWriter->dReader.iRow = 0;
H
Hongze Cheng 已提交
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727

_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 已提交
728 729
  }

H
Hongze Cheng 已提交
730 731 732 733
_exit:
  return code;
}

H
Hongze Cheng 已提交
734 735
static int32_t tsdbSnapWriteTableDataStart(STsdbSnapWriter* pWriter, TABLEID* pId) {
  int32_t code = 0;
H
Hongze Cheng 已提交
736 737 738 739 740 741 742 743 744 745 746

  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 已提交
747
  code = tBlockDataInit(&pWriter->dWriter.bData, pId, pWriter->skmTable.pTSchema, NULL, 0);
H
Hongze Cheng 已提交
748 749 750 751 752
  if (code) goto _err;

  return code;

_err:
S
Shengliang Guan 已提交
753
  tsdbError("vgId:%d, %s failed since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, tstrerror(code));
H
Hongze Cheng 已提交
754 755 756 757 758 759 760 761
  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 已提交
762 763 764
  int32_t c = 1;
  if (pWriter->dReader.pBlockIdx) {
    c = tTABLEIDCmprFn(pWriter->dReader.pBlockIdx, &pWriter->id);
765
    ASSERT(c >= 0);
H
Hongze Cheng 已提交
766 767 768 769 770
  }

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

H
Hongze Cheng 已提交
771 772
    for (; pWriter->dReader.iRow < pWriter->dReader.bData.nRow; pWriter->dReader.iRow++) {
      TSDBROW row = tsdbRowFromBlockData(&pWriter->dReader.bData, pWriter->dReader.iRow);
H
Hongze Cheng 已提交
773 774

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

H
Hongze Cheng 已提交
777 778
      if (pBData->nRow >= pWriter->maxRow) {
        code = tsdbWriteDataBlock(pWriter->dWriter.pWriter, pBData, &pWriter->dWriter.mDataBlk, pWriter->cmprAlg);
H
Hongze Cheng 已提交
779 780 781 782
        if (code) goto _err;
      }
    }

H
Hongze Cheng 已提交
783
    code = tsdbWriteDataBlock(pWriter->dWriter.pWriter, pBData, &pWriter->dWriter.mDataBlk, pWriter->cmprAlg);
H
Hongze Cheng 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
    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 已提交
808 809 810
  pWriter->id.suid = 0;
  pWriter->id.uid = 0;

H
Hongze Cheng 已提交
811 812 813 814 815 816
  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
817 818 819 820
static int32_t tsdbSnapWriteOpenFile(STsdbSnapWriter* pWriter, int32_t fid) {
  int32_t code = 0;
  STsdb*  pTsdb = pWriter->pTsdb;

821
  ASSERT(pWriter->dWriter.pWriter == NULL);
H
Hongze Cheng 已提交
822 823

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

H
Hongze Cheng 已提交
827
  // Reader
H
Hongze Cheng 已提交
828 829 830 831 832 833 834
  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 {
835
    ASSERT(pWriter->dReader.pReader == NULL);
H
Hongze Cheng 已提交
836
    taosArrayClear(pWriter->dReader.aBlockIdx);
H
Hongze Cheng 已提交
837
  }
H
Hongze Cheng 已提交
838 839 840
  pWriter->dReader.iBlockIdx = 0;  // point to the next one
  code = tsdbSnapNextTableData(pWriter);
  if (code) goto _err;
H
Hongze Cheng 已提交
841

H
Hongze Cheng 已提交
842
  // Writer
H
Hongze Cheng 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
  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 已提交
864

H
Hongze Cheng 已提交
865
  code = tsdbDataFWriterOpen(&pWriter->dWriter.pWriter, pWriter->pTsdb, &wSet);
H
Hongze Cheng 已提交
866
  if (code) goto _err;
H
Hongze Cheng 已提交
867 868 869
  taosArrayClear(pWriter->dWriter.aBlockIdx);
  tMapDataReset(&pWriter->dWriter.mDataBlk);
  taosArrayClear(pWriter->dWriter.aSttBlk);
H
Hongze Cheng 已提交
870 871
  tBlockDataReset(&pWriter->dWriter.bData);
  tBlockDataReset(&pWriter->dWriter.sData);
H
Hongze Cheng 已提交
872 873 874 875 876 877 878 879 880 881

  return code;

_err:
  return code;
}

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

882
  ASSERT(pWriter->dWriter.pWriter);
H
Hongze Cheng 已提交
883

H
Hongze Cheng 已提交
884 885
  code = tsdbSnapWriteTableDataEnd(pWriter);
  if (code) goto _err;
H
Hongze Cheng 已提交
886

H
Hongze Cheng 已提交
887 888 889
  // copy remain table data
  TABLEID id = {.suid = INT64_MAX, .uid = INT64_MAX};
  code = tsdbSnapWriteCopyData(pWriter, &id);
H
Hongze Cheng 已提交
890
  if (code) goto _err;
H
Hongze Cheng 已提交
891

H
Hongze Cheng 已提交
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
  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 已提交
915 916 917 918
  }

_exit:
  return code;
H
Hongze Cheng 已提交
919 920 921

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

H
Hongze Cheng 已提交
924
static int32_t tsdbSnapWriteToDataFile(STsdbSnapWriter* pWriter, int32_t iRow, int8_t* done) {
H
Hongze Cheng 已提交
925 926
  int32_t code = 0;

H
Hongze Cheng 已提交
927 928 929
  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 已提交
930 931
  TSDBKEY     key = TSDBROW_KEY(&row);

H
Hongze Cheng 已提交
932 933 934 935
  *done = 0;
  while (pWriter->dReader.iRow < pWriter->dReader.bData.nRow ||
         pWriter->dReader.iDataBlk < pWriter->dReader.mDataBlk.nItem) {
    // Merge row by row
H
Hongze Cheng 已提交
936 937 938
    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 已提交
939

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

H
Hongze Cheng 已提交
942 943 944 945 946 947 948 949
      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 {
950
        ASSERT(0);
H
Hongze Cheng 已提交
951
      }
H
Hongze Cheng 已提交
952

H
Hongze Cheng 已提交
953
      if (pWriter->dWriter.bData.nRow >= pWriter->maxRow) {
H
Hongze Cheng 已提交
954 955 956
        code = tsdbWriteDataBlock(pWriter->dWriter.pWriter, &pWriter->dWriter.bData, &pWriter->dWriter.mDataBlk,
                                  pWriter->cmprAlg);
        if (code) goto _err;
H
Hongze Cheng 已提交
957 958
      }

H
Hongze Cheng 已提交
959 960 961 962
      if (c < 0) {
        *done = 1;
        goto _exit;
      }
H
Hongze Cheng 已提交
963
    }
H
Hongze Cheng 已提交
964

H
Hongze Cheng 已提交
965
    // Merge row by block
H
Hongze Cheng 已提交
966
    SDataBlk tDataBlk = {.minKey = key, .maxKey = key};
H
Hongze Cheng 已提交
967
    for (; pWriter->dReader.iDataBlk < pWriter->dReader.mDataBlk.nItem; pWriter->dReader.iDataBlk++) {
H
Hongze Cheng 已提交
968
      SDataBlk dataBlk;
H
Hongze Cheng 已提交
969
      tMapDataGetItemByIdx(&pWriter->dReader.mDataBlk, pWriter->dReader.iDataBlk, &dataBlk, tGetDataBlk);
H
Hongze Cheng 已提交
970 971 972

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

H
Hongze Cheng 已提交
977 978
        code = tMapDataPutItem(&pWriter->dWriter.mDataBlk, &dataBlk, tPutDataBlk);
        if (code) goto _err;
H
Hongze Cheng 已提交
979
      } else if (c > 0) {
H
Hongze Cheng 已提交
980 981 982 983
        code = tBlockDataAppendRow(&pWriter->dWriter.bData, &row, NULL, id.uid);
        if (code) goto _err;

        if (pWriter->dWriter.bData.nRow >= pWriter->maxRow) {
H
Hongze Cheng 已提交
984 985 986
          code = tsdbWriteDataBlock(pWriter->dWriter.pWriter, &pWriter->dWriter.bData, &pWriter->dWriter.mDataBlk,
                                    pWriter->cmprAlg);
          if (code) goto _err;
H
Hongze Cheng 已提交
987
        }
H
Hongze Cheng 已提交
988 989 990

        *done = 1;
        goto _exit;
H
Hongze Cheng 已提交
991 992 993
      } else {
        code = tsdbReadDataBlockEx(pWriter->dReader.pReader, &dataBlk, &pWriter->dReader.bData);
        if (code) goto _err;
H
Hongze Cheng 已提交
994
        pWriter->dReader.iRow = 0;
H
Hongze Cheng 已提交
995

H
Hongze Cheng 已提交
996 997
        pWriter->dReader.iDataBlk++;
        break;
H
Hongze Cheng 已提交
998
      }
H
Hongze Cheng 已提交
999
    }
H
Hongze Cheng 已提交
1000
  }
H
Hongze Cheng 已提交
1001

H
Hongze Cheng 已提交
1002 1003 1004 1005
_exit:
  return code;

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

H
Hongze Cheng 已提交
1010
static int32_t tsdbSnapWriteToSttFile(STsdbSnapWriter* pWriter, int32_t iRow) {
H
Hongze Cheng 已提交
1011 1012
  int32_t code = 0;

H
Hongze Cheng 已提交
1013 1014 1015 1016
  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 已提交
1017

H
Hongze Cheng 已提交
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
  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 已提交
1032 1033
    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 已提交
1034 1035 1036 1037
    if (code) goto _err;
  }

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

H
Hongze Cheng 已提交
1040 1041
  if (pBData->nRow >= pWriter->maxRow) {
    code = tsdbWriteSttBlock(pWriter->dWriter.pWriter, pBData, pWriter->dWriter.aSttBlk, pWriter->cmprAlg);
H
Hongze Cheng 已提交
1042
    if (code) goto _err;
H
Hongze Cheng 已提交
1043 1044
  }

H
Hongze Cheng 已提交
1045 1046 1047 1048 1049 1050 1051
_exit:
  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
1052 1053 1054 1055 1056 1057 1058
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 已提交
1059
  if (tTABLEIDCmprFn(&pWriter->id, &id) != 0) {
H
Hongze Cheng 已提交
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
    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 已提交
1072 1073 1074 1075
  if (pWriter->dReader.pBlockIdx && tTABLEIDCmprFn(pWriter->dReader.pBlockIdx, &id) == 0) {
    code = tsdbSnapWriteToDataFile(pWriter, iRow, &done);
    if (code) goto _err;
  }
H
Hongze Cheng 已提交
1076 1077 1078

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

H
Hongze Cheng 已提交
1083 1084 1085 1086
_exit:
  return code;

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

H
Hongze Cheng 已提交
1091 1092 1093 1094 1095 1096 1097 1098
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 已提交
1099
  if (code) goto _err;
H
Hongze Cheng 已提交
1100

1101
  ASSERT(pBlockData->nRow > 0);
H
Hongze Cheng 已提交
1102

H
Hongze Cheng 已提交
1103 1104 1105 1106
  // 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 已提交
1107

H
Hongze Cheng 已提交
1108
    if (pWriter->dWriter.pWriter == NULL || pWriter->fid != fid) {
H
Hongze Cheng 已提交
1109
      if (pWriter->dWriter.pWriter) {
S
Shengliang Guan 已提交
1110
        // ASSERT(fid > pWriter->fid);
H
Hongze Cheng 已提交
1111 1112 1113 1114 1115

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

H
Hongze Cheng 已提交
1116 1117 1118 1119 1120 1121 1122 1123
      code = tsdbSnapWriteOpenFile(pWriter, fid);
      if (code) goto _err;
    }

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

H
Hongze Cheng 已提交
1124 1125 1126
  return code;

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

H
Hongze Cheng 已提交
1132
// SNAP_DATA_DEL
H
Hongze Cheng 已提交
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
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 已提交
1162 1163
static int32_t tsdbSnapWriteDel(STsdbSnapWriter* pWriter, uint8_t* pData, uint32_t nData) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1164 1165
  STsdb*  pTsdb = pWriter->pTsdb;

H
Hongze Cheng 已提交
1166
  // Open del file if not opened yet
H
Hongze Cheng 已提交
1167
  if (pWriter->pDelFWriter == NULL) {
H
Hongze Cheng 已提交
1168
    SDelFile* pDelFile = pWriter->fs.pDelFile;
H
Hongze Cheng 已提交
1169 1170 1171

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

H
Hongze Cheng 已提交
1175
      code = tsdbReadDelIdx(pWriter->pDelFReader, pWriter->aDelIdxR);
H
Hongze Cheng 已提交
1176
      if (code) goto _err;
H
Hongze Cheng 已提交
1177 1178
    } else {
      taosArrayClear(pWriter->aDelIdxR);
H
Hongze Cheng 已提交
1179
    }
H
Hongze Cheng 已提交
1180
    pWriter->iDelIdx = 0;
H
Hongze Cheng 已提交
1181 1182

    // writer
H
Hongze Cheng 已提交
1183
    SDelFile delFile = {.commitID = pWriter->commitID};
H
Hongze Cheng 已提交
1184 1185
    code = tsdbDelFWriterOpen(&pWriter->pDelFWriter, &delFile, pTsdb);
    if (code) goto _err;
H
Hongze Cheng 已提交
1186
    taosArrayClear(pWriter->aDelIdxW);
H
Hongze Cheng 已提交
1187 1188
  }

H
Hongze Cheng 已提交
1189 1190
  SSnapDataHdr* pHdr = (SSnapDataHdr*)pData;
  TABLEID       id = *(TABLEID*)pHdr->data;
H
Hongze Cheng 已提交
1191

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

H
Hongze Cheng 已提交
1194 1195 1196
  // Move write data < id
  code = tsdbSnapMoveWriteDelData(pWriter, &id);
  if (code) goto _err;
H
Hongze Cheng 已提交
1197

H
Hongze Cheng 已提交
1198
  // Merge incoming data with current
H
Hongze Cheng 已提交
1199 1200 1201
  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 已提交
1202

H
Hongze Cheng 已提交
1203
    code = tsdbReadDelData(pWriter->pDelFReader, pDelIdx, pWriter->aDelData);
H
Hongze Cheng 已提交
1204 1205
    if (code) goto _err;

H
Hongze Cheng 已提交
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
    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 已提交
1218 1219 1220
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
1221
  }
H
Hongze Cheng 已提交
1222

H
Hongze Cheng 已提交
1223 1224 1225 1226 1227 1228 1229
  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 已提交
1230 1231 1232 1233 1234
  }

  return code;

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

H
Hongze Cheng 已提交
1240 1241
static int32_t tsdbSnapWriteDelEnd(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1242 1243
  STsdb*  pTsdb = pWriter->pTsdb;

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

H
Hongze Cheng 已提交
1246 1247 1248
  TABLEID id = {.suid = INT64_MAX, .uid = INT64_MAX};
  code = tsdbSnapMoveWriteDelData(pWriter, &id);
  if (code) goto _err;
H
Hongze Cheng 已提交
1249

H
Hongze Cheng 已提交
1250 1251 1252
  code = tsdbWriteDelIdx(pWriter->pDelFWriter, pWriter->aDelIdxW);
  if (code) goto _err;

H
Hongze Cheng 已提交
1253 1254 1255
  code = tsdbUpdateDelFileHdr(pWriter->pDelFWriter);
  if (code) goto _err;

H
Hongze Cheng 已提交
1256
  code = tsdbFSUpsertDelFile(&pWriter->fs, &pWriter->pDelFWriter->fDel);
H
Hongze Cheng 已提交
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
  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 已提交
1267
  tsdbInfo("vgId:%d, vnode snapshot tsdb write del for %s end", TD_VID(pTsdb->pVnode), pTsdb->path);
H
Hongze Cheng 已提交
1268 1269 1270
  return code;

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

H
Hongze Cheng 已提交
1276
// APIs
H
Hongze Cheng 已提交
1277
int32_t tsdbSnapWriterOpen(STsdb* pTsdb, int64_t sver, int64_t ever, STsdbSnapWriter** ppWriter) {
H
Hongze Cheng 已提交
1278
  int32_t          code = 0;
H
Hongze Cheng 已提交
1279
  int32_t          lino = 0;
H
Hongze Cheng 已提交
1280 1281 1282 1283 1284 1285
  STsdbSnapWriter* pWriter = NULL;

  // alloc
  pWriter = (STsdbSnapWriter*)taosMemoryCalloc(1, sizeof(*pWriter));
  if (pWriter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1286
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1287 1288 1289 1290 1291
  }
  pWriter->pTsdb = pTsdb;
  pWriter->sver = sver;
  pWriter->ever = ever;

H
Hongze Cheng 已提交
1292
  code = tsdbFSCopy(pTsdb, &pWriter->fs);
H
Hongze Cheng 已提交
1293
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1294

H
Hongze Cheng 已提交
1295
  // config
H
Hongze Cheng 已提交
1296 1297 1298 1299 1300
  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 已提交
1301 1302
  pWriter->commitID = pTsdb->pVnode->state.commitID;

H
Hongze Cheng 已提交
1303
  // SNAP_DATA_TSDB
H
Hongze Cheng 已提交
1304
  code = tBlockDataCreate(&pWriter->bData);
H
Hongze Cheng 已提交
1305
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1306

H
Hongze Cheng 已提交
1307 1308 1309 1310 1311
  pWriter->fid = INT32_MIN;
  pWriter->id = (TABLEID){0};
  // Reader
  pWriter->dReader.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pWriter->dReader.aBlockIdx == NULL) {
H
Hongze Cheng 已提交
1312
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1313
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1314
  }
H
Hongze Cheng 已提交
1315
  code = tBlockDataCreate(&pWriter->dReader.bData);
H
Hongze Cheng 已提交
1316
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1317

H
Hongze Cheng 已提交
1318 1319 1320
  // Writer
  pWriter->dWriter.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pWriter->dWriter.aBlockIdx == NULL) {
H
Hongze Cheng 已提交
1321
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1322
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1323
  }
H
Hongze Cheng 已提交
1324 1325
  pWriter->dWriter.aSttBlk = taosArrayInit(0, sizeof(SSttBlk));
  if (pWriter->dWriter.aSttBlk == NULL) {
H
Hongze Cheng 已提交
1326
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1327
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1328
  }
H
Hongze Cheng 已提交
1329
  code = tBlockDataCreate(&pWriter->dWriter.bData);
H
Hongze Cheng 已提交
1330
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1331
  code = tBlockDataCreate(&pWriter->dWriter.sData);
H
Hongze Cheng 已提交
1332
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1333

H
Hongze Cheng 已提交
1334
  // SNAP_DATA_DEL
H
Hongze Cheng 已提交
1335 1336 1337
  pWriter->aDelIdxR = taosArrayInit(0, sizeof(SDelIdx));
  if (pWriter->aDelIdxR == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1338
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1339 1340 1341 1342
  }
  pWriter->aDelData = taosArrayInit(0, sizeof(SDelData));
  if (pWriter->aDelData == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1343
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1344 1345 1346 1347
  }
  pWriter->aDelIdxW = taosArrayInit(0, sizeof(SDelIdx));
  if (pWriter->aDelIdxW == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1348
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1349
  }
H
Hongze Cheng 已提交
1350

H
Hongze Cheng 已提交
1351 1352
_exit:
  if (code) {
S
Shengliang Guan 已提交
1353
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
    *ppWriter = NULL;

    if (pWriter) {
      if (pWriter->aDelIdxW) taosArrayDestroy(pWriter->aDelIdxW);
      if (pWriter->aDelData) taosArrayDestroy(pWriter->aDelData);
      if (pWriter->aDelIdxR) taosArrayDestroy(pWriter->aDelIdxR);
      tBlockDataDestroy(&pWriter->dWriter.sData, 1);
      tBlockDataDestroy(&pWriter->dWriter.bData, 1);
      if (pWriter->dWriter.aSttBlk) taosArrayDestroy(pWriter->dWriter.aSttBlk);
      if (pWriter->dWriter.aBlockIdx) taosArrayDestroy(pWriter->dWriter.aBlockIdx);
      tBlockDataDestroy(&pWriter->dReader.bData, 1);
      if (pWriter->dReader.aBlockIdx) taosArrayDestroy(pWriter->dReader.aBlockIdx);
      tBlockDataDestroy(&pWriter->bData, 1);
      tsdbFSDestroy(&pWriter->fs);
      taosMemoryFree(pWriter);
    }
  } else {
S
Shengliang Guan 已提交
1371
    tsdbInfo("vgId:%d, %s done", TD_VID(pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
1372 1373
    *ppWriter = pWriter;
  }
H
Hongze Cheng 已提交
1374 1375 1376
  return code;
}

H
Hongze Cheng 已提交
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
int32_t tsdbSnapWriterPrepareClose(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
  if (pWriter->dWriter.pWriter) {
    code = tsdbSnapWriteCloseFile(pWriter);
    if (code) goto _exit;
  }

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

  code = tsdbFSPrepareCommit(pWriter->pTsdb, &pWriter->fs);
  if (code) goto _exit;

_exit:
  if (code) {
S
Shengliang Guan 已提交
1392
    tsdbError("vgId:%d, %s failed since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, tstrerror(code));
H
Hongze Cheng 已提交
1393 1394 1395 1396
  }
  return code;
}

H
Hongze Cheng 已提交
1397
int32_t tsdbSnapWriterClose(STsdbSnapWriter** ppWriter, int8_t rollback) {
H
Hongze Cheng 已提交
1398
  int32_t          code = 0;
H
Hongze Cheng 已提交
1399
  STsdbSnapWriter* pWriter = *ppWriter;
H
Hongze Cheng 已提交
1400
  STsdb*           pTsdb = pWriter->pTsdb;
H
Hongze Cheng 已提交
1401 1402

  if (rollback) {
H
Hongze Cheng 已提交
1403
    tsdbRollbackCommit(pWriter->pTsdb);
H
Hongze Cheng 已提交
1404
  } else {
H
Hongze Cheng 已提交
1405 1406 1407
    // lock
    taosThreadRwlockWrlock(&pTsdb->rwLock);

H
Hongze Cheng 已提交
1408
    code = tsdbFSCommit(pWriter->pTsdb);
H
Hongze Cheng 已提交
1409 1410 1411 1412 1413 1414 1415
    if (code) {
      taosThreadRwlockUnlock(&pTsdb->rwLock);
      goto _err;
    }

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

H
Hongze Cheng 已提交
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
  // SNAP_DATA_DEL
  taosArrayDestroy(pWriter->aDelIdxW);
  taosArrayDestroy(pWriter->aDelData);
  taosArrayDestroy(pWriter->aDelIdxR);

  // SNAP_DATA_TSDB

  // Writer
  tBlockDataDestroy(&pWriter->dWriter.sData, 1);
  tBlockDataDestroy(&pWriter->dWriter.bData, 1);
  taosArrayDestroy(pWriter->dWriter.aSttBlk);
  tMapDataClear(&pWriter->dWriter.mDataBlk);
  taosArrayDestroy(pWriter->dWriter.aBlockIdx);

  // Reader
  tBlockDataDestroy(&pWriter->dReader.bData, 1);
  tMapDataClear(&pWriter->dReader.mDataBlk);
  taosArrayDestroy(pWriter->dReader.aBlockIdx);

  tBlockDataDestroy(&pWriter->bData, 1);
1438
  tDestroyTSchema(pWriter->skmTable.pTSchema);
H
Hongze Cheng 已提交
1439

H
Hongze Cheng 已提交
1440 1441 1442
  for (int32_t iBuf = 0; iBuf < sizeof(pWriter->aBuf) / sizeof(uint8_t*); iBuf++) {
    tFree(pWriter->aBuf[iBuf]);
  }
S
Shengliang Guan 已提交
1443
  tsdbInfo("vgId:%d, %s done", TD_VID(pWriter->pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
1444 1445 1446 1447 1448
  taosMemoryFree(pWriter);
  *ppWriter = NULL;
  return code;

_err:
S
Shengliang Guan 已提交
1449
  tsdbError("vgId:%d, vnode snapshot tsdb writer close for %s failed since %s", TD_VID(pWriter->pTsdb->pVnode),
C
Cary Xu 已提交
1450 1451 1452
            pWriter->pTsdb->path, tstrerror(code));
  taosMemoryFree(pWriter);
  *ppWriter = NULL;
H
Hongze Cheng 已提交
1453 1454 1455 1456
  return code;
}

int32_t tsdbSnapWrite(STsdbSnapWriter* pWriter, uint8_t* pData, uint32_t nData) {
H
Hongze Cheng 已提交
1457
  int32_t       code = 0;
H
Hongze Cheng 已提交
1458
  SSnapDataHdr* pHdr = (SSnapDataHdr*)pData;
H
Hongze Cheng 已提交
1459 1460

  // ts data
C
Cary Xu 已提交
1461
  if (pHdr->type == SNAP_DATA_TSDB) {
H
Hongze Cheng 已提交
1462
    code = tsdbSnapWriteData(pWriter, pData, nData);
H
Hongze Cheng 已提交
1463
    if (code) goto _err;
H
Hongze Cheng 已提交
1464 1465

    goto _exit;
H
Hongze Cheng 已提交
1466
  } else {
H
Hongze Cheng 已提交
1467
    if (pWriter->dWriter.pWriter) {
H
Hongze Cheng 已提交
1468
      code = tsdbSnapWriteCloseFile(pWriter);
H
Hongze Cheng 已提交
1469 1470
      if (code) goto _err;
    }
H
Hongze Cheng 已提交
1471 1472 1473
  }

  // del data
C
Cary Xu 已提交
1474
  if (pHdr->type == SNAP_DATA_DEL) {
H
Hongze Cheng 已提交
1475
    code = tsdbSnapWriteDel(pWriter, pData, nData);
H
Hongze Cheng 已提交
1476 1477 1478
    if (code) goto _err;
  }

H
Hongze Cheng 已提交
1479
_exit:
C
Cary Xu 已提交
1480
  tsdbDebug("vgId:%d, tsdb snapshot write for %s succeed", TD_VID(pWriter->pTsdb->pVnode), pWriter->pTsdb->path);
H
Hongze Cheng 已提交
1481 1482 1483
  return code;

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