• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3534

21 Nov 2024 07:36AM UTC coverage: 60.825% (+2.0%) from 58.848%
#3534

push

travis-ci

web-flow
Merge pull request #28810 from taosdata/ehn/add-sync-heartbeat-sent-time-to-log

ehn:add-sync-heartbeat-sent-time-to-log

120023 of 252376 branches covered (47.56%)

Branch coverage included in aggregate %.

43 of 47 new or added lines in 3 files covered. (91.49%)

2254 existing lines in 162 files now uncovered.

200876 of 275203 relevant lines covered (72.99%)

16110754.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

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

16
#include "tsdb.h"
17
#include "util/tsimplehash.h"
18

19
#define MEM_MIN_HASH 1024
20
#define SL_MAX_LEVEL 5
21

22
// sizeof(SMemSkipListNode) + sizeof(SMemSkipListNode *) * (l) * 2
23
#define SL_NODE_SIZE(l)               (sizeof(SMemSkipListNode) + ((l) << 4))
24
#define SL_NODE_FORWARD(n, l)         ((n)->forwards[l])
25
#define SL_NODE_BACKWARD(n, l)        ((n)->forwards[(n)->level + (l)])
26
#define SL_GET_NODE_FORWARD(n, l)     ((SMemSkipListNode *)atomic_load_ptr(&SL_NODE_FORWARD(n, l)))
27
#define SL_GET_NODE_BACKWARD(n, l)    ((SMemSkipListNode *)atomic_load_ptr(&SL_NODE_BACKWARD(n, l)))
28
#define SL_SET_NODE_FORWARD(n, l, p)  atomic_store_ptr(&SL_NODE_FORWARD(n, l), p)
29
#define SL_SET_NODE_BACKWARD(n, l, p) atomic_store_ptr(&SL_NODE_BACKWARD(n, l), p)
30

31
#define SL_MOVE_BACKWARD 0x1
32
#define SL_MOVE_FROM_POS 0x2
33

34
static void    tbDataMovePosTo(STbData *pTbData, SMemSkipListNode **pos, STsdbRowKey *pKey, int32_t flags);
35
static int32_t tsdbGetOrCreateTbData(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid, STbData **ppTbData);
36
static int32_t tsdbInsertRowDataToTable(SMemTable *pMemTable, STbData *pTbData, int64_t version,
37
                                        SSubmitTbData *pSubmitTbData, int32_t *affectedRows);
38
static int32_t tsdbInsertColDataToTable(SMemTable *pMemTable, STbData *pTbData, int64_t version,
39
                                        SSubmitTbData *pSubmitTbData, int32_t *affectedRows);
40

41
static int32_t tTbDataCmprFn(const SRBTreeNode *n1, const SRBTreeNode *n2) {
3,151,721✔
42
  STbData *tbData1 = TCONTAINER_OF(n1, STbData, rbtn);
3,151,721✔
43
  STbData *tbData2 = TCONTAINER_OF(n2, STbData, rbtn);
3,151,721✔
44
  if (tbData1->suid < tbData2->suid) return -1;
3,151,721✔
45
  if (tbData1->suid > tbData2->suid) return 1;
2,994,924✔
46
  if (tbData1->uid < tbData2->uid) return -1;
2,771,454✔
47
  if (tbData1->uid > tbData2->uid) return 1;
2,209,948!
48
  return 0;
×
49
}
50

51
int32_t tsdbMemTableCreate(STsdb *pTsdb, SMemTable **ppMemTable) {
44,909✔
52
  int32_t    code = 0;
44,909✔
53
  SMemTable *pMemTable = NULL;
44,909✔
54

55
  pMemTable = (SMemTable *)taosMemoryCalloc(1, sizeof(*pMemTable));
44,909✔
56
  if (pMemTable == NULL) {
44,921!
57
    code = terrno;
×
58
    goto _err;
×
59
  }
60
  taosInitRWLatch(&pMemTable->latch);
44,921✔
61
  pMemTable->pTsdb = pTsdb;
44,919✔
62
  pMemTable->pPool = pTsdb->pVnode->inUse;
44,919✔
63
  pMemTable->nRef = 1;
44,919✔
64
  pMemTable->minVer = VERSION_MAX;
44,919✔
65
  pMemTable->maxVer = VERSION_MIN;
44,919✔
66
  pMemTable->minKey = TSKEY_MAX;
44,919✔
67
  pMemTable->maxKey = TSKEY_MIN;
44,919✔
68
  pMemTable->nRow = 0;
44,919✔
69
  pMemTable->nDel = 0;
44,919✔
70
  pMemTable->nTbData = 0;
44,919✔
71
  pMemTable->nBucket = MEM_MIN_HASH;
44,919✔
72
  pMemTable->aBucket = (STbData **)taosMemoryCalloc(pMemTable->nBucket, sizeof(STbData *));
44,919✔
73
  if (pMemTable->aBucket == NULL) {
44,921!
74
    code = terrno;
×
75
    taosMemoryFree(pMemTable);
×
76
    goto _err;
×
77
  }
78
  vnodeBufPoolRef(pMemTable->pPool);
44,921✔
79
  tRBTreeCreate(pMemTable->tbDataTree, tTbDataCmprFn);
44,918✔
80

81
  *ppMemTable = pMemTable;
44,918✔
82
  return code;
44,918✔
83

84
_err:
×
85
  *ppMemTable = NULL;
×
86
  return code;
×
87
}
88

