tsdbCommit.c 19.5 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 22
  int32_t minutes;
  int8_t  precision;
H
Hongze Cheng 已提交
23 24
  int32_t minRow;
  int32_t maxRow;
H
Hongze Cheng 已提交
25
  // --------------
H
Hongze Cheng 已提交
26
  TSKEY   nextKey;  // need to be reset by each table commit
H
Hongze Cheng 已提交
27 28 29
  int32_t commitFid;
  TSKEY   minKey;
  TSKEY   maxKey;
H
Hongze Cheng 已提交
30
  // commit file data
H
Hongze Cheng 已提交
31 32
  SDataFReader *pReader;
  SMapData      oBlockIdx;  // SMapData<SBlockIdx>, read from reader
H
Hongze Cheng 已提交
33
  SMapData      oBlock;     // SMapData<SBlock>, read from reader
H
Hongze Cheng 已提交
34 35
  SDataFWriter *pWriter;
  SMapData      nBlockIdx;  // SMapData<SBlockIdx>, build by committer
H
Hongze Cheng 已提交
36
  SMapData      nBlock;     // SMapData<SBlock>
H
Hongze Cheng 已提交
37
  /* commit del */
H
Hongze Cheng 已提交
38
  SDelFReader *pDelFReader;
H
Hongze Cheng 已提交
39 40
  SMapData     oDelIdxMap;   // SMapData<SDelIdx>, old
  SMapData     oDelDataMap;  // SMapData<SDelData>, old
H
Hongze Cheng 已提交
41
  SDelFWriter *pDelFWriter;
H
Hongze Cheng 已提交
42 43
  SMapData     nDelIdxMap;   // SMapData<SDelIdx>, new
  SMapData     nDelDataMap;  // SMapData<SDelData>, new
H
Hongze Cheng 已提交
44
} SCommitter;
H
refact  
Hongze Cheng 已提交
45

H
Hongze Cheng 已提交
46 47 48 49 50
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 已提交
51

H
refact  
Hongze Cheng 已提交
52
int32_t tsdbBegin(STsdb *pTsdb) {
H
Hongze Cheng 已提交
53
  int32_t code = 0;
H
Hongze Cheng 已提交
54

H
Hongze Cheng 已提交
55 56 57
  code = tsdbMemTableCreate(pTsdb, &pTsdb->mem);
  if (code) {
    goto _err;
H
Hongze Cheng 已提交
58 59
  }

H
Hongze Cheng 已提交
60 61 62 63
  return code;

_err:
  return code;
H
Hongze Cheng 已提交
64 65
}

