tsdbMerge.c 10.4 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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/>.
 */

#include "tsdb.h"

H
Hongze Cheng 已提交
18 19 20 21 22 23 24
typedef struct {
  int64_t suid;
  int64_t uid;
  TSDBROW row;
} SRowInfo;

typedef struct {
H
Hongze Cheng 已提交
25
  SRowInfo   rowInfo;
H
Hongze Cheng 已提交
26 27 28 29 30 31
  SArray    *aBlockL;  // SArray<SBlockL>
  int32_t    iBlockL;
  SBlockData bData;
  int32_t    iRow;
} SLDataIter;

H
Hongze Cheng 已提交
32
typedef struct {
H
Hongze Cheng 已提交
33 34
  SRBTreeNode *pNode;
  SRBTree      rbt;
H
Hongze Cheng 已提交
35 36
} SDataMerger;

H
Hongze Cheng 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
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 已提交
56 57 58 59 60 61 62 63 64 65 66
static void tDataMergerInit(SDataMerger *pMerger, SArray *aNodeP) {
  pMerger->pNode = NULL;
  pMerger->rbt = tRBTreeCreate(tRowInfoCmprFn);
  for (int32_t iNode = 0; iNode < taosArrayGetSize(aNodeP); iNode++) {
    SRBTreeNode *pNode = (SRBTreeNode *)taosArrayGetP(aNodeP, iNode);

    pNode = tRBTreePut(&pMerger->rbt, pNode);
    ASSERT(pNode);
  }
}

H
Hongze Cheng 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80
static int32_t tDataMergeNext(SDataMerger *pMerger, SRowInfo **ppInfo) {
  int32_t code = 0;

  if (pMerger->pNode) {
    // next current iter
    SLDataIter *pIter = (SLDataIter *)pMerger->pNode->payload;

    pIter->iRow++;
    if (pIter->iRow < pIter->bData.nRow) {
      pIter->rowInfo.uid = pIter->bData.uid ? pIter->bData.uid : pIter->bData.aUid[pIter->iRow];
      pIter->rowInfo.row = tsdbRowFromBlockData(&pIter->bData, pIter->iRow);
    } else {
      pIter->iBlockL++;
      if (pIter->iBlockL < taosArrayGetSize(pIter->aBlockL)) {
H
Hongze Cheng 已提交
81
        // code = tsdbReadLastBlock(NULL, (SBlockL *)taosArrayGet(pIter->aBlockL, pIter->iBlockL), &pIter->bData);
H
Hongze Cheng 已提交
82
        if (code) goto _exit;
H
Hongze Cheng 已提交
83

H
Hongze Cheng 已提交
84 85 86 87 88 89 90 91 92
        pIter->iRow = 0;
        pIter->rowInfo.suid = pIter->bData.suid;
        pIter->rowInfo.uid = pIter->bData.uid ? pIter->bData.uid : pIter->bData.aUid[0];
        pIter->rowInfo.row = tsdbRowFromBlockData(&pIter->bData, 0);
      } else {
        pMerger->pNode = NULL;
      }
    }

H
Hongze Cheng 已提交
93 94
    if (pMerger->pNode && pMerger->rbt.min) {
      int32_t c = tRowInfoCmprFn(pMerger->pNode->payload, pMerger->rbt.min->payload);
H
Hongze Cheng 已提交
95 96 97 98 99 100 101 102 103 104 105
      if (c > 0) {
        pMerger->pNode = tRBTreePut(&pMerger->rbt, pMerger->pNode);
        ASSERT(pMerger->pNode);
        pMerger->pNode = NULL;
      } else {
        ASSERT(c);
      }
    }
  }

  if (pMerger->pNode == NULL) {
H
Hongze Cheng 已提交
106
    pMerger->pNode = pMerger->rbt.min;
H
Hongze Cheng 已提交
107 108 109 110 111 112 113 114 115 116
    if (pMerger->pNode) {
      tRBTreeDrop(&pMerger->rbt, pMerger->pNode);
    }
  }

  if (pMerger->pNode) {
    *ppInfo = &((SLDataIter *)pMerger->pNode->payload)[0].rowInfo;
  } else {
    *ppInfo = NULL;
  }
H
Hongze Cheng 已提交
117

H
Hongze Cheng 已提交
118 119
_exit:
  return code;
H
Hongze Cheng 已提交
120 121 122
}

