tsdbMemTable2.c 12.5 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
struct SMemSkipListNode {
  int8_t            level;
  SMemSkipListNode *forwards[0];
};

H
Hongze Cheng 已提交
23 24 25 26
typedef struct {
  tb_uid_t  uid;
  STSchema *pTSchema;
} SSkmInfo;
H
Hongze Cheng 已提交
27

H
Hongze Cheng 已提交
28 29
#define SL_MAX_LEVEL 5

H
Hongze Cheng 已提交
30 31 32 33 34
#define SL_NODE_SIZE(l)        (sizeof(SMemSkipListNode) + sizeof(SMemSkipListNode *) * (l)*2)
#define SL_NODE_FORWARD(n, l)  ((n)->forwards[l])
#define SL_NODE_BACKWARD(n, l) ((n)->forwards[(n)->level + (l)])
#define SL_NODE_DATA(n)        (&SL_NODE_BACKWARD(n, (n)->level))

H
more  
Hongze Cheng 已提交
35 36 37
#define SL_MOVE_BACKWARD 0x1
#define SL_MOVE_FROM_POS 0x2

H
Hongze Cheng 已提交
38 39
static int32_t tsdbGetOrCreateMemData(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid, SMemData **ppMemData);
static int     memDataPCmprFn(const void *p1, const void *p2);
H
Hongze Cheng 已提交
40 41 42
static int32_t tPutTSDBRow(uint8_t *p, TSDBROW *pRow);
static int32_t tGetTSDBRow(uint8_t *p, TSDBROW *pRow);
static int8_t  tsdbMemSkipListRandLevel(SMemSkipList *pSl);
H
Hongze Cheng 已提交
43 44
static int32_t tsdbInsertTableDataImpl(SMemTable *pMemTable, SMemData *pMemData, int64_t version,
                                       SVSubmitBlk *pSubmitBlk);
H
more  
Hongze Cheng 已提交
45

