tsdbCommit.c 33.7 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

H
Hongze Cheng 已提交
16
#include "tsdb.h"
H
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18
typedef struct {
H
Hongze Cheng 已提交
19
  STsdb *pTsdb;
H
Hongze Cheng 已提交
20
  /* commit data */
H
Hongze Cheng 已提交
21
  int64_t commitID;
H
Hongze Cheng 已提交
22 23
  int32_t minutes;
  int8_t  precision;
H
Hongze Cheng 已提交
24 25
  int32_t minRow;
  int32_t maxRow;
H
Hongze Cheng 已提交
26
  int8_t  cmprAlg;
H
Hongze Cheng 已提交
27
  // --------------
H
Hongze Cheng 已提交
28
  TSKEY   nextKey;  // reset by each table commit
H
Hongze Cheng 已提交
29 30 31
  int32_t commitFid;
  TSKEY   minKey;
  TSKEY   maxKey;
H
Hongze Cheng 已提交
32
  // commit file data
H
Hongze Cheng 已提交
33
  SDataFReader *pReader;
H
Hongze Cheng 已提交
34 35 36 37
  SMapData      oBlockIdxMap;  // SMapData<SBlockIdx>, read from reader
  SMapData      oBlockMap;     // SMapData<SBlock>, read from reader
  SBlock        oBlock;
  SBlockData    oBlockData;
H
Hongze Cheng 已提交
38
  SDataFWriter *pWriter;
H
Hongze Cheng 已提交
39 40 41 42
  SMapData      nBlockIdxMap;  // SMapData<SBlockIdx>, build by committer
  SMapData      nBlockMap;     // SMapData<SBlock>
  SBlock        nBlock;
  SBlockData    nBlockData;
H
Hongze Cheng 已提交
43 44 45
  int64_t       suid;
  int64_t       uid;
  STSchema     *pTSchema;
H
Hongze Cheng 已提交
46
  /* commit del */
H
Hongze Cheng 已提交
47
  SDelFReader *pDelFReader;
H
Hongze Cheng 已提交
48 49
  SMapData     oDelIdxMap;   // SMapData<SDelIdx>, old
  SMapData     oDelDataMap;  // SMapData<SDelData>, old
H
Hongze Cheng 已提交
50
  SDelFWriter *pDelFWriter;
H
Hongze Cheng 已提交
51 52
  SMapData     nDelIdxMap;   // SMapData<SDelIdx>, new
  SMapData     nDelDataMap;  // SMapData<SDelData>, new
H
Hongze Cheng 已提交
53 54
  SArray      *aDelIdx;
  SArray      *aDelData;
H
Hongze Cheng 已提交
55
} SCommitter;
H
refact  
Hongze Cheng 已提交
56

H
Hongze Cheng 已提交
57 58 59 60 61
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 已提交
62

H
refact  
Hongze Cheng 已提交
63
int32_t tsdbBegin(STsdb *pTsdb) {
H
Hongze Cheng 已提交
64
  int32_t code = 0;
H
Hongze Cheng 已提交
65

66 67
  if (!pTsdb) return code;

H
Hongze Cheng 已提交
68
  code = tsdbMemTableCreate(pTsdb, &pTsdb->mem);
H
Hongze Cheng 已提交
69
  if (code) goto _err;
H
Hongze Cheng 已提交
70

H
Hongze Cheng 已提交
71 72 73
  return code;

_err:
H
Hongze Cheng 已提交
74
  tsdbError("vgId:%d tsdb begin failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
75
  return code;
H
Hongze Cheng 已提交
76 77
}

H
more  
Hongze Cheng 已提交
78
int32_t tsdbCommit(STsdb *pTsdb) {
79
  if (!pTsdb) return 0;
H
Hongze Cheng 已提交
80

H
more  
Hongze Cheng 已提交
81
  int32_t    code = 0;
H
Hongze Cheng 已提交
82 83 84 85
  SCommitter commith;
  SMemTable *pMemTable = pTsdb->mem;

  // check
H
Hongze Cheng 已提交
86 87
  if (pMemTable->nRow == 0 && pMemTable->nDel == 0) {
    // TODO: lock?
H
Hongze Cheng 已提交
88 89 90 91
    pTsdb->mem = NULL;
    tsdbMemTableDestroy(pMemTable);
    goto _exit;
  }
H
refact  
Hongze Cheng 已提交
92

H
more  
Hongze Cheng 已提交
93
  // start commit
H
more  
Hongze Cheng 已提交
94
  code = tsdbStartCommit(pTsdb, &commith);
H
Hongze Cheng 已提交
95
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
96

H
refact  
Hongze Cheng 已提交
97 98
  // commit impl
  code = tsdbCommitData(&commith);
H
Hongze Cheng 已提交
99
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
100 101

  code = tsdbCommitDel(&commith);
H
Hongze Cheng 已提交
102
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
103 104

  code = tsdbCommitCache(&commith);
H
Hongze Cheng 已提交
105
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
106 107

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

H
Hongze Cheng 已提交
111
_exit:
H
refact  
Hongze Cheng 已提交
112 113 114
  return code;

_err:
H
Hongze Cheng 已提交
115
  tsdbEndCommit(&commith, code);
C
Cary Xu 已提交
116
  tsdbError("vgId:%d, failed to commit since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
refact  
Hongze Cheng 已提交
117 118 119
  return code;
}

H
Hongze Cheng 已提交
120
static int32_t tsdbCommitDelStart(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
121 122 123 124 125 126
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
  SDelFile  *pDelFileR = NULL;  // TODO
  SDelFile  *pDelFileW = NULL;  // TODO

H
Hongze Cheng 已提交
127 128 129
  tMapDataReset(&pCommitter->oDelIdxMap);
  tMapDataReset(&pCommitter->nDelIdxMap);

H
Hongze Cheng 已提交
130
  // load old
H
Hongze Cheng 已提交
131
  if (pDelFileR) {
H
Hongze Cheng 已提交
132
    code = tsdbDelFReaderOpen(&pCommitter->pDelFReader, pDelFileR, pTsdb, NULL);
H
Hongze Cheng 已提交
133
    if (code) goto _err;
H
Hongze Cheng 已提交
134

H
Hongze Cheng 已提交
135 136
    code = tsdbReadDelIdx(pCommitter->pDelFReader, &pCommitter->oDelIdxMap, NULL);
    if (code) goto _err;
H
Hongze Cheng 已提交
137 138
  }

H
Hongze Cheng 已提交
139
  // prepare new
H
Hongze Cheng 已提交
140
  code = tsdbDelFWriterOpen(&pCommitter->pDelFWriter, pDelFileW, pTsdb);
H
Hongze Cheng 已提交
141
  if (code) goto _err;
H
Hongze Cheng 已提交
142 143 144 145 146 147 148

_exit:
  tsdbDebug("vgId:%d commit del start", TD_VID(pTsdb->pVnode));
  return code;

_err:
  tsdbError("vgId:%d commit del start failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
149 150 151
  return code;
}

H
Hongze Cheng 已提交
152
static int32_t tsdbCommitTableDel(SCommitter *pCommitter, STbData *pTbData, SDelIdx *pDelIdx) {
H
Hongze Cheng 已提交
153
  int32_t   code = 0;
H
Hongze Cheng 已提交
154
  SDelData *pDelData = &(SDelData){};
H
Hongze Cheng 已提交
155 156 157
  tb_uid_t  suid;
  tb_uid_t  uid;
  SDelIdx   delIdx;  // TODO
H
Hongze Cheng 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

  // check no del data, just return
  if (pTbData && pTbData->pHead == NULL) {
    pTbData = NULL;
  }
  if (pTbData == NULL && pDelIdx == NULL) goto _exit;

  // prepare
  if (pTbData) {
    delIdx.suid = pTbData->suid;
    delIdx.uid = pTbData->uid;
  } else {
    delIdx.suid = pDelIdx->suid;
    delIdx.uid = pDelIdx->uid;
  }

  // start
  tMapDataReset(&pCommitter->oDelDataMap);
  tMapDataReset(&pCommitter->nDelDataMap);

  if (pDelIdx) {
    code = tsdbReadDelData(pCommitter->pDelFReader, pDelIdx, &pCommitter->oDelDataMap, NULL);
    if (code) goto _err;
  }

  // disk
  for (int32_t iDelData = 0; iDelData < pCommitter->oDelDataMap.nItem; iDelData++) {
H
Hongze Cheng 已提交
185
    code = tMapDataGetItemByIdx(&pCommitter->oDelDataMap, iDelData, pDelData, tGetDelData);
H
Hongze Cheng 已提交
186 187
    if (code) goto _err;

H
Hongze Cheng 已提交
188
    code = tMapDataPutItem(&pCommitter->nDelDataMap, pDelData, tPutDelData);
H
Hongze Cheng 已提交
189 190 191 192
    if (code) goto _err;
  }

  // memory
H
Hongze Cheng 已提交
193 194 195
  pDelData = pTbData ? pTbData->pHead : NULL;
  for (; pDelData; pDelData = pDelData->pNext) {
    code = tMapDataPutItem(&pCommitter->nDelDataMap, pDelData, tPutDelData);
H
Hongze Cheng 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    if (code) goto _err;
  }

  ASSERT(pCommitter->nDelDataMap.nItem > 0);

  // write
  code = tsdbWriteDelData(pCommitter->pDelFWriter, &pCommitter->nDelDataMap, NULL, &delIdx);
  if (code) goto _err;

  // put delIdx
  code = tMapDataPutItem(&pCommitter->nDelIdxMap, &delIdx, tPutDelIdx);
  if (code) goto _err;

_exit:
  return code;

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

H
Hongze Cheng 已提交
217
static int32_t tsdbCommitDelImpl(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
  int32_t    iDelIdx = 0;
  int32_t    nDelIdx = pCommitter->oDelIdxMap.nItem;
  int32_t    iTbData = 0;
  int32_t    nTbData = taosArrayGetSize(pMemTable->aTbData);
  STbData   *pTbData;
  SDelIdx   *pDelIdx;
  SDelIdx    delIdx;
  int32_t    c;

  ASSERT(nTbData > 0);
H
Hongze Cheng 已提交
231

H
Hongze Cheng 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245
  pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
  if (iDelIdx < nDelIdx) {
    code = tMapDataGetItemByIdx(&pCommitter->oDelIdxMap, iDelIdx, &delIdx, tGetDelIdx);
    if (code) goto _err;
    pDelIdx = &delIdx;
  } else {
    pDelIdx = NULL;
  }

  while (true) {
    if (pTbData == NULL && pDelIdx == NULL) break;

    if (pTbData && pDelIdx) {
      c = tTABLEIDCmprFn(pTbData, pDelIdx);
H
Hongze Cheng 已提交
246
      if (c == 0) {
H
Hongze Cheng 已提交
247
        goto _commit_mem_and_disk_del;
H
Hongze Cheng 已提交
248
      } else if (c < 0) {
H
Hongze Cheng 已提交
249
        goto _commit_mem_del;
H
Hongze Cheng 已提交
250
      } else {
H
Hongze Cheng 已提交
251
        goto _commit_disk_del;
H
Hongze Cheng 已提交
252
      }
H
Hongze Cheng 已提交
253
    } else {
H
Hongze Cheng 已提交
254 255
      if (pTbData) goto _commit_mem_del;
      if (pDelIdx) goto _commit_disk_del;
H
Hongze Cheng 已提交
256 257
    }

H
Hongze Cheng 已提交
258 259 260 261 262 263 264
  _commit_mem_del:
    code = tsdbCommitTableDel(pCommitter, pTbData, NULL);
    if (code) goto _err;
    iTbData++;
    if (iTbData < nTbData) {
      pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
    } else {
H
Hongze Cheng 已提交
265 266
      pTbData = NULL;
    }
H
Hongze Cheng 已提交
267
    continue;
H
Hongze Cheng 已提交
268

H
Hongze Cheng 已提交
269 270 271 272 273 274 275 276 277 278 279 280
  _commit_disk_del:
    code = tsdbCommitTableDel(pCommitter, NULL, pDelIdx);
    if (code) goto _err;
    iDelIdx++;
    if (iDelIdx < nDelIdx) {
      code = tMapDataGetItemByIdx(&pCommitter->oDelIdxMap, iDelIdx, &delIdx, tGetDelIdx);
      if (code) goto _err;
      pDelIdx = &delIdx;
    } else {
      pDelIdx = NULL;
    }
    continue;
H
Hongze Cheng 已提交
281

H
Hongze Cheng 已提交
282 283
  _commit_mem_and_disk_del:
    code = tsdbCommitTableDel(pCommitter, pTbData, pDelIdx);
H
Hongze Cheng 已提交
284
    if (code) goto _err;
H
Hongze Cheng 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    iTbData++;
    iDelIdx++;
    if (iTbData < nTbData) {
      pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
    } else {
      pTbData = NULL;
    }
    if (iDelIdx < nDelIdx) {
      code = tMapDataGetItemByIdx(&pCommitter->oDelIdxMap, iDelIdx, &delIdx, tGetDelIdx);
      if (code) goto _err;
      pDelIdx = &delIdx;
    } else {
      pDelIdx = NULL;
    }
    continue;
H
Hongze Cheng 已提交
300 301
  }

H
Hongze Cheng 已提交
302
  return code;
H
Hongze Cheng 已提交
303 304 305 306

_err:
  tsdbError("vgId:%d commit del impl failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  return code;
H
Hongze Cheng 已提交
307 308 309 310
}

static int32_t tsdbCommitDelEnd(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
311

H
Hongze Cheng 已提交
312 313
  code = tsdbWriteDelIdx(pCommitter->pDelFWriter, &pCommitter->nDelIdxMap, NULL);
  if (code) goto _err;
H
Hongze Cheng 已提交
314

H
Hongze Cheng 已提交
315
  code = tsdbUpdateDelFileHdr(pCommitter->pDelFWriter, NULL);
H
Hongze Cheng 已提交
316
  if (code) goto _err;
H
Hongze Cheng 已提交
317 318

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

  if (pCommitter->pDelFReader) {
    code = tsdbDelFReaderClose(pCommitter->pDelFReader);
    if (code) goto _err;
  }

H
Hongze Cheng 已提交
326 327 328
  return code;

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

H
Hongze Cheng 已提交
333 334 335 336 337
static int32_t tsdbCommitFileDataStart(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SDFileSet *pRSet = NULL;
  SDFileSet  wSet;
H
Hongze Cheng 已提交
338

H
Hongze Cheng 已提交
339 340
  // memory
  pCommitter->nextKey = TSKEY_MAX;
H
Hongze Cheng 已提交
341

H
Hongze Cheng 已提交
342 343 344 345 346 347 348 349
  // old
  tMapDataReset(&pCommitter->oBlockIdxMap);
  tMapDataReset(&pCommitter->oBlockMap);
  tBlockReset(&pCommitter->oBlock);
  tBlockDataReset(&pCommitter->oBlockData);
  pRSet = tsdbFSStateGetDFileSet(pTsdb->fs->nState, pCommitter->commitFid);
  if (pRSet) {
    code = tsdbDataFReaderOpen(&pCommitter->pReader, pTsdb, pRSet);
H
Hongze Cheng 已提交
350 351
    if (code) goto _err;

H
Hongze Cheng 已提交
352
    code = tsdbReadBlockIdx(pCommitter->pReader, &pCommitter->oBlockIdxMap, NULL);
H
Hongze Cheng 已提交
353
    if (code) goto _err;
H
Hongze Cheng 已提交
354
  }
H
Hongze Cheng 已提交
355

H
Hongze Cheng 已提交
356 357 358 359
  // new
  tMapDataReset(&pCommitter->nBlockIdxMap);
  tMapDataReset(&pCommitter->nBlockMap);
  tBlockReset(&pCommitter->nBlock);
H
Hongze Cheng 已提交
360
  tBlockDataReset(&pCommitter->nBlockData);
H
Hongze Cheng 已提交
361 362 363 364 365 366 367 368 369 370
  if (pRSet) {
    wSet = (SDFileSet){.diskId = pRSet->diskId,
                       .fid = pCommitter->commitFid,
                       .fHead = {.commitID = pCommitter->commitID, .offset = 0, .size = 0},
                       .fData = pRSet->fData,
                       .fLast = {.commitID = pCommitter->commitID, .size = 0},
                       .fSma = pRSet->fSma};
  } else {
    STfs   *pTfs = pTsdb->pVnode->pTfs;
    SDiskID did = {.level = 0, .id = 0};
H
Hongze Cheng 已提交
371

H
Hongze Cheng 已提交
372 373
    // TODO: alloc a new disk
    // tfsAllocDisk(pTfs, 0, &did);
H
Hongze Cheng 已提交
374

H
Hongze Cheng 已提交
375 376
    // create the directory
    tfsMkdirRecurAt(pTfs, pTsdb->path, did);
H
Hongze Cheng 已提交
377

H
Hongze Cheng 已提交
378 379 380 381 382 383
    wSet = (SDFileSet){.diskId = did,
                       .fid = pCommitter->commitFid,
                       .fHead = {.commitID = pCommitter->commitID, .offset = 0, .size = 0},
                       .fData = {.commitID = pCommitter->commitID, .size = 0},
                       .fLast = {.commitID = pCommitter->commitID, .size = 0},
                       .fSma = {.commitID = pCommitter->commitID, .size = 0}};
H
Hongze Cheng 已提交
384
  }
H
Hongze Cheng 已提交
385 386
  code = tsdbDataFWriterOpen(&pCommitter->pWriter, pTsdb, &wSet);
  if (code) goto _err;
H
Hongze Cheng 已提交
387

H
Hongze Cheng 已提交
388
_exit:
H
Hongze Cheng 已提交
389 390 391
  return code;

_err:
H
Hongze Cheng 已提交
392
  tsdbError("vgId:%d commit file data start failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
393
  return code;
H
Hongze Cheng 已提交
394 395
}

H
Hongze Cheng 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
static int32_t tsdbCommitterUpdateSchema(SCommitter *pCommitter, int64_t suid, int64_t uid, int32_t sver) {
  int32_t code = 0;

  if (pCommitter->pTSchema) {
    if (pCommitter->suid == suid) {
      if (suid == 0) {
        if (pCommitter->uid == uid && sver == pCommitter->pTSchema->version) goto _exit;
      } else {
        if (sver == pCommitter->pTSchema->version) goto _exit;
      }
    }
  }

_update_schema:
  pCommitter->suid = suid;
  pCommitter->uid = uid;
  tTSchemaDestroy(pCommitter->pTSchema);
  pCommitter->pTSchema = metaGetTbTSchema(pCommitter->pTsdb->pVnode->pMeta, uid, sver);
  if (pCommitter->pTSchema == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
422 423
static int32_t tsdbMergeTableData(SCommitter *pCommitter, STbDataIter *pIter, SBlock *pBlockMerge, TSDBKEY toKey,
                                  int8_t toDataOnly) {
H
Hongze Cheng 已提交
424
  int32_t     code = 0;
H
Hongze Cheng 已提交
425 426 427 428
  SBlockIdx  *pBlockIdx = &(SBlockIdx){.suid = pIter->pTbData->suid, .uid = pIter->pTbData->uid};
  SBlockData *pBlockDataMerge = &pCommitter->oBlockData;
  SBlockData *pBlockData = &pCommitter->nBlockData;
  SBlock     *pBlock = &pCommitter->nBlock;
H
Hongze Cheng 已提交
429
  TSDBROW    *pRow1;
H
Hongze Cheng 已提交
430 431 432 433
  TSDBROW     row2;
  TSDBROW    *pRow2 = &row2;
  TSDBROW     row;
  TSDBROW    *pRow = &row;
H
Hongze Cheng 已提交
434
  int32_t     c = 0;
H
Hongze Cheng 已提交
435
  TSKEY       lastKey;
H
Hongze Cheng 已提交
436 437

  // read SBlockData
H
Hongze Cheng 已提交
438
  code = tsdbReadBlockData(pCommitter->pReader, pBlockIdx, pBlockMerge, pBlockDataMerge, NULL, NULL);
H
Hongze Cheng 已提交
439 440 441 442
  if (code) goto _err;

  // loop to merge
  pRow1 = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
443
  *pRow2 = tsdbRowFromBlockData(pBlockDataMerge, 0);
H
Hongze Cheng 已提交
444
  ASSERT(pRow1 && tsdbKeyCmprFn(&TSDBROW_KEY(pRow1), &toKey) < 0);
H
Hongze Cheng 已提交
445 446 447 448
  ASSERT(tsdbKeyCmprFn(&TSDBROW_KEY(pRow2), &toKey) < 0);
  code = tsdbCommitterUpdateSchema(pCommitter, pBlockIdx->suid, pBlockIdx->uid, TSDBROW_SVERSION(pRow1));
  if (code) goto _err;

H
Hongze Cheng 已提交
449 450 451
  lastKey = TSKEY_MIN;
  tBlockReset(pBlock);
  tBlockDataReset(pBlockData);
H
Hongze Cheng 已提交
452 453
  while (true) {
    if (pRow1 == NULL && pRow2 == NULL) {
H
Hongze Cheng 已提交
454
      if (pBlockData->nRow == 0) {
H
Hongze Cheng 已提交
455 456 457 458 459 460 461
        break;
      } else {
        goto _write_block;
      }
    }

    if (pRow1 && pRow2) {
H
Hongze Cheng 已提交
462 463
      if (tsdbRowCmprFn(pRow1, pRow2) < 0) {
        *pRow = *pRow1;
H
Hongze Cheng 已提交
464 465 466 467

        tsdbTbDataIterNext(pIter);
        pRow1 = tsdbTbDataIterGet(pIter);

H
Hongze Cheng 已提交
468 469 470 471 472 473 474 475
        if (pRow1) {
          if (tsdbKeyCmprFn(&TSDBROW_KEY(pRow1), &toKey) < 0) {
            code = tsdbCommitterUpdateSchema(pCommitter, pBlockIdx->suid, pBlockIdx->uid, TSDBROW_VERSION(pRow1));
            if (code) goto _err;
          } else {
            pRow1 = NULL;
          }
        }
H
Hongze Cheng 已提交
476
      } else if (tsdbRowCmprFn(pRow1, pRow2) > 0) {
H
Hongze Cheng 已提交
477 478 479 480 481 482 483
        *pRow = *pRow2;

        if (pRow2->iRow + 1 < pBlockDataMerge->nRow) {
          *pRow2 = tsdbRowFromBlockData(pBlockDataMerge, pRow2->iRow + 1);
        } else {
          pRow2 = NULL;
        }
H
Hongze Cheng 已提交
484 485 486 487
      } else {
        ASSERT(0);
      }
    } else if (pRow1) {
H
Hongze Cheng 已提交
488 489
      *pRow = *pRow1;

H
Hongze Cheng 已提交
490 491
      tsdbTbDataIterNext(pIter);
      pRow1 = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
492 493 494 495 496 497 498 499
      if (pRow1) {
        if (tsdbKeyCmprFn(&TSDBROW_KEY(pRow1), &toKey) < 0) {
          code = tsdbCommitterUpdateSchema(pCommitter, pBlockIdx->suid, pBlockIdx->uid, TSDBROW_VERSION(pRow1));
          if (code) goto _err;
        } else {
          pRow1 = NULL;
        }
      }
H
Hongze Cheng 已提交
500
    } else {
H
Hongze Cheng 已提交
501
      *pRow = *pRow2;
H
Hongze Cheng 已提交
502

H
Hongze Cheng 已提交
503 504 505 506 507
      if (pRow2->iRow + 1 < pBlockDataMerge->nRow) {
        *pRow2 = tsdbRowFromBlockData(pBlockDataMerge, pRow2->iRow + 1);
      } else {
        pRow2 = NULL;
      }
H
Hongze Cheng 已提交
508 509
    }

H
Hongze Cheng 已提交
510
    code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->pTSchema);
H
Hongze Cheng 已提交
511 512 513 514 515 516 517
    if (code) goto _err;

    pBlock->minVersion = TMIN(pBlock->minVersion, TSDBROW_VERSION(pRow));
    pBlock->maxVersion = TMAX(pBlock->maxVersion, TSDBROW_VERSION(pRow));
    pBlock->nRow++;
    if (lastKey == TSDBROW_TS(pRow)) {
      pBlock->hasDup = 1;
H
Hongze Cheng 已提交
518
    } else {
H
Hongze Cheng 已提交
519
      lastKey = TSDBROW_TS(pRow);
H
Hongze Cheng 已提交
520 521
    }

H
Hongze Cheng 已提交
522 523 524
    if (pBlockData->nRow >= pCommitter->maxRow * 4 / 5) goto _write_block;
    continue;

H
Hongze Cheng 已提交
525
  _write_block:
H
Hongze Cheng 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
    if (!toDataOnly && pBlockData->nRow < pCommitter->minRow) {
      pBlock->last = 1;
    } else {
      pBlock->last = 0;
    }

    code = tsdbWriteBlockData(pCommitter->pWriter, pBlockData, NULL, NULL, pBlockIdx, pBlock, pCommitter->cmprAlg);
    if (code) goto _err;

    code = tMapDataPutItem(&pCommitter->nBlockMap, pBlock, tPutBlock);
    if (code) goto _err;

    lastKey = TSKEY_MIN;
    tBlockReset(pBlock);
    tBlockDataReset(pBlockData);
H
Hongze Cheng 已提交
541 542 543 544 545 546 547 548 549
  }

  return code;

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

H
Hongze Cheng 已提交
550
static int32_t tsdbCommitTableMemData(SCommitter *pCommitter, STbDataIter *pIter, TSDBKEY toKey, int8_t toDataOnly) {
H
Hongze Cheng 已提交
551 552
  int32_t     code = 0;
  TSDBROW    *pRow;
H
Hongze Cheng 已提交
553
  SBlock     *pBlock = &pCommitter->nBlock;
H
Hongze Cheng 已提交
554
  SBlockData *pBlockData = &pCommitter->nBlockData;
H
Hongze Cheng 已提交
555 556 557
  TSKEY       lastKey = TSKEY_MIN;
  int64_t     suid = pIter->pTbData->suid;
  int64_t     uid = pIter->pTbData->uid;
H
Hongze Cheng 已提交
558

H
Hongze Cheng 已提交
559
  tBlockReset(pBlock);
H
Hongze Cheng 已提交
560
  tBlockDataReset(pBlockData);
H
Hongze Cheng 已提交
561
  pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
562
  ASSERT(pRow && tsdbKeyCmprFn(&TSDBROW_KEY(pRow), &toKey) < 0);
H
Hongze Cheng 已提交
563
  while (true) {
H
Hongze Cheng 已提交
564
    if (pRow == NULL) {
H
Hongze Cheng 已提交
565 566 567 568 569 570 571
      if (pBlockData->nRow > 0) {
        goto _write_block;
      } else {
        break;
      }
    }

H
Hongze Cheng 已提交
572
    // update schema
H
Hongze Cheng 已提交
573 574 575
    code = tsdbCommitterUpdateSchema(pCommitter, pIter->pTbData->suid, pIter->pTbData->uid, TSDBROW_SVERSION(pRow));
    if (code) goto _err;

H
Hongze Cheng 已提交
576
    // append
H
Hongze Cheng 已提交
577 578 579
    code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->pTSchema);
    if (code) goto _err;

H
Hongze Cheng 已提交
580 581 582 583 584 585 586 587 588 589
    // update
    pBlock->minVersion = TMIN(pBlock->minVersion, TSDBROW_VERSION(pRow));
    pBlock->maxVersion = TMIN(pBlock->maxVersion, TSDBROW_VERSION(pRow));
    pBlock->nRow++;
    if (TSDBROW_TS(pRow) == lastKey) {
      pBlock->hasDup = 1;
    } else {
      lastKey = TSDBROW_TS(pRow);
    }

H
Hongze Cheng 已提交
590 591
    tsdbTbDataIterNext(pIter);
    pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
592
    if (pRow && tsdbKeyCmprFn(&TSDBROW_KEY(pRow), &toKey) >= 0) pRow = NULL;
H
Hongze Cheng 已提交
593 594

    if (pBlockData->nRow >= pCommitter->maxRow * 4 / 5) goto _write_block;
H
Hongze Cheng 已提交
595
    continue;
H
Hongze Cheng 已提交
596 597

  _write_block:
H
Hongze Cheng 已提交
598 599 600 601 602 603 604 605 606 607 608 609 610 611
    if (!toDataOnly && pBlockData->nRow < pCommitter->minKey) {
      pBlock->last = 1;
    } else {
      pBlock->last = 0;
    }

    code = tsdbWriteBlockData(pCommitter->pWriter, pBlockData, NULL, NULL, &(SBlockIdx){.suid = suid, .uid = uid},
                              pBlock, pCommitter->cmprAlg);
    if (code) goto _err;

    code = tMapDataPutItem(&pCommitter->nBlockMap, pBlock, tPutBlock);
    if (code) goto _err;

    tBlockReset(pBlock);
H
Hongze Cheng 已提交
612
    tBlockDataReset(pBlockData);
H
Hongze Cheng 已提交
613
    lastKey = TSKEY_MIN;
H
Hongze Cheng 已提交
614 615 616 617 618
  }

  return code;

_err:
H
Hongze Cheng 已提交
619
  tsdbError("vgId:%d tsdb commit table mem data failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
620 621 622
  return code;
}

H
Hongze Cheng 已提交
623
static int32_t tsdbCommitTableDiskData(SCommitter *pCommitter, SBlock *pBlock, SBlockIdx *pBlockIdx) {
H
Hongze Cheng 已提交
624
  int32_t code = 0;
H
Hongze Cheng 已提交
625 626

  if (pBlock->last) {
H
Hongze Cheng 已提交
627
    code = tsdbReadBlockData(pCommitter->pReader, pBlockIdx, pBlock, &pCommitter->oBlockData, NULL, NULL);
H
Hongze Cheng 已提交
628 629
    if (code) goto _err;

H
Hongze Cheng 已提交
630 631 632 633 634 635 636 637 638
    tBlockReset(&pCommitter->nBlock);
    pCommitter->nBlock.minKey = pBlock->minKey;
    pCommitter->nBlock.maxKey = pBlock->maxKey;
    pCommitter->nBlock.minVersion = pBlock->minVersion;
    pCommitter->nBlock.nRow = pBlock->nRow;
    pCommitter->nBlock.last = pBlock->last;
    pCommitter->nBlock.hasDup = pBlock->hasDup;
    code = tsdbWriteBlockData(pCommitter->pWriter, &pCommitter->oBlockData, NULL, NULL, pBlockIdx, &pCommitter->nBlock,
                              pCommitter->cmprAlg);
H
Hongze Cheng 已提交
639 640 641 642 643 644 645 646 647 648 649 650 651
    if (code) goto _err;

    code = tMapDataPutItem(&pCommitter->nBlockMap, &pCommitter->nBlock, tPutBlock);
    if (code) goto _err;
  } else {
    code = tMapDataPutItem(&pCommitter->nBlockMap, pBlock, tPutBlock);
    if (code) goto _err;
  }

  return code;

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

H
Hongze Cheng 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
static int32_t tsdbCommitTableDataEnd(SCommitter *pCommitter, int64_t suid, int64_t uid) {
  int32_t    code = 0;
  SBlockIdx *pBlockIdx = &(SBlockIdx){.suid = suid, .uid = uid};

  code = tsdbWriteBlock(pCommitter->pWriter, &pCommitter->nBlockMap, NULL, pBlockIdx);
  if (code) goto _err;

  code = tMapDataPutItem(&pCommitter->nBlockIdxMap, pBlockIdx, tPutBlockIdx);
  if (code) goto _err;

  return code;

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

H
Hongze Cheng 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
static int32_t tsdbGetOvlpNRow(STbDataIter *pIter, SBlock *pBlock) {
  int32_t     nRow = 0;
  TSDBROW    *pRow;
  TSDBKEY     key;
  int32_t     c = 0;
  STbDataIter iter = *pIter;

  iter.pRow = NULL;
  while (true) {
    pRow = tsdbTbDataIterGet(pIter);

    if (pRow == NULL) break;
    key = TSDBROW_KEY(pRow);

    c = tBlockCmprFn(&(SBlock){.maxKey = key, .minKey = key}, pBlock);
    if (c == 0) {
      nRow++;
    } else if (c > 0) {
      break;
    } else {
      ASSERT(0);
    }
  }

  return nRow;
}

static int32_t tsdbMergeAsSubBlock(SCommitter *pCommitter, STbDataIter *pIter, SBlock *pBlock) {
  int32_t     code = 0;
  SBlockData *pBlockData = &pCommitter->nBlockData;
  SBlockIdx  *pBlockIdx = &(SBlockIdx){.suid = pIter->pTbData->suid, .uid = pIter->pTbData->uid};
  TSDBROW    *pRow;

  tBlockDataReset(pBlockData);
  pRow = tsdbTbDataIterGet(pIter);
  code = tsdbCommitterUpdateSchema(pCommitter, pBlockIdx->suid, pBlockIdx->uid, TSDBROW_SVERSION(pRow));
  if (code) goto _err;
  while (true) {
    if (pRow) break;
    code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->pTSchema);
    if (code) goto _err;

    tsdbTbDataIterNext(pIter);
    pRow = tsdbTbDataIterGet(pIter);
    if (pRow) {
      int32_t c = tBlockCmprFn(&(SBlock){}, pBlock);

      if (c == 0) {
        code = tsdbCommitterUpdateSchema(pCommitter, pIter->pTbData->suid, pIter->pTbData->uid, TSDBROW_SVERSION(pRow));
        if (code) goto _err;
      } else if (c > 0) {
        pRow = NULL;
      } else {
        ASSERT(0);
      }
    }
  }

  // write as a subblock
  code = tBlockCopy(pBlock, &pCommitter->nBlock);
  if (code) goto _err;

  code = tsdbWriteBlockData(pCommitter->pWriter, pBlockData, NULL, NULL, pBlockIdx, &pCommitter->nBlock,
                            pCommitter->cmprAlg);
  if (code) goto _err;

  code = tMapDataPutItem(&pCommitter->nBlockMap, &pCommitter->nBlock, tPutBlock);
  if (code) goto _err;

  return code;

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

H
Hongze Cheng 已提交
748 749 750 751
static int32_t tsdbCommitTableData(SCommitter *pCommitter, STbData *pTbData, SBlockIdx *pBlockIdx) {
  int32_t      code = 0;
  STbDataIter *pIter = &(STbDataIter){0};
  TSDBROW     *pRow;
H
Hongze Cheng 已提交
752
  int32_t      iBlock;
H
Hongze Cheng 已提交
753
  int32_t      nBlock;
H
Hongze Cheng 已提交
754 755
  int64_t      suid;
  int64_t      uid;
H
Hongze Cheng 已提交
756 757 758 759

  if (pTbData) {
    tsdbTbDataIterOpen(pTbData, &(TSDBKEY){.ts = pCommitter->minKey, .version = VERSION_MIN}, 0, pIter);
    pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
760 761
    if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) pRow = NULL;

H
Hongze Cheng 已提交
762 763
    suid = pTbData->suid;
    uid = pTbData->uid;
H
Hongze Cheng 已提交
764 765 766 767 768 769 770 771 772 773 774
  } else {
    pIter = NULL;
    pRow = NULL;
  }

  if (pBlockIdx) {
    code = tsdbReadBlock(pCommitter->pReader, pBlockIdx, &pCommitter->oBlockMap, NULL);
    if (code) goto _err;

    nBlock = pCommitter->oBlockMap.nItem;
    ASSERT(nBlock > 0);
H
Hongze Cheng 已提交
775

H
Hongze Cheng 已提交
776 777
    suid = pBlockIdx->suid;
    uid = pBlockIdx->uid;
H
Hongze Cheng 已提交
778 779 780 781
  } else {
    nBlock = 0;
  }

H
Hongze Cheng 已提交
782
  if (pRow == NULL && nBlock == 0) goto _exit;
H
Hongze Cheng 已提交
783 784

  // start ===========
H
Hongze Cheng 已提交
785
  tMapDataReset(&pCommitter->nBlockMap);
H
Hongze Cheng 已提交
786
  SBlock *pBlock = &pCommitter->oBlock;
H
Hongze Cheng 已提交
787

H
Hongze Cheng 已提交
788
  iBlock = 0;
H
Hongze Cheng 已提交
789 790 791 792 793 794
  if (iBlock < nBlock) {
    tMapDataGetItemByIdx(&pCommitter->oBlockMap, iBlock, pBlock, tGetBlock);
  } else {
    pBlock = NULL;
  }

H
Hongze Cheng 已提交
795 796
  // merge ===========
  while (true) {
H
Hongze Cheng 已提交
797
    if (pRow == NULL && pBlock == NULL) break;
H
Hongze Cheng 已提交
798

H
Hongze Cheng 已提交
799
    if (pRow && pBlock) {
H
Hongze Cheng 已提交
800
      if (pBlock->last) {
H
Hongze Cheng 已提交
801
        code = tsdbMergeTableData(pCommitter, pIter, pBlock,
H
Hongze Cheng 已提交
802
                                  (TSDBKEY){.ts = pCommitter->maxKey + 1, .version = VERSION_MIN}, 0);
H
Hongze Cheng 已提交
803 804 805
        if (code) goto _err;

        pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
806
        if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) pRow = NULL;
H
Hongze Cheng 已提交
807
        iBlock++;
H
Hongze Cheng 已提交
808 809 810 811 812
        if (iBlock < nBlock) {
          tMapDataGetItemByIdx(&pCommitter->oBlockMap, iBlock, pBlock, tGetBlock);
        } else {
          pBlock = NULL;
        }
H
Hongze Cheng 已提交
813 814

        ASSERT(pRow == NULL && pBlock == NULL);
H
Hongze Cheng 已提交
815
      } else {
H
Hongze Cheng 已提交
816
        int32_t c = tBlockCmprFn(&(SBlock){.maxKey = TSDBROW_KEY(pRow), .minKey = TSDBROW_KEY(pRow)}, pBlock);
H
Hongze Cheng 已提交
817
        if (c > 0) {
H
Hongze Cheng 已提交
818
          // only disk data
H
Hongze Cheng 已提交
819
          code = tsdbCommitTableDiskData(pCommitter, pBlock, pBlockIdx);
H
Hongze Cheng 已提交
820 821
          if (code) goto _err;

H
Hongze Cheng 已提交
822 823 824 825 826 827
          iBlock++;
          if (iBlock < nBlock) {
            tMapDataGetItemByIdx(&pCommitter->oBlockMap, iBlock, pBlock, tGetBlock);
          } else {
            pBlock = NULL;
          }
H
Hongze Cheng 已提交
828
        } else if (c < 0) {
H
Hongze Cheng 已提交
829
          // only memory data
H
Hongze Cheng 已提交
830
          code = tsdbCommitTableMemData(pCommitter, pIter, pBlock->minKey, 1);
H
Hongze Cheng 已提交
831 832 833
          if (code) goto _err;

          pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
834
          if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) pRow = NULL;
H
Hongze Cheng 已提交
835
        } else {
H
Hongze Cheng 已提交
836
          // merge memory and disk
H
Hongze Cheng 已提交
837 838
          int32_t nOvlp = tsdbGetOvlpNRow(pIter, pBlock);
          ASSERT(nOvlp);
H
Hongze Cheng 已提交
839
          if (pBlock->nRow + nOvlp <= pCommitter->maxRow && pBlock->nSubBlock < TSDB_MAX_SUBBLOCKS) {
H
Hongze Cheng 已提交
840 841
            code = tsdbMergeAsSubBlock(pCommitter, pIter, pBlock);
            if (code) goto _err;
H
Hongze Cheng 已提交
842
          } else {
H
Hongze Cheng 已提交
843 844 845 846 847 848 849 850 851 852
            TSDBKEY toKey = {.ts = pCommitter->maxKey + 1, .version = VERSION_MIN};
            int8_t  toDataOnly = 0;

            if (iBlock < nBlock - 1) {
              toDataOnly = 1;

              SBlock nextBlock = {0};
              tBlockReset(&nextBlock);
              tMapDataGetItemByIdx(&pCommitter->oBlockMap, iBlock + 1, &nextBlock, tGetBlock);
              toKey = nextBlock.minKey;
H
Hongze Cheng 已提交
853
            }
H
Hongze Cheng 已提交
854 855 856

            code = tsdbMergeTableData(pCommitter, pIter, pBlock, toKey, toDataOnly);
            if (code) goto _err;
H
Hongze Cheng 已提交
857
          }
H
Hongze Cheng 已提交
858 859 860 861 862 863 864 865 866

          pRow = tsdbTbDataIterGet(pIter);
          if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) pRow = NULL;
          iBlock++;
          if (iBlock < nBlock) {
            tMapDataGetItemByIdx(&pCommitter->oBlockMap, iBlock, pBlock, tGetBlock);
          } else {
            pBlock = NULL;
          }
H
Hongze Cheng 已提交
867 868 869
        }
      }
    } else if (pBlock) {
H
Hongze Cheng 已提交
870
      code = tsdbCommitTableDiskData(pCommitter, pBlock, pBlockIdx);
H
Hongze Cheng 已提交
871 872
      if (code) goto _err;

H
Hongze Cheng 已提交
873 874 875 876 877 878
      iBlock++;
      if (iBlock < nBlock) {
        tMapDataGetItemByIdx(&pCommitter->oBlockMap, iBlock, pBlock, tGetBlock);
      } else {
        pBlock = NULL;
      }
H
Hongze Cheng 已提交
879
    } else {
H
Hongze Cheng 已提交
880 881
      code =
          tsdbCommitTableMemData(pCommitter, pIter, (TSDBKEY){.ts = pCommitter->maxKey + 1, .version = VERSION_MIN}, 0);
H
Hongze Cheng 已提交
882 883 884
      if (code) goto _err;

      pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
885 886
      if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) pRow = NULL;
      ASSERT(pRow == NULL);
H
Hongze Cheng 已提交
887 888 889
    }
  }

H
Hongze Cheng 已提交
890 891
  // end =====================
  code = tsdbCommitTableDataEnd(pCommitter, suid, uid);
H
Hongze Cheng 已提交
892 893 894 895 896 897 898 899 900 901 902 903 904 905
  if (code) goto _err;

_exit:
  if (pIter) {
    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 已提交
906 907 908 909 910 911
static int32_t tsdbCommitFileDataImpl(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
  int32_t    iTbData = 0;
  int32_t    nTbData = taosArrayGetSize(pMemTable->aTbData);
H
Hongze Cheng 已提交
912
  int32_t    iBlockIdx = 0;
H
Hongze Cheng 已提交
913
  int32_t    nBlockIdx = pCommitter->oBlockIdxMap.nItem;
H
Hongze Cheng 已提交
914
  STbData   *pTbData;
H
Hongze Cheng 已提交
915
  SBlockIdx *pBlockIdx = &(SBlockIdx){0};
H
Hongze Cheng 已提交
916

H
Hongze Cheng 已提交
917
  ASSERT(nTbData > 0);
H
Hongze Cheng 已提交
918

H
Hongze Cheng 已提交
919 920
  pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
  if (iBlockIdx < nBlockIdx) {
H
Hongze Cheng 已提交
921
    tMapDataGetItemByIdx(&pCommitter->oBlockIdxMap, iBlockIdx, pBlockIdx, tGetBlockIdx);
H
Hongze Cheng 已提交
922 923
  } else {
    pBlockIdx = NULL;
H
Hongze Cheng 已提交
924 925
  }

H
Hongze Cheng 已提交
926 927 928
  while (pTbData || pBlockIdx) {
    if (pTbData && pBlockIdx) {
      int32_t c = tTABLEIDCmprFn(pTbData, pBlockIdx);
H
Hongze Cheng 已提交
929

H
Hongze Cheng 已提交
930
      if (c == 0) {
H
Hongze Cheng 已提交
931
        goto _commit_table_mem_and_disk;
H
Hongze Cheng 已提交
932
      } else if (c < 0) {
H
Hongze Cheng 已提交
933
        goto _commit_table_mem_data;
H
Hongze Cheng 已提交
934
      } else {
H
Hongze Cheng 已提交
935
        goto _commit_table_disk_data;
H
Hongze Cheng 已提交
936
      }
H
Hongze Cheng 已提交
937
    } else if (pBlockIdx) {
H
Hongze Cheng 已提交
938 939 940 941
      goto _commit_table_disk_data;
    } else {
      goto _commit_table_mem_data;
    }
H
Hongze Cheng 已提交
942

H
Hongze Cheng 已提交
943 944 945 946 947 948 949
  _commit_table_mem_data:
    code = tsdbCommitTableData(pCommitter, pTbData, NULL);
    if (code) goto _err;

    iTbData++;
    if (iTbData < nTbData) {
      pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
H
Hongze Cheng 已提交
950
    } else {
H
Hongze Cheng 已提交
951 952 953
      pTbData = NULL;
    }
    continue;
H
Hongze Cheng 已提交
954

H
Hongze Cheng 已提交
955 956 957 958 959 960 961 962 963
  _commit_table_disk_data:
    code = tsdbCommitTableData(pCommitter, NULL, pBlockIdx);
    if (code) goto _err;

    iBlockIdx++;
    if (iBlockIdx < nBlockIdx) {
      tMapDataGetItemByIdx(&pCommitter->oBlockIdxMap, iBlockIdx, pBlockIdx, tGetBlockIdx);
    } else {
      pBlockIdx = NULL;
H
Hongze Cheng 已提交
964
    }
H
Hongze Cheng 已提交
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
    continue;

  _commit_table_mem_and_disk:
    code = tsdbCommitTableData(pCommitter, pTbData, pBlockIdx);
    if (code) goto _err;

    iBlockIdx++;
    if (iBlockIdx < nBlockIdx) {
      tMapDataGetItemByIdx(&pCommitter->oBlockIdxMap, iBlockIdx, pBlockIdx, tGetBlockIdx);
    } else {
      pBlockIdx = NULL;
    }
    iTbData++;
    if (iTbData < nTbData) {
      pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
    } else {
      pTbData = NULL;
    }
    continue;
H
Hongze Cheng 已提交
984 985
  }

H
Hongze Cheng 已提交
986 987 988
  return code;

_err:
H
Hongze Cheng 已提交
989
  tsdbError("vgId:%d commit file data impl failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
990 991 992 993 994
  return code;
}

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

H
Hongze Cheng 已提交
996
  // write blockIdx
H
Hongze Cheng 已提交
997
  code = tsdbWriteBlockIdx(pCommitter->pWriter, &pCommitter->nBlockIdxMap, NULL);
H
Hongze Cheng 已提交
998 999
  if (code) goto _err;

H
Hongze Cheng 已提交
1000
  // update file header
H
Hongze Cheng 已提交
1001
  code = tsdbUpdateDFileSetHeader(pCommitter->pWriter);
H
Hongze Cheng 已提交
1002 1003
  if (code) goto _err;

H
Hongze Cheng 已提交
1004 1005 1006 1007
  // upsert SDFileSet
  code = tsdbFSStateUpsertDFileSet(pCommitter->pTsdb->fs->nState, tsdbDataFWriterGetWSet(pCommitter->pWriter));
  if (code) goto _err;

H
Hongze Cheng 已提交
1008
  // close and sync
H
Hongze Cheng 已提交
1009
  code = tsdbDataFWriterClose(&pCommitter->pWriter, 1);
H
Hongze Cheng 已提交
1010 1011 1012
  if (code) goto _err;

  if (pCommitter->pReader) {
H
Hongze Cheng 已提交
1013
    code = tsdbDataFReaderClose(&pCommitter->pReader);
H
Hongze Cheng 已提交
1014 1015 1016 1017 1018 1019 1020
    goto _err;
  }

_exit:
  return code;

_err:
H
Hongze Cheng 已提交
1021
  tsdbError("vgId:%d commit file data end failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1022 1023 1024
  return code;
}

H
Hongze Cheng 已提交
1025
static int32_t tsdbCommitFileData(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1026 1027
  int32_t code = 0;

H
Hongze Cheng 已提交
1028 1029
  // commit file data start
  code = tsdbCommitFileDataStart(pCommitter);
H
Hongze Cheng 已提交
1030
  if (code) goto _err;
H
Hongze Cheng 已提交
1031

H
Hongze Cheng 已提交
1032 1033
  // commit file data impl
  code = tsdbCommitFileDataImpl(pCommitter);
H
Hongze Cheng 已提交
1034
  if (code) goto _err;
H
Hongze Cheng 已提交
1035

H
Hongze Cheng 已提交
1036 1037
  // commit file data end
  code = tsdbCommitFileDataEnd(pCommitter);
H
Hongze Cheng 已提交
1038
  if (code) goto _err;
H
Hongze Cheng 已提交
1039 1040 1041 1042

  return code;

_err:
H
Hongze Cheng 已提交
1043
  tsdbError("vgId:%d commit file data failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1044 1045 1046
  return code;
}

H
Hongze Cheng 已提交
1047 1048
// ----------------------------------------------------------------------------
static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1049
  int32_t code = 0;
H
Hongze Cheng 已提交
1050

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

H
Hongze Cheng 已提交
1054 1055 1056 1057
  // lock();
  pTsdb->imem = pTsdb->mem;
  pTsdb->mem = NULL;
  // unlock();
H
Hongze Cheng 已提交
1058

H
Hongze Cheng 已提交
1059
  pCommitter->pTsdb = pTsdb;
H
Hongze Cheng 已提交
1060
  pCommitter->commitID = pTsdb->pVnode->state.commitID;
H
Hongze Cheng 已提交
1061 1062 1063 1064
  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 已提交
1065
  pCommitter->cmprAlg = pTsdb->pVnode->config.tsdbCfg.compression;
H
Hongze Cheng 已提交
1066

H
Hongze Cheng 已提交
1067 1068 1069 1070 1071 1072 1073
  code = tsdbFSBegin(pTsdb->fs);
  if (code) goto _err;

  return code;

_err:
  tsdbError("vgId:%d tsdb start commit failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1074 1075 1076
  return code;
}

H
Hongze Cheng 已提交
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
static int32_t tsdbCommitDataStart(SCommitter *pCommitter) {
  int32_t code = 0;

  pCommitter->pReader = NULL;
  pCommitter->oBlockIdxMap = tMapDataInit();
  pCommitter->oBlockMap = tMapDataInit();
  pCommitter->oBlock = tBlockInit();
  pCommitter->pWriter = NULL;
  pCommitter->nBlockIdxMap = tMapDataInit();
  pCommitter->nBlockMap = tMapDataInit();
  pCommitter->nBlock = tBlockInit();
  code = tBlockDataInit(&pCommitter->oBlockData);
  if (code) goto _exit;
  code = tBlockDataInit(&pCommitter->nBlockData);
  if (code) {
    tBlockDataClear(&pCommitter->oBlockData);
    goto _exit;
  }

_exit:
  return code;
}

static void tsdbCommitDataEnd(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1101 1102 1103 1104 1105 1106 1107 1108
  // tMapDataClear(&pCommitter->oBlockIdxMap);
  // tMapDataClear(&pCommitter->oBlockMap);
  // tBlockClear(&pCommitter->oBlock);
  // tBlockDataClear(&pCommitter->oBlockData);
  // tMapDataClear(&pCommitter->nBlockIdxMap);
  // tMapDataClear(&pCommitter->nBlockMap);
  // tBlockClear(&pCommitter->nBlock);
  // tBlockDataClear(&pCommitter->nBlockData);
H
Hongze Cheng 已提交
1109 1110
}

H
Hongze Cheng 已提交
1111 1112 1113 1114
static int32_t tsdbCommitData(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1115

H
Hongze Cheng 已提交
1116
  // check
H
Hongze Cheng 已提交
1117
  if (pMemTable->nRow == 0) goto _exit;
H
Hongze Cheng 已提交
1118

H
Hongze Cheng 已提交
1119 1120
  // start ====================
  code = tsdbCommitDataStart(pCommitter);
H
Hongze Cheng 已提交
1121
  if (code) goto _err;
H
Hongze Cheng 已提交
1122 1123 1124

  // impl ====================
  pCommitter->nextKey = pMemTable->minKey;
H
Hongze Cheng 已提交
1125 1126 1127 1128 1129
  while (pCommitter->nextKey < TSKEY_MAX) {
    pCommitter->commitFid = tsdbKeyFid(pCommitter->nextKey, pCommitter->minutes, pCommitter->precision);
    tsdbFidKeyRange(pCommitter->commitFid, pCommitter->minutes, pCommitter->precision, &pCommitter->minKey,
                    &pCommitter->maxKey);
    code = tsdbCommitFileData(pCommitter);
H
Hongze Cheng 已提交
1130
    if (code) goto _err;
H
Hongze Cheng 已提交
1131
  }
H
Hongze Cheng 已提交
1132

H
Hongze Cheng 已提交
1133 1134 1135
  // end ====================
  tsdbCommitDataEnd(pCommitter);

H
Hongze Cheng 已提交
1136 1137 1138
_exit:
  tsdbDebug("vgId:%d commit data done, nRow:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nRow);
  return code;
H
Hongze Cheng 已提交
1139

H
Hongze Cheng 已提交
1140
_err:
H
Hongze Cheng 已提交
1141
  tsdbCommitDataEnd(pCommitter);
H
Hongze Cheng 已提交
1142 1143 1144
  tsdbError("vgId:%d commit data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  return code;
}
H
Hongze Cheng 已提交
1145

H
Hongze Cheng 已提交
1146 1147 1148 1149
static int32_t tsdbCommitDel(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1150

H
Hongze Cheng 已提交
1151 1152
  if (pMemTable->nDel == 0) {
    goto _exit;
H
Hongze Cheng 已提交
1153
  }
H
Hongze Cheng 已提交
1154

H
Hongze Cheng 已提交
1155 1156 1157 1158 1159
  // start
  code = tsdbCommitDelStart(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1160

H
Hongze Cheng 已提交
1161 1162 1163 1164 1165
  // impl
  code = tsdbCommitDelImpl(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1166

H
Hongze Cheng 已提交
1167 1168 1169 1170 1171
  // end
  code = tsdbCommitDelEnd(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1172

H
Hongze Cheng 已提交
1173
_exit:
H
Hongze Cheng 已提交
1174
  tsdbDebug("vgId:%d commit del done, nDel:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nDel);
H
Hongze Cheng 已提交
1175 1176 1177
  return code;

_err:
H
Hongze Cheng 已提交
1178
  tsdbError("vgId:%d commit del failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1179
  return code;
H
Hongze Cheng 已提交
1180 1181 1182 1183 1184 1185 1186
}

static int32_t tsdbCommitCache(SCommitter *pCommitter) {
  int32_t code = 0;
  // TODO
  return code;
}
H
Hongze Cheng 已提交
1187 1188

static int32_t tsdbEndCommit(SCommitter *pCommitter, int32_t eno) {
H
Hongze Cheng 已提交
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

  if (eno == 0) {
    code = tsdbFSCommit(pTsdb->fs);
  } else {
    code = tsdbFSRollback(pTsdb->fs);
  }

  tsdbMemTableDestroy(pMemTable);
  pTsdb->imem = NULL;

  tsdbInfo("vgId:%d tsdb end commit", TD_VID(pTsdb->pVnode));
  return code;

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