tsdbSnapshot.c 30.3 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 554 555
    SDiskID    did;
    bool       hasData;
    bool       hasTomb;
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
    }

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

H
Hongze Cheng 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650 651
    // 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 已提交
652
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
653

H
Hongze Cheng 已提交
654 655
        code = TARRAY2_APPEND(writer->ctx->sttReaderArr, reader);
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
656 657
      }
    }
H
Hongze Cheng 已提交
658
  }
H
Hongze Cheng 已提交
659

H
Hongze Cheng 已提交
660 661
_exit:
  if (code) {
H
Hongze Cheng 已提交
662
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
663
  }
H
Hongze Cheng 已提交
664 665 666
  return code;
}

H
Hongze Cheng 已提交
667 668 669 670 671 672 673
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 已提交
674
  int32_t code = 0;
H
Hongze Cheng 已提交
675
  int32_t lino = 0;
H
Hongze Cheng 已提交
676

H
Hongze Cheng 已提交
677 678 679 680
  // data ieter
  if (writer->ctx->dataReader) {
    STsdbIter*      iter;
    STsdbIterConfig config = {0};
H
Hongze Cheng 已提交
681

H
Hongze Cheng 已提交
682 683 684
    // data
    config.type = TSDB_ITER_TYPE_DATA;
    config.dataReader = writer->ctx->dataReader;
H
Hongze Cheng 已提交
685

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

H
Hongze Cheng 已提交
689
    code = TARRAY2_APPEND(writer->ctx->dataIterArr, iter);
H
Hongze Cheng 已提交
690
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
691

H
Hongze Cheng 已提交
692 693 694
    // tome
    config.type = TSDB_ITER_TYPE_DATA_TOMB;
    config.dataReader = writer->ctx->dataReader;
H
Hongze Cheng 已提交
695

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

H
Hongze Cheng 已提交
699 700 701
    code = TARRAY2_APPEND(writer->ctx->tombIterArr, iter);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
702

H
Hongze Cheng 已提交
703 704 705 706 707
  // stt iter
  SSttFileReader* sttFileReader;
  TARRAY2_FOREACH(writer->ctx->sttReaderArr, sttFileReader) {
    STsdbIter*      iter;
    STsdbIterConfig config = {0};
H
Hongze Cheng 已提交
708

H
Hongze Cheng 已提交
709 710 711
    // data
    config.type = TSDB_ITER_TYPE_STT;
    config.sttReader = sttFileReader;
H
Hongze Cheng 已提交
712

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

H
Hongze Cheng 已提交
716
    code = TARRAY2_APPEND(writer->ctx->dataIterArr, iter);
H
Hongze Cheng 已提交
717
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
718

H
Hongze Cheng 已提交
719 720 721
    // tomb
    config.type = TSDB_ITER_TYPE_STT_TOMB;
    config.sttReader = sttFileReader;
H
Hongze Cheng 已提交
722

H
Hongze Cheng 已提交
723 724 725 726
    code = tsdbIterOpen(&config, &iter);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = TARRAY2_APPEND(writer->ctx->tombIterArr, iter);
H
Hongze Cheng 已提交
727
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
728 729
  }

H
Hongze Cheng 已提交
730 731 732 733
  // open merger
  code = tsdbIterMergerOpen(writer->ctx->dataIterArr, &writer->ctx->dataIterMerger, false);
  TSDB_CHECK_CODE(code, lino, _exit);

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

H
Hongze Cheng 已提交
737 738
_exit:
  if (code) {
H
Hongze Cheng 已提交
739
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
740
  }
H
Hongze Cheng 已提交
741 742 743
  return code;
}

H
Hongze Cheng 已提交
744 745 746 747 748 749 750 751 752
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 已提交
753 754 755
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
756 757 758 759 760 761 762 763 764 765 766 767 768
  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 已提交
769

H
Hongze Cheng 已提交
770
  code = tsdbFSetWriterOpen(&config, &writer->ctx->fsetWriter);
H
Hongze Cheng 已提交
771
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
772

H
Hongze Cheng 已提交
773 774
_exit:
  if (code) {
H
Hongze Cheng 已提交
775
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
776
  }
H
Hongze Cheng 已提交
777 778 779
  return code;
}

H
Hongze Cheng 已提交
780 781 782 783 784
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 已提交
785
  int32_t code = 0;
