tsdbCommit.c 35.4 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

H
Hongze Cheng 已提交
16
#include "tsdb.h"
H
Hongze Cheng 已提交
17 18 19 20 21
typedef struct {
  int64_t   suid;
  int64_t   uid;
  STSchema *pTSchema;
} SSkmInfo;
H
Hongze Cheng 已提交
22

H
Hongze Cheng 已提交
23
typedef struct {
H
Hongze Cheng 已提交
24
  STsdb *pTsdb;
H
Hongze Cheng 已提交
25
  int8_t toMerge;
H
Hongze Cheng 已提交
26
  /* commit data */
H
Hongze Cheng 已提交
27
  int64_t commitID;
H
Hongze Cheng 已提交
28 29
  int32_t minutes;
  int8_t  precision;
H
Hongze Cheng 已提交
30 31
  int32_t minRow;
  int32_t maxRow;
H
Hongze Cheng 已提交
32
  int8_t  cmprAlg;
H
Hongze Cheng 已提交
33
  int8_t  maxLast;
H
Hongze Cheng 已提交
34 35
  SArray *aTbDataP;  // memory
  STsdbFS fs;        // disk
H
Hongze Cheng 已提交
36
  // --------------
H
Hongze Cheng 已提交
37
  TSKEY   nextKey;  // reset by each table commit
H
Hongze Cheng 已提交
38 39 40
  int32_t commitFid;
  TSKEY   minKey;
  TSKEY   maxKey;
H
Hongze Cheng 已提交
41
  // commit file data
H
Hongze Cheng 已提交
42 43
  struct {
    SDataFReader *pReader;
H
Hongze Cheng 已提交
44 45 46 47 48
    SArray       *aBlockIdx;  // SArray<SBlockIdx>
    int32_t       iBlockIdx;
    SBlockIdx    *pBlockIdx;
    SMapData      mBlock;  // SMapData<SBlock>
    SBlockData    bData;
H
Hongze Cheng 已提交
49 50 51 52
  } dReader;
  struct {
    SDataFWriter *pWriter;
    SArray       *aBlockIdx;  // SArray<SBlockIdx>
H
Hongze Cheng 已提交
53
    SArray       *aBlockL;    // SArray<SBlockL>
H
Hongze Cheng 已提交
54 55
    SMapData      mBlock;     // SMapData<SBlock>
    SBlockData    bData;
H
Hongze Cheng 已提交
56
    SBlockData    bDatal;
H
Hongze Cheng 已提交
57 58 59
  } dWriter;
  SSkmInfo skmTable;
  SSkmInfo skmRow;
H
Hongze Cheng 已提交
60
  /* commit del */
H
Hongze Cheng 已提交
61 62
  SDelFReader *pDelFReader;
  SDelFWriter *pDelFWriter;
H
Hongze Cheng 已提交
63 64 65
  SArray      *aDelIdx;   // SArray<SDelIdx>
  SArray      *aDelIdxN;  // SArray<SDelIdx>
  SArray      *aDelData;  // SArray<SDelData>
H
Hongze Cheng 已提交
66
} SCommitter;
H
refact  
Hongze Cheng 已提交
67

H
Hongze Cheng 已提交
68 69 70 71 72
static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitter *pCommitter);
static int32_t tsdbCommitData(SCommitter *pCommitter);
static int32_t tsdbCommitDel(SCommitter *pCommitter);
static int32_t tsdbCommitCache(SCommitter *pCommitter);
static int32_t tsdbEndCommit(SCommitter *pCommitter, int32_t eno);
H
refact  
Hongze Cheng 已提交
73

