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

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

H
Hongze Cheng 已提交
18
typedef enum { MEMORY_DATA_ITER = 0, STT_DATA_ITER } EDataIterT;
H
Hongze Cheng 已提交
19

H
more  
Hongze Cheng 已提交
20
#define USE_STREAM_COMPRESSION 0
H
compare  
Hongze Cheng 已提交
21

H
Hongze Cheng 已提交
22 23 24
typedef struct {
  SRBTreeNode n;
  SRowInfo    r;
H
Hongze Cheng 已提交
25
  EDataIterT  type;
H
Hongze Cheng 已提交
26 27 28 29 30 31
  union {
    struct {
      int32_t     iTbDataP;
      STbDataIter iter;
    };  // memory data iter
    struct {
H
Hongze Cheng 已提交
32 33 34
      int32_t    iStt;
      SArray    *aSttBlk;
      int32_t    iSttBlk;
H
Hongze Cheng 已提交
35 36
      SBlockData bData;
      int32_t    iRow;
H
Hongze Cheng 已提交
37
    };  // stt file data iter
H
Hongze Cheng 已提交
38 39 40
  };
} SDataIter;

H
Hongze Cheng 已提交
41
typedef struct {
H
Hongze Cheng 已提交
42
  STsdb *pTsdb;
H
Hongze Cheng 已提交
43
  /* commit data */
H
Hongze Cheng 已提交
44
  int64_t commitID;
H
Hongze Cheng 已提交
45 46
  int32_t minutes;
  int8_t  precision;
H
Hongze Cheng 已提交
47 48
  int32_t minRow;
  int32_t maxRow;
H
Hongze Cheng 已提交
49
  int8_t  cmprAlg;
H
Hongze Cheng 已提交
50
  int8_t  sttTrigger;
H
Hongze Cheng 已提交
51 52
  SArray *aTbDataP;  // memory
  STsdbFS fs;        // disk
H
Hongze Cheng 已提交
53
  // --------------
H
Hongze Cheng 已提交
54
  TSKEY   nextKey;  // reset by each table commit
H
Hongze Cheng 已提交
55 56 57
  int32_t commitFid;
  TSKEY   minKey;
  TSKEY   maxKey;
H
Hongze Cheng 已提交
58
  // commit file data
H
Hongze Cheng 已提交
59 60
  struct {
    SDataFReader *pReader;
H
Hongze Cheng 已提交
61 62 63
    SArray       *aBlockIdx;  // SArray<SBlockIdx>
    int32_t       iBlockIdx;
    SBlockIdx    *pBlockIdx;
H
Hongze Cheng 已提交
64
    SMapData      mBlock;  // SMapData<SDataBlk>
H
Hongze Cheng 已提交
65
    SBlockData    bData;
H
Hongze Cheng 已提交
66
  } dReader;
H
Hongze Cheng 已提交
67 68 69
  struct {
    SDataIter *pIter;
    SRBTree    rbt;
H
Hongze Cheng 已提交
70
    SDataIter  dataIter;
H
Hongze Cheng 已提交
71
    SDataIter  aDataIter[TSDB_MAX_STT_TRIGGER];
H
Hongze Cheng 已提交
72
    int8_t     toLastOnly;
H
Hongze Cheng 已提交
73
  };
H
Hongze Cheng 已提交
74 75 76
  struct {
    SDataFWriter *pWriter;
    SArray       *aBlockIdx;  // SArray<SBlockIdx>
H
Hongze Cheng 已提交
77
    SArray       *aSttBlk;    // SArray<SSttBlk>
H
Hongze Cheng 已提交
78
    SMapData      mBlock;     // SMapData<SDataBlk>
H
Hongze Cheng 已提交
79
    SBlockData    bData;
H
compare  
Hongze Cheng 已提交
80
#if USE_STREAM_COMPRESSION
H
Hongze Cheng 已提交
81
    SDiskDataBuilder *pBuilder;
H
compare  
Hongze Cheng 已提交
82 83 84
#else
    SBlockData bDatal;
#endif
H
Hongze Cheng 已提交
85 86 87
  } dWriter;
  SSkmInfo skmTable;
  SSkmInfo skmRow;
H
Hongze Cheng 已提交
88
  /* commit del */
H
Hongze Cheng 已提交
89 90
  SDelFReader *pDelFReader;
  SDelFWriter *pDelFWriter;
H
Hongze Cheng 已提交
91 92 93
  SArray      *aDelIdx;   // SArray<SDelIdx>
  SArray      *aDelIdxN;  // SArray<SDelIdx>
  SArray      *aDelData;  // SArray<SDelData>
H
Hongze Cheng 已提交
94
} SCommitter;
H
refact  
Hongze Cheng 已提交
95

H
Hongze Cheng 已提交
96 97 98 99 100
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
Hongze Cheng 已提交
101 102
static int32_t tsdbNextCommitRow(SCommitter *pCommitter);

H
Hongze Cheng 已提交
103
int32_t tRowInfoCmprFn(const void *p1, const void *p2) {
H
Hongze Cheng 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  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
refact  
Hongze Cheng 已提交
121

H
refact  
Hongze Cheng 已提交
122
int32_t tsdbBegin(STsdb *pTsdb) {
H
Hongze Cheng 已提交
123
  int32_t code = 0;
H
Hongze Cheng 已提交
124
  int32_t lino = 0;
H
Hongze Cheng 已提交
125

126 127
  if (!pTsdb) return code;

H
Hongze Cheng 已提交
128 129
  SMemTable *pMemTable;
  code = tsdbMemTableCreate(pTsdb, &pMemTable);
H
Hongze Cheng 已提交
130
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
131

H
Hongze Cheng 已提交
132
  // lock
H
Hongze Cheng 已提交
133
  if ((code = taosThreadRwlockWrlock(&pTsdb->rwLock))) {
H
Hongze Cheng 已提交
134
    code = TAOS_SYSTEM_ERROR(code);
H
Hongze Cheng 已提交
135
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
136 137 138 139 140
  }

  pTsdb->mem = pMemTable;

  // unlock
H
Hongze Cheng 已提交
141
  if ((code = taosThreadRwlockUnlock(&pTsdb->rwLock))) {
H
Hongze Cheng 已提交
142
    code = TAOS_SYSTEM_ERROR(code);
H
Hongze Cheng 已提交
143
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
144 145
  }

H
Hongze Cheng 已提交
146 147
_exit:
  if (code) {
S
Shengliang Guan 已提交
148
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
149
  }
H
Hongze Cheng 已提交
150
  return code;
H
Hongze Cheng 已提交
151 152
}

H
more  
Hongze Cheng 已提交
153
int32_t tsdbCommit(STsdb *pTsdb) {
154
  if (!pTsdb) return 0;
H
Hongze Cheng 已提交
155

H
more  
Hongze Cheng 已提交
156
  int32_t    code = 0;
H
Hongze Cheng 已提交
157
  int32_t    lino = 0;
H
Hongze Cheng 已提交
158 159 160 161
  SCommitter commith;
  SMemTable *pMemTable = pTsdb->mem;

  // check
H
Hongze Cheng 已提交
162
  if (pMemTable->nRow == 0 && pMemTable->nDel == 0) {
H
Hongze Cheng 已提交
163
    taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
164
    pTsdb->mem = NULL;
H
Hongze Cheng 已提交
165 166
    taosThreadRwlockUnlock(&pTsdb->rwLock);

H
Hongze Cheng 已提交
167
    tsdbUnrefMemTable(pMemTable, NULL);
H
Hongze Cheng 已提交
168 169
    goto _exit;
  }
H
refact  
Hongze Cheng 已提交
170

H
more  
Hongze Cheng 已提交
171
  // start commit
H
more  
Hongze Cheng 已提交
172
  code = tsdbStartCommit(pTsdb, &commith);
H
Hongze Cheng 已提交
173
  TSDB_CHECK_CODE(code, lino, _exit);
H
refact  
Hongze Cheng 已提交
174

H
refact  
Hongze Cheng 已提交
175 176
  // commit impl
  code = tsdbCommitData(&commith);
H
Hongze Cheng 已提交
177
  TSDB_CHECK_CODE(code, lino, _exit);
H
refact  
Hongze Cheng 已提交
178 179

  code = tsdbCommitDel(&commith);
H
Hongze Cheng 已提交
180
  TSDB_CHECK_CODE(code, lino, _exit);
H
refact  
Hongze Cheng 已提交
181 182

  // end commit
H
more  
Hongze Cheng 已提交
183
  code = tsdbEndCommit(&commith, 0);
H
Hongze Cheng 已提交
184
  TSDB_CHECK_CODE(code, lino, _exit);
H
refact  
Hongze Cheng 已提交
185

H
Hongze Cheng 已提交
186
_exit:
H
Hongze Cheng 已提交
187 188
  if (code) {
    tsdbEndCommit(&commith, code);
S
Shengliang Guan 已提交
189
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
190
  }
H
refact  
Hongze Cheng 已提交
191 192 193
  return code;
}

H
Hongze Cheng 已提交
194
static int32_t tsdbCommitDelStart(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
195
  int32_t    code = 0;
H
Hongze Cheng 已提交
196
  int32_t    lino = 0;
H
Hongze Cheng 已提交
197 198 199
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;

H
Hongze Cheng 已提交
200
  if ((pCommitter->aDelIdx = taosArrayInit(0, sizeof(SDelIdx))) == NULL) {
H
Hongze Cheng 已提交
201
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
202
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
203 204
  }

H
Hongze Cheng 已提交
205
  if ((pCommitter->aDelData = taosArrayInit(0, sizeof(SDelData))) == NULL) {
H
Hongze Cheng 已提交
206
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
207
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
208 209
  }

H
Hongze Cheng 已提交
210
  if ((pCommitter->aDelIdxN = taosArrayInit(0, sizeof(SDelIdx))) == NULL) {
H
Hongze Cheng 已提交
211
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
212
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
213
  }
H
Hongze Cheng 已提交
214

H
Hongze Cheng 已提交
215
  SDelFile *pDelFileR = pCommitter->fs.pDelFile;
H
Hongze Cheng 已提交
216
  if (pDelFileR) {
H
Hongze Cheng 已提交
217
    code = tsdbDelFReaderOpen(&pCommitter->pDelFReader, pDelFileR, pTsdb);
H
Hongze Cheng 已提交
218
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
219

H
Hongze Cheng 已提交
220
    code = tsdbReadDelIdx(pCommitter->pDelFReader, pCommitter->aDelIdx);
H
Hongze Cheng 已提交
221
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
222 223
  }

H
Hongze Cheng 已提交
224
  // prepare new
H
Hongze Cheng 已提交
225 226
  SDelFile wDelFile = {.commitID = pCommitter->commitID, .size = 0, .offset = 0};
  code = tsdbDelFWriterOpen(&pCommitter->pDelFWriter, &wDelFile, pTsdb);
H
Hongze Cheng 已提交
227
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
228 229

_exit:
H
Hongze Cheng 已提交
230
  if (code) {
H
Hongze Cheng 已提交
231
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
232 233 234
  } else {
    tsdbDebug("vgId:%d, commit del start", TD_VID(pTsdb->pVnode));
  }
H
Hongze Cheng 已提交
235 236 237
  return code;
}

H
Hongze Cheng 已提交
238
static int32_t tsdbCommitTableDel(SCommitter *pCommitter, STbData *pTbData, SDelIdx *pDelIdx) {
H
Hongze Cheng 已提交
239
  int32_t   code = 0;
H
Hongze Cheng 已提交
240
  int32_t   lino = 0;
H
Hongze Cheng 已提交
241
  SDelData *pDelData;
H
Hongze Cheng 已提交
242 243
  tb_uid_t  suid;
  tb_uid_t  uid;
H
Hongze Cheng 已提交
244 245

  if (pTbData) {
H
Hongze Cheng 已提交
246 247
    suid = pTbData->suid;
    uid = pTbData->uid;
H
Hongze Cheng 已提交
248

H
Hongze Cheng 已提交
249 250 251 252
    if (pTbData->pHead == NULL) {
      pTbData = NULL;
    }
  }
H
Hongze Cheng 已提交
253 254

  if (pDelIdx) {
H
Hongze Cheng 已提交
255 256 257
    suid = pDelIdx->suid;
    uid = pDelIdx->uid;

H
Hongze Cheng 已提交
258
    code = tsdbReadDelData(pCommitter->pDelFReader, pDelIdx, pCommitter->aDelData);
H
Hongze Cheng 已提交
259
    TSDB_CHECK_CODE(code, lino, _exit);
260 261
  } else {
    taosArrayClear(pCommitter->aDelData);
H
Hongze Cheng 已提交
262 263
  }

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

H
Hongze Cheng 已提交
266
  SDelIdx delIdx = {.suid = suid, .uid = uid};
H
Hongze Cheng 已提交
267 268

  // memory
H
Hongze Cheng 已提交
269 270
  pDelData = pTbData ? pTbData->pHead : NULL;
  for (; pDelData; pDelData = pDelData->pNext) {
H
Hongze Cheng 已提交
271 272
    if (taosArrayPush(pCommitter->aDelData, pDelData) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
273
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
274
    }
H
Hongze Cheng 已提交
275 276 277
  }

  // write
H
Hongze Cheng 已提交
278
  code = tsdbWriteDelData(pCommitter->pDelFWriter, pCommitter->aDelData, &delIdx);
H
Hongze Cheng 已提交
279
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
280 281

  // put delIdx
282
  if (taosArrayPush(pCommitter->aDelIdxN, &delIdx) == NULL) {
H
Hongze Cheng 已提交
283
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
284
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
285
  }
H
Hongze Cheng 已提交
286 287

_exit:
H
Hongze Cheng 已提交
288
  if (code) {
H
Hongze Cheng 已提交
289
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
290
              tstrerror(code));
H
Hongze Cheng 已提交
291
  }
H
Hongze Cheng 已提交
292 293 294
  return code;
}

