tsdbCommit.c 47.2 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

H
Hongze Cheng 已提交
18
typedef enum { MEMORY_DATA_ITER = 0, STT_DATA_ITER } EDataIterT;
H
Hongze Cheng 已提交
19

H
Hongze Cheng 已提交
20 21 22
typedef struct {
  SRBTreeNode n;
  SRowInfo    r;
H
Hongze Cheng 已提交
23
  EDataIterT  type;
H
Hongze Cheng 已提交
24 25 26 27 28 29
  union {
    struct {
      int32_t     iTbDataP;
      STbDataIter iter;
    };  // memory data iter
    struct {
H
Hongze Cheng 已提交
30 31 32
      int32_t    iStt;
      SArray    *aSttBlk;
      int32_t    iSttBlk;
H
Hongze Cheng 已提交
33 34
      SBlockData bData;
      int32_t    iRow;
H
Hongze Cheng 已提交
35
    };  // stt file data iter
H
Hongze Cheng 已提交
36 37 38
  };
} SDataIter;

H
Hongze Cheng 已提交
39
typedef struct {
H
Hongze Cheng 已提交
40
  STsdb *pTsdb;
H
Hongze Cheng 已提交
41
  /* commit data */
H
Hongze Cheng 已提交
42
  int64_t commitID;
H
Hongze Cheng 已提交
43 44
  int32_t minutes;
  int8_t  precision;
H
Hongze Cheng 已提交
45 46
  int32_t minRow;
  int32_t maxRow;
H
Hongze Cheng 已提交
47
  int8_t  cmprAlg;
H
Hongze Cheng 已提交
48
  int8_t  sttTrigger;
H
Hongze Cheng 已提交
49 50
  SArray *aTbDataP;  // memory
  STsdbFS fs;        // disk
H
Hongze Cheng 已提交
51
  // --------------
H
Hongze Cheng 已提交
52
  TSKEY   nextKey;  // reset by each table commit
H
Hongze Cheng 已提交
53 54 55
  int32_t commitFid;
  TSKEY   minKey;
  TSKEY   maxKey;
H
Hongze Cheng 已提交
56
  // commit file data
H
Hongze Cheng 已提交
57 58
  struct {
    SDataFReader *pReader;
H
Hongze Cheng 已提交
59 60 61
    SArray       *aBlockIdx;  // SArray<SBlockIdx>
    int32_t       iBlockIdx;
    SBlockIdx    *pBlockIdx;
H
Hongze Cheng 已提交
62
    SMapData      mBlock;  // SMapData<SDataBlk>
H
Hongze Cheng 已提交
63
    SBlockData    bData;
H
Hongze Cheng 已提交
64
  } dReader;
H
Hongze Cheng 已提交
65 66 67
  struct {
    SDataIter *pIter;
    SRBTree    rbt;
H
Hongze Cheng 已提交
68
    SDataIter  dataIter;
H
Hongze Cheng 已提交
69
    SDataIter  aDataIter[TSDB_MAX_STT_TRIGGER];
H
Hongze Cheng 已提交
70
    int8_t     toLastOnly;
H
Hongze Cheng 已提交
71
  };
H
Hongze Cheng 已提交
72
  struct {
H
Hongze Cheng 已提交
73 74 75 76 77 78
    SDataFWriter     *pWriter;
    SArray           *aBlockIdx;  // SArray<SBlockIdx>
    SArray           *aSttBlk;    // SArray<SSttBlk>
    SMapData          mBlock;     // SMapData<SDataBlk>
    SBlockData        bData;
    SDiskDataBuilder *pBuilder;
H
Hongze Cheng 已提交
79 80 81
  } dWriter;
  SSkmInfo skmTable;
  SSkmInfo skmRow;
H
Hongze Cheng 已提交
82
  /* commit del */
H
Hongze Cheng 已提交
83 84
  SDelFReader *pDelFReader;
  SDelFWriter *pDelFWriter;
H
Hongze Cheng 已提交
85 86 87
  SArray      *aDelIdx;   // SArray<SDelIdx>
  SArray      *aDelIdxN;  // SArray<SDelIdx>
  SArray      *aDelData;  // SArray<SDelData>
H
Hongze Cheng 已提交
88
} SCommitter;
H
refact  
Hongze Cheng 已提交
89

H
Hongze Cheng 已提交
90 91 92 93 94
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
Hongze Cheng 已提交
95 96
static int32_t tsdbNextCommitRow(SCommitter *pCommitter);

H
Hongze Cheng 已提交
97
int32_t tRowInfoCmprFn(const void *p1, const void *p2) {
H
Hongze Cheng 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  SRowInfo *pInfo1 = (SRowInfo *)p1;
  SRowInfo *pInfo2 = (SRowInfo *)p2;

  if (pInfo1->suid < pInfo2->suid) {
    return -1;
  } else if (pInfo1->suid > pInfo2->suid) {
    return 1;
  }

  if (pInfo1->uid < pInfo2->uid) {
    return -1;
  } else if (pInfo1->uid > pInfo2->uid) {
    return 1;
  }

  return tsdbRowCmprFn(&pInfo1->row, &pInfo2->row);
}
H
refact  
Hongze Cheng 已提交
115

H
refact  
Hongze Cheng 已提交
116
int32_t tsdbBegin(STsdb *pTsdb) {
H
Hongze Cheng 已提交
117
  int32_t code = 0;
H
Hongze Cheng 已提交
118
  int32_t lino = 0;
H
Hongze Cheng 已提交
119

120 121
  if (!pTsdb) return code;

H
Hongze Cheng 已提交
122 123
  SMemTable *pMemTable;
  code = tsdbMemTableCreate(pTsdb, &pMemTable);
H
Hongze Cheng 已提交
124
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
125

H
Hongze Cheng 已提交
126
  // lock
H
Hongze Cheng 已提交
127
  if ((code = taosThreadRwlockWrlock(&pTsdb->rwLock))) {
H
Hongze Cheng 已提交
128
    code = TAOS_SYSTEM_ERROR(code);
H
Hongze Cheng 已提交
129
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
130 131 132 133 134
  }

  pTsdb->mem = pMemTable;

  // unlock
H
Hongze Cheng 已提交
135
  if ((code = taosThreadRwlockUnlock(&pTsdb->rwLock))) {
H
Hongze Cheng 已提交
136
    code = TAOS_SYSTEM_ERROR(code);
H
Hongze Cheng 已提交
137
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
138 139
  }

H
Hongze Cheng 已提交
140 141
_exit:
  if (code) {
H
Hongze Cheng 已提交
142
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
143
  }
H
Hongze Cheng 已提交
144
  return code;
H
Hongze Cheng 已提交
145 146
}

H
more  
Hongze Cheng 已提交
147
int32_t tsdbCommit(STsdb *pTsdb) {
148
  if (!pTsdb) return 0;
H
Hongze Cheng 已提交
149

H
more  
Hongze Cheng 已提交
150
  int32_t    code = 0;
H
Hongze Cheng 已提交
151
  int32_t    lino = 0;
H
Hongze Cheng 已提交
152 153 154 155
  SCommitter commith;
  SMemTable *pMemTable = pTsdb->mem;

  // check
H
Hongze Cheng 已提交
156
  if (pMemTable->nRow == 0 && pMemTable->nDel == 0) {
H
Hongze Cheng 已提交
157
    taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
158
    pTsdb->mem = NULL;
H
Hongze Cheng 已提交
159 160 161
    taosThreadRwlockUnlock(&pTsdb->rwLock);

    tsdbUnrefMemTable(pMemTable);
H
Hongze Cheng 已提交
162 163
    goto _exit;
  }
H
refact  
Hongze Cheng 已提交
164

H
more  
Hongze Cheng 已提交
165
  // start commit
H
more  
Hongze Cheng 已提交
166
  code = tsdbStartCommit(pTsdb, &commith);
H
Hongze Cheng 已提交
167
  TSDB_CHECK_CODE(code, lino, _exit);
H
refact  
Hongze Cheng 已提交
168

H
refact  
Hongze Cheng 已提交
169 170
  // commit impl
  code = tsdbCommitData(&commith);
H
Hongze Cheng 已提交
171
  TSDB_CHECK_CODE(code, lino, _exit);
H
refact  
Hongze Cheng 已提交
172 173

  code = tsdbCommitDel(&commith);
H
Hongze Cheng 已提交
174
  TSDB_CHECK_CODE(code, lino, _exit);
H
refact  
Hongze Cheng 已提交
175 176

  // end commit
H
more  
Hongze Cheng 已提交
177
  code = tsdbEndCommit(&commith, 0);
H
Hongze Cheng 已提交
178
  TSDB_CHECK_CODE(code, lino, _exit);
H
refact  
Hongze Cheng 已提交
179

H
Hongze Cheng 已提交
180
_exit:
H
Hongze Cheng 已提交
181 182
  if (code) {
    tsdbEndCommit(&commith, code);
H
Hongze Cheng 已提交
183
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
184
  }
H
refact  
Hongze Cheng 已提交
185 186 187
  return code;
}

