tsdbCommit.c 33.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

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
} SCommitter;
H
refact  
Hongze Cheng 已提交
54

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

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

H
Hongze Cheng 已提交
64
  code = tsdbMemTableCreate(pTsdb, &pTsdb->mem);
H
Hongze Cheng 已提交
65
  if (code) goto _err;
H
Hongze Cheng 已提交
66

H
Hongze Cheng 已提交
67 68 69
  return code;

_err:
H
Hongze Cheng 已提交
70
  tsdbError("vgId:%d tsdb begin failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
71
  return code;
H
Hongze Cheng 已提交
72 73
}

H
more  
Hongze Cheng 已提交
74
int32_t tsdbCommit(STsdb *pTsdb) {
75
  if (!pTsdb) return 0;
H
Hongze Cheng 已提交
76

H
more  
Hongze Cheng 已提交
77
  int32_t    code = 0;
H
Hongze Cheng 已提交
78 79 80 81
  SCommitter commith;
  SMemTable *pMemTable = pTsdb->mem;

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

H
more  
Hongze Cheng 已提交
89
  // start commit
H
more  
Hongze Cheng 已提交
90
  code = tsdbStartCommit(pTsdb, &commith);
H
Hongze Cheng 已提交
91
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
92

H
refact  
Hongze Cheng 已提交
93 94
  // commit impl
  code = tsdbCommitData(&commith);
H
Hongze Cheng 已提交
95
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
96 97

  code = tsdbCommitDel(&commith);
H
Hongze Cheng 已提交
98
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
99 100

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

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

H
Hongze Cheng 已提交
107
_exit:
H
refact  
Hongze Cheng 已提交
108 109 110
  return code;

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

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

H
Hongze Cheng 已提交
123 124 125
  tMapDataReset(&pCommitter->oDelIdxMap);
  tMapDataReset(&pCommitter->nDelIdxMap);

H
Hongze Cheng 已提交
126
  // load old
H
Hongze Cheng 已提交
127
  if (pDelFileR) {
H
Hongze Cheng 已提交
128
    code = tsdbDelFReaderOpen(&pCommitter->pDelFReader, pDelFileR, pTsdb, NULL);
H
Hongze Cheng 已提交
129
    if (code) goto _err;
H
Hongze Cheng 已提交
130

H
Hongze Cheng 已提交
131 132
    code = tsdbReadDelIdx(pCommitter->pDelFReader, &pCommitter->oDelIdxMap, NULL);
    if (code) goto _err;
H
Hongze Cheng 已提交
133 134
  }

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

_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 已提交
145 146 147
  return code;
}

H
Hongze Cheng 已提交
148
static int32_t tsdbCommitTableDel(SCommitter *pCommitter, STbData *pTbData, SDelIdx *pDelIdx) {
H
Hongze Cheng 已提交
149
  int32_t   code = 0;
H
Hongze Cheng 已提交
150
  SDelData *pDelData = &(SDelData){};
H
Hongze Cheng 已提交
151 152 153
  tb_uid_t  suid;
  tb_uid_t  uid;
  SDelIdx   delIdx;  // TODO
H
Hongze Cheng 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

  // 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;
  }
  delIdx.minKey = TSKEY_MAX;
  delIdx.maxKey = TSKEY_MIN;
  delIdx.minVersion = INT64_MAX;
H
Hongze Cheng 已提交
172
  delIdx.maxVersion = INT64_MIN;
H
Hongze Cheng 已提交
173 174 175 176 177 178 179 180 181 182 183 184

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

H
Hongze Cheng 已提交
191 192 193 194
    if (delIdx.minKey > pDelData->sKey) delIdx.minKey = pDelData->sKey;
    if (delIdx.maxKey < pDelData->eKey) delIdx.maxKey = pDelData->eKey;
    if (delIdx.minVersion > pDelData->version) delIdx.minVersion = pDelData->version;
    if (delIdx.maxVersion < pDelData->version) delIdx.maxVersion = pDelData->version;
H
Hongze Cheng 已提交
195 196 197
  }

  // memory
H
Hongze Cheng 已提交
198 199 200
  pDelData = pTbData ? pTbData->pHead : NULL;
  for (; pDelData; pDelData = pDelData->pNext) {
    code = tMapDataPutItem(&pCommitter->nDelDataMap, pDelData, tPutDelData);
H
Hongze Cheng 已提交
201 202
    if (code) goto _err;

H
Hongze Cheng 已提交
203 204 205 206
    if (delIdx.minKey > pDelData->sKey) delIdx.minKey = pDelData->sKey;
    if (delIdx.maxKey < pDelData->eKey) delIdx.maxKey = pDelData->eKey;
    if (delIdx.minVersion > pDelData->version) delIdx.minVersion = pDelData->version;
    if (delIdx.maxVersion < pDelData->version) delIdx.maxVersion = pDelData->version;
H
Hongze Cheng 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  }

  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 已提交