H
Hongze Cheng 已提交
786
  int32_t lino = 0;
H
Hongze Cheng 已提交
787

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

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

H
Hongze Cheng 已提交
792
  writer->ctx->fid = fid;
H
Hongze Cheng 已提交
793 794
  STFileSet** fsetPtr = TARRAY2_SEARCH(writer->fsetArr, &fset, tsdbTFileSetCmprFn, TD_EQ);
  writer->ctx->fset = (fsetPtr == NULL) ? NULL : *fsetPtr;
H
Hongze Cheng 已提交
795

H
Hongze Cheng 已提交
796
  int32_t level = tsdbFidLevel(fid, &writer->tsdb->keepCfg, taosGetTimestampSec());
H
Hongze Cheng 已提交
797 798 799
  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 已提交
800
  }
H
Hongze Cheng 已提交
801
  tfsMkdirRecurAt(writer->tsdb->pVnode->pTfs, writer->tsdb->path, writer->ctx->did);
H
Hongze Cheng 已提交
802

H
Hongze Cheng 已提交
803 804 805 806 807 808 809 810 811 812 813 814 815 816
  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 已提交
817 818
_exit:
  if (code) {
H
Hongze Cheng 已提交
819
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
820
  }
H
Hongze Cheng 已提交
821 822 823
  return code;
}

H
Hongze Cheng 已提交
824
static int32_t tsdbSnapWriteTombRecord(STsdbSnapWriter* writer, const STombRecord* record) {
H
Hongze Cheng 已提交
825
  int32_t code = 0;
H
Hongze Cheng 已提交
826
  int32_t lino = 0;
H
Hongze Cheng 已提交
827

H
Hongze Cheng 已提交
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
  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 已提交
846
    goto _exit;
H
Hongze Cheng 已提交
847 848
  }

H
Hongze Cheng 已提交
849
  code = tsdbFSetWriteTombRecord(writer->ctx->fsetWriter, record);
H
Hongze Cheng 已提交
850
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
851

H
Hongze Cheng 已提交
852
_exit:
H
Hongze Cheng 已提交
853
  if (code) {
H
Hongze Cheng 已提交
854
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
855
  }
H
Hongze Cheng 已提交
856
  return code;
H
Hongze Cheng 已提交
857 858
}

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

H
Hongze Cheng 已提交
862
  int32_t code = 0;
H
Hongze Cheng 已提交
863
  int32_t lino = 0;
H
Hongze Cheng 已提交
864

H
Hongze Cheng 已提交
865 866 867 868
  SRowInfo row = {
      .suid = INT64_MAX,
      .uid = INT64_MAX,
  };
H
Hongze Cheng 已提交
869

H
Hongze Cheng 已提交
870
  code = tsdbSnapWriteTimeSeriesRow(writer, &row);
H
Hongze Cheng 已提交
871
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
872

H
Hongze Cheng 已提交
873 874 875 876
  STombRecord record = {
      .suid = INT64_MAX,
      .uid = INT64_MAX,
  };
H
Hongze Cheng 已提交
877

H
Hongze Cheng 已提交
878
  code = tsdbSnapWriteTombRecord(writer, &record);
H
Hongze Cheng 已提交
879
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
880

H
Hongze Cheng 已提交
881 882
  // close write
  code = tsdbSnapWriteFileSetCloseWriter(writer);
H
Hongze Cheng 已提交
883
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
884

H
Hongze Cheng 已提交
885
  code = tsdbSnapWriteFileSetCloseIter(writer);
H
Hongze Cheng 已提交
886
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
887

H
Hongze Cheng 已提交
888
  code = tsdbSnapWriteFileSetCloseReader(writer);
H
Hongze Cheng 已提交
889
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
890

H
Hongze Cheng 已提交
891
  writer->ctx->fsetWriteBegin = false;
H
Hongze Cheng 已提交
892

H
Hongze Cheng 已提交
893 894
_exit:
  if (code) {
H
Hongze Cheng 已提交
895
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
896
  }
H
Hongze Cheng 已提交
897 898 899
  return code;
}