89
void tsdbMemTableDestroy(SMemTable *pMemTable, bool proactive) {
44,921✔
90
  if (pMemTable) {
44,921!
91
    vnodeBufPoolUnRef(pMemTable->pPool, proactive);
44,921✔
92
    taosMemoryFree(pMemTable->aBucket);
44,921✔
93
    taosMemoryFree(pMemTable);
44,919✔
94
  }
95
}
44,918✔
96

97
static FORCE_INLINE STbData *tsdbGetTbDataFromMemTableImpl(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid) {
98
  STbData *pTbData = pMemTable->aBucket[TABS(uid) % pMemTable->nBucket];
29,597,451✔
99

100
  while (pTbData) {
29,674,056✔
101
    if (pTbData->uid == uid) break;
20,076,151✔
102
    pTbData = pTbData->next;
76,605✔
103
  }
104

105
  return pTbData;
29,597,451✔
106
}
107

108
STbData *tsdbGetTbDataFromMemTable(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid) {
15,723,643✔
109
  STbData *pTbData;
110

111
  taosRLockLatch(&pMemTable->latch);
15,723,643✔
112
  pTbData = tsdbGetTbDataFromMemTableImpl(pMemTable, suid, uid);
15,736,556✔
113
  taosRUnLockLatch(&pMemTable->latch);
15,736,556✔
114

115
  return pTbData;
15,736,379✔
116
}
117

118
int32_t tsdbInsertTableData(STsdb *pTsdb, int64_t version, SSubmitTbData *pSubmitTbData, int32_t *affectedRows) {
13,798,617✔
119
  int32_t    code = 0;
13,798,617✔
120
  SMemTable *pMemTable = pTsdb->mem;
13,798,617✔
121
  STbData   *pTbData = NULL;
13,798,617✔
122
  tb_uid_t   suid = pSubmitTbData->suid;
13,798,617✔
123
  tb_uid_t   uid = pSubmitTbData->uid;
13,798,617✔
124

125
  if (tsBypassFlag & TSDB_BYPASS_RB_TSDB_WRITE_MEM) {
13,798,617!
126
    goto _err;
×
127
  }
128

129
  // create/get STbData to op
130
  code = tsdbGetOrCreateTbData(pMemTable, suid, uid, &pTbData);
13,798,617✔
131
  if (code) {
13,796,721!
132
    goto _err;
×
133
  }
134

135
  // do insert impl
136
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
13,796,721✔
137
    code = tsdbInsertColDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
149✔
138
  } else {
139
    code = tsdbInsertRowDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
13,796,572✔
140
  }
141
  if (code) goto _err;
13,800,834!
142

143
  // update
144
  pMemTable->minVer = TMIN(pMemTable->minVer, version);
13,800,834✔
145
  pMemTable->maxVer = TMAX(pMemTable->maxVer, version);
13,800,834✔
146

147
  return code;
13,800,834✔
148

149
_err:
×
150
  terrno = code;
×
151
  return code;
×
152
}
153

154
int32_t tsdbDeleteTableData(STsdb *pTsdb, int64_t version, tb_uid_t suid, tb_uid_t uid, TSKEY sKey, TSKEY eKey) {
62,605✔
155
  int32_t    code = 0;
62,605✔
156
  SMemTable *pMemTable = pTsdb->mem;
62,605✔
157
  STbData   *pTbData = NULL;
62,605✔
158
  SVBufPool *pPool = pTsdb->pVnode->inUse;
62,605✔
159

160
  // check if table exists
161
  SMetaInfo info;
162
  code = metaGetInfo(pTsdb->pVnode->pMeta, uid, &info, NULL);
62,605✔
163
  if (code) {
62,608!
164
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
165
    goto _err;
×
166
  }
167
  if (info.suid != suid) {
62,608!
168
    code = TSDB_CODE_INVALID_MSG;
×
169
    goto _err;
×
170
  }
171

172
  code = tsdbGetOrCreateTbData(pMemTable, suid, uid, &pTbData);
62,608✔
173
  if (code) {
62,611!
174
    goto _err;
×
175
  }
176

177
  // do delete
178
  SDelData *pDelData = (SDelData *)vnodeBufPoolMalloc(pPool, sizeof(*pDelData));
62,611✔
179
  if (pDelData == NULL) {
62,609!
180
    code = terrno;
×
181
    goto _err;
×
182
  }
183
  pDelData->version = version;
62,609✔
184
  pDelData->sKey = sKey;
62,609✔
185
  pDelData->eKey = eKey;
62,609✔
186
  pDelData->pNext = NULL;
62,609✔
187
  taosWLockLatch(&pTbData->lock);
62,609✔
188
  if (pTbData->pHead == NULL) {
62,611✔
189
    pTbData->pHead = pTbData->pTail = pDelData;
18,844✔
190
  } else {
191
    pTbData->pTail->pNext = pDelData;
43,767✔
192
    pTbData->pTail = pDelData;
43,767✔
193
  }
194
  taosWUnLockLatch(&pTbData->lock);
62,611✔
195

196
  pMemTable->nDel++;
62,611✔
197
  pMemTable->minVer = TMIN(pMemTable->minVer, version);
62,611✔
198
  pMemTable->maxVer = TMAX(pMemTable->maxVer, version);
62,611✔
199

200
  if (tsdbCacheDel(pTsdb, suid, uid, sKey, eKey) != 0) {
62,611!
201
    tsdbError("vgId:%d, failed to delete cache data from table suid:%" PRId64 " uid:%" PRId64 " skey:%" PRId64
×
202
              " eKey:%" PRId64 " at version %" PRId64,
203
              TD_VID(pTsdb->pVnode), suid, uid, sKey, eKey, version);
204
  }
205

206
  tsdbTrace("vgId:%d, delete data from table suid:%" PRId64 " uid:%" PRId64 " skey:%" PRId64 " eKey:%" PRId64
62,607✔
207
            " at version %" PRId64,
208
            TD_VID(pTsdb->pVnode), suid, uid, sKey, eKey, version);
209
  return code;
62,608✔
210

211
_err:
×
212
  tsdbError("vgId:%d, failed to delete data from table suid:%" PRId64 " uid:%" PRId64 " skey:%" PRId64 " eKey:%" PRId64
×
213
            " at version %" PRId64 " since %s",
214
            TD_VID(pTsdb->pVnode), suid, uid, sKey, eKey, version, tstrerror(code));
215
  return code;
×
216
}
217