227
static int32_t tsdbCommitDelImpl(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240
  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 已提交
241

H
Hongze Cheng 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255
  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 已提交
256
      if (c == 0) {
H
Hongze Cheng 已提交
257
        goto _commit_mem_and_disk_del;
H
Hongze Cheng 已提交
258
      } else if (c < 0) {
H
Hongze Cheng 已提交
259
        goto _commit_mem_del;
H
Hongze Cheng 已提交
260
      } else {
H
Hongze Cheng 已提交
261
        goto _commit_disk_del;
H
Hongze Cheng 已提交
262
      }
H
Hongze Cheng 已提交
263
    } else {
H
Hongze Cheng 已提交
264 265
      if (pTbData) goto _commit_mem_del;
      if (pDelIdx) goto _commit_disk_del;
H
Hongze Cheng 已提交
266 267
    }

H
Hongze Cheng 已提交
268 269 270 271 272 273 274
  _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 已提交
275 276
      pTbData = NULL;
    }
H
Hongze Cheng 已提交
277
    continue;
H
Hongze Cheng 已提交
278

H
Hongze Cheng 已提交
279 280 281 282 283 284 285 286 287 288 289 290
  _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 已提交
291

H
Hongze Cheng 已提交
292 293
  _commit_mem_and_disk_del:
    code = tsdbCommitTableDel(pCommitter, pTbData, pDelIdx);
H
Hongze Cheng 已提交
294
    if (code) goto _err;
H
Hongze Cheng 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    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 已提交
310 311
  }

H
Hongze Cheng 已提交
312
  return code;
H
Hongze Cheng 已提交
313 314 315 316

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

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

H
Hongze Cheng 已提交
322 323
  code = tsdbWriteDelIdx(pCommitter->pDelFWriter, &pCommitter->nDelIdxMap, NULL);
  if (code) goto _err;
H
Hongze Cheng 已提交
324

H
Hongze Cheng 已提交
325
  code = tsdbUpdateDelFileHdr(pCommitter->pDelFWriter, NULL);
H
Hongze Cheng 已提交
326
  if (code) goto _err;
H
Hongze Cheng 已提交
327 328

  code = tsdbDelFWriterClose(pCommitter->pDelFWriter, 1);
H
Hongze Cheng 已提交
329
  if (code) goto _err;
H
Hongze Cheng 已提交
330 331 332 333 334 335

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

H
Hongze Cheng 已提交
336 337 338
  return code;

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

H
Hongze Cheng 已提交
343 344
#define ROW_END(pRow, maxKey) (((pRow) == NULL) || ((pRow)->pTSRow->ts > (maxKey)))

H
Hongze Cheng 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 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 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
// static int32_t tsdbCommitMemoryData(SCommitter *pCommitter, SBlockIdx *pBlockIdx, STbDataIter *pIter, TSDBKEY eKey,
//                                     bool toDataOnly) {
//   int32_t   code = 0;
//   TSDBROW  *pRow;
//   STSchema *pTSchema = NULL;  // TODO
//   TSDBKEY   key;
//   SBlock   *pBlock = &pCommitter->nBlock;

//   if (pIter == NULL) goto _exit;

//   tBlockReset(pBlock);
//   tBlockDataReset(&pCommitter->nBlockData);
//   while (true) {
//     pRow = tsdbTbDataIterGet(pIter);

//     if (pRow == NULL || tsdbKeyCmprFn(&(TSDBKEY){.ts = pRow->pTSRow->ts, .version = pRow->version}, &eKey) > 0) {
//       if (pCommitter->nBlockData.nRow == 0) {
//         break;
//       } else {
//         goto _write_block_data;
//       }
//     }

//     // update schema
//     if (pTSchema == NULL || pTSchema->version != TSDBROW_SVERSION(pRow)) {
//       // TODO
//       // pTSchema = NULL;
//     }

//     // append row
//     code = tBlockDataAppendRow(&pCommitter->nBlockData, pRow, pTSchema);
//     if (code) goto _err;

//     // update info
//     key = tsdbRowKey(pRow);
//     if (tsdbKeyCmprFn(&key, &pBlock->info.maxKey) > 0) pBlock->info.maxKey = key;
//     if (tsdbKeyCmprFn(&key, &pBlock->info.minKey) < 0) pBlock->info.minKey = key;
//     if (key.version > pBlock->info.maxVersion) pBlock->info.maxVersion = key.version;
//     if (key.version < pBlock->info.minVerion) pBlock->info.minVerion = key.version;

