tsdbCommit.c 42.0 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
  int8_t  maxLast;
H
Hongze Cheng 已提交
39 40
  SArray *aTbDataP;  // memory
  STsdbFS fs;        // disk
H
Hongze Cheng 已提交
41
  // --------------
H
Hongze Cheng 已提交
42
  TSKEY   nextKey;  // reset by each table commit
H
Hongze Cheng 已提交
43 44 45
  int32_t commitFid;
  TSKEY   minKey;
  TSKEY   maxKey;
H
Hongze Cheng 已提交
46
  // commit file data
H
Hongze Cheng 已提交
47 48
  struct {
    SDataFReader *pReader;
H
Hongze Cheng 已提交
49 50 51 52 53
    SArray       *aBlockIdx;  // SArray<SBlockIdx>
    int32_t       iBlockIdx;
    SBlockIdx    *pBlockIdx;
    SMapData      mBlock;  // SMapData<SBlock>
    SBlockData    bData;
H
Hongze Cheng 已提交
54 55 56 57
  } dReader;
  struct {
    SDataFWriter *pWriter;
    SArray       *aBlockIdx;  // SArray<SBlockIdx>
H
Hongze Cheng 已提交
58
    SArray       *aBlockL;    // SArray<SBlockL>
H
Hongze Cheng 已提交
59 60
    SMapData      mBlock;     // SMapData<SBlock>
    SBlockData    bData;
H
Hongze Cheng 已提交
61
    SBlockData    bDatal;
H
Hongze Cheng 已提交
62 63 64
  } dWriter;
  SSkmInfo skmTable;
  SSkmInfo skmRow;
H
Hongze Cheng 已提交
65
  /* commit del */
H
Hongze Cheng 已提交
66 67
  SDelFReader *pDelFReader;
  SDelFWriter *pDelFWriter;
H
Hongze Cheng 已提交
68 69 70
  SArray      *aDelIdx;   // SArray<SDelIdx>
  SArray      *aDelIdxN;  // SArray<SDelIdx>
  SArray      *aDelData;  // SArray<SDelData>
H
Hongze Cheng 已提交
71
} SCommitter;
H
refact  
Hongze Cheng 已提交
72

H
Hongze Cheng 已提交
73 74 75 76 77
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 已提交
78