H
Hongze Cheng 已提交
900
static int32_t tsdbSnapWriteTimeSeriesData(STsdbSnapWriter* writer, SSnapDataHdr* hdr) {
H
Hongze Cheng 已提交
901
  int32_t code = 0;
H
Hongze Cheng 已提交
902
  int32_t lino = 0;
H
Hongze Cheng 已提交
903

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

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

H
Hongze Cheng 已提交
909 910 911 912
  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 已提交
913

H
Hongze Cheng 已提交
914
    code = tsdbSnapWriteFileSetBegin(writer, fid);
H
Hongze Cheng 已提交
915
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
916 917
  }

H
Hongze Cheng 已提交
918 919 920 921 922 923 924 925 926
  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 已提交
927 928
  }

H
Hongze Cheng 已提交
929
_exit:
H
Hongze Cheng 已提交
930
  if (code) {
H
Hongze Cheng 已提交
931
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
932
  } else {
H
Hongze Cheng 已提交
933 934
    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 已提交
935
  }
H
Hongze Cheng 已提交
936
  tBlockDataDestroy(blockData);
H
Hongze Cheng 已提交
937 938 939
  return code;
}

H
Hongze Cheng 已提交
940
static int32_t tsdbSnapWriteDecmprTombBlock(SSnapDataHdr* hdr, STombBlock* tombBlock) {
H
Hongze Cheng 已提交
941
  int32_t code = 0;
H
Hongze Cheng 已提交
942
  int32_t lino = 0;
H
Hongze Cheng 已提交
943

H
Hongze Cheng 已提交
944
  int64_t size = hdr->size;
H
Hongze Cheng 已提交
945 946 947
  ASSERT(size % TOMB_RECORD_ELEM_NUM == 0);
  size = size / TOMB_RECORD_ELEM_NUM;
  ASSERT(size % sizeof(int64_t) == 0);
H
Hongze Cheng 已提交
948

H
Hongze Cheng 已提交
949 950 951 952 953
  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 已提交
954

H
Hongze Cheng 已提交
955 956 957
_exit:
  return code;
}
H
Hongze Cheng 已提交
958

