tsdbSnapshot.c 31.0 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
#include "tsdbDataFileRW.h"
#include "tsdbFS2.h"
H
Hongze Cheng 已提交
19
#include "tsdbFSetRW.h"
H
Hongze Cheng 已提交
20 21
#include "tsdbIter.h"
#include "tsdbSttFileRW.h"
H
Hongze Cheng 已提交
22

H
Hongze Cheng 已提交
23
extern int32_t tsdbUpdateTableSchema(SMeta* pMeta, int64_t suid, int64_t uid, SSkmInfo* pSkmInfo);
H
Hongze Cheng 已提交
24

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

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

  TFileSetArray* fsetArr;

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

H
Hongze Cheng 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57
  // 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 已提交
58
};
H
Hongze Cheng 已提交
59

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

H
Hongze Cheng 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  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 已提交
80
  }
H
Hongze Cheng 已提交
81

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

H
Hongze Cheng 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  // 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 已提交
102

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

H
Hongze Cheng 已提交
108 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
_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 已提交
153

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

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

H
Hongze Cheng 已提交
161 162 163 164 165
    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 已提交
166 167
  }

H
Hongze Cheng 已提交
168 169 170 171 172 173 174 175
  // 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 已提交
176
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
177

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

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

H
Hongze Cheng 已提交
185 186 187 188 189
    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 已提交
190 191
  }

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

H
Hongze Cheng 已提交
196
  code = tsdbIterMergerOpen(reader->tombIterArr, &reader->tombIterMerger, true);
H
Hongze Cheng 已提交
197
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
198

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

H
Hongze Cheng 已提交
206 207 208 209 210 211
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 已提交
212
}
H
Hongze Cheng 已提交
213

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

H
Hongze Cheng 已提交
218 219
  ASSERT(reader->ctx->fset == NULL);

H
Hongze Cheng 已提交
220 221 222 223
  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 已提交
224

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

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
286
  tBlockDataReset(reader->blockData);
H
Hongze Cheng 已提交
287

H
Hongze Cheng 已提交
288
  TABLEID tbid[1] = {0};
H
Hongze Cheng 已提交
289
  for (SRowInfo* row; (row = tsdbIterMergerGetData(reader->dataIterMerger));) {
H
Hongze Cheng 已提交
290 291 292 293 294 295 296 297 298 299 300
    // skip dropped table
    if (row->uid != tbid->uid) {
      tbid->suid = row->suid;
      tbid->uid = row->uid;
      if (metaGetInfo(reader->tsdb->pVnode->pMeta, tbid->uid, &info, NULL) != 0) {
        code = tsdbIterMergerSkipTableData(reader->dataIterMerger, tbid);
        TSDB_CHECK_CODE(code, lino, _exit);
        continue;
      }
    }

H
Hongze Cheng 已提交
301 302 303
    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 已提交
304

H
Hongze Cheng 已提交
305
      TABLEID tbid1 = {
H
Hongze Cheng 已提交
306 307 308
          .suid = row->suid,
          .uid = row->suid ? 0 : row->uid,
      };
H
Hongze Cheng 已提交
309
      code = tBlockDataInit(reader->blockData, &tbid1, reader->skmTb->pTSchema, NULL, 0);
H
add log  
Hongze Cheng 已提交
310
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
311 312
    }

H
Hongze Cheng 已提交
313 314
    if (!TABLE_SAME_SCHEMA(reader->blockData->suid, reader->blockData->uid, row->suid, row->uid)) {
      break;
H
Hongze Cheng 已提交
315 316
    }

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

H
Hongze Cheng 已提交
320
    code = tsdbIterMergerNext(reader->dataIterMerger);
H
add log  
Hongze Cheng 已提交
321
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
322

H
Hongze Cheng 已提交
323 324 325
    if (reader->blockData->nRow >= 81920) {
      break;
    }
H
Hongze Cheng 已提交
326
  }
H
Hongze Cheng 已提交
327

H
Hongze Cheng 已提交
328 329 330
  if (reader->blockData->nRow > 0) {
    ASSERT(reader->blockData->suid || reader->blockData->uid);
    code = tsdbSnapCmprData(reader, data);
H
add log  
Hongze Cheng 已提交
331
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
332 333
  }

H
add log  
Hongze Cheng 已提交
334 335
_exit:
  if (code) {
H
Hongze Cheng 已提交
336
    TSDB_ERROR_LOG(TD_VID(reader->tsdb->pVnode), code, lino);
H
add log  
Hongze Cheng 已提交
337
  }
H
Hongze Cheng 已提交
338 339 340
  return code;
}

H
Hongze Cheng 已提交
341
static int32_t tsdbSnapCmprTombData(STsdbSnapReader* reader, uint8_t** data) {
H
add log  
Hongze Cheng 已提交
342 343 344
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
345
  int64_t size = 0;
H
Hongze Cheng 已提交
346 347
  for (int32_t i = 0; i < ARRAY_SIZE(reader->tombBlock->dataArr); i++) {
    size += TARRAY2_DATA_LEN(reader->tombBlock->dataArr + i);
H
Hongze Cheng 已提交
348
  }
H
Hongze Cheng 已提交
349

H
Hongze Cheng 已提交
350
  data[0] = taosMemoryMalloc(size + sizeof(SSnapDataHdr));
H
Hongze Cheng 已提交
351
  if (data[0] == NULL) {
H
Hongze Cheng 已提交
352
    code = TSDB_CODE_OUT_OF_MEMORY;
H
add log  
Hongze Cheng 已提交
353
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
354
  }
H
Hongze Cheng 已提交
355

H
Hongze Cheng 已提交
356
  SSnapDataHdr* hdr = (SSnapDataHdr*)(data[0]);
H
Hongze Cheng 已提交
357 358
  hdr->type = SNAP_DATA_DEL;
  hdr->size = size;
H
Hongze Cheng 已提交
359

H
Hongze Cheng 已提交
360
  uint8_t* tdata = hdr->data;
H
Hongze Cheng 已提交
361
  for (int32_t i = 0; i < ARRAY_SIZE(reader->tombBlock->dataArr); i++) {
H
Hongze Cheng 已提交
362 363
    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 已提交
364 365
  }

H
Hongze Cheng 已提交
366 367
_exit:
  if (code) {
H
Hongze Cheng 已提交
368
    TSDB_ERROR_LOG(TD_VID(reader->tsdb->pVnode), code, lino);
H
Hongze Cheng 已提交
369 370 371
  }
  return code;
}
H
Hongze Cheng 已提交
372

