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

taosdata / TDengine / #5138

05 Nov 2026 03:32AM UTC coverage: 73.942% (-0.3%) from 74.268%
#5138

push

travis-ci

jbjia
fix: capture coverage once from per-thread gcda

Per-case lcov walked the shared build tree and kept each case busy for minutes. Pass GCOV_PREFIX into the test container so each thread writes its own counters, then merge and capture after the suite.

373256 of 504798 relevant lines covered (73.94%)

4305711.12 hits per line

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

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

18
#include "meta.h"
19
#include "osAtomic.h"
20
#include "osTime.h"
21
#include "tencode.h"
22
#include "thash.h"
23
#include "types.h"
24

25
void _metaReaderInit(SMetaReader *pReader, void *pVnode, int32_t flags, SStoreMeta *pAPI, int64_t txnId) {
5,173,508 ✔
26
  SMeta *pMeta = ((SVnode *)pVnode)->pMeta;
5,173,508 ✔
27
  metaReaderDoInit(pReader, pMeta, flags, txnId);
5,173,508 ✔
28
  pReader->pAPI = pAPI;
5,176,986 ✔
29
}
5,176,986 ✔
30

31
void metaReaderDoInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags, int64_t txnId) {
6,665,851 ✔
32
  memset(pReader, 0, sizeof(*pReader));
6,665,851 ✔
33
  pReader->pMeta = pMeta;
6,665,851 ✔
34
  pReader->flags = flags;
6,665,851 ✔
35
  pReader->txnId = txnId;
6,665,851 ✔
36
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
6,665,851 ✔
37
    metaRLock(pMeta);
5,688,332 ✔
38
  }
39
}
6,665,150 ✔
40

41
void metaReaderReleaseLock(SMetaReader *pReader) {
545,925 ✔
42
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
545,925 ✔
43
    metaULock(pReader->pMeta);
546,027 ✔
44
    pReader->flags |= META_READER_NOLOCK;
546,023 ✔
45
  }
46
}
545,921 ✔
47

48
void metaReaderClear(SMetaReader *pReader) {
7,472,658 ✔
49
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
7,472,658 ✔
50
    metaULock(pReader->pMeta);
5,141,222 ✔
51
  }
52
  tFreeSSeriesWrapper(&pReader->me.series);
7,472,185 ✔
53
  tDecoderClear(&pReader->coder);
7,474,443 ✔
54
  tdbFree(pReader->pBuf);
7,474,219 ✔
55
  pReader->pBuf = NULL;
7,474,436 ✔
56
}
7,474,436 ✔
57

58
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
9,167,448 ✔
59
  int32_t  code = 0;
9,167,448 ✔
60
  SMeta   *pMeta = pReader->pMeta;
9,167,448 ✔
61
  STbDbKey tbDbKey = {.version = version, .uid = uid};
9,167,448 ✔
62

63
  // query table.db
64
  if ((code = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf)) < 0) {
9,169,303 ✔
65
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
×
66
  }
67

68
  // decode the entry
69
  tFreeSSeriesWrapper(&pReader->me.series);
9,172,578 ✔
70
  // The bit-5 tag trailer is optional: a record without it must leave the tag fields
71
  // zeroed. Otherwise the reader keeps stale decoder-pool pointers from the previously
72
  // decoded entry, and cursor reuse shows phantom tags on tag-less tables.
73
  pReader->me.ntbEntry.schemaTag.nCols = 0;
9,170,731 ✔
74
  pReader->me.ntbEntry.schemaTag.version = 0;
9,170,731 ✔
75
  pReader->me.ntbEntry.schemaTag.pSchema = NULL;
9,170,731 ✔
76
  pReader->me.ntbEntry.pTags = NULL;
9,170,731 ✔
77
  tDecoderClear(&pReader->coder);
9,170,731 ✔
78
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
9,170,208 ✔
79

80
  code = metaDecodeEntry(&pReader->coder, &pReader->me);
9,166,838 ✔
81
  if (code) {
9,170,220 ✔
82
    tDecoderClear(&pReader->coder);
×
83
    return code;
×
84
  }
85
  // taosMemoryFreeClear(pReader->me.colCmpr.pColCmpr);
86

87
  return 0;
8,990,119 ✔
88
}
89

90
bool metaIsTableExist(void *pVnode, tb_uid_t uid) {
264,415 ✔
91
  SVnode *pVnodeObj = pVnode;
264,415 ✔
92
  metaRLock(pVnodeObj->pMeta);  // query uid.idx
264,415 ✔
93

94
  if (tdbTbGet(pVnodeObj->pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
264,566 ✔
95
    metaULock(pVnodeObj->pMeta);
89 ✔
96
    return false;
89 ✔
97
  }
98

99
  // batch meta txn: check txn.idx for PRE_CREATE entries (invisible to external readers)
100
  if (metaHasPendingTxnEntries(pVnodeObj->pMeta)) {
528,891 ✔
101
    void   *pTxnVal = NULL;
85 ✔
102
    int32_t txnValLen = 0;
85 ✔
103
    if (tdbTbGet(pVnodeObj->pMeta->pTxnIdx, &uid, sizeof(uid), &pTxnVal, &txnValLen) == 0) {
85 ✔
104
      STxnIdxVal txnVal = *(const STxnIdxVal *)pTxnVal;
77 ✔
105
      if (txnVal.txnStatus == META_TXN_PRE_CREATE) {
77 ✔
106
        // Check if txn is committed  ->  table exists
107
        int8_t finalStatus = metaGetTxnMetaStatus(pVnodeObj->pMeta, txnVal.txnId);
1 ✔
108
        if (finalStatus != TXN_META_COMMITTED) {
1 ✔
109
          tdbFree(pTxnVal);
1 ✔
110
          metaULock(pVnodeObj->pMeta);
1 ✔
111
          return false;
1 ✔
112
        }
113
      }
114
      // Check if txn is committed + PRE_DROP  ->  table doesn't exist
115
      if (txnVal.txnStatus == META_TXN_PRE_DROP) {
76 ✔
116
        int8_t finalStatus = metaGetTxnMetaStatus(pVnodeObj->pMeta, txnVal.txnId);
6 ✔
117
        if (finalStatus == TXN_META_COMMITTED) {
6 ✔
118
          tdbFree(pTxnVal);
×
119
          metaULock(pVnodeObj->pMeta);
×
120
          return false;
×
121
        }
122
      }
123
      tdbFree(pTxnVal);
76 ✔
124
    }
125
  }  // metaHasPendingTxnEntries
126

127
  metaULock(pVnodeObj->pMeta);
264,537 ✔
128
  return true;
264,524 ✔
129
}
130

131

132

133
int metaReaderGetTableEntryByUid(SMetaReader *pReader, tb_uid_t uid) {
4,921,955 ✔
134
  SMeta  *pMeta = pReader->pMeta;
4,921,955 ✔
135
  int64_t version1;
116,481 ✔
136

137
  // query uid.idx
138
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pReader->pBuf, &pReader->szBuf) < 0) {
4,921,955 ✔
139
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
804 ✔
140
  }
141

142
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
4,923,593 ✔
143
  int code = metaGetTableEntryByVersion(pReader, version1, uid);
4,923,593 ✔
144
  if (code) return code;
4,923,126 ✔
145

146
  // batch meta txn: finalized txn visibility adjustment
147
  if (pReader->me.txnId != 0) {
4,923,126 ✔
148
    int8_t finalStatus = metaGetTxnMetaStatus(pReader->pMeta, pReader->me.txnId);
766 ✔
149
    if (finalStatus == TXN_META_COMMITTED) {
766 ✔
150
      // COMMITTED: PRE_CREATE/PRE_ALTER  ->  visible as normal; PRE_DROP  ->  invisible (deleted)
151
      if (pReader->me.txnStatus == META_TXN_PRE_DROP) {
×
152
        return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
153
      }
154
      // PRE_CREATE and PRE_ALTER: fall through (visible, use current version)
155
      return 0;
×
156
    }
157
  }
158

159
  // batch meta txn: PRE_CREATE entries visibility:
160
  //   - ROLLEDBACK txn: always invisible
161
  //   - other-txn reader OR non-txn reader (txnId == 0): invisible (snapshot isolation)
162
  //   - same-txn reader: visible (RYOW  -  read your own writes)
163
  if (pReader->me.txnStatus == META_TXN_PRE_CREATE) {
4,923,126 ✔
164
    int8_t finalStatus = metaGetTxnMetaStatus(pReader->pMeta, pReader->me.txnId);
435 ✔
165
    if (finalStatus == TXN_META_ROLLEDBACK) {
435 ✔
166
      return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
167
    }
168
    if (pReader->txnId == 0 || pReader->txnId != pReader->me.txnId) {
435 ✔
169
      return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
103 ✔
170
    }
171
  }
172

173
  // batch meta txn: PRE_ALTER entries  -  redirect to old version for snapshot isolation
174
  // Same-txn readers see the new (altered) version directly
175
  if (pReader->me.txnStatus == META_TXN_PRE_ALTER && pReader->me.txnOrigVer > 0) {
4,923,023 ✔
176
    if (pReader->txnId == 0 || pReader->txnId != pReader->me.txnId) {
137 ✔
177
      int64_t prevVer = pReader->me.txnOrigVer;
735 ✔
178
      tDecoderClear(&pReader->coder);
735 ✔
179
      code = metaGetTableEntryByVersion(pReader, prevVer, uid);
98 ✔
180
      if (code) return code;
98 ✔
181
    }
182
  }
183

184
  return 0;
4,805,909 ✔
185
}
186

187
static int32_t getUidVersion(SMetaReader *pReader, int64_t *version, tb_uid_t uid) {
89 ✔
188
  int32_t code = 0;
89 ✔
189
  SMeta *pMeta = pReader->pMeta;
89 ✔
190
  void* pKey = NULL;
89 ✔
191
  int   kLen = 0;
89 ✔
192

193
  TBC* pCur = NULL;
89 ✔
194
  code = tdbTbcOpen(pMeta->pTbDb, (TBC**)&pCur, NULL);
89 ✔
195
  if (code != 0) {
89 ✔
196
    return TAOS_GET_TERRNO(code);
×
197
  }
198
  STbDbKey key = {.version = *version, .uid = INT64_MAX};
89 ✔
199
  int      c = 0;
89 ✔
200
  code = tdbTbcMoveTo(pCur, &key, sizeof(key), &c);
89 ✔
201
  if (code != 0) {
89 ✔
202
    goto END;
×
203
  }
204

205
  void   *tKey = NULL;
89 ✔
206
  code = tdbTbcGet(pCur, (const void**)&tKey, &kLen, NULL, NULL);
89 ✔
207
  if (code != 0) {
89 ✔
208
    goto END;
×
209
  }
210
  STbDbKey* tmp = (STbDbKey*)tKey;
89 ✔
211
  if (tmp->uid == uid && tmp->version <= *version) {
89 ✔
212
    *version = tmp->version;
36 ✔
213
    goto END;
36 ✔
214
  }
215
  
216
  while (1) {
94 ✔
217
    int32_t ret = tdbTbcPrev(pCur, &pKey, &kLen, NULL, NULL);
147 ✔
218
    if (ret < 0) break;
147 ✔
219

220
    STbDbKey* tmp = (STbDbKey*)pKey;
147 ✔
221
    if (tmp->uid == uid && tmp->version <= *version) {
147 ✔
222
      *version = tmp->version;
53 ✔
223
      goto END;
53 ✔
224
    }
225
  }
226
  code = TSDB_CODE_NOT_FOUND;
×
227
  metaError("%s uid:%" PRId64 " version:%" PRId64 " not found", __func__, uid, *version);
×
228
END:
89 ✔
229
  tdbFree(pKey);
89 ✔
230
  tdbTbcClose(pCur);
89 ✔
231
  return code;
89 ✔
232
} 
233

234
// get table entry according to the latest version number that is less than or equal to version and uid, if version < 0, get latest version
235
int metaReaderGetTableEntryByVersionUid(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
3,041,489 ✔
236
  if (version < 0) {
3,041,489 ✔
237
    return metaReaderGetTableEntryByUid(pReader, uid);
3,041,603 ✔
238
  }
239
  SMeta *pMeta = pReader->pMeta;
×
240

241
  SMetaInfo info;
×
242
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
×
243
  if (TSDB_CODE_SUCCESS != code) {
×
244
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
×
245
  }
246
  if (info.version <= version) {
×
247
    version = info.version;
×
248
  } else {
249
    if (getUidVersion(pReader, &version, uid) != 0) {
×
250
      version = -1;
×
251
    }
252
  }
253
  code = metaGetTableEntryByVersion(pReader, version, uid);
×
254
  if (code) return code;
×
255

256
  // batch meta txn: finalized txn visibility adjustment
257
  if (pReader->me.txnId != 0) {
×
258
    int8_t finalStatus = metaGetTxnMetaStatus(pReader->pMeta, pReader->me.txnId);
×
259
    if (finalStatus == TXN_META_COMMITTED) {
×
260
      if (pReader->me.txnStatus == META_TXN_PRE_DROP) {
×
261
        return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
262
      }
263
      return 0;
×
264
    }
265
  }
266

267
  // batch meta txn: PRE_CREATE entries are invisible unless same-txn reader
268
  if (pReader->me.txnStatus == META_TXN_PRE_CREATE) {
×
269
    if (pReader->txnId == 0 || pReader->txnId != pReader->me.txnId) {
×
270
      return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
271
    }
272
  }
273

274
  // batch meta txn: PRE_ALTER entries  -  redirect to old version for snapshot isolation
275
  // Same-txn readers see the new (altered) version directly
276
  if (pReader->me.txnStatus == META_TXN_PRE_ALTER && pReader->me.txnOrigVer > 0) {
×
277
    if (pReader->txnId == 0 || pReader->txnId != pReader->me.txnId) {
×
278
      int64_t prevVer = pReader->me.txnOrigVer;
×
279
      tDecoderClear(&pReader->coder);
×
280
      code = metaGetTableEntryByVersion(pReader, prevVer, uid);
×
281
      if (code) return code;
×
282
    }
283
  }
284

285
  return 0;
×
286
}
287

288
int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
1,683,772 ✔
289
  SMeta *pMeta = pReader->pMeta;
1,683,772 ✔
290

291
  SMetaInfo info;
1,683,772 ✔
292
  int32_t   code = metaGetInfo(pMeta, uid, &info, pReader);
1,683,930 ✔
293
  if (TSDB_CODE_SUCCESS != code) {
1,685,098 ✔
294
    return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code);
623 ✔
295
  }
296

297
  code = metaGetTableEntryByVersion(pReader, info.version, uid);
1,684,475 ✔
298
  if (code) return code;
1,682,674 ✔
299

300
  // batch meta txn: finalized txn visibility adjustment
301
  if (pReader->me.txnId != 0) {
1,682,674 ✔
302
    int8_t finalStatus = metaGetTxnMetaStatus(pReader->pMeta, pReader->me.txnId);
150 ✔
303
    if (finalStatus == TXN_META_COMMITTED) {
150 ✔
304
      if (pReader->me.txnStatus == META_TXN_PRE_DROP) {
×
305
        return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
306
      }
307
      return 0;
×
308
    }
309
  }
310

311
  if (pReader->me.txnStatus != META_TXN_NORMAL) {
1,682,674 ✔
312
    if (pReader->me.txnStatus == META_TXN_PRE_CREATE) {
150 ✔
313
      // batch meta txn: PRE_CREATE visibility:
314
      //   - ROLLEDBACK: invisible
315
      //   - other-txn reader OR non-txn reader (txnId==0): invisible (snapshot isolation)
316
      //   - same-txn reader: visible (RYOW  -  read your own writes)
317
      int8_t finalStatus = metaGetTxnMetaStatus(pReader->pMeta, pReader->me.txnId);
1 ✔
318
      if (finalStatus == TXN_META_ROLLEDBACK) {
1 ✔
319
        return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
320
      }
321
      if (pReader->txnId == 0 || pReader->txnId != pReader->me.txnId) {
1 ✔
322
        return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
323
      }
324
    } else if (pReader->me.txnStatus == META_TXN_PRE_ALTER && pReader->me.txnOrigVer > 0) {
149 ✔
325
      // batch meta txn: PRE_ALTER entries  -  redirect to old version for snapshot isolation
326
      // Same-txn readers see the new (altered) version directly
327
      if (pReader->txnId == 0 || pReader->txnId != pReader->me.txnId) {
139 ✔
328
        int64_t prevVer = pReader->me.txnOrigVer;
214 ✔
329
        tDecoderClear(&pReader->coder);
214 ✔
330
        code = metaGetTableEntryByVersion(pReader, prevVer, uid);
33 ✔
331
        if (code) return code;
33 ✔
332
      }
333
    }
334
  }
335

336
  return 0;
1,681,653 ✔
337
}
338

339
int metaGetTableEntryByVersionName(SMetaReader *pReader, int64_t version, const char *name) {
3,133,945 ✔
340
  SMeta   *pMeta = pReader->pMeta;
3,133,945 ✔
341
  tb_uid_t uid;
20,389 ✔
342

343
  // query name.idx
344
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
3,133,945 ✔
345
    return terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
93,180 ✔
346
  }
347

348
  uid = *(tb_uid_t *)pReader->pBuf;
3,040,477 ✔
349
  // PRE_CREATE filtering is handled inside metaReaderGetTableEntryByVersionUid
350
  return metaReaderGetTableEntryByVersionUid(pReader, version, uid);
3,040,477 ✔
351
}
352

353
int metaGetTableEntryByName(SMetaReader *pReader, const char *name) {
3,133,330 ✔
354
  return metaGetTableEntryByVersionName(pReader, -1, name);
3,133,330 ✔
355
}
356

357
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
304,005 ✔
358
  void    *pData = NULL;
304,005 ✔
359
  int      nData = 0;
304,010 ✔
360
  tb_uid_t uid = 0;
304,014 ✔
361

362
  metaRLock(pMeta);
304,014 ✔
363

364
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
304,057 ✔
365
    uid = *(tb_uid_t *)pData;
11,210 ✔
366
    tdbFree(pData);
11,210 ✔
367
  }
368

369
  metaULock(pMeta);
304,005 ✔
370

371
  return uid;
304,062 ✔
372
}
373

