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

taosdata / TDengine / #3654

14 Mar 2025 08:10AM UTC coverage: 36.951% (+14.4%) from 22.565%
#3654

push

travis-ci

web-flow
feat(keep): support keep on super table level. (#30097)

* Feat: support use keep while create super table.

* Test(keep): add test for create super table with keep option.

* Feat(keep): Add tmsg for create keep.

* Feat(keep): support alter table option keep.

* Fix(keep): Add baisc test for alter table option.

* Fix(keep): memory leek.

* Feat(keep): add keep to metaEntry&metaCache and fix earliestTs with stn keep.

* Test(keep): add some cases for select with stb keep.

* Fix: fix ci core while alter stb.

* Feat(keep): delete expired data in super table level.

* Feat: remove get stb keep while query.

* Fix : build error.

* Revert "Fix : build error."

This reverts commit 0ed66e4e8.

* Revert "Feat(keep): delete expired data in super table level."

This reverts commit 36330f6b4.

* Fix : build errors.

* Feat : support restart taosd.

* Fix : alter table comment problems.

* Test : add tests for super table keep.

* Fix: change sdb stb reserve size.

* Test: add more tests.

* Feat: Disable normal tables and sub tables from setting the keep parameter

* Fix: add more checks to avoid unknown address.

* Docs: Add docs for stable keep.

* Fix: some review changes.

* Fix: review errors.

83566 of 302527 branches covered (27.62%)

Branch coverage included in aggregate %.

54 of 99 new or added lines in 12 files covered. (54.55%)

5128 existing lines in 33 files now uncovered.

140127 of 302857 relevant lines covered (46.27%)

678538.13 hits per line

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

20.35
/source/dnode/vnode/src/meta/metaTable.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 "meta.h"
17

18
extern SDmNotifyHandle dmNotifyHdl;
19

20
int32_t metaAddTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
21
int32_t metaDropTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
22
int32_t metaAlterTableColumnName(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
23
int32_t metaAlterTableColumnBytes(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pRsp);
24
int32_t metaUpdateTableTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
25
int32_t metaUpdateTableMultiTagValue(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
26
int32_t metaUpdateTableOptions2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
27
int32_t metaUpdateTableColCompress2(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq);
28

29
int32_t metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
30

31
int32_t    metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema);
32
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs);
33
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl);
34
void       metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey);
35
// opt ins_tables query
36
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME);
37
static int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME);
38

39
int32_t updataTableColCmpr(SColCmprWrapper *pWp, SSchema *pSchema, int8_t add, uint32_t compress) {
×
40
  int32_t nCols = pWp->nCols;
×
41
  int32_t ver = pWp->version;
×
42
  if (add) {
×
43
    SColCmpr *p = taosMemoryRealloc(pWp->pColCmpr, sizeof(SColCmpr) * (nCols + 1));
×
44
    if (p == NULL) {
×
45
      return terrno;
×
46
    }
47
    pWp->pColCmpr = p;
×
48

49
    SColCmpr *pCol = p + nCols;
×
50
    pCol->id = pSchema->colId;
×
51
    pCol->alg = compress;
×
52
    pWp->nCols = nCols + 1;
×
53
    pWp->version = ver;
×
54
  } else {
55
    for (int32_t i = 0; i < nCols; i++) {
×
56
      SColCmpr *pOCmpr = &pWp->pColCmpr[i];
×
57
      if (pOCmpr->id == pSchema->colId) {
×
58
        int32_t left = (nCols - i - 1) * sizeof(SColCmpr);
×
59
        if (left) {
×
60
          memmove(pWp->pColCmpr + i, pWp->pColCmpr + i + 1, left);
×
61
        }
62
        nCols--;
×
63
        break;
×
64
      }
65
    }
66
    pWp->nCols = nCols;
×
67
    pWp->version = ver;
×
68
  }
69
  return 0;
×
70
}
71

72
int metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema, STableMetaRsp *pMetaRsp) {
588✔
73
  pMetaRsp->pSchemas = taosMemoryMalloc(pSchema->nCols * sizeof(SSchema));
588!
74
  if (NULL == pMetaRsp->pSchemas) {
588!
75
    return terrno;
×
76
  }
77

78
  pMetaRsp->pSchemaExt = taosMemoryMalloc(pSchema->nCols * sizeof(SSchemaExt));
588!
79
  if (pMetaRsp->pSchemaExt == NULL) {
588!
80
    taosMemoryFree(pMetaRsp->pSchemas);
×
81
    return terrno;
×
82
  }
83

84
  tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN);
588✔
85
  pMetaRsp->numOfColumns = pSchema->nCols;
588✔
86
  pMetaRsp->tableType = TSDB_NORMAL_TABLE;
588✔
87
  pMetaRsp->sversion = pSchema->version;
588✔
88
  pMetaRsp->tuid = uid;
588✔
89