H
Hongze Cheng 已提交
373
static int32_t tsdbSnapReadTombData(STsdbSnapReader* reader, uint8_t** data) {
H
Hongze Cheng 已提交
374 375 376
  int32_t   code = 0;
  int32_t   lino = 0;
  SMetaInfo info;
H
Hongze Cheng 已提交
377

H
Hongze Cheng 已提交
378
  tTombBlockClear(reader->tombBlock);
H
Hongze Cheng 已提交
379

H
Hongze Cheng 已提交
380
  TABLEID tbid[1] = {0};
H
Hongze Cheng 已提交
381
  for (STombRecord* record; (record = tsdbIterMergerGetTombRecord(reader->tombIterMerger)) != NULL;) {
H
Hongze Cheng 已提交
382 383 384 385
    if (record->uid != tbid->uid) {
      tbid->suid = record->suid;
      tbid->uid = record->uid;
      if (metaGetInfo(reader->tsdb->pVnode->pMeta, tbid->uid, &info, NULL) != 0) {
H
Hongze Cheng 已提交
386
        code = tsdbIterMergerSkipTableData(reader->tombIterMerger, tbid);
H
Hongze Cheng 已提交
387 388 389 390 391
        TSDB_CHECK_CODE(code, lino, _exit);
        continue;
      }
    }

H
Hongze Cheng 已提交
392
    code = tTombBlockPut(reader->tombBlock, record);
H
add log  
Hongze Cheng 已提交
393
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
394

H
Hongze Cheng 已提交
395 396 397
    code = tsdbIterMergerNext(reader->tombIterMerger);
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
398 399
    if (TOMB_BLOCK_SIZE(reader->tombBlock) >= 81920) {
      break;
H
Hongze Cheng 已提交
400
    }
H
Hongze Cheng 已提交
401 402
  }

H
Hongze Cheng 已提交
403 404
  if (TOMB_BLOCK_SIZE(reader->tombBlock) > 0) {
    code = tsdbSnapCmprTombData(reader, data);
H
Hongze Cheng 已提交
405
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
406
  }
H
Hongze Cheng 已提交
407 408

_exit:
H
add log  
Hongze Cheng 已提交
409
  if (code) {
H
Hongze Cheng 已提交
410
    TSDB_ERROR_LOG(TD_VID(reader->tsdb->pVnode), code, lino);
H
add log  
Hongze Cheng 已提交
411
  }
H
Hongze Cheng 已提交
412 413
  return code;
}
H
more  
Hongze Cheng 已提交
414