H
Hongze Cheng 已提交
188
static int32_t tsdbCommitDelStart(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
189
  int32_t    code = 0;
H
Hongze Cheng 已提交
190
  int32_t    lino = 0;
H
Hongze Cheng 已提交
191 192 193
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

H
Hongze Cheng 已提交
194
  if ((pCommitter->aDelIdx = taosArrayInit(0, sizeof(SDelIdx))) == NULL) {
H
Hongze Cheng 已提交
195
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
196
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
197 198
  }

H
Hongze Cheng 已提交
199
  if ((pCommitter->aDelData = taosArrayInit(0, sizeof(SDelData))) == NULL) {
H
Hongze Cheng 已提交
200
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
201
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
202 203
  }

H
Hongze Cheng 已提交
204
  if ((pCommitter->aDelIdxN = taosArrayInit(0, sizeof(SDelIdx))) == NULL) {
H
Hongze Cheng 已提交
205
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
206
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
207
  }
H
Hongze Cheng 已提交
208

H
Hongze Cheng 已提交
209
  SDelFile *pDelFileR = pCommitter->fs.pDelFile;
H
Hongze Cheng 已提交
210
  if (pDelFileR) {
H
Hongze Cheng 已提交
211
    code = tsdbDelFReaderOpen(&pCommitter->pDelFReader, pDelFileR, pTsdb);
H
Hongze Cheng 已提交
212
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
213

H
Hongze Cheng 已提交
214
    code = tsdbReadDelIdx(pCommitter->pDelFReader, pCommitter->aDelIdx);
H
Hongze Cheng 已提交
215
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
216 217
  }

H
Hongze Cheng 已提交
218
  // prepare new
H
Hongze Cheng 已提交
219 220
  SDelFile wDelFile = {.commitID = pCommitter->commitID, .size = 0, .offset = 0};
  code = tsdbDelFWriterOpen(&pCommitter->pDelFWriter, &wDelFile, pTsdb);
H
Hongze Cheng 已提交
221
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
222 223

_exit:
H
Hongze Cheng 已提交
224
  if (code) {
H
Hongze Cheng 已提交
225
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
226 227 228
  } else {
    tsdbDebug("vgId:%d, commit del start", TD_VID(pTsdb->pVnode));
  }
H
Hongze Cheng 已提交
229 230 231
  return code;
}

H
Hongze Cheng 已提交
232
static int32_t tsdbCommitTableDel(SCommitter *pCommitter, STbData *pTbData, SDelIdx *pDelIdx) {
H
Hongze Cheng 已提交
233
  int32_t   code = 0;
H
Hongze Cheng 已提交
234
  int32_t   lino = 0;
H
Hongze Cheng 已提交
235
  SDelData *pDelData;
H
Hongze Cheng 已提交
236 237
  tb_uid_t  suid;
  tb_uid_t  uid;
H
Hongze Cheng 已提交
238 239

  if (pTbData) {
H
Hongze Cheng 已提交
240 241
    suid = pTbData->suid;
    uid = pTbData->uid;
H
Hongze Cheng 已提交
242

H
Hongze Cheng 已提交
243 244 245 246
    if (pTbData->pHead == NULL) {
      pTbData = NULL;
    }
  }
H
Hongze Cheng 已提交
247 248

  if (pDelIdx) {
H
Hongze Cheng 已提交
249 250 251
    suid = pDelIdx->suid;
    uid = pDelIdx->uid;

H
Hongze Cheng 已提交
252
    code = tsdbReadDelData(pCommitter->pDelFReader, pDelIdx, pCommitter->aDelData);
H
Hongze Cheng 已提交
253
    TSDB_CHECK_CODE(code, lino, _exit);
254 255
  } else {
    taosArrayClear(pCommitter->aDelData);
H
Hongze Cheng 已提交
256 257
  }

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

H
Hongze Cheng 已提交
260
  SDelIdx delIdx = {.suid = suid, .uid = uid};
H
Hongze Cheng 已提交
261 262

  // memory
H
Hongze Cheng 已提交
263 264
  pDelData = pTbData ? pTbData->pHead : NULL;
  for (; pDelData; pDelData = pDelData->pNext) {
H
Hongze Cheng 已提交
265 266
    if (taosArrayPush(pCommitter->aDelData, pDelData) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
267
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
268
    }
H
Hongze Cheng 已提交
269 270 271
  }

  // write
H
Hongze Cheng 已提交
272
  code = tsdbWriteDelData(pCommitter->pDelFWriter, pCommitter->aDelData, &delIdx);
H
Hongze Cheng 已提交
273
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
274 275

  // put delIdx
276
  if (taosArrayPush(pCommitter->aDelIdxN, &delIdx) == NULL) {
H
Hongze Cheng 已提交
277
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
278
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
279
  }
H
Hongze Cheng 已提交
280 281

_exit:
H
Hongze Cheng 已提交
282
  if (code) {
H
Hongze Cheng 已提交
283
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
284
              tstrerror(code));
H
Hongze Cheng 已提交
285
  }
H
Hongze Cheng 已提交
286 287 288
  return code;
}

H
Hongze Cheng 已提交
289 290
static int32_t tsdbCommitDelEnd(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
291
  int32_t lino = 0;
H
Hongze Cheng 已提交
292
  STsdb  *pTsdb = pCommitter->pTsdb;
H
Hongze Cheng 已提交
293

H
Hongze Cheng 已提交
294
  code = tsdbWriteDelIdx(pCommitter->pDelFWriter, pCommitter->aDelIdxN);
H
Hongze Cheng 已提交
295
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
296

H
Hongze Cheng 已提交
297
  code = tsdbUpdateDelFileHdr(pCommitter->pDelFWriter);
H
Hongze Cheng 已提交
298
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
299

H
Hongze Cheng 已提交
300
  code = tsdbFSUpsertDelFile(&pCommitter->fs, &pCommitter->pDelFWriter->fDel);
H
Hongze Cheng 已提交
301
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
302

H
Hongze Cheng 已提交
303
  code = tsdbDelFWriterClose(&pCommitter->pDelFWriter, 1);
H
Hongze Cheng 已提交
304
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
305 306

  if (pCommitter->pDelFReader) {
H
Hongze Cheng 已提交
307
    code = tsdbDelFReaderClose(&pCommitter->pDelFReader);
H
Hongze Cheng 已提交
308
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
309 310
  }

H
Hongze Cheng 已提交
311 312 313 314
  taosArrayDestroy(pCommitter->aDelIdx);
  taosArrayDestroy(pCommitter->aDelData);
  taosArrayDestroy(pCommitter->aDelIdxN);

H
Hongze Cheng 已提交
315 316
_exit:
  if (code) {
H
Hongze Cheng 已提交
317
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
318
              tstrerror(code));
H
Hongze Cheng 已提交
319
  }
H
Hongze Cheng 已提交
320 321 322
  return code;
}

H
Hongze Cheng 已提交
323
int32_t tsdbUpdateTableSchema(SMeta *pMeta, int64_t suid, int64_t uid, SSkmInfo *pSkmInfo) {
H
Hongze Cheng 已提交
324
  int32_t code = 0;
H
Hongze Cheng 已提交
325
  int32_t lino = 0;
H
Hongze Cheng 已提交
326

H
Hongze Cheng 已提交
327
  if (suid) {
H
Hongze Cheng 已提交
328 329
    if (pSkmInfo->suid == suid) {
      pSkmInfo->uid = uid;
H
Hongze Cheng 已提交
330 331
      goto _exit;
    }
H
Hongze Cheng 已提交
332
  } else {
H
Hongze Cheng 已提交
333
    if (pSkmInfo->uid == uid) goto _exit;
H
Hongze Cheng 已提交
334 335
  }

H
Hongze Cheng 已提交
336 337 338 339
  pSkmInfo->suid = suid;
  pSkmInfo->uid = uid;
  tTSchemaDestroy(pSkmInfo->pTSchema);
  code = metaGetTbTSchemaEx(pMeta, suid, uid, -1, &pSkmInfo->pTSchema);
H
Hongze Cheng 已提交
340
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
341 342 343 344 345 346 347

_exit:
  return code;
}

static int32_t tsdbCommitterUpdateRowSchema(SCommitter *pCommitter, int64_t suid, int64_t uid, int32_t sver) {
  int32_t code = 0;
H
Hongze Cheng 已提交
348
  int32_t lino = 0;
H
Hongze Cheng 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

  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);
H
Hongze Cheng 已提交
364
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
365 366 367 368 369

_exit:
  return code;
}

H
Hongze Cheng 已提交
370 371
static int32_t tsdbCommitterNextTableData(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
372
  int32_t lino = 0;
H
Hongze Cheng 已提交
373 374 375 376 377 378 379 380

  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 已提交
381
    code = tsdbReadDataBlk(pCommitter->dReader.pReader, pCommitter->dReader.pBlockIdx, &pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
382
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
383 384 385 386 387 388 389 390 391 392

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

_exit:
  return code;
}

H
Hongze Cheng 已提交
393 394
static int32_t tsdbOpenCommitIter(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
395
  int32_t lino = 0;
H
Hongze Cheng 已提交
396 397

  pCommitter->pIter = NULL;
H
Hongze Cheng 已提交
398
  tRBTreeCreate(&pCommitter->rbt, tRowInfoCmprFn);
H
Hongze Cheng 已提交
399 400

  // memory
H
Hongze Cheng 已提交
401
  TSDBKEY    tKey = {.ts = pCommitter->minKey, .version = VERSION_MIN};
H
Hongze Cheng 已提交
402 403
  SDataIter *pIter = &pCommitter->dataIter;
  pIter->type = MEMORY_DATA_ITER;
H
Hongze Cheng 已提交
404 405 406 407 408 409 410
  pIter->iTbDataP = 0;
  for (; pIter->iTbDataP < taosArrayGetSize(pCommitter->aTbDataP); pIter->iTbDataP++) {
    STbData *pTbData = (STbData *)taosArrayGetP(pCommitter->aTbDataP, pIter->iTbDataP);
    tsdbTbDataIterOpen(pTbData, &tKey, 0, &pIter->iter);
    TSDBROW *pRow = tsdbTbDataIterGet(&pIter->iter);
    if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
      pCommitter->nextKey = TMIN(pCommitter->nextKey, TSDBROW_TS(pRow));
H
Hongze Cheng 已提交
411
      pRow = NULL;
H
Hongze Cheng 已提交
412 413
    }

H
Hongze Cheng 已提交
414 415
    if (pRow == NULL) continue;

H
Hongze Cheng 已提交
416 417 418 419 420
    pIter->r.suid = pTbData->suid;
    pIter->r.uid = pTbData->uid;
    pIter->r.row = *pRow;
    break;
  }
H
Hongze Cheng 已提交
421
  ASSERT(pIter->iTbDataP < taosArrayGetSize(pCommitter->aTbDataP));
H
Hongze Cheng 已提交
422 423 424
  tRBTreePut(&pCommitter->rbt, (SRBTreeNode *)pIter);

  // disk
H
Hongze Cheng 已提交
425
  pCommitter->toLastOnly = 0;
H
Hongze Cheng 已提交
426
  SDataFReader *pReader = pCommitter->dReader.pReader;
H
Hongze Cheng 已提交
427
  if (pReader) {
H
Hongze Cheng 已提交
428
    if (pReader->pSet->nSttF >= pCommitter->sttTrigger) {
H
Hongze Cheng 已提交
429
      int8_t iIter = 0;
H
Hongze Cheng 已提交
430
      for (int32_t iStt = 0; iStt < pReader->pSet->nSttF; iStt++) {
H
Hongze Cheng 已提交
431
        pIter = &pCommitter->aDataIter[iIter];
H
Hongze Cheng 已提交
432
        pIter->type = STT_DATA_ITER;
H
Hongze Cheng 已提交
433
        pIter->iStt = iStt;
H
Hongze Cheng 已提交
434

H
Hongze Cheng 已提交
435
        code = tsdbReadSttBlk(pCommitter->dReader.pReader, iStt, pIter->aSttBlk);
H
Hongze Cheng 已提交
436
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
437

H
Hongze Cheng 已提交
438
        if (taosArrayGetSize(pIter->aSttBlk) == 0) continue;
H
Hongze Cheng 已提交
439

H
Hongze Cheng 已提交
440 441
        pIter->iSttBlk = 0;
        SSttBlk *pSttBlk = (SSttBlk *)taosArrayGet(pIter->aSttBlk, 0);
H
Hongze Cheng 已提交
442
        code = tsdbReadSttBlockEx(pCommitter->dReader.pReader, iStt, pSttBlk, &pIter->bData);
H
Hongze Cheng 已提交
443
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
444 445 446 447 448 449 450 451 452

        pIter->iRow = 0;
        pIter->r.suid = pIter->bData.suid;
        pIter->r.uid = pIter->bData.uid ? pIter->bData.uid : pIter->bData.aUid[0];
        pIter->r.row = tsdbRowFromBlockData(&pIter->bData, 0);

        tRBTreePut(&pCommitter->rbt, (SRBTreeNode *)pIter);
        iIter++;
      }
H
Hongze Cheng 已提交
453
    } else {
H
Hongze Cheng 已提交
454 455 456
      for (int32_t iStt = 0; iStt < pReader->pSet->nSttF; iStt++) {
        SSttFile *pSttFile = pReader->pSet->aSttF[iStt];
        if (pSttFile->size > pSttFile->offset) {
H
Hongze Cheng 已提交
457 458 459 460
          pCommitter->toLastOnly = 1;
          break;
        }
      }
H
Hongze Cheng 已提交
461
    }
H
Hongze Cheng 已提交
462 463 464
  }

  code = tsdbNextCommitRow(pCommitter);
H
Hongze Cheng 已提交
465
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
466

H
Hongze Cheng 已提交
467 468
_exit:
  if (code) {
H
Hongze Cheng 已提交
469
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
470
              tstrerror(code));
H
Hongze Cheng 已提交
471
  }