H
Hongze Cheng 已提交
295 296
static int32_t tsdbCommitDelEnd(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
297
  int32_t lino = 0;
H
Hongze Cheng 已提交
298
  STsdb  *pTsdb = pCommitter->pTsdb;
H
Hongze Cheng 已提交
299

H
Hongze Cheng 已提交
300
  code = tsdbWriteDelIdx(pCommitter->pDelFWriter, pCommitter->aDelIdxN);
H
Hongze Cheng 已提交
301
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
302

H
Hongze Cheng 已提交
303
  code = tsdbUpdateDelFileHdr(pCommitter->pDelFWriter);
H
Hongze Cheng 已提交
304
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
305

H
Hongze Cheng 已提交
306
  code = tsdbFSUpsertDelFile(&pCommitter->fs, &pCommitter->pDelFWriter->fDel);
H
Hongze Cheng 已提交
307
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
308

H
Hongze Cheng 已提交
309
  code = tsdbDelFWriterClose(&pCommitter->pDelFWriter, 1);
H
Hongze Cheng 已提交
310
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
311 312

  if (pCommitter->pDelFReader) {
H
Hongze Cheng 已提交
313
    code = tsdbDelFReaderClose(&pCommitter->pDelFReader);
H
Hongze Cheng 已提交
314
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
315 316
  }

H
Hongze Cheng 已提交
317 318 319 320
  taosArrayDestroy(pCommitter->aDelIdx);
  taosArrayDestroy(pCommitter->aDelData);
  taosArrayDestroy(pCommitter->aDelIdxN);

H
Hongze Cheng 已提交
321 322
_exit:
  if (code) {
H
Hongze Cheng 已提交
323
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
324
              tstrerror(code));
H
Hongze Cheng 已提交
325
  }
H
Hongze Cheng 已提交
326 327 328
  return code;
}

H
Hongze Cheng 已提交
329
int32_t tsdbUpdateTableSchema(SMeta *pMeta, int64_t suid, int64_t uid, SSkmInfo *pSkmInfo) {
H
Hongze Cheng 已提交
330
  int32_t code = 0;
H
Hongze Cheng 已提交
331
  int32_t lino = 0;
H
Hongze Cheng 已提交
332

H
Hongze Cheng 已提交
333
  if (suid) {
H
Hongze Cheng 已提交
334 335
    if (pSkmInfo->suid == suid) {
      pSkmInfo->uid = uid;
H
Hongze Cheng 已提交
336 337
      goto _exit;
    }
H
Hongze Cheng 已提交
338
  } else {
H
Hongze Cheng 已提交
339
    if (pSkmInfo->uid == uid) goto _exit;
H
Hongze Cheng 已提交
340 341
  }

H
Hongze Cheng 已提交
342 343 344 345
  pSkmInfo->suid = suid;
  pSkmInfo->uid = uid;
  tTSchemaDestroy(pSkmInfo->pTSchema);
  code = metaGetTbTSchemaEx(pMeta, suid, uid, -1, &pSkmInfo->pTSchema);
H
Hongze Cheng 已提交
346
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
347 348 349 350 351 352 353

_exit:
  return code;
}

static int32_t tsdbCommitterUpdateRowSchema(SCommitter *pCommitter, int64_t suid, int64_t uid, int32_t sver) {
  int32_t code = 0;
H
Hongze Cheng 已提交
354
  int32_t lino = 0;
H
Hongze Cheng 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369

  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);
H
Hongze Cheng 已提交
370
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
371 372 373 374 375

_exit:
  return code;
}

H
Hongze Cheng 已提交
376 377
static int32_t tsdbCommitterNextTableData(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
378
  int32_t lino = 0;
H
Hongze Cheng 已提交
379 380 381 382 383 384 385 386

  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 已提交
387
    code = tsdbReadDataBlk(pCommitter->dReader.pReader, pCommitter->dReader.pBlockIdx, &pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
388
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
389 390 391 392 393 394 395 396 397 398

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

_exit:
  return code;
}

H
Hongze Cheng 已提交
399 400 401 402 403 404 405
static int32_t tDataIterCmprFn(const SRBTreeNode *n1, const SRBTreeNode *n2) {
  SDataIter *pIter1 = (SDataIter *)((uint8_t *)n1 - offsetof(SDataIter, n));
  SDataIter *pIter2 = (SDataIter *)((uint8_t *)n2 - offsetof(SDataIter, n));

  return tRowInfoCmprFn(&pIter1->r, &pIter2->r);
}

H
Hongze Cheng 已提交
406 407
static int32_t tsdbOpenCommitIter(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
408
  int32_t lino = 0;
H
Hongze Cheng 已提交
409 410

  pCommitter->pIter = NULL;
H
Hongze Cheng 已提交
411
  tRBTreeCreate(&pCommitter->rbt, tDataIterCmprFn);
H
Hongze Cheng 已提交
412 413

  // memory
H
Hongze Cheng 已提交
414
  TSDBKEY    tKey = {.ts = pCommitter->minKey, .version = VERSION_MIN};
H
Hongze Cheng 已提交
415 416
  SDataIter *pIter = &pCommitter->dataIter;
  pIter->type = MEMORY_DATA_ITER;
H
Hongze Cheng 已提交
417 418 419 420 421 422 423
  pIter->iTbDataP = 0;
  for (; pIter->iTbDataP < taosArrayGetSize(pCommitter->aTbDataP); pIter->iTbDataP++) {
    STbData *pTbData = (STbData *)taosArrayGetP(pCommitter->aTbDataP, pIter->iTbDataP);
    tsdbTbDataIterOpen(pTbData, &tKey, 0, &pIter->iter);
    TSDBROW *pRow = tsdbTbDataIterGet(&pIter->iter);
    if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
      pCommitter->nextKey = TMIN(pCommitter->nextKey, TSDBROW_TS(pRow));
H
Hongze Cheng 已提交
424
      pRow = NULL;
H
Hongze Cheng 已提交
425 426
    }

H
Hongze Cheng 已提交
427 428
    if (pRow == NULL) continue;

H
Hongze Cheng 已提交
429 430 431 432 433
    pIter->r.suid = pTbData->suid;
    pIter->r.uid = pTbData->uid;
    pIter->r.row = *pRow;
    break;
  }
H
Hongze Cheng 已提交
434
  ASSERT(pIter->iTbDataP < taosArrayGetSize(pCommitter->aTbDataP));
H
Hongze Cheng 已提交
435 436 437
  tRBTreePut(&pCommitter->rbt, (SRBTreeNode *)pIter);

  // disk
H
Hongze Cheng 已提交
438
  pCommitter->toLastOnly = 0;
H
Hongze Cheng 已提交
439
  SDataFReader *pReader = pCommitter->dReader.pReader;
H
Hongze Cheng 已提交
440
  if (pReader) {
H
Hongze Cheng 已提交
441
    if (pReader->pSet->nSttF >= pCommitter->sttTrigger) {
H
Hongze Cheng 已提交
442
      int8_t iIter = 0;
H
Hongze Cheng 已提交
443
      for (int32_t iStt = 0; iStt < pReader->pSet->nSttF; iStt++) {
H
Hongze Cheng 已提交
444
        pIter = &pCommitter->aDataIter[iIter];
H
Hongze Cheng 已提交
445
        pIter->type = STT_DATA_ITER;
H
Hongze Cheng 已提交
446
        pIter->iStt = iStt;
H
Hongze Cheng 已提交
447

H
Hongze Cheng 已提交
448
        code = tsdbReadSttBlk(pCommitter->dReader.pReader, iStt, pIter->aSttBlk);
H
Hongze Cheng 已提交
449
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
450

H
Hongze Cheng 已提交
451
        if (taosArrayGetSize(pIter->aSttBlk) == 0) continue;
H
Hongze Cheng 已提交
452

H
Hongze Cheng 已提交
453 454
        pIter->iSttBlk = 0;
        SSttBlk *pSttBlk = (SSttBlk *)taosArrayGet(pIter->aSttBlk, 0);
H
Hongze Cheng 已提交
455
        code = tsdbReadSttBlockEx(pCommitter->dReader.pReader, iStt, pSttBlk, &pIter->bData);
H
Hongze Cheng 已提交
456
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
457 458 459 460 461 462 463 464 465

        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);

        tRBTreePut(&pCommitter->rbt, (SRBTreeNode *)pIter);
        iIter++;
      }
H
Hongze Cheng 已提交
466
    } else {
H
Hongze Cheng 已提交
467 468 469
      for (int32_t iStt = 0; iStt < pReader->pSet->nSttF; iStt++) {
        SSttFile *pSttFile = pReader->pSet->aSttF[iStt];
        if (pSttFile->size > pSttFile->offset) {
H
Hongze Cheng 已提交
470 471 472 473
          pCommitter->toLastOnly = 1;
          break;
        }
      }
H
Hongze Cheng 已提交
474
    }
H
Hongze Cheng 已提交
475 476 477
  }

  code = tsdbNextCommitRow(pCommitter);