H
more  
Hongze Cheng 已提交
46 47 48 49 50 51 52 53 54 55 56 57
// SMemTable ==============================================
int32_t tsdbMemTableCreate2(STsdb *pTsdb, SMemTable **ppMemTable) {
  int32_t    code = 0;
  SMemTable *pMemTable = NULL;

  pMemTable = (SMemTable *)taosMemoryCalloc(1, sizeof(*pMemTable));
  if (pMemTable == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pMemTable->pTsdb = pTsdb;
  pMemTable->nRef = 1;
H
Hongze Cheng 已提交
58 59
  pMemTable->minKey = (TSDBKEY){.version = INT64_MAX, .ts = TSKEY_MAX};
  pMemTable->maxKey = (TSDBKEY){.version = -1, .ts = TSKEY_MIN};
H
more  
Hongze Cheng 已提交
60
  pMemTable->nRows = 0;
H
Hongze Cheng 已提交
61 62
  pMemTable->aMemData = taosArrayInit(512, sizeof(SMemData *));
  if (pMemTable->aMemData == NULL) {
H
more  
Hongze Cheng 已提交
63 64 65 66 67 68 69 70 71 72
    taosMemoryFree(pMemTable);
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  *ppMemTable = pMemTable;
  return code;

_err:
  *ppMemTable = NULL;
H
Hongze Cheng 已提交
73 74 75
  return code;
}

H
more  
Hongze Cheng 已提交
76
void tsdbMemTableDestroy2(SMemTable *pMemTable) {
H
Hongze Cheng 已提交
77
  taosArrayDestroyEx(pMemTable->aMemData, NULL /*TODO*/);
H
more  
Hongze Cheng 已提交
78 79 80 81
  taosMemoryFree(pMemTable);
}

int32_t tsdbInsertTableData2(STsdb *pTsdb, int64_t version, SVSubmitBlk *pSubmitBlk) {
H
Hongze Cheng 已提交
82 83 84
  int32_t    code = 0;
  SMemTable *pMemTable = (SMemTable *)pTsdb->mem;  // TODO
  SMemData  *pMemData;
H
Hongze Cheng 已提交
85
  TSDBROW    row = {.version = version};
H
Hongze Cheng 已提交
86 87

  ASSERT(pMemTable);
H
Hongze Cheng 已提交
88
  ASSERT(pSubmitBlk->nData > 0);
H
more  
Hongze Cheng 已提交
89 90

  {
H
Hongze Cheng 已提交
91
    // check if table exists (todo)
H
more  
Hongze Cheng 已提交
92 93
  }

H
Hongze Cheng 已提交
94
  code = tsdbGetOrCreateMemData(pMemTable, pSubmitBlk->suid, pSubmitBlk->uid, &pMemData);
H
more  
Hongze Cheng 已提交
95
  if (code) {
S
Shengliang Guan 已提交
96
    tsdbError("vgId:%d, failed to create/get table data since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
more  
Hongze Cheng 已提交
97 98 99 100
    goto _err;
  }

  // do insert
H
Hongze Cheng 已提交
101 102 103
  code = tsdbInsertTableDataImpl(pMemTable, pMemData, version, pSubmitBlk);
  if (code) {
    goto _err;
H
Hongze Cheng 已提交
104
  }
H
more  
Hongze Cheng 已提交
105 106 107 108 109 110 111 112

  return code;

_err:
  return code;
}

int32_t tsdbDeleteTableData2(STsdb *pTsdb, int64_t version, tb_uid_t suid, tb_uid_t uid, TSKEY sKey, TSKEY eKey) {
H
Hongze Cheng 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  int32_t    code = 0;
  SMemTable *pMemTable = (SMemTable *)pTsdb->mem;  // TODO
  SMemData  *pMemData;
  SVBufPool *pPool = pTsdb->pVnode->inUse;

  ASSERT(pMemTable);

  {
    // check if table exists (todo)
  }

  code = tsdbGetOrCreateMemData(pMemTable, suid, uid, &pMemData);
  if (code) {
    goto _err;
  }

  // do delete
  SDelOp *pDelOp = (SDelOp *)vnodeBufPoolMalloc(pPool, sizeof(*pDelOp));
  if (pDelOp == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pDelOp->version = version;
  pDelOp->sKey = sKey;
  pDelOp->eKey = eKey;
  pDelOp->pNext = NULL;
  if (pMemData->delOpHead == NULL) {
    ASSERT(pMemData->delOpTail == NULL);
    pMemData->delOpHead = pMemData->delOpTail = pDelOp;
  } else {
    pMemData->delOpTail->pNext = pDelOp;
    pMemData->delOpTail = pDelOp;
  }

  {
    // update the state of pMemTable, pMemData, last and lastrow (todo)
  }

S
Shengliang Guan 已提交
151
  tsdbDebug("vgId:%d, delete data from table suid:%" PRId64 " uid:%" PRId64 " sKey:%" PRId64 " eKey:%" PRId64
H
Hongze Cheng 已提交
152 153 154 155 156
            " since %s",
            TD_VID(pTsdb->pVnode), suid, uid, sKey, eKey, tstrerror(code));
  return code;

_err:
S
Shengliang Guan 已提交
157
  tsdbError("vgId:%d, failed to delete data from table suid:%" PRId64 " uid:%" PRId64 " sKey:%" PRId64 " eKey:%" PRId64
H
Hongze Cheng 已提交
158 159
            " since %s",
            TD_VID(pTsdb->pVnode), suid, uid, sKey, eKey, tstrerror(code));
H
more  
Hongze Cheng 已提交
160 161 162
  return code;
}

H
Hongze Cheng 已提交
163
static int32_t tsdbGetOrCreateMemData(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid, SMemData **ppMemData) {
H
more  
Hongze Cheng 已提交
164
  int32_t    code = 0;
H
Hongze Cheng 已提交
165 166
  int32_t    idx = 0;
  SMemData  *pMemDataT = &(SMemData){.suid = suid, .uid = uid};
H
more  
Hongze Cheng 已提交
167
  SMemData  *pMemData = NULL;
H
Hongze Cheng 已提交
168
  SVBufPool *pPool = pMemTable->pTsdb->pVnode->inUse;
H
Hongze Cheng 已提交
169
  int8_t     maxLevel = pMemTable->pTsdb->pVnode->config.tsdbCfg.slLevel;
H
more  
Hongze Cheng 已提交
170

H
Hongze Cheng 已提交
171
  // get
H
Hongze Cheng 已提交
172
  idx = taosArraySearchIdx(pMemTable->aMemData, &pMemDataT, memDataPCmprFn, TD_GE);
H
Hongze Cheng 已提交
173
  if (idx >= 0) {
H
Hongze Cheng 已提交
174
    pMemData = (SMemData *)taosArrayGet(pMemTable->aMemData, idx);
H
Hongze Cheng 已提交
175 176
    if (memDataPCmprFn(&pMemDataT, &pMemData) == 0) goto _exit;
  }
H
more  
Hongze Cheng 已提交
177

H
Hongze Cheng 已提交
178
  // create
H
more  
Hongze Cheng 已提交
179
  pMemData = vnodeBufPoolMalloc(pPool, sizeof(*pMemData) + SL_NODE_SIZE(maxLevel) * 2);
H
more  
Hongze Cheng 已提交
180
  if (pMemData == NULL) {
H
Hongze Cheng 已提交
181 182 183 184 185 186 187 188
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pMemData->suid = suid;
  pMemData->uid = uid;
  pMemData->minKey = (TSDBKEY){.version = INT64_MAX, .ts = TSKEY_MAX};
  pMemData->maxKey = (TSDBKEY){.version = -1, .ts = TSKEY_MIN};
  pMemData->delOpHead = pMemData->delOpTail = NULL;
H
Hongze Cheng 已提交
189 190 191 192 193
  pMemData->sl.seed = taosRand();
  pMemData->sl.size = 0;
  pMemData->sl.maxLevel = maxLevel;
  pMemData->sl.level = 0;
  pMemData->sl.pHead = (SMemSkipListNode *)&pMemData[1];
H
more  
Hongze Cheng 已提交
194 195 196
  pMemData->sl.pTail = (SMemSkipListNode *)POINTER_SHIFT(pMemData->sl.pHead, SL_NODE_SIZE(maxLevel));
  pMemData->sl.pHead->level = maxLevel;
  pMemData->sl.pTail->level = maxLevel;
H
Hongze Cheng 已提交
197 198

  for (int8_t iLevel = 0; iLevel < pMemData->sl.maxLevel; iLevel++) {
H
more  
Hongze Cheng 已提交
199 200 201 202
    SL_NODE_FORWARD(pMemData->sl.pHead, iLevel) = pMemData->sl.pTail;
    SL_NODE_BACKWARD(pMemData->sl.pHead, iLevel) = NULL;
    SL_NODE_BACKWARD(pMemData->sl.pTail, iLevel) = pMemData->sl.pHead;
    SL_NODE_FORWARD(pMemData->sl.pTail, iLevel) = NULL;
H
Hongze Cheng 已提交
203
  }
H
Hongze Cheng 已提交
204 205

  if (idx < 0) idx = 0;
H
Hongze Cheng 已提交
206
  if (taosArrayInsert(pMemTable->aMemData, idx, &pMemData) == NULL) {
H
Hongze Cheng 已提交
207 208
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
H
more  
Hongze Cheng 已提交
209 210
  }

H
Hongze Cheng 已提交
211
_exit:
H
more  
Hongze Cheng 已提交
212 213 214 215 216 217
  *ppMemData = pMemData;
  return code;

_err:
  *ppMemData = NULL;
  return code;
H
Hongze Cheng 已提交
218 219
}

H
Hongze Cheng 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
static int memDataPCmprFn(const void *p1, const void *p2) {
  SMemData *pMemData1 = *(SMemData **)p1;
  SMemData *pMemData2 = *(SMemData **)p2;

  if (pMemData1->suid < pMemData2->suid) {
    return -1;
  } else if (pMemData1->suid > pMemData2->suid) {
    return 1;
  }

  if (pMemData1->uid < pMemData2->uid) {
    return -1;
  } else if (pMemData1->uid > pMemData2->uid) {
    return 1;
  }

  return 0;
}

H
Hongze Cheng 已提交
239 240
static int32_t tPutTSDBRow(uint8_t *p, TSDBROW *pRow) {
  int32_t n = 0;
H
Hongze Cheng 已提交
241

H
Hongze Cheng 已提交
242 243
  n += tPutI64(p ? p + n : p, pRow->version);
  n += tPutTSRow(p ? p + n : p, &pRow->tsRow);
H
Hongze Cheng 已提交
244

H
Hongze Cheng 已提交
245 246
  return n;
}
H
Hongze Cheng 已提交
247

H
Hongze Cheng 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
static int32_t tGetTSDBRow(uint8_t *p, TSDBROW *pRow) {
  int32_t n = 0;

  n += tGetI64(p + n, &pRow->version);
  n += tGetTSRow(p + n, &pRow->tsRow);

  return n;
}

static FORCE_INLINE int8_t tsdbMemSkipListRandLevel(SMemSkipList *pSl) {
  int8_t         level = 1;
  int8_t         tlevel = TMIN(pSl->maxLevel, pSl->level + 1);
  const uint32_t factor = 4;

  while ((taosRandR(&pSl->seed) % factor) == 0 && level < tlevel) {
    level++;
  }

  return level;
}

H
more  
Hongze Cheng 已提交
269
static void memDataMovePosTo(SMemData *pMemData, SMemSkipListNode **pos, TSDBKEY *pKey, int32_t flags) {
H
Hongze Cheng 已提交
270 271
  SMemSkipListNode *px;
  SMemSkipListNode *pn;
H
more  
Hongze Cheng 已提交
272
  TSDBKEY          *pTKey;
H
more  
Hongze Cheng 已提交
273
  int               c;
H
more  
Hongze Cheng 已提交
274 275
  int               backward = flags & SL_MOVE_BACKWARD;
  int               fromPos = flags & SL_MOVE_FROM_POS;
H
more  
Hongze Cheng 已提交
276

H
more  
Hongze Cheng 已提交
277
  if (backward) {
H
Hongze Cheng 已提交
278
    px = pMemData->sl.pTail;
H
more  
Hongze Cheng 已提交
279 280 281 282 283 284 285 286 287

    for (int8_t iLevel = pMemData->sl.maxLevel - 1; iLevel >= pMemData->sl.level; iLevel--) {
      pos[iLevel] = px;
    }

    if (pMemData->sl.level) {
      if (fromPos) px = pos[pMemData->sl.level - 1];

      for (int8_t iLevel = pMemData->sl.level - 1; iLevel >= 0; iLevel--) {
H
Hongze Cheng 已提交
288 289
        pn = SL_NODE_BACKWARD(px, iLevel);
        while (pn != pMemData->sl.pHead) {
H
more  
Hongze Cheng 已提交
290
          pTKey = (TSDBKEY *)SL_NODE_DATA(pn);
H
Hongze Cheng 已提交
291

H
more  
Hongze Cheng 已提交
292
          c = tsdbKeyCmprFn(pTKey, pKey);
H
Hongze Cheng 已提交
293 294 295 296 297 298
          if (c <= 0) {
            break;
          } else {
            px = pn;
            pn = SL_NODE_BACKWARD(px, iLevel);
          }
H
more  
Hongze Cheng 已提交
299
        }
H
more  
Hongze Cheng 已提交
300 301

        pos[iLevel] = px;
H
more  
Hongze Cheng 已提交
302 303
      }
    }
H
more  
Hongze Cheng 已提交
304 305
  } else {
    px = pMemData->sl.pHead;
H
more  
Hongze Cheng 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

    for (int8_t iLevel = pMemData->sl.maxLevel - 1; iLevel >= pMemData->sl.level; iLevel--) {
      pos[iLevel] = px;
    }

    if (pMemData->sl.level) {
      if (fromPos) px = pos[pMemData->sl.level - 1];

      for (int8_t iLevel = pMemData->sl.level - 1; iLevel >= 0; iLevel--) {
        pn = SL_NODE_FORWARD(px, iLevel);
        while (pn != pMemData->sl.pHead) {
          pTKey = (TSDBKEY *)SL_NODE_DATA(pn);

          c = tsdbKeyCmprFn(pTKey, pKey);
          if (c >= 0) {
            break;
          } else {
            px = pn;
            pn = SL_NODE_FORWARD(px, iLevel);
          }
H
more  
Hongze Cheng 已提交
326
        }
H
more  
Hongze Cheng 已提交
327 328

        pos[iLevel] = px;
H
more  
Hongze Cheng 已提交
329 330
      }
    }
H
more  
Hongze Cheng 已提交
331 332 333
  }
}

H
more  
Hongze Cheng 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
static int32_t memDataDoPut(SMemTable *pMemTable, SMemData *pMemData, SMemSkipListNode **pos, TSDBROW *pRow,
                            int8_t forward) {
  int32_t           code = 0;
  int8_t            level;
  SMemSkipListNode *pNode;
  SVBufPool        *pPool = pMemTable->pTsdb->pVnode->inUse;

  // node
  level = tsdbMemSkipListRandLevel(&pMemData->sl);
  pNode = (SMemSkipListNode *)vnodeBufPoolMalloc(pPool, SL_NODE_SIZE(level) + tPutTSDBRow(NULL, pRow));
  if (pNode == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
  pNode->level = level;
  for (int8_t iLevel = 0; iLevel < level; iLevel++) {
    SL_NODE_FORWARD(pNode, iLevel) = NULL;
    SL_NODE_BACKWARD(pNode, iLevel) = NULL;
  }

  tPutTSDBRow((uint8_t *)SL_NODE_DATA(pNode), pRow);

  // put
  for (int8_t iLevel = 0; iLevel < pNode->level; iLevel++) {
H
more  
Hongze Cheng 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    SMemSkipListNode *px = pos[iLevel];

    if (forward) {
      SMemSkipListNode *pNext = SL_NODE_FORWARD(px, iLevel);

      SL_NODE_FORWARD(pNode, iLevel) = pNext;
      SL_NODE_BACKWARD(pNode, iLevel) = px;

      SL_NODE_BACKWARD(pNext, iLevel) = pNode;
      SL_NODE_FORWARD(px, iLevel) = pNode;
    } else {
      SMemSkipListNode *pPrev = SL_NODE_BACKWARD(px, iLevel);

      SL_NODE_FORWARD(pNode, iLevel) = px;
      SL_NODE_BACKWARD(pNode, iLevel) = pPrev;

      SL_NODE_FORWARD(pPrev, iLevel) = pNode;
      SL_NODE_BACKWARD(px, iLevel) = pNode;
    }
H
more  
Hongze Cheng 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
  }

  pMemData->sl.size++;
  if (pMemData->sl.level < pNode->level) {
    pMemData->sl.level = pNode->level;
  }

_exit:
  return code;
}

static int32_t tsdbInsertTableDataImpl(SMemTable *pMemTable, SMemData *pMemData, int64_t version,
                                       SVSubmitBlk *pSubmitBlk) {
  int32_t  code = 0;
  int32_t  n = 0;
  uint8_t *p = pSubmitBlk->pData;
  int32_t  nRow = 0;
  TSDBROW  row = {.version = version};

  SMemSkipListNode *pos[SL_MAX_LEVEL];

  ASSERT(pSubmitBlk->nData);

  // backward put first data
  n += tGetTSRow(p + n, &row.tsRow);
  ASSERT(n <= pSubmitBlk->nData);

  memDataMovePosTo(pMemData, pos, &(TSDBKEY){.version = version, .ts = row.tsRow.ts}, SL_MOVE_BACKWARD);
  code = memDataDoPut(pMemTable, pMemData, pos, &row, 0);
  if (code) {
    goto _exit;
  }
  nRow++;

H
more  
Hongze Cheng 已提交
411 412 413 414 415 416 417
  if (tsdbKeyCmprFn((TSDBKEY *)&row, &pMemData->minKey) < 0) {
    pMemData->minKey = *(TSDBKEY *)&row;
  }
  if (tsdbKeyCmprFn((TSDBKEY *)&row, &pMemTable->minKey) < 0) {
    pMemTable->minKey = *(TSDBKEY *)&row;
  }

H
more  
Hongze Cheng 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
  // forward put rest
  for (int8_t iLevel = 0; iLevel < pMemData->sl.maxLevel; iLevel++) {
    pos[iLevel] = SL_NODE_BACKWARD(pos[iLevel], iLevel);
  }
  while (n < pSubmitBlk->nData) {
    n += tGetTSRow(p + n, &row.tsRow);
    ASSERT(n <= pSubmitBlk->nData);

    memDataMovePosTo(pMemData, pos, &(TSDBKEY){.version = version, .ts = row.tsRow.ts}, SL_MOVE_FROM_POS);
    code = memDataDoPut(pMemTable, pMemData, pos, &row, 1);
    if (code) {
      goto _exit;
    }

    nRow++;
  }

H
more  
Hongze Cheng 已提交
435 436 437 438 439 440 441 442
  if (tsdbKeyCmprFn((TSDBKEY *)&row, &pMemData->maxKey) > 0) {
    pMemData->maxKey = *(TSDBKEY *)&row;
  }
  if (tsdbKeyCmprFn((TSDBKEY *)&row, &pMemTable->maxKey) > 0) {
    pMemTable->maxKey = *(TSDBKEY *)&row;
  }
  pMemTable->nRows += nRow;

H
more  
Hongze Cheng 已提交
443
_exit:
H
Hongze Cheng 已提交
444
  return code;
H
more  
Hongze Cheng 已提交
445
}