374
int metaGetTableNameByUid(void *pVnode, uint64_t uid, char *tbName, int64_t txnId) {
51,224 ✔
375
  int         code = 0;
51,224 ✔
376
  SMetaReader mr = {0};
51,224 ✔
377
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK, txnId);
51,224 ✔
378
  code = metaReaderGetTableEntryByUid(&mr, uid);
51,233 ✔
379
  if (code < 0) {
51,221 ✔
380
    metaReaderClear(&mr);
1 ✔
381
    return code;
1 ✔
382
  }
383

384
  STR_TO_VARSTR(tbName, mr.me.name);
51,220 ✔
385
  metaReaderClear(&mr);
51,220 ✔
386

387
  return 0;
51,233 ✔
388
}
389

390
int metaGetTableSzNameByUid(void *meta, uint64_t uid, char *tbName) {
×
391
  int         code = 0;
×
392
  SMetaReader mr = {0};
×
393
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK, 0);
×
394
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
395
  if (code < 0) {
×
396
    metaReaderClear(&mr);
×
397
    return code;
×
398
  }
399
  tstrncpy(tbName, mr.me.name, TSDB_TABLE_NAME_LEN);
×
400
  metaReaderClear(&mr);
×
401

402
  return 0;
×
403
}
404

405
int metaGetTableUidByName(void *pVnode, char *tbName, uint64_t *uid, int64_t txnId) {
319 ✔
406
  int         code = 0;
319 ✔
407
  SMetaReader mr = {0};
319 ✔
408
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK, txnId);
319 ✔
409

410
  SMetaReader *pReader = &mr;
319 ✔
411

412
  // query name.idx
413
  if (tdbTbGet(((SMeta *)pReader->pMeta)->pNameIdx, tbName, strlen(tbName) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
319 ✔
414
    metaReaderClear(&mr);
10 ✔
415
    return terrno = TSDB_CODE_TDB_TABLE_NOT_EXIST;
10 ✔
416
  }
417

418
  *uid = *(tb_uid_t *)pReader->pBuf;
309 ✔
419

420
  metaReaderClear(&mr);
309 ✔
421

422
  return 0;
309 ✔
423
}
424

425
int32_t metaGetTableUidsByNames(void *pVnode, const SArray *pTbNames, SArray *pUids, int64_t txnId) {
×
426
  int32_t            code = TSDB_CODE_SUCCESS;
×
427
  int32_t            numOfNames = 0;
×
428
  STdbTbBatchGetArg *pArgs = NULL;
×
429
  SMeta             *pMeta = ((SVnode *)pVnode)->pMeta;
×
430

431
  TAOS_UNUSED(txnId);
×
432
  if (NULL == pTbNames || NULL == pUids) {
×
433
    return TSDB_CODE_INVALID_PARA;
×
434
  }
435

436
  numOfNames = taosArrayGetSize(pTbNames);
×
437
  if (numOfNames == 0) {
×
438
    return TSDB_CODE_SUCCESS;
×
439
  }
440

441
  pArgs = taosMemoryCalloc(numOfNames, sizeof(*pArgs));
×
442
  if (NULL == pArgs) {
×
443
    return terrno;
×
444
  }
445

446
  for (int32_t i = 0; i < numOfNames; ++i) {
×
447
    char *name = taosArrayGetP(pTbNames, i);
×
448
    if (NULL == name) {
×
449
      code = TSDB_CODE_INVALID_PARA;
×
450
      goto _exit;
×
451
    }
452
    pArgs[i].pKey = name;
×
453
    pArgs[i].kLen = (int32_t)strlen(name) + 1;
×
454
  }
455

456
  metaRLock(pMeta);
×
457
  code = tdbTbGetBatch(pMeta->pNameIdx, pArgs, numOfNames);
×
458
  metaULock(pMeta);
×
459
  if (code != TSDB_CODE_SUCCESS) {
×
460
    goto _exit;
×
461
  }
462

463
  for (int32_t i = 0; i < numOfNames; ++i) {
×
464
    uint64_t uid = 0;
×
465
    if (pArgs[i].code == TSDB_CODE_SUCCESS) {
×
466
      if (pArgs[i].vLen != sizeof(uid)) {
×
467
        code = TSDB_CODE_INVALID_DATA_FMT;
×
468
        goto _exit;
×
469
      }
470
      uid = *(uint64_t *)pArgs[i].pVal;
×
471
    }
472

473
    if (NULL == taosArrayPush(pUids, &uid)) {
×
474
      code = terrno;
×
475
      goto _exit;
×
476
    }
477
  }
478

479
_exit:
×
480
  for (int32_t i = 0; i < numOfNames; ++i) {
×
481
    tdbFree(pArgs[i].pVal);
×
482
  }
483
  taosMemoryFree(pArgs);
×
484
  return code;
×
485
}
486

487
static void metaSetTableBatchInfo(SMetaTableBatchInfo *pInfo, const SMetaEntry *pEntry) {
3,670 ✔
488
  pInfo->type = pEntry->type;
3,670 ✔
489
  if (pEntry->type == TSDB_CHILD_TABLE || pEntry->type == TSDB_VIRTUAL_CHILD_TABLE) {
3,670 ✔
490
    pInfo->suid = pEntry->ctbEntry.suid;
3,611 ✔
491
  } else if (pEntry->type == TSDB_SUPER_TABLE) {
59 ✔
492
    pInfo->suid = pEntry->uid;
27 ✔
493
  } else {
494
    pInfo->suid = 0;
32 ✔
495
  }
496
}
3,110 ✔
497

498
static int32_t metaDecodeTableBatchInfo(SMeta *pMeta, const STdbTbBatchGetArg *pArg, SMetaTableBatchInfo *pInfo,
3,671 ✔
499
                                        int64_t txnId, bool checkTxn, bool *pNeedOldVersion) {
500
  SDecoder   decoder = {0};
3,671 ✔
501
  SMetaEntry entry = {0};
3,671 ✔
502
  int32_t    code = TSDB_CODE_SUCCESS;
3,671 ✔
503

504
  if (pArg->code != TSDB_CODE_SUCCESS) {
3,671 ✔
505
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
506
  }
507

508
  tDecoderInit(&decoder, pArg->pVal, pArg->vLen);
3,671 ✔
509
  code = metaDecodeEntry(&decoder, &entry);
3,671 ✔
510
  if (code == TSDB_CODE_SUCCESS) {
3,670 ✔
511
    metaSetTableBatchInfo(pInfo, &entry);
3,670 ✔
512
    if (checkTxn && entry.txnId != 0) {
3,667 ✔
513
      int8_t finalStatus = metaGetTxnMetaStatus(pMeta, entry.txnId);
19 ✔
514
      if (finalStatus == TXN_META_COMMITTED) {
19 ✔
515
        if (entry.txnStatus == META_TXN_PRE_DROP) {
×
516
          code = TSDB_CODE_PAR_TABLE_NOT_EXIST;
560 ✔
517
        }
518
      } else if (entry.txnStatus == META_TXN_PRE_CREATE) {
19 ✔
519
        if (finalStatus == TXN_META_ROLLEDBACK || txnId == 0 || txnId != entry.txnId) {
5 ✔
520
          code = TSDB_CODE_PAR_TABLE_NOT_EXIST;
562 ✔
521
        }
522
      } else if (entry.txnStatus == META_TXN_PRE_ALTER && entry.txnOrigVer > 0 &&
14 ✔
523
                 (txnId == 0 || txnId != entry.txnId)) {
4 ✔
524
        *pNeedOldVersion = true;
6 ✔
525
      }
526
    }
527
  }
528

529
  tFreeSSeriesWrapper(&entry.series);
3,667 ✔
530
  tDecoderClear(&decoder);
3,669 ✔
531
  return code;
3,671 ✔
532
}
533

534
int32_t metaGetTableInfosByNames(void *pVnode, const SArray *pTbNames, SArray *pInfos, int64_t txnId) {
8,823 ✔
535
  int32_t            code = TSDB_CODE_SUCCESS;
8,823 ✔
536
  int32_t            numOfNames = 0;
8,823 ✔
537
  STdbTbBatchGetArg *pNameArgs = NULL;
8,823 ✔
538
  STdbTbBatchGetArg *pUidArgs = NULL;
8,823 ✔
539
  STdbTbBatchGetArg *pTableArgs = NULL;
8,823 ✔
540
  STdbTbBatchGetArg *pOldTableArgs = NULL;
8,823 ✔
541
  STbDbKey          *pTableKeys = NULL;
8,823 ✔
542
  STbDbKey          *pOldTableKeys = NULL;
8,823 ✔
543
  bool               hasOldVersion = false;
8,823 ✔
544
  SMeta             *pMeta = ((SVnode *)pVnode)->pMeta;
8,823 ✔
545

546
  if (NULL == pTbNames || NULL == pInfos) {
8,823 ✔
547
    return TSDB_CODE_INVALID_PARA;
×
548
  }
549

550
  numOfNames = taosArrayGetSize(pTbNames);
8,825 ✔
551
  if (numOfNames == 0) {
8,825 ✔
552
    return TSDB_CODE_SUCCESS;
×
553
  }
554

555
  pNameArgs = taosMemoryCalloc(numOfNames, sizeof(*pNameArgs));
8,825 ✔
556
  pUidArgs = taosMemoryCalloc(numOfNames, sizeof(*pUidArgs));
8,822 ✔
557
  pTableArgs = taosMemoryCalloc(numOfNames, sizeof(*pTableArgs));
8,821 ✔
558
  pOldTableArgs = taosMemoryCalloc(numOfNames, sizeof(*pOldTableArgs));
8,821 ✔
559
  pTableKeys = taosMemoryCalloc(numOfNames, sizeof(*pTableKeys));
8,821 ✔
560
  pOldTableKeys = taosMemoryCalloc(numOfNames, sizeof(*pOldTableKeys));
8,822 ✔
561
  if (NULL == pNameArgs || NULL == pUidArgs || NULL == pTableArgs || NULL == pOldTableArgs || NULL == pTableKeys ||
8,822 ✔
562
      NULL == pOldTableKeys) {
560 ✔
563
    code = terrno;
×
564
    goto _exit;
×
565
  }
566

567
  for (int32_t i = 0; i < numOfNames; ++i) {
24,276 ✔
568
    SMetaTableBatchInfo info = {.type = TSDB_TABLE_MAX, .code = TSDB_CODE_PAR_TABLE_NOT_EXIST};
15,452 ✔
569
    char               *name = taosArrayGetP(pTbNames, i);
15,450 ✔
570
    if (NULL == name) {
15,451 ✔
571
      info.code = TSDB_CODE_INVALID_PARA;
×
572
    } else {
573
      pNameArgs[i].pKey = name;
15,451 ✔
574
      pNameArgs[i].kLen = (int32_t)strlen(name) + 1;
15,451 ✔
575
    }
576
    if (NULL == taosArrayPush(pInfos, &info)) {
15,452 ✔
577
      code = terrno;
×
578
      goto _exit;
×
579
    }
580
  }
581

582
  metaRLock(pMeta);
8,824 ✔
583
  code = tdbTbGetBatch(pMeta->pNameIdx, pNameArgs, numOfNames);
8,823 ✔
584
  if (code != TSDB_CODE_SUCCESS) {
8,823 ✔
585
    goto _unlock;
×
586
  }
587

588
  for (int32_t i = 0; i < numOfNames; ++i) {
24,276 ✔
589
    SMetaTableBatchInfo *pInfo = taosArrayGet(pInfos, i);
15,452 ✔
590
    if (NULL == pInfo || pNameArgs[i].code != TSDB_CODE_SUCCESS || pNameArgs[i].vLen != sizeof(pInfo->uid)) {
15,453 ✔
591
      continue;
11,787 ✔
592
    }
593
    memcpy(&pInfo->uid, pNameArgs[i].pVal, sizeof(pInfo->uid));
3,666 ✔
594
    pUidArgs[i].pKey = &pInfo->uid;
3,666 ✔
595
    pUidArgs[i].kLen = sizeof(pInfo->uid);
3,666 ✔
596
  }
597

598
  code = tdbTbGetBatch(pMeta->pUidIdx, pUidArgs, numOfNames);
8,824 ✔
599
  if (code != TSDB_CODE_SUCCESS) {
8,825 ✔
600
    goto _unlock;
×
601
  }
602

603
  for (int32_t i = 0; i < numOfNames; ++i) {
24,273 ✔
604
    SMetaTableBatchInfo *pInfo = taosArrayGet(pInfos, i);
15,451 ✔
605
    SUidIdxVal           uidInfo = {0};
15,452 ✔
606
    if (NULL == pInfo || pNameArgs[i].code != TSDB_CODE_SUCCESS || pUidArgs[i].code != TSDB_CODE_SUCCESS ||
15,448 ✔
607
        pUidArgs[i].vLen != sizeof(uidInfo)) {
3,105 ✔
608
      continue;
11,783 ✔
609
    }
610
    memcpy(&uidInfo, pUidArgs[i].pVal, sizeof(uidInfo));
3,665 ✔
611
    pTableKeys[i] = (STbDbKey){.version = uidInfo.version, .uid = pInfo->uid};
3,665 ✔
612
    pTableArgs[i].pKey = &pTableKeys[i];
3,665 ✔
613
    pTableArgs[i].kLen = sizeof(pTableKeys[i]);
3,665 ✔
614
  }
615

616
  code = tdbTbGetBatch(pMeta->pTbDb, pTableArgs, numOfNames);
8,822 ✔
617
  if (code != TSDB_CODE_SUCCESS) {
8,825 ✔
618
    goto _unlock;
×
619
  }
620

621
  for (int32_t i = 0; i < numOfNames; ++i) {
24,267 ✔
622
    SMetaTableBatchInfo *pInfo = taosArrayGet(pInfos, i);
15,448 ✔
623
    bool                 needOldVersion = false;
15,447 ✔
624
    if (NULL == pInfo || pNameArgs[i].code != TSDB_CODE_SUCCESS || pUidArgs[i].code != TSDB_CODE_SUCCESS) {
15,444 ✔
625
      continue;
11,779 ✔
626
    }
627
    pInfo->code = metaDecodeTableBatchInfo(pMeta, &pTableArgs[i], pInfo, txnId, true, &needOldVersion);
3,665 ✔
628
    if (pInfo->code == TSDB_CODE_SUCCESS && needOldVersion) {
3,665 ✔
629
      SDecoder   decoder = {0};
6 ✔
630
      SMetaEntry entry = {0};
6 ✔
631
      tDecoderInit(&decoder, pTableArgs[i].pVal, pTableArgs[i].vLen);
6 ✔
632
      code = metaDecodeEntry(&decoder, &entry);
6 ✔
633
      if (code == TSDB_CODE_SUCCESS) {
6 ✔
634
        pOldTableKeys[i] = (STbDbKey){.version = entry.txnOrigVer, .uid = pInfo->uid};
6 ✔
635
        pOldTableArgs[i].pKey = &pOldTableKeys[i];
6 ✔
636
        pOldTableArgs[i].kLen = sizeof(pOldTableKeys[i]);
6 ✔
637
        hasOldVersion = true;
6 ✔
638
      } else {
639
        pInfo->code = code;
×
640
      }
641
      tFreeSSeriesWrapper(&entry.series);
6 ✔
642
      tDecoderClear(&decoder);
6 ✔
643
    }
644
  }
645

646
  if (hasOldVersion) {
8,819 ✔
647
    code = tdbTbGetBatch(pMeta->pTbDb, pOldTableArgs, numOfNames);
6 ✔
648
    if (code != TSDB_CODE_SUCCESS) {
6 ✔
649
      goto _unlock;
×
650
    }
651

652
    for (int32_t i = 0; i < numOfNames; ++i) {
12 ✔
653
      SMetaTableBatchInfo *pInfo = taosArrayGet(pInfos, i);
6 ✔
654
      bool                 unused = false;
6 ✔
655
      if (NULL == pInfo || NULL == pOldTableArgs[i].pKey) {
6 ✔
656
        continue;
×
657
      }
658
      pInfo->code = metaDecodeTableBatchInfo(pMeta, &pOldTableArgs[i], pInfo, txnId, false, &unused);
6 ✔
659
    }
660
  }
661

662
_unlock:
8,819 ✔
663
  metaULock(pMeta);
8,819 ✔
664
_exit:
8,822 ✔
665
  for (int32_t i = 0; i < numOfNames; ++i) {
24,272 ✔
666
    if (NULL != pNameArgs) {
15,446 ✔
667
      tdbFree(pNameArgs[i].pVal);
15,447 ✔
668
    }
669
    if (NULL != pUidArgs) {
15,447 ✔
670
      tdbFree(pUidArgs[i].pVal);
15,448 ✔
671
    }
672
    if (NULL != pTableArgs) {
15,447 ✔
673
      tdbFree(pTableArgs[i].pVal);
15,448 ✔
674
    }
675
    if (NULL != pOldTableArgs) {
15,448 ✔
676
      tdbFree(pOldTableArgs[i].pVal);
15,449 ✔
677
    }
678
  }
679
  taosMemoryFree(pNameArgs);
8,826 ✔
680
  taosMemoryFree(pUidArgs);
8,824 ✔
681
  taosMemoryFree(pTableArgs);
8,824 ✔
682
  taosMemoryFree(pOldTableArgs);
8,821 ✔
683
  taosMemoryFree(pTableKeys);
8,819 ✔
684
  taosMemoryFree(pOldTableKeys);
8,818 ✔
685
  return code;
8,819 ✔
686
}
687

688
int metaGetTableTypeSuidByName(void *pVnode, char *tbName, ETableType *tbType, uint64_t *suid, int64_t txnId) {
155 ✔
689
  int         code = 0;
155 ✔
690
  SMetaReader mr = {0};
155 ✔
691
  metaReaderDoInit(&mr, ((SVnode *)pVnode)->pMeta, META_READER_LOCK, txnId);
155 ✔
692

693
  code = metaGetTableEntryByName(&mr, tbName);
155 ✔
694
  if (code == 0) *tbType = mr.me.type;
155 ✔
695
  if (TSDB_CHILD_TABLE == mr.me.type || TSDB_VIRTUAL_CHILD_TABLE == mr.me.type) {
155 ✔
696
    *suid = mr.me.ctbEntry.suid;
146 ✔
697
  } else if (TSDB_SUPER_TABLE == mr.me.type) {
9 ✔
698
    *suid = mr.me.uid;
6 ✔
699
  } else {
700
    *suid = 0;
3 ✔
701
  }
702
  metaDebug("metaGetTableTypeSuidByName table %s code:%d", tbName, code);
155 ✔
703

704
  metaReaderClear(&mr);
155 ✔
705
  return code;
155 ✔
706
}
707