90
  memcpy(pMetaRsp->pSchemas, pSchema->pSchema, pSchema->nCols * sizeof(SSchema));
588✔
91

92
  return 0;
588✔
93
}
94

95
int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
4✔
96
  int32_t code = 0;
4✔
97

98
#ifdef USE_INVERTED_INDEX
99
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
4!
100
    return TSDB_CODE_INVALID_PARA;
×
101
  }
102
  void       *data = pCtbEntry->ctbEntry.pTags;
4✔
103
  const char *tagName = pSchema->name;
4✔
104

105
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
4✔
106
  tb_uid_t    tuid = pCtbEntry->uid;
4✔
107
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
4✔
108
  int32_t     nTagData = 0;
4✔
109

110
  SArray *pTagVals = NULL;
4✔
111
  code = tTagToValArray((const STag *)data, &pTagVals);
4✔
112
  if (code) {
4!
113
    return code;
×
114
  }
115

116
  SIndexMultiTerm *terms = indexMultiTermCreate();
4✔
117
  if (terms == NULL) {
4!
118
    return terrno;
×
119
  }
120

121
  int16_t nCols = taosArrayGetSize(pTagVals);
4✔
122
  for (int i = 0; i < nCols; i++) {
12✔
123
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
8✔
124
    char     type = pTagVal->type;
8✔
125

126
    char   *key = pTagVal->pKey;
8✔
127
    int32_t nKey = strlen(key);
8✔
128

129
    SIndexTerm *term = NULL;
8✔
130
    if (type == TSDB_DATA_TYPE_NULL) {
8!
131
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
132
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
8✔
133
      if (pTagVal->nData > 0) {
4!
134
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
4!
135
        if (val == NULL) {
4!
136
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
137
        }
138
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
4✔
139
        if (len < 0) {
4!
140
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
141
        }
142
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
4✔
143
        type = TSDB_DATA_TYPE_VARCHAR;
4✔
144
        term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len);
4✔
145
        taosMemoryFree(val);
4!
146
      } else if (pTagVal->nData == 0) {
×
147
        term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
×
148
      }
149
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
4!
150
      double val = *(double *)(&pTagVal->i64);
4✔
151
      int    len = sizeof(val);
4✔
152
      term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len);
4✔
153
    } else if (type == TSDB_DATA_TYPE_BOOL) {
×
154
      int val = *(int *)(&pTagVal->i64);
×
155
      int len = sizeof(val);
×
156
      term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
×
157
    }
158

159
    if (term != NULL) {
8!
160
      int32_t ret = indexMultiTermAdd(terms, term);
8✔
161
      if (ret < 0) {
8!
162
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
163
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
164
      }
165
    } else {
166
      code = terrno;
×
167
      goto _exception;
×
168
    }
169
  }
170
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
4✔
171
  indexMultiTermDestroy(terms);
4✔
172

173
  taosArrayDestroy(pTagVals);
4✔
174
  return code;
4✔
175
_exception:
×
176
  indexMultiTermDestroy(terms);
×
177
  taosArrayDestroy(pTagVals);
×
178
#endif
179
  return code;
×
180
}
181
int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) {
×
182
int32_t code = 0;
×
183
#ifdef USE_INVERTED_INDEX
184
  if (pMeta->pTagIvtIdx == NULL || pCtbEntry == NULL) {
×
185
    return TSDB_CODE_INVALID_PARA;
×
186
  }
187
  void       *data = pCtbEntry->ctbEntry.pTags;
×
188
  const char *tagName = pSchema->name;
×
189

190
  tb_uid_t    suid = pCtbEntry->ctbEntry.suid;
×
191
  tb_uid_t    tuid = pCtbEntry->uid;
×
192
  const void *pTagData = pCtbEntry->ctbEntry.pTags;
×
193
  int32_t     nTagData = 0;
×
194

195
  SArray *pTagVals = NULL;
×
196
  code = tTagToValArray((const STag *)data, &pTagVals);
×
197
  if (code) {
×
198
    return code;
×
199
  }
200

201
  SIndexMultiTerm *terms = indexMultiTermCreate();
×
202
  if (terms == NULL) {
×
203
    return terrno;
×
204
  }
205

206
  int16_t nCols = taosArrayGetSize(pTagVals);
×
207
  for (int i = 0; i < nCols; i++) {
×
208
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
×
209
    char     type = pTagVal->type;
×
210

211
    char   *key = pTagVal->pKey;
×
212
    int32_t nKey = strlen(key);
×
213

214
    SIndexTerm *term = NULL;
×
215
    if (type == TSDB_DATA_TYPE_NULL) {
×
216
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0);
×
217
    } else if (type == TSDB_DATA_TYPE_NCHAR) {
×
218
      if (pTagVal->nData > 0) {
×
219
        char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE);
×
220
        if (val == NULL) {
×
221
          TAOS_CHECK_GOTO(terrno, NULL, _exception);
×
222
        }
223
        int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE, NULL);
×
224
        if (len < 0) {
×
225
          TAOS_CHECK_GOTO(len, NULL, _exception);
×
226
        }
227
        memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE);
