TDengine/source/dnode/mnode/impl/src/mndStb.c

2604 lines
80 KiB
C
Raw Normal View History

2021-09-22 08:15:20 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-05-06 14:58:02 +00:00
#define _DEFAULT_SOURCE
2021-12-10 07:21:34 +00:00
#include "mndStb.h"
2021-12-10 07:20:04 +00:00
#include "mndDb.h"
2021-12-09 10:53:09 +00:00
#include "mndDnode.h"
2022-03-23 05:50:53 +00:00
#include "mndInfoSchema.h"
2021-12-09 10:53:09 +00:00
#include "mndMnode.h"
2022-04-18 13:23:22 +00:00
#include "mndPerfSchema.h"
2022-06-25 10:03:12 +00:00
#include "mndPrivilege.h"
2022-04-21 09:50:14 +00:00
#include "mndScheduler.h"
2021-12-09 10:53:09 +00:00
#include "mndShow.h"
2022-06-14 07:46:16 +00:00
#include "mndSma.h"
#include "mndTopic.h"
2021-12-09 10:53:09 +00:00
#include "mndTrans.h"
#include "mndUser.h"
#include "mndVgroup.h"
2021-12-10 06:12:11 +00:00
#include "tname.h"
2021-10-17 03:42:05 +00:00
2022-05-07 04:07:45 +00:00
#define STB_VER_NUMBER 1
#define STB_RESERVE_SIZE 64
2021-12-10 07:20:04 +00:00
static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw);
static int32_t mndStbActionInsert(SSdb *pSdb, SStbObj *pStb);
static int32_t mndStbActionDelete(SSdb *pSdb, SStbObj *pStb);
2022-01-17 07:38:50 +00:00
static int32_t mndStbActionUpdate(SSdb *pSdb, SStbObj *pOld, SStbObj *pNew);
2022-06-22 03:18:10 +00:00
static int32_t mndProcessTtlTimer(SRpcMsg *pReq);
2022-06-14 07:46:16 +00:00
static int32_t mndProcessCreateStbReq(SRpcMsg *pReq);
static int32_t mndProcessAlterStbReq(SRpcMsg *pReq);
static int32_t mndProcessDropStbReq(SRpcMsg *pReq);
2022-05-16 06:55:31 +00:00
static int32_t mndProcessTableMetaReq(SRpcMsg *pReq);
static int32_t mndRetrieveStb(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
2021-12-10 07:20:04 +00:00
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter);
2022-06-20 12:58:36 +00:00
static int32_t mndProcessTableCfgReq(SRpcMsg *pReq);
static int32_t mndAlterStbImp(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb, bool needRsp,
void *alterOriData, int32_t alterOriDataLen);
static int32_t mndCheckColAndTagModifiable(SMnode *pMnode, const char *stbname, int64_t suid, col_id_t colId);
2021-12-10 07:20:04 +00:00
int32_t mndInitStb(SMnode *pMnode) {
2022-05-07 04:07:45 +00:00
SSdbTable table = {
.sdbType = SDB_STB,
.keyType = SDB_KEY_BINARY,
.encodeFp = (SdbEncodeFp)mndStbActionEncode,
.decodeFp = (SdbDecodeFp)mndStbActionDecode,
.insertFp = (SdbInsertFp)mndStbActionInsert,
.updateFp = (SdbUpdateFp)mndStbActionUpdate,
.deleteFp = (SdbDeleteFp)mndStbActionDelete,
};
2021-12-10 07:20:04 +00:00
2022-06-14 07:46:16 +00:00
mndSetMsgHandle(pMnode, TDMT_MND_CREATE_STB, mndProcessCreateStbReq);
mndSetMsgHandle(pMnode, TDMT_MND_ALTER_STB, mndProcessAlterStbReq);
mndSetMsgHandle(pMnode, TDMT_MND_DROP_STB, mndProcessDropStbReq);
mndSetMsgHandle(pMnode, TDMT_VND_CREATE_STB_RSP, mndTransProcessRsp);
mndSetMsgHandle(pMnode, TDMT_VND_ALTER_STB_RSP, mndTransProcessRsp);
mndSetMsgHandle(pMnode, TDMT_VND_DROP_STB_RSP, mndTransProcessRsp);
2022-02-24 11:45:05 +00:00
mndSetMsgHandle(pMnode, TDMT_MND_TABLE_META, mndProcessTableMetaReq);
2022-06-22 03:18:10 +00:00
mndSetMsgHandle(pMnode, TDMT_MND_TTL_TIMER, mndProcessTtlTimer);
2022-06-20 05:32:21 +00:00
mndSetMsgHandle(pMnode, TDMT_MND_TABLE_CFG, mndProcessTableCfgReq);
2021-12-10 07:20:04 +00:00
mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STB, mndRetrieveStb);
mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STB, mndCancelGetNextStb);
2021-12-09 11:32:45 +00:00
return sdbSetTable(pMnode->pSdb, table);
2021-12-09 10:53:09 +00:00
}
2021-12-10 07:20:04 +00:00
void mndCleanupStb(SMnode *pMnode) {}
2021-12-09 10:53:09 +00:00
SSdbRaw *mndStbActionEncode(SStbObj *pStb) {
2021-12-31 06:22:50 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
2022-07-01 02:52:59 +00:00
int32_t size = sizeof(SStbObj) + (pStb->numOfColumns + pStb->numOfTags) * sizeof(SSchema) + pStb->commentLen +
2022-06-21 10:41:12 +00:00
pStb->ast1Len + pStb->ast2Len + STB_RESERVE_SIZE + taosArrayGetSize(pStb->pFuncs) * TSDB_FUNC_NAME_LEN;
2022-05-07 04:07:45 +00:00
SSdbRaw *pRaw = sdbAllocRaw(SDB_STB, STB_VER_NUMBER, size);
if (pRaw == NULL) goto _OVER;
2021-12-09 10:53:09 +00:00
int32_t dataPos = 0;
SDB_SET_BINARY(pRaw, dataPos, pStb->name, TSDB_TABLE_FNAME_LEN, _OVER)
SDB_SET_BINARY(pRaw, dataPos, pStb->db, TSDB_DB_FNAME_LEN, _OVER)
SDB_SET_INT64(pRaw, dataPos, pStb->createdTime, _OVER)
SDB_SET_INT64(pRaw, dataPos, pStb->updateTime, _OVER)
SDB_SET_INT64(pRaw, dataPos, pStb->uid, _OVER)
SDB_SET_INT64(pRaw, dataPos, pStb->dbUid, _OVER)
SDB_SET_INT32(pRaw, dataPos, pStb->tagVer, _OVER)
SDB_SET_INT32(pRaw, dataPos, pStb->colVer, _OVER)
2022-07-01 02:52:59 +00:00
SDB_SET_INT32(pRaw, dataPos, pStb->smaVer, _OVER)
SDB_SET_INT32(pRaw, dataPos, pStb->nextColId, _OVER)
SDB_SET_INT64(pRaw, dataPos, pStb->maxdelay[0], _OVER)
SDB_SET_INT64(pRaw, dataPos, pStb->maxdelay[1], _OVER)
SDB_SET_INT64(pRaw, dataPos, pStb->watermark[0], _OVER)
SDB_SET_INT64(pRaw, dataPos, pStb->watermark[1], _OVER)
SDB_SET_INT32(pRaw, dataPos, pStb->ttl, _OVER)
SDB_SET_INT32(pRaw, dataPos, pStb->numOfColumns, _OVER)
SDB_SET_INT32(pRaw, dataPos, pStb->numOfTags, _OVER)
2022-07-01 02:52:59 +00:00
SDB_SET_INT32(pRaw, dataPos, pStb->numOfFuncs, _OVER)
SDB_SET_INT32(pRaw, dataPos, pStb->commentLen, _OVER)
SDB_SET_INT32(pRaw, dataPos, pStb->ast1Len, _OVER)
SDB_SET_INT32(pRaw, dataPos, pStb->ast2Len, _OVER)
2021-12-10 06:15:19 +00:00
2022-02-08 05:45:24 +00:00
for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
SSchema *pSchema = &pStb->pColumns[i];
SDB_SET_INT8(pRaw, dataPos, pSchema->type, _OVER)
2022-05-07 04:07:45 +00:00
SDB_SET_INT8(pRaw, dataPos, pSchema->flags, _OVER)
SDB_SET_INT16(pRaw, dataPos, pSchema->colId, _OVER)
SDB_SET_INT32(pRaw, dataPos, pSchema->bytes, _OVER)
SDB_SET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
2022-02-08 05:45:24 +00:00
}
for (int32_t i = 0; i < pStb->numOfTags; ++i) {
SSchema *pSchema = &pStb->pTags[i];
SDB_SET_INT8(pRaw, dataPos, pSchema->type, _OVER)
2022-05-07 04:07:45 +00:00
SDB_SET_INT8(pRaw, dataPos, pSchema->flags, _OVER)
SDB_SET_INT16(pRaw, dataPos, pSchema->colId, _OVER)
SDB_SET_INT32(pRaw, dataPos, pSchema->bytes, _OVER)
SDB_SET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
2021-12-09 10:53:09 +00:00
}
2022-07-01 02:52:59 +00:00
for (int32_t i = 0; i < pStb->numOfFuncs; ++i) {
char *func = taosArrayGet(pStb->pFuncs, i);
SDB_SET_BINARY(pRaw, dataPos, func, TSDB_FUNC_NAME_LEN, _OVER)
}
2022-03-24 05:57:33 +00:00
if (pStb->commentLen > 0) {
2022-06-16 12:45:00 +00:00
SDB_SET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen + 1, _OVER)
2022-03-24 05:57:33 +00:00
}
2022-07-01 02:52:59 +00:00
if (pStb->ast1Len > 0) {
SDB_SET_BINARY(pRaw, dataPos, pStb->pAst1, pStb->ast1Len, _OVER)
}
2022-07-01 02:52:59 +00:00
if (pStb->ast2Len > 0) {
SDB_SET_BINARY(pRaw, dataPos, pStb->pAst2, pStb->ast2Len, _OVER)
}
2022-07-01 02:52:59 +00:00
2022-05-07 04:07:45 +00:00
SDB_SET_RESERVE(pRaw, dataPos, STB_RESERVE_SIZE, _OVER)
SDB_SET_DATALEN(pRaw, dataPos, _OVER)
2021-12-31 06:22:50 +00:00
terrno = 0;
_OVER:
2021-12-31 06:22:50 +00:00
if (terrno != 0) {
mError("stb:%s, failed to encode to raw:%p since %s", pStb->name, pRaw, terrstr());
sdbFreeRaw(pRaw);
return NULL;
}
2021-12-09 10:53:09 +00:00
2021-12-31 06:22:50 +00:00
mTrace("stb:%s, encode to raw:%p, row:%p", pStb->name, pRaw, pStb);
2021-12-09 10:53:09 +00:00
return pRaw;
}
2021-12-10 07:20:04 +00:00
static SSdbRow *mndStbActionDecode(SSdbRaw *pRaw) {
2021-12-31 06:22:50 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
2022-12-01 08:04:39 +00:00
SSdbRow *pRow = NULL;
SStbObj *pStb = NULL;
2021-12-31 06:22:50 +00:00
2021-12-09 10:53:09 +00:00
int8_t sver = 0;
if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
2021-12-09 10:53:09 +00:00
2022-05-07 04:07:45 +00:00
if (sver != STB_VER_NUMBER) {
2021-12-09 10:53:09 +00:00
terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
goto _OVER;
2021-12-09 10:53:09 +00:00
}
2022-12-01 08:04:39 +00:00
pRow = sdbAllocRow(sizeof(SStbObj));
if (pRow == NULL) goto _OVER;
2021-12-31 06:22:50 +00:00
2022-12-01 08:04:39 +00:00
pStb = sdbGetRowObj(pRow);
if (pStb == NULL) goto _OVER;
2021-12-09 10:53:09 +00:00
int32_t dataPos = 0;
SDB_GET_BINARY(pRaw, dataPos, pStb->name, TSDB_TABLE_FNAME_LEN, _OVER)
SDB_GET_BINARY(pRaw, dataPos, pStb->db, TSDB_DB_FNAME_LEN, _OVER)
SDB_GET_INT64(pRaw, dataPos, &pStb->createdTime, _OVER)
SDB_GET_INT64(pRaw, dataPos, &pStb->updateTime, _OVER)
SDB_GET_INT64(pRaw, dataPos, &pStb->uid, _OVER)
SDB_GET_INT64(pRaw, dataPos, &pStb->dbUid, _OVER)
SDB_GET_INT32(pRaw, dataPos, &pStb->tagVer, _OVER)
SDB_GET_INT32(pRaw, dataPos, &pStb->colVer, _OVER)
2022-07-01 02:52:59 +00:00
SDB_GET_INT32(pRaw, dataPos, &pStb->smaVer, _OVER)
SDB_GET_INT32(pRaw, dataPos, &pStb->nextColId, _OVER)
SDB_GET_INT64(pRaw, dataPos, &pStb->maxdelay[0], _OVER)
SDB_GET_INT64(pRaw, dataPos, &pStb->maxdelay[1], _OVER)
SDB_GET_INT64(pRaw, dataPos, &pStb->watermark[0], _OVER)
SDB_GET_INT64(pRaw, dataPos, &pStb->watermark[1], _OVER)
SDB_GET_INT32(pRaw, dataPos, &pStb->ttl, _OVER)
SDB_GET_INT32(pRaw, dataPos, &pStb->numOfColumns, _OVER)
SDB_GET_INT32(pRaw, dataPos, &pStb->numOfTags, _OVER)
2022-07-01 02:52:59 +00:00
SDB_GET_INT32(pRaw, dataPos, &pStb->numOfFuncs, _OVER)
SDB_GET_INT32(pRaw, dataPos, &pStb->commentLen, _OVER)
SDB_GET_INT32(pRaw, dataPos, &pStb->ast1Len, _OVER)
SDB_GET_INT32(pRaw, dataPos, &pStb->ast2Len, _OVER)
2021-12-10 06:15:19 +00:00
2022-03-25 16:29:53 +00:00
pStb->pColumns = taosMemoryCalloc(pStb->numOfColumns, sizeof(SSchema));
pStb->pTags = taosMemoryCalloc(pStb->numOfTags, sizeof(SSchema));
2022-07-01 05:47:53 +00:00
pStb->pFuncs = taosArrayInit(pStb->numOfFuncs, TSDB_FUNC_NAME_LEN);
2022-07-01 02:52:59 +00:00
if (pStb->pColumns == NULL || pStb->pTags == NULL || pStb->pFuncs == NULL) {
goto _OVER;
2022-01-24 05:59:09 +00:00
}
2021-12-10 06:15:19 +00:00
2022-02-08 05:45:24 +00:00
for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
SSchema *pSchema = &pStb->pColumns[i];
SDB_GET_INT8(pRaw, dataPos, &pSchema->type, _OVER)
2022-05-07 04:07:45 +00:00
SDB_GET_INT8(pRaw, dataPos, &pSchema->flags, _OVER)
SDB_GET_INT16(pRaw, dataPos, &pSchema->colId, _OVER)
SDB_GET_INT32(pRaw, dataPos, &pSchema->bytes, _OVER)
SDB_GET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
2022-02-08 05:45:24 +00:00
}
for (int32_t i = 0; i < pStb->numOfTags; ++i) {
SSchema *pSchema = &pStb->pTags[i];
SDB_GET_INT8(pRaw, dataPos, &pSchema->type, _OVER)
2022-05-07 04:07:45 +00:00
SDB_GET_INT8(pRaw, dataPos, &pSchema->flags, _OVER)
SDB_GET_INT16(pRaw, dataPos, &pSchema->colId, _OVER)
SDB_GET_INT32(pRaw, dataPos, &pSchema->bytes, _OVER)
SDB_GET_BINARY(pRaw, dataPos, pSchema->name, TSDB_COL_NAME_LEN, _OVER)
2021-12-09 10:53:09 +00:00
}
2022-07-01 02:52:59 +00:00
for (int32_t i = 0; i < pStb->numOfFuncs; ++i) {
char funcName[TSDB_FUNC_NAME_LEN] = {0};
SDB_GET_BINARY(pRaw, dataPos, funcName, TSDB_FUNC_NAME_LEN, _OVER)
taosArrayPush(pStb->pFuncs, funcName);
}
2022-03-23 09:53:00 +00:00
if (pStb->commentLen > 0) {
2022-06-16 12:45:00 +00:00
pStb->comment = taosMemoryCalloc(pStb->commentLen + 1, 1);
if (pStb->comment == NULL) goto _OVER;
2022-06-16 12:45:00 +00:00
SDB_GET_BINARY(pRaw, dataPos, pStb->comment, pStb->commentLen + 1, _OVER)
2022-03-23 09:53:00 +00:00
}
2022-07-01 02:52:59 +00:00
if (pStb->ast1Len > 0) {
pStb->pAst1 = taosMemoryCalloc(pStb->ast1Len, 1);
if (pStb->pAst1 == NULL) goto _OVER;
SDB_GET_BINARY(pRaw, dataPos, pStb->pAst1, pStb->ast1Len, _OVER)
}
2022-07-01 02:52:59 +00:00
if (pStb->ast2Len > 0) {
pStb->pAst2 = taosMemoryCalloc(pStb->ast2Len, 1);
if (pStb->pAst2 == NULL) goto _OVER;
SDB_GET_BINARY(pRaw, dataPos, pStb->pAst2, pStb->ast2Len, _OVER)
}
2022-05-07 04:07:45 +00:00
SDB_GET_RESERVE(pRaw, dataPos, STB_RESERVE_SIZE, _OVER)
2021-12-31 06:22:50 +00:00
terrno = 0;
_OVER:
2021-12-31 06:22:50 +00:00
if (terrno != 0) {
2022-12-01 08:04:39 +00:00
mError("stb:%s, failed to decode from raw:%p since %s", pStb == NULL ? "null" : pStb->name, pRaw, terrstr());
if (pStb != NULL) {
taosMemoryFreeClear(pStb->pColumns);
taosMemoryFreeClear(pStb->pTags);
taosMemoryFreeClear(pStb->comment);
}
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear(pRow);
2021-12-31 06:22:50 +00:00
return NULL;
}
2021-12-09 10:53:09 +00:00
2021-12-31 06:22:50 +00:00
mTrace("stb:%s, decode from raw:%p, row:%p", pStb->name, pRaw, pStb);
2021-12-09 10:53:09 +00:00
return pRow;
}
2022-08-18 06:26:11 +00:00
void mndFreeStb(SStbObj *pStb) {
taosArrayDestroy(pStb->pFuncs);
taosMemoryFreeClear(pStb->pColumns);
taosMemoryFreeClear(pStb->pTags);
taosMemoryFreeClear(pStb->comment);
taosMemoryFreeClear(pStb->pAst1);
taosMemoryFreeClear(pStb->pAst2);
}
2021-12-10 07:20:04 +00:00
static int32_t mndStbActionInsert(SSdb *pSdb, SStbObj *pStb) {
2021-12-31 06:22:50 +00:00
mTrace("stb:%s, perform insert action, row:%p", pStb->name, pStb);
2021-12-09 10:53:09 +00:00
return 0;
}
2021-12-10 07:20:04 +00:00
static int32_t mndStbActionDelete(SSdb *pSdb, SStbObj *pStb) {
2021-12-31 06:22:50 +00:00
mTrace("stb:%s, perform delete action, row:%p", pStb->name, pStb);
2022-08-18 06:26:11 +00:00
mndFreeStb(pStb);
2021-12-09 10:53:09 +00:00
return 0;
}
2022-01-17 07:38:50 +00:00
static int32_t mndStbActionUpdate(SSdb *pSdb, SStbObj *pOld, SStbObj *pNew) {
mTrace("stb:%s, perform update action, old row:%p new row:%p", pOld->name, pOld, pNew);
2021-12-09 10:53:09 +00:00
2022-01-17 07:38:50 +00:00
taosWLockLatch(&pOld->lock);
2022-02-08 05:45:24 +00:00
if (pOld->numOfColumns < pNew->numOfColumns) {
2022-03-25 16:29:53 +00:00
void *pColumns = taosMemoryMalloc(pNew->numOfColumns * sizeof(SSchema));
2022-02-08 11:52:48 +00:00
if (pColumns != NULL) {
2022-03-25 16:29:53 +00:00
taosMemoryFree(pOld->pColumns);
2022-02-08 11:52:48 +00:00
pOld->pColumns = pColumns;
2022-02-08 05:45:24 +00:00
} else {
terrno = TSDB_CODE_OUT_OF_MEMORY;
mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
taosWUnLockLatch(&pOld->lock);
}
}
if (pOld->numOfTags < pNew->numOfTags) {
2022-03-25 16:29:53 +00:00
void *pTags = taosMemoryMalloc(pNew->numOfTags * sizeof(SSchema));
2022-02-08 11:52:48 +00:00
if (pTags != NULL) {
2022-03-25 16:29:53 +00:00
taosMemoryFree(pOld->pTags);
2022-02-08 11:52:48 +00:00
pOld->pTags = pTags;
2022-01-28 01:29:17 +00:00
} else {
terrno = TSDB_CODE_OUT_OF_MEMORY;
mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
taosWUnLockLatch(&pOld->lock);
2021-12-16 12:16:47 +00:00
}
2021-12-10 03:42:03 +00:00
}
if (pOld->commentLen < pNew->commentLen && pNew->commentLen > 0) {
2022-06-16 12:45:00 +00:00
void *comment = taosMemoryMalloc(pNew->commentLen + 1);
2022-03-24 08:04:27 +00:00
if (comment != NULL) {
2022-03-25 16:29:53 +00:00
taosMemoryFree(pOld->comment);
2022-03-24 08:04:27 +00:00
pOld->comment = comment;
} else {
terrno = TSDB_CODE_OUT_OF_MEMORY;
mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
taosWUnLockLatch(&pOld->lock);
}
}
2022-07-01 05:47:53 +00:00
pOld->commentLen = pNew->commentLen;
2022-03-24 08:04:27 +00:00
if (pOld->ast1Len < pNew->ast1Len) {
2022-07-01 02:52:59 +00:00
void *pAst1 = taosMemoryMalloc(pNew->ast1Len + 1);
if (pAst1 != NULL) {
taosMemoryFree(pOld->pAst1);
pOld->pAst1 = pAst1;
} else {
terrno = TSDB_CODE_OUT_OF_MEMORY;
mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
taosWUnLockLatch(&pOld->lock);
}
}
if (pOld->ast2Len < pNew->ast2Len) {
2022-07-01 02:52:59 +00:00
void *pAst2 = taosMemoryMalloc(pNew->ast2Len + 1);
if (pAst2 != NULL) {
taosMemoryFree(pOld->pAst2);
pOld->pAst2 = pAst2;
} else {
terrno = TSDB_CODE_OUT_OF_MEMORY;
mTrace("stb:%s, failed to perform update action since %s", pOld->name, terrstr());
taosWUnLockLatch(&pOld->lock);
}
}
2022-01-28 01:29:17 +00:00
pOld->updateTime = pNew->updateTime;
pOld->tagVer = pNew->tagVer;
pOld->colVer = pNew->colVer;
2022-06-14 07:46:16 +00:00
pOld->smaVer = pNew->smaVer;
pOld->nextColId = pNew->nextColId;
2022-05-11 03:37:13 +00:00
pOld->ttl = pNew->ttl;
if (pNew->numOfColumns > 0) {
pOld->numOfColumns = pNew->numOfColumns;
memcpy(pOld->pColumns, pNew->pColumns, pOld->numOfColumns * sizeof(SSchema));
}
if (pNew->numOfTags > 0) {
pOld->numOfTags = pNew->numOfTags;
memcpy(pOld->pTags, pNew->pTags, pOld->numOfTags * sizeof(SSchema));
}
if (pNew->commentLen > 0) {
2022-06-16 12:45:00 +00:00
memcpy(pOld->comment, pNew->comment, pNew->commentLen + 1);
2022-07-01 02:52:59 +00:00
pOld->commentLen = pNew->commentLen;
2022-03-24 08:04:27 +00:00
}
if (pNew->ast1Len != 0) {
memcpy(pOld->pAst1, pNew->pAst1, pNew->ast1Len);
2022-07-01 02:52:59 +00:00
pOld->ast1Len = pNew->ast1Len;
}
if (pNew->ast2Len != 0) {
memcpy(pOld->pAst2, pNew->pAst2, pNew->ast2Len);
2022-07-01 02:52:59 +00:00
pOld->ast2Len = pNew->ast2Len;
}
2022-01-17 07:38:50 +00:00
taosWUnLockLatch(&pOld->lock);
2021-12-09 10:53:09 +00:00
return 0;
}
2021-12-10 07:20:04 +00:00
SStbObj *mndAcquireStb(SMnode *pMnode, char *stbName) {
2021-12-31 06:22:50 +00:00
SSdb *pSdb = pMnode->pSdb;
2021-12-20 12:30:55 +00:00
SStbObj *pStb = sdbAcquire(pSdb, SDB_STB, stbName);
2022-01-11 02:48:19 +00:00
if (pStb == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
2021-12-20 12:30:55 +00:00
terrno = TSDB_CODE_MND_STB_NOT_EXIST;
}
return pStb;
2021-12-10 06:12:11 +00:00
}
2021-12-10 07:20:04 +00:00
void mndReleaseStb(SMnode *pMnode, SStbObj *pStb) {
2021-12-10 06:12:11 +00:00
SSdb *pSdb = pMnode->pSdb;
sdbRelease(pSdb, pStb);
}
2022-05-06 17:47:45 +00:00
SDbObj *mndAcquireDbByStb(SMnode *pMnode, const char *stbName) {
2021-12-10 07:20:04 +00:00
SName name = {0};
tNameFromString(&name, stbName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
2021-12-09 10:53:09 +00:00
2021-12-10 07:20:04 +00:00
char db[TSDB_TABLE_FNAME_LEN] = {0};
tNameGetFullDbName(&name, db);
2021-12-10 03:42:03 +00:00
2021-12-10 07:20:04 +00:00
return mndAcquireDb(pMnode, db);
}
2021-12-09 10:53:09 +00:00
2022-06-14 07:46:16 +00:00
static FORCE_INLINE int32_t schemaExColIdCompare(const void *colId, const void *pSchema) {
2022-04-18 13:23:22 +00:00
if (*(col_id_t *)colId < ((SSchema *)pSchema)->colId) {
2022-04-01 09:43:50 +00:00
return -1;
2022-04-18 13:23:22 +00:00
} else if (*(col_id_t *)colId > ((SSchema *)pSchema)->colId) {
2022-04-01 09:43:50 +00:00
return 1;
}
return 0;
}
static void *mndBuildVCreateStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen,
void *alterOriData, int32_t alterOriDataLen) {
2022-05-07 10:03:06 +00:00
SEncoder encoder = {0};
2022-04-20 10:25:08 +00:00
int32_t contLen;
SName name = {0};
SVCreateStbReq req = {0};
2022-04-20 10:03:50 +00:00
2022-01-17 07:38:50 +00:00
tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
2022-03-24 03:48:00 +00:00
char dbFName[TSDB_DB_FNAME_LEN] = {0};
tNameGetFullDbName(&name, dbFName);
2022-01-17 07:38:50 +00:00
req.name = (char *)tNameGetTableName(&name);
2022-04-20 10:03:50 +00:00
req.suid = pStb->uid;
req.rollup = pStb->ast1Len > 0 ? 1 : 0;
2022-07-10 10:54:58 +00:00
req.alterOriData = alterOriData;
req.alterOriDataLen = alterOriDataLen;
2022-05-26 08:29:52 +00:00
// todo
req.schemaRow.nCols = pStb->numOfColumns;
req.schemaRow.version = pStb->colVer;
2022-05-26 08:29:52 +00:00
req.schemaRow.pSchema = pStb->pColumns;
2022-04-22 12:34:37 +00:00
req.schemaTag.nCols = pStb->numOfTags;
2022-05-26 08:29:52 +00:00
req.schemaTag.version = pStb->tagVer;
2022-04-22 12:34:37 +00:00
req.schemaTag.pSchema = pStb->pTags;
2021-12-25 07:04:48 +00:00
2022-04-02 02:37:17 +00:00
if (req.rollup) {
2022-06-27 06:47:14 +00:00
req.rsmaParam.maxdelay[0] = pStb->maxdelay[0];
req.rsmaParam.maxdelay[1] = pStb->maxdelay[1];
2022-08-19 09:19:50 +00:00
req.rsmaParam.watermark[0] = pStb->watermark[0];
req.rsmaParam.watermark[1] = pStb->watermark[1];
2022-04-21 07:58:52 +00:00
if (pStb->ast1Len > 0) {
2022-06-27 06:47:14 +00:00
if (mndConvertRsmaTask(&req.rsmaParam.qmsg[0], &req.rsmaParam.qmsgLen[0], pStb->pAst1, pStb->uid,
STREAM_TRIGGER_WINDOW_CLOSE, req.rsmaParam.watermark[0]) < 0) {
goto _err;
2022-04-21 09:50:14 +00:00
}
2022-04-21 07:58:52 +00:00
}
if (pStb->ast2Len > 0) {
2022-06-27 06:47:14 +00:00
if (mndConvertRsmaTask(&req.rsmaParam.qmsg[1], &req.rsmaParam.qmsgLen[1], pStb->pAst2, pStb->uid,
STREAM_TRIGGER_WINDOW_CLOSE, req.rsmaParam.watermark[1]) < 0) {
goto _err;
2022-04-21 09:50:14 +00:00
}
2022-04-21 07:58:52 +00:00
}
2022-04-18 13:23:22 +00:00
}
2022-04-20 10:03:50 +00:00
// get length
2022-04-27 09:39:54 +00:00
int32_t ret = 0;
tEncodeSize(tEncodeSVCreateStbReq, &req, contLen, ret);
if (ret < 0) {
goto _err;
2022-04-20 10:03:50 +00:00
}
2022-04-20 10:25:08 +00:00
contLen += sizeof(SMsgHead);
2022-04-02 02:37:17 +00:00
2022-11-04 02:47:49 +00:00
SMsgHead *pHead = taosMemoryCalloc(1, contLen);
2022-01-28 02:54:50 +00:00
if (pHead == NULL) {
2021-12-25 07:04:48 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
goto _err;
2021-12-25 07:04:48 +00:00
}
2022-01-28 02:54:50 +00:00
pHead->contLen = htonl(contLen);
pHead->vgId = htonl(pVgroup->vgId);
2021-12-25 07:50:12 +00:00
2022-01-28 02:54:50 +00:00
void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
2022-05-07 10:03:06 +00:00
tEncoderInit(&encoder, pBuf, contLen - sizeof(SMsgHead));
if (tEncodeSVCreateStbReq(&encoder, &req) < 0) {
taosMemoryFreeClear(pHead);
tEncoderClear(&encoder);
goto _err;
2022-04-20 10:03:50 +00:00
}
2022-05-07 10:03:06 +00:00
tEncoderClear(&encoder);
2021-12-25 07:04:48 +00:00
2022-01-28 02:54:50 +00:00
*pContLen = contLen;
2022-06-27 06:47:14 +00:00
taosMemoryFreeClear(req.rsmaParam.qmsg[0]);
taosMemoryFreeClear(req.rsmaParam.qmsg[1]);
2022-01-28 02:54:50 +00:00
return pHead;
_err:
2022-06-27 06:47:14 +00:00
taosMemoryFreeClear(req.rsmaParam.qmsg[0]);
taosMemoryFreeClear(req.rsmaParam.qmsg[1]);
return NULL;
}
2022-02-09 09:50:52 +00:00
static void *mndBuildVDropStbReq(SMnode *pMnode, SVgObj *pVgroup, SStbObj *pStb, int32_t *pContLen) {
2022-04-28 04:09:31 +00:00
SName name = {0};
SVDropStbReq req = {0};
int32_t contLen = 0;
int32_t ret = 0;
SMsgHead *pHead = NULL;
2022-05-07 10:03:06 +00:00
SEncoder encoder = {0};
2022-04-28 04:09:31 +00:00
2022-01-28 02:54:50 +00:00
tNameFromString(&name, pStb->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
req.name = (char *)tNameGetTableName(&name);
req.suid = pStb->uid;
2022-04-28 04:09:31 +00:00
tEncodeSize(tEncodeSVDropStbReq, &req, contLen, ret);
if (ret < 0) return NULL;
contLen += sizeof(SMsgHead);
pHead = taosMemoryMalloc(contLen);
2022-01-28 02:54:50 +00:00
if (pHead == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return NULL;
}
2022-01-28 02:54:50 +00:00
pHead->contLen = htonl(contLen);
pHead->vgId = htonl(pVgroup->vgId);
void *pBuf = POINTER_SHIFT(pHead, sizeof(SMsgHead));
2022-04-28 04:09:31 +00:00
2022-05-07 10:03:06 +00:00
tEncoderInit(&encoder, pBuf, contLen - sizeof(SMsgHead));
tEncodeSVDropStbReq(&encoder, &req);
tEncoderClear(&encoder);
2022-01-28 02:54:50 +00:00
*pContLen = contLen;
return pHead;
}
2022-05-06 17:47:45 +00:00
int32_t mndCheckCreateStbReq(SMCreateStbReq *pCreate) {
2021-12-10 07:20:04 +00:00
if (pCreate->igExists < 0 || pCreate->igExists > 1) {
2021-12-16 12:16:47 +00:00
terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
2021-12-10 07:20:04 +00:00
return -1;
}
2021-12-09 10:53:09 +00:00
if (pCreate->numOfColumns < TSDB_MIN_COLUMNS || pCreate->numOfTags + pCreate->numOfColumns > TSDB_MAX_COLUMNS) {
2022-08-04 05:39:16 +00:00
terrno = TSDB_CODE_PAR_INVALID_COLUMNS_NUM;
2021-12-10 07:20:04 +00:00
return -1;
}
2021-12-10 03:42:03 +00:00
2021-12-10 07:20:04 +00:00
if (pCreate->numOfTags <= 0 || pCreate->numOfTags > TSDB_MAX_TAGS) {
2021-12-16 12:16:47 +00:00
terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
2021-12-10 07:20:04 +00:00
return -1;
}
2021-12-10 06:12:11 +00:00
2022-02-15 09:24:34 +00:00
SField *pField = taosArrayGet(pCreate->pColumns, 0);
2022-02-09 10:28:11 +00:00
if (pField->type != TSDB_DATA_TYPE_TIMESTAMP) {
2022-08-04 05:39:16 +00:00
terrno = TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
2022-02-09 09:50:52 +00:00
return -1;
}
2022-02-09 10:28:11 +00:00
for (int32_t i = 0; i < pCreate->numOfColumns; ++i) {
SField *pField1 = taosArrayGet(pCreate->pColumns, i);
2022-09-30 06:03:46 +00:00
if (pField1->type >= TSDB_DATA_TYPE_MAX) {
2022-02-09 10:28:11 +00:00
terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
return -1;
}
if (pField1->bytes <= 0) {
2022-02-09 10:28:11 +00:00
terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
return -1;
}
if (pField1->name[0] == 0) {
2022-02-09 10:28:11 +00:00
terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
return -1;
}
}
for (int32_t i = 0; i < pCreate->numOfTags; ++i) {
SField *pField1 = taosArrayGet(pCreate->pTags, i);
2022-09-30 06:03:46 +00:00
if (pField1->type >= TSDB_DATA_TYPE_MAX) {
2021-12-16 12:16:47 +00:00
terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
2021-12-10 07:20:04 +00:00
return -1;
}
if (pField1->bytes <= 0) {
2021-12-16 12:16:47 +00:00
terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
2021-12-10 07:20:04 +00:00
return -1;
}
if (pField1->name[0] == 0) {
2021-12-16 12:16:47 +00:00
terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
2021-12-10 07:20:04 +00:00
return -1;
}
}
2021-12-10 06:12:11 +00:00
2021-12-10 07:20:04 +00:00
return 0;
}
static int32_t mndSetCreateStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
2021-12-21 06:07:27 +00:00
SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
if (pRedoRaw == NULL) return -1;
2022-07-01 02:52:59 +00:00
if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
sdbFreeRaw(pRedoRaw);
return -1;
}
2021-12-21 06:07:27 +00:00
if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_CREATING) != 0) return -1;
return 0;
}
static int32_t mndSetCreateStbUndoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
2021-12-21 06:07:27 +00:00
SSdbRaw *pUndoRaw = mndStbActionEncode(pStb);
if (pUndoRaw == NULL) return -1;
2022-07-01 02:52:59 +00:00
if (mndTransAppendUndolog(pTrans, pUndoRaw) != 0) {
sdbFreeRaw(pUndoRaw);
return -1;
}
2021-12-21 06:07:27 +00:00
if (sdbSetRawStatus(pUndoRaw, SDB_STATUS_DROPPED) != 0) return -1;
return 0;
}
static int32_t mndSetCreateStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
2021-12-21 06:07:27 +00:00
SSdbRaw *pCommitRaw = mndStbActionEncode(pStb);
if (pCommitRaw == NULL) return -1;
2022-07-01 02:52:59 +00:00
if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
sdbFreeRaw(pCommitRaw);
return -1;
}
2021-12-21 06:07:27 +00:00
if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;
return 0;
}
static int32_t mndSetCreateStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
2021-12-31 06:22:50 +00:00
SSdb *pSdb = pMnode->pSdb;
SVgObj *pVgroup = NULL;
2021-12-31 06:22:50 +00:00
void *pIter = NULL;
2022-01-28 01:29:17 +00:00
int32_t contLen;
while (1) {
pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
if (pIter == NULL) break;
2022-07-01 09:02:33 +00:00
if (!mndVgroupInDb(pVgroup, pDb->uid)) {
2022-07-01 05:47:53 +00:00
sdbRelease(pSdb, pVgroup);
continue;
}
2022-07-10 10:54:58 +00:00
void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen, NULL, 0);
2022-01-17 07:50:22 +00:00
if (pReq == NULL) {
sdbCancelFetch(pSdb, pIter);
sdbRelease(pSdb, pVgroup);
return -1;
}
2021-12-21 06:07:27 +00:00
STransAction action = {0};
action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
2022-01-17 07:50:22 +00:00
action.pCont = pReq;
2021-12-25 08:05:18 +00:00
action.contLen = contLen;
2021-12-24 07:00:51 +00:00
action.msgType = TDMT_VND_CREATE_STB;
2022-05-07 04:07:45 +00:00
action.acceptableCode = TSDB_CODE_TDB_STB_ALREADY_EXIST;
action.retryCode = TSDB_CODE_TDB_STB_NOT_EXIST;
if (mndTransAppendRedoAction(pTrans, &action) != 0) {
2022-03-25 16:29:53 +00:00
taosMemoryFree(pReq);
sdbCancelFetch(pSdb, pIter);
sdbRelease(pSdb, pVgroup);
return -1;
}
sdbRelease(pSdb, pVgroup);
}
2021-12-21 06:07:27 +00:00
return 0;
}
static int32_t mndSetCreateStbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
2021-12-31 06:22:50 +00:00
SSdb *pSdb = pMnode->pSdb;
SVgObj *pVgroup = NULL;
2021-12-31 06:22:50 +00:00
void *pIter = NULL;
while (1) {
pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
if (pIter == NULL) break;
2022-07-01 09:02:33 +00:00
if (!mndVgroupInDb(pVgroup, pDb->uid)) {
2022-07-01 05:47:53 +00:00
sdbRelease(pSdb, pVgroup);
continue;
}
2022-01-28 02:54:50 +00:00
int32_t contLen = 0;
2022-02-09 09:50:52 +00:00
void *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
2022-01-17 07:50:22 +00:00
if (pReq == NULL) {
sdbCancelFetch(pSdb, pIter);
sdbRelease(pSdb, pVgroup);
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
2021-12-21 06:07:27 +00:00
STransAction action = {0};
action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
2022-01-17 07:50:22 +00:00
action.pCont = pReq;
2022-01-28 02:54:50 +00:00
action.contLen = contLen;
2021-12-24 07:00:51 +00:00
action.msgType = TDMT_VND_DROP_STB;
2022-05-07 04:07:45 +00:00
action.acceptableCode = TSDB_CODE_TDB_STB_NOT_EXIST;
if (mndTransAppendUndoAction(pTrans, &action) != 0) {
2022-03-25 16:29:53 +00:00
taosMemoryFree(pReq);
sdbCancelFetch(pSdb, pIter);
sdbRelease(pSdb, pVgroup);
return -1;
}
sdbRelease(pSdb, pVgroup);
}
2021-12-21 06:07:27 +00:00
return 0;
}
2022-03-24 05:57:33 +00:00
static SSchema *mndFindStbColumns(const SStbObj *pStb, const char *colName) {
2022-04-01 09:43:50 +00:00
for (int32_t col = 0; col < pStb->numOfColumns; ++col) {
2022-03-24 05:57:33 +00:00
SSchema *pSchema = &pStb->pColumns[col];
2022-04-01 09:43:50 +00:00
if (strncasecmp(pSchema->name, colName, TSDB_COL_NAME_LEN) == 0) {
2022-03-24 05:57:33 +00:00
return pSchema;
}
}
return NULL;
}
2022-05-06 17:47:45 +00:00
int32_t mndBuildStbFromReq(SMnode *pMnode, SStbObj *pDst, SMCreateStbReq *pCreate, SDbObj *pDb) {
memcpy(pDst->name, pCreate->name, TSDB_TABLE_FNAME_LEN);
memcpy(pDst->db, pDb->name, TSDB_DB_FNAME_LEN);
pDst->createdTime = taosGetTimestampMs();
pDst->updateTime = pDst->createdTime;
pDst->uid =
(pCreate->source == TD_REQ_FROM_TAOX) ? pCreate->suid : mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN);
2022-05-06 17:47:45 +00:00
pDst->dbUid = pDb->uid;
pDst->tagVer = 1;
pDst->colVer = 1;
2022-07-01 02:52:59 +00:00
pDst->smaVer = 1;
2022-05-06 17:47:45 +00:00
pDst->nextColId = 1;
pDst->maxdelay[0] = pCreate->delay1;
pDst->maxdelay[1] = pCreate->delay2;
pDst->watermark[0] = pCreate->watermark1;
pDst->watermark[1] = pCreate->watermark2;
2022-05-06 17:47:45 +00:00
pDst->ttl = pCreate->ttl;
pDst->numOfColumns = pCreate->numOfColumns;
pDst->numOfTags = pCreate->numOfTags;
2022-07-01 02:52:59 +00:00
pDst->numOfFuncs = pCreate->numOfFuncs;
2022-05-06 17:47:45 +00:00
pDst->commentLen = pCreate->commentLen;
2022-06-20 12:58:36 +00:00
pDst->pFuncs = pCreate->pFuncs;
pCreate->pFuncs = NULL;
2022-06-25 10:03:12 +00:00
2022-05-06 17:47:45 +00:00
if (pDst->commentLen > 0) {
2022-06-16 12:45:00 +00:00
pDst->comment = taosMemoryCalloc(pDst->commentLen + 1, 1);
2022-05-06 17:47:45 +00:00
if (pDst->comment == NULL) {
2022-03-24 05:57:33 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
2022-07-01 02:52:59 +00:00
memcpy(pDst->comment, pCreate->pComment, pDst->commentLen + 1);
2022-03-23 09:53:00 +00:00
}
2021-12-10 07:20:04 +00:00
2022-05-06 17:47:45 +00:00
pDst->ast1Len = pCreate->ast1Len;
if (pDst->ast1Len > 0) {
pDst->pAst1 = taosMemoryCalloc(pDst->ast1Len, 1);
if (pDst->pAst1 == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
2022-05-06 17:47:45 +00:00
memcpy(pDst->pAst1, pCreate->pAst1, pDst->ast1Len);
}
2022-05-06 17:47:45 +00:00
pDst->ast2Len = pCreate->ast2Len;
if (pDst->ast2Len > 0) {
pDst->pAst2 = taosMemoryCalloc(pDst->ast2Len, 1);
if (pDst->pAst2 == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
2022-05-06 17:47:45 +00:00
memcpy(pDst->pAst2, pCreate->pAst2, pDst->ast2Len);
}
2022-05-06 17:47:45 +00:00
pDst->pColumns = taosMemoryCalloc(1, pDst->numOfColumns * sizeof(SSchema));
pDst->pTags = taosMemoryCalloc(1, pDst->numOfTags * sizeof(SSchema));
if (pDst->pColumns == NULL || pDst->pTags == NULL) {
2022-02-08 05:45:24 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
2022-02-08 09:08:40 +00:00
2022-05-06 17:47:45 +00:00
for (int32_t i = 0; i < pDst->numOfColumns; ++i) {
2022-02-09 10:28:11 +00:00
SField *pField = taosArrayGet(pCreate->pColumns, i);
2022-05-06 17:47:45 +00:00
SSchema *pSchema = &pDst->pColumns[i];
2022-02-09 10:28:11 +00:00
pSchema->type = pField->type;
pSchema->bytes = pField->bytes;
pSchema->flags = pField->flags;
2022-02-09 10:28:11 +00:00
memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
2022-05-06 17:47:45 +00:00
pSchema->colId = pDst->nextColId;
pDst->nextColId++;
2022-02-08 05:45:24 +00:00
}
2022-05-06 17:47:45 +00:00
for (int32_t i = 0; i < pDst->numOfTags; ++i) {
2022-02-09 10:28:11 +00:00
SField *pField = taosArrayGet(pCreate->pTags, i);
2022-05-06 17:47:45 +00:00
SSchema *pSchema = &pDst->pTags[i];
2022-02-09 10:28:11 +00:00
pSchema->type = pField->type;
pSchema->bytes = pField->bytes;
memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
2022-05-06 17:47:45 +00:00
pSchema->colId = pDst->nextColId;
pDst->nextColId++;
2021-12-28 10:31:14 +00:00
}
2022-05-06 17:47:45 +00:00
return 0;
}
2022-05-16 06:55:31 +00:00
static int32_t mndCreateStb(SMnode *pMnode, SRpcMsg *pReq, SMCreateStbReq *pCreate, SDbObj *pDb) {
2022-05-06 17:47:45 +00:00
SStbObj stbObj = {0};
2022-01-24 05:59:09 +00:00
int32_t code = -1;
2022-05-06 17:47:45 +00:00
2022-09-22 08:18:51 +00:00
STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_DB_INSIDE, pReq, "create-stb");
if (pTrans == NULL) goto _OVER;
2021-12-10 07:20:04 +00:00
2022-09-23 07:42:36 +00:00
mInfo("trans:%d, used to create stb:%s", pTrans->id, pCreate->name);
2022-05-23 13:08:00 +00:00
if (mndBuildStbFromReq(pMnode, &stbObj, pCreate, pDb) != 0) goto _OVER;
2022-05-06 17:47:45 +00:00
if (mndAddStbToTrans(pMnode, pTrans, pDb, &stbObj) < 0) goto _OVER;
if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
2021-12-21 06:07:27 +00:00
code = 0;
_OVER:
2021-12-10 07:20:04 +00:00
mndTransDrop(pTrans);
2022-07-06 13:17:52 +00:00
mndStbActionDelete(pMnode->pSdb, &stbObj);
2021-12-21 06:07:27 +00:00
return code;
2021-12-10 06:12:11 +00:00
}
2022-05-07 04:07:45 +00:00
2022-05-06 17:47:45 +00:00
int32_t mndAddStbToTrans(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
2022-07-25 14:14:10 +00:00
mndTransSetDbName(pTrans, pDb->name, pStb->name);
2022-05-06 17:47:45 +00:00
if (mndSetCreateStbRedoLogs(pMnode, pTrans, pDb, pStb) != 0) return -1;
if (mndSetCreateStbUndoLogs(pMnode, pTrans, pDb, pStb) != 0) return -1;
if (mndSetCreateStbCommitLogs(pMnode, pTrans, pDb, pStb) != 0) return -1;
if (mndSetCreateStbRedoActions(pMnode, pTrans, pDb, pStb) != 0) return -1;
if (mndSetCreateStbUndoActions(pMnode, pTrans, pDb, pStb) != 0) return -1;
return 0;
}
2021-12-10 06:12:11 +00:00
2022-06-22 03:18:10 +00:00
static int32_t mndProcessTtlTimer(SRpcMsg *pReq) {
2022-07-11 07:16:10 +00:00
SMnode *pMnode = pReq->info.node;
SSdb *pSdb = pMnode->pSdb;
SVgObj *pVgroup = NULL;
void *pIter = NULL;
SVDropTtlTableReq ttlReq = {.timestamp = taosGetTimestampSec()};
int32_t reqLen = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
int32_t contLen = reqLen + sizeof(SMsgHead);
2022-06-21 10:01:40 +00:00
mInfo("start to process ttl timer");
2022-06-21 10:01:40 +00:00
while (1) {
pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
if (pIter == NULL) break;
2022-06-22 03:18:10 +00:00
SMsgHead *pHead = rpcMallocCont(contLen);
2022-06-21 10:01:40 +00:00
if (pHead == NULL) {
2022-06-22 03:18:10 +00:00
sdbCancelFetch(pSdb, pVgroup);
2022-06-21 10:01:40 +00:00
sdbRelease(pSdb, pVgroup);
continue;
}
pHead->contLen = htonl(contLen);
pHead->vgId = htonl(pVgroup->vgId);
2022-07-11 07:16:10 +00:00
tSerializeSVDropTtlTableReq((char *)pHead + sizeof(SMsgHead), contLen, &ttlReq);
2022-06-21 10:01:40 +00:00
SRpcMsg rpcMsg = {.msgType = TDMT_VND_DROP_TTL_TABLE, .pCont = pHead, .contLen = contLen};
2022-06-22 03:18:10 +00:00
SEpSet epSet = mndGetVgroupEpset(pMnode, pVgroup);
2022-06-21 10:01:40 +00:00
int32_t code = tmsgSendReq(&epSet, &rpcMsg);
2022-06-22 03:18:10 +00:00
if (code != 0) {
2022-07-11 07:16:10 +00:00
mError("vgId:%d, failed to send drop ttl table request to vnode since 0x%x", pVgroup->vgId, code);
2022-06-22 03:18:10 +00:00
} else {
2022-09-23 07:42:36 +00:00
mInfo("vgId:%d, send drop ttl table request to vnode, time:%d", pVgroup->vgId, ttlReq.timestamp);
2022-06-21 10:01:40 +00:00
}
sdbRelease(pSdb, pVgroup);
}
2022-06-22 03:18:10 +00:00
2022-06-21 10:01:40 +00:00
return 0;
}
2022-07-11 09:36:07 +00:00
static int32_t mndFindSuperTableTagIndex(const SStbObj *pStb, const char *tagName) {
for (int32_t tag = 0; tag < pStb->numOfTags; tag++) {
if (strcasecmp(pStb->pTags[tag].name, tagName) == 0) {
return tag;
}
}
return -1;
}
static int32_t mndFindSuperTableColumnIndex(const SStbObj *pStb, const char *colName) {
for (int32_t col = 0; col < pStb->numOfColumns; col++) {
if (strcasecmp(pStb->pColumns[col].name, colName) == 0) {
return col;
}
}
return -1;
}
static int32_t mndBuildStbFromAlter(SStbObj *pStb, SStbObj *pDst, SMCreateStbReq *createReq) {
taosRLockLatch(&pStb->lock);
memcpy(pDst, pStb, sizeof(SStbObj));
taosRUnLockLatch(&pStb->lock);
pDst->updateTime = taosGetTimestampMs();
pDst->numOfColumns = createReq->numOfColumns;
pDst->numOfTags = createReq->numOfTags;
pDst->pColumns = taosMemoryCalloc(1, pDst->numOfColumns * sizeof(SSchema));
pDst->pTags = taosMemoryCalloc(1, pDst->numOfTags * sizeof(SSchema));
if (pDst->pColumns == NULL || pDst->pTags == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
for (int32_t i = 0; i < pDst->numOfColumns; ++i) {
SField *pField = taosArrayGet(createReq->pColumns, i);
SSchema *pSchema = &pDst->pColumns[i];
pSchema->type = pField->type;
pSchema->bytes = pField->bytes;
pSchema->flags = pField->flags;
memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
int32_t cIndex = mndFindSuperTableColumnIndex(pStb, pField->name);
if (cIndex >= 0) {
2022-07-11 09:36:07 +00:00
pSchema->colId = pStb->pColumns[cIndex].colId;
} else {
2022-07-11 09:36:07 +00:00
pSchema->colId = pDst->nextColId++;
}
}
for (int32_t i = 0; i < pDst->numOfTags; ++i) {
SField *pField = taosArrayGet(createReq->pTags, i);
SSchema *pSchema = &pDst->pTags[i];
pSchema->type = pField->type;
pSchema->bytes = pField->bytes;
memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
int32_t cIndex = mndFindSuperTableTagIndex(pStb, pField->name);
if (cIndex >= 0) {
2022-07-11 09:36:07 +00:00
pSchema->colId = pStb->pTags[cIndex].colId;
} else {
2022-07-11 09:36:07 +00:00
pSchema->colId = pDst->nextColId++;
}
}
pDst->tagVer = createReq->tagVer;
pDst->colVer = createReq->colVer;
return TSDB_CODE_SUCCESS;
}
2022-06-14 07:46:16 +00:00
static int32_t mndProcessCreateStbReq(SRpcMsg *pReq) {
2022-05-16 06:55:31 +00:00
SMnode *pMnode = pReq->info.node;
2022-02-09 10:28:11 +00:00
int32_t code = -1;
SStbObj *pStb = NULL;
SDbObj *pDb = NULL;
SMCreateStbReq createReq = {0};
2022-07-10 05:45:26 +00:00
bool isAlter = false;
2021-12-10 06:12:11 +00:00
2022-05-16 06:55:31 +00:00
if (tDeserializeSMCreateStbReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
2022-02-15 10:17:18 +00:00
terrno = TSDB_CODE_INVALID_MSG;
goto _OVER;
2022-02-15 10:17:18 +00:00
}
2021-12-10 06:12:11 +00:00
2022-09-23 07:42:36 +00:00
mInfo("stb:%s, start to create", createReq.name);
2022-02-15 09:24:34 +00:00
if (mndCheckCreateStbReq(&createReq) != 0) {
terrno = TSDB_CODE_INVALID_MSG;
goto _OVER;
2022-02-15 09:24:34 +00:00
}
2021-12-10 07:20:04 +00:00
2022-02-09 10:28:11 +00:00
pStb = mndAcquireStb(pMnode, createReq.name);
2021-12-10 07:20:04 +00:00
if (pStb != NULL) {
2022-02-09 10:28:11 +00:00
if (createReq.igExists) {
2022-07-10 03:17:51 +00:00
if (createReq.source == TD_REQ_FROM_APP) {
2022-09-23 07:42:36 +00:00
mInfo("stb:%s, already exist, ignore exist is set", createReq.name);
2022-07-10 03:17:51 +00:00
code = 0;
goto _OVER;
} else if (pStb->uid != createReq.suid) {
mError("stb:%s, already exist while create, input suid:%" PRId64 " not match with exist suid:%" PRId64,
createReq.name, createReq.suid, pStb->uid);
terrno = TSDB_CODE_MND_STABLE_UID_NOT_MATCH;
goto _OVER;
} else if (createReq.tagVer > 0 || createReq.colVer > 0) {
2022-07-11 08:12:07 +00:00
int32_t tagDelta = createReq.tagVer - pStb->tagVer;
int32_t colDelta = createReq.colVer - pStb->colVer;
int32_t verDelta = tagDelta + colDelta;
2022-07-10 03:17:51 +00:00
mInfo("stb:%s, already exist while create, input tagVer:%d colVer:%d, exist tagVer:%d colVer:%d",
createReq.name, createReq.tagVer, createReq.colVer, pStb->tagVer, pStb->colVer);
if (tagDelta <= 0 && colDelta <= 0) {
mInfo("stb:%s, schema version is not incremented and nothing needs to be done", createReq.name);
code = 0;
goto _OVER;
} else if ((tagDelta == 1 || colDelta == 1) && (verDelta == 1)) {
2022-07-10 05:45:26 +00:00
isAlter = true;
2022-07-10 03:17:51 +00:00
mInfo("stb:%s, schema version is only increased by 1 number, do alter operation", createReq.name);
} else {
mError("stb:%s, schema version increase more than 1 number, error is returned", createReq.name);
terrno = TSDB_CODE_MND_INVALID_SCHEMA_VER;
goto _OVER;
}
} else {
2022-09-30 06:03:46 +00:00
mError("stb:%s, already exist while create, input tagVer:%d colVer:%d is invalid, origin tagVer:%d colVer:%d",
createReq.name, createReq.tagVer, createReq.colVer, pStb->tagVer, pStb->colVer);
2022-07-10 03:17:51 +00:00
terrno = TSDB_CODE_MND_INVALID_SCHEMA_VER;
goto _OVER;
}
2021-12-10 07:20:04 +00:00
} else {
terrno = TSDB_CODE_MND_STB_ALREADY_EXIST;
goto _OVER;
2021-12-10 07:20:04 +00:00
}
2022-01-11 02:48:19 +00:00
} else if (terrno != TSDB_CODE_MND_STB_NOT_EXIST) {
goto _OVER;
} else if (createReq.source == TD_REQ_FROM_TAOX && (createReq.tagVer != 1 || createReq.colVer != 1)) {
mInfo("stb:%s, alter table does not need to be done, because table is deleted", createReq.name);
code = 0;
goto _OVER;
2021-12-10 07:20:04 +00:00
}
2022-02-09 10:28:11 +00:00
pDb = mndAcquireDbByStb(pMnode, createReq.name);
2021-12-10 07:20:04 +00:00
if (pDb == NULL) {
terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
goto _OVER;
2021-12-10 07:20:04 +00:00
}
2022-06-25 01:09:33 +00:00
if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
goto _OVER;
2022-02-15 09:24:34 +00:00
}
2022-04-28 08:31:19 +00:00
int32_t numOfStbs = -1;
2022-05-07 04:07:45 +00:00
if (mndGetNumOfStbs(pMnode, pDb->name, &numOfStbs) != 0) {
goto _OVER;
}
2022-04-29 12:11:58 +00:00
if (pDb->cfg.numOfStables == 1 && numOfStbs != 0) {
2022-04-28 08:31:19 +00:00
terrno = TSDB_CODE_MND_SINGLE_STB_MODE_DB;
goto _OVER;
}
2022-08-03 01:18:36 +00:00
if ((terrno = grantCheck(TSDB_GRANT_STABLE)) < 0) {
code = -1;
goto _OVER;
}
2022-07-10 05:45:26 +00:00
if (isAlter) {
bool needRsp = false;
2022-07-11 08:12:07 +00:00
SStbObj pDst = {0};
2022-07-11 09:36:07 +00:00
if (mndBuildStbFromAlter(pStb, &pDst, &createReq) != 0) {
taosMemoryFreeClear(pDst.pTags);
taosMemoryFreeClear(pDst.pColumns);
2022-07-11 08:12:07 +00:00
goto _OVER;
}
code = mndAlterStbImp(pMnode, pReq, pDb, &pDst, needRsp, NULL, 0);
taosMemoryFreeClear(pDst.pTags);
taosMemoryFreeClear(pDst.pColumns);
2022-07-10 05:45:26 +00:00
} else {
code = mndCreateStb(pMnode, pReq, &createReq, pDb);
}
2022-05-21 08:35:24 +00:00
if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
2021-12-10 07:20:04 +00:00
_OVER:
2022-05-21 08:35:24 +00:00
if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
2022-02-09 10:28:11 +00:00
mError("stb:%s, failed to create since %s", createReq.name, terrstr());
2021-12-10 07:20:04 +00:00
}
2022-02-09 10:28:11 +00:00
mndReleaseStb(pMnode, pStb);
mndReleaseDb(pMnode, pDb);
2022-02-15 09:24:34 +00:00
tFreeSMCreateStbReq(&createReq);
2022-02-09 10:28:11 +00:00
return code;
2021-12-10 07:20:04 +00:00
}
2022-05-07 05:06:23 +00:00
static int32_t mndCheckAlterStbReq(SMAlterStbReq *pAlter) {
2022-07-01 02:52:59 +00:00
if (pAlter->commentLen >= 0) return 0;
if (pAlter->ttl != 0) return 0;
2022-05-11 03:26:45 +00:00
2022-02-09 12:29:42 +00:00
if (pAlter->numOfFields < 1 || pAlter->numOfFields != (int32_t)taosArrayGetSize(pAlter->pFields)) {
terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
return -1;
}
2021-12-12 02:46:49 +00:00
2022-02-09 12:29:42 +00:00
for (int32_t i = 0; i < pAlter->numOfFields; ++i) {
SField *pField = taosArrayGet(pAlter->pFields, i);
if (pField->name[0] == 0) {
2022-02-08 05:45:24 +00:00
terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
return -1;
}
2021-12-12 02:46:49 +00:00
}
return 0;
}
2022-02-08 08:50:47 +00:00
static int32_t mndAllocStbSchemas(const SStbObj *pOld, SStbObj *pNew) {
2022-03-25 16:29:53 +00:00
pNew->pTags = taosMemoryCalloc(pNew->numOfTags, sizeof(SSchema));
pNew->pColumns = taosMemoryCalloc(pNew->numOfColumns, sizeof(SSchema));
2022-02-08 08:50:47 +00:00
if (pNew->pTags == NULL || pNew->pColumns == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
memcpy(pNew->pColumns, pOld->pColumns, sizeof(SSchema) * pOld->numOfColumns);
memcpy(pNew->pTags, pOld->pTags, sizeof(SSchema) * pOld->numOfTags);
return 0;
}
2022-05-11 03:37:13 +00:00
static int32_t mndUpdateStbCommentAndTTL(const SStbObj *pOld, SStbObj *pNew, char *pComment, int32_t commentLen,
int32_t ttl) {
2022-05-11 03:26:45 +00:00
if (commentLen > 0) {
pNew->commentLen = commentLen;
2022-06-16 12:45:00 +00:00
pNew->comment = taosMemoryCalloc(1, commentLen + 1);
2022-05-11 03:26:45 +00:00
if (pNew->comment == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
2022-06-16 12:45:00 +00:00
memcpy(pNew->comment, pComment, commentLen + 1);
} else if (commentLen == 0) {
2022-06-16 12:45:00 +00:00
pNew->commentLen = 0;
2022-07-01 02:52:59 +00:00
} else {
2022-05-11 03:26:45 +00:00
}
2022-06-16 12:45:00 +00:00
2022-05-11 03:37:13 +00:00
if (ttl >= 0) {
pNew->ttl = ttl;
}
2022-05-11 03:26:45 +00:00
if (mndAllocStbSchemas(pOld, pNew) != 0) {
return -1;
}
return 0;
}
2022-02-09 12:29:42 +00:00
static int32_t mndAddSuperTableTag(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ntags) {
2022-02-08 08:50:47 +00:00
if (pOld->numOfTags + ntags > TSDB_MAX_TAGS) {
terrno = TSDB_CODE_MND_TOO_MANY_TAGS;
return -1;
}
if (pOld->numOfColumns + ntags + pOld->numOfTags > TSDB_MAX_COLUMNS) {
terrno = TSDB_CODE_MND_TOO_MANY_COLUMNS;
return -1;
}
2022-02-09 12:29:42 +00:00
pNew->numOfTags = pNew->numOfTags + ntags;
if (mndAllocStbSchemas(pOld, pNew) != 0) {
return -1;
}
2022-02-08 08:50:47 +00:00
for (int32_t i = 0; i < ntags; i++) {
2022-02-09 12:29:42 +00:00
SField *pField = taosArrayGet(pFields, i);
2022-05-11 02:31:35 +00:00
if (mndFindSuperTableColumnIndex(pOld, pField->name) >= 0) {
2022-02-09 02:52:06 +00:00
terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
2022-02-08 08:50:47 +00:00
return -1;
}
2022-05-11 02:31:35 +00:00
if (mndFindSuperTableTagIndex(pOld, pField->name) >= 0) {
2022-02-09 02:52:06 +00:00
terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
2022-02-08 08:50:47 +00:00
return -1;
}
2022-02-09 12:29:42 +00:00
SSchema *pSchema = &pNew->pTags[pOld->numOfTags + i];
pSchema->bytes = pField->bytes;
pSchema->type = pField->type;
memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
2022-02-08 08:50:47 +00:00
pSchema->colId = pNew->nextColId;
pNew->nextColId++;
2022-02-09 12:29:42 +00:00
2022-09-23 07:42:36 +00:00
mInfo("stb:%s, start to add tag %s", pNew->name, pSchema->name);
2022-02-08 08:50:47 +00:00
}
pNew->tagVer++;
2022-02-08 08:50:47 +00:00
return 0;
}
static int32_t mndCheckAlterColForTopic(SMnode *pMnode, const char *stbFullName, int64_t suid, col_id_t colId) {
SSdb *pSdb = pMnode->pSdb;
void *pIter = NULL;
while (1) {
SMqTopicObj *pTopic = NULL;
pIter = sdbFetch(pSdb, SDB_TOPIC, pIter, (void **)&pTopic);
if (pIter == NULL) break;
2022-09-23 07:42:36 +00:00
mInfo("topic:%s, check tag and column modifiable, stb:%s suid:%" PRId64 " colId:%d, subType:%d sql:%s",
2022-10-13 03:56:16 +00:00
pTopic->name, stbFullName, suid, colId, pTopic->subType, pTopic->sql);
if (pTopic->subType != TOPIC_SUB_TYPE__COLUMN) {
sdbRelease(pSdb, pTopic);
continue;
}
SNode *pAst = NULL;
if (nodesStringToNode(pTopic->ast, &pAst) != 0) {
ASSERT(0);
return -1;
}
SNodeList *pNodeList = NULL;
nodesCollectColumns((SSelectStmt *)pAst, SQL_CLAUSE_FROM, NULL, COLLECT_COL_TYPE_ALL, &pNodeList);
SNode *pNode = NULL;
FOREACH(pNode, pNodeList) {
SColumnNode *pCol = (SColumnNode *)pNode;
2022-10-13 03:56:16 +00:00
mInfo("topic:%s, check colId:%d tableId:%" PRId64 " ctbStbUid:%" PRId64, pTopic->name, pCol->colId, pCol->tableId,
pTopic->ctbStbUid);
if (pCol->tableId != suid && pTopic->ctbStbUid != suid) {
2022-09-23 07:42:36 +00:00
mInfo("topic:%s, check colId:%d passed", pTopic->name, pCol->colId);
goto NEXT;
}
if (pCol->colId > 0 && pCol->colId == colId) {
terrno = TSDB_CODE_MND_FIELD_CONFLICT_WITH_TOPIC;
mError("topic:%s, check colId:%d conflicted", pTopic->name, pCol->colId);
2022-11-24 06:33:27 +00:00
nodesDestroyNode(pAst);
nodesDestroyList(pNodeList);
sdbRelease(pSdb, pTopic);
return -1;
}
2022-09-23 07:42:36 +00:00
mInfo("topic:%s, check colId:%d passed", pTopic->name, pCol->colId);
}
NEXT:
sdbRelease(pSdb, pTopic);
nodesDestroyNode(pAst);
2022-09-16 06:36:01 +00:00
nodesDestroyList(pNodeList);
}
return 0;
}
static int32_t mndCheckAlterColForStream(SMnode *pMnode, const char *stbFullName, int64_t suid, col_id_t colId) {
SSdb *pSdb = pMnode->pSdb;
void *pIter = NULL;
while (1) {
SStreamObj *pStream = NULL;
pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
if (pIter == NULL) break;
SNode *pAst = NULL;
if (nodesStringToNode(pStream->ast, &pAst) != 0) {
ASSERT(0);
return -1;
}
SNodeList *pNodeList = NULL;
nodesCollectColumns((SSelectStmt *)pAst, SQL_CLAUSE_FROM, NULL, COLLECT_COL_TYPE_ALL, &pNodeList);
SNode *pNode = NULL;
FOREACH(pNode, pNodeList) {
SColumnNode *pCol = (SColumnNode *)pNode;
if (pCol->tableId != suid) {
2022-09-23 07:42:36 +00:00
mInfo("stream:%s, check colId:%d passed", pStream->name, pCol->colId);
goto NEXT;
}
if (pCol->colId > 0 && pCol->colId == colId) {
terrno = TSDB_CODE_MND_STREAM_MUST_BE_DELETED;
mError("stream:%s, check colId:%d conflicted", pStream->name, pCol->colId);
2022-11-24 06:33:27 +00:00
nodesDestroyNode(pAst);
nodesDestroyList(pNodeList);
sdbRelease(pSdb, pStream);
return -1;
}
2022-09-23 07:42:36 +00:00
mInfo("stream:%s, check colId:%d passed", pStream->name, pCol->colId);
}
NEXT:
sdbRelease(pSdb, pStream);
nodesDestroyNode(pAst);
2022-09-16 06:36:01 +00:00
nodesDestroyList(pNodeList);
}
return 0;
}
static int32_t mndCheckAlterColForTSma(SMnode *pMnode, const char *stbFullName, int64_t suid, col_id_t colId) {
SSdb *pSdb = pMnode->pSdb;
void *pIter = NULL;
while (1) {
SSmaObj *pSma = NULL;
pIter = sdbFetch(pSdb, SDB_SMA, pIter, (void **)&pSma);
if (pIter == NULL) break;
2022-10-13 03:56:16 +00:00
mInfo("tsma:%s, check tag and column modifiable, stb:%s suid:%" PRId64 " colId:%d, sql:%s", pSma->name, stbFullName,
suid, colId, pSma->sql);
SNode *pAst = NULL;
if (nodesStringToNode(pSma->ast, &pAst) != 0) {
terrno = TSDB_CODE_SDB_INVALID_DATA_CONTENT;
mError("tsma:%s, check tag and column modifiable, stb:%s suid:%" PRId64 " colId:%d failed since parse AST err",
pSma->name, stbFullName, suid, colId);
return -1;
}
SNodeList *pNodeList = NULL;
nodesCollectColumns((SSelectStmt *)pAst, SQL_CLAUSE_FROM, NULL, COLLECT_COL_TYPE_ALL, &pNodeList);
SNode *pNode = NULL;
FOREACH(pNode, pNodeList) {
SColumnNode *pCol = (SColumnNode *)pNode;
2022-09-23 07:42:36 +00:00
mInfo("tsma:%s, check colId:%d tableId:%" PRId64, pSma->name, pCol->colId, pCol->tableId);
if ((pCol->tableId != suid) && (pSma->stbUid != suid)) {
2022-09-23 07:42:36 +00:00
mInfo("tsma:%s, check colId:%d passed", pSma->name, pCol->colId);
goto NEXT;
}
if ((pCol->colId) > 0 && (pCol->colId == colId)) {
terrno = TSDB_CODE_MND_FIELD_CONFLICT_WITH_TSMA;
mError("tsma:%s, check colId:%d conflicted", pSma->name, pCol->colId);
2022-11-24 06:33:27 +00:00
nodesDestroyNode(pAst);
nodesDestroyList(pNodeList);
sdbRelease(pSdb, pSma);
return -1;
}
2022-09-23 07:42:36 +00:00
mInfo("tsma:%s, check colId:%d passed", pSma->name, pCol->colId);
}
NEXT:
sdbRelease(pSdb, pSma);
nodesDestroyNode(pAst);
2022-09-16 06:36:01 +00:00
nodesDestroyList(pNodeList);
}
return 0;
}
int32_t mndCheckColAndTagModifiable(SMnode *pMnode, const char *stbFullName, int64_t suid, col_id_t colId) {
if (mndCheckAlterColForTopic(pMnode, stbFullName, suid, colId) < 0) {
return -1;
}
if (mndCheckAlterColForStream(pMnode, stbFullName, suid, colId) < 0) {
return -1;
}
if (mndCheckAlterColForTSma(pMnode, stbFullName, suid, colId) < 0) {
return -1;
}
return 0;
}
static int32_t mndDropSuperTableTag(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const char *tagName) {
2022-02-08 08:50:47 +00:00
int32_t tag = mndFindSuperTableTagIndex(pOld, tagName);
if (tag < 0) {
terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
return -1;
}
col_id_t colId = pOld->pTags[tag].colId;
if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
return -1;
}
2022-02-08 08:50:47 +00:00
if (mndAllocStbSchemas(pOld, pNew) != 0) {
return -1;
}
memmove(pNew->pTags + tag, pNew->pTags + tag + 1, sizeof(SSchema) * (pNew->numOfTags - tag - 1));
2022-02-09 02:52:06 +00:00
pNew->numOfTags--;
2022-02-08 08:50:47 +00:00
pNew->tagVer++;
2022-09-23 07:42:36 +00:00
mInfo("stb:%s, start to drop tag %s", pNew->name, tagName);
2022-02-08 08:50:47 +00:00
return 0;
}
static int32_t mndAlterStbTagName(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, SArray *pFields) {
2022-02-09 12:29:42 +00:00
if ((int32_t)taosArrayGetSize(pFields) != 2) {
terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
return -1;
}
SField *pField0 = taosArrayGet(pFields, 0);
SField *pField1 = taosArrayGet(pFields, 1);
const char *oldTagName = pField0->name;
const char *newTagName = pField1->name;
2022-02-08 08:50:47 +00:00
int32_t tag = mndFindSuperTableTagIndex(pOld, oldTagName);
if (tag < 0) {
terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
return -1;
}
col_id_t colId = pOld->pTags[tag].colId;
if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
return -1;
}
2022-02-08 08:50:47 +00:00
if (mndFindSuperTableTagIndex(pOld, newTagName) >= 0) {
2022-02-09 02:52:06 +00:00
terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
2022-02-08 08:50:47 +00:00
return -1;
}
2022-02-09 02:52:06 +00:00
if (mndFindSuperTableColumnIndex(pOld, newTagName) >= 0) {
terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
2022-02-08 08:50:47 +00:00
return -1;
}
if (mndAllocStbSchemas(pOld, pNew) != 0) {
return -1;
}
SSchema *pSchema = (SSchema *)(pNew->pTags + tag);
memcpy(pSchema->name, newTagName, TSDB_COL_NAME_LEN);
pNew->tagVer++;
2022-09-23 07:42:36 +00:00
mInfo("stb:%s, start to modify tag %s to %s", pNew->name, oldTagName, newTagName);
2022-02-08 08:50:47 +00:00
return 0;
}
static int32_t mndAlterStbTagBytes(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
2022-02-09 12:29:42 +00:00
int32_t tag = mndFindSuperTableTagIndex(pOld, pField->name);
2022-02-08 08:50:47 +00:00
if (tag < 0) {
terrno = TSDB_CODE_MND_TAG_NOT_EXIST;
return -1;
}
col_id_t colId = pOld->pTags[tag].colId;
if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
return -1;
}
2022-02-08 08:50:47 +00:00
if (mndAllocStbSchemas(pOld, pNew) != 0) {
return -1;
}
SSchema *pTag = pNew->pTags + tag;
2022-02-09 03:36:17 +00:00
if (!(pTag->type == TSDB_DATA_TYPE_BINARY || pTag->type == TSDB_DATA_TYPE_NCHAR)) {
terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
return -1;
}
2022-02-09 12:29:42 +00:00
if (pField->bytes <= pTag->bytes) {
2022-02-09 06:38:02 +00:00
terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
return -1;
}
2022-02-09 12:29:42 +00:00
pTag->bytes = pField->bytes;
pNew->tagVer++;
2022-02-08 08:50:47 +00:00
2022-09-23 07:42:36 +00:00
mInfo("stb:%s, start to modify tag len %s to %d", pNew->name, pField->name, pField->bytes);
2022-02-08 08:50:47 +00:00
return 0;
}
2022-02-09 12:29:42 +00:00
static int32_t mndAddSuperTableColumn(const SStbObj *pOld, SStbObj *pNew, SArray *pFields, int32_t ncols) {
2022-02-08 08:50:47 +00:00
if (pOld->numOfColumns + ncols + pOld->numOfTags > TSDB_MAX_COLUMNS) {
terrno = TSDB_CODE_MND_TOO_MANY_COLUMNS;
return -1;
}
2022-02-09 12:29:42 +00:00
pNew->numOfColumns = pNew->numOfColumns + ncols;
if (mndAllocStbSchemas(pOld, pNew) != 0) {
return -1;
}
2022-02-08 08:50:47 +00:00
for (int32_t i = 0; i < ncols; i++) {
2022-02-09 12:29:42 +00:00
SField *pField = taosArrayGet(pFields, i);
2022-05-11 02:31:35 +00:00
if (mndFindSuperTableColumnIndex(pOld, pField->name) >= 0) {
2022-02-09 06:38:02 +00:00
terrno = TSDB_CODE_MND_COLUMN_ALREADY_EXIST;
2022-02-08 08:50:47 +00:00
return -1;
}
2022-05-11 02:31:35 +00:00
if (mndFindSuperTableTagIndex(pOld, pField->name) >= 0) {
2022-02-09 06:38:02 +00:00
terrno = TSDB_CODE_MND_TAG_ALREADY_EXIST;
2022-02-08 08:50:47 +00:00
return -1;
}
2022-02-09 12:29:42 +00:00
SSchema *pSchema = &pNew->pColumns[pOld->numOfColumns + i];
pSchema->bytes = pField->bytes;
pSchema->type = pField->type;
memcpy(pSchema->name, pField->name, TSDB_COL_NAME_LEN);
2022-02-08 08:50:47 +00:00
pSchema->colId = pNew->nextColId;
pNew->nextColId++;
2022-02-09 12:29:42 +00:00
2022-09-23 07:42:36 +00:00
mInfo("stb:%s, start to add column %s", pNew->name, pSchema->name);
2022-02-08 08:50:47 +00:00
}
pNew->colVer++;
2022-02-08 08:50:47 +00:00
return 0;
}
static int32_t mndDropSuperTableColumn(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const char *colName) {
2022-02-08 08:50:47 +00:00
int32_t col = mndFindSuperTableColumnIndex(pOld, colName);
2022-02-09 06:38:02 +00:00
if (col < 0) {
2022-02-08 08:50:47 +00:00
terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
return -1;
}
2022-02-09 06:38:02 +00:00
if (col == 0) {
terrno = TSDB_CODE_MND_INVALID_STB_ALTER_OPTION;
return -1;
}
if (pOld->numOfColumns == 2) {
terrno = TSDB_CODE_MND_INVALID_STB_ALTER_OPTION;
return -1;
}
col_id_t colId = pOld->pColumns[col].colId;
if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
return -1;
}
2022-02-08 08:50:47 +00:00
if (mndAllocStbSchemas(pOld, pNew) != 0) {
return -1;
}
memmove(pNew->pColumns + col, pNew->pColumns + col + 1, sizeof(SSchema) * (pNew->numOfColumns - col - 1));
2022-02-09 02:52:06 +00:00
pNew->numOfColumns--;
2022-02-08 08:50:47 +00:00
pNew->colVer++;
2022-09-23 07:42:36 +00:00
mInfo("stb:%s, start to drop col %s", pNew->name, colName);
2022-02-08 08:50:47 +00:00
return 0;
}
static int32_t mndAlterStbColumnBytes(SMnode *pMnode, const SStbObj *pOld, SStbObj *pNew, const SField *pField) {
2022-02-09 12:29:42 +00:00
int32_t col = mndFindSuperTableColumnIndex(pOld, pField->name);
2022-02-08 08:50:47 +00:00
if (col < 0) {
terrno = TSDB_CODE_MND_COLUMN_NOT_EXIST;
return -1;
}
uint32_t nLen = 0;
for (int32_t i = 0; i < pOld->numOfColumns; ++i) {
2022-02-09 12:29:42 +00:00
nLen += (pOld->pColumns[i].colId == col) ? pField->bytes : pOld->pColumns[i].bytes;
2022-02-08 08:50:47 +00:00
}
if (nLen > TSDB_MAX_BYTES_PER_ROW) {
terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
return -1;
}
col_id_t colId = pOld->pColumns[col].colId;
if (mndCheckColAndTagModifiable(pMnode, pOld->name, pOld->uid, colId) != 0) {
return -1;
}
2022-02-08 08:50:47 +00:00
if (mndAllocStbSchemas(pOld, pNew) != 0) {
return -1;
}
SSchema *pCol = pNew->pColumns + col;
2022-02-09 06:38:02 +00:00
if (!(pCol->type == TSDB_DATA_TYPE_BINARY || pCol->type == TSDB_DATA_TYPE_NCHAR)) {
terrno = TSDB_CODE_MND_INVALID_STB_OPTION;
2022-02-08 08:50:47 +00:00
return -1;
}
2022-02-09 12:29:42 +00:00
if (pField->bytes <= pCol->bytes) {
2022-02-09 06:38:02 +00:00
terrno = TSDB_CODE_MND_INVALID_ROW_BYTES;
2022-02-09 03:36:17 +00:00
return -1;
}
2022-02-09 12:29:42 +00:00
pCol->bytes = pField->bytes;
pNew->colVer++;
2022-02-08 08:50:47 +00:00
2022-09-23 07:42:36 +00:00
mInfo("stb:%s, start to modify col len %s to %d", pNew->name, pField->name, pField->bytes);
2022-02-08 08:50:47 +00:00
return 0;
}
2022-02-08 11:52:48 +00:00
static int32_t mndSetAlterStbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
2022-02-08 09:08:40 +00:00
SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
if (pRedoRaw == NULL) return -1;
2022-07-01 02:52:59 +00:00
if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
sdbFreeRaw(pRedoRaw);
return -1;
}
2022-04-29 03:40:29 +00:00
if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_READY) != 0) return -1;
2022-02-08 09:08:40 +00:00
return 0;
}
2022-02-08 11:52:48 +00:00
static int32_t mndSetAlterStbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
2022-02-08 09:08:40 +00:00
SSdbRaw *pCommitRaw = mndStbActionEncode(pStb);
if (pCommitRaw == NULL) return -1;
2022-07-01 02:52:59 +00:00
if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
sdbFreeRaw(pCommitRaw);
return -1;
}
2022-02-08 09:08:40 +00:00
if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY) != 0) return -1;
return 0;
}
static int32_t mndSetAlterStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb, void *alterOriData,
int32_t alterOriDataLen) {
2022-02-08 09:08:40 +00:00
SSdb *pSdb = pMnode->pSdb;
SVgObj *pVgroup = NULL;
void *pIter = NULL;
int32_t contLen;
while (1) {
pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
if (pIter == NULL) break;
2022-07-01 09:02:33 +00:00
if (!mndVgroupInDb(pVgroup, pDb->uid)) {
2022-07-01 05:47:53 +00:00
sdbRelease(pSdb, pVgroup);
continue;
}
2022-07-10 10:54:58 +00:00
void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen, alterOriData, alterOriDataLen);
2022-02-08 09:08:40 +00:00
if (pReq == NULL) {
sdbCancelFetch(pSdb, pIter);
sdbRelease(pSdb, pVgroup);
return -1;
}
STransAction action = {0};
action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
action.pCont = pReq;
action.contLen = contLen;
2022-02-08 11:52:48 +00:00
action.msgType = TDMT_VND_ALTER_STB;
2022-02-08 09:08:40 +00:00
if (mndTransAppendRedoAction(pTrans, &action) != 0) {
2022-03-25 16:29:53 +00:00
taosMemoryFree(pReq);
2022-02-08 09:08:40 +00:00
sdbCancelFetch(pSdb, pIter);
sdbRelease(pSdb, pVgroup);
return -1;
}
sdbRelease(pSdb, pVgroup);
}
return 0;
}
2022-06-02 04:34:35 +00:00
static int32_t mndBuildStbSchemaImp(SDbObj *pDb, SStbObj *pStb, const char *tbName, STableMetaRsp *pRsp) {
taosRLockLatch(&pStb->lock);
int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
pRsp->pSchemas = taosMemoryCalloc(totalCols, sizeof(SSchema));
if (pRsp->pSchemas == NULL) {
taosRUnLockLatch(&pStb->lock);
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
2022-09-30 06:03:46 +00:00
tstrncpy(pRsp->dbFName, pStb->db, sizeof(pRsp->dbFName));
tstrncpy(pRsp->tbName, tbName, sizeof(pRsp->tbName));
tstrncpy(pRsp->stbName, tbName, sizeof(pRsp->stbName));
2022-06-02 04:34:35 +00:00
pRsp->dbId = pDb->uid;
pRsp->numOfTags = pStb->numOfTags;
pRsp->numOfColumns = pStb->numOfColumns;
pRsp->precision = pDb->cfg.precision;
pRsp->tableType = TSDB_SUPER_TABLE;
pRsp->sversion = pStb->colVer;
pRsp->tversion = pStb->tagVer;
pRsp->suid = pStb->uid;
pRsp->tuid = pStb->uid;
for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
SSchema *pSchema = &pRsp->pSchemas[i];
SSchema *pSrcSchema = &pStb->pColumns[i];
memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
pSchema->type = pSrcSchema->type;
pSchema->colId = pSrcSchema->colId;
pSchema->bytes = pSrcSchema->bytes;
}
for (int32_t i = 0; i < pStb->numOfTags; ++i) {
SSchema *pSchema = &pRsp->pSchemas[i + pStb->numOfColumns];
SSchema *pSrcSchema = &pStb->pTags[i];
memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
pSchema->type = pSrcSchema->type;
pSchema->colId = pSrcSchema->colId;
pSchema->bytes = pSrcSchema->bytes;
}
taosRUnLockLatch(&pStb->lock);
return 0;
}
2022-06-20 12:58:36 +00:00
static int32_t mndBuildStbCfgImp(SDbObj *pDb, SStbObj *pStb, const char *tbName, STableCfgRsp *pRsp) {
taosRLockLatch(&pStb->lock);
int32_t totalCols = pStb->numOfColumns + pStb->numOfTags;
pRsp->pSchemas = taosMemoryCalloc(totalCols, sizeof(SSchema));
if (pRsp->pSchemas == NULL) {
taosRUnLockLatch(&pStb->lock);
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
2022-09-30 06:03:46 +00:00
tstrncpy(pRsp->dbFName, pStb->db, sizeof(pRsp->dbFName));
tstrncpy(pRsp->tbName, tbName, sizeof(pRsp->tbName));
tstrncpy(pRsp->stbName, tbName, sizeof(pRsp->stbName));
2022-06-20 12:58:36 +00:00
pRsp->numOfTags = pStb->numOfTags;
pRsp->numOfColumns = pStb->numOfColumns;
pRsp->tableType = TSDB_SUPER_TABLE;
2022-06-21 10:41:12 +00:00
pRsp->delay1 = pStb->maxdelay[0];
pRsp->delay2 = pStb->maxdelay[1];
pRsp->watermark1 = pStb->watermark[0];
pRsp->watermark2 = pStb->watermark[1];
2022-06-20 12:58:36 +00:00
pRsp->ttl = pStb->ttl;
pRsp->commentLen = pStb->commentLen;
if (pStb->commentLen > 0) {
pRsp->pComment = strdup(pStb->comment);
}
for (int32_t i = 0; i < pStb->numOfColumns; ++i) {
SSchema *pSchema = &pRsp->pSchemas[i];
SSchema *pSrcSchema = &pStb->pColumns[i];
memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
pSchema->type = pSrcSchema->type;
pSchema->colId = pSrcSchema->colId;
pSchema->bytes = pSrcSchema->bytes;
}
for (int32_t i = 0; i < pStb->numOfTags; ++i) {
SSchema *pSchema = &pRsp->pSchemas[i + pStb->numOfColumns];
SSchema *pSrcSchema = &pStb->pTags[i];
memcpy(pSchema->name, pSrcSchema->name, TSDB_COL_NAME_LEN);
pSchema->type = pSrcSchema->type;
pSchema->colId = pSrcSchema->colId;
pSchema->bytes = pSrcSchema->bytes;
}
2022-07-01 05:47:53 +00:00
if (pStb->numOfFuncs > 0) {
2022-11-28 08:13:18 +00:00
pRsp->pFuncs = taosArrayDup(pStb->pFuncs, NULL);
2022-06-20 12:58:36 +00:00
}
2022-06-25 10:03:12 +00:00
2022-06-20 12:58:36 +00:00
taosRUnLockLatch(&pStb->lock);
return 0;
}
2022-06-16 08:01:22 +00:00
static int32_t mndBuildStbSchema(SMnode *pMnode, const char *dbFName, const char *tbName, STableMetaRsp *pRsp,
int32_t *smaVer) {
2022-06-02 04:34:35 +00:00
char tbFName[TSDB_TABLE_FNAME_LEN] = {0};
snprintf(tbFName, sizeof(tbFName), "%s.%s", dbFName, tbName);
SDbObj *pDb = mndAcquireDb(pMnode, dbFName);
if (pDb == NULL) {
terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
return -1;
}
SStbObj *pStb = mndAcquireStb(pMnode, tbFName);
if (pStb == NULL) {
mndReleaseDb(pMnode, pDb);
2022-06-15 12:58:18 +00:00
terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
2022-06-02 04:34:35 +00:00
return -1;
}
2022-06-13 02:38:45 +00:00
if (smaVer) {
*smaVer = pStb->smaVer;
}
2022-06-02 04:34:35 +00:00
int32_t code = mndBuildStbSchemaImp(pDb, pStb, tbName, pRsp);
mndReleaseDb(pMnode, pDb);
mndReleaseStb(pMnode, pStb);
return code;
}
2022-06-20 12:58:36 +00:00
static int32_t mndBuildStbCfg(SMnode *pMnode, const char *dbFName, const char *tbName, STableCfgRsp *pRsp) {
2022-06-25 10:03:12 +00:00
char tbFName[TSDB_TABLE_FNAME_LEN] = {0};
snprintf(tbFName, sizeof(tbFName), "%s.%s", dbFName, tbName);
SDbObj *pDb = mndAcquireDb(pMnode, dbFName);
if (pDb == NULL) {
terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
return -1;
}
SStbObj *pStb = mndAcquireStb(pMnode, tbFName);
if (pStb == NULL) {
mndReleaseDb(pMnode, pDb);
terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
return -1;
}
2022-06-20 12:58:36 +00:00
2022-06-25 10:03:12 +00:00
int32_t code = mndBuildStbCfgImp(pDb, pStb, tbName, pRsp);
2022-06-20 12:58:36 +00:00
2022-06-25 10:03:12 +00:00
mndReleaseDb(pMnode, pDb);
mndReleaseStb(pMnode, pStb);
return code;
}
2022-06-20 12:58:36 +00:00
2022-07-10 05:45:26 +00:00
static int32_t mndBuildSMAlterStbRsp(SDbObj *pDb, SStbObj *pObj, void **pCont, int32_t *pLen) {
2022-06-14 07:46:16 +00:00
int32_t ret;
2022-06-01 12:28:29 +00:00
SEncoder ec = {0};
uint32_t contLen = 0;
2022-06-01 12:28:29 +00:00
SMAlterStbRsp alterRsp = {0};
SName name = {0};
2022-07-10 05:45:26 +00:00
tNameFromString(&name, pObj->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
2022-06-01 12:28:29 +00:00
alterRsp.pMeta = taosMemoryCalloc(1, sizeof(STableMetaRsp));
if (NULL == alterRsp.pMeta) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
2022-06-02 04:34:35 +00:00
ret = mndBuildStbSchemaImp(pDb, pObj, name.tname, alterRsp.pMeta);
2022-06-01 12:28:29 +00:00
if (ret) {
tFreeSMAlterStbRsp(&alterRsp);
return ret;
}
2022-06-01 12:28:29 +00:00
tEncodeSize(tEncodeSMAlterStbRsp, &alterRsp, contLen, ret);
if (ret) {
tFreeSMAlterStbRsp(&alterRsp);
return ret;
}
void *cont = taosMemoryMalloc(contLen);
2022-06-01 12:28:29 +00:00
tEncoderInit(&ec, cont, contLen);
tEncodeSMAlterStbRsp(&ec, &alterRsp);
tEncoderClear(&ec);
tFreeSMAlterStbRsp(&alterRsp);
*pCont = cont;
*pLen = contLen;
2022-06-01 12:28:29 +00:00
return 0;
}
2022-09-16 06:36:01 +00:00
int32_t mndBuildSMCreateStbRsp(SMnode *pMnode, char *dbFName, char *stbFName, void **pCont, int32_t *pLen) {
int32_t ret = -1;
SDbObj *pDb = mndAcquireDb(pMnode, dbFName);
if (NULL == pDb) {
return -1;
}
SStbObj *pObj = mndAcquireStb(pMnode, stbFName);
if (NULL == pObj) {
goto _OVER;
}
2022-09-16 06:36:01 +00:00
SEncoder ec = {0};
uint32_t contLen = 0;
SMCreateStbRsp stbRsp = {0};
2022-09-16 06:36:01 +00:00
SName name = {0};
tNameFromString(&name, pObj->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
stbRsp.pMeta = taosMemoryCalloc(1, sizeof(STableMetaRsp));
if (NULL == stbRsp.pMeta) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
goto _OVER;
}
ret = mndBuildStbSchemaImp(pDb, pObj, name.tname, stbRsp.pMeta);
if (ret) {
tFreeSMCreateStbRsp(&stbRsp);
goto _OVER;
}
tEncodeSize(tEncodeSMCreateStbRsp, &stbRsp, contLen, ret);
if (ret) {
tFreeSMCreateStbRsp(&stbRsp);
goto _OVER;
}
void *cont = taosMemoryMalloc(contLen);
tEncoderInit(&ec, cont, contLen);
tEncodeSMCreateStbRsp(&ec, &stbRsp);
tEncoderClear(&ec);
tFreeSMCreateStbRsp(&stbRsp);
*pCont = cont;
*pLen = contLen;
ret = 0;
2022-09-16 06:36:01 +00:00
_OVER:
if (pObj) {
mndReleaseStb(pMnode, pObj);
}
2022-09-16 06:36:01 +00:00
if (pDb) {
mndReleaseDb(pMnode, pDb);
}
return ret;
}
static int32_t mndAlterStbImp(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb, bool needRsp,
void *alterOriData, int32_t alterOriDataLen) {
2022-07-10 05:45:26 +00:00
int32_t code = -1;
2022-09-22 08:18:51 +00:00
STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB_INSIDE, pReq, "alter-stb");
2022-07-10 05:45:26 +00:00
if (pTrans == NULL) goto _OVER;
2022-09-23 07:42:36 +00:00
mInfo("trans:%d, used to alter stb:%s", pTrans->id, pStb->name);
2022-07-25 14:14:10 +00:00
mndTransSetDbName(pTrans, pDb->name, pStb->name);
2022-07-10 05:45:26 +00:00
if (needRsp) {
void *pCont = NULL;
int32_t contLen = 0;
if (mndBuildSMAlterStbRsp(pDb, pStb, &pCont, &contLen) != 0) goto _OVER;
mndTransSetRpcRsp(pTrans, pCont, contLen);
}
if (mndSetAlterStbRedoLogs(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
if (mndSetAlterStbCommitLogs(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
2022-07-10 11:01:56 +00:00
if (mndSetAlterStbRedoActions(pMnode, pTrans, pDb, pStb, alterOriData, alterOriDataLen) != 0) goto _OVER;
2022-07-10 05:45:26 +00:00
if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
code = 0;
_OVER:
mndTransDrop(pTrans);
return code;
}
2022-05-16 06:55:31 +00:00
static int32_t mndAlterStb(SMnode *pMnode, SRpcMsg *pReq, const SMAlterStbReq *pAlter, SDbObj *pDb, SStbObj *pOld) {
bool needRsp = true;
int32_t code = -1;
SField *pField0 = NULL;
2022-02-08 09:08:40 +00:00
SStbObj stbObj = {0};
taosRLockLatch(&pOld->lock);
memcpy(&stbObj, pOld, sizeof(SStbObj));
taosRUnLockLatch(&pOld->lock);
2022-02-08 09:08:40 +00:00
stbObj.pColumns = NULL;
stbObj.pTags = NULL;
stbObj.updateTime = taosGetTimestampMs();
2022-06-01 12:28:29 +00:00
stbObj.lock = 0;
2022-02-15 09:24:34 +00:00
2022-02-08 11:52:48 +00:00
switch (pAlter->alterType) {
2022-02-09 02:52:06 +00:00
case TSDB_ALTER_TABLE_ADD_TAG:
2022-02-09 12:29:42 +00:00
code = mndAddSuperTableTag(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
2022-02-08 08:50:47 +00:00
break;
2022-02-09 02:52:06 +00:00
case TSDB_ALTER_TABLE_DROP_TAG:
2022-05-11 03:26:45 +00:00
pField0 = taosArrayGet(pAlter->pFields, 0);
code = mndDropSuperTableTag(pMnode, pOld, &stbObj, pField0->name);
2022-02-08 08:50:47 +00:00
break;
2022-02-08 09:13:43 +00:00
case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
code = mndAlterStbTagName(pMnode, pOld, &stbObj, pAlter->pFields);
2022-02-08 08:50:47 +00:00
break;
2022-02-08 09:13:43 +00:00
case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
2022-05-11 03:26:45 +00:00
pField0 = taosArrayGet(pAlter->pFields, 0);
code = mndAlterStbTagBytes(pMnode, pOld, &stbObj, pField0);
2022-02-08 08:50:47 +00:00
break;
case TSDB_ALTER_TABLE_ADD_COLUMN:
2022-02-09 12:29:42 +00:00
code = mndAddSuperTableColumn(pOld, &stbObj, pAlter->pFields, pAlter->numOfFields);
2022-02-08 08:50:47 +00:00
break;
case TSDB_ALTER_TABLE_DROP_COLUMN:
2022-05-11 03:26:45 +00:00
pField0 = taosArrayGet(pAlter->pFields, 0);
code = mndDropSuperTableColumn(pMnode, pOld, &stbObj, pField0->name);
2022-02-08 08:50:47 +00:00
break;
2022-02-08 09:13:43 +00:00
case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
2022-05-11 03:26:45 +00:00
pField0 = taosArrayGet(pAlter->pFields, 0);
code = mndAlterStbColumnBytes(pMnode, pOld, &stbObj, pField0);
2022-02-08 08:50:47 +00:00
break;
2022-05-11 03:26:45 +00:00
case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
2022-06-01 12:28:29 +00:00
needRsp = false;
2022-05-11 03:37:13 +00:00
code = mndUpdateStbCommentAndTTL(pOld, &stbObj, pAlter->comment, pAlter->commentLen, pAlter->ttl);
2022-05-11 03:26:45 +00:00
break;
2022-02-08 08:50:47 +00:00
default:
2022-06-01 12:28:29 +00:00
needRsp = false;
2022-05-11 02:31:35 +00:00
terrno = TSDB_CODE_OPS_NOT_SUPPORT;
2022-02-08 08:50:47 +00:00
break;
}
if (code != 0) goto _OVER;
2022-07-10 11:01:56 +00:00
code = mndAlterStbImp(pMnode, pReq, pDb, &stbObj, needRsp, pReq->pCont, pReq->contLen);
2022-02-08 08:50:47 +00:00
_OVER:
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear(stbObj.pTags);
taosMemoryFreeClear(stbObj.pColumns);
if (pAlter->commentLen > 0) {
taosMemoryFreeClear(stbObj.comment);
}
2022-02-08 08:50:47 +00:00
return code;
}
2021-12-12 02:46:49 +00:00
2022-06-14 07:46:16 +00:00
static int32_t mndProcessAlterStbReq(SRpcMsg *pReq) {
2022-05-16 06:55:31 +00:00
SMnode *pMnode = pReq->info.node;
2022-05-07 05:06:23 +00:00
int32_t code = -1;
SDbObj *pDb = NULL;
SStbObj *pStb = NULL;
SMAlterStbReq alterReq = {0};
2021-12-12 02:46:49 +00:00
2022-05-16 06:55:31 +00:00
if (tDeserializeSMAlterStbReq(pReq->pCont, pReq->contLen, &alterReq) != 0) {
2022-02-15 09:24:34 +00:00
terrno = TSDB_CODE_INVALID_MSG;
goto _OVER;
2022-02-15 09:24:34 +00:00
}
2021-12-12 02:46:49 +00:00
2022-09-23 07:42:36 +00:00
mInfo("stb:%s, start to alter", alterReq.name);
if (mndCheckAlterStbReq(&alterReq) != 0) goto _OVER;
2021-12-12 02:46:49 +00:00
2022-02-09 12:29:42 +00:00
pDb = mndAcquireDbByStb(pMnode, alterReq.name);
2022-02-09 02:52:06 +00:00
if (pDb == NULL) {
terrno = TSDB_CODE_MND_INVALID_DB;
goto _OVER;
2021-12-12 02:46:49 +00:00
}
2022-02-09 12:29:42 +00:00
pStb = mndAcquireStb(pMnode, alterReq.name);
2022-02-09 02:52:06 +00:00
if (pStb == NULL) {
terrno = TSDB_CODE_MND_STB_NOT_EXIST;
goto _OVER;
2022-02-08 09:08:40 +00:00
}
2021-12-12 02:46:49 +00:00
2022-06-25 01:09:33 +00:00
if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
goto _OVER;
2022-02-15 09:24:34 +00:00
}
2022-02-09 12:29:42 +00:00
code = mndAlterStb(pMnode, pReq, &alterReq, pDb, pStb);
2022-05-21 08:35:24 +00:00
if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
2021-12-12 02:46:49 +00:00
_OVER:
2022-05-21 08:35:24 +00:00
if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
2022-02-09 12:29:42 +00:00
mError("stb:%s, failed to alter since %s", alterReq.name, terrstr());
2021-12-12 02:46:49 +00:00
}
2022-02-09 12:29:42 +00:00
mndReleaseStb(pMnode, pStb);
mndReleaseDb(pMnode, pDb);
tFreeSMAltertbReq(&alterReq);
2022-02-09 12:29:42 +00:00
return code;
2021-12-12 02:46:49 +00:00
}
2021-12-10 07:20:04 +00:00
2021-12-21 06:07:27 +00:00
static int32_t mndSetDropStbRedoLogs(SMnode *pMnode, STrans *pTrans, SStbObj *pStb) {
SSdbRaw *pRedoRaw = mndStbActionEncode(pStb);
if (pRedoRaw == NULL) return -1;
2022-07-01 02:52:59 +00:00
if (mndTransAppendRedolog(pTrans, pRedoRaw) != 0) {
sdbFreeRaw(pRedoRaw);
return -1;
}
2021-12-21 06:07:27 +00:00
if (sdbSetRawStatus(pRedoRaw, SDB_STATUS_DROPPING) != 0) return -1;
return 0;
}
static int32_t mndSetDropStbCommitLogs(SMnode *pMnode, STrans *pTrans, SStbObj *pStb) {
SSdbRaw *pCommitRaw = mndStbActionEncode(pStb);
if (pCommitRaw == NULL) return -1;
2022-07-01 02:52:59 +00:00
if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
sdbFreeRaw(pCommitRaw);
return -1;
}
2021-12-21 06:07:27 +00:00
if (sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED) != 0) return -1;
return 0;
}
2022-01-28 01:29:17 +00:00
static int32_t mndSetDropStbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) {
SSdb *pSdb = pMnode->pSdb;
SVgObj *pVgroup = NULL;
void *pIter = NULL;
2021-12-21 06:07:27 +00:00
2022-01-28 01:29:17 +00:00
while (1) {
pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
if (pIter == NULL) break;
2022-07-01 09:02:33 +00:00
if (!mndVgroupInDb(pVgroup, pDb->uid)) {
2022-07-01 05:47:53 +00:00
sdbRelease(pSdb, pVgroup);
continue;
}
2022-01-28 02:54:50 +00:00
int32_t contLen = 0;
2022-02-09 09:50:52 +00:00
void *pReq = mndBuildVDropStbReq(pMnode, pVgroup, pStb, &contLen);
2022-01-28 01:29:17 +00:00
if (pReq == NULL) {
sdbCancelFetch(pSdb, pIter);
sdbRelease(pSdb, pVgroup);
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
STransAction action = {0};
action.epSet = mndGetVgroupEpset(pMnode, pVgroup);
action.pCont = pReq;
action.contLen = contLen;
action.msgType = TDMT_VND_DROP_STB;
2022-10-31 11:57:27 +00:00
action.acceptableCode = TSDB_CODE_TDB_STB_NOT_EXIST;
2022-01-28 01:29:17 +00:00
if (mndTransAppendRedoAction(pTrans, &action) != 0) {
2022-03-25 16:29:53 +00:00
taosMemoryFree(pReq);
2022-01-28 01:29:17 +00:00
sdbCancelFetch(pSdb, pIter);
sdbRelease(pSdb, pVgroup);
return -1;
}
sdbRelease(pSdb, pVgroup);
}
return 0;
}
2022-05-16 06:55:31 +00:00
static int32_t mndDropStb(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SStbObj *pStb) {
2021-12-21 06:07:27 +00:00
int32_t code = -1;
2022-09-22 08:18:51 +00:00
STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB_INSIDE, pReq, "drop-stb");
if (pTrans == NULL) goto _OVER;
2021-12-12 02:46:49 +00:00
2022-09-23 07:42:36 +00:00
mInfo("trans:%d, used to drop stb:%s", pTrans->id, pStb->name);
2022-07-25 14:14:10 +00:00
mndTransSetDbName(pTrans, pDb->name, pStb->name);
2021-12-12 02:46:49 +00:00
if (mndSetDropStbRedoLogs(pMnode, pTrans, pStb) != 0) goto _OVER;
if (mndSetDropStbCommitLogs(pMnode, pTrans, pStb) != 0) goto _OVER;
if (mndSetDropStbRedoActions(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
2022-06-14 07:46:16 +00:00
if (mndDropSmasByStb(pMnode, pTrans, pDb, pStb) != 0) goto _OVER;
if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER;
2021-12-12 02:46:49 +00:00
2021-12-21 06:07:27 +00:00
code = 0;
_OVER:
2021-12-12 02:46:49 +00:00
mndTransDrop(pTrans);
2022-01-24 06:18:36 +00:00
return code;
2021-12-12 02:46:49 +00:00
}
static int32_t mndCheckDropStbForTopic(SMnode *pMnode, const char *stbFullName, int64_t suid) {
SSdb *pSdb = pMnode->pSdb;
void *pIter = NULL;
while (1) {
SMqTopicObj *pTopic = NULL;
pIter = sdbFetch(pSdb, SDB_TOPIC, pIter, (void **)&pTopic);
if (pIter == NULL) break;
if (pTopic->subType == TOPIC_SUB_TYPE__TABLE) {
if (pTopic->stbUid == suid) {
sdbRelease(pSdb, pTopic);
return -1;
}
}
if (pTopic->subType != TOPIC_SUB_TYPE__COLUMN) {
sdbRelease(pSdb, pTopic);
continue;
}
SNode *pAst = NULL;
if (nodesStringToNode(pTopic->ast, &pAst) != 0) {
ASSERT(0);
return -1;
}
SNodeList *pNodeList = NULL;
nodesCollectColumns((SSelectStmt *)pAst, SQL_CLAUSE_FROM, NULL, COLLECT_COL_TYPE_ALL, &pNodeList);
SNode *pNode = NULL;
FOREACH(pNode, pNodeList) {
SColumnNode *pCol = (SColumnNode *)pNode;
2022-08-17 11:19:58 +00:00
if (pCol->tableId == suid) {
sdbRelease(pSdb, pTopic);
nodesDestroyNode(pAst);
2022-09-16 06:36:01 +00:00
nodesDestroyList(pNodeList);
return -1;
} else {
goto NEXT;
}
}
NEXT:
sdbRelease(pSdb, pTopic);
nodesDestroyNode(pAst);
2022-09-16 06:36:01 +00:00
nodesDestroyList(pNodeList);
}
return 0;
}
static int32_t mndCheckDropStbForStream(SMnode *pMnode, const char *stbFullName, int64_t suid) {
SSdb *pSdb = pMnode->pSdb;
void *pIter = NULL;
while (1) {
SStreamObj *pStream = NULL;
pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
if (pIter == NULL) break;
2022-08-17 11:44:53 +00:00
if (pStream->smaId != 0) {
sdbRelease(pSdb, pStream);
continue;
}
if (pStream->targetStbUid == suid) {
2022-08-17 11:19:58 +00:00
sdbRelease(pSdb, pStream);
return -1;
}
SNode *pAst = NULL;
if (nodesStringToNode(pStream->ast, &pAst) != 0) {
ASSERT(0);
return -1;
}
SNodeList *pNodeList = NULL;
nodesCollectColumns((SSelectStmt *)pAst, SQL_CLAUSE_FROM, NULL, COLLECT_COL_TYPE_ALL, &pNodeList);
SNode *pNode = NULL;
FOREACH(pNode, pNodeList) {
SColumnNode *pCol = (SColumnNode *)pNode;
2022-08-17 11:19:58 +00:00
if (pCol->tableId == suid) {
sdbRelease(pSdb, pStream);
nodesDestroyNode(pAst);
2022-09-16 06:36:01 +00:00
nodesDestroyList(pNodeList);
return -1;
} else {
goto NEXT;
}
}
NEXT:
sdbRelease(pSdb, pStream);
nodesDestroyNode(pAst);
2022-09-16 06:36:01 +00:00
nodesDestroyList(pNodeList);
}
return 0;
}
2022-06-14 07:46:16 +00:00
static int32_t mndProcessDropStbReq(SRpcMsg *pReq) {
2022-05-16 06:55:31 +00:00
SMnode *pMnode = pReq->info.node;
2022-02-15 09:24:34 +00:00
int32_t code = -1;
SDbObj *pDb = NULL;
SStbObj *pStb = NULL;
2022-02-09 11:41:14 +00:00
SMDropStbReq dropReq = {0};
2022-02-15 09:24:34 +00:00
2022-05-16 06:55:31 +00:00
if (tDeserializeSMDropStbReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
2022-02-15 09:24:34 +00:00
terrno = TSDB_CODE_INVALID_MSG;
goto _OVER;
2022-02-15 09:24:34 +00:00
}
2021-12-12 02:46:49 +00:00
2022-09-23 07:42:36 +00:00
mInfo("stb:%s, start to drop", dropReq.name);
2021-12-12 02:46:49 +00:00
2022-02-15 09:24:34 +00:00
pStb = mndAcquireStb(pMnode, dropReq.name);
2021-12-12 02:46:49 +00:00
if (pStb == NULL) {
2022-02-09 11:41:14 +00:00
if (dropReq.igNotExists) {
2022-09-23 07:42:36 +00:00
mInfo("stb:%s, not exist, ignore not exist is set", dropReq.name);
2022-02-15 09:24:34 +00:00
code = 0;
goto _OVER;
2021-12-12 02:46:49 +00:00
} else {
terrno = TSDB_CODE_MND_STB_NOT_EXIST;
goto _OVER;
2021-12-12 02:46:49 +00:00
}
}
2022-07-13 07:33:31 +00:00
if (dropReq.source == TD_REQ_FROM_TAOX && pStb->uid != dropReq.suid) {
code = 0;
2022-07-08 10:37:47 +00:00
goto _OVER;
}
2022-02-15 09:24:34 +00:00
pDb = mndAcquireDbByStb(pMnode, dropReq.name);
2022-01-28 01:29:17 +00:00
if (pDb == NULL) {
terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
goto _OVER;
2022-01-28 01:29:17 +00:00
}
2022-06-25 01:09:33 +00:00
if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) {
goto _OVER;
2022-02-15 09:24:34 +00:00
}
if (mndCheckDropStbForTopic(pMnode, dropReq.name, pStb->uid) < 0) {
terrno = TSDB_CODE_MND_TOPIC_MUST_BE_DELETED;
goto _OVER;
}
if (mndCheckDropStbForStream(pMnode, dropReq.name, pStb->uid) < 0) {
terrno = TSDB_CODE_MND_STREAM_MUST_BE_DELETED;
goto _OVER;
}
2022-02-15 09:24:34 +00:00
code = mndDropStb(pMnode, pReq, pDb, pStb);
2022-05-21 08:35:24 +00:00
if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
2022-02-15 09:24:34 +00:00
_OVER:
2022-05-21 08:35:24 +00:00
if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
2022-02-09 11:41:14 +00:00
mError("stb:%s, failed to drop since %s", dropReq.name, terrstr());
2021-12-12 02:46:49 +00:00
}
2022-02-15 09:24:34 +00:00
mndReleaseDb(pMnode, pDb);
mndReleaseStb(pMnode, pStb);
return code;
2021-12-12 02:46:49 +00:00
}
2021-12-10 07:20:04 +00:00
2022-05-16 06:55:31 +00:00
static int32_t mndProcessTableMetaReq(SRpcMsg *pReq) {
SMnode *pMnode = pReq->info.node;
2022-02-15 07:24:27 +00:00
int32_t code = -1;
STableInfoReq infoReq = {0};
STableMetaRsp metaRsp = {0};
2022-02-06 07:37:16 +00:00
2022-08-24 09:36:10 +00:00
SUserObj *pUser = mndAcquireUser(pMnode, pReq->info.conn.user);
if (pUser == NULL) return 0;
bool sysinfo = pUser->sysInfo;
2022-05-16 06:55:31 +00:00
if (tDeserializeSTableInfoReq(pReq->pCont, pReq->contLen, &infoReq) != 0) {
2022-02-15 07:24:27 +00:00
terrno = TSDB_CODE_INVALID_MSG;
goto _OVER;
2022-02-15 07:24:27 +00:00
}
2022-02-06 07:37:16 +00:00
2022-02-24 11:45:05 +00:00
if (0 == strcmp(infoReq.dbFName, TSDB_INFORMATION_SCHEMA_DB)) {
2022-09-23 07:42:36 +00:00
mInfo("information_schema table:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
2022-08-24 09:36:10 +00:00
if (mndBuildInsTableSchema(pMnode, infoReq.dbFName, infoReq.tbName, sysinfo, &metaRsp) != 0) {
goto _OVER;
2022-02-24 11:45:05 +00:00
}
2022-04-13 02:48:26 +00:00
} else if (0 == strcmp(infoReq.dbFName, TSDB_PERFORMANCE_SCHEMA_DB)) {
2022-09-23 07:42:36 +00:00
mInfo("performance_schema table:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
2022-04-13 02:48:26 +00:00
if (mndBuildPerfsTableSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp) != 0) {
goto _OVER;
2022-04-13 02:48:26 +00:00
}
2022-02-24 11:45:05 +00:00
} else {
2022-09-23 07:42:36 +00:00
mInfo("stb:%s.%s, start to retrieve meta", infoReq.dbFName, infoReq.tbName);
2022-06-13 07:36:28 +00:00
if (mndBuildStbSchema(pMnode, infoReq.dbFName, infoReq.tbName, &metaRsp, NULL) != 0) {
goto _OVER;
2022-02-24 11:45:05 +00:00
}
2022-02-15 07:24:27 +00:00
}
2022-02-08 05:57:32 +00:00
2022-02-15 07:24:27 +00:00
int32_t rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
if (rspLen < 0) {
terrno = TSDB_CODE_INVALID_MSG;
goto _OVER;
2022-02-15 07:24:27 +00:00
}
2022-02-08 05:57:32 +00:00
2022-02-15 08:46:21 +00:00
void *pRsp = rpcMallocCont(rspLen);
2022-02-15 07:24:27 +00:00
if (pRsp == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
goto _OVER;
2022-02-15 07:24:27 +00:00
}
2022-02-06 07:37:16 +00:00
2022-02-15 07:24:27 +00:00
tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
2022-05-16 06:55:31 +00:00
pReq->info.rsp = pRsp;
pReq->info.rspLen = rspLen;
2022-02-15 07:24:27 +00:00
code = 0;
2022-02-08 05:57:32 +00:00
mTrace("%s.%s, meta is retrieved", infoReq.dbFName, infoReq.tbName);
2022-02-06 07:37:16 +00:00
_OVER:
2022-02-15 07:24:27 +00:00
if (code != 0) {
mError("stb:%s.%s, failed to retrieve meta since %s", infoReq.dbFName, infoReq.tbName, terrstr());
}
2022-02-08 05:57:32 +00:00
2022-08-24 09:36:10 +00:00
mndReleaseUser(pMnode, pUser);
2022-02-15 07:24:27 +00:00
tFreeSTableMetaRsp(&metaRsp);
return code;
}
2022-02-08 05:57:32 +00:00
2022-06-20 12:58:36 +00:00
static int32_t mndProcessTableCfgReq(SRpcMsg *pReq) {
2022-06-25 10:03:12 +00:00
SMnode *pMnode = pReq->info.node;
int32_t code = -1;
STableCfgReq cfgReq = {0};
STableCfgRsp cfgRsp = {0};
2022-06-20 12:58:36 +00:00
if (tDeserializeSTableCfgReq(pReq->pCont, pReq->contLen, &cfgReq) != 0) {
terrno = TSDB_CODE_INVALID_MSG;
goto _OVER;
}
if (0 == strcmp(cfgReq.dbFName, TSDB_INFORMATION_SCHEMA_DB)) {
2022-09-23 07:42:36 +00:00
mInfo("information_schema table:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
2022-06-20 12:58:36 +00:00
if (mndBuildInsTableCfg(pMnode, cfgReq.dbFName, cfgReq.tbName, &cfgRsp) != 0) {
goto _OVER;
}
} else if (0 == strcmp(cfgReq.dbFName, TSDB_PERFORMANCE_SCHEMA_DB)) {
2022-09-23 07:42:36 +00:00
mInfo("performance_schema table:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
2022-06-20 12:58:36 +00:00
if (mndBuildPerfsTableCfg(pMnode, cfgReq.dbFName, cfgReq.tbName, &cfgRsp) != 0) {
goto _OVER;
}
} else {
2022-09-23 07:42:36 +00:00
mInfo("stb:%s.%s, start to retrieve cfg", cfgReq.dbFName, cfgReq.tbName);
2022-06-20 12:58:36 +00:00
if (mndBuildStbCfg(pMnode, cfgReq.dbFName, cfgReq.tbName, &cfgRsp) != 0) {
goto _OVER;
}
}
int32_t rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp);
if (rspLen < 0) {
terrno = TSDB_CODE_INVALID_MSG;
goto _OVER;
}
void *pRsp = rpcMallocCont(rspLen);
if (pRsp == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
goto _OVER;
}
tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp);
pReq->info.rsp = pRsp;
pReq->info.rspLen = rspLen;
code = 0;
mTrace("%s.%s, cfg is retrieved", cfgReq.dbFName, cfgReq.tbName);
_OVER:
if (code != 0) {
mError("stb:%s.%s, failed to retrieve cfg since %s", cfgReq.dbFName, cfgReq.tbName, terrstr());
}
tFreeSTableCfgRsp(&cfgRsp);
return code;
}
2022-06-13 02:38:45 +00:00
int32_t mndValidateStbInfo(SMnode *pMnode, SSTableVersion *pStbVersions, int32_t numOfStbs, void **ppRsp,
2022-02-15 07:24:27 +00:00
int32_t *pRspLen) {
2022-06-13 02:38:45 +00:00
SSTbHbRsp hbRsp = {0};
2022-06-13 06:06:11 +00:00
hbRsp.pMetaRsp = taosArrayInit(numOfStbs, sizeof(STableMetaRsp));
if (hbRsp.pMetaRsp == NULL) {
2022-02-15 07:24:27 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
2022-02-08 05:57:32 +00:00
2022-06-13 06:06:11 +00:00
hbRsp.pIndexRsp = taosArrayInit(numOfStbs, sizeof(STableIndexRsp));
2022-06-13 02:38:45 +00:00
if (NULL == hbRsp.pIndexRsp) {
2022-06-13 06:06:11 +00:00
taosArrayDestroy(hbRsp.pMetaRsp);
2022-06-13 02:38:45 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
2022-06-16 08:01:22 +00:00
2022-02-15 07:24:27 +00:00
for (int32_t i = 0; i < numOfStbs; ++i) {
2022-06-13 02:38:45 +00:00
SSTableVersion *pStbVersion = &pStbVersions[i];
2022-02-15 07:24:27 +00:00
pStbVersion->suid = be64toh(pStbVersion->suid);
pStbVersion->sversion = ntohs(pStbVersion->sversion);
pStbVersion->tversion = ntohs(pStbVersion->tversion);
2022-06-13 02:38:45 +00:00
pStbVersion->smaVer = ntohl(pStbVersion->smaVer);
2022-02-08 05:57:32 +00:00
2022-02-15 07:24:27 +00:00
STableMetaRsp metaRsp = {0};
2022-06-16 08:01:22 +00:00
int32_t smaVer = 0;
2022-09-23 07:42:36 +00:00
mInfo("stb:%s.%s, start to retrieve meta", pStbVersion->dbFName, pStbVersion->stbName);
2022-06-13 02:38:45 +00:00
if (mndBuildStbSchema(pMnode, pStbVersion->dbFName, pStbVersion->stbName, &metaRsp, &smaVer) != 0) {
2022-02-15 07:24:27 +00:00
metaRsp.numOfColumns = -1;
metaRsp.suid = pStbVersion->suid;
2022-06-13 06:06:11 +00:00
taosArrayPush(hbRsp.pMetaRsp, &metaRsp);
2022-06-13 02:38:45 +00:00
continue;
2022-02-06 07:37:16 +00:00
}
2022-02-08 05:57:32 +00:00
2022-05-26 12:37:55 +00:00
if (pStbVersion->sversion != metaRsp.sversion || pStbVersion->tversion != metaRsp.tversion) {
2022-06-13 06:06:11 +00:00
taosArrayPush(hbRsp.pMetaRsp, &metaRsp);
2022-05-21 13:59:04 +00:00
} else {
tFreeSTableMetaRsp(&metaRsp);
2022-02-08 05:57:32 +00:00
}
2022-06-13 02:38:45 +00:00
2022-06-13 07:36:28 +00:00
if (pStbVersion->smaVer && pStbVersion->smaVer != smaVer) {
2022-06-16 08:01:22 +00:00
bool exist = false;
char tbFName[TSDB_TABLE_FNAME_LEN];
2022-06-13 06:06:11 +00:00
STableIndexRsp indexRsp = {0};
2022-06-13 11:47:14 +00:00
indexRsp.pIndex = taosArrayInit(10, sizeof(STableIndexInfo));
if (NULL == indexRsp.pIndex) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
2022-06-16 08:01:22 +00:00
2022-06-13 02:38:45 +00:00
sprintf(tbFName, "%s.%s", pStbVersion->dbFName, pStbVersion->stbName);
2022-06-13 06:06:11 +00:00
int32_t code = mndGetTableSma(pMnode, tbFName, &indexRsp, &exist);
2022-06-13 02:38:45 +00:00
if (code || !exist) {
2022-06-13 06:06:11 +00:00
indexRsp.suid = pStbVersion->suid;
indexRsp.version = -1;
indexRsp.pIndex = NULL;
2022-06-13 02:38:45 +00:00
}
2022-06-13 06:06:11 +00:00
2022-06-13 07:36:28 +00:00
strcpy(indexRsp.dbFName, pStbVersion->dbFName);
strcpy(indexRsp.tbName, pStbVersion->stbName);
2022-06-13 06:06:11 +00:00
taosArrayPush(hbRsp.pIndexRsp, &indexRsp);
2022-06-13 02:38:45 +00:00
}
2022-02-15 07:24:27 +00:00
}
2022-02-08 05:57:32 +00:00
2022-06-13 02:38:45 +00:00
int32_t rspLen = tSerializeSSTbHbRsp(NULL, 0, &hbRsp);
2022-02-15 07:24:27 +00:00
if (rspLen < 0) {
2022-06-13 02:38:45 +00:00
tFreeSSTbHbRsp(&hbRsp);
2022-02-15 07:24:27 +00:00
terrno = TSDB_CODE_INVALID_MSG;
return -1;
2022-02-06 07:37:16 +00:00
}
2022-03-25 16:29:53 +00:00
void *pRsp = taosMemoryMalloc(rspLen);
2022-02-15 07:24:27 +00:00
if (pRsp == NULL) {
2022-06-13 02:38:45 +00:00
tFreeSSTbHbRsp(&hbRsp);
2022-02-15 07:24:27 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
2022-02-06 07:37:16 +00:00
}
2022-06-13 02:38:45 +00:00
tSerializeSSTbHbRsp(pRsp, rspLen, &hbRsp);
tFreeSSTbHbRsp(&hbRsp);
2022-02-15 07:24:27 +00:00
*ppRsp = pRsp;
*pRspLen = rspLen;
2022-02-06 07:37:16 +00:00
return 0;
}
2022-04-28 08:31:19 +00:00
int32_t mndGetNumOfStbs(SMnode *pMnode, char *dbName, int32_t *pNumOfStbs) {
2022-02-16 12:18:18 +00:00
SSdb *pSdb = pMnode->pSdb;
2021-12-09 10:53:09 +00:00
SDbObj *pDb = mndAcquireDb(pMnode, dbName);
if (pDb == NULL) {
terrno = TSDB_CODE_MND_DB_NOT_SELECTED;
return -1;
}
2021-12-10 07:20:04 +00:00
int32_t numOfStbs = 0;
2021-12-31 06:22:50 +00:00
void *pIter = NULL;
2021-12-09 10:53:09 +00:00
while (1) {
2021-12-10 07:20:04 +00:00
SStbObj *pStb = NULL;
2021-12-17 04:00:54 +00:00
pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
2021-12-09 10:53:09 +00:00
if (pIter == NULL) break;
2022-02-16 12:18:18 +00:00
if (pStb->dbUid == pDb->uid) {
2021-12-10 07:20:04 +00:00
numOfStbs++;
2021-12-09 10:53:09 +00:00
}
2021-12-10 06:15:19 +00:00
sdbRelease(pSdb, pStb);
2021-12-09 10:53:09 +00:00
}
2021-12-10 07:20:04 +00:00
*pNumOfStbs = numOfStbs;
2022-02-16 12:18:18 +00:00
mndReleaseDb(pMnode, pDb);
2021-12-09 10:53:09 +00:00
return 0;
}
2022-06-16 08:01:22 +00:00
void mndExtractDbNameFromStbFullName(const char *stbFullName, char *dst) {
SName name = {0};
tNameFromString(&name, stbFullName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
tNameGetFullDbName(&name, dst);
}
void mndExtractTbNameFromStbFullName(const char *stbFullName, char *dst, int32_t dstSize) {
2021-12-25 05:27:37 +00:00
int32_t pos = -1;
int32_t num = 0;
2022-06-16 08:01:22 +00:00
for (pos = 0; stbFullName[pos] != 0; ++pos) {
if (stbFullName[pos] == TS_PATH_DELIMITER[0]) num++;
2021-12-09 10:53:09 +00:00
if (num == 2) break;
}
if (num == 2) {
2022-06-16 08:01:22 +00:00
tstrncpy(dst, stbFullName + pos + 1, dstSize);
2021-12-09 10:53:09 +00:00
}
}
2022-05-16 06:55:31 +00:00
static int32_t mndRetrieveStb(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
SMnode *pMnode = pReq->info.node;
2021-12-27 07:15:03 +00:00
SSdb *pSdb = pMnode->pSdb;
2021-12-10 07:20:04 +00:00
int32_t numOfRows = 0;
SStbObj *pStb = NULL;
int32_t cols = 0;
2021-12-09 10:53:09 +00:00
2022-04-18 13:23:22 +00:00
SDbObj *pDb = NULL;
2022-03-16 09:52:44 +00:00
if (strlen(pShow->db) > 0) {
pDb = mndAcquireDb(pMnode, pShow->db);
2022-03-26 07:03:22 +00:00
if (pDb == NULL) return terrno;
2022-03-16 09:52:44 +00:00
}
2021-12-27 07:15:03 +00:00
2021-12-09 10:53:09 +00:00
while (numOfRows < rows) {
2021-12-10 07:20:04 +00:00
pShow->pIter = sdbFetch(pSdb, SDB_STB, pShow->pIter, (void **)&pStb);
2021-12-09 10:53:09 +00:00
if (pShow->pIter == NULL) break;
2022-03-16 09:52:44 +00:00
if (pDb != NULL && pStb->dbUid != pDb->uid) {
2021-12-10 06:15:19 +00:00
sdbRelease(pSdb, pStb);
2021-12-09 10:53:09 +00:00
continue;
}
cols = 0;
2022-03-16 09:52:44 +00:00
SName name = {0};
2022-04-18 13:23:22 +00:00
char stbName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
2022-06-16 08:01:22 +00:00
mndExtractTbNameFromStbFullName(pStb->name, &stbName[VARSTR_HEADER_SIZE], TSDB_TABLE_NAME_LEN);
varDataSetLen(stbName, strlen(&stbName[VARSTR_HEADER_SIZE]));
2021-12-09 10:53:09 +00:00
2022-04-18 13:23:22 +00:00
SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataAppend(pColInfo, numOfRows, (const char *)stbName, false);
2022-04-18 13:23:22 +00:00
char db[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
tNameFromString(&name, pStb->db, T_NAME_ACCT | T_NAME_DB);
tNameGetDbName(&name, varDataVal(db));
varDataSetLen(db, strlen(varDataVal(db)));
2022-04-14 03:36:06 +00:00
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2022-04-18 13:23:22 +00:00
colDataAppend(pColInfo, numOfRows, (const char *)db, false);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataAppend(pColInfo, numOfRows, (const char *)&pStb->createdTime, false);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataAppend(pColInfo, numOfRows, (const char *)&pStb->numOfColumns, false);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataAppend(pColInfo, numOfRows, (const char *)&pStb->numOfTags, false);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataAppend(pColInfo, numOfRows, (const char *)&pStb->updateTime, false); // number of tables
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2022-06-16 12:45:00 +00:00
if (pStb->commentLen > 0) {
char comment[TSDB_TB_COMMENT_LEN + VARSTR_HEADER_SIZE] = {0};
STR_TO_VARSTR(comment, pStb->comment);
colDataAppend(pColInfo, numOfRows, comment, false);
} else if (pStb->commentLen == 0) {
2022-06-16 12:45:00 +00:00
char comment[VARSTR_HEADER_SIZE + VARSTR_HEADER_SIZE] = {0};
STR_TO_VARSTR(comment, "");
colDataAppend(pColInfo, numOfRows, comment, false);
} else {
colDataAppendNULL(pColInfo, numOfRows);
2022-03-23 12:22:22 +00:00
}
2022-03-16 09:52:44 +00:00
char watermark[64 + VARSTR_HEADER_SIZE] = {0};
sprintf(varDataVal(watermark), "%" PRId64 "a,%" PRId64 "a", pStb->watermark[0], pStb->watermark[1]);
varDataSetLen(watermark, strlen(varDataVal(watermark)));
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataAppend(pColInfo, numOfRows, (const char *)watermark, false);
char maxDelay[64 + VARSTR_HEADER_SIZE] = {0};
sprintf(varDataVal(maxDelay), "%" PRId64 "a,%" PRId64 "a", pStb->maxdelay[0], pStb->maxdelay[1]);
varDataSetLen(maxDelay, strlen(varDataVal(maxDelay)));
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataAppend(pColInfo, numOfRows, (const char *)maxDelay, false);
2022-09-30 06:03:46 +00:00
char rollup[160 + VARSTR_HEADER_SIZE] = {0};
int32_t rollupNum = (int32_t)taosArrayGetSize(pStb->pFuncs);
2022-10-18 08:33:27 +00:00
char *sep = ", ";
int32_t sepLen = strlen(sep);
2022-10-19 01:15:04 +00:00
int32_t rollupLen = sizeof(rollup) - VARSTR_HEADER_SIZE - 2;
for (int32_t i = 0; i < rollupNum; ++i) {
char *funcName = taosArrayGet(pStb->pFuncs, i);
if (i) {
2022-10-18 08:33:27 +00:00
strncat(varDataVal(rollup), sep, rollupLen);
rollupLen -= sepLen;
}
2022-10-18 08:33:27 +00:00
strncat(varDataVal(rollup), funcName, rollupLen);
rollupLen -= strlen(funcName);
}
varDataSetLen(rollup, strlen(varDataVal(rollup)));
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataAppend(pColInfo, numOfRows, (const char *)rollup, false);
2021-12-09 10:53:09 +00:00
numOfRows++;
2021-12-10 06:15:19 +00:00
sdbRelease(pSdb, pStb);
2021-12-09 10:53:09 +00:00
}
2022-03-16 10:24:11 +00:00
if (pDb != NULL) {
mndReleaseDb(pMnode, pDb);
}
pShow->numOfRows += numOfRows;
2021-12-09 10:53:09 +00:00
return numOfRows;
}
2021-12-10 07:20:04 +00:00
static void mndCancelGetNextStb(SMnode *pMnode, void *pIter) {
2021-12-09 10:53:09 +00:00
SSdb *pSdb = pMnode->pSdb;
sdbCancelFetch(pSdb, pIter);
2022-02-24 11:45:05 +00:00
}
2022-09-26 08:18:48 +00:00
const char *mndGetStbStr(const char *src) {
char *posDb = strstr(src, TS_PATH_DELIMITER);
if (posDb != NULL) ++posDb;
if (posDb == NULL) return src;
char *posStb = strstr(posDb, TS_PATH_DELIMITER);
if (posStb != NULL) ++posStb;
if (posStb == NULL) return posDb;
return posStb;
2022-11-04 02:47:49 +00:00
}