708
int metaReadNext(SMetaReader *pReader) {
×
709
  SMeta *pMeta = pReader->pMeta;
×
710

711
  // TODO
712

713
  return 0;
×
714
}
715

716
int metaGetTableTtlByUid(void *meta, uint64_t uid, int64_t *ttlDays) {
×
717
  int         code = -1;
×
718
  SMetaReader mr = {0};
×
719
  metaReaderDoInit(&mr, (SMeta *)meta, META_READER_LOCK, 0);
×
720
  code = metaReaderGetTableEntryByUid(&mr, uid);
×
721
  if (code < 0) {
×
722
    goto _exit;
×
723
  }
724
  if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
×
725
    *ttlDays = mr.me.ctbEntry.ttlDays;
×
726
  } else if (mr.me.type == TSDB_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
727
    *ttlDays = mr.me.ntbEntry.ttlDays;
×
728
  } else {
729
    goto _exit;
×
730
  }
731

732
  code = 0;
×
733

734
_exit:
×
735
  metaReaderClear(&mr);
×
736
  return code;
×
737
}
738

739
#if 1  // ===================================================
740
SMTbCursor *metaOpenTbCursor(void *pVnode) {
190,989 ✔
741
  SMTbCursor *pTbCur = NULL;
190,989 ✔
742
  int32_t     code;
6,037 ✔
743

744
  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
190,989 ✔
745
  if (pTbCur == NULL) {
190,932 ✔
746
    return NULL;
×
747
  }
748

749
  SVnode *pVnodeObj = pVnode;
190,932 ✔
750
  // tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
751
  pTbCur->pMeta = pVnodeObj->pMeta;
190,932 ✔
752
  pTbCur->paused = 1;
190,932 ✔
753
  code = metaResumeTbCursor(pTbCur, 1, 0);
190,932 ✔
754
  if (code) {
190,773 ✔
755
    terrno = code;
×
756
    taosMemoryFree(pTbCur);
×
757
    return NULL;
×
758
  }
759
  return pTbCur;
184,781 ✔
760
}
761

762
void metaCloseTbCursor(SMTbCursor *pTbCur) {
585,035 ✔
763
  if (pTbCur) {
585,035 ✔
764
    tdbFree(pTbCur->pKey);
191,162 ✔
765
    tdbFree(pTbCur->pVal);
191,160 ✔
766
    if (!pTbCur->paused) {
191,187 ✔
767
      metaReaderClear(&pTbCur->mr);
37,380 ✔
768
      if (pTbCur->pDbc) {
37,381 ✔
769
        tdbTbcClose((TBC *)pTbCur->pDbc);
37,381 ✔
770
      }
771
    }
772
    taosMemoryFree(pTbCur);
191,189 ✔
773
  }
774
}
584,988 ✔
775

776
void metaPauseTbCursor(SMTbCursor *pTbCur) {
153,978 ✔
777
  if (!pTbCur->paused) {
153,978 ✔
778
    metaReaderClear(&pTbCur->mr);
153,989 ✔
779
    tdbTbcClose((TBC *)pTbCur->pDbc);
153,989 ✔
780
    pTbCur->paused = 1;
153,991 ✔
781
  }
782
}
153,980 ✔
783
int32_t metaResumeTbCursor(SMTbCursor *pTbCur, int8_t first, int8_t move) {
191,253 ✔
784
  int32_t code = 0;
191,253 ✔
785
  int32_t lino;
6,037 ✔
786
  int8_t  locked = 0;
191,253 ✔
787
  bool    dbcOpened = false;
191,253 ✔
788
  if (pTbCur->paused) {
191,253 ✔
789
    metaReaderDoInit(&pTbCur->mr, pTbCur->pMeta, META_READER_LOCK, 0);
191,260 ✔
790
    locked = 1;
191,283 ✔
791
    code = tdbTbcOpen(((SMeta *)pTbCur->pMeta)->pUidIdx, (TBC **)&pTbCur->pDbc, NULL);
191,283 ✔
792
    if (code != 0) {
191,129 ✔
793
      TSDB_CHECK_CODE(code, lino, _exit);
×
794
    }
795
    dbcOpened = true;
191,129 ✔
796

797
    if (first) {
191,129 ✔
798
      code = tdbTbcMoveToFirst((TBC *)pTbCur->pDbc);
190,951 ✔
799
      TSDB_CHECK_CODE(code, lino, _exit);
190,935 ✔
800
    } else {
801
      int c = 1;
178 ✔
802
      code = tdbTbcMoveTo(pTbCur->pDbc, pTbCur->pKey, pTbCur->kLen, &c);
178 ✔
803
      TSDB_CHECK_CODE(code, lino, _exit);
178 ✔
804
      if (c == 0) {
178 ✔
805
        if (move) tdbTbcMoveToNext(pTbCur->pDbc);
178 ✔
806
      } else if (c < 0) {
×
807
        code = tdbTbcMoveToPrev(pTbCur->pDbc);
×
808
        TSDB_CHECK_CODE(code, lino, _exit);
×
809
      } else {
810
        code = tdbTbcMoveToNext(pTbCur->pDbc);
×
811
        TSDB_CHECK_CODE(code, lino, _exit);
×
812
      }
813
    }
814

815
    pTbCur->paused = 0;
184,968 ✔
816
  }
817

818
_exit:
×
819
  if (code != 0) {
191,106 ✔
820
    // tdbTbcOpen succeeded but a later positioning step failed: close the dbc
821
    // opened above. pDbc is NULLed and paused stays 1, so a subsequent
822
    // metaCloseTbCursor (which skips tdbTbcClose when paused) will not
823
    // double-free; a retry re-opens a fresh dbc via tdbTbcOpen.
824
    if (dbcOpened && pTbCur->pDbc) {
×
825
      tdbTbcClose((TBC *)pTbCur->pDbc);
×
826
      pTbCur->pDbc = NULL;
×
827
    }
828
    if (locked) {
×
829
      metaReaderReleaseLock(&pTbCur->mr);
×
830
    }
831
  }
832
  return code;
190,990 ✔
833
}
834

835
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
2,688,900 ✔
836
  int    ret;
68,801 ✔
837
  void  *pBuf;
68,801 ✔
838
  STbCfg tbCfg;
68,801 ✔
839
  SMeta *pMeta = (SMeta *)pTbCur->mr.pMeta;
2,688,900 ✔
840
  // 1-entry suid cache: avoids a tdbTbGet(pTxnIdx) per CTB when consecutive loop
841
  // iterations skip children of the same dropped STB.
842
  tb_uid_t lastSuid = 0;
2,688,900 ✔
843
  bool     lastSkip = false;
2,688,900 ✔
844

845
  for (;;) {
68,817 ✔
846
    ret = tdbTbcNext((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
2,750,473 ✔
847
    if (ret < 0) {
2,753,457 ✔
848
      return ret;
185,125 ✔
849
    }
850

851
    tDecoderClear(&pTbCur->mr.coder);
2,562,294 ✔
852

853
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
2,564,492 ✔
854
    if (ret) return ret;
2,562,397 ✔
855

856
    if (pTbCur->mr.me.type == jumpTableType) {
2,562,397 ✔
857
      continue;
14,704 ✔
858
    }
859

860
    // batch meta txn: all visibility filtering is gated on a single fast-path check.
861
    // metaHasPendingTxnEntries() returns false in the common case (no active txns),
862
    // making the entire block below free.
863
    if (metaHasPendingTxnEntries(pMeta)) {
2,548,186 ✔
864
      // 1. Parent STB PRE_DROP: child tables are logically gone when the STB is
865
      //    dropped by the same transaction or committed-but-not-yet-vacuumed.
866
      //    This check is INDEPENDENT of the entry's own txnStatus  -  a CTB can
867
      //    simultaneously have its own PRE_CREATE/PRE_ALTER and a dropped parent.
868
      //    1-entry suid cache: all CTBs of the same STB share one tdbTbGet call.
869
      if (pTbCur->mr.me.type == TSDB_CHILD_TABLE || pTbCur->mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
50,689 ✔
870
        tb_uid_t suid = pTbCur->mr.me.ctbEntry.suid;
50,809 ✔
871
        bool     skipChild;
×
872
        if (suid == lastSuid) {
50,612 ✔
873
          skipChild = lastSkip;
46,852 ✔
874
        } else {
875
          skipChild = false;
3,760 ✔
876
          void   *pSuTxnVal = NULL;
3,760 ✔
877
          int32_t suTxnValLen = 0;
3,760 ✔
878
          if (tdbTbGet(pMeta->pTxnIdx, &suid, sizeof(suid), &pSuTxnVal, &suTxnValLen) == 0) {
3,761 ✔
879
            STxnIdxVal suTxnVal = *(const STxnIdxVal *)pSuTxnVal;
5 ✔
880
            tdbFree(pSuTxnVal);
5 ✔
881
            if (suTxnVal.txnStatus == META_TXN_PRE_DROP) {
5 ✔
882
              if ((pTbCur->txnId != 0 && pTbCur->txnId == suTxnVal.txnId) ||
6 ✔
883
                  metaGetTxnMetaStatus(pMeta, suTxnVal.txnId) == TXN_META_COMMITTED) {
3 ✔
884
                skipChild = true;
×
885
              }
886
            }
887
          }
888
          lastSuid = suid;
3,737 ✔
889
          lastSkip = skipChild;
3,737 ✔
890
        }
891
        if (skipChild) continue;
50,564 ✔
892
      }
893

894
      // 2. Entry-level txn status: short-circuit for normal (txnStatus == 0) entries.
895
      if (pTbCur->mr.me.txnStatus != META_TXN_NORMAL) {
50,487 ✔
896
        int8_t finalStatus = metaGetTxnMetaStatus(pMeta, pTbCur->mr.me.txnId);
48,911 ✔
897
        if (pTbCur->mr.me.txnStatus == META_TXN_PRE_CREATE) {
48,911 ✔
898
          // skip uncommitted shadow-creates; allow same-txn and committed
899
          if (finalStatus != TXN_META_COMMITTED && (pTbCur->txnId == 0 || pTbCur->txnId != pTbCur->mr.me.txnId)) {
45,698 ✔
900
            continue;
45,664 ✔
901
          }
902
        } else if (pTbCur->mr.me.txnStatus == META_TXN_PRE_DROP) {
3,213 ✔
903
          // logically deleted once committed
904
          if ((finalStatus == TXN_META_COMMITTED) ||
3,199 ✔
905
              ((pTbCur->txnId != 0) && (pTbCur->txnId == pTbCur->mr.me.txnId))) {
3,198 ✔
906
            continue;
1,204 ✔
907
          }
908
        } else if (pTbCur->mr.me.txnStatus == META_TXN_PRE_ALTER) {
14 ✔
909
          // redirect other-txn readers to old version for snapshot isolation
910
          if (finalStatus != TXN_META_COMMITTED && (pTbCur->txnId == 0 || pTbCur->txnId != pTbCur->mr.me.txnId)) {
14 ✔
911
            if (pTbCur->mr.me.txnOrigVer > 0) {
9 ✔
912
              tb_uid_t uid = *(tb_uid_t *)pTbCur->pKey;
9 ✔
913
              tDecoderClear(&pTbCur->mr.coder);
9 ✔
914
              ret = metaGetTableEntryByVersion(&pTbCur->mr, pTbCur->mr.me.txnOrigVer, uid);
9 ✔
915
              if (ret != 0) return ret;
9 ✔
916
              // If the old version is PRE_CREATE (same-txn CREATE then ALTER),
917
              // the table never existed outside this txn  -  skip for other sessions.
918
              if (pTbCur->mr.me.txnStatus == META_TXN_PRE_CREATE) {
9 ✔
919
                int8_t prevEntryFinalStatus = metaGetTxnMetaStatus(pMeta, pTbCur->mr.me.txnId);
2 ✔
920
                if (prevEntryFinalStatus != TXN_META_COMMITTED) {
2 ✔
921
                  continue;
2 ✔
922
                }
923
              }
924
            }
925
          }
926
        }
927
      }
928
    }  // metaHasPendingTxnEntries
929

930
    break;
2,438,331 ✔
931
  }
932

933
  return 0;
2,438,331 ✔
934
}
935

936
int32_t metaTbCursorPrev(SMTbCursor *pTbCur, ETableType jumpTableType) {
×
937
  int      ret;
×
938
  void    *pBuf;
×
939
  STbCfg   tbCfg;
×
940
  SMeta   *pMeta = (SMeta *)pTbCur->mr.pMeta;
×
941
  tb_uid_t lastSuid = 0;
×
942
  bool     lastSkip = false;
×
943

944
  for (;;) {
×
945
    ret = tdbTbcPrev((TBC *)pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
×
946
    if (ret < 0) {
×
947
      return -1;
×
948
    }
949

950
    tDecoderClear(&pTbCur->mr.coder);
×
951

952
    ret = metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
×
953
    if (ret < 0) {
×
954
      return ret;
×
955
    }
956

957
    if (pTbCur->mr.me.type == jumpTableType) {
×
958
      continue;
×
959
    }
960

961
    // batch meta txn: all visibility filtering is gated on a single fast-path check.
962
    // metaHasPendingTxnEntries() returns false in the common case (no active txns),
963
    // making the entire block below free.
964
    if (metaHasPendingTxnEntries(pMeta)) {
×
965
      // 1. Parent STB PRE_DROP: child tables are logically gone when the STB is
966
      //    dropped by the same transaction or committed-but-not-yet-vacuumed.
967
      //    This check is INDEPENDENT of the entry's own txnStatus  -  a CTB can
968
      //    simultaneously have its own PRE_CREATE/PRE_ALTER and a dropped parent.
969
      //    1-entry suid cache: all CTBs of the same STB share one tdbTbGet call.
970
      if (pTbCur->mr.me.type == TSDB_CHILD_TABLE || pTbCur->mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
×
971
        tb_uid_t suid = pTbCur->mr.me.ctbEntry.suid;
×
972
        bool     skipChild;
×
973
        if (suid == lastSuid) {
×
974
          skipChild = lastSkip;
×
975
        } else {
976
          skipChild = false;
×
977
          void   *pSuTxnVal = NULL;
×
978
          int32_t suTxnValLen = 0;
×
979
          if (tdbTbGet(pMeta->pTxnIdx, &suid, sizeof(suid), &pSuTxnVal, &suTxnValLen) == 0) {
×
980
            STxnIdxVal suTxnVal = *(const STxnIdxVal *)pSuTxnVal;
×
981
            tdbFree(pSuTxnVal);
×
982
            if (suTxnVal.txnStatus == META_TXN_PRE_DROP) {
×
983
              if ((pTbCur->txnId != 0 && pTbCur->txnId == suTxnVal.txnId) ||
×
984
                  metaGetTxnMetaStatus(pMeta, suTxnVal.txnId) == TXN_META_COMMITTED) {
×
985
                skipChild = true;
×
986
              }
987
            }
988
          }
989
          lastSuid = suid;
×
990
          lastSkip = skipChild;
×
991
        }
992
        if (skipChild) continue;
×
993
      }
994

995
      // 2. Entry-level txn status: short-circuit for normal (txnStatus == 0) entries.
996
      if (pTbCur->mr.me.txnStatus != META_TXN_NORMAL) {
×
997
        int8_t finalStatus = metaGetTxnMetaStatus(pMeta, pTbCur->mr.me.txnId);
×
998
        if (pTbCur->mr.me.txnStatus == META_TXN_PRE_CREATE) {
×
999
          // skip uncommitted shadow-creates; allow same-txn and committed
1000
          if (finalStatus != TXN_META_COMMITTED && (pTbCur->txnId == 0 || pTbCur->txnId != pTbCur->mr.me.txnId)) {
×
1001
            continue;
×
1002
          }
1003
        } else if (pTbCur->mr.me.txnStatus == META_TXN_PRE_DROP) {
×
1004
          // logically deleted once committed
1005
          if ((finalStatus == TXN_META_COMMITTED) ||
×
1006
              ((pTbCur->txnId != 0) && (pTbCur->txnId == pTbCur->mr.me.txnId))) {
×
1007
            continue;
×
1008
          }
1009
        } else if (pTbCur->mr.me.txnStatus == META_TXN_PRE_ALTER) {
×
1010
          // redirect other-txn readers to old version for snapshot isolation
1011
          if (finalStatus != TXN_META_COMMITTED && (pTbCur->txnId == 0 || pTbCur->txnId != pTbCur->mr.me.txnId)) {
×
1012
            if (pTbCur->mr.me.txnOrigVer > 0) {
×
1013
              tb_uid_t uid = *(tb_uid_t *)pTbCur->pKey;
×
1014
              tDecoderClear(&pTbCur->mr.coder);
×
1015
              ret = metaGetTableEntryByVersion(&pTbCur->mr, pTbCur->mr.me.txnOrigVer, uid);
×
1016
              if (ret != 0) return ret;
×
1017
              // If the old version is PRE_CREATE (same-txn CREATE then ALTER),
1018
              // the table never existed outside this txn  -  skip for other sessions.
1019
              if (pTbCur->mr.me.txnStatus == META_TXN_PRE_CREATE) {
×
1020
                int8_t prevEntryFinalStatus = metaGetTxnMetaStatus(pMeta, pTbCur->mr.me.txnId);
×
1021
                if (prevEntryFinalStatus != TXN_META_COMMITTED) {
×
1022
                  continue;
×
1023
                }
1024
              }
1025
            }
1026
          }
1027
        }
1028
      }
1029
    }  // metaHasPendingTxnEntries
1030

1031
    break;
×
1032
  }
1033

1034
  return 0;
×
1035
}
1036

1037
static SSchemaWrapper * getSchemaWithVer(SMeta *pMeta, tb_uid_t uid, int32_t sver, SExtSchema **extSchema) {
746 ✔
1038
  SDecoder        dc = {0};
746 ✔
1039
  int32_t         code = 0;
746 ✔
1040
  void           *pData = NULL;
746 ✔
1041
  int             nData = 0;
746 ✔
1042
  SSchemaWrapper  schema = {0};
745 ✔
1043
  SSchemaWrapper *pSchema = NULL;
744 ✔
1044
  
1045
  // query from skm ext db
1046
  if (extSchema != NULL) {
744 ✔
1047
    if ((tdbTbGet(pMeta->pSkmExtDb, &(SSkmDbKey){.uid = uid, .sver = sver}, sizeof(SSkmDbKey), &pData, &nData)) == 0) {
731 ✔
1048
      *extSchema = taosMemoryMalloc(nData);
9 ✔
1049
      if (*extSchema) {
9 ✔
1050
        (void)memcpy(*extSchema, pData, nData);
9 ✔
1051
      }
1052
    }
1053
    tdbFree(pData);
733 ✔
1054
    pData = NULL;
733 ✔
1055
  }
1056
  
1057
  // query from skm db
1058
  if ((code = tdbTbGet(pMeta->pSkmDb, &(SSkmDbKey){.uid = uid, .sver = sver}, sizeof(SSkmDbKey), &pData, &nData)) < 0) {
746 ✔
1059
    goto _err;
1 ✔
1060
  }
1061

1062
  tDecoderInit(&dc, pData, nData);
747 ✔
1063
  if ((code = tDecodeSSchemaWrapperEx(&dc, &schema)) != 0) {
742 ✔
1064
    tDecoderClear(&dc);
×
1065
    goto _err;
×
1066
  }
1067
  pSchema = tCloneSSchemaWrapper(&schema);
746 ✔
1068
  if (pSchema == NULL) {
746 ✔
1069
    tDecoderClear(&dc);
×
1070
    code = terrno;
×
1071
    goto _err;
×
1072
  }
1073
  tDecoderClear(&dc);
746 ✔
1074

1075
  tdbFree(pData);
743 ✔
1076
  return pSchema;
742 ✔
1077

1078
_err:
1 ✔
1079
  tdbFree(pData);
1 ✔
1080
  tDeleteSchemaWrapper(pSchema);
×
1081
  if (extSchema != NULL) {
1 ✔
1082
    taosMemoryFreeClear(*extSchema);
1 ✔
1083
  }
1084
  terrno = code;
1 ✔
1085
  return NULL;
1 ✔
1086
}
1087

1088
static int32_t metaGetSuidByUidIfTableNotExist(SMeta *pMeta, int64_t uid, int64_t* id){
615 ✔
1089
  int32_t code = 0;
615 ✔
1090
  void   *pKey = NULL;
615 ✔
1091
  void   *pVal = NULL;
614 ✔
1092
  int     kLen = 0;
614 ✔
1093
  int     vLen = 0;
614 ✔
1094

1095
  int64_t* suid = (int64_t*)taosHashGet(pMeta->uidSuidHash, &uid, sizeof(uid));
615 ✔
1096
  if (suid != NULL) {
616 ✔
1097
    *id = *suid;
400 ✔
1098
    return code;
400 ✔
1099
  }
1100

1101
  TBC *pCur = NULL;
216 ✔
1102
  code = tdbTbcOpen(pMeta->pTbDb, (TBC**)&pCur, NULL);
216 ✔
1103
  if (code != 0) {
215 ✔
1104
    goto END;
×
1105
  }
1106
  code = tdbTbcMoveToFirst(pCur);
215 ✔
1107
  if (code != 0) {
216 ✔
1108
    goto END;
×
1109
  }
1110

1111
  void       *tKey = NULL;
216 ✔
1112
  void       *tVal = NULL;
216 ✔
1113
  int         tKLen = 0;
216 ✔
1114
  int         tVLen = 0;
216 ✔
1115
  code = tdbTbcGet(pCur, (const void**)&tKey, &tKLen, (const void**)&tVal, &tVLen);
216 ✔
1116
  if (code != 0) {
216 ✔
1117
    goto END;
×
1118
  }
1119
  STbDbKey *tmp = (STbDbKey*)tKey;
216 ✔
1120
  if (tmp->uid == uid) {
216 ✔
1121
    SMetaEntry  me = {0};
×
1122
    SDecoder    dc = {0};
×
1123
    tDecoderInit(&dc, tVal, tVLen);
×
1124
    if (metaDecodeEntry(&dc, &me) == 0 && (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE)) {
×
1125
      *id = me.ctbEntry.suid;
×
1126
    }
1127
    tDecoderClear(&dc);
×
1128
    goto END;
×
1129
  }
1130

1131
  while (1) {
6,607 ✔
1132
    int32_t ret = tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen);
6,823 ✔
1133
    if (ret < 0) break;
6,823 ✔
1134

1135
    tmp = (STbDbKey*)pKey;
6,823 ✔
1136
    if (tmp->uid == uid) {
6,823 ✔
1137
      SMetaEntry  me = {0};
216 ✔
1138
      SDecoder    dc = {0};
216 ✔
1139
      tDecoderInit(&dc, pVal, vLen);
216 ✔
1140
      if (metaDecodeEntry(&dc, &me) == 0 && (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE)) {
216 ✔
1141
        *id = me.ctbEntry.suid;
214 ✔
1142
      }
1143
      tDecoderClear(&dc);
215 ✔
1144
      goto END;
215 ✔
1145
    }
1146
  }
1147

1148
END:
215 ✔
1149
  tdbFree(pKey);
216 ✔
1150
  tdbFree(pVal);
216 ✔
1151
  tdbTbcClose(pCur);
216 ✔
1152
  if (code != 0) {
215 ✔
1153
    metaError("%s failed to get suid reason:%s", __func__, tstrerror(terrno));
×
1154
  } else {
1155
    code = taosHashPut(pMeta->uidSuidHash, &uid, sizeof(uid), id, sizeof(*id));
215 ✔
1156
    if (code != 0) {
215 ✔
1157
      metaError("%s failed to put suid into uidSuidHash, reason:%s", __func__, tstrerror(terrno));
×
1158
    }
1159
  }
1160
  return code;
215 ✔
1161
}
1162

1163
int32_t metaGetTbnameByIdIfTableNotExist(SMeta *pMeta, int64_t uid, char *tbname){
615 ✔
1164
  int32_t code = 0;
615 ✔
1165
  void   *pKey = NULL;
615 ✔
1166
  void   *pVal = NULL;
614 ✔
1167
  int     kLen = 0;
614 ✔
1168
  int     vLen = 0;
614 ✔
1169

1170
  char* name = (char*)taosHashGet(pMeta->uidNameHash, &uid, sizeof(uid));
615 ✔
1171
  if (name != NULL) {
614 ✔
1172
    tstrncpy(tbname, name, TSDB_TABLE_NAME_LEN);
399 ✔
1173
    return code;
399 ✔
1174
  }
1175

1176
  TBC *pCur = NULL;
215 ✔
1177
  code = tdbTbcOpen(pMeta->pTbDb, (TBC**)&pCur, NULL);
215 ✔
1178
  if (code != 0) {
214 ✔
1179
    goto END;
×
1180
  }
1181
  code = tdbTbcMoveToFirst(pCur);
214 ✔
1182
  if (code != 0) {
215 ✔
1183
    goto END;
×
1184
  }
1185

1186
  void       *tKey = NULL;
215 ✔
1187
  void       *tVal = NULL;
215 ✔
1188
  int         tKLen = 0;
215 ✔
1189
  int         tVLen = 0;
214 ✔
1190
  code = tdbTbcGet(pCur, (const void**)&tKey, &tKLen, (const void**)&tVal, &tVLen);
214 ✔
1191
  if (code != 0) {
214 ✔
1192
    goto END;
×
1193
  }
1194
  STbDbKey *tmp = (STbDbKey*)tKey;
214 ✔
1195
  if (tmp->uid == uid) {
214 ✔
1196
    SMetaEntry  me = {0};
×
1197
    SDecoder    dc = {0};
×
1198
    tDecoderInit(&dc, tVal, tVLen);
×
1199
    code = metaDecodeEntry(&dc, &me);
×
1200
    if (code == 0){
×
1201
      tstrncpy(tbname, me.name, TSDB_TABLE_NAME_LEN);
×
1202
    }
1203
    tDecoderClear(&dc);
×
1204
    goto END;
×
1205
  }
1206

1207
  while (1) {
6,607 ✔
1208
    int32_t ret = tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen);
6,821 ✔
1209
    if (ret < 0) break;
6,822 ✔
1210

1211
    tmp = (STbDbKey*)pKey;
6,822 ✔
1212
    if (tmp->uid == uid) {
6,822 ✔
1213
      SMetaEntry  me = {0};
215 ✔
1214
      SDecoder    dc = {0};
215 ✔
1215
      tDecoderInit(&dc, pVal, vLen);
215 ✔
1216
      code = metaDecodeEntry(&dc, &me);
215 ✔
1217
      if (code == 0){
215 ✔
1218
        tstrncpy(tbname, me.name, TSDB_TABLE_NAME_LEN);
215 ✔
1219
      }
1220
      tDecoderClear(&dc);
215 ✔
1221
      goto END;
215 ✔
1222
    }
1223
  }
1224

1225
END:
215 ✔
1226
  tdbFree(pKey);
215 ✔
1227
  tdbFree(pVal);
215 ✔
1228
  tdbTbcClose(pCur);
215 ✔
1229
  if (code != 0) {
214 ✔
1230
    metaError("%s failed to get suid reason:%s", __func__, tstrerror(terrno));
×
1231
  } else {
1232
    code = taosHashPut(pMeta->uidNameHash, &uid, sizeof(uid), tbname, strlen(tbname) + 1);
214 ✔
1233
    if (code != 0) {
214 ✔
1234
      metaError("%s failed to put suid into uidSuidHash, reason:%s", __func__, tstrerror(terrno));
×
1235
    }
1236
  }
1237
  return code;
214 ✔
1238
}
1239