H
refact  
Hongze Cheng 已提交
74
int32_t tsdbBegin(STsdb *pTsdb) {
H
Hongze Cheng 已提交
75
  int32_t code = 0;
H
Hongze Cheng 已提交
76

77 78
  if (!pTsdb) return code;

H
Hongze Cheng 已提交
79 80
  SMemTable *pMemTable;
  code = tsdbMemTableCreate(pTsdb, &pMemTable);
H
Hongze Cheng 已提交
81
  if (code) goto _err;
H
Hongze Cheng 已提交
82

H
Hongze Cheng 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  // lock
  code = taosThreadRwlockWrlock(&pTsdb->rwLock);
  if (code) {
    code = TAOS_SYSTEM_ERROR(code);
    goto _err;
  }

  pTsdb->mem = pMemTable;

  // unlock
  code = taosThreadRwlockUnlock(&pTsdb->rwLock);
  if (code) {
    code = TAOS_SYSTEM_ERROR(code);
    goto _err;
  }

H
Hongze Cheng 已提交
99 100 101
  return code;

_err:
S
Shengliang Guan 已提交
102
  tsdbError("vgId:%d, tsdb begin failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
103
  return code;
H
Hongze Cheng 已提交
104 105
}

H
more  
Hongze Cheng 已提交
106
int32_t tsdbCommit(STsdb *pTsdb) {
107
  if (!pTsdb) return 0;
H
Hongze Cheng 已提交
108

H
more  
Hongze Cheng 已提交
109
  int32_t    code = 0;
H
Hongze Cheng 已提交
110 111 112 113
  SCommitter commith;
  SMemTable *pMemTable = pTsdb->mem;

  // check
H
Hongze Cheng 已提交
114
  if (pMemTable->nRow == 0 && pMemTable->nDel == 0) {
H
Hongze Cheng 已提交
115
    taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
116
    pTsdb->mem = NULL;
H
Hongze Cheng 已提交
117 118 119
    taosThreadRwlockUnlock(&pTsdb->rwLock);

    tsdbUnrefMemTable(pMemTable);
H
Hongze Cheng 已提交
120 121
    goto _exit;
  }
H
refact  
Hongze Cheng 已提交
122

H
more  
Hongze Cheng 已提交
123
  // start commit
H
more  
Hongze Cheng 已提交
124
  code = tsdbStartCommit(pTsdb, &commith);
H
Hongze Cheng 已提交
125
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
126

H
refact  
Hongze Cheng 已提交
127 128
  // commit impl
  code = tsdbCommitData(&commith);
H
Hongze Cheng 已提交
129
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
130 131

  code = tsdbCommitDel(&commith);
H
Hongze Cheng 已提交
132
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
133 134

  // end commit
H
more  
Hongze Cheng 已提交
135
  code = tsdbEndCommit(&commith, 0);
H
Hongze Cheng 已提交
136
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
137

H
Hongze Cheng 已提交
138
_exit:
H
refact  
Hongze Cheng 已提交
139 140 141
  return code;

_err:
H
Hongze Cheng 已提交
142
  tsdbEndCommit(&commith, code);
C
Cary Xu 已提交
143
  tsdbError("vgId:%d, failed to commit since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
refact  
Hongze Cheng 已提交
144 145 146
  return code;
}

H
Hongze Cheng 已提交
147
static int32_t tsdbCommitDelStart(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
148 149 150 151
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

H
Hongze Cheng 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  pCommitter->aDelIdx = taosArrayInit(0, sizeof(SDelIdx));
  if (pCommitter->aDelIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  pCommitter->aDelData = taosArrayInit(0, sizeof(SDelData));
  if (pCommitter->aDelData == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  pCommitter->aDelIdxN = taosArrayInit(0, sizeof(SDelIdx));
  if (pCommitter->aDelIdxN == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
169

H
Hongze Cheng 已提交
170
  SDelFile *pDelFileR = pCommitter->fs.pDelFile;
H
Hongze Cheng 已提交
171
  if (pDelFileR) {
H
Hongze Cheng 已提交
172
    code = tsdbDelFReaderOpen(&pCommitter->pDelFReader, pDelFileR, pTsdb);
H
Hongze Cheng 已提交
173
    if (code) goto _err;
H
Hongze Cheng 已提交
174

H
Hongze Cheng 已提交
175
    code = tsdbReadDelIdx(pCommitter->pDelFReader, pCommitter->aDelIdx);
H
Hongze Cheng 已提交
176
    if (code) goto _err;
H
Hongze Cheng 已提交
177 178
  }

H
Hongze Cheng 已提交
179
  // prepare new
H
Hongze Cheng 已提交
180 181
  SDelFile wDelFile = {.commitID = pCommitter->commitID, .size = 0, .offset = 0};
  code = tsdbDelFWriterOpen(&pCommitter->pDelFWriter, &wDelFile, pTsdb);
H
Hongze Cheng 已提交
182
  if (code) goto _err;
H
Hongze Cheng 已提交
183 184

_exit:
S
Shengliang Guan 已提交
185
  tsdbDebug("vgId:%d, commit del start", TD_VID(pTsdb->pVnode));
H
Hongze Cheng 已提交
186 187 188
  return code;

_err:
S
Shengliang Guan 已提交
189
  tsdbError("vgId:%d, commit del start failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
190 191 192
  return code;
}

H
Hongze Cheng 已提交
193
static int32_t tsdbCommitTableDel(SCommitter *pCommitter, STbData *pTbData, SDelIdx *pDelIdx) {
H
Hongze Cheng 已提交
194
  int32_t   code = 0;
H
Hongze Cheng 已提交
195
  SDelData *pDelData;
H
Hongze Cheng 已提交
196 197
  tb_uid_t  suid;
  tb_uid_t  uid;
H
Hongze Cheng 已提交
198 199

  if (pTbData) {
H
Hongze Cheng 已提交
200 201
    suid = pTbData->suid;
    uid = pTbData->uid;
H
Hongze Cheng 已提交
202

H
Hongze Cheng 已提交
203 204 205 206
    if (pTbData->pHead == NULL) {
      pTbData = NULL;
    }
  }
H
Hongze Cheng 已提交
207 208

  if (pDelIdx) {
H
Hongze Cheng 已提交
209 210 211
    suid = pDelIdx->suid;
    uid = pDelIdx->uid;

H
Hongze Cheng 已提交
212
    code = tsdbReadDelData(pCommitter->pDelFReader, pDelIdx, pCommitter->aDelData);
H
Hongze Cheng 已提交
213
    if (code) goto _err;
214 215
  } else {
    taosArrayClear(pCommitter->aDelData);
H
Hongze Cheng 已提交
216 217
  }

H
Hongze Cheng 已提交
218
  if (pTbData == NULL && pDelIdx == NULL) goto _exit;
H
Hongze Cheng 已提交
219

H
Hongze Cheng 已提交
220
  SDelIdx delIdx = {.suid = suid, .uid = uid};
H
Hongze Cheng 已提交
221 222

  // memory
H
Hongze Cheng 已提交
223 224
  pDelData = pTbData ? pTbData->pHead : NULL;
  for (; pDelData; pDelData = pDelData->pNext) {
H
Hongze Cheng 已提交
225 226 227 228
    if (taosArrayPush(pCommitter->aDelData, pDelData) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
229 230 231
  }

  // write
H
Hongze Cheng 已提交
232
  code = tsdbWriteDelData(pCommitter->pDelFWriter, pCommitter->aDelData, &delIdx);
H
Hongze Cheng 已提交
233 234 235
  if (code) goto _err;

  // put delIdx
236
  if (taosArrayPush(pCommitter->aDelIdxN, &delIdx) == NULL) {
H
Hongze Cheng 已提交
237 238 239
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
240 241 242 243 244

_exit:
  return code;

_err:
S
Shengliang Guan 已提交
245
  tsdbError("vgId:%d, commit table del failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
246 247 248
  return code;
}

H
Hongze Cheng 已提交
249 250
static int32_t tsdbCommitDelEnd(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
251
  STsdb  *pTsdb = pCommitter->pTsdb;
H
Hongze Cheng 已提交
252

H
Hongze Cheng 已提交
253
  code = tsdbWriteDelIdx(pCommitter->pDelFWriter, pCommitter->aDelIdxN);
H
Hongze Cheng 已提交
254
  if (code) goto _err;
H
Hongze Cheng 已提交
255

H
Hongze Cheng 已提交
256 257 258
  code = tsdbUpdateDelFileHdr(pCommitter->pDelFWriter);
  if (code) goto _err;

H
Hongze Cheng 已提交
259
  code = tsdbFSUpsertDelFile(&pCommitter->fs, &pCommitter->pDelFWriter->fDel);
H
Hongze Cheng 已提交
260
  if (code) goto _err;
H
Hongze Cheng 已提交
261

H
Hongze Cheng 已提交
262
  code = tsdbDelFWriterClose(&pCommitter->pDelFWriter, 1);
H
Hongze Cheng 已提交
263
  if (code) goto _err;
H
Hongze Cheng 已提交
264 265

  if (pCommitter->pDelFReader) {
H
Hongze Cheng 已提交
266
    code = tsdbDelFReaderClose(&pCommitter->pDelFReader);
H
Hongze Cheng 已提交
267 268 269
    if (code) goto _err;
  }

H
Hongze Cheng 已提交
270 271 272 273
  taosArrayDestroy(pCommitter->aDelIdx);
  taosArrayDestroy(pCommitter->aDelData);
  taosArrayDestroy(pCommitter->aDelIdxN);

H
Hongze Cheng 已提交
274 275 276
  return code;

_err:
S
Shengliang Guan 已提交
277
  tsdbError("vgId:%d, commit del end failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
278 279 280
  return code;
}

H
Hongze Cheng 已提交
281
static int32_t tsdbCommitterUpdateTableSchema(SCommitter *pCommitter, int64_t suid, int64_t uid) {
H
Hongze Cheng 已提交
282 283
  int32_t code = 0;

H
Hongze Cheng 已提交
284 285 286 287
  if (suid) {
    if (pCommitter->skmTable.suid == suid) goto _exit;
  } else {
    if (pCommitter->skmTable.uid == uid) goto _exit;
H
Hongze Cheng 已提交
288 289 290 291 292
  }

  pCommitter->skmTable.suid = suid;
  pCommitter->skmTable.uid = uid;
  tTSchemaDestroy(pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
293
  code = metaGetTbTSchemaEx(pCommitter->pTsdb->pVnode->pMeta, suid, uid, -1, &pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  if (code) goto _exit;

_exit:
  return code;
}

static int32_t tsdbCommitterUpdateRowSchema(SCommitter *pCommitter, int64_t suid, int64_t uid, int32_t sver) {
  int32_t code = 0;

  if (pCommitter->skmRow.pTSchema) {
    if (pCommitter->skmRow.suid == suid) {
      if (suid == 0) {
        if (pCommitter->skmRow.uid == uid && sver == pCommitter->skmRow.pTSchema->version) goto _exit;
      } else {
        if (sver == pCommitter->skmRow.pTSchema->version) goto _exit;
      }
    }
  }

  pCommitter->skmRow.suid = suid;
  pCommitter->skmRow.uid = uid;
  tTSchemaDestroy(pCommitter->skmRow.pTSchema);
  code = metaGetTbTSchemaEx(pCommitter->pTsdb->pVnode->pMeta, suid, uid, sver, &pCommitter->skmRow.pTSchema);
  if (code) {
    goto _exit;
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
325 326 327 328 329 330 331 332 333 334
static int32_t tsdbCommitterNextTableData(SCommitter *pCommitter) {
  int32_t code = 0;

  ASSERT(pCommitter->dReader.pBlockIdx);

  pCommitter->dReader.iBlockIdx++;
  if (pCommitter->dReader.iBlockIdx < taosArrayGetSize(pCommitter->dReader.aBlockIdx)) {
    pCommitter->dReader.pBlockIdx =
        (SBlockIdx *)taosArrayGet(pCommitter->dReader.aBlockIdx, pCommitter->dReader.iBlockIdx);

H
Hongze Cheng 已提交
335
    code = tsdbReadBlock(pCommitter->dReader.pReader, pCommitter->dReader.pBlockIdx, &pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
336 337 338 339 340 341 342 343 344 345 346
    if (code) goto _exit;

    ASSERT(pCommitter->dReader.mBlock.nItem > 0);
  } else {
    pCommitter->dReader.pBlockIdx = NULL;
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
347 348 349 350
static int32_t tsdbCommitFileDataStart(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SDFileSet *pRSet = NULL;
H
Hongze Cheng 已提交
351

H
Hongze Cheng 已提交
352
  // memory
H
Hongze Cheng 已提交
353 354 355
  pCommitter->commitFid = tsdbKeyFid(pCommitter->nextKey, pCommitter->minutes, pCommitter->precision);
  tsdbFidKeyRange(pCommitter->commitFid, pCommitter->minutes, pCommitter->precision, &pCommitter->minKey,
                  &pCommitter->maxKey);
H
Hongze Cheng 已提交
356
  pCommitter->nextKey = TSKEY_MAX;
H
Hongze Cheng 已提交
357

H
Hongze Cheng 已提交
358
  // Reader
H
Hongze Cheng 已提交
359 360
  pRSet = (SDFileSet *)taosArraySearch(pCommitter->fs.aDFileSet, &(SDFileSet){.fid = pCommitter->commitFid},
                                       tDFileSetCmprFn, TD_EQ);
H
Hongze Cheng 已提交
361
  if (pRSet) {
H
Hongze Cheng 已提交
362
    code = tsdbDataFReaderOpen(&pCommitter->dReader.pReader, pTsdb, pRSet);
H
Hongze Cheng 已提交
363 364
    if (code) goto _err;

H
Hongze Cheng 已提交
365
    // data
H
Hongze Cheng 已提交
366
    code = tsdbReadBlockIdx(pCommitter->dReader.pReader, pCommitter->dReader.aBlockIdx);
H
Hongze Cheng 已提交
367
    if (code) goto _err;
H
Hongze Cheng 已提交
368

H
Hongze Cheng 已提交
369 370 371 372 373
    pCommitter->dReader.iBlockIdx = 0;
    if (pCommitter->dReader.iBlockIdx < taosArrayGetSize(pCommitter->dReader.aBlockIdx)) {
      pCommitter->dReader.pBlockIdx =
          (SBlockIdx *)taosArrayGet(pCommitter->dReader.aBlockIdx, pCommitter->dReader.iBlockIdx);

H
Hongze Cheng 已提交
374
      code = tsdbReadBlock(pCommitter->dReader.pReader, pCommitter->dReader.pBlockIdx, &pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
375 376 377 378
      if (code) goto _err;
    } else {
      pCommitter->dReader.pBlockIdx = NULL;
    }
H
Hongze Cheng 已提交
379
    tBlockDataReset(&pCommitter->dReader.bData);
H
Hongze Cheng 已提交
380
  } else {
H
Hongze Cheng 已提交
381
    pCommitter->dReader.pBlockIdx = NULL;
H
Hongze Cheng 已提交
382
  }
H
Hongze Cheng 已提交
383

H
Hongze Cheng 已提交
384
  // Writer
H
Hongze Cheng 已提交
385 386 387
  SHeadFile fHead;
  SDataFile fData;
  SSmaFile  fSma;
H
Hongze Cheng 已提交
388 389
  SLastFile fLast;
  SDFileSet wSet = {0};
H
Hongze Cheng 已提交
390
  if (pRSet) {
H
Hongze Cheng 已提交
391
    ASSERT(pRSet->nLastF < pCommitter->maxLast);
H
Hongze Cheng 已提交
392

H
Hongze Cheng 已提交
393
    fHead = (SHeadFile){.commitID = pCommitter->commitID};
H
Hongze Cheng 已提交
394 395
    fData = *pRSet->pDataF;
    fSma = *pRSet->pSmaF;
H
Hongze Cheng 已提交
396 397 398 399 400 401 402 403 404 405 406 407
    fLast = (SLastFile){.commitID = pCommitter->commitID};

    wSet.diskId = pRSet->diskId;
    wSet.fid = pCommitter->commitFid;
    wSet.pHeadF = &fHead;
    wSet.pDataF = &fData;
    wSet.pSmaF = &fSma;
    for (int8_t iLast = 0; iLast < pRSet->nLastF; iLast++) {
      wSet.aLastF[iLast] = pRSet->aLastF[iLast];
    }
    wSet.nLastF = pRSet->nLastF + 1;
    wSet.aLastF[wSet.nLastF - 1] = &fLast;  // todo
H
Hongze Cheng 已提交
408 409 410 411

    if (wSet.nLastF == pCommitter->maxLast) {
      pCommitter->toMerge = 1;
    }
H
Hongze Cheng 已提交
412
  } else {
H
Hongze Cheng 已提交
413 414 415 416
    fHead = (SHeadFile){.commitID = pCommitter->commitID};
    fData = (SDataFile){.commitID = pCommitter->commitID};
    fSma = (SSmaFile){.commitID = pCommitter->commitID};
    fLast = (SLastFile){.commitID = pCommitter->commitID};
417

H
Hongze Cheng 已提交
418
    SDiskID did = {0};
419
    tfsAllocDisk(pTsdb->pVnode->pTfs, 0, &did);
H
Hongze Cheng 已提交
420 421
    tfsMkdirRecurAt(pTsdb->pVnode->pTfs, pTsdb->path, did);

422
    wSet.diskId = did;
H
Hongze Cheng 已提交
423
    wSet.fid = pCommitter->commitFid;
H
Hongze Cheng 已提交
424 425 426
    wSet.pHeadF = &fHead;
    wSet.pDataF = &fData;
    wSet.pSmaF = &fSma;
H
Hongze Cheng 已提交
427
    wSet.nLastF = 1;
H
Hongze Cheng 已提交
428
    wSet.aLastF[0] = &fLast;
H
Hongze Cheng 已提交
429
  }
H
Hongze Cheng 已提交
430
  code = tsdbDataFWriterOpen(&pCommitter->dWriter.pWriter, pTsdb, &wSet);
H
Hongze Cheng 已提交
431
  if (code) goto _err;
H
Hongze Cheng 已提交
432

H
Hongze Cheng 已提交
433 434 435 436 437 438
  taosArrayClear(pCommitter->dWriter.aBlockIdx);
  taosArrayClear(pCommitter->dWriter.aBlockL);
  tMapDataReset(&pCommitter->dWriter.mBlock);
  tBlockDataReset(&pCommitter->dWriter.bData);
  tBlockDataReset(&pCommitter->dWriter.bDatal);

H
Hongze Cheng 已提交
439
_exit:
H
Hongze Cheng 已提交
440 441 442
  return code;

_err:
S
Shengliang Guan 已提交
443
  tsdbError("vgId:%d, commit file data start failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
444
  return code;
H
Hongze Cheng 已提交
445 446
}

H
Hongze Cheng 已提交
447
static int32_t tsdbCommitDataBlock(SCommitter *pCommitter, SBlock *pBlock) {
H
Hongze Cheng 已提交
448 449 450
  int32_t     code = 0;
  SBlockData *pBlockData = &pCommitter->dWriter.bData;
  SBlock      block;
H
Hongze Cheng 已提交
451

H
Hongze Cheng 已提交
452
  ASSERT(pBlockData->nRow > 0);
H
Hongze Cheng 已提交
453

H
Hongze Cheng 已提交
454 455 456 457 458 459
  if (pBlock) {
    block = *pBlock;  // as a subblock
  } else {
    tBlockReset(&block);  // as a new block
  }

H
Hongze Cheng 已提交
460
  // info
H
Hongze Cheng 已提交
461 462 463
  block.nRow += pBlockData->nRow;
  for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
    TSDBKEY key = {.ts = pBlockData->aTSKEY[iRow], .version = pBlockData->aVersion[iRow]};
H
Hongze Cheng 已提交
464

H
Hongze Cheng 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
    if (iRow == 0) {
      if (tsdbKeyCmprFn(&block.minKey, &key) > 0) {
        block.minKey = key;
      }
    } else {
      if (pBlockData->aTSKEY[iRow] == pBlockData->aTSKEY[iRow - 1]) {
        block.hasDup = 1;
      }
    }

    if (iRow == pBlockData->nRow - 1 && tsdbKeyCmprFn(&block.maxKey, &key) < 0) {
      block.maxKey = key;
    }

    block.minVer = TMIN(block.minVer, key.version);
    block.maxVer = TMAX(block.maxVer, key.version);
  }

  // write
H
Hongze Cheng 已提交
484 485
  block.nSubBlock++;
  code = tsdbWriteBlockData(pCommitter->dWriter.pWriter, pBlockData, &block.aSubBlock[block.nSubBlock - 1],
H
Hongze Cheng 已提交
486
                            ((block.nSubBlock == 1) && !block.hasDup) ? &block.smaInfo : NULL, pCommitter->cmprAlg, 0);
H
Hongze Cheng 已提交
487 488 489
  if (code) goto _err;

  // put SBlock
H
Hongze Cheng 已提交
490
  code = tMapDataPutItem(&pCommitter->dWriter.mBlock, &block, tPutBlock);
H
Hongze Cheng 已提交
491
  if (code) goto _err;
H
Hongze Cheng 已提交
492

H
Hongze Cheng 已提交
493
  // clear
H
Hongze Cheng 已提交
494
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
495

H
Hongze Cheng 已提交
496 497 498 499
  return code;

_err:
  tsdbError("vgId:%d tsdb commit data block failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
500 501 502 503
  return code;
}

static int32_t tsdbCommitLastBlock(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
504 505 506 507 508 509
  int32_t     code = 0;
  SBlockL     blockL;
  SBlockData *pBlockData = &pCommitter->dWriter.bDatal;

  ASSERT(pBlockData->nRow > 0);

H
Hongze Cheng 已提交
510
  // info
H
Hongze Cheng 已提交
511 512
  blockL.suid = pBlockData->suid;
  blockL.nRow = pBlockData->nRow;
H
Hongze Cheng 已提交
513 514
  blockL.minKey = TSKEY_MAX;
  blockL.maxKey = TSKEY_MIN;
H
Hongze Cheng 已提交
515 516 517
  blockL.minVer = VERSION_MAX;
  blockL.maxVer = VERSION_MIN;
  for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
H
Hongze Cheng 已提交
518 519
    blockL.minKey = TMIN(blockL.minKey, pBlockData->aTSKEY[iRow]);
    blockL.maxKey = TMAX(blockL.maxKey, pBlockData->aTSKEY[iRow]);
H
Hongze Cheng 已提交
520
    blockL.minVer = TMIN(blockL.minVer, pBlockData->aVersion[iRow]);
H
Hongze Cheng 已提交
521
    blockL.maxVer = TMAX(blockL.maxVer, pBlockData->aVersion[iRow]);
H
Hongze Cheng 已提交
522 523 524
  }
  blockL.minUid = pBlockData->uid ? pBlockData->uid : pBlockData->aUid[0];
  blockL.maxUid = pBlockData->uid ? pBlockData->uid : pBlockData->aUid[pBlockData->nRow - 1];
H
Hongze Cheng 已提交
525

H
Hongze Cheng 已提交
526
  // write
H
Hongze Cheng 已提交
527
  code = tsdbWriteBlockData(pCommitter->dWriter.pWriter, pBlockData, &blockL.bInfo, NULL, pCommitter->cmprAlg, 1);
H
Hongze Cheng 已提交
528
  if (code) goto _err;
H
Hongze Cheng 已提交
529

H
Hongze Cheng 已提交
530
  // push SBlockL
H
Hongze Cheng 已提交
531 532
  if (taosArrayPush(pCommitter->dWriter.aBlockL, &blockL) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
533
    goto _err;
H
Hongze Cheng 已提交
534 535
  }

H
Hongze Cheng 已提交
536
  // clear
H
Hongze Cheng 已提交
537
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
538

H
Hongze Cheng 已提交
539 540 541 542
  return code;

_err:
  tsdbError("vgId:%d tsdb commit last block failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
543 544 545
  return code;
}

H
Hongze Cheng 已提交
546
static int32_t tsdbMergeCommitDataBlock(SCommitter *pCommitter, STbDataIter *pIter, SBlock *pBlock) {
H
Hongze Cheng 已提交
547
  int32_t     code = 0;
H
Hongze Cheng 已提交
548 549 550
  STbData    *pTbData = pIter->pTbData;
  SBlockData *pBlockDataR = &pCommitter->dReader.bData;
  SBlockData *pBlockDataW = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
551

H
Hongze Cheng 已提交
552
  code = tsdbReadDataBlock(pCommitter->dReader.pReader, pBlock, pBlockDataR);
H
Hongze Cheng 已提交
553 554
  if (code) goto _err;

H
Hongze Cheng 已提交
555
  tBlockDataClear(pBlockDataW);
H
Hongze Cheng 已提交
556 557 558 559 560 561 562
  int32_t  iRow = 0;
  TSDBROW  row;
  TSDBROW *pRow1 = tsdbTbDataIterGet(pIter);
  TSDBROW *pRow2 = &row;
  *pRow2 = tsdbRowFromBlockData(pBlockDataR, iRow);
  while (pRow1 && pRow2) {
    int32_t c = tsdbRowCmprFn(pRow1, pRow2);
H
Hongze Cheng 已提交
563

H
Hongze Cheng 已提交
564 565 566
    if (c < 0) {
      code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow1));
      if (code) goto _err;
H
Hongze Cheng 已提交
567

H
Hongze Cheng 已提交
568
      code = tBlockDataAppendRow(pBlockDataW, pRow1, pCommitter->skmRow.pTSchema, pTbData->uid);
H
Hongze Cheng 已提交
569
      if (code) goto _err;
H
Hongze Cheng 已提交
570

H
Hongze Cheng 已提交
571 572 573 574
      // next
      tsdbTbDataIterNext(pIter);
      pRow1 = tsdbTbDataIterGet(pIter);
    } else if (c > 0) {
H
Hongze Cheng 已提交
575
      code = tBlockDataAppendRow(pBlockDataW, pRow2, NULL, pTbData->uid);
H
Hongze Cheng 已提交
576
      if (code) goto _err;
H
Hongze Cheng 已提交
577

H
Hongze Cheng 已提交
578 579 580
      iRow++;
      if (iRow < pBlockDataR->nRow) {
        *pRow2 = tsdbRowFromBlockData(pBlockDataR, iRow);
H
Hongze Cheng 已提交
581
      } else {
H
Hongze Cheng 已提交
582
        pRow2 = NULL;
H
Hongze Cheng 已提交
583
      }
H
Hongze Cheng 已提交
584 585
    } else {
      ASSERT(0);
H
Hongze Cheng 已提交
586 587
    }

H
Hongze Cheng 已提交
588 589
    // check
    if (pBlockDataW->nRow >= pCommitter->maxRow * 4 / 5) {
H
Hongze Cheng 已提交
590
      code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
591
      if (code) goto _err;
H
Hongze Cheng 已提交
592
    }
H
Hongze Cheng 已提交
593
  }
H
Hongze Cheng 已提交
594

H
Hongze Cheng 已提交
595
  while (pRow2) {
H
Hongze Cheng 已提交
596
    code = tBlockDataAppendRow(pBlockDataW, pRow2, NULL, pTbData->uid);
H
Hongze Cheng 已提交
597 598
    if (code) goto _err;

H
Hongze Cheng 已提交
599 600 601
    iRow++;
    if (iRow < pBlockDataR->nRow) {
      *pRow2 = tsdbRowFromBlockData(pBlockDataR, iRow);
H
Hongze Cheng 已提交
602
    } else {
H
Hongze Cheng 已提交
603
      pRow2 = NULL;
H
Hongze Cheng 已提交
604 605
    }

H
Hongze Cheng 已提交
606 607
    // check
    if (pBlockDataW->nRow >= pCommitter->maxRow * 4 / 5) {
H
Hongze Cheng 已提交
608
      code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
609
      if (code) goto _err;
H
Hongze Cheng 已提交
610
    }
H
Hongze Cheng 已提交
611
  }
H
Hongze Cheng 已提交
612

H
Hongze Cheng 已提交
613 614
  // check
  if (pBlockDataW->nRow > 0) {
H
Hongze Cheng 已提交
615
    code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
616
    if (code) goto _err;
H
Hongze Cheng 已提交
617 618 619 620 621
  }

  return code;

_err:
H
Hongze Cheng 已提交
622
  tsdbError("vgId:%d, tsdb merge commit data failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
623 624 625
  return code;
}

H
Hongze Cheng 已提交
626 627 628
static int32_t tsdbCommitTableMemData(SCommitter *pCommitter, STbDataIter *pIter, TSDBKEY toKey) {
  int32_t     code = 0;
  STbData    *pTbData = pIter->pTbData;
H
Hongze Cheng 已提交
629
  SBlockData *pBlockData = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
630

H
Hongze Cheng 已提交
631
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
632
  TSDBROW *pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
633
  while (true) {
H
Hongze Cheng 已提交
634
    if (pRow == NULL) {
H
Hongze Cheng 已提交
635 636 637 638 639 640 641
      if (pBlockData->nRow > 0) {
        goto _write_block;
      } else {
        break;
      }
    }

H
Hongze Cheng 已提交
642
    // update schema
H
Hongze Cheng 已提交
643
    code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow));
H
Hongze Cheng 已提交
644 645
    if (code) goto _err;

H
Hongze Cheng 已提交
646
    // append
H
Hongze Cheng 已提交
647
    code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema, pTbData->uid);
H
Hongze Cheng 已提交
648 649 650 651
    if (code) goto _err;

    tsdbTbDataIterNext(pIter);
    pRow = tsdbTbDataIterGet(pIter);
652
    if (pRow) {
H
Hongze Cheng 已提交
653 654
      TSDBKEY rowKey = TSDBROW_KEY(pRow);
      if (tsdbKeyCmprFn(&rowKey, &toKey) >= 0) {
655 656 657
        pRow = NULL;
      }
    }
H
Hongze Cheng 已提交
658

H
Hongze Cheng 已提交
659 660
    if (pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
    _write_block:
H
Hongze Cheng 已提交
661
      code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
662 663
      if (code) goto _err;
    }
H
Hongze Cheng 已提交
664 665 666 667 668
  }

  return code;

_err:
S
Shengliang Guan 已提交
669
  tsdbError("vgId:%d, tsdb commit table mem data failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
670 671 672
  return code;
}

H
Hongze Cheng 已提交
673 674
static int32_t tsdbGetNumOfRowsLessThan(STbDataIter *pIter, TSDBKEY key) {
  int32_t nRow = 0;
H
Hongze Cheng 已提交
675

H
Hongze Cheng 已提交
676
  STbDataIter iter = *pIter;
H
Hongze Cheng 已提交
677
  while (true) {
H
Hongze Cheng 已提交
678
    TSDBROW *pRow = tsdbTbDataIterGet(&iter);
H
Hongze Cheng 已提交
679 680
    if (pRow == NULL) break;

H
Hongze Cheng 已提交
681 682
    int32_t c = tsdbKeyCmprFn(&TSDBROW_KEY(pRow), &key);
    if (c < 0) {
H
Hongze Cheng 已提交
683
      nRow++;
H
Hongze Cheng 已提交
684
      tsdbTbDataIterNext(&iter);
H
Hongze Cheng 已提交
685 686 687 688 689 690 691 692 693 694 695 696
    } else if (c > 0) {
      break;
    } else {
      ASSERT(0);
    }
  }

  return nRow;
}

static int32_t tsdbMergeAsSubBlock(SCommitter *pCommitter, STbDataIter *pIter, SBlock *pBlock) {
  int32_t     code = 0;
H
Hongze Cheng 已提交
697
  STbData    *pTbData = pIter->pTbData;
H
Hongze Cheng 已提交
698
  SBlockData *pBlockData = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
699

H
Hongze Cheng 已提交
700
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
701
  TSDBROW *pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
702
  while (true) {
H
Hongze Cheng 已提交
703 704 705 706 707 708 709 710 711 712
    if (pRow == NULL) break;

    code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow));
    if (code) goto _err;

    code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema, pTbData->uid);
    if (code) goto _err;

    tsdbTbDataIterNext(pIter);
    pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
713
    if (pRow) {
H
Hongze Cheng 已提交
714 715
      TSDBKEY rowKey = TSDBROW_KEY(pRow);
      if (tsdbKeyCmprFn(&rowKey, &pBlock->maxKey) > 0) {
H
Hongze Cheng 已提交
716 717 718 719 720
        pRow = NULL;
      }
    }
  }

H
Hongze Cheng 已提交
721
  ASSERT(pBlockData->nRow > 0 && pBlock->nRow + pBlockData->nRow <= pCommitter->maxRow);
H
Hongze Cheng 已提交
722

H
Hongze Cheng 已提交
723
  code = tsdbCommitDataBlock(pCommitter, pBlock);
H
Hongze Cheng 已提交
724 725 726 727 728
  if (code) goto _err;

  return code;

_err:
S
Shengliang Guan 已提交
729
  tsdbError("vgId:%d, tsdb merge as subblock failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
730 731 732
  return code;
}

H
Hongze Cheng 已提交
733 734 735 736 737
static int32_t tsdbCommitLastFile(SCommitter *pCommitter, STbDataIter *pIter) {
  int32_t     code = 0;
  STbData    *pTbData = pIter->pTbData;
  SBlockData *pBlockData = &pCommitter->dWriter.bDatal;
  TSDBROW    *pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
738 739 740 741 742

  if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
    pRow = NULL;
  }

H
Hongze Cheng 已提交
743
  if (pRow == NULL) goto _exit;
H
Hongze Cheng 已提交
744

H
Hongze Cheng 已提交
745 746 747
  if (pBlockData->suid || pBlockData->uid) {
    if (pBlockData->suid != pTbData->suid || pBlockData->suid == 0) {
      if (pBlockData->nRow > 0) {
H
Hongze Cheng 已提交
748 749 750 751
        code = tsdbCommitLastBlock(pCommitter);
        if (code) goto _err;
      }

H
Hongze Cheng 已提交
752
      tBlockDataReset(pBlockData);
H
Hongze Cheng 已提交
753
    }
H
Hongze Cheng 已提交
754
  }
H
Hongze Cheng 已提交
755

H
Hongze Cheng 已提交
756 757 758 759
  if (!pBlockData->suid && !pBlockData->uid) {
    code = tBlockDataInit(pBlockData, pTbData->suid, pTbData->uid, pCommitter->skmTable.pTSchema);
    if (code) goto _err;
  }
H
Hongze Cheng 已提交
760

H
Hongze Cheng 已提交
761 762 763
  while (pRow) {
    code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow));
    if (code) goto _err;
H
Hongze Cheng 已提交
764

H
Hongze Cheng 已提交
765 766
    code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema, pTbData->uid);
    if (code) goto _err;
H
Hongze Cheng 已提交
767

H
Hongze Cheng 已提交
768 769 770 771
    tsdbTbDataIterNext(pIter);
    pRow = tsdbTbDataIterGet(pIter);
    if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
      pRow = NULL;
H
Hongze Cheng 已提交
772 773
    }

H
Hongze Cheng 已提交
774 775
    if (pBlockData->nRow >= pCommitter->maxRow) {
      code = tsdbCommitLastBlock(pCommitter);
H
Hongze Cheng 已提交
776
      if (code) goto _err;
H
Hongze Cheng 已提交
777 778 779
    }
  }

H
Hongze Cheng 已提交
780
_exit:
H
Hongze Cheng 已提交
781 782 783
  return code;

_err:
H
Hongze Cheng 已提交
784
  tsdbError("vgId:%d tsdb merge commit last failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
785 786 787
  return code;
}

H
Hongze Cheng 已提交
788
static int32_t tsdbMergeCommitData(SCommitter *pCommitter, STbDataIter *pIter) {
H
Hongze Cheng 已提交
789 790 791 792 793 794
  int32_t  code = 0;
  STbData *pTbData = pIter->pTbData;
  int32_t  iBlock = 0;
  SBlock   block;
  SBlock  *pBlock = &block;
  TSDBROW *pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
795

H
Hongze Cheng 已提交
796
  if (pCommitter->dReader.pBlockIdx && tTABLEIDCmprFn(pTbData, pCommitter->dReader.pBlockIdx) == 0) {
H
Hongze Cheng 已提交
797 798 799 800 801 802
    tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
  } else {
    pBlock = NULL;
  }

  while (pBlock && pRow) {
H
Hongze Cheng 已提交
803 804 805 806
    SBlock  tBlock = {.minKey = TSDBROW_KEY(pRow), .maxKey = TSDBROW_KEY(pRow)};
    int32_t c = tBlockCmprFn(pBlock, &tBlock);

    if (c < 0) {
H
Hongze Cheng 已提交
807 808 809 810 811 812 813 814 815
      code = tMapDataPutItem(&pCommitter->dWriter.mBlock, pBlock, tPutBlock);
      if (code) goto _err;

      iBlock++;
      if (iBlock < pCommitter->dReader.mBlock.nItem) {
        tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
      } else {
        pBlock = NULL;
      }
H
Hongze Cheng 已提交
816
    } else if (c > 0) {
H
Hongze Cheng 已提交
817
      code = tsdbCommitTableMemData(pCommitter, pIter, pBlock->minKey);
H
Hongze Cheng 已提交
818 819 820 821 822 823
      if (code) goto _err;

      pRow = tsdbTbDataIterGet(pIter);
      if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
        pRow = NULL;
      }
H
Hongze Cheng 已提交
824
    } else {
H
Hongze Cheng 已提交
825
      int32_t nOvlp = tsdbGetNumOfRowsLessThan(pIter, pBlock->maxKey);
H
Hongze Cheng 已提交
826 827 828 829 830 831 832

      ASSERT(nOvlp > 0);

      if (pBlock->nRow + nOvlp <= pCommitter->maxRow && pBlock->nSubBlock < TSDB_MAX_SUBBLOCKS) {
        code = tsdbMergeAsSubBlock(pCommitter, pIter, pBlock);
        if (code) goto _err;
      } else {
H
Hongze Cheng 已提交
833
        code = tsdbMergeCommitDataBlock(pCommitter, pIter, pBlock);
H
Hongze Cheng 已提交
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
        if (code) goto _err;
      }

      // next
      pRow = tsdbTbDataIterGet(pIter);
      if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
        pRow = NULL;
      }
      iBlock++;
      if (iBlock < pCommitter->dReader.mBlock.nItem) {
        tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
      } else {
        pBlock = NULL;
      }
    }
  }

  while (pBlock) {
    code = tMapDataPutItem(&pCommitter->dWriter.mBlock, pBlock, tPutBlock);
    if (code) goto _err;

    iBlock++;
    if (iBlock < pCommitter->dReader.mBlock.nItem) {
      tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
    } else {
      pBlock = NULL;
    }
  }

H
Hongze Cheng 已提交
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
_exit:
  return code;

_err:
  return code;
}

static int32_t tsdbCommitTableData(SCommitter *pCommitter, STbData *pTbData) {
  int32_t code = 0;

  ASSERT(pCommitter->dReader.pBlockIdx == NULL || tTABLEIDCmprFn(pCommitter->dReader.pBlockIdx, pTbData) >= 0);

  // merge commit table data
  STbDataIter iter = {0};
  TSDBROW    *pRow;

H
Hongze Cheng 已提交
879 880
  tMapDataReset(&pCommitter->dWriter.mBlock);

H
Hongze Cheng 已提交
881 882 883 884 885 886 887 888 889 890 891 892 893
  tsdbTbDataIterOpen(pTbData, &(TSDBKEY){.ts = pCommitter->minKey, .version = VERSION_MIN}, 0, &iter);
  pRow = tsdbTbDataIterGet(&iter);
  if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
    pRow = NULL;
  }
  if (pRow == NULL) {
    if (pCommitter->dReader.pBlockIdx && tTABLEIDCmprFn(pCommitter->dReader.pBlockIdx, pTbData) == 0) {
      code = tMapDataCopy(&pCommitter->dReader.mBlock, &pCommitter->dWriter.mBlock);
      if (code) goto _err;
    }
    goto _exit;
  }

H
Hongze Cheng 已提交
894 895 896 897 898 899 900
  code = tsdbCommitterUpdateTableSchema(pCommitter, pTbData->suid, pTbData->uid);
  if (code) goto _err;
  code = tBlockDataInit(&pCommitter->dReader.bData, pTbData->suid, pTbData->uid, pCommitter->skmTable.pTSchema);
  if (code) goto _err;
  code = tBlockDataInit(&pCommitter->dWriter.bData, pTbData->suid, pTbData->uid, pCommitter->skmTable.pTSchema);
  if (code) goto _err;

H
Hongze Cheng 已提交
901 902 903 904 905
  // commit data
  code = tsdbMergeCommitData(pCommitter, &iter);
  if (code) goto _err;

  // commit last
H
Hongze Cheng 已提交
906
  code = tsdbCommitLastFile(pCommitter, &iter);
H
Hongze Cheng 已提交
907
  if (code) goto _err;
H
Hongze Cheng 已提交
908

H
Hongze Cheng 已提交
909
_exit:
H
Hongze Cheng 已提交
910 911
  if (pCommitter->dWriter.mBlock.nItem > 0) {
    SBlockIdx blockIdx = {.suid = pTbData->suid, .uid = pTbData->uid};
H
Hongze Cheng 已提交
912
    code = tsdbWriteBlock(pCommitter->dWriter.pWriter, &pCommitter->dWriter.mBlock, &blockIdx);
H
Hongze Cheng 已提交
913 914 915 916 917 918 919
    if (code) goto _err;

    if (taosArrayPush(pCommitter->dWriter.aBlockIdx, &blockIdx) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
  }
H
Hongze Cheng 已提交
920
  pRow = tsdbTbDataIterGet(&iter);
H
Hongze Cheng 已提交
921 922 923 924 925 926 927 928 929 930 931
  if (pRow) {
    pCommitter->nextKey = TMIN(pCommitter->nextKey, TSDBROW_TS(pRow));
  }

  return code;

_err:
  tsdbError("vgId:%d tsdb commit table data failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
  return code;
}

H
Hongze Cheng 已提交
932 933 934
static int32_t tsdbCommitFileDataEnd(SCommitter *pCommitter) {
  int32_t code = 0;

H
Hongze Cheng 已提交
935
  // write aBlockIdx
H
Hongze Cheng 已提交
936
  code = tsdbWriteBlockIdx(pCommitter->dWriter.pWriter, pCommitter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
937 938
  if (code) goto _err;

H
Hongze Cheng 已提交
939
  // write aBlockL
H
Hongze Cheng 已提交
940
  code = tsdbWriteBlockL(pCommitter->dWriter.pWriter, pCommitter->dWriter.aBlockL);
H
Hongze Cheng 已提交
941 942
  if (code) goto _err;

H
Hongze Cheng 已提交
943
  // update file header
H
Hongze Cheng 已提交
944
  code = tsdbUpdateDFileSetHeader(pCommitter->dWriter.pWriter);
H
Hongze Cheng 已提交
945 946 947
  if (code) goto _err;

  // upsert SDFileSet
H
Hongze Cheng 已提交
948
  code = tsdbFSUpsertFSet(&pCommitter->fs, &pCommitter->dWriter.pWriter->wSet);
H
Hongze Cheng 已提交
949 950 951
  if (code) goto _err;

  // close and sync
H
Hongze Cheng 已提交
952
  code = tsdbDataFWriterClose(&pCommitter->dWriter.pWriter, 1);
H
Hongze Cheng 已提交
953 954
  if (code) goto _err;

H
Hongze Cheng 已提交
955 956
  if (pCommitter->dReader.pReader) {
    code = tsdbDataFReaderClose(&pCommitter->dReader.pReader);
H
Hongze Cheng 已提交
957
    if (code) goto _err;
H
Hongze Cheng 已提交
958 959 960 961 962 963
  }

_exit:
  return code;

_err:
S
Shengliang Guan 已提交
964
  tsdbError("vgId:%d, commit file data end failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
965 966 967
  return code;
}

H
Hongze Cheng 已提交
968 969 970
static int32_t tsdbMoveCommitData(SCommitter *pCommitter, TABLEID toTable) {
  int32_t code = 0;

H
Hongze Cheng 已提交
971
  // .data
H
Hongze Cheng 已提交
972 973 974 975
  while (true) {
    if (pCommitter->dReader.pBlockIdx == NULL || tTABLEIDCmprFn(pCommitter->dReader.pBlockIdx, &toTable) >= 0) break;

    SBlockIdx blockIdx = *pCommitter->dReader.pBlockIdx;
H
Hongze Cheng 已提交
976
    code = tsdbWriteBlock(pCommitter->dWriter.pWriter, &pCommitter->dReader.mBlock, &blockIdx);
H
Hongze Cheng 已提交
977 978 979 980 981 982 983
    if (code) goto _err;

    if (taosArrayPush(pCommitter->dWriter.aBlockIdx, &blockIdx) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }

H
Hongze Cheng 已提交
984 985
    code = tsdbCommitterNextTableData(pCommitter);
    if (code) goto _err;
H
Hongze Cheng 已提交
986 987 988 989 990 991 992 993 994
  }

  return code;

_err:
  tsdbError("vgId:%d tsdb move commit data failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
  return code;
}

H
Hongze Cheng 已提交
995
static int32_t tsdbCommitFileData(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
996 997 998
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
999 1000 1001 1002 1003 1004

  // commit file data start
  code = tsdbCommitFileDataStart(pCommitter);
  if (code) goto _err;

  // commit file data impl
H
Hongze Cheng 已提交
1005 1006
  for (int32_t iTbData = 0; iTbData < taosArrayGetSize(pCommitter->aTbDataP); iTbData++) {
    STbData *pTbData = (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData);
H
Hongze Cheng 已提交
1007

H
Hongze Cheng 已提交
1008
    // move commit until current (suid, uid)
H
Hongze Cheng 已提交
1009
    code = tsdbMoveCommitData(pCommitter, *(TABLEID *)pTbData);
H
Hongze Cheng 已提交
1010 1011
    if (code) goto _err;

H
Hongze Cheng 已提交
1012
    // commit current table data
H
Hongze Cheng 已提交
1013
    code = tsdbCommitTableData(pCommitter, pTbData);
H
Hongze Cheng 已提交
1014
    if (code) goto _err;
H
Hongze Cheng 已提交
1015 1016 1017 1018 1019 1020

    // move next reader table data if need
    if (pCommitter->dReader.pBlockIdx && tTABLEIDCmprFn(pTbData, pCommitter->dReader.pBlockIdx) == 0) {
      code = tsdbCommitterNextTableData(pCommitter);
      if (code) goto _err;
    }
H
Hongze Cheng 已提交
1021
  }
H
Hongze Cheng 已提交
1022

H
Hongze Cheng 已提交
1023 1024
  code = tsdbMoveCommitData(pCommitter, (TABLEID){.suid = INT64_MAX, .uid = INT64_MAX});
  if (code) goto _err;
H
Hongze Cheng 已提交
1025

H
Hongze Cheng 已提交
1026
  if (pCommitter->dWriter.bDatal.nRow > 0) {
H
Hongze Cheng 已提交
1027
    code = tsdbCommitLastBlock(pCommitter);
H
Hongze Cheng 已提交
1028
    if (code) goto _err;
H
Hongze Cheng 已提交
1029 1030
  }

H
Hongze Cheng 已提交
1031 1032
  // commit file data end
  code = tsdbCommitFileDataEnd(pCommitter);
H
Hongze Cheng 已提交
1033
  if (code) goto _err;
H
Hongze Cheng 已提交
1034 1035 1036 1037

  return code;

_err:
S
Shengliang Guan 已提交
1038
  tsdbError("vgId:%d, commit file data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1039 1040
  tsdbDataFReaderClose(&pCommitter->dReader.pReader);
  tsdbDataFWriterClose(&pCommitter->dWriter.pWriter, 0);
H
Hongze Cheng 已提交
1041 1042 1043
  return code;
}

H
Hongze Cheng 已提交
1044 1045
// ----------------------------------------------------------------------------
static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1046
  int32_t code = 0;
H
Hongze Cheng 已提交
1047

H
Hongze Cheng 已提交
1048 1049
  memset(pCommitter, 0, sizeof(*pCommitter));
  ASSERT(pTsdb->mem && pTsdb->imem == NULL);
H
Hongze Cheng 已提交
1050

H
more  
Hongze Cheng 已提交
1051
  taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1052 1053
  pTsdb->imem = pTsdb->mem;
  pTsdb->mem = NULL;
H
more  
Hongze Cheng 已提交
1054
  taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1055

H
Hongze Cheng 已提交
1056
  pCommitter->pTsdb = pTsdb;
H
Hongze Cheng 已提交
1057
  pCommitter->commitID = pTsdb->pVnode->state.commitID;
H
Hongze Cheng 已提交
1058 1059 1060 1061
  pCommitter->minutes = pTsdb->keepCfg.days;
  pCommitter->precision = pTsdb->keepCfg.precision;
  pCommitter->minRow = pTsdb->pVnode->config.tsdbCfg.minRows;
  pCommitter->maxRow = pTsdb->pVnode->config.tsdbCfg.maxRows;
H
Hongze Cheng 已提交
1062
  pCommitter->cmprAlg = pTsdb->pVnode->config.tsdbCfg.compression;
H
Hongze Cheng 已提交
1063
  pCommitter->maxLast = TSDB_DEFAULT_LAST_FILE;  // TODO: make it as a config
H
Hongze Cheng 已提交
1064 1065 1066 1067 1068
  pCommitter->aTbDataP = tsdbMemTableGetTbDataArray(pTsdb->imem);
  if (pCommitter->aTbDataP == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
1069
  code = tsdbFSCopy(pTsdb, &pCommitter->fs);
H
Hongze Cheng 已提交
1070 1071 1072 1073 1074
  if (code) goto _err;

  return code;

_err:
S
Shengliang Guan 已提交
1075
  tsdbError("vgId:%d, tsdb start commit failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1076 1077 1078
  return code;
}

H
Hongze Cheng 已提交
1079 1080 1081
static int32_t tsdbCommitDataStart(SCommitter *pCommitter) {
  int32_t code = 0;

H
Hongze Cheng 已提交
1082
  // Reader
H
Hongze Cheng 已提交
1083 1084
  pCommitter->dReader.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pCommitter->dReader.aBlockIdx == NULL) {
H
Hongze Cheng 已提交
1085 1086 1087 1088
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
1089
  code = tBlockDataCreate(&pCommitter->dReader.bData);
H
Hongze Cheng 已提交
1090 1091
  if (code) goto _exit;

H
Hongze Cheng 已提交
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
  // Writer
  pCommitter->dWriter.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pCommitter->dWriter.aBlockIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  pCommitter->dWriter.aBlockL = taosArrayInit(0, sizeof(SBlockL));
  if (pCommitter->dWriter.aBlockL == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
1105
  code = tBlockDataCreate(&pCommitter->dWriter.bData);
H
Hongze Cheng 已提交
1106
  if (code) goto _exit;
H
Hongze Cheng 已提交
1107

H
Hongze Cheng 已提交
1108
  code = tBlockDataCreate(&pCommitter->dWriter.bDatal);
H
Hongze Cheng 已提交
1109 1110
  if (code) goto _exit;

H
Hongze Cheng 已提交
1111 1112 1113 1114 1115
_exit:
  return code;
}

static void tsdbCommitDataEnd(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1116
  // Reader
H
Hongze Cheng 已提交
1117 1118
  taosArrayDestroy(pCommitter->dReader.aBlockIdx);
  tMapDataClear(&pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
1119
  tBlockDataDestroy(&pCommitter->dReader.bData, 1);
H
Hongze Cheng 已提交
1120 1121

  // Writer
H
Hongze Cheng 已提交
1122
  taosArrayDestroy(pCommitter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
1123
  taosArrayDestroy(pCommitter->dWriter.aBlockL);
H
Hongze Cheng 已提交
1124
  tMapDataClear(&pCommitter->dWriter.mBlock);
H
Hongze Cheng 已提交
1125 1126
  tBlockDataDestroy(&pCommitter->dWriter.bData, 1);
  tBlockDataDestroy(&pCommitter->dWriter.bDatal, 1);
H
Hongze Cheng 已提交
1127 1128
  tTSchemaDestroy(pCommitter->skmTable.pTSchema);
  tTSchemaDestroy(pCommitter->skmRow.pTSchema);
H
Hongze Cheng 已提交
1129 1130
}

H
Hongze Cheng 已提交
1131 1132 1133 1134
static int32_t tsdbCommitData(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1135

H
Hongze Cheng 已提交
1136
  // check
H
Hongze Cheng 已提交
1137
  if (pMemTable->nRow == 0) goto _exit;
H
Hongze Cheng 已提交
1138

H
Hongze Cheng 已提交
1139 1140
  // start ====================
  code = tsdbCommitDataStart(pCommitter);
H
Hongze Cheng 已提交
1141
  if (code) goto _err;
H
Hongze Cheng 已提交
1142 1143 1144

  // impl ====================
  pCommitter->nextKey = pMemTable->minKey;
H
Hongze Cheng 已提交
1145 1146
  while (pCommitter->nextKey < TSKEY_MAX) {
    code = tsdbCommitFileData(pCommitter);
H
Hongze Cheng 已提交
1147
    if (code) goto _err;
H
Hongze Cheng 已提交
1148
  }
H
Hongze Cheng 已提交
1149

H
Hongze Cheng 已提交
1150 1151 1152
  // end ====================
  tsdbCommitDataEnd(pCommitter);

H
Hongze Cheng 已提交
1153
_exit:
S
Shengliang Guan 已提交
1154
  tsdbDebug("vgId:%d, commit data done, nRow:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nRow);
H
Hongze Cheng 已提交
1155
  return code;
H
Hongze Cheng 已提交
1156

H
Hongze Cheng 已提交
1157
_err:
H
Hongze Cheng 已提交
1158
  tsdbCommitDataEnd(pCommitter);
S
Shengliang Guan 已提交
1159
  tsdbError("vgId:%d, commit data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1160 1161
  return code;
}
H
Hongze Cheng 已提交
1162

H
Hongze Cheng 已提交
1163 1164 1165 1166
static int32_t tsdbCommitDel(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1167

H
Hongze Cheng 已提交
1168 1169
  if (pMemTable->nDel == 0) {
    goto _exit;
H
Hongze Cheng 已提交
1170
  }
H
Hongze Cheng 已提交
1171

H
Hongze Cheng 已提交
1172 1173 1174 1175 1176
  // start
  code = tsdbCommitDelStart(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1177

H
Hongze Cheng 已提交
1178
  // impl
H
Hongze Cheng 已提交
1179 1180 1181
  int32_t  iDelIdx = 0;
  int32_t  nDelIdx = taosArrayGetSize(pCommitter->aDelIdx);
  int32_t  iTbData = 0;
H
Hongze Cheng 已提交
1182
  int32_t  nTbData = taosArrayGetSize(pCommitter->aTbDataP);
H
Hongze Cheng 已提交
1183 1184 1185 1186 1187
  STbData *pTbData;
  SDelIdx *pDelIdx;

  ASSERT(nTbData > 0);

H
Hongze Cheng 已提交
1188
  pTbData = (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData);
H
Hongze Cheng 已提交
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
  pDelIdx = (iDelIdx < nDelIdx) ? (SDelIdx *)taosArrayGet(pCommitter->aDelIdx, iDelIdx) : NULL;
  while (true) {
    if (pTbData == NULL && pDelIdx == NULL) break;

    if (pTbData && pDelIdx) {
      int32_t c = tTABLEIDCmprFn(pTbData, pDelIdx);

      if (c == 0) {
        goto _commit_mem_and_disk_del;
      } else if (c < 0) {
        goto _commit_mem_del;
      } else {
        goto _commit_disk_del;
      }
    } else if (pTbData) {
      goto _commit_mem_del;
    } else {
      goto _commit_disk_del;
    }

  _commit_mem_del:
    code = tsdbCommitTableDel(pCommitter, pTbData, NULL);
    if (code) goto _err;

    iTbData++;
H
Hongze Cheng 已提交
1214
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData) : NULL;
H
Hongze Cheng 已提交
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
    continue;

  _commit_disk_del:
    code = tsdbCommitTableDel(pCommitter, NULL, pDelIdx);
    if (code) goto _err;

    iDelIdx++;
    pDelIdx = (iDelIdx < nDelIdx) ? (SDelIdx *)taosArrayGet(pCommitter->aDelIdx, iDelIdx) : NULL;
    continue;

  _commit_mem_and_disk_del:
    code = tsdbCommitTableDel(pCommitter, pTbData, pDelIdx);
    if (code) goto _err;

    iTbData++;
H
Hongze Cheng 已提交
1230
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData) : NULL;
H
Hongze Cheng 已提交
1231 1232 1233
    iDelIdx++;
    pDelIdx = (iDelIdx < nDelIdx) ? (SDelIdx *)taosArrayGet(pCommitter->aDelIdx, iDelIdx) : NULL;
    continue;
H
Hongze Cheng 已提交
1234
  }
H
Hongze Cheng 已提交
1235

H
Hongze Cheng 已提交
1236 1237 1238 1239 1240
  // end
  code = tsdbCommitDelEnd(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1241

H
Hongze Cheng 已提交
1242
_exit:
S
Shengliang Guan 已提交
1243
  tsdbDebug("vgId:%d, commit del done, nDel:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nDel);
H
Hongze Cheng 已提交
1244 1245 1246
  return code;

_err:
S
Shengliang Guan 已提交
1247
  tsdbError("vgId:%d, commit del failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1248
  return code;
H
Hongze Cheng 已提交
1249 1250
}

H
Hongze Cheng 已提交
1251
static int32_t tsdbEndCommit(SCommitter *pCommitter, int32_t eno) {
H
Hongze Cheng 已提交
1252 1253 1254 1255
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

H
Hongze Cheng 已提交
1256
  ASSERT(eno == 0);
H
Hongze Cheng 已提交
1257 1258 1259

  code = tsdbFSCommit1(pTsdb, &pCommitter->fs);
  if (code) goto _err;
H
Hongze Cheng 已提交
1260

H
Hongze Cheng 已提交
1261
  // lock
H
Hongze Cheng 已提交
1262
  taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1263 1264 1265 1266 1267 1268 1269 1270

  // commit or rollback
  code = tsdbFSCommit2(pTsdb, &pCommitter->fs);
  if (code) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
    goto _err;
  }

H
Hongze Cheng 已提交
1271
  pTsdb->imem = NULL;
H
Hongze Cheng 已提交
1272 1273

  // unlock
H
Hongze Cheng 已提交
1274 1275 1276
  taosThreadRwlockUnlock(&pTsdb->rwLock);

  tsdbUnrefMemTable(pMemTable);
H
Hongze Cheng 已提交
1277
  tsdbFSDestroy(&pCommitter->fs);
H
Hongze Cheng 已提交
1278
  taosArrayDestroy(pCommitter->aTbDataP);
H
Hongze Cheng 已提交
1279

H
Hongze Cheng 已提交
1280 1281 1282 1283 1284
  if (pCommitter->toMerge) {
    code = tsdbMerge(pTsdb);
    if (code) goto _err;
  }

S
Shengliang Guan 已提交
1285
  tsdbInfo("vgId:%d, tsdb end commit", TD_VID(pTsdb->pVnode));
H
Hongze Cheng 已提交
1286 1287 1288
  return code;

_err:
S
Shengliang Guan 已提交
1289
  tsdbError("vgId:%d, tsdb end commit failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1290 1291
  return code;
}