218
int32_t tsdbTbDataIterCreate(STbData *pTbData, STsdbRowKey *pFrom, int8_t backward, STbDataIter **ppIter) {
4,569,056✔
219
  int32_t code = 0;
4,569,056✔
220

221
  (*ppIter) = (STbDataIter *)taosMemoryCalloc(1, sizeof(STbDataIter));
4,569,056✔
222
  if ((*ppIter) == NULL) {
4,569,816!
UNCOV
223
    code = terrno;
×
UNCOV
224
    goto _exit;
×
225
  }
226

227
  tsdbTbDataIterOpen(pTbData, pFrom, backward, *ppIter);
4,569,816✔
228

229
_exit:
4,566,969✔
230
  return code;
4,566,969✔
231
}
232

233
void *tsdbTbDataIterDestroy(STbDataIter *pIter) {
4,566,404✔
234
  if (pIter) {
4,566,404!
235
    taosMemoryFree(pIter);
4,566,436✔
236
  }
237
  return NULL;
4,569,370✔
238
}
239

240
void tsdbTbDataIterOpen(STbData *pTbData, STsdbRowKey *pFrom, int8_t backward, STbDataIter *pIter) {
5,908,241✔
241
  SMemSkipListNode *pos[SL_MAX_LEVEL];
242
  SMemSkipListNode *pHead;
243
  SMemSkipListNode *pTail;
244

245
  pHead = pTbData->sl.pHead;
5,908,241✔
246
  pTail = pTbData->sl.pTail;
5,908,241✔
247
  pIter->pTbData = pTbData;
5,908,241✔
248
  pIter->backward = backward;
5,908,241✔
249
  pIter->pRow = NULL;
5,908,241✔
250
  if (pFrom == NULL) {
5,908,241✔
251
    // create from head or tail
252
    if (backward) {
351!
253
      pIter->pNode = SL_GET_NODE_BACKWARD(pTbData->sl.pTail, 0);
351✔
254
    } else {
255
      pIter->pNode = SL_GET_NODE_FORWARD(pTbData->sl.pHead, 0);
×
256
    }
257
  } else {
258
    // create from a key
259
    if (backward) {
5,907,890✔
260
      tbDataMovePosTo(pTbData, pos, pFrom, SL_MOVE_BACKWARD);
837,985✔
261
      pIter->pNode = SL_GET_NODE_BACKWARD(pos[0], 0);
837,926✔
262
    } else {
263
      tbDataMovePosTo(pTbData, pos, pFrom, 0);
5,069,905✔
264
      pIter->pNode = SL_GET_NODE_FORWARD(pos[0], 0);
5,060,813✔
265
    }
266
  }
267
}
5,898,256✔
268

269
bool tsdbTbDataIterNext(STbDataIter *pIter) {
1,740,445,779✔
270
  pIter->pRow = NULL;
1,740,445,779✔
271
  if (pIter->backward) {
1,740,445,779✔
272
    if (pIter->pNode == pIter->pTbData->sl.pHead) {
128,267,032!
273
      return false;
×
274
    }
275

276
    pIter->pNode = SL_GET_NODE_BACKWARD(pIter->pNode, 0);
128,267,032✔
277
    if (pIter->pNode == pIter->pTbData->sl.pHead) {
128,109,505✔
278
      return false;
322,298✔
279
    }
280
  } else {
281
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
1,612,178,747!
282
      return false;
×
283
    }
284

285
    pIter->pNode = SL_GET_NODE_FORWARD(pIter->pNode, 0);
1,612,178,747✔
286
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
1,611,564,143✔
287
      return false;
2,724,049✔
288
    }