H
Hongze Cheng 已提交
478
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
479

H
Hongze Cheng 已提交
480 481
_exit:
  if (code) {
S
Shengliang Guan 已提交
482
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
483
              tstrerror(code));
H
Hongze Cheng 已提交
484
  }
H
Hongze Cheng 已提交
485 486 487
  return code;
}

H
Hongze Cheng 已提交
488 489
static int32_t tsdbCommitFileDataStart(SCommitter *pCommitter) {
  int32_t    code = 0;
H
Hongze Cheng 已提交
490
  int32_t    lino = 0;
H
Hongze Cheng 已提交
491 492
  STsdb     *pTsdb = pCommitter->pTsdb;
  SDFileSet *pRSet = NULL;
H
Hongze Cheng 已提交
493

H
Hongze Cheng 已提交
494
  // memory
H
Hongze Cheng 已提交
495 496 497
  pCommitter->commitFid = tsdbKeyFid(pCommitter->nextKey, pCommitter->minutes, pCommitter->precision);
  tsdbFidKeyRange(pCommitter->commitFid, pCommitter->minutes, pCommitter->precision, &pCommitter->minKey,
                  &pCommitter->maxKey);
H
Hongze Cheng 已提交
498
  pCommitter->nextKey = TSKEY_MAX;
H
Hongze Cheng 已提交
499

H
Hongze Cheng 已提交
500
  // Reader
H
Hongze Cheng 已提交
501 502
  SDFileSet tDFileSet = {.fid = pCommitter->commitFid};
  pRSet = (SDFileSet *)taosArraySearch(pCommitter->fs.aDFileSet, &tDFileSet, tDFileSetCmprFn, TD_EQ);
H
Hongze Cheng 已提交
503
  if (pRSet) {
H
Hongze Cheng 已提交
504
    code = tsdbDataFReaderOpen(&pCommitter->dReader.pReader, pTsdb, pRSet);
H
Hongze Cheng 已提交
505
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
506

H
Hongze Cheng 已提交
507
    // data
H
Hongze Cheng 已提交
508
    code = tsdbReadBlockIdx(pCommitter->dReader.pReader, pCommitter->dReader.aBlockIdx);
H
Hongze Cheng 已提交
509
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
510

H
Hongze Cheng 已提交
511
    pCommitter->dReader.iBlockIdx = 0;
H
Hongze Cheng 已提交
512 513
    if (taosArrayGetSize(pCommitter->dReader.aBlockIdx) > 0) {
      pCommitter->dReader.pBlockIdx = (SBlockIdx *)taosArrayGet(pCommitter->dReader.aBlockIdx, 0);
H
Hongze Cheng 已提交
514
      code = tsdbReadDataBlk(pCommitter->dReader.pReader, pCommitter->dReader.pBlockIdx, &pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
515
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
516 517 518
    } else {
      pCommitter->dReader.pBlockIdx = NULL;
    }
H
Hongze Cheng 已提交
519
    tBlockDataReset(&pCommitter->dReader.bData);
H
Hongze Cheng 已提交
520
  } else {
H
Hongze Cheng 已提交
521
    pCommitter->dReader.pBlockIdx = NULL;
H
Hongze Cheng 已提交
522
  }
H
Hongze Cheng 已提交
523

H
Hongze Cheng 已提交
524
  // Writer
H
Hongze Cheng 已提交
525 526 527
  SHeadFile fHead = {.commitID = pCommitter->commitID};
  SDataFile fData = {.commitID = pCommitter->commitID};
  SSmaFile  fSma = {.commitID = pCommitter->commitID};
H
Hongze Cheng 已提交
528
  SSttFile  fStt = {.commitID = pCommitter->commitID};
H
Hongze Cheng 已提交
529
  SDFileSet wSet = {.fid = pCommitter->commitFid, .pHeadF = &fHead, .pDataF = &fData, .pSmaF = &fSma};
H
Hongze Cheng 已提交
530
  if (pRSet) {
H
Hongze Cheng 已提交
531
    ASSERT(pRSet->nSttF <= pCommitter->sttTrigger);
H
Hongze Cheng 已提交
532 533
    fData = *pRSet->pDataF;
    fSma = *pRSet->pSmaF;
H
Hongze Cheng 已提交
534
    wSet.diskId = pRSet->diskId;
H
Hongze Cheng 已提交
535
    if (pRSet->nSttF < pCommitter->sttTrigger) {
H
Hongze Cheng 已提交
536 537
      for (int32_t iStt = 0; iStt < pRSet->nSttF; iStt++) {
        wSet.aSttF[iStt] = pRSet->aSttF[iStt];
H
Hongze Cheng 已提交
538
      }
H
Hongze Cheng 已提交
539
      wSet.nSttF = pRSet->nSttF + 1;
H
Hongze Cheng 已提交
540
    } else {
H
Hongze Cheng 已提交
541
      wSet.nSttF = 1;
H
Hongze Cheng 已提交
542
    }
H
Hongze Cheng 已提交
543
  } else {
H
Hongze Cheng 已提交
544
    SDiskID did = {0};
545
    tfsAllocDisk(pTsdb->pVnode->pTfs, 0, &did);
H
Hongze Cheng 已提交
546
    tfsMkdirRecurAt(pTsdb->pVnode->pTfs, pTsdb->path, did);
547
    wSet.diskId = did;
H
Hongze Cheng 已提交
548
    wSet.nSttF = 1;
H
Hongze Cheng 已提交
549
  }
H
Hongze Cheng 已提交
550
  wSet.aSttF[wSet.nSttF - 1] = &fStt;
H
Hongze Cheng 已提交
551
  code = tsdbDataFWriterOpen(&pCommitter->dWriter.pWriter, pTsdb, &wSet);
H
Hongze Cheng 已提交
552
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
553

H
Hongze Cheng 已提交
554
  taosArrayClear(pCommitter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
555
  taosArrayClear(pCommitter->dWriter.aSttBlk);
H
Hongze Cheng 已提交
556 557
  tMapDataReset(&pCommitter->dWriter.mBlock);
  tBlockDataReset(&pCommitter->dWriter.bData);
H
compare  
Hongze Cheng 已提交
558
#if USE_STREAM_COMPRESSION
H
Hongze Cheng 已提交
559
  tDiskDataBuilderClear(pCommitter->dWriter.pBuilder);
H
compare  
Hongze Cheng 已提交
560
#else
H
Hongze Cheng 已提交
561
  tBlockDataReset(&pCommitter->dWriter.bDatal);
H
compare  
Hongze Cheng 已提交
562
#endif
H
Hongze Cheng 已提交
563

H
Hongze Cheng 已提交
564 565
  // open iter
  code = tsdbOpenCommitIter(pCommitter);
H
Hongze Cheng 已提交
566
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
567

H
Hongze Cheng 已提交
568
_exit:
H
Hongze Cheng 已提交
569
  if (code) {
S
Shengliang Guan 已提交
570
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
571
  }
H
Hongze Cheng 已提交
572
  return code;
H
Hongze Cheng 已提交
573 574
}

H
Hongze Cheng 已提交
575 576
int32_t tsdbWriteDataBlock(SDataFWriter *pWriter, SBlockData *pBlockData, SMapData *mDataBlk, int8_t cmprAlg) {
  int32_t code = 0;
H
Hongze Cheng 已提交
577
  int32_t lino = 0;
H
Hongze Cheng 已提交
578

H
Hongze Cheng 已提交
579
  if (pBlockData->nRow == 0) return code;
H
Hongze Cheng 已提交
580

H
Hongze Cheng 已提交
581
  SDataBlk dataBlk;
H
Hongze Cheng 已提交
582
  tDataBlkReset(&dataBlk);
H
Hongze Cheng 已提交
583

H
Hongze Cheng 已提交
584
  // info
H
Hongze Cheng 已提交
585
  dataBlk.nRow += pBlockData->nRow;
H
Hongze Cheng 已提交
586 587
  for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
    TSDBKEY key = {.ts = pBlockData->aTSKEY[iRow], .version = pBlockData->aVersion[iRow]};
H
Hongze Cheng 已提交
588

H
Hongze Cheng 已提交
589
    if (iRow == 0) {
H
Hongze Cheng 已提交
590 591
      if (tsdbKeyCmprFn(&dataBlk.minKey, &key) > 0) {
        dataBlk.minKey = key;
H
Hongze Cheng 已提交
592 593 594
      }
    } else {
      if (pBlockData->aTSKEY[iRow] == pBlockData->aTSKEY[iRow - 1]) {
H
Hongze Cheng 已提交
595
        dataBlk.hasDup = 1;
H
Hongze Cheng 已提交
596 597 598
      }
    }

H
Hongze Cheng 已提交
599 600
    if (iRow == pBlockData->nRow - 1 && tsdbKeyCmprFn(&dataBlk.maxKey, &key) < 0) {
      dataBlk.maxKey = key;
H
Hongze Cheng 已提交
601 602
    }

H
Hongze Cheng 已提交
603 604
    dataBlk.minVer = TMIN(dataBlk.minVer, key.version);
    dataBlk.maxVer = TMAX(dataBlk.maxVer, key.version);
H
Hongze Cheng 已提交
605 606 607
  }

  // write
H
Hongze Cheng 已提交
608
  dataBlk.nSubBlock++;
H
Hongze Cheng 已提交
609 610
  code = tsdbWriteBlockData(pWriter, pBlockData, &dataBlk.aSubBlock[dataBlk.nSubBlock - 1],
                            ((dataBlk.nSubBlock == 1) && !dataBlk.hasDup) ? &dataBlk.smaInfo : NULL, cmprAlg, 0);
H
Hongze Cheng 已提交
611
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
612

H
Hongze Cheng 已提交
613
  // put SDataBlk
H
Hongze Cheng 已提交
614
  code = tMapDataPutItem(mDataBlk, &dataBlk, tPutDataBlk);
H
Hongze Cheng 已提交
615
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
616

H
Hongze Cheng 已提交
617
  // clear
H
Hongze Cheng 已提交
618
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
619

H
Hongze Cheng 已提交
620 621
_exit:
  if (code) {
622 623
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino,
              tstrerror(code));
H
Hongze Cheng 已提交
624
  }
H
Hongze Cheng 已提交
625 626 627
  return code;
}