// ================================================================================
H
Hongze Cheng 已提交
123 124
typedef struct {
  STsdb  *pTsdb;
H
Hongze Cheng 已提交
125
  int8_t  maxLast;
H
Hongze Cheng 已提交
126
  int64_t commitID;
H
Hongze Cheng 已提交
127
  STsdbFS fs;
H
Hongze Cheng 已提交
128 129
  struct {
    SDataFReader *pReader;
H
Hongze Cheng 已提交
130
    SArray       *aBlockIdx;
H
Hongze Cheng 已提交
131
    SLDataIter    aLDataiter[TSDB_MAX_LAST_FILE];
H
Hongze Cheng 已提交
132
    SDataMerger   merger;
H
Hongze Cheng 已提交
133 134 135
  } dReader;
  struct {
    SDataFWriter *pWriter;
H
Hongze Cheng 已提交
136 137
    SArray       *aBlockIdx;
    SArray       *aBlockL;
H
Hongze Cheng 已提交
138 139
    SBlockData    bData;
    SBlockData    bDatal;
H
Hongze Cheng 已提交
140
  } dWriter;
H
Hongze Cheng 已提交
141 142
} STsdbMerger;

H
Hongze Cheng 已提交
143 144 145
extern int32_t tsdbReadLastBlockEx(SDataFReader *pReader, int32_t iLast, SBlockL *pBlockL,
                                   SBlockData *pBlockData);  // todo