H
Hongze Cheng 已提交
472 473 474
  return code;
}

H
Hongze Cheng 已提交
475 476
static int32_t tsdbCommitFileDataStart(SCommitter *pCommitter) {
  int32_t    code = 0;
H
Hongze Cheng 已提交
477
  int32_t    lino = 0;
H
Hongze Cheng 已提交
478 479
  STsdb     *pTsdb = pCommitter->pTsdb;
  SDFileSet *pRSet = NULL;
H
Hongze Cheng 已提交
480

H
Hongze Cheng 已提交
481
  // memory
H
Hongze Cheng 已提交
482 483 484
  pCommitter->commitFid = tsdbKeyFid(pCommitter->nextKey, pCommitter->minutes, pCommitter->precision);
  tsdbFidKeyRange(pCommitter->commitFid, pCommitter->minutes, pCommitter->precision, &pCommitter->minKey,
                  &pCommitter->maxKey);
H
Hongze Cheng 已提交
485
  pCommitter->nextKey = TSKEY_MAX;
H
Hongze Cheng 已提交
486

H
Hongze Cheng 已提交
487
  // Reader
H
Hongze Cheng 已提交
488 489
  SDFileSet tDFileSet = {.fid = pCommitter->commitFid};
  pRSet = (SDFileSet *)taosArraySearch(pCommitter->fs.aDFileSet, &tDFileSet, tDFileSetCmprFn, TD_EQ);
H
Hongze Cheng 已提交
490
  if (pRSet) {
H
Hongze Cheng 已提交
491
    code = tsdbDataFReaderOpen(&pCommitter->dReader.pReader, pTsdb, pRSet);
H
Hongze Cheng 已提交
492
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
493

H
Hongze Cheng 已提交
494
    // data
H
Hongze Cheng 已提交
495
    code = tsdbReadBlockIdx(pCommitter->dReader.pReader, pCommitter->dReader.aBlockIdx);
H
Hongze Cheng 已提交
496
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
497

H
Hongze Cheng 已提交
498
    pCommitter->dReader.iBlockIdx = 0;
H
Hongze Cheng 已提交
499 500
    if (taosArrayGetSize(pCommitter->dReader.aBlockIdx) > 0) {
      pCommitter->dReader.pBlockIdx = (SBlockIdx *)taosArrayGet(pCommitter->dReader.aBlockIdx, 0);
H
Hongze Cheng 已提交
501
      code = tsdbReadDataBlk(pCommitter->dReader.pReader, pCommitter->dReader.pBlockIdx, &pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
502
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
503 504 505
    } else {
      pCommitter->dReader.pBlockIdx = NULL;
    }
H
Hongze Cheng 已提交
506
    tBlockDataReset(&pCommitter->dReader.bData);
H
Hongze Cheng 已提交
507
  } else {
H
Hongze Cheng 已提交
508
    pCommitter->dReader.pBlockIdx = NULL;
H
Hongze Cheng 已提交
509
  }
H
Hongze Cheng 已提交
510

H
Hongze Cheng 已提交
511
  // Writer
H
Hongze Cheng 已提交
512 513 514
  SHeadFile fHead = {.commitID = pCommitter->commitID};
  SDataFile fData = {.commitID = pCommitter->commitID};
  SSmaFile  fSma = {.commitID = pCommitter->commitID};
H
Hongze Cheng 已提交
515
  SSttFile  fStt = {.commitID = pCommitter->commitID};
H
Hongze Cheng 已提交
516
  SDFileSet wSet = {.fid = pCommitter->commitFid, .pHeadF = &fHead, .pDataF = &fData, .pSmaF = &fSma};
H
Hongze Cheng 已提交
517
  if (pRSet) {
H
Hongze Cheng 已提交
518
    ASSERT(pRSet->nSttF <= pCommitter->sttTrigger);
H
Hongze Cheng 已提交
519 520
    fData = *pRSet->pDataF;
    fSma = *pRSet->pSmaF;
H
Hongze Cheng 已提交
521
    wSet.diskId = pRSet->diskId;
H
Hongze Cheng 已提交
522
    if (pRSet->nSttF < pCommitter->sttTrigger) {
H
Hongze Cheng 已提交
523 524
      for (int32_t iStt = 0; iStt < pRSet->nSttF; iStt++) {
        wSet.aSttF[iStt] = pRSet->aSttF[iStt];
H
Hongze Cheng 已提交
525
      }
H
Hongze Cheng 已提交
526
      wSet.nSttF = pRSet->nSttF + 1;
H
Hongze Cheng 已提交
527
    } else {
H
Hongze Cheng 已提交
528
      wSet.nSttF = 1;
H
Hongze Cheng 已提交
529
    }
H
Hongze Cheng 已提交
530
  } else {
H
Hongze Cheng 已提交
531
    SDiskID did = {0};
532
    tfsAllocDisk(pTsdb->pVnode->pTfs, 0, &did);
H
Hongze Cheng 已提交
533
    tfsMkdirRecurAt(pTsdb->pVnode->pTfs, pTsdb->path, did);
534
    wSet.diskId = did;
H
Hongze Cheng 已提交
535
    wSet.nSttF = 1;
H
Hongze Cheng 已提交
536
  }
H
Hongze Cheng 已提交
537
  wSet.aSttF[wSet.nSttF - 1] = &fStt;
H
Hongze Cheng 已提交
538
  code = tsdbDataFWriterOpen(&pCommitter->dWriter.pWriter, pTsdb, &wSet);
H
Hongze Cheng 已提交
539
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
540

H
Hongze Cheng 已提交
541
  taosArrayClear(pCommitter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
542
  taosArrayClear(pCommitter->dWriter.aSttBlk);
H
Hongze Cheng 已提交
543 544
  tMapDataReset(&pCommitter->dWriter.mBlock);
  tBlockDataReset(&pCommitter->dWriter.bData);
H
Hongze Cheng 已提交
545
  tDiskDataBuilderClear(pCommitter->dWriter.pBuilder);
H
Hongze Cheng 已提交
546

H
Hongze Cheng 已提交
547 548
  // open iter
  code = tsdbOpenCommitIter(pCommitter);
H
Hongze Cheng 已提交
549
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
550

H
Hongze Cheng 已提交
551
_exit:
H
Hongze Cheng 已提交
552
  if (code) {
H
Hongze Cheng 已提交
553
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
554
  }
H
Hongze Cheng 已提交
555
  return code;
H
Hongze Cheng 已提交
556 557
}

H
Hongze Cheng 已提交
558 559
int32_t tsdbWriteDataBlock(SDataFWriter *pWriter, SBlockData *pBlockData, SMapData *mDataBlk, int8_t cmprAlg) {
  int32_t code = 0;
H
Hongze Cheng 已提交
560
  int32_t lino = 0;
H
Hongze Cheng 已提交
561

H
Hongze Cheng 已提交
562
  if (pBlockData->nRow == 0) return code;
H
Hongze Cheng 已提交
563

H
Hongze Cheng 已提交
564
  SDataBlk dataBlk;
H
Hongze Cheng 已提交
565
  tDataBlkReset(&dataBlk);
H
Hongze Cheng 已提交
566

H
Hongze Cheng 已提交
567
  // info
H
Hongze Cheng 已提交
568
  dataBlk.nRow += pBlockData->nRow;
H
Hongze Cheng 已提交
569 570
  for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
    TSDBKEY key = {.ts = pBlockData->aTSKEY[iRow], .version = pBlockData->aVersion[iRow]};
H
Hongze Cheng 已提交
571

H
Hongze Cheng 已提交
572
    if (iRow == 0) {
H
Hongze Cheng 已提交
573 574
      if (tsdbKeyCmprFn(&dataBlk.minKey, &key) > 0) {
        dataBlk.minKey = key;
H
Hongze Cheng 已提交
575 576 577
      }
    } else {
      if (pBlockData->aTSKEY[iRow] == pBlockData->aTSKEY[iRow - 1]) {
H
Hongze Cheng 已提交
578
        dataBlk.hasDup = 1;
H
Hongze Cheng 已提交
579 580 581
      }
    }

H
Hongze Cheng 已提交
582 583
    if (iRow == pBlockData->nRow - 1 && tsdbKeyCmprFn(&dataBlk.maxKey, &key) < 0) {
      dataBlk.maxKey = key;
H
Hongze Cheng 已提交
584 585
    }

H
Hongze Cheng 已提交
586 587
    dataBlk.minVer = TMIN(dataBlk.minVer, key.version);
    dataBlk.maxVer = TMAX(dataBlk.maxVer, key.version);
H
Hongze Cheng 已提交
588 589 590
  }

  // write
H
Hongze Cheng 已提交
591
  dataBlk.nSubBlock++;
H
Hongze Cheng 已提交
592 593
  code = tsdbWriteBlockData(pWriter, pBlockData, &dataBlk.aSubBlock[dataBlk.nSubBlock - 1],
                            ((dataBlk.nSubBlock == 1) && !dataBlk.hasDup) ? &dataBlk.smaInfo : NULL, cmprAlg, 0);
H
Hongze Cheng 已提交
594
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
595

H
Hongze Cheng 已提交
596
  // put SDataBlk
H
Hongze Cheng 已提交
597
  code = tMapDataPutItem(mDataBlk, &dataBlk, tPutDataBlk);
H
Hongze Cheng 已提交
598
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
599

H
Hongze Cheng 已提交
600
  // clear
H
Hongze Cheng 已提交
601
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
602

H
Hongze Cheng 已提交
603 604
_exit:
  if (code) {
H
Hongze Cheng 已提交
605
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
606
  }
H
Hongze Cheng 已提交
607 608 609
  return code;
}

H
Hongze Cheng 已提交
610 611
int32_t tsdbWriteSttBlock(SDataFWriter *pWriter, SBlockData *pBlockData, SArray *aSttBlk, int8_t cmprAlg) {
  int32_t code = 0;
H
Hongze Cheng 已提交
612
  int32_t lino = 0;
H
Hongze Cheng 已提交
613
  SSttBlk sstBlk;
H
Hongze Cheng 已提交
614

H
Hongze Cheng 已提交
615
  if (pBlockData->nRow == 0) return code;
H
Hongze Cheng 已提交
616

H
Hongze Cheng 已提交
617
  // info
H
Hongze Cheng 已提交
618 619 620 621 622 623
  sstBlk.suid = pBlockData->suid;
  sstBlk.nRow = pBlockData->nRow;
  sstBlk.minKey = TSKEY_MAX;
  sstBlk.maxKey = TSKEY_MIN;
  sstBlk.minVer = VERSION_MAX;
  sstBlk.maxVer = VERSION_MIN;
H
Hongze Cheng 已提交
624
  for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
H
Hongze Cheng 已提交
625 626 627 628
    sstBlk.minKey = TMIN(sstBlk.minKey, pBlockData->aTSKEY[iRow]);
    sstBlk.maxKey = TMAX(sstBlk.maxKey, pBlockData->aTSKEY[iRow]);
    sstBlk.minVer = TMIN(sstBlk.minVer, pBlockData->aVersion[iRow]);
    sstBlk.maxVer = TMAX(sstBlk.maxVer, pBlockData->aVersion[iRow]);
H
Hongze Cheng 已提交
629
  }
H
Hongze Cheng 已提交
630 631
  sstBlk.minUid = pBlockData->uid ? pBlockData->uid : pBlockData->aUid[0];
  sstBlk.maxUid = pBlockData->uid ? pBlockData->uid : pBlockData->aUid[pBlockData->nRow - 1];
H
Hongze Cheng 已提交
632

H
Hongze Cheng 已提交
633
  // write
H
Hongze Cheng 已提交
634
  code = tsdbWriteBlockData(pWriter, pBlockData, &sstBlk.bInfo, NULL, cmprAlg, 1);
H
Hongze Cheng 已提交
635
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
636

H
Hongze Cheng 已提交
637
  // push SSttBlk
H
Hongze Cheng 已提交
638
  if (taosArrayPush(aSttBlk, &sstBlk) == NULL) {
H
Hongze Cheng 已提交
639
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
640
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
641 642
  }

H
Hongze Cheng 已提交
643
  // clear
H
Hongze Cheng 已提交
644
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
645

H
Hongze Cheng 已提交
646 647
_exit:
  if (code) {
H
Hongze Cheng 已提交
648
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
649
  }
H
Hongze Cheng 已提交
650 651 652
  return code;
}