//     // iter next
//     tsdbTbDataIterNext(pIter);

//     // check write
//     if (pCommitter->nBlockData.nRow < pCommitter->maxRow * 4 / 5) {
//       continue;
//     }

//   _write_block_data:
//     if (!toDataOnly && pCommitter->nBlockData.nRow < pCommitter->minKey) {
//       pCommitter->nBlock.last = 1;
//     } else {
//       pCommitter->nBlock.last = 0;
//     }

//     code = tsdbWriteBlockData(pCommitter->pWriter, &pCommitter->nBlockData, NULL, NULL, pBlockIdx, pBlock);
//     if (code) goto _err;

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

//     // update info
//     if (tsdbKeyCmprFn(&pBlock->info.minKey, &pBlockIdx->info.minKey) < 0) pBlock->info.minKey =
//     pBlockIdx->info.minKey; if (tsdbKeyCmprFn(&pBlock->info.maxKey, &pBlockIdx->info.maxKey) < 0) pBlock->info.maxKey
//     = pBlockIdx->info.maxKey; if (pBlock->info.minVerion < pBlockIdx->info.minVerion) pBlockIdx->info.minVerion =
//     pBlock->info.minVerion; if (pBlock->info.maxVersion < pBlockIdx->info.maxVersion) pBlockIdx->info.maxVersion =
//     pBlock->info.maxVersion;

//     tBlockReset(pBlock);
//     tBlockDataReset(&pCommitter->nBlockData);
//   }

// _exit:
//   return code;

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

// static int32_t tsdbGetOverlapRowNumber(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 = tsdbRowKey(pRow);

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

//   return nRow;
// }

// static int32_t tsdbMergeCommitImpl(SCommitter *pCommitter, SBlockIdx *pBlockIdx, STbDataIter *pIter, SBlock *pBlock,
//                                    int8_t toDataOnly) {
//   int32_t  code = 0;
//   int32_t  iRow = 0;
//   int32_t  nRow = 0;
//   int32_t  c;
//   TSDBROW *pRow;
//   SBlock   block = tBlockInit();
//   TSDBKEY  key1;
//   TSDBKEY  key2;

//   tBlockDataReset(&pCommitter->nBlockData);

//   // load last and merge until {pCommitter->maxKey, INT64_MAX}
//   code = tsdbReadBlockData(pCommitter->pReader, pBlockIdx, pBlock, &pCommitter->oBlockData, NULL, 0, NULL, NULL);
//   if (code) goto _err;

//   iRow = 0;
//   nRow = pCommitter->oBlockData.nRow;
//   pRow = tsdbTbDataIterGet(pIter);

//   while (true) {
//     if ((pRow == NULL || pRow->pTSRow->ts > pCommitter->maxKey) && (iRow >= nRow)) {
//       if (pCommitter->nBlockData.nRow > 0) {
//         goto _write_block_data;
//       } else {
//         break;
//       }
//     }

//     // TODO

//   _write_block_data:
//     block.last = pCommitter->nBlockData.nRow < pCommitter->minRow ? 1 : 0;
//     code = tsdbWriteBlockData(pCommitter->pWriter, &pCommitter->nBlockData, NULL, NULL, pBlockIdx, &block);
//     if (code) goto _err;

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

//   tBlockReset(&block);
//   tBlockDataReset(&pCommitter->nBlockData);

//   return code;

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

// static int32_t tsdbMergeCommit(SCommitter *pCommitter, SBlockIdx *pBlockIdx, STbDataIter *pIter, SBlock *pBlock,
//                                int8_t isLastBlock) {
//   int32_t  code = 0;
//   TSDBROW *pRow;
//   TSDBKEY  key;
//   int32_t  c;

//   if (pBlock == NULL) {  // (pIter && pBlock == NULL)
//     key.ts = pCommitter->maxKey;
//     key.version = INT64_MAX;
//     code = tsdbCommitMemoryData(pCommitter, pBlockIdx, pIter, key, 0);
//     if (code) goto _err;
//   } else if (pBlock->last) {
//     // merge
//     code = tsdbMergeCommitImpl(pCommitter, pBlockIdx, pIter, pBlock, 0);
//     if (code) goto _err;
//   } else {  // pBlock && pBlock->last == 0 && (pIter == NULL || pIter)
//     // memory
//     if (pIter) {
//       key.ts = pBlock->info.minKey.ts;
//       key.version = pBlock->info.minKey.version - 1;
//       code = tsdbCommitMemoryData(pCommitter, pBlockIdx, pIter, key, 1);
//       if (code) goto _err;
//     }

//     // merge or move block
//     pRow = tsdbTbDataIterGet(pIter);
//     key.ts = pRow->pTSRow->ts;
//     key.version = pRow->version;

//     c = tBlockCmprFn(&(SBlock){.info.maxKey = key, .info.minKey = key}, pBlock);
//     if (c > 0) {
//       // move block
//       code = tMapDataPutItem(&pCommitter->nBlockMap, pBlock, tPutBlock);
//       if (code) goto _err;
//     } else if (c == 0) {
//       int32_t nOverlap = tsdbGetOverlapRowNumber(pIter, pBlock);

//       if (pBlock->nRow + nOverlap > pCommitter->maxRow || pBlock->nSubBlock == TSDB_MAX_SUBBLOCKS) {
//         code = tsdbMergeCommitImpl(pCommitter, pBlockIdx, pIter, pBlock, 1);
//         if (code) goto _err;
//       } else {
//         // add as a subblock
//       }
//     } else {
//       ASSERT(0);
//     }
//   }

//   return code;

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

// static int32_t tsdbCommitTableData(SCommitter *pCommitter, STbData *pTbData, SBlockIdx *pBlockIdx) {
//   int32_t      code = 0;
//   STbDataIter  iter;
//   STbDataIter *pIter = &iter;
//   TSDBROW     *pRow;
//   int64_t      suid;
//   int64_t      uid;
//   SBlockIdx    blockIdx;

//   // create iter
//   if (pTbData) {
//     suid = pTbData->suid;
//     uid = pTbData->uid;
//     tsdbTbDataIterOpen(pTbData, &(TSDBKEY){.ts = pCommitter->minKey, .version = 0}, 0, pIter);
//   } else {
//     suid = pBlockIdx->suid;
//     uid = pBlockIdx->uid;
//     pIter = NULL;
//   }

//   // check
//   pRow = tsdbTbDataIterGet(pIter);
//   if (ROW_END(pRow, pCommitter->maxKey) && pBlockIdx == NULL) goto _exit;

//   // start ================================
//   tMapDataReset(&pCommitter->oBlockMap);
//   tBlockReset(&pCommitter->oBlock);
//   tBlockDataReset(&pCommitter->oBlockData);
//   if (pBlockIdx) {
//     code = tsdbReadBlock(pCommitter->pReader, pBlockIdx, &pCommitter->oBlockMap, NULL);
//     if (code) goto _err;
//   }

//   blockIdx = tBlockIdxInit(suid, uid);
//   tMapDataReset(&pCommitter->nBlockMap);
//   tBlockReset(&pCommitter->nBlock);
//   tBlockDataReset(&pCommitter->nBlockData);

//   // impl ===============================
//   int32_t iBlock = 0;
//   int32_t nBlock = pCommitter->oBlockMap.nItem;

//   // merge
//   pRow = tsdbTbDataIterGet(pIter);
//   while (!ROW_END(pRow, pCommitter->maxKey) && iBlock < nBlock) {
//     tMapDataGetItemByIdx(&pCommitter->oBlockMap, iBlock, &pCommitter->oBlock, tGetBlock);
//     code = tsdbMergeCommit(pCommitter, &blockIdx, pIter, &pCommitter->oBlock, iBlock == (nBlock - 1));
//     if (code) goto _err;

//     pRow = tsdbTbDataIterGet(pIter);
//     iBlock++;
//   }

//   // mem
//   pRow = tsdbTbDataIterGet(pIter);
//   while (!ROW_END(pRow, pCommitter->maxKey)) {
//     code = tsdbMergeCommit(pCommitter, &blockIdx, pIter, NULL, 0);
//     if (code) goto _err;

//     pRow = tsdbTbDataIterGet(pIter);
//   }

//   // disk
//   while (iBlock < nBlock) {
//     tMapDataGetItemByIdx(&pCommitter->oBlockMap, iBlock, &pCommitter->oBlock, tGetBlock);

//     code = tsdbMergeCommit(pCommitter, &blockIdx, NULL, &pCommitter->oBlock, 0);
//     if (code) goto _err;

//     iBlock++;
//   }

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

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

// _exit:
//   pRow = tsdbTbDataIterGet(pIter);
//   if (pRow) {
//     ASSERT(pRow->pTSRow->ts > pCommitter->maxKey);
//     if (pCommitter->nextKey > pRow->pTSRow->ts) {
//       pCommitter->nextKey = pRow->pTSRow->ts;
//     }
//   }

//   return code;

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

H
Hongze Cheng 已提交
654 655 656 657 658
static int32_t tsdbCommitFileDataStart(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SDFileSet *pRSet = NULL;
  SDFileSet  wSet;
H
Hongze Cheng 已提交
659

H
Hongze Cheng 已提交
660 661
  // memory
  pCommitter->nextKey = TSKEY_MAX;
H
Hongze Cheng 已提交
662

H
Hongze Cheng 已提交
663 664 665 666 667 668 669 670
  // 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 已提交
671 672
    if (code) goto _err;

H
Hongze Cheng 已提交
673
    code = tsdbReadBlockIdx(pCommitter->pReader, &pCommitter->oBlockIdxMap, NULL);
H
Hongze Cheng 已提交
674
    if (code) goto _err;
H
Hongze Cheng 已提交
675
  }
H
Hongze Cheng 已提交
676

H
Hongze Cheng 已提交
677 678 679 680
  // new
  tMapDataReset(&pCommitter->nBlockIdxMap);
  tMapDataReset(&pCommitter->nBlockMap);
  tBlockReset(&pCommitter->nBlock);
H
Hongze Cheng 已提交
681
  tBlockDataReset(&pCommitter->nBlockData);
H
Hongze Cheng 已提交
682 683 684 685 686 687 688 689 690 691
  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 已提交
692

H
Hongze Cheng 已提交
693 694
    // TODO: alloc a new disk
    // tfsAllocDisk(pTfs, 0, &did);
H
Hongze Cheng 已提交
695

H
Hongze Cheng 已提交
696 697
    // create the directory
    tfsMkdirRecurAt(pTfs, pTsdb->path, did);
H
Hongze Cheng 已提交
698

H
Hongze Cheng 已提交
699 700 701 702 703 704
    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 已提交
705
  }
H
Hongze Cheng 已提交
706 707
  code = tsdbDataFWriterOpen(&pCommitter->pWriter, pTsdb, &wSet);
  if (code) goto _err;
H
Hongze Cheng 已提交
708

H
Hongze Cheng 已提交
709
_exit:
H
Hongze Cheng 已提交
710 711 712
  return code;

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

H
Hongze Cheng 已提交
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
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 已提交
743
static int32_t tsdbCommitMemoryData(SCommitter *pCommitter, STbData *pTbData) {
H
Hongze Cheng 已提交
744
  int32_t      code = 0;
H
Hongze Cheng 已提交
745 746 747
  STsdb       *pTsdb = pCommitter->pTsdb;
  STbDataIter *pIter = &(STbDataIter){0};
  TSDBKEY      key = {.ts = pCommitter->minKey, .version = VERSION_MIN};
H
Hongze Cheng 已提交
748
  TSDBROW      row;
H
Hongze Cheng 已提交
749 750 751
  TSDBROW     *pRow;

  // create iter
H
Hongze Cheng 已提交
752
  tsdbTbDataIterOpen(pTbData, &key, 0, pIter);
H
Hongze Cheng 已提交
753
  pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
754

H
Hongze Cheng 已提交
755
  if (pRow == NULL || TSDBROW_TS(pRow) > pCommitter->maxKey) goto _exit;
H
Hongze Cheng 已提交
756

H
Hongze Cheng 已提交
757
  // main loop
H
Hongze Cheng 已提交
758
  SMapData   *mBlock = &pCommitter->nBlockMap;
H
Hongze Cheng 已提交
759 760 761
  SBlockIdx  *pBlockIdx = &(SBlockIdx){.suid = pTbData->suid, .uid = pTbData->uid};
  SBlock     *pBlock = &pCommitter->nBlock;
  SBlockData *pBlockData = &pCommitter->nBlockData;
H
Hongze Cheng 已提交
762

H
Hongze Cheng 已提交
763
  tMapDataReset(mBlock);
H
Hongze Cheng 已提交
764 765 766
  tBlockIdxReset(pBlockIdx);
  tBlockReset(pBlock);
  tBlockDataReset(pBlockData);
H
Hongze Cheng 已提交
767 768 769 770 771
  while (pRow != NULL && TSDBROW_TS(pRow) <= pCommitter->maxKey) {
    code = tsdbCommitterUpdateSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow));
    if (code) goto _err;

    code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->pTSchema);
H
Hongze Cheng 已提交
772
    if (code) goto _err;
H
Hongze Cheng 已提交
773

H
Hongze Cheng 已提交
774 775 776
    pBlock->minVersion = TMIN(pBlock->minVersion, TSDBROW_VERSION(pRow));
    pBlock->maxVersion = TMAX(pBlock->maxVersion, TSDBROW_VERSION(pRow));
    pBlock->nRow++;
H
more  
Hongze Cheng 已提交
777

H
Hongze Cheng 已提交
778
    // next
H
Hongze Cheng 已提交
779
    tsdbTbDataIterNext(pIter);
H
Hongze Cheng 已提交
780
    pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
781

H
Hongze Cheng 已提交
782
    if (pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
H
Hongze Cheng 已提交
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
      ASSERT(0);
      // // SBlock
      // pBlock->last = 0;
      // pBlock->cmprAlg = pCommitter->cmprAlg;
      // code = tsdbWriteBlockData(pCommitter->pWriter, pBlockData, NULL, NULL, pBlockIdx, pBlock);
      // if (code) goto _err;

      // // SBlockIdx
      // pBlockIdx->minKey = TMIN(pBlockIdx->minKey, pBlock->minKey.ts);
      // pBlockIdx->maxKey = TMAX(pBlockIdx->maxKey, pBlock->maxKey.ts);
      // pBlockIdx->minVersion = TMIN(pBlockIdx->minVersion, pBlock->minVersion);
      // pBlockIdx->maxVersion = TMAX(pBlockIdx->maxVersion, pBlock->maxVersion);

      // tBlockReset(pBlock);
      // tBlockDataReset(pBlockData);
H
Hongze Cheng 已提交
798
    }
H
Hongze Cheng 已提交
799 800
  }

H
more  
Hongze Cheng 已提交
801
  if (pBlockData->nRow > 0) {
H
Hongze Cheng 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
    // SBlock
    row = tBlockDataFirstRow(pBlockData);
    if (tsdbKeyCmprFn(&pBlock->minKey, &TSDBROW_KEY(&row)) > 0) pBlock->minKey = TSDBROW_KEY(&row);
    row = tBlockDataLastRow(pBlockData);
    if (tsdbKeyCmprFn(&pBlock->maxKey, &TSDBROW_KEY(&row)) < 0) pBlock->maxKey = TSDBROW_KEY(&row);
    pBlock->last = 1;
    pBlock->cmprAlg = pCommitter->cmprAlg;
    code = tsdbWriteBlockData(pCommitter->pWriter, pBlockData, NULL, NULL, pBlockIdx, pBlock);
    if (code) goto _err;

    // SBlockIdx
    code = tMapDataPutItem(mBlock, pBlock, tPutBlock);
    if (code) goto _err;
    pBlockIdx->minKey = TMIN(pBlockIdx->minKey, pBlock->minKey.ts);
    pBlockIdx->maxKey = TMAX(pBlockIdx->maxKey, pBlock->maxKey.ts);
    pBlockIdx->minVersion = TMIN(pBlockIdx->minVersion, pBlock->minVersion);
    pBlockIdx->maxVersion = TMAX(pBlockIdx->maxVersion, pBlock->maxVersion);
H
more  
Hongze Cheng 已提交
819 820
  }

H
Hongze Cheng 已提交
821 822 823 824 825 826 827
  // write block
  code = tsdbWriteBlock(pCommitter->pWriter, mBlock, NULL, pBlockIdx);
  if (code) goto _err;

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

H
Hongze Cheng 已提交
828
_exit:
H
Hongze Cheng 已提交
829
  if (pRow) pCommitter->nextKey = TMIN(pCommitter->nextKey, TSDBROW_TS(pRow));
H
Hongze Cheng 已提交
830 831 832
  return code;

_err:
H
Hongze Cheng 已提交
833
  tsdbError("vgId:%d tsdb commit memory data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
834 835 836 837 838
  return code;
}

