tsdbSnapshot.c 46.4 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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 已提交
17 18 19 20
#include "tsdbDataFileRW.h"
#include "tsdbFS2.h"
#include "tsdbIter.h"
#include "tsdbSttFileRW.h"
H
Hongze Cheng 已提交
21

H
Hongze Cheng 已提交
22 23 24
extern int32_t tsdbUpdateTableSchema(SMeta* pMeta, int64_t suid, int64_t uid, SSkmInfo* pSkmInfo);
extern int32_t tsdbWriteDataBlock(SDataFWriter* pWriter, SBlockData* pBlockData, SMapData* mDataBlk, int8_t cmprAlg);
extern int32_t tsdbWriteSttBlock(SDataFWriter* pWriter, SBlockData* pBlockData, SArray* aSttBlk, int8_t cmprAlg);
H
Hongze Cheng 已提交
25

H
Hongze Cheng 已提交
26
// STsdbSnapReader ========================================
H
Hongze Cheng 已提交
27
struct STsdbSnapReader {
H
Hongze Cheng 已提交
28 29 30 31 32
  STsdb*  tsdb;
  int64_t sver;
  int64_t ever;
  int8_t  type;

H
Hongze Cheng 已提交
33
  uint8_t* aBuf[5];
H
Hongze Cheng 已提交
34 35 36 37 38 39 40 41 42 43 44
  SSkmInfo skmTb[1];

  TFileSetArray* fsetArr;

  // context
  struct {
    int32_t    fsetArrIdx;
    STFileSet* fset;
    bool       isDataDone;
    bool       isTombDone;
  } ctx[1];
H
Hongze Cheng 已提交
45

H
Hongze Cheng 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58
  // reader
  SDataFileReader*    dataReader;
  TSttFileReaderArray sttReaderArr[1];

  // iter
  TTsdbIterArray dataIterArr[1];
  SIterMerger*   dataIterMerger;
  TTsdbIterArray tombIterArr[1];
  SIterMerger*   tombIterMerger;

  // data
  SBlockData blockData[1];
  STombBlock tombBlock[1];
H
Hongze Cheng 已提交
59
};
H
Hongze Cheng 已提交
60

H
Hongze Cheng 已提交
61
static int32_t tsdbSnapReadFileSetOpenReader(STsdbSnapReader* reader) {
H
Hongze Cheng 已提交
62
  int32_t code = 0;
H
add log  
Hongze Cheng 已提交
63
  int32_t lino = 0;
H
Hongze Cheng 已提交
64

H
Hongze Cheng 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  ASSERT(reader->dataReader == NULL);
  ASSERT(TARRAY2_SIZE(reader->sttReaderArr) == 0);

  // data
  SDataFileReaderConfig config = {
      .tsdb = reader->tsdb,
      .szPage = reader->tsdb->pVnode->config.tsdbPageSize,
      .bufArr = reader->aBuf,
  };
  bool hasDataFile = false;
  for (int32_t ftype = 0; ftype < TSDB_FTYPE_MAX; ftype++) {
    if (reader->ctx->fset->farr[ftype] != NULL) {
      hasDataFile = true;
      config.files[ftype].exist = true;
      config.files[ftype].file = reader->ctx->fset->farr[ftype]->f[0];
    }
H
Hongze Cheng 已提交
81
  }
H
Hongze Cheng 已提交
82

H
Hongze Cheng 已提交
83 84 85 86
  if (hasDataFile) {
    code = tsdbDataFileReaderOpen(NULL, &config, &reader->dataReader);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
87

H
Hongze Cheng 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  // stt
  SSttLvl* lvl;
  TARRAY2_FOREACH(reader->ctx->fset->lvlArr, lvl) {
    STFileObj* fobj;
    TARRAY2_FOREACH(lvl->fobjArr, fobj) {
      SSttFileReader*      sttReader;
      SSttFileReaderConfig config = {
          .tsdb = reader->tsdb,
          .szPage = reader->tsdb->pVnode->config.tsdbPageSize,
          .file = fobj->f[0],
          .bufArr = reader->aBuf,
      };

      code = tsdbSttFileReaderOpen(fobj->fname, &config, &sttReader);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
103

H
Hongze Cheng 已提交
104 105 106 107
      code = TARRAY2_APPEND(reader->sttReaderArr, sttReader);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  }
H
Hongze Cheng 已提交
108

H
Hongze Cheng 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(reader->tsdb->pVnode), code, lino);
  }
  return code;
}

static int32_t tsdbSnapReadFileSetCloseReader(STsdbSnapReader* reader) {
  int32_t code = 0;
  int32_t lino = 0;

  TARRAY2_CLEAR(reader->sttReaderArr, tsdbSttFileReaderClose);
  tsdbDataFileReaderClose(&reader->dataReader);

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(reader->tsdb->pVnode), code, lino);
  }
  return code;
}

static int32_t tsdbSnapReadFileSetOpenIter(STsdbSnapReader* reader) {
  int32_t code = 0;
  int32_t lino = 0;

  ASSERT(reader->dataIterMerger == NULL);
  ASSERT(reader->tombIterMerger == NULL);
  ASSERT(TARRAY2_SIZE(reader->dataIterArr) == 0);
  ASSERT(TARRAY2_SIZE(reader->tombIterArr) == 0);

  STsdbIter*      iter;
  STsdbIterConfig config = {
      .filterByVersion = true,
      .verRange[0] = reader->sver,
      .verRange[1] = reader->ever,
  };

  // data file
  if (reader->dataReader) {
    // data
    config.type = TSDB_ITER_TYPE_DATA;
    config.dataReader = reader->dataReader;

    code = tsdbIterOpen(&config, &iter);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
154

H
Hongze Cheng 已提交
155
    code = TARRAY2_APPEND(reader->dataIterArr, iter);
H
add log  
Hongze Cheng 已提交
156
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
157

H
Hongze Cheng 已提交
158 159 160
    // tomb
    config.type = TSDB_ITER_TYPE_DATA_TOMB;
    config.dataReader = reader->dataReader;
H
Hongze Cheng 已提交
161

H
Hongze Cheng 已提交
162 163 164 165 166
    code = tsdbIterOpen(&config, &iter);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = TARRAY2_APPEND(reader->tombIterArr, iter);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
167 168
  }

H
Hongze Cheng 已提交
169 170 171 172 173 174 175 176
  // stt file
  SSttFileReader* sttReader;
  TARRAY2_FOREACH(reader->sttReaderArr, sttReader) {
    // data
    config.type = TSDB_ITER_TYPE_STT;
    config.sttReader = sttReader;

    code = tsdbIterOpen(&config, &iter);
H
add log  
Hongze Cheng 已提交
177
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
178

H
Hongze Cheng 已提交
179 180
    code = TARRAY2_APPEND(reader->dataIterArr, iter);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
181

H
Hongze Cheng 已提交
182 183 184
    // tomb
    config.type = TSDB_ITER_TYPE_STT_TOMB;
    config.sttReader = sttReader;
H
Hongze Cheng 已提交
185

H
Hongze Cheng 已提交
186 187 188 189 190
    code = tsdbIterOpen(&config, &iter);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = TARRAY2_APPEND(reader->tombIterArr, iter);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
191 192
  }

H
Hongze Cheng 已提交
193 194 195 196 197 198
  // merger
  code = tsdbIterMergerOpen(reader->dataIterArr, &reader->dataIterMerger, false);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbIterMergerOpen(reader->tombIterArr, &reader->dataIterMerger, true);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
199

H
add log  
Hongze Cheng 已提交
200 201
_exit:
  if (code) {
H
Hongze Cheng 已提交
202
    TSDB_ERROR_LOG(TD_VID(reader->tsdb->pVnode), code, lino);
H
add log  
Hongze Cheng 已提交
203
  }
H
Hongze Cheng 已提交
204 205 206
  return code;
}

H
Hongze Cheng 已提交
207 208 209 210 211 212
static int32_t tsdbSnapReadFileSetCloseIter(STsdbSnapReader* reader) {
  tsdbIterMergerClose(&reader->dataIterMerger);
  tsdbIterMergerClose(&reader->tombIterMerger);
  TARRAY2_CLEAR(reader->dataIterArr, tsdbIterClose);
  TARRAY2_CLEAR(reader->tombIterArr, tsdbIterClose);
  return 0;
H
Hongze Cheng 已提交
213
}
H
Hongze Cheng 已提交
214

H
Hongze Cheng 已提交
215
static int32_t tsdbSnapReadFileSetBegin(STsdbSnapReader* reader) {
H
Hongze Cheng 已提交
216 217
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
218

H
Hongze Cheng 已提交
219 220 221 222
  if (reader->ctx->fsetArrIdx < TARRAY2_SIZE(reader->fsetArr)) {
    reader->ctx->fset = TARRAY2_GET(reader->fsetArr, reader->ctx->fsetArrIdx++);
    reader->ctx->isDataDone = false;
    reader->ctx->isTombDone = false;
H
Hongze Cheng 已提交
223

H
Hongze Cheng 已提交
224 225
    code = tsdbSnapReadFileSetOpenReader(reader);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
226

H
Hongze Cheng 已提交
227 228
    code = tsdbSnapReadFileSetOpenIter(reader);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
229
  }
H
Hongze Cheng 已提交
230

H
Hongze Cheng 已提交
231 232
_exit:
  if (code) {
H
Hongze Cheng 已提交
233
    TSDB_ERROR_LOG(TD_VID(reader->tsdb->pVnode), code, lino);
H
Hongze Cheng 已提交
234
  }
H
Hongze Cheng 已提交
235 236 237
  return code;
}

H
Hongze Cheng 已提交
238 239 240 241 242
static int32_t tsdbSnapReadFileSetEnd(STsdbSnapReader* reader) {
  tsdbSnapReadFileSetCloseIter(reader);
  tsdbSnapReadFileSetCloseReader(reader);
  reader->ctx->fset = NULL;
  return 0;
H
Hongze Cheng 已提交
243 244
}

H
Hongze Cheng 已提交
245
static int32_t tsdbSnapCmprData(STsdbSnapReader* reader, uint8_t** data) {
H
Hongze Cheng 已提交
246
  int32_t code = 0;
H
Hongze Cheng 已提交
247
  int32_t lino = 0;
H
Hongze Cheng 已提交
248 249

  int32_t aBufN[5] = {0};
H
Hongze Cheng 已提交
250 251
  code = tCmprBlockData(reader->blockData, NO_COMPRESSION, NULL, NULL, reader->aBuf, aBufN);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
252 253

  int32_t size = aBufN[0] + aBufN[1] + aBufN[2] + aBufN[3];
H
Hongze Cheng 已提交
254 255
  *data = taosMemoryMalloc(sizeof(SSnapDataHdr) + size);
  if (*data == NULL) {
H
Hongze Cheng 已提交
256
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
257
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
258 259
  }

H
Hongze Cheng 已提交
260 261
  SSnapDataHdr* pHdr = (SSnapDataHdr*)*data;
  pHdr->type = reader->type;
H
Hongze Cheng 已提交
262 263
  pHdr->size = size;

H
Hongze Cheng 已提交
264 265
  memcpy(pHdr->data, reader->aBuf[3], aBufN[3]);
  memcpy(pHdr->data + aBufN[3], reader->aBuf[2], aBufN[2]);
H
Hongze Cheng 已提交
266
  if (aBufN[1]) {
H
Hongze Cheng 已提交
267
    memcpy(pHdr->data + aBufN[3] + aBufN[2], reader->aBuf[1], aBufN[1]);
H
Hongze Cheng 已提交
268 269
  }
  if (aBufN[0]) {
H
Hongze Cheng 已提交
270
    memcpy(pHdr->data + aBufN[3] + aBufN[2] + aBufN[1], reader->aBuf[0], aBufN[0]);
H
Hongze Cheng 已提交
271 272 273
  }

_exit:
H
Hongze Cheng 已提交
274 275 276
  if (code) {
    TSDB_ERROR_LOG(TD_VID(reader->tsdb->pVnode), lino, code);
  }
H
Hongze Cheng 已提交
277 278 279
  return code;
}

H
Hongze Cheng 已提交
280
static int32_t tsdbSnapReadTimeSeriesData(STsdbSnapReader* reader, uint8_t** data) {
H
Hongze Cheng 已提交
281
  int32_t code = 0;
H
add log  
Hongze Cheng 已提交
282 283
  int32_t lino = 0;

H
Hongze Cheng 已提交
284
  tBlockDataReset(reader->blockData);
H
Hongze Cheng 已提交
285

H
Hongze Cheng 已提交
286 287 288 289
  for (SRowInfo* row; (row = tsdbIterMergerGetData(reader->dataIterMerger));) {
    if (reader->blockData->suid == 0 && reader->blockData->uid == 0) {
      code = tsdbUpdateSkmTb(reader->tsdb, (TABLEID*)row, reader->skmTb);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
290

H
Hongze Cheng 已提交
291 292 293 294 295
      TABLEID tbid = {
          .suid = row->suid,
          .uid = row->suid ? 0 : row->uid,
      };
      code = tBlockDataInit(reader->blockData, &tbid, reader->skmTb->pTSchema, NULL, 0);
H
add log  
Hongze Cheng 已提交
296
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
297 298
    }

H
Hongze Cheng 已提交
299 300
    if (!TABLE_SAME_SCHEMA(reader->blockData->suid, reader->blockData->uid, row->suid, row->uid)) {
      break;
H
Hongze Cheng 已提交
301 302
    }

H
Hongze Cheng 已提交
303
    code = tBlockDataAppendRow(reader->blockData, &row->row, NULL, row->uid);
H
add log  
Hongze Cheng 已提交
304
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
305

H
Hongze Cheng 已提交
306
    code = tsdbIterMergerNext(reader->dataIterMerger);
H
add log  
Hongze Cheng 已提交
307
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
308

H
Hongze Cheng 已提交
309 310 311
    if (reader->blockData->nRow >= 81920) {
      break;
    }
H
Hongze Cheng 已提交
312
  }
H
Hongze Cheng 已提交
313

H
Hongze Cheng 已提交
314 315 316
  if (reader->blockData->nRow > 0) {
    ASSERT(reader->blockData->suid || reader->blockData->uid);
    code = tsdbSnapCmprData(reader, data);
H
add log  
Hongze Cheng 已提交
317
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
318 319
  }

H
add log  
Hongze Cheng 已提交
320 321
_exit:
  if (code) {
H
Hongze Cheng 已提交
322
    TSDB_ERROR_LOG(TD_VID(reader->tsdb->pVnode), code, lino);
H
add log  
Hongze Cheng 已提交
323
  }
H
Hongze Cheng 已提交
324 325 326
  return code;
}

H
Hongze Cheng 已提交
327
static int32_t tsdbSnapCmprTombData(STsdbSnapReader* reader, uint8_t** data) {
H
add log  
Hongze Cheng 已提交
328 329 330
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
331 332 333
  int64_t size = sizeof(SSnapDataHdr);
  for (int32_t i = 0; i < ARRAY_SIZE(reader->tombBlock->dataArr); i++) {
    size += TARRAY2_DATA_LEN(reader->tombBlock->dataArr + i);
H
Hongze Cheng 已提交
334
  }
H
Hongze Cheng 已提交
335

H
Hongze Cheng 已提交
336 337
  data[0] = taosMemoryMalloc(size);
  if (data[0] == NULL) {
H
Hongze Cheng 已提交
338
    code = TSDB_CODE_OUT_OF_MEMORY;
H
add log  
Hongze Cheng 已提交
339
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
340
  }
H
Hongze Cheng 已提交
341

H
Hongze Cheng 已提交
342 343 344
  SSnapDataHdr* hdr = (SSnapDataHdr*)data[0];
  hdr->type = SNAP_DATA_DEL;
  hdr->size = size;
H
Hongze Cheng 已提交
345

H
Hongze Cheng 已提交
346 347 348 349
  uint8_t* tdata = hdr->data;
  for (int32_t i = 0; i < TARRAY_SIZE(reader->tombBlock->dataArr); i++) {
    memcpy(tdata, TARRAY2_DATA(reader->tombBlock->dataArr + i), TARRAY2_DATA_LEN(reader->tombBlock->dataArr + i));
    tdata += TARRAY2_DATA_LEN(reader->tombBlock->dataArr + i);
H
Hongze Cheng 已提交
350 351
  }

H
Hongze Cheng 已提交
352 353
_exit:
  if (code) {
H
Hongze Cheng 已提交
354
    TSDB_ERROR_LOG(TD_VID(reader->tsdb->pVnode), code, lino);
H
Hongze Cheng 已提交
355 356 357
  }
  return code;
}
H
Hongze Cheng 已提交
358

H
Hongze Cheng 已提交
359
static int32_t tsdbSnapReadTombData(STsdbSnapReader* reader, uint8_t** data) {
H
Hongze Cheng 已提交
360 361
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
362

H
Hongze Cheng 已提交
363
  tTombBlockClear(reader->tombBlock);
H
Hongze Cheng 已提交
364

H
Hongze Cheng 已提交
365 366
  for (STombRecord* record; (record = tsdbIterMergerGetTombRecord(reader->tombIterMerger)) != NULL;) {
    code = tTombBlockPut(reader->tombBlock, record);
H
add log  
Hongze Cheng 已提交
367
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
368

H
Hongze Cheng 已提交
369 370
    if (TOMB_BLOCK_SIZE(reader->tombBlock) >= 81920) {
      break;
H
Hongze Cheng 已提交
371
    }
H
Hongze Cheng 已提交
372 373
  }

H
Hongze Cheng 已提交
374 375
  if (TOMB_BLOCK_SIZE(reader->tombBlock) > 0) {
    code = tsdbSnapCmprTombData(reader, data);
H
Hongze Cheng 已提交
376
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
377
  }
H
Hongze Cheng 已提交
378 379

_exit:
H
add log  
Hongze Cheng 已提交
380
  if (code) {
H
Hongze Cheng 已提交
381
    TSDB_ERROR_LOG(TD_VID(reader->tsdb->pVnode), code, lino);
H
add log  
Hongze Cheng 已提交
382
  }
H
Hongze Cheng 已提交
383 384
  return code;
}
H
more  
Hongze Cheng 已提交
385

