tsdbCommit.c 51.7 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 24 25 26 27 28
typedef struct {
  int64_t suid;
  int64_t uid;
  TSDBROW row;
} SRowInfo;

H
Hongze Cheng 已提交
29 30
typedef enum { MEMORY_DATA_ITER = 0, LAST_DATA_ITER } EDataIterT;

H
Hongze Cheng 已提交
31 32 33
typedef struct {
  SRBTreeNode n;
  SRowInfo    r;
H
Hongze Cheng 已提交
34
  EDataIterT  type;
H
Hongze Cheng 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  union {
    struct {
      int32_t     iTbDataP;
      STbDataIter iter;
    };  // memory data iter
    struct {
      int32_t    iLast;
      SArray    *aBlockL;
      int32_t    iBlockL;
      SBlockData bData;
      int32_t    iRow;
    };  // last file data iter
  };
} SDataIter;

H
Hongze Cheng 已提交
50
typedef struct {
H
Hongze Cheng 已提交
51
  STsdb *pTsdb;
H
Hongze Cheng 已提交
52
  /* commit data */
H
Hongze Cheng 已提交
53
  int64_t commitID;
H
Hongze Cheng 已提交
54 55
  int32_t minutes;
  int8_t  precision;
H
Hongze Cheng 已提交
56 57
  int32_t minRow;
  int32_t maxRow;
H
Hongze Cheng 已提交
58
  int8_t  cmprAlg;
H
Hongze Cheng 已提交
59
  int8_t  maxLast;
H
Hongze Cheng 已提交
60 61
  SArray *aTbDataP;  // memory
  STsdbFS fs;        // disk
H
Hongze Cheng 已提交
62
  // --------------
H
Hongze Cheng 已提交
63
  TSKEY   nextKey;  // reset by each table commit
H
Hongze Cheng 已提交
64 65 66
  int32_t commitFid;
  TSKEY   minKey;
  TSKEY   maxKey;
H
Hongze Cheng 已提交
67
  // commit file data
H
Hongze Cheng 已提交
68 69
  struct {
    SDataFReader *pReader;
H
Hongze Cheng 已提交
70 71 72 73 74
    SArray       *aBlockIdx;  // SArray<SBlockIdx>
    int32_t       iBlockIdx;
    SBlockIdx    *pBlockIdx;
    SMapData      mBlock;  // SMapData<SBlock>
    SBlockData    bData;
H
Hongze Cheng 已提交
75
  } dReader;
H
Hongze Cheng 已提交
76 77 78
  struct {
    SDataIter *pIter;
    SRBTree    rbt;
H
Hongze Cheng 已提交
79 80
    SDataIter  dataIter;
    SDataIter  aDataIter[TSDB_MAX_LAST_FILE];
H
Hongze Cheng 已提交
81
    int8_t     toLast;
H
Hongze Cheng 已提交
82
  };
H
Hongze Cheng 已提交
83 84 85
  struct {
    SDataFWriter *pWriter;
    SArray       *aBlockIdx;  // SArray<SBlockIdx>
H
Hongze Cheng 已提交
86
    SArray       *aBlockL;    // SArray<SBlockL>
H
Hongze Cheng 已提交
87 88
    SMapData      mBlock;     // SMapData<SBlock>
    SBlockData    bData;
H
Hongze Cheng 已提交
89
    SBlockData    bDatal;
H
Hongze Cheng 已提交
90 91 92
  } dWriter;
  SSkmInfo skmTable;
  SSkmInfo skmRow;
H
Hongze Cheng 已提交
93
  /* commit del */
H
Hongze Cheng 已提交
94 95
  SDelFReader *pDelFReader;
  SDelFWriter *pDelFWriter;
H
Hongze Cheng 已提交
96 97 98
  SArray      *aDelIdx;   // SArray<SDelIdx>
  SArray      *aDelIdxN;  // SArray<SDelIdx>
  SArray      *aDelData;  // SArray<SDelData>
H
Hongze Cheng 已提交
99
} SCommitter;
H
refact  
Hongze Cheng 已提交
100

H
Hongze Cheng 已提交
101 102 103
extern int32_t tsdbReadLastBlockEx(SDataFReader *pReader, int32_t iLast, SBlockL *pBlockL,
                                   SBlockData *pBlockData);  // todo

H
Hongze Cheng 已提交
104 105 106 107 108
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 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
static int32_t tsdbNextCommitRow(SCommitter *pCommitter);