H
Hongze Cheng 已提交
653 654 655 656 657 658 659
static int32_t tsdbCommitSttBlk(SDataFWriter *pWriter, SDiskDataBuilder *pBuilder, SArray *aSttBlk) {
  int32_t code = 0;
  int32_t lino = 0;

  if (pBuilder->nRow == 0) return code;

  // gnrt
H
Hongze Cheng 已提交
660 661 662
  const SDiskData *pDiskData;
  const SBlkInfo  *pBlkInfo;
  code = tGnrtDiskData(pBuilder, &pDiskData, &pBlkInfo);
H
Hongze Cheng 已提交
663 664
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
665 666 667 668 669 670 671
  SSttBlk sttBlk = {.suid = pBuilder->suid,
                    .minUid = pBlkInfo->minUid,
                    .maxUid = pBlkInfo->maxUid,
                    .minKey = pBlkInfo->minKey,
                    .maxKey = pBlkInfo->maxKey,
                    .minVer = pBlkInfo->minVer,
                    .maxVer = pBlkInfo->maxVer};
H
Hongze Cheng 已提交
672
  // write
H
Hongze Cheng 已提交
673
  code = tsdbWriteDiskData(pWriter, pDiskData, &sttBlk.bInfo, NULL);
H
Hongze Cheng 已提交
674 675 676 677 678 679 680 681
  TSDB_CHECK_CODE(code, lino, _exit);

  // push
  if (taosArrayPush(aSttBlk, &sttBlk) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
682 683
  // clear
  tDiskDataBuilderClear(pBuilder);
H
Hongze Cheng 已提交
684 685 686 687 688 689 690 691

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
  return code;
}

H
Hongze Cheng 已提交
692 693
static int32_t tsdbCommitFileDataEnd(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
694
  int32_t lino = 0;
H
Hongze Cheng 已提交
695

H
Hongze Cheng 已提交
696
  // write aBlockIdx
H
Hongze Cheng 已提交
697
  code = tsdbWriteBlockIdx(pCommitter->dWriter.pWriter, pCommitter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
698
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
699

H
Hongze Cheng 已提交
700 701
  // write aSttBlk
  code = tsdbWriteSttBlk(pCommitter->dWriter.pWriter, pCommitter->dWriter.aSttBlk);
H
Hongze Cheng 已提交
702
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
703

H
Hongze Cheng 已提交
704
  // update file header
H
Hongze Cheng 已提交
705
  code = tsdbUpdateDFileSetHeader(pCommitter->dWriter.pWriter);
H
Hongze Cheng 已提交
706
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
707 708

  // upsert SDFileSet
H
Hongze Cheng 已提交
709
  code = tsdbFSUpsertFSet(&pCommitter->fs, &pCommitter->dWriter.pWriter->wSet);
H
Hongze Cheng 已提交
710
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
711 712

  // close and sync
H
Hongze Cheng 已提交
713
  code = tsdbDataFWriterClose(&pCommitter->dWriter.pWriter, 1);
H
Hongze Cheng 已提交
714
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
715

H
Hongze Cheng 已提交
716 717
  if (pCommitter->dReader.pReader) {
    code = tsdbDataFReaderClose(&pCommitter->dReader.pReader);
H
Hongze Cheng 已提交
718
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
719 720 721
  }

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

H
Hongze Cheng 已提交
729 730
static int32_t tsdbMoveCommitData(SCommitter *pCommitter, TABLEID toTable) {
  int32_t code = 0;
H
Hongze Cheng 已提交
731
  int32_t lino = 0;
H
Hongze Cheng 已提交
732

H
Hongze Cheng 已提交
733
  while (pCommitter->dReader.pBlockIdx && tTABLEIDCmprFn(pCommitter->dReader.pBlockIdx, &toTable) < 0) {
H
Hongze Cheng 已提交
734
    SBlockIdx blockIdx = *pCommitter->dReader.pBlockIdx;
H
Hongze Cheng 已提交
735
    code = tsdbWriteDataBlk(pCommitter->dWriter.pWriter, &pCommitter->dReader.mBlock, &blockIdx);
H
Hongze Cheng 已提交
736
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
737 738 739

    if (taosArrayPush(pCommitter->dWriter.aBlockIdx, &blockIdx) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
740
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
741 742
    }

H
Hongze Cheng 已提交
743
    code = tsdbCommitterNextTableData(pCommitter);
H
Hongze Cheng 已提交
744
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
745 746
  }

H
Hongze Cheng 已提交
747 748
_exit:
  if (code) {
H
Hongze Cheng 已提交
749
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
750
              tstrerror(code));
H
Hongze Cheng 已提交
751
  }
H
Hongze Cheng 已提交
752 753 754
  return code;
}

H
Hongze Cheng 已提交
755
static int32_t tsdbCommitFileDataImpl(SCommitter *pCommitter);
H
Hongze Cheng 已提交
756
static int32_t tsdbCommitFileData(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
757
  int32_t    code = 0;
H
Hongze Cheng 已提交
758
  int32_t    lino = 0;
H
Hongze Cheng 已提交
759 760
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
761 762 763

  // commit file data start
  code = tsdbCommitFileDataStart(pCommitter);
H
Hongze Cheng 已提交
764
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
765

H
Hongze Cheng 已提交
766 767
  // impl
  code = tsdbCommitFileDataImpl(pCommitter);
H
Hongze Cheng 已提交
768
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
769

H
Hongze Cheng 已提交
770 771
  // commit file data end
  code = tsdbCommitFileDataEnd(pCommitter);
H
Hongze Cheng 已提交
772
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
773

H
Hongze Cheng 已提交
774 775
_exit:
  if (code) {
H
Hongze Cheng 已提交
776
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
777 778 779
    tsdbDataFReaderClose(&pCommitter->dReader.pReader);
    tsdbDataFWriterClose(&pCommitter->dWriter.pWriter, 0);
  }
H
Hongze Cheng 已提交
780 781 782
  return code;
}

H
Hongze Cheng 已提交
783 784
// ----------------------------------------------------------------------------
static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitter *pCommitter) {
H
Hongze Cheng 已提交
785
  int32_t code = 0;
H
Hongze Cheng 已提交
786
  int32_t lino = 0;
H
Hongze Cheng 已提交
787

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

H
more  
Hongze Cheng 已提交
791
  taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
792 793
  pTsdb->imem = pTsdb->mem;
  pTsdb->mem = NULL;
H
more  
Hongze Cheng 已提交
794
  taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
795

H
Hongze Cheng 已提交
796
  pCommitter->pTsdb = pTsdb;
H
Hongze Cheng 已提交
797
  pCommitter->commitID = pTsdb->pVnode->state.commitID;
H
Hongze Cheng 已提交
798 799 800 801
  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 已提交
802
  pCommitter->cmprAlg = pTsdb->pVnode->config.tsdbCfg.compression;
H
Hongze Cheng 已提交
803
  pCommitter->sttTrigger = pTsdb->pVnode->config.sttTrigger;
H
Hongze Cheng 已提交
804 805 806
  pCommitter->aTbDataP = tsdbMemTableGetTbDataArray(pTsdb->imem);
  if (pCommitter->aTbDataP == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
807
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
808
  }
H
Hongze Cheng 已提交
809
  code = tsdbFSCopy(pTsdb, &pCommitter->fs);
H
Hongze Cheng 已提交
810
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
811

H
Hongze Cheng 已提交
812 813
_exit:
  if (code) {
H
Hongze Cheng 已提交
814
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
815
  }
H
Hongze Cheng 已提交
816 817 818
  return code;
}

H
Hongze Cheng 已提交
819 820
static int32_t tsdbCommitDataStart(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
821
  int32_t lino = 0;
H
Hongze Cheng 已提交
822

H
Hongze Cheng 已提交
823
  // reader
H
Hongze Cheng 已提交
824 825
  pCommitter->dReader.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pCommitter->dReader.aBlockIdx == NULL) {
H
Hongze Cheng 已提交
826
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
827
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
828 829
  }

H
Hongze Cheng 已提交
830
  code = tBlockDataCreate(&pCommitter->dReader.bData);
H
Hongze Cheng 已提交
831
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
832

H
Hongze Cheng 已提交
833
  // merger
H
Hongze Cheng 已提交
834
  for (int32_t iStt = 0; iStt < TSDB_MAX_STT_TRIGGER; iStt++) {
H
Hongze Cheng 已提交
835 836 837
    SDataIter *pIter = &pCommitter->aDataIter[iStt];
    pIter->aSttBlk = taosArrayInit(0, sizeof(SSttBlk));
    if (pIter->aSttBlk == NULL) {
H
Hongze Cheng 已提交
838
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
839
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
840 841 842
    }

    code = tBlockDataCreate(&pIter->bData);
H
Hongze Cheng 已提交
843
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
844 845 846
  }

  // writer
H
Hongze Cheng 已提交
847 848 849
  pCommitter->dWriter.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pCommitter->dWriter.aBlockIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
850
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
851 852
  }

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

