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

H
Hongze Cheng 已提交
16
#include "tsdb.h"
H
Hongze Cheng 已提交
17 18 19 20 21
typedef struct {
  int64_t   suid;
  int64_t   uid;
  STSchema *pTSchema;
} SSkmInfo;
H
Hongze Cheng 已提交
22

H
Hongze Cheng 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
typedef struct {
  int64_t suid;
  int64_t uid;
  TSDBROW row;
} SRowInfo;

typedef struct {
  SRBTreeNode n;
  SRowInfo    r;
  int8_t      type;
  union {
    struct {
      SArray     *aTbDataP;
      int32_t     iTbDataP;
      STbDataIter iter;
    };  // memory data iter
    struct {
      int32_t    iLast;
      SArray    *aBlockL;
      int32_t    iBlockL;
      SBlockData bData;
      int32_t    iRow;
    };  // last file data iter
  };
} SDataIter;

H
Hongze Cheng 已提交
49
typedef struct {
H
Hongze Cheng 已提交
50
  STsdb *pTsdb;
H
Hongze Cheng 已提交
51
  int8_t toMerge;
H
Hongze Cheng 已提交
52
  /* commit data */
H
Hongze Cheng 已提交
53
  int64_t commitID;
H
Hongze Cheng 已提交
54 55
  int32_t minutes;
  int8_t  precision;
H
Hongze Cheng 已提交
56 57
  int32_t minRow;
  int32_t maxRow;
H
Hongze Cheng 已提交
58
  int8_t  cmprAlg;
H
Hongze Cheng 已提交
59
  int8_t  maxLast;
H
Hongze Cheng 已提交
60 61
  SArray *aTbDataP;  // memory
  STsdbFS fs;        // disk
H
Hongze Cheng 已提交
62
  // --------------
H
Hongze Cheng 已提交
63
  TSKEY   nextKey;  // reset by each table commit
H
Hongze Cheng 已提交
64 65 66
  int32_t commitFid;
  TSKEY   minKey;
  TSKEY   maxKey;
H
Hongze Cheng 已提交
67
  // commit file data
H
Hongze Cheng 已提交
68 69
  struct {
    SDataFReader *pReader;
H
Hongze Cheng 已提交
70 71 72 73 74
    SArray       *aBlockIdx;  // SArray<SBlockIdx>
    int32_t       iBlockIdx;
    SBlockIdx    *pBlockIdx;
    SMapData      mBlock;  // SMapData<SBlock>
    SBlockData    bData;
H
Hongze Cheng 已提交
75
  } dReader;
H
Hongze Cheng 已提交
76 77 78 79
  struct {
    SDataIter *pIter;
    SRBTree    rbt;
  };
H
Hongze Cheng 已提交
80 81 82
  struct {
    SDataFWriter *pWriter;
    SArray       *aBlockIdx;  // SArray<SBlockIdx>
H
Hongze Cheng 已提交
83
    SArray       *aBlockL;    // SArray<SBlockL>
H
Hongze Cheng 已提交
84 85
    SMapData      mBlock;     // SMapData<SBlock>
    SBlockData    bData;
H
Hongze Cheng 已提交
86
    SBlockData    bDatal;
H
Hongze Cheng 已提交
87 88 89
  } dWriter;
  SSkmInfo skmTable;
  SSkmInfo skmRow;
H
Hongze Cheng 已提交
90
  /* commit del */
H
Hongze Cheng 已提交
91 92
  SDelFReader *pDelFReader;
  SDelFWriter *pDelFWriter;
H
Hongze Cheng 已提交
93 94 95
  SArray      *aDelIdx;   // SArray<SDelIdx>
  SArray      *aDelIdxN;  // SArray<SDelIdx>
  SArray      *aDelData;  // SArray<SDelData>
H
Hongze Cheng 已提交
96
} SCommitter;
H
refact  
Hongze Cheng 已提交
97

H
Hongze Cheng 已提交
98 99 100
extern int32_t tsdbReadLastBlockEx(SDataFReader *pReader, int32_t iLast, SBlockL *pBlockL,
                                   SBlockData *pBlockData);  // todo

H
Hongze Cheng 已提交
101 102 103 104 105
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 已提交
106