H
Hongze Cheng 已提交
628 629
int32_t tsdbWriteSttBlock(SDataFWriter *pWriter, SBlockData *pBlockData, SArray *aSttBlk, int8_t cmprAlg) {
  int32_t code = 0;
H
Hongze Cheng 已提交
630
  int32_t lino = 0;
H
Hongze Cheng 已提交
631
  SSttBlk sstBlk;
H
Hongze Cheng 已提交
632

H
Hongze Cheng 已提交
633
  if (pBlockData->nRow == 0) return code;
H
Hongze Cheng 已提交
634

H
Hongze Cheng 已提交
635
  // info
H
Hongze Cheng 已提交
636 637 638 639 640 641
  sstBlk.suid = pBlockData->suid;
  sstBlk.nRow = pBlockData->nRow;
  sstBlk.minKey = TSKEY_MAX;
  sstBlk.maxKey = TSKEY_MIN;
  sstBlk.minVer = VERSION_MAX;
  sstBlk.maxVer = VERSION_MIN;
H
Hongze Cheng 已提交
642
  for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
H
Hongze Cheng 已提交
643 644 645 646
    sstBlk.minKey = TMIN(sstBlk.minKey, pBlockData->aTSKEY[iRow]);
    sstBlk.maxKey = TMAX(sstBlk.maxKey, pBlockData->aTSKEY[iRow]);
    sstBlk.minVer = TMIN(sstBlk.minVer, pBlockData->aVersion[iRow]);
    sstBlk.maxVer = TMAX(sstBlk.maxVer, pBlockData->aVersion[iRow]);
H
Hongze Cheng 已提交
647
  }
H
Hongze Cheng 已提交
648 649
  sstBlk.minUid = pBlockData->uid ? pBlockData->uid : pBlockData->aUid[0];
  sstBlk.maxUid = pBlockData->uid ? pBlockData->uid : pBlockData->aUid[pBlockData->nRow - 1];
H
Hongze Cheng 已提交
650

H
Hongze Cheng 已提交
651
  // write
H
Hongze Cheng 已提交
652
  code = tsdbWriteBlockData(pWriter, pBlockData, &sstBlk.bInfo, NULL, cmprAlg, 1);
H
Hongze Cheng 已提交
653
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
654

H
Hongze Cheng 已提交
655
  // push SSttBlk
H
Hongze Cheng 已提交
656
  if (taosArrayPush(aSttBlk, &sstBlk) == NULL) {
H
Hongze Cheng 已提交
657
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
658
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
659 660
  }

H
Hongze Cheng 已提交
661
  // clear
H
Hongze Cheng 已提交
662
  tBlockDataClear(pBlockData);
H
Hongze Cheng 已提交
663

H
Hongze Cheng 已提交
664 665
_exit:
  if (code) {
666 667
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino,
              tstrerror(code));
H
Hongze Cheng 已提交
668
  }
H
Hongze Cheng 已提交
669
  return code;
H
Hongze Cheng 已提交
670 671
}

H
Hongze Cheng 已提交
672 673 674 675 676 677 678
static int32_t tsdbCommitSttBlk(SDataFWriter *pWriter, SDiskDataBuilder *pBuilder, SArray *aSttBlk) {
  int32_t code = 0;
  int32_t lino = 0;

  if (pBuilder->nRow == 0) return code;

  // gnrt
H
Hongze Cheng 已提交
679 680 681
  const SDiskData *pDiskData;
  const SBlkInfo  *pBlkInfo;
  code = tGnrtDiskData(pBuilder, &pDiskData, &pBlkInfo);
H
Hongze Cheng 已提交
682 683
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
684 685 686 687 688 689
  SSttBlk sttBlk = {.suid = pBuilder->suid,
                    .minUid = pBlkInfo->minUid,
                    .maxUid = pBlkInfo->maxUid,
                    .minKey = pBlkInfo->minKey,
                    .maxKey = pBlkInfo->maxKey,
                    .minVer = pBlkInfo->minVer,
H
Hongze Cheng 已提交
690 691
                    .maxVer = pBlkInfo->maxVer,
                    .nRow = pBuilder->nRow};
H
Hongze Cheng 已提交
692
  // write
H
Hongze Cheng 已提交
693
  code = tsdbWriteDiskData(pWriter, pDiskData, &sttBlk.bInfo, NULL);
H
Hongze Cheng 已提交
694 695 696 697 698 699 700
  TSDB_CHECK_CODE(code, lino, _exit);

  // push
  if (taosArrayPush(aSttBlk, &sttBlk) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
701

H
Hongze Cheng 已提交
702 703
  // clear
  tDiskDataBuilderClear(pBuilder);
H
Hongze Cheng 已提交
704 705 706

_exit:
  if (code) {
707 708
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pWriter->pTsdb->pVnode), __func__, lino,
              tstrerror(code));
H
Hongze Cheng 已提交
709
  }
H
Hongze Cheng 已提交
710 711 712
  return code;
}

H
Hongze Cheng 已提交
713 714
static int32_t tsdbCommitFileDataEnd(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
715
  int32_t lino = 0;
H
Hongze Cheng 已提交
716

H
Hongze Cheng 已提交
717
  // write aBlockIdx
H
Hongze Cheng 已提交
718
  code = tsdbWriteBlockIdx(pCommitter->dWriter.pWriter, pCommitter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
719
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
720

H
Hongze Cheng 已提交
721 722
  // write aSttBlk
  code = tsdbWriteSttBlk(pCommitter->dWriter.pWriter, pCommitter->dWriter.aSttBlk);
H
Hongze Cheng 已提交
723
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
724

H
Hongze Cheng 已提交
725
  // update file header
H
Hongze Cheng 已提交
726
  code = tsdbUpdateDFileSetHeader(pCommitter->dWriter.pWriter);
H
Hongze Cheng 已提交
727
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
728 729

  // upsert SDFileSet
H
Hongze Cheng 已提交
730
  code = tsdbFSUpsertFSet(&pCommitter->fs, &pCommitter->dWriter.pWriter->wSet);
H
Hongze Cheng 已提交
731
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
732 733

  // close and sync
H
Hongze Cheng 已提交
734
  code = tsdbDataFWriterClose(&pCommitter->dWriter.pWriter, 1);
H
Hongze Cheng 已提交
735
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
736

H
Hongze Cheng 已提交
737 738
  if (pCommitter->dReader.pReader) {
    code = tsdbDataFReaderClose(&pCommitter->dReader.pReader);
H
Hongze Cheng 已提交
739
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
740 741 742
  }

_exit:
H
Hongze Cheng 已提交
743
  if (code) {
S
Shengliang Guan 已提交
744
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
745
              tstrerror(code));
H
Hongze Cheng 已提交
746
  }
H
Hongze Cheng 已提交
747 748 749
  return code;
}

H
Hongze Cheng 已提交
750 751
static int32_t tsdbMoveCommitData(SCommitter *pCommitter, TABLEID toTable) {
  int32_t code = 0;
H
Hongze Cheng 已提交
752
  int32_t lino = 0;
H
Hongze Cheng 已提交
753

H
Hongze Cheng 已提交
754
  while (pCommitter->dReader.pBlockIdx && tTABLEIDCmprFn(pCommitter->dReader.pBlockIdx, &toTable) < 0) {
H
Hongze Cheng 已提交
755
    SBlockIdx blockIdx = *pCommitter->dReader.pBlockIdx;
H
Hongze Cheng 已提交
756
    code = tsdbWriteDataBlk(pCommitter->dWriter.pWriter, &pCommitter->dReader.mBlock, &blockIdx);
H
Hongze Cheng 已提交
757
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
758 759 760

    if (taosArrayPush(pCommitter->dWriter.aBlockIdx, &blockIdx) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
761
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
762 763
    }

H
Hongze Cheng 已提交
764
    code = tsdbCommitterNextTableData(pCommitter);
H
Hongze Cheng 已提交
765
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
766 767
  }

H
Hongze Cheng 已提交
768 769
_exit:
  if (code) {
S
Shengliang Guan 已提交
770
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
771
              tstrerror(code));
H
Hongze Cheng 已提交
772
  }
H
Hongze Cheng 已提交
773 774 775
  return code;
}

H
Hongze Cheng 已提交
776
static int32_t tsdbCommitFileDataImpl(SCommitter *pCommitter);
H
Hongze Cheng 已提交
777
static int32_t tsdbCommitFileData(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
778
  int32_t    code = 0;
H
Hongze Cheng 已提交
779
  int32_t    lino = 0;
H
Hongze Cheng 已提交
780 781
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
782 783 784

  // commit file data start
  code = tsdbCommitFileDataStart(pCommitter);
H
Hongze Cheng 已提交
785
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
786

H
Hongze Cheng 已提交
787 788
  // impl
  code = tsdbCommitFileDataImpl(pCommitter);
H
Hongze Cheng 已提交
789
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
790

H
Hongze Cheng 已提交
791 792
  // commit file data end
  code = tsdbCommitFileDataEnd(pCommitter);
H
Hongze Cheng 已提交
793
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
794

H
Hongze Cheng 已提交
795 796
_exit:
  if (code) {
S
Shengliang Guan 已提交
797
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
798 799 800
    tsdbDataFReaderClose(&pCommitter->dReader.pReader);
    tsdbDataFWriterClose(&pCommitter->dWriter.pWriter, 0);
  }
H
Hongze Cheng 已提交
801 802 803
  return code;
}

H
Hongze Cheng 已提交
804 805
// ----------------------------------------------------------------------------
static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitter *pCommitter) {
H
Hongze Cheng 已提交
806
  int32_t code = 0;
H
Hongze Cheng 已提交
807
  int32_t lino = 0;
H
Hongze Cheng 已提交
808

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

H
more  
Hongze Cheng 已提交
812
  taosThreadRwlockWrlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
813 814
  pTsdb->imem = pTsdb->mem;
  pTsdb->mem = NULL;
H
more  
Hongze Cheng 已提交
815
  taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
816

H
Hongze Cheng 已提交
817
  pCommitter->pTsdb = pTsdb;
H
Hongze Cheng 已提交
818
  pCommitter->commitID = pTsdb->pVnode->state.commitID;
H
Hongze Cheng 已提交
819 820 821 822
  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 已提交
823
  pCommitter->cmprAlg = pTsdb->pVnode->config.tsdbCfg.compression;
H
Hongze Cheng 已提交
824
  pCommitter->sttTrigger = pTsdb->pVnode->config.sttTrigger;
H
Hongze Cheng 已提交
825 826 827
  pCommitter->aTbDataP = tsdbMemTableGetTbDataArray(pTsdb->imem);
  if (pCommitter->aTbDataP == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
828
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
829
  }
H
Hongze Cheng 已提交
830
  code = tsdbFSCopy(pTsdb, &pCommitter->fs);
H
Hongze Cheng 已提交
831
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
832

H
Hongze Cheng 已提交
833 834
_exit:
  if (code) {
S
Shengliang Guan 已提交
835
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
836
  }
H
Hongze Cheng 已提交
837 838 839
  return code;
}