289
  }
290

291
  return true;
1,736,627,301✔
292
}
293

294
int64_t tsdbCountTbDataRows(STbData *pTbData) {
×
295
  SMemSkipListNode *pNode = pTbData->sl.pHead;
×
296
  int64_t           rowsNum = 0;
×
297

298
  while (NULL != pNode) {
×
299
    pNode = SL_GET_NODE_FORWARD(pNode, 0);
×
300
    if (pNode == pTbData->sl.pTail) {
×
301
      return rowsNum;
×
302
    }
303

304
    rowsNum++;
×
305
  }
306

307
  return rowsNum;
×
308
}
309

310
void tsdbMemTableCountRows(SMemTable *pMemTable, SSHashObj *pTableMap, int64_t *rowsNum) {
×
311
  taosRLockLatch(&pMemTable->latch);
×
312
  for (int32_t i = 0; i < pMemTable->nBucket; ++i) {
×
313
    STbData *pTbData = pMemTable->aBucket[i];
×
314
    while (pTbData) {
×
315
      void *p = tSimpleHashGet(pTableMap, &pTbData->uid, sizeof(pTbData->uid));
×
316
      if (p == NULL) {
×
317
        pTbData = pTbData->next;
×
318
        continue;
×
319
      }
320

321
      *rowsNum += tsdbCountTbDataRows(pTbData);
×
322
      pTbData = pTbData->next;
×
323
    }
324
  }
325
  taosRUnLockLatch(&pMemTable->latch);
×
326
}
×
327

328
static int32_t tsdbMemTableRehash(SMemTable *pMemTable) {
48✔
329
  int32_t code = 0;
48✔
330

331
  int32_t   nBucket = pMemTable->nBucket * 2;
48✔
332
  STbData **aBucket = (STbData **)taosMemoryCalloc(nBucket, sizeof(STbData *));
48✔
333
  if (aBucket == NULL) {
48!
334
    code = terrno;
×
335
    goto _exit;
×
336
  }
337

338
  for (int32_t iBucket = 0; iBucket < pMemTable->nBucket; iBucket++) {
99,376✔
339
    STbData *pTbData = pMemTable->aBucket[iBucket];
99,328✔
340

341
    while (pTbData) {
198,656✔
342
      STbData *pNext = pTbData->next;
99,328✔
343

344
      int32_t idx = TABS(pTbData->uid) % nBucket;
99,328✔
345
      pTbData->next = aBucket[idx];
99,328✔
346
      aBucket[idx] = pTbData;
99,328✔
347

348
      pTbData = pNext;
99,328✔
349
    }
350
  }
351

352
  taosMemoryFree(pMemTable->aBucket);
48✔
353
  pMemTable->nBucket = nBucket;
48✔
354
  pMemTable->aBucket = aBucket;
48✔
355

356
_exit:
48✔
357
  return code;
48✔
358
}
359