H
Hongze Cheng 已提交
859
  code = tBlockDataCreate(&pCommitter->dWriter.bData);
H
Hongze Cheng 已提交
860
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
861

H
Hongze Cheng 已提交
862
  code = tDiskDataBuilderCreate(&pCommitter->dWriter.pBuilder);
H
Hongze Cheng 已提交
863
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
864

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

static void tsdbCommitDataEnd(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
874
  // reader
H
Hongze Cheng 已提交
875 876
  taosArrayDestroy(pCommitter->dReader.aBlockIdx);
  tMapDataClear(&pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
877
  tBlockDataDestroy(&pCommitter->dReader.bData, 1);
H
Hongze Cheng 已提交
878

H
Hongze Cheng 已提交
879
  // merger
H
Hongze Cheng 已提交
880
  for (int32_t iStt = 0; iStt < TSDB_MAX_STT_TRIGGER; iStt++) {
H
Hongze Cheng 已提交
881 882
    SDataIter *pIter = &pCommitter->aDataIter[iStt];
    taosArrayDestroy(pIter->aSttBlk);
H
Hongze Cheng 已提交
883 884 885 886
    tBlockDataDestroy(&pIter->bData, 1);
  }

  // writer
H
Hongze Cheng 已提交
887
  taosArrayDestroy(pCommitter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
888
  taosArrayDestroy(pCommitter->dWriter.aSttBlk);
H
Hongze Cheng 已提交
889
  tMapDataClear(&pCommitter->dWriter.mBlock);
H
Hongze Cheng 已提交
890
  tBlockDataDestroy(&pCommitter->dWriter.bData, 1);
H
Hongze Cheng 已提交
891
  tDiskDataBuilderDestroy(pCommitter->dWriter.pBuilder);
H
Hongze Cheng 已提交
892 893
  tTSchemaDestroy(pCommitter->skmTable.pTSchema);
  tTSchemaDestroy(pCommitter->skmRow.pTSchema);
H
Hongze Cheng 已提交
894 895
}

H
Hongze Cheng 已提交
896
static int32_t tsdbCommitData(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
897 898 899
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
900 901
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
902

H
Hongze Cheng 已提交
903
  // check
H
Hongze Cheng 已提交
904
  if (pMemTable->nRow == 0) goto _exit;
H
Hongze Cheng 已提交
905

H
Hongze Cheng 已提交
906 907
  // start ====================
  code = tsdbCommitDataStart(pCommitter);
H
Hongze Cheng 已提交
908
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
909 910 911

  // impl ====================
  pCommitter->nextKey = pMemTable->minKey;
H
Hongze Cheng 已提交
912 913
  while (pCommitter->nextKey < TSKEY_MAX) {
    code = tsdbCommitFileData(pCommitter);
H
Hongze Cheng 已提交
914
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
915
  }
H
Hongze Cheng 已提交
916

H
Hongze Cheng 已提交
917 918 919
  // end ====================
  tsdbCommitDataEnd(pCommitter);

H
Hongze Cheng 已提交
920
_exit:
H
Hongze Cheng 已提交
921
  if (code) {
H
Hongze Cheng 已提交
922
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
923
  }
H
Hongze Cheng 已提交
924 925
  return code;
}
H
Hongze Cheng 已提交
926

H
Hongze Cheng 已提交
927
static int32_t tsdbCommitDel(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
928 929 930
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
931 932
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
933

H
Hongze Cheng 已提交
934 935
  if (pMemTable->nDel == 0) {
    goto _exit;
H
Hongze Cheng 已提交
936
  }
H
Hongze Cheng 已提交
937

H
Hongze Cheng 已提交
938 939 940
  // start
  code = tsdbCommitDelStart(pCommitter);
  if (code) {
H
Hongze Cheng 已提交
941
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
942
  }
H
Hongze Cheng 已提交
943

H
Hongze Cheng 已提交
944
  // impl
H
Hongze Cheng 已提交
945 946 947
  int32_t  iDelIdx = 0;
  int32_t  nDelIdx = taosArrayGetSize(pCommitter->aDelIdx);
  int32_t  iTbData = 0;
H
Hongze Cheng 已提交
948
  int32_t  nTbData = taosArrayGetSize(pCommitter->aTbDataP);
H
Hongze Cheng 已提交
949 950 951 952 953
  STbData *pTbData;
  SDelIdx *pDelIdx;

  ASSERT(nTbData > 0);

H
Hongze Cheng 已提交
954
  pTbData = (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData);
H
Hongze Cheng 已提交
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
  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);
H
Hongze Cheng 已提交
977
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
978 979

    iTbData++;
H
Hongze Cheng 已提交
980
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData) : NULL;
H
Hongze Cheng 已提交
981 982 983 984
    continue;

  _commit_disk_del:
    code = tsdbCommitTableDel(pCommitter, NULL, pDelIdx);
H
Hongze Cheng 已提交
985
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
986 987 988 989 990 991 992

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

  _commit_mem_and_disk_del:
    code = tsdbCommitTableDel(pCommitter, pTbData, pDelIdx);
H
Hongze Cheng 已提交
993
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
994 995

    iTbData++;
H
Hongze Cheng 已提交
996
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData) : NULL;
H
Hongze Cheng 已提交
997 998 999
    iDelIdx++;
    pDelIdx = (iDelIdx < nDelIdx) ? (SDelIdx *)taosArrayGet(pCommitter->aDelIdx, iDelIdx) : NULL;
    continue;
H
Hongze Cheng 已提交
1000
  }
H
Hongze Cheng 已提交
1001

H
Hongze Cheng 已提交
1002 1003
  // end
  code = tsdbCommitDelEnd(pCommitter);
H
Hongze Cheng 已提交
1004
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1005

H
Hongze Cheng 已提交
1006
_exit:
H
Hongze Cheng 已提交
1007
  if (code) {
H
Hongze Cheng 已提交
1008
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1009 1010 1011
  } else {
    tsdbDebug("vgId:%d commit del done, nDel:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nDel);
  }
H
Hongze Cheng 已提交
1012
  return code;
H
Hongze Cheng 已提交
1013 1014
}