H
Hongze Cheng 已提交
840 841
static int32_t tsdbCommitDataStart(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
842
  int32_t lino = 0;
H
Hongze Cheng 已提交
843

H
Hongze Cheng 已提交
844
  // reader
H
Hongze Cheng 已提交
845 846
  pCommitter->dReader.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pCommitter->dReader.aBlockIdx == NULL) {
H
Hongze Cheng 已提交
847
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
848
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
849 850
  }

H
Hongze Cheng 已提交
851
  code = tBlockDataCreate(&pCommitter->dReader.bData);
H
Hongze Cheng 已提交
852
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
853

H
Hongze Cheng 已提交
854
  // merger
H
Hongze Cheng 已提交
855
  for (int32_t iStt = 0; iStt < TSDB_MAX_STT_TRIGGER; iStt++) {
H
Hongze Cheng 已提交
856 857 858
    SDataIter *pIter = &pCommitter->aDataIter[iStt];
    pIter->aSttBlk = taosArrayInit(0, sizeof(SSttBlk));
    if (pIter->aSttBlk == NULL) {
H
Hongze Cheng 已提交
859
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
860
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
861 862 863
    }

    code = tBlockDataCreate(&pIter->bData);
H
Hongze Cheng 已提交
864
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
865 866 867
  }

  // writer
H
Hongze Cheng 已提交
868 869 870
  pCommitter->dWriter.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pCommitter->dWriter.aBlockIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
871
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
872 873
  }

H
Hongze Cheng 已提交
874 875
  pCommitter->dWriter.aSttBlk = taosArrayInit(0, sizeof(SSttBlk));
  if (pCommitter->dWriter.aSttBlk == NULL) {
H
Hongze Cheng 已提交
876
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
877
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
878 879
  }

H
Hongze Cheng 已提交
880
  code = tBlockDataCreate(&pCommitter->dWriter.bData);
H
Hongze Cheng 已提交
881
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
882

H
compare  
Hongze Cheng 已提交
883
#if USE_STREAM_COMPRESSION
H
Hongze Cheng 已提交
884
  code = tDiskDataBuilderCreate(&pCommitter->dWriter.pBuilder);
H
compare  
Hongze Cheng 已提交
885
#else
H
Hongze Cheng 已提交
886
  code = tBlockDataCreate(&pCommitter->dWriter.bDatal);
H
compare  
Hongze Cheng 已提交
887
#endif
H
Hongze Cheng 已提交
888
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
889

H
Hongze Cheng 已提交
890
_exit:
H
Hongze Cheng 已提交
891
  if (code) {
S
Shengliang Guan 已提交
892
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
893
              tstrerror(code));
H
Hongze Cheng 已提交
894
  }
H
Hongze Cheng 已提交
895 896 897 898
  return code;
}

static void tsdbCommitDataEnd(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
899
  // reader
H
Hongze Cheng 已提交
900 901
  taosArrayDestroy(pCommitter->dReader.aBlockIdx);
  tMapDataClear(&pCommitter->dReader.mBlock);
H
Hongze Cheng 已提交
902
  tBlockDataDestroy(&pCommitter->dReader.bData, 1);
H
Hongze Cheng 已提交
903

H
Hongze Cheng 已提交
904
  // merger
H
Hongze Cheng 已提交
905
  for (int32_t iStt = 0; iStt < TSDB_MAX_STT_TRIGGER; iStt++) {
H
Hongze Cheng 已提交
906 907
    SDataIter *pIter = &pCommitter->aDataIter[iStt];
    taosArrayDestroy(pIter->aSttBlk);
H
Hongze Cheng 已提交
908 909 910 911
    tBlockDataDestroy(&pIter->bData, 1);
  }

  // writer
H
Hongze Cheng 已提交
912
  taosArrayDestroy(pCommitter->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
913
  taosArrayDestroy(pCommitter->dWriter.aSttBlk);
H
Hongze Cheng 已提交
914
  tMapDataClear(&pCommitter->dWriter.mBlock);
H
Hongze Cheng 已提交
915
  tBlockDataDestroy(&pCommitter->dWriter.bData, 1);
H
compare  
Hongze Cheng 已提交
916
#if USE_STREAM_COMPRESSION
H
Hongze Cheng 已提交
917
  tDiskDataBuilderDestroy(pCommitter->dWriter.pBuilder);
H
compare  
Hongze Cheng 已提交
918
#else
H
Hongze Cheng 已提交
919
  tBlockDataDestroy(&pCommitter->dWriter.bDatal, 1);
H
compare  
Hongze Cheng 已提交
920
#endif
H
Hongze Cheng 已提交
921 922
  tTSchemaDestroy(pCommitter->skmTable.pTSchema);
  tTSchemaDestroy(pCommitter->skmRow.pTSchema);
H
Hongze Cheng 已提交
923 924
}

H
Hongze Cheng 已提交
925
static int32_t tsdbCommitData(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
926 927 928
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
929 930
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
931

H
Hongze Cheng 已提交
932
  // check
H
Hongze Cheng 已提交
933
  if (pMemTable->nRow == 0) goto _exit;
H
Hongze Cheng 已提交
934

H
Hongze Cheng 已提交
935 936
  // start ====================
  code = tsdbCommitDataStart(pCommitter);
H
Hongze Cheng 已提交
937
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
938 939 940

  // impl ====================
  pCommitter->nextKey = pMemTable->minKey;
H
Hongze Cheng 已提交
941 942
  while (pCommitter->nextKey < TSKEY_MAX) {
    code = tsdbCommitFileData(pCommitter);
H
Hongze Cheng 已提交
943
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
944
  }
H
Hongze Cheng 已提交
945

H
Hongze Cheng 已提交
946 947 948
  // end ====================
  tsdbCommitDataEnd(pCommitter);

H
Hongze Cheng 已提交
949
_exit:
H
Hongze Cheng 已提交
950
  if (code) {
S
Shengliang Guan 已提交
951
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
952
  }
H
Hongze Cheng 已提交
953 954
  return code;
}
H
Hongze Cheng 已提交
955

H
Hongze Cheng 已提交
956
static int32_t tsdbCommitDel(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
957 958 959
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
960 961
  STsdb     *pTsdb = pCommitter->pTsdb;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
962

H
Hongze Cheng 已提交
963 964
  if (pMemTable->nDel == 0) {
    goto _exit;
H
Hongze Cheng 已提交
965
  }
H
Hongze Cheng 已提交
966

H
Hongze Cheng 已提交
967 968 969
  // start
  code = tsdbCommitDelStart(pCommitter);
  if (code) {
H
Hongze Cheng 已提交
970
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
971
  }
H
Hongze Cheng 已提交
972

H
Hongze Cheng 已提交
973
  // impl
H
Hongze Cheng 已提交
974 975 976
  int32_t  iDelIdx = 0;
  int32_t  nDelIdx = taosArrayGetSize(pCommitter->aDelIdx);
  int32_t  iTbData = 0;
H
Hongze Cheng 已提交
977
  int32_t  nTbData = taosArrayGetSize(pCommitter->aTbDataP);
H
Hongze Cheng 已提交
978 979 980 981 982
  STbData *pTbData;
  SDelIdx *pDelIdx;

  ASSERT(nTbData > 0);

H
Hongze Cheng 已提交
983
  pTbData = (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData);
H
Hongze Cheng 已提交
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
  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);
H
Hongze Cheng 已提交
1006
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1007 1008

    iTbData++;
H
Hongze Cheng 已提交
1009
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData) : NULL;
H
Hongze Cheng 已提交
1010 1011 1012 1013
    continue;

  _commit_disk_del:
    code = tsdbCommitTableDel(pCommitter, NULL, pDelIdx);
H
Hongze Cheng 已提交
1014
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1015 1016 1017 1018 1019 1020 1021

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

  _commit_mem_and_disk_del:
    code = tsdbCommitTableDel(pCommitter, pTbData, pDelIdx);
H
Hongze Cheng 已提交
1022
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1023 1024

    iTbData++;
H
Hongze Cheng 已提交
1025
    pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData) : NULL;
H
Hongze Cheng 已提交
1026 1027 1028
    iDelIdx++;
    pDelIdx = (iDelIdx < nDelIdx) ? (SDelIdx *)taosArrayGet(pCommitter->aDelIdx, iDelIdx) : NULL;
    continue;
H
Hongze Cheng 已提交
1029
  }
H
Hongze Cheng 已提交
1030

H
Hongze Cheng 已提交
1031 1032
  // end
  code = tsdbCommitDelEnd(pCommitter);
H
Hongze Cheng 已提交
1033
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1034

H
Hongze Cheng 已提交
1035
_exit:
H
Hongze Cheng 已提交
1036
  if (code) {
S
Shengliang Guan 已提交
1037
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1038
  } else {
S
Shengliang Guan 已提交
1039
    tsdbDebug("vgId:%d, commit del done, nDel:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nDel);
H
Hongze Cheng 已提交
1040
  }
H
Hongze Cheng 已提交
1041
  return code;
H
Hongze Cheng 已提交
1042 1043
}

H
Hongze Cheng 已提交
1044
static int32_t tsdbEndCommit(SCommitter *pCommitter, int32_t eno) {
H
Hongze Cheng 已提交
1045 1046
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
1047
  STsdb  *pTsdb = pCommitter->pTsdb;
H
Hongze Cheng 已提交
1048

H
Hongze Cheng 已提交
1049 1050 1051 1052 1053
  if (eno) {
    code = eno;
    TSDB_CHECK_CODE(code, lino, _exit);
  } else {
    code = tsdbFSPrepareCommit(pCommitter->pTsdb, &pCommitter->fs);
H
Hongze Cheng 已提交
1054
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1055 1056
  }

H
Hongze Cheng 已提交
1057
  pTsdb->imem = NULL;
H
Hongze Cheng 已提交
1058 1059

  // unlock
H
Hongze Cheng 已提交
1060 1061
  taosThreadRwlockUnlock(&pTsdb->rwLock);

H
Hongze Cheng 已提交
1062
  tsdbUnrefMemTable(pMemTable, NULL);
H
Hongze Cheng 已提交
1063
_exit:
H
Hongze Cheng 已提交
1064
  tsdbFSDestroy(&pCommitter->fs);
H
Hongze Cheng 已提交
1065
  taosArrayDestroy(pCommitter->aTbDataP);
H
Hongze Cheng 已提交
1066
  pCommitter->aTbDataP = NULL;
H
Hongze Cheng 已提交
1067
  if (code || eno) {
S
Shengliang Guan 已提交
1068
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1069
  } else {
S
Shengliang Guan 已提交
1070
    tsdbInfo("vgId:%d, tsdb end commit", TD_VID(pTsdb->pVnode));
H
Hongze Cheng 已提交
1071
  }
H
Hongze Cheng 已提交
1072 1073
  return code;
}
H
Hongze Cheng 已提交
1074

