tsdbCommit.c 41.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
typedef enum { MEMORY_DATA_ITER = 0, LAST_DATA_ITER } EDataIterT;

H
Hongze Cheng 已提交
25 26 27
typedef struct {
  SRBTreeNode n;
  SRowInfo    r;
H
Hongze Cheng 已提交
28
  EDataIterT  type;
H
Hongze Cheng 已提交
29 30 31 32 33 34
  union {
    struct {
      int32_t     iTbDataP;
      STbDataIter iter;
    };  // memory data iter
    struct {
H
Hongze Cheng 已提交
35 36 37
      int32_t    iSst;
      SArray    *aSstBlk;
      int32_t    iSstBlk;
H
Hongze Cheng 已提交
38 39
      SBlockData bData;
      int32_t    iRow;
H
Hongze Cheng 已提交
40
    };  // sst file data iter
H
Hongze Cheng 已提交
41 42 43
  };
} SDataIter;

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

H
Hongze Cheng 已提交
95 96
extern int32_t tsdbReadSstBlockEx(SDataFReader *pReader, int32_t iSst, SSstBlk *aSstBlk,
                                  SBlockData *pBlockData);  // todo
H
Hongze Cheng 已提交
97

H
Hongze Cheng 已提交
98 99 100 101 102
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 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
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 已提交
123