1240
/**
1241
 * @param type 0x01 fetchRsmaSchema if table is rsma
1242
 */
1243
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, SExtSchema **extSchema,
1,181,155 ✔
1244
                                   int8_t type, bool ignoreExist) {
1245
  int32_t         code = 0;
1,181,155 ✔
1246
  void           *pData = NULL;
1,181,155 ✔
1247
  int             nData = 0;
1,181,172 ✔
1248
  int64_t         version;
3,953 ✔
1249
  SSchemaWrapper  schema = {0};
1,181,211 ✔
1250
  SSchemaWrapper *pSchema = NULL;
1,181,211 ✔
1251
  SDecoder        dc = {0};
1,181,211 ✔
1252
  if (lock) {
1,180,727 ✔
1253
    metaRLock(pMeta);
1,156,943 ✔
1254
  }
1255
_query:
1,276,493 ✔
1256
  code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData);
1,280,500 ✔
1257
  if (code == TSDB_CODE_NOT_FOUND && ignoreExist){
1,279,704 ✔
1258
    int64_t id = 0;
612 ✔
1259
    code = metaGetSuidByUidIfTableNotExist(pMeta, uid, &id);
613 ✔
1260
    if (code != 0) {
615 ✔
1261
      goto _err;
×
1262
    }
1263
    pSchema = getSchemaWithVer(pMeta, id, sver, extSchema);
615 ✔
1264
    goto _exit;
613 ✔
1265
  }
1266

1267
  if (code < 0) {
1,279,092 ✔
1268
    goto _err;
1,194 ✔
1269
  }
1270

1271
  version = ((SUidIdxVal *)pData)[0].version;
1,277,898 ✔
1272

1273
  if ((code = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData)) !=
1,277,898 ✔
1274
      0) {
1275
    goto _err;
×
1276
  }
1277

1278
  SMetaEntry me = {0};
1,278,642 ✔
1279
  tDecoderInit(&dc, pData, nData);
1,278,452 ✔
1280
  code = metaDecodeEntry(&dc, &me);
1,278,165 ✔
1281
  if (code) {
1,278,390 ✔
1282
    tDecoderClear(&dc);
×
1283
    goto _err;
×
1284
  }
1285

1286
  // batch meta txn: finalized txn visibility adjustment
1287
  if (me.txnId != 0) {
1,278,390 ✔
1288
    int8_t finalStatus = metaGetTxnMetaStatus(pMeta, me.txnId);
105 ✔
1289
    if (finalStatus == TXN_META_COMMITTED) {
105 ✔
1290
      if (me.txnStatus == META_TXN_PRE_DROP) {
×
1291
        tDecoderClear(&dc);
×
1292
        code = TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
1293
        goto _err;
×
1294
      }
1295
      // PRE_CREATE and PRE_ALTER: visible with current version, skip filters below
1296
      goto _schema;
×
1297
    }
1298
  }
1299

1300
  if (me.txnStatus != META_TXN_NORMAL) {
1,278,390 ✔
1301
    // batch meta txn: PRE_CREATE entries are invisible  -  return error
1302
    if (me.txnStatus == META_TXN_PRE_CREATE) {
105 ✔
1303
      tDecoderClear(&dc);
58 ✔
1304
      code = TSDB_CODE_PAR_TABLE_NOT_EXIST;
58 ✔
1305
      goto _err;
58 ✔
1306
    }
1307

1308
    // batch meta txn: PRE_ALTER entries should return old schema (snapshot isolation)
1309
    // Redirect to old version entry via txnOrigVer
1310
    if (me.txnStatus == META_TXN_PRE_ALTER && me.txnOrigVer > 0) {
47 ✔
1311
      int64_t oldVersion = me.txnOrigVer;
39 ✔
1312
      tDecoderClear(&dc);
39 ✔
1313

1314
      if ((code = tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = me.uid, .version = oldVersion}, sizeof(STbDbKey), &pData,
39 ✔
1315
                           &nData)) != 0) {
1316
        metaError("vgId:%d, PRE_ALTER: old version %" PRId64 " not found for uid %" PRId64, TD_VID(pMeta->pVnode),
×
1317
                  oldVersion, me.uid);
1318
        goto _err;
×
1319
      }
1320

1321
      memset(&me, 0, sizeof(me));
39 ✔
1322
      tDecoderInit(&dc, pData, nData);
39 ✔
1323
      code = metaDecodeEntry(&dc, &me);
39 ✔
1324
      if (code) {
39 ✔
1325
        tDecoderClear(&dc);
×
1326
        goto _err;
×
1327
      }
1328
    }
1329
  }
1330

1331
_schema:
1,278,332 ✔
1332
  if (me.type == TSDB_SUPER_TABLE) {
1,278,490 ✔
1333
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
861,621 ✔
1334
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
861,486 ✔
1335
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
861,486 ✔
1336
      if (TABLE_IS_ROLLUP(me.flags)) {
861,483 ✔
1337
        if ((type == 0x01) && (code = metaGetRsmaSchema(&me, &pSchema->pRsma)) != 0) {
144 ✔
1338
          tDecoderClear(&dc);
×
1339
          goto _err;
×
1340
        }
1341
      }
1342
      tDecoderClear(&dc);
861,483 ✔
1343
      goto _exit;
861,265 ✔
1344
    }
1345
  } else if (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE) {
416,869 ✔
1346
    uid = me.ctbEntry.suid;
97,682 ✔
1347
    tDecoderClear(&dc);
97,682 ✔
1348
    goto _query;
97,700 ✔
1349
  } else {
1350
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
319,187 ✔
1351
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
319,288 ✔
1352
      if (extSchema != NULL) *extSchema = metaGetSExtSchema(&me);
319,288 ✔
1353
      tDecoderClear(&dc);
319,281 ✔
1354
      goto _exit;
319,172 ✔
1355
    }
1356
  }
1357
  tDecoderClear(&dc);
237 ✔
1358

1359
  pSchema = getSchemaWithVer(pMeta, uid, sver, extSchema);
132 ✔
1360

1361
_exit:
1,181,182 ✔
1362
  if (lock) {
1,181,473 ✔
1363
    metaULock(pMeta);
1,158,024 ✔
1364
  }
1365
  tdbFree(pData);
1,181,422 ✔
1366
  return pSchema;
1,181,380 ✔
1367

1368
_err:
1,126 ✔
1369
  if (lock) {
1,252 ✔
1370
    metaULock(pMeta);
901 ✔
1371
  }
1372
  tdbFree(pData);
1,252 ✔
1373
  tDeleteSchemaWrapper(pSchema);
5 ✔
1374
  if (extSchema != NULL) {
1,252 ✔
1375
    taosMemoryFreeClear(*extSchema);
×
1376
  }
1377
  terrno = code;
1,252 ✔
1378
  return NULL;
1,252 ✔
1379
}
1380

1381
int64_t metaGetTableCreateTime(SMeta *pMeta, tb_uid_t uid, int lock) {
2,811 ✔
1382
  void    *pData = NULL;
2,811 ✔
1383
  int      nData = 0;
2,811 ✔
1384
  int64_t  version = 0;
2,812 ✔
1385
  SDecoder dc = {0};
2,812 ✔
1386
  int64_t  createTime = INT64_MAX;
2,812 ✔
1387
  if (lock) {
2,812 ✔
1388
    metaRLock(pMeta);
2,812 ✔
1389
  }
1390

1391
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
2,812 ✔
1392
    goto _exit;
2 ✔
1393
  }
1394

1395
  version = ((SUidIdxVal *)pData)[0].version;
2,810 ✔
1396

1397
  if (tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData) != 0) {
2,810 ✔
1398
    goto _exit;
×
1399
  }
1400

1401
  SMetaEntry me = {0};
2,809 ✔
1402
  tDecoderInit(&dc, pData, nData);
2,809 ✔
1403
  int32_t code = metaDecodeEntry(&dc, &me);
2,809 ✔
1404
  if (code) {
2,809 ✔
1405
    tDecoderClear(&dc);
×
1406
    goto _exit;
×
1407
  }
1408
  if (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE) {
2,809 ✔
1409
    createTime = me.ctbEntry.btime;
2,807 ✔
1410
  } else if (me.type == TSDB_NORMAL_TABLE || me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
2 ✔
1411
    createTime = me.ntbEntry.btime;
2 ✔
1412
  }
1413
  tDecoderClear(&dc);
2,809 ✔
1414

1415
_exit:
2,811 ✔
1416
  if (lock) {
2,812 ✔
1417
    metaULock(pMeta);
2,812 ✔
1418
  }
1419
  tdbFree(pData);
2,810 ✔
1420
  return createTime;
2,812 ✔
1421
}
1422