H
Hongze Cheng 已提交
1075
// ================================================================================
H
Hongze Cheng 已提交
1076

H
Hongze Cheng 已提交
1077 1078
static FORCE_INLINE SRowInfo *tsdbGetCommitRow(SCommitter *pCommitter) {
  return (pCommitter->pIter) ? &pCommitter->pIter->r : NULL;
H
Hongze Cheng 已提交
1079 1080
}

H
Hongze Cheng 已提交
1081
static int32_t tsdbNextCommitRow(SCommitter *pCommitter) {
H
Hongze Cheng 已提交
1082
  int32_t code = 0;
H
Hongze Cheng 已提交
1083
  int32_t lino = 0;
H
Hongze Cheng 已提交
1084 1085 1086

  if (pCommitter->pIter) {
    SDataIter *pIter = pCommitter->pIter;
H
Hongze Cheng 已提交
1087
    if (pCommitter->pIter->type == MEMORY_DATA_ITER) {  // memory
H
Hongze Cheng 已提交
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
      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++;
H
Hongze Cheng 已提交
1104 1105
        if (pIter->iTbDataP < taosArrayGetSize(pCommitter->aTbDataP)) {
          STbData *pTbData = (STbData *)taosArrayGetP(pCommitter->aTbDataP, pIter->iTbDataP);
H
Hongze Cheng 已提交
1106 1107 1108 1109 1110 1111 1112 1113 1114
          TSDBKEY  keyFrom = {.ts = pCommitter->minKey, .version = VERSION_MIN};
          tsdbTbDataIterOpen(pTbData, &keyFrom, 0, &pIter->iter);
          pRow = tsdbTbDataIterGet(&pIter->iter);
          continue;
        } else {
          pCommitter->pIter = NULL;
          break;
        }
      }
H
Hongze Cheng 已提交
1115
    } else if (pCommitter->pIter->type == STT_DATA_ITER) {  // last file
H
Hongze Cheng 已提交
1116 1117 1118 1119 1120
      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 {
H
Hongze Cheng 已提交
1121 1122 1123
        pIter->iSttBlk++;
        if (pIter->iSttBlk < taosArrayGetSize(pIter->aSttBlk)) {
          SSttBlk *pSttBlk = (SSttBlk *)taosArrayGet(pIter->aSttBlk, pIter->iSttBlk);
H
Hongze Cheng 已提交
1124

H
Hongze Cheng 已提交
1125
          code = tsdbReadSttBlockEx(pCommitter->dReader.pReader, pIter->iStt, pSttBlk, &pIter->bData);
H
Hongze Cheng 已提交
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
          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);
    }
  }

_exit:
H
Hongze Cheng 已提交
1161
  if (code) {
S
Shengliang Guan 已提交
1162
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
1163
              tstrerror(code));
H
Hongze Cheng 已提交
1164
  }
H
Hongze Cheng 已提交
1165 1166 1167
  return code;
}

H
Hongze Cheng 已提交
1168
static int32_t tsdbCommitAheadBlock(SCommitter *pCommitter, SDataBlk *pDataBlk) {
H
Hongze Cheng 已提交
1169 1170 1171
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1172 1173 1174 1175 1176 1177 1178 1179
  SBlockData *pBlockData = &pCommitter->dWriter.bData;
  SRowInfo   *pRowInfo = tsdbGetCommitRow(pCommitter);
  TABLEID     id = {.suid = pRowInfo->suid, .uid = pRowInfo->uid};

  tBlockDataClear(pBlockData);
  while (pRowInfo) {
    ASSERT(pRowInfo->row.type == 0);
    code = tsdbCommitterUpdateRowSchema(pCommitter, id.suid, id.uid, TSDBROW_SVERSION(&pRowInfo->row));
H
Hongze Cheng 已提交
1180
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1181 1182

    code = tBlockDataAppendRow(pBlockData, &pRowInfo->row, pCommitter->skmRow.pTSchema, id.uid);
H
Hongze Cheng 已提交
1183
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1184 1185

    code = tsdbNextCommitRow(pCommitter);
H
Hongze Cheng 已提交
1186
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1187 1188 1189 1190 1191 1192 1193

    pRowInfo = tsdbGetCommitRow(pCommitter);
    if (pRowInfo) {
      if (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid) {
        pRowInfo = NULL;
      } else {
        TSDBKEY tKey = TSDBROW_KEY(&pRowInfo->row);
H
Hongze Cheng 已提交
1194
        if (tsdbKeyCmprFn(&tKey, &pDataBlk->minKey) >= 0) pRowInfo = NULL;
H
Hongze Cheng 已提交
1195 1196 1197
      }
    }

H
Hongze Cheng 已提交
1198
    if (pBlockData->nRow >= pCommitter->maxRow) {
H
Hongze Cheng 已提交
1199 1200
      code =
          tsdbWriteDataBlock(pCommitter->dWriter.pWriter, pBlockData, &pCommitter->dWriter.mBlock, pCommitter->cmprAlg);
H
Hongze Cheng 已提交
1201
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1202 1203 1204
    }
  }

H
Hongze Cheng 已提交
1205
  code = tsdbWriteDataBlock(pCommitter->dWriter.pWriter, pBlockData, &pCommitter->dWriter.mBlock, pCommitter->cmprAlg);
H
Hongze Cheng 已提交
1206
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1207

H
Hongze Cheng 已提交
1208 1209
_exit:
  if (code) {
S
Shengliang Guan 已提交
1210
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
1211
              tstrerror(code));
H
Hongze Cheng 已提交
1212
  }
H
Hongze Cheng 已提交
1213 1214 1215
  return code;
}

H
Hongze Cheng 已提交
1216
static int32_t tsdbCommitMergeBlock(SCommitter *pCommitter, SDataBlk *pDataBlk) {
H
Hongze Cheng 已提交
1217 1218 1219
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1220 1221 1222 1223 1224
  SRowInfo   *pRowInfo = tsdbGetCommitRow(pCommitter);
  TABLEID     id = {.suid = pRowInfo->suid, .uid = pRowInfo->uid};
  SBlockData *pBDataR = &pCommitter->dReader.bData;
  SBlockData *pBDataW = &pCommitter->dWriter.bData;

H
Hongze Cheng 已提交
1225
  code = tsdbReadDataBlock(pCommitter->dReader.pReader, pDataBlk, pBDataR);
H
Hongze Cheng 已提交
1226
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236

  tBlockDataClear(pBDataW);
  int32_t  iRow = 0;
  TSDBROW  row = tsdbRowFromBlockData(pBDataR, 0);
  TSDBROW *pRow = &row;

  while (pRow && pRowInfo) {
    int32_t c = tsdbRowCmprFn(pRow, &pRowInfo->row);
    if (c < 0) {
      code = tBlockDataAppendRow(pBDataW, pRow, NULL, id.uid);
H
Hongze Cheng 已提交
1237
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247

      iRow++;
      if (iRow < pBDataR->nRow) {
        row = tsdbRowFromBlockData(pBDataR, iRow);
      } else {
        pRow = NULL;
      }
    } else if (c > 0) {
      ASSERT(pRowInfo->row.type == 0);
      code = tsdbCommitterUpdateRowSchema(pCommitter, id.suid, id.uid, TSDBROW_SVERSION(&pRowInfo->row));
H
Hongze Cheng 已提交
1248
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1249 1250

      code = tBlockDataAppendRow(pBDataW, &pRowInfo->row, pCommitter->skmRow.pTSchema, id.uid);
H
Hongze Cheng 已提交
1251
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1252 1253

      code = tsdbNextCommitRow(pCommitter);
H
Hongze Cheng 已提交
1254
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1255 1256 1257 1258 1259 1260 1261

      pRowInfo = tsdbGetCommitRow(pCommitter);
      if (pRowInfo) {
        if (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid) {
          pRowInfo = NULL;
        } else {
          TSDBKEY tKey = TSDBROW_KEY(&pRowInfo->row);
H
Hongze Cheng 已提交
1262
          if (tsdbKeyCmprFn(&tKey, &pDataBlk->maxKey) > 0) pRowInfo = NULL;
H
Hongze Cheng 已提交
1263 1264 1265 1266 1267 1268
        }
      }
    } else {
      ASSERT(0);
    }

H
Hongze Cheng 已提交
1269
    if (pBDataW->nRow >= pCommitter->maxRow) {
H
Hongze Cheng 已提交
1270
      code = tsdbWriteDataBlock(pCommitter->dWriter.pWriter, pBDataW, &pCommitter->dWriter.mBlock, pCommitter->cmprAlg);
H
Hongze Cheng 已提交
1271
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1272 1273 1274 1275 1276
    }
  }

  while (pRow) {
    code = tBlockDataAppendRow(pBDataW, pRow, NULL, id.uid);
H
Hongze Cheng 已提交
1277
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1278 1279 1280 1281 1282 1283 1284 1285

    iRow++;
    if (iRow < pBDataR->nRow) {
      row = tsdbRowFromBlockData(pBDataR, iRow);
    } else {
      pRow = NULL;
    }

H
Hongze Cheng 已提交
1286
    if (pBDataW->nRow >= pCommitter->maxRow) {
H
Hongze Cheng 已提交
1287
      code = tsdbWriteDataBlock(pCommitter->dWriter.pWriter, pBDataW, &pCommitter->dWriter.mBlock, pCommitter->cmprAlg);
H
Hongze Cheng 已提交
1288
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1289 1290 1291
    }
  }

H
Hongze Cheng 已提交
1292
  code = tsdbWriteDataBlock(pCommitter->dWriter.pWriter, pBDataW, &pCommitter->dWriter.mBlock, pCommitter->cmprAlg);
H
Hongze Cheng 已提交
1293
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1294

H
Hongze Cheng 已提交
1295 1296
_exit:
  if (code) {
S
Shengliang Guan 已提交
1297
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
1298
              tstrerror(code));
H
Hongze Cheng 已提交
1299
  }
H
Hongze Cheng 已提交
1300 1301 1302
  return code;
}