360
static int32_t tsdbGetOrCreateTbData(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid, STbData **ppTbData) {
13,860,895✔
361
  int32_t code = 0;
13,860,895✔
362

363
  // get
364
  STbData *pTbData = tsdbGetTbDataFromMemTableImpl(pMemTable, suid, uid);
13,860,895✔
365
  if (pTbData) goto _exit;
13,860,895✔
366

367
  // create
368
  SVBufPool *pPool = pMemTable->pTsdb->pVnode->inUse;
232,752✔
369
  int8_t     maxLevel = pMemTable->pTsdb->pVnode->config.tsdbCfg.slLevel;
232,752✔
370

371
  pTbData = vnodeBufPoolMallocAligned(pPool, sizeof(*pTbData) + SL_NODE_SIZE(maxLevel) * 2);
232,752✔
372
  if (pTbData == NULL) {
233,156!
373
    code = terrno;
×
374
    goto _exit;
×
375
  }
376
  pTbData->suid = suid;
233,156✔
377
  pTbData->uid = uid;
233,156✔
378
  pTbData->minKey = TSKEY_MAX;
233,156✔
379
  pTbData->maxKey = TSKEY_MIN;
233,156✔
380
  pTbData->pHead = NULL;
233,156✔
381
  pTbData->pTail = NULL;
233,156✔
382
  pTbData->sl.seed = taosRand();
233,156✔
383
  pTbData->sl.size = 0;
233,175✔
384
  pTbData->sl.maxLevel = maxLevel;
233,175✔
385
  pTbData->sl.level = 0;
233,175✔
386
  pTbData->sl.pHead = (SMemSkipListNode *)&pTbData[1];
233,175✔
387
  pTbData->sl.pTail = (SMemSkipListNode *)POINTER_SHIFT(pTbData->sl.pHead, SL_NODE_SIZE(maxLevel));
233,175✔
388
  pTbData->sl.pHead->level = maxLevel;
233,175✔
389
  pTbData->sl.pTail->level = maxLevel;
233,175✔
390
  for (int8_t iLevel = 0; iLevel < maxLevel; iLevel++) {
1,399,000✔
391
    SL_NODE_FORWARD(pTbData->sl.pHead, iLevel) = pTbData->sl.pTail;
1,165,825✔
392
    SL_NODE_BACKWARD(pTbData->sl.pTail, iLevel) = pTbData->sl.pHead;
1,165,825✔
393

394
    SL_NODE_BACKWARD(pTbData->sl.pHead, iLevel) = NULL;
1,165,825✔
395
    SL_NODE_FORWARD(pTbData->sl.pTail, iLevel) = NULL;
1,165,825✔
396
  }
397
  taosInitRWLatch(&pTbData->lock);
233,175✔
398

399
  taosWLockLatch(&pMemTable->latch);
233,166✔
400

401
  if (pMemTable->nTbData >= pMemTable->nBucket) {
233,168✔
402
    code = tsdbMemTableRehash(pMemTable);
48✔
403
    if (code) {
48!
404
      taosWUnLockLatch(&pMemTable->latch);
×
405
      goto _exit;
×
406
    }
407
  }
408

409
  int32_t idx = TABS(uid) % pMemTable->nBucket;
233,168✔
410
  pTbData->next = pMemTable->aBucket[idx];
233,168✔
411
  pMemTable->aBucket[idx] = pTbData;
233,168✔
412
  pMemTable->nTbData++;
233,168✔
413

414
  if (tRBTreePut(pMemTable->tbDataTree, pTbData->rbtn) == NULL) {
233,168!
415
    taosWUnLockLatch(&pMemTable->latch);
×
416
    code = TSDB_CODE_INTERNAL_ERROR;
×
417
    goto _exit;
×
418
  }
419

420
  taosWUnLockLatch(&pMemTable->latch);
233,155✔
421

422
_exit:
13,860,191✔
423
  if (code) {
13,860,191!
424
    *ppTbData = NULL;
×
425
  } else {
426
    *ppTbData = pTbData;
13,860,191✔
427
  }
428
  return code;
13,860,191✔
429
}
430

431
static void tbDataMovePosTo(STbData *pTbData, SMemSkipListNode **pos, STsdbRowKey *pKey, int32_t flags) {
56,996,823✔
432
  SMemSkipListNode *px;
433
  SMemSkipListNode *pn;
434
  STsdbRowKey       tKey;
435
  int32_t           backward = flags & SL_MOVE_BACKWARD;
56,996,823✔
436
  int32_t           fromPos = flags & SL_MOVE_FROM_POS;
56,996,823✔
437

438
  if (backward) {
56,996,823✔
439
    px = pTbData->sl.pTail;
14,632,584✔
440

441
    if (!fromPos) {
14,632,584!
442
      for (int8_t iLevel = pTbData->sl.level; iLevel < pTbData->sl.maxLevel; iLevel++) {
20,682,358✔
443
        pos[iLevel] = px;
6,049,651✔
444
      }
445
    }
446

447
    if (pTbData->sl.level) {
14,632,584✔
448
      if (fromPos) px = pos[pTbData->sl.level - 1];
14,398,816!
449

450
      for (int8_t iLevel = pTbData->sl.level - 1; iLevel >= 0; iLevel--) {
81,306,709✔
451
        pn = SL_GET_NODE_BACKWARD(px, iLevel);
66,924,390✔
452
        while (pn != pTbData->sl.pHead) {
77,519,520✔
453
          tsdbRowGetKey(&pn->row, &tKey);
76,306,215✔
454

455
          int32_t c = tsdbRowKeyCmpr(&tKey, pKey);
76,303,259✔
456
          if (c <= 0) {
76,300,464✔
457
            break;
65,694,588✔
458
          } else {
459
            px = pn;
10,605,876✔
460
            pn = SL_GET_NODE_BACKWARD(px, iLevel);
10,605,876✔
461
          }
462
        }
463

464
        pos[iLevel] = px;
66,907,893✔
465
      }
466
    }
467
  } else {
468
    px = pTbData->sl.pHead;
42,364,239✔
469

470
    if (!fromPos) {
42,364,239✔
471
      for (int8_t iLevel = pTbData->sl.level; iLevel < pTbData->sl.maxLevel; iLevel++) {
10,004,164✔
472
        pos[iLevel] = px;
4,934,554✔
473
      }
474
    }
475

476
    if (pTbData->sl.level) {
42,364,239✔
477
      if (fromPos) px = pos[pTbData->sl.level - 1];
42,359,999✔
478

479
      for (int8_t iLevel = pTbData->sl.level - 1; iLevel >= 0; iLevel--) {
248,406,605✔
480
        pn = SL_GET_NODE_FORWARD(px, iLevel);
206,063,026✔
481
        while (pn != pTbData->sl.pTail) {
674,692,636✔
482
          tsdbRowGetKey(&pn->row, &tKey);
670,393,283✔
483

484
          int32_t c = tsdbRowKeyCmpr(&tKey, pKey);
670,486,451✔
485
          if (c >= 0) {
670,872,787✔
486
            break;
201,747,253✔
487
          } else {
488
            px = pn;
469,125,534✔
489
            pn = SL_GET_NODE_FORWARD(px, iLevel);
469,125,534✔
490
          }
491
        }
492

493
        pos[iLevel] = px;
206,046,606✔
494
      }
495
    }
496
  }
497
}
56,963,906✔
498