×
228
        type = TSDB_DATA_TYPE_VARCHAR;
×
229
        term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len);
×
230
        taosMemoryFree(val);
×
231
      } else if (pTagVal->nData == 0) {
×
232
        term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, pTagVal->pData, 0);
×
233
      }
234
    } else if (type == TSDB_DATA_TYPE_DOUBLE) {
×
235
      double val = *(double *)(&pTagVal->i64);
×
236
      int    len = sizeof(val);
×
237
      term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, (const char *)&val, len);
×
238
    } else if (type == TSDB_DATA_TYPE_BOOL) {
×
239
      int val = *(int *)(&pTagVal->i64);
×
240
      int len = sizeof(val);
×
241
      term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len);
×
242
    }
243
    if (term != NULL) {
×
244
      int32_t ret = indexMultiTermAdd(terms, term);
×
245
      if (ret < 0) {
×
246
        metaError("vgId:%d, failed to add term to multi term, uid: %" PRId64 ", key: %s, type: %d, ret: %d",
×
247
                  TD_VID(pMeta->pVnode), tuid, key, type, ret);
248
      }
249
    } else {
250
      code = terrno;
×
251
      goto _exception;
×
252
    }
253
  }
254
  code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid);
×
255
  indexMultiTermDestroy(terms);
×
256
  taosArrayDestroy(pTagVals);
×
257
  return code;
×
258
_exception:
×
259
  indexMultiTermDestroy(terms);
×
260
  taosArrayDestroy(pTagVals);
×
261
#endif
262
  return code;
×
263
}
264

265
static int32_t metaDropTables(SMeta *pMeta, SArray *tbUids) {
×
266
  int32_t code = 0;
×
267
  if (taosArrayGetSize(tbUids) == 0) return TSDB_CODE_SUCCESS;
×
268

269
  int64_t    nCtbDropped = 0;
×
270
  SSHashObj *suidHash = tSimpleHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
×
271
  if (suidHash == NULL) {
×
272
    return terrno;
×
273
  }
274

275
  metaWLock(pMeta);
×
276
  for (int i = 0; i < taosArrayGetSize(tbUids); ++i) {
×
277
    tb_uid_t uid = *(tb_uid_t *)taosArrayGet(tbUids, i);
×
278
    tb_uid_t suid = 0;
×
279
    int8_t   sysTbl = 0;
×
280
    int      type;
281
    code = metaDropTableByUid(pMeta, uid, &type, &suid, &sysTbl);
×
282
    if (code) return code;
×
283
    if (!sysTbl && type == TSDB_CHILD_TABLE && suid != 0 && suidHash) {
×
284
      int64_t *pVal = tSimpleHashGet(suidHash, &suid, sizeof(tb_uid_t));
×
285
      if (pVal) {
×
286
        nCtbDropped = *pVal + 1;
×
287
      } else {
288
        nCtbDropped = 1;
×
289
      }
290
      code = tSimpleHashPut(suidHash, &suid, sizeof(tb_uid_t), &nCtbDropped, sizeof(int64_t));
×
291
      if (code) return code;
×
292
    }
293
    /*
294
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
295
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, uid, suid, NULL);
296
    }
297
    */
298
    metaDebug("batch drop table:%" PRId64, uid);
×
299
  }
300
  metaULock(pMeta);
×
301

302
  // update timeseries
303
  void   *pCtbDropped = NULL;
×
304
  int32_t iter = 0;
×
305
  while ((pCtbDropped = tSimpleHashIterate(suidHash, pCtbDropped, &iter))) {
×
306
    tb_uid_t    *pSuid = tSimpleHashGetKey(pCtbDropped, NULL);
×
307
    int32_t      nCols = 0;
×
308
    SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
×
309
    if (metaGetStbStats(pMeta->pVnode, *pSuid, NULL, &nCols) == 0) {
×
310
      pStats->numOfTimeSeries -= *(int64_t *)pCtbDropped * (nCols - 1);
×
311
    }
312
  }
313
  tSimpleHashCleanup(suidHash);
×
314

315
  pMeta->changed = true;
×
316
  return 0;
×
317
}
318