H
Hongze Cheng 已提交
1303
static int32_t tsdbMergeTableData(SCommitter *pCommitter, TABLEID id) {
H
Hongze Cheng 已提交
1304 1305 1306
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
1307 1308 1309 1310 1311
  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;
H
Hongze Cheng 已提交
1312 1313
    SDataBlk  block;
    SDataBlk *pDataBlk = &block;
H
Hongze Cheng 已提交
1314 1315 1316 1317
    SRowInfo *pRowInfo = tsdbGetCommitRow(pCommitter);

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

H
Hongze Cheng 已提交
1318 1319 1320
    tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pDataBlk, tGetDataBlk);
    while (pDataBlk && pRowInfo) {
      SDataBlk tBlock = {.minKey = TSDBROW_KEY(&pRowInfo->row), .maxKey = TSDBROW_KEY(&pRowInfo->row)};
H
Hongze Cheng 已提交
1321
      int32_t  c = tDataBlkCmprFn(pDataBlk, &tBlock);
H
Hongze Cheng 已提交
1322 1323

      if (c < 0) {
H
Hongze Cheng 已提交
1324
        code = tMapDataPutItem(&pCommitter->dWriter.mBlock, pDataBlk, tPutDataBlk);
H
Hongze Cheng 已提交
1325
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1326 1327 1328

        iBlock++;
        if (iBlock < pCommitter->dReader.mBlock.nItem) {
H
Hongze Cheng 已提交
1329
          tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pDataBlk, tGetDataBlk);
H
Hongze Cheng 已提交
1330
        } else {
H
Hongze Cheng 已提交
1331
          pDataBlk = NULL;
H
Hongze Cheng 已提交
1332 1333
        }
      } else if (c > 0) {
H
Hongze Cheng 已提交
1334
        code = tsdbCommitAheadBlock(pCommitter, pDataBlk);
H
Hongze Cheng 已提交
1335
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1336 1337 1338

        pRowInfo = tsdbGetCommitRow(pCommitter);
        if (pRowInfo && (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid)) pRowInfo = NULL;
H
Hongze Cheng 已提交
1339
      } else {
H
Hongze Cheng 已提交
1340
        code = tsdbCommitMergeBlock(pCommitter, pDataBlk);
H
Hongze Cheng 已提交
1341
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1342 1343 1344

        iBlock++;
        if (iBlock < pCommitter->dReader.mBlock.nItem) {
H
Hongze Cheng 已提交
1345
          tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pDataBlk, tGetDataBlk);
H
Hongze Cheng 已提交
1346
        } else {
H
Hongze Cheng 已提交
1347
          pDataBlk = NULL;
H
Hongze Cheng 已提交
1348 1349 1350
        }
        pRowInfo = tsdbGetCommitRow(pCommitter);
        if (pRowInfo && (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid)) pRowInfo = NULL;
H
Hongze Cheng 已提交
1351 1352 1353
      }
    }

H
Hongze Cheng 已提交
1354 1355
    while (pDataBlk) {
      code = tMapDataPutItem(&pCommitter->dWriter.mBlock, pDataBlk, tPutDataBlk);
H
Hongze Cheng 已提交
1356
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1357 1358 1359

      iBlock++;
      if (iBlock < pCommitter->dReader.mBlock.nItem) {
H
Hongze Cheng 已提交
1360
        tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pDataBlk, tGetDataBlk);
H
Hongze Cheng 已提交
1361
      } else {
H
Hongze Cheng 已提交
1362
        pDataBlk = NULL;
H
Hongze Cheng 已提交
1363 1364
      }
    }
H
Hongze Cheng 已提交
1365 1366

    code = tsdbCommitterNextTableData(pCommitter);
H
Hongze Cheng 已提交
1367
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1368 1369 1370
  }

_exit:
H
Hongze Cheng 已提交
1371
  if (code) {
S
Shengliang Guan 已提交
1372
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
1373
              tstrerror(code));
H
Hongze Cheng 已提交
1374
  }
H
Hongze Cheng 已提交
1375 1376 1377
  return code;
}

H
Hongze Cheng 已提交
1378
static int32_t tsdbInitSttBlockBuilderIfNeed(SCommitter *pCommitter, TABLEID id) {
H
Hongze Cheng 已提交
1379
  int32_t code = 0;
H
Hongze Cheng 已提交
1380
  int32_t lino = 0;
H
Hongze Cheng 已提交
1381

H
compare  
Hongze Cheng 已提交
1382
#if USE_STREAM_COMPRESSION
H
Hongze Cheng 已提交
1383 1384 1385
  SDiskDataBuilder *pBuilder = pCommitter->dWriter.pBuilder;
  if (pBuilder->suid || pBuilder->uid) {
    if (!TABLE_SAME_SCHEMA(pBuilder->suid, pBuilder->uid, id.suid, id.uid)) {
H
Hongze Cheng 已提交
1386
      code = tsdbCommitSttBlk(pCommitter->dWriter.pWriter, pBuilder, pCommitter->dWriter.aSttBlk);
H
Hongze Cheng 已提交
1387
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1388 1389

      tDiskDataBuilderClear(pBuilder);
H
Hongze Cheng 已提交
1390 1391 1392
    }
  }

H
Hongze Cheng 已提交
1393
  if (!pBuilder->suid && !pBuilder->uid) {
H
Hongze Cheng 已提交
1394 1395
    ASSERT(pCommitter->skmTable.suid == id.suid);
    ASSERT(pCommitter->skmTable.uid == id.uid);
H
Hongze Cheng 已提交
1396
    code = tDiskDataBuilderInit(pBuilder, pCommitter->skmTable.pTSchema, &id, pCommitter->cmprAlg, 0);
H
Hongze Cheng 已提交
1397
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1398
  }
H
compare  
Hongze Cheng 已提交
1399 1400 1401 1402 1403 1404 1405 1406
#else
  SBlockData *pBData = &pCommitter->dWriter.bDatal;
  if (pBData->suid || pBData->uid) {
    if (!TABLE_SAME_SCHEMA(pBData->suid, pBData->uid, id.suid, id.uid)) {
      code = tsdbWriteSttBlock(pCommitter->dWriter.pWriter, pBData, pCommitter->dWriter.aSttBlk, pCommitter->cmprAlg);
      TSDB_CHECK_CODE(code, lino, _exit);

      tBlockDataReset(pBData);
H
Hongze Cheng 已提交
1407 1408 1409
    }
  }

H
compare  
Hongze Cheng 已提交
1410
  if (!pBData->suid && !pBData->uid) {
H
Hongze Cheng 已提交
1411 1412
    ASSERT(pCommitter->skmTable.suid == id.suid);
    ASSERT(pCommitter->skmTable.uid == id.uid);
H
compare  
Hongze Cheng 已提交
1413 1414 1415
    TABLEID tid = {.suid = id.suid, .uid = id.suid ? 0 : id.uid};
    code = tBlockDataInit(pBData, &tid, pCommitter->skmTable.pTSchema, NULL, 0);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1416
  }
H
compare  
Hongze Cheng 已提交
1417
#endif
H
Hongze Cheng 已提交
1418

H
Hongze Cheng 已提交
1419
_exit:
H
Hongze Cheng 已提交
1420
  if (code) {
S
Shengliang Guan 已提交
1421
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
1422
              tstrerror(code));
H
Hongze Cheng 已提交
1423
  }
H
Hongze Cheng 已提交
1424 1425 1426 1427 1428
  return code;
}

static int32_t tsdbAppendLastBlock(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1429
  int32_t lino = 0;
H
Hongze Cheng 已提交
1430 1431

  SBlockData *pBData = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
1432
  TABLEID     id = {.suid = pBData->suid, .uid = pBData->uid};
H
Hongze Cheng 已提交
1433

H
Hongze Cheng 已提交
1434
  code = tsdbInitSttBlockBuilderIfNeed(pCommitter, id);
H
Hongze Cheng 已提交
1435
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1436

H
Hongze Cheng 已提交
1437 1438 1439
  for (int32_t iRow = 0; iRow < pBData->nRow; iRow++) {
    TSDBROW row = tsdbRowFromBlockData(pBData, iRow);

H
compare  
Hongze Cheng 已提交
1440
#if USE_STREAM_COMPRESSION
H
Hongze Cheng 已提交
1441
    code = tDiskDataAddRow(pCommitter->dWriter.pBuilder, &row, NULL, &id);
H
Hongze Cheng 已提交
1442
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1443

H
Hongze Cheng 已提交
1444
    if (pCommitter->dWriter.pBuilder->nRow >= pCommitter->maxRow) {
H
Hongze Cheng 已提交
1445
      code = tsdbCommitSttBlk(pCommitter->dWriter.pWriter, pCommitter->dWriter.pBuilder, pCommitter->dWriter.aSttBlk);
H
Hongze Cheng 已提交
1446
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1447 1448 1449

      code = tsdbInitSttBlockBuilderIfNeed(pCommitter, id);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1450
    }
H
compare  
Hongze Cheng 已提交
1451 1452 1453 1454 1455 1456 1457 1458
#else
    code = tBlockDataAppendRow(&pCommitter->dWriter.bDatal, &row, NULL, id.uid);
    TSDB_CHECK_CODE(code, lino, _exit);

    if (pCommitter->dWriter.bDatal.nRow >= pCommitter->maxRow) {
      code = tsdbWriteSttBlock(pCommitter->dWriter.pWriter, &pCommitter->dWriter.bDatal, pCommitter->dWriter.aSttBlk,
                               pCommitter->cmprAlg);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1459
    }
H
compare  
Hongze Cheng 已提交
1460
#endif
H
Hongze Cheng 已提交
1461 1462
  }

H
Hongze Cheng 已提交
1463 1464
_exit:
  if (code) {
S
Shengliang Guan 已提交
1465
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
1466
              tstrerror(code));
H
Hongze Cheng 已提交
1467
  }
H
Hongze Cheng 已提交
1468 1469 1470
  return code;
}

