tsdbCommit.c 43.1 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
typedef struct {
H
Hongze Cheng 已提交
30
  STsdb *pTsdb;
H
Hongze Cheng 已提交
31
  /* commit data */
H
Hongze Cheng 已提交
32
  int64_t commitID;
H
Hongze Cheng 已提交
33 34
  int32_t minutes;
  int8_t  precision;
H
Hongze Cheng 已提交
35 36
  int32_t minRow;
  int32_t maxRow;
H
Hongze Cheng 已提交
37
  int8_t  cmprAlg;
H
Hongze Cheng 已提交
38
  SArray *aTbDataP;
H
Hongze Cheng 已提交
39
  STsdbFS fs;
H
Hongze Cheng 已提交
40
  // --------------
H
Hongze Cheng 已提交
41
  TSKEY   nextKey;  // reset by each table commit
H
Hongze Cheng 已提交
42 43 44
  int32_t commitFid;
  TSKEY   minKey;
  TSKEY   maxKey;
H
Hongze Cheng 已提交
45
  // commit file data
H
Hongze Cheng 已提交
46 47
  struct {
    SDataFReader *pReader;
H
Hongze Cheng 已提交
48 49 50 51 52 53 54 55 56 57 58
    // data
    SArray    *aBlockIdx;  // SArray<SBlockIdx>
    int32_t    iBlockIdx;
    SBlockIdx *pBlockIdx;
    SMapData   mBlock;  // SMapData<SBlock>
    SBlockData bData;
    // last
    SArray    *aBlockL;  // SArray<SBlockL>
    int32_t    iBlockL;
    SBlockData bDatal;
    int32_t    iRow;
H
Hongze Cheng 已提交
59 60
    SRowInfo  *pRowInfo;
    SRowInfo   rowInfo;
H
Hongze Cheng 已提交
61 62 63 64
  } dReader;
  struct {
    SDataFWriter *pWriter;
    SArray       *aBlockIdx;  // SArray<SBlockIdx>
H
Hongze Cheng 已提交
65
    SArray       *aBlockL;    // SArray<SBlockL>
H
Hongze Cheng 已提交
66 67
    SMapData      mBlock;     // SMapData<SBlock>
    SBlockData    bData;
H
Hongze Cheng 已提交
68
    SBlockData    bDatal;
H
Hongze Cheng 已提交
69 70 71
  } dWriter;
  SSkmInfo skmTable;
  SSkmInfo skmRow;
H
Hongze Cheng 已提交
72
  /* commit del */
H
Hongze Cheng 已提交
73 74
  SDelFReader *pDelFReader;
  SDelFWriter *pDelFWriter;
H
Hongze Cheng 已提交
75 76 77
  SArray      *aDelIdx;   // SArray<SDelIdx>
  SArray      *aDelIdxN;  // SArray<SDelIdx>
  SArray      *aDelData;  // SArray<SDelData>
H
Hongze Cheng 已提交
78
} SCommitter;
H
refact  
Hongze Cheng 已提交
79

H
Hongze Cheng 已提交
80 81 82 83 84
static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitter *pCommitter);
static int32_t tsdbCommitData(SCommitter *pCommitter);
static int32_t tsdbCommitDel(SCommitter *pCommitter);
static int32_t tsdbCommitCache(SCommitter *pCommitter);
static int32_t tsdbEndCommit(SCommitter *pCommitter, int32_t eno);
H
refact  
Hongze Cheng 已提交
85