H
Hongze Cheng 已提交
1015
static int32_t tsdbEndCommit(SCommitter *pCommitter, int32_t eno) {
H
Hongze Cheng 已提交
1016 1017 1018
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1019 1020 1021
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

H
Hongze Cheng 已提交
1022
  ASSERT(eno == 0);
H
Hongze Cheng 已提交
1023 1024

  code = tsdbFSCommit1(pTsdb, &pCommitter->fs);
H
Hongze Cheng 已提交
1025
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1026

H
Hongze Cheng 已提交
1027
  // lock
H
Hongze Cheng 已提交
1028
  taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1029 1030 1031 1032 1033

  // commit or rollback
  code = tsdbFSCommit2(pTsdb, &pCommitter->fs);
  if (code) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1034
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1035 1036
  }

H
Hongze Cheng 已提交
1037
  pTsdb->imem = NULL;
H
Hongze Cheng 已提交
1038 1039

  // unlock
H
Hongze Cheng 已提交
1040 1041 1042
  taosThreadRwlockUnlock(&pTsdb->rwLock);

  tsdbUnrefMemTable(pMemTable);
H
Hongze Cheng 已提交
1043
  tsdbFSDestroy(&pCommitter->fs);
H
Hongze Cheng 已提交
1044
  taosArrayDestroy(pCommitter->aTbDataP);
H
Hongze Cheng 已提交
1045

H
Hongze Cheng 已提交
1046 1047
_exit:
  if (code) {
H
Hongze Cheng 已提交
1048
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1049 1050 1051
  } else {
    tsdbInfo("vgId:%d tsdb end commit", TD_VID(pTsdb->pVnode));
  }
H
Hongze Cheng 已提交
1052 1053
  return code;
}
H
Hongze Cheng 已提交
1054

H
Hongze Cheng 已提交
1055
// ================================================================================
H
Hongze Cheng 已提交
1056

H
Hongze Cheng 已提交
1057 1058
static FORCE_INLINE SRowInfo *tsdbGetCommitRow(SCommitter *pCommitter) {
  return (pCommitter->pIter) ? &pCommitter->pIter->r : NULL;
H
Hongze Cheng 已提交
1059 1060
}

H
Hongze Cheng 已提交
1061
static int32_t tsdbNextCommitRow(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1062
  int32_t code = 0;
H
Hongze Cheng 已提交
1063
  int32_t lino = 0;
H
Hongze Cheng 已提交
1064 1065 1066

  if (pCommitter->pIter) {
    SDataIter *pIter = pCommitter->pIter;
H
Hongze Cheng 已提交
1067
    if (pCommitter->pIter->type == MEMORY_DATA_ITER) {  // memory
H
Hongze Cheng 已提交
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
      tsdbTbDataIterNext(&pIter->iter);
      TSDBROW *pRow = tsdbTbDataIterGet(&pIter->iter);
      while (true) {
        if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
          pCommitter->nextKey = TMIN(pCommitter->nextKey, TSDBROW_TS(pRow));
          pRow = NULL;
        }

        if (pRow) {
          pIter->r.suid = pIter->iter.pTbData->suid;
          pIter->r.uid = pIter->iter.pTbData->uid;
          pIter->r.row = *pRow;
          break;
        }

        pIter->iTbDataP++;
H
Hongze Cheng 已提交
1084 1085
        if (pIter->iTbDataP < taosArrayGetSize(pCommitter->aTbDataP)) {
          STbData *pTbData = (STbData *)taosArrayGetP(pCommitter->aTbDataP, pIter->iTbDataP);
H
Hongze Cheng 已提交
1086 1087 1088 1089 1090 1091 1092 1093 1094
          TSDBKEY  keyFrom = {.ts = pCommitter->minKey, .version = VERSION_MIN};
          tsdbTbDataIterOpen(pTbData, &keyFrom, 0, &pIter->iter);
          pRow = tsdbTbDataIterGet(&pIter->iter);
          continue;
        } else {
          pCommitter->pIter = NULL;
          break;
        }
      }
H
Hongze Cheng 已提交
1095
    } else if (pCommitter->pIter->type == STT_DATA_ITER) {  // last file
H
Hongze Cheng 已提交
1096 1097 1098 1099 1100
      pIter->iRow++;
      if (pIter->iRow < pIter->bData.nRow) {
        pIter->r.uid = pIter->bData.uid ? pIter->bData.uid : pIter->bData.aUid[pIter->iRow];
        pIter->r.row = tsdbRowFromBlockData(&pIter->bData, pIter->iRow);
      } else {
H
Hongze Cheng 已提交
1101 1102 1103
        pIter->iSttBlk++;
        if (pIter->iSttBlk < taosArrayGetSize(pIter->aSttBlk)) {
          SSttBlk *pSttBlk = (SSttBlk *)taosArrayGet(pIter->aSttBlk, pIter->iSttBlk);
H
Hongze Cheng 已提交
1104

H
Hongze Cheng 已提交
1105
          code = tsdbReadSttBlockEx(pCommitter->dReader.pReader, pIter->iStt, pSttBlk, &pIter->bData);
H
Hongze Cheng 已提交
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
          if (code) goto _exit;

          pIter->iRow = 0;
          pIter->r.suid = pIter->bData.suid;
          pIter->r.uid = pIter->bData.uid ? pIter->bData.uid : pIter->bData.aUid[0];
          pIter->r.row = tsdbRowFromBlockData(&pIter->bData, 0);
        } else {
          pCommitter->pIter = NULL;
        }
      }
    } else {
      ASSERT(0);
    }

    // compare with min in RB Tree
    pIter = (SDataIter *)tRBTreeMin(&pCommitter->rbt);
    if (pCommitter->pIter && pIter) {
      int32_t c = tRowInfoCmprFn(&pCommitter->pIter->r, &pIter->r);
      if (c > 0) {
        tRBTreePut(&pCommitter->rbt, (SRBTreeNode *)pCommitter->pIter);
        pCommitter->pIter = NULL;
      } else {
        ASSERT(c);
      }
    }
  }

  if (pCommitter->pIter == NULL) {
    pCommitter->pIter = (SDataIter *)tRBTreeMin(&pCommitter->rbt);
    if (pCommitter->pIter) {
      tRBTreeDrop(&pCommitter->rbt, (SRBTreeNode *)pCommitter->pIter);
    }
  }

_exit:
H
Hongze Cheng 已提交
1141
  if (code) {
H
Hongze Cheng 已提交
1142
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
1143
              tstrerror(code));
H
Hongze Cheng 已提交
1144
  }
H
Hongze Cheng 已提交
1145 1146 1147
  return code;
}

H
Hongze Cheng 已提交
1148
static int32_t tsdbCommitAheadBlock(SCommitter *pCommitter, SDataBlk *pDataBlk) {
H
Hongze Cheng 已提交
1149 1150 1151
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1152 1153 1154 1155 1156 1157 1158 1159
  SBlockData *pBlockData = &pCommitter->dWriter.bData;
  SRowInfo   *pRowInfo = tsdbGetCommitRow(pCommitter);
  TABLEID     id = {.suid = pRowInfo->suid, .uid = pRowInfo->uid};

  tBlockDataClear(pBlockData);
  while (pRowInfo) {
    ASSERT(pRowInfo->row.type == 0);
    code = tsdbCommitterUpdateRowSchema(pCommitter, id.suid, id.uid, TSDBROW_SVERSION(&pRowInfo->row));
H
Hongze Cheng 已提交
1160
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1161 1162

    code = tBlockDataAppendRow(pBlockData, &pRowInfo->row, pCommitter->skmRow.pTSchema, id.uid);
H
Hongze Cheng 已提交
1163
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1164 1165

    code = tsdbNextCommitRow(pCommitter);
H
Hongze Cheng 已提交
1166
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1167 1168 1169 1170 1171 1172 1173

    pRowInfo = tsdbGetCommitRow(pCommitter);
    if (pRowInfo) {
      if (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid) {
        pRowInfo = NULL;
      } else {
        TSDBKEY tKey = TSDBROW_KEY(&pRowInfo->row);
H
Hongze Cheng 已提交
1174
        if (tsdbKeyCmprFn(&tKey, &pDataBlk->minKey) >= 0) pRowInfo = NULL;
H
Hongze Cheng 已提交
1175 1176 1177
      }
    }

H
Hongze Cheng 已提交
1178
    if (pBlockData->nRow >= pCommitter->maxRow) {
H
Hongze Cheng 已提交
1179 1180
      code =
          tsdbWriteDataBlock(pCommitter->dWriter.pWriter, pBlockData, &pCommitter->dWriter.mBlock, pCommitter->cmprAlg);
H
Hongze Cheng 已提交
1181
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1182 1183 1184
    }
  }

H
Hongze Cheng 已提交
1185
  code = tsdbWriteDataBlock(pCommitter->dWriter.pWriter, pBlockData, &pCommitter->dWriter.mBlock, pCommitter->cmprAlg);
H
Hongze Cheng 已提交
1186
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1187

H
Hongze Cheng 已提交
1188 1189
_exit:
  if (code) {
H
Hongze Cheng 已提交
1190
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
1191
              tstrerror(code));
H
Hongze Cheng 已提交
1192
  }
H
Hongze Cheng 已提交
1193 1194 1195
  return code;
}