H
refact  
Hongze Cheng 已提交
107
int32_t tsdbBegin(STsdb *pTsdb) {
H
Hongze Cheng 已提交
108
  int32_t code = 0;
H
Hongze Cheng 已提交
109

110 111
  if (!pTsdb) return code;

H
Hongze Cheng 已提交
112 113
  SMemTable *pMemTable;
  code = tsdbMemTableCreate(pTsdb, &pMemTable);
H
Hongze Cheng 已提交
114
  if (code) goto _err;
H
Hongze Cheng 已提交
115

H
Hongze Cheng 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
  // 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 已提交
132 133 134
  return code;

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

H
more  
Hongze Cheng 已提交
139
int32_t tsdbCommit(STsdb *pTsdb) {
140
  if (!pTsdb) return 0;
H
Hongze Cheng 已提交
141

H
more  
Hongze Cheng 已提交
142
  int32_t    code = 0;
H
Hongze Cheng 已提交
143 144 145 146
  SCommitter commith;
  SMemTable *pMemTable = pTsdb->mem;

  // check
H
Hongze Cheng 已提交
147
  if (pMemTable->nRow == 0 && pMemTable->nDel == 0) {
H
Hongze Cheng 已提交
148
    taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
149
    pTsdb->mem = NULL;
H
Hongze Cheng 已提交
150 151 152
    taosThreadRwlockUnlock(&pTsdb->rwLock);

    tsdbUnrefMemTable(pMemTable);
H
Hongze Cheng 已提交
153 154
    goto _exit;
  }
H
refact  
Hongze Cheng 已提交
155

H
more  
Hongze Cheng 已提交
156
  // start commit
H
more  
Hongze Cheng 已提交
157
  code = tsdbStartCommit(pTsdb, &commith);
H
Hongze Cheng 已提交
158
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
159

H
refact  
Hongze Cheng 已提交
160 161
  // commit impl
  code = tsdbCommitData(&commith);
H
Hongze Cheng 已提交
162
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
163 164

  code = tsdbCommitDel(&commith);
H
Hongze Cheng 已提交
165
  if (code) goto _err;
H
refact  
Hongze Cheng 已提交
166 167

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

H
Hongze Cheng 已提交
171
_exit:
H
refact  
Hongze Cheng 已提交
172 173 174
  return code;

_err:
H
Hongze Cheng 已提交
175
  tsdbEndCommit(&commith, code);
C
Cary Xu 已提交
176
  tsdbError("vgId:%d, failed to commit since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
refact  
Hongze Cheng 已提交
177 178 179
  return code;
}

H
Hongze Cheng 已提交
180
static int32_t tsdbCommitDelStart(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
181 182 183 184
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

H
Hongze Cheng 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
  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 已提交
202

H
Hongze Cheng 已提交
203
  SDelFile *pDelFileR = pCommitter->fs.pDelFile;
H
Hongze Cheng 已提交
204
  if (pDelFileR) {
H
Hongze Cheng 已提交
205
    code = tsdbDelFReaderOpen(&pCommitter->pDelFReader, pDelFileR, pTsdb);
H
Hongze Cheng 已提交
206
    if (code) goto _err;
H
Hongze Cheng 已提交
207

H
Hongze Cheng 已提交
208
    code = tsdbReadDelIdx(pCommitter->pDelFReader, pCommitter->aDelIdx);
H
Hongze Cheng 已提交
209
    if (code) goto _err;
H
Hongze Cheng 已提交
210 211
  }

H
Hongze Cheng 已提交
212
  // prepare new
H
Hongze Cheng 已提交
213 214
  SDelFile wDelFile = {.commitID = pCommitter->commitID, .size = 0, .offset = 0};
  code = tsdbDelFWriterOpen(&pCommitter->pDelFWriter, &wDelFile, pTsdb);
H
Hongze Cheng 已提交
215
  if (code) goto _err;
H
Hongze Cheng 已提交
216 217

_exit:
S
Shengliang Guan 已提交
218
  tsdbDebug("vgId:%d, commit del start", TD_VID(pTsdb->pVnode));
H
Hongze Cheng 已提交
219 220 221
  return code;

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

H
Hongze Cheng 已提交
226
static int32_t tsdbCommitTableDel(SCommitter *pCommitter, STbData *pTbData, SDelIdx *pDelIdx) {
H
Hongze Cheng 已提交
227
  int32_t   code = 0;
H
Hongze Cheng 已提交
228
  SDelData *pDelData;
H
Hongze Cheng 已提交
229 230
  tb_uid_t  suid;
  tb_uid_t  uid;
H
Hongze Cheng 已提交
231 232

  if (pTbData) {
H
Hongze Cheng 已提交
233 234
    suid = pTbData->suid;
    uid = pTbData->uid;
H
Hongze Cheng 已提交
235

H
Hongze Cheng 已提交
236 237 238 239
    if (pTbData->pHead == NULL) {
      pTbData = NULL;
    }
  }
H
Hongze Cheng 已提交
240 241

  if (pDelIdx) {
H
Hongze Cheng 已提交
242 243 244
    suid = pDelIdx->suid;
    uid = pDelIdx->uid;

H
Hongze Cheng 已提交
245
    code = tsdbReadDelData(pCommitter->pDelFReader, pDelIdx, pCommitter->aDelData);
H
Hongze Cheng 已提交
246
    if (code) goto _err;
247 248
  } else {
    taosArrayClear(pCommitter->aDelData);
H
Hongze Cheng 已提交
249 250
  }

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

H
Hongze Cheng 已提交
253
  SDelIdx delIdx = {.suid = suid, .uid = uid};
H
Hongze Cheng 已提交
254 255

  // memory
H
Hongze Cheng 已提交
256 257
  pDelData = pTbData ? pTbData->pHead : NULL;
  for (; pDelData; pDelData = pDelData->pNext) {
H
Hongze Cheng 已提交
258 259 260 261
    if (taosArrayPush(pCommitter->aDelData, pDelData) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
262 263 264
  }

  // write
H
Hongze Cheng 已提交
265
  code = tsdbWriteDelData(pCommitter->pDelFWriter, pCommitter->aDelData, &delIdx);
H
Hongze Cheng 已提交
266 267 268
  if (code) goto _err;

  // put delIdx
269
  if (taosArrayPush(pCommitter->aDelIdxN, &delIdx) == NULL) {
H
Hongze Cheng 已提交
270 271 272
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
273 274 275 276 277

_exit:
  return code;

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

H
Hongze Cheng 已提交
282 283
static int32_t tsdbCommitDelEnd(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
284
  STsdb  *pTsdb = pCommitter->pTsdb;
H
Hongze Cheng 已提交
285

H
Hongze Cheng 已提交
286
  code = tsdbWriteDelIdx(pCommitter->pDelFWriter, pCommitter->aDelIdxN);
H
Hongze Cheng 已提交
287
  if (code) goto _err;
H
Hongze Cheng 已提交
288

H
Hongze Cheng 已提交
289 290 291
  code = tsdbUpdateDelFileHdr(pCommitter->pDelFWriter);
  if (code) goto _err;

H
Hongze Cheng 已提交
292
  code = tsdbFSUpsertDelFile(&pCommitter->fs, &pCommitter->pDelFWriter->fDel);
H
Hongze Cheng 已提交
293
  if (code) goto _err;
H
Hongze Cheng 已提交
294

H
Hongze Cheng 已提交
295
  code = tsdbDelFWriterClose(&pCommitter->pDelFWriter, 1);
H
Hongze Cheng 已提交
296
  if (code) goto _err;
H
Hongze Cheng 已提交
297 298

  if (pCommitter->pDelFReader) {
H
Hongze Cheng 已提交
299
    code = tsdbDelFReaderClose(&pCommitter->pDelFReader);
H
Hongze Cheng 已提交
300 301 302
    if (code) goto _err;
  }

H
Hongze Cheng 已提交
303 304 305 306
  taosArrayDestroy(pCommitter->aDelIdx);
  taosArrayDestroy(pCommitter->aDelData);
  taosArrayDestroy(pCommitter->aDelIdxN);

H
Hongze Cheng 已提交
307 308 309
  return code;

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

H
Hongze Cheng 已提交
314
static int32_t tsdbCommitterUpdateTableSchema(SCommitter *pCommitter, int64_t suid, int64_t uid) {
H
Hongze Cheng 已提交
315 316
  int32_t code = 0;

H
Hongze Cheng 已提交
317 318 319 320
  if (suid) {
    if (pCommitter->skmTable.suid == suid) goto _exit;
  } else {
    if (pCommitter->skmTable.uid == uid) goto _exit;
H
Hongze Cheng 已提交
321 322 323 324 325
  }

  pCommitter->skmTable.suid = suid;
  pCommitter->skmTable.uid = uid;
  tTSchemaDestroy(pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
326
  code = metaGetTbTSchemaEx(pCommitter->pTsdb->pVnode->pMeta, suid, uid, -1, &pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
  if (code) goto _exit;

_exit:
  return code;
}

static int32_t tsdbCommitterUpdateRowSchema(SCommitter *pCommitter, int64_t suid, int64_t uid, int32_t sver) {
  int32_t code = 0;

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

  pCommitter->skmRow.suid = suid;
  pCommitter->skmRow.uid = uid;
  tTSchemaDestroy(pCommitter->skmRow.pTSchema);
  code = metaGetTbTSchemaEx(pCommitter->pTsdb->pVnode->pMeta, suid, uid, sver, &pCommitter->skmRow.pTSchema);
  if (code) {
    goto _exit;
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
358 359 360 361 362 363 364 365 366 367
static int32_t tsdbCommitterNextTableData(SCommitter *pCommitter) {
  int32_t code = 0;

  ASSERT(pCommitter->dReader.pBlockIdx);

  pCommitter->dReader.iBlockIdx++;
  if (pCommitter->dReader.iBlockIdx < taosArrayGetSize(pCommitter->dReader.aBlockIdx)) {
    pCommitter->dReader.pBlockIdx =
        (SBlockIdx *)taosArrayGet(pCommitter->dReader.aBlockIdx, pCommitter->dReader.iBlockIdx);

H
Hongze Cheng 已提交
368
    code = tsdbReadBlock(pCommitter->dReader.pReader, pCommitter->dReader.pBlockIdx, &pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
369 370 371 372 373 374 375 376 377 378 379
    if (code) goto _exit;

    ASSERT(pCommitter->dReader.mBlock.nItem > 0);
  } else {
    pCommitter->dReader.pBlockIdx = NULL;
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
380 381 382 383
static int32_t tsdbCommitFileDataStart(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SDFileSet *pRSet = NULL;
H
Hongze Cheng 已提交
384

H
Hongze Cheng 已提交
385
  // memory
H
Hongze Cheng 已提交
386 387 388
  pCommitter->commitFid = tsdbKeyFid(pCommitter->nextKey, pCommitter->minutes, pCommitter->precision);
  tsdbFidKeyRange(pCommitter->commitFid, pCommitter->minutes, pCommitter->precision, &pCommitter->minKey,
                  &pCommitter->maxKey);
H
Hongze Cheng 已提交
389
  pCommitter->nextKey = TSKEY_MAX;
H
Hongze Cheng 已提交
390

H
Hongze Cheng 已提交
391
  // Reader
H
Hongze Cheng 已提交
392 393
  pRSet = (SDFileSet *)taosArraySearch(pCommitter->fs.aDFileSet, &(SDFileSet){.fid = pCommitter->commitFid},
                                       tDFileSetCmprFn, TD_EQ);
H
Hongze Cheng 已提交
394
  if (pRSet) {
H
Hongze Cheng 已提交
395
    code = tsdbDataFReaderOpen(&pCommitter->dReader.pReader, pTsdb, pRSet);
H
Hongze Cheng 已提交
396 397
    if (code) goto _err;

H
Hongze Cheng 已提交
398
    // data
H
Hongze Cheng 已提交
399
    code = tsdbReadBlockIdx(pCommitter->dReader.pReader, pCommitter->dReader.aBlockIdx);
H
Hongze Cheng 已提交
400
    if (code) goto _err;
H
Hongze Cheng 已提交
401

H
Hongze Cheng 已提交
402 403 404 405 406
    pCommitter->dReader.iBlockIdx = 0;
    if (pCommitter->dReader.iBlockIdx < taosArrayGetSize(pCommitter->dReader.aBlockIdx)) {
      pCommitter->dReader.pBlockIdx =
          (SBlockIdx *)taosArrayGet(pCommitter->dReader.aBlockIdx, pCommitter->dReader.iBlockIdx);

H
Hongze Cheng 已提交
407
      code = tsdbReadBlock(pCommitter->dReader.pReader, pCommitter->dReader.pBlockIdx, &pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
408 409 410 411
      if (code) goto _err;
    } else {
      pCommitter->dReader.pBlockIdx = NULL;
    }
H
Hongze Cheng 已提交
412
    tBlockDataReset(&pCommitter->dReader.bData);
H
Hongze Cheng 已提交
413
  } else {
H
Hongze Cheng 已提交
414
    pCommitter->dReader.pBlockIdx = NULL;
H
Hongze Cheng 已提交
415
  }
H
Hongze Cheng 已提交
416

H
Hongze Cheng 已提交
417
  // Writer
H
Hongze Cheng 已提交
418 419 420
  SHeadFile fHead;
  SDataFile fData;
  SSmaFile  fSma;
H
Hongze Cheng 已提交
421 422
  SLastFile fLast;
  SDFileSet wSet = {0};
H
Hongze Cheng 已提交
423
  if (pRSet) {
H
Hongze Cheng 已提交
424
    ASSERT(pCommitter->maxLast == 1 || pRSet->nLastF < pCommitter->maxLast);
H
Hongze Cheng 已提交
425

H
Hongze Cheng 已提交
426
    fHead = (SHeadFile){.commitID = pCommitter->commitID};
H
Hongze Cheng 已提交
427 428
    fData = *pRSet->pDataF;
    fSma = *pRSet->pSmaF;
H
Hongze Cheng 已提交
429 430 431 432 433 434 435 436 437 438 439 440
    fLast = (SLastFile){.commitID = pCommitter->commitID};

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

H
Hongze Cheng 已提交
447
    SDiskID did = {0};
448
    tfsAllocDisk(pTsdb->pVnode->pTfs, 0, &did);
H
Hongze Cheng 已提交
449 450
    tfsMkdirRecurAt(pTsdb->pVnode->pTfs, pTsdb->path, did);

451
    wSet.diskId = did;
H
Hongze Cheng 已提交
452
    wSet.fid = pCommitter->commitFid;
H
Hongze Cheng 已提交
453 454 455
    wSet.pHeadF = &fHead;
    wSet.pDataF = &fData;
    wSet.pSmaF = &fSma;
H
Hongze Cheng 已提交
456
    wSet.nLastF = 1;
H
Hongze Cheng 已提交
457
    wSet.aLastF[0] = &fLast;
H
Hongze Cheng 已提交
458
  }
H
Hongze Cheng 已提交
459 460 461
  if (wSet.nLastF == pCommitter->maxLast) {
    pCommitter->toMerge = 1;
  }
H
Hongze Cheng 已提交
462
  code = tsdbDataFWriterOpen(&pCommitter->dWriter.pWriter, pTsdb, &wSet);
H
Hongze Cheng 已提交
463
  if (code) goto _err;
H
Hongze Cheng 已提交
464

H
Hongze Cheng 已提交
465 466 467 468 469 470
  taosArrayClear(pCommitter->dWriter.aBlockIdx);
  taosArrayClear(pCommitter->dWriter.aBlockL);
  tMapDataReset(&pCommitter->dWriter.mBlock);
  tBlockDataReset(&pCommitter->dWriter.bData);
  tBlockDataReset(&pCommitter->dWriter.bDatal);

H
Hongze Cheng 已提交
471
_exit:
H
Hongze Cheng 已提交
472 473 474
  return code;

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

H
Hongze Cheng 已提交
479
static int32_t tsdbCommitDataBlock(SCommitter *pCommitter, SBlock *pBlock) {
H
Hongze Cheng 已提交
480 481 482
  int32_t     code = 0;
  SBlockData *pBlockData = &pCommitter->dWriter.bData;
  SBlock      block;
H
Hongze Cheng 已提交
483

H
Hongze Cheng 已提交
484
  ASSERT(pBlockData->nRow > 0);
H
Hongze Cheng 已提交
485

H
Hongze Cheng 已提交
486 487 488 489 490 491
  if (pBlock) {
    block = *pBlock;  // as a subblock
  } else {
    tBlockReset(&block);  // as a new block
  }

H
Hongze Cheng 已提交
492
  // info
H
Hongze Cheng 已提交
493 494 495
  block.nRow += pBlockData->nRow;
  for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
    TSDBKEY key = {.ts = pBlockData->aTSKEY[iRow], .version = pBlockData->aVersion[iRow]};
H
Hongze Cheng 已提交
496

H
Hongze Cheng 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
    if (iRow == 0) {
      if (tsdbKeyCmprFn(&block.minKey, &key) > 0) {
        block.minKey = key;
      }
    } else {
      if (pBlockData->aTSKEY[iRow] == pBlockData->aTSKEY[iRow - 1]) {
        block.hasDup = 1;
      }
    }

    if (iRow == pBlockData->nRow - 1 && tsdbKeyCmprFn(&block.maxKey, &key) < 0) {
      block.maxKey = key;
    }

    block.minVer = TMIN(block.minVer, key.version);
    block.maxVer = TMAX(block.maxVer, key.version);
  }

  // write
H
Hongze Cheng 已提交
516 517
  block.nSubBlock++;
  code = tsdbWriteBlockData(pCommitter->dWriter.pWriter, pBlockData, &block.aSubBlock[block.nSubBlock - 1],
H
Hongze Cheng 已提交
518
                            ((block.nSubBlock == 1) && !block.hasDup) ? &block.smaInfo : NULL, pCommitter->cmprAlg, 0);
H
Hongze Cheng 已提交
519 520 521
  if (code) goto _err;

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

H
Hongze Cheng 已提交
525
  // clear
H
Hongze Cheng 已提交
526
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
527

H
Hongze Cheng 已提交
528 529 530 531
  return code;

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

static int32_t tsdbCommitLastBlock(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
536 537 538 539 540 541
  int32_t     code = 0;
  SBlockL     blockL;
  SBlockData *pBlockData = &pCommitter->dWriter.bDatal;

  ASSERT(pBlockData->nRow > 0);

H
Hongze Cheng 已提交
542
  // info
H
Hongze Cheng 已提交
543 544
  blockL.suid = pBlockData->suid;
  blockL.nRow = pBlockData->nRow;
H
Hongze Cheng 已提交
545 546
  blockL.minKey = TSKEY_MAX;
  blockL.maxKey = TSKEY_MIN;
H
Hongze Cheng 已提交
547 548 549
  blockL.minVer = VERSION_MAX;
  blockL.maxVer = VERSION_MIN;
  for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
H
Hongze Cheng 已提交
550 551
    blockL.minKey = TMIN(blockL.minKey, pBlockData->aTSKEY[iRow]);
    blockL.maxKey = TMAX(blockL.maxKey, pBlockData->aTSKEY[iRow]);
H
Hongze Cheng 已提交
552
    blockL.minVer = TMIN(blockL.minVer, pBlockData->aVersion[iRow]);
H
Hongze Cheng 已提交
553
    blockL.maxVer = TMAX(blockL.maxVer, pBlockData->aVersion[iRow]);
H
Hongze Cheng 已提交
554 555 556
  }
  blockL.minUid = pBlockData->uid ? pBlockData->uid : pBlockData->aUid[0];
  blockL.maxUid = pBlockData->uid ? pBlockData->uid : pBlockData->aUid[pBlockData->nRow - 1];
H
Hongze Cheng 已提交
557

H
Hongze Cheng 已提交
558
  // write
H
Hongze Cheng 已提交
559
  code = tsdbWriteBlockData(pCommitter->dWriter.pWriter, pBlockData, &blockL.bInfo, NULL, pCommitter->cmprAlg, 1);
H
Hongze Cheng 已提交
560
  if (code) goto _err;
H
Hongze Cheng 已提交
561

H
Hongze Cheng 已提交
562
  // push SBlockL
H
Hongze Cheng 已提交
563 564
  if (taosArrayPush(pCommitter->dWriter.aBlockL, &blockL) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
565
    goto _err;
H
Hongze Cheng 已提交
566 567
  }

H
Hongze Cheng 已提交
568
  // clear
H
Hongze Cheng 已提交
569
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
570

H
Hongze Cheng 已提交
571 572 573 574
  return code;

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

H
Hongze Cheng 已提交
578
static int32_t tsdbMergeCommitDataBlock(SCommitter *pCommitter, STbDataIter *pIter, SBlock *pBlock) {
H
Hongze Cheng 已提交
579
  int32_t     code = 0;
H
Hongze Cheng 已提交
580 581 582
  STbData    *pTbData = pIter->pTbData;
  SBlockData *pBlockDataR = &pCommitter->dReader.bData;
  SBlockData *pBlockDataW = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
583

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

H
Hongze Cheng 已提交
587
  tBlockDataClear(pBlockDataW);
H
Hongze Cheng 已提交
588 589 590 591 592 593 594
  int32_t  iRow = 0;
  TSDBROW  row;
  TSDBROW *pRow1 = tsdbTbDataIterGet(pIter);
  TSDBROW *pRow2 = &row;
  *pRow2 = tsdbRowFromBlockData(pBlockDataR, iRow);
  while (pRow1 && pRow2) {
    int32_t c = tsdbRowCmprFn(pRow1, pRow2);
H
Hongze Cheng 已提交
595

H
Hongze Cheng 已提交
596 597 598
    if (c < 0) {
      code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow1));
      if (code) goto _err;
H
Hongze Cheng 已提交
599

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

H
Hongze Cheng 已提交
603 604 605 606
      // next
      tsdbTbDataIterNext(pIter);
      pRow1 = tsdbTbDataIterGet(pIter);
    } else if (c > 0) {
H
Hongze Cheng 已提交
607
      code = tBlockDataAppendRow(pBlockDataW, pRow2, NULL, pTbData->uid);
H
Hongze Cheng 已提交
608
      if (code) goto _err;
H
Hongze Cheng 已提交
609

H
Hongze Cheng 已提交
610 611 612
      iRow++;
      if (iRow < pBlockDataR->nRow) {
        *pRow2 = tsdbRowFromBlockData(pBlockDataR, iRow);
H
Hongze Cheng 已提交
613
      } else {
H
Hongze Cheng 已提交
614
        pRow2 = NULL;
H
Hongze Cheng 已提交
615
      }
H
Hongze Cheng 已提交
616 617
    } else {
      ASSERT(0);
H
Hongze Cheng 已提交
618 619
    }

H
Hongze Cheng 已提交
620 621
    // check
    if (pBlockDataW->nRow >= pCommitter->maxRow * 4 / 5) {
H
Hongze Cheng 已提交
622
      code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
623
      if (code) goto _err;
H
Hongze Cheng 已提交
624
    }
H
Hongze Cheng 已提交
625
  }
H
Hongze Cheng 已提交
626

H
Hongze Cheng 已提交
627
  while (pRow2) {
H
Hongze Cheng 已提交
628
    code = tBlockDataAppendRow(pBlockDataW, pRow2, NULL, pTbData->uid);
H
Hongze Cheng 已提交
629 630
    if (code) goto _err;

H
Hongze Cheng 已提交
631 632 633
    iRow++;
    if (iRow < pBlockDataR->nRow) {
      *pRow2 = tsdbRowFromBlockData(pBlockDataR, iRow);
H
Hongze Cheng 已提交
634
    } else {
H
Hongze Cheng 已提交
635
      pRow2 = NULL;
H
Hongze Cheng 已提交
636 637
    }

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

H
Hongze Cheng 已提交
645 646
  // check
  if (pBlockDataW->nRow > 0) {
H
Hongze Cheng 已提交
647
    code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
648
    if (code) goto _err;
H
Hongze Cheng 已提交
649 650 651 652 653
  }

  return code;

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

H
Hongze Cheng 已提交
658 659 660
static int32_t tsdbCommitTableMemData(SCommitter *pCommitter, STbDataIter *pIter, TSDBKEY toKey) {
  int32_t     code = 0;
  STbData    *pTbData = pIter->pTbData;
H
Hongze Cheng 已提交
661
  SBlockData *pBlockData = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
662

H
Hongze Cheng 已提交
663
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
664
  TSDBROW *pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
665
  while (true) {
H
Hongze Cheng 已提交
666
    if (pRow == NULL) {
H
Hongze Cheng 已提交
667 668 669 670 671 672 673
      if (pBlockData->nRow > 0) {
        goto _write_block;
      } else {
        break;
      }
    }

H
Hongze Cheng 已提交
674
    // update schema
H
Hongze Cheng 已提交
675
    code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow));
H
Hongze Cheng 已提交
676 677
    if (code) goto _err;

H
Hongze Cheng 已提交
678
    // append
H
Hongze Cheng 已提交
679
    code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema, pTbData->uid);
H
Hongze Cheng 已提交
680 681 682 683
    if (code) goto _err;

    tsdbTbDataIterNext(pIter);
    pRow = tsdbTbDataIterGet(pIter);
684
    if (pRow) {
H
Hongze Cheng 已提交
685 686
      TSDBKEY rowKey = TSDBROW_KEY(pRow);
      if (tsdbKeyCmprFn(&rowKey, &toKey) >= 0) {
687 688 689
        pRow = NULL;
      }
    }
H
Hongze Cheng 已提交
690

H
Hongze Cheng 已提交
691 692
    if (pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
    _write_block:
H
Hongze Cheng 已提交
693
      code = tsdbCommitDataBlock(pCommitter, NULL);
H
Hongze Cheng 已提交
694 695
      if (code) goto _err;
    }
H
Hongze Cheng 已提交
696 697 698 699 700
  }

  return code;

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

H
Hongze Cheng 已提交
705 706
static int32_t tsdbGetNumOfRowsLessThan(STbDataIter *pIter, TSDBKEY key) {
  int32_t nRow = 0;
H
Hongze Cheng 已提交
707

H
Hongze Cheng 已提交
708
  STbDataIter iter = *pIter;
H
Hongze Cheng 已提交
709
  while (true) {
H
Hongze Cheng 已提交
710
    TSDBROW *pRow = tsdbTbDataIterGet(&iter);
H
Hongze Cheng 已提交
711 712
    if (pRow == NULL) break;

H
Hongze Cheng 已提交
713 714
    int32_t c = tsdbKeyCmprFn(&TSDBROW_KEY(pRow), &key);
    if (c < 0) {
H
Hongze Cheng 已提交
715
      nRow++;
H
Hongze Cheng 已提交
716
      tsdbTbDataIterNext(&iter);
H
Hongze Cheng 已提交
717 718 719 720 721 722 723 724 725 726 727 728
    } else if (c > 0) {
      break;
    } else {
      ASSERT(0);
    }
  }

  return nRow;
}

static int32_t tsdbMergeAsSubBlock(SCommitter *pCommitter, STbDataIter *pIter, SBlock *pBlock) {
  int32_t     code = 0;
H
Hongze Cheng 已提交
729
  STbData    *pTbData = pIter->pTbData;
H
Hongze Cheng 已提交
730
  SBlockData *pBlockData = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
731

H
Hongze Cheng 已提交
732
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
733
  TSDBROW *pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
734
  while (true) {
H
Hongze Cheng 已提交
735 736 737 738 739 740 741 742 743 744
    if (pRow == NULL) break;

    code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow));
    if (code) goto _err;

    code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema, pTbData->uid);
    if (code) goto _err;

    tsdbTbDataIterNext(pIter);
    pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
745
    if (pRow) {
H
Hongze Cheng 已提交
746 747
      TSDBKEY rowKey = TSDBROW_KEY(pRow);
      if (tsdbKeyCmprFn(&rowKey, &pBlock->maxKey) > 0) {
H
Hongze Cheng 已提交
748 749 750 751 752
        pRow = NULL;
      }
    }
  }

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

H
Hongze Cheng 已提交
755
  code = tsdbCommitDataBlock(pCommitter, pBlock);
H
Hongze Cheng 已提交
756 757 758 759 760
  if (code) goto _err;

  return code;

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

H
Hongze Cheng 已提交
765 766 767 768 769
static int32_t tsdbCommitLastFile(SCommitter *pCommitter, STbDataIter *pIter) {
  int32_t     code = 0;
  STbData    *pTbData = pIter->pTbData;
  SBlockData *pBlockData = &pCommitter->dWriter.bDatal;
  TSDBROW    *pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
770 771 772 773 774

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

H
Hongze Cheng 已提交
775
  if (pRow == NULL) goto _exit;
H
Hongze Cheng 已提交
776

H
Hongze Cheng 已提交
777 778 779
  if (pBlockData->suid || pBlockData->uid) {
    if (pBlockData->suid != pTbData->suid || pBlockData->suid == 0) {
      if (pBlockData->nRow > 0) {
H
Hongze Cheng 已提交
780 781 782 783
        code = tsdbCommitLastBlock(pCommitter);
        if (code) goto _err;
      }

H
Hongze Cheng 已提交
784
      tBlockDataReset(pBlockData);
H
Hongze Cheng 已提交
785
    }
H
Hongze Cheng 已提交
786
  }
H
Hongze Cheng 已提交
787

H
Hongze Cheng 已提交
788
  if (!pBlockData->suid && !pBlockData->uid) {
H
Hongze Cheng 已提交
789
    code = tBlockDataInit(pBlockData, pTbData->suid, 0, pCommitter->skmTable.pTSchema);
H
Hongze Cheng 已提交
790 791
    if (code) goto _err;
  }
H
Hongze Cheng 已提交
792

H
Hongze Cheng 已提交
793 794 795
  while (pRow) {
    code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow));
    if (code) goto _err;
H
Hongze Cheng 已提交
796

H
Hongze Cheng 已提交
797 798
    code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema, pTbData->uid);
    if (code) goto _err;
H
Hongze Cheng 已提交
799

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

H
Hongze Cheng 已提交
806 807
    if (pBlockData->nRow >= pCommitter->maxRow) {
      code = tsdbCommitLastBlock(pCommitter);
H
Hongze Cheng 已提交
808
      if (code) goto _err;
H
Hongze Cheng 已提交
809 810 811
    }
  }

H
Hongze Cheng 已提交
812
_exit:
H
Hongze Cheng 已提交
813 814 815
  return code;

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

H
Hongze Cheng 已提交
820
static int32_t tsdbMergeCommitData(SCommitter *pCommitter, STbDataIter *pIter) {
H
Hongze Cheng 已提交
821 822 823 824 825 826
  int32_t  code = 0;
  STbData *pTbData = pIter->pTbData;
  int32_t  iBlock = 0;
  SBlock   block;
  SBlock  *pBlock = &block;
  TSDBROW *pRow = tsdbTbDataIterGet(pIter);
H
Hongze Cheng 已提交
827

H
Hongze Cheng 已提交
828
  if (pCommitter->dReader.pBlockIdx && tTABLEIDCmprFn(pTbData, pCommitter->dReader.pBlockIdx) == 0) {
H
Hongze Cheng 已提交
829 830 831 832 833 834
    tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
  } else {
    pBlock = NULL;
  }

  while (pBlock && pRow) {
H
Hongze Cheng 已提交
835 836 837 838
    SBlock  tBlock = {.minKey = TSDBROW_KEY(pRow), .maxKey = TSDBROW_KEY(pRow)};
    int32_t c = tBlockCmprFn(pBlock, &tBlock);

    if (c < 0) {
H
Hongze Cheng 已提交
839 840 841 842 843 844 845 846 847
      code = tMapDataPutItem(&pCommitter->dWriter.mBlock, pBlock, tPutBlock);
      if (code) goto _err;

      iBlock++;
      if (iBlock < pCommitter->dReader.mBlock.nItem) {
        tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
      } else {
        pBlock = NULL;
      }
H
Hongze Cheng 已提交
848
    } else if (c > 0) {
H
Hongze Cheng 已提交
849
      code = tsdbCommitTableMemData(pCommitter, pIter, pBlock->minKey);
H
Hongze Cheng 已提交
850 851 852 853 854 855
      if (code) goto _err;

      pRow = tsdbTbDataIterGet(pIter);
      if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
        pRow = NULL;
      }
H
Hongze Cheng 已提交
856
    } else {
H
Hongze Cheng 已提交
857
      int32_t nOvlp = tsdbGetNumOfRowsLessThan(pIter, pBlock->maxKey);
H
Hongze Cheng 已提交
858 859 860 861 862 863 864

      ASSERT(nOvlp > 0);

      if (pBlock->nRow + nOvlp <= pCommitter->maxRow && pBlock->nSubBlock < TSDB_MAX_SUBBLOCKS) {
        code = tsdbMergeAsSubBlock(pCommitter, pIter, pBlock);
        if (code) goto _err;
      } else {
H
Hongze Cheng 已提交
865
        code = tsdbMergeCommitDataBlock(pCommitter, pIter, pBlock);
H
Hongze Cheng 已提交
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
        if (code) goto _err;
      }

      // next
      pRow = tsdbTbDataIterGet(pIter);
      if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
        pRow = NULL;
      }
      iBlock++;
      if (iBlock < pCommitter->dReader.mBlock.nItem) {
        tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
      } else {
        pBlock = NULL;
      }
    }
  }

  while (pBlock) {
    code = tMapDataPutItem(&pCommitter->dWriter.mBlock, pBlock, tPutBlock);
    if (code) goto _err;

    iBlock++;
    if (iBlock < pCommitter->dReader.mBlock.nItem) {
      tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
    } else {
      pBlock = NULL;
    }
  }

H
Hongze Cheng 已提交
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
_exit:
  return code;

_err:
  return code;
}

static int32_t tsdbCommitTableData(SCommitter *pCommitter, STbData *pTbData) {
  int32_t code = 0;

  ASSERT(pCommitter->dReader.pBlockIdx == NULL || tTABLEIDCmprFn(pCommitter->dReader.pBlockIdx, pTbData) >= 0);

  // merge commit table data
  STbDataIter iter = {0};
  TSDBROW    *pRow;

H
Hongze Cheng 已提交
911 912
  tMapDataReset(&pCommitter->dWriter.mBlock);

H
Hongze Cheng 已提交
913 914 915 916 917 918 919 920 921 922 923 924 925
  tsdbTbDataIterOpen(pTbData, &(TSDBKEY){.ts = pCommitter->minKey, .version = VERSION_MIN}, 0, &iter);
  pRow = tsdbTbDataIterGet(&iter);
  if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
    pRow = NULL;
  }
  if (pRow == NULL) {
    if (pCommitter->dReader.pBlockIdx && tTABLEIDCmprFn(pCommitter->dReader.pBlockIdx, pTbData) == 0) {
      code = tMapDataCopy(&pCommitter->dReader.mBlock, &pCommitter->dWriter.mBlock);
      if (code) goto _err;
    }
    goto _exit;
  }

H
Hongze Cheng 已提交
926 927 928 929 930 931 932
  code = tsdbCommitterUpdateTableSchema(pCommitter, pTbData->suid, pTbData->uid);
  if (code) goto _err;
  code = tBlockDataInit(&pCommitter->dReader.bData, pTbData->suid, pTbData->uid, pCommitter->skmTable.pTSchema);
  if (code) goto _err;
  code = tBlockDataInit(&pCommitter->dWriter.bData, pTbData->suid, pTbData->uid, pCommitter->skmTable.pTSchema);
  if (code) goto _err;

H
Hongze Cheng 已提交
933 934 935 936 937
  // commit data
  code = tsdbMergeCommitData(pCommitter, &iter);
  if (code) goto _err;

  // commit last
H
Hongze Cheng 已提交
938
  code = tsdbCommitLastFile(pCommitter, &iter);
H
Hongze Cheng 已提交
939
  if (code) goto _err;
H
Hongze Cheng 已提交
940

H
Hongze Cheng 已提交
941
_exit:
H
Hongze Cheng 已提交
942 943
  if (pCommitter->dWriter.mBlock.nItem > 0) {
    SBlockIdx blockIdx = {.suid = pTbData->suid, .uid = pTbData->uid};
H
Hongze Cheng 已提交
944
    code = tsdbWriteBlock(pCommitter->dWriter.pWriter, &pCommitter->dWriter.mBlock, &blockIdx);
H
Hongze Cheng 已提交
945 946 947 948 949 950 951
    if (code) goto _err;

    if (taosArrayPush(pCommitter->dWriter.aBlockIdx, &blockIdx) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
  }
H
Hongze Cheng 已提交
952
  pRow = tsdbTbDataIterGet(&iter);
H
Hongze Cheng 已提交
953 954 955 956 957 958 959 960 961 962 963
  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 已提交
964 965 966
static int32_t tsdbCommitFileDataEnd(SCommitter *pCommitter) {
  int32_t code = 0;

H
Hongze Cheng 已提交
967
  // write aBlockIdx
H
Hongze Cheng 已提交
968
  code = tsdbWriteBlockIdx(pCommitter->dWriter.pWriter, pCommitter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
969 970
  if (code) goto _err;

H
Hongze Cheng 已提交
971
  // write aBlockL
H
Hongze Cheng 已提交
972
  code = tsdbWriteBlockL(pCommitter->dWriter.pWriter, pCommitter->dWriter.aBlockL);
H
Hongze Cheng 已提交
973 974
  if (code) goto _err;

H
Hongze Cheng 已提交
975
  // update file header
H
Hongze Cheng 已提交
976
  code = tsdbUpdateDFileSetHeader(pCommitter->dWriter.pWriter);
H
Hongze Cheng 已提交
977 978 979
  if (code) goto _err;

  // upsert SDFileSet
H
Hongze Cheng 已提交
980
  code = tsdbFSUpsertFSet(&pCommitter->fs, &pCommitter->dWriter.pWriter->wSet);
H
Hongze Cheng 已提交
981 982 983
  if (code) goto _err;

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

H
Hongze Cheng 已提交
987 988
  if (pCommitter->dReader.pReader) {
    code = tsdbDataFReaderClose(&pCommitter->dReader.pReader);
H
Hongze Cheng 已提交
989
    if (code) goto _err;
H
Hongze Cheng 已提交
990 991 992 993 994 995
  }

_exit:
  return code;

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

H
Hongze Cheng 已提交
1000 1001 1002
static int32_t tsdbMoveCommitData(SCommitter *pCommitter, TABLEID toTable) {
  int32_t code = 0;

H
Hongze Cheng 已提交
1003
  while (pCommitter->dReader.pBlockIdx && tTABLEIDCmprFn(pCommitter->dReader.pBlockIdx, &toTable) < 0) {
H
Hongze Cheng 已提交
1004
    SBlockIdx blockIdx = *pCommitter->dReader.pBlockIdx;
H
Hongze Cheng 已提交
1005
    code = tsdbWriteBlock(pCommitter->dWriter.pWriter, &pCommitter->dReader.mBlock, &blockIdx);
H
Hongze Cheng 已提交
1006 1007 1008 1009 1010 1011 1012
    if (code) goto _err;

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

H
Hongze Cheng 已提交
1013 1014
    code = tsdbCommitterNextTableData(pCommitter);
    if (code) goto _err;
H
Hongze Cheng 已提交
1015 1016 1017 1018 1019 1020 1021 1022 1023
  }

  return code;

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

H
Hongze Cheng 已提交
1024
static int32_t tsdbCommitFileDataImpl(SCommitter *pCommitter);
H
Hongze Cheng 已提交
1025
static int32_t tsdbCommitFileData(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1026 1027 1028
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1029 1030 1031 1032 1033

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

H
Hongze Cheng 已提交
1034 1035 1036 1037 1038
#if 1
  // impl
  code = tsdbCommitFileDataImpl(pCommitter);
  if (code) goto _err;
#else
H
Hongze Cheng 已提交
1039
  // commit file data impl
H
Hongze Cheng 已提交
1040 1041
  for (int32_t iTbData = 0; iTbData < taosArrayGetSize(pCommitter->aTbDataP); iTbData++) {
    STbData *pTbData = (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData);
H
Hongze Cheng 已提交
1042

H
Hongze Cheng 已提交
1043
    // move commit until current (suid, uid)
H
Hongze Cheng 已提交
1044
    code = tsdbMoveCommitData(pCommitter, *(TABLEID *)pTbData);
H
Hongze Cheng 已提交
1045 1046
    if (code) goto _err;

H
Hongze Cheng 已提交
1047
    // commit current table data
H
Hongze Cheng 已提交
1048
    code = tsdbCommitTableData(pCommitter, pTbData);
H
Hongze Cheng 已提交
1049
    if (code) goto _err;
H
Hongze Cheng 已提交
1050 1051 1052 1053 1054 1055

    // move next reader table data if need
    if (pCommitter->dReader.pBlockIdx && tTABLEIDCmprFn(pTbData, pCommitter->dReader.pBlockIdx) == 0) {
      code = tsdbCommitterNextTableData(pCommitter);
      if (code) goto _err;
    }
H
Hongze Cheng 已提交
1056
  }
H
Hongze Cheng 已提交
1057

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

H
Hongze Cheng 已提交
1061
  if (pCommitter->dWriter.bDatal.nRow > 0) {
H
Hongze Cheng 已提交
1062
    code = tsdbCommitLastBlock(pCommitter);
H
Hongze Cheng 已提交
1063
    if (code) goto _err;
H
Hongze Cheng 已提交
1064
  }
H
Hongze Cheng 已提交
1065
#endif
H
Hongze Cheng 已提交
1066

H
Hongze Cheng 已提交
1067 1068
  // commit file data end
  code = tsdbCommitFileDataEnd(pCommitter);
H
Hongze Cheng 已提交
1069
  if (code) goto _err;
H
Hongze Cheng 已提交
1070 1071 1072 1073

  return code;

_err:
S
Shengliang Guan 已提交
1074
  tsdbError("vgId:%d, commit file data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1075 1076
  tsdbDataFReaderClose(&pCommitter->dReader.pReader);
  tsdbDataFWriterClose(&pCommitter->dWriter.pWriter, 0);
H
Hongze Cheng 已提交
1077 1078 1079
  return code;
}

H
Hongze Cheng 已提交
1080 1081
// ----------------------------------------------------------------------------
static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1082
  int32_t code = 0;
H
Hongze Cheng 已提交
1083

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

H
more  
Hongze Cheng 已提交
1087
  taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1088 1089
  pTsdb->imem = pTsdb->mem;
  pTsdb->mem = NULL;
H
more  
Hongze Cheng 已提交
1090
  taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1091

H
Hongze Cheng 已提交
1092
  pCommitter->pTsdb = pTsdb;
H
Hongze Cheng 已提交
1093
  pCommitter->commitID = pTsdb->pVnode->state.commitID;
H
Hongze Cheng 已提交
1094 1095 1096 1097
  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 已提交
1098
  pCommitter->cmprAlg = pTsdb->pVnode->config.tsdbCfg.compression;
H
Hongze Cheng 已提交
1099
  pCommitter->maxLast = TSDB_DEFAULT_LAST_FILE;  // TODO: make it as a config
H
Hongze Cheng 已提交
1100 1101 1102 1103 1104
  pCommitter->aTbDataP = tsdbMemTableGetTbDataArray(pTsdb->imem);
  if (pCommitter->aTbDataP == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
1105
  code = tsdbFSCopy(pTsdb, &pCommitter->fs);
H
Hongze Cheng 已提交
1106 1107 1108 1109 1110
  if (code) goto _err;

  return code;

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

H
Hongze Cheng 已提交
1115 1116 1117
static int32_t tsdbCommitDataStart(SCommitter *pCommitter) {
  int32_t code = 0;

H
Hongze Cheng 已提交
1118
  // Reader
H
Hongze Cheng 已提交
1119 1120
  pCommitter->dReader.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pCommitter->dReader.aBlockIdx == NULL) {
H
Hongze Cheng 已提交
1121 1122 1123 1124
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
1125
  code = tBlockDataCreate(&pCommitter->dReader.bData);
H
Hongze Cheng 已提交
1126 1127
  if (code) goto _exit;

H
Hongze Cheng 已提交
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
  // Writer
  pCommitter->dWriter.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pCommitter->dWriter.aBlockIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  pCommitter->dWriter.aBlockL = taosArrayInit(0, sizeof(SBlockL));
  if (pCommitter->dWriter.aBlockL == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

H
Hongze Cheng 已提交
1141
  code = tBlockDataCreate(&pCommitter->dWriter.bData);
H
Hongze Cheng 已提交
1142
  if (code) goto _exit;
H
Hongze Cheng 已提交
1143

H
Hongze Cheng 已提交
1144
  code = tBlockDataCreate(&pCommitter->dWriter.bDatal);
H
Hongze Cheng 已提交
1145 1146
  if (code) goto _exit;

H
Hongze Cheng 已提交
1147 1148 1149 1150 1151
_exit:
  return code;
}

static void tsdbCommitDataEnd(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1152
  // Reader
H
Hongze Cheng 已提交
1153 1154
  taosArrayDestroy(pCommitter->dReader.aBlockIdx);
  tMapDataClear(&pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
1155
  tBlockDataDestroy(&pCommitter->dReader.bData, 1);
H
Hongze Cheng 已提交
1156 1157

  // Writer
H
Hongze Cheng 已提交
1158
  taosArrayDestroy(pCommitter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
1159
  taosArrayDestroy(pCommitter->dWriter.aBlockL);
H
Hongze Cheng 已提交
1160
  tMapDataClear(&pCommitter->dWriter.mBlock);
H
Hongze Cheng 已提交
1161 1162
  tBlockDataDestroy(&pCommitter->dWriter.bData, 1);
  tBlockDataDestroy(&pCommitter->dWriter.bDatal, 1);
H
Hongze Cheng 已提交
1163 1164
  tTSchemaDestroy(pCommitter->skmTable.pTSchema);
  tTSchemaDestroy(pCommitter->skmRow.pTSchema);
H
Hongze Cheng 已提交
1165 1166
}

H
Hongze Cheng 已提交
1167 1168 1169 1170
static int32_t tsdbCommitData(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1171

H
Hongze Cheng 已提交
1172
  // check
H
Hongze Cheng 已提交
1173
  if (pMemTable->nRow == 0) goto _exit;
H
Hongze Cheng 已提交
1174

H
Hongze Cheng 已提交
1175 1176
  // start ====================
  code = tsdbCommitDataStart(pCommitter);
H
Hongze Cheng 已提交
1177
  if (code) goto _err;
H
Hongze Cheng 已提交
1178 1179 1180

  // impl ====================
  pCommitter->nextKey = pMemTable->minKey;
H
Hongze Cheng 已提交
1181 1182
  while (pCommitter->nextKey < TSKEY_MAX) {
    code = tsdbCommitFileData(pCommitter);
H
Hongze Cheng 已提交
1183
    if (code) goto _err;
H
Hongze Cheng 已提交
1184
  }
H
Hongze Cheng 已提交
1185

H
Hongze Cheng 已提交
1186 1187 1188
  // end ====================
  tsdbCommitDataEnd(pCommitter);

H
Hongze Cheng 已提交
1189
_exit:
H
Hongze Cheng 已提交
1190
  tsdbInfo("vgId:%d, commit data done, nRow:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nRow);
H
Hongze Cheng 已提交
1191
  return code;
H
Hongze Cheng 已提交
1192

H
Hongze Cheng 已提交
1193
_err:
H
Hongze Cheng 已提交
1194
  tsdbCommitDataEnd(pCommitter);
S
Shengliang Guan 已提交
1195
  tsdbError("vgId:%d, commit data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1196 1197
  return code;
}
H
Hongze Cheng 已提交
1198

H
Hongze Cheng 已提交
1199 1200 1201 1202
static int32_t tsdbCommitDel(SCommitter *pCommitter) {
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1203

H
Hongze Cheng 已提交
1204 1205
  if (pMemTable->nDel == 0) {
    goto _exit;
H
Hongze Cheng 已提交
1206
  }
H
Hongze Cheng 已提交
1207

H
Hongze Cheng 已提交
1208 1209 1210 1211 1212
  // start
  code = tsdbCommitDelStart(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1213

H
Hongze Cheng 已提交
1214
  // impl
H
Hongze Cheng 已提交
1215 1216 1217
  int32_t  iDelIdx = 0;
  int32_t  nDelIdx = taosArrayGetSize(pCommitter->aDelIdx);
  int32_t  iTbData = 0;
H
Hongze Cheng 已提交
1218
  int32_t  nTbData = taosArrayGetSize(pCommitter->aTbDataP);
H
Hongze Cheng 已提交
1219 1220 1221 1222 1223
  STbData *pTbData;
  SDelIdx *pDelIdx;

  ASSERT(nTbData > 0);

H
Hongze Cheng 已提交
1224
  pTbData = (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData);
H
Hongze Cheng 已提交
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
  pDelIdx = (iDelIdx < nDelIdx) ? (SDelIdx *)taosArrayGet(pCommitter->aDelIdx, iDelIdx) : NULL;
  while (true) {
    if (pTbData == NULL && pDelIdx == NULL) break;

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

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

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

    iTbData++;
H
Hongze Cheng 已提交
1250
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData) : NULL;
H
Hongze Cheng 已提交
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
    continue;

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

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

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

    iTbData++;
H
Hongze Cheng 已提交
1266
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData) : NULL;
H
Hongze Cheng 已提交
1267 1268 1269
    iDelIdx++;
    pDelIdx = (iDelIdx < nDelIdx) ? (SDelIdx *)taosArrayGet(pCommitter->aDelIdx, iDelIdx) : NULL;
    continue;
H
Hongze Cheng 已提交
1270
  }
H
Hongze Cheng 已提交
1271

H
Hongze Cheng 已提交
1272 1273 1274 1275 1276
  // end
  code = tsdbCommitDelEnd(pCommitter);
  if (code) {
    goto _err;
  }
H
Hongze Cheng 已提交
1277

H
Hongze Cheng 已提交
1278
_exit:
S
Shengliang Guan 已提交
1279
  tsdbDebug("vgId:%d, commit del done, nDel:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nDel);
H
Hongze Cheng 已提交
1280 1281 1282
  return code;

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

H
Hongze Cheng 已提交
1287
static int32_t tsdbEndCommit(SCommitter *pCommitter, int32_t eno) {
H
Hongze Cheng 已提交
1288 1289 1290 1291
  int32_t    code = 0;
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

H
Hongze Cheng 已提交
1292
  ASSERT(eno == 0);
H
Hongze Cheng 已提交
1293 1294 1295

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

H
Hongze Cheng 已提交
1297
  // lock
H
Hongze Cheng 已提交
1298
  taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1299 1300 1301 1302 1303 1304 1305 1306

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

H
Hongze Cheng 已提交
1307
  pTsdb->imem = NULL;
H
Hongze Cheng 已提交
1308 1309

  // unlock
H
Hongze Cheng 已提交
1310 1311 1312
  taosThreadRwlockUnlock(&pTsdb->rwLock);

  tsdbUnrefMemTable(pMemTable);
H
Hongze Cheng 已提交
1313
  tsdbFSDestroy(&pCommitter->fs);
H
Hongze Cheng 已提交
1314
  taosArrayDestroy(pCommitter->aTbDataP);
H
Hongze Cheng 已提交
1315

H
Hongze Cheng 已提交
1316 1317 1318 1319
  // if (pCommitter->toMerge) {
  //   code = tsdbMerge(pTsdb);
  //   if (code) goto _err;
  // }
H
Hongze Cheng 已提交
1320

S
Shengliang Guan 已提交
1321
  tsdbInfo("vgId:%d, tsdb end commit", TD_VID(pTsdb->pVnode));
H
Hongze Cheng 已提交
1322 1323 1324
  return code;

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

H
Hongze Cheng 已提交
1329
// ================================================================================
H
Hongze Cheng 已提交
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
static int32_t tRowInfoCmprFn(const void *p1, const void *p2) {
  SRowInfo *pInfo1 = (SRowInfo *)p1;
  SRowInfo *pInfo2 = (SRowInfo *)p2;

  if (pInfo1->suid < pInfo2->suid) {
    return -1;
  } else if (pInfo1->suid > pInfo2->suid) {
    return 1;
  }

  if (pInfo1->uid < pInfo2->uid) {
    return -1;
  } else if (pInfo1->uid > pInfo2->uid) {
    return 1;
  }

  return tsdbRowCmprFn(&pInfo1->row, &pInfo2->row);
}

H
Hongze Cheng 已提交
1349 1350 1351 1352 1353
static SRowInfo *tsdbGetCommitRow(SCommitter *pCommitter) {
  // TODO
  return NULL;
}

H
Hongze Cheng 已提交
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
static int32_t tsdbNextCommitRow(SCommitter *pCommitter, SRowInfo **ppInfo) {
  int32_t code = 0;

  if (pCommitter->pIter) {
    SDataIter *pIter = pCommitter->pIter;
    if (pCommitter->pIter->type == 0) {  // memory
      tsdbTbDataIterNext(&pIter->iter);
      TSDBROW *pRow = tsdbTbDataIterGet(&pIter->iter);
      while (true) {
        if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
          pCommitter->nextKey = TMIN(pCommitter->nextKey, TSDBROW_TS(pRow));
          pRow = NULL;
        }

        if (pRow) {
          pIter->r.suid = pIter->iter.pTbData->suid;
          pIter->r.uid = pIter->iter.pTbData->uid;
          pIter->r.row = *pRow;
          break;
        }

        pIter->iTbDataP++;
        if (pIter->iTbDataP < taosArrayGetSize(pIter->aTbDataP)) {
          STbData *pTbData = (STbData *)taosArrayGetP(pIter->aTbDataP, pIter->iTbDataP);
          TSDBKEY  keyFrom = {.ts = pCommitter->minKey, .version = VERSION_MIN};
          tsdbTbDataIterOpen(pTbData, &keyFrom, 0, &pIter->iter);
          pRow = tsdbTbDataIterGet(&pIter->iter);
          continue;
        } else {
          pCommitter->pIter = NULL;
          break;
        }
      }
    } else if (pCommitter->pIter->type == 1) {  // last file
      pIter->iRow++;
      if (pIter->iRow < pIter->bData.nRow) {
        pIter->r.uid = pIter->bData.uid ? pIter->bData.uid : pIter->bData.aUid[pIter->iRow];
        pIter->r.row = tsdbRowFromBlockData(&pIter->bData, pIter->iRow);
      } else {
        pIter->iBlockL++;
        if (pIter->iBlockL < taosArrayGetSize(pIter->aBlockL)) {
          SBlockL *pBlockL = (SBlockL *)taosArrayGet(pIter->aBlockL, pIter->iBlockL);

          code = tsdbReadLastBlockEx(pCommitter->dReader.pReader, pIter->iLast, pBlockL, &pIter->bData);
          if (code) goto _exit;

          pIter->iRow = 0;
          pIter->r.suid = pIter->bData.suid;
          pIter->r.uid = pIter->bData.uid ? pIter->bData.uid : pIter->bData.aUid[0];
          pIter->r.row = tsdbRowFromBlockData(&pIter->bData, 0);
        } else {
          pCommitter->pIter = NULL;
        }
      }
    } else {
      ASSERT(0);
    }

    // compare with min in RB Tree
    pIter = (SDataIter *)tRBTreeMin(&pCommitter->rbt);
    if (pCommitter->pIter && pIter) {
      int32_t c = tRowInfoCmprFn(&pCommitter->pIter->r, &pIter->r);
      if (c > 0) {
        tRBTreePut(&pCommitter->rbt, (SRBTreeNode *)pCommitter->pIter);
        pCommitter->pIter = NULL;
      } else {
        ASSERT(c);
      }
    }
  }

  if (pCommitter->pIter == NULL) {
    pCommitter->pIter = (SDataIter *)tRBTreeMin(&pCommitter->rbt);
    if (pCommitter->pIter) {
      tRBTreeDrop(&pCommitter->rbt, (SRBTreeNode *)pCommitter->pIter);
    }
  }

  if (pCommitter->pIter) {
    *ppInfo = &pCommitter->pIter->r;
  } else {
    *ppInfo = NULL;
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
static int32_t tsdbMergeTableData(SCommitter *pCommitter, TABLEID id) {
  int32_t    code = 0;
  SBlockIdx *pBlockIdx = pCommitter->dReader.pBlockIdx;

  ASSERT(pBlockIdx == NULL || tTABLEIDCmprFn(pBlockIdx, &id) >= 0);
  if (pBlockIdx && pBlockIdx->suid == id.suid && pBlockIdx->uid == id.uid) {
    int32_t   iBlock = 0;
    SBlock    block;
    SBlock   *pBlock = &block;
    SRowInfo *pRowInfo = tsdbGetCommitRow(pCommitter);

    ASSERT(pRowInfo->suid == id.suid && pRowInfo->uid == id.uid);

    tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
    while (pBlock && pRowInfo) {
      SBlock  tBlock = {.minKey = TSDBROW_KEY(&pRowInfo->row), .maxKey = TSDBROW_KEY(&pRowInfo->row)};
      int32_t c = tBlockCmprFn(pBlock, &tBlock);

      if (c < 0) {
        code = tMapDataPutItem(&pCommitter->dWriter.mBlock, pBlock, tPutBlock);
        if (code) goto _err;

        iBlock++;
        if (iBlock < pCommitter->dReader.mBlock.nItem) {
          tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
        } else {
          pBlock = NULL;
        }
      } else if (c > 0) {
      } else {
      }
    }

    while (pBlock) {
      code = tMapDataPutItem(&pCommitter->dWriter.mBlock, pBlock, tPutBlock);
      if (code) goto _err;

      iBlock++;
      if (iBlock < pCommitter->dReader.mBlock.nItem) {
        tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
      } else {
        pBlock = NULL;
      }
    }
  }

_exit:
  return code;

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

static int32_t tsdbCommitTableData2(SCommitter *pCommitter, TABLEID id) {
  int32_t code = 0;
  // TODO
  return code;
}

H
Hongze Cheng 已提交
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
static int32_t tsdbCommitFileDataImpl(SCommitter *pCommitter) {
  int32_t code = 0;

  SRowInfo *pRowInfo = NULL;
  TABLEID   id = {0};
  while (true) {
    code = tsdbNextCommitRow(pCommitter, &pRowInfo);
    if (code) goto _err;

    if (pRowInfo == NULL) {
H
Hongze Cheng 已提交
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
      /* end current table data commit (todo) */

      /* end remain table data commit*/
      code = tsdbMoveCommitData(pCommitter, (TABLEID){.suid = INT64_MAX, .uid = INT64_MAX});
      if (code) goto _err;

      if (pCommitter->dWriter.bDatal.nRow > 0) {
        code = tsdbCommitLastBlock(pCommitter);
        if (code) goto _err;
      }

H
Hongze Cheng 已提交
1523 1524 1525 1526
      break;
    }

    if (id.suid != pRowInfo->suid || id.uid != pRowInfo->uid) {
H
Hongze Cheng 已提交
1527
      /* end current table data commit (todo) */
H
Hongze Cheng 已提交
1528

H
Hongze Cheng 已提交
1529
      /* start new table data commit */
H
Hongze Cheng 已提交
1530 1531
      id.suid = pRowInfo->suid;
      id.uid = pRowInfo->uid;
H
Hongze Cheng 已提交
1532 1533 1534 1535 1536 1537 1538 1539 1540
      // reader
      code = tsdbMoveCommitData(pCommitter, id);
      if (code) goto _err;
      // writer
      tMapDataReset(&pCommitter->dWriter.mBlock);
      // other
      code = tsdbCommitterUpdateTableSchema(pCommitter, id.suid, id.uid);
      if (code) goto _err;
      code = tBlockDataInit(&pCommitter->dWriter.bData, id.suid, id.uid, pCommitter->skmRow.pTSchema);
H
Hongze Cheng 已提交
1541 1542 1543
      if (code) goto _err;
    }

H
Hongze Cheng 已提交
1544 1545
    /* merge with data in .data file */
    code = tsdbMergeTableData(pCommitter, id);
H
Hongze Cheng 已提交
1546 1547
    if (code) goto _err;

H
Hongze Cheng 已提交
1548 1549 1550
    /* handle remain table data */
    code = tsdbCommitTableData2(pCommitter, id);
    if (code) goto _err;
H
Hongze Cheng 已提交
1551 1552 1553 1554 1555 1556 1557 1558
  }

  return code;

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