1423
SMCtbCursor *metaOpenCtbCursor(void *pVnode, tb_uid_t uid, int lock, int64_t txnId) {
728,083 ✔
1424
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
728,083 ✔
1425
  SMCtbCursor *pCtbCur = NULL;
728,083 ✔
1426
  SCtbIdxKey   ctbIdxKey;
16,987 ✔
1427
  int          ret = 0;
728,083 ✔
1428
  int          c = 0;
728,083 ✔
1429

1430
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
728,083 ✔
1431
  if (pCtbCur == NULL) {
727,875 ✔
1432
    return NULL;
×
1433
  }
1434

1435
  pCtbCur->pMeta = pMeta;
727,875 ✔
1436
  pCtbCur->suid = uid;
727,875 ✔
1437
  pCtbCur->lock = lock;
727,875 ✔
1438
  pCtbCur->txnId = txnId;
727,875 ✔
1439
  pCtbCur->paused = 1;
727,875 ✔
1440

1441
  ret = metaResumeCtbCursor(pCtbCur, 1);
727,875 ✔
1442
  if (ret < 0) {
727,734 ✔
1443
    metaCloseCtbCursor(pCtbCur);
2 ✔
1444
    terrno = ret;
2 ✔
1445
    return NULL;
2 ✔
1446
  }
1447
  return pCtbCur;
710,818 ✔
1448
}
1449

1450
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
728,369 ✔
1451
  if (pCtbCur) {
728,369 ✔
1452
    if (!pCtbCur->paused) {
728,503 ✔
1453
      if (pCtbCur->pMeta && pCtbCur->lock) metaULock(pCtbCur->pMeta);
718,568 ✔
1454
      if (pCtbCur->pCur) {
718,527 ✔
1455
        tdbTbcClose(pCtbCur->pCur);
718,637 ✔
1456
      }
1457
    }
1458
    tdbFree(pCtbCur->pKey);
728,308 ✔
1459
    tdbFree(pCtbCur->pVal);
728,338 ✔
1460
  }
1461
  taosMemoryFree(pCtbCur);
728,245 ✔
1462
}
728,089 ✔
1463

1464
void metaPauseCtbCursor(SMCtbCursor *pCtbCur) {
9,944 ✔
1465
  if (!pCtbCur->paused) {
9,944 ✔
1466
    tdbTbcClose((TBC *)pCtbCur->pCur);
9,954 ✔
1467
    if (pCtbCur->lock) {
9,950 ✔
1468
      metaULock(pCtbCur->pMeta);
9,952 ✔
1469
    }
1470
    pCtbCur->paused = 1;
9,951 ✔
1471
  }
1472
}
9,941 ✔
1473

1474
int32_t metaResumeCtbCursor(SMCtbCursor *pCtbCur, int8_t first) {
728,043 ✔
1475
  if (pCtbCur->paused) {
728,043 ✔
1476
    pCtbCur->paused = 0;
728,182 ✔
1477

1478
    if (pCtbCur->lock) {
728,182 ✔
1479
      metaRLock(pCtbCur->pMeta);
703,939 ✔
1480
    }
1481

1482
    // Check STB PRE_DROP state once here, under the lock, so metaCtbCursorNext
1483
    // can skip the B+ tree lookup on every iteration.
1484
    pCtbCur->stbTxnDropped = 0;
728,577 ✔
1485
    if (metaHasPendingTxnEntries(pCtbCur->pMeta)) {
1,440,307 ✔
1486
      void   *pSuTxnVal = NULL;
564 ✔
1487
      int32_t suTxnValLen = 0;
564 ✔
1488
      if (tdbTbGet(pCtbCur->pMeta->pTxnIdx, &pCtbCur->suid, sizeof(pCtbCur->suid), &pSuTxnVal, &suTxnValLen) == 0) {
564 ✔
1489
        STxnIdxVal suTxnVal = *(const STxnIdxVal *)pSuTxnVal;
166 ✔
1490
        tdbFree(pSuTxnVal);
166 ✔
1491
        if (suTxnVal.txnStatus == META_TXN_PRE_DROP) {
166 ✔
1492
          if (pCtbCur->txnId != 0 && pCtbCur->txnId == suTxnVal.txnId) {
92 ✔
1493
            pCtbCur->stbTxnDropped = 1;
×
1494
          } else if (metaGetTxnMetaStatus(pCtbCur->pMeta, suTxnVal.txnId) == TXN_META_COMMITTED) {
92 ✔
1495
            pCtbCur->stbTxnDropped = 1;
24 ✔
1496
          }
1497
        }
1498
      }
1499
    }
1500

1501
    int ret = 0;
728,718 ✔
1502
    ret = tdbTbcOpen(pCtbCur->pMeta->pCtbIdx, (TBC **)&pCtbCur->pCur, NULL);
728,718 ✔
1503
    if (ret < 0) {
727,921 ✔
1504
      return ret;
×
1505
    }
1506

1507
    if (first) {
727,921 ✔
1508
      SCtbIdxKey ctbIdxKey;
727,918 ✔
1509
      // move to the suid
1510
      ctbIdxKey.suid = pCtbCur->suid;
728,133 ✔
1511
      ctbIdxKey.uid = INT64_MIN;
728,133 ✔
1512
      int c = 0;
728,133 ✔
1513
      ret = tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
727,129 ✔
1514
      if (ret < 0) {
728,049 ✔
1515
        return ret;
1 ✔
1516
      }
1517
      if (c > 0 && tdbTbcIsValid(pCtbCur->pCur)) {
728,048 ✔
1518
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
45,046 ✔
1519
      }
1520
    } else {
1521
      int c = 0;
3 ✔
1522
      ret = tdbTbcMoveTo(pCtbCur->pCur, pCtbCur->pKey, pCtbCur->kLen, &c);
×
1523
      if (ret < 0 || !tdbTbcIsValid(pCtbCur->pCur)) {
×
1524
        return ret;
×
1525
      }
1526
      if (c < 0) {
×
1527
        ret = tdbTbcMoveToPrev(pCtbCur->pCur);
×
1528
      } else {
1529
        ret = tdbTbcMoveToNext(pCtbCur->pCur);
×
1530
      }
1531
    }
1532
    return ret;
727,842 ✔
1533
  }
1534
  return 0;
×
1535
}
1536

1537
int32_t metaCtbCursorNextChecked(SMCtbCursor *pCtbCur, tb_uid_t *pUid) {
5,308,760 ✔
1538
  int         ret;
171,754 ✔
1539
  SCtbIdxKey *pCtbIdxKey;
171,754 ✔
1540
  *pUid = 0;
5,308,760 ✔
1541

1542
  // STB PRE_DROP was checked once in metaResumeCtbCursor (under the lock).
1543
  // If it was dropped, all children are invisible  -  no need to iterate.
1544
  if (pCtbCur->stbTxnDropped) {
5,308,760 ✔
1545
    return 0;
24 ✔
1546
  }
1547

1548
  while (tdbTbcIsValid(pCtbCur->pCur)) {
5,309,610 ✔
1549
    ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
4,790,001 ✔
1550
    if (ret < 0) {
4,797,636 ✔
1551
      return ret;
×
1552
    }
1553

1554
    pCtbIdxKey = pCtbCur->pKey;
4,797,635 ✔
1555
    if (pCtbIdxKey->suid > pCtbCur->suid) {
4,797,635 ✔
1556
      return 0;
206,826 ✔
1557
    }
1558

1559
    // batch meta txn: skip PRE_CREATE entries via txn.idx lookup
1560
    // but allow same-txn visibility: if cursor's txnId matches the entry's, don't skip
1561
    if (metaHasPendingTxnEntries(pCtbCur->pMeta)) {
9,026,688 ✔
1562
      void   *pTxnVal = NULL;
2,629 ✔
1563
      int32_t txnValLen = 0;
2,629 ✔
1564
      if (tdbTbGet(pCtbCur->pMeta->pTxnIdx, &pCtbIdxKey->uid, sizeof(pCtbIdxKey->uid), &pTxnVal, &txnValLen) == 0) {
2,621 ✔
1565
        STxnIdxVal txnVal      = *(const STxnIdxVal *)pTxnVal;
1,495 ✔
1566
        int8_t     st          = txnVal.txnStatus;
1,495 ✔
1567
        int64_t    entryTxnId  = txnVal.txnId;
1,495 ✔
1568
        tdbFree(pTxnVal);
1,495 ✔
1569
        if (st == META_TXN_PRE_CREATE) {
1,498 ✔
1570
          int8_t finalStatus = metaGetTxnMetaStatus(pCtbCur->pMeta, entryTxnId);
130 ✔
1571
          if (finalStatus == TXN_META_COMMITTED) {
130 ✔
1572
            // Committed PRE_CREATE: visible, don't skip
1573
          } else if (pCtbCur->txnId == 0 || pCtbCur->txnId != entryTxnId) {
130 ✔
1574
            continue;
127 ✔
1575
          }
1576
        }
1577
        // Committed PRE_DROP: skip (logically deleted)
1578
        if (st == META_TXN_PRE_DROP) {
1,371 ✔
1579
          int8_t finalStatus = metaGetTxnMetaStatus(pCtbCur->pMeta, entryTxnId);
6 ✔
1580
          if (finalStatus == TXN_META_COMMITTED) {
6 ✔
1581
            continue;
×
1582
          }
1583
        }
1584
      }
1585
    }  // metaHasPendingTxnEntries
1586

1587
    *pUid = pCtbIdxKey->uid;
4,589,825 ✔
1588
    return 0;
4,589,825 ✔
1589
  }
1590
  return 0;
504,840 ✔
1591
}
1592

1593
tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
5,232,407 ✔
1594
  tb_uid_t uid = 0;
5,232,407 ✔
1595
  int32_t  code = metaCtbCursorNextChecked(pCtbCur, &uid);
5,235,176 ✔
1596
  if (code != 0) {
5,231,566 ✔
1597
    terrno = code;
×
1598
  }
1599
  return uid;
5,231,566 ✔
1600
}
1601

1602
struct SMStbCursor {
1603
  SMeta   *pMeta;
1604
  TBC     *pCur;
1605
  tb_uid_t suid;
1606
  void    *pKey;
1607
  void    *pVal;
1608
  int      kLen;
1609
  int      vLen;
1610
};
1611

1612
SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
320,187 ✔
1613
  SMStbCursor *pStbCur = NULL;
320,187 ✔
1614
  int          ret = 0;
320,187 ✔
1615
  int          c = 0;
320,187 ✔
1616

1617
  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
320,189 ✔
1618
  if (pStbCur == NULL) {
320,198 ✔
1619
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1620
    return NULL;
×
1621
  }
1622

1623
  pStbCur->pMeta = pMeta;
320,198 ✔
1624
  pStbCur->suid = suid;
320,198 ✔
1625
  metaRLock(pMeta);
320,198 ✔
1626

1627
  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
320,198 ✔
1628
  if (ret < 0) {
320,191 ✔
1629
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1630
    metaULock(pMeta);
×
1631
    taosMemoryFree(pStbCur);
×
1632
    return NULL;
×
1633
  }
1634

1635
  // move to the suid
1636
  ret = tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
320,191 ✔
1637
  if (c > 0) {
320,196 ✔
1638
    ret = tdbTbcMoveToNext(pStbCur->pCur);
×
1639
  }
1640

1641
  return pStbCur;
316,329 ✔
1642
}
1643

1644
void metaCloseStbCursor(SMStbCursor *pStbCur) {
320,189 ✔
1645
  if (pStbCur) {
320,189 ✔
1646
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
320,192 ✔
1647
    if (pStbCur->pCur) {
320,194 ✔
1648
      tdbTbcClose(pStbCur->pCur);
320,199 ✔
1649

1650
      tdbFree(pStbCur->pKey);
320,193 ✔
1651
      tdbFree(pStbCur->pVal);
320,195 ✔
1652
    }
1653

1654
    taosMemoryFree(pStbCur);
320,192 ✔
1655
  }
1656
}
320,193 ✔
1657

1658
tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
718,219 ✔
1659
  int ret;
6,041 ✔
1660

1661
  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
718,219 ✔
1662
  if (ret < 0) {
718,232 ✔
1663
    return 0;
316,339 ✔
1664
  }
1665
  return *(tb_uid_t *)pStbCur->pKey;
398,032 ✔
1666
}
1667

1668
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
1,014,749 ✔
1669
  STSchema       *pTSchema = NULL;
1,014,749 ✔
1670
  SSchemaWrapper *pSW = NULL;
1,014,749 ✔
1671

1672
  pSW = metaGetTableSchema(pMeta, uid, sver, lock, NULL, 0, false);
1,014,749 ✔
1673
  if (!pSW) return NULL;
1,016,125 ✔
1674

1675
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
1,016,016 ✔
1676

1677
  tDeleteSchemaWrapper(pSW);
3,839 ✔
1678
  return pTSchema;
1,015,763 ✔
1679
}
1680

1681
/**
1682
 * Fetch rsma schema if table is rsma
1683
 */
1684
SRSchema *metaGetTbTSchemaR(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
37 ✔
1685
  SRSchema       *pRSchema = NULL;
37 ✔
1686
  SSchemaWrapper *pSW = NULL;
37 ✔
1687

1688
  if (!(pRSchema = (SRSchema *)taosMemoryCalloc(1, sizeof(SRSchema)))) goto _err;
37 ✔
1689
  if (!(pSW = metaGetTableSchema(pMeta, uid, sver, lock, (SExtSchema **)&pRSchema->extSchema, 0x01, false))) goto _err;
37 ✔
1690
  if (!(pRSchema->tSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version))) goto _err;
37 ✔
1691

1692
  if (pSW->pRsma) {
37 ✔
1693
    if (!(pRSchema->funcIds = taosMemoryCalloc(pSW->nCols, sizeof(func_id_t)))) goto _err;
37 ✔
1694
    memcpy(pRSchema->funcIds, pSW->pRsma->funcIds, pSW->nCols * sizeof(func_id_t));
37 ✔
1695

1696
    pRSchema->tbType = pSW->pRsma->tbType;
37 ✔
1697
    pRSchema->tbUid = uid;
37 ✔
1698
    tstrncpy(pRSchema->tbName, pSW->pRsma->tbName, TSDB_TABLE_NAME_LEN);
37 ✔
1699
    pRSchema->interval[0] = pSW->pRsma->interval[0];
37 ✔
1700
    pRSchema->interval[1] = pSW->pRsma->interval[1];
37 ✔
1701
  }
1702

1703
_exit:
37 ✔
1704
  tDeleteSchemaWrapper(pSW);
×
1705
  return pRSchema;
37 ✔
1706
_err:
×
1707
  tDeleteSchemaWrapper(pSW);
×
1708
  tFreeSRSchema(&pRSchema);
×
1709
  return NULL;
×
1710
}
1711

1712
int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
1,544 ✔
1713
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
1,544 ✔
1714
  if (*ppTSchema == NULL) {
1,545 ✔
1715
    return terrno;
×
1716
  }
1717
  return TSDB_CODE_SUCCESS;
1,545 ✔
1718
}
1719

1720
int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) {
1,012,385 ✔
1721
  *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock);
1,012,385 ✔
1722
  if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) {
1,013,246 ✔
1723
    return terrno;
×
1724
  }
1725
  return TSDB_CODE_SUCCESS;
1,009,612 ✔
1726
}
1727

1728
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);
1729

1730
static void metaCollectSchemaFailureSnapshot(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver) {
1 ✔
1731
  if (pMeta == NULL || pMeta->pVnode == NULL) {
1 ✔
1732
    metaDebug("schema failure snapshot skipped, metadata is unavailable");
×
1733
    return;
×
1734
  }
1735

1736
  tb_uid_t  schemaUid = suid ? suid : uid;
1 ✔
1737
  SMetaInfo table = {.version = -1, .skmVer = -1};
1 ✔
1738
  SMetaInfo owner = {.version = -1, .skmVer = -1};
1 ✔
1739
  bool      tableTruncated = false, ownerTruncated = false;
1 ✔
1740

1741
  // Sample only the in-memory cache. Even a TDB read can flush dirty pages or
1742
  // wait for memory, so diagnostics must not perform additional TDB operations.
1743
  int32_t lockCode = taosThreadRwlockTryRdlock(&pMeta->lock);
1 ✔
1744
  if (lockCode != 0) {
1 ✔
1745
    metaDebug("vgId:%d, schema failure snapshot skipped, uid:%" PRId64
×
1746
              ", suid:%" PRId64 ", sver:%d, lockCode:%d",
1747
              TD_VID(pMeta->pVnode), uid, suid, sver, lockCode);
1748
    return;
×
1749
  }
1750
  int32_t tableCode = metaCacheGetForDiagnostics(pMeta, uid, &table, &tableTruncated);
1 ✔
1751
  int32_t ownerCode = tableCode;
1 ✔
1752
  if (schemaUid == uid) {
1 ✔
1753
    owner = table;
1 ✔
1754
    ownerTruncated = tableTruncated;
1 ✔
1755
  } else {
1756
    ownerCode = metaCacheGetForDiagnostics(pMeta, schemaUid, &owner, &ownerTruncated);
×
1757
  }
1758
  metaULock(pMeta);
1 ✔
1759

1760
  // Cache misses/truncated walks do not establish that table/schema.db is missing.
1761
  metaDebug("vgId:%d, schema failure snapshot, source:cache, uid:%" PRId64
1 ✔
1762
            ", suid:%" PRId64 ", schemaUid:%" PRId64 ", sver:%d"
1763
            ", tableCacheCode:0x%x, tableCacheTruncated:%d, cachedSuid:%" PRId64
1764
            ", tableCacheMetaVer:%" PRId64 ", tableCacheSver:%d"
1765
            ", ownerCacheCode:0x%x, ownerCacheTruncated:%d"
1766
            ", ownerCacheMetaVer:%" PRId64 ", ownerCacheSver:%d",
1767
            TD_VID(pMeta->pVnode), uid, suid, schemaUid, sver,
1768
            tableCode, tableTruncated, table.suid, table.version, table.skmVer,
1769
            ownerCode, ownerTruncated, owner.version, owner.skmVer);
1770
}
1771