H
Hongze Cheng 已提交
1196
static int32_t tsdbCommitMergeBlock(SCommitter *pCommitter, SDataBlk *pDataBlk) {
H
Hongze Cheng 已提交
1197 1198 1199
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1200 1201 1202 1203 1204
  SRowInfo   *pRowInfo = tsdbGetCommitRow(pCommitter);
  TABLEID     id = {.suid = pRowInfo->suid, .uid = pRowInfo->uid};
  SBlockData *pBDataR = &pCommitter->dReader.bData;
  SBlockData *pBDataW = &pCommitter->dWriter.bData;

H
Hongze Cheng 已提交
1205
  code = tsdbReadDataBlock(pCommitter->dReader.pReader, pDataBlk, pBDataR);
H
Hongze Cheng 已提交
1206
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216

  tBlockDataClear(pBDataW);
  int32_t  iRow = 0;
  TSDBROW  row = tsdbRowFromBlockData(pBDataR, 0);
  TSDBROW *pRow = &row;

  while (pRow && pRowInfo) {
    int32_t c = tsdbRowCmprFn(pRow, &pRowInfo->row);
    if (c < 0) {
      code = tBlockDataAppendRow(pBDataW, pRow, NULL, id.uid);
H
Hongze Cheng 已提交
1217
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227

      iRow++;
      if (iRow < pBDataR->nRow) {
        row = tsdbRowFromBlockData(pBDataR, iRow);
      } else {
        pRow = NULL;
      }
    } else if (c > 0) {
      ASSERT(pRowInfo->row.type == 0);
      code = tsdbCommitterUpdateRowSchema(pCommitter, id.suid, id.uid, TSDBROW_SVERSION(&pRowInfo->row));
H
Hongze Cheng 已提交
1228
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1229 1230

      code = tBlockDataAppendRow(pBDataW, &pRowInfo->row, pCommitter->skmRow.pTSchema, id.uid);
H
Hongze Cheng 已提交
1231
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1232 1233

      code = tsdbNextCommitRow(pCommitter);
H
Hongze Cheng 已提交
1234
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1235 1236 1237 1238 1239 1240 1241

      pRowInfo = tsdbGetCommitRow(pCommitter);
      if (pRowInfo) {
        if (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid) {
          pRowInfo = NULL;
        } else {
          TSDBKEY tKey = TSDBROW_KEY(&pRowInfo->row);
H
Hongze Cheng 已提交
1242
          if (tsdbKeyCmprFn(&tKey, &pDataBlk->maxKey) > 0) pRowInfo = NULL;
H
Hongze Cheng 已提交
1243 1244 1245 1246 1247 1248
        }
      }
    } else {
      ASSERT(0);
    }

H
Hongze Cheng 已提交
1249
    if (pBDataW->nRow >= pCommitter->maxRow) {
H
Hongze Cheng 已提交
1250
      code = tsdbWriteDataBlock(pCommitter->dWriter.pWriter, pBDataW, &pCommitter->dWriter.mBlock, pCommitter->cmprAlg);
H
Hongze Cheng 已提交
1251
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1252 1253 1254 1255 1256
    }
  }

  while (pRow) {
    code = tBlockDataAppendRow(pBDataW, pRow, NULL, id.uid);
H
Hongze Cheng 已提交
1257
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1258 1259 1260 1261 1262 1263 1264 1265

    iRow++;
    if (iRow < pBDataR->nRow) {
      row = tsdbRowFromBlockData(pBDataR, iRow);
    } else {
      pRow = NULL;
    }

H
Hongze Cheng 已提交
1266
    if (pBDataW->nRow >= pCommitter->maxRow) {
H
Hongze Cheng 已提交
1267
      code = tsdbWriteDataBlock(pCommitter->dWriter.pWriter, pBDataW, &pCommitter->dWriter.mBlock, pCommitter->cmprAlg);
H
Hongze Cheng 已提交
1268
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1269 1270 1271
    }
  }

H
Hongze Cheng 已提交
1272
  code = tsdbWriteDataBlock(pCommitter->dWriter.pWriter, pBDataW, &pCommitter->dWriter.mBlock, pCommitter->cmprAlg);
H
Hongze Cheng 已提交
1273
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1274

H
Hongze Cheng 已提交
1275 1276
_exit:
  if (code) {
H
Hongze Cheng 已提交
1277
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
1278
              tstrerror(code));
H
Hongze Cheng 已提交
1279
  }
H
Hongze Cheng 已提交
1280 1281 1282
  return code;
}

H
Hongze Cheng 已提交
1283
static int32_t tsdbMergeTableData(SCommitter *pCommitter, TABLEID id) {
H
Hongze Cheng 已提交
1284 1285 1286
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1287 1288 1289 1290 1291
  SBlockIdx *pBlockIdx = pCommitter->dReader.pBlockIdx;

  ASSERT(pBlockIdx == NULL || tTABLEIDCmprFn(pBlockIdx, &id) >= 0);
  if (pBlockIdx && pBlockIdx->suid == id.suid && pBlockIdx->uid == id.uid) {
    int32_t   iBlock = 0;
H
Hongze Cheng 已提交
1292 1293
    SDataBlk  block;
    SDataBlk *pDataBlk = &block;
H
Hongze Cheng 已提交
1294 1295 1296 1297
    SRowInfo *pRowInfo = tsdbGetCommitRow(pCommitter);

    ASSERT(pRowInfo->suid == id.suid && pRowInfo->uid == id.uid);

H
Hongze Cheng 已提交
1298 1299 1300
    tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pDataBlk, tGetDataBlk);
    while (pDataBlk && pRowInfo) {
      SDataBlk tBlock = {.minKey = TSDBROW_KEY(&pRowInfo->row), .maxKey = TSDBROW_KEY(&pRowInfo->row)};
H
Hongze Cheng 已提交
1301
      int32_t  c = tDataBlkCmprFn(pDataBlk, &tBlock);
H
Hongze Cheng 已提交
1302 1303

      if (c < 0) {
H
Hongze Cheng 已提交
1304
        code = tMapDataPutItem(&pCommitter->dWriter.mBlock, pDataBlk, tPutDataBlk);
H
Hongze Cheng 已提交
1305
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1306 1307 1308

        iBlock++;
        if (iBlock < pCommitter->dReader.mBlock.nItem) {
H
Hongze Cheng 已提交
1309
          tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pDataBlk, tGetDataBlk);
H
Hongze Cheng 已提交
1310
        } else {
H
Hongze Cheng 已提交
1311
          pDataBlk = NULL;
H
Hongze Cheng 已提交
1312 1313
        }
      } else if (c > 0) {
H
Hongze Cheng 已提交
1314
        code = tsdbCommitAheadBlock(pCommitter, pDataBlk);
H
Hongze Cheng 已提交
1315
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1316 1317 1318

        pRowInfo = tsdbGetCommitRow(pCommitter);
        if (pRowInfo && (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid)) pRowInfo = NULL;
H
Hongze Cheng 已提交
1319
      } else {
H
Hongze Cheng 已提交
1320
        code = tsdbCommitMergeBlock(pCommitter, pDataBlk);
H
Hongze Cheng 已提交
1321
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1322 1323 1324

        iBlock++;
        if (iBlock < pCommitter->dReader.mBlock.nItem) {
H
Hongze Cheng 已提交
1325
          tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pDataBlk, tGetDataBlk);
H
Hongze Cheng 已提交
1326
        } else {
H
Hongze Cheng 已提交
1327
          pDataBlk = NULL;
H
Hongze Cheng 已提交
1328 1329 1330
        }
        pRowInfo = tsdbGetCommitRow(pCommitter);
        if (pRowInfo && (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid)) pRowInfo = NULL;
H
Hongze Cheng 已提交
1331 1332 1333
      }
    }

H
Hongze Cheng 已提交
1334 1335
    while (pDataBlk) {
      code = tMapDataPutItem(&pCommitter->dWriter.mBlock, pDataBlk, tPutDataBlk);
H
Hongze Cheng 已提交
1336
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1337 1338 1339

      iBlock++;
      if (iBlock < pCommitter->dReader.mBlock.nItem) {
H
Hongze Cheng 已提交
1340
        tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pDataBlk, tGetDataBlk);
H
Hongze Cheng 已提交
1341
      } else {
H
Hongze Cheng 已提交
1342
        pDataBlk = NULL;
H
Hongze Cheng 已提交
1343 1344
      }
    }
H
Hongze Cheng 已提交
1345 1346

    code = tsdbCommitterNextTableData(pCommitter);
H
Hongze Cheng 已提交
1347
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1348 1349 1350
  }

_exit:
H
Hongze Cheng 已提交
1351
  if (code) {
H
Hongze Cheng 已提交
1352
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
1353
              tstrerror(code));
H
Hongze Cheng 已提交
1354
  }
H
Hongze Cheng 已提交
1355 1356 1357
  return code;
}

H
Hongze Cheng 已提交
1358
static int32_t tsdbInitSttBlockBuilderIfNeed(SCommitter *pCommitter, TABLEID id) {
H
Hongze Cheng 已提交
1359
  int32_t code = 0;
H
Hongze Cheng 已提交
1360
  int32_t lino = 0;
H
Hongze Cheng 已提交
1361

H
Hongze Cheng 已提交
1362 1363 1364
  SDiskDataBuilder *pBuilder = pCommitter->dWriter.pBuilder;
  if (pBuilder->suid || pBuilder->uid) {
    if (!TABLE_SAME_SCHEMA(pBuilder->suid, pBuilder->uid, id.suid, id.uid)) {
H
Hongze Cheng 已提交
1365
      code = tsdbCommitSttBlk(pCommitter->dWriter.pWriter, pBuilder, pCommitter->dWriter.aSttBlk);
H
Hongze Cheng 已提交
1366
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1367 1368

      tDiskDataBuilderClear(pBuilder);
H
Hongze Cheng 已提交
1369 1370 1371
    }
  }

H
Hongze Cheng 已提交
1372
  if (!pBuilder->suid && !pBuilder->uid) {
H
Hongze Cheng 已提交
1373 1374
    ASSERT(pCommitter->skmTable.suid == id.suid);
    ASSERT(pCommitter->skmTable.uid == id.uid);
H
Hongze Cheng 已提交
1375
    code = tDiskDataBuilderInit(pBuilder, pCommitter->skmTable.pTSchema, &id, pCommitter->cmprAlg, 0);
H
Hongze Cheng 已提交
1376
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1377 1378
  }

H
Hongze Cheng 已提交
1379
_exit:
H
Hongze Cheng 已提交
1380
  if (code) {
H
Hongze Cheng 已提交
1381
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
1382
              tstrerror(code));
H
Hongze Cheng 已提交
1383
  }
H
Hongze Cheng 已提交
1384 1385 1386 1387 1388
  return code;
}