499
static FORCE_INLINE int8_t tsdbMemSkipListRandLevel(SMemSkipList *pSl) {
500
  int8_t level = 1;
393,077,814✔
501
  int8_t tlevel = TMIN(pSl->maxLevel, pSl->level + 1);
393,077,814✔
502

503
  while ((taosRandR(&pSl->seed) & 0x3) == 0 && level < tlevel) {
523,401,639✔
504
    level++;
130,323,825✔
505
  }
506

507
  return level;
392,969,191✔
508
}
509
static int32_t tbDataDoPut(SMemTable *pMemTable, STbData *pTbData, SMemSkipListNode **pos, TSDBROW *pRow,
393,077,814✔
510
                           int8_t forward) {
511
  int32_t           code = 0;
393,077,814✔
512
  int8_t            level;
513
  SMemSkipListNode *pNode = NULL;
393,077,814✔
514
  SVBufPool        *pPool = pMemTable->pTsdb->pVnode->inUse;
393,077,814✔
515
  int64_t           nSize;
516

517
  // create node
518
  level = tsdbMemSkipListRandLevel(&pTbData->sl);
393,077,814✔
519
  nSize = SL_NODE_SIZE(level);
392,969,191✔
520
  if (pRow->type == TSDBROW_ROW_FMT) {
392,969,191!
521
    pNode = (SMemSkipListNode *)vnodeBufPoolMallocAligned(pPool, nSize + pRow->pTSRow->len);
393,004,188✔
522
  } else if (pRow->type == TSDBROW_COL_FMT) {
×
523
    pNode = (SMemSkipListNode *)vnodeBufPoolMallocAligned(pPool, nSize);
254✔
524
  }
525
  if (pNode == NULL) {
392,965,268!
526
    code = terrno;
×
527
    goto _exit;
×
528
  }
529

530
  pNode->level = level;
392,965,268✔
531
  pNode->row = *pRow;
392,965,268✔
532
  if (pRow->type == TSDBROW_ROW_FMT) {
392,965,268!
533
    pNode->row.pTSRow = (SRow *)((char *)pNode + nSize);
392,974,698✔
534
    memcpy(pNode->row.pTSRow, pRow->pTSRow, pRow->pTSRow->len);
392,974,698✔
535
  }
536

537
  // set node
538
  if (forward) {
392,965,268✔
539
    for (int8_t iLevel = 0; iLevel < level; iLevel++) {
884,184,671✔
540
      SL_NODE_FORWARD(pNode, iLevel) = SL_NODE_FORWARD(pos[iLevel], iLevel);
504,957,906✔
541
      SL_NODE_BACKWARD(pNode, iLevel) = pos[iLevel];
504,957,906✔
542
    }
543
  } else {
544
    for (int8_t iLevel = 0; iLevel < level; iLevel++) {
31,994,496✔
545
      SL_NODE_FORWARD(pNode, iLevel) = pos[iLevel];
18,255,993✔
546
      SL_NODE_BACKWARD(pNode, iLevel) = SL_NODE_BACKWARD(pos[iLevel], iLevel);
18,255,993✔
547
    }
548
  }
549

550
  // set forward and backward
551
  if (forward) {
392,965,268✔
552
    for (int8_t iLevel = level - 1; iLevel >= 0; iLevel--) {
884,383,663✔
553
      SMemSkipListNode *pNext = pos[iLevel]->forwards[iLevel];
505,014,555✔
554

555
      SL_SET_NODE_FORWARD(pos[iLevel], iLevel, pNode);
505,014,555✔
556
      SL_SET_NODE_BACKWARD(pNext, iLevel, pNode);
505,182,226✔
557

558
      pos[iLevel] = pNode;
505,151,249✔
559
    }
560
  } else {
561
    for (int8_t iLevel = level - 1; iLevel >= 0; iLevel--) {
32,069,775✔
562
      SMemSkipListNode *pPrev = pos[iLevel]->forwards[pos[iLevel]->level + iLevel];
18,260,818✔
563

564
      SL_SET_NODE_FORWARD(pPrev, iLevel, pNode);
18,260,818✔
565
      SL_SET_NODE_BACKWARD(pos[iLevel], iLevel, pNode);
18,282,830✔
566

567
      pos[iLevel] = pNode;
18,336,921✔
568
    }
569
  }
570

571
  pTbData->sl.size++;
393,178,065✔
572
  if (pTbData->sl.level < pNode->level) {
393,178,065✔
573
    pTbData->sl.level = pNode->level;
788,176✔
574
  }
575

576
_exit:
392,389,889✔
577
  return code;
393,178,065✔
578
}
579