H
refact  
Hongze Cheng 已提交
124
int32_t tsdbBegin(STsdb *pTsdb) {
H
Hongze Cheng 已提交
125
  int32_t code = 0;
H
Hongze Cheng 已提交
126

127 128
  if (!pTsdb) return code;

H
Hongze Cheng 已提交
129 130
  SMemTable *pMemTable;
  code = tsdbMemTableCreate(pTsdb, &pMemTable);
H
Hongze Cheng 已提交
131
  if (code) goto _err;
H
Hongze Cheng 已提交
132

H
Hongze Cheng 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  // 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 已提交
149 150 151
  return code;

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

H
more  
Hongze Cheng 已提交
156
int32_t tsdbCommit(STsdb *pTsdb) {
157
  if (!pTsdb) return 0;
H
Hongze Cheng 已提交
158

H
more  
Hongze Cheng 已提交
159
  int32_t    code = 0;
H
Hongze Cheng 已提交
160 161 162 163
  SCommitter commith;
  SMemTable *pMemTable = pTsdb->mem;

  // check
H
Hongze Cheng 已提交
164
  if (pMemTable->nRow == 0 && pMemTable->nDel == 0) {
H
Hongze Cheng 已提交
165
    taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
166
    pTsdb->mem = NULL;
H
Hongze Cheng 已提交
167 168 169
    taosThreadRwlockUnlock(&pTsdb->rwLock);

    tsdbUnrefMemTable(pMemTable);
H
Hongze Cheng 已提交
170 171
    goto _exit;
  }
H
refact  
Hongze Cheng 已提交
172

H
more  
Hongze Cheng 已提交
173
  // start commit
H
more  
Hongze Cheng 已提交
174
  code = tsdbStartCommit(pTsdb, &commith);
H
Hongze Cheng 已提交
175
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
176

H
refact  
Hongze Cheng 已提交
177 178
  // commit impl
  code = tsdbCommitData(&commith);
H
Hongze Cheng 已提交
179
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
180 181

  code = tsdbCommitDel(&commith);
H
Hongze Cheng 已提交
182
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
183 184

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

H
Hongze Cheng 已提交
188
_exit:
H
refact  
Hongze Cheng 已提交
189 190 191
  return code;

_err:
H
Hongze Cheng 已提交
192
  tsdbEndCommit(&commith, code);
C
Cary Xu 已提交
193
  tsdbError("vgId:%d, failed to commit since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
refact  
Hongze Cheng 已提交
194 195 196
  return code;
}

H
Hongze Cheng 已提交
197
static int32_t tsdbCommitDelStart(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
198 199 200 201
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

H
Hongze Cheng 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
  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 已提交
219

H
Hongze Cheng 已提交
220
  SDelFile *pDelFileR = pCommitter->fs.pDelFile;
H
Hongze Cheng 已提交
221
  if (pDelFileR) {
H
Hongze Cheng 已提交
222
    code = tsdbDelFReaderOpen(&pCommitter->pDelFReader, pDelFileR, pTsdb);
H
Hongze Cheng 已提交
223
    if (code) goto _err;
H
Hongze Cheng 已提交
224

H
Hongze Cheng 已提交
225
    code = tsdbReadDelIdx(pCommitter->pDelFReader, pCommitter->aDelIdx);
H
Hongze Cheng 已提交
226
    if (code) goto _err;
H
Hongze Cheng 已提交
227 228
  }

H
Hongze Cheng 已提交
229
  // prepare new
H
Hongze Cheng 已提交
230 231
  SDelFile wDelFile = {.commitID = pCommitter->commitID, .size = 0, .offset = 0};
  code = tsdbDelFWriterOpen(&pCommitter->pDelFWriter, &wDelFile, pTsdb);
H
Hongze Cheng 已提交
232
  if (code) goto _err;
H
Hongze Cheng 已提交
233 234

_exit:
S
Shengliang Guan 已提交
235
  tsdbDebug("vgId:%d, commit del start", TD_VID(pTsdb->pVnode));
H
Hongze Cheng 已提交
236 237 238
  return code;

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

H
Hongze Cheng 已提交
243
static int32_t tsdbCommitTableDel(SCommitter *pCommitter, STbData *pTbData, SDelIdx *pDelIdx) {
H
Hongze Cheng 已提交
244
  int32_t   code = 0;
H
Hongze Cheng 已提交
245
  SDelData *pDelData;
H
Hongze Cheng 已提交
246 247
  tb_uid_t  suid;
  tb_uid_t  uid;
H
Hongze Cheng 已提交
248 249

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

H
Hongze Cheng 已提交
253 254 255 256
    if (pTbData->pHead == NULL) {
      pTbData = NULL;
    }
  }
H
Hongze Cheng 已提交
257 258

  if (pDelIdx) {
H
Hongze Cheng 已提交
259 260 261
    suid = pDelIdx->suid;
    uid = pDelIdx->uid;

H
Hongze Cheng 已提交
262
    code = tsdbReadDelData(pCommitter->pDelFReader, pDelIdx, pCommitter->aDelData);
H
Hongze Cheng 已提交
263
    if (code) goto _err;
264 265
  } else {
    taosArrayClear(pCommitter->aDelData);
H
Hongze Cheng 已提交
266 267
  }

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

H
Hongze Cheng 已提交
270
  SDelIdx delIdx = {.suid = suid, .uid = uid};
H
Hongze Cheng 已提交
271 272

  // memory
H
Hongze Cheng 已提交
273 274
  pDelData = pTbData ? pTbData->pHead : NULL;
  for (; pDelData; pDelData = pDelData->pNext) {
H
Hongze Cheng 已提交
275 276 277 278
    if (taosArrayPush(pCommitter->aDelData, pDelData) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
279 280 281
  }

  // write
H
Hongze Cheng 已提交
282
  code = tsdbWriteDelData(pCommitter->pDelFWriter, pCommitter->aDelData, &delIdx);
H
Hongze Cheng 已提交
283 284 285
  if (code) goto _err;

  // put delIdx
286
  if (taosArrayPush(pCommitter->aDelIdxN, &delIdx) == NULL) {
H
Hongze Cheng 已提交
287 288 289
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
290 291 292 293 294

_exit:
  return code;

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

H
Hongze Cheng 已提交
299 300
static int32_t tsdbCommitDelEnd(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
301
  STsdb  *pTsdb = pCommitter->pTsdb;
H
Hongze Cheng 已提交
302

H
Hongze Cheng 已提交
303
  code = tsdbWriteDelIdx(pCommitter->pDelFWriter, pCommitter->aDelIdxN);
H
Hongze Cheng 已提交
304
  if (code) goto _err;
H
Hongze Cheng 已提交
305

H
Hongze Cheng 已提交
306 307 308
  code = tsdbUpdateDelFileHdr(pCommitter->pDelFWriter);
  if (code) goto _err;

H
Hongze Cheng 已提交
309
  code = tsdbFSUpsertDelFile(&pCommitter->fs, &pCommitter->pDelFWriter->fDel);
H
Hongze Cheng 已提交
310
  if (code) goto _err;
H
Hongze Cheng 已提交
311

H
Hongze Cheng 已提交
312
  code = tsdbDelFWriterClose(&pCommitter->pDelFWriter, 1);
H
Hongze Cheng 已提交
313
  if (code) goto _err;
H
Hongze Cheng 已提交
314 315

  if (pCommitter->pDelFReader) {
H
Hongze Cheng 已提交
316
    code = tsdbDelFReaderClose(&pCommitter->pDelFReader);
H
Hongze Cheng 已提交
317 318 319
    if (code) goto _err;
  }

H
Hongze Cheng 已提交
320 321 322 323
  taosArrayDestroy(pCommitter->aDelIdx);
  taosArrayDestroy(pCommitter->aDelData);
  taosArrayDestroy(pCommitter->aDelIdxN);

H
Hongze Cheng 已提交
324 325 326
  return code;

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

H
Hongze Cheng 已提交
331
static int32_t tsdbCommitterUpdateTableSchema(SCommitter *pCommitter, int64_t suid, int64_t uid) {
H
Hongze Cheng 已提交
332 333
  int32_t code = 0;

H
Hongze Cheng 已提交
334
  if (suid) {
H
Hongze Cheng 已提交
335 336 337 338
    if (pCommitter->skmTable.suid == suid) {
      pCommitter->skmTable.uid = uid;
      goto _exit;
    }
H
Hongze Cheng 已提交
339 340
  } else {
    if (pCommitter->skmTable.uid == uid) goto _exit;
H
Hongze Cheng 已提交
341 342 343 344 345
  }

  pCommitter->skmTable.suid = suid;
  pCommitter->skmTable.uid = uid;
  tTSchemaDestroy(pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
346
  code = metaGetTbTSchemaEx(pCommitter->pTsdb->pVnode->pMeta, suid, uid, -1, &pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
347 348 349 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
  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 已提交
378 379 380 381 382 383 384 385 386 387
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 已提交
388
    code = tsdbReadBlock(pCommitter->dReader.pReader, pCommitter->dReader.pBlockIdx, &pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
389 390 391 392 393 394 395 396 397 398 399
    if (code) goto _exit;

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

_exit:
  return code;
}

H
Hongze Cheng 已提交
400 401 402 403
static int32_t tsdbOpenCommitIter(SCommitter *pCommitter) {
  int32_t code = 0;

  pCommitter->pIter = NULL;
H
Hongze Cheng 已提交
404
  tRBTreeCreate(&pCommitter->rbt, tRowInfoCmprFn);
H
Hongze Cheng 已提交
405 406

  // memory
H
Hongze Cheng 已提交
407
  TSDBKEY    tKey = {.ts = pCommitter->minKey, .version = VERSION_MIN};
H
Hongze Cheng 已提交
408 409
  SDataIter *pIter = &pCommitter->dataIter;
  pIter->type = MEMORY_DATA_ITER;
H
Hongze Cheng 已提交
410 411 412 413 414 415 416
  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 已提交
417
      pRow = NULL;
H
Hongze Cheng 已提交
418 419
    }

H
Hongze Cheng 已提交
420 421
    if (pRow == NULL) continue;

H
Hongze Cheng 已提交
422 423 424 425 426
    pIter->r.suid = pTbData->suid;
    pIter->r.uid = pTbData->uid;
    pIter->r.row = *pRow;
    break;
  }
H
Hongze Cheng 已提交
427
  ASSERT(pIter->iTbDataP < taosArrayGetSize(pCommitter->aTbDataP));
H
Hongze Cheng 已提交
428 429 430
  tRBTreePut(&pCommitter->rbt, (SRBTreeNode *)pIter);

  // disk
H
Hongze Cheng 已提交
431
  pCommitter->toLastOnly = 0;
H
Hongze Cheng 已提交
432
  SDataFReader *pReader = pCommitter->dReader.pReader;
H
Hongze Cheng 已提交
433
  if (pReader) {
H
Hongze Cheng 已提交
434
    if (pReader->pSet->nSstF >= pCommitter->maxLast) {
H
Hongze Cheng 已提交
435
      int8_t iIter = 0;
H
Hongze Cheng 已提交
436
      for (int32_t iSst = 0; iSst < pReader->pSet->nSstF; iSst++) {
H
Hongze Cheng 已提交
437 438
        pIter = &pCommitter->aDataIter[iIter];
        pIter->type = LAST_DATA_ITER;
H
Hongze Cheng 已提交
439
        pIter->iSst = iSst;
H
Hongze Cheng 已提交
440

H
Hongze Cheng 已提交
441
        code = tsdbReadSstBlk(pCommitter->dReader.pReader, iSst, pIter->aSstBlk);
H
Hongze Cheng 已提交
442 443
        if (code) goto _err;

H
Hongze Cheng 已提交
444
        if (taosArrayGetSize(pIter->aSstBlk) == 0) continue;
H
Hongze Cheng 已提交
445

H
Hongze Cheng 已提交
446 447 448
        pIter->iSstBlk = 0;
        SSstBlk *pSstBlk = (SSstBlk *)taosArrayGet(pIter->aSstBlk, 0);
        code = tsdbReadSstBlockEx(pCommitter->dReader.pReader, iSst, pSstBlk, &pIter->bData);
H
Hongze Cheng 已提交
449 450 451 452 453 454 455 456 457 458
        if (code) goto _err;

        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 已提交
459
    } else {
H
Hongze Cheng 已提交
460 461
      for (int32_t iSst = 0; iSst < pReader->pSet->nSstF; iSst++) {
        SSstFile *pSstFile = pReader->pSet->aSstF[iSst];
H
Hongze Cheng 已提交
462
        if (pSstFile->size > pSstFile->offset) {
H
Hongze Cheng 已提交
463 464 465 466
          pCommitter->toLastOnly = 1;
          break;
        }
      }
H
Hongze Cheng 已提交
467
    }
H
Hongze Cheng 已提交
468 469 470 471 472 473 474 475 476 477 478
  }

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

  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
479 480 481 482
static int32_t tsdbCommitFileDataStart(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SDFileSet *pRSet = NULL;
H
Hongze Cheng 已提交
483

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

H
Hongze Cheng 已提交
490
  // Reader
H
Hongze Cheng 已提交
491 492
  SDFileSet tDFileSet = {.fid = pCommitter->commitFid};
  pRSet = (SDFileSet *)taosArraySearch(pCommitter->fs.aDFileSet, &tDFileSet, tDFileSetCmprFn, TD_EQ);
H
Hongze Cheng 已提交
493
  if (pRSet) {
H
Hongze Cheng 已提交
494
    code = tsdbDataFReaderOpen(&pCommitter->dReader.pReader, pTsdb, pRSet);
H
Hongze Cheng 已提交
495 496
    if (code) goto _err;

H
Hongze Cheng 已提交
497
    // data
H
Hongze Cheng 已提交
498
    code = tsdbReadBlockIdx(pCommitter->dReader.pReader, pCommitter->dReader.aBlockIdx);
H
Hongze Cheng 已提交
499
    if (code) goto _err;
H
Hongze Cheng 已提交
500

H
Hongze Cheng 已提交
501
    pCommitter->dReader.iBlockIdx = 0;
H
Hongze Cheng 已提交
502 503
    if (taosArrayGetSize(pCommitter->dReader.aBlockIdx) > 0) {
      pCommitter->dReader.pBlockIdx = (SBlockIdx *)taosArrayGet(pCommitter->dReader.aBlockIdx, 0);
H
Hongze Cheng 已提交
504
      code = tsdbReadBlock(pCommitter->dReader.pReader, pCommitter->dReader.pBlockIdx, &pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
505 506 507 508
      if (code) goto _err;
    } else {
      pCommitter->dReader.pBlockIdx = NULL;
    }
H
Hongze Cheng 已提交
509
    tBlockDataReset(&pCommitter->dReader.bData);
H
Hongze Cheng 已提交
510
  } else {
H
Hongze Cheng 已提交
511
    pCommitter->dReader.pBlockIdx = NULL;
H
Hongze Cheng 已提交
512
  }
H
Hongze Cheng 已提交
513

H
Hongze Cheng 已提交
514
  // Writer
H
Hongze Cheng 已提交
515 516 517
  SHeadFile fHead = {.commitID = pCommitter->commitID};
  SDataFile fData = {.commitID = pCommitter->commitID};
  SSmaFile  fSma = {.commitID = pCommitter->commitID};
H
Hongze Cheng 已提交
518
  SSstFile  fSst = {.commitID = pCommitter->commitID};
H
Hongze Cheng 已提交
519
  SDFileSet wSet = {.fid = pCommitter->commitFid, .pHeadF = &fHead, .pDataF = &fData, .pSmaF = &fSma};
H
Hongze Cheng 已提交
520
  if (pRSet) {
H
Hongze Cheng 已提交
521
    ASSERT(pRSet->nSstF <= pCommitter->maxLast);
H
Hongze Cheng 已提交
522 523
    fData = *pRSet->pDataF;
    fSma = *pRSet->pSmaF;
H
Hongze Cheng 已提交
524
    wSet.diskId = pRSet->diskId;
H
Hongze Cheng 已提交
525
    if (pRSet->nSstF < pCommitter->maxLast) {
H
Hongze Cheng 已提交
526 527
      for (int32_t iSst = 0; iSst < pRSet->nSstF; iSst++) {
        wSet.aSstF[iSst] = pRSet->aSstF[iSst];
H
Hongze Cheng 已提交
528
      }
H
Hongze Cheng 已提交
529
      wSet.nSstF = pRSet->nSstF + 1;
H
Hongze Cheng 已提交
530
    } else {
H
Hongze Cheng 已提交
531
      wSet.nSstF = 1;
H
Hongze Cheng 已提交
532
    }
H
Hongze Cheng 已提交
533
  } else {
H
Hongze Cheng 已提交
534
    SDiskID did = {0};
535
    tfsAllocDisk(pTsdb->pVnode->pTfs, 0, &did);
H
Hongze Cheng 已提交
536
    tfsMkdirRecurAt(pTsdb->pVnode->pTfs, pTsdb->path, did);
537
    wSet.diskId = did;
H
Hongze Cheng 已提交
538
    wSet.nSstF = 1;
H
Hongze Cheng 已提交
539
  }
H
Hongze Cheng 已提交
540
  wSet.aSstF[wSet.nSstF - 1] = &fSst;
H
Hongze Cheng 已提交
541
  code = tsdbDataFWriterOpen(&pCommitter->dWriter.pWriter, pTsdb, &wSet);
H
Hongze Cheng 已提交
542
  if (code) goto _err;
H
Hongze Cheng 已提交
543

H
Hongze Cheng 已提交
544
  taosArrayClear(pCommitter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
545
  taosArrayClear(pCommitter->dWriter.aSstBlk);
H
Hongze Cheng 已提交
546 547 548 549
  tMapDataReset(&pCommitter->dWriter.mBlock);
  tBlockDataReset(&pCommitter->dWriter.bData);
  tBlockDataReset(&pCommitter->dWriter.bDatal);

H
Hongze Cheng 已提交
550 551 552 553
  // open iter
  code = tsdbOpenCommitIter(pCommitter);
  if (code) goto _err;

H
Hongze Cheng 已提交
554
_exit:
H
Hongze Cheng 已提交
555 556 557
  return code;

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

H
Hongze Cheng 已提交
562
static int32_t tsdbCommitDataBlock(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
563 564
  int32_t     code = 0;
  SBlockData *pBlockData = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
565
  SDataBlk    block;
H
Hongze Cheng 已提交
566

H
Hongze Cheng 已提交
567
  ASSERT(pBlockData->nRow > 0);
H
Hongze Cheng 已提交
568

H
Hongze Cheng 已提交
569
  tDataBlkReset(&block);
H
Hongze Cheng 已提交
570

H
Hongze Cheng 已提交
571
  // info
H
Hongze Cheng 已提交
572 573 574
  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 已提交
575

H
Hongze Cheng 已提交
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
    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 已提交
595 596
  block.nSubBlock++;
  code = tsdbWriteBlockData(pCommitter->dWriter.pWriter, pBlockData, &block.aSubBlock[block.nSubBlock - 1],
H
Hongze Cheng 已提交
597
                            ((block.nSubBlock == 1) && !block.hasDup) ? &block.smaInfo : NULL, pCommitter->cmprAlg, 0);
H
Hongze Cheng 已提交
598 599
  if (code) goto _err;

H
Hongze Cheng 已提交
600 601
  // put SDataBlk
  code = tMapDataPutItem(&pCommitter->dWriter.mBlock, &block, tPutDataBlk);
H
Hongze Cheng 已提交
602
  if (code) goto _err;
H
Hongze Cheng 已提交
603

H
Hongze Cheng 已提交
604
  // clear
H
Hongze Cheng 已提交
605
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
606

H
Hongze Cheng 已提交
607 608 609 610
  return code;

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

static int32_t tsdbCommitLastBlock(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
615
  int32_t     code = 0;
H
Hongze Cheng 已提交
616
  SSstBlk     blockL;
H
Hongze Cheng 已提交
617 618 619 620
  SBlockData *pBlockData = &pCommitter->dWriter.bDatal;

  ASSERT(pBlockData->nRow > 0);

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

H
Hongze Cheng 已提交
637
  // write
H
Hongze Cheng 已提交
638
  code = tsdbWriteBlockData(pCommitter->dWriter.pWriter, pBlockData, &blockL.bInfo, NULL, pCommitter->cmprAlg, 1);
H
Hongze Cheng 已提交
639
  if (code) goto _err;
H
Hongze Cheng 已提交
640

H
Hongze Cheng 已提交
641 642
  // push SSstBlk
  if (taosArrayPush(pCommitter->dWriter.aSstBlk, &blockL) == NULL) {
H
Hongze Cheng 已提交
643
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
644
    goto _err;
H
Hongze Cheng 已提交
645 646
  }

H
Hongze Cheng 已提交
647
  // clear
H
Hongze Cheng 已提交
648
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
649

H
Hongze Cheng 已提交
650 651 652 653
  return code;

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

H
Hongze Cheng 已提交
657 658 659
static int32_t tsdbCommitFileDataEnd(SCommitter *pCommitter) {
  int32_t code = 0;

H
Hongze Cheng 已提交
660
  // write aBlockIdx
H
Hongze Cheng 已提交
661
  code = tsdbWriteBlockIdx(pCommitter->dWriter.pWriter, pCommitter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
662 663
  if (code) goto _err;

H
Hongze Cheng 已提交
664 665
  // write aSstBlk
  code = tsdbWriteSstBlk(pCommitter->dWriter.pWriter, pCommitter->dWriter.aSstBlk);
H
Hongze Cheng 已提交
666 667
  if (code) goto _err;

H
Hongze Cheng 已提交
668
  // update file header
H
Hongze Cheng 已提交
669
  code = tsdbUpdateDFileSetHeader(pCommitter->dWriter.pWriter);
H
Hongze Cheng 已提交
670 671 672
  if (code) goto _err;

  // upsert SDFileSet
H
Hongze Cheng 已提交
673
  code = tsdbFSUpsertFSet(&pCommitter->fs, &pCommitter->dWriter.pWriter->wSet);
H
Hongze Cheng 已提交
674 675 676
  if (code) goto _err;

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

H
Hongze Cheng 已提交
680 681
  if (pCommitter->dReader.pReader) {
    code = tsdbDataFReaderClose(&pCommitter->dReader.pReader);
H
Hongze Cheng 已提交
682
    if (code) goto _err;
H
Hongze Cheng 已提交
683 684 685 686 687 688
  }

_exit:
  return code;

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

H
Hongze Cheng 已提交
693 694 695
static int32_t tsdbMoveCommitData(SCommitter *pCommitter, TABLEID toTable) {
  int32_t code = 0;

H
Hongze Cheng 已提交
696
  while (pCommitter->dReader.pBlockIdx && tTABLEIDCmprFn(pCommitter->dReader.pBlockIdx, &toTable) < 0) {
H
Hongze Cheng 已提交
697
    SBlockIdx blockIdx = *pCommitter->dReader.pBlockIdx;
H
Hongze Cheng 已提交
698
    code = tsdbWriteBlock(pCommitter->dWriter.pWriter, &pCommitter->dReader.mBlock, &blockIdx);
H
Hongze Cheng 已提交
699 700 701 702 703 704 705
    if (code) goto _err;

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

H
Hongze Cheng 已提交
706 707
    code = tsdbCommitterNextTableData(pCommitter);
    if (code) goto _err;
H
Hongze Cheng 已提交
708 709 710 711 712 713 714 715 716
  }

  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 已提交
717
static int32_t tsdbCommitFileDataImpl(SCommitter *pCommitter);
H
Hongze Cheng 已提交
718
static int32_t tsdbCommitFileData(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
719 720 721
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
722 723 724 725 726

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

H
Hongze Cheng 已提交
727 728 729
  // impl
  code = tsdbCommitFileDataImpl(pCommitter);
  if (code) goto _err;
H
Hongze Cheng 已提交
730

H
Hongze Cheng 已提交
731 732
  // commit file data end
  code = tsdbCommitFileDataEnd(pCommitter);
H
Hongze Cheng 已提交
733
  if (code) goto _err;
H
Hongze Cheng 已提交
734 735 736 737

  return code;

_err:
S
Shengliang Guan 已提交
738
  tsdbError("vgId:%d, commit file data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
739 740
  tsdbDataFReaderClose(&pCommitter->dReader.pReader);
  tsdbDataFWriterClose(&pCommitter->dWriter.pWriter, 0);
H
Hongze Cheng 已提交
741 742 743
  return code;
}

H
Hongze Cheng 已提交
744 745
// ----------------------------------------------------------------------------
static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitter *pCommitter) {
H
Hongze Cheng 已提交
746
  int32_t code = 0;
H
Hongze Cheng 已提交
747

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

H
more  
Hongze Cheng 已提交
751
  taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
752 753
  pTsdb->imem = pTsdb->mem;
  pTsdb->mem = NULL;
H
more  
Hongze Cheng 已提交
754
  taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
755

H
Hongze Cheng 已提交
756
  pCommitter->pTsdb = pTsdb;
H
Hongze Cheng 已提交
757
  pCommitter->commitID = pTsdb->pVnode->state.commitID;
H
Hongze Cheng 已提交
758 759 760 761
  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 已提交
762
  pCommitter->cmprAlg = pTsdb->pVnode->config.tsdbCfg.compression;
H
Hongze Cheng 已提交
763
  pCommitter->maxLast = TSDB_DEFAULT_LAST_FILE;  // TODO: make it as a config
H
Hongze Cheng 已提交
764 765 766 767 768
  pCommitter->aTbDataP = tsdbMemTableGetTbDataArray(pTsdb->imem);
  if (pCommitter->aTbDataP == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
769
  code = tsdbFSCopy(pTsdb, &pCommitter->fs);
H
Hongze Cheng 已提交
770 771 772 773 774
  if (code) goto _err;

  return code;

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

H
Hongze Cheng 已提交
779 780 781
static int32_t tsdbCommitDataStart(SCommitter *pCommitter) {
  int32_t code = 0;

H
Hongze Cheng 已提交
782
  // reader
H
Hongze Cheng 已提交
783 784
  pCommitter->dReader.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pCommitter->dReader.aBlockIdx == NULL) {
H
Hongze Cheng 已提交
785 786 787 788
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
789
  code = tBlockDataCreate(&pCommitter->dReader.bData);
H
Hongze Cheng 已提交
790 791
  if (code) goto _exit;

H
Hongze Cheng 已提交
792
  // merger
H
Hongze Cheng 已提交
793 794 795 796
  for (int32_t iSst = 0; iSst < TSDB_MAX_LAST_FILE; iSst++) {
    SDataIter *pIter = &pCommitter->aDataIter[iSst];
    pIter->aSstBlk = taosArrayInit(0, sizeof(SSstBlk));
    if (pIter->aSstBlk == NULL) {
H
Hongze Cheng 已提交
797 798 799 800 801 802 803 804 805
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }

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

  // writer
H
Hongze Cheng 已提交
806 807 808 809 810 811
  pCommitter->dWriter.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pCommitter->dWriter.aBlockIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
812 813
  pCommitter->dWriter.aSstBlk = taosArrayInit(0, sizeof(SSstBlk));
  if (pCommitter->dWriter.aSstBlk == NULL) {
H
Hongze Cheng 已提交
814 815 816 817
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
818
  code = tBlockDataCreate(&pCommitter->dWriter.bData);
H
Hongze Cheng 已提交
819
  if (code) goto _exit;
H
Hongze Cheng 已提交
820

H
Hongze Cheng 已提交
821
  code = tBlockDataCreate(&pCommitter->dWriter.bDatal);
H
Hongze Cheng 已提交
822 823
  if (code) goto _exit;

H
Hongze Cheng 已提交
824 825 826 827 828
_exit:
  return code;
}

static void tsdbCommitDataEnd(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
829
  // reader
H
Hongze Cheng 已提交
830 831
  taosArrayDestroy(pCommitter->dReader.aBlockIdx);
  tMapDataClear(&pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
832
  tBlockDataDestroy(&pCommitter->dReader.bData, 1);
H
Hongze Cheng 已提交
833

H
Hongze Cheng 已提交
834
  // merger
H
Hongze Cheng 已提交
835 836 837
  for (int32_t iSst = 0; iSst < TSDB_MAX_LAST_FILE; iSst++) {
    SDataIter *pIter = &pCommitter->aDataIter[iSst];
    taosArrayDestroy(pIter->aSstBlk);
H
Hongze Cheng 已提交
838 839 840 841
    tBlockDataDestroy(&pIter->bData, 1);
  }

  // writer
H
Hongze Cheng 已提交
842
  taosArrayDestroy(pCommitter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
843
  taosArrayDestroy(pCommitter->dWriter.aSstBlk);
H
Hongze Cheng 已提交
844
  tMapDataClear(&pCommitter->dWriter.mBlock);
H
Hongze Cheng 已提交
845 846
  tBlockDataDestroy(&pCommitter->dWriter.bData, 1);
  tBlockDataDestroy(&pCommitter->dWriter.bDatal, 1);
H
Hongze Cheng 已提交
847 848
  tTSchemaDestroy(pCommitter->skmTable.pTSchema);
  tTSchemaDestroy(pCommitter->skmRow.pTSchema);
H
Hongze Cheng 已提交
849 850
}

H
Hongze Cheng 已提交
851 852 853 854
static int32_t tsdbCommitData(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
855

H
Hongze Cheng 已提交
856
  // check
H
Hongze Cheng 已提交
857
  if (pMemTable->nRow == 0) goto _exit;
H
Hongze Cheng 已提交
858

H
Hongze Cheng 已提交
859 860
  // start ====================
  code = tsdbCommitDataStart(pCommitter);
H
Hongze Cheng 已提交
861
  if (code) goto _err;
H
Hongze Cheng 已提交
862 863 864

  // impl ====================
  pCommitter->nextKey = pMemTable->minKey;
H
Hongze Cheng 已提交
865 866
  while (pCommitter->nextKey < TSKEY_MAX) {
    code = tsdbCommitFileData(pCommitter);
H
Hongze Cheng 已提交
867
    if (code) goto _err;
H
Hongze Cheng 已提交
868
  }
H
Hongze Cheng 已提交
869

H
Hongze Cheng 已提交
870 871 872
  // end ====================
  tsdbCommitDataEnd(pCommitter);

H
Hongze Cheng 已提交
873
_exit:
H
Hongze Cheng 已提交
874
  tsdbInfo("vgId:%d, commit data done, nRow:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nRow);
H
Hongze Cheng 已提交
875
  return code;
H
Hongze Cheng 已提交
876

H
Hongze Cheng 已提交
877
_err:
H
Hongze Cheng 已提交
878
  tsdbCommitDataEnd(pCommitter);
S
Shengliang Guan 已提交
879
  tsdbError("vgId:%d, commit data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
880 881
  return code;
}
H
Hongze Cheng 已提交
882

H
Hongze Cheng 已提交
883 884 885 886
static int32_t tsdbCommitDel(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
887

H
Hongze Cheng 已提交
888 889
  if (pMemTable->nDel == 0) {
    goto _exit;
H
Hongze Cheng 已提交
890
  }
H
Hongze Cheng 已提交
891

H
Hongze Cheng 已提交
892 893 894 895 896
  // start
  code = tsdbCommitDelStart(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
897

H
Hongze Cheng 已提交
898
  // impl
H
Hongze Cheng 已提交
899 900 901
  int32_t  iDelIdx = 0;
  int32_t  nDelIdx = taosArrayGetSize(pCommitter->aDelIdx);
  int32_t  iTbData = 0;
H
Hongze Cheng 已提交
902
  int32_t  nTbData = taosArrayGetSize(pCommitter->aTbDataP);
H
Hongze Cheng 已提交
903 904 905 906 907
  STbData *pTbData;
  SDelIdx *pDelIdx;

  ASSERT(nTbData > 0);

H
Hongze Cheng 已提交
908
  pTbData = (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData);
H
Hongze Cheng 已提交
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
  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 已提交
934
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData) : NULL;
H
Hongze Cheng 已提交
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
    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 已提交
950
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData) : NULL;
H
Hongze Cheng 已提交
951 952 953
    iDelIdx++;
    pDelIdx = (iDelIdx < nDelIdx) ? (SDelIdx *)taosArrayGet(pCommitter->aDelIdx, iDelIdx) : NULL;
    continue;
H
Hongze Cheng 已提交
954
  }
H
Hongze Cheng 已提交
955

H
Hongze Cheng 已提交
956 957 958 959 960
  // end
  code = tsdbCommitDelEnd(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
961

H
Hongze Cheng 已提交
962
_exit:
S
Shengliang Guan 已提交
963
  tsdbDebug("vgId:%d, commit del done, nDel:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nDel);
H
Hongze Cheng 已提交
964 965 966
  return code;

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

H
Hongze Cheng 已提交
971
static int32_t tsdbEndCommit(SCommitter *pCommitter, int32_t eno) {
H
Hongze Cheng 已提交
972 973 974 975
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

H
Hongze Cheng 已提交
976
  ASSERT(eno == 0);
H
Hongze Cheng 已提交
977 978 979

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

H
Hongze Cheng 已提交
981
  // lock
H
Hongze Cheng 已提交
982
  taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
983 984 985 986 987 988 989 990

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

H
Hongze Cheng 已提交
991
  pTsdb->imem = NULL;
H
Hongze Cheng 已提交
992 993

  // unlock
H
Hongze Cheng 已提交
994 995 996
  taosThreadRwlockUnlock(&pTsdb->rwLock);

  tsdbUnrefMemTable(pMemTable);
H
Hongze Cheng 已提交
997
  tsdbFSDestroy(&pCommitter->fs);
H
Hongze Cheng 已提交
998
  taosArrayDestroy(pCommitter->aTbDataP);
H
Hongze Cheng 已提交
999

H
Hongze Cheng 已提交
1000 1001 1002 1003
  // if (pCommitter->toMerge) {
  //   code = tsdbMerge(pTsdb);
  //   if (code) goto _err;
  // }
H
Hongze Cheng 已提交
1004

S
Shengliang Guan 已提交
1005
  tsdbInfo("vgId:%d, tsdb end commit", TD_VID(pTsdb->pVnode));
H
Hongze Cheng 已提交
1006 1007 1008
  return code;

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

H
Hongze Cheng 已提交
1013
// ================================================================================
H
Hongze Cheng 已提交
1014

H
Hongze Cheng 已提交
1015 1016
static FORCE_INLINE SRowInfo *tsdbGetCommitRow(SCommitter *pCommitter) {
  return (pCommitter->pIter) ? &pCommitter->pIter->r : NULL;
H
Hongze Cheng 已提交
1017 1018
}

H
Hongze Cheng 已提交
1019
static int32_t tsdbNextCommitRow(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1020 1021 1022 1023
  int32_t code = 0;

  if (pCommitter->pIter) {
    SDataIter *pIter = pCommitter->pIter;
H
Hongze Cheng 已提交
1024
    if (pCommitter->pIter->type == MEMORY_DATA_ITER) {  // memory
H
Hongze Cheng 已提交
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
      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 已提交
1041 1042
        if (pIter->iTbDataP < taosArrayGetSize(pCommitter->aTbDataP)) {
          STbData *pTbData = (STbData *)taosArrayGetP(pCommitter->aTbDataP, pIter->iTbDataP);
H
Hongze Cheng 已提交
1043 1044 1045 1046 1047 1048 1049 1050 1051
          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 已提交
1052
    } else if (pCommitter->pIter->type == LAST_DATA_ITER) {  // last file
H
Hongze Cheng 已提交
1053 1054 1055 1056 1057
      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 已提交
1058 1059 1060
        pIter->iSstBlk++;
        if (pIter->iSstBlk < taosArrayGetSize(pIter->aSstBlk)) {
          SSstBlk *pSstBlk = (SSstBlk *)taosArrayGet(pIter->aSstBlk, pIter->iSstBlk);
H
Hongze Cheng 已提交
1061

H
Hongze Cheng 已提交
1062
          code = tsdbReadSstBlockEx(pCommitter->dReader.pReader, pIter->iSst, pSstBlk, &pIter->bData);
H
Hongze Cheng 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
          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 已提交
1101
static int32_t tsdbCommitAheadBlock(SCommitter *pCommitter, SDataBlk *pDataBlk) {
H
Hongze Cheng 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
  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);
H
Hongze Cheng 已提交
1125
        if (tsdbKeyCmprFn(&tKey, &pDataBlk->minKey) >= 0) pRowInfo = NULL;
H
Hongze Cheng 已提交
1126 1127 1128
      }
    }

H
Hongze Cheng 已提交
1129 1130
    if (pBlockData->nRow >= pCommitter->maxRow) {
      code = tsdbCommitDataBlock(pCommitter);
H
Hongze Cheng 已提交
1131 1132 1133 1134 1135
      if (code) goto _err;
    }
  }

  if (pBlockData->nRow) {
H
Hongze Cheng 已提交
1136
    code = tsdbCommitDataBlock(pCommitter);
H
Hongze Cheng 已提交
1137 1138 1139 1140 1141 1142 1143
    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 已提交
1144 1145 1146
  return code;
}

H
Hongze Cheng 已提交
1147
static int32_t tsdbCommitMergeBlock(SCommitter *pCommitter, SDataBlk *pDataBlk) {
H
Hongze Cheng 已提交
1148 1149 1150 1151 1152 1153
  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;

H
Hongze Cheng 已提交
1154
  code = tsdbReadDataBlock(pCommitter->dReader.pReader, pDataBlk, pBDataR);
H
Hongze Cheng 已提交
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
  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);
H
Hongze Cheng 已提交
1191
          if (tsdbKeyCmprFn(&tKey, &pDataBlk->maxKey) > 0) pRowInfo = NULL;
H
Hongze Cheng 已提交
1192 1193 1194 1195 1196 1197
        }
      }
    } else {
      ASSERT(0);
    }

H
Hongze Cheng 已提交
1198 1199
    if (pBDataW->nRow >= pCommitter->maxRow) {
      code = tsdbCommitDataBlock(pCommitter);
H
Hongze Cheng 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
      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;
    }

H
Hongze Cheng 已提交
1215 1216
    if (pBDataW->nRow >= pCommitter->maxRow) {
      code = tsdbCommitDataBlock(pCommitter);
H
Hongze Cheng 已提交
1217 1218 1219 1220 1221
      if (code) goto _err;
    }
  }

  if (pBDataW->nRow) {
H
Hongze Cheng 已提交
1222
    code = tsdbCommitDataBlock(pCommitter);
H
Hongze Cheng 已提交
1223 1224 1225 1226 1227 1228 1229
    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 已提交
1230 1231 1232
  return code;
}

H
Hongze Cheng 已提交
1233 1234 1235 1236 1237 1238 1239
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;
H
Hongze Cheng 已提交
1240 1241
    SDataBlk  block;
    SDataBlk *pDataBlk = &block;
H
Hongze Cheng 已提交
1242 1243 1244 1245
    SRowInfo *pRowInfo = tsdbGetCommitRow(pCommitter);

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

H
Hongze Cheng 已提交
1246 1247 1248
    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 已提交
1249
      int32_t  c = tDataBlkCmprFn(pDataBlk, &tBlock);
H
Hongze Cheng 已提交
1250 1251

      if (c < 0) {
H
Hongze Cheng 已提交
1252
        code = tMapDataPutItem(&pCommitter->dWriter.mBlock, pDataBlk, tPutDataBlk);
H
Hongze Cheng 已提交
1253 1254 1255 1256
        if (code) goto _err;

        iBlock++;
        if (iBlock < pCommitter->dReader.mBlock.nItem) {
H
Hongze Cheng 已提交
1257
          tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pDataBlk, tGetDataBlk);
H
Hongze Cheng 已提交
1258
        } else {
H
Hongze Cheng 已提交
1259
          pDataBlk = NULL;
H
Hongze Cheng 已提交
1260 1261
        }
      } else if (c > 0) {
H
Hongze Cheng 已提交
1262
        code = tsdbCommitAheadBlock(pCommitter, pDataBlk);
H
Hongze Cheng 已提交
1263 1264 1265 1266
        if (code) goto _err;

        pRowInfo = tsdbGetCommitRow(pCommitter);
        if (pRowInfo && (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid)) pRowInfo = NULL;
H
Hongze Cheng 已提交
1267
      } else {
H
Hongze Cheng 已提交
1268
        code = tsdbCommitMergeBlock(pCommitter, pDataBlk);
H
Hongze Cheng 已提交
1269 1270 1271 1272
        if (code) goto _err;

        iBlock++;
        if (iBlock < pCommitter->dReader.mBlock.nItem) {
H
Hongze Cheng 已提交
1273
          tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pDataBlk, tGetDataBlk);
H
Hongze Cheng 已提交
1274
        } else {
H
Hongze Cheng 已提交
1275
          pDataBlk = NULL;
H
Hongze Cheng 已提交
1276 1277 1278
        }
        pRowInfo = tsdbGetCommitRow(pCommitter);
        if (pRowInfo && (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid)) pRowInfo = NULL;
H
Hongze Cheng 已提交
1279 1280 1281
      }
    }

H
Hongze Cheng 已提交
1282 1283
    while (pDataBlk) {
      code = tMapDataPutItem(&pCommitter->dWriter.mBlock, pDataBlk, tPutDataBlk);
H
Hongze Cheng 已提交
1284 1285 1286 1287
      if (code) goto _err;

      iBlock++;
      if (iBlock < pCommitter->dReader.mBlock.nItem) {
H
Hongze Cheng 已提交
1288
        tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pDataBlk, tGetDataBlk);
H
Hongze Cheng 已提交
1289
      } else {
H
Hongze Cheng 已提交
1290
        pDataBlk = NULL;
H
Hongze Cheng 已提交
1291 1292
      }
    }
H
Hongze Cheng 已提交
1293 1294 1295

    code = tsdbCommitterNextTableData(pCommitter);
    if (code) goto _err;
H
Hongze Cheng 已提交
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
  }

_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 已提交
1306
static int32_t tsdbInitLastBlockIfNeed(SCommitter *pCommitter, TABLEID id) {
H
Hongze Cheng 已提交
1307 1308 1309 1310
  int32_t code = 0;

  SBlockData *pBDatal = &pCommitter->dWriter.bDatal;
  if (pBDatal->suid || pBDatal->uid) {
H
Hongze Cheng 已提交
1311
    if (pBDatal->suid != id.suid || id.uid == 0) {
H
Hongze Cheng 已提交
1312 1313
      if (pBDatal->nRow) {
        code = tsdbCommitLastBlock(pCommitter);
H
Hongze Cheng 已提交
1314
        if (code) goto _exit;
H
Hongze Cheng 已提交
1315 1316 1317 1318 1319 1320
      }
      tBlockDataReset(pBDatal);
    }
  }

  if (!pBDatal->suid && !pBDatal->uid) {
H
Hongze Cheng 已提交
1321 1322
    ASSERT(pCommitter->skmTable.suid == id.suid);
    ASSERT(pCommitter->skmTable.uid == id.uid);
H
Hongze Cheng 已提交
1323
    code = tBlockDataInit(pBDatal, id.suid, id.suid ? 0 : id.uid, pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
1324
    if (code) goto _exit;
H
Hongze Cheng 已提交
1325 1326
  }

H
Hongze Cheng 已提交
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
_exit:
  return code;
}

static int32_t tsdbAppendLastBlock(SCommitter *pCommitter) {
  int32_t code = 0;

  SBlockData *pBData = &pCommitter->dWriter.bData;
  SBlockData *pBDatal = &pCommitter->dWriter.bDatal;

  TABLEID id = {.suid = pBData->suid, .uid = pBData->uid};
  code = tsdbInitLastBlockIfNeed(pCommitter, id);
  if (code) goto _err;

H
Hongze Cheng 已提交
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
  for (int32_t iRow = 0; iRow < pBData->nRow; iRow++) {
    TSDBROW row = tsdbRowFromBlockData(pBData, iRow);
    code = tBlockDataAppendRow(pBDatal, &row, NULL, pBData->uid);
    if (code) goto _err;

    if (pBDatal->nRow >= pCommitter->maxRow) {
      code = tsdbCommitLastBlock(pCommitter);
      if (code) goto _err;
    }
  }

  return code;

_err:
  return code;
}

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

H
Hongze Cheng 已提交
1361
  SRowInfo *pRowInfo = tsdbGetCommitRow(pCommitter);
H
Hongze Cheng 已提交
1362 1363 1364 1365
  if (pRowInfo && (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid)) {
    pRowInfo = NULL;
  }

H
Hongze Cheng 已提交
1366
  if (pRowInfo == NULL) goto _exit;
H
Hongze Cheng 已提交
1367

H
Hongze Cheng 已提交
1368
  SBlockData *pBData;
H
Hongze Cheng 已提交
1369
  if (pCommitter->toLastOnly) {
H
Hongze Cheng 已提交
1370
    pBData = &pCommitter->dWriter.bDatal;
H
Hongze Cheng 已提交
1371 1372
    code = tsdbInitLastBlockIfNeed(pCommitter, id);
    if (code) goto _err;
H
Hongze Cheng 已提交
1373
  } else {
H
Hongze Cheng 已提交
1374
    pBData = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
1375
    ASSERT(pBData->nRow == 0);
H
Hongze Cheng 已提交
1376
  }
H
Hongze Cheng 已提交
1377

H
Hongze Cheng 已提交
1378 1379 1380 1381
  while (pRowInfo) {
    STSchema *pTSchema = NULL;
    if (pRowInfo->row.type == 0) {
      code = tsdbCommitterUpdateRowSchema(pCommitter, id.suid, id.uid, TSDBROW_SVERSION(&pRowInfo->row));
H
Hongze Cheng 已提交
1382
      if (code) goto _err;
H
Hongze Cheng 已提交
1383 1384
      pTSchema = pCommitter->skmRow.pTSchema;
    }
H
Hongze Cheng 已提交
1385

H
Hongze Cheng 已提交
1386 1387
    code = tBlockDataAppendRow(pBData, &pRowInfo->row, pTSchema, id.uid);
    if (code) goto _err;
H
Hongze Cheng 已提交
1388

H
Hongze Cheng 已提交
1389 1390
    code = tsdbNextCommitRow(pCommitter);
    if (code) goto _err;
H
Hongze Cheng 已提交
1391

H
Hongze Cheng 已提交
1392 1393 1394
    pRowInfo = tsdbGetCommitRow(pCommitter);
    if (pRowInfo && (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid)) {
      pRowInfo = NULL;
H
Hongze Cheng 已提交
1395 1396
    }

H
Hongze Cheng 已提交
1397 1398 1399
    if (pBData->nRow >= pCommitter->maxRow) {
      if (pCommitter->toLastOnly) {
        code = tsdbCommitLastBlock(pCommitter);
H
Hongze Cheng 已提交
1400 1401
        if (code) goto _err;
      } else {
H
Hongze Cheng 已提交
1402
        code = tsdbCommitDataBlock(pCommitter);
H
Hongze Cheng 已提交
1403 1404
        if (code) goto _err;
      }
H
Hongze Cheng 已提交
1405 1406 1407
    }
  }

H
Hongze Cheng 已提交
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
  if (!pCommitter->toLastOnly && pBData->nRow) {
    if (pBData->nRow > pCommitter->minRow) {
      code = tsdbCommitDataBlock(pCommitter);
      if (code) goto _err;
    } else {
      code = tsdbAppendLastBlock(pCommitter);
      if (code) goto _err;
    }
  }

H
Hongze Cheng 已提交
1418
_exit:
H
Hongze Cheng 已提交
1419 1420 1421
  return code;

_err:
H
Hongze Cheng 已提交
1422
  tsdbError("vgId:%d tsdb commit table data failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1423 1424 1425
  return code;
}

H
Hongze Cheng 已提交
1426 1427 1428
static int32_t tsdbCommitFileDataImpl(SCommitter *pCommitter) {
  int32_t code = 0;

H
Hongze Cheng 已提交
1429
  SRowInfo *pRowInfo;
H
Hongze Cheng 已提交
1430
  TABLEID   id = {0};
H
Hongze Cheng 已提交
1431
  while ((pRowInfo = tsdbGetCommitRow(pCommitter)) != NULL) {
H
Hongze Cheng 已提交
1432 1433 1434
    ASSERT(pRowInfo->suid != id.suid || pRowInfo->uid != id.uid);
    id.suid = pRowInfo->suid;
    id.uid = pRowInfo->uid;
H
Hongze Cheng 已提交
1435

H
Hongze Cheng 已提交
1436 1437
    code = tsdbMoveCommitData(pCommitter, id);
    if (code) goto _err;
H
Hongze Cheng 已提交
1438 1439

    // start
H
Hongze Cheng 已提交
1440
    tMapDataReset(&pCommitter->dWriter.mBlock);
H
Hongze Cheng 已提交
1441 1442

    // impl
H
Hongze Cheng 已提交
1443 1444
    code = tsdbCommitterUpdateTableSchema(pCommitter, id.suid, id.uid);
    if (code) goto _err;
H
Hongze Cheng 已提交
1445
    code = tBlockDataInit(&pCommitter->dReader.bData, id.suid, id.uid, pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
1446
    if (code) goto _err;
H
Hongze Cheng 已提交
1447
    code = tBlockDataInit(&pCommitter->dWriter.bData, id.suid, id.uid, pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
1448
    if (code) goto _err;
H
Hongze Cheng 已提交
1449

H
Hongze Cheng 已提交
1450 1451
    /* merge with data in .data file */
    code = tsdbMergeTableData(pCommitter, id);
H
Hongze Cheng 已提交
1452 1453
    if (code) goto _err;

H
Hongze Cheng 已提交
1454
    /* handle remain table data */
H
Hongze Cheng 已提交
1455
    code = tsdbCommitTableData(pCommitter, id);
H
Hongze Cheng 已提交
1456
    if (code) goto _err;
H
Hongze Cheng 已提交
1457

H
Hongze Cheng 已提交
1458
    // end
H
Hongze Cheng 已提交
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
    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 已提交
1469 1470
  }

H
Hongze Cheng 已提交
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
  id.suid = INT64_MAX;
  id.uid = INT64_MAX;
  code = tsdbMoveCommitData(pCommitter, id);
  if (code) goto _err;

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

H
Hongze Cheng 已提交
1481 1482 1483 1484 1485 1486
  return code;

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