H
Hongze Cheng 已提交
415
int32_t tsdbSnapReaderOpen(STsdb* tsdb, int64_t sver, int64_t ever, int8_t type, STsdbSnapReader** reader) {
H
Hongze Cheng 已提交
416 417
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
418

H
Hongze Cheng 已提交
419 420
  reader[0] = (STsdbSnapReader*)taosMemoryCalloc(1, sizeof(*reader[0]));
  if (reader[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;
H
more  
Hongze Cheng 已提交
421

H
Hongze Cheng 已提交
422 423 424 425
  reader[0]->tsdb = tsdb;
  reader[0]->sver = sver;
  reader[0]->ever = ever;
  reader[0]->type = type;
H
Hongze Cheng 已提交
426

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

H
Hongze Cheng 已提交
430 431
_exit:
  if (code) {
H
Hongze Cheng 已提交
432
    tsdbError("vgId:%d %s failed at line %d since %s, sver:%" PRId64 " ever:%" PRId64 " type:%d", TD_VID(tsdb->pVnode),
H
Hongze Cheng 已提交
433
              __func__, lino, tstrerror(code), sver, ever, type);
H
Hongze Cheng 已提交
434 435 436
    tsdbFSDestroyRefSnapshot(&reader[0]->fsetArr);
    taosMemoryFree(reader[0]);
    reader[0] = NULL;
H
Hongze Cheng 已提交
437
  } else {
H
Hongze Cheng 已提交
438
    tsdbInfo("vgId:%d %s done, sver:%" PRId64 " ever:%" PRId64 " type:%d", TD_VID(tsdb->pVnode), __func__, sver, ever,
H
Hongze Cheng 已提交
439
             type);
H
Hongze Cheng 已提交
440
  }
H
more  
Hongze Cheng 已提交
441
  return code;
H
Hongze Cheng 已提交
442 443
}

H
Hongze Cheng 已提交
444 445 446
int32_t tsdbSnapReaderClose(STsdbSnapReader** reader) {
  if (reader[0] == NULL) return 0;

H
Hongze Cheng 已提交
447 448
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
449

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

H
Hongze Cheng 已提交
452 453
  tTombBlockDestroy(reader[0]->tombBlock);
  tBlockDataDestroy(reader[0]->blockData);
H
Hongze Cheng 已提交
454

H
Hongze Cheng 已提交
455 456 457 458 459 460
  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 已提交
461

H
Hongze Cheng 已提交
462 463 464
  tsdbFSDestroyRefSnapshot(&reader[0]->fsetArr);
  tDestroyTSchema(reader[0]->skmTb->pTSchema);

H
Hongze Cheng 已提交
465
  for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->aBuf); ++i) {
H
Hongze Cheng 已提交
466
    tFree(reader[0]->aBuf[i]);
H
Hongze Cheng 已提交
467
  }
H
Hongze Cheng 已提交
468 469 470

  taosMemoryFree(reader[0]);
  reader[0] = NULL;
H
Hongze Cheng 已提交
471 472 473

_exit:
  if (code) {
H
Hongze Cheng 已提交
474
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
475
  } else {
H
Hongze Cheng 已提交
476
    tsdbDebug("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
477
  }
H
Hongze Cheng 已提交
478 479 480
  return code;
}

H
Hongze Cheng 已提交
481
int32_t tsdbSnapRead(STsdbSnapReader* reader, uint8_t** data) {
H
more  
Hongze Cheng 已提交
482
  int32_t code = 0;
H
add log  
Hongze Cheng 已提交
483
  int32_t lino = 0;
H
Hongze Cheng 已提交
484

H
Hongze Cheng 已提交
485
  data[0] = NULL;
H
Hongze Cheng 已提交
486

H
Hongze Cheng 已提交
487 488 489 490 491 492 493 494
  for (;;) {
    if (reader->ctx->fset == NULL) {
      code = tsdbSnapReadFileSetBegin(reader);
      TSDB_CHECK_CODE(code, lino, _exit);

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

H
Hongze Cheng 已提交
497 498 499 500 501 502 503 504
    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 已提交
505
    }
H
Hongze Cheng 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518

    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 已提交
519
  }
H
Hongze Cheng 已提交
520

H
Hongze Cheng 已提交
521
_exit:
H
add log  
Hongze Cheng 已提交
522
  if (code) {
H
Hongze Cheng 已提交
523
    TSDB_ERROR_LOG(TD_VID(reader->tsdb->pVnode), code, lino);
H
add log  
Hongze Cheng 已提交
524
  } else {
H
Hongze Cheng 已提交
525
    tsdbDebug("vgId:%d %s done", TD_VID(reader->tsdb->pVnode), __func__);
H
add log  
Hongze Cheng 已提交
526
  }
H
more  
Hongze Cheng 已提交
527 528 529
  return code;
}

H
Hongze Cheng 已提交
530
// STsdbSnapWriter ========================================
H
Hongze Cheng 已提交
531
struct STsdbSnapWriter {
H
Hongze Cheng 已提交
532
  STsdb*   tsdb;
H
Hongze Cheng 已提交
533 534
  int64_t  sver;
  int64_t  ever;
H
Hongze Cheng 已提交
535 536 537 538 539 540
  int32_t  minutes;
  int8_t   precision;
  int32_t  minRow;
  int32_t  maxRow;
  int8_t   cmprAlg;
  int64_t  commitID;
H
Hongze Cheng 已提交
541 542 543
  int32_t  szPage;
  int64_t  compactVersion;
  int64_t  now;
H
Hongze Cheng 已提交
544
  uint8_t* aBuf[5];
H
Hongze Cheng 已提交
545

H
Hongze Cheng 已提交
546 547 548 549
  TFileSetArray* fsetArr;
  TFileOpArray   fopArr[1];

  struct {
H
Hongze Cheng 已提交
550
    bool       fsetWriteBegin;
H
Hongze Cheng 已提交
551 552
    int32_t    fid;
    STFileSet* fset;
H
Hongze Cheng 已提交
553
    SDiskID    did;
H
Hongze Cheng 已提交
554 555
    bool       hasData;  // if have time series data
    bool       hasTomb;  // if have tomb data
H
Hongze Cheng 已提交
556 557 558 559 560 561 562 563 564 565 566

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

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

H
Hongze Cheng 已提交
567 568 569
    // writer
    SFSetWriter* fsetWriter;
  } ctx[1];
H
Hongze Cheng 已提交
570 571
};

H
Hongze Cheng 已提交
572 573
// APIs
static int32_t tsdbSnapWriteTimeSeriesRow(STsdbSnapWriter* writer, SRowInfo* row) {
H
Hongze Cheng 已提交
574
  int32_t code = 0;
H
Hongze Cheng 已提交
575 576
  int32_t lino = 0;

H
Hongze Cheng 已提交
577 578 579 580 581
  while (writer->ctx->hasData) {
    SRowInfo* row1 = tsdbIterMergerGetData(writer->ctx->dataIterMerger);
    if (row1 == NULL) {
      writer->ctx->hasData = false;
      break;
H
Hongze Cheng 已提交
582
    }
H
Hongze Cheng 已提交
583

H
Hongze Cheng 已提交
584 585 586
    int32_t c = tRowInfoCmprFn(row1, row);
    if (c <= 0) {
      code = tsdbFSetWriteRow(writer->ctx->fsetWriter, row1);
H
Hongze Cheng 已提交
587
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
588

H
Hongze Cheng 已提交
589
      code = tsdbIterMergerNext(writer->ctx->dataIterMerger);
H
Hongze Cheng 已提交
590
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
591 592
    } else {
      break;
H
Hongze Cheng 已提交
593
    }
H
Hongze Cheng 已提交
594
  }
H
Hongze Cheng 已提交
595

H
Hongze Cheng 已提交
596 597 598
  if (row->suid == INT64_MAX) {
    ASSERT(writer->ctx->hasData == false);
    goto _exit;
H
Hongze Cheng 已提交
599
  }
H
Hongze Cheng 已提交
600

H
Hongze Cheng 已提交
601
  code = tsdbFSetWriteRow(writer->ctx->fsetWriter, row);
H
Hongze Cheng 已提交
602 603
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
604
_exit:
H
Hongze Cheng 已提交
605
  if (code) {
H
Hongze Cheng 已提交
606
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
607
  }
H
Hongze Cheng 已提交
608 609 610
  return code;
}

H
Hongze Cheng 已提交
611
static int32_t tsdbSnapWriteFileSetOpenReader(STsdbSnapWriter* writer) {
H
Hongze Cheng 已提交
612
  int32_t code = 0;
H
Hongze Cheng 已提交
613 614
  int32_t lino = 0;

H
Hongze Cheng 已提交
615 616
  ASSERT(writer->ctx->dataReader == NULL);
  ASSERT(TARRAY2_SIZE(writer->ctx->sttReaderArr) == 0);
H
Hongze Cheng 已提交
617

H
Hongze Cheng 已提交
618 619 620 621 622 623 624
  if (writer->ctx->fset) {
    // open data reader
    SDataFileReaderConfig dataFileReaderConfig = {
        .tsdb = writer->tsdb,
        .bufArr = writer->aBuf,
        .szPage = writer->szPage,
    };
H
Hongze Cheng 已提交
625

H
Hongze Cheng 已提交
626 627 628
    for (int32_t ftype = 0; ftype < TSDB_FTYPE_MAX; ++ftype) {
      if (writer->ctx->fset->farr[ftype] == NULL) {
        continue;
H
Hongze Cheng 已提交
629
      }
H
Hongze Cheng 已提交
630 631 632

      dataFileReaderConfig.files[ftype].exist = true;
      dataFileReaderConfig.files[ftype].file = writer->ctx->fset->farr[ftype]->f[0];
H
Hongze Cheng 已提交
633 634 635 636 637 638 639 640 641

      STFileOp fileOp = {
          .optype = TSDB_FOP_REMOVE,
          .fid = writer->ctx->fset->fid,
          .of = writer->ctx->fset->farr[ftype]->f[0],
      };

      code = TARRAY2_APPEND(writer->fopArr, fileOp);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
642 643
    }

H
Hongze Cheng 已提交
644
    code = tsdbDataFileReaderOpen(NULL, &dataFileReaderConfig, &writer->ctx->dataReader);
H
Hongze Cheng 已提交
645
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
646

H
Hongze Cheng 已提交
647 648 649 650 651 652 653 654 655 656 657 658 659 660
    // open stt reader array
    SSttLvl* lvl;
    TARRAY2_FOREACH(writer->ctx->fset->lvlArr, lvl) {
      STFileObj* fobj;
      TARRAY2_FOREACH(lvl->fobjArr, fobj) {
        SSttFileReader*      reader;
        SSttFileReaderConfig sttFileReaderConfig = {
            .tsdb = writer->tsdb,
            .szPage = writer->szPage,
            .bufArr = writer->aBuf,
            .file = fobj->f[0],
        };

        code = tsdbSttFileReaderOpen(fobj->fname, &sttFileReaderConfig, &reader);
H
Hongze Cheng 已提交
661
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
662

H
Hongze Cheng 已提交
663 664
        code = TARRAY2_APPEND(writer->ctx->sttReaderArr, reader);
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
665 666 667 668 669 670 671 672 673

        STFileOp fileOp = {
            .optype = TSDB_FOP_REMOVE,
            .fid = fobj->f->fid,
            .of = fobj->f[0],
        };

        code = TARRAY2_APPEND(writer->fopArr, fileOp);
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
674 675
      }
    }
H
Hongze Cheng 已提交
676
  }
H
Hongze Cheng 已提交
677

H
Hongze Cheng 已提交
678 679
_exit:
  if (code) {
H
Hongze Cheng 已提交
680
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
681
  }
H
Hongze Cheng 已提交
682 683 684
  return code;
}

H
Hongze Cheng 已提交
685 686 687 688 689 690 691
static int32_t tsdbSnapWriteFileSetCloseReader(STsdbSnapWriter* writer) {
  TARRAY2_CLEAR(writer->ctx->sttReaderArr, tsdbSttFileReaderClose);
  tsdbDataFileReaderClose(&writer->ctx->dataReader);
  return 0;
}

static int32_t tsdbSnapWriteFileSetOpenIter(STsdbSnapWriter* writer) {
H
Hongze Cheng 已提交
692
  int32_t code = 0;
H
Hongze Cheng 已提交
693
  int32_t lino = 0;
H
Hongze Cheng 已提交
694

H
Hongze Cheng 已提交
695 696 697 698
  // data ieter
  if (writer->ctx->dataReader) {
    STsdbIter*      iter;
    STsdbIterConfig config = {0};
H
Hongze Cheng 已提交
699

H
Hongze Cheng 已提交
700 701 702
    // data
    config.type = TSDB_ITER_TYPE_DATA;
    config.dataReader = writer->ctx->dataReader;
H
Hongze Cheng 已提交
703

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

H
Hongze Cheng 已提交
707
    code = TARRAY2_APPEND(writer->ctx->dataIterArr, iter);
H
Hongze Cheng 已提交
708
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
709

H
Hongze Cheng 已提交
710 711 712
    // tome
    config.type = TSDB_ITER_TYPE_DATA_TOMB;
    config.dataReader = writer->ctx->dataReader;
H
Hongze Cheng 已提交
713

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

H
Hongze Cheng 已提交
717 718 719
    code = TARRAY2_APPEND(writer->ctx->tombIterArr, iter);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
720

H
Hongze Cheng 已提交
721 722 723 724 725
  // stt iter
  SSttFileReader* sttFileReader;
  TARRAY2_FOREACH(writer->ctx->sttReaderArr, sttFileReader) {
    STsdbIter*      iter;
    STsdbIterConfig config = {0};
H
Hongze Cheng 已提交
726

H
Hongze Cheng 已提交
727 728 729
    // data
    config.type = TSDB_ITER_TYPE_STT;
    config.sttReader = sttFileReader;
H
Hongze Cheng 已提交
730

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

H
Hongze Cheng 已提交
734
    code = TARRAY2_APPEND(writer->ctx->dataIterArr, iter);
H
Hongze Cheng 已提交
735
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
736

H
Hongze Cheng 已提交
737 738 739
    // tomb
    config.type = TSDB_ITER_TYPE_STT_TOMB;
    config.sttReader = sttFileReader;
H
Hongze Cheng 已提交
740

H
Hongze Cheng 已提交
741 742 743 744
    code = tsdbIterOpen(&config, &iter);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = TARRAY2_APPEND(writer->ctx->tombIterArr, iter);
H
Hongze Cheng 已提交
745
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
746 747
  }

H
Hongze Cheng 已提交
748 749 750 751
  // open merger
  code = tsdbIterMergerOpen(writer->ctx->dataIterArr, &writer->ctx->dataIterMerger, false);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
752
  code = tsdbIterMergerOpen(writer->ctx->tombIterArr, &writer->ctx->tombIterMerger, true);
H
Hongze Cheng 已提交
753
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
754

H
Hongze Cheng 已提交
755 756
_exit:
  if (code) {
H
Hongze Cheng 已提交
757
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
758
  }
H
Hongze Cheng 已提交
759 760 761
  return code;
}

H
Hongze Cheng 已提交
762 763 764 765 766 767 768 769 770
static int32_t tsdbSnapWriteFileSetCloseIter(STsdbSnapWriter* writer) {
  tsdbIterMergerClose(&writer->ctx->dataIterMerger);
  tsdbIterMergerClose(&writer->ctx->tombIterMerger);
  TARRAY2_CLEAR(writer->ctx->dataIterArr, tsdbIterClose);
  TARRAY2_CLEAR(writer->ctx->tombIterArr, tsdbIterClose);
  return 0;
}

static int32_t tsdbSnapWriteFileSetOpenWriter(STsdbSnapWriter* writer) {
H
Hongze Cheng 已提交
771 772 773
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
774 775 776 777 778 779 780 781 782 783 784 785 786
  SFSetWriterConfig config = {
      .tsdb = writer->tsdb,
      .toSttOnly = false,
      .compactVersion = writer->compactVersion,
      .minRow = writer->minRow,
      .maxRow = writer->maxRow,
      .szPage = writer->szPage,
      .cmprAlg = writer->cmprAlg,
      .fid = writer->ctx->fid,
      .cid = writer->commitID,
      .did = writer->ctx->did,
      .level = 0,
  };
H
Hongze Cheng 已提交
787

H
Hongze Cheng 已提交
788
  code = tsdbFSetWriterOpen(&config, &writer->ctx->fsetWriter);
H
Hongze Cheng 已提交
789
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
790

H
Hongze Cheng 已提交
791 792
_exit:
  if (code) {
H
Hongze Cheng 已提交
793
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
794
  }
H
Hongze Cheng 已提交
795 796 797
  return code;
}

H
Hongze Cheng 已提交
798 799 800 801 802
static int32_t tsdbSnapWriteFileSetCloseWriter(STsdbSnapWriter* writer) {
  return tsdbFSetWriterClose(&writer->ctx->fsetWriter, 0, writer->fopArr);
}

static int32_t tsdbSnapWriteFileSetBegin(STsdbSnapWriter* writer, int32_t fid) {
H
Hongze Cheng 已提交
803
  int32_t code = 0;
H
Hongze Cheng 已提交
804
  int32_t lino = 0;
H
Hongze Cheng 已提交
805

H
Hongze Cheng 已提交
806
  ASSERT(writer->ctx->fsetWriteBegin == false);
H
Hongze Cheng 已提交
807

H
Hongze Cheng 已提交
808
  STFileSet* fset = &(STFileSet){.fid = fid};
H
Hongze Cheng 已提交
809

H
Hongze Cheng 已提交
810
  writer->ctx->fid = fid;
H
Hongze Cheng 已提交
811 812
  STFileSet** fsetPtr = TARRAY2_SEARCH(writer->fsetArr, &fset, tsdbTFileSetCmprFn, TD_EQ);
  writer->ctx->fset = (fsetPtr == NULL) ? NULL : *fsetPtr;
H
Hongze Cheng 已提交
813

H
Hongze Cheng 已提交
814
  int32_t level = tsdbFidLevel(fid, &writer->tsdb->keepCfg, taosGetTimestampSec());
H
Hongze Cheng 已提交
815 816 817
  if (tfsAllocDisk(writer->tsdb->pVnode->pTfs, level, &writer->ctx->did)) {
    code = TSDB_CODE_NO_AVAIL_DISK;
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
818
  }
H
Hongze Cheng 已提交
819
  tfsMkdirRecurAt(writer->tsdb->pVnode->pTfs, writer->tsdb->path, writer->ctx->did);
H
Hongze Cheng 已提交
820

H
Hongze Cheng 已提交
821 822 823 824 825 826 827 828 829 830 831 832 833 834
  writer->ctx->hasData = true;
  writer->ctx->hasTomb = true;

  code = tsdbSnapWriteFileSetOpenReader(writer);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbSnapWriteFileSetOpenIter(writer);
  TSDB_CHECK_CODE(code, lino, _exit);

  code = tsdbSnapWriteFileSetOpenWriter(writer);
  TSDB_CHECK_CODE(code, lino, _exit);

  writer->ctx->fsetWriteBegin = true;

H
Hongze Cheng 已提交
835 836
_exit:
  if (code) {
H
Hongze Cheng 已提交
837
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
838
  }
H
Hongze Cheng 已提交
839 840 841
  return code;
}

H
Hongze Cheng 已提交
842
static int32_t tsdbSnapWriteTombRecord(STsdbSnapWriter* writer, const STombRecord* record) {
H
Hongze Cheng 已提交
843
  int32_t code = 0;
H
Hongze Cheng 已提交
844
  int32_t lino = 0;
H
Hongze Cheng 已提交
845

H
Hongze Cheng 已提交
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
  while (writer->ctx->hasTomb) {
    STombRecord* record1 = tsdbIterMergerGetTombRecord(writer->ctx->tombIterMerger);
    if (record1 == NULL) {
      writer->ctx->hasTomb = false;
      break;
    }

    int32_t c = tTombRecordCompare(record1, record);
    if (c <= 0) {
      code = tsdbFSetWriteTombRecord(writer->ctx->fsetWriter, record1);
      TSDB_CHECK_CODE(code, lino, _exit);
    } else {
      break;
    }
  }

  if (record->suid == INT64_MAX) {
    ASSERT(writer->ctx->hasTomb == false);
H
Hongze Cheng 已提交
864
    goto _exit;
H
Hongze Cheng 已提交
865 866
  }

H
Hongze Cheng 已提交
867
  code = tsdbFSetWriteTombRecord(writer->ctx->fsetWriter, record);
H
Hongze Cheng 已提交
868
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
869

H
Hongze Cheng 已提交
870
_exit:
H
Hongze Cheng 已提交
871
  if (code) {
H
Hongze Cheng 已提交
872
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
873
  }
H
Hongze Cheng 已提交
874
  return code;
H
Hongze Cheng 已提交
875 876
}

H
Hongze Cheng 已提交
877 878 879
static int32_t tsdbSnapWriteFileSetEnd(STsdbSnapWriter* writer) {
  if (!writer->ctx->fsetWriteBegin) return 0;

H
Hongze Cheng 已提交
880
  int32_t code = 0;
H
Hongze Cheng 已提交
881
  int32_t lino = 0;
H
Hongze Cheng 已提交
882

H
Hongze Cheng 已提交
883
  // end timeseries data write
H
Hongze Cheng 已提交
884 885 886 887
  SRowInfo row = {
      .suid = INT64_MAX,
      .uid = INT64_MAX,
  };
H
Hongze Cheng 已提交
888

H
Hongze Cheng 已提交
889
  code = tsdbSnapWriteTimeSeriesRow(writer, &row);
H
Hongze Cheng 已提交
890
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
891

H
Hongze Cheng 已提交
892
  // end tombstone data write
H
Hongze Cheng 已提交
893 894 895 896
  STombRecord record = {
      .suid = INT64_MAX,
      .uid = INT64_MAX,
  };
H
Hongze Cheng 已提交
897

H
Hongze Cheng 已提交
898
  code = tsdbSnapWriteTombRecord(writer, &record);
H
Hongze Cheng 已提交
899
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
900

H
Hongze Cheng 已提交
901 902
  // close write
  code = tsdbSnapWriteFileSetCloseWriter(writer);
H
Hongze Cheng 已提交
903
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
904

H
Hongze Cheng 已提交
905
  code = tsdbSnapWriteFileSetCloseIter(writer);
H
Hongze Cheng 已提交
906
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
907

H
Hongze Cheng 已提交
908
  code = tsdbSnapWriteFileSetCloseReader(writer);
H
Hongze Cheng 已提交
909
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
910

H
Hongze Cheng 已提交
911
  writer->ctx->fsetWriteBegin = false;
H
Hongze Cheng 已提交
912

H
Hongze Cheng 已提交
913 914
_exit:
  if (code) {
H
Hongze Cheng 已提交
915
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
916
  }
H
Hongze Cheng 已提交
917 918 919
  return code;
}

H
Hongze Cheng 已提交
920
static int32_t tsdbSnapWriteTimeSeriesData(STsdbSnapWriter* writer, SSnapDataHdr* hdr) {
H
Hongze Cheng 已提交
921
  int32_t code = 0;
H
Hongze Cheng 已提交
922
  int32_t lino = 0;
H
Hongze Cheng 已提交
923

H
Hongze Cheng 已提交
924
  SBlockData blockData[1] = {0};
H
Hongze Cheng 已提交
925

H
Hongze Cheng 已提交
926
  code = tDecmprBlockData(hdr->data, hdr->size - sizeof(*hdr), blockData, writer->aBuf);
H
Hongze Cheng 已提交
927
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
928

H
Hongze Cheng 已提交
929 930 931 932
  int32_t fid = tsdbKeyFid(blockData->aTSKEY[0], writer->minutes, writer->precision);
  if (!writer->ctx->fsetWriteBegin || fid != writer->ctx->fid) {
    code = tsdbSnapWriteFileSetEnd(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
933

H
Hongze Cheng 已提交
934
    code = tsdbSnapWriteFileSetBegin(writer, fid);
H
Hongze Cheng 已提交
935
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
936 937
  }

H
Hongze Cheng 已提交
938 939 940 941 942 943 944 945 946
  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);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
947 948
  }

H
Hongze Cheng 已提交
949
_exit:
H
Hongze Cheng 已提交
950
  if (code) {
H
Hongze Cheng 已提交
951
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
952
  } else {
H
Hongze Cheng 已提交
953 954
    tsdbDebug("vgId:%d %s done, suid:%" PRId64 " uid:%" PRId64 " nRow:%d", TD_VID(writer->tsdb->pVnode), __func__,
              blockData->suid, blockData->uid, blockData->nRow);
H
Hongze Cheng 已提交
955
  }
H
Hongze Cheng 已提交
956
  tBlockDataDestroy(blockData);
H
Hongze Cheng 已提交
957 958 959
  return code;
}

H
Hongze Cheng 已提交
960
static int32_t tsdbSnapWriteDecmprTombBlock(SSnapDataHdr* hdr, STombBlock* tombBlock) {
H
Hongze Cheng 已提交
961
  int32_t code = 0;
H
Hongze Cheng 已提交
962
  int32_t lino = 0;
H
Hongze Cheng 已提交
963

H
Hongze Cheng 已提交
964
  int64_t size = hdr->size;
H
Hongze Cheng 已提交
965 966 967
  ASSERT(size % TOMB_RECORD_ELEM_NUM == 0);
  size = size / TOMB_RECORD_ELEM_NUM;
  ASSERT(size % sizeof(int64_t) == 0);
H
Hongze Cheng 已提交
968

H
Hongze Cheng 已提交
969 970 971 972 973
  int64_t* data = (int64_t*)hdr->data;
  for (int32_t i = 0; i < TOMB_RECORD_ELEM_NUM; ++i) {
    code = TARRAY2_APPEND_BATCH(&tombBlock->dataArr[i], hdr->data + i * size, size / sizeof(int64_t));
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
974

H
Hongze Cheng 已提交
975 976 977
_exit:
  return code;
}
H
Hongze Cheng 已提交
978

H
Hongze Cheng 已提交
979 980 981
static int32_t tsdbSnapWriteTombData(STsdbSnapWriter* writer, SSnapDataHdr* hdr) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
982

H
Hongze Cheng 已提交
983 984
  STombRecord record;
  STombBlock  tombBlock[1] = {0};
H
Hongze Cheng 已提交
985

H
Hongze Cheng 已提交
986 987
  code = tsdbSnapWriteDecmprTombBlock(hdr, tombBlock);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
988

H
Hongze Cheng 已提交
989 990 991 992 993
  tTombBlockGet(tombBlock, 0, &record);
  int32_t fid = tsdbKeyFid(record.skey, writer->minutes, writer->precision);
  if (!writer->ctx->fsetWriteBegin || fid != writer->ctx->fid) {
    code = tsdbSnapWriteFileSetEnd(writer);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
994

H
Hongze Cheng 已提交
995 996 997
    code = tsdbSnapWriteFileSetBegin(writer, fid);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
998

H
Hongze Cheng 已提交
999 1000 1001 1002 1003
  if (writer->ctx->hasData) {
    SRowInfo row = {
        .suid = INT64_MAX,
        .uid = INT64_MAX,
    };
H
Hongze Cheng 已提交
1004

H
Hongze Cheng 已提交
1005
    code = tsdbSnapWriteTimeSeriesRow(writer, &row);
H
Hongze Cheng 已提交
1006
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1007 1008
  }

H
Hongze Cheng 已提交
1009
  ASSERT(writer->ctx->hasData == false);
H
Hongze Cheng 已提交
1010

H
Hongze Cheng 已提交
1011 1012
  for (int32_t i = 0; i < TOMB_BLOCK_SIZE(tombBlock); ++i) {
    tTombBlockGet(tombBlock, i, &record);
H
Hongze Cheng 已提交
1013

H
Hongze Cheng 已提交
1014
    code = tsdbSnapWriteTombRecord(writer, &record);
H
Hongze Cheng 已提交
1015 1016 1017
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
1018
  tTombBlockDestroy(tombBlock);
H
Hongze Cheng 已提交
1019 1020 1021

_exit:
  if (code) {
H
Hongze Cheng 已提交
1022
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1023
  }
H
Hongze Cheng 已提交
1024 1025 1026
  return code;
}

H
Hongze Cheng 已提交
1027
int32_t tsdbSnapWriterOpen(STsdb* pTsdb, int64_t sver, int64_t ever, STsdbSnapWriter** writer) {
H
Hongze Cheng 已提交
1028 1029
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1030

H
Hongze Cheng 已提交
1031 1032 1033 1034
  // disable background tasks
  tsdbFSDisableBgTask(pTsdb->pFS);

  // start to write
H
Hongze Cheng 已提交
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
  writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
  if (writer[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;

  writer[0]->tsdb = pTsdb;
  writer[0]->sver = sver;
  writer[0]->ever = ever;
  writer[0]->minutes = pTsdb->keepCfg.days;
  writer[0]->precision = pTsdb->keepCfg.precision;
  writer[0]->minRow = pTsdb->pVnode->config.tsdbCfg.minRows;
  writer[0]->maxRow = pTsdb->pVnode->config.tsdbCfg.maxRows;
  writer[0]->commitID = tsdbFSAllocEid(pTsdb->pFS);
  writer[0]->szPage = pTsdb->pVnode->config.tsdbPageSize;
  writer[0]->compactVersion = INT64_MAX;
  writer[0]->now = taosGetTimestampMs();

  code = tsdbFSCreateCopySnapshot(pTsdb->pFS, &writer[0]->fsetArr);
H
Hongze Cheng 已提交
1051
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1052

H
Hongze Cheng 已提交
1053 1054
_exit:
  if (code) {
H
Hongze Cheng 已提交
1055
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1056
  } else {
H
Hongze Cheng 已提交
1057
    tsdbInfo("vgId:%d %s done, sver:%" PRId64 " ever:%" PRId64, TD_VID(pTsdb->pVnode), __func__, sver, ever);
H
Hongze Cheng 已提交
1058
  }
H
Hongze Cheng 已提交
1059 1060 1061
  return code;
}

H
Hongze Cheng 已提交
1062
int32_t tsdbSnapWriterPrepareClose(STsdbSnapWriter* writer) {
H
Hongze Cheng 已提交
1063
  int32_t code = 0;
H
Hongze Cheng 已提交
1064 1065
  int32_t lino = 0;

H
Hongze Cheng 已提交
1066 1067
  code = tsdbSnapWriteFileSetEnd(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1068

H
Hongze Cheng 已提交
1069
  code = tsdbFSEditBegin(writer->tsdb->pFS, writer->fopArr, TSDB_FEDIT_COMMIT);
H
Hongze Cheng 已提交
1070
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1071 1072 1073

_exit:
  if (code) {
H
Hongze Cheng 已提交
1074
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1075
  } else {
H
Hongze Cheng 已提交
1076
    tsdbDebug("vgId:%d %s done", TD_VID(writer->tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
1077 1078 1079 1080
  }
  return code;
}

H
Hongze Cheng 已提交
1081
int32_t tsdbSnapWriterClose(STsdbSnapWriter** writer, int8_t rollback) {
H
Hongze Cheng 已提交
1082 1083
  if (writer[0] == NULL) return 0;

H
Hongze Cheng 已提交
1084 1085 1086
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1087
  STsdb* tsdb = writer[0]->tsdb;
H
Hongze Cheng 已提交
1088 1089

  if (rollback) {
H
Hongze Cheng 已提交
1090 1091
    code = tsdbFSEditAbort(writer[0]->tsdb->pFS);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1092
  } else {
H
Hongze Cheng 已提交
1093
    taosThreadRwlockWrlock(&writer[0]->tsdb->rwLock);
H
Hongze Cheng 已提交
1094

H
Hongze Cheng 已提交
1095
    code = tsdbFSEditCommit(writer[0]->tsdb->pFS);
H
Hongze Cheng 已提交
1096
    if (code) {
H
Hongze Cheng 已提交
1097
      taosThreadRwlockUnlock(&writer[0]->tsdb->rwLock);
H
Hongze Cheng 已提交
1098 1099 1100
      TSDB_CHECK_CODE(code, lino, _exit);
    }

H
Hongze Cheng 已提交
1101
    taosThreadRwlockUnlock(&writer[0]->tsdb->rwLock);
H
Hongze Cheng 已提交
1102
  }
H
Hongze Cheng 已提交
1103
  tsdbFSEnableBgTask(tsdb->pFS);
H
Hongze Cheng 已提交
1104

H
Hongze Cheng 已提交
1105 1106 1107 1108 1109 1110
  tsdbIterMergerClose(&writer[0]->ctx->tombIterMerger);
  tsdbIterMergerClose(&writer[0]->ctx->dataIterMerger);
  TARRAY2_DESTROY(writer[0]->ctx->tombIterArr, tsdbIterClose);
  TARRAY2_DESTROY(writer[0]->ctx->dataIterArr, tsdbIterClose);
  TARRAY2_DESTROY(writer[0]->ctx->sttReaderArr, tsdbSttFileReaderClose);
  tsdbDataFileReaderClose(&writer[0]->ctx->dataReader);
H
Hongze Cheng 已提交
1111

H
Hongze Cheng 已提交
1112 1113
  TARRAY2_DESTROY(writer[0]->fopArr, NULL);
  tsdbFSDestroyCopySnapshot(&writer[0]->fsetArr);
H
Hongze Cheng 已提交
1114

H
Hongze Cheng 已提交
1115 1116
  for (int32_t i = 0; i < ARRAY_SIZE(writer[0]->aBuf); ++i) {
    tFree(writer[0]->aBuf[i]);
H
Hongze Cheng 已提交
1117 1118
  }

H
Hongze Cheng 已提交
1119 1120
  taosMemoryFree(writer[0]);
  writer[0] = NULL;
H
Hongze Cheng 已提交
1121 1122 1123

_exit:
  if (code) {
H
Hongze Cheng 已提交
1124
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1125
  } else {
H
Hongze Cheng 已提交
1126
    tsdbInfo("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
  }
  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 已提交
1143 1144
  }

H
Hongze Cheng 已提交
1145
_exit:
H
Hongze Cheng 已提交
1146 1147
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s, type:%d index:%" PRId64 " size:%" PRId64,
H
Hongze Cheng 已提交
1148
              TD_VID(writer->tsdb->pVnode), __func__, lino, tstrerror(code), hdr->type, hdr->index, hdr->size);
H
Hongze Cheng 已提交
1149
  } else {
H
Hongze Cheng 已提交
1150 1151
    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 已提交
1152
  }
H
Hongze Cheng 已提交
1153 1154
  return code;
}