580
static int32_t tsdbInsertColDataToTable(SMemTable *pMemTable, STbData *pTbData, int64_t version,
149✔
581
                                        SSubmitTbData *pSubmitTbData, int32_t *affectedRows) {
582
  int32_t code = 0;
149✔
583

584
  SVBufPool *pPool = pMemTable->pTsdb->pVnode->inUse;
149✔
585
  int32_t    nColData = TARRAY_SIZE(pSubmitTbData->aCol);
149✔
586
  SColData  *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
149✔
587

588
  // copy and construct block data
589
  SBlockData *pBlockData = vnodeBufPoolMalloc(pPool, sizeof(*pBlockData));
149✔
590
  if (pBlockData == NULL) {
149!
591
    code = terrno;
×
592
    goto _exit;
×
593
  }
594

595
  pBlockData->suid = pTbData->suid;
149✔
596
  pBlockData->uid = pTbData->uid;
149✔
597
  pBlockData->nRow = aColData[0].nVal;
149✔
598
  pBlockData->aUid = NULL;
149✔
599
  pBlockData->aVersion = vnodeBufPoolMalloc(pPool, aColData[0].nData);
149✔
600
  if (pBlockData->aVersion == NULL) {
149!
601
    code = terrno;
×
602
    goto _exit;
×
603
  }
604
  for (int32_t i = 0; i < pBlockData->nRow; i++) {  // todo: here can be optimized
403✔
605
    pBlockData->aVersion[i] = version;
254✔
606
  }
607

608
  pBlockData->aTSKEY = vnodeBufPoolMalloc(pPool, aColData[0].nData);
149✔
609
  if (pBlockData->aTSKEY == NULL) {
149!
610
    code = terrno;
×
611
    goto _exit;
×
612
  }
613
  memcpy(pBlockData->aTSKEY, aColData[0].pData, aColData[0].nData);
149✔
614

615
  pBlockData->nColData = nColData - 1;
149✔
616
  pBlockData->aColData = vnodeBufPoolMalloc(pPool, sizeof(SColData) * pBlockData->nColData);
149✔
617
  if (pBlockData->aColData == NULL) {
149!
618
    code = terrno;
×
619
    goto _exit;
×
620
  }
621

622
  for (int32_t iColData = 0; iColData < pBlockData->nColData; ++iColData) {
668✔
623
    code = tColDataCopy(&aColData[iColData + 1], &pBlockData->aColData[iColData], (xMallocFn)vnodeBufPoolMalloc, pPool);
519✔
624
    if (code) goto _exit;
519!
625
  }
626

627
  // loop to add each row to the skiplist
628
  SMemSkipListNode *pos[SL_MAX_LEVEL];
629
  TSDBROW           tRow = tsdbRowFromBlockData(pBlockData, 0);
149✔
630
  STsdbRowKey       key;
631

632
  // first row
633
  tsdbRowGetKey(&tRow, &key);
149✔
634
  tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_BACKWARD);
149✔
635
  if ((code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 0))) goto _exit;
149!
636
  pTbData->minKey = TMIN(pTbData->minKey, key.key.ts);
149✔
637

638
  // remain row
639
  ++tRow.iRow;
149✔
640
  if (tRow.iRow < pBlockData->nRow) {
149✔
641
    for (int8_t iLevel = pos[0]->level; iLevel < pTbData->sl.maxLevel; iLevel++) {
186✔
642
      pos[iLevel] = SL_NODE_BACKWARD(pos[iLevel], iLevel);
148✔
643
    }
644

645
    while (tRow.iRow < pBlockData->nRow) {
143✔
646
      tsdbRowGetKey(&tRow, &key);
105✔
647

648
      if (SL_NODE_FORWARD(pos[0], 0) != pTbData->sl.pTail) {
105!
649
        tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_FROM_POS);
×
650
      }
651

652
      if ((code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 1))) goto _exit;
105!
653

654
      ++tRow.iRow;
105✔
655
    }
656
  }
657

658
  if (key.key.ts >= pTbData->maxKey) {
149✔
659
    pTbData->maxKey = key.key.ts;
148✔
660
  }
661

662
  if (!TSDB_CACHE_NO(pMemTable->pTsdb->pVnode->config)) {
149!
663
    if (tsdbCacheColFormatUpdate(pMemTable->pTsdb, pTbData->suid, pTbData->uid, pBlockData) != 0) {
×
664
      tsdbError("vgId:%d, failed to update cache data from table suid:%" PRId64 " uid:%" PRId64 " at version %" PRId64,
×
665
                TD_VID(pMemTable->pTsdb->pVnode), pTbData->suid, pTbData->uid, version);
666
    }
667
  }
668

669
  // SMemTable
670
  pMemTable->minKey = TMIN(pMemTable->minKey, pTbData->minKey);
149✔
671
  pMemTable->maxKey = TMAX(pMemTable->maxKey, pTbData->maxKey);
149✔
672
  pMemTable->nRow += pBlockData->nRow;
149✔
673

674
  if (affectedRows) *affectedRows = pBlockData->nRow;
149!
675

676
_exit:
×
677
  return code;
149✔
678
}
679