319
static int32_t metaFilterTableByHash(SMeta *pMeta, SArray *uidList) {
×
320
  int32_t code = 0;
×
321
  // 1, tranverse table's
322
  // 2, validate table name using vnodeValidateTableHash
323
  // 3, push invalidated table's uid into uidList
324

325
  TBC *pCur;
326
  code = tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
×
327
  if (code < 0) {
×
328
    return code;
×
329
  }
330

331
  code = tdbTbcMoveToFirst(pCur);
×
332
  if (code) {
×
333
    tdbTbcClose(pCur);
×
334
    return code;
×
335
  }
336

337
  void *pData = NULL, *pKey = NULL;
×
338
  int   nData = 0, nKey = 0;
×
339

340
  while (1) {
×
341
    int32_t ret = tdbTbcNext(pCur, &pKey, &nKey, &pData, &nData);
×
342
    if (ret < 0) {
×
343
      break;
×
344
    }
345

346
    SMetaEntry me = {0};
×
347
    SDecoder   dc = {0};
×
348
    tDecoderInit(&dc, pData, nData);
×
349
    code = metaDecodeEntry(&dc, &me);
×
350
    if (code < 0) {
×
351
      tDecoderClear(&dc);
×
352
      return code;
×
353
    }
354

355
    if (me.type != TSDB_SUPER_TABLE) {
×
356
      char tbFName[TSDB_TABLE_FNAME_LEN + 1];
357
      snprintf(tbFName, sizeof(tbFName), "%s.%s", pMeta->pVnode->config.dbname, me.name);
×
358
      tbFName[TSDB_TABLE_FNAME_LEN] = '\0';
×
359
      int32_t ret = vnodeValidateTableHash(pMeta->pVnode, tbFName);
×
360
      if (ret < 0 && terrno == TSDB_CODE_VND_HASH_MISMATCH) {
×
361
        if (taosArrayPush(uidList, &me.uid) == NULL) {
×
362
          code = terrno;
×
363
          break;
×
364
        }
365
      }
366
    }
367
    tDecoderClear(&dc);
×
368
  }
369
  tdbFree(pData);
×
370
  tdbFree(pKey);
×
371
  tdbTbcClose(pCur);
×
372

373
  return 0;
×
374
}
375

376
int32_t metaTrimTables(SMeta *pMeta, int64_t version) {
×
377
  int32_t code = 0;
×
378

379
  SArray *tbUids = taosArrayInit(8, sizeof(int64_t));
×
380
  if (tbUids == NULL) {
×
381
    return terrno;
×
382
  }
383

384
  code = metaFilterTableByHash(pMeta, tbUids);
×
385
  if (code != 0) {
×
386
    goto end;
×
387
  }
388
  if (TARRAY_SIZE(tbUids) == 0) {
×
389
    goto end;
×
390
  }
391

392
  metaInfo("vgId:%d, trim %ld tables", TD_VID(pMeta->pVnode), taosArrayGetSize(tbUids));
×
393
  code = metaDropTables(pMeta, tbUids);
×
394
  if (code) goto end;
×
395

396
end:
×
397
  taosArrayDestroy(tbUids);
×
398

399
  return code;
×
400
}
401

402
int metaTtlFindExpired(SMeta *pMeta, int64_t timePointMs, SArray *tbUids, int32_t ttlDropMaxCount) {
404✔
403
  metaRLock(pMeta);
404✔
404

405
  int ret = ttlMgrFindExpired(pMeta->pTtlMgr, timePointMs, tbUids, ttlDropMaxCount);
404✔
406

407
  metaULock(pMeta);
404✔
408

409
  if (ret != 0) {
404!
410
    metaError("ttl failed to find expired table, ret:%d", ret);
×
411
  }
412

413
  return ret;
404✔
414
}
415

416
static int metaBuildBtimeIdxKey(SBtimeIdxKey *btimeKey, const SMetaEntry *pME) {
×
417
  int64_t btime;
418
  if (pME->type == TSDB_CHILD_TABLE) {
×
419
    btime = pME->ctbEntry.btime;
×
420
  } else if (pME->type == TSDB_NORMAL_TABLE) {
×
421
    btime = pME->ntbEntry.btime;
×
422
  } else {
423
    return TSDB_CODE_FAILED;
×
424
  }
425

426
  btimeKey->btime = btime;
×
427
  btimeKey->uid = pME->uid;
×
428
  return 0;
×
429
}
430

431
static int metaBuildNColIdxKey(SNcolIdxKey *ncolKey, const SMetaEntry *pME) {
×
432
  if (pME->type == TSDB_NORMAL_TABLE) {
×
433
    ncolKey->ncol = pME->ntbEntry.schemaRow.nCols;
×
434
    ncolKey->uid = pME->uid;
×
435
  } else {
436
    return TSDB_CODE_FAILED;
×
437
  }
438
  return 0;
×
439
}
440

441
static void metaDeleteTtl(SMeta *pMeta, const SMetaEntry *pME) {
×
442
  if (pME->type != TSDB_CHILD_TABLE && pME->type != TSDB_NORMAL_TABLE) return;
×
443

444
  STtlDelTtlCtx ctx = {.uid = pME->uid, .pTxn = pMeta->txn};
×
445
  if (pME->type == TSDB_CHILD_TABLE) {
×
446
    ctx.ttlDays = pME->ctbEntry.ttlDays;
×
447
  } else {
448
    ctx.ttlDays = pME->ntbEntry.ttlDays;
×
449
  }
450

451
  int32_t ret = ttlMgrDeleteTtl(pMeta->pTtlMgr, &ctx);
×
452
  if (ret < 0) {
×
453
    metaError("vgId:%d, failed to delete ttl for table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), pME->name,
×
454
              pME->uid, tstrerror(ret));
455
  }
456
  return;
×
457
}
458