1772
static void metaLogSchemaFailureSnapshot(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver) {
24 ✔
1773
  enum { SCHEMA_DIAG_INTERVAL_MS = 5000 };
2 ✔
1774
  // Process-wide budget shared by all vnodes. busy also prevents overlapping
1775
  // samples when sampling/logging exceeds the interval; it protects nextMs.
1776
  static int32_t busy = 0;
2 ✔
1777
  static int64_t nextMs = 0;
2 ✔
1778
  if (!(metaDebugFlag & DEBUG_DEBUG) || atomic_val_compare_exchange_32(&busy, 0, 1) != 0) {
24 ✔
1779
    return;
23 ✔
1780
  }
1781

1782
  int32_t savedErrno = terrno;
1 ✔
1783
  int64_t now = taosGetMonoTimestampMs();
1 ✔
1784
  if (now >= nextMs) {
1 ✔
1785
    // Failed/contended attempts consume the budget too, avoiding a retry storm.
1786
    nextMs = now + SCHEMA_DIAG_INTERVAL_MS;
1 ✔
1787
    metaCollectSchemaFailureSnapshot(pMeta, suid, uid, sver);
1 ✔
1788
  }
1789
  terrno = savedErrno;
1 ✔
1790
  (void)atomic_exchange_32(&busy, 0);
1 ✔
1791
}
1792

1793
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
173,001 ✔
1794
  int32_t code = 0;
173,001 ✔
1795
  int32_t lino;
424 ✔
1796
  const int32_t requestedSver = sver;
173,001 ✔
1797
  const char   *stage = "latest-schema";
173,001 ✔
1798
  int32_t       storageCode = 0;
173,001 ✔
1799

1800
  void     *pData = NULL;
173,001 ✔
1801
  int       nData = 0;
173,201 ✔
1802
  SSkmDbKey skmDbKey;
172,952 ✔
1803
  if (sver <= 0) {
172,841 ✔
1804
    SMetaInfo info;
103,038 ✔
1805
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
103,258 ✔
1806
      sver = info.skmVer;
103,140 ✔
1807
    } else {
1808
      TBC *pSkmDbC = NULL;
99 ✔
1809
      int  c;
109 ✔
1810

1811
      skmDbKey.uid = suid ? suid : uid;
109 ✔
1812
      skmDbKey.sver = INT32_MAX;
109 ✔
1813

1814
      stage = "schema-cursor-open";
109 ✔
1815
      code = tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
109 ✔
1816
      TSDB_CHECK_CODE(code, lino, _exit);
109 ✔
1817
      metaRLock(pMeta);
109 ✔
1818

1819
      stage = "schema-cursor-seek";
109 ✔
1820
      storageCode = tdbTbcMoveTo(pSkmDbC, &skmDbKey, sizeof(skmDbKey), &c);
109 ✔
1821
      if (storageCode < 0) {
109 ✔
1822
        metaULock(pMeta);
×
1823
        tdbTbcClose(pSkmDbC);
×
1824
        code = TSDB_CODE_NOT_FOUND;
×
1825
        goto _exit;
×
1826
      }
1827

1828
      if (c == 0) {
109 ✔
1829
        metaULock(pMeta);
×
1830
        tdbTbcClose(pSkmDbC);
×
1831
        code = TSDB_CODE_FAILED;
×
1832
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
×
1833
        goto _exit;
×
1834
      }
1835

1836
      if (c < 0) {
109 ✔
1837
        int32_t ret = tdbTbcMoveToPrev(pSkmDbC);
9 ✔
1838
      }
1839

1840
      const void *pKey = NULL;
109 ✔
1841
      int32_t     nKey = 0;
109 ✔
1842
      int32_t     ret = tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);
109 ✔
1843

1844
      stage = "schema-cursor-entry";
109 ✔
1845
      storageCode = ret;
109 ✔
1846
      if (ret != 0 || ((SSkmDbKey *)pKey)->uid != skmDbKey.uid) {
109 ✔
1847
        metaULock(pMeta);
×
1848
        tdbTbcClose(pSkmDbC);
×
1849
        code = TSDB_CODE_NOT_FOUND;
×
1850
        goto _exit;
×
1851
      }
1852

1853
      sver = ((SSkmDbKey *)pKey)->sver;
109 ✔
1854

1855
      metaULock(pMeta);
109 ✔
1856
      tdbTbcClose(pSkmDbC);
109 ✔
1857
    }
1858
  }
1859

1860
  stage = "schema-version";
173,334 ✔
1861
  if (!(sver > 0)) {
172,909 ✔
1862
    code = TSDB_CODE_NOT_FOUND;
×
1863
    goto _exit;
×
1864
  }
1865

1866
  skmDbKey.uid = suid ? suid : uid;
173,088 ✔
1867
  skmDbKey.sver = sver;
173,088 ✔
1868
  stage = "schema-db-get";
173,088 ✔
1869
  metaRLock(pMeta);
173,088 ✔
1870
  storageCode = tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData);
173,295 ✔
1871
  if (storageCode < 0) {
173,187 ✔
1872
    metaULock(pMeta);
24 ✔
1873
    code = TSDB_CODE_NOT_FOUND;
24 ✔
1874
    goto _exit;
24 ✔
1875
  }
1876
  metaULock(pMeta);
173,163 ✔
1877

1878
  // decode
1879
  SDecoder        dc = {0};
173,284 ✔
1880
  SSchemaWrapper  schema;
173,291 ✔
1881
  SSchemaWrapper *pSchemaWrapper = &schema;
172,934 ✔
1882

1883
  stage = "schema-decode";
172,934 ✔
1884
  tDecoderInit(&dc, pData, nData);
172,934 ✔
1885
  code = tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
173,574 ✔
1886
  tDecoderClear(&dc);
173,574 ✔
1887
  tdbFree(pData);
173,274 ✔
1888
  if (TSDB_CODE_SUCCESS != code) {
173,173 ✔
1889
    taosMemoryFree(pSchemaWrapper->pSchema);
×
1890
    goto _exit;
×
1891
  }
1892

1893
  // convert
1894
  stage = "schema-build";
173,173 ✔
1895
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
173,173 ✔
1896
  if (pTSchema == NULL) {
173,239 ✔
1897
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1898
  }
1899

1900
  *ppTSchema = pTSchema;
173,239 ✔
1901
  taosMemoryFree(pSchemaWrapper->pSchema);
173,239 ✔
1902

1903
_exit:
173,125 ✔
1904
  if (code != TSDB_CODE_SUCCESS) {
173,202 ✔
1905
    int32_t savedErrno = terrno;
24 ✔
1906
    // Preserve the underlying TDB result: callers still receive the original
1907
    // API error, which maps failed schema reads to NOT_FOUND.
1908
    metaError("vgId:%d, schema lookup failed, stage:%s, suid:%" PRId64
26 ✔
1909
              ", uid:%" PRId64 ", schemaUid:%" PRId64
1910
              ", requestedSver:%d, resolvedSver:%d, schemaBytes:%d"
1911
              ", storageCode:0x%x(%s), code:0x%x(%s)",
1912
              TD_VID(pMeta->pVnode), stage, suid, uid, suid ? suid : uid,
1913
              requestedSver, sver, nData, storageCode, tstrerror(storageCode),
1914
              code, tstrerror(code));
1915
    // Skip extra diagnostics when NOT_FOUND masked an I/O/allocation error.
1916
    if (code == TSDB_CODE_NOT_FOUND &&
24 ✔
1917
        (storageCode == TSDB_CODE_SUCCESS || storageCode == TSDB_CODE_NOT_FOUND)) {
24 ✔
1918
      metaLogSchemaFailureSnapshot(pMeta, suid, uid, sver);
24 ✔
1919
    }
1920
    terrno = savedErrno;
24 ✔
1921
  }
1922
  return code;
173,202 ✔
1923
}
1924

1925
// N.B. Called by statusReq per second
1926
int64_t metaGetTbNum(SMeta *pMeta) {
519,781 ✔
1927
  // num of child tables (excluding normal tables , stables and others)
1928

1929
  /* int64_t num = 0; */
1930
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */
1931

1932
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
519,781 ✔
1933
}
1934

1935
void metaUpdTimeSeriesNum(SMeta *pMeta) {
320,158 ✔
1936
  int64_t nCtbTimeSeries = 0;
320,158 ✔
1937
  if (vnodeGetTimeSeriesNum(pMeta->pVnode, &nCtbTimeSeries) == 0) {
320,158 ✔
1938
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfTimeSeries, nCtbTimeSeries);
320,163 ✔
1939
  }
1940
}
320,171 ✔
1941

1942
static FORCE_INLINE int64_t metaGetTimeSeriesNumImpl(SMeta *pMeta, bool forceUpd) {
45,803 ✔
1943
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
1944
  SVnodeStats *pStats = &pMeta->pVnode->config.vndStats;
919,739 ✔
1945
  if (forceUpd || pStats->numOfTimeSeries <= 0) {
919,788 ✔
1946
    metaUpdTimeSeriesNum(pMeta);
320,118 ✔
1947
  }
1948

1949
  return pStats->numOfTimeSeries + pStats->numOfNTimeSeries;
919,793 ✔
1950
}
1951

1952
// type: 1 reported timeseries
1953
int64_t metaGetTimeSeriesNum(SMeta *pMeta, int type) {
919,739 ✔
1954
  int64_t nTimeSeries = metaGetTimeSeriesNumImpl(pMeta, false);
919,793 ✔
1955
  if (type == 1) {
919,793 ✔
1956
    atomic_store_64(&pMeta->pVnode->config.vndStats.numOfReportedTimeSeries, nTimeSeries);
519,781 ✔
1957
  }
1958
  return nTimeSeries;
919,784 ✔
1959
}
1960

1961
typedef struct {
1962
  SMeta   *pMeta;
1963
  TBC     *pCur;
1964
  tb_uid_t uid;
1965
  void    *pKey;
1966
  void    *pVal;
1967
  int      kLen;
1968
  int      vLen;
1969
} SMSmaCursor;
1970

1971
SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
×
1972
  SMSmaCursor *pSmaCur = NULL;
×
1973
  SSmaIdxKey   smaIdxKey;
×
1974
  int          ret;
×
1975
  int          c;
×
1976

1977
  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
×
1978
  if (pSmaCur == NULL) {
×
1979
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1980
    return NULL;
×
1981
  }
1982

1983
  pSmaCur->pMeta = pMeta;
×
1984
  pSmaCur->uid = uid;
×
1985
  metaRLock(pMeta);
×
1986

1987
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
×
1988
  if (ret < 0) {
×
1989
    metaULock(pMeta);
×
1990
    taosMemoryFree(pSmaCur);
×
1991
    return NULL;
×
1992
  }
1993

1994
  // move to the suid
1995
  smaIdxKey.uid = uid;
×
1996
  smaIdxKey.smaUid = INT64_MIN;
×
1997
  ret = tdbTbcMoveTo(pSmaCur->pCur, &smaIdxKey, sizeof(smaIdxKey), &c);
×
1998
  if (c > 0) {
×
1999
    ret = tdbTbcMoveToNext(pSmaCur->pCur);
×
2000
  }
2001

2002
  return pSmaCur;
×
2003
}
2004

2005
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
×
2006
  if (pSmaCur) {
×
2007
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
×
2008
    if (pSmaCur->pCur) {
×
2009
      tdbTbcClose(pSmaCur->pCur);
×
2010
      pSmaCur->pCur = NULL;
×
2011

2012
      tdbFree(pSmaCur->pKey);
×
2013
      tdbFree(pSmaCur->pVal);
×
2014
    }
2015

2016
    taosMemoryFree(pSmaCur);
×
2017
  }
2018
}
×
2019

2020
tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
×
2021
  int         ret;
×
2022
  SSmaIdxKey *pSmaIdxKey;
×
2023

2024
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
×
2025
  if (ret < 0) {
×
2026
    return 0;
×
2027
  }
2028

2029
  pSmaIdxKey = pSmaCur->pKey;
×
2030
  if (pSmaIdxKey->uid > pSmaCur->uid) {
×
2031
    return 0;
×
2032
  }
2033

2034
  return pSmaIdxKey->uid;
×
2035
}
2036

2037
STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
×
2038
  STSmaWrapper *pSW = NULL;
×
2039
  SArray       *pSmaIds = NULL;
×
2040
  SMetaReader   mr = {0};
×
2041

2042
  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
×
2043
    return NULL;
×
2044
  }
2045

2046
  pSW = taosMemoryCalloc(1, sizeof(*pSW));
×
2047
  if (!pSW) {
×
2048
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2049
    goto _err;
×
2050
  }
2051

2052
  pSW->number = taosArrayGetSize(pSmaIds);
×
2053
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));
×
2054

2055
  if (!pSW->tSma) {
×
2056
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2057
    goto _err;
×
2058
  }
2059

2060
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK, 0);
×
2061
  int64_t smaId;
×
2062
  int     smaIdx = 0;
×
2063
  STSma  *pTSma = NULL;
×
2064
  for (int i = 0; i < pSW->number; ++i) {
×
2065
    smaId = *(tb_uid_t *)taosArrayGet(pSmaIds, i);
×
2066
    if (metaReaderGetTableEntryByUid(&mr, smaId) < 0) {
×
2067
      tDecoderClear(&mr.coder);
×
2068
      metaWarn("vgId:%d, no entry for tbId:%" PRIi64 ", smaId:%" PRIi64, TD_VID(pMeta->pVnode), uid, smaId);
×
2069
      continue;
×
2070
    }
2071
    tDecoderClear(&mr.coder);
×
2072
    pTSma = pSW->tSma + smaIdx;
×
2073
    memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
2074
    if (deepCopy) {
×
2075
      if (pTSma->exprLen > 0) {
×
2076
        if (!(pTSma->expr = taosMemoryCalloc(1, pTSma->exprLen))) {
×
2077
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2078
          goto _err;
×
2079
        }
2080
        memcpy((void *)pTSma->expr, mr.me.smaEntry.tsma->expr, pTSma->exprLen);
×
2081
      }
2082
      if (pTSma->tagsFilterLen > 0) {
×
2083
        if (!(pTSma->tagsFilter = taosMemoryCalloc(1, pTSma->tagsFilterLen))) {
×
2084
          terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2085
          goto _err;
×
2086
        }
2087
      }
2088
      memcpy((void *)pTSma->tagsFilter, mr.me.smaEntry.tsma->tagsFilter, pTSma->tagsFilterLen);
×
2089
    } else {
2090
      pTSma->exprLen = 0;
×
2091
      pTSma->expr = NULL;
×
2092
      pTSma->tagsFilterLen = 0;
×
2093
      pTSma->tagsFilter = NULL;
×
2094
    }
2095

2096
    ++smaIdx;
×
2097
  }
2098

2099
  if (smaIdx <= 0) goto _err;
×
2100
  pSW->number = smaIdx;
×
2101

2102
  metaReaderClear(&mr);
×
2103
  taosArrayDestroy(pSmaIds);
×
2104
  return pSW;
×
2105
_err:
×
2106
  metaReaderClear(&mr);
×
2107
  taosArrayDestroy(pSmaIds);
×
2108
  pSW = tFreeTSmaWrapper(pSW, deepCopy);
×
2109
  return NULL;
×
2110
}
2111

2112
STSma *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid) {
×
2113
  STSma      *pTSma = NULL;
×
2114
  SMetaReader mr = {0};
×
2115
  metaReaderDoInit(&mr, pMeta, META_READER_LOCK, 0);
×
2116
  if (metaReaderGetTableEntryByUid(&mr, indexUid) < 0) {
×
2117
    metaWarn("vgId:%d, failed to get table entry for smaId:%" PRIi64, TD_VID(pMeta->pVnode), indexUid);
×
2118
    metaReaderClear(&mr);
×
2119
    return NULL;
×
2120
  }
2121
  pTSma = (STSma *)taosMemoryMalloc(sizeof(STSma));
×
2122
  if (!pTSma) {
×
2123
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2124
    metaReaderClear(&mr);
×
2125
    return NULL;
×
2126
  }
2127

2128
  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
×
2129

2130
  metaReaderClear(&mr);
×
2131
  return pTSma;
×
2132
}
2133

2134
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
×
2135
  SArray     *pUids = NULL;
×
2136
  SSmaIdxKey *pSmaIdxKey = NULL;
×
2137

2138
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
×
2139
  if (!pCur) {
×
2140
    return NULL;
×
2141
  }
2142

2143
  while (1) {
×
2144
    tb_uid_t id = metaSmaCursorNext(pCur);
×
2145
    if (id == 0) {
×
2146
      break;
×
2147
    }
2148

2149
    if (!pUids) {
×
2150
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
2151
      if (!pUids) {
×
2152
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2153
        metaCloseSmaCursor(pCur);
×
2154
        return NULL;
×
2155
      }
2156
    }
2157

2158
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
×
2159

2160
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
×
2161
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2162
      metaCloseSmaCursor(pCur);
×
2163
      taosArrayDestroy(pUids);
×
2164
      return NULL;
×
2165
    }
2166
  }
2167

2168
  metaCloseSmaCursor(pCur);
×
2169
  return pUids;
×
2170
}
2171

2172
SArray *metaGetSmaTbUids(SMeta *pMeta) {
×
2173
  SArray     *pUids = NULL;
×
2174
  SSmaIdxKey *pSmaIdxKey = NULL;
×
2175
  tb_uid_t    lastUid = 0;
×
2176

2177
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
×
2178
  if (!pCur) {
×
2179
    return NULL;
×
2180
  }
2181

2182
  while (1) {
×
2183
    tb_uid_t uid = metaSmaCursorNext(pCur);
×
2184
    if (uid == 0) {
×
2185
      break;
×
2186
    }
2187

2188
    if (lastUid == uid) {
×
2189
      continue;
×
2190
    }
2191

2192
    lastUid = uid;
×
2193

2194
    if (!pUids) {
×
2195
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
×
2196
      if (!pUids) {
×
2197
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2198
        metaCloseSmaCursor(pCur);
×
2199
        return NULL;
×
2200
      }
2201
    }
2202

2203
    if (!taosArrayPush(pUids, &uid)) {
×
2204
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2205
      metaCloseSmaCursor(pCur);
×
2206
      taosArrayDestroy(pUids);
×
2207
      return NULL;
×
2208
    }
2209
  }
2210

2211
  metaCloseSmaCursor(pCur);
×
2212
  return pUids;
×
2213
}
2214

2215
#endif
2216

2217
const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) {
3,658,195 ✔
2218
  STag *tag = (STag *)pTag;
3,658,195 ✔
2219
  if (type == TSDB_DATA_TYPE_JSON) {
3,658,195 ✔
2220
    return tag;
3,207 ✔
2221
  }
2222
  bool find = tTagGet(tag, val);
3,654,988 ✔
2223

2224
  if (!find) {
3,658,063 ✔
2225
    return NULL;
16,660 ✔
2226
  }
2227

2228
  return val;
3,588,567 ✔
2229
}
2230

2231
typedef struct {
2232
  SMeta   *pMeta;
2233
  TBC     *pCur;
2234
  tb_uid_t suid;
2235
  int16_t  cid;
2236
  int16_t  type;
2237
  void    *pKey;
2238
  void    *pVal;
2239
  int32_t  kLen;
2240
  int32_t  vLen;
2241
} SIdxCursor;
2242

2243
int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
10 ✔
2244
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
10 ✔
2245
  SMetaFltParam *param = arg;
10 ✔
2246
  int32_t        ret = 0;
10 ✔
2247

2248
  SIdxCursor *pCursor = NULL;
10 ✔
2249
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
10 ✔
2250
  if (pCursor == NULL) {
10 ✔
2251
    return terrno;
×
2252
  }
2253
  pCursor->pMeta = pMeta;
10 ✔
2254
  pCursor->suid = param->suid;
10 ✔
2255
  pCursor->cid = param->cid;
10 ✔
2256
  pCursor->type = param->type;
10 ✔
2257

2258
  metaRLock(pMeta);
10 ✔
2259
  ret = tdbTbcOpen(pMeta->pBtimeIdx, &pCursor->pCur, NULL);
10 ✔
2260
  if (ret != 0) {
10 ✔
2261
    goto END;
×
2262
  }
2263
  int64_t uidLimit = param->reverse ? INT64_MAX : 0;
10 ✔
2264

2265
  SBtimeIdxKey  btimeKey = {.btime = *(int64_t *)(param->val), .uid = uidLimit};
10 ✔
2266
  SBtimeIdxKey *pBtimeKey = &btimeKey;
10 ✔
2267

2268
  int cmp = 0;
10 ✔
2269
  if (tdbTbcMoveTo(pCursor->pCur, &btimeKey, sizeof(btimeKey), &cmp) < 0) {
10 ✔
2270
    goto END;
×
2271
  }
2272

2273
  int32_t valid = 0;
10 ✔
2274
  int32_t count = 0;
10 ✔
2275

2276
  static const int8_t TRY_ERROR_LIMIT = 1;
×
2277
  do {
38,149 ✔
2278
    void   *entryKey = NULL;
38,159 ✔
2279
    int32_t nEntryKey = -1;
38,101 ✔
2280
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
38,101 ✔
2281
    if (valid < 0) break;
38,992 ✔
2282

2283
    SBtimeIdxKey *p = entryKey;
38,982 ✔
2284
    if (count > TRY_ERROR_LIMIT) break;
38,982 ✔
2285

2286
    terrno = TSDB_CODE_SUCCESS;
38,982 ✔
2287
    int32_t cmp = (*param->filterFunc)((void *)&p->btime, (void *)&pBtimeKey->btime, param->type);
38,978 ✔
2288
    if (terrno != TSDB_CODE_SUCCESS) {
38,438 ✔
2289
      ret = terrno;
×
2290
      break;
×
2291
    }
2292
    if (cmp == 0) {
38,445 ✔
2293
      if (taosArrayPush(pUids, &p->uid) == NULL) {
76,717 ✔
2294
        ret = terrno;
×
2295
        break;
×
2296
      }
2297
    } else {
2298
      if (param->equal == true) {
×
2299
        if (count > TRY_ERROR_LIMIT) break;
×
2300
        count++;
×
2301
      }
2302
    }
2303
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
38,272 ✔
2304
    if (valid < 0) break;
38,189 ✔
2305
  } while (1);
2306

2307
END:
30 ✔
2308
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
10 ✔
2309
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
10 ✔
2310
  taosMemoryFree(pCursor);
10 ✔
2311
  return ret;
10 ✔
2312
}
2313

2314
int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
2315
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
2316
  SMetaFltParam *param = arg;
×
2317
  int32_t        ret = 0;
×
2318
  char          *buf = NULL;
×
2319

2320
  STagIdxKey *pKey = NULL;
×
2321
  int32_t     nKey = 0;
×
2322

2323
  SIdxCursor *pCursor = NULL;
×
2324
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
2325
  if (pCursor == NULL) {
×
2326
    return terrno;
×
2327
  }
2328
  pCursor->pMeta = pMeta;
×
2329
  pCursor->suid = param->suid;
×
2330
  pCursor->cid = param->cid;
×
2331
  pCursor->type = param->type;
×
2332

2333
  char *pName = param->val;
×
2334

2335
  metaRLock(pMeta);
×
2336
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
×
2337
  if (ret != 0) {
×
2338
    goto END;
×
2339
  }
2340

2341
  int cmp = 0;
×
2342
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
×
2343
    goto END;
×
2344
  }
2345
  int32_t valid = 0;
×
2346
  int32_t count = 0;
×
2347

2348
  int32_t TRY_ERROR_LIMIT = 1;
×
2349
  do {
×
2350
    void   *pEntryKey = NULL, *pEntryVal = NULL;
×
2351
    int32_t nEntryKey = -1, nEntryVal = 0;
×
2352
    valid = tdbTbcGet(pCursor->pCur, (const void **)pEntryKey, &nEntryKey, (const void **)&pEntryVal, &nEntryVal);
×
2353
    if (valid < 0) break;
×
2354

2355
    if (count > TRY_ERROR_LIMIT) break;
×
2356

2357
    char *pTableKey = (char *)pEntryKey;
×
2358
    terrno = TSDB_CODE_SUCCESS;
×
2359
    cmp = (*param->filterFunc)(pTableKey, pName, pCursor->type);
×
2360
    if (terrno != TSDB_CODE_SUCCESS) {
×
2361
      ret = terrno;
×
2362
      goto END;
×
2363
    }
2364
    if (cmp == 0) {
×
2365
      tb_uid_t tuid = *(tb_uid_t *)pEntryVal;
×
2366
      if (taosArrayPush(pUids, &tuid) == NULL) {
×
2367
        ret = terrno;
×
2368
        goto END;
×
2369
      }
2370
    } else {
2371
      if (param->equal == true) {
×
2372
        if (count > TRY_ERROR_LIMIT) break;
×
2373
        count++;
×
2374
      }
2375
    }
2376
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
×
2377
    if (valid < 0) {
×
2378
      break;
×
2379
    }
2380
  } while (1);
2381

2382
END:
×
2383
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
2384
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
2385
  taosMemoryFree(buf);
×
2386
  taosMemoryFree(pKey);
×
2387

2388
  taosMemoryFree(pCursor);
×
2389

2390
  return ret;
×
2391
}
2392
int32_t metaFilterTtl(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
×
2393
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
×
2394
  SMetaFltParam *param = arg;
×
2395
  int32_t        ret = 0;
×
2396
  char          *buf = NULL;
×
2397

2398
  STtlIdxKey *pKey = NULL;
×
2399
  int32_t     nKey = 0;
×
2400

2401
  SIdxCursor *pCursor = NULL;
×
2402
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
×
2403
  if (pCursor == NULL) {
×
2404
    return terrno;
×
2405
  }
2406
  pCursor->pMeta = pMeta;
×
2407
  pCursor->suid = param->suid;
×
2408
  pCursor->cid = param->cid;
×
2409
  pCursor->type = param->type;
×
2410

2411
  metaRLock(pMeta);
×
2412
  // ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);
2413

2414
END:
×
2415
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
×
2416
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
×
2417
  taosMemoryFree(buf);
×
2418
  taosMemoryFree(pKey);
×
2419

2420
  taosMemoryFree(pCursor);
×
2421

2422
  return ret;
×
2423
  // impl later
2424
  return 0;
2425
}
2426
int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) {
6,475 ✔
2427
  SMeta         *pMeta = ((SVnode *)pVnode)->pMeta;
6,475 ✔
2428
  SMetaFltParam *param = arg;
6,475 ✔
2429

2430
  SMetaEntry oStbEntry = {0};
6,475 ✔
2431
  int32_t    code = 0;
6,475 ✔
2432
  void      *pData = NULL;
6,475 ✔
2433
  int        nData = 0;
6,462 ✔
2434

2435
  SDecoder    dc = {0};
6,462 ✔
2436
  STbDbKey    tbDbKey = {0};
6,462 ✔
2437
  STagIdxKey *pKey = NULL;
6,460 ✔
2438
  int32_t     nKey = 0;
6,454 ✔
2439

2440
  SIdxCursor *pCursor = NULL;
6,455 ✔
2441
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
6,455 ✔
2442
  if (!pCursor) {
6,453 ✔
2443
    return terrno;
×
2444
  }
2445
  pCursor->pMeta = pMeta;
6,453 ✔
2446
  pCursor->suid = param->suid;
6,453 ✔
2447
  pCursor->cid = param->cid;
6,453 ✔
2448
  pCursor->type = param->type;
6,453 ✔
2449

2450
  metaRLock(pMeta);
6,453 ✔
2451

2452
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END);
6,482 ✔
2453

2454
  tbDbKey.uid = param->suid;
6,483 ✔
2455
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
6,483 ✔
2456

2457
  TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END);
6,483 ✔
2458

2459
  tDecoderInit(&dc, pData, nData);
6,485 ✔
2460

2461
  code = metaDecodeEntry(&dc, &oStbEntry);
6,456 ✔
2462
  if (code) {
6,491 ✔
2463
    tDecoderClear(&dc);
×
2464
    goto END;
×
2465
  }
2466

2467
  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
6,491 ✔
2468
    TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END);
×
2469
  }
2470

2471
  code = TSDB_CODE_INVALID_PARA;
6,491 ✔
2472

2473
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
9,689 ✔
2474
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
9,635 ✔
2475
    if (IS_IDX_ON(schema)) {
9,635 ✔
2476
      if (schema->colId == param->cid && param->type == schema->type) {
9,311 ✔
2477
        code = 0;
6,429 ✔
2478
        break;
6,429 ✔
2479
      }
2480
    }
2481
  }
2482

2483
  TAOS_CHECK_GOTO(code, NULL, END);
6,491 ✔
2484

2485
  code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
6,437 ✔
2486
  if (code != 0) {
6,436 ✔
2487
    TAOS_CHECK_GOTO(terrno, NULL, END);
×
2488
  }
2489

2490
  int32_t nTagData = 0;
6,436 ✔
2491
  void   *tagData = NULL;
6,436 ✔
2492

2493
  if (param->val == NULL) {
6,436 ✔
2494
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
×
2495
    goto END;
×
2496
  } else {
2497
    if (IS_VAR_DATA_TYPE(param->type)) {
6,436 ✔
2498
      // NCHAR probe constants are converted to UCS-4 by index execution before
2499
      // parameter initialization; the tag-index key uses the same encoding.
2500
      tagData = varDataVal(param->val);
706 ✔
2501
      nTagData = varDataLen(param->val);
706 ✔
2502
    } else {
2503
      tagData = param->val;
5,730 ✔
2504
      nTagData = tDataTypes[param->type].bytes;
5,730 ✔
2505
    }
2506
  }
2507

2508
  TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
6,440 ✔
2509
                                      param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey),
2510
                  NULL, END);
2511

2512
  int cmp = 0;
6,436 ✔
2513
  TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END);
6,437 ✔
2514

2515
  int     count = 0;
6,429 ✔
2516
  int32_t valid = 0;
6,469 ✔
2517
  bool    found = false;
6,429 ✔
2518

2519
  static const int8_t TRY_ERROR_LIMIT = 1;
2520

2521
  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
2522
  /// target:                        [suid, cid2, type2]
2523
  int diffCidCount = 0;
6,429 ✔
2524
  do {
77,900 ✔
2525
    void   *entryKey = NULL, *entryVal = NULL;
84,329 ✔
2526
    int32_t nEntryKey, nEntryVal;
84,316 ✔
2527

2528
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
84,314 ✔
2529
    if (valid < 0) {
84,273 ✔
2530
      break;
3,206 ✔
2531
    }
2532
    if (count > TRY_ERROR_LIMIT) {
81,059 ✔
2533
      break;
1,345 ✔
2534
    }
2535

2536
    STagIdxKey *p = entryKey;
79,714 ✔
2537
    if (p == NULL) break;
79,714 ✔
2538

2539
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
79,714 ✔
2540
      if (found == true) break;  //
2,251 ✔
2541
      if (diffCidCount > TRY_ERROR_LIMIT) break;
373 ✔
2542
      diffCidCount++;
373 ✔
2543
      count++;
373 ✔
2544
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
373 ✔
2545
      if (valid < 0) {
361 ✔
2546
        code = valid;
×
2547
        break;
×
2548
      } else {
2549
        continue;
361 ✔
2550
      }
2551
    }
2552

2553
    terrno = TSDB_CODE_SUCCESS;
77,463 ✔
2554
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
77,447 ✔
2555
    if (terrno != TSDB_CODE_SUCCESS) {
77,443 ✔
2556
      TAOS_CHECK_GOTO(terrno, NULL, END);
×
2557
      break;
×
2558
    }
2559
    if (cmp == 0) {
77,448 ✔
2560
      // match
2561
      tb_uid_t tuid = 0;
71,303 ✔
2562
      if (IS_VAR_DATA_TYPE(pKey->type)) {
71,288 ✔
2563
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
767 ✔
2564
      } else {
2565
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
70,521 ✔
2566
      }
2567
      if (taosArrayPush(pUids, &tuid) == NULL) {
71,442 ✔
2568
        TAOS_CHECK_GOTO(terrno, NULL, END);
×
2569
      }
2570
      found = true;
71,442 ✔
2571
    } else {
2572
      if (param->equal == true) {
6,145 ✔
2573
        if (count > TRY_ERROR_LIMIT) break;
3,618 ✔
2574
        count++;
3,618 ✔
2575
      }
2576
    }
2577
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
77,585 ✔
2578
    if (valid < 0) {
77,550 ✔
2579
      code = valid;
×
2580
      break;
×
2581
    }
2582
  } while (1);
2583

2584
END:
6,508 ✔
2585
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
6,484 ✔
2586
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
6,481 ✔
2587
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
6,479 ✔
2588
  tDecoderClear(&dc);
6,479 ✔
2589
  tdbFree(pData);
6,489 ✔
2590

2591
  taosMemoryFree(pKey);
6,476 ✔
2592

2593
  taosMemoryFree(pCursor);
6,475 ✔
2594

2595
  return code;
6,483 ✔
2596
}
2597

2598
static int32_t metaGetTableTagByUidVersion(SMeta *pMeta, int64_t suid, int64_t uid, int64_t version, void** tag) {
124,274 ✔
2599
  void   *val = NULL;
124,274 ✔
2600
  int32_t len = 0;
124,284 ✔
2601
  int ret = 0;
124,292 ✔
2602

2603
  if (version != -1) {
124,292 ✔
2604
    SMetaReader mr = {0};
89 ✔
2605
    metaReaderDoInit(&mr, pMeta, META_READER_NOLOCK, 0);
89 ✔
2606
    if (getUidVersion(&mr, &version, uid) != 0) {
89 ✔
2607
      version = -1;
×
2608
    }
2609

2610
    ret = metaGetTableEntryByVersion(&mr, version, uid);
89 ✔
2611
    if (ret == 0) {
89 ✔
2612
      // ctbEntry/ntbEntry share a union: child tables carry tags in ctbEntry.pTags, normal and
2613
      // virtual-normal tables carry owned tags in ntbEntry.pTags (NULL when tag-less).
2614
      const STag *pTags = NULL;
89 ✔
2615
      if (mr.me.type == TSDB_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
89 ✔
2616
        pTags = (const STag *)mr.me.ntbEntry.pTags;
×
2617
      } else {
2618
        pTags = (const STag *)mr.me.ctbEntry.pTags;
89 ✔
2619
      }
2620
      if (pTags != NULL) {
89 ✔
2621
        val = (void *)pTags;
89 ✔
2622
        len = pTags->len;
89 ✔
2623
        *tag = taosMemoryMalloc(len);
89 ✔
2624
        if (*tag) {
88 ✔
2625
          memcpy(*tag, val, len);
89 ✔
2626
        } else {
2627
          ret = terrno;
×
2628
        }
2629
      }
2630
    }
2631
    metaReaderClear(&mr);
89 ✔
2632
  } else {
2633
    SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
124,203 ✔
2634
    ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), &val, &len);
124,198 ✔
2635
    if (ret == 0) {
124,343 ✔
2636
      *tag = taosMemoryMalloc(len);
124,296 ✔
2637
      if (*tag) {
124,257 ✔
2638
        memcpy(*tag, val, len);
124,328 ✔
2639
      } else {
2640
        ret = terrno;
×
2641
      }
2642
      tdbFree(val);
124,328 ✔
2643
    } else {
2644
      // normal / virtual-normal tables are not indexed in pCtbIdx; fetch the entry directly and
2645
      // copy the owned tags so tag-cond pruning sees them (NULL when tag-less).
2646
      SMetaEntry *pEntry = NULL;
47 ✔
2647
      ret = 0;
47 ✔
2648
      if (metaFetchEntryByUid(pMeta, uid, &pEntry) == 0 && pEntry != NULL) {
47 ✔
2649
        if (pEntry->type == TSDB_NORMAL_TABLE || pEntry->type == TSDB_VIRTUAL_NORMAL_TABLE) {
46 ✔
2650
          if (pEntry->ntbEntry.pTags != NULL) {
46 ✔
2651
            const STag *pTags = (const STag *)pEntry->ntbEntry.pTags;
16 ✔
2652
            *tag = taosMemoryMalloc(pTags->len);
16 ✔
2653
            if (*tag) {
16 ✔
2654
              memcpy(*tag, pTags, pTags->len);
16 ✔
2655
            } else {
2656
              ret = terrno;
×
2657
            }
2658
          }
2659
          metaFetchEntryFree(&pEntry);
46 ✔
2660
        } else {
2661
          // a non-ntb entry (e.g. a child table) missing from pCtbIdx is an index inconsistency --
2662
          // surface the original lookup failure instead of silently treating it as tag-less.
2663
          int32_t type = pEntry->type;
×
2664
          metaFetchEntryFree(&pEntry);
×
2665
          ret = TSDB_CODE_INTERNAL_ERROR;
×
2666
          metaError("vgId:%d, table uid:%" PRId64 " type:%d missing from ctbIdx", TD_VID(pMeta->pVnode), uid, type);
×
2667
        }
2668
      }
2669
    }
2670
  }
2671

2672
  return ret;
124,395 ✔
2673
}
2674

2675
/**
2676
 * batch-meta-txn: if uid has an uncommitted PRE_ALTER, return the old tag blob
2677
 * from pTbDb at txnOrigVer so that non-owning readers see the pre-alter value.
2678
 * Returns a taosMemoryMalloc'd buffer on redirect (caller owns), NULL otherwise.
2679
 * Must be called under metaRLock (held by the caller).
2680
 */
2681
static void *metaGetOldTagIfPreAlter(SMeta *pMeta, tb_uid_t uid, int32_t *pOutLen) {
3 ✔
2682
  void   *pTxnVal = NULL;
3 ✔
2683
  int32_t txnValLen = 0;
3 ✔
2684
  if (tdbTbGet(pMeta->pTxnIdx, &uid, sizeof(uid), &pTxnVal, &txnValLen) != 0) {
3 ✔
2685
    return NULL;
×
2686
  }
2687
  STxnIdxVal txnVal = *(const STxnIdxVal *)pTxnVal;
3 ✔
2688
  tdbFree(pTxnVal);
3 ✔
2689

2690
  if (txnVal.txnStatus != META_TXN_PRE_ALTER || txnVal.txnOrigVer < 0) {
3 ✔
2691
    return NULL;
3 ✔
2692
  }
2693
  // If the txn is already committed the dirty pCtbIdx value is correct  -  no redirect.
2694
  if (metaGetTxnMetaStatus(pMeta, txnVal.txnId) == TXN_META_COMMITTED) {
×
2695
    return NULL;
×
2696
  }
2697

2698
  // Read old entry from pTbDb at txnOrigVer.
2699
  void    *pOldBuf = NULL;
×
2700
  int32_t  oldLen = 0;
×
2701
  STbDbKey oldKey = {.version = txnVal.txnOrigVer, .uid = uid};
×
2702
  if (tdbTbGet(pMeta->pTbDb, &oldKey, sizeof(oldKey), &pOldBuf, &oldLen) != 0) {
×
2703
    return NULL;
×
2704
  }
2705

2706
  SDecoder   dc = {0};
×
2707
  SMetaEntry me = {0};
×
2708
  tDecoderInit(&dc, pOldBuf, oldLen);
×
2709
  void *result = NULL;
×
2710
  if (metaDecodeEntry(&dc, &me) == 0) {
×
2711
    // ctbEntry/ntbEntry share a union: child tables carry tags in ctbEntry.pTags, normal and
2712
    // virtual-normal tables carry owned tags in ntbEntry.pTags.
2713
    const STag *pOldTags = NULL;
×
2714
    if (me.type == TSDB_CHILD_TABLE || me.type == TSDB_VIRTUAL_CHILD_TABLE) {
×
2715
      pOldTags = (const STag *)me.ctbEntry.pTags;
×
2716
    } else if (me.type == TSDB_NORMAL_TABLE || me.type == TSDB_VIRTUAL_NORMAL_TABLE) {
×
2717
      pOldTags = (const STag *)me.ntbEntry.pTags;
×
2718
    }
2719
    if (pOldTags != NULL) {
×
2720
      result = taosMemoryMalloc(pOldTags->len);
×
2721
      if (result != NULL) {
×
2722
        memcpy(result, pOldTags, pOldTags->len);
×
2723
        *pOutLen = pOldTags->len;
×
2724
      }
2725
    }
2726
  }
2727
  tDecoderClear(&dc);
×
2728
  tdbFree(pOldBuf);
×
2729
  return result;
×
2730
}
2731
int32_t metaGetTableTagsByUidsVersion(void *pVnode, int64_t suid, SArray *uidList, int64_t version) {
14,205 ✔
2732
  SMeta        *pMeta = ((SVnode *)pVnode)->pMeta;
14,205 ✔
2733
  const int32_t LIMIT = 128;
14,205 ✔
2734

2735
  int32_t isLock = false;
14,205 ✔
2736
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
14,205 ✔
2737
  for (int i = 0; i < sz; i++) {
138,673 ✔
2738
    STUidTagInfo *p = taosArrayGet(uidList, i);
124,460 ✔
2739
    if (p->pTagVal != NULL) continue;
124,469 ✔
2740

2741
    if (i % LIMIT == 0) {
124,469 ✔
2742
      if (isLock) metaULock(pMeta);
14,350 ✔
2743

2744
      metaRLock(pMeta);
14,350 ✔
2745
      isLock = true;
14,351 ✔
2746
    }
2747
    // batch-meta-txn: redirect uncommitted PRE_ALTER to the old tag
2748
    if (metaHasPendingTxnEntries(pMeta)) {
124,475 ✔
2749
      int32_t oldLen = 0;
×
2750
      void   *pOldTags = metaGetOldTagIfPreAlter(pMeta, p->uid, &oldLen);
×
2751
      if (pOldTags != NULL) {
×
2752
        p->pTagVal = pOldTags;
×
2753
        continue;
×
2754
      }
2755
    }
2756
    int32_t code = metaGetTableTagByUidVersion(pMeta, suid, p->uid, version, &p->pTagVal);
124,474 ✔
2757
    if (code != 0) {
124,467 ✔
2758
      metaError("vgId:%d, failed to table tags, code:%d, suid: %" PRId64 ", uid: %" PRId64 " version: %" PRId64, TD_VID(pMeta->pVnode), code, suid, p->uid, version);
16 ✔
2759
    }
2760
  }
2761
  if (isLock) metaULock(pMeta);
14,213 ✔
2762
  return 0;
14,203 ✔
2763
}
2764

2765
int32_t metaGetTableTags(void *pVnode, uint64_t suid, SArray *pUidTagInfo, int64_t txnId) {
63,499 ✔
2766
  SMeta       *pMeta = ((SVnode *)pVnode)->pMeta;
63,499 ✔
2767
  SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, suid, 1, txnId);
63,499 ✔
2768
  if (!pCur) {
63,525 ✔
2769
    TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
2770
  }
2771

2772
  while (1) {
2,052,285 ✔
2773
    tb_uid_t uid = metaCtbCursorNext(pCur);
2,114,642 ✔
2774
    if (uid == 0) {
2,114,278 ✔
2775
      metaDebug("got uid 0 and uidTagSize:%d", (int32_t)taosArrayGetSize(pUidTagInfo));
63,557 ✔
2776
      break;
63,553 ✔
2777
    }
2778

2779
    STUidTagInfo info = {.uid = uid};
2,050,721 ✔
2780

2781
    // batch-meta-txn: redirect uncommitted PRE_ALTER to the old tag blob
2782
    if (metaHasPendingTxnEntries(pMeta)) {
2,050,194 ✔
2783
      int32_t oldLen = 0;
3 ✔
2784
      void   *pOldTags = metaGetOldTagIfPreAlter(pMeta, uid, &oldLen);
3 ✔
2785
      if (pOldTags != NULL) {
3 ✔
2786
        info.pTagVal = pOldTags;
×
2787
        if (taosArrayPush(pUidTagInfo, &info) == NULL) {
×
2788
          taosMemoryFree(pOldTags);
×
2789
          metaCloseCtbCursor(pCur);
×
2790
          return terrno;
×
2791
        }
2792
        continue;
×
2793
      }
2794
    }
2795

2796
    info.pTagVal = taosMemoryMalloc(pCur->vLen);
2,050,194 ✔
2797
    if (!info.pTagVal) {
2,051,225 ✔
2798
      metaCloseCtbCursor(pCur);
×
2799
      return terrno;
×
2800
    }
2801
    memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
2,051,225 ✔
2802
    if (taosArrayPush(pUidTagInfo, &info) == NULL) {
2,051,133 ✔
2803
      taosMemoryFreeClear(info.pTagVal);
×
2804
      metaCloseCtbCursor(pCur);
×
2805
      return terrno;
×
2806
    }
2807
  }
2808
  metaCloseCtbCursor(pCur);
63,554 ✔
2809
  return TSDB_CODE_SUCCESS;
63,540 ✔
2810
}
2811

2812
int32_t metaFlagCache(SVnode *pVnode) {
×
2813
  SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, 0);
×
2814
  if (!pCur) {
×
2815
    return terrno;
×
2816
  }
2817

2818
  SArray *suids = NULL;
×
2819
  while (1) {
×
2820
    tb_uid_t id = metaStbCursorNext(pCur);
×
2821
    if (id == 0) {
×
2822
      break;
×
2823
    }
2824

2825
    if (!suids) {
×
2826
      suids = taosArrayInit(8, sizeof(tb_uid_t));
×
2827
      if (!suids) {
×
2828
        metaCloseStbCursor(pCur);
×
2829
        return terrno;
×
2830
      }
2831
    }
2832

2833
    if (taosArrayPush(suids, &id) == NULL) {
×
2834
      metaCloseStbCursor(pCur);
×
2835
      taosArrayDestroy(suids);
×
2836
      return terrno;
×
2837
    }
2838
  }
2839

2840
  metaCloseStbCursor(pCur);
×
2841

2842
  for (int idx = 0; suids && idx < TARRAY_SIZE(suids); ++idx) {
×
2843
    tb_uid_t id = ((tb_uid_t *)TARRAY_DATA(suids))[idx];
×
2844
    STsdb   *pTsdb = pVnode->pTsdb;
×
2845
    SMeta   *pMeta = pVnode->pMeta;
×
2846
    SArray  *uids = NULL;
×
2847

2848
    int32_t code = metaGetChildUidsOfSuperTable(pMeta, id, &uids);
×
2849
    if (code) {
×
2850
      metaError("vgId:%d, failed to get subtables, suid:%" PRId64 " since %s.", TD_VID(pVnode), id, tstrerror(code));
×
2851

2852
      taosArrayDestroy(uids);
×
2853
      taosArrayDestroy(suids);
×
2854

2855
      return code;
×
2856
    }
2857

2858
    if (uids && TARRAY_SIZE(uids) > 0) {
×
2859
      STSchema *pTSchema = NULL;
×
2860

2861
      code = metaGetTbTSchemaEx(pMeta, id, id, -1, &pTSchema);
×
2862
      if (code) {
×
2863
        metaError("vgId:%d, failed to get schema, suid:%" PRId64 " since %s.", TD_VID(pVnode), id, tstrerror(code));
×
2864

2865
        taosArrayDestroy(uids);
×
2866
        taosArrayDestroy(suids);
×
2867

2868
        return code;
×
2869
      }
2870

2871
      int32_t nCol = pTSchema->numOfCols;
×
2872
      for (int32_t i = 0; i < nCol; ++i) {
×
2873
        int16_t cid = pTSchema->columns[i].colId;
×
2874
        int8_t  col_type = pTSchema->columns[i].type;
×
2875

2876
        code = tsdbCacheNewSTableColumn(pTsdb, uids, cid, col_type);
×
2877
        if (code) {
×
2878
          metaError("vgId:%d, failed to flag cache, suid:%" PRId64 " since %s.", TD_VID(pVnode), id, tstrerror(code));
×
2879

2880
          tDestroyTSchema(pTSchema);
×
2881
          taosArrayDestroy(uids);
×
2882
          taosArrayDestroy(suids);
×
2883

2884
          return code;
×
2885
        }
2886
      }
2887

2888
      tDestroyTSchema(pTSchema);
×
2889
    }
2890

2891
    taosArrayDestroy(uids);
×
2892
  }
2893

2894
  taosArrayDestroy(suids);
×
2895

2896
  return TSDB_CODE_SUCCESS;
×
2897
}
2898

2899
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
6,036,755 ✔
2900
  int32_t code = 0;
6,036,755 ✔
2901
  void   *pData = NULL;
6,036,755 ✔
2902
  int     nData = 0;
6,037,058 ✔
2903
  int     lock = 0;
6,037,274 ✔
2904

2905
  if (pReader && !(pReader->flags & META_READER_NOLOCK)) {
6,037,274 ✔
2906
    lock = 1;
1,683,723 ✔
2907
  }
2908

2909
  if (!lock) metaRLock(pMeta);
6,036,434 ✔
2910

2911
  // search cache
2912
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
6,037,750 ✔
2913
    if (!lock) metaULock(pMeta);
5,555,240 ✔
2914
    goto _exit;
5,555,278 ✔
2915
  }
2916

2917
  // search TDB
2918
  if ((code = tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData)) < 0) {
483,203 ✔
2919
    // not found
2920
    if (!lock) metaULock(pMeta);
468,251 ✔
2921
    goto _exit;
468,296 ✔
2922
  }
2923

2924
  if (!lock) metaULock(pMeta);
15,055 ✔
2925

2926
  pInfo->uid = uid;
15,055 ✔
2927
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
15,055 ✔
2928
  pInfo->version = ((SUidIdxVal *)pData)->version;
15,055 ✔
2929
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;
15,055 ✔
2930

2931
  if (lock) {
15,055 ✔
2932
    metaULock(pReader->pMeta);
13,425 ✔
2933
    // metaReaderReleaseLock(pReader);
2934
  }
2935
  // upsert the cache
2936
  metaWLock(pMeta);
15,055 ✔
2937
  int32_t ret = metaCacheUpsert(pMeta, pInfo);
15,036 ✔
2938
  if (ret != 0) {
15,033 ✔
2939
    metaError("vgId:%d, failed to upsert cache, uid:%" PRId64, TD_VID(pMeta->pVnode), uid);
×
2940
  }
2941
  metaULock(pMeta);
15,033 ✔
2942

2943
  if (lock) {
15,035 ✔
2944
    metaRLock(pReader->pMeta);
13,426 ✔
2945
  }
2946

2947
_exit:
5,994,744 ✔
2948
  tdbFree(pData);
6,038,113 ✔
2949
  return code;
6,038,034 ✔
2950
}
2951

2952
int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t *numOfCols, int8_t *flags) {
844,011 ✔
2953
  int32_t code = 0;
844,011 ✔
2954

2955
  if (!numOfTables && !numOfCols) goto _exit;
844,011 ✔
2956

2957
  SVnode *pVnodeObj = pVnode;
844,011 ✔
2958
  metaRLock(pVnodeObj->pMeta);
844,011 ✔
2959

2960
  // fast path: search cache
2961
  SMetaStbStats state = {0};
844,143 ✔
2962
  if (metaStatsCacheGet(pVnodeObj->pMeta, uid, &state) == TSDB_CODE_SUCCESS) {
844,103 ✔
2963
    metaULock(pVnodeObj->pMeta);
820,361 ✔
2964
    if (numOfTables) *numOfTables = state.ctbNum;
820,394 ✔
2965
    if (numOfCols) *numOfCols = state.colNum;
820,394 ✔
2966
    if (flags) *flags = state.flags;
820,394 ✔
2967
    goto _exit;
820,394 ✔
2968
  }
2969

2970
  // slow path: search TDB
2971
  int64_t ctbNum = 0;
23,782 ✔
2972
  int32_t colNum = 0;
23,782 ✔
2973
  int64_t keep = 0;
23,782 ✔
2974
  int8_t  flag = 0;
23,786 ✔
2975

2976
  code = vnodeGetCtbNum(pVnode, uid, &ctbNum);
23,787 ✔
2977
  if (TSDB_CODE_SUCCESS == code) {
23,786 ✔
2978
    code = vnodeGetStbColumnNum(pVnode, uid, &colNum);
23,786 ✔
2979
  }
2980
  if (TSDB_CODE_SUCCESS == code) {
23,786 ✔
2981
    code = vnodeGetStbInfo(pVnode, uid, &keep, &flag);
23,786 ✔
2982
  }
2983
  metaULock(pVnodeObj->pMeta);
23,787 ✔
2984
  if (TSDB_CODE_SUCCESS != code) {
23,787 ✔
2985
    goto _exit;
×
2986
  }
2987

2988
  if (numOfTables) *numOfTables = ctbNum;
23,787 ✔
2989
  if (numOfCols) *numOfCols = colNum;
23,787 ✔
2990
  if (flags) *flags = flag;
23,787 ✔
2991

2992
  state.uid = uid;
23,787 ✔
2993
  state.ctbNum = ctbNum;
23,787 ✔
2994
  state.colNum = colNum;
23,787 ✔
2995
  state.flags = flag;
23,787 ✔
2996
  state.keep = keep;
23,787 ✔
2997
  // upsert the cache
2998
  metaWLock(pVnodeObj->pMeta);
23,787 ✔
2999

3000
  int32_t ret = metaStatsCacheUpsert(pVnodeObj->pMeta, &state);
23,787 ✔
3001
  if (ret) {
23,787 ✔
3002
    metaError("failed to upsert stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64 ", flags:%" PRIi8,
×
3003
              uid, ctbNum, colNum, keep, flag);
3004
  }
3005

3006
  metaULock(pVnodeObj->pMeta);
23,787 ✔
3007

3008
_exit:
844,181 ✔
3009
  return code;
844,177 ✔
3010
}
3011

3012
void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t deltaCtb, int32_t deltaCol, int64_t deltaKeep) {
405,815 ✔
3013
  SMetaStbStats stats = {0};
405,815 ✔
3014

3015
  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
405,831 ✔
3016
    stats.ctbNum += deltaCtb;
379,125 ✔
3017
    stats.colNum += deltaCol;
379,125 ✔
3018
    if (deltaKeep > 0) {
379,125 ✔
3019
      stats.keep = deltaKeep;
9 ✔
3020
    }
3021

3022
    int32_t code = metaStatsCacheUpsert(pMeta, &stats);
379,125 ✔
3023
    if (code) {
379,072 ✔
3024
      metaError("vgId:%d, failed to update stats, uid:%" PRId64 ", ctbNum:%" PRId64 ", colNum:%d, keep:%" PRId64,
×
3025
                TD_VID(pMeta->pVnode), uid, deltaCtb, deltaCol, deltaKeep > 0 ? deltaKeep : stats.keep);
3026
    }
3027
  }
3028
}
405,827 ✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc