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 346 347
  int64_t size = sizeof(SSnapDataHdr);
  for (int32_t i = 0; i < ARRAY_SIZE(reader->tombBlock->dataArr); i++) {
    size += TARRAY2_DATA_LEN(reader->tombBlock->dataArr + i);
H
Hongze Cheng 已提交
348
  }
H
Hongze Cheng 已提交
349

H
Hongze Cheng 已提交
350 351
  data[0] = taosMemoryMalloc(size);
  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 357 358
  SSnapDataHdr* hdr = (SSnapDataHdr*)data[0];
  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
    if (TOMB_BLOCK_SIZE(reader->tombBlock) >= 81920) {
      break;
H
Hongze Cheng 已提交
397
    }
H
Hongze Cheng 已提交
398 399
  }

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

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

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

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

H
Hongze Cheng 已提交
419 420 421 422
  reader[0]->tsdb = tsdb;
  reader[0]->sver = sver;
  reader[0]->ever = ever;
  reader[0]->type = type;
H
Hongze Cheng 已提交
423

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

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

H
Hongze Cheng 已提交
441 442 443
int32_t tsdbSnapReaderClose(STsdbSnapReader** reader) {
  if (reader[0] == NULL) return 0;

H
Hongze Cheng 已提交
444 445
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
446

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

H
Hongze Cheng 已提交
449 450
  tTombBlockDestroy(reader[0]->tombBlock);
  tBlockDataDestroy(reader[0]->blockData);
H
Hongze Cheng 已提交
451

H
Hongze Cheng 已提交
452 453 454 455 456 457
  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 已提交
458

H
Hongze Cheng 已提交
459 460 461
  tsdbFSDestroyRefSnapshot(&reader[0]->fsetArr);
  tDestroyTSchema(reader[0]->skmTb->pTSchema);

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

  taosMemoryFree(reader[0]);
  reader[0] = NULL;
H
Hongze Cheng 已提交
468 469 470

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

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

H
Hongze Cheng 已提交
482
  data[0] = NULL;
H
Hongze Cheng 已提交
483

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

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

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

    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 已提交
516
  }
H
Hongze Cheng 已提交
517

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

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

H
Hongze Cheng 已提交
543 544 545 546
  TFileSetArray* fsetArr;
  TFileOpArray   fopArr[1];

  struct {
H
Hongze Cheng 已提交
547
    bool       fsetWriteBegin;
H
Hongze Cheng 已提交
548 549
    int32_t    fid;
    STFileSet* fset;
H
Hongze Cheng 已提交
550 551 552
    SDiskID    did;
    bool       hasData;
    bool       hasTomb;
H
Hongze Cheng 已提交
553 554 555 556 557 558 559 560 561 562 563

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

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

H
Hongze Cheng 已提交
564 565 566
    // writer
    SFSetWriter* fsetWriter;
  } ctx[1];
H
Hongze Cheng 已提交
567 568
};

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

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

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

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

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

H
Hongze Cheng 已提交
598
  code = tsdbFSetWriteRow(writer->ctx->fsetWriter, row);
H
Hongze Cheng 已提交
599 600
  TSDB_CHECK_CODE(code, lino, _exit);

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

H
Hongze Cheng 已提交
608
static int32_t tsdbSnapWriteFileSetOpenReader(STsdbSnapWriter* writer) {
H
Hongze Cheng 已提交
609
  int32_t code = 0;
H
Hongze Cheng 已提交
610 611
  int32_t lino = 0;

H
Hongze Cheng 已提交
612 613
  ASSERT(writer->ctx->dataReader == NULL);
  ASSERT(TARRAY2_SIZE(writer->ctx->sttReaderArr) == 0);
H
Hongze Cheng 已提交
614

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

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

      dataFileReaderConfig.files[ftype].exist = true;
      dataFileReaderConfig.files[ftype].file = writer->ctx->fset->farr[ftype]->f[0];
H
Hongze Cheng 已提交
630 631
    }

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

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

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

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

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

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

H
Hongze Cheng 已提交
679 680 681
    // data
    config.type = TSDB_ITER_TYPE_DATA;
    config.dataReader = writer->ctx->dataReader;
H
Hongze Cheng 已提交
682

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

H
Hongze Cheng 已提交
686
    code = TARRAY2_APPEND(writer->ctx->dataIterArr, iter);
H
Hongze Cheng 已提交
687
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
688

H
Hongze Cheng 已提交
689 690 691
    // tome
    config.type = TSDB_ITER_TYPE_DATA_TOMB;
    config.dataReader = writer->ctx->dataReader;
H
Hongze Cheng 已提交
692

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

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

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

H
Hongze Cheng 已提交
706 707 708
    // data
    config.type = TSDB_ITER_TYPE_STT;
    config.sttReader = sttFileReader;
H
Hongze Cheng 已提交
709

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

H
Hongze Cheng 已提交
713
    code = TARRAY2_APPEND(writer->ctx->dataIterArr, iter);
H
Hongze Cheng 已提交
714
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
715

H
Hongze Cheng 已提交
716 717 718
    // tomb
    config.type = TSDB_ITER_TYPE_STT_TOMB;
    config.sttReader = sttFileReader;
H
Hongze Cheng 已提交
719

H
Hongze Cheng 已提交
720 721 722 723
    code = tsdbIterOpen(&config, &iter);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = TARRAY2_APPEND(writer->ctx->tombIterArr, iter);
H
Hongze Cheng 已提交
724
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
725 726
  }

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

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

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

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

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

H
Hongze Cheng 已提交
767
  code = tsdbFSetWriterOpen(&config, &writer->ctx->fsetWriter);