static int32_t tsdbAppendLastBlock(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1389
  int32_t lino = 0;
H
Hongze Cheng 已提交
1390 1391

  SBlockData *pBData = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
1392
  TABLEID     id = {.suid = pBData->suid, .uid = pBData->uid};
H
Hongze Cheng 已提交
1393

H
Hongze Cheng 已提交
1394
  code = tsdbInitSttBlockBuilderIfNeed(pCommitter, id);
H
Hongze Cheng 已提交
1395
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1396

H
Hongze Cheng 已提交
1397 1398
  for (int32_t iRow = 0; iRow < pBData->nRow; iRow++) {
    TSDBROW row = tsdbRowFromBlockData(pBData, iRow);
H
Hongze Cheng 已提交
1399

H
Hongze Cheng 已提交
1400
    code = tDiskDataAddRow(pCommitter->dWriter.pBuilder, &row, NULL, &id);
H
Hongze Cheng 已提交
1401
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1402

H
Hongze Cheng 已提交
1403
    if (pCommitter->dWriter.pBuilder->nRow >= pCommitter->maxRow) {
H
Hongze Cheng 已提交
1404
      code = tsdbCommitSttBlk(pCommitter->dWriter.pWriter, pCommitter->dWriter.pBuilder, pCommitter->dWriter.aSttBlk);
H
Hongze Cheng 已提交
1405
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1406 1407 1408

      code = tsdbInitSttBlockBuilderIfNeed(pCommitter, id);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1409 1410 1411
    }
  }

H
Hongze Cheng 已提交
1412 1413
_exit:
  if (code) {
H
Hongze Cheng 已提交
1414
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
1415
              tstrerror(code));
H
Hongze Cheng 已提交
1416
  }
H
Hongze Cheng 已提交
1417 1418 1419
  return code;
}

H
Hongze Cheng 已提交
1420
static int32_t tsdbCommitTableData(SCommitter *pCommitter, TABLEID id) {
H
Hongze Cheng 已提交
1421
  int32_t code = 0;
H
Hongze Cheng 已提交
1422
  int32_t lino = 0;
H
Hongze Cheng 已提交
1423

H
Hongze Cheng 已提交
1424
  SRowInfo *pRowInfo = tsdbGetCommitRow(pCommitter);
H
Hongze Cheng 已提交
1425 1426 1427 1428
  if (pRowInfo && (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid)) {
    pRowInfo = NULL;
  }

H
Hongze Cheng 已提交
1429
  if (pRowInfo == NULL) goto _exit;
H
Hongze Cheng 已提交
1430

H
Hongze Cheng 已提交
1431
  if (pCommitter->toLastOnly) {
H
Hongze Cheng 已提交
1432 1433
    code = tsdbInitSttBlockBuilderIfNeed(pCommitter, id);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1434

H
Hongze Cheng 已提交
1435 1436 1437 1438 1439 1440 1441 1442
    while (pRowInfo) {
      STSchema *pTSchema = NULL;
      if (pRowInfo->row.type == 0) {
        code = tsdbCommitterUpdateRowSchema(pCommitter, id.suid, id.uid, TSDBROW_SVERSION(&pRowInfo->row));
        TSDB_CHECK_CODE(code, lino, _exit);
        pTSchema = pCommitter->skmRow.pTSchema;
      }

H
Hongze Cheng 已提交
1443
      code = tDiskDataAddRow(pCommitter->dWriter.pBuilder, &pRowInfo->row, pTSchema, &id);
H
Hongze Cheng 已提交
1444
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1445

H
Hongze Cheng 已提交
1446 1447
      code = tsdbNextCommitRow(pCommitter);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1448

H
Hongze Cheng 已提交
1449 1450 1451 1452
      pRowInfo = tsdbGetCommitRow(pCommitter);
      if (pRowInfo && (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid)) {
        pRowInfo = NULL;
      }
H
Hongze Cheng 已提交
1453

H
Hongze Cheng 已提交
1454
      if (pCommitter->dWriter.pBuilder->nRow >= pCommitter->maxRow) {
H
Hongze Cheng 已提交
1455
        code = tsdbCommitSttBlk(pCommitter->dWriter.pWriter, pCommitter->dWriter.pBuilder, pCommitter->dWriter.aSttBlk);
H
Hongze Cheng 已提交
1456
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1457 1458 1459

        code = tsdbInitSttBlockBuilderIfNeed(pCommitter, id);
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1460
      }
H
Hongze Cheng 已提交
1461
    }
H
Hongze Cheng 已提交
1462 1463 1464
  } else {
    SBlockData *pBData = &pCommitter->dWriter.bData;
    ASSERT(pBData->nRow == 0);
H
Hongze Cheng 已提交
1465

H
Hongze Cheng 已提交
1466 1467 1468 1469
    while (pRowInfo) {
      STSchema *pTSchema = NULL;
      if (pRowInfo->row.type == 0) {
        code = tsdbCommitterUpdateRowSchema(pCommitter, id.suid, id.uid, TSDBROW_SVERSION(&pRowInfo->row));
H
Hongze Cheng 已提交
1470
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
        pTSchema = pCommitter->skmRow.pTSchema;
      }

      code = tBlockDataAppendRow(pBData, &pRowInfo->row, pTSchema, id.uid);
      TSDB_CHECK_CODE(code, lino, _exit);

      code = tsdbNextCommitRow(pCommitter);
      TSDB_CHECK_CODE(code, lino, _exit);

      pRowInfo = tsdbGetCommitRow(pCommitter);
      if (pRowInfo && (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid)) {
        pRowInfo = NULL;
      }

      if (pBData->nRow >= pCommitter->maxRow) {
H
Hongze Cheng 已提交
1486 1487
        code =
            tsdbWriteDataBlock(pCommitter->dWriter.pWriter, pBData, &pCommitter->dWriter.mBlock, pCommitter->cmprAlg);
H
Hongze Cheng 已提交
1488
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1489
      }
H
Hongze Cheng 已提交
1490 1491
    }

H
Hongze Cheng 已提交
1492 1493 1494 1495 1496 1497 1498 1499 1500
    if (pBData->nRow) {
      if (pBData->nRow > pCommitter->minRow) {
        code =
            tsdbWriteDataBlock(pCommitter->dWriter.pWriter, pBData, &pCommitter->dWriter.mBlock, pCommitter->cmprAlg);
        TSDB_CHECK_CODE(code, lino, _exit);
      } else {
        code = tsdbAppendLastBlock(pCommitter);
        TSDB_CHECK_CODE(code, lino, _exit);
      }
H
Hongze Cheng 已提交
1501 1502 1503
    }
  }

H
Hongze Cheng 已提交
1504
_exit:
H
Hongze Cheng 已提交
1505
  if (code) {
H
Hongze Cheng 已提交
1506
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
1507
              tstrerror(code));
H
Hongze Cheng 已提交
1508
  }
H
Hongze Cheng 已提交
1509 1510 1511
  return code;
}

H
Hongze Cheng 已提交
1512 1513
static int32_t tsdbCommitFileDataImpl(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1514
  int32_t lino = 0;
H
Hongze Cheng 已提交
1515

H
Hongze Cheng 已提交
1516
  SRowInfo *pRowInfo;
H
Hongze Cheng 已提交
1517
  TABLEID   id = {0};
H
Hongze Cheng 已提交
1518
  while ((pRowInfo = tsdbGetCommitRow(pCommitter)) != NULL) {
H
Hongze Cheng 已提交
1519 1520 1521
    ASSERT(pRowInfo->suid != id.suid || pRowInfo->uid != id.uid);
    id.suid = pRowInfo->suid;
    id.uid = pRowInfo->uid;
H
Hongze Cheng 已提交
1522

H
Hongze Cheng 已提交
1523
    code = tsdbMoveCommitData(pCommitter, id);
H
Hongze Cheng 已提交
1524
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1525 1526

    // start
H
Hongze Cheng 已提交
1527
    tMapDataReset(&pCommitter->dWriter.mBlock);
H
Hongze Cheng 已提交
1528 1529

    // impl
H
Hongze Cheng 已提交
1530
    code = tsdbUpdateTableSchema(pCommitter->pTsdb->pVnode->pMeta, id.suid, id.uid, &pCommitter->skmTable);
H
Hongze Cheng 已提交
1531
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1532
    code = tBlockDataInit(&pCommitter->dReader.bData, &id, pCommitter->skmTable.pTSchema, NULL, 0);
H
Hongze Cheng 已提交
1533
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1534
    code = tBlockDataInit(&pCommitter->dWriter.bData, &id, pCommitter->skmTable.pTSchema, NULL, 0);
H
Hongze Cheng 已提交
1535
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1536

H
Hongze Cheng 已提交
1537 1538
    /* merge with data in .data file */
    code = tsdbMergeTableData(pCommitter, id);
H
Hongze Cheng 已提交
1539
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1540

H
Hongze Cheng 已提交
1541
    /* handle remain table data */
H
Hongze Cheng 已提交
1542
    code = tsdbCommitTableData(pCommitter, id);
H
Hongze Cheng 已提交
1543
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1544

H
Hongze Cheng 已提交
1545
    // end
H
Hongze Cheng 已提交
1546 1547
    if (pCommitter->dWriter.mBlock.nItem > 0) {
      SBlockIdx blockIdx = {.suid = id.suid, .uid = id.uid};
H
Hongze Cheng 已提交
1548
      code = tsdbWriteDataBlk(pCommitter->dWriter.pWriter, &pCommitter->dWriter.mBlock, &blockIdx);
H
Hongze Cheng 已提交
1549
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1550 1551 1552

      if (taosArrayPush(pCommitter->dWriter.aBlockIdx, &blockIdx) == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1553
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1554 1555
      }
    }
H
Hongze Cheng 已提交
1556 1557
  }

H
Hongze Cheng 已提交
1558 1559 1560
  id.suid = INT64_MAX;
  id.uid = INT64_MAX;
  code = tsdbMoveCommitData(pCommitter, id);
H
Hongze Cheng 已提交
1561
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1562

H
Hongze Cheng 已提交
1563
  code = tsdbCommitSttBlk(pCommitter->dWriter.pWriter, pCommitter->dWriter.pBuilder, pCommitter->dWriter.aSttBlk);
H
Hongze Cheng 已提交
1564
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1565

H
Hongze Cheng 已提交
1566 1567
_exit:
  if (code) {
H
Hongze Cheng 已提交
1568
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
1569
              tstrerror(code));
H
Hongze Cheng 已提交
1570
  }
H
Hongze Cheng 已提交
1571 1572
  return code;
}