H
refact  
Hongze Cheng 已提交
86
int32_t tsdbBegin(STsdb *pTsdb) {
H
Hongze Cheng 已提交
87
  int32_t code = 0;
H
Hongze Cheng 已提交
88

89 90
  if (!pTsdb) return code;

H
Hongze Cheng 已提交
91 92
  SMemTable *pMemTable;
  code = tsdbMemTableCreate(pTsdb, &pMemTable);
H
Hongze Cheng 已提交
93
  if (code) goto _err;
H
Hongze Cheng 已提交
94

H
Hongze Cheng 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  // 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 已提交
111 112 113
  return code;

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

H
more  
Hongze Cheng 已提交
118
int32_t tsdbCommit(STsdb *pTsdb) {
119
  if (!pTsdb) return 0;
H
Hongze Cheng 已提交
120

H
more  
Hongze Cheng 已提交
121
  int32_t    code = 0;
H
Hongze Cheng 已提交
122 123 124 125
  SCommitter commith;
  SMemTable *pMemTable = pTsdb->mem;

  // check
H
Hongze Cheng 已提交
126
  if (pMemTable->nRow == 0 && pMemTable->nDel == 0) {
H
Hongze Cheng 已提交
127
    taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
128
    pTsdb->mem = NULL;
H
Hongze Cheng 已提交
129 130 131
    taosThreadRwlockUnlock(&pTsdb->rwLock);

    tsdbUnrefMemTable(pMemTable);
H
Hongze Cheng 已提交
132 133
    goto _exit;
  }
H
refact  
Hongze Cheng 已提交
134

H
more  
Hongze Cheng 已提交
135
  // start commit
H
more  
Hongze Cheng 已提交
136
  code = tsdbStartCommit(pTsdb, &commith);
H
Hongze Cheng 已提交
137
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
138

H
refact  
Hongze Cheng 已提交
139 140
  // commit impl
  code = tsdbCommitData(&commith);
H
Hongze Cheng 已提交
141
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
142 143

  code = tsdbCommitDel(&commith);
H
Hongze Cheng 已提交
144
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
145 146

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

H
Hongze Cheng 已提交
150
_exit:
H
refact  
Hongze Cheng 已提交
151 152 153
  return code;

_err:
H
Hongze Cheng 已提交
154
  tsdbEndCommit(&commith, code);
C
Cary Xu 已提交
155
  tsdbError("vgId:%d, failed to commit since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
refact  
Hongze Cheng 已提交
156 157 158
  return code;
}

H
Hongze Cheng 已提交
159
static int32_t tsdbCommitDelStart(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
160 161 162 163
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

H
Hongze Cheng 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  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 已提交
181

H
Hongze Cheng 已提交
182
  SDelFile *pDelFileR = pCommitter->fs.pDelFile;
H
Hongze Cheng 已提交
183
  if (pDelFileR) {
H
Hongze Cheng 已提交
184
    code = tsdbDelFReaderOpen(&pCommitter->pDelFReader, pDelFileR, pTsdb);
H
Hongze Cheng 已提交
185
    if (code) goto _err;
H
Hongze Cheng 已提交
186

H
Hongze Cheng 已提交
187
    code = tsdbReadDelIdx(pCommitter->pDelFReader, pCommitter->aDelIdx);
H
Hongze Cheng 已提交
188
    if (code) goto _err;
H
Hongze Cheng 已提交
189 190
  }

H
Hongze Cheng 已提交
191
  // prepare new
H
Hongze Cheng 已提交
192 193
  SDelFile wDelFile = {.commitID = pCommitter->commitID, .size = 0, .offset = 0};
  code = tsdbDelFWriterOpen(&pCommitter->pDelFWriter, &wDelFile, pTsdb);
H
Hongze Cheng 已提交
194
  if (code) goto _err;
H
Hongze Cheng 已提交
195 196

_exit:
S
Shengliang Guan 已提交
197
  tsdbDebug("vgId:%d, commit del start", TD_VID(pTsdb->pVnode));
H
Hongze Cheng 已提交
198 199 200
  return code;

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

H
Hongze Cheng 已提交
205
static int32_t tsdbCommitTableDel(SCommitter *pCommitter, STbData *pTbData, SDelIdx *pDelIdx) {
H
Hongze Cheng 已提交
206
  int32_t   code = 0;
H
Hongze Cheng 已提交
207
  SDelData *pDelData;
H
Hongze Cheng 已提交
208 209
  tb_uid_t  suid;
  tb_uid_t  uid;
H
Hongze Cheng 已提交
210 211

  if (pTbData) {
H
Hongze Cheng 已提交
212 213
    suid = pTbData->suid;
    uid = pTbData->uid;
H
Hongze Cheng 已提交
214

H
Hongze Cheng 已提交
215 216 217 218
    if (pTbData->pHead == NULL) {
      pTbData = NULL;
    }
  }
H
Hongze Cheng 已提交
219 220

  if (pDelIdx) {
H
Hongze Cheng 已提交
221 222 223
    suid = pDelIdx->suid;
    uid = pDelIdx->uid;

H
Hongze Cheng 已提交
224
    code = tsdbReadDelData(pCommitter->pDelFReader, pDelIdx, pCommitter->aDelData);
H
Hongze Cheng 已提交
225
    if (code) goto _err;
226 227
  } else {
    taosArrayClear(pCommitter->aDelData);
H
Hongze Cheng 已提交
228 229
  }

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

H
Hongze Cheng 已提交
232
  SDelIdx delIdx = {.suid = suid, .uid = uid};
H
Hongze Cheng 已提交
233 234

  // memory
H
Hongze Cheng 已提交
235 236
  pDelData = pTbData ? pTbData->pHead : NULL;
  for (; pDelData; pDelData = pDelData->pNext) {
H
Hongze Cheng 已提交
237 238 239 240
    if (taosArrayPush(pCommitter->aDelData, pDelData) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
241 242 243
  }

  // write
H
Hongze Cheng 已提交
244
  code = tsdbWriteDelData(pCommitter->pDelFWriter, pCommitter->aDelData, &delIdx);
H
Hongze Cheng 已提交
245 246 247
  if (code) goto _err;

  // put delIdx
248
  if (taosArrayPush(pCommitter->aDelIdxN, &delIdx) == NULL) {
H
Hongze Cheng 已提交
249 250 251
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
252 253 254 255 256

_exit:
  return code;

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

H
Hongze Cheng 已提交
261 262
static int32_t tsdbCommitDelEnd(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
263
  STsdb  *pTsdb = pCommitter->pTsdb;
H
Hongze Cheng 已提交
264

H
Hongze Cheng 已提交
265
  code = tsdbWriteDelIdx(pCommitter->pDelFWriter, pCommitter->aDelIdxN);
H
Hongze Cheng 已提交
266
  if (code) goto _err;
H
Hongze Cheng 已提交
267

H
Hongze Cheng 已提交
268 269 270
  code = tsdbUpdateDelFileHdr(pCommitter->pDelFWriter);
  if (code) goto _err;

H
Hongze Cheng 已提交
271
  code = tsdbFSUpsertDelFile(&pCommitter->fs, &pCommitter->pDelFWriter->fDel);
H
Hongze Cheng 已提交
272
  if (code) goto _err;
H
Hongze Cheng 已提交
273

H
Hongze Cheng 已提交
274
  code = tsdbDelFWriterClose(&pCommitter->pDelFWriter, 1);
H
Hongze Cheng 已提交
275
  if (code) goto _err;
H
Hongze Cheng 已提交
276 277

  if (pCommitter->pDelFReader) {
H
Hongze Cheng 已提交
278
    code = tsdbDelFReaderClose(&pCommitter->pDelFReader);
H
Hongze Cheng 已提交
279 280 281
    if (code) goto _err;
  }

H
Hongze Cheng 已提交
282 283 284 285
  taosArrayDestroy(pCommitter->aDelIdx);
  taosArrayDestroy(pCommitter->aDelData);
  taosArrayDestroy(pCommitter->aDelIdxN);

H
Hongze Cheng 已提交
286 287 288
  return code;

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

H
Hongze Cheng 已提交
293
static int32_t tsdbCommitterUpdateTableSchema(SCommitter *pCommitter, int64_t suid, int64_t uid) {
H
Hongze Cheng 已提交
294 295
  int32_t code = 0;

H
Hongze Cheng 已提交
296 297 298 299
  if (suid) {
    if (pCommitter->skmTable.suid == suid) goto _exit;
  } else {
    if (pCommitter->skmTable.uid == uid) goto _exit;
H
Hongze Cheng 已提交
300 301 302 303 304
  }

  pCommitter->skmTable.suid = suid;
  pCommitter->skmTable.uid = uid;
  tTSchemaDestroy(pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
305
  code = metaGetTbTSchemaEx(pCommitter->pTsdb->pVnode->pMeta, suid, uid, -1, &pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
  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 已提交
337
static int32_t tsdbCommitterNextLastRow(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
338 339 340 341 342
  int32_t code = 0;

  ASSERT(pCommitter->dReader.pReader);
  ASSERT(pCommitter->dReader.pRowInfo);

H
Hongze Cheng 已提交
343
  SBlockData *pBlockDatal = &pCommitter->dReader.bDatal;
H
Hongze Cheng 已提交
344
  pCommitter->dReader.iRow++;
H
Hongze Cheng 已提交
345
  if (pCommitter->dReader.iRow < pBlockDatal->nRow) {
H
Hongze Cheng 已提交
346 347 348
    if (pBlockDatal->uid) {
      pCommitter->dReader.pRowInfo->uid = pBlockDatal->uid;
    } else {
H
Hongze Cheng 已提交
349 350 351
      pCommitter->dReader.pRowInfo->uid = pBlockDatal->aUid[pCommitter->dReader.iRow];
    }
    pCommitter->dReader.pRowInfo->row = tsdbRowFromBlockData(pBlockDatal, pCommitter->dReader.iRow);
H
Hongze Cheng 已提交
352 353 354
  } else {
    pCommitter->dReader.iBlockL++;
    if (pCommitter->dReader.iBlockL < taosArrayGetSize(pCommitter->dReader.aBlockL)) {
H
Hongze Cheng 已提交
355 356 357 358
      SBlockL *pBlockL = (SBlockL *)taosArrayGet(pCommitter->dReader.aBlockL, pCommitter->dReader.iBlockL);
      int64_t  suid = pBlockL->suid;
      int64_t  uid = pBlockL->maxUid;

H
Hongze Cheng 已提交
359
      code = tsdbCommitterUpdateTableSchema(pCommitter, suid, uid);
H
Hongze Cheng 已提交
360 361 362 363 364 365
      if (code) goto _exit;

      code = tBlockDataInit(pBlockDatal, suid, suid ? 0 : uid, pCommitter->skmTable.pTSchema);
      if (code) goto _exit;

      code = tsdbReadLastBlock(pCommitter->dReader.pReader, pBlockL, pBlockDatal);
H
Hongze Cheng 已提交
366 367 368
      if (code) goto _exit;

      pCommitter->dReader.iRow = 0;
H
Hongze Cheng 已提交
369 370 371 372 373 374 375
      pCommitter->dReader.pRowInfo->suid = pBlockDatal->suid;
      if (pBlockDatal->uid) {
        pCommitter->dReader.pRowInfo->uid = pBlockDatal->uid;
      } else {
        pCommitter->dReader.pRowInfo->uid = pBlockDatal->aUid[0];
      }
      pCommitter->dReader.pRowInfo->row = tsdbRowFromBlockData(pBlockDatal, pCommitter->dReader.iRow);
H
Hongze Cheng 已提交
376 377 378 379 380 381 382 383 384
    } else {
      pCommitter->dReader.pRowInfo = NULL;
    }
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
385 386 387 388 389 390 391 392 393 394
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 已提交
395
    code = tsdbReadBlock(pCommitter->dReader.pReader, pCommitter->dReader.pBlockIdx, &pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
396 397 398 399 400 401 402 403 404 405 406
    if (code) goto _exit;

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

_exit:
  return code;
}

H
Hongze Cheng 已提交
407 408 409 410
static int32_t tsdbCommitFileDataStart(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SDFileSet *pRSet = NULL;
H
Hongze Cheng 已提交
411

H
Hongze Cheng 已提交
412
  // memory
H
Hongze Cheng 已提交
413 414 415
  pCommitter->commitFid = tsdbKeyFid(pCommitter->nextKey, pCommitter->minutes, pCommitter->precision);
  tsdbFidKeyRange(pCommitter->commitFid, pCommitter->minutes, pCommitter->precision, &pCommitter->minKey,
                  &pCommitter->maxKey);
H
Hongze Cheng 已提交
416
  pCommitter->nextKey = TSKEY_MAX;
H
Hongze Cheng 已提交
417

H
Hongze Cheng 已提交
418
  // Reader
H
Hongze Cheng 已提交
419 420
  pRSet = (SDFileSet *)taosArraySearch(pCommitter->fs.aDFileSet, &(SDFileSet){.fid = pCommitter->commitFid},
                                       tDFileSetCmprFn, TD_EQ);
H
Hongze Cheng 已提交
421
  if (pRSet) {
H
Hongze Cheng 已提交
422
    code = tsdbDataFReaderOpen(&pCommitter->dReader.pReader, pTsdb, pRSet);
H
Hongze Cheng 已提交
423 424
    if (code) goto _err;

H
Hongze Cheng 已提交
425
    // data
H
Hongze Cheng 已提交
426
    code = tsdbReadBlockIdx(pCommitter->dReader.pReader, pCommitter->dReader.aBlockIdx);
H
Hongze Cheng 已提交
427
    if (code) goto _err;
H
Hongze Cheng 已提交
428

H
Hongze Cheng 已提交
429 430 431 432 433
    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 已提交
434
      code = tsdbReadBlock(pCommitter->dReader.pReader, pCommitter->dReader.pBlockIdx, &pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
435 436 437 438
      if (code) goto _err;
    } else {
      pCommitter->dReader.pBlockIdx = NULL;
    }
H
Hongze Cheng 已提交
439
    tBlockDataReset(&pCommitter->dReader.bData);
H
Hongze Cheng 已提交
440 441

    // last
H
Hongze Cheng 已提交
442
    code = tsdbReadBlockL(pCommitter->dReader.pReader, pCommitter->dReader.aBlockL);
H
Hongze Cheng 已提交
443 444
    if (code) goto _err;

H
Hongze Cheng 已提交
445 446 447
    pCommitter->dReader.iBlockL = -1;
    pCommitter->dReader.iRow = -1;
    pCommitter->dReader.pRowInfo = &pCommitter->dReader.rowInfo;
H
Hongze Cheng 已提交
448
    tBlockDataReset(&pCommitter->dReader.bDatal);
H
Hongze Cheng 已提交
449
    code = tsdbCommitterNextLastRow(pCommitter);
H
Hongze Cheng 已提交
450
    if (code) goto _err;
H
Hongze Cheng 已提交
451
  } else {
H
Hongze Cheng 已提交
452
    pCommitter->dReader.pBlockIdx = NULL;
H
Hongze Cheng 已提交
453
    pCommitter->dReader.pRowInfo = NULL;
H
Hongze Cheng 已提交
454
  }
H
Hongze Cheng 已提交
455

H
Hongze Cheng 已提交
456
  // Writer
H
Hongze Cheng 已提交
457 458 459 460 461
  SHeadFile fHead;
  SDataFile fData;
  SLastFile fLast;
  SSmaFile  fSma;
  SDFileSet wSet = {.pHeadF = &fHead, .pDataF = &fData, .pLastF = &fLast, .pSmaF = &fSma};
H
Hongze Cheng 已提交
462
  if (pRSet) {
H
Hongze Cheng 已提交
463 464
    wSet.diskId = pRSet->diskId;
    wSet.fid = pCommitter->commitFid;
H
Hongze Cheng 已提交
465
    fHead = (SHeadFile){.commitID = pCommitter->commitID, .size = 0, .offset = 0};
H
Hongze Cheng 已提交
466
    fData = *pRSet->pDataF;
H
Hongze Cheng 已提交
467
    fLast = (SLastFile){.commitID = pCommitter->commitID, .size = 0, .offset = 0};
H
Hongze Cheng 已提交
468
    fSma = *pRSet->pSmaF;
H
Hongze Cheng 已提交
469
  } else {
470 471 472 473
    SDiskID did = {0};

    tfsAllocDisk(pTsdb->pVnode->pTfs, 0, &did);

H
Hongze Cheng 已提交
474 475
    tfsMkdirRecurAt(pTsdb->pVnode->pTfs, pTsdb->path, did);

476
    wSet.diskId = did;
H
Hongze Cheng 已提交
477
    wSet.fid = pCommitter->commitFid;
H
Hongze Cheng 已提交
478
    fHead = (SHeadFile){.commitID = pCommitter->commitID, .size = 0, .offset = 0};
H
Hongze Cheng 已提交
479
    fData = (SDataFile){.commitID = pCommitter->commitID, .size = 0};
H
Hongze Cheng 已提交
480
    fLast = (SLastFile){.commitID = pCommitter->commitID, .size = 0, .offset = 0};
H
Hongze Cheng 已提交
481
    fSma = (SSmaFile){.commitID = pCommitter->commitID, .size = 0};
H
Hongze Cheng 已提交
482
  }
H
Hongze Cheng 已提交
483
  code = tsdbDataFWriterOpen(&pCommitter->dWriter.pWriter, pTsdb, &wSet);
H
Hongze Cheng 已提交
484
  if (code) goto _err;
H
Hongze Cheng 已提交
485

H
Hongze Cheng 已提交
486 487 488 489 490 491
  taosArrayClear(pCommitter->dWriter.aBlockIdx);
  taosArrayClear(pCommitter->dWriter.aBlockL);
  tMapDataReset(&pCommitter->dWriter.mBlock);
  tBlockDataReset(&pCommitter->dWriter.bData);
  tBlockDataReset(&pCommitter->dWriter.bDatal);

H
Hongze Cheng 已提交
492
_exit:
H
Hongze Cheng 已提交
493 494 495
  return code;

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

H
Hongze Cheng 已提交
500
static int32_t tsdbCommitDataBlock(SCommitter *pCommitter, SBlock *pBlock) {
H
Hongze Cheng 已提交
501 502 503
  int32_t     code = 0;
  SBlockData *pBlockData = &pCommitter->dWriter.bData;
  SBlock      block;
H
Hongze Cheng 已提交
504

H
Hongze Cheng 已提交
505
  ASSERT(pBlockData->nRow > 0);
H
Hongze Cheng 已提交
506

H
Hongze Cheng 已提交
507 508 509 510 511 512
  if (pBlock) {
    block = *pBlock;  // as a subblock
  } else {
    tBlockReset(&block);  // as a new block
  }

H
Hongze Cheng 已提交
513
  // info
H
Hongze Cheng 已提交
514 515 516
  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 已提交
517

H
Hongze Cheng 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
    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 已提交
537 538
  block.nSubBlock++;
  code = tsdbWriteBlockData(pCommitter->dWriter.pWriter, pBlockData, &block.aSubBlock[block.nSubBlock - 1],
H
Hongze Cheng 已提交
539
                            ((block.nSubBlock == 1) && !block.hasDup) ? &block.smaInfo : NULL, pCommitter->cmprAlg, 0);
H
Hongze Cheng 已提交
540 541 542
  if (code) goto _err;

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

H
Hongze Cheng 已提交
546
  // clear
H
Hongze Cheng 已提交
547
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
548

H
Hongze Cheng 已提交
549 550 551 552
  return code;

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

static int32_t tsdbCommitLastBlock(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
557 558 559 560 561 562
  int32_t     code = 0;
  SBlockL     blockL;
  SBlockData *pBlockData = &pCommitter->dWriter.bDatal;

  ASSERT(pBlockData->nRow > 0);

H
Hongze Cheng 已提交
563
  // info
H
Hongze Cheng 已提交
564 565
  blockL.suid = pBlockData->suid;
  blockL.nRow = pBlockData->nRow;
H
Hongze Cheng 已提交
566 567
  blockL.minKey = TSKEY_MAX;
  blockL.maxKey = TSKEY_MIN;
H
Hongze Cheng 已提交
568 569 570
  blockL.minVer = VERSION_MAX;
  blockL.maxVer = VERSION_MIN;
  for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
H
Hongze Cheng 已提交
571 572
    blockL.minKey = TMIN(blockL.minKey, pBlockData->aTSKEY[iRow]);
    blockL.maxKey = TMAX(blockL.maxKey, pBlockData->aTSKEY[iRow]);
H
Hongze Cheng 已提交
573
    blockL.minVer = TMIN(blockL.minVer, pBlockData->aVersion[iRow]);
H
Hongze Cheng 已提交
574
    blockL.maxVer = TMAX(blockL.maxVer, pBlockData->aVersion[iRow]);
H
Hongze Cheng 已提交
575 576 577
  }
  blockL.minUid = pBlockData->uid ? pBlockData->uid : pBlockData->aUid[0];
  blockL.maxUid = pBlockData->uid ? pBlockData->uid : pBlockData->aUid[pBlockData->nRow - 1];
H
Hongze Cheng 已提交
578

H
Hongze Cheng 已提交
579
  // write
H
Hongze Cheng 已提交
580
  code = tsdbWriteBlockData(pCommitter->dWriter.pWriter, pBlockData, &blockL.bInfo, NULL, pCommitter->cmprAlg, 1);
H
Hongze Cheng 已提交
581
  if (code) goto _err;
H
Hongze Cheng 已提交
582

H
Hongze Cheng 已提交
583
  // push SBlockL
H
Hongze Cheng 已提交
584 585
  if (taosArrayPush(pCommitter->dWriter.aBlockL, &blockL) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
586
    goto _err;
H
Hongze Cheng 已提交
587 588
  }

H
Hongze Cheng 已提交
589
  // clear
H
Hongze Cheng 已提交
590
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
591

H
Hongze Cheng 已提交
592 593 594 595
  return code;

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

H
Hongze Cheng 已提交
599
static int32_t tsdbMergeCommitData(SCommitter *pCommitter, STbDataIter *pIter, SBlock *pBlock) {
H
Hongze Cheng 已提交
600
  int32_t     code = 0;
H
Hongze Cheng 已提交
601 602 603
  STbData    *pTbData = pIter->pTbData;
  SBlockData *pBlockDataR = &pCommitter->dReader.bData;
  SBlockData *pBlockDataW = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
604

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

H
Hongze Cheng 已提交
608
  tBlockDataClear(pBlockDataW);
H
Hongze Cheng 已提交
609 610 611 612 613 614 615
  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 已提交
616

H
Hongze Cheng 已提交
617 618 619
    if (c < 0) {
      code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow1));
      if (code) goto _err;
H
Hongze Cheng 已提交
620

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

H
Hongze Cheng 已提交
624 625 626 627
      // next
      tsdbTbDataIterNext(pIter);
      pRow1 = tsdbTbDataIterGet(pIter);
    } else if (c > 0) {
H
Hongze Cheng 已提交
628
      code = tBlockDataAppendRow(pBlockDataW, pRow2, NULL, pTbData->uid);
H
Hongze Cheng 已提交
629
      if (code) goto _err;
H
Hongze Cheng 已提交
630

H
Hongze Cheng 已提交
631 632 633
      iRow++;
      if (iRow < pBlockDataR->nRow) {
        *pRow2 = tsdbRowFromBlockData(pBlockDataR, iRow);
H
Hongze Cheng 已提交
634
      } else {
H
Hongze Cheng 已提交
635
        pRow2 = NULL;
H
Hongze Cheng 已提交
636
      }
H
Hongze Cheng 已提交
637 638
    } else {
      ASSERT(0);
H
Hongze Cheng 已提交
639 640
    }

H
Hongze Cheng 已提交
641 642
    // check
    if (pBlockDataW->nRow >= pCommitter->maxRow * 4 / 5) {
H
Hongze Cheng 已提交
643
      code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
644
      if (code) goto _err;
H
Hongze Cheng 已提交
645
    }
H
Hongze Cheng 已提交
646
  }
H
Hongze Cheng 已提交
647

H
Hongze Cheng 已提交
648
  while (pRow2) {
H
Hongze Cheng 已提交
649
    code = tBlockDataAppendRow(pBlockDataW, pRow2, NULL, pTbData->uid);
H
Hongze Cheng 已提交
650 651
    if (code) goto _err;

H
Hongze Cheng 已提交
652 653 654
    iRow++;
    if (iRow < pBlockDataR->nRow) {
      *pRow2 = tsdbRowFromBlockData(pBlockDataR, iRow);
H
Hongze Cheng 已提交
655
    } else {
H
Hongze Cheng 已提交
656
      pRow2 = NULL;
H
Hongze Cheng 已提交
657 658
    }

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

H
Hongze Cheng 已提交
666 667
  // check
  if (pBlockDataW->nRow > 0) {
H
Hongze Cheng 已提交
668
    code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
669
    if (code) goto _err;
H
Hongze Cheng 已提交
670 671 672 673 674
  }

  return code;

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

H
Hongze Cheng 已提交
679 680 681
static int32_t tsdbCommitTableMemData(SCommitter *pCommitter, STbDataIter *pIter, TSDBKEY toKey) {
  int32_t     code = 0;
  STbData    *pTbData = pIter->pTbData;
H
Hongze Cheng 已提交
682
  SBlockData *pBlockData = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
683

H
Hongze Cheng 已提交
684
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
685
  TSDBROW *pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
686
  while (true) {
H
Hongze Cheng 已提交
687
    if (pRow == NULL) {
H
Hongze Cheng 已提交
688 689 690 691 692 693 694
      if (pBlockData->nRow > 0) {
        goto _write_block;
      } else {
        break;
      }
    }

H
Hongze Cheng 已提交
695
    // update schema
H
Hongze Cheng 已提交
696
    code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow));
H
Hongze Cheng 已提交
697 698
    if (code) goto _err;

H
Hongze Cheng 已提交
699
    // append
H
Hongze Cheng 已提交
700
    code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema, pTbData->uid);
H
Hongze Cheng 已提交
701 702 703 704
    if (code) goto _err;

    tsdbTbDataIterNext(pIter);
    pRow = tsdbTbDataIterGet(pIter);
705
    if (pRow) {
H
Hongze Cheng 已提交
706 707
      TSDBKEY rowKey = TSDBROW_KEY(pRow);
      if (tsdbKeyCmprFn(&rowKey, &toKey) >= 0) {
708 709 710
        pRow = NULL;
      }
    }
H
Hongze Cheng 已提交
711

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

  return code;

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

H
Hongze Cheng 已提交
726 727
static int32_t tsdbGetNumOfRowsLessThan(STbDataIter *pIter, TSDBKEY key) {
  int32_t nRow = 0;
H
Hongze Cheng 已提交
728

H
Hongze Cheng 已提交
729
  STbDataIter iter = *pIter;
H
Hongze Cheng 已提交
730
  while (true) {
H
Hongze Cheng 已提交
731
    TSDBROW *pRow = tsdbTbDataIterGet(&iter);
H
Hongze Cheng 已提交
732 733
    if (pRow == NULL) break;

H
Hongze Cheng 已提交
734 735
    int32_t c = tsdbKeyCmprFn(&TSDBROW_KEY(pRow), &key);
    if (c < 0) {
H
Hongze Cheng 已提交
736
      nRow++;
H
Hongze Cheng 已提交
737
      tsdbTbDataIterNext(&iter);
H
Hongze Cheng 已提交
738 739 740 741 742 743 744 745 746 747 748 749
    } 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 已提交
750
  STbData    *pTbData = pIter->pTbData;
H
Hongze Cheng 已提交
751
  SBlockData *pBlockData = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
752

H
Hongze Cheng 已提交
753
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
754
  TSDBROW *pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
755
  while (true) {
H
Hongze Cheng 已提交
756 757 758 759 760 761 762 763 764 765
    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 已提交
766
    if (pRow) {
H
Hongze Cheng 已提交
767 768
      TSDBKEY rowKey = TSDBROW_KEY(pRow);
      if (tsdbKeyCmprFn(&rowKey, &pBlock->maxKey) > 0) {
H
Hongze Cheng 已提交
769 770 771 772 773
        pRow = NULL;
      }
    }
  }

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

H
Hongze Cheng 已提交
776
  code = tsdbCommitDataBlock(pCommitter, pBlock);
H
Hongze Cheng 已提交
777 778 779 780 781
  if (code) goto _err;

  return code;

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

H
Hongze Cheng 已提交
786 787 788 789
static int32_t tsdbMergeCommitLast(SCommitter *pCommitter, STbDataIter *pIter) {
  int32_t  code = 0;
  STbData *pTbData = pIter->pTbData;
  int32_t  nRow = tsdbGetNumOfRowsLessThan(pIter, (TSDBKEY){.ts = pCommitter->maxKey + 1, .version = VERSION_MIN});
H
Hongze Cheng 已提交
790

H
Hongze Cheng 已提交
791 792
  if (pCommitter->dReader.pRowInfo && tTABLEIDCmprFn(pTbData, pCommitter->dReader.pRowInfo) == 0) {
    if (pCommitter->dReader.pRowInfo->suid) {  // super table
H
Hongze Cheng 已提交
793 794 795 796
      for (int32_t iRow = pCommitter->dReader.iRow; iRow < pCommitter->dReader.bDatal.nRow; iRow++) {
        if (pTbData->uid != pCommitter->dReader.bDatal.aUid[iRow]) break;
        nRow++;
      }
H
Hongze Cheng 已提交
797
    } else {  // normal table
H
Hongze Cheng 已提交
798 799
      ASSERT(pCommitter->dReader.iRow == 0);
      nRow += pCommitter->dReader.bDatal.nRow;
H
Hongze Cheng 已提交
800 801 802 803 804
    }
  }

  if (nRow == 0) goto _exit;

H
Hongze Cheng 已提交
805
  TSDBROW *pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
806 807 808 809
  if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
    pRow = NULL;
  }

H
Hongze Cheng 已提交
810 811
  SRowInfo *pRowInfo = pCommitter->dReader.pRowInfo;
  if (pRowInfo && pRowInfo->uid != pTbData->uid) {
H
Hongze Cheng 已提交
812 813 814 815
    pRowInfo = NULL;
  }

  while (nRow) {
H
Hongze Cheng 已提交
816 817 818
    SBlockData *pBlockData;
    int8_t      toData;

H
Hongze Cheng 已提交
819
    if (nRow < pCommitter->minRow) {  // to .last
H
Hongze Cheng 已提交
820
      toData = 0;
H
Hongze Cheng 已提交
821 822
      pBlockData = &pCommitter->dWriter.bDatal;

H
Hongze Cheng 已提交
823
      // commit and reset block data schema if need
H
Hongze Cheng 已提交
824 825
      // QUESTION: Is there a case that pBlockData->nRow == 0 but need to change schema ?
      if (pBlockData->suid || pBlockData->uid) {
H
Hongze Cheng 已提交
826
        if (pBlockData->suid != pTbData->suid || pBlockData->suid == 0) {
H
Hongze Cheng 已提交
827 828 829 830
          if (pBlockData->nRow > 0) {
            code = tsdbCommitLastBlock(pCommitter);
            if (code) goto _err;
          }
H
Hongze Cheng 已提交
831

H
Hongze Cheng 已提交
832 833 834 835 836 837
          tBlockDataReset(pBlockData);
        }
      }

      // set block data schema if need
      if (pBlockData->suid == 0 && pBlockData->uid == 0) {
H
Hongze Cheng 已提交
838 839 840
        code = tsdbCommitterUpdateTableSchema(pCommitter, pTbData->suid, pTbData->uid);
        if (code) goto _err;

H
Hongze Cheng 已提交
841 842
        code =
            tBlockDataInit(pBlockData, pTbData->suid, pTbData->suid ? 0 : pTbData->uid, pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
843 844 845 846 847 848 849 850
        if (code) goto _err;
      }

      if (pBlockData->nRow + nRow > pCommitter->maxRow) {
        code = tsdbCommitLastBlock(pCommitter);
        if (code) goto _err;
      }
    } else {  // to .data
H
Hongze Cheng 已提交
851
      toData = 1;
H
Hongze Cheng 已提交
852
      pBlockData = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
853
      ASSERT(pBlockData->nRow == 0);
H
Hongze Cheng 已提交
854 855 856 857 858 859 860 861
    }

    while (pRow && pRowInfo) {
      int32_t c = tsdbRowCmprFn(pRow, &pRowInfo->row);
      if (c < 0) {
        code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow));
        if (code) goto _err;

H
Hongze Cheng 已提交
862
        code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema, pTbData->uid);
H
Hongze Cheng 已提交
863 864 865 866 867 868 869 870
        if (code) goto _err;

        tsdbTbDataIterNext(pIter);
        pRow = tsdbTbDataIterGet(pIter);
        if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
          pRow = NULL;
        }
      } else if (c > 0) {
H
Hongze Cheng 已提交
871
        code = tBlockDataAppendRow(pBlockData, &pRowInfo->row, NULL, pTbData->uid);
H
Hongze Cheng 已提交
872 873 874 875 876 877
        if (code) goto _err;

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

        pRowInfo = pCommitter->dReader.pRowInfo;
H
Hongze Cheng 已提交
878
        if (pRowInfo && pRowInfo->uid != pTbData->uid) {
H
Hongze Cheng 已提交
879 880 881 882 883 884 885
          pRowInfo = NULL;
        }
      } else {
        ASSERT(0);
      }

      nRow--;
H
Hongze Cheng 已提交
886 887 888
      if (toData) {
        if (nRow == 0 || pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
          code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
889 890 891 892 893 894 895 896 897 898
          if (code) goto _err;
          goto _outer_break;
        }
      }
    }

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

H
Hongze Cheng 已提交
899
      code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema, pTbData->uid);
H
Hongze Cheng 已提交
900 901 902 903 904 905 906 907
      if (code) goto _err;

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

H
Hongze Cheng 已提交
908
      nRow--;
H
Hongze Cheng 已提交
909 910 911
      if (toData) {
        if (nRow == 0 || pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
          code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
912 913
          if (code) goto _err;
          goto _outer_break;
H
Hongze Cheng 已提交
914 915
        }
      }
H
Hongze Cheng 已提交
916 917 918
    }

    while (pRowInfo) {
H
Hongze Cheng 已提交
919
      code = tBlockDataAppendRow(pBlockData, &pRowInfo->row, NULL, pTbData->uid);
H
Hongze Cheng 已提交
920 921 922 923
      if (code) goto _err;

      code = tsdbCommitterNextLastRow(pCommitter);
      if (code) goto _err;
H
Hongze Cheng 已提交
924 925

      pRowInfo = pCommitter->dReader.pRowInfo;
H
Hongze Cheng 已提交
926
      if (pRowInfo && pRowInfo->uid != pTbData->uid) {
H
Hongze Cheng 已提交
927 928 929
        pRowInfo = NULL;
      }

H
Hongze Cheng 已提交
930
      nRow--;
H
Hongze Cheng 已提交
931 932 933
      if (toData) {
        if (nRow == 0 || pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
          code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
934 935 936
          if (code) goto _err;
          goto _outer_break;
        }
H
Hongze Cheng 已提交
937 938 939
      }
    }

H
Hongze Cheng 已提交
940 941
  _outer_break:
    ASSERT(nRow >= 0);
H
Hongze Cheng 已提交
942 943
  }

H
Hongze Cheng 已提交
944
_exit:
H
Hongze Cheng 已提交
945 946 947
  return code;

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

H
Hongze Cheng 已提交
952 953 954 955
static int32_t tsdbCommitTableData(SCommitter *pCommitter, STbData *pTbData) {
  int32_t code = 0;

  ASSERT(pCommitter->dReader.pBlockIdx == NULL || tTABLEIDCmprFn(pCommitter->dReader.pBlockIdx, pTbData) >= 0);
H
Hongze Cheng 已提交
956
  ASSERT(pCommitter->dReader.pRowInfo == NULL || tTABLEIDCmprFn(pCommitter->dReader.pRowInfo, pTbData) >= 0);
H
Hongze Cheng 已提交
957 958 959 960 961 962 963 964 965 966 967 968

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

  tsdbTbDataIterOpen(pTbData, &(TSDBKEY){.ts = pCommitter->minKey, .version = VERSION_MIN}, 0, pIter);
  pRow = tsdbTbDataIterGet(pIter);
  if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
    pRow = NULL;
  }

H
Hongze Cheng 已提交
969 970 971
  if (pRow == NULL) {
    if (pCommitter->dReader.pBlockIdx && tTABLEIDCmprFn(pCommitter->dReader.pBlockIdx, pTbData) == 0) {
      SBlockIdx blockIdx = {.suid = pTbData->suid, .uid = pTbData->uid};
H
Hongze Cheng 已提交
972
      code = tsdbWriteBlock(pCommitter->dWriter.pWriter, &pCommitter->dReader.mBlock, &blockIdx);
H
Hongze Cheng 已提交
973 974 975 976 977 978 979 980 981 982
      if (code) goto _err;

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

    goto _exit;
  }
H
Hongze Cheng 已提交
983

H
Hongze Cheng 已提交
984 985 986
  int32_t iBlock = 0;
  SBlock  block;
  SBlock *pBlock = &block;
H
Hongze Cheng 已提交
987
  if (pCommitter->dReader.pBlockIdx && tTABLEIDCmprFn(pTbData, pCommitter->dReader.pBlockIdx) == 0) {
H
Hongze Cheng 已提交
988 989 990 991 992
    tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
  } else {
    pBlock = NULL;
  }

H
Hongze Cheng 已提交
993
  code = tsdbCommitterUpdateTableSchema(pCommitter, pTbData->suid, pTbData->uid);
H
Hongze Cheng 已提交
994 995
  if (code) goto _err;

H
Hongze Cheng 已提交
996
  tMapDataReset(&pCommitter->dWriter.mBlock);
H
Hongze Cheng 已提交
997 998
  code = tBlockDataInit(&pCommitter->dReader.bData, pTbData->suid, pTbData->uid, pCommitter->skmTable.pTSchema);
  if (code) goto _err;
H
Hongze Cheng 已提交
999
  code = tBlockDataInit(&pCommitter->dWriter.bData, pTbData->suid, pTbData->uid, pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
1000
  if (code) goto _err;
H
Hongze Cheng 已提交
1001 1002

  // .data merge
H
Hongze Cheng 已提交
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
  while (pBlock && pRow) {
    int32_t c = tBlockCmprFn(pBlock, &(SBlock){.minKey = TSDBROW_KEY(pRow), .maxKey = TSDBROW_KEY(pRow)});
    if (c < 0) {  // disk
      code = tMapDataPutItem(&pCommitter->dWriter.mBlock, pBlock, tPutBlock);
      if (code) goto _err;

      // next
      iBlock++;
      if (iBlock < pCommitter->dReader.mBlock.nItem) {
        tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
      } else {
        pBlock = NULL;
      }
H
Hongze Cheng 已提交
1016 1017
    } else if (c > 0) {  // memory
      code = tsdbCommitTableMemData(pCommitter, pIter, pBlock->minKey);
H
Hongze Cheng 已提交
1018 1019 1020 1021 1022 1023 1024 1025
      if (code) goto _err;

      // next
      pRow = tsdbTbDataIterGet(pIter);
      if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
        pRow = NULL;
      }
    } else {  // merge
H
Hongze Cheng 已提交
1026
      int32_t nOvlp = tsdbGetNumOfRowsLessThan(pIter, pBlock->maxKey);
H
Hongze Cheng 已提交
1027 1028 1029 1030 1031 1032 1033

      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 已提交
1034
        code = tsdbMergeCommitData(pCommitter, pIter, pBlock);
H
Hongze Cheng 已提交
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
        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;

H
Hongze Cheng 已提交
1056
    // next
H
Hongze Cheng 已提交
1057 1058 1059 1060 1061 1062 1063 1064
    iBlock++;
    if (iBlock < pCommitter->dReader.mBlock.nItem) {
      tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
    } else {
      pBlock = NULL;
    }
  }

H
Hongze Cheng 已提交
1065
  // .data append and .last merge
H
Hongze Cheng 已提交
1066 1067
  code = tsdbMergeCommitLast(pCommitter, pIter);
  if (code) goto _err;
H
Hongze Cheng 已提交
1068 1069 1070 1071

  // end
  if (pCommitter->dWriter.mBlock.nItem > 0) {
    SBlockIdx blockIdx = {.suid = pTbData->suid, .uid = pTbData->uid};
H
Hongze Cheng 已提交
1072
    code = tsdbWriteBlock(pCommitter->dWriter.pWriter, &pCommitter->dWriter.mBlock, &blockIdx);
H
Hongze Cheng 已提交
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
    if (code) goto _err;

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

_exit:
  pRow = tsdbTbDataIterGet(pIter);
  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 已提交
1094 1095 1096
static int32_t tsdbCommitFileDataEnd(SCommitter *pCommitter) {
  int32_t code = 0;

H
Hongze Cheng 已提交
1097
  // write aBlockIdx
H
Hongze Cheng 已提交
1098
  code = tsdbWriteBlockIdx(pCommitter->dWriter.pWriter, pCommitter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
1099 1100
  if (code) goto _err;

H
Hongze Cheng 已提交
1101
  // write aBlockL
H
Hongze Cheng 已提交
1102
  code = tsdbWriteBlockL(pCommitter->dWriter.pWriter, pCommitter->dWriter.aBlockL);
H
Hongze Cheng 已提交
1103 1104
  if (code) goto _err;

H
Hongze Cheng 已提交
1105
  // update file header
H
Hongze Cheng 已提交
1106
  code = tsdbUpdateDFileSetHeader(pCommitter->dWriter.pWriter);
H
Hongze Cheng 已提交
1107 1108 1109
  if (code) goto _err;

  // upsert SDFileSet
H
Hongze Cheng 已提交
1110
  code = tsdbFSUpsertFSet(&pCommitter->fs, &pCommitter->dWriter.pWriter->wSet);
H
Hongze Cheng 已提交
1111 1112 1113
  if (code) goto _err;

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

H
Hongze Cheng 已提交
1117 1118
  if (pCommitter->dReader.pReader) {
    code = tsdbDataFReaderClose(&pCommitter->dReader.pReader);
H
Hongze Cheng 已提交
1119
    if (code) goto _err;
H
Hongze Cheng 已提交
1120 1121 1122 1123 1124 1125
  }

_exit:
  return code;

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

H
Hongze Cheng 已提交
1130 1131 1132
static int32_t tsdbMoveCommitData(SCommitter *pCommitter, TABLEID toTable) {
  int32_t code = 0;

H
Hongze Cheng 已提交
1133
  // .data
H
Hongze Cheng 已提交
1134 1135 1136 1137
  while (true) {
    if (pCommitter->dReader.pBlockIdx == NULL || tTABLEIDCmprFn(pCommitter->dReader.pBlockIdx, &toTable) >= 0) break;

    SBlockIdx blockIdx = *pCommitter->dReader.pBlockIdx;
H
Hongze Cheng 已提交
1138
    code = tsdbWriteBlock(pCommitter->dWriter.pWriter, &pCommitter->dReader.mBlock, &blockIdx);
H
Hongze Cheng 已提交
1139 1140 1141 1142 1143 1144 1145
    if (code) goto _err;

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

H
Hongze Cheng 已提交
1146 1147
    code = tsdbCommitterNextTableData(pCommitter);
    if (code) goto _err;
H
Hongze Cheng 已提交
1148 1149
  }

H
Hongze Cheng 已提交
1150
  // .last
H
Hongze Cheng 已提交
1151
  while (true) {
H
Hongze Cheng 已提交
1152
    if (pCommitter->dReader.pRowInfo == NULL || tTABLEIDCmprFn(pCommitter->dReader.pRowInfo, &toTable) >= 0) break;
H
Hongze Cheng 已提交
1153

H
Hongze Cheng 已提交
1154 1155 1156 1157 1158 1159 1160 1161 1162
    SBlockData *pBlockDataR = &pCommitter->dReader.bDatal;
    SBlockData *pBlockDataW = &pCommitter->dWriter.bDatal;
    tb_uid_t    suid = pCommitter->dReader.pRowInfo->suid;
    tb_uid_t    uid = pCommitter->dReader.pRowInfo->uid;

    ASSERT((pBlockDataR->suid && !pBlockDataR->uid) || (!pBlockDataR->suid && pBlockDataR->uid));
    ASSERT(pBlockDataR->nRow > 0);

    // commit and reset block data schema if need
H
Hongze Cheng 已提交
1163 1164 1165 1166 1167 1168
    if (pBlockDataW->suid || pBlockDataW->uid) {
      if (pBlockDataW->suid != suid || pBlockDataW->suid == 0) {
        if (pBlockDataW->nRow > 0) {
          code = tsdbCommitLastBlock(pCommitter);
          if (code) goto _err;
        }
H
Hongze Cheng 已提交
1169
        tBlockDataReset(pBlockDataW);
H
Hongze Cheng 已提交
1170
      }
H
Hongze Cheng 已提交
1171 1172
    }

H
Hongze Cheng 已提交
1173 1174
    // set block data schema if need
    if (pBlockDataW->suid == 0 && pBlockDataW->uid == 0) {
H
Hongze Cheng 已提交
1175
      code = tsdbCommitterUpdateTableSchema(pCommitter, suid, uid);
H
Hongze Cheng 已提交
1176 1177
      if (code) goto _err;

H
Hongze Cheng 已提交
1178
      code = tBlockDataInit(pBlockDataW, suid, suid ? 0 : uid, pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
1179
      if (code) goto _err;
H
Hongze Cheng 已提交
1180 1181
    }

H
Hongze Cheng 已提交
1182 1183
    // check if it can make sure that one table data in one block
    int32_t nRow = 0;
H
Hongze Cheng 已提交
1184
    if (pBlockDataR->suid) {
H
Hongze Cheng 已提交
1185 1186
      int32_t iRow = pCommitter->dReader.iRow;
      while ((iRow < pBlockDataR->nRow) && (pBlockDataR->aUid[iRow] == uid)) {
H
Hongze Cheng 已提交
1187
        nRow++;
H
Hongze Cheng 已提交
1188
        iRow++;
H
Hongze Cheng 已提交
1189 1190 1191 1192
      }
    } else {
      ASSERT(pCommitter->dReader.iRow == 0);
      nRow = pBlockDataR->nRow;
H
Hongze Cheng 已提交
1193
    }
H
Hongze Cheng 已提交
1194

H
Hongze Cheng 已提交
1195
    ASSERT(nRow > 0 && nRow < pCommitter->minRow);
H
Hongze Cheng 已提交
1196

H
Hongze Cheng 已提交
1197 1198
    if (pBlockDataW->nRow + nRow > pCommitter->maxRow) {
      ASSERT(pBlockDataW->nRow > 0);
H
Hongze Cheng 已提交
1199

H
Hongze Cheng 已提交
1200 1201
      code = tsdbCommitLastBlock(pCommitter);
      if (code) goto _err;
H
Hongze Cheng 已提交
1202 1203
    }

H
Hongze Cheng 已提交
1204
    while (nRow > 0) {
H
Hongze Cheng 已提交
1205
      code = tBlockDataAppendRow(pBlockDataW, &pCommitter->dReader.pRowInfo->row, NULL, uid);
H
Hongze Cheng 已提交
1206 1207
      if (code) goto _err;

H
Hongze Cheng 已提交
1208
      code = tsdbCommitterNextLastRow(pCommitter);
H
Hongze Cheng 已提交
1209
      if (code) goto _err;
H
Hongze Cheng 已提交
1210 1211

      nRow--;
H
Hongze Cheng 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
    }
  }

  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 已提交
1222
static int32_t tsdbCommitFileData(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1223 1224 1225
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1226 1227 1228 1229 1230 1231

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

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

H
Hongze Cheng 已提交
1235
    // move commit until current (suid, uid)
H
Hongze Cheng 已提交
1236
    code = tsdbMoveCommitData(pCommitter, *(TABLEID *)pTbData);
H
Hongze Cheng 已提交
1237 1238
    if (code) goto _err;

H
Hongze Cheng 已提交
1239
    // commit current table data
H
Hongze Cheng 已提交
1240
    code = tsdbCommitTableData(pCommitter, pTbData);
H
Hongze Cheng 已提交
1241
    if (code) goto _err;
H
Hongze Cheng 已提交
1242 1243 1244 1245 1246 1247

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

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

H
Hongze Cheng 已提交
1253
  if (pCommitter->dWriter.bDatal.nRow > 0) {
H
Hongze Cheng 已提交
1254
    code = tsdbCommitLastBlock(pCommitter);
H
Hongze Cheng 已提交
1255
    if (code) goto _err;
H
Hongze Cheng 已提交
1256 1257
  }

H
Hongze Cheng 已提交
1258 1259
  // commit file data end
  code = tsdbCommitFileDataEnd(pCommitter);
H
Hongze Cheng 已提交
1260
  if (code) goto _err;
H
Hongze Cheng 已提交
1261 1262 1263 1264

  return code;

_err:
S
Shengliang Guan 已提交
1265
  tsdbError("vgId:%d, commit file data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1266 1267
  tsdbDataFReaderClose(&pCommitter->dReader.pReader);
  tsdbDataFWriterClose(&pCommitter->dWriter.pWriter, 0);
H
Hongze Cheng 已提交
1268 1269 1270
  return code;
}

H
Hongze Cheng 已提交
1271 1272
// ----------------------------------------------------------------------------
static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1273
  int32_t code = 0;
H
Hongze Cheng 已提交
1274

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

H
more  
Hongze Cheng 已提交
1278
  taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1279 1280
  pTsdb->imem = pTsdb->mem;
  pTsdb->mem = NULL;
H
more  
Hongze Cheng 已提交
1281
  taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1282

H
Hongze Cheng 已提交
1283
  pCommitter->pTsdb = pTsdb;
H
Hongze Cheng 已提交
1284
  pCommitter->commitID = pTsdb->pVnode->state.commitID;
H
Hongze Cheng 已提交
1285 1286 1287 1288
  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 已提交
1289
  pCommitter->cmprAlg = pTsdb->pVnode->config.tsdbCfg.compression;
H
Hongze Cheng 已提交
1290 1291 1292 1293 1294
  pCommitter->aTbDataP = tsdbMemTableGetTbDataArray(pTsdb->imem);
  if (pCommitter->aTbDataP == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
1295

H
Hongze Cheng 已提交
1296
  code = tsdbFSCopy(pTsdb, &pCommitter->fs);
H
Hongze Cheng 已提交
1297 1298 1299 1300 1301
  if (code) goto _err;

  return code;

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

H
Hongze Cheng 已提交
1306 1307 1308
static int32_t tsdbCommitDataStart(SCommitter *pCommitter) {
  int32_t code = 0;

H
Hongze Cheng 已提交
1309
  // Reader
H
Hongze Cheng 已提交
1310 1311
  pCommitter->dReader.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pCommitter->dReader.aBlockIdx == NULL) {
H
Hongze Cheng 已提交
1312 1313 1314 1315
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
1316
  code = tBlockDataCreate(&pCommitter->dReader.bData);
H
Hongze Cheng 已提交
1317 1318
  if (code) goto _exit;

H
Hongze Cheng 已提交
1319 1320
  pCommitter->dReader.aBlockL = taosArrayInit(0, sizeof(SBlockL));
  if (pCommitter->dReader.aBlockL == NULL) {
H
Hongze Cheng 已提交
1321 1322 1323 1324
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
1325
  code = tBlockDataCreate(&pCommitter->dReader.bDatal);
H
Hongze Cheng 已提交
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
  if (code) goto _exit;

  // Writer
  pCommitter->dWriter.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pCommitter->dWriter.aBlockIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

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

H
Hongze Cheng 已提交
1341
  code = tBlockDataCreate(&pCommitter->dWriter.bData);
H
Hongze Cheng 已提交
1342
  if (code) goto _exit;
H
Hongze Cheng 已提交
1343

H
Hongze Cheng 已提交
1344
  code = tBlockDataCreate(&pCommitter->dWriter.bDatal);
H
Hongze Cheng 已提交
1345 1346
  if (code) goto _exit;

H
Hongze Cheng 已提交
1347 1348 1349 1350 1351
_exit:
  return code;
}

static void tsdbCommitDataEnd(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1352
  // Reader
H
Hongze Cheng 已提交
1353 1354
  taosArrayDestroy(pCommitter->dReader.aBlockIdx);
  tMapDataClear(&pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
1355
  tBlockDataDestroy(&pCommitter->dReader.bData, 1);
H
Hongze Cheng 已提交
1356
  taosArrayDestroy(pCommitter->dReader.aBlockL);
H
Hongze Cheng 已提交
1357
  tBlockDataDestroy(&pCommitter->dReader.bDatal, 1);
H
Hongze Cheng 已提交
1358 1359

  // Writer
H
Hongze Cheng 已提交
1360
  taosArrayDestroy(pCommitter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
1361
  taosArrayDestroy(pCommitter->dWriter.aBlockL);
H
Hongze Cheng 已提交
1362
  tMapDataClear(&pCommitter->dWriter.mBlock);
H
Hongze Cheng 已提交
1363 1364
  tBlockDataDestroy(&pCommitter->dWriter.bData, 1);
  tBlockDataDestroy(&pCommitter->dWriter.bDatal, 1);
H
Hongze Cheng 已提交
1365 1366
  tTSchemaDestroy(pCommitter->skmTable.pTSchema);
  tTSchemaDestroy(pCommitter->skmRow.pTSchema);
H
Hongze Cheng 已提交
1367 1368
}

H
Hongze Cheng 已提交
1369 1370 1371 1372
static int32_t tsdbCommitData(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1373

H
Hongze Cheng 已提交
1374
  // check
H
Hongze Cheng 已提交
1375
  if (pMemTable->nRow == 0) goto _exit;
H
Hongze Cheng 已提交
1376

H
Hongze Cheng 已提交
1377 1378
  // start ====================
  code = tsdbCommitDataStart(pCommitter);
H
Hongze Cheng 已提交
1379
  if (code) goto _err;
H
Hongze Cheng 已提交
1380 1381 1382

  // impl ====================
  pCommitter->nextKey = pMemTable->minKey;
H
Hongze Cheng 已提交
1383 1384
  while (pCommitter->nextKey < TSKEY_MAX) {
    code = tsdbCommitFileData(pCommitter);
H
Hongze Cheng 已提交
1385
    if (code) goto _err;
H
Hongze Cheng 已提交
1386
  }
H
Hongze Cheng 已提交
1387

H
Hongze Cheng 已提交
1388 1389 1390
  // end ====================
  tsdbCommitDataEnd(pCommitter);

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

H
Hongze Cheng 已提交
1395
_err:
H
Hongze Cheng 已提交
1396
  tsdbCommitDataEnd(pCommitter);
S
Shengliang Guan 已提交
1397
  tsdbError("vgId:%d, commit data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1398 1399
  return code;
}
H
Hongze Cheng 已提交
1400

H
Hongze Cheng 已提交
1401 1402 1403 1404
static int32_t tsdbCommitDel(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1405

H
Hongze Cheng 已提交
1406 1407
  if (pMemTable->nDel == 0) {
    goto _exit;
H
Hongze Cheng 已提交
1408
  }
H
Hongze Cheng 已提交
1409

H
Hongze Cheng 已提交
1410 1411 1412 1413 1414
  // start
  code = tsdbCommitDelStart(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1415

H
Hongze Cheng 已提交
1416
  // impl
H
Hongze Cheng 已提交
1417 1418 1419
  int32_t  iDelIdx = 0;
  int32_t  nDelIdx = taosArrayGetSize(pCommitter->aDelIdx);
  int32_t  iTbData = 0;
H
Hongze Cheng 已提交
1420
  int32_t  nTbData = taosArrayGetSize(pCommitter->aTbDataP);
H
Hongze Cheng 已提交
1421 1422 1423 1424 1425
  STbData *pTbData;
  SDelIdx *pDelIdx;

  ASSERT(nTbData > 0);

H
Hongze Cheng 已提交
1426
  pTbData = (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData);
H
Hongze Cheng 已提交
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
  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 已提交
1452
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData) : NULL;
H
Hongze Cheng 已提交
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
    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 已提交
1468
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData) : NULL;
H
Hongze Cheng 已提交
1469 1470 1471
    iDelIdx++;
    pDelIdx = (iDelIdx < nDelIdx) ? (SDelIdx *)taosArrayGet(pCommitter->aDelIdx, iDelIdx) : NULL;
    continue;
H
Hongze Cheng 已提交
1472
  }
H
Hongze Cheng 已提交
1473

H
Hongze Cheng 已提交
1474 1475 1476 1477 1478
  // end
  code = tsdbCommitDelEnd(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1479

H
Hongze Cheng 已提交
1480
_exit:
S
Shengliang Guan 已提交
1481
  tsdbDebug("vgId:%d, commit del done, nDel:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nDel);
H
Hongze Cheng 已提交
1482 1483 1484
  return code;

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

H
Hongze Cheng 已提交
1489
static int32_t tsdbEndCommit(SCommitter *pCommitter, int32_t eno) {
H
Hongze Cheng 已提交
1490 1491 1492 1493
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

H
Hongze Cheng 已提交
1494
  ASSERT(eno == 0);
H
Hongze Cheng 已提交
1495 1496 1497

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

H
Hongze Cheng 已提交
1499
  // lock
H
Hongze Cheng 已提交
1500
  taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1501 1502 1503 1504 1505 1506 1507 1508

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

H
Hongze Cheng 已提交
1509
  pTsdb->imem = NULL;
H
Hongze Cheng 已提交
1510 1511

  // unlock
H
Hongze Cheng 已提交
1512 1513 1514
  taosThreadRwlockUnlock(&pTsdb->rwLock);

  tsdbUnrefMemTable(pMemTable);
H
Hongze Cheng 已提交
1515
  tsdbFSDestroy(&pCommitter->fs);
H
Hongze Cheng 已提交
1516
  taosArrayDestroy(pCommitter->aTbDataP);
H
Hongze Cheng 已提交
1517

S
Shengliang Guan 已提交
1518
  tsdbInfo("vgId:%d, tsdb end commit", TD_VID(pTsdb->pVnode));
H
Hongze Cheng 已提交
1519 1520 1521
  return code;

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