H
Hongze Cheng 已提交
386
int32_t tsdbSnapReaderOpen(STsdb* tsdb, int64_t sver, int64_t ever, int8_t type, STsdbSnapReader** reader) {
H
Hongze Cheng 已提交
387 388
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
389

H
more  
Hongze Cheng 已提交
390
  // alloc
H
Hongze Cheng 已提交
391 392
  reader[0] = (STsdbSnapReader*)taosMemoryCalloc(1, sizeof(*reader[0]));
  if (reader[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;
H
more  
Hongze Cheng 已提交
393

H
Hongze Cheng 已提交
394 395 396 397
  reader[0]->tsdb = tsdb;
  reader[0]->sver = sver;
  reader[0]->ever = ever;
  reader[0]->type = type;
H
Hongze Cheng 已提交
398

H
Hongze Cheng 已提交
399
  code = tsdbFSCreateRefSnapshot(tsdb->pFS, &reader[0]->fsetArr);
H
Hongze Cheng 已提交
400
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
401

H
Hongze Cheng 已提交
402 403
_exit:
  if (code) {
H
Hongze Cheng 已提交
404
    tsdbError("vgId:%d %s failed at line %d since %s, sver:%" PRId64 " ever:%" PRId64 " type:%d", TD_VID(tsdb->pVnode),
H
Hongze Cheng 已提交
405
              __func__, lino, tstrerror(code), sver, ever, type);
H
Hongze Cheng 已提交
406 407 408
    tsdbFSDestroyRefSnapshot(&reader[0]->fsetArr);
    taosMemoryFree(reader[0]);
    reader[0] = NULL;
H
Hongze Cheng 已提交
409
  } else {
H
Hongze Cheng 已提交
410
    tsdbInfo("vgId:%d %s done, sver:%" PRId64 " ever:%" PRId64 " type:%d", TD_VID(tsdb->pVnode), __func__, sver, ever,
H
Hongze Cheng 已提交
411
             type);
H
Hongze Cheng 已提交
412
  }
H
more  
Hongze Cheng 已提交
413
  return code;
H
Hongze Cheng 已提交
414 415
}

H
Hongze Cheng 已提交
416 417 418
int32_t tsdbSnapReaderClose(STsdbSnapReader** reader) {
  if (reader[0] == NULL) return 0;

H
Hongze Cheng 已提交
419 420
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
421

H
Hongze Cheng 已提交
422
  STsdb* tsdb = reader[0]->tsdb;
H
Hongze Cheng 已提交
423

H
Hongze Cheng 已提交
424 425
  tTombBlockDestroy(reader[0]->tombBlock);
  tBlockDataDestroy(reader[0]->blockData);
H
Hongze Cheng 已提交
426

H
Hongze Cheng 已提交
427 428 429 430 431 432
  tsdbIterMergerClose(&reader[0]->dataIterMerger);
  tsdbIterMergerClose(&reader[0]->tombIterMerger);
  TARRAY2_DESTROY(reader[0]->dataIterArr, tsdbIterClose);
  TARRAY2_DESTROY(reader[0]->tombIterArr, tsdbIterClose);
  TARRAY2_DESTROY(reader[0]->sttReaderArr, tsdbSttFileReaderClose);
  tsdbDataFileReaderClose(&reader[0]->dataReader);
H
Hongze Cheng 已提交
433

H
Hongze Cheng 已提交
434 435 436 437 438
  tsdbFSDestroyRefSnapshot(&reader[0]->fsetArr);
  tDestroyTSchema(reader[0]->skmTb->pTSchema);

  for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->aBuf);) {
    tFree(reader[0]->aBuf[i]);
H
Hongze Cheng 已提交
439
  }
H
Hongze Cheng 已提交
440 441 442

  taosMemoryFree(reader[0]);
  reader[0] = NULL;
H
Hongze Cheng 已提交
443 444 445

_exit:
  if (code) {
H
Hongze Cheng 已提交
446
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
447
  } else {
H
Hongze Cheng 已提交
448
    tsdbDebug("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
449
  }
H
Hongze Cheng 已提交
450 451 452
  return code;
}

H
Hongze Cheng 已提交
453
int32_t tsdbSnapRead(STsdbSnapReader* reader, uint8_t** data) {
H
more  
Hongze Cheng 已提交
454
  int32_t code = 0;
H
add log  
Hongze Cheng 已提交
455
  int32_t lino = 0;
H
Hongze Cheng 已提交
456

H
Hongze Cheng 已提交
457
  data[0] = NULL;
H
Hongze Cheng 已提交
458

H
Hongze Cheng 已提交
459 460 461 462 463 464 465 466
  for (;;) {
    if (reader->ctx->fset == NULL) {
      code = tsdbSnapReadFileSetBegin(reader);
      TSDB_CHECK_CODE(code, lino, _exit);

      if (reader->ctx->fset == NULL) {
        break;
      }
H
Hongze Cheng 已提交
467
    }
H
Hongze Cheng 已提交
468

H
Hongze Cheng 已提交
469 470 471 472 473 474 475 476
    if (!reader->ctx->isDataDone) {
      code = tsdbSnapReadTimeSeriesData(reader, data);
      TSDB_CHECK_CODE(code, lino, _exit);
      if (data[0]) {
        goto _exit;
      } else {
        reader->ctx->isDataDone = true;
      }
H
Hongze Cheng 已提交
477
    }
H
Hongze Cheng 已提交
478 479 480 481 482 483 484 485 486 487 488 489 490

    if (!reader->ctx->isTombDone) {
      code = tsdbSnapReadTombData(reader, data);
      TSDB_CHECK_CODE(code, lino, _exit);
      if (data[0]) {
        goto _exit;
      } else {
        reader->ctx->isTombDone = true;
      }
    }

    code = tsdbSnapReadFileSetEnd(reader);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
491
  }
H
Hongze Cheng 已提交
492

H
Hongze Cheng 已提交
493
_exit:
H
add log  
Hongze Cheng 已提交
494
  if (code) {
H
Hongze Cheng 已提交
495
    TSDB_ERROR_LOG(TD_VID(reader->tsdb->pVnode), code, lino);
H
add log  
Hongze Cheng 已提交
496
  } else {
H
Hongze Cheng 已提交
497
    tsdbDebug("vgId:%d %s done", TD_VID(reader->tsdb->pVnode), __func__);
H
add log  
Hongze Cheng 已提交
498
  }
H
more  
Hongze Cheng 已提交
499 500 501
  return code;
}

H
Hongze Cheng 已提交
502
// STsdbSnapWriter ========================================
H
Hongze Cheng 已提交
503
struct STsdbSnapWriter {
H
Hongze Cheng 已提交
504
  STsdb*   tsdb;
H
Hongze Cheng 已提交
505 506
  int64_t  sver;
  int64_t  ever;
H
Hongze Cheng 已提交
507 508 509 510 511 512
  int32_t  minutes;
  int8_t   precision;
  int32_t  minRow;
  int32_t  maxRow;
  int8_t   cmprAlg;
  int64_t  commitID;
H
Hongze Cheng 已提交
513
  uint8_t* aBuf[5];
H
Hongze Cheng 已提交
514

H
Hongze Cheng 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
  TFileSetArray* fsetArr;
  TFileOpArray   fopArr[1];

  struct {
    bool fsetWriteBegin;

    int32_t    fid;
    STFileSet* fset;

    bool hasData;
    bool hasTomb;

    // reader
    SDataFileReader*    dataReader;
    TSttFileReaderArray sttReaderArr[1];

    // iter/merger
    TTsdbIterArray dataIterArr[1];
    SIterMerger*   dataIterMerger;
    TTsdbIterArray tombIterArr[1];
    SIterMerger*   tombIterMerger;
  } ctx[1];

  SDataFileWriter* dataWriter;
  SSttFileWriter*  sttWriter;

  SBlockData blockData[1];
  STombBlock tombBlock[1];
H
Hongze Cheng 已提交
543 544
};

H
Hongze Cheng 已提交
545
#if 0
H
Hongze Cheng 已提交
546
// SNAP_DATA_TSDB
H
Hongze Cheng 已提交
547
static int32_t tsdbSnapWriteTableDataStart(STsdbSnapWriter* pWriter, TABLEID* pId) {
H
Hongze Cheng 已提交
548
  int32_t code = 0;
H
Hongze Cheng 已提交
549 550
  int32_t lino = 0;

H
Hongze Cheng 已提交
551 552 553 554 555 556
  if (pId) {
    pWriter->tbid = *pId;
  } else {
    pWriter->tbid = (TABLEID){INT64_MAX, INT64_MAX};
  }

H
Hongze Cheng 已提交
557 558
  if (pWriter->pDIter) {
    STsdbDataIter2* pIter = pWriter->pDIter;
H
Hongze Cheng 已提交
559

H
Hongze Cheng 已提交
560
    // assert last table data end
H
Hongze Cheng 已提交
561 562 563
    ASSERT(pIter->dIter.iRow >= pIter->dIter.bData.nRow);
    ASSERT(pIter->dIter.iDataBlk >= pIter->dIter.mDataBlk.nItem);

H
Hongze Cheng 已提交
564
    for (;;) {
H
Hongze Cheng 已提交
565
      if (pIter->dIter.iBlockIdx >= taosArrayGetSize(pIter->dIter.aBlockIdx)) {
H
Hongze Cheng 已提交
566 567 568 569
        pWriter->pDIter = NULL;
        break;
      }

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

H
Hongze Cheng 已提交
572
      int32_t c = tTABLEIDCmprFn(pBlockIdx, &pWriter->tbid);
H
Hongze Cheng 已提交
573 574 575 576 577 578
      if (c < 0) {
        code = tsdbReadDataBlk(pIter->dIter.pReader, pBlockIdx, &pIter->dIter.mDataBlk);
        TSDB_CHECK_CODE(code, lino, _exit);

        SBlockIdx* pNewBlockIdx = taosArrayReserve(pWriter->aBlockIdx, 1);
        if (pNewBlockIdx == NULL) {
H
Hongze Cheng 已提交
579
          code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
580 581 582 583 584 585
          TSDB_CHECK_CODE(code, lino, _exit);
        }

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

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

H
Hongze Cheng 已提交
589 590
        pIter->dIter.iBlockIdx++;
      } else if (c == 0) {
H
Hongze Cheng 已提交
591 592 593
        code = tsdbReadDataBlk(pIter->dIter.pReader, pBlockIdx, &pIter->dIter.mDataBlk);
        TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
594
        pIter->dIter.iDataBlk = 0;
H
Hongze Cheng 已提交
595
        pIter->dIter.iBlockIdx++;
H
Hongze Cheng 已提交
596 597 598

        break;
      } else {
H
Hongze Cheng 已提交
599
        pIter->dIter.iDataBlk = pIter->dIter.mDataBlk.nItem;
H
Hongze Cheng 已提交
600 601 602 603
        break;
      }
    }
  }
H
Hongze Cheng 已提交
604

H
Hongze Cheng 已提交
605
  if (pId) {
H
Hongze Cheng 已提交
606
    code = tsdbUpdateTableSchema(pWriter->tsdb->pVnode->pMeta, pId->suid, pId->uid, &pWriter->skmTable);
H
Hongze Cheng 已提交
607
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
608

H
Hongze Cheng 已提交
609
    tMapDataReset(&pWriter->mDataBlk);
H
Hongze Cheng 已提交
610

H
Hongze Cheng 已提交
611 612
    code = tBlockDataInit(&pWriter->bData, pId, pWriter->skmTable.pTSchema, NULL, 0);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
613
  }
H
Hongze Cheng 已提交
614

H
Hongze Cheng 已提交
615 616 617 618 619
  if (!TABLE_SAME_SCHEMA(pWriter->tbid.suid, pWriter->tbid.uid, pWriter->sData.suid, pWriter->sData.uid)) {
    if ((pWriter->sData.nRow > 0)) {
      code = tsdbWriteSttBlock(pWriter->pDataFWriter, &pWriter->sData, pWriter->aSttBlk, pWriter->cmprAlg);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
620

H
Hongze Cheng 已提交
621 622 623 624 625
    if (pId) {
      TABLEID id = {.suid = pWriter->tbid.suid, .uid = pWriter->tbid.suid ? 0 : pWriter->tbid.uid};
      code = tBlockDataInit(&pWriter->sData, &id, pWriter->skmTable.pTSchema, NULL, 0);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
626
  }
H
Hongze Cheng 已提交
627

H
Hongze Cheng 已提交
628 629
_exit:
  if (code) {
H
Hongze Cheng 已提交
630
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->tsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
631
  } else {
H
Hongze Cheng 已提交
632
    tsdbTrace("vgId:%d %s done, suid:%" PRId64 " uid:%" PRId64, TD_VID(pWriter->tsdb->pVnode), __func__,
H
Hongze Cheng 已提交
633
              pWriter->tbid.suid, pWriter->tbid.uid);
H
Hongze Cheng 已提交
634
  }
H
Hongze Cheng 已提交
635 636 637
  return code;
}

H
Hongze Cheng 已提交
638 639 640 641 642 643 644 645 646 647
static int32_t tsdbSnapWriteTableRowImpl(STsdbSnapWriter* pWriter, TSDBROW* pRow) {
  int32_t code = 0;
  int32_t lino = 0;

  code = tBlockDataAppendRow(&pWriter->bData, pRow, pWriter->skmTable.pTSchema, pWriter->tbid.uid);
  TSDB_CHECK_CODE(code, lino, _exit);

  if (pWriter->bData.nRow >= pWriter->maxRow) {
    code = tsdbWriteDataBlock(pWriter->pDataFWriter, &pWriter->bData, &pWriter->mDataBlk, pWriter->cmprAlg);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
648 649 650
  }

_exit:
H
Hongze Cheng 已提交
651
  if (code) {
H
Hongze Cheng 已提交
652
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->tsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
653
  }
H
Hongze Cheng 已提交
654 655 656
  return code;
}

H
Hongze Cheng 已提交
657
static int32_t tsdbSnapWriteTableRow(STsdbSnapWriter* pWriter, TSDBROW* pRow) {
H
Hongze Cheng 已提交
658
  int32_t code = 0;
H
Hongze Cheng 已提交
659 660 661
  int32_t lino = 0;

  TSDBKEY inKey = pRow ? TSDBROW_KEY(pRow) : TSDBKEY_MAX;
H
Hongze Cheng 已提交
662

H
Hongze Cheng 已提交
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
  if (pWriter->pDIter == NULL || (pWriter->pDIter->dIter.iRow >= pWriter->pDIter->dIter.bData.nRow &&
                                  pWriter->pDIter->dIter.iDataBlk >= pWriter->pDIter->dIter.mDataBlk.nItem)) {
    goto _write_row;
  } else {
    for (;;) {
      while (pWriter->pDIter->dIter.iRow < pWriter->pDIter->dIter.bData.nRow) {
        TSDBROW row = tsdbRowFromBlockData(&pWriter->pDIter->dIter.bData, pWriter->pDIter->dIter.iRow);

        int32_t c = tsdbKeyCmprFn(&inKey, &TSDBROW_KEY(&row));
        if (c < 0) {
          goto _write_row;
        } else if (c > 0) {
          code = tsdbSnapWriteTableRowImpl(pWriter, &row);
          TSDB_CHECK_CODE(code, lino, _exit);

          pWriter->pDIter->dIter.iRow++;
        } else {
          ASSERT(0);
        }
      }
H
Hongze Cheng 已提交
683

H
Hongze Cheng 已提交
684 685
      for (;;) {
        if (pWriter->pDIter->dIter.iDataBlk >= pWriter->pDIter->dIter.mDataBlk.nItem) goto _write_row;
H
Hongze Cheng 已提交
686

H
Hongze Cheng 已提交
687 688 689 690 691 692 693 694 695 696 697 698 699
        // FIXME: Here can be slow, use array instead
        SDataBlk dataBlk;
        tMapDataGetItemByIdx(&pWriter->pDIter->dIter.mDataBlk, pWriter->pDIter->dIter.iDataBlk, &dataBlk, tGetDataBlk);

        int32_t c = tDataBlkCmprFn(&dataBlk, &(SDataBlk){.minKey = inKey, .maxKey = inKey});
        if (c > 0) {
          goto _write_row;
        } else if (c < 0) {
          if (pWriter->bData.nRow > 0) {
            code = tsdbWriteDataBlock(pWriter->pDataFWriter, &pWriter->bData, &pWriter->mDataBlk, pWriter->cmprAlg);
            TSDB_CHECK_CODE(code, lino, _exit);
          }

700
          tMapDataPutItem(&pWriter->mDataBlk, &dataBlk, tPutDataBlk);
H
Hongze Cheng 已提交
701 702 703 704 705 706 707 708 709 710
          pWriter->pDIter->dIter.iDataBlk++;
        } else {
          code = tsdbReadDataBlockEx(pWriter->pDataFReader, &dataBlk, &pWriter->pDIter->dIter.bData);
          TSDB_CHECK_CODE(code, lino, _exit);

          pWriter->pDIter->dIter.iRow = 0;
          pWriter->pDIter->dIter.iDataBlk++;
          break;
        }
      }
H
Hongze Cheng 已提交
711
    }
H
Hongze Cheng 已提交
712
  }
H
Hongze Cheng 已提交
713

H
Hongze Cheng 已提交
714 715 716 717
_write_row:
  if (pRow) {
    code = tsdbSnapWriteTableRowImpl(pWriter, pRow);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
718 719
  }

H
Hongze Cheng 已提交
720
_exit:
H
Hongze Cheng 已提交
721
  if (code) {
H
Hongze Cheng 已提交
722
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->tsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
723
  }
H
Hongze Cheng 已提交
724 725 726
  return code;
}

H
Hongze Cheng 已提交
727 728
static int32_t tsdbSnapWriteTableDataEnd(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
729
  int32_t lino = 0;
H
Hongze Cheng 已提交
730

H
Hongze Cheng 已提交
731 732 733
  // write a NULL row to end current table data write
  code = tsdbSnapWriteTableRow(pWriter, NULL);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
734

H
Hongze Cheng 已提交
735 736
  if (pWriter->bData.nRow > 0) {
    if (pWriter->bData.nRow < pWriter->minRow) {
H
Hongze Cheng 已提交
737
      ASSERT(TABLE_SAME_SCHEMA(pWriter->sData.suid, pWriter->sData.uid, pWriter->tbid.suid, pWriter->tbid.uid));
H
Hongze Cheng 已提交
738
      for (int32_t iRow = 0; iRow < pWriter->bData.nRow; iRow++) {
H
Hongze Cheng 已提交
739 740
        code =
            tBlockDataAppendRow(&pWriter->sData, &tsdbRowFromBlockData(&pWriter->bData, iRow), NULL, pWriter->tbid.uid);
H
Hongze Cheng 已提交
741
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
742

H
Hongze Cheng 已提交
743 744 745 746
        if (pWriter->sData.nRow >= pWriter->maxRow) {
          code = tsdbWriteSttBlock(pWriter->pDataFWriter, &pWriter->sData, pWriter->aSttBlk, pWriter->cmprAlg);
          TSDB_CHECK_CODE(code, lino, _exit);
        }
H
Hongze Cheng 已提交
747
      }
H
Hongze Cheng 已提交
748

H
Hongze Cheng 已提交
749
      tBlockDataClear(&pWriter->bData);
H
Hongze Cheng 已提交
750 751 752
    } else {
      code = tsdbWriteDataBlock(pWriter->pDataFWriter, &pWriter->bData, &pWriter->mDataBlk, pWriter->cmprAlg);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
753 754
    }
  }
H
Hongze Cheng 已提交
755

H
Hongze Cheng 已提交
756 757 758
  if (pWriter->mDataBlk.nItem) {
    SBlockIdx* pBlockIdx = taosArrayReserve(pWriter->aBlockIdx, 1);
    if (pBlockIdx == NULL) {
H
Hongze Cheng 已提交
759
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
760
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
761
    }
H
Hongze Cheng 已提交
762

H
Hongze Cheng 已提交
763 764
    pBlockIdx->suid = pWriter->tbid.suid;
    pBlockIdx->uid = pWriter->tbid.uid;
H
Hongze Cheng 已提交
765

H
Hongze Cheng 已提交
766 767 768
    code = tsdbWriteDataBlk(pWriter->pDataFWriter, &pWriter->mDataBlk, pBlockIdx);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
769

H
Hongze Cheng 已提交
770 771
_exit:
  if (code) {
H
Hongze Cheng 已提交
772
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->tsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
773
  }
H
Hongze Cheng 已提交
774 775 776
  return code;
}

H
Hongze Cheng 已提交
777
static int32_t tsdbSnapWriteFileDataStart(STsdbSnapWriter* pWriter, int32_t fid) {
H
Hongze Cheng 已提交
778
  int32_t code = 0;
H
Hongze Cheng 已提交
779
  int32_t lino = 0;
H
Hongze Cheng 已提交
780

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

H
Hongze Cheng 已提交
783
  STsdb* pTsdb = pWriter->tsdb;
H
Hongze Cheng 已提交
784 785

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

H
Hongze Cheng 已提交
789 790 791 792
  // open reader
  pWriter->pDataFReader = NULL;
  pWriter->iterList = NULL;
  pWriter->pDIter = NULL;
H
Hongze Cheng 已提交
793
  pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
794
  tRBTreeCreate(&pWriter->rbt, tsdbDataIterCmprFn);
H
Hongze Cheng 已提交
795
  if (pSet) {
H
Hongze Cheng 已提交
796 797
    code = tsdbDataFReaderOpen(&pWriter->pDataFReader, pTsdb, pSet);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
798

H
Hongze Cheng 已提交
799 800 801 802 803 804
    code = tsdbOpenDataFileDataIter(pWriter->pDataFReader, &pWriter->pDIter);
    TSDB_CHECK_CODE(code, lino, _exit);
    if (pWriter->pDIter) {
      pWriter->pDIter->next = pWriter->iterList;
      pWriter->iterList = pWriter->pDIter;
    }
H
Hongze Cheng 已提交
805

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

H
Hongze Cheng 已提交
810
      if (pWriter->pSIter) {
H
Hongze Cheng 已提交
811
        code = tsdbDataIterNext2(pWriter->pSIter, NULL);
H
Hongze Cheng 已提交
812
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
813

H
Hongze Cheng 已提交
814
        // add to tree
H
Hongze Cheng 已提交
815
        tRBTreePut(&pWriter->rbt, &pWriter->pSIter->rbtn);
H
Hongze Cheng 已提交
816

H
Hongze Cheng 已提交
817
        // add to list
H
Hongze Cheng 已提交
818 819
        pWriter->pSIter->next = pWriter->iterList;
        pWriter->iterList = pWriter->pSIter;
H
Hongze Cheng 已提交
820 821 822
      }
    }

H
Hongze Cheng 已提交
823
    pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
824 825 826 827 828 829
  }

  // open writer
  SDiskID diskId;
  if (pSet) {
    diskId = pSet->diskId;
H
Hongze Cheng 已提交
830
  } else {
H
Hongze Cheng 已提交
831 832 833 834 835 836 837 838 839 840 841 842
    tfsAllocDisk(pTsdb->pVnode->pTfs, 0 /*TODO*/, &diskId);
    tfsMkdirRecurAt(pTsdb->pVnode->pTfs, pTsdb->path, diskId);
  }
  SDFileSet wSet = {.diskId = diskId,
                    .fid = fid,
                    .pHeadF = &(SHeadFile){.commitID = pWriter->commitID},
                    .pDataF = (pSet) ? pSet->pDataF : &(SDataFile){.commitID = pWriter->commitID},
                    .pSmaF = (pSet) ? pSet->pSmaF : &(SSmaFile){.commitID = pWriter->commitID},
                    .nSttF = 1,
                    .aSttF = {&(SSttFile){.commitID = pWriter->commitID}}};
  code = tsdbDataFWriterOpen(&pWriter->pDataFWriter, pTsdb, &wSet);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
843

H
Hongze Cheng 已提交
844 845 846 847 848
  if (pWriter->aBlockIdx) {
    taosArrayClear(pWriter->aBlockIdx);
  } else if ((pWriter->aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
849
  }
H
Hongze Cheng 已提交
850

H
Hongze Cheng 已提交
851
  tMapDataReset(&pWriter->mDataBlk);
H
Hongze Cheng 已提交
852

H
Hongze Cheng 已提交
853 854 855 856 857
  if (pWriter->aSttBlk) {
    taosArrayClear(pWriter->aSttBlk);
  } else if ((pWriter->aSttBlk = taosArrayInit(0, sizeof(SSttBlk))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
858 859
  }

H
Hongze Cheng 已提交
860
  tBlockDataReset(&pWriter->bData);
H
Hongze Cheng 已提交
861
  tBlockDataReset(&pWriter->sData);
H
Hongze Cheng 已提交
862

H
Hongze Cheng 已提交
863 864 865 866 867 868 869
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s, fid:%d", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code),
              fid);
  } else {
    tsdbDebug("vgId:%d %s done, fid:%d", TD_VID(pTsdb->pVnode), __func__, fid);
  }
H
Hongze Cheng 已提交
870 871 872
  return code;
}

H
Hongze Cheng 已提交
873 874 875 876 877 878
static int32_t tsdbSnapWriteTableData(STsdbSnapWriter* pWriter, SRowInfo* pRowInfo) {
  int32_t code = 0;
  int32_t lino = 0;

  // switch to new table if need
  if (pRowInfo == NULL || pRowInfo->uid != pWriter->tbid.uid) {
H
Hongze Cheng 已提交
879
    if (pWriter->tbid.uid) {
H
Hongze Cheng 已提交
880 881
      code = tsdbSnapWriteTableDataEnd(pWriter);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
882
    }
H
Hongze Cheng 已提交
883 884 885

    code = tsdbSnapWriteTableDataStart(pWriter, (TABLEID*)pRowInfo);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
886 887
  }

H
Hongze Cheng 已提交
888
  if (pRowInfo == NULL) goto _exit;
H
Hongze Cheng 已提交
889

H
Hongze Cheng 已提交
890 891
  code = tsdbSnapWriteTableRow(pWriter, &pRowInfo->row);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
892

H
Hongze Cheng 已提交
893 894
_exit:
  if (code) {
H
Hongze Cheng 已提交
895
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->tsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
896
  }
H
Hongze Cheng 已提交
897 898 899
  return code;
}

H
Hongze Cheng 已提交
900
static int32_t tsdbSnapWriteNextRow(STsdbSnapWriter* pWriter, SRowInfo** ppRowInfo) {
H
Hongze Cheng 已提交
901
  int32_t code = 0;
H
Hongze Cheng 已提交
902
  int32_t lino = 0;
H
Hongze Cheng 已提交
903

H
Hongze Cheng 已提交
904
  if (pWriter->pSIter) {
H
Hongze Cheng 已提交
905
    code = tsdbDataIterNext2(pWriter->pSIter, NULL);
H
Hongze Cheng 已提交
906
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
907

H
Hongze Cheng 已提交
908 909
    if (pWriter->pSIter->rowInfo.suid == 0 && pWriter->pSIter->rowInfo.uid == 0) {
      pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
910 911 912
    } else {
      SRBTreeNode* pNode = tRBTreeMin(&pWriter->rbt);
      if (pNode) {
H
Hongze Cheng 已提交
913
        int32_t c = tsdbDataIterCmprFn(&pWriter->pSIter->rbtn, pNode);
H
Hongze Cheng 已提交
914
        if (c > 0) {
H
Hongze Cheng 已提交
915 916
          tRBTreePut(&pWriter->rbt, &pWriter->pSIter->rbtn);
          pWriter->pSIter = NULL;
H
Hongze Cheng 已提交
917 918 919 920 921
        } else if (c == 0) {
          ASSERT(0);
        }
      }
    }
H
Hongze Cheng 已提交
922
  }
H
Hongze Cheng 已提交
923

H
Hongze Cheng 已提交
924
  if (pWriter->pSIter == NULL) {
H
Hongze Cheng 已提交
925 926 927
    SRBTreeNode* pNode = tRBTreeMin(&pWriter->rbt);
    if (pNode) {
      tRBTreeDrop(&pWriter->rbt, pNode);
H
Hongze Cheng 已提交
928
      pWriter->pSIter = TSDB_RBTN_TO_DATA_ITER(pNode);
H
Hongze Cheng 已提交
929
    }
H
Hongze Cheng 已提交
930
  }
H
Hongze Cheng 已提交
931

H
Hongze Cheng 已提交
932
  if (ppRowInfo) {
H
Hongze Cheng 已提交
933 934
    if (pWriter->pSIter) {
      *ppRowInfo = &pWriter->pSIter->rowInfo;
H
Hongze Cheng 已提交
935 936 937
    } else {
      *ppRowInfo = NULL;
    }
H
Hongze Cheng 已提交
938
  }
H
Hongze Cheng 已提交
939

H
Hongze Cheng 已提交
940 941
_exit:
  if (code) {
H
Hongze Cheng 已提交
942
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->tsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
943
  }
H
Hongze Cheng 已提交
944 945 946
  return code;
}

H
Hongze Cheng 已提交
947
static int32_t tsdbSnapWriteGetRow(STsdbSnapWriter* pWriter, SRowInfo** ppRowInfo) {
H
Hongze Cheng 已提交
948
  int32_t code = 0;
H
Hongze Cheng 已提交
949
  int32_t lino = 0;
H
Hongze Cheng 已提交
950

H
Hongze Cheng 已提交
951 952
  if (pWriter->pSIter) {
    *ppRowInfo = &pWriter->pSIter->rowInfo;
H
Hongze Cheng 已提交
953
    goto _exit;
H
Hongze Cheng 已提交
954 955
  }

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

H
Hongze Cheng 已提交
959
_exit:
H
Hongze Cheng 已提交
960
  if (code) {
H
Hongze Cheng 已提交
961
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->tsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
962
  }
H
Hongze Cheng 已提交
963
  return code;
H
Hongze Cheng 已提交
964 965
}

H
Hongze Cheng 已提交
966
static int32_t tsdbSnapWriteFileDataEnd(STsdbSnapWriter* pWriter) {
H
Hongze Cheng 已提交
967
  int32_t code = 0;
H
Hongze Cheng 已提交
968
  int32_t lino = 0;
H
Hongze Cheng 已提交
969

H
Hongze Cheng 已提交
970
  ASSERT(pWriter->pDataFWriter);
H
Hongze Cheng 已提交
971

H
Hongze Cheng 已提交
972 973 974 975 976 977 978
  // consume remain data and end with a NULL table row
  SRowInfo* pRowInfo;
  code = tsdbSnapWriteGetRow(pWriter, &pRowInfo);
  TSDB_CHECK_CODE(code, lino, _exit);
  for (;;) {
    code = tsdbSnapWriteTableData(pWriter, pRowInfo);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
979

H
Hongze Cheng 已提交
980
    if (pRowInfo == NULL) break;
H
Hongze Cheng 已提交
981

H
Hongze Cheng 已提交
982 983 984
    code = tsdbSnapWriteNextRow(pWriter, &pRowInfo);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
985

H
Hongze Cheng 已提交
986 987 988
  // do file-level updates
  code = tsdbWriteSttBlk(pWriter->pDataFWriter, pWriter->aSttBlk);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
989

H
Hongze Cheng 已提交
990 991
  code = tsdbWriteBlockIdx(pWriter->pDataFWriter, pWriter->aBlockIdx);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
992

H
Hongze Cheng 已提交
993 994
  code = tsdbUpdateDFileSetHeader(pWriter->pDataFWriter);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
995

H
Hongze Cheng 已提交
996 997
  code = tsdbFSUpsertFSet(&pWriter->fs, &pWriter->pDataFWriter->wSet);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
998

H
Hongze Cheng 已提交
999 1000
  code = tsdbDataFWriterClose(&pWriter->pDataFWriter, 1);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1001

H
Hongze Cheng 已提交
1002 1003 1004
  if (pWriter->pDataFReader) {
    code = tsdbDataFReaderClose(&pWriter->pDataFReader);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1005
  }
H
Hongze Cheng 已提交
1006

H
Hongze Cheng 已提交
1007 1008 1009 1010 1011 1012
  // clear sources
  while (pWriter->iterList) {
    STsdbDataIter2* pIter = pWriter->iterList;
    pWriter->iterList = pIter->next;
    tsdbCloseDataIter2(pIter);
  }
H
Hongze Cheng 已提交
1013

H
Hongze Cheng 已提交
1014 1015
_exit:
  if (code) {
H
Hongze Cheng 已提交
1016
    tsdbError("vgId:%d %s failed since %s", TD_VID(pWriter->tsdb->pVnode), __func__, tstrerror(code));
H
Hongze Cheng 已提交
1017
  } else {
H
Hongze Cheng 已提交
1018
    tsdbDebug("vgId:%d %s is done", TD_VID(pWriter->tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
1019
  }
H
Hongze Cheng 已提交
1020 1021 1022
  return code;
}

H
Hongze Cheng 已提交
1023
static int32_t tsdbSnapWriteTimeSeriesData(STsdbSnapWriter* pWriter, SSnapDataHdr* pHdr) {
H
Hongze Cheng 已提交
1024
  int32_t code = 0;
H
Hongze Cheng 已提交
1025
  int32_t lino = 0;
H
Hongze Cheng 已提交
1026

H
Hongze Cheng 已提交
1027 1028
  code = tDecmprBlockData(pHdr->data, pHdr->size, &pWriter->inData, pWriter->aBuf);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1029

H
Hongze Cheng 已提交
1030
  ASSERT(pWriter->inData.nRow > 0);
H
Hongze Cheng 已提交
1031

H
Hongze Cheng 已提交
1032 1033 1034 1035
  // switch to new data file if need
  int32_t fid = tsdbKeyFid(pWriter->inData.aTSKEY[0], pWriter->minutes, pWriter->precision);
  if (pWriter->fid != fid) {
    if (pWriter->pDataFWriter) {
H
Hongze Cheng 已提交
1036
      code = tsdbSnapWriteFileDataEnd(pWriter);
H
Hongze Cheng 已提交
1037
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1038 1039
    }

H
Hongze Cheng 已提交
1040
    code = tsdbSnapWriteFileDataStart(pWriter, fid);
H
Hongze Cheng 已提交
1041
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1042 1043
  }

H
Hongze Cheng 已提交
1044 1045 1046 1047
  // loop write each row
  SRowInfo* pRowInfo;
  code = tsdbSnapWriteGetRow(pWriter, &pRowInfo);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1048
  for (int32_t iRow = 0; iRow < pWriter->inData.nRow; ++iRow) {
H
Hongze Cheng 已提交
1049 1050 1051
    SRowInfo rInfo = {.suid = pWriter->inData.suid,
                      .uid = pWriter->inData.uid ? pWriter->inData.uid : pWriter->inData.aUid[iRow],
                      .row = tsdbRowFromBlockData(&pWriter->inData, iRow)};
H
Hongze Cheng 已提交
1052

H
Hongze Cheng 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
    for (;;) {
      if (pRowInfo == NULL) {
        code = tsdbSnapWriteTableData(pWriter, &rInfo);
        TSDB_CHECK_CODE(code, lino, _exit);
        break;
      } else {
        int32_t c = tRowInfoCmprFn(&rInfo, pRowInfo);
        if (c < 0) {
          code = tsdbSnapWriteTableData(pWriter, &rInfo);
          TSDB_CHECK_CODE(code, lino, _exit);
          break;
        } else if (c > 0) {
          code = tsdbSnapWriteTableData(pWriter, pRowInfo);
          TSDB_CHECK_CODE(code, lino, _exit);

          code = tsdbSnapWriteNextRow(pWriter, &pRowInfo);
          TSDB_CHECK_CODE(code, lino, _exit);
        } else {
          ASSERT(0);
        }
      }
    }
H
Hongze Cheng 已提交
1075 1076
  }

H
Hongze Cheng 已提交
1077
_exit:
H
Hongze Cheng 已提交
1078
  if (code) {
H
Hongze Cheng 已提交
1079
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->tsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1080
  } else {
H
Hongze Cheng 已提交
1081
    tsdbDebug("vgId:%d %s done, suid:%" PRId64 " uid:%" PRId64 " nRow:%d", TD_VID(pWriter->tsdb->pVnode), __func__,
H
Hongze Cheng 已提交
1082 1083
              pWriter->inData.suid, pWriter->inData.uid, pWriter->inData.nRow);
  }
H
Hongze Cheng 已提交
1084 1085 1086 1087
  return code;
}

// SNAP_DATA_DEL
H
Hongze Cheng 已提交
1088
static int32_t tsdbSnapWriteDelTableDataStart(STsdbSnapWriter* pWriter, TABLEID* pId) {
H
Hongze Cheng 已提交
1089
  int32_t code = 0;
H
Hongze Cheng 已提交
1090
  int32_t lino = 0;
H
Hongze Cheng 已提交
1091

H
Hongze Cheng 已提交
1092 1093 1094 1095
  if (pId) {
    pWriter->tbid = *pId;
  } else {
    pWriter->tbid = (TABLEID){.suid = INT64_MAX, .uid = INT64_MAX};
H
Hongze Cheng 已提交
1096
  }
H
Hongze Cheng 已提交
1097

H
Hongze Cheng 已提交
1098
  taosArrayClear(pWriter->aDelData);
H
Hongze Cheng 已提交
1099

H
Hongze Cheng 已提交
1100 1101
  if (pWriter->pTIter) {
    while (pWriter->pTIter->tIter.iDelIdx < taosArrayGetSize(pWriter->pTIter->tIter.aDelIdx)) {
H
Hongze Cheng 已提交
1102
      SDelIdx* pDelIdx = taosArrayGet(pWriter->pTIter->tIter.aDelIdx, pWriter->pTIter->tIter.iDelIdx);
H
Hongze Cheng 已提交
1103

H
Hongze Cheng 已提交
1104 1105
      int32_t c = tTABLEIDCmprFn(pDelIdx, &pWriter->tbid);
      if (c < 0) {
H
Haojun Liao 已提交
1106
        code = tsdbReadDelDatav1(pWriter->pDelFReader, pDelIdx, pWriter->pTIter->tIter.aDelData, INT64_MAX);
H
Hongze Cheng 已提交
1107
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1108

H
Hongze Cheng 已提交
1109
        SDelIdx* pDelIdxNew = taosArrayReserve(pWriter->aDelIdx, 1);
H
Hongze Cheng 已提交
1110 1111 1112 1113
        if (pDelIdxNew == NULL) {
          code = TSDB_CODE_OUT_OF_MEMORY;
          TSDB_CHECK_CODE(code, lino, _exit);
        }
H
Hongze Cheng 已提交
1114

H
Hongze Cheng 已提交
1115 1116
        pDelIdxNew->suid = pDelIdx->suid;
        pDelIdxNew->uid = pDelIdx->uid;
H
Hongze Cheng 已提交
1117

H
Hongze Cheng 已提交
1118 1119
        code = tsdbWriteDelData(pWriter->pDelFWriter, pWriter->pTIter->tIter.aDelData, pDelIdxNew);
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1120

H
Hongze Cheng 已提交
1121 1122
        pWriter->pTIter->tIter.iDelIdx++;
      } else if (c == 0) {
H
Haojun Liao 已提交
1123
        code = tsdbReadDelDatav1(pWriter->pDelFReader, pDelIdx, pWriter->aDelData, INT64_MAX);
H
Hongze Cheng 已提交
1124
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1125

H
Hongze Cheng 已提交
1126 1127 1128 1129
        pWriter->pTIter->tIter.iDelIdx++;
        break;
      } else {
        break;
H
Hongze Cheng 已提交
1130
      }
H
Hongze Cheng 已提交
1131 1132 1133
    }
  }

H
Hongze Cheng 已提交
1134
_exit:
H
Hongze Cheng 已提交
1135
  if (code) {
H
Hongze Cheng 已提交
1136
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->tsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1137
  } else {
H
Hongze Cheng 已提交
1138
    tsdbTrace("vgId:%d %s done, suid:%" PRId64 " uid:%" PRId64, TD_VID(pWriter->tsdb->pVnode), __func__,
H
Hongze Cheng 已提交
1139
              pWriter->tbid.suid, pWriter->tbid.uid);
H
Hongze Cheng 已提交
1140
  }
H
Hongze Cheng 已提交
1141 1142 1143
  return code;
}

H
Hongze Cheng 已提交
1144
static int32_t tsdbSnapWriteDelTableDataEnd(STsdbSnapWriter* pWriter) {
H
Hongze Cheng 已提交
1145
  int32_t code = 0;
H
Hongze Cheng 已提交
1146
  int32_t lino = 0;
H
Hongze Cheng 已提交
1147

H
Hongze Cheng 已提交
1148 1149 1150
  if (taosArrayGetSize(pWriter->aDelData) > 0) {
    SDelIdx* pDelIdx = taosArrayReserve(pWriter->aDelIdx, 1);
    if (pDelIdx == NULL) {
H
Hongze Cheng 已提交
1151
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1152
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1153 1154
    }

H
Hongze Cheng 已提交
1155 1156
    pDelIdx->suid = pWriter->tbid.suid;
    pDelIdx->uid = pWriter->tbid.uid;
H
Hongze Cheng 已提交
1157

H
Hongze Cheng 已提交
1158 1159
    code = tsdbWriteDelData(pWriter->pDelFWriter, pWriter->aDelData, pDelIdx);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1160 1161 1162
  }

_exit:
H
Hongze Cheng 已提交
1163
  if (code) {
H
Hongze Cheng 已提交
1164
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->tsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1165
  } else {
H
Hongze Cheng 已提交
1166
    tsdbTrace("vgId:%d %s done", TD_VID(pWriter->tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
1167
  }
H
Hongze Cheng 已提交
1168 1169 1170
  return code;
}

H
Hongze Cheng 已提交
1171
static int32_t tsdbSnapWriteDelTableData(STsdbSnapWriter* pWriter, TABLEID* pId, uint8_t* pData, int64_t size) {
H
Hongze Cheng 已提交
1172
  int32_t code = 0;
H
Hongze Cheng 已提交
1173
  int32_t lino = 0;
H
Hongze Cheng 已提交
1174

H
Hongze Cheng 已提交
1175 1176 1177 1178
  if (pId == NULL || pId->uid != pWriter->tbid.uid) {
    if (pWriter->tbid.uid) {
      code = tsdbSnapWriteDelTableDataEnd(pWriter);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1179 1180
    }

H
Hongze Cheng 已提交
1181 1182
    code = tsdbSnapWriteDelTableDataStart(pWriter, pId);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1183 1184
  }

H
Hongze Cheng 已提交
1185
  if (pId == NULL) goto _exit;
H
Hongze Cheng 已提交
1186

H
Hongze Cheng 已提交
1187 1188 1189 1190
  int64_t n = 0;
  while (n < size) {
    SDelData delData;
    n += tGetDelData(pData + n, &delData);
H
Hongze Cheng 已提交
1191

H
Hongze Cheng 已提交
1192
    if (taosArrayPush(pWriter->aDelData, &delData) == NULL) {
H
Hongze Cheng 已提交
1193 1194 1195 1196 1197
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  }
  ASSERT(n == size);
H
Hongze Cheng 已提交
1198

H
Hongze Cheng 已提交
1199 1200
_exit:
  if (code) {
H
Hongze Cheng 已提交
1201
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->tsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1202
  }
H
Hongze Cheng 已提交
1203 1204
  return code;
}
H
Hongze Cheng 已提交
1205

H
Hongze Cheng 已提交
1206 1207 1208
static int32_t tsdbSnapWriteDelDataStart(STsdbSnapWriter* pWriter) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1209

H
Hongze Cheng 已提交
1210
  STsdb*    pTsdb = pWriter->tsdb;
H
Hongze Cheng 已提交
1211
  SDelFile* pDelFile = pWriter->fs.pDelFile;
H
Hongze Cheng 已提交
1212

H
Hongze Cheng 已提交
1213
  pWriter->tbid = (TABLEID){0};
H
Hongze Cheng 已提交
1214

H
Hongze Cheng 已提交
1215 1216 1217 1218
  // reader
  if (pDelFile) {
    code = tsdbDelFReaderOpen(&pWriter->pDelFReader, pDelFile, pTsdb);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1219

H
Hongze Cheng 已提交
1220 1221
    code = tsdbOpenTombFileDataIter(pWriter->pDelFReader, &pWriter->pTIter);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1222
  }
H
Hongze Cheng 已提交
1223

H
Hongze Cheng 已提交
1224 1225 1226
  // writer
  code = tsdbDelFWriterOpen(&pWriter->pDelFWriter, &(SDelFile){.commitID = pWriter->commitID}, pTsdb);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1227

H
Hongze Cheng 已提交
1228
  if ((pWriter->aDelIdx = taosArrayInit(0, sizeof(SDelIdx))) == NULL) {
H
Hongze Cheng 已提交
1229
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1230 1231 1232 1233 1234
    TSDB_CHECK_CODE(code, lino, _exit);
  }
  if ((pWriter->aDelData = taosArrayInit(0, sizeof(SDelData))) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1235 1236
  }

H
Hongze Cheng 已提交
1237 1238 1239 1240 1241 1242
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbDebug("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
  }
H
Hongze Cheng 已提交
1243 1244 1245
  return code;
}

H
Hongze Cheng 已提交
1246
static int32_t tsdbSnapWriteDelDataEnd(STsdbSnapWriter* pWriter) {
H
Hongze Cheng 已提交
1247
  int32_t code = 0;
H
Hongze Cheng 已提交
1248
  int32_t lino = 0;
H
Hongze Cheng 已提交
1249

H
Hongze Cheng 已提交
1250
  STsdb* pTsdb = pWriter->tsdb;
H
Hongze Cheng 已提交
1251

H
Hongze Cheng 已提交
1252
  // end remaining table with NULL data
H
Hongze Cheng 已提交
1253 1254
  code = tsdbSnapWriteDelTableData(pWriter, NULL, NULL, 0);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1255

H
Hongze Cheng 已提交
1256 1257 1258
  // update file-level info
  code = tsdbWriteDelIdx(pWriter->pDelFWriter, pWriter->aDelIdx);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1259

H
Hongze Cheng 已提交
1260
  code = tsdbUpdateDelFileHdr(pWriter->pDelFWriter);
H
Hongze Cheng 已提交
1261
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1262

H
Hongze Cheng 已提交
1263
  code = tsdbFSUpsertDelFile(&pWriter->fs, &pWriter->pDelFWriter->fDel);
H
Hongze Cheng 已提交
1264
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1265 1266

  code = tsdbDelFWriterClose(&pWriter->pDelFWriter, 1);
H
Hongze Cheng 已提交
1267
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1268 1269 1270

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

H
Hongze Cheng 已提交
1274 1275 1276
  if (pWriter->pTIter) {
    tsdbCloseDataIter2(pWriter->pTIter);
    pWriter->pTIter = NULL;
H
Hongze Cheng 已提交
1277 1278
  }

H
Hongze Cheng 已提交
1279 1280 1281 1282 1283 1284
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbInfo("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
  }
H
Hongze Cheng 已提交
1285
  return code;
H
Hongze Cheng 已提交
1286
}
H
Hongze Cheng 已提交
1287

H
Hongze Cheng 已提交
1288 1289 1290 1291
static int32_t tsdbSnapWriteDelData(STsdbSnapWriter* pWriter, SSnapDataHdr* pHdr) {
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1292
  STsdb* pTsdb = pWriter->tsdb;
H
Hongze Cheng 已提交
1293 1294 1295 1296 1297 1298 1299

  // start to write del data if need
  if (pWriter->pDelFWriter == NULL) {
    code = tsdbSnapWriteDelDataStart(pWriter);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
1300
  // do write del data
H
Hongze Cheng 已提交
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
  code = tsdbSnapWriteDelTableData(pWriter, (TABLEID*)pHdr->data, pHdr->data + sizeof(TABLEID),
                                   pHdr->size - sizeof(TABLEID));
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed since %s", TD_VID(pTsdb->pVnode), __func__, tstrerror(code));
  } else {
    tsdbTrace("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
  }
H
Hongze Cheng 已提交
1311 1312
  return code;
}
H
Hongze Cheng 已提交
1313
#endif
H
Hongze Cheng 已提交
1314

H
Hongze Cheng 已提交
1315
// APIs
H
Hongze Cheng 已提交
1316
int32_t tsdbSnapWriterOpen(STsdb* pTsdb, int64_t sver, int64_t ever, STsdbSnapWriter** ppWriter) {
H
Hongze Cheng 已提交
1317 1318
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1319

H
Hongze Cheng 已提交
1320
#if 0
H
Hongze Cheng 已提交
1321
  // alloc
H
Hongze Cheng 已提交
1322
  STsdbSnapWriter* pWriter = (STsdbSnapWriter*)taosMemoryCalloc(1, sizeof(*pWriter));
H
Hongze Cheng 已提交
1323 1324
  if (pWriter == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1325
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1326
  }
H
Hongze Cheng 已提交
1327
  pWriter->tsdb = pTsdb;
H
Hongze Cheng 已提交
1328 1329
  pWriter->sver = sver;
  pWriter->ever = ever;
H
Hongze Cheng 已提交
1330 1331 1332 1333 1334
  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 已提交
1335 1336
  pWriter->commitID = pTsdb->pVnode->state.commitID;

H
Hongze Cheng 已提交
1337 1338 1339
  code = tsdbFSCopy(pTsdb, &pWriter->fs);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
1340
  // SNAP_DATA_TSDB
H
Hongze Cheng 已提交
1341
  code = tBlockDataCreate(&pWriter->inData);
H
Hongze Cheng 已提交
1342
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1343

H
Hongze Cheng 已提交
1344
  pWriter->fid = INT32_MIN;
H
Hongze Cheng 已提交
1345

H
Hongze Cheng 已提交
1346
  code = tBlockDataCreate(&pWriter->bData);
H
Hongze Cheng 已提交
1347
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1348 1349

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

H
Hongze Cheng 已提交
1352
  // SNAP_DATA_DEL
H
Hongze Cheng 已提交
1353
#endif
H
Hongze Cheng 已提交
1354

H
Hongze Cheng 已提交
1355 1356
_exit:
  if (code) {
H
Hongze Cheng 已提交
1357
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1358 1359 1360 1361 1362 1363 1364 1365
    // if (pWriter) {
    //   tBlockDataDestroy(&pWriter->sData);
    //   tBlockDataDestroy(&pWriter->bData);
    //   tBlockDataDestroy(&pWriter->inData);
    //   tsdbFSDestroy(&pWriter->fs);
    //   taosMemoryFree(pWriter);
    //   pWriter = NULL;
    // }
H
Hongze Cheng 已提交
1366
  } else {
H
Hongze Cheng 已提交
1367
    tsdbInfo("vgId:%d %s done, sver:%" PRId64 " ever:%" PRId64, TD_VID(pTsdb->pVnode), __func__, sver, ever);
H
Hongze Cheng 已提交
1368
  }
H
Hongze Cheng 已提交
1369
  // *ppWriter = pWriter;
H
Hongze Cheng 已提交
1370 1371 1372
  return code;
}

H
Hongze Cheng 已提交
1373
int32_t tsdbSnapWriterPrepareClose(STsdbSnapWriter* writer) {
H
Hongze Cheng 已提交
1374
  int32_t code = 0;
H
Hongze Cheng 已提交
1375 1376
  int32_t lino = 0;

H
Hongze Cheng 已提交
1377
#if 0
H
Hongze Cheng 已提交
1378
  if (pWriter->pDataFWriter) {
H
Hongze Cheng 已提交
1379
    code = tsdbSnapWriteFileDataEnd(pWriter);
H
Hongze Cheng 已提交
1380
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1381 1382
  }

H
Hongze Cheng 已提交
1383 1384 1385 1386
  if (pWriter->pDelFWriter) {
    code = tsdbSnapWriteDelDataEnd(pWriter);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
1387

H
Hongze Cheng 已提交
1388
  code = tsdbFSPrepareCommit(pWriter->tsdb, &pWriter->fs);
H
Hongze Cheng 已提交
1389
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1390
#endif
H
Hongze Cheng 已提交
1391 1392 1393

_exit:
  if (code) {
H
Hongze Cheng 已提交
1394
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(writer->tsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1395
  } else {
H
Hongze Cheng 已提交
1396
    tsdbDebug("vgId:%d %s done", TD_VID(writer->tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
1397 1398 1399 1400
  }
  return code;
}

H
Hongze Cheng 已提交
1401
int32_t tsdbSnapWriterClose(STsdbSnapWriter** writer, int8_t rollback) {
H
Hongze Cheng 已提交
1402 1403 1404
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1405 1406 1407
#if 0
  STsdbSnapWriter* pWriter = *writer;
  STsdb*           pTsdb = pWriter->tsdb;
H
Hongze Cheng 已提交
1408 1409

  if (rollback) {
H
Hongze Cheng 已提交
1410
    tsdbRollbackCommit(pWriter->tsdb);
H
Hongze Cheng 已提交
1411
  } else {
H
Hongze Cheng 已提交
1412 1413 1414
    // lock
    taosThreadRwlockWrlock(&pTsdb->rwLock);

H
Hongze Cheng 已提交
1415
    code = tsdbFSCommit(pWriter->tsdb);
H
Hongze Cheng 已提交
1416 1417
    if (code) {
      taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1418
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1419 1420 1421 1422
    }

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

H
Hongze Cheng 已提交
1425 1426
  // SNAP_DATA_DEL
  taosArrayDestroy(pWriter->aDelData);
H
Hongze Cheng 已提交
1427
  taosArrayDestroy(pWriter->aDelIdx);
H
Hongze Cheng 已提交
1428 1429

  // SNAP_DATA_TSDB
1430
  tBlockDataDestroy(&pWriter->sData);
H
Hongze Cheng 已提交
1431
  tBlockDataDestroy(&pWriter->bData);
H
Hongze Cheng 已提交
1432 1433 1434
  taosArrayDestroy(pWriter->aSttBlk);
  tMapDataClear(&pWriter->mDataBlk);
  taosArrayDestroy(pWriter->aBlockIdx);
1435
  tDestroyTSchema(pWriter->skmTable.pTSchema);
1436
  tBlockDataDestroy(&pWriter->inData);
H
Hongze Cheng 已提交
1437

H
Hongze Cheng 已提交
1438 1439 1440
  for (int32_t iBuf = 0; iBuf < sizeof(pWriter->aBuf) / sizeof(uint8_t*); iBuf++) {
    tFree(pWriter->aBuf[iBuf]);
  }
H
Hongze Cheng 已提交
1441
  tsdbFSDestroy(&pWriter->fs);
H
Hongze Cheng 已提交
1442
  taosMemoryFree(pWriter);
H
Hongze Cheng 已提交
1443 1444
  *writer = NULL;
#endif
H
Hongze Cheng 已提交
1445

H
Hongze Cheng 已提交
1446 1447
_exit:
  if (code) {
H
Hongze Cheng 已提交
1448
    // tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1449
  } else {
H
Hongze Cheng 已提交
1450
    // tsdbInfo("vgId:%d %s done", TD_VID(pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
1451
  }
H
Hongze Cheng 已提交
1452 1453 1454
  return code;
}

H
Hongze Cheng 已提交
1455
static int32_t tsdbSnapWriteDoWriteTimeSeriesRow(STsdbSnapWriter* writer, const SRowInfo* row) {
H
Hongze Cheng 已提交
1456 1457
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1458

H
Hongze Cheng 已提交
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
  // TODO

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
  }
  return code;
}

static int32_t tsdbSnapWriteTimeSeriesRow(STsdbSnapWriter* writer, const SRowInfo* row) {
  int32_t code = 0;
  int32_t lino = 0;

  while (writer->ctx->hasData) {
    SRowInfo* row1 = tsdbIterMergerGetData(writer->ctx->dataIterMerger);
    if (row1 == NULL) {
      writer->ctx->hasData = false;
      break;
    }

    int32_t c = tRowInfoCmprFn(row1, row);
    if (c <= 0) {
      code = tsdbSnapWriteDoWriteTimeSeriesRow(writer, row1);
      TSDB_CHECK_CODE(code, lino, _exit);

      code = tsdbIterMergerNext(writer->ctx->dataIterMerger);
      TSDB_CHECK_CODE(code, lino, _exit);
    } else {
      break;
    }
  }

  if (row->suid == INT64_MAX) {
    ASSERT(writer->ctx->hasData == false);
H
Hongze Cheng 已提交
1493
    goto _exit;
H
Hongze Cheng 已提交
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
  }

  code = tsdbSnapWriteDoWriteTimeSeriesRow(writer, row);
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
  }
  return code;
}

static int32_t tsdbSnapWriteFileSetBegin(STsdbSnapWriter* writer, int32_t fid) {
  int32_t code = 0;
  int32_t lino = 0;

  // TODO

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
  }
  return code;
}

static int32_t tsdbSnapWriteTombRecord(STsdbSnapWriter* writer, const STombRecord* record) {
  int32_t code = 0;
  int32_t lino = 0;

  // TODO

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
  }
  return code;
}

static int32_t tsdbSnapWriteFileSetEnd(STsdbSnapWriter* writer) {
  if (!writer->ctx->fsetWriteBegin) return 0;

  int32_t code = 0;
  int32_t lino = 0;

  // TODO
  SRowInfo row = {
      .suid = INT64_MAX,
      .uid = INT64_MAX,
  };

  code = tsdbSnapWriteTimeSeriesRow(writer, &row);
  TSDB_CHECK_CODE(code, lino, _exit);

  STombRecord record = {
      .suid = INT64_MAX,
      .uid = INT64_MAX,
  };

  code = tsdbSnapWriteTombRecord(writer, &record);
  TSDB_CHECK_CODE(code, lino, _exit);

  // close write
  code = tsdbSttFileWriterClose(&writer->sttWriter, 0, writer->fopArr);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbDataFileWriterClose(&writer->dataWriter, 0, writer->fopArr);
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
  }
  return code;
}

static int32_t tsdbSnapWriteTimeSeriesData(STsdbSnapWriter* writer, SSnapDataHdr* hdr) {
  int32_t code = 0;
  int32_t lino = 0;

  SBlockData blockData[1] = {0};

  code = tDecmprBlockData(hdr->data, hdr->size, blockData, writer->aBuf);
  TSDB_CHECK_CODE(code, lino, _exit);

  int32_t fid = tsdbKeyFid(blockData->aTSKEY[0], writer->minutes, writer->precision);
  if (fid != writer->ctx->fid) {
    code = tsdbSnapWriteFileSetEnd(writer);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = tsdbSnapWriteFileSetBegin(writer, fid);
H
Hongze Cheng 已提交
1584
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1585 1586
  }

H
Hongze Cheng 已提交
1587 1588 1589 1590 1591 1592 1593 1594
  for (int32_t i = 0; i < blockData->nRow; ++i) {
    SRowInfo rowInfo = {
        .suid = blockData->suid,
        .uid = blockData->uid ? blockData->uid : blockData->aUid[i],
        .row = tsdbRowFromBlockData(blockData, i),
    };

    code = tsdbSnapWriteTimeSeriesRow(writer, &rowInfo);
H
Hongze Cheng 已提交
1595
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
  }

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
  } else {
    tsdbDebug("vgId:%d %s done, suid:%" PRId64 " uid:%" PRId64 " nRow:%d", TD_VID(writer->tsdb->pVnode), __func__,
              blockData->suid, blockData->uid, blockData->nRow);
  }
  tBlockDataDestroy(blockData);
  return code;
}

static int32_t tsdbSnapWriteDecmprTombBlock(SSnapDataHdr* hdr, STombBlock* tombBlock) {
  int32_t code = 0;
  int32_t lino = 0;

  // TODO

_exit:
  return code;
}

static int32_t tsdbSnapWriteTombData(STsdbSnapWriter* writer, SSnapDataHdr* hdr) {
  int32_t code = 0;
  int32_t lino = 0;

  STombBlock tombBlock[1] = {0};

  code = tsdbSnapWriteDecmprTombBlock(hdr, tombBlock);
  TSDB_CHECK_CODE(code, lino, _exit);

  for (int32_t i = 0; i < TOMB_BLOCK_SIZE(tombBlock); ++i) {
    STombRecord record;
    tTombBlockGet(tombBlock, i, &record);

    code = tsdbSnapWriteTombRecord(writer, &record);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  tTombBlockDestroy(tombBlock);

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
  }
  return code;
}

int32_t tsdbSnapWrite(STsdbSnapWriter* writer, SSnapDataHdr* hdr) {
  int32_t code = 0;
  int32_t lino = 0;

  if (hdr->type == SNAP_DATA_TSDB) {
    code = tsdbSnapWriteTimeSeriesData(writer, hdr);
    TSDB_CHECK_CODE(code, lino, _exit);
  } else if (hdr->type == SNAP_DATA_DEL) {
    code = tsdbSnapWriteTombData(writer, hdr);
    TSDB_CHECK_CODE(code, lino, _exit);
  } else {
    ASSERT(0);
H
Hongze Cheng 已提交
1657 1658
  }

H
Hongze Cheng 已提交
1659
_exit:
H
Hongze Cheng 已提交
1660 1661
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s, type:%d index:%" PRId64 " size:%" PRId64,
H
Hongze Cheng 已提交
1662
              TD_VID(writer->tsdb->pVnode), __func__, lino, tstrerror(code), hdr->type, hdr->index, hdr->size);
H
Hongze Cheng 已提交
1663
  } else {
H
Hongze Cheng 已提交
1664 1665
    tsdbDebug("vgId:%d %s done, type:%d index:%" PRId64 " size:%" PRId64, TD_VID(writer->tsdb->pVnode), __func__,
              hdr->type, hdr->index, hdr->size);
H
Hongze Cheng 已提交
1666
  }
H
Hongze Cheng 已提交
1667 1668
  return code;
}