459
static int metaDropTableByUid(SMeta *pMeta, tb_uid_t uid, int *type, tb_uid_t *pSuid, int8_t *pSysTbl) {
×
460
  void      *pData = NULL;
×
461
  int        nData = 0;
×
462
  int        rc = 0;
×
463
  SMetaEntry e = {0};
×
464
  SDecoder   dc = {0};
×
465
  int32_t    ret = 0;
×
466

467
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
×
468
  if (rc < 0) {
×
469
    return rc;
×
470
  }
471
  int64_t version = ((SUidIdxVal *)pData)[0].version;
×
472

473
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
×
474
  if (rc < 0) {
×
475
    tdbFree(pData);
×
476
    return rc;
×
477
  }
478

479
  tDecoderInit(&dc, pData, nData);
×
480
  rc = metaDecodeEntry(&dc, &e);
×
481
  if (rc < 0) {
×
482
    tDecoderClear(&dc);
×
483
    return rc;
×
484
  }
485

486
  if (type) *type = e.type;
×
487

488
  if (e.type == TSDB_CHILD_TABLE) {
×
489
    if (pSuid) *pSuid = e.ctbEntry.suid;
×
490
    void *tData = NULL;
×
491
    int   tLen = 0;
×
492

493
    if (tdbTbGet(pMeta->pUidIdx, &e.ctbEntry.suid, sizeof(tb_uid_t), &tData, &tLen) == 0) {
×
494
      STbDbKey tbDbKey = {.uid = e.ctbEntry.suid, .version = ((SUidIdxVal *)tData)[0].version};
×
495
      if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &tData, &tLen) == 0) {
×
496
        SDecoder   tdc = {0};
×
497
        SMetaEntry stbEntry = {0};
×
498

499
        tDecoderInit(&tdc, tData, tLen);
×
500
        int32_t ret = metaDecodeEntry(&tdc, &stbEntry);
×
501
        if (ret < 0) {
×
502
          tDecoderClear(&tdc);
×
503
          metaError("vgId:%d, failed to decode child table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
504
                    e.ctbEntry.suid, tstrerror(ret));
505
          return ret;
×
506
        }
507

508
        if (pSysTbl) *pSysTbl = metaTbInFilterCache(pMeta, stbEntry.name, 1) ? 1 : 0;
×
509

510
        SSchema        *pTagColumn = NULL;
×
511
        SSchemaWrapper *pTagSchema = &stbEntry.stbEntry.schemaTag;
×
512
        if (pTagSchema->nCols == 1 && pTagSchema->pSchema[0].type == TSDB_DATA_TYPE_JSON) {
×
513
          pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[0];
×
514
          ret = metaDelJsonVarFromIdx(pMeta, &e, pTagColumn);
×
515
          if (ret < 0) {
×
516
            metaError("vgId:%d, failed to delete json var from idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
517
                      e.name, e.uid, tstrerror(ret));
518
          }
519
        } else {
520
          for (int i = 0; i < pTagSchema->nCols; i++) {
×
521
            pTagColumn = &stbEntry.stbEntry.schemaTag.pSchema[i];
×
522
            if (!IS_IDX_ON(pTagColumn)) continue;
×
523
            STagIdxKey *pTagIdxKey = NULL;
×
524
            int32_t     nTagIdxKey;
525

526
            const void *pTagData = NULL;
×
527
            int32_t     nTagData = 0;
×
528

529
            STagVal tagVal = {.cid = pTagColumn->colId};
×
530
            if (tTagGet((const STag *)e.ctbEntry.pTags, &tagVal)) {
×
531
              if (IS_VAR_DATA_TYPE(pTagColumn->type)) {
×
532
                pTagData = tagVal.pData;
×
533
                nTagData = (int32_t)tagVal.nData;
×
534
              } else {
535
                pTagData = &(tagVal.i64);
×
536
                nTagData = tDataTypes[pTagColumn->type].bytes;
×
537
              }
538
            } else {
539
              if (!IS_VAR_DATA_TYPE(pTagColumn->type)) {
×
540
                nTagData = tDataTypes[pTagColumn->type].bytes;
×
541
              }
542
            }
543

544
            if (metaCreateTagIdxKey(e.ctbEntry.suid, pTagColumn->colId, pTagData, nTagData, pTagColumn->type, uid,
×
545
                                    &pTagIdxKey, &nTagIdxKey) == 0) {
546
              ret = tdbTbDelete(pMeta->pTagIdx, pTagIdxKey, nTagIdxKey, pMeta->txn);
×
547
              if (ret < 0) {
×
548
                metaError("vgId:%d, failed to delete tag idx key:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode),
×
549
                          e.name, e.uid, tstrerror(ret));
550
              }
551
            }
552
            metaDestroyTagIdxKey(pTagIdxKey);
×
553
            pTagIdxKey = NULL;
×
554
          }
555
        }
556
        tDecoderClear(&tdc);
×
557
      }
558
      tdbFree(tData);
×
559
    }