H
Hongze Cheng 已提交
1471
static int32_t tsdbCommitTableData(SCommitter *pCommitter, TABLEID id) {
H
Hongze Cheng 已提交
1472
  int32_t code = 0;
H
Hongze Cheng 已提交
1473
  int32_t lino = 0;
H
Hongze Cheng 已提交
1474

H
Hongze Cheng 已提交
1475
  SRowInfo *pRowInfo = tsdbGetCommitRow(pCommitter);
H
Hongze Cheng 已提交
1476 1477 1478 1479
  if (pRowInfo && (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid)) {
    pRowInfo = NULL;
  }

H
Hongze Cheng 已提交
1480
  if (pRowInfo == NULL) goto _exit;
H
Hongze Cheng 已提交
1481

H
Hongze Cheng 已提交
1482
  if (pCommitter->toLastOnly) {
H
Hongze Cheng 已提交
1483 1484
    code = tsdbInitSttBlockBuilderIfNeed(pCommitter, id);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1485

H
Hongze Cheng 已提交
1486 1487 1488 1489 1490 1491 1492 1493
    while (pRowInfo) {
      STSchema *pTSchema = NULL;
      if (pRowInfo->row.type == 0) {
        code = tsdbCommitterUpdateRowSchema(pCommitter, id.suid, id.uid, TSDBROW_SVERSION(&pRowInfo->row));
        TSDB_CHECK_CODE(code, lino, _exit);
        pTSchema = pCommitter->skmRow.pTSchema;
      }

H
compare  
Hongze Cheng 已提交
1494
#if USE_STREAM_COMPRESSION
H
Hongze Cheng 已提交
1495
      code = tDiskDataAddRow(pCommitter->dWriter.pBuilder, &pRowInfo->row, pTSchema, &id);
H
compare  
Hongze Cheng 已提交
1496 1497 1498
#else
      code = tBlockDataAppendRow(&pCommitter->dWriter.bDatal, &pRowInfo->row, pTSchema, id.uid);
#endif
H
Hongze Cheng 已提交
1499
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1500

H
Hongze Cheng 已提交
1501 1502
      code = tsdbNextCommitRow(pCommitter);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1503

H
Hongze Cheng 已提交
1504 1505 1506 1507
      pRowInfo = tsdbGetCommitRow(pCommitter);
      if (pRowInfo && (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid)) {
        pRowInfo = NULL;
      }
H
Hongze Cheng 已提交
1508

H
compare  
Hongze Cheng 已提交
1509
#if USE_STREAM_COMPRESSION
H
Hongze Cheng 已提交
1510
      if (pCommitter->dWriter.pBuilder->nRow >= pCommitter->maxRow) {
H
Hongze Cheng 已提交
1511
        code = tsdbCommitSttBlk(pCommitter->dWriter.pWriter, pCommitter->dWriter.pBuilder, pCommitter->dWriter.aSttBlk);
H
Hongze Cheng 已提交
1512
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1513 1514 1515

        code = tsdbInitSttBlockBuilderIfNeed(pCommitter, id);
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1516
      }
H
compare  
Hongze Cheng 已提交
1517
#else
1518
      if (pCommitter->dWriter.bDatal.nRow >= pCommitter->maxRow) {
H
compare  
Hongze Cheng 已提交
1519 1520 1521 1522 1523
        code = tsdbWriteSttBlock(pCommitter->dWriter.pWriter, &pCommitter->dWriter.bDatal, pCommitter->dWriter.aSttBlk,
                                 pCommitter->cmprAlg);
        TSDB_CHECK_CODE(code, lino, _exit);
      }
#endif
H
Hongze Cheng 已提交
1524 1525
    }
  } else {
H
Hongze Cheng 已提交
1526
    SBlockData *pBData = &pCommitter->dWriter.bData;
H
Hongze Cheng 已提交
1527
    ASSERT(pBData->nRow == 0);
H
Hongze Cheng 已提交
1528

H
Hongze Cheng 已提交
1529 1530 1531 1532
    while (pRowInfo) {
      STSchema *pTSchema = NULL;
      if (pRowInfo->row.type == 0) {
        code = tsdbCommitterUpdateRowSchema(pCommitter, id.suid, id.uid, TSDBROW_SVERSION(&pRowInfo->row));
H
Hongze Cheng 已提交
1533
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1534 1535
        pTSchema = pCommitter->skmRow.pTSchema;
      }
H
Hongze Cheng 已提交
1536

H
Hongze Cheng 已提交
1537 1538
      code = tBlockDataAppendRow(pBData, &pRowInfo->row, pTSchema, id.uid);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1539

H
Hongze Cheng 已提交
1540 1541
      code = tsdbNextCommitRow(pCommitter);
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1542

H
Hongze Cheng 已提交
1543 1544 1545 1546
      pRowInfo = tsdbGetCommitRow(pCommitter);
      if (pRowInfo && (pRowInfo->suid != id.suid || pRowInfo->uid != id.uid)) {
        pRowInfo = NULL;
      }
H
Hongze Cheng 已提交
1547

H
Hongze Cheng 已提交
1548
      if (pBData->nRow >= pCommitter->maxRow) {
H
Hongze Cheng 已提交
1549 1550
        code =
            tsdbWriteDataBlock(pCommitter->dWriter.pWriter, pBData, &pCommitter->dWriter.mBlock, pCommitter->cmprAlg);
H
Hongze Cheng 已提交
1551
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1552
      }
H
Hongze Cheng 已提交
1553 1554
    }

H
Hongze Cheng 已提交
1555 1556 1557 1558 1559 1560 1561 1562 1563
    if (pBData->nRow) {
      if (pBData->nRow > pCommitter->minRow) {
        code =
            tsdbWriteDataBlock(pCommitter->dWriter.pWriter, pBData, &pCommitter->dWriter.mBlock, pCommitter->cmprAlg);
        TSDB_CHECK_CODE(code, lino, _exit);
      } else {
        code = tsdbAppendLastBlock(pCommitter);
        TSDB_CHECK_CODE(code, lino, _exit);
      }
H
Hongze Cheng 已提交
1564 1565 1566
    }
  }

H
Hongze Cheng 已提交
1567
_exit:
H
Hongze Cheng 已提交
1568
  if (code) {
S
Shengliang Guan 已提交
1569
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
1570
              tstrerror(code));
H
Hongze Cheng 已提交
1571
  }
H
Hongze Cheng 已提交
1572 1573 1574
  return code;
}

H
Hongze Cheng 已提交
1575 1576
static int32_t tsdbCommitFileDataImpl(SCommitter *pCommitter) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1577
  int32_t lino = 0;
H
Hongze Cheng 已提交
1578

H
Hongze Cheng 已提交
1579
  SRowInfo *pRowInfo;
H
Hongze Cheng 已提交
1580
  TABLEID   id = {0};
H
Hongze Cheng 已提交
1581
  while ((pRowInfo = tsdbGetCommitRow(pCommitter)) != NULL) {
H
Hongze Cheng 已提交
1582 1583 1584
    ASSERT(pRowInfo->suid != id.suid || pRowInfo->uid != id.uid);
    id.suid = pRowInfo->suid;
    id.uid = pRowInfo->uid;
H
Hongze Cheng 已提交
1585

H
Hongze Cheng 已提交
1586
    code = tsdbMoveCommitData(pCommitter, id);
H
Hongze Cheng 已提交
1587
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1588 1589

    // start
H
Hongze Cheng 已提交
1590
    tMapDataReset(&pCommitter->dWriter.mBlock);
H
Hongze Cheng 已提交
1591 1592

    // impl
H
Hongze Cheng 已提交
1593
    code = tsdbUpdateTableSchema(pCommitter->pTsdb->pVnode->pMeta, id.suid, id.uid, &pCommitter->skmTable);
H
Hongze Cheng 已提交
1594
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1595
    code = tBlockDataInit(&pCommitter->dReader.bData, &id, pCommitter->skmTable.pTSchema, NULL, 0);
H
Hongze Cheng 已提交
1596
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1597
    code = tBlockDataInit(&pCommitter->dWriter.bData, &id, pCommitter->skmTable.pTSchema, NULL, 0);
H
Hongze Cheng 已提交
1598
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1599

H
Hongze Cheng 已提交
1600 1601
    /* merge with data in .data file */
    code = tsdbMergeTableData(pCommitter, id);
H
Hongze Cheng 已提交
1602
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1603

H
Hongze Cheng 已提交
1604
    /* handle remain table data */
H
Hongze Cheng 已提交
1605
    code = tsdbCommitTableData(pCommitter, id);
H
Hongze Cheng 已提交
1606
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1607

H
Hongze Cheng 已提交
1608
    // end
H
Hongze Cheng 已提交
1609 1610
    if (pCommitter->dWriter.mBlock.nItem > 0) {
      SBlockIdx blockIdx = {.suid = id.suid, .uid = id.uid};
H
Hongze Cheng 已提交
1611
      code = tsdbWriteDataBlk(pCommitter->dWriter.pWriter, &pCommitter->dWriter.mBlock, &blockIdx);
H
Hongze Cheng 已提交
1612
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1613 1614 1615

      if (taosArrayPush(pCommitter->dWriter.aBlockIdx, &blockIdx) == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1616
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1617 1618
      }
    }
H
Hongze Cheng 已提交
1619 1620
  }

H
Hongze Cheng 已提交
1621 1622 1623
  id.suid = INT64_MAX;
  id.uid = INT64_MAX;
  code = tsdbMoveCommitData(pCommitter, id);
H
Hongze Cheng 已提交
1624
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1625

H
compare  
Hongze Cheng 已提交
1626
#if USE_STREAM_COMPRESSION
H
Hongze Cheng 已提交
1627
  code = tsdbCommitSttBlk(pCommitter->dWriter.pWriter, pCommitter->dWriter.pBuilder, pCommitter->dWriter.aSttBlk);
H
compare  
Hongze Cheng 已提交
1628
#else
H
Hongze Cheng 已提交
1629 1630
  code = tsdbWriteSttBlock(pCommitter->dWriter.pWriter, &pCommitter->dWriter.bDatal, pCommitter->dWriter.aSttBlk,
                           pCommitter->cmprAlg);
H
compare  
Hongze Cheng 已提交
1631
#endif
H
Hongze Cheng 已提交
1632
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
1633

H
Hongze Cheng 已提交
1634 1635
_exit:
  if (code) {
S
Shengliang Guan 已提交
1636
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pCommitter->pTsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
1637
              tstrerror(code));
H
Hongze Cheng 已提交
1638
  }
H
Hongze Cheng 已提交
1639 1640
  return code;
}
H
Hongze Cheng 已提交
1641 1642 1643 1644 1645

int32_t tsdbFinishCommit(STsdb *pTsdb) {
  int32_t    code = 0;
  int32_t    lino = 0;
  SMemTable *pMemTable = pTsdb->imem;
H
Hongze Cheng 已提交
1646

H
Hongze Cheng 已提交
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
  // lock
  taosThreadRwlockWrlock(&pTsdb->rwLock);

  code = tsdbFSCommit(pTsdb);
  if (code) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  pTsdb->imem = NULL;

  // unlock
  taosThreadRwlockUnlock(&pTsdb->rwLock);
H
Hongze Cheng 已提交
1660 1661 1662
  if (pMemTable) {
    tsdbUnrefMemTable(pMemTable);
  }
H
Hongze Cheng 已提交
1663 1664 1665

_exit:
  if (code) {
S
Shengliang Guan 已提交
1666
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1667
  } else {
S
Shengliang Guan 已提交
1668
    tsdbInfo("vgId:%d, tsdb finish commit", TD_VID(pTsdb->pVnode));
H
Hongze Cheng 已提交
1669
  }
H
Hongze Cheng 已提交
1670
  return code;
H
Hongze Cheng 已提交
1671
}
H
Hongze Cheng 已提交
1672

H
Hongze Cheng 已提交
1673 1674 1675 1676 1677 1678 1679 1680 1681
int32_t tsdbRollbackCommit(STsdb *pTsdb) {
  int32_t code = 0;
  int32_t lino = 0;

  code = tsdbFSRollback(pTsdb);
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
S
Shengliang Guan 已提交
1682
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
1683
  } else {
S
Shengliang Guan 已提交
1684
    tsdbInfo("vgId:%d, tsdb rollback commit", TD_VID(pTsdb->pVnode));
H
Hongze Cheng 已提交
1685
  }
H
Hongze Cheng 已提交
1686 1687
  return code;
}