H
Hongze Cheng 已提交
768
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
769

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
846
  code = tsdbFSetWriteTombRecord(writer->ctx->fsetWriter, record);
H
Hongze Cheng 已提交
847
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
848

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

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

H
Hongze Cheng 已提交
859
  int32_t code = 0;
H
Hongze Cheng 已提交
860
  int32_t lino = 0;
H
Hongze Cheng 已提交
861

H
Hongze Cheng 已提交
862 863 864 865
  SRowInfo row = {
      .suid = INT64_MAX,
      .uid = INT64_MAX,
  };
H
Hongze Cheng 已提交
866

H
Hongze Cheng 已提交
867
  code = tsdbSnapWriteTimeSeriesRow(writer, &row);
H
Hongze Cheng 已提交
868
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
869

H
Hongze Cheng 已提交
870 871 872 873
  STombRecord record = {
      .suid = INT64_MAX,
      .uid = INT64_MAX,
  };
H
Hongze Cheng 已提交
874

H
Hongze Cheng 已提交
875
  code = tsdbSnapWriteTombRecord(writer, &record);
H
Hongze Cheng 已提交
876
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
877

H
Hongze Cheng 已提交
878 879
  // close write
  code = tsdbSnapWriteFileSetCloseWriter(writer);
H
Hongze Cheng 已提交
880
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
881

H
Hongze Cheng 已提交
882
  code = tsdbSnapWriteFileSetCloseIter(writer);
H
Hongze Cheng 已提交
883
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
884

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

H
Hongze Cheng 已提交
888
  writer->ctx->fsetWriteBegin = false;
H
Hongze Cheng 已提交
889

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

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

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

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

H
Hongze Cheng 已提交
906 907 908 909
  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 已提交
910

H
Hongze Cheng 已提交
911
    code = tsdbSnapWriteFileSetBegin(writer, fid);
H
Hongze Cheng 已提交
912
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
913 914
  }

H
Hongze Cheng 已提交
915 916 917 918 919 920 921 922 923
  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 已提交
924 925
  }

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

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

H
Hongze Cheng 已提交
941 942 943 944
  int64_t size = hdr->size - sizeof(*hdr);
  ASSERT(size % TOMB_RECORD_ELEM_NUM == 0);
  size = size / TOMB_RECORD_ELEM_NUM;
  ASSERT(size % sizeof(int64_t) == 0);
H
Hongze Cheng 已提交
945

H
Hongze Cheng 已提交
946 947 948 949 950
  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 已提交
951

H
Hongze Cheng 已提交
952 953 954
_exit:
  return code;
}
H
Hongze Cheng 已提交
955

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

H
Hongze Cheng 已提交
960 961
  STombRecord record;
  STombBlock  tombBlock[1] = {0};
H
Hongze Cheng 已提交
962

H
Hongze Cheng 已提交
963 964
  code = tsdbSnapWriteDecmprTombBlock(hdr, tombBlock);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
965

H
Hongze Cheng 已提交
966 967 968 969 970
  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 已提交
971

H
Hongze Cheng 已提交
972 973 974
    code = tsdbSnapWriteFileSetBegin(writer, fid);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
975

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

H
Hongze Cheng 已提交
982
    code = tsdbSnapWriteTimeSeriesRow(writer, &row);
H
Hongze Cheng 已提交
983
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
984 985
  }

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

H
Hongze Cheng 已提交
988 989
  for (int32_t i = 0; i < TOMB_BLOCK_SIZE(tombBlock); ++i) {
    tTombBlockGet(tombBlock, i, &record);
H
Hongze Cheng 已提交
990

H
Hongze Cheng 已提交
991
    code = tsdbSnapWriteTombRecord(writer, &record);
H
Hongze Cheng 已提交
992 993 994
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
995
  tTombBlockDestroy(tombBlock);
H
Hongze Cheng 已提交
996 997 998

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

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

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

H
Hongze Cheng 已提交
1026 1027
  tsdbFSDisableBgTask(pTsdb->pFS);

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

H
Hongze Cheng 已提交
1037
int32_t tsdbSnapWriterPrepareClose(STsdbSnapWriter* writer) {
H
Hongze Cheng 已提交
1038
  int32_t code = 0;
H
Hongze Cheng 已提交
1039 1040
  int32_t lino = 0;

H
Hongze Cheng 已提交
1041 1042
  code = tsdbSnapWriteFileSetEnd(writer);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1043

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

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

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

H
Hongze Cheng 已提交
1059 1060 1061
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1062
  STsdb* tsdb = writer[0]->tsdb;
H
Hongze Cheng 已提交
1063 1064

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

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

H
Hongze Cheng 已提交
1076
    taosThreadRwlockUnlock(&writer[0]->tsdb->rwLock);
H
Hongze Cheng 已提交
1077
  }
H
Hongze Cheng 已提交
1078
  tsdbFSEnableBgTask(tsdb->pFS);
H
Hongze Cheng 已提交
1079

H
Hongze Cheng 已提交
1080 1081 1082 1083 1084 1085
  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 已提交
1086

H
Hongze Cheng 已提交
1087 1088
  TARRAY2_DESTROY(writer[0]->fopArr, NULL);
  tsdbFSDestroyCopySnapshot(&writer[0]->fsetArr);
H
Hongze Cheng 已提交
1089

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

H
Hongze Cheng 已提交
1094 1095
  taosMemoryFree(writer[0]);
  writer[0] = NULL;
H
Hongze Cheng 已提交
1096 1097 1098

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

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