560
  }
561

562
  ret = tdbTbDelete(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), pMeta->txn);
×
563
  if (ret < 0) {
×
564
    metaError("vgId:%d, failed to delete table:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
565
              tstrerror(ret));
566
  }
567
  ret = tdbTbDelete(pMeta->pNameIdx, e.name, strlen(e.name) + 1, pMeta->txn);
×
568
  if (ret < 0) {
×
569
    metaError("vgId:%d, failed to delete name idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
570
              tstrerror(ret));
571
  }
572
  ret = tdbTbDelete(pMeta->pUidIdx, &uid, sizeof(uid), pMeta->txn);
×
573
  if (ret < 0) {
×
574
    metaError("vgId:%d, failed to delete uid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
575
              tstrerror(ret));
576
  }
577

578
  if (e.type == TSDB_CHILD_TABLE || e.type == TSDB_NORMAL_TABLE) metaDeleteBtimeIdx(pMeta, &e);
×
579
  if (e.type == TSDB_NORMAL_TABLE) metaDeleteNcolIdx(pMeta, &e);
×
580

581
  if (e.type != TSDB_SUPER_TABLE) metaDeleteTtl(pMeta, &e);
×
582

583
  if (e.type == TSDB_CHILD_TABLE) {
×
584
    ret =
585
        tdbTbDelete(pMeta->pCtbIdx, &(SCtbIdxKey){.suid = e.ctbEntry.suid, .uid = uid}, sizeof(SCtbIdxKey), pMeta->txn);
×
586
    if (ret < 0) {
×
587
      metaError("vgId:%d, failed to delete ctb idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
588
                tstrerror(ret));
589
    }
590

591
    --pMeta->pVnode->config.vndStats.numOfCTables;
×
NEW
592
    metaUpdateStbStats(pMeta, e.ctbEntry.suid, -1, 0, -1);
×
593
    ret = metaUidCacheClear(pMeta, e.ctbEntry.suid);
×
594
    if (ret < 0) {
×
595
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
596
                e.ctbEntry.suid, tstrerror(ret));
597
    }
598
    ret = metaTbGroupCacheClear(pMeta, e.ctbEntry.suid);
×
599
    if (ret < 0) {
×
600
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
601
                e.ctbEntry.suid, tstrerror(ret));
602
    }
603
    /*
604
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
605
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, e.ctbEntry.suid, NULL);
606
    }
607
    */
608
  } else if (e.type == TSDB_NORMAL_TABLE) {
×
609
    // drop schema.db (todo)
610

611
    --pMeta->pVnode->config.vndStats.numOfNTables;
×
612
    pMeta->pVnode->config.vndStats.numOfNTimeSeries -= e.ntbEntry.schemaRow.nCols - 1;
×
613

614
    /*
615
    if (!TSDB_CACHE_NO(pMeta->pVnode->config)) {
616
      tsdbCacheDropTable(pMeta->pVnode->pTsdb, e.uid, -1, &e.ntbEntry.schemaRow);
617
    }
618
    */
619
  } else if (e.type == TSDB_SUPER_TABLE) {
×
620
    ret = tdbTbDelete(pMeta->pSuidIdx, &e.uid, sizeof(tb_uid_t), pMeta->txn);
×
621
    if (ret < 0) {
×
622
      metaError("vgId:%d, failed to delete suid idx:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
623
                tstrerror(ret));
624
    }
625
    // drop schema.db (todo)
626

627
    ret = metaStatsCacheDrop(pMeta, uid);
×
628
    if (ret < 0) {
×
629
      metaError("vgId:%d, failed to drop stats cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
630
                tstrerror(ret));
631
    }
632
    ret = metaUidCacheClear(pMeta, uid);
×
633
    if (ret < 0) {
×
634
      metaError("vgId:%d, failed to clear uid cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
635
                tstrerror(ret));
636
    }
637
    ret = metaTbGroupCacheClear(pMeta, uid);
×
638
    if (ret < 0) {
×
639
      metaError("vgId:%d, failed to clear group cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name,
×
640
                e.uid, tstrerror(ret));
641
    }
642
    --pMeta->pVnode->config.vndStats.numOfSTables;
×
643
  }
644

645
  ret = metaCacheDrop(pMeta, uid);
×
646
  if (ret < 0) {
×
647
    metaError("vgId:%d, failed to drop cache:%s uid:%" PRId64 " since %s", TD_VID(pMeta->pVnode), e.name, e.uid,
×
648
              tstrerror(ret));
649
  }
650

651
  tDecoderClear(&dc);
×
652
  tdbFree(pData);
×
653

654
  return 0;
×
655
}
656