H
Hongze Cheng 已提交
146 147 148 149 150 151 152 153 154 155 156
static int32_t tsdbMergeFileDataStart(STsdbMerger *pMerger, SDFileSet *pSet) {
  int32_t code = 0;
  STsdb  *pTsdb = pMerger->pTsdb;

  // reader
  code = tsdbDataFReaderOpen(&pMerger->dReader.pReader, pTsdb, pSet);
  if (code) goto _err;

  code = tsdbReadBlockIdx(pMerger->dReader.pReader, pMerger->dReader.aBlockIdx);
  if (code) goto _err;

H
Hongze Cheng 已提交
157 158
  pMerger->dReader.merger.pNode = NULL;
  pMerger->dReader.merger.rbt = tRBTreeCreate(tRowInfoCmprFn);
H
Hongze Cheng 已提交
159
  for (int8_t iLast = 0; iLast < pSet->nLastF; iLast++) {
H
Hongze Cheng 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173
    SRBTreeNode *pNode = (SRBTreeNode *)taosMemoryCalloc(1, sizeof(*pNode) + sizeof(SLDataIter));
    if (pNode == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }

    SLDataIter *pIter = (SLDataIter *)pNode->payload;

    pIter->aBlockL = taosArrayInit(0, sizeof(SBlockL));
    if (pIter->aBlockL == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
    code = tBlockDataCreate(&pIter->bData);
H
Hongze Cheng 已提交
174
    if (code) goto _err;
H
Hongze Cheng 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

    code = tsdbReadBlockL(pMerger->dReader.pReader, iLast, pIter->aBlockL);
    if (code) goto _err;

    if (taosArrayGetSize(pIter->aBlockL) == 0) continue;
    pIter->iBlockL = 0;

    SBlockL *pBlockL = (SBlockL *)taosArrayGet(pIter->aBlockL, 0);
    code = tsdbReadLastBlockEx(pMerger->dReader.pReader, iLast, pBlockL, &pIter->bData);
    if (code) goto _err;

    pIter->iRow = 0;
    pIter->rowInfo.suid = pIter->bData.suid;
    pIter->rowInfo.uid = pIter->bData.uid ? pIter->bData.uid : pIter->bData.aUid[0];
    pIter->rowInfo.row = tsdbRowFromBlockData(&pIter->bData, 0);

    pNode = tRBTreePut(&pMerger->dReader.merger.rbt, pNode);
    ASSERT(pNode);
H
Hongze Cheng 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
  }

  // writer
  SHeadFile fHead = {.commitID = pMerger->commitID};
  SDataFile fData = *pSet->pDataF;
  SSmaFile  fSma = *pSet->pSmaF;
  SLastFile fLast = {.commitID = pMerger->commitID};
  SDFileSet wSet = {.diskId = pSet->diskId,
                    .fid = pSet->fid,
                    .nLastF = 1,
                    .pHeadF = &fHead,
                    .pDataF = &fData,
                    .pSmaF = &fSma,
                    .aLastF[0] = &fLast};
  code = tsdbDataFWriterOpen(&pMerger->dWriter.pWriter, pTsdb, &wSet);
  if (code) goto _err;

  return code;

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

static int32_t tsdbMergeFileDataEnd(STsdbMerger *pMerger) {
  int32_t code = 0;
  STsdb  *pTsdb = pMerger->pTsdb;

  // write aBlockIdx
  code = tsdbWriteBlockIdx(pMerger->dWriter.pWriter, pMerger->dWriter.aBlockIdx);
  if (code) goto _err;

  // write aBlockL
  code = tsdbWriteBlockL(pMerger->dWriter.pWriter, pMerger->dWriter.aBlockL);
  if (code) goto _err;

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

  // upsert SDFileSet
  code = tsdbFSUpsertFSet(&pMerger->fs, &pMerger->dWriter.pWriter->wSet);
  if (code) goto _err;

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

  if (pMerger->dReader.pReader) {
    code = tsdbDataFReaderClose(&pMerger->dReader.pReader);
    if (code) goto _err;
  }

  return code;

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

static int32_t tsdbMergeFileData(STsdbMerger *pMerger, SDFileSet *pSet) {
  int32_t code = 0;
  STsdb  *pTsdb = pMerger->pTsdb;

  // start
  code = tsdbMergeFileDataStart(pMerger, pSet);
  if (code) goto _err;

  // impl
H
Hongze Cheng 已提交
262 263
  SRowInfo  rInfo = {.suid = INT64_MIN};
  SRowInfo *pInfo;
H
Hongze Cheng 已提交
264
  int64_t   nRow = 0;
H
Hongze Cheng 已提交
265
  while (true) {
H
Hongze Cheng 已提交
266 267 268 269
    code = tDataMergeNext(&pMerger->dReader.merger, &pInfo);
    if (code) goto _err;

    if (pInfo == NULL) break;
H
Hongze Cheng 已提交
270
    nRow++;
H
Hongze Cheng 已提交
271 272 273

    ASSERT(tRowInfoCmprFn(pInfo, &rInfo) > 0);
    rInfo = *pInfo;
H
Hongze Cheng 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
  }

  // end
  code = tsdbMergeFileDataEnd(pMerger);
  if (code) goto _err;

  return code;

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

static int32_t tsdbStartMerge(STsdbMerger *pMerger, STsdb *pTsdb) {
  int32_t code = 0;
H
Hongze Cheng 已提交
289 290 291

  pMerger->pTsdb = pTsdb;
  pMerger->maxLast = TSDB_DEFAULT_LAST_FILE;
H
Hongze Cheng 已提交
292
  pMerger->commitID = ++pTsdb->pVnode->state.commitID;
H
Hongze Cheng 已提交
293
  code = tsdbFSCopy(pTsdb, &pMerger->fs);
H
Hongze Cheng 已提交
294 295 296 297 298 299 300 301
  if (code) goto _exit;

  // reader
  pMerger->dReader.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pMerger->dReader.aBlockIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
H
Hongze Cheng 已提交
302 303 304 305 306 307 308
  // for (int8_t iLast = 0; iLast < TSDB_MAX_LAST_FILE; iLast++) {
  //   pMerger->dReader.aBlockL[iLast] = taosArrayInit(0, sizeof(SBlockL));
  //   if (pMerger->dReader.aBlockL[iLast] == NULL) {
  //     code = TSDB_CODE_OUT_OF_MEMORY;
  //     goto _exit;
  //   }
  // }
H
Hongze Cheng 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

  // writer
  pMerger->dWriter.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
  if (pMerger->dWriter.aBlockIdx == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
  pMerger->dWriter.aBlockL = taosArrayInit(0, sizeof(SBlockL));
  if (pMerger->dWriter.aBlockL == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

_exit:
  return code;
}

static int32_t tsdbEndMerge(STsdbMerger *pMerger) {
  int32_t code = 0;
  STsdb  *pTsdb = pMerger->pTsdb;

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

H
Hongze Cheng 已提交
333 334 335 336 337 338 339 340 341 342 343
  taosThreadRwlockWrlock(&pTsdb->rwLock);
  code = tsdbFSCommit2(pTsdb, &pMerger->fs);
  if (code) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
    goto _err;
  }
  taosThreadRwlockUnlock(&pTsdb->rwLock);

  // writer
  taosArrayDestroy(pMerger->dWriter.aBlockL);
  taosArrayDestroy(pMerger->dWriter.aBlockIdx);
H
Hongze Cheng 已提交
344

H
Hongze Cheng 已提交
345
  // reader
H
Hongze Cheng 已提交
346 347 348
  // for (int8_t iLast = 0; iLast < TSDB_MAX_LAST_FILE; iLast++) {
  //   taosArrayDestroy(pMerger->dReader.aBlockL[iLast]);
  // }
H
Hongze Cheng 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
  taosArrayDestroy(pMerger->dReader.aBlockIdx);
  tsdbFSDestroy(&pMerger->fs);

  return code;

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

int32_t tsdbMerge(STsdb *pTsdb) {
  int32_t     code = 0;
  STsdbMerger merger = {0};

  code = tsdbStartMerge(&merger, pTsdb);
  if (code) goto _err;

  for (int32_t iSet = 0; iSet < taosArrayGetSize(merger.fs.aDFileSet); iSet++) {
    SDFileSet *pSet = (SDFileSet *)taosArrayGet(merger.fs.aDFileSet, iSet);
    if (pSet->nLastF < merger.maxLast) continue;

    code = tsdbMergeFileData(&merger, pSet);
    if (code) goto _err;
  }

  code = tsdbEndMerge(&merger);
  if (code) goto _err;
H
Hongze Cheng 已提交
376 377 378 379 380

  return code;

_err:
  tsdbError("vgId:%d tsdb merge failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
381 382
  return code;
}