680
static int32_t tsdbInsertRowDataToTable(SMemTable *pMemTable, STbData *pTbData, int64_t version,
13,796,321✔
681
                                        SSubmitTbData *pSubmitTbData, int32_t *affectedRows) {
682
  int32_t code = 0;
13,796,321✔
683

684
  int32_t           nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
13,796,321✔
685
  SRow            **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
13,796,321✔
686
  STsdbRowKey       key;
687
  SMemSkipListNode *pos[SL_MAX_LEVEL];
688
  TSDBROW           tRow = {.type = TSDBROW_ROW_FMT, .version = version};
13,796,321✔
689
  int32_t           iRow = 0;
13,796,321✔
690

691
  // backward put first data
692
  tRow.pTSRow = aRow[iRow++];
13,796,321✔
693
  tsdbRowGetKey(&tRow, &key);
13,796,321✔
694
  tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_BACKWARD);
13,794,486✔
695
  code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 0);
13,766,892✔
696
  if (code) goto _exit;
13,801,898!
697

698
  pTbData->minKey = TMIN(pTbData->minKey, key.key.ts);
13,801,898✔
699

700
  // forward put rest data
701
  if (iRow < nRow) {
13,801,898✔
702
    for (int8_t iLevel = pos[0]->level; iLevel < pTbData->sl.maxLevel; iLevel++) {
5,229,885✔
703
      pos[iLevel] = SL_NODE_BACKWARD(pos[iLevel], iLevel);
4,120,331✔
704
    }
705

706
    while (iRow < nRow) {
380,475,000✔
707
      tRow.pTSRow = aRow[iRow];
379,364,364✔
708
      tsdbRowGetKey(&tRow, &key);
379,364,364✔
709

710
      if (SL_NODE_FORWARD(pos[0], 0) != pTbData->sl.pTail) {
379,333,647✔
711
        tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_FROM_POS);
37,295,211✔
712
      }
713

714
      code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 1);
379,330,649✔
715
      if (code) goto _exit;
379,365,446!
716

717
      iRow++;
379,365,446✔
718
    }
719
  }
720

721
  if (key.key.ts >= pTbData->maxKey) {
13,802,980✔
722
    pTbData->maxKey = key.key.ts;
13,625,886✔
723
  }
724
  if (!TSDB_CACHE_NO(pMemTable->pTsdb->pVnode->config)) {
13,802,980✔
725
    TAOS_UNUSED(tsdbCacheRowFormatUpdate(pMemTable->pTsdb, pTbData->suid, pTbData->uid, version, nRow, aRow));
246,055✔
726
  }
727

728
  // SMemTable
729
  pMemTable->minKey = TMIN(pMemTable->minKey, pTbData->minKey);
13,801,070✔
730
  pMemTable->maxKey = TMAX(pMemTable->maxKey, pTbData->maxKey);
13,801,070✔
731
  pMemTable->nRow += nRow;
13,801,070✔
732

733
  if (affectedRows) *affectedRows = nRow;
13,801,070!
734

735
_exit:
×
736
  return code;
13,801,070✔
737
}
738

739
int32_t tsdbGetNRowsInTbData(STbData *pTbData) { return pTbData->sl.size; }
1,623✔
740

741
int32_t tsdbRefMemTable(SMemTable *pMemTable, SQueryNode *pQNode) {
4,166,660✔
742
  int32_t code = 0;
4,166,660✔
743

744
  int32_t nRef = atomic_fetch_add_32(&pMemTable->nRef, 1);
4,166,660✔
745
  if (nRef <= 0) {
4,171,319!
746
    tsdbError("vgId:%d, memtable ref count is invalid, ref:%d", TD_VID(pMemTable->pTsdb->pVnode), nRef);
×
747
  }
748

749
  vnodeBufPoolRegisterQuery(pMemTable->pPool, pQNode);
4,171,319✔
750

751
_exit:
4,171,534✔
752
  return code;
4,171,534✔
753
}
754

755
void tsdbUnrefMemTable(SMemTable *pMemTable, SQueryNode *pNode, bool proactive) {
4,203,426✔
756
  if (pNode) {
4,203,426✔
757
    vnodeBufPoolDeregisterQuery(pMemTable->pPool, pNode, proactive);
4,171,784✔
758
  }
759

760
  if (atomic_sub_fetch_32(&pMemTable->nRef, 1) == 0) {
4,203,416✔
761
    tsdbMemTableDestroy(pMemTable, proactive);
31,700✔
762
  }
763
}
4,203,638✔
764

765
static FORCE_INLINE int32_t tbDataPCmprFn(const void *p1, const void *p2) {
766
  STbData *pTbData1 = *(STbData **)p1;
767
  STbData *pTbData2 = *(STbData **)p2;
768

769
  if (pTbData1->suid < pTbData2->suid) {
770
    return -1;
771
  } else if (pTbData1->suid > pTbData2->suid) {
772
    return 1;
773
  }
774

775
  if (pTbData1->uid < pTbData2->uid) {
776
    return -1;
777
  } else if (pTbData1->uid > pTbData2->uid) {
778
    return 1;
779
  }
780

781
  return 0;
782
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc