tsdbCommit.c 32.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 18 19 20 21
typedef struct {
  int64_t   suid;
  int64_t   uid;
  STSchema *pTSchema;
} SSkmInfo;
H
Hongze Cheng 已提交
22

H
Hongze Cheng 已提交
23
typedef struct {
H
Hongze Cheng 已提交
24
  STsdb *pTsdb;
H
Hongze Cheng 已提交
25
  /* commit data */
H
Hongze Cheng 已提交
26
  int64_t commitID;
H
Hongze Cheng 已提交
27 28
  int32_t minutes;
  int8_t  precision;
H
Hongze Cheng 已提交
29 30
  int32_t minRow;
  int32_t maxRow;
H
Hongze Cheng 已提交
31
  int8_t  cmprAlg;
H
Hongze Cheng 已提交
32
  STsdbFS fs;
H
Hongze Cheng 已提交
33
  // --------------
H
Hongze Cheng 已提交
34
  TSKEY   nextKey;  // reset by each table commit
H
Hongze Cheng 已提交
35 36 37
  int32_t commitFid;
  TSKEY   minKey;
  TSKEY   maxKey;
H
Hongze Cheng 已提交
38
  // commit file data
H
Hongze Cheng 已提交
39
  SDataFReader *pReader;
H
Hongze Cheng 已提交
40 41
  SArray       *aBlockIdx;  // SArray<SBlockIdx>
  SMapData      oBlockMap;  // SMapData<SBlock>, read from reader
H
Hongze Cheng 已提交
42
  SBlockData    oBlockData;
H
Hongze Cheng 已提交
43
  SDataFWriter *pWriter;
H
Hongze Cheng 已提交
44 45
  SArray       *aBlockIdxN;  // SArray<SBlockIdx>
  SMapData      nBlockMap;   // SMapData<SBlock>
H
Hongze Cheng 已提交
46
  SBlockData    nBlockData;
H
Hongze Cheng 已提交
47 48
  SSkmInfo      skmTable;
  SSkmInfo      skmRow;
H
Hongze Cheng 已提交
49
  /* commit del */
H
Hongze Cheng 已提交
50 51
  SDelFReader *pDelFReader;
  SDelFWriter *pDelFWriter;
H
Hongze Cheng 已提交
52 53 54
  SArray      *aDelIdx;   // SArray<SDelIdx>
  SArray      *aDelIdxN;  // SArray<SDelIdx>
  SArray      *aDelData;  // SArray<SDelData>
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 69
  SMemTable *pMemTable;
  code = tsdbMemTableCreate(pTsdb, &pMemTable);
H
Hongze Cheng 已提交
70
  if (code) goto _err;
H
Hongze Cheng 已提交
71

H
Hongze Cheng 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  // lock
  code = taosThreadRwlockWrlock(&pTsdb->rwLock);
  if (code) {
    code = TAOS_SYSTEM_ERROR(code);
    goto _err;
  }

  pTsdb->mem = pMemTable;

  // unlock
  code = taosThreadRwlockUnlock(&pTsdb->rwLock);
  if (code) {
    code = TAOS_SYSTEM_ERROR(code);
    goto _err;
  }

H
Hongze Cheng 已提交
88 89 90
  return code;

_err:
H
Hongze Cheng 已提交
91
  tsdbError("vgId:%d tsdb begin failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
92
  return code;
H
Hongze Cheng 已提交
93 94
}

H
more  
Hongze Cheng 已提交
95
int32_t tsdbCommit(STsdb *pTsdb) {
96
  if (!pTsdb) return 0;
H
Hongze Cheng 已提交
97

H
more  
Hongze Cheng 已提交
98
  int32_t    code = 0;
H
Hongze Cheng 已提交
99 100 101 102
  SCommitter commith;
  SMemTable *pMemTable = pTsdb->mem;

  // check
H
Hongze Cheng 已提交
103
  if (pMemTable->nRow == 0 && pMemTable->nDel == 0) {
H
Hongze Cheng 已提交
104
    taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
105
    pTsdb->mem = NULL;
H
Hongze Cheng 已提交
106 107 108
    taosThreadRwlockUnlock(&pTsdb->rwLock);

    tsdbUnrefMemTable(pMemTable);
H
Hongze Cheng 已提交
109 110
    goto _exit;
  }
H
refact  
Hongze Cheng 已提交
111

H
more  
Hongze Cheng 已提交
112
  // start commit
H
more  
Hongze Cheng 已提交
113
  code = tsdbStartCommit(pTsdb, &commith);
H
Hongze Cheng 已提交
114
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
115

H
refact  
Hongze Cheng 已提交
116 117
  // commit impl
  code = tsdbCommitData(&commith);
H
Hongze Cheng 已提交
118
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
119 120

  code = tsdbCommitDel(&commith);
H
Hongze Cheng 已提交
121
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
122 123

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

H
Hongze Cheng 已提交
127
_exit:
H
refact  
Hongze Cheng 已提交
128 129 130
  return code;

_err:
H
Hongze Cheng 已提交
131
  tsdbEndCommit(&commith, code);
C
Cary Xu 已提交
132
  tsdbError("vgId:%d, failed to commit since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
refact  
Hongze Cheng 已提交
133 134 135
  return code;
}

H
Hongze Cheng 已提交
136
static int32_t tsdbCommitDelStart(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
137 138 139 140
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

H
Hongze Cheng 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  pCommitter->aDelIdx = taosArrayInit(0, sizeof(SDelIdx));
  if (pCommitter->aDelIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  pCommitter->aDelData = taosArrayInit(0, sizeof(SDelData));
  if (pCommitter->aDelData == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  pCommitter->aDelIdxN = taosArrayInit(0, sizeof(SDelIdx));
  if (pCommitter->aDelIdxN == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
158

H
Hongze Cheng 已提交
159
  SDelFile *pDelFileR = pCommitter->fs.pDelFile;
H
Hongze Cheng 已提交
160
  if (pDelFileR) {
H
Hongze Cheng 已提交
161
    code = tsdbDelFReaderOpen(&pCommitter->pDelFReader, pDelFileR, pTsdb, NULL);
H
Hongze Cheng 已提交
162
    if (code) goto _err;
H
Hongze Cheng 已提交
163

H
Hongze Cheng 已提交
164
    code = tsdbReadDelIdx(pCommitter->pDelFReader, pCommitter->aDelIdx, NULL);
H
Hongze Cheng 已提交
165
    if (code) goto _err;
H
Hongze Cheng 已提交
166 167
  }

H
Hongze Cheng 已提交
168
  // prepare new
H
Hongze Cheng 已提交
169 170
  SDelFile wDelFile = {.commitID = pCommitter->commitID, .size = 0, .offset = 0};
  code = tsdbDelFWriterOpen(&pCommitter->pDelFWriter, &wDelFile, pTsdb);
H
Hongze Cheng 已提交
171
  if (code) goto _err;
H
Hongze Cheng 已提交
172 173 174 175 176 177 178

_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 已提交
179 180 181
  return code;
}

H
Hongze Cheng 已提交
182
static int32_t tsdbCommitTableDel(SCommitter *pCommitter, STbData *pTbData, SDelIdx *pDelIdx) {
H
Hongze Cheng 已提交
183
  int32_t   code = 0;
H
Hongze Cheng 已提交
184
  SDelData *pDelData;
H
Hongze Cheng 已提交
185 186
  tb_uid_t  suid;
  tb_uid_t  uid;
H
Hongze Cheng 已提交
187 188

  if (pTbData) {
H
Hongze Cheng 已提交
189 190
    suid = pTbData->suid;
    uid = pTbData->uid;
H
Hongze Cheng 已提交
191

H
Hongze Cheng 已提交
192 193 194 195
    if (pTbData->pHead == NULL) {
      pTbData = NULL;
    }
  }
H
Hongze Cheng 已提交
196 197

  if (pDelIdx) {
H
Hongze Cheng 已提交
198 199 200 201
    suid = pDelIdx->suid;
    uid = pDelIdx->uid;

    code = tsdbReadDelData(pCommitter->pDelFReader, pDelIdx, pCommitter->aDelData, NULL);
H
Hongze Cheng 已提交
202
    if (code) goto _err;
203 204
  } else {
    taosArrayClear(pCommitter->aDelData);
H
Hongze Cheng 已提交
205 206
  }

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

H
Hongze Cheng 已提交
209
  SDelIdx delIdx = {.suid = suid, .uid = uid};
H
Hongze Cheng 已提交
210 211

  // memory
H
Hongze Cheng 已提交
212 213
  pDelData = pTbData ? pTbData->pHead : NULL;
  for (; pDelData; pDelData = pDelData->pNext) {
H
Hongze Cheng 已提交
214 215 216 217
    if (taosArrayPush(pCommitter->aDelData, pDelData) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
218 219 220
  }

  // write
H
Hongze Cheng 已提交
221
  code = tsdbWriteDelData(pCommitter->pDelFWriter, pCommitter->aDelData, NULL, &delIdx);
H
Hongze Cheng 已提交
222 223 224
  if (code) goto _err;

  // put delIdx
225
  if (taosArrayPush(pCommitter->aDelIdxN, &delIdx) == NULL) {
H
Hongze Cheng 已提交
226 227 228
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
229 230 231 232 233 234 235 236 237

_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 已提交
238 239
static int32_t tsdbCommitDelEnd(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
240
  STsdb  *pTsdb = pCommitter->pTsdb;
H
Hongze Cheng 已提交
241

H
Hongze Cheng 已提交
242
  code = tsdbWriteDelIdx(pCommitter->pDelFWriter, pCommitter->aDelIdxN, NULL);
H
Hongze Cheng 已提交
243
  if (code) goto _err;
H
Hongze Cheng 已提交
244

H
Hongze Cheng 已提交
245 246 247
  code = tsdbUpdateDelFileHdr(pCommitter->pDelFWriter);
  if (code) goto _err;

H
Hongze Cheng 已提交
248
  code = tsdbFSUpsertDelFile(&pCommitter->fs, &pCommitter->pDelFWriter->fDel);
H
Hongze Cheng 已提交
249
  if (code) goto _err;
H
Hongze Cheng 已提交
250

H
Hongze Cheng 已提交
251
  code = tsdbDelFWriterClose(&pCommitter->pDelFWriter, 1);
H
Hongze Cheng 已提交
252
  if (code) goto _err;
H
Hongze Cheng 已提交
253 254

  if (pCommitter->pDelFReader) {
H
Hongze Cheng 已提交
255
    code = tsdbDelFReaderClose(&pCommitter->pDelFReader);
H
Hongze Cheng 已提交
256 257 258
    if (code) goto _err;
  }

H
Hongze Cheng 已提交
259 260 261 262
  taosArrayDestroy(pCommitter->aDelIdx);
  taosArrayDestroy(pCommitter->aDelData);
  taosArrayDestroy(pCommitter->aDelIdxN);

H
Hongze Cheng 已提交
263 264 265
  return code;

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

H
Hongze Cheng 已提交
270 271 272 273
static int32_t tsdbCommitFileDataStart(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SDFileSet *pRSet = NULL;
H
Hongze Cheng 已提交
274

H
Hongze Cheng 已提交
275 276
  // memory
  pCommitter->nextKey = TSKEY_MAX;
H
Hongze Cheng 已提交
277

H
Hongze Cheng 已提交
278
  // old
H
Hongze Cheng 已提交
279
  taosArrayClear(pCommitter->aBlockIdx);
H
Hongze Cheng 已提交
280 281
  tMapDataReset(&pCommitter->oBlockMap);
  tBlockDataReset(&pCommitter->oBlockData);
H
Hongze Cheng 已提交
282 283
  pRSet = (SDFileSet *)taosArraySearch(pCommitter->fs.aDFileSet, &(SDFileSet){.fid = pCommitter->commitFid},
                                       tDFileSetCmprFn, TD_EQ);
H
Hongze Cheng 已提交
284 285
  if (pRSet) {
    code = tsdbDataFReaderOpen(&pCommitter->pReader, pTsdb, pRSet);
H
Hongze Cheng 已提交
286 287
    if (code) goto _err;

H
Hongze Cheng 已提交
288
    code = tsdbReadBlockIdx(pCommitter->pReader, pCommitter->aBlockIdx, NULL);
H
Hongze Cheng 已提交
289
    if (code) goto _err;
H
Hongze Cheng 已提交
290
  }
H
Hongze Cheng 已提交
291

H
Hongze Cheng 已提交
292
  // new
H
Hongze Cheng 已提交
293 294 295 296 297 298
  SHeadFile fHead;
  SDataFile fData;
  SLastFile fLast;
  SSmaFile  fSma;
  SDFileSet wSet = {.pHeadF = &fHead, .pDataF = &fData, .pLastF = &fLast, .pSmaF = &fSma};

H
Hongze Cheng 已提交
299
  taosArrayClear(pCommitter->aBlockIdxN);
H
Hongze Cheng 已提交
300
  tMapDataReset(&pCommitter->nBlockMap);
H
Hongze Cheng 已提交
301
  tBlockDataReset(&pCommitter->nBlockData);
H
Hongze Cheng 已提交
302
  if (pRSet) {
H
Hongze Cheng 已提交
303 304 305 306 307 308
    wSet.diskId = pRSet->diskId;
    wSet.fid = pCommitter->commitFid;
    fHead = (SHeadFile){.commitID = pCommitter->commitID, .offset = 0, .size = 0};
    fData = *pRSet->pDataF;
    fLast = (SLastFile){.commitID = pCommitter->commitID, .size = 0};
    fSma = *pRSet->pSmaF;
H
Hongze Cheng 已提交
309
  } else {
H
Hongze Cheng 已提交
310 311 312 313 314 315
    wSet.diskId = (SDiskID){.level = 0, .id = 0};
    wSet.fid = pCommitter->commitFid;
    fHead = (SHeadFile){.commitID = pCommitter->commitID, .offset = 0, .size = 0};
    fData = (SDataFile){.commitID = pCommitter->commitID, .size = 0};
    fLast = (SLastFile){.commitID = pCommitter->commitID, .size = 0};
    fSma = (SSmaFile){.commitID = pCommitter->commitID, .size = 0};
H
Hongze Cheng 已提交
316
  }
H
Hongze Cheng 已提交
317 318
  code = tsdbDataFWriterOpen(&pCommitter->pWriter, pTsdb, &wSet);
  if (code) goto _err;
H
Hongze Cheng 已提交
319

H
Hongze Cheng 已提交
320
_exit:
H
Hongze Cheng 已提交
321 322 323
  return code;

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

H
Hongze Cheng 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
static int32_t tsdbCommitterUpdateTableSchema(SCommitter *pCommitter, int64_t suid, int64_t uid, int32_t sver) {
  int32_t code = 0;

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

  pCommitter->skmTable.suid = suid;
  pCommitter->skmTable.uid = uid;
  tTSchemaDestroy(pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
344 345
  code = metaGetTbTSchemaEx(pCommitter->pTsdb->pVnode->pMeta, suid, uid, sver, &pCommitter->skmTable.pTSchema);
  if (code) goto _exit;
H
Hongze Cheng 已提交
346 347 348 349 350 351

_exit:
  return code;
}

static int32_t tsdbCommitterUpdateRowSchema(SCommitter *pCommitter, int64_t suid, int64_t uid, int32_t sver) {
H
Hongze Cheng 已提交
352 353
  int32_t code = 0;

H
Hongze Cheng 已提交
354 355
  if (pCommitter->skmRow.pTSchema) {
    if (pCommitter->skmRow.suid == suid) {
H
Hongze Cheng 已提交
356
      if (suid == 0) {
H
Hongze Cheng 已提交
357
        if (pCommitter->skmRow.uid == uid && sver == pCommitter->skmRow.pTSchema->version) goto _exit;
H
Hongze Cheng 已提交
358
      } else {
H
Hongze Cheng 已提交
359
        if (sver == pCommitter->skmRow.pTSchema->version) goto _exit;
H
Hongze Cheng 已提交
360 361 362 363
      }
    }
  }

H
Hongze Cheng 已提交
364 365 366
  pCommitter->skmRow.suid = suid;
  pCommitter->skmRow.uid = uid;
  tTSchemaDestroy(pCommitter->skmRow.pTSchema);
H
Hongze Cheng 已提交
367 368 369
  code = metaGetTbTSchemaEx(pCommitter->pTsdb->pVnode->pMeta, suid, uid, sver, &pCommitter->skmRow.pTSchema);
  if (code) {
    goto _exit;
H
Hongze Cheng 已提交
370 371 372 373 374 375
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
376 377 378 379
static int32_t tsdbCommitBlockData(SCommitter *pCommitter, SBlockData *pBlockData, SBlock *pBlock, SBlockIdx *pBlockIdx,
                                   int8_t toDataOnly) {
  int32_t code = 0;

H
Hongze Cheng 已提交
380 381 382 383 384 385
  if (pBlock->nSubBlock == 0) {
    if (!toDataOnly && pBlockData->nRow < pCommitter->minRow) {
      pBlock->last = 1;
    } else {
      pBlock->last = 0;
    }
H
Hongze Cheng 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399
  }

  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;

  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
400 401
static int32_t tsdbMergeTableData(SCommitter *pCommitter, STbDataIter *pIter, SBlock *pBlockMerge, TSDBKEY toKey,
                                  int8_t toDataOnly) {
H
Hongze Cheng 已提交
402
  int32_t     code = 0;
H
Hongze Cheng 已提交
403 404 405
  SBlockIdx  *pBlockIdx = &(SBlockIdx){.suid = pIter->pTbData->suid, .uid = pIter->pTbData->uid};
  SBlockData *pBlockDataMerge = &pCommitter->oBlockData;
  SBlockData *pBlockData = &pCommitter->nBlockData;
H
Hongze Cheng 已提交
406 407
  SBlock      block;
  SBlock     *pBlock = &block;
H
Hongze Cheng 已提交
408
  TSDBROW    *pRow1;
H
Hongze Cheng 已提交
409 410
  TSDBROW     row2;
  TSDBROW    *pRow2 = &row2;
H
Hongze Cheng 已提交
411 412

  // read SBlockData
H
Hongze Cheng 已提交
413
  code = tsdbReadBlockData(pCommitter->pReader, pBlockIdx, pBlockMerge, pBlockDataMerge, NULL, NULL);
H
Hongze Cheng 已提交
414 415
  if (code) goto _err;

H
Hongze Cheng 已提交
416 417 418
  code = tBlockDataSetSchema(pBlockData, pCommitter->skmTable.pTSchema);
  if (code) goto _err;

H
Hongze Cheng 已提交
419 420
  // loop to merge
  pRow1 = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
421
  *pRow2 = tsdbRowFromBlockData(pBlockDataMerge, 0);
H
Hongze Cheng 已提交
422
  ASSERT(pRow1 && tsdbKeyCmprFn(&TSDBROW_KEY(pRow1), &toKey) < 0);
H
Hongze Cheng 已提交
423
  ASSERT(tsdbKeyCmprFn(&TSDBROW_KEY(pRow2), &toKey) < 0);
H
Hongze Cheng 已提交
424
  code = tsdbCommitterUpdateRowSchema(pCommitter, pBlockIdx->suid, pBlockIdx->uid, TSDBROW_SVERSION(pRow1));
H
Hongze Cheng 已提交
425 426
  if (code) goto _err;

H
Hongze Cheng 已提交
427
  tBlockReset(pBlock);
H
Hongze Cheng 已提交
428
  tBlockDataClearData(pBlockData);
H
Hongze Cheng 已提交
429 430
  while (true) {
    if (pRow1 == NULL && pRow2 == NULL) {
H
Hongze Cheng 已提交
431
      if (pBlockData->nRow == 0) {
H
Hongze Cheng 已提交
432 433 434 435 436 437 438
        break;
      } else {
        goto _write_block;
      }
    }

    if (pRow1 && pRow2) {
H
Hongze Cheng 已提交
439 440 441 442 443
      int32_t c = tsdbRowCmprFn(pRow1, pRow2);
      if (c < 0) {
        goto _append_mem_row;
      } else if (c > 0) {
        goto _append_block_row;
H
Hongze Cheng 已提交
444 445 446 447
      } else {
        ASSERT(0);
      }
    } else if (pRow1) {
H
Hongze Cheng 已提交
448
      goto _append_mem_row;
H
Hongze Cheng 已提交
449
    } else {
H
Hongze Cheng 已提交
450 451 452 453
      goto _append_block_row;
    }

  _append_mem_row:
H
Hongze Cheng 已提交
454
    code = tBlockDataAppendRow(pBlockData, pRow1, pCommitter->skmRow.pTSchema);
H
Hongze Cheng 已提交
455
    if (code) goto _err;
H
Hongze Cheng 已提交
456

H
Hongze Cheng 已提交
457 458 459 460
    tsdbTbDataIterNext(pIter);
    pRow1 = tsdbTbDataIterGet(pIter);
    if (pRow1) {
      if (tsdbKeyCmprFn(&TSDBROW_KEY(pRow1), &toKey) < 0) {
H
Hongze Cheng 已提交
461
        code = tsdbCommitterUpdateRowSchema(pCommitter, pBlockIdx->suid, pBlockIdx->uid, TSDBROW_SVERSION(pRow1));
H
Hongze Cheng 已提交
462
        if (code) goto _err;
H
Hongze Cheng 已提交
463
      } else {
H
Hongze Cheng 已提交
464
        pRow1 = NULL;
H
Hongze Cheng 已提交
465
      }
H
Hongze Cheng 已提交
466 467
    }

H
Hongze Cheng 已提交
468 469 470 471 472 473 474
    if (pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
      goto _write_block;
    } else {
      continue;
    }

  _append_block_row:
H
Hongze Cheng 已提交
475
    code = tBlockDataAppendRow(pBlockData, pRow2, NULL);
H
Hongze Cheng 已提交
476 477
    if (code) goto _err;

H
Hongze Cheng 已提交
478 479
    if (pRow2->iRow + 1 < pBlockDataMerge->nRow) {
      *pRow2 = tsdbRowFromBlockData(pBlockDataMerge, pRow2->iRow + 1);
H
Hongze Cheng 已提交
480
    } else {
H
Hongze Cheng 已提交
481
      pRow2 = NULL;
H
Hongze Cheng 已提交
482 483
    }

H
Hongze Cheng 已提交
484 485 486 487 488
    if (pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
      goto _write_block;
    } else {
      continue;
    }
H
Hongze Cheng 已提交
489

H
Hongze Cheng 已提交
490
  _write_block:
H
Hongze Cheng 已提交
491
    code = tsdbCommitBlockData(pCommitter, pBlockData, pBlock, pBlockIdx, toDataOnly);
H
Hongze Cheng 已提交
492 493 494
    if (code) goto _err;

    tBlockReset(pBlock);
H
Hongze Cheng 已提交
495
    tBlockDataClearData(pBlockData);
H
Hongze Cheng 已提交
496 497 498 499 500 501 502 503 504
  }

  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 已提交
505
static int32_t tsdbCommitTableMemData(SCommitter *pCommitter, STbDataIter *pIter, TSDBKEY toKey, int8_t toDataOnly) {
H
Hongze Cheng 已提交
506 507
  int32_t     code = 0;
  TSDBROW    *pRow;
H
Hongze Cheng 已提交
508 509
  SBlock      block;
  SBlock     *pBlock = &block;
H
Hongze Cheng 已提交
510
  SBlockData *pBlockData = &pCommitter->nBlockData;
H
Hongze Cheng 已提交
511 512
  int64_t     suid = pIter->pTbData->suid;
  int64_t     uid = pIter->pTbData->uid;
H
Hongze Cheng 已提交
513

H
Hongze Cheng 已提交
514 515 516
  code = tBlockDataSetSchema(pBlockData, pCommitter->skmTable.pTSchema);
  if (code) goto _err;

H
Hongze Cheng 已提交
517
  tBlockReset(pBlock);
H
Hongze Cheng 已提交
518
  tBlockDataClearData(pBlockData);
H
Hongze Cheng 已提交
519
  pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
520
  ASSERT(pRow && tsdbKeyCmprFn(&TSDBROW_KEY(pRow), &toKey) < 0);
H
Hongze Cheng 已提交
521
  while (true) {
H
Hongze Cheng 已提交
522
    if (pRow == NULL) {
H
Hongze Cheng 已提交
523 524 525 526 527 528 529
      if (pBlockData->nRow > 0) {
        goto _write_block;
      } else {
        break;
      }
    }

H
Hongze Cheng 已提交
530
    // update schema
H
Hongze Cheng 已提交
531
    code = tsdbCommitterUpdateRowSchema(pCommitter, suid, uid, TSDBROW_SVERSION(pRow));
H
Hongze Cheng 已提交
532 533
    if (code) goto _err;

H
Hongze Cheng 已提交
534
    // append
H
Hongze Cheng 已提交
535
    code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema);
H
Hongze Cheng 已提交
536 537 538 539
    if (code) goto _err;

    tsdbTbDataIterNext(pIter);
    pRow = tsdbTbDataIterGet(pIter);
540 541 542
    // if (pRow && tsdbKeyCmprFn(&TSDBROW_KEY(pRow), &toKey) >= 0) pRow = NULL;
    // crash on CI, use the block following
    if (pRow) {
543 544
      TSDBKEY tmpKey = TSDBROW_KEY(pRow);
      if (tsdbKeyCmprFn(&tmpKey, &toKey) >= 0) {
545 546 547
        pRow = NULL;
      }
    }
H
Hongze Cheng 已提交
548 549

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

  _write_block:
H
Hongze Cheng 已提交
553
    code = tsdbCommitBlockData(pCommitter, pBlockData, pBlock, &(SBlockIdx){.suid = suid, .uid = uid}, toDataOnly);
H
Hongze Cheng 已提交
554 555 556
    if (code) goto _err;

    tBlockReset(pBlock);
H
Hongze Cheng 已提交
557
    tBlockDataClearData(pBlockData);
H
Hongze Cheng 已提交
558 559 560 561 562
  }

  return code;

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

H
Hongze Cheng 已提交
567
static int32_t tsdbCommitTableDiskData(SCommitter *pCommitter, SBlock *pBlock, SBlockIdx *pBlockIdx) {
H
Hongze Cheng 已提交
568
  int32_t code = 0;
H
Hongze Cheng 已提交
569
  SBlock  block;
H
Hongze Cheng 已提交
570 571

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

H
Hongze Cheng 已提交
575 576
    tBlockReset(&block);
    code = tsdbCommitBlockData(pCommitter, &pCommitter->oBlockData, &block, pBlockIdx, 0);
H
Hongze Cheng 已提交
577 578 579 580 581 582 583 584 585 586
    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 已提交
587 588 589
  return code;
}

H
Hongze Cheng 已提交
590 591
static int32_t tsdbCommitTableDataEnd(SCommitter *pCommitter, int64_t suid, int64_t uid) {
  int32_t    code = 0;
H
Hongze Cheng 已提交
592 593
  SBlockIdx  blockIdx = {.suid = suid, .uid = uid};
  SBlockIdx *pBlockIdx = &blockIdx;
H
Hongze Cheng 已提交
594 595 596 597

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

H
Hongze Cheng 已提交
598 599 600 601
  if (taosArrayPush(pCommitter->aBlockIdxN, pBlockIdx) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
602 603 604 605 606 607 608 609

  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 已提交
610 611 612 613 614 615 616 617 618
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) {
H
Hongze Cheng 已提交
619
    pRow = tsdbTbDataIterGet(&iter);
H
Hongze Cheng 已提交
620 621 622 623 624 625 626

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

    c = tBlockCmprFn(&(SBlock){.maxKey = key, .minKey = key}, pBlock);
    if (c == 0) {
      nRow++;
H
Hongze Cheng 已提交
627
      tsdbTbDataIterNext(&iter);
H
Hongze Cheng 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640 641
    } 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};
H
Hongze Cheng 已提交
642
  SBlock      block;
H
Hongze Cheng 已提交
643 644
  TSDBROW    *pRow;

H
Hongze Cheng 已提交
645 646 647
  code = tBlockDataSetSchema(pBlockData, pCommitter->skmTable.pTSchema);
  if (code) goto _err;

H
Hongze Cheng 已提交
648
  pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
649
  code = tsdbCommitterUpdateRowSchema(pCommitter, pBlockIdx->suid, pBlockIdx->uid, TSDBROW_SVERSION(pRow));
H
Hongze Cheng 已提交
650 651
  if (code) goto _err;
  while (true) {
H
Hongze Cheng 已提交
652
    if (pRow == NULL) break;
H
Hongze Cheng 已提交
653
    code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema);
H
Hongze Cheng 已提交
654 655 656 657 658
    if (code) goto _err;

    tsdbTbDataIterNext(pIter);
    pRow = tsdbTbDataIterGet(pIter);
    if (pRow) {
H
Hongze Cheng 已提交
659 660
      TSDBKEY key = TSDBROW_KEY(pRow);
      int32_t c = tBlockCmprFn(&(SBlock){.minKey = key, .maxKey = key}, pBlock);
H
Hongze Cheng 已提交
661 662

      if (c == 0) {
H
Hongze Cheng 已提交
663 664
        code =
            tsdbCommitterUpdateRowSchema(pCommitter, pIter->pTbData->suid, pIter->pTbData->uid, TSDBROW_SVERSION(pRow));
H
Hongze Cheng 已提交
665 666 667 668 669 670 671 672 673
        if (code) goto _err;
      } else if (c > 0) {
        pRow = NULL;
      } else {
        ASSERT(0);
      }
    }
  }

H
Hongze Cheng 已提交
674 675
  block = *pBlock;
  code = tsdbCommitBlockData(pCommitter, pBlockData, &block, pBlockIdx, 0);
H
Hongze Cheng 已提交
676 677 678 679 680 681 682 683 684
  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 已提交
685 686
static int32_t tsdbCommitTableData(SCommitter *pCommitter, STbData *pTbData, SBlockIdx *pBlockIdx) {
  int32_t      code = 0;
H
Hongze Cheng 已提交
687 688
  STbDataIter  iter = {0};
  STbDataIter *pIter = &iter;
H
Hongze Cheng 已提交
689
  TSDBROW     *pRow;
H
Hongze Cheng 已提交
690
  int32_t      iBlock;
H
Hongze Cheng 已提交
691
  int32_t      nBlock;
H
Hongze Cheng 已提交
692 693
  int64_t      suid;
  int64_t      uid;
H
Hongze Cheng 已提交
694 695 696 697

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

H
Hongze Cheng 已提交
700 701
    suid = pTbData->suid;
    uid = pTbData->uid;
H
Hongze Cheng 已提交
702 703 704 705 706 707 708 709 710 711 712
  } 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 已提交
713

H
Hongze Cheng 已提交
714 715
    suid = pBlockIdx->suid;
    uid = pBlockIdx->uid;
H
Hongze Cheng 已提交
716 717 718 719
  } else {
    nBlock = 0;
  }

H
Hongze Cheng 已提交
720
  if (pRow == NULL && nBlock == 0) goto _exit;
H
Hongze Cheng 已提交
721 722

  // start ===========
H
Hongze Cheng 已提交
723
  tMapDataReset(&pCommitter->nBlockMap);
H
Hongze Cheng 已提交
724 725
  SBlock  block;
  SBlock *pBlock = &block;
H
Hongze Cheng 已提交
726

H
Hongze Cheng 已提交
727
  iBlock = 0;
H
Hongze Cheng 已提交
728 729 730 731 732 733
  if (iBlock < nBlock) {
    tMapDataGetItemByIdx(&pCommitter->oBlockMap, iBlock, pBlock, tGetBlock);
  } else {
    pBlock = NULL;
  }

H
Hongze Cheng 已提交
734 735 736 737 738
  if (pRow) {
    code = tsdbCommitterUpdateTableSchema(pCommitter, pTbData->suid, pTbData->uid, pTbData->maxSkmVer);
    if (code) goto _err;
  }

H
Hongze Cheng 已提交
739 740
  // merge ===========
  while (true) {
H
Hongze Cheng 已提交
741
    if (pRow == NULL && pBlock == NULL) break;
H
Hongze Cheng 已提交
742

H
Hongze Cheng 已提交
743
    if (pRow && pBlock) {
H
Hongze Cheng 已提交
744
      if (pBlock->last) {
H
Hongze Cheng 已提交
745
        code = tsdbMergeTableData(pCommitter, pIter, pBlock,
H
Hongze Cheng 已提交
746
                                  (TSDBKEY){.ts = pCommitter->maxKey + 1, .version = VERSION_MIN}, 0);
H
Hongze Cheng 已提交
747 748 749
        if (code) goto _err;

        pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
750
        if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) pRow = NULL;
H
Hongze Cheng 已提交
751
        iBlock++;
H
Hongze Cheng 已提交
752 753 754 755 756
        if (iBlock < nBlock) {
          tMapDataGetItemByIdx(&pCommitter->oBlockMap, iBlock, pBlock, tGetBlock);
        } else {
          pBlock = NULL;
        }
H
Hongze Cheng 已提交
757 758

        ASSERT(pRow == NULL && pBlock == NULL);
H
Hongze Cheng 已提交
759
      } else {
H
Hongze Cheng 已提交
760
        int32_t c = tBlockCmprFn(&(SBlock){.maxKey = TSDBROW_KEY(pRow), .minKey = TSDBROW_KEY(pRow)}, pBlock);
H
Hongze Cheng 已提交
761
        if (c > 0) {
H
Hongze Cheng 已提交
762
          // only disk data
H
Hongze Cheng 已提交
763
          code = tsdbCommitTableDiskData(pCommitter, pBlock, pBlockIdx);
H
Hongze Cheng 已提交
764 765
          if (code) goto _err;

H
Hongze Cheng 已提交
766 767 768 769 770 771
          iBlock++;
          if (iBlock < nBlock) {
            tMapDataGetItemByIdx(&pCommitter->oBlockMap, iBlock, pBlock, tGetBlock);
          } else {
            pBlock = NULL;
          }
H
Hongze Cheng 已提交
772
        } else if (c < 0) {
H
Hongze Cheng 已提交
773
          // only memory data
H
Hongze Cheng 已提交
774
          code = tsdbCommitTableMemData(pCommitter, pIter, pBlock->minKey, 1);
H
Hongze Cheng 已提交
775 776 777
          if (code) goto _err;

          pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
778
          if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) pRow = NULL;
H
Hongze Cheng 已提交
779
        } else {
H
Hongze Cheng 已提交
780
          // merge memory and disk
H
Hongze Cheng 已提交
781 782
          int32_t nOvlp = tsdbGetOvlpNRow(pIter, pBlock);
          ASSERT(nOvlp);
H
Hongze Cheng 已提交
783
          if (pBlock->nRow + nOvlp <= pCommitter->maxRow && pBlock->nSubBlock < TSDB_MAX_SUBBLOCKS) {
H
Hongze Cheng 已提交
784 785
            code = tsdbMergeAsSubBlock(pCommitter, pIter, pBlock);
            if (code) goto _err;
H
Hongze Cheng 已提交
786
          } else {
H
Hongze Cheng 已提交
787 788 789 790 791 792 793 794 795 796
            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 已提交
797
            }
H
Hongze Cheng 已提交
798 799 800

            code = tsdbMergeTableData(pCommitter, pIter, pBlock, toKey, toDataOnly);
            if (code) goto _err;
H
Hongze Cheng 已提交
801
          }
H
Hongze Cheng 已提交
802 803 804 805 806 807 808 809 810

          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 已提交
811 812 813
        }
      }
    } else if (pBlock) {
H
Hongze Cheng 已提交
814
      code = tsdbCommitTableDiskData(pCommitter, pBlock, pBlockIdx);
H
Hongze Cheng 已提交
815 816
      if (code) goto _err;

H
Hongze Cheng 已提交
817 818 819 820 821 822
      iBlock++;
      if (iBlock < nBlock) {
        tMapDataGetItemByIdx(&pCommitter->oBlockMap, iBlock, pBlock, tGetBlock);
      } else {
        pBlock = NULL;
      }
H
Hongze Cheng 已提交
823
    } else {
H
Hongze Cheng 已提交
824 825
      code =
          tsdbCommitTableMemData(pCommitter, pIter, (TSDBKEY){.ts = pCommitter->maxKey + 1, .version = VERSION_MIN}, 0);
H
Hongze Cheng 已提交
826 827 828
      if (code) goto _err;

      pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
829 830
      if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) pRow = NULL;
      ASSERT(pRow == NULL);
H
Hongze Cheng 已提交
831 832 833
    }
  }

H
Hongze Cheng 已提交
834 835
  // end =====================
  code = tsdbCommitTableDataEnd(pCommitter, suid, uid);
H
Hongze Cheng 已提交
836 837 838 839 840 841 842 843 844 845 846 847 848 849
  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 已提交
850 851 852 853 854 855 856 857 858 859 860 861
static int32_t tsdbCommitFileDataEnd(SCommitter *pCommitter) {
  int32_t code = 0;

  // write blockIdx
  code = tsdbWriteBlockIdx(pCommitter->pWriter, pCommitter->aBlockIdxN, NULL);
  if (code) goto _err;

  // update file header
  code = tsdbUpdateDFileSetHeader(pCommitter->pWriter);
  if (code) goto _err;

  // upsert SDFileSet
H
Hongze Cheng 已提交
862
  code = tsdbFSUpsertFSet(&pCommitter->fs, &pCommitter->pWriter->wSet);
H
Hongze Cheng 已提交
863 864 865 866 867 868 869 870
  if (code) goto _err;

  // close and sync
  code = tsdbDataFWriterClose(&pCommitter->pWriter, 1);
  if (code) goto _err;

  if (pCommitter->pReader) {
    code = tsdbDataFReaderClose(&pCommitter->pReader);
H
Hongze Cheng 已提交
871
    if (code) goto _err;
H
Hongze Cheng 已提交
872 873 874 875 876 877 878 879 880 881 882
  }

_exit:
  return code;

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

static int32_t tsdbCommitFileData(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
883 884 885
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
886 887 888 889 890 891

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

  // commit file data impl
H
Hongze Cheng 已提交
892 893
  int32_t    iTbData = 0;
  int32_t    nTbData = taosArrayGetSize(pMemTable->aTbData);
H
Hongze Cheng 已提交
894
  int32_t    iBlockIdx = 0;
H
Hongze Cheng 已提交
895
  int32_t    nBlockIdx = taosArrayGetSize(pCommitter->aBlockIdx);
H
Hongze Cheng 已提交
896
  STbData   *pTbData;
H
Hongze Cheng 已提交
897
  SBlockIdx *pBlockIdx;
H
Hongze Cheng 已提交
898

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

H
Hongze Cheng 已提交
901
  pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
H
Hongze Cheng 已提交
902
  pBlockIdx = (iBlockIdx < nBlockIdx) ? (SBlockIdx *)taosArrayGet(pCommitter->aBlockIdx, iBlockIdx) : NULL;
H
Hongze Cheng 已提交
903 904 905
  while (pTbData || pBlockIdx) {
    if (pTbData && pBlockIdx) {
      int32_t c = tTABLEIDCmprFn(pTbData, pBlockIdx);
H
Hongze Cheng 已提交
906

H
Hongze Cheng 已提交
907
      if (c == 0) {
H
Hongze Cheng 已提交
908
        goto _commit_table_mem_and_disk;
H
Hongze Cheng 已提交
909
      } else if (c < 0) {
H
Hongze Cheng 已提交
910
        goto _commit_table_mem_data;
H
Hongze Cheng 已提交
911
      } else {
H
Hongze Cheng 已提交
912
        goto _commit_table_disk_data;
H
Hongze Cheng 已提交
913
      }
H
Hongze Cheng 已提交
914
    } else if (pBlockIdx) {
H
Hongze Cheng 已提交
915 916 917 918
      goto _commit_table_disk_data;
    } else {
      goto _commit_table_mem_data;
    }
H
Hongze Cheng 已提交
919

H
Hongze Cheng 已提交
920 921 922 923 924
  _commit_table_mem_data:
    code = tsdbCommitTableData(pCommitter, pTbData, NULL);
    if (code) goto _err;

    iTbData++;
H
Hongze Cheng 已提交
925
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData) : NULL;
H
Hongze Cheng 已提交
926
    continue;
H
Hongze Cheng 已提交
927

H
Hongze Cheng 已提交
928 929 930 931 932
  _commit_table_disk_data:
    code = tsdbCommitTableData(pCommitter, NULL, pBlockIdx);
    if (code) goto _err;

    iBlockIdx++;
H
Hongze Cheng 已提交
933
    pBlockIdx = (iBlockIdx < nBlockIdx) ? (SBlockIdx *)taosArrayGet(pCommitter->aBlockIdx, iBlockIdx) : NULL;
H
Hongze Cheng 已提交
934 935 936 937 938 939 940
    continue;

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

    iBlockIdx++;
H
Hongze Cheng 已提交
941
    pBlockIdx = (iBlockIdx < nBlockIdx) ? (SBlockIdx *)taosArrayGet(pCommitter->aBlockIdx, iBlockIdx) : NULL;
H
Hongze Cheng 已提交
942
    iTbData++;
H
Hongze Cheng 已提交
943
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData) : NULL;
H
Hongze Cheng 已提交
944
    continue;
H
Hongze Cheng 已提交
945 946
  }

H
Hongze Cheng 已提交
947 948
  // commit file data end
  code = tsdbCommitFileDataEnd(pCommitter);
H
Hongze Cheng 已提交
949
  if (code) goto _err;
H
Hongze Cheng 已提交
950 951 952 953

  return code;

_err:
H
Hongze Cheng 已提交
954
  tsdbError("vgId:%d commit file data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
955 956
  tsdbDataFReaderClose(&pCommitter->pReader);
  tsdbDataFWriterClose(&pCommitter->pWriter, 0);
H
Hongze Cheng 已提交
957 958 959
  return code;
}

H
Hongze Cheng 已提交
960 961
// ----------------------------------------------------------------------------
static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitter *pCommitter) {
H
Hongze Cheng 已提交
962
  int32_t code = 0;
H
Hongze Cheng 已提交
963

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

H
more  
Hongze Cheng 已提交
967
  taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
968 969
  pTsdb->imem = pTsdb->mem;
  pTsdb->mem = NULL;
H
more  
Hongze Cheng 已提交
970
  taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
971

H
Hongze Cheng 已提交
972
  pCommitter->pTsdb = pTsdb;
H
Hongze Cheng 已提交
973
  pCommitter->commitID = pTsdb->pVnode->state.commitID;
H
Hongze Cheng 已提交
974 975 976 977
  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 已提交
978
  pCommitter->cmprAlg = pTsdb->pVnode->config.tsdbCfg.compression;
H
Hongze Cheng 已提交
979

H
Hongze Cheng 已提交
980
  code = tsdbFSCopy(pTsdb, &pCommitter->fs);
H
Hongze Cheng 已提交
981 982 983 984 985 986
  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 已提交
987 988 989
  return code;
}

H
Hongze Cheng 已提交
990 991 992
static int32_t tsdbCommitDataStart(SCommitter *pCommitter) {
  int32_t code = 0;

H
Hongze Cheng 已提交
993
  pCommitter->aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
H
Hongze Cheng 已提交
994 995 996 997 998
  if (pCommitter->aBlockIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
999
  pCommitter->aBlockIdxN = taosArrayInit(0, sizeof(SBlockIdx));
H
Hongze Cheng 已提交
1000 1001 1002 1003 1004
  if (pCommitter->aBlockIdxN == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
1005 1006
  code = tBlockDataInit(&pCommitter->oBlockData);
  if (code) goto _exit;
H
Hongze Cheng 已提交
1007

H
Hongze Cheng 已提交
1008
  code = tBlockDataInit(&pCommitter->nBlockData);
H
Hongze Cheng 已提交
1009
  if (code) goto _exit;
H
Hongze Cheng 已提交
1010 1011 1012 1013 1014 1015

_exit:
  return code;
}

static void tsdbCommitDataEnd(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1016
  taosArrayDestroy(pCommitter->aBlockIdx);
H
Hongze Cheng 已提交
1017
  tMapDataClear(&pCommitter->oBlockMap);
H
Hongze Cheng 已提交
1018
  tBlockDataClear(&pCommitter->oBlockData, 1);
H
Hongze Cheng 已提交
1019
  taosArrayDestroy(pCommitter->aBlockIdxN);
H
Hongze Cheng 已提交
1020
  tMapDataClear(&pCommitter->nBlockMap);
H
Hongze Cheng 已提交
1021
  tBlockDataClear(&pCommitter->nBlockData, 1);
H
Hongze Cheng 已提交
1022 1023
  tTSchemaDestroy(pCommitter->skmTable.pTSchema);
  tTSchemaDestroy(pCommitter->skmRow.pTSchema);
H
Hongze Cheng 已提交
1024 1025
}

H
Hongze Cheng 已提交
1026 1027 1028 1029
static int32_t tsdbCommitData(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1030

H
Hongze Cheng 已提交
1031
  // check
H
Hongze Cheng 已提交
1032
  if (pMemTable->nRow == 0) goto _exit;
H
Hongze Cheng 已提交
1033

H
Hongze Cheng 已提交
1034 1035
  // start ====================
  code = tsdbCommitDataStart(pCommitter);
H
Hongze Cheng 已提交
1036
  if (code) goto _err;
H
Hongze Cheng 已提交
1037 1038 1039

  // impl ====================
  pCommitter->nextKey = pMemTable->minKey;
H
Hongze Cheng 已提交
1040 1041 1042 1043 1044
  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 已提交
1045
    if (code) goto _err;
H
Hongze Cheng 已提交
1046
  }
H
Hongze Cheng 已提交
1047

H
Hongze Cheng 已提交
1048 1049 1050
  // end ====================
  tsdbCommitDataEnd(pCommitter);

H
Hongze Cheng 已提交
1051 1052 1053
_exit:
  tsdbDebug("vgId:%d commit data done, nRow:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nRow);
  return code;
H
Hongze Cheng 已提交
1054

H
Hongze Cheng 已提交
1055
_err:
H
Hongze Cheng 已提交
1056
  tsdbCommitDataEnd(pCommitter);
H
Hongze Cheng 已提交
1057 1058 1059
  tsdbError("vgId:%d commit data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  return code;
}
H
Hongze Cheng 已提交
1060

H
Hongze Cheng 已提交
1061 1062 1063 1064
static int32_t tsdbCommitDel(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1065

H
Hongze Cheng 已提交
1066 1067
  if (pMemTable->nDel == 0) {
    goto _exit;
H
Hongze Cheng 已提交
1068
  }
H
Hongze Cheng 已提交
1069

H
Hongze Cheng 已提交
1070 1071 1072 1073 1074
  // start
  code = tsdbCommitDelStart(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1075

H
Hongze Cheng 已提交
1076
  // impl
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 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
  int32_t  iDelIdx = 0;
  int32_t  nDelIdx = taosArrayGetSize(pCommitter->aDelIdx);
  int32_t  iTbData = 0;
  int32_t  nTbData = taosArrayGetSize(pMemTable->aTbData);
  STbData *pTbData;
  SDelIdx *pDelIdx;

  ASSERT(nTbData > 0);

  pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
  pDelIdx = (iDelIdx < nDelIdx) ? (SDelIdx *)taosArrayGet(pCommitter->aDelIdx, iDelIdx) : NULL;
  while (true) {
    if (pTbData == NULL && pDelIdx == NULL) break;

    if (pTbData && pDelIdx) {
      int32_t c = tTABLEIDCmprFn(pTbData, pDelIdx);

      if (c == 0) {
        goto _commit_mem_and_disk_del;
      } else if (c < 0) {
        goto _commit_mem_del;
      } else {
        goto _commit_disk_del;
      }
    } else if (pTbData) {
      goto _commit_mem_del;
    } else {
      goto _commit_disk_del;
    }

  _commit_mem_del:
    code = tsdbCommitTableDel(pCommitter, pTbData, NULL);
    if (code) goto _err;

    iTbData++;
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData) : NULL;
    continue;

  _commit_disk_del:
    code = tsdbCommitTableDel(pCommitter, NULL, pDelIdx);
    if (code) goto _err;

    iDelIdx++;
    pDelIdx = (iDelIdx < nDelIdx) ? (SDelIdx *)taosArrayGet(pCommitter->aDelIdx, iDelIdx) : NULL;
    continue;

  _commit_mem_and_disk_del:
    code = tsdbCommitTableDel(pCommitter, pTbData, pDelIdx);
    if (code) goto _err;

    iTbData++;
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData) : NULL;
    iDelIdx++;
    pDelIdx = (iDelIdx < nDelIdx) ? (SDelIdx *)taosArrayGet(pCommitter->aDelIdx, iDelIdx) : NULL;
    continue;
H
Hongze Cheng 已提交
1132
  }
H
Hongze Cheng 已提交
1133

H
Hongze Cheng 已提交
1134 1135 1136 1137 1138
  // end
  code = tsdbCommitDelEnd(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1139

H
Hongze Cheng 已提交
1140
_exit:
H
Hongze Cheng 已提交
1141
  tsdbDebug("vgId:%d commit del done, nDel:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nDel);
H
Hongze Cheng 已提交
1142 1143 1144
  return code;

_err:
H
Hongze Cheng 已提交
1145
  tsdbError("vgId:%d commit del failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1146
  return code;
H
Hongze Cheng 已提交
1147 1148
}

H
Hongze Cheng 已提交
1149
static int32_t tsdbEndCommit(SCommitter *pCommitter, int32_t eno) {
H
Hongze Cheng 已提交
1150 1151 1152 1153
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

H
Hongze Cheng 已提交
1154 1155 1156 1157
  ASSERT(eno);

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

H
Hongze Cheng 已提交
1159
  // lock
H
Hongze Cheng 已提交
1160
  taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1161 1162 1163 1164 1165 1166 1167 1168

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

H
Hongze Cheng 已提交
1169
  pTsdb->imem = NULL;
H
Hongze Cheng 已提交
1170 1171

  // unlock
H
Hongze Cheng 已提交
1172 1173 1174
  taosThreadRwlockUnlock(&pTsdb->rwLock);

  tsdbUnrefMemTable(pMemTable);
H
Hongze Cheng 已提交
1175
  tsdbFSDestroy(&pCommitter->fs);
H
Hongze Cheng 已提交
1176 1177 1178 1179 1180 1181

  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 已提交
1182 1183
  return code;
}