static int32_t tRowInfoCmprFn(const void *p1, const void *p2) {
  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 已提交
129

H
refact  
Hongze Cheng 已提交
130
int32_t tsdbBegin(STsdb *pTsdb) {
H
Hongze Cheng 已提交
131
  int32_t code = 0;
H
Hongze Cheng 已提交
132

133 134
  if (!pTsdb) return code;

H
Hongze Cheng 已提交
135 136
  SMemTable *pMemTable;
  code = tsdbMemTableCreate(pTsdb, &pMemTable);
H
Hongze Cheng 已提交
137
  if (code) goto _err;
H
Hongze Cheng 已提交
138

H
Hongze Cheng 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  // 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 已提交
155 156 157
  return code;

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

H
more  
Hongze Cheng 已提交
162
int32_t tsdbCommit(STsdb *pTsdb) {
163
  if (!pTsdb) return 0;
H
Hongze Cheng 已提交
164

H
more  
Hongze Cheng 已提交
165
  int32_t    code = 0;
H
Hongze Cheng 已提交
166 167 168 169
  SCommitter commith;
  SMemTable *pMemTable = pTsdb->mem;

  // check
H
Hongze Cheng 已提交
170
  if (pMemTable->nRow == 0 && pMemTable->nDel == 0) {
H
Hongze Cheng 已提交
171
    taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
172
    pTsdb->mem = NULL;
H
Hongze Cheng 已提交
173 174 175
    taosThreadRwlockUnlock(&pTsdb->rwLock);

    tsdbUnrefMemTable(pMemTable);
H
Hongze Cheng 已提交
176 177
    goto _exit;
  }
H
refact  
Hongze Cheng 已提交
178

H
more  
Hongze Cheng 已提交
179
  // start commit
H
more  
Hongze Cheng 已提交
180
  code = tsdbStartCommit(pTsdb, &commith);
H
Hongze Cheng 已提交
181
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
182

H
refact  
Hongze Cheng 已提交
183 184
  // commit impl
  code = tsdbCommitData(&commith);
H
Hongze Cheng 已提交
185
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
186 187

  code = tsdbCommitDel(&commith);
H
Hongze Cheng 已提交
188
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
189 190

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

H
Hongze Cheng 已提交
194
_exit:
H
refact  
Hongze Cheng 已提交
195 196 197
  return code;

_err:
H
Hongze Cheng 已提交
198
  tsdbEndCommit(&commith, code);
C
Cary Xu 已提交
199
  tsdbError("vgId:%d, failed to commit since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
refact  
Hongze Cheng 已提交
200 201 202
  return code;
}

H
Hongze Cheng 已提交
203
static int32_t tsdbCommitDelStart(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
204 205 206 207
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

H
Hongze Cheng 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  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 已提交
225

H
Hongze Cheng 已提交
226
  SDelFile *pDelFileR = pCommitter->fs.pDelFile;
H
Hongze Cheng 已提交
227
  if (pDelFileR) {
H
Hongze Cheng 已提交
228
    code = tsdbDelFReaderOpen(&pCommitter->pDelFReader, pDelFileR, pTsdb);
H
Hongze Cheng 已提交
229
    if (code) goto _err;
H
Hongze Cheng 已提交
230

H
Hongze Cheng 已提交
231
    code = tsdbReadDelIdx(pCommitter->pDelFReader, pCommitter->aDelIdx);
H
Hongze Cheng 已提交
232
    if (code) goto _err;
H
Hongze Cheng 已提交
233 234
  }

H
Hongze Cheng 已提交
235
  // prepare new
H
Hongze Cheng 已提交
236 237
  SDelFile wDelFile = {.commitID = pCommitter->commitID, .size = 0, .offset = 0};
  code = tsdbDelFWriterOpen(&pCommitter->pDelFWriter, &wDelFile, pTsdb);
H
Hongze Cheng 已提交
238
  if (code) goto _err;
H
Hongze Cheng 已提交
239 240

_exit:
S
Shengliang Guan 已提交
241
  tsdbDebug("vgId:%d, commit del start", TD_VID(pTsdb->pVnode));
H
Hongze Cheng 已提交
242 243 244
  return code;

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

H
Hongze Cheng 已提交
249
static int32_t tsdbCommitTableDel(SCommitter *pCommitter, STbData *pTbData, SDelIdx *pDelIdx) {
H
Hongze Cheng 已提交
250
  int32_t   code = 0;
H
Hongze Cheng 已提交
251
  SDelData *pDelData;
H
Hongze Cheng 已提交
252 253
  tb_uid_t  suid;
  tb_uid_t  uid;
H
Hongze Cheng 已提交
254 255

  if (pTbData) {
H
Hongze Cheng 已提交
256 257
    suid = pTbData->suid;
    uid = pTbData->uid;
H
Hongze Cheng 已提交
258

H
Hongze Cheng 已提交
259 260 261 262
    if (pTbData->pHead == NULL) {
      pTbData = NULL;
    }
  }
H
Hongze Cheng 已提交
263 264

  if (pDelIdx) {
H
Hongze Cheng 已提交
265 266 267
    suid = pDelIdx->suid;
    uid = pDelIdx->uid;

H
Hongze Cheng 已提交
268
    code = tsdbReadDelData(pCommitter->pDelFReader, pDelIdx, pCommitter->aDelData);
H
Hongze Cheng 已提交
269
    if (code) goto _err;
270 271
  } else {
    taosArrayClear(pCommitter->aDelData);
H
Hongze Cheng 已提交
272 273
  }

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

H
Hongze Cheng 已提交
276
  SDelIdx delIdx = {.suid = suid, .uid = uid};
H
Hongze Cheng 已提交
277 278

  // memory
H
Hongze Cheng 已提交
279 280
  pDelData = pTbData ? pTbData->pHead : NULL;
  for (; pDelData; pDelData = pDelData->pNext) {
H
Hongze Cheng 已提交
281 282 283 284
    if (taosArrayPush(pCommitter->aDelData, pDelData) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
285 286 287
  }

  // write
H
Hongze Cheng 已提交
288
  code = tsdbWriteDelData(pCommitter->pDelFWriter, pCommitter->aDelData, &delIdx);
H
Hongze Cheng 已提交
289 290 291
  if (code) goto _err;

  // put delIdx
292
  if (taosArrayPush(pCommitter->aDelIdxN, &delIdx) == NULL) {
H
Hongze Cheng 已提交
293 294 295
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
296 297 298 299 300

_exit:
  return code;

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

H
Hongze Cheng 已提交
305 306
static int32_t tsdbCommitDelEnd(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
307
  STsdb  *pTsdb = pCommitter->pTsdb;
H
Hongze Cheng 已提交
308

H
Hongze Cheng 已提交
309
  code = tsdbWriteDelIdx(pCommitter->pDelFWriter, pCommitter->aDelIdxN);
H
Hongze Cheng 已提交
310
  if (code) goto _err;
H
Hongze Cheng 已提交
311

H
Hongze Cheng 已提交
312 313 314
  code = tsdbUpdateDelFileHdr(pCommitter->pDelFWriter);
  if (code) goto _err;

H
Hongze Cheng 已提交
315
  code = tsdbFSUpsertDelFile(&pCommitter->fs, &pCommitter->pDelFWriter->fDel);
H
Hongze Cheng 已提交
316
  if (code) goto _err;
H
Hongze Cheng 已提交
317

H
Hongze Cheng 已提交
318
  code = tsdbDelFWriterClose(&pCommitter->pDelFWriter, 1);
H
Hongze Cheng 已提交
319
  if (code) goto _err;
H
Hongze Cheng 已提交
320 321

  if (pCommitter->pDelFReader) {
H
Hongze Cheng 已提交
322
    code = tsdbDelFReaderClose(&pCommitter->pDelFReader);
H
Hongze Cheng 已提交
323 324 325
    if (code) goto _err;
  }

H
Hongze Cheng 已提交
326 327 328 329
  taosArrayDestroy(pCommitter->aDelIdx);
  taosArrayDestroy(pCommitter->aDelData);
  taosArrayDestroy(pCommitter->aDelIdxN);

H
Hongze Cheng 已提交
330 331 332
  return code;

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

H
Hongze Cheng 已提交
337
static int32_t tsdbCommitterUpdateTableSchema(SCommitter *pCommitter, int64_t suid, int64_t uid) {
H
Hongze Cheng 已提交
338 339
  int32_t code = 0;

H
Hongze Cheng 已提交
340 341 342 343
  if (suid) {
    if (pCommitter->skmTable.suid == suid) goto _exit;
  } else {
    if (pCommitter->skmTable.uid == uid) goto _exit;
H
Hongze Cheng 已提交
344 345 346 347 348
  }

  pCommitter->skmTable.suid = suid;
  pCommitter->skmTable.uid = uid;
  tTSchemaDestroy(pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
349
  code = metaGetTbTSchemaEx(pCommitter->pTsdb->pVnode->pMeta, suid, uid, -1, &pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
  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 已提交
381 382 383 384 385 386 387 388 389 390
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 已提交
391
    code = tsdbReadBlock(pCommitter->dReader.pReader, pCommitter->dReader.pBlockIdx, &pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
392 393 394 395 396 397 398 399 400 401 402
    if (code) goto _exit;

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

_exit:
  return code;
}

H
Hongze Cheng 已提交
403 404 405 406 407 408 409
static int32_t tsdbOpenCommitIter(SCommitter *pCommitter) {
  int32_t code = 0;

  tRBTreeCreate(&pCommitter->rbt, tRowInfoCmprFn);
  pCommitter->pIter = NULL;

  // memory
H
Hongze Cheng 已提交
410 411
  SDataIter *pIter = &pCommitter->dataIter;
  pIter->type = MEMORY_DATA_ITER;
H
Hongze Cheng 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
  pIter->iTbDataP = 0;
  for (; pIter->iTbDataP < taosArrayGetSize(pCommitter->aTbDataP); pIter->iTbDataP++) {
    STbData *pTbData = (STbData *)taosArrayGetP(pCommitter->aTbDataP, pIter->iTbDataP);
    TSDBKEY  tKey = {.ts = pCommitter->minKey, .version = VERSION_MIN};
    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));
      continue;
    }

    pIter->r.suid = pTbData->suid;
    pIter->r.uid = pTbData->uid;
    pIter->r.row = *pRow;
    break;
  }

  tRBTreePut(&pCommitter->rbt, (SRBTreeNode *)pIter);

  // disk
H
Hongze Cheng 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
  SDataFReader *pReader = pCommitter->dReader.pReader;
  if (pReader && pReader->pSet->nLastF >= pCommitter->maxLast) {
    int8_t iIter = 0;
    for (int32_t iLast = 0; iLast < pReader->pSet->nLastF; iLast++) {
      pIter = &pCommitter->aDataIter[iIter];
      pIter->type = LAST_DATA_ITER;
      pIter->iLast = iLast;

      code = tsdbReadBlockL(pCommitter->dReader.pReader, iLast, pIter->aBlockL);
      if (code) goto _err;

      if (taosArrayGetSize(pIter->aBlockL) == 0) continue;

      pIter->iBlockL = 0;
      SBlockL *pBlockL = (SBlockL *)taosArrayGet(pIter->aBlockL, 0);
      code = tsdbReadLastBlockEx(pCommitter->dReader.pReader, iLast, pBlockL, &pIter->bData);
      if (code) goto _err;

      pIter->r.suid = pIter->bData.suid;
      pIter->r.uid = pIter->bData.uid;
      pIter->r.row = tsdbRowFromBlockData(&pIter->bData, 0);

      tRBTreePut(&pCommitter->rbt, (SRBTreeNode *)pIter);
      iIter++;
    }
H
Hongze Cheng 已提交
458 459 460 461

    pCommitter->toLast = 0;
  } else {
    pCommitter->toLast = 1;
H
Hongze Cheng 已提交
462 463 464 465 466 467 468 469 470 471 472
  }

  code = tsdbNextCommitRow(pCommitter);
  if (code) goto _err;

  return code;

_err:
  return code;
}

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

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

H
Hongze Cheng 已提交
484
  // Reader
H
Hongze Cheng 已提交
485 486
  pRSet = (SDFileSet *)taosArraySearch(pCommitter->fs.aDFileSet, &(SDFileSet){.fid = pCommitter->commitFid},
                                       tDFileSetCmprFn, TD_EQ);
H
Hongze Cheng 已提交
487
  if (pRSet) {
H
Hongze Cheng 已提交
488
    code = tsdbDataFReaderOpen(&pCommitter->dReader.pReader, pTsdb, pRSet);
H
Hongze Cheng 已提交
489 490
    if (code) goto _err;

H
Hongze Cheng 已提交
491
    // data
H
Hongze Cheng 已提交
492
    code = tsdbReadBlockIdx(pCommitter->dReader.pReader, pCommitter->dReader.aBlockIdx);
H
Hongze Cheng 已提交
493
    if (code) goto _err;
H
Hongze Cheng 已提交
494

H
Hongze Cheng 已提交
495 496 497 498 499
    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 已提交
500
      code = tsdbReadBlock(pCommitter->dReader.pReader, pCommitter->dReader.pBlockIdx, &pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
501 502 503 504
      if (code) goto _err;
    } else {
      pCommitter->dReader.pBlockIdx = NULL;
    }
H
Hongze Cheng 已提交
505
    tBlockDataReset(&pCommitter->dReader.bData);
H
Hongze Cheng 已提交
506
  } else {
H
Hongze Cheng 已提交
507
    pCommitter->dReader.pBlockIdx = NULL;
H
Hongze Cheng 已提交
508
  }
H
Hongze Cheng 已提交
509

H
Hongze Cheng 已提交
510
  // Writer
H
Hongze Cheng 已提交
511 512 513
  SHeadFile fHead;
  SDataFile fData;
  SSmaFile  fSma;
H
Hongze Cheng 已提交
514 515
  SLastFile fLast;
  SDFileSet wSet = {0};
H
Hongze Cheng 已提交
516
  if (pRSet) {
H
Hongze Cheng 已提交
517
    ASSERT(pCommitter->maxLast == 1 || pRSet->nLastF < pCommitter->maxLast);
H
Hongze Cheng 已提交
518

H
Hongze Cheng 已提交
519
    fHead = (SHeadFile){.commitID = pCommitter->commitID};
H
Hongze Cheng 已提交
520 521
    fData = *pRSet->pDataF;
    fSma = *pRSet->pSmaF;
H
Hongze Cheng 已提交
522 523 524 525 526 527 528 529 530 531 532 533
    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 已提交
534
  } else {
H
Hongze Cheng 已提交
535 536 537 538
    fHead = (SHeadFile){.commitID = pCommitter->commitID};
    fData = (SDataFile){.commitID = pCommitter->commitID};
    fSma = (SSmaFile){.commitID = pCommitter->commitID};
    fLast = (SLastFile){.commitID = pCommitter->commitID};
539

H
Hongze Cheng 已提交
540
    SDiskID did = {0};
541
    tfsAllocDisk(pTsdb->pVnode->pTfs, 0, &did);
H
Hongze Cheng 已提交
542 543
    tfsMkdirRecurAt(pTsdb->pVnode->pTfs, pTsdb->path, did);

544
    wSet.diskId = did;
H
Hongze Cheng 已提交
545
    wSet.fid = pCommitter->commitFid;
H
Hongze Cheng 已提交
546 547 548
    wSet.pHeadF = &fHead;
    wSet.pDataF = &fData;
    wSet.pSmaF = &fSma;
H
Hongze Cheng 已提交
549
    wSet.nLastF = 1;
H
Hongze Cheng 已提交
550
    wSet.aLastF[0] = &fLast;
H
Hongze Cheng 已提交
551
  }
H
Hongze Cheng 已提交
552
  code = tsdbDataFWriterOpen(&pCommitter->dWriter.pWriter, pTsdb, &wSet);
H
Hongze Cheng 已提交
553
  if (code) goto _err;
H
Hongze Cheng 已提交
554

H
Hongze Cheng 已提交
555 556 557 558 559 560
  taosArrayClear(pCommitter->dWriter.aBlockIdx);
  taosArrayClear(pCommitter->dWriter.aBlockL);
  tMapDataReset(&pCommitter->dWriter.mBlock);
  tBlockDataReset(&pCommitter->dWriter.bData);
  tBlockDataReset(&pCommitter->dWriter.bDatal);

H
Hongze Cheng 已提交
561 562 563 564
  // open iter
  code = tsdbOpenCommitIter(pCommitter);
  if (code) goto _err;

H
Hongze Cheng 已提交
565
_exit:
H
Hongze Cheng 已提交
566 567 568
  return code;

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

H
Hongze Cheng 已提交
573
static int32_t tsdbCommitDataBlock(SCommitter *pCommitter, SBlock *pBlock) {
H
Hongze Cheng 已提交
574 575 576
  int32_t     code = 0;
  SBlockData *pBlockData = &pCommitter->dWriter.bData;
  SBlock      block;
H
Hongze Cheng 已提交
577

H
Hongze Cheng 已提交
578
  ASSERT(pBlockData->nRow > 0);
H
Hongze Cheng 已提交
579

H
Hongze Cheng 已提交
580 581 582 583 584 585
  if (pBlock) {
    block = *pBlock;  // as a subblock
  } else {
    tBlockReset(&block);  // as a new block
  }

H
Hongze Cheng 已提交
586
  // info
H
Hongze Cheng 已提交
587 588 589
  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 已提交
590

H
Hongze Cheng 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
    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 已提交
610 611
  block.nSubBlock++;
  code = tsdbWriteBlockData(pCommitter->dWriter.pWriter, pBlockData, &block.aSubBlock[block.nSubBlock - 1],
H
Hongze Cheng 已提交
612
                            ((block.nSubBlock == 1) && !block.hasDup) ? &block.smaInfo : NULL, pCommitter->cmprAlg, 0);
H
Hongze Cheng 已提交
613 614 615
  if (code) goto _err;

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

H
Hongze Cheng 已提交
619
  // clear
H
Hongze Cheng 已提交
620
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
621

H
Hongze Cheng 已提交
622 623 624 625
  return code;

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

static int32_t tsdbCommitLastBlock(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
630 631 632 633 634 635
  int32_t     code = 0;
  SBlockL     blockL;
  SBlockData *pBlockData = &pCommitter->dWriter.bDatal;

  ASSERT(pBlockData->nRow > 0);

H
Hongze Cheng 已提交
636
  // info
H
Hongze Cheng 已提交
637 638
  blockL.suid = pBlockData->suid;
  blockL.nRow = pBlockData->nRow;
H
Hongze Cheng 已提交
639 640
  blockL.minKey = TSKEY_MAX;
  blockL.maxKey = TSKEY_MIN;
H
Hongze Cheng 已提交
641 642 643
  blockL.minVer = VERSION_MAX;
  blockL.maxVer = VERSION_MIN;
  for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
H
Hongze Cheng 已提交
644 645
    blockL.minKey = TMIN(blockL.minKey, pBlockData->aTSKEY[iRow]);
    blockL.maxKey = TMAX(blockL.maxKey, pBlockData->aTSKEY[iRow]);
H
Hongze Cheng 已提交
646
    blockL.minVer = TMIN(blockL.minVer, pBlockData->aVersion[iRow]);
H
Hongze Cheng 已提交
647
    blockL.maxVer = TMAX(blockL.maxVer, pBlockData->aVersion[iRow]);
H
Hongze Cheng 已提交
648 649 650
  }
  blockL.minUid = pBlockData->uid ? pBlockData->uid : pBlockData->aUid[0];
  blockL.maxUid = pBlockData->uid ? pBlockData->uid : pBlockData->aUid[pBlockData->nRow - 1];
H
Hongze Cheng 已提交
651

H
Hongze Cheng 已提交
652
  // write
H
Hongze Cheng 已提交
653
  code = tsdbWriteBlockData(pCommitter->dWriter.pWriter, pBlockData, &blockL.bInfo, NULL, pCommitter->cmprAlg, 1);
H
Hongze Cheng 已提交
654
  if (code) goto _err;
H
Hongze Cheng 已提交
655

H
Hongze Cheng 已提交
656
  // push SBlockL
H
Hongze Cheng 已提交
657 658
  if (taosArrayPush(pCommitter->dWriter.aBlockL, &blockL) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
659
    goto _err;
H
Hongze Cheng 已提交
660 661
  }

H
Hongze Cheng 已提交
662
  // clear
H
Hongze Cheng 已提交
663
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
664

H
Hongze Cheng 已提交
665 666 667 668
  return code;

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

H
Hongze Cheng 已提交
672
#if 0
H
Hongze Cheng 已提交
673
static int32_t tsdbMergeCommitDataBlock(SCommitter *pCommitter, STbDataIter *pIter, SBlock *pBlock) {
H
Hongze Cheng 已提交
674
  int32_t     code = 0;
H
Hongze Cheng 已提交
675 676 677
  STbData    *pTbData = pIter->pTbData;
  SBlockData *pBlockDataR = &pCommitter->dReader.bData;
  SBlockData *pBlockDataW = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
678

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

H
Hongze Cheng 已提交
682
  tBlockDataClear(pBlockDataW);
H
Hongze Cheng 已提交
683 684 685 686 687 688 689
  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 已提交
690

H
Hongze Cheng 已提交
691 692 693
    if (c < 0) {
      code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow1));
      if (code) goto _err;
H
Hongze Cheng 已提交
694

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

H
Hongze Cheng 已提交
698 699 700 701
      // next
      tsdbTbDataIterNext(pIter);
      pRow1 = tsdbTbDataIterGet(pIter);
    } else if (c > 0) {
H
Hongze Cheng 已提交
702
      code = tBlockDataAppendRow(pBlockDataW, pRow2, NULL, pTbData->uid);
H
Hongze Cheng 已提交
703
      if (code) goto _err;
H
Hongze Cheng 已提交
704

H
Hongze Cheng 已提交
705 706 707
      iRow++;
      if (iRow < pBlockDataR->nRow) {
        *pRow2 = tsdbRowFromBlockData(pBlockDataR, iRow);
H
Hongze Cheng 已提交
708
      } else {
H
Hongze Cheng 已提交
709
        pRow2 = NULL;
H
Hongze Cheng 已提交
710
      }
H
Hongze Cheng 已提交
711 712
    } else {
      ASSERT(0);
H
Hongze Cheng 已提交
713 714
    }

H
Hongze Cheng 已提交
715 716
    // check
    if (pBlockDataW->nRow >= pCommitter->maxRow * 4 / 5) {
H
Hongze Cheng 已提交
717
      code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
718
      if (code) goto _err;
H
Hongze Cheng 已提交
719
    }
H
Hongze Cheng 已提交
720
  }
H
Hongze Cheng 已提交
721

H
Hongze Cheng 已提交
722
  while (pRow2) {
H
Hongze Cheng 已提交
723
    code = tBlockDataAppendRow(pBlockDataW, pRow2, NULL, pTbData->uid);
H
Hongze Cheng 已提交
724 725
    if (code) goto _err;

H
Hongze Cheng 已提交
726 727 728
    iRow++;
    if (iRow < pBlockDataR->nRow) {
      *pRow2 = tsdbRowFromBlockData(pBlockDataR, iRow);
H
Hongze Cheng 已提交
729
    } else {
H
Hongze Cheng 已提交
730
      pRow2 = NULL;
H
Hongze Cheng 已提交
731 732
    }

H
Hongze Cheng 已提交
733 734
    // check
    if (pBlockDataW->nRow >= pCommitter->maxRow * 4 / 5) {
H
Hongze Cheng 已提交
735
      code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
736
      if (code) goto _err;
H
Hongze Cheng 已提交
737
    }
H
Hongze Cheng 已提交
738
  }
H
Hongze Cheng 已提交
739

H
Hongze Cheng 已提交
740 741
  // check
  if (pBlockDataW->nRow > 0) {
H
Hongze Cheng 已提交
742
    code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
743
    if (code) goto _err;
H
Hongze Cheng 已提交
744 745 746 747 748
  }

  return code;

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

H
Hongze Cheng 已提交
753
static int32_t tsdbCommitTableMemData(SCommitter *pCommitter, STbDataIter *pIter, TSDBKEY toKey) {
H
Hongze Cheng 已提交
754
  int32_t code = 0;
H
Hongze Cheng 已提交
755
  STbData    *pTbData = pIter->pTbData;
H
Hongze Cheng 已提交
756
  SBlockData *pBlockData = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
757

H
Hongze Cheng 已提交
758
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
759
  TSDBROW *pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
760
  while (true) {
H
Hongze Cheng 已提交
761
    if (pRow == NULL) {
H
Hongze Cheng 已提交
762 763 764 765 766 767 768
      if (pBlockData->nRow > 0) {
        goto _write_block;
      } else {
        break;
      }
    }

H
Hongze Cheng 已提交
769
    // update schema
H
Hongze Cheng 已提交
770
    code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow));
H
Hongze Cheng 已提交
771 772
    if (code) goto _err;

H
Hongze Cheng 已提交
773
    // append
H
Hongze Cheng 已提交
774
    code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema, pTbData->uid);
H
Hongze Cheng 已提交
775 776 777 778
    if (code) goto _err;

    tsdbTbDataIterNext(pIter);
    pRow = tsdbTbDataIterGet(pIter);
779
    if (pRow) {
H
Hongze Cheng 已提交
780 781
      TSDBKEY rowKey = TSDBROW_KEY(pRow);
      if (tsdbKeyCmprFn(&rowKey, &toKey) >= 0) {
782 783 784
        pRow = NULL;
      }
    }
H
Hongze Cheng 已提交
785

H
Hongze Cheng 已提交
786 787
    if (pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
    _write_block:
H
Hongze Cheng 已提交
788
      code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
789 790
      if (code) goto _err;
    }
H
Hongze Cheng 已提交
791 792 793 794 795
  }

  return code;

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

H
Hongze Cheng 已提交
800 801
static int32_t tsdbGetNumOfRowsLessThan(STbDataIter *pIter, TSDBKEY key) {
  int32_t nRow = 0;
H
Hongze Cheng 已提交
802

H
Hongze Cheng 已提交
803
  STbDataIter iter = *pIter;
H
Hongze Cheng 已提交
804
  while (true) {
H
Hongze Cheng 已提交
805
    TSDBROW *pRow = tsdbTbDataIterGet(&iter);
H
Hongze Cheng 已提交
806 807
    if (pRow == NULL) break;

H
Hongze Cheng 已提交
808 809
    int32_t c = tsdbKeyCmprFn(&TSDBROW_KEY(pRow), &key);
    if (c < 0) {
H
Hongze Cheng 已提交
810
      nRow++;
H
Hongze Cheng 已提交
811
      tsdbTbDataIterNext(&iter);
H
Hongze Cheng 已提交
812 813 814 815 816 817 818 819 820 821 822 823
    } 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 已提交
824
  STbData    *pTbData = pIter->pTbData;
H
Hongze Cheng 已提交
825
  SBlockData *pBlockData = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
826

H
Hongze Cheng 已提交
827
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
828
  TSDBROW *pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
829
  while (true) {
H
Hongze Cheng 已提交
830 831 832 833 834 835 836 837 838 839
    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 已提交
840
    if (pRow) {
H
Hongze Cheng 已提交
841 842
      TSDBKEY rowKey = TSDBROW_KEY(pRow);
      if (tsdbKeyCmprFn(&rowKey, &pBlock->maxKey) > 0) {
H
Hongze Cheng 已提交
843 844 845 846 847
        pRow = NULL;
      }
    }
  }

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

H
Hongze Cheng 已提交
850
  code = tsdbCommitDataBlock(pCommitter, pBlock);
H
Hongze Cheng 已提交
851 852 853 854 855
  if (code) goto _err;

  return code;

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

H
Hongze Cheng 已提交
860 861 862 863 864
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 已提交
865 866 867 868 869

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

H
Hongze Cheng 已提交
870
  if (pRow == NULL) goto _exit;
H
Hongze Cheng 已提交
871

H
Hongze Cheng 已提交
872 873 874
  if (pBlockData->suid || pBlockData->uid) {
    if (pBlockData->suid != pTbData->suid || pBlockData->suid == 0) {
      if (pBlockData->nRow > 0) {
H
Hongze Cheng 已提交
875 876 877 878
        code = tsdbCommitLastBlock(pCommitter);
        if (code) goto _err;
      }

H
Hongze Cheng 已提交
879
      tBlockDataReset(pBlockData);
H
Hongze Cheng 已提交
880
    }
H
Hongze Cheng 已提交
881
  }
H
Hongze Cheng 已提交
882

H
Hongze Cheng 已提交
883
  if (!pBlockData->suid && !pBlockData->uid) {
H
Hongze Cheng 已提交
884
    code = tBlockDataInit(pBlockData, pTbData->suid, 0, pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
885 886
    if (code) goto _err;
  }
H
Hongze Cheng 已提交
887

H
Hongze Cheng 已提交
888 889 890
  while (pRow) {
    code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow));
    if (code) goto _err;
H
Hongze Cheng 已提交
891

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

H
Hongze Cheng 已提交
895 896 897 898
    tsdbTbDataIterNext(pIter);
    pRow = tsdbTbDataIterGet(pIter);
    if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
      pRow = NULL;
H
Hongze Cheng 已提交
899 900
    }

H
Hongze Cheng 已提交
901 902
    if (pBlockData->nRow >= pCommitter->maxRow) {
      code = tsdbCommitLastBlock(pCommitter);
H
Hongze Cheng 已提交
903
      if (code) goto _err;
H
Hongze Cheng 已提交
904 905 906
    }
  }

H
Hongze Cheng 已提交
907
_exit:
H
Hongze Cheng 已提交
908 909 910
  return code;

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

H
Hongze Cheng 已提交
915
static int32_t tsdbMergeCommitData(SCommitter *pCommitter, STbDataIter *pIter) {
H
Hongze Cheng 已提交
916 917 918 919 920 921
  int32_t  code = 0;
  STbData *pTbData = pIter->pTbData;
  int32_t  iBlock = 0;
  SBlock   block;
  SBlock  *pBlock = &block;
  TSDBROW *pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
922

H
Hongze Cheng 已提交
923
  if (pCommitter->dReader.pBlockIdx && tTABLEIDCmprFn(pTbData, pCommitter->dReader.pBlockIdx) == 0) {
H
Hongze Cheng 已提交
924 925 926 927 928 929
    tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
  } else {
    pBlock = NULL;
  }

  while (pBlock && pRow) {
H
Hongze Cheng 已提交
930 931 932 933
    SBlock  tBlock = {.minKey = TSDBROW_KEY(pRow), .maxKey = TSDBROW_KEY(pRow)};
    int32_t c = tBlockCmprFn(pBlock, &tBlock);

    if (c < 0) {
H
Hongze Cheng 已提交
934 935 936 937 938 939 940 941 942
      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 已提交
943
    } else if (c > 0) {
H
Hongze Cheng 已提交
944
      code = tsdbCommitTableMemData(pCommitter, pIter, pBlock->minKey);
H
Hongze Cheng 已提交
945 946 947 948 949 950
      if (code) goto _err;

      pRow = tsdbTbDataIterGet(pIter);
      if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
        pRow = NULL;
      }
H
Hongze Cheng 已提交
951
    } else {
H
Hongze Cheng 已提交
952
      int32_t nOvlp = tsdbGetNumOfRowsLessThan(pIter, pBlock->maxKey);
H
Hongze Cheng 已提交
953 954 955 956 957 958 959

      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 已提交
960
        code = tsdbMergeCommitDataBlock(pCommitter, pIter, pBlock);
H
Hongze Cheng 已提交
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
        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 已提交
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
_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 已提交
1006 1007
  tMapDataReset(&pCommitter->dWriter.mBlock);

H
Hongze Cheng 已提交
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
  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 已提交
1021 1022 1023 1024 1025 1026 1027
  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 已提交
1028 1029 1030 1031 1032
  // commit data
  code = tsdbMergeCommitData(pCommitter, &iter);
  if (code) goto _err;

  // commit last
H
Hongze Cheng 已提交
1033
  code = tsdbCommitLastFile(pCommitter, &iter);
H
Hongze Cheng 已提交
1034
  if (code) goto _err;
H
Hongze Cheng 已提交
1035

H
Hongze Cheng 已提交
1036
_exit:
H
Hongze Cheng 已提交
1037 1038
  if (pCommitter->dWriter.mBlock.nItem > 0) {
    SBlockIdx blockIdx = {.suid = pTbData->suid, .uid = pTbData->uid};
H
Hongze Cheng 已提交
1039
    code = tsdbWriteBlock(pCommitter->dWriter.pWriter, &pCommitter->dWriter.mBlock, &blockIdx);
H
Hongze Cheng 已提交
1040 1041 1042 1043 1044 1045 1046
    if (code) goto _err;

    if (taosArrayPush(pCommitter->dWriter.aBlockIdx, &blockIdx) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
  }
H
Hongze Cheng 已提交
1047
  pRow = tsdbTbDataIterGet(&iter);
H
Hongze Cheng 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
  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 已提交
1058
#endif
H
Hongze Cheng 已提交
1059

H
Hongze Cheng 已提交
1060 1061 1062
static int32_t tsdbCommitFileDataEnd(SCommitter *pCommitter) {
  int32_t code = 0;

H
Hongze Cheng 已提交
1063
  // write aBlockIdx
H
Hongze Cheng 已提交
1064
  code = tsdbWriteBlockIdx(pCommitter->dWriter.pWriter, pCommitter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
1065 1066
  if (code) goto _err;

H
Hongze Cheng 已提交
1067
  // write aBlockL
H
Hongze Cheng 已提交
1068
  code = tsdbWriteBlockL(pCommitter->dWriter.pWriter, pCommitter->dWriter.aBlockL);
H
Hongze Cheng 已提交
1069 1070
  if (code) goto _err;

H
Hongze Cheng 已提交
1071
  // update file header
H
Hongze Cheng 已提交
1072
  code = tsdbUpdateDFileSetHeader(pCommitter->dWriter.pWriter);
H
Hongze Cheng 已提交
1073 1074 1075
  if (code) goto _err;

  // upsert SDFileSet
H
Hongze Cheng 已提交
1076
  code = tsdbFSUpsertFSet(&pCommitter->fs, &pCommitter->dWriter.pWriter->wSet);
H
Hongze Cheng 已提交
1077 1078 1079
  if (code) goto _err;

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

H
Hongze Cheng 已提交
1083 1084
  if (pCommitter->dReader.pReader) {
    code = tsdbDataFReaderClose(&pCommitter->dReader.pReader);
H
Hongze Cheng 已提交
1085
    if (code) goto _err;
H
Hongze Cheng 已提交
1086 1087 1088 1089 1090 1091
  }

_exit:
  return code;

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

H
Hongze Cheng 已提交
1096 1097 1098
static int32_t tsdbMoveCommitData(SCommitter *pCommitter, TABLEID toTable) {
  int32_t code = 0;

H
Hongze Cheng 已提交
1099
  while (pCommitter->dReader.pBlockIdx && tTABLEIDCmprFn(pCommitter->dReader.pBlockIdx, &toTable) < 0) {
H
Hongze Cheng 已提交
1100
    SBlockIdx blockIdx = *pCommitter->dReader.pBlockIdx;
H
Hongze Cheng 已提交
1101
    code = tsdbWriteBlock(pCommitter->dWriter.pWriter, &pCommitter->dReader.mBlock, &blockIdx);
H
Hongze Cheng 已提交
1102 1103 1104 1105 1106 1107 1108
    if (code) goto _err;

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

H
Hongze Cheng 已提交
1109 1110
    code = tsdbCommitterNextTableData(pCommitter);
    if (code) goto _err;
H
Hongze Cheng 已提交
1111 1112 1113 1114 1115 1116 1117 1118 1119
  }

  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 已提交
1120
static int32_t tsdbCommitFileDataImpl(SCommitter *pCommitter);
H
Hongze Cheng 已提交
1121
static int32_t tsdbCommitFileData(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1122 1123 1124
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1125 1126 1127 1128 1129

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

H
Hongze Cheng 已提交
1130 1131 1132 1133 1134
#if 1
  // impl
  code = tsdbCommitFileDataImpl(pCommitter);
  if (code) goto _err;
#else
H
Hongze Cheng 已提交
1135
  // commit file data impl
H
Hongze Cheng 已提交
1136 1137
  for (int32_t iTbData = 0; iTbData < taosArrayGetSize(pCommitter->aTbDataP); iTbData++) {
    STbData *pTbData = (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData);
H
Hongze Cheng 已提交
1138

H
Hongze Cheng 已提交
1139
    // move commit until current (suid, uid)
H
Hongze Cheng 已提交
1140
    code = tsdbMoveCommitData(pCommitter, *(TABLEID *)pTbData);
H
Hongze Cheng 已提交
1141 1142
    if (code) goto _err;

H
Hongze Cheng 已提交
1143
    // commit current table data
H
Hongze Cheng 已提交
1144
    code = tsdbCommitTableData(pCommitter, pTbData);
H
Hongze Cheng 已提交
1145
    if (code) goto _err;
H
Hongze Cheng 已提交
1146 1147 1148 1149 1150 1151

    // 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 已提交
1152
  }
H
Hongze Cheng 已提交
1153

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

H
Hongze Cheng 已提交
1157
  if (pCommitter->dWriter.bDatal.nRow > 0) {
H
Hongze Cheng 已提交
1158
    code = tsdbCommitLastBlock(pCommitter);
H
Hongze Cheng 已提交
1159
    if (code) goto _err;
H
Hongze Cheng 已提交
1160
  }
H
Hongze Cheng 已提交
1161
#endif
H
Hongze Cheng 已提交
1162

H
Hongze Cheng 已提交
1163 1164
  // commit file data end
  code = tsdbCommitFileDataEnd(pCommitter);
H
Hongze Cheng 已提交
1165
  if (code) goto _err;
H
Hongze Cheng 已提交
1166 1167 1168 1169

  return code;

_err:
S
Shengliang Guan 已提交
1170
  tsdbError("vgId:%d, commit file data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1171 1172
  tsdbDataFReaderClose(&pCommitter->dReader.pReader);
  tsdbDataFWriterClose(&pCommitter->dWriter.pWriter, 0);
H
Hongze Cheng 已提交
1173 1174 1175
  return code;
}

H
Hongze Cheng 已提交
1176 1177
// ----------------------------------------------------------------------------
static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1178
  int32_t code = 0;
H
Hongze Cheng 已提交
1179

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

H
more  
Hongze Cheng 已提交
1183
  taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1184 1185
  pTsdb->imem = pTsdb->mem;
  pTsdb->mem = NULL;
H
more  
Hongze Cheng 已提交
1186
  taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1187

H
Hongze Cheng 已提交
1188
  pCommitter->pTsdb = pTsdb;
H
Hongze Cheng 已提交
1189
  pCommitter->commitID = pTsdb->pVnode->state.commitID;
H
Hongze Cheng 已提交
1190 1191 1192 1193
  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 已提交
1194
  pCommitter->cmprAlg = pTsdb->pVnode->config.tsdbCfg.compression;
H
Hongze Cheng 已提交
1195
  pCommitter->maxLast = TSDB_DEFAULT_LAST_FILE;  // TODO: make it as a config
H
Hongze Cheng 已提交
1196 1197 1198 1199 1200
  pCommitter->aTbDataP = tsdbMemTableGetTbDataArray(pTsdb->imem);
  if (pCommitter->aTbDataP == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
1201
  code = tsdbFSCopy(pTsdb, &pCommitter->fs);
H
Hongze Cheng 已提交
1202 1203 1204 1205 1206
  if (code) goto _err;

  return code;

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

H
Hongze Cheng 已提交
1211 1212 1213
static int32_t tsdbCommitDataStart(SCommitter *pCommitter) {
  int32_t code = 0;

H
Hongze Cheng 已提交
1214
  // reader
H
Hongze Cheng 已提交
1215 1216
  pCommitter->dReader.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pCommitter->dReader.aBlockIdx == NULL) {
H
Hongze Cheng 已提交
1217 1218 1219 1220
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
1221
  code = tBlockDataCreate(&pCommitter->dReader.bData);
H
Hongze Cheng 已提交
1222 1223
  if (code) goto _exit;

H
Hongze Cheng 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
  // merger
  for (int32_t iLast = 0; iLast < TSDB_MAX_LAST_FILE; iLast++) {
    SDataIter *pIter = &pCommitter->aDataIter[iLast];
    pIter->aBlockL = taosArrayInit(0, sizeof(SBlockL));
    if (pIter->aBlockL == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }

    code = tBlockDataCreate(&pIter->bData);
    if (code) goto _exit;
  }

  // writer
H
Hongze Cheng 已提交
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
  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 已提交
1250
  code = tBlockDataCreate(&pCommitter->dWriter.bData);
H
Hongze Cheng 已提交
1251
  if (code) goto _exit;
H
Hongze Cheng 已提交
1252

H
Hongze Cheng 已提交
1253
  code = tBlockDataCreate(&pCommitter->dWriter.bDatal);
H
Hongze Cheng 已提交
1254 1255
  if (code) goto _exit;

H
Hongze Cheng 已提交
1256 1257 1258 1259 1260
_exit:
  return code;
}

static void tsdbCommitDataEnd(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1261
  // reader
H
Hongze Cheng 已提交
1262 1263
  taosArrayDestroy(pCommitter->dReader.aBlockIdx);
  tMapDataClear(&pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
1264
  tBlockDataDestroy(&pCommitter->dReader.bData, 1);
H
Hongze Cheng 已提交
1265

H
Hongze Cheng 已提交
1266 1267 1268 1269 1270 1271 1272 1273
  // merger
  for (int32_t iLast = 0; iLast < TSDB_MAX_LAST_FILE; iLast++) {
    SDataIter *pIter = &pCommitter->aDataIter[iLast];
    taosArrayDestroy(pIter->aBlockL);
    tBlockDataDestroy(&pIter->bData, 1);
  }

  // writer
H
Hongze Cheng 已提交
1274
  taosArrayDestroy(pCommitter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
1275
  taosArrayDestroy(pCommitter->dWriter.aBlockL);
H
Hongze Cheng 已提交
1276
  tMapDataClear(&pCommitter->dWriter.mBlock);
H
Hongze Cheng 已提交
1277 1278
  tBlockDataDestroy(&pCommitter->dWriter.bData, 1);
  tBlockDataDestroy(&pCommitter->dWriter.bDatal, 1);
H
Hongze Cheng 已提交
1279 1280
  tTSchemaDestroy(pCommitter->skmTable.pTSchema);
  tTSchemaDestroy(pCommitter->skmRow.pTSchema);
H
Hongze Cheng 已提交
1281 1282
}

H
Hongze Cheng 已提交
1283 1284 1285 1286
static int32_t tsdbCommitData(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1287

H
Hongze Cheng 已提交
1288
  // check
H
Hongze Cheng 已提交
1289
  if (pMemTable->nRow == 0) goto _exit;
H
Hongze Cheng 已提交
1290

H
Hongze Cheng 已提交
1291 1292
  // start ====================
  code = tsdbCommitDataStart(pCommitter);
H
Hongze Cheng 已提交
1293
  if (code) goto _err;
H
Hongze Cheng 已提交
1294 1295 1296

  // impl ====================
  pCommitter->nextKey = pMemTable->minKey;
H
Hongze Cheng 已提交
1297 1298
  while (pCommitter->nextKey < TSKEY_MAX) {
    code = tsdbCommitFileData(pCommitter);
H
Hongze Cheng 已提交
1299
    if (code) goto _err;
H
Hongze Cheng 已提交
1300
  }
H
Hongze Cheng 已提交
1301

H
Hongze Cheng 已提交
1302 1303 1304
  // end ====================
  tsdbCommitDataEnd(pCommitter);

H
Hongze Cheng 已提交
1305
_exit:
H
Hongze Cheng 已提交
1306
  tsdbInfo("vgId:%d, commit data done, nRow:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nRow);
H
Hongze Cheng 已提交
1307
  return code;
H
Hongze Cheng 已提交
1308

H
Hongze Cheng 已提交
1309
_err:
H
Hongze Cheng 已提交
1310
  tsdbCommitDataEnd(pCommitter);
S
Shengliang Guan 已提交
1311
  tsdbError("vgId:%d, commit data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1312 1313
  return code;
}
H
Hongze Cheng 已提交
1314

H
Hongze Cheng 已提交
1315 1316 1317 1318
static int32_t tsdbCommitDel(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1319

H
Hongze Cheng 已提交
1320 1321
  if (pMemTable->nDel == 0) {
    goto _exit;
H
Hongze Cheng 已提交
1322
  }
H
Hongze Cheng 已提交
1323

H
Hongze Cheng 已提交
1324 1325 1326 1327 1328
  // start
  code = tsdbCommitDelStart(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1329

H
Hongze Cheng 已提交
1330
  // impl
H
Hongze Cheng 已提交
1331 1332 1333
  int32_t  iDelIdx = 0;
  int32_t  nDelIdx = taosArrayGetSize(pCommitter->aDelIdx);
  int32_t  iTbData = 0;
H
Hongze Cheng 已提交
1334
  int32_t  nTbData = taosArrayGetSize(pCommitter->aTbDataP);
H
Hongze Cheng 已提交
1335 1336 1337 1338 1339
  STbData *pTbData;
  SDelIdx *pDelIdx;

  ASSERT(nTbData > 0);

H
Hongze Cheng 已提交
1340
  pTbData = (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData);
H
Hongze Cheng 已提交
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
  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 已提交
1366
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData) : NULL;
H
Hongze Cheng 已提交
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
    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 已提交
1382
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData) : NULL;
H
Hongze Cheng 已提交
1383 1384 1385
    iDelIdx++;
    pDelIdx = (iDelIdx < nDelIdx) ? (SDelIdx *)taosArrayGet(pCommitter->aDelIdx, iDelIdx) : NULL;
    continue;
H
Hongze Cheng 已提交
1386
  }
H
Hongze Cheng 已提交
1387

H
Hongze Cheng 已提交
1388 1389 1390 1391 1392
  // end
  code = tsdbCommitDelEnd(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1393

H
Hongze Cheng 已提交
1394
_exit:
S
Shengliang Guan 已提交
1395
  tsdbDebug("vgId:%d, commit del done, nDel:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nDel);
H
Hongze Cheng 已提交
1396 1397 1398
  return code;

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

H
Hongze Cheng 已提交
1403
static int32_t tsdbEndCommit(SCommitter *pCommitter, int32_t eno) {
H
Hongze Cheng 已提交
1404 1405 1406 1407
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

H
Hongze Cheng 已提交
1408
  ASSERT(eno == 0);
H
Hongze Cheng 已提交
1409 1410 1411

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

H
Hongze Cheng 已提交
1413
  // lock
H
Hongze Cheng 已提交
1414
  taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1415 1416 1417 1418 1419 1420 1421 1422

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

H
Hongze Cheng 已提交
1423
  pTsdb->imem = NULL;
H
Hongze Cheng 已提交
1424 1425

  // unlock
H
Hongze Cheng 已提交
1426 1427 1428
  taosThreadRwlockUnlock(&pTsdb->rwLock);

  tsdbUnrefMemTable(pMemTable);
H
Hongze Cheng 已提交
1429
  tsdbFSDestroy(&pCommitter->fs);
H
Hongze Cheng 已提交
1430
  taosArrayDestroy(pCommitter->aTbDataP);
H
Hongze Cheng 已提交
1431

H
Hongze Cheng 已提交
1432 1433 1434 1435
  // if (pCommitter->toMerge) {
  //   code = tsdbMerge(pTsdb);
  //   if (code) goto _err;
  // }
H
Hongze Cheng 已提交
1436

S
Shengliang Guan 已提交
1437
  tsdbInfo("vgId:%d, tsdb end commit", TD_VID(pTsdb->pVnode));
H
Hongze Cheng 已提交
1438 1439 1440
  return code;

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

H
Hongze Cheng 已提交
1445
// ================================================================================
H
Hongze Cheng 已提交
1446

H
Hongze Cheng 已提交
1447 1448
static FORCE_INLINE SRowInfo *tsdbGetCommitRow(SCommitter *pCommitter) {
  return (pCommitter->pIter) ? &pCommitter->pIter->r : NULL;
H
Hongze Cheng 已提交
1449 1450
}

H
Hongze Cheng 已提交
1451
static int32_t tsdbNextCommitRow(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
  int32_t code = 0;

  if (pCommitter->pIter) {
    SDataIter *pIter = pCommitter->pIter;
    if (pCommitter->pIter->type == 0) {  // memory
      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 已提交
1473 1474
        if (pIter->iTbDataP < taosArrayGetSize(pCommitter->aTbDataP)) {
          STbData *pTbData = (STbData *)taosArrayGetP(pCommitter->aTbDataP, pIter->iTbDataP);
H
Hongze Cheng 已提交
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
          TSDBKEY  keyFrom = {.ts = pCommitter->minKey, .version = VERSION_MIN};
          tsdbTbDataIterOpen(pTbData, &keyFrom, 0, &pIter->iter);
          pRow = tsdbTbDataIterGet(&pIter->iter);
          continue;
        } else {
          pCommitter->pIter = NULL;
          break;
        }
      }
    } else if (pCommitter->pIter->type == 1) {  // last file
      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 {
        pIter->iBlockL++;
        if (pIter->iBlockL < taosArrayGetSize(pIter->aBlockL)) {
          SBlockL *pBlockL = (SBlockL *)taosArrayGet(pIter->aBlockL, pIter->iBlockL);

          code = tsdbReadLastBlockEx(pCommitter->dReader.pReader, pIter->iLast, pBlockL, &pIter->bData);
          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:
  return code;
}

H
Hongze Cheng 已提交
1533
static int32_t tsdbCommitAheadBlock(SCommitter *pCommitter, SBlock *pBlock) {
H
Hongze Cheng 已提交
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
  int32_t     code = 0;
  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));
    if (code) goto _err;

    code = tBlockDataAppendRow(pBlockData, &pRowInfo->row, pCommitter->skmRow.pTSchema, id.uid);
    if (code) goto _err;

    code = tsdbNextCommitRow(pCommitter);
    if (code) goto _err;

    pRowInfo = tsdbGetCommitRow(pCommitter);
    if (pRowInfo) {
      if (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid) {
        pRowInfo = NULL;
      } else {
        TSDBKEY tKey = TSDBROW_KEY(&pRowInfo->row);
        if (tsdbKeyCmprFn(&tKey, &pBlock->minKey) >= 0) pRowInfo = NULL;
      }
    }

    if (pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
      code = tsdbCommitDataBlock(pCommitter, NULL);
      if (code) goto _err;
    }
  }

  if (pBlockData->nRow) {
    code = tsdbCommitDataBlock(pCommitter, NULL);
    if (code) goto _err;
  }

  return code;

_err:
  tsdbError("vgId:%d, tsdb commit ahead block failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1576 1577 1578 1579
  return code;
}

static int32_t tsdbCommitMergeBlock(SCommitter *pCommitter, SBlock *pBlock) {
H
Hongze Cheng 已提交
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
  int32_t     code = 0;
  SRowInfo   *pRowInfo = tsdbGetCommitRow(pCommitter);
  TABLEID     id = {.suid = pRowInfo->suid, .uid = pRowInfo->uid};
  SBlockData *pBDataR = &pCommitter->dReader.bData;
  SBlockData *pBDataW = &pCommitter->dWriter.bData;

  code = tsdbReadDataBlock(pCommitter->dReader.pReader, pBlock, pBDataR);
  if (code) goto _err;

  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);
      if (code) goto _err;

      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));
      if (code) goto _err;

      code = tBlockDataAppendRow(pBDataW, &pRowInfo->row, pCommitter->skmRow.pTSchema, id.uid);
      if (code) goto _err;

      code = tsdbNextCommitRow(pCommitter);
      if (code) goto _err;

      pRowInfo = tsdbGetCommitRow(pCommitter);
      if (pRowInfo) {
        if (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid) {
          pRowInfo = NULL;
        } else {
          TSDBKEY tKey = TSDBROW_KEY(&pRowInfo->row);
          if (tsdbKeyCmprFn(&tKey, &pBlock->maxKey) > 0) pRowInfo = NULL;
        }
      }
    } else {
      ASSERT(0);
    }

    if (pBDataW->nRow >= pCommitter->maxRow * 4 / 5) {
      code = tsdbCommitDataBlock(pCommitter, NULL);
      if (code) goto _err;
    }
  }

  while (pRow) {
    code = tBlockDataAppendRow(pBDataW, pRow, NULL, id.uid);
    if (code) goto _err;

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

    if (pBDataW->nRow >= pCommitter->maxRow * 4 / 5) {
      code = tsdbCommitDataBlock(pCommitter, NULL);
      if (code) goto _err;
    }
  }

  if (pBDataW->nRow) {
    code = tsdbCommitDataBlock(pCommitter, NULL);
    if (code) goto _err;
  }

  return code;

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

H
Hongze Cheng 已提交
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
static int32_t tsdbMergeTableData(SCommitter *pCommitter, TABLEID id) {
  int32_t    code = 0;
  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;
    SBlock    block;
    SBlock   *pBlock = &block;
    SRowInfo *pRowInfo = tsdbGetCommitRow(pCommitter);

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

    tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
    while (pBlock && pRowInfo) {
      SBlock  tBlock = {.minKey = TSDBROW_KEY(&pRowInfo->row), .maxKey = TSDBROW_KEY(&pRowInfo->row)};
      int32_t c = tBlockCmprFn(pBlock, &tBlock);

      if (c < 0) {
        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;
        }
      } else if (c > 0) {
H
Hongze Cheng 已提交
1694 1695 1696 1697 1698
        code = tsdbCommitAheadBlock(pCommitter, pBlock);
        if (code) goto _err;

        pRowInfo = tsdbGetCommitRow(pCommitter);
        if (pRowInfo && (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid)) pRowInfo = NULL;
H
Hongze Cheng 已提交
1699
      } else {
H
Hongze Cheng 已提交
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
        code = tsdbCommitMergeBlock(pCommitter, pBlock);
        if (code) goto _err;

        iBlock++;
        if (iBlock < pCommitter->dReader.mBlock.nItem) {
          tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
        } else {
          pBlock = NULL;
        }
        pRowInfo = tsdbGetCommitRow(pCommitter);
        if (pRowInfo && (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid)) pRowInfo = NULL;
H
Hongze Cheng 已提交
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
      }
    }

    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;
      }
    }
  }

_exit:
  return code;

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

H
Hongze Cheng 已提交
1735
static int32_t tsdbCommitTableData(SCommitter *pCommitter, TABLEID id) {
H
Hongze Cheng 已提交
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
  int32_t   code = 0;
  SRowInfo *pRowInfo = tsdbGetCommitRow(pCommitter);

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

  if (pRowInfo == NULL) goto _err;

#if 0
  if (pBlockData->suid || pBlockData->uid) {
    if (pBlockData->suid != pTbData->suid || pBlockData->suid == 0) {
      if (pBlockData->nRow > 0) {
        code = tsdbCommitLastBlock(pCommitter);
        if (code) goto _err;
      }

      tBlockDataReset(pBlockData);
    }
  }

  if (!pBlockData->suid && !pBlockData->uid) {
    code = tBlockDataInit(pBlockData, pTbData->suid, 0, pCommitter->skmTable.pTSchema);
    if (code) goto _err;
  }
#endif

  SBlockData *pBlockData = NULL;  // TODO
  while (pRowInfo) {
    STSchema *pTSchema = NULL;
    if (pRowInfo->row.type == 0) {
      code = tsdbCommitterUpdateRowSchema(pCommitter, id.suid, id.uid, TSDBROW_SVERSION(&pRowInfo->row));
      if (code) goto _err;

      pTSchema = pCommitter->skmRow.pTSchema;
    }

    code = tBlockDataAppendRow(pBlockData, &pRowInfo->row, pTSchema, id.uid);
    if (code) goto _err;

    code = tsdbNextCommitRow(pCommitter);
    if (code) goto _err;

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

  return code;

_err:
H
Hongze Cheng 已提交
1788 1789 1790
  return code;
}

H
Hongze Cheng 已提交
1791 1792 1793 1794 1795 1796
static int32_t tsdbCommitFileDataImpl(SCommitter *pCommitter) {
  int32_t code = 0;

  SRowInfo *pRowInfo = NULL;
  TABLEID   id = {0};
  while (true) {
H
Hongze Cheng 已提交
1797
    pRowInfo = tsdbGetCommitRow(pCommitter);
H
Hongze Cheng 已提交
1798
    if (pRowInfo == NULL) {
H
Hongze Cheng 已提交
1799 1800 1801 1802 1803 1804 1805 1806 1807
      /* end remain table data commit*/
      code = tsdbMoveCommitData(pCommitter, (TABLEID){.suid = INT64_MAX, .uid = INT64_MAX});
      if (code) goto _err;

      if (pCommitter->dWriter.bDatal.nRow > 0) {
        code = tsdbCommitLastBlock(pCommitter);
        if (code) goto _err;
      }

H
Hongze Cheng 已提交
1808 1809 1810
      break;
    }

H
Hongze Cheng 已提交
1811
    ASSERT(pRowInfo->suid != id.suid || pRowInfo->uid != id.uid);
H
Hongze Cheng 已提交
1812

H
Hongze Cheng 已提交
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
    /* start new table data commit */
    id.suid = pRowInfo->suid;
    id.uid = pRowInfo->uid;
    // reader
    code = tsdbMoveCommitData(pCommitter, id);
    if (code) goto _err;
    // writer
    tMapDataReset(&pCommitter->dWriter.mBlock);
    // other
    code = tsdbCommitterUpdateTableSchema(pCommitter, id.suid, id.uid);
    if (code) goto _err;
H
Hongze Cheng 已提交
1824
    code = tBlockDataInit(&pCommitter->dReader.bData, id.suid, id.uid, pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
1825
    if (code) goto _err;
H
Hongze Cheng 已提交
1826
    code = tBlockDataInit(&pCommitter->dWriter.bData, id.suid, id.uid, pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
1827
    if (code) goto _err;
H
Hongze Cheng 已提交
1828

H
Hongze Cheng 已提交
1829 1830
    /* merge with data in .data file */
    code = tsdbMergeTableData(pCommitter, id);
H
Hongze Cheng 已提交
1831 1832
    if (code) goto _err;

H
Hongze Cheng 已提交
1833
    /* handle remain table data */
H
Hongze Cheng 已提交
1834
    code = tsdbCommitTableData(pCommitter, id);
H
Hongze Cheng 已提交
1835
    if (code) goto _err;
H
Hongze Cheng 已提交
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846

    if (pCommitter->dWriter.mBlock.nItem > 0) {
      SBlockIdx blockIdx = {.suid = id.suid, .uid = id.uid};
      code = tsdbWriteBlock(pCommitter->dWriter.pWriter, &pCommitter->dWriter.mBlock, &blockIdx);
      if (code) goto _err;

      if (taosArrayPush(pCommitter->dWriter.aBlockIdx, &blockIdx) == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        goto _err;
      }
    }
H
Hongze Cheng 已提交
1847 1848 1849 1850 1851 1852 1853 1854
  }

  return code;

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