657
static int metaDeleteBtimeIdx(SMeta *pMeta, const SMetaEntry *pME) {
×
658
  SBtimeIdxKey btimeKey = {0};
×
659
  if (metaBuildBtimeIdxKey(&btimeKey, pME) < 0) {
×
660
    return 0;
×
661
  }
662
  return tdbTbDelete(pMeta->pBtimeIdx, &btimeKey, sizeof(btimeKey), pMeta->txn);
×
663
}
664

665
int metaDeleteNcolIdx(SMeta *pMeta, const SMetaEntry *pME) {
×
666
  SNcolIdxKey ncolKey = {0};
×
667
  if (metaBuildNColIdxKey(&ncolKey, pME) < 0) {
×
668
    return 0;
×
669
  }
670
  return tdbTbDelete(pMeta->pNcolIdx, &ncolKey, sizeof(ncolKey), pMeta->txn);
×
671
}
672

673
int metaAlterTable(SMeta *pMeta, int64_t version, SVAlterTbReq *pReq, STableMetaRsp *pMetaRsp) {
×
674
  pMeta->changed = true;
×
675
  switch (pReq->action) {
×
676
    case TSDB_ALTER_TABLE_ADD_COLUMN:
×
677
    case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION:
678
      return metaAddTableColumn(pMeta, version, pReq, pMetaRsp);
×
679
    case TSDB_ALTER_TABLE_DROP_COLUMN:
×
680
      return metaDropTableColumn(pMeta, version, pReq, pMetaRsp);
×
681
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
×
682
      return metaAlterTableColumnBytes(pMeta, version, pReq, pMetaRsp);
×
683
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
×
684
      return metaAlterTableColumnName(pMeta, version, pReq, pMetaRsp);
×
685
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
×
686
      return metaUpdateTableTagValue(pMeta, version, pReq);
×
687
    case TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL:
×
688
      return metaUpdateTableMultiTagValue(pMeta, version, pReq);
×
689
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
×
690
      return metaUpdateTableOptions2(pMeta, version, pReq);
×
691
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS:
×
692
      return metaUpdateTableColCompress2(pMeta, version, pReq);
×
693
    default:
×
694
      return terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION;
×
695
      break;
696
  }
697
}
698

699
static int metaUpdateChangeTime(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
×
700
  if (!tsTtlChangeOnWrite) return 0;
×
701

702
  if (changeTimeMs <= 0) {
×
703
    metaWarn("Skip to change ttl deletetion time on write, uid: %" PRId64, uid);
×
704
    return TSDB_CODE_VERSION_NOT_COMPATIBLE;
×
705
  }
706

707
  STtlUpdCtimeCtx ctx = {.uid = uid, .changeTimeMs = changeTimeMs, .pTxn = pMeta->txn};
×
708

709
  return ttlMgrUpdateChangeTime(pMeta->pTtlMgr, &ctx);
×
710
}
711

712
int metaUpdateChangeTimeWithLock(SMeta *pMeta, tb_uid_t uid, int64_t changeTimeMs) {
435,813✔
713
  if (!tsTtlChangeOnWrite) return 0;
435,813!
714

715
  metaWLock(pMeta);
×
716
  int ret = metaUpdateChangeTime(pMeta, uid, changeTimeMs);
×
717
  metaULock(pMeta);
×
718
  return ret;
×
719
}
720

721
int metaCreateTagIdxKey(tb_uid_t suid, int32_t cid, const void *pTagData, int32_t nTagData, int8_t type, tb_uid_t uid,
21,068✔
722
                        STagIdxKey **ppTagIdxKey, int32_t *nTagIdxKey) {
723
  if (IS_VAR_DATA_TYPE(type)) {
21,068!
724
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + VARSTR_HEADER_SIZE + sizeof(tb_uid_t);
4✔
725
  } else {
726
    *nTagIdxKey = sizeof(STagIdxKey) + nTagData + sizeof(tb_uid_t);
21,064✔
727
  }
728

729
  *ppTagIdxKey = (STagIdxKey *)taosMemoryMalloc(*nTagIdxKey);
21,068!
730
  if (*ppTagIdxKey == NULL) {
21,068!
731
    return terrno;
×
732
  }
733

734
  taosSetInt64Aligned(&((*ppTagIdxKey)->suid), suid);
21,068✔
735
  (*ppTagIdxKey)->cid = cid;
21,068✔
736
  (*ppTagIdxKey)->isNull = (pTagData == NULL) ? 1 : 0;
21,068✔
737
  (*ppTagIdxKey)->type = type;
21,068✔
738

739
  // refactor
740
  if (IS_VAR_DATA_TYPE(type)) {
21,068!
741
    memcpy((*ppTagIdxKey)->data, (uint16_t *)&nTagData, VARSTR_HEADER_SIZE);
4✔
742
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE, pTagData, nTagData);
4!
743
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + VARSTR_HEADER_SIZE + nTagData), uid);
4✔
744
  } else {
745
    if (pTagData != NULL) memcpy((*ppTagIdxKey)->data, pTagData, nTagData);
21,064!
746
    taosSetInt64Aligned((tb_uid_t *)((*ppTagIdxKey)->data + nTagData), uid);
21,064✔
747
  }
748

749
  return 0;
21,068✔
750
}
751

752
void metaDestroyTagIdxKey(STagIdxKey *pTagIdxKey) {
21,068✔
753
  if (pTagIdxKey) taosMemoryFree(pTagIdxKey);
21,068!
754
}
21,068✔
755

756
static void colCompressDebug(SHashObj *pColCmprObj) {
151✔
757
  void *p = taosHashIterate(pColCmprObj, NULL);
151✔
758
  while (p) {
1,182✔
759
    uint32_t cmprAlg = *(uint32_t *)p;
1,031✔
760
    col_id_t colId = *(col_id_t *)taosHashGetKey(p, NULL);
1,031✔
761
    p = taosHashIterate(pColCmprObj, p);
1,031✔
762

763
    uint8_t l1, l2, lvl;
764
    tcompressDebug(cmprAlg, &l1, &l2, &lvl);
1,031✔
765

766
    const char *l1str = columnEncodeStr(l1);
1,031✔
767
    const char *l2str = columnCompressStr(l2);
1,031✔
768
    const char *lvlstr = columnLevelStr(lvl);
1,031✔
769
    metaDebug("colId: %d, encode:%s, compress:%s,level:%s", colId, l1str, l2str, lvlstr);
1,031✔
770
  }
771
  return;
151✔
772
}
773

774
int32_t metaGetColCmpr(SMeta *pMeta, tb_uid_t uid, SHashObj **ppColCmprObj) {
151✔
775
  int rc = 0;
151✔
776

777
  SHashObj *pColCmprObj = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
151✔
778
  if (pColCmprObj == NULL) {
151!
779
    pColCmprObj = NULL;
×
780
    return TSDB_CODE_OUT_OF_MEMORY;
×
781
  }
782

783
  void      *pData = NULL;
151✔
784
  int        nData = 0;
151✔
785
  SMetaEntry e = {0};
151✔
786
  SDecoder   dc = {0};
151✔
787

788
  *ppColCmprObj = NULL;
151✔
789

790
  metaRLock(pMeta);
151✔
791
  rc = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
151✔
792
  if (rc < 0) {
151!
793
    taosHashClear(pColCmprObj);
×
794
    metaULock(pMeta);
×
795
    return TSDB_CODE_FAILED;
×
796
  }
797
  int64_t version = ((SUidIdxVal *)pData)[0].version;
151✔
798
  rc = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.version = version, .uid = uid}, sizeof(STbDbKey), &pData, &nData);
151✔
799
  if (rc < 0) {
151!
800
    metaULock(pMeta);
×
801
    taosHashClear(pColCmprObj);
×
802
    metaError("failed to get table entry");
×
803
    return rc;
×
804
  }
805

806
  tDecoderInit(&dc, pData, nData);
151✔
807
  rc = metaDecodeEntry(&dc, &e);
151✔
808
  if (rc < 0) {
150!
809
    tDecoderClear(&dc);
×
810
    tdbFree(pData);
×
811
    metaULock(pMeta);
×
812
    taosHashClear(pColCmprObj);
×
813
    return rc;
×
814
  }
815
  if (useCompress(e.type)) {
150!
816
    SColCmprWrapper *p = &e.colCmpr;
151✔
817
    for (int32_t i = 0; i < p->nCols; i++) {
1,181✔
818
      SColCmpr *pCmpr = &p->pColCmpr[i];
1,030✔
819
      rc = taosHashPut(pColCmprObj, &pCmpr->id, sizeof(pCmpr->id), &pCmpr->alg, sizeof(pCmpr->alg));
1,030✔
820
      if (rc < 0) {
1,030!
821
        tDecoderClear(&dc);
×
822
        tdbFree(pData);
×
823
        metaULock(pMeta);
×
824
        taosHashClear(pColCmprObj);
×
825
        return rc;
×
826
      }
827
    }
828
  } else {
829
    tDecoderClear(&dc);
×
830
    tdbFree(pData);
×
831
    metaULock(pMeta);
×
832
    taosHashClear(pColCmprObj);
×
833
    return 0;
×
834
  }
835
  tDecoderClear(&dc);
151✔
836
  tdbFree(pData);
151✔
837
  metaULock(pMeta);
151✔
838

839
  *ppColCmprObj = pColCmprObj;
151✔
840
  colCompressDebug(pColCmprObj);
151✔
841

842
  return 0;
151✔
843
}
844
// refactor later
845
void *metaGetIdx(SMeta *pMeta) { return pMeta->pTagIdx; }
203✔
846
void *metaGetIvtIdx(SMeta *pMeta) { return pMeta->pTagIvtIdx; }
202✔
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

© 2026 Coveralls, Inc