static int32_t tsdbCommitFileDataImpl(SCommitter *pCommitter) {
  int32_t    code = 0;
H
Hongze Cheng 已提交
839
  int32_t    c;
H
Hongze Cheng 已提交
840 841 842 843
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
  int32_t    iTbData = 0;
  int32_t    nTbData = taosArrayGetSize(pMemTable->aTbData);
H
Hongze Cheng 已提交
844
  int32_t    iBlockIdx = 0;
H
Hongze Cheng 已提交
845
  int32_t    nBlockIdx = pCommitter->oBlockIdxMap.nItem;
H
Hongze Cheng 已提交
846
  STbData   *pTbData;
H
Hongze Cheng 已提交
847
  SBlockIdx *pBlockIdx = &(SBlockIdx){0};
H
Hongze Cheng 已提交
848

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

H
Hongze Cheng 已提交
851 852
  pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
  if (iBlockIdx < nBlockIdx) {
H
Hongze Cheng 已提交
853
    tMapDataGetItemByIdx(&pCommitter->oBlockIdxMap, iBlockIdx, pBlockIdx, tGetBlockIdx);
H
Hongze Cheng 已提交
854 855
  } else {
    pBlockIdx = NULL;
H
Hongze Cheng 已提交
856 857
  }

H
Hongze Cheng 已提交
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
  // merge
  while (pTbData && pBlockIdx) {
    c = tTABLEIDCmprFn(pTbData, pBlockIdx);

    if (c == 0) {
      // merge commit
      // code = tsdbMergeCommit(pCommitter, pTbData, pBlockIdx);
      // if (code) goto _err;

      iTbData++;
      iBlockIdx++;
      if (iTbData < nTbData) {
        pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
      } else {
        pTbData = NULL;
      }
      if (iBlockIdx < nBlockIdx) {
        tMapDataGetItemByIdx(&pCommitter->oBlockIdxMap, iBlockIdx, pBlockIdx, tGetBlockIdx);
      } else {
        pBlockIdx = NULL;
      }
    } else if (c < 0) {
      // commit memory data
      // code = tsdbCommitMemoryData(pCommitter, pTbData);
      // if (code) goto _err;

      iTbData++;
      if (iTbData < nTbData) {
        pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
      } else {
        pTbData = NULL;
      }
    } else {
      // commit disk data
      // code = tsdbCommitDiskData(pCommitter, pBlockIdx);
      // if (code) goto _err;

      iBlockIdx++;
      if (iBlockIdx < nBlockIdx) {
        tMapDataGetItemByIdx(&pCommitter->oBlockIdxMap, iBlockIdx, pBlockIdx, tGetBlockIdx);
      } else {
        pBlockIdx = NULL;
      }
    }
  }

  // disk
  while (pBlockIdx) {
    // commit disk data
    // code = tsdbCommitDiskData(pCommitter, pBlockIdx);
    // if (code) goto _err;

    iBlockIdx++;
    if (iBlockIdx < nBlockIdx) {
      tMapDataGetItemByIdx(&pCommitter->oBlockIdxMap, iBlockIdx, pBlockIdx, tGetBlockIdx);
    } else {
      pBlockIdx = NULL;
    }
  }

  // memory
  while (pTbData) {
    // commit memory data
    code = tsdbCommitMemoryData(pCommitter, pTbData);
    if (code) goto _err;

    iTbData++;
    if (iTbData < nTbData) {
      pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
    } else {
      pTbData = NULL;
    }
  }

H
Hongze Cheng 已提交
932 933 934
  return code;

_err:
H
Hongze Cheng 已提交
935
  tsdbError("vgId:%d commit file data impl failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
936 937 938 939 940
  return code;
}

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

H
Hongze Cheng 已提交
942
  // write blockIdx
H
Hongze Cheng 已提交
943
  code = tsdbWriteBlockIdx(pCommitter->pWriter, &pCommitter->nBlockIdxMap, NULL);
H
Hongze Cheng 已提交
944 945
  if (code) goto _err;

H
Hongze Cheng 已提交
946
  // update file header
H
Hongze Cheng 已提交
947 948 949
  code = tsdbUpdateDFileSetHeader(pCommitter->pWriter, NULL);
  if (code) goto _err;

H
Hongze Cheng 已提交
950 951 952 953
  // upsert SDFileSet
  code = tsdbFSStateUpsertDFileSet(pCommitter->pTsdb->fs->nState, tsdbDataFWriterGetWSet(pCommitter->pWriter));
  if (code) goto _err;

H
Hongze Cheng 已提交
954
  // close and sync
H
Hongze Cheng 已提交
955
  code = tsdbDataFWriterClose(&pCommitter->pWriter, 1);
H
Hongze Cheng 已提交
956 957 958
  if (code) goto _err;

  if (pCommitter->pReader) {
H
Hongze Cheng 已提交
959
    code = tsdbDataFReaderClose(&pCommitter->pReader);
H
Hongze Cheng 已提交
960 961 962 963 964 965 966
    goto _err;
  }

_exit:
  return code;

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

H
Hongze Cheng 已提交
971
static int32_t tsdbCommitFileData(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
972 973
  int32_t code = 0;

H
Hongze Cheng 已提交
974 975
  // commit file data start
  code = tsdbCommitFileDataStart(pCommitter);
H
Hongze Cheng 已提交
976
  if (code) goto _err;
H
Hongze Cheng 已提交
977

H
Hongze Cheng 已提交
978 979
  // commit file data impl
  code = tsdbCommitFileDataImpl(pCommitter);
H
Hongze Cheng 已提交
980
  if (code) goto _err;
H
Hongze Cheng 已提交
981

H
Hongze Cheng 已提交
982 983
  // commit file data end
  code = tsdbCommitFileDataEnd(pCommitter);
H
Hongze Cheng 已提交
984
  if (code) goto _err;
H
Hongze Cheng 已提交
985 986 987 988

  return code;

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

H
Hongze Cheng 已提交
993 994
// ----------------------------------------------------------------------------
static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitter *pCommitter) {
H
Hongze Cheng 已提交
995
  int32_t code = 0;
H
Hongze Cheng 已提交
996

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

H
Hongze Cheng 已提交
1000 1001 1002 1003
  // lock();
  pTsdb->imem = pTsdb->mem;
  pTsdb->mem = NULL;
  // unlock();
H
Hongze Cheng 已提交
1004

H
Hongze Cheng 已提交
1005
  pCommitter->pTsdb = pTsdb;
H
Hongze Cheng 已提交
1006
  pCommitter->commitID = pTsdb->pVnode->state.commitID;
H
Hongze Cheng 已提交
1007 1008 1009 1010
  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 已提交
1011
  pCommitter->cmprAlg = pTsdb->pVnode->config.tsdbCfg.compression;
H
Hongze Cheng 已提交
1012

H
Hongze Cheng 已提交
1013 1014 1015 1016 1017 1018 1019
  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 已提交
1020 1021 1022
  return code;
}

H
Hongze Cheng 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
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) {
  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 已提交
1057 1058 1059 1060
static int32_t tsdbCommitData(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1061

H
Hongze Cheng 已提交
1062
  // check
H
Hongze Cheng 已提交
1063
  if (pMemTable->nRow == 0) goto _exit;
H
Hongze Cheng 已提交
1064

H
Hongze Cheng 已提交
1065 1066
  // start ====================
  code = tsdbCommitDataStart(pCommitter);
H
Hongze Cheng 已提交
1067
  if (code) goto _err;
H
Hongze Cheng 已提交
1068 1069 1070

  // impl ====================
  pCommitter->nextKey = pMemTable->minKey;
H
Hongze Cheng 已提交
1071 1072 1073 1074 1075
  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 已提交
1076
    if (code) goto _err;
H
Hongze Cheng 已提交
1077
  }
H
Hongze Cheng 已提交
1078

H
Hongze Cheng 已提交
1079 1080 1081
  // end ====================
  tsdbCommitDataEnd(pCommitter);

H
Hongze Cheng 已提交
1082 1083 1084
_exit:
  tsdbDebug("vgId:%d commit data done, nRow:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nRow);
  return code;
H
Hongze Cheng 已提交
1085

H
Hongze Cheng 已提交
1086
_err:
H
Hongze Cheng 已提交
1087
  tsdbCommitDataEnd(pCommitter);
H
Hongze Cheng 已提交
1088 1089 1090
  tsdbError("vgId:%d commit data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  return code;
}
H
Hongze Cheng 已提交
1091

H
Hongze Cheng 已提交
1092 1093 1094 1095
static int32_t tsdbCommitDel(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1096

H
Hongze Cheng 已提交
1097 1098
  if (pMemTable->nDel == 0) {
    goto _exit;
H
Hongze Cheng 已提交
1099
  }
H
Hongze Cheng 已提交
1100

H
Hongze Cheng 已提交
1101 1102 1103 1104 1105
  // start
  code = tsdbCommitDelStart(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1106

H
Hongze Cheng 已提交
1107 1108 1109 1110 1111
  // impl
  code = tsdbCommitDelImpl(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1112

H
Hongze Cheng 已提交
1113 1114 1115 1116 1117
  // end
  code = tsdbCommitDelEnd(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1118

H
Hongze Cheng 已提交
1119
_exit:
H
Hongze Cheng 已提交
1120
  tsdbDebug("vgId:%d commit del done, nDel:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nDel);
H
Hongze Cheng 已提交
1121 1122 1123
  return code;

_err:
H
Hongze Cheng 已提交
1124
  tsdbError("vgId:%d commit del failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1125
  return code;
H
Hongze Cheng 已提交
1126 1127 1128 1129 1130 1131 1132
}

static int32_t tsdbCommitCache(SCommitter *pCommitter) {
  int32_t code = 0;
  // TODO
  return code;
}
H
Hongze Cheng 已提交
1133 1134 1135 1136 1137 1138

static int32_t tsdbEndCommit(SCommitter *pCommitter, int32_t eno) {
  int32_t code = 0;
  // TODO
  return code;
}