H
more  
Hongze Cheng 已提交
66
int32_t tsdbCommit(STsdb *pTsdb) {
67
  if (!pTsdb) return 0;
H
Hongze Cheng 已提交
68

H
more  
Hongze Cheng 已提交
69
  int32_t    code = 0;
H
Hongze Cheng 已提交
70 71 72 73 74 75 76 77 78
  SCommitter commith;
  SMemTable *pMemTable = pTsdb->mem;

  // check
  if (pMemTable->nRow == 0 && pMemTable->nDel == 0) {  // TODO
    pTsdb->mem = NULL;
    tsdbMemTableDestroy(pMemTable);
    goto _exit;
  }
H
refact  
Hongze Cheng 已提交
79

H
more  
Hongze Cheng 已提交
80
  // start commit
H
more  
Hongze Cheng 已提交
81 82 83
  code = tsdbStartCommit(pTsdb, &commith);
  if (code) {
    goto _err;
H
refact  
Hongze Cheng 已提交
84 85
  }

H
refact  
Hongze Cheng 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  // commit impl
  code = tsdbCommitData(&commith);
  if (code) {
    goto _err;
  }

  code = tsdbCommitDel(&commith);
  if (code) {
    goto _err;
  }

  code = tsdbCommitCache(&commith);
  if (code) {
    goto _err;
  }

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
149 150 151 152 153 154 155 156 157 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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
static int32_t tsdbCommitTableDel(SCommitter *pCommitter, STbData *pTbData, SDelIdx *pDelIdx) {
  int32_t  code = 0;
  SDelData delData;
  SDelOp  *pDelOp;
  tb_uid_t suid;
  tb_uid_t uid;
  SDelIdx  delIdx;  // TODO

  // 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;
  delIdx.maxVersion = -1;

  // 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++) {
    code = tMapDataGetItemByIdx(&pCommitter->oDelDataMap, iDelData, &delData, tGetDelData);
    if (code) goto _err;

    code = tMapDataPutItem(&pCommitter->nDelDataMap, &delData, tPutDelData);
    if (code) goto _err;

    if (delIdx.minKey > delData.sKey) delIdx.minKey = delData.sKey;
    if (delIdx.maxKey < delData.eKey) delIdx.maxKey = delData.eKey;
    if (delIdx.minVersion > delData.version) delIdx.minVersion = delData.version;
    if (delIdx.maxVersion < delData.version) delIdx.maxVersion = delData.version;
  }

  // memory
  pDelOp = pTbData ? pTbData->pHead : NULL;
  for (; pDelOp; pDelOp = pDelOp->pNext) {
    delData.version = pDelOp->version;
    delData.sKey = pDelOp->sKey;
    delData.eKey = pDelOp->eKey;

    code = tMapDataPutItem(&pCommitter->nDelDataMap, &delData, tPutDelData);
    if (code) goto _err;

    if (delIdx.minKey > delData.sKey) delIdx.minKey = delData.sKey;
    if (delIdx.maxKey < delData.eKey) delIdx.maxKey = delData.eKey;
    if (delIdx.minVersion > delData.version) delIdx.minVersion = delData.version;
    if (delIdx.maxVersion < delData.version) delIdx.maxVersion = delData.version;
  }

  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 已提交
233
static int32_t tsdbCommitDelImpl(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246
  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 已提交
247

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

H
Hongze Cheng 已提交
274 275 276 277 278 279 280
  _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 已提交
281 282
      pTbData = NULL;
    }
H
Hongze Cheng 已提交
283
    continue;
H
Hongze Cheng 已提交
284

H
Hongze Cheng 已提交
285 286 287 288 289 290 291 292 293 294 295 296
  _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 已提交
297

H
Hongze Cheng 已提交
298 299
  _commit_mem_and_disk_del:
    code = tsdbCommitTableDel(pCommitter, pTbData, pDelIdx);
H
Hongze Cheng 已提交
300
    if (code) goto _err;
H
Hongze Cheng 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    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 已提交
316 317
  }

H
Hongze Cheng 已提交
318
  return code;
H
Hongze Cheng 已提交
319 320 321 322

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

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

H
Hongze Cheng 已提交
328 329
  code = tsdbWriteDelIdx(pCommitter->pDelFWriter, &pCommitter->nDelIdxMap, NULL);
  if (code) goto _err;
H
Hongze Cheng 已提交
330

H
Hongze Cheng 已提交
331
  code = tsdbUpdateDelFileHdr(pCommitter->pDelFWriter, NULL);
H
Hongze Cheng 已提交
332
  if (code) goto _err;
H
Hongze Cheng 已提交
333 334

  code = tsdbDelFWriterClose(pCommitter->pDelFWriter, 1);
H
Hongze Cheng 已提交
335
  if (code) goto _err;
H
Hongze Cheng 已提交
336 337 338 339 340 341

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

H
Hongze Cheng 已提交
342 343 344
  return code;

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

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

H
Hongze Cheng 已提交
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
static int32_t tsdbMergeCommit(SCommitter *pCommitter, SBlockIdx *pBlockIdx, STbDataIter *pIter, SBlock *pBlock) {
  int32_t    code = 0;
  TSDBROW   *pRow;
  SBlock     block = BLOCK_INIT_VAL;
  SBlockData bData;

  if (pBlock == NULL) {
    while (true) {
      pRow = tsdbTbDataIterGet(pIter);

      if (pRow == NULL || pRow->pTSRow->ts > pCommitter->maxKey) {
        if (bData.nRow == 0) {
          break;
        } else {
          goto _write_block_data;
        }
      }

      code = tsdbBlockDataAppendRow(&bData, pRow, NULL /*TODO*/);
      if (code) goto _err;

      if (bData.nRow >= pCommitter->maxRow * 4 / 5) {
        goto _write_block_data;
      } else {
        continue;
      }

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

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

      // reset block and bdata
      block = BLOCK_INIT_VAL;
      tsdbBlockDataReset(&bData);
    }
  } else if (pBlock->last) {
    // 1. read last block data
    // 2. loop to merge memory data and last block data to write to .data file or .last file
  } else {
    // while (true) {
    //   pRow = tsdbTbDataIterGet(pIter);

    //   if (pRow == NULL) /* code */
    // }
  }

  return code;

_err:
  tsdbError("vgId:%d merge commit failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
405
  return code;
H
Hongze Cheng 已提交
406 407
}

H
Hongze Cheng 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
static int32_t tsdbCommitTableData(SCommitter *pCommitter, STbData *pTbData, SBlockIdx *pBlockIdx) {
  int32_t      code = 0;
  STbDataIter  iter;
  STbDataIter *pIter = &iter;
  TSDBROW     *pRow;
  SBlockIdx    blockIdx;  // TODO

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

  // check
  pRow = tsdbTbDataIterGet(pIter);
  if (ROW_END(pRow, pCommitter->maxKey) && pBlockIdx == NULL) goto _exit;
H
Hongze Cheng 已提交
425 426 427 428 429 430 431 432

  // start ================================
  tMapDataReset(&pCommitter->oBlock);
  tMapDataReset(&pCommitter->nBlock);
  if (pBlockIdx) {
    code = tsdbReadBlock(pCommitter->pReader, pBlockIdx, &pCommitter->oBlock, NULL);
    if (code) goto _err;
  }
H
Hongze Cheng 已提交
433

H
Hongze Cheng 已提交
434
  // impl ===============================
H
Hongze Cheng 已提交
435 436 437 438
  SBlock  block;
  SBlock *pBlock = &block;
  int32_t iBlock = 0;
  int32_t nBlock = pCommitter->oBlock.nItem;
H
Hongze Cheng 已提交
439

H
Hongze Cheng 已提交
440 441 442 443
  // merge
  pRow = tsdbTbDataIterGet(pIter);
  while (!ROW_END(pRow, pCommitter->maxKey) && iBlock < nBlock) {
    tMapDataGetItemByIdx(&pCommitter->oBlock, iBlock, pBlock, tGetBlock);
H
Hongze Cheng 已提交
444
    code = tsdbMergeCommit(pCommitter, &blockIdx, pIter, pBlock);
H
Hongze Cheng 已提交
445 446 447 448
    if (code) goto _err;

    pRow = tsdbTbDataIterGet(pIter);
    iBlock++;
H
Hongze Cheng 已提交
449
  }
H
Hongze Cheng 已提交
450

H
Hongze Cheng 已提交
451 452 453
  // mem
  pRow = tsdbTbDataIterGet(pIter);
  while (!ROW_END(pRow, pCommitter->maxKey)) {
H
Hongze Cheng 已提交
454
    code = tsdbMergeCommit(pCommitter, &blockIdx, pIter, NULL);
H
Hongze Cheng 已提交
455
    if (code) goto _err;
H
Hongze Cheng 已提交
456

H
Hongze Cheng 已提交
457 458
    pRow = tsdbTbDataIterGet(pIter);
  }
H
Hongze Cheng 已提交
459

H
Hongze Cheng 已提交
460 461 462 463
  // disk
  while (iBlock < nBlock) {
    tMapDataGetItemByIdx(&pCommitter->oBlock, iBlock, pBlock, tGetBlock);

H
Hongze Cheng 已提交
464
    code = tsdbMergeCommit(pCommitter, &blockIdx, NULL, pBlock);
H
Hongze Cheng 已提交
465 466 467
    if (code) goto _err;

    iBlock++;
H
Hongze Cheng 已提交
468 469
  }

H
Hongze Cheng 已提交
470 471 472 473 474 475
  // end ===============================
  code = tsdbWriteBlock(pCommitter->pWriter, &pCommitter->nBlock, NULL, &blockIdx);
  if (code) goto _err;

  code = tMapDataPutItem(&pCommitter->nBlockIdx, &blockIdx, tPutBlockIdx);
  if (code) goto _err;
H
Hongze Cheng 已提交
476

H
Hongze Cheng 已提交
477
_exit:
H
Hongze Cheng 已提交
478 479 480 481 482 483 484 485
  pRow = tsdbTbDataIterGet(pIter);
  if (pRow) {
    ASSERT(pRow->pTSRow->ts > pCommitter->maxKey);
    if (pCommitter->nextKey > pRow->pTSRow->ts) {
      pCommitter->nextKey = pRow->pTSRow->ts;
    }
  }

H
Hongze Cheng 已提交
486 487 488
  return code;

_err:
H
Hongze Cheng 已提交
489
  tsdbError("vgId:%d commit Table data failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
490 491 492 493 494 495
  return code;
}

static int32_t tsdbCommitFileDataStart(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
H
Hongze Cheng 已提交
496 497
  SDFileSet *pRSet = NULL;  // TODO
  SDFileSet *pWSet = NULL;  // TODO
H
Hongze Cheng 已提交
498

H
Hongze Cheng 已提交
499
  // memory
H
Hongze Cheng 已提交
500
  pCommitter->nextKey = TSKEY_MAX;
H
Hongze Cheng 已提交
501
  tMapDataReset(&pCommitter->oBlockIdx);
H
Hongze Cheng 已提交
502
  tMapDataReset(&pCommitter->oBlock);
H
Hongze Cheng 已提交
503
  tMapDataReset(&pCommitter->nBlockIdx);
H
Hongze Cheng 已提交
504
  tMapDataReset(&pCommitter->nBlock);
H
Hongze Cheng 已提交
505

H
Hongze Cheng 已提交
506
  // load old
H
Hongze Cheng 已提交
507
  if (pRSet) {
H
Hongze Cheng 已提交
508
    code = tsdbDataFReaderOpen(&pCommitter->pReader, pTsdb, pRSet);
H
Hongze Cheng 已提交
509 510
    if (code) goto _err;

H
Hongze Cheng 已提交
511
    code = tsdbReadBlockIdx(pCommitter->pReader, &pCommitter->oBlockIdx, NULL);
H
Hongze Cheng 已提交
512
    if (code) goto _err;
H
Hongze Cheng 已提交
513 514
  }

H
Hongze Cheng 已提交
515
  // create new
H
Hongze Cheng 已提交
516
  code = tsdbDataFWriterOpen(&pCommitter->pWriter, pTsdb, pWSet);
H
Hongze Cheng 已提交
517 518 519 520 521 522
  if (code) goto _err;

_exit:
  return code;

_err:
H
Hongze Cheng 已提交
523
  tsdbError("vgId:%d commit file data start failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
524 525 526 527 528
  return code;
}

static int32_t tsdbCommitFileDataImpl(SCommitter *pCommitter) {
  int32_t    code = 0;
H
Hongze Cheng 已提交
529
  int32_t    c;
H
Hongze Cheng 已提交
530 531 532 533
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
  int32_t    iTbData = 0;
  int32_t    nTbData = taosArrayGetSize(pMemTable->aTbData);
H
Hongze Cheng 已提交
534 535
  int32_t    iBlockIdx = 0;
  int32_t    nBlockIdx = pCommitter->oBlockIdx.nItem;
H
Hongze Cheng 已提交
536
  STbData   *pTbData;
H
Hongze Cheng 已提交
537
  SBlockIdx *pBlockIdx;
H
Hongze Cheng 已提交
538
  SBlockIdx  blockIdx;
H
Hongze Cheng 已提交
539

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

H
Hongze Cheng 已提交
542 543
  pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
  if (iBlockIdx < nBlockIdx) {
H
Hongze Cheng 已提交
544
    pBlockIdx = &blockIdx;
H
Hongze Cheng 已提交
545
    code = tMapDataGetItemByIdx(&pCommitter->oBlockIdx, iBlockIdx, pBlockIdx, tGetBlockIdx);
H
Hongze Cheng 已提交
546
    if (code) goto _err;
H
Hongze Cheng 已提交
547 548
  } else {
    pBlockIdx = NULL;
H
Hongze Cheng 已提交
549 550 551 552
  }

  while (true) {
    if (pTbData == NULL && pBlockIdx == NULL) break;
H
Hongze Cheng 已提交
553 554

    if (pTbData && pBlockIdx) {
H
Hongze Cheng 已提交
555 556
      c = tTABLEIDCmprFn(pTbData, pBlockIdx);

H
Hongze Cheng 已提交
557
      if (c == 0) {
H
Hongze Cheng 已提交
558
        goto _commit_mem_and_disk_data;
H
Hongze Cheng 已提交
559
      } else if (c < 0) {
H
Hongze Cheng 已提交
560
        goto _commit_mem_data;
H
Hongze Cheng 已提交
561
      } else {
H
Hongze Cheng 已提交
562
        goto _commit_disk_data;
H
Hongze Cheng 已提交
563
      }
H
Hongze Cheng 已提交
564 565
    } else if (pTbData) {
      goto _commit_mem_data;
H
Hongze Cheng 已提交
566
    } else {
H
Hongze Cheng 已提交
567
      goto _commit_disk_data;
H
Hongze Cheng 已提交
568 569
    }

H
Hongze Cheng 已提交
570 571 572
  _commit_mem_data:
    code = tsdbCommitTableData(pCommitter, pTbData, NULL);
    if (code) goto _err;
H
Hongze Cheng 已提交
573

H
Hongze Cheng 已提交
574 575 576 577
    iTbData++;
    if (iTbData < nTbData) {
      pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
    } else {
H
Hongze Cheng 已提交
578
      pTbData = NULL;
H
Hongze Cheng 已提交
579
    }
H
Hongze Cheng 已提交
580
    continue;
H
Hongze Cheng 已提交
581

H
Hongze Cheng 已提交
582 583 584
  _commit_disk_data:
    code = tsdbCommitTableData(pCommitter, NULL, pBlockIdx);
    if (code) goto _err;
H
Hongze Cheng 已提交
585

H
Hongze Cheng 已提交
586 587
    iBlockIdx++;
    if (iBlockIdx < nBlockIdx) {
H
Hongze Cheng 已提交
588
      pBlockIdx = &blockIdx;
H
Hongze Cheng 已提交
589
      code = tMapDataGetItemByIdx(&pCommitter->oBlockIdx, iBlockIdx, pBlockIdx, tGetBlockIdx);
H
Hongze Cheng 已提交
590 591 592 593 594
      if (code) goto _err;
    } else {
      pBlockIdx = NULL;
    }
    continue;
H
Hongze Cheng 已提交
595

H
Hongze Cheng 已提交
596 597
  _commit_mem_and_disk_data:
    code = tsdbCommitTableData(pCommitter, pTbData, pBlockIdx);
H
Hongze Cheng 已提交
598
    if (code) goto _err;
H
Hongze Cheng 已提交
599

H
Hongze Cheng 已提交
600 601 602 603 604 605 606 607
    iTbData++;
    iBlockIdx++;
    if (iTbData < nTbData) {
      pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
    } else {
      pTbData = NULL;
    }
    if (iBlockIdx < nBlockIdx) {
H
Hongze Cheng 已提交
608
      pBlockIdx = &blockIdx;
H
Hongze Cheng 已提交
609
      code = tMapDataGetItemByIdx(&pCommitter->oBlockIdx, iBlockIdx, pBlockIdx, tGetBlockIdx);
H
Hongze Cheng 已提交
610 611 612 613 614
      if (code) goto _err;
    } else {
      pBlockIdx = NULL;
    }
    continue;
H
Hongze Cheng 已提交
615 616 617 618 619
  }

  return code;

_err:
H
Hongze Cheng 已提交
620
  tsdbError("vgId:%d commit file data impl failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
621 622 623 624 625
  return code;
}

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

H
Hongze Cheng 已提交
627 628
  // write blockIdx
  code = tsdbWriteBlockIdx(pCommitter->pWriter, &pCommitter->nBlockIdx, NULL);
H
Hongze Cheng 已提交
629 630
  if (code) goto _err;

H
Hongze Cheng 已提交
631
  // update file header
H
Hongze Cheng 已提交
632 633 634
  code = tsdbUpdateDFileSetHeader(pCommitter->pWriter, NULL);
  if (code) goto _err;

H
Hongze Cheng 已提交
635 636
  // close and sync
  code = tsdbDataFWriterClose(pCommitter->pWriter, 1);
H
Hongze Cheng 已提交
637 638 639
  if (code) goto _err;

  if (pCommitter->pReader) {
H
Hongze Cheng 已提交
640
    code = tsdbDataFReaderClose(pCommitter->pReader);
H
Hongze Cheng 已提交
641 642 643 644 645 646 647
    goto _err;
  }

_exit:
  return code;

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

H
Hongze Cheng 已提交
652
static int32_t tsdbCommitFileData(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
653 654
  int32_t code = 0;

H
Hongze Cheng 已提交
655 656
  // commit file data start
  code = tsdbCommitFileDataStart(pCommitter);
H
Hongze Cheng 已提交
657 658 659 660
  if (code) {
    goto _err;
  }

H
Hongze Cheng 已提交
661 662
  // commit file data impl
  code = tsdbCommitFileDataImpl(pCommitter);
H
Hongze Cheng 已提交
663 664 665 666
  if (code) {
    goto _err;
  }

H
Hongze Cheng 已提交
667 668
  // commit file data end
  code = tsdbCommitFileDataEnd(pCommitter);
H
Hongze Cheng 已提交
669 670 671 672 673 674 675 676 677 678
  if (code) {
    goto _err;
  }

  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
679 680
// ----------------------------------------------------------------------------
static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitter *pCommitter) {
H
Hongze Cheng 已提交
681
  int32_t code = 0;
H
Hongze Cheng 已提交
682

H
Hongze Cheng 已提交
683 684 685 686 687 688
  memset(pCommitter, 0, sizeof(*pCommitter));
  ASSERT(pTsdb->mem && pTsdb->imem == NULL);
  // lock();
  pTsdb->imem = pTsdb->mem;
  pTsdb->mem = NULL;
  // unlock();
H
Hongze Cheng 已提交
689

H
Hongze Cheng 已提交
690
  pCommitter->pTsdb = pTsdb;
H
Hongze Cheng 已提交
691 692 693 694

  return code;
}

H
Hongze Cheng 已提交
695 696 697 698
static int32_t tsdbCommitData(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
699

H
Hongze Cheng 已提交
700
  // check
H
Hongze Cheng 已提交
701
  if (pMemTable->nRow == 0) goto _exit;
H
Hongze Cheng 已提交
702

H
Hongze Cheng 已提交
703 704 705 706 707 708 709
  // loop
  pCommitter->nextKey = pMemTable->minKey.ts;
  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 已提交
710
    if (code) goto _err;
H
Hongze Cheng 已提交
711
  }
H
Hongze Cheng 已提交
712

H
Hongze Cheng 已提交
713 714 715
_exit:
  tsdbDebug("vgId:%d commit data done, nRow:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nRow);
  return code;
H
Hongze Cheng 已提交
716

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

H
Hongze Cheng 已提交
722 723 724 725
static int32_t tsdbCommitDel(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
726

H
Hongze Cheng 已提交
727 728
  if (pMemTable->nDel == 0) {
    goto _exit;
H
Hongze Cheng 已提交
729
  }
H
Hongze Cheng 已提交
730

H
Hongze Cheng 已提交
731 732 733 734 735
  // start
  code = tsdbCommitDelStart(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
736

H
Hongze Cheng 已提交
737 738 739 740 741
  // impl
  code = tsdbCommitDelImpl(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
742

H
Hongze Cheng 已提交
743 744 745 746 747
  // end
  code = tsdbCommitDelEnd(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
748

H
Hongze Cheng 已提交
749
_exit:
H
Hongze Cheng 已提交
750
  tsdbDebug("vgId:%d commit del done, nDel:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nDel);
H
Hongze Cheng 已提交
751 752 753
  return code;

_err:
H
Hongze Cheng 已提交
754
  tsdbError("vgId:%d commit del failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
755
  return code;
H
Hongze Cheng 已提交
756 757 758 759 760 761 762
}

static int32_t tsdbCommitCache(SCommitter *pCommitter) {
  int32_t code = 0;
  // TODO
  return code;
}
H
Hongze Cheng 已提交
763 764 765 766 767 768

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