H
refact  
Hongze Cheng 已提交
79
int32_t tsdbBegin(STsdb *pTsdb) {
H
Hongze Cheng 已提交
80
  int32_t code = 0;
H
Hongze Cheng 已提交
81

82 83
  if (!pTsdb) return code;

H
Hongze Cheng 已提交
84 85
  SMemTable *pMemTable;
  code = tsdbMemTableCreate(pTsdb, &pMemTable);
H
Hongze Cheng 已提交
86
  if (code) goto _err;
H
Hongze Cheng 已提交
87

H
Hongze Cheng 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  // 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 已提交
104 105 106
  return code;

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

H
more  
Hongze Cheng 已提交
111
int32_t tsdbCommit(STsdb *pTsdb) {
112
  if (!pTsdb) return 0;
H
Hongze Cheng 已提交
113

H
more  
Hongze Cheng 已提交
114
  int32_t    code = 0;
H
Hongze Cheng 已提交
115 116 117 118
  SCommitter commith;
  SMemTable *pMemTable = pTsdb->mem;

  // check
H
Hongze Cheng 已提交
119
  if (pMemTable->nRow == 0 && pMemTable->nDel == 0) {
H
Hongze Cheng 已提交
120
    taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
121
    pTsdb->mem = NULL;
H
Hongze Cheng 已提交
122 123 124
    taosThreadRwlockUnlock(&pTsdb->rwLock);

    tsdbUnrefMemTable(pMemTable);
H
Hongze Cheng 已提交
125 126
    goto _exit;
  }
H
refact  
Hongze Cheng 已提交
127

H
more  
Hongze Cheng 已提交
128
  // start commit
H
more  
Hongze Cheng 已提交
129
  code = tsdbStartCommit(pTsdb, &commith);
H
Hongze Cheng 已提交
130
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
131

H
refact  
Hongze Cheng 已提交
132 133
  // commit impl
  code = tsdbCommitData(&commith);
H
Hongze Cheng 已提交
134
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
135 136

  code = tsdbCommitDel(&commith);
H
Hongze Cheng 已提交
137
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
138 139

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

H
Hongze Cheng 已提交
143
_exit:
H
refact  
Hongze Cheng 已提交
144 145 146
  return code;

_err:
H
Hongze Cheng 已提交
147
  tsdbEndCommit(&commith, code);
C
Cary Xu 已提交
148
  tsdbError("vgId:%d, failed to commit since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
refact  
Hongze Cheng 已提交
149 150 151
  return code;
}

H
Hongze Cheng 已提交
152
static int32_t tsdbCommitDelStart(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
153 154 155 156
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

H
Hongze Cheng 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  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 已提交
174

H
Hongze Cheng 已提交
175
  SDelFile *pDelFileR = pCommitter->fs.pDelFile;
H
Hongze Cheng 已提交
176
  if (pDelFileR) {
H
Hongze Cheng 已提交
177
    code = tsdbDelFReaderOpen(&pCommitter->pDelFReader, pDelFileR, pTsdb);
H
Hongze Cheng 已提交
178
    if (code) goto _err;
H
Hongze Cheng 已提交
179

H
Hongze Cheng 已提交
180
    code = tsdbReadDelIdx(pCommitter->pDelFReader, pCommitter->aDelIdx);
H
Hongze Cheng 已提交
181
    if (code) goto _err;
H
Hongze Cheng 已提交
182 183
  }

H
Hongze Cheng 已提交
184
  // prepare new
H
Hongze Cheng 已提交
185 186
  SDelFile wDelFile = {.commitID = pCommitter->commitID, .size = 0, .offset = 0};
  code = tsdbDelFWriterOpen(&pCommitter->pDelFWriter, &wDelFile, pTsdb);
H
Hongze Cheng 已提交
187
  if (code) goto _err;
H
Hongze Cheng 已提交
188 189

_exit:
S
Shengliang Guan 已提交
190
  tsdbDebug("vgId:%d, commit del start", TD_VID(pTsdb->pVnode));
H
Hongze Cheng 已提交
191 192 193
  return code;

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

H
Hongze Cheng 已提交
198
static int32_t tsdbCommitTableDel(SCommitter *pCommitter, STbData *pTbData, SDelIdx *pDelIdx) {
H
Hongze Cheng 已提交
199
  int32_t   code = 0;
H
Hongze Cheng 已提交
200
  SDelData *pDelData;
H
Hongze Cheng 已提交
201 202
  tb_uid_t  suid;
  tb_uid_t  uid;
H
Hongze Cheng 已提交
203 204

  if (pTbData) {
H
Hongze Cheng 已提交
205 206
    suid = pTbData->suid;
    uid = pTbData->uid;
H
Hongze Cheng 已提交
207

H
Hongze Cheng 已提交
208 209 210 211
    if (pTbData->pHead == NULL) {
      pTbData = NULL;
    }
  }
H
Hongze Cheng 已提交
212 213

  if (pDelIdx) {
H
Hongze Cheng 已提交
214 215 216
    suid = pDelIdx->suid;
    uid = pDelIdx->uid;

H
Hongze Cheng 已提交
217
    code = tsdbReadDelData(pCommitter->pDelFReader, pDelIdx, pCommitter->aDelData);
H
Hongze Cheng 已提交
218
    if (code) goto _err;
219 220
  } else {
    taosArrayClear(pCommitter->aDelData);
H
Hongze Cheng 已提交
221 222
  }

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

H
Hongze Cheng 已提交
225
  SDelIdx delIdx = {.suid = suid, .uid = uid};
H
Hongze Cheng 已提交
226 227

  // memory
H
Hongze Cheng 已提交
228 229
  pDelData = pTbData ? pTbData->pHead : NULL;
  for (; pDelData; pDelData = pDelData->pNext) {
H
Hongze Cheng 已提交
230 231 232 233
    if (taosArrayPush(pCommitter->aDelData, pDelData) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
234 235 236
  }

  // write
H
Hongze Cheng 已提交
237
  code = tsdbWriteDelData(pCommitter->pDelFWriter, pCommitter->aDelData, &delIdx);
H
Hongze Cheng 已提交
238 239 240
  if (code) goto _err;

  // put delIdx
241
  if (taosArrayPush(pCommitter->aDelIdxN, &delIdx) == NULL) {
H
Hongze Cheng 已提交
242 243 244
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
245 246 247 248 249

_exit:
  return code;

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

H
Hongze Cheng 已提交
254 255
static int32_t tsdbCommitDelEnd(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
256
  STsdb  *pTsdb = pCommitter->pTsdb;
H
Hongze Cheng 已提交
257

H
Hongze Cheng 已提交
258
  code = tsdbWriteDelIdx(pCommitter->pDelFWriter, pCommitter->aDelIdxN);
H
Hongze Cheng 已提交
259
  if (code) goto _err;
H
Hongze Cheng 已提交
260

H
Hongze Cheng 已提交
261 262 263
  code = tsdbUpdateDelFileHdr(pCommitter->pDelFWriter);
  if (code) goto _err;

H
Hongze Cheng 已提交
264
  code = tsdbFSUpsertDelFile(&pCommitter->fs, &pCommitter->pDelFWriter->fDel);
H
Hongze Cheng 已提交
265
  if (code) goto _err;
H
Hongze Cheng 已提交
266

H
Hongze Cheng 已提交
267
  code = tsdbDelFWriterClose(&pCommitter->pDelFWriter, 1);
H
Hongze Cheng 已提交
268
  if (code) goto _err;
H
Hongze Cheng 已提交
269 270

  if (pCommitter->pDelFReader) {
H
Hongze Cheng 已提交
271
    code = tsdbDelFReaderClose(&pCommitter->pDelFReader);
H
Hongze Cheng 已提交
272 273 274
    if (code) goto _err;
  }

H
Hongze Cheng 已提交
275 276 277 278
  taosArrayDestroy(pCommitter->aDelIdx);
  taosArrayDestroy(pCommitter->aDelData);
  taosArrayDestroy(pCommitter->aDelIdxN);

H
Hongze Cheng 已提交
279 280 281
  return code;

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

H
Hongze Cheng 已提交
286
static int32_t tsdbCommitterUpdateTableSchema(SCommitter *pCommitter, int64_t suid, int64_t uid) {
H
Hongze Cheng 已提交
287 288
  int32_t code = 0;

H
Hongze Cheng 已提交
289 290 291 292
  if (suid) {
    if (pCommitter->skmTable.suid == suid) goto _exit;
  } else {
    if (pCommitter->skmTable.uid == uid) goto _exit;
H
Hongze Cheng 已提交
293 294 295 296 297
  }

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

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

H
Hongze Cheng 已提交
336
  SBlockData *pBlockDatal = &pCommitter->dReader.bDatal;
H
Hongze Cheng 已提交
337
  pCommitter->dReader.iRow++;
H
Hongze Cheng 已提交
338
  if (pCommitter->dReader.iRow < pBlockDatal->nRow) {
H
Hongze Cheng 已提交
339 340 341
    if (pBlockDatal->uid) {
      pCommitter->dReader.pRowInfo->uid = pBlockDatal->uid;
    } else {
H
Hongze Cheng 已提交
342 343 344
      pCommitter->dReader.pRowInfo->uid = pBlockDatal->aUid[pCommitter->dReader.iRow];
    }
    pCommitter->dReader.pRowInfo->row = tsdbRowFromBlockData(pBlockDatal, pCommitter->dReader.iRow);
H
Hongze Cheng 已提交
345 346 347
  } else {
    pCommitter->dReader.iBlockL++;
    if (pCommitter->dReader.iBlockL < taosArrayGetSize(pCommitter->dReader.aBlockL)) {
H
Hongze Cheng 已提交
348 349 350 351
      SBlockL *pBlockL = (SBlockL *)taosArrayGet(pCommitter->dReader.aBlockL, pCommitter->dReader.iBlockL);
      int64_t  suid = pBlockL->suid;
      int64_t  uid = pBlockL->maxUid;

H
Hongze Cheng 已提交
352
      code = tsdbCommitterUpdateTableSchema(pCommitter, suid, uid);
H
Hongze Cheng 已提交
353 354 355 356 357 358
      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 已提交
359 360 361
      if (code) goto _exit;

      pCommitter->dReader.iRow = 0;
H
Hongze Cheng 已提交
362 363 364 365 366 367 368
      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 已提交
369 370 371 372 373 374 375 376 377
    } else {
      pCommitter->dReader.pRowInfo = NULL;
    }
  }

_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 tsdbCommitFileDataStart(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SDFileSet *pRSet = NULL;
H
Hongze Cheng 已提交
404

H
Hongze Cheng 已提交
405
  // memory
H
Hongze Cheng 已提交
406 407 408
  pCommitter->commitFid = tsdbKeyFid(pCommitter->nextKey, pCommitter->minutes, pCommitter->precision);
  tsdbFidKeyRange(pCommitter->commitFid, pCommitter->minutes, pCommitter->precision, &pCommitter->minKey,
                  &pCommitter->maxKey);
H
Hongze Cheng 已提交
409
  pCommitter->nextKey = TSKEY_MAX;
H
Hongze Cheng 已提交
410

H
Hongze Cheng 已提交
411
  // Reader
H
Hongze Cheng 已提交
412 413
  pRSet = (SDFileSet *)taosArraySearch(pCommitter->fs.aDFileSet, &(SDFileSet){.fid = pCommitter->commitFid},
                                       tDFileSetCmprFn, TD_EQ);
H
Hongze Cheng 已提交
414
  if (pRSet) {
H
Hongze Cheng 已提交
415
    code = tsdbDataFReaderOpen(&pCommitter->dReader.pReader, pTsdb, pRSet);
H
Hongze Cheng 已提交
416 417
    if (code) goto _err;

H
Hongze Cheng 已提交
418
    // data
H
Hongze Cheng 已提交
419
    code = tsdbReadBlockIdx(pCommitter->dReader.pReader, pCommitter->dReader.aBlockIdx);
H
Hongze Cheng 已提交
420
    if (code) goto _err;
H
Hongze Cheng 已提交
421

H
Hongze Cheng 已提交
422 423 424 425 426
    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 已提交
427
      code = tsdbReadBlock(pCommitter->dReader.pReader, pCommitter->dReader.pBlockIdx, &pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
428 429 430 431
      if (code) goto _err;
    } else {
      pCommitter->dReader.pBlockIdx = NULL;
    }
H
Hongze Cheng 已提交
432
    tBlockDataReset(&pCommitter->dReader.bData);
H
Hongze Cheng 已提交
433
  } else {
H
Hongze Cheng 已提交
434
    pCommitter->dReader.pBlockIdx = NULL;
H
Hongze Cheng 已提交
435
  }
H
Hongze Cheng 已提交
436

H
Hongze Cheng 已提交
437
  // Writer
H
Hongze Cheng 已提交
438 439 440
  SHeadFile fHead;
  SDataFile fData;
  SSmaFile  fSma;
H
Hongze Cheng 已提交
441 442
  SLastFile fLast;
  SDFileSet wSet = {0};
H
Hongze Cheng 已提交
443
  if (pRSet) {
H
Hongze Cheng 已提交
444 445
    ASSERT(pRSet->nLastF < pCommitter->maxLast);
    fHead = (SHeadFile){.commitID = pCommitter->commitID};
H
Hongze Cheng 已提交
446 447
    fData = *pRSet->pDataF;
    fSma = *pRSet->pSmaF;
H
Hongze Cheng 已提交
448 449 450 451 452 453 454 455 456 457 458 459
    fLast = (SLastFile){.commitID = pCommitter->commitID};

    wSet.diskId = pRSet->diskId;
    wSet.fid = pCommitter->commitFid;
    wSet.pHeadF = &fHead;
    wSet.pDataF = &fData;
    wSet.pSmaF = &fSma;
    for (int8_t iLast = 0; iLast < pRSet->nLastF; iLast++) {
      wSet.aLastF[iLast] = pRSet->aLastF[iLast];
    }
    wSet.nLastF = pRSet->nLastF + 1;
    wSet.aLastF[wSet.nLastF - 1] = &fLast;  // todo
H
Hongze Cheng 已提交
460
  } else {
H
Hongze Cheng 已提交
461 462 463 464
    fHead = (SHeadFile){.commitID = pCommitter->commitID};
    fData = (SDataFile){.commitID = pCommitter->commitID};
    fSma = (SSmaFile){.commitID = pCommitter->commitID};
    fLast = (SLastFile){.commitID = pCommitter->commitID};
465

H
Hongze Cheng 已提交
466
    SDiskID did = {0};
467
    tfsAllocDisk(pTsdb->pVnode->pTfs, 0, &did);
H
Hongze Cheng 已提交
468 469
    tfsMkdirRecurAt(pTsdb->pVnode->pTfs, pTsdb->path, did);

470
    wSet.diskId = did;
H
Hongze Cheng 已提交
471
    wSet.fid = pCommitter->commitFid;
H
Hongze Cheng 已提交
472 473 474
    wSet.pHeadF = &fHead;
    wSet.pDataF = &fData;
    wSet.pSmaF = &fSma;
H
Hongze Cheng 已提交
475
    wSet.nLastF = 1;
H
Hongze Cheng 已提交
476
    wSet.aLastF[0] = &fLast;
H
Hongze Cheng 已提交
477
  }
H
Hongze Cheng 已提交
478
  code = tsdbDataFWriterOpen(&pCommitter->dWriter.pWriter, pTsdb, &wSet);
H
Hongze Cheng 已提交
479
  if (code) goto _err;
H
Hongze Cheng 已提交
480

H
Hongze Cheng 已提交
481 482 483 484 485 486
  taosArrayClear(pCommitter->dWriter.aBlockIdx);
  taosArrayClear(pCommitter->dWriter.aBlockL);
  tMapDataReset(&pCommitter->dWriter.mBlock);
  tBlockDataReset(&pCommitter->dWriter.bData);
  tBlockDataReset(&pCommitter->dWriter.bDatal);

H
Hongze Cheng 已提交
487
_exit:
H
Hongze Cheng 已提交
488 489 490
  return code;

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

H
Hongze Cheng 已提交
495
static int32_t tsdbCommitDataBlock(SCommitter *pCommitter, SBlock *pBlock) {
H
Hongze Cheng 已提交
496 497 498
  int32_t     code = 0;
  SBlockData *pBlockData = &pCommitter->dWriter.bData;
  SBlock      block;
H
Hongze Cheng 已提交
499

H
Hongze Cheng 已提交
500
  ASSERT(pBlockData->nRow > 0);
H
Hongze Cheng 已提交
501

H
Hongze Cheng 已提交
502 503 504 505 506 507
  if (pBlock) {
    block = *pBlock;  // as a subblock
  } else {
    tBlockReset(&block);  // as a new block
  }

H
Hongze Cheng 已提交
508
  // info
H
Hongze Cheng 已提交
509 510 511
  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 已提交
512

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

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

H
Hongze Cheng 已提交
541
  // clear
H
Hongze Cheng 已提交
542
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
543

H
Hongze Cheng 已提交
544 545 546 547
  return code;

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

static int32_t tsdbCommitLastBlock(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
552 553 554 555 556 557
  int32_t     code = 0;
  SBlockL     blockL;
  SBlockData *pBlockData = &pCommitter->dWriter.bDatal;

  ASSERT(pBlockData->nRow > 0);

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

H
Hongze Cheng 已提交
574
  // write
H
Hongze Cheng 已提交
575
  code = tsdbWriteBlockData(pCommitter->dWriter.pWriter, pBlockData, &blockL.bInfo, NULL, pCommitter->cmprAlg, 1);
H
Hongze Cheng 已提交
576
  if (code) goto _err;
H
Hongze Cheng 已提交
577

H
Hongze Cheng 已提交
578
  // push SBlockL
H
Hongze Cheng 已提交
579 580
  if (taosArrayPush(pCommitter->dWriter.aBlockL, &blockL) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
581
    goto _err;
H
Hongze Cheng 已提交
582 583
  }

H
Hongze Cheng 已提交
584
  // clear
H
Hongze Cheng 已提交
585
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
586

H
Hongze Cheng 已提交
587 588 589 590
  return code;

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

H
Hongze Cheng 已提交
594
static int32_t tsdbMergeCommitData(SCommitter *pCommitter, STbDataIter *pIter, SBlock *pBlock) {
H
Hongze Cheng 已提交
595
  int32_t     code = 0;
H
Hongze Cheng 已提交
596 597 598
  STbData    *pTbData = pIter->pTbData;
  SBlockData *pBlockDataR = &pCommitter->dReader.bData;
  SBlockData *pBlockDataW = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
599

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

H
Hongze Cheng 已提交
603
  tBlockDataClear(pBlockDataW);
H
Hongze Cheng 已提交
604 605 606 607 608 609 610
  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 已提交
611

H
Hongze Cheng 已提交
612 613 614
    if (c < 0) {
      code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow1));
      if (code) goto _err;
H
Hongze Cheng 已提交
615

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

H
Hongze Cheng 已提交
619 620 621 622
      // next
      tsdbTbDataIterNext(pIter);
      pRow1 = tsdbTbDataIterGet(pIter);
    } else if (c > 0) {
H
Hongze Cheng 已提交
623
      code = tBlockDataAppendRow(pBlockDataW, pRow2, NULL, pTbData->uid);
H
Hongze Cheng 已提交
624
      if (code) goto _err;
H
Hongze Cheng 已提交
625

H
Hongze Cheng 已提交
626 627 628
      iRow++;
      if (iRow < pBlockDataR->nRow) {
        *pRow2 = tsdbRowFromBlockData(pBlockDataR, iRow);
H
Hongze Cheng 已提交
629
      } else {
H
Hongze Cheng 已提交
630
        pRow2 = NULL;
H
Hongze Cheng 已提交
631
      }
H
Hongze Cheng 已提交
632 633
    } else {
      ASSERT(0);
H
Hongze Cheng 已提交
634 635
    }

H
Hongze Cheng 已提交
636 637
    // check
    if (pBlockDataW->nRow >= pCommitter->maxRow * 4 / 5) {
H
Hongze Cheng 已提交
638
      code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
639
      if (code) goto _err;
H
Hongze Cheng 已提交
640
    }
H
Hongze Cheng 已提交
641
  }
H
Hongze Cheng 已提交
642

H
Hongze Cheng 已提交
643
  while (pRow2) {
H
Hongze Cheng 已提交
644
    code = tBlockDataAppendRow(pBlockDataW, pRow2, NULL, pTbData->uid);
H
Hongze Cheng 已提交
645 646
    if (code) goto _err;

H
Hongze Cheng 已提交
647 648 649
    iRow++;
    if (iRow < pBlockDataR->nRow) {
      *pRow2 = tsdbRowFromBlockData(pBlockDataR, iRow);
H
Hongze Cheng 已提交
650
    } else {
H
Hongze Cheng 已提交
651
      pRow2 = NULL;
H
Hongze Cheng 已提交
652 653
    }

H
Hongze Cheng 已提交
654 655
    // check
    if (pBlockDataW->nRow >= pCommitter->maxRow * 4 / 5) {
H
Hongze Cheng 已提交
656
      code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
657
      if (code) goto _err;
H
Hongze Cheng 已提交
658
    }
H
Hongze Cheng 已提交
659
  }
H
Hongze Cheng 已提交
660

H
Hongze Cheng 已提交
661 662
  // check
  if (pBlockDataW->nRow > 0) {
H
Hongze Cheng 已提交
663
    code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
664
    if (code) goto _err;
H
Hongze Cheng 已提交
665 666 667 668 669
  }

  return code;

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

H
Hongze Cheng 已提交
674 675 676
static int32_t tsdbCommitTableMemData(SCommitter *pCommitter, STbDataIter *pIter, TSDBKEY toKey) {
  int32_t     code = 0;
  STbData    *pTbData = pIter->pTbData;
H
Hongze Cheng 已提交
677
  SBlockData *pBlockData = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
678

H
Hongze Cheng 已提交
679
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
680
  TSDBROW *pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
681
  while (true) {
H
Hongze Cheng 已提交
682
    if (pRow == NULL) {
H
Hongze Cheng 已提交
683 684 685 686 687 688 689
      if (pBlockData->nRow > 0) {
        goto _write_block;
      } else {
        break;
      }
    }

H
Hongze Cheng 已提交
690
    // update schema
H
Hongze Cheng 已提交
691
    code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow));
H
Hongze Cheng 已提交
692 693
    if (code) goto _err;

H
Hongze Cheng 已提交
694
    // append
H
Hongze Cheng 已提交
695
    code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema, pTbData->uid);
H
Hongze Cheng 已提交
696 697 698 699
    if (code) goto _err;

    tsdbTbDataIterNext(pIter);
    pRow = tsdbTbDataIterGet(pIter);
700
    if (pRow) {
H
Hongze Cheng 已提交
701 702
      TSDBKEY rowKey = TSDBROW_KEY(pRow);
      if (tsdbKeyCmprFn(&rowKey, &toKey) >= 0) {
703 704 705
        pRow = NULL;
      }
    }
H
Hongze Cheng 已提交
706

H
Hongze Cheng 已提交
707 708
    if (pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
    _write_block:
H
Hongze Cheng 已提交
709
      code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
710 711
      if (code) goto _err;
    }
H
Hongze Cheng 已提交
712 713 714 715 716
  }

  return code;

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

H
Hongze Cheng 已提交
721 722
static int32_t tsdbGetNumOfRowsLessThan(STbDataIter *pIter, TSDBKEY key) {
  int32_t nRow = 0;
H
Hongze Cheng 已提交
723

H
Hongze Cheng 已提交
724
  STbDataIter iter = *pIter;
H
Hongze Cheng 已提交
725
  while (true) {
H
Hongze Cheng 已提交
726
    TSDBROW *pRow = tsdbTbDataIterGet(&iter);
H
Hongze Cheng 已提交
727 728
    if (pRow == NULL) break;

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

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

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

H
Hongze Cheng 已提交
771
  code = tsdbCommitDataBlock(pCommitter, pBlock);
H
Hongze Cheng 已提交
772 773 774 775 776
  if (code) goto _err;

  return code;

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

H
Hongze Cheng 已提交
781 782 783 784
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 已提交
785

H
Hongze Cheng 已提交
786 787
  if (pCommitter->dReader.pRowInfo && tTABLEIDCmprFn(pTbData, pCommitter->dReader.pRowInfo) == 0) {
    if (pCommitter->dReader.pRowInfo->suid) {  // super table
H
Hongze Cheng 已提交
788 789 790 791
      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 已提交
792
    } else {  // normal table
H
Hongze Cheng 已提交
793 794
      ASSERT(pCommitter->dReader.iRow == 0);
      nRow += pCommitter->dReader.bDatal.nRow;
H
Hongze Cheng 已提交
795 796 797 798 799
    }
  }

  if (nRow == 0) goto _exit;

H
Hongze Cheng 已提交
800
  TSDBROW *pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
801 802 803 804
  if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
    pRow = NULL;
  }

H
Hongze Cheng 已提交
805 806
  SRowInfo *pRowInfo = pCommitter->dReader.pRowInfo;
  if (pRowInfo && pRowInfo->uid != pTbData->uid) {
H
Hongze Cheng 已提交
807 808 809 810
    pRowInfo = NULL;
  }

  while (nRow) {
H
Hongze Cheng 已提交
811 812 813
    SBlockData *pBlockData;
    int8_t      toData;

H
Hongze Cheng 已提交
814
    if (nRow < pCommitter->minRow) {  // to .last
H
Hongze Cheng 已提交
815
      toData = 0;
H
Hongze Cheng 已提交
816 817
      pBlockData = &pCommitter->dWriter.bDatal;

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

H
Hongze Cheng 已提交
827 828 829 830 831 832
          tBlockDataReset(pBlockData);
        }
      }

      // set block data schema if need
      if (pBlockData->suid == 0 && pBlockData->uid == 0) {
H
Hongze Cheng 已提交
833 834
        code =
            tBlockDataInit(pBlockData, pTbData->suid, pTbData->suid ? 0 : pTbData->uid, pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
835 836 837 838 839 840 841 842
        if (code) goto _err;
      }

      if (pBlockData->nRow + nRow > pCommitter->maxRow) {
        code = tsdbCommitLastBlock(pCommitter);
        if (code) goto _err;
      }
    } else {  // to .data
H
Hongze Cheng 已提交
843
      toData = 1;
H
Hongze Cheng 已提交
844
      pBlockData = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
845
      ASSERT(pBlockData->nRow == 0);
H
Hongze Cheng 已提交
846 847 848 849 850 851 852 853
    }

    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 已提交
854
        code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema, pTbData->uid);
H
Hongze Cheng 已提交
855 856 857 858 859 860 861 862
        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 已提交
863
        code = tBlockDataAppendRow(pBlockData, &pRowInfo->row, NULL, pTbData->uid);
H
Hongze Cheng 已提交
864 865 866 867 868 869
        if (code) goto _err;

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

        pRowInfo = pCommitter->dReader.pRowInfo;
H
Hongze Cheng 已提交
870
        if (pRowInfo && pRowInfo->uid != pTbData->uid) {
H
Hongze Cheng 已提交
871 872 873 874 875 876 877
          pRowInfo = NULL;
        }
      } else {
        ASSERT(0);
      }

      nRow--;
H
Hongze Cheng 已提交
878 879 880
      if (toData) {
        if (nRow == 0 || pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
          code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
881 882 883 884 885 886 887 888 889 890
          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 已提交
891
      code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema, pTbData->uid);
H
Hongze Cheng 已提交
892 893 894 895 896 897 898 899
      if (code) goto _err;

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

H
Hongze Cheng 已提交
900
      nRow--;
H
Hongze Cheng 已提交
901 902 903
      if (toData) {
        if (nRow == 0 || pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
          code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
904 905
          if (code) goto _err;
          goto _outer_break;
H
Hongze Cheng 已提交
906 907
        }
      }
H
Hongze Cheng 已提交
908 909 910
    }

    while (pRowInfo) {
H
Hongze Cheng 已提交
911
      code = tBlockDataAppendRow(pBlockData, &pRowInfo->row, NULL, pTbData->uid);
H
Hongze Cheng 已提交
912 913 914 915
      if (code) goto _err;

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

      pRowInfo = pCommitter->dReader.pRowInfo;
H
Hongze Cheng 已提交
918
      if (pRowInfo && pRowInfo->uid != pTbData->uid) {
H
Hongze Cheng 已提交
919 920 921
        pRowInfo = NULL;
      }

H
Hongze Cheng 已提交
922
      nRow--;
H
Hongze Cheng 已提交
923 924 925
      if (toData) {
        if (nRow == 0 || pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
          code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
926 927 928
          if (code) goto _err;
          goto _outer_break;
        }
H
Hongze Cheng 已提交
929 930 931
      }
    }

H
Hongze Cheng 已提交
932 933
  _outer_break:
    ASSERT(nRow >= 0);
H
Hongze Cheng 已提交
934 935
  }

H
Hongze Cheng 已提交
936
_exit:
H
Hongze Cheng 已提交
937 938 939
  return code;

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

H
Hongze Cheng 已提交
944 945 946 947
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 已提交
948
  ASSERT(pCommitter->dReader.pRowInfo == NULL || tTABLEIDCmprFn(pCommitter->dReader.pRowInfo, pTbData) >= 0);
H
Hongze Cheng 已提交
949 950 951 952 953 954 955 956 957 958 959 960 961 962

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

  if (pRow == NULL) goto _exit;

H
Hongze Cheng 已提交
963 964 965
  int32_t iBlock = 0;
  SBlock  block;
  SBlock *pBlock = &block;
H
Hongze Cheng 已提交
966
  if (pCommitter->dReader.pBlockIdx && tTABLEIDCmprFn(pTbData, pCommitter->dReader.pBlockIdx) == 0) {
H
Hongze Cheng 已提交
967 968 969 970 971
    tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
  } else {
    pBlock = NULL;
  }

H
Hongze Cheng 已提交
972
  code = tsdbCommitterUpdateTableSchema(pCommitter, pTbData->suid, pTbData->uid);
H
Hongze Cheng 已提交
973 974
  if (code) goto _err;

H
Hongze Cheng 已提交
975
  tMapDataReset(&pCommitter->dWriter.mBlock);
H
Hongze Cheng 已提交
976 977
  code = tBlockDataInit(&pCommitter->dReader.bData, pTbData->suid, pTbData->uid, pCommitter->skmTable.pTSchema);
  if (code) goto _err;
H
Hongze Cheng 已提交
978
  code = tBlockDataInit(&pCommitter->dWriter.bData, pTbData->suid, pTbData->uid, pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
979
  if (code) goto _err;
H
Hongze Cheng 已提交
980 981

  // .data merge
H
Hongze Cheng 已提交
982 983 984 985 986 987 988 989 990 991 992 993 994
  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 已提交
995 996
    } else if (c > 0) {  // memory
      code = tsdbCommitTableMemData(pCommitter, pIter, pBlock->minKey);
H
Hongze Cheng 已提交
997 998 999 1000 1001 1002 1003 1004
      if (code) goto _err;

      // next
      pRow = tsdbTbDataIterGet(pIter);
      if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
        pRow = NULL;
      }
    } else {  // merge
H
Hongze Cheng 已提交
1005
      int32_t nOvlp = tsdbGetNumOfRowsLessThan(pIter, pBlock->maxKey);
H
Hongze Cheng 已提交
1006 1007 1008 1009 1010 1011 1012

      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 已提交
1013
        code = tsdbMergeCommitData(pCommitter, pIter, pBlock);
H
Hongze Cheng 已提交
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
        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 已提交
1035
    // next
H
Hongze Cheng 已提交
1036 1037 1038 1039 1040 1041 1042 1043
    iBlock++;
    if (iBlock < pCommitter->dReader.mBlock.nItem) {
      tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
    } else {
      pBlock = NULL;
    }
  }

H
Hongze Cheng 已提交
1044
  // .data append and .last merge
H
Hongze Cheng 已提交
1045 1046
  code = tsdbMergeCommitLast(pCommitter, pIter);
  if (code) goto _err;
H
Hongze Cheng 已提交
1047 1048 1049 1050

  // end
  if (pCommitter->dWriter.mBlock.nItem > 0) {
    SBlockIdx blockIdx = {.suid = pTbData->suid, .uid = pTbData->uid};
H
Hongze Cheng 已提交
1051
    code = tsdbWriteBlock(pCommitter->dWriter.pWriter, &pCommitter->dWriter.mBlock, &blockIdx);
H
Hongze Cheng 已提交
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
    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 已提交
1073 1074 1075
static int32_t tsdbCommitFileDataEnd(SCommitter *pCommitter) {
  int32_t code = 0;

H
Hongze Cheng 已提交
1076
  // write aBlockIdx
H
Hongze Cheng 已提交
1077
  code = tsdbWriteBlockIdx(pCommitter->dWriter.pWriter, pCommitter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
1078 1079
  if (code) goto _err;

H
Hongze Cheng 已提交
1080
  // write aBlockL
H
Hongze Cheng 已提交
1081
  code = tsdbWriteBlockL(pCommitter->dWriter.pWriter, pCommitter->dWriter.aBlockL);
H
Hongze Cheng 已提交
1082 1083
  if (code) goto _err;

H
Hongze Cheng 已提交
1084
  // update file header
H
Hongze Cheng 已提交
1085
  code = tsdbUpdateDFileSetHeader(pCommitter->dWriter.pWriter);
H
Hongze Cheng 已提交
1086 1087 1088
  if (code) goto _err;

  // upsert SDFileSet
H
Hongze Cheng 已提交
1089
  code = tsdbFSUpsertFSet(&pCommitter->fs, &pCommitter->dWriter.pWriter->wSet);
H
Hongze Cheng 已提交
1090 1091 1092
  if (code) goto _err;

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

H
Hongze Cheng 已提交
1096 1097
  if (pCommitter->dReader.pReader) {
    code = tsdbDataFReaderClose(&pCommitter->dReader.pReader);
H
Hongze Cheng 已提交
1098
    if (code) goto _err;
H
Hongze Cheng 已提交
1099 1100 1101 1102 1103 1104
  }

_exit:
  return code;

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

H
Hongze Cheng 已提交
1109 1110 1111
static int32_t tsdbMoveCommitData(SCommitter *pCommitter, TABLEID toTable) {
  int32_t code = 0;

H
Hongze Cheng 已提交
1112
  // .data
H
Hongze Cheng 已提交
1113 1114 1115 1116
  while (true) {
    if (pCommitter->dReader.pBlockIdx == NULL || tTABLEIDCmprFn(pCommitter->dReader.pBlockIdx, &toTable) >= 0) break;

    SBlockIdx blockIdx = *pCommitter->dReader.pBlockIdx;
H
Hongze Cheng 已提交
1117
    code = tsdbWriteBlock(pCommitter->dWriter.pWriter, &pCommitter->dReader.mBlock, &blockIdx);
H
Hongze Cheng 已提交
1118 1119 1120 1121 1122 1123 1124
    if (code) goto _err;

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

H
Hongze Cheng 已提交
1125 1126
    code = tsdbCommitterNextTableData(pCommitter);
    if (code) goto _err;
H
Hongze Cheng 已提交
1127 1128
  }

H
Hongze Cheng 已提交
1129
  // .last
H
Hongze Cheng 已提交
1130
  while (true) {
H
Hongze Cheng 已提交
1131
    if (pCommitter->dReader.pRowInfo == NULL || tTABLEIDCmprFn(pCommitter->dReader.pRowInfo, &toTable) >= 0) break;
H
Hongze Cheng 已提交
1132

H
Hongze Cheng 已提交
1133 1134 1135 1136 1137 1138 1139 1140 1141
    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 已提交
1142 1143 1144 1145 1146 1147
    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 已提交
1148
        tBlockDataReset(pBlockDataW);
H
Hongze Cheng 已提交
1149
      }
H
Hongze Cheng 已提交
1150 1151
    }

H
Hongze Cheng 已提交
1152 1153
    // set block data schema if need
    if (pBlockDataW->suid == 0 && pBlockDataW->uid == 0) {
H
Hongze Cheng 已提交
1154
      code = tsdbCommitterUpdateTableSchema(pCommitter, suid, uid);
H
Hongze Cheng 已提交
1155 1156
      if (code) goto _err;

H
Hongze Cheng 已提交
1157
      code = tBlockDataInit(pBlockDataW, suid, suid ? 0 : uid, pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
1158
      if (code) goto _err;
H
Hongze Cheng 已提交
1159 1160
    }

H
Hongze Cheng 已提交
1161 1162
    // check if it can make sure that one table data in one block
    int32_t nRow = 0;
H
Hongze Cheng 已提交
1163
    if (pBlockDataR->suid) {
H
Hongze Cheng 已提交
1164 1165
      int32_t iRow = pCommitter->dReader.iRow;
      while ((iRow < pBlockDataR->nRow) && (pBlockDataR->aUid[iRow] == uid)) {
H
Hongze Cheng 已提交
1166
        nRow++;
H
Hongze Cheng 已提交
1167
        iRow++;
H
Hongze Cheng 已提交
1168 1169 1170 1171
      }
    } else {
      ASSERT(pCommitter->dReader.iRow == 0);
      nRow = pBlockDataR->nRow;
H
Hongze Cheng 已提交
1172
    }
H
Hongze Cheng 已提交
1173

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

H
Hongze Cheng 已提交
1176 1177
    if (pBlockDataW->nRow + nRow > pCommitter->maxRow) {
      ASSERT(pBlockDataW->nRow > 0);
H
Hongze Cheng 已提交
1178

H
Hongze Cheng 已提交
1179 1180
      code = tsdbCommitLastBlock(pCommitter);
      if (code) goto _err;
H
Hongze Cheng 已提交
1181 1182
    }

H
Hongze Cheng 已提交
1183
    while (nRow > 0) {
H
Hongze Cheng 已提交
1184
      code = tBlockDataAppendRow(pBlockDataW, &pCommitter->dReader.pRowInfo->row, NULL, uid);
H
Hongze Cheng 已提交
1185 1186
      if (code) goto _err;

H
Hongze Cheng 已提交
1187
      code = tsdbCommitterNextLastRow(pCommitter);
H
Hongze Cheng 已提交
1188
      if (code) goto _err;
H
Hongze Cheng 已提交
1189 1190

      nRow--;
H
Hongze Cheng 已提交
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
    }
  }

  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 已提交
1201
static int32_t tsdbCommitFileData(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1202 1203 1204
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1205 1206 1207 1208 1209 1210

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

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

H
Hongze Cheng 已提交
1214
    // move commit until current (suid, uid)
H
Hongze Cheng 已提交
1215
    code = tsdbMoveCommitData(pCommitter, *(TABLEID *)pTbData);
H
Hongze Cheng 已提交
1216 1217
    if (code) goto _err;

H
Hongze Cheng 已提交
1218
    // commit current table data
H
Hongze Cheng 已提交
1219
    code = tsdbCommitTableData(pCommitter, pTbData);
H
Hongze Cheng 已提交
1220
    if (code) goto _err;
H
Hongze Cheng 已提交
1221 1222 1223 1224 1225 1226

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

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

H
Hongze Cheng 已提交
1232
  if (pCommitter->dWriter.bDatal.nRow > 0) {
H
Hongze Cheng 已提交
1233
    code = tsdbCommitLastBlock(pCommitter);
H
Hongze Cheng 已提交
1234
    if (code) goto _err;
H
Hongze Cheng 已提交
1235 1236
  }

H
Hongze Cheng 已提交
1237 1238
  // commit file data end
  code = tsdbCommitFileDataEnd(pCommitter);
H
Hongze Cheng 已提交
1239
  if (code) goto _err;
H
Hongze Cheng 已提交
1240 1241 1242 1243

  return code;

_err:
S
Shengliang Guan 已提交
1244
  tsdbError("vgId:%d, commit file data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1245 1246
  tsdbDataFReaderClose(&pCommitter->dReader.pReader);
  tsdbDataFWriterClose(&pCommitter->dWriter.pWriter, 0);
H
Hongze Cheng 已提交
1247 1248 1249
  return code;
}

H
Hongze Cheng 已提交
1250 1251
// ----------------------------------------------------------------------------
static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1252
  int32_t code = 0;
H
Hongze Cheng 已提交
1253

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

H
more  
Hongze Cheng 已提交
1257
  taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1258 1259
  pTsdb->imem = pTsdb->mem;
  pTsdb->mem = NULL;
H
more  
Hongze Cheng 已提交
1260
  taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1261

H
Hongze Cheng 已提交
1262
  pCommitter->pTsdb = pTsdb;
H
Hongze Cheng 已提交
1263
  pCommitter->commitID = pTsdb->pVnode->state.commitID;
H
Hongze Cheng 已提交
1264 1265 1266 1267
  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 已提交
1268
  pCommitter->cmprAlg = pTsdb->pVnode->config.tsdbCfg.compression;
H
Hongze Cheng 已提交
1269
  pCommitter->maxLast = TSDB_DEFAULT_LAST_FILE;  // TODO: make it as a config
H
Hongze Cheng 已提交
1270 1271 1272 1273 1274
  pCommitter->aTbDataP = tsdbMemTableGetTbDataArray(pTsdb->imem);
  if (pCommitter->aTbDataP == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
1275
  code = tsdbFSCopy(pTsdb, &pCommitter->fs);
H
Hongze Cheng 已提交
1276 1277 1278 1279 1280
  if (code) goto _err;

  return code;

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

H
Hongze Cheng 已提交
1285 1286 1287
static int32_t tsdbCommitDataStart(SCommitter *pCommitter) {
  int32_t code = 0;

H
Hongze Cheng 已提交
1288
  // Reader
H
Hongze Cheng 已提交
1289 1290
  pCommitter->dReader.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pCommitter->dReader.aBlockIdx == NULL) {
H
Hongze Cheng 已提交
1291 1292 1293 1294
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
1295
  code = tBlockDataCreate(&pCommitter->dReader.bData);
H
Hongze Cheng 已提交
1296 1297
  if (code) goto _exit;

H
Hongze Cheng 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
  // 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 已提交
1311
  code = tBlockDataCreate(&pCommitter->dWriter.bData);
H
Hongze Cheng 已提交
1312
  if (code) goto _exit;
H
Hongze Cheng 已提交
1313

H
Hongze Cheng 已提交
1314
  code = tBlockDataCreate(&pCommitter->dWriter.bDatal);
H
Hongze Cheng 已提交
1315 1316
  if (code) goto _exit;

H
Hongze Cheng 已提交
1317 1318 1319 1320 1321
_exit:
  return code;
}

static void tsdbCommitDataEnd(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1322
  // Reader
H
Hongze Cheng 已提交
1323 1324
  taosArrayDestroy(pCommitter->dReader.aBlockIdx);
  tMapDataClear(&pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
1325
  tBlockDataDestroy(&pCommitter->dReader.bData, 1);
H
Hongze Cheng 已提交
1326 1327

  // Writer
H
Hongze Cheng 已提交
1328
  taosArrayDestroy(pCommitter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
1329
  taosArrayDestroy(pCommitter->dWriter.aBlockL);
H
Hongze Cheng 已提交
1330
  tMapDataClear(&pCommitter->dWriter.mBlock);
H
Hongze Cheng 已提交
1331 1332
  tBlockDataDestroy(&pCommitter->dWriter.bData, 1);
  tBlockDataDestroy(&pCommitter->dWriter.bDatal, 1);
H
Hongze Cheng 已提交
1333 1334
  tTSchemaDestroy(pCommitter->skmTable.pTSchema);
  tTSchemaDestroy(pCommitter->skmRow.pTSchema);
H
Hongze Cheng 已提交
1335 1336
}

H
Hongze Cheng 已提交
1337 1338 1339 1340
static int32_t tsdbCommitData(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1341

H
Hongze Cheng 已提交
1342
  // check
H
Hongze Cheng 已提交
1343
  if (pMemTable->nRow == 0) goto _exit;
H
Hongze Cheng 已提交
1344

H
Hongze Cheng 已提交
1345 1346
  // start ====================
  code = tsdbCommitDataStart(pCommitter);
H
Hongze Cheng 已提交
1347
  if (code) goto _err;
H
Hongze Cheng 已提交
1348 1349 1350

  // impl ====================
  pCommitter->nextKey = pMemTable->minKey;
H
Hongze Cheng 已提交
1351 1352
  while (pCommitter->nextKey < TSKEY_MAX) {
    code = tsdbCommitFileData(pCommitter);
H
Hongze Cheng 已提交
1353
    if (code) goto _err;
H
Hongze Cheng 已提交
1354
  }
H
Hongze Cheng 已提交
1355

H
Hongze Cheng 已提交
1356 1357 1358
  // end ====================
  tsdbCommitDataEnd(pCommitter);

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

H
Hongze Cheng 已提交
1363
_err:
H
Hongze Cheng 已提交
1364
  tsdbCommitDataEnd(pCommitter);
S
Shengliang Guan 已提交
1365
  tsdbError("vgId:%d, commit data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1366 1367
  return code;
}
H
Hongze Cheng 已提交
1368

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

H
Hongze Cheng 已提交
1374 1375
  if (pMemTable->nDel == 0) {
    goto _exit;
H
Hongze Cheng 已提交
1376
  }
H
Hongze Cheng 已提交
1377

H
Hongze Cheng 已提交
1378 1379 1380 1381 1382
  // start
  code = tsdbCommitDelStart(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1383

H
Hongze Cheng 已提交
1384
  // impl
H
Hongze Cheng 已提交
1385 1386 1387
  int32_t  iDelIdx = 0;
  int32_t  nDelIdx = taosArrayGetSize(pCommitter->aDelIdx);
  int32_t  iTbData = 0;
H
Hongze Cheng 已提交
1388
  int32_t  nTbData = taosArrayGetSize(pCommitter->aTbDataP);
H
Hongze Cheng 已提交
1389 1390 1391 1392 1393
  STbData *pTbData;
  SDelIdx *pDelIdx;

  ASSERT(nTbData > 0);

H
Hongze Cheng 已提交
1394
  pTbData = (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData);
H
Hongze Cheng 已提交
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
  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 已提交
1420
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData) : NULL;
H
Hongze Cheng 已提交
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
    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 已提交
1436
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData) : NULL;
H
Hongze Cheng 已提交
1437 1438 1439
    iDelIdx++;
    pDelIdx = (iDelIdx < nDelIdx) ? (SDelIdx *)taosArrayGet(pCommitter->aDelIdx, iDelIdx) : NULL;
    continue;
H
Hongze Cheng 已提交
1440
  }
H
Hongze Cheng 已提交
1441

H
Hongze Cheng 已提交
1442 1443 1444 1445 1446
  // end
  code = tsdbCommitDelEnd(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1447

H
Hongze Cheng 已提交
1448
_exit:
S
Shengliang Guan 已提交
1449
  tsdbDebug("vgId:%d, commit del done, nDel:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nDel);
H
Hongze Cheng 已提交
1450 1451 1452
  return code;

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

H
Hongze Cheng 已提交
1457
static int32_t tsdbEndCommit(SCommitter *pCommitter, int32_t eno) {
H
Hongze Cheng 已提交
1458 1459 1460 1461
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

H
Hongze Cheng 已提交
1462
  ASSERT(eno == 0);
H
Hongze Cheng 已提交
1463 1464 1465

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

H
Hongze Cheng 已提交
1467
  // lock
H
Hongze Cheng 已提交
1468
  taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1469 1470 1471 1472 1473 1474 1475 1476

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

H
Hongze Cheng 已提交
1477
  pTsdb->imem = NULL;
H
Hongze Cheng 已提交
1478 1479

  // unlock
H
Hongze Cheng 已提交
1480 1481 1482
  taosThreadRwlockUnlock(&pTsdb->rwLock);

  tsdbUnrefMemTable(pMemTable);
H
Hongze Cheng 已提交
1483
  tsdbFSDestroy(&pCommitter->fs);
H
Hongze Cheng 已提交
1484
  taosArrayDestroy(pCommitter->aTbDataP);
H
Hongze Cheng 已提交
1485

S
Shengliang Guan 已提交
1486
  tsdbInfo("vgId:%d, tsdb end commit", TD_VID(pTsdb->pVnode));
H
Hongze Cheng 已提交
1487 1488 1489
  return code;

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