H
Hongze Cheng 已提交
959 960 961
static int32_t tsdbSnapWriteTombData(STsdbSnapWriter* writer, SSnapDataHdr* hdr) {
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
962

H
Hongze Cheng 已提交
963 964
  STombRecord record;
  STombBlock  tombBlock[1] = {0};
H
Hongze Cheng 已提交
965

H
Hongze Cheng 已提交
966 967
  code = tsdbSnapWriteDecmprTombBlock(hdr, tombBlock);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
968

H
Hongze Cheng 已提交
969 970 971 972 973
  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 已提交
974

H
Hongze Cheng 已提交
975 976 977
    code = tsdbSnapWriteFileSetBegin(writer, fid);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
978

H
Hongze Cheng 已提交
979 980 981 982 983
  if (writer->ctx->hasData) {
    SRowInfo row = {
        .suid = INT64_MAX,
        .uid = INT64_MAX,
    };
H
Hongze Cheng 已提交
984

H
Hongze Cheng 已提交
985
    code = tsdbSnapWriteTimeSeriesRow(writer, &row);
H
Hongze Cheng 已提交
986
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
987 988
  }

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

H
Hongze Cheng 已提交
991 992
  for (int32_t i = 0; i < TOMB_BLOCK_SIZE(tombBlock); ++i) {
    tTombBlockGet(tombBlock, i, &record);
H
Hongze Cheng 已提交
993

H
Hongze Cheng 已提交
994
    code = tsdbSnapWriteTombRecord(writer, &record);
H
Hongze Cheng 已提交
995 996 997
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
998
  tTombBlockDestroy(tombBlock);
H
Hongze Cheng 已提交
999 1000 1001

_exit:
  if (code) {
H
Hongze Cheng 已提交
1002
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1003
  }
H
Hongze Cheng 已提交
1004 1005 1006
  return code;
}

H
Hongze Cheng 已提交
1007
int32_t tsdbSnapWriterOpen(STsdb* pTsdb, int64_t sver, int64_t ever, STsdbSnapWriter** writer) {
H
Hongze Cheng 已提交
1008 1009
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1010

H
Hongze Cheng 已提交
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
  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 已提交
1027
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1028

H
Hongze Cheng 已提交
1029 1030
  tsdbFSDisableBgTask(pTsdb->pFS);

H
Hongze Cheng 已提交
1031 1032
_exit:
  if (code) {
H
Hongze Cheng 已提交
1033
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1034
  } else {
H
Hongze Cheng 已提交
1035
    tsdbInfo("vgId:%d %s done, sver:%" PRId64 " ever:%" PRId64, TD_VID(pTsdb->pVnode), __func__, sver, ever);
H
Hongze Cheng 已提交
1036
  }
H
Hongze Cheng 已提交
1037 1038 1039
  return code;
}

H
Hongze Cheng 已提交
1040
int32_t tsdbSnapWriterPrepareClose(STsdbSnapWriter* writer) {
H
Hongze Cheng 已提交
1041
  int32_t code = 0;
H
Hongze Cheng 已提交
1042 1043
  int32_t lino = 0;

H
Hongze Cheng 已提交
1044 1045
  code = tsdbSnapWriteFileSetEnd(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1046

H
Hongze Cheng 已提交
1047
  code = tsdbFSEditBegin(writer->tsdb->pFS, writer->fopArr, TSDB_FEDIT_COMMIT);
H
Hongze Cheng 已提交
1048
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1049 1050 1051

_exit:
  if (code) {
H
Hongze Cheng 已提交
1052
    TSDB_ERROR_LOG(TD_VID(writer->tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1053
  } else {
H
Hongze Cheng 已提交
1054
    tsdbDebug("vgId:%d %s done", TD_VID(writer->tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
1055 1056 1057 1058
  }
  return code;
}

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

H
Hongze Cheng 已提交
1062 1063 1064
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1065
  STsdb* tsdb = writer[0]->tsdb;
H
Hongze Cheng 已提交
1066 1067

  if (rollback) {
H
Hongze Cheng 已提交
1068 1069
    code = tsdbFSEditAbort(writer[0]->tsdb->pFS);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1070
  } else {
H
Hongze Cheng 已提交
1071
    taosThreadRwlockWrlock(&writer[0]->tsdb->rwLock);
H
Hongze Cheng 已提交
1072

H
Hongze Cheng 已提交
1073
    code = tsdbFSEditCommit(writer[0]->tsdb->pFS);
H
Hongze Cheng 已提交
1074
    if (code) {
H
Hongze Cheng 已提交
1075
      taosThreadRwlockUnlock(&writer[0]->tsdb->rwLock);
H
Hongze Cheng 已提交
1076 1077 1078
      TSDB_CHECK_CODE(code, lino, _exit);
    }

H
Hongze Cheng 已提交
1079
    taosThreadRwlockUnlock(&writer[0]->tsdb->rwLock);
H
Hongze Cheng 已提交
1080
  }
H
Hongze Cheng 已提交
1081
  tsdbFSEnableBgTask(tsdb->pFS);
H
Hongze Cheng 已提交
1082

H
Hongze Cheng 已提交
1083 1084 1085 1086 1087 1088
  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 已提交
1089

H
Hongze Cheng 已提交
1090 1091
  TARRAY2_DESTROY(writer[0]->fopArr, NULL);
  tsdbFSDestroyCopySnapshot(&writer[0]->fsetArr);
H
Hongze Cheng 已提交
1092

H
Hongze Cheng 已提交
1093 1094
  for (int32_t i = 0; i < ARRAY_SIZE(writer[0]->aBuf); ++i) {
    tFree(writer[0]->aBuf[i]);
H
Hongze Cheng 已提交
1095 1096
  }

H
Hongze Cheng 已提交
1097 1098
  taosMemoryFree(writer[0]);
  writer[0] = NULL;
H
Hongze Cheng 已提交
1099 1100 1101

_exit:
  if (code) {
H
Hongze Cheng 已提交
1102
    TSDB_ERROR_LOG(TD_VID(tsdb->pVnode), lino, code);
H
Hongze Cheng 已提交
1103
  } else {
H
Hongze Cheng 已提交
1104
    tsdbInfo("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
H
Hongze Cheng 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
  }
  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 已提交
1121 1122
  }

H
Hongze Cheng 已提交
1123
_exit:
H
Hongze Cheng 已提交
1124 1125
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s, type:%d index:%" PRId64 " size:%" PRId64,
H
Hongze Cheng 已提交
1126
              TD_VID(writer->tsdb->pVnode), __func__, lino, tstrerror(code), hdr->type, hdr->index, hdr->size);
H
Hongze Cheng 已提交
1127
  } else {
H
Hongze Cheng 已提交
1128 1129
    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 已提交
1130
  }
H
Hongze Cheng 已提交
1131 1132
  return code;
}