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

347 lines
12 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/>.
*/
2021-10-16 07:16:05 +00:00
#define _DEFAULT_SOURCE
2021-12-02 11:56:43 +00:00
#include "mndShow.h"
2022-06-25 01:09:33 +00:00
#include "mndPrivilege.h"
#include "systable.h"
2021-10-17 03:42:05 +00:00
2021-12-13 06:48:10 +00:00
#define SHOW_STEP_SIZE 100
2022-04-20 12:41:53 +00:00
static SShowObj *mndCreateShowObj(SMnode *pMnode, SRetrieveTableReq *pReq);
2021-12-06 06:38:37 +00:00
static void mndFreeShowObj(SShowObj *pShow);
static SShowObj *mndAcquireShowObj(SMnode *pMnode, int64_t showId);
2021-12-06 06:38:37 +00:00
static void mndReleaseShowObj(SShowObj *pShow, bool forceRemove);
2021-12-06 07:06:29 +00:00
static bool mndCheckRetrieveFinished(SShowObj *pShow);
2022-05-16 06:55:31 +00:00
static int32_t mndProcessRetrieveSysTableReq(SRpcMsg *pReq);
2021-12-02 11:56:43 +00:00
int32_t mndInitShow(SMnode *pMnode) {
SShowMgmt *pMgmt = &pMnode->showMgmt;
2022-04-22 11:47:00 +00:00
pMgmt->cache = taosCacheInit(TSDB_DATA_TYPE_INT, 5000, true, (__cache_free_fn_t)mndFreeShowObj, "show");
2021-12-02 11:56:43 +00:00
if (pMgmt->cache == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
mError("failed to alloc show cache since %s", terrstr());
return -1;
}
2022-03-09 06:58:15 +00:00
mndSetMsgHandle(pMnode, TDMT_MND_SYSTABLE_RETRIEVE, mndProcessRetrieveSysTableReq);
2021-12-02 11:56:43 +00:00
return 0;
}
2021-12-03 09:22:44 +00:00
void mndCleanupShow(SMnode *pMnode) {
SShowMgmt *pMgmt = &pMnode->showMgmt;
if (pMgmt->cache != NULL) {
taosCacheCleanup(pMgmt->cache);
pMgmt->cache = NULL;
}
}
2022-04-28 07:53:12 +00:00
static int32_t convertToRetrieveType(char *name, int32_t len) {
2022-04-20 12:41:53 +00:00
int32_t type = -1;
if (strncasecmp(name, TSDB_INS_TABLE_DNODES, len) == 0) {
type = TSDB_MGMT_TABLE_DNODE;
} else if (strncasecmp(name, TSDB_INS_TABLE_MNODES, len) == 0) {
type = TSDB_MGMT_TABLE_MNODE;
} else if (strncasecmp(name, TSDB_INS_TABLE_MODULES, len) == 0) {
type = TSDB_MGMT_TABLE_MODULE;
} else if (strncasecmp(name, TSDB_INS_TABLE_QNODES, len) == 0) {
type = TSDB_MGMT_TABLE_QNODE;
} else if (strncasecmp(name, TSDB_INS_TABLE_SNODES, len) == 0) {
type = TSDB_MGMT_TABLE_SNODE;
} else if (strncasecmp(name, TSDB_INS_TABLE_CLUSTER, len) == 0) {
type = TSDB_MGMT_TABLE_CLUSTER;
2022-08-01 12:47:08 +00:00
} else if (strncasecmp(name, TSDB_INS_TABLE_DATABASES, len) == 0) {
2022-04-20 12:41:53 +00:00
type = TSDB_MGMT_TABLE_DB;
2022-08-01 12:47:08 +00:00
} else if (strncasecmp(name, TSDB_INS_TABLE_FUNCTIONS, len) == 0) {
2022-04-20 12:41:53 +00:00
type = TSDB_MGMT_TABLE_FUNC;
2022-08-01 12:47:08 +00:00
} else if (strncasecmp(name, TSDB_INS_TABLE_INDEXES, len) == 0) {
2022-07-02 10:34:00 +00:00
type = TSDB_MGMT_TABLE_INDEX;
2022-08-01 12:47:08 +00:00
} else if (strncasecmp(name, TSDB_INS_TABLE_STABLES, len) == 0) {
2022-04-20 12:41:53 +00:00
type = TSDB_MGMT_TABLE_STB;
2022-08-01 12:47:08 +00:00
} else if (strncasecmp(name, TSDB_INS_TABLE_TABLES, len) == 0) {
2022-04-20 12:41:53 +00:00
type = TSDB_MGMT_TABLE_TABLE;
2022-08-01 12:47:08 +00:00
} else if (strncasecmp(name, TSDB_INS_TABLE_TAGS, len) == 0) {
2022-07-16 08:03:45 +00:00
type = TSDB_MGMT_TABLE_TAG;
2022-08-01 12:47:08 +00:00
} else if (strncasecmp(name, TSDB_INS_TABLE_TABLE_DISTRIBUTED, len) == 0) {
2022-04-20 12:41:53 +00:00
// type = TSDB_MGMT_TABLE_DIST;
2022-08-01 12:47:08 +00:00
} else if (strncasecmp(name, TSDB_INS_TABLE_USERS, len) == 0) {
2022-04-20 12:41:53 +00:00
type = TSDB_MGMT_TABLE_USER;
} else if (strncasecmp(name, TSDB_INS_TABLE_LICENCES, len) == 0) {
type = TSDB_MGMT_TABLE_GRANTS;
} else if (strncasecmp(name, TSDB_INS_TABLE_VGROUPS, len) == 0) {
type = TSDB_MGMT_TABLE_VGROUP;
} else if (strncasecmp(name, TSDB_PERFS_TABLE_CONSUMERS, len) == 0) {
2022-04-20 12:41:53 +00:00
type = TSDB_MGMT_TABLE_CONSUMERS;
2022-08-30 05:49:35 +00:00
} else if (strncasecmp(name, TSDB_INS_TABLE_SUBSCRIPTIONS, len) == 0) {
2022-05-09 10:09:55 +00:00
type = TSDB_MGMT_TABLE_SUBSCRIPTIONS;
} else if (strncasecmp(name, TSDB_PERFS_TABLE_TRANS, len) == 0) {
2022-04-20 12:41:53 +00:00
type = TSDB_MGMT_TABLE_TRANS;
} else if (strncasecmp(name, TSDB_PERFS_TABLE_SMAS, len) == 0) {
2022-04-20 12:41:53 +00:00
type = TSDB_MGMT_TABLE_SMAS;
} else if (strncasecmp(name, TSDB_INS_TABLE_CONFIGS, len) == 0) {
type = TSDB_MGMT_TABLE_CONFIGS;
} else if (strncasecmp(name, TSDB_PERFS_TABLE_CONNECTIONS, len) == 0) {
2022-04-20 12:41:53 +00:00
type = TSDB_MGMT_TABLE_CONNS;
} else if (strncasecmp(name, TSDB_PERFS_TABLE_QUERIES, len) == 0) {
2022-04-20 12:41:53 +00:00
type = TSDB_MGMT_TABLE_QUERIES;
2022-04-28 07:53:12 +00:00
} else if (strncasecmp(name, TSDB_INS_TABLE_VNODES, len) == 0) {
2022-04-20 12:41:53 +00:00
type = TSDB_MGMT_TABLE_VNODES;
2022-08-30 05:49:35 +00:00
} else if (strncasecmp(name, TSDB_INS_TABLE_TOPICS, len) == 0) {
type = TSDB_MGMT_TABLE_TOPICS;
2022-08-30 05:49:35 +00:00
} else if (strncasecmp(name, TSDB_INS_TABLE_STREAMS, len) == 0) {
2022-04-28 07:53:12 +00:00
type = TSDB_MGMT_TABLE_STREAMS;
2022-06-15 12:59:33 +00:00
} else if (strncasecmp(name, TSDB_PERFS_TABLE_APPS, len) == 0) {
type = TSDB_MGMT_TABLE_APPS;
} else if (strncasecmp(name, TSDB_INS_TABLE_STREAM_TASKS, len) == 0) {
type = TSDB_MGMT_TABLE_STREAM_TASKS;
} else if (strncasecmp(name, TSDB_INS_TABLE_USER_PRIVILEGES, len) == 0) {
type = TSDB_MGMT_TABLE_PRIVILEGES;
2022-04-20 12:41:53 +00:00
} else {
2023-01-03 07:33:50 +00:00
mError("invalid show name:%s len:%d", name, len);
2022-04-20 12:41:53 +00:00
}
return type;
}
static SShowObj *mndCreateShowObj(SMnode *pMnode, SRetrieveTableReq *pReq) {
2021-12-03 09:22:44 +00:00
SShowMgmt *pMgmt = &pMnode->showMgmt;
int64_t showId = atomic_add_fetch_64(&pMgmt->showId, 1);
if (showId == 0) atomic_add_fetch_64(&pMgmt->showId, 1);
2021-12-03 09:22:44 +00:00
2022-04-28 07:53:12 +00:00
int32_t size = sizeof(SShowObj);
2022-04-20 12:41:53 +00:00
2021-12-29 06:35:23 +00:00
SShowObj showObj = {0};
2022-04-28 07:53:12 +00:00
showObj.id = showId;
2021-12-29 06:35:23 +00:00
showObj.pMnode = pMnode;
2022-04-28 07:53:12 +00:00
showObj.type = convertToRetrieveType(pReq->tb, tListLen(pReq->tb));
2022-01-05 12:18:56 +00:00
memcpy(showObj.db, pReq->db, TSDB_DB_FNAME_LEN);
2021-12-03 09:22:44 +00:00
2022-02-24 12:17:48 +00:00
int32_t keepTime = tsShellActivityTimer * 6 * 1000;
SShowObj *pShow = taosCachePut(pMgmt->cache, &showId, sizeof(int64_t), &showObj, size, keepTime);
2021-12-29 06:35:23 +00:00
if (pShow == NULL) {
2021-12-06 06:38:37 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
2021-12-30 12:07:32 +00:00
mError("show:0x%" PRIx64 ", failed to put into cache since %s", showId, terrstr());
2021-12-06 06:38:37 +00:00
return NULL;
}
2021-12-29 06:35:23 +00:00
2021-12-30 12:07:32 +00:00
mTrace("show:0x%" PRIx64 ", is created, data:%p", showId, pShow);
2021-12-29 06:35:23 +00:00
return pShow;
2021-12-03 09:22:44 +00:00
}
2021-12-06 06:38:37 +00:00
static void mndFreeShowObj(SShowObj *pShow) {
2021-12-03 09:22:44 +00:00
SMnode *pMnode = pShow->pMnode;
SShowMgmt *pMgmt = &pMnode->showMgmt;
ShowFreeIterFp freeFp = pMgmt->freeIterFps[pShow->type];
if (freeFp != NULL) {
if (pShow->pIter != NULL) {
(*freeFp)(pMnode, pShow->pIter);
}
}
2021-12-30 12:07:32 +00:00
mTrace("show:0x%" PRIx64 ", is destroyed, data:%p", pShow->id, pShow);
2021-12-06 06:38:37 +00:00
}
static SShowObj *mndAcquireShowObj(SMnode *pMnode, int64_t showId) {
2021-12-06 06:38:37 +00:00
SShowMgmt *pMgmt = &pMnode->showMgmt;
SShowObj *pShow = taosCacheAcquireByKey(pMgmt->cache, &showId, sizeof(showId));
2021-12-06 06:38:37 +00:00
if (pShow == NULL) {
2021-12-30 12:07:32 +00:00
mError("show:0x%" PRIx64 ", already destroyed", showId);
2021-12-06 06:38:37 +00:00
return NULL;
}
2021-12-30 12:07:32 +00:00
mTrace("show:0x%" PRIx64 ", acquired from cache, data:%p", pShow->id, pShow);
2021-12-06 06:38:37 +00:00
return pShow;
}
static void mndReleaseShowObj(SShowObj *pShow, bool forceRemove) {
if (pShow == NULL) return;
2021-12-30 12:07:32 +00:00
mTrace("show:0x%" PRIx64 ", released from cache, data:%p force:%d", pShow->id, pShow, forceRemove);
2021-12-29 06:35:23 +00:00
2021-12-06 07:06:29 +00:00
// A bug in tcache.c
forceRemove = 0;
2021-12-06 06:38:37 +00:00
SMnode *pMnode = pShow->pMnode;
SShowMgmt *pMgmt = &pMnode->showMgmt;
taosCacheRelease(pMgmt->cache, (void **)(&pShow), forceRemove);
2021-12-03 09:22:44 +00:00
}
2021-12-02 11:56:43 +00:00
2022-05-16 06:55:31 +00:00
static int32_t mndProcessRetrieveSysTableReq(SRpcMsg *pReq) {
SMnode *pMnode = pReq->info.node;
2022-03-09 06:58:15 +00:00
SShowMgmt *pMgmt = &pMnode->showMgmt;
2022-04-14 10:16:02 +00:00
SShowObj *pShow = NULL;
int32_t rowsToRead = SHOW_STEP_SIZE;
2022-03-09 06:58:15 +00:00
int32_t size = 0;
int32_t rowsRead = 0;
SRetrieveTableReq retrieveReq = {0};
2022-05-16 06:55:31 +00:00
if (tDeserializeSRetrieveTableReq(pReq->pCont, pReq->contLen, &retrieveReq) != 0) {
2022-03-09 06:58:15 +00:00
terrno = TSDB_CODE_INVALID_MSG;
return -1;
}
if (retrieveReq.showId == 0) {
2022-12-04 11:33:59 +00:00
STableMetaRsp *pMeta = taosHashGet(pMnode->infosMeta, retrieveReq.tb, strlen(retrieveReq.tb));
2022-04-15 05:57:32 +00:00
if (pMeta == NULL) {
2022-12-04 11:33:59 +00:00
pMeta = taosHashGet(pMnode->perfsMeta, retrieveReq.tb, strlen(retrieveReq.tb));
if (pMeta == NULL) {
terrno = TSDB_CODE_MND_INVALID_SYS_TABLENAME;
mError("failed to process show-retrieve req:%p since %s", pShow, terrstr());
return -1;
}
2022-04-15 05:57:32 +00:00
}
2022-04-20 12:41:53 +00:00
pShow = mndCreateShowObj(pMnode, &retrieveReq);
2022-03-09 06:58:15 +00:00
if (pShow == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
mError("failed to process show-meta req since %s", terrstr());
return -1;
}
2022-03-16 09:52:44 +00:00
2022-04-15 05:57:32 +00:00
pShow->pMeta = pMeta;
pShow->numOfColumns = pShow->pMeta->numOfColumns;
2022-03-09 06:58:15 +00:00
} else {
pShow = mndAcquireShowObj(pMnode, retrieveReq.showId);
if (pShow == NULL) {
terrno = TSDB_CODE_MND_INVALID_SHOWOBJ;
mError("failed to process show-retrieve req:%p since %s", pShow, terrstr());
return -1;
}
}
ShowRetrieveFp retrieveFp = pMgmt->retrieveFps[pShow->type];
if (retrieveFp == NULL) {
2022-04-14 10:16:02 +00:00
mndReleaseShowObj(pShow, false);
2022-03-09 06:58:15 +00:00
terrno = TSDB_CODE_MSG_NOT_PROCESSED;
mError("show:0x%" PRIx64 ", failed to retrieve data since %s", pShow->id, terrstr());
return -1;
}
2022-04-14 10:16:02 +00:00
mDebug("show:0x%" PRIx64 ", start retrieve data, type:%d", pShow->id, pShow->type);
2022-06-25 12:05:27 +00:00
if (retrieveReq.user[0] != 0) {
memcpy(pReq->info.conn.user, retrieveReq.user, TSDB_USER_LEN);
} else {
2022-06-27 03:07:07 +00:00
memcpy(pReq->info.conn.user, TSDB_DEFAULT_USER, strlen(TSDB_DEFAULT_USER) + 1);
2022-06-25 12:05:27 +00:00
}
2022-08-12 06:55:31 +00:00
if (retrieveReq.db[0] && mndCheckShowPrivilege(pMnode, pReq->info.conn.user, pShow->type, retrieveReq.db) != 0) {
2022-06-25 10:03:12 +00:00
return -1;
}
2022-11-05 17:34:04 +00:00
int32_t numOfCols = pShow->pMeta->numOfColumns;
2022-03-09 06:58:15 +00:00
2022-11-05 17:34:04 +00:00
SSDataBlock *pBlock = createDataBlock();
2022-04-14 10:16:02 +00:00
for (int32_t i = 0; i < numOfCols; ++i) {
SColumnInfoData idata = {0};
2022-11-05 17:34:04 +00:00
SSchema *p = &pShow->pMeta->pSchemas[i];
idata.info.bytes = p->bytes;
2022-04-14 10:16:02 +00:00
idata.info.type = p->type;
idata.info.colId = p->colId;
2022-11-05 17:34:04 +00:00
blockDataAppendColInfo(pBlock, &idata);
}
2022-03-11 11:29:43 +00:00
blockDataEnsureCapacity(pBlock, rowsToRead);
2022-11-05 17:34:04 +00:00
2022-04-14 10:16:02 +00:00
if (mndCheckRetrieveFinished(pShow)) {
mDebug("show:0x%" PRIx64 ", read finished, numOfRows:%d", pShow->id, pShow->numOfRows);
rowsRead = 0;
} else {
2022-04-14 10:16:02 +00:00
rowsRead = (*retrieveFp)(pReq, pShow, pBlock, rowsToRead);
2022-03-26 07:03:22 +00:00
if (rowsRead < 0) {
terrno = rowsRead;
mDebug("show:0x%" PRIx64 ", retrieve completed", pShow->id);
2022-04-14 10:16:02 +00:00
mndReleaseShowObj(pShow, true);
2022-05-26 09:18:25 +00:00
blockDataDestroy(pBlock);
2022-03-26 07:03:22 +00:00
return -1;
}
2022-03-09 06:58:15 +00:00
pBlock->info.rows = rowsRead;
mDebug("show:0x%" PRIx64 ", stop retrieve data, rowsRead:%d numOfRows:%d", pShow->id, rowsRead, pShow->numOfRows);
}
2022-03-09 06:58:15 +00:00
2022-04-14 10:16:02 +00:00
size = sizeof(SRetrieveMetaTableRsp) + sizeof(int32_t) + sizeof(SSysTableSchema) * pShow->pMeta->numOfColumns +
blockDataGetSize(pBlock) + blockDataGetSerialMetaSize(taosArrayGetSize(pBlock->pDataBlock));
SRetrieveMetaTableRsp *pRsp = rpcMallocCont(size);
if (pRsp == NULL) {
2022-04-14 10:16:02 +00:00
mndReleaseShowObj(pShow, false);
terrno = TSDB_CODE_OUT_OF_MEMORY;
mError("show:0x%" PRIx64 ", failed to retrieve data since %s", pShow->id, terrstr());
blockDataDestroy(pBlock);
return -1;
}
pRsp->handle = htobe64(pShow->id);
if (rowsRead > 0) {
2022-04-14 10:16:02 +00:00
char *pStart = pRsp->data;
SSchema *ps = pShow->pMeta->pSchemas;
*(int32_t *)pStart = htonl(pShow->pMeta->numOfColumns);
pStart += sizeof(int32_t); // number of columns
for (int32_t i = 0; i < pShow->pMeta->numOfColumns; ++i) {
SSysTableSchema *pSchema = (SSysTableSchema *)pStart;
pSchema->bytes = htonl(ps[i].bytes);
pSchema->colId = htons(ps[i].colId);
pSchema->type = ps[i].type;
pStart += sizeof(SSysTableSchema);
}
2022-11-09 05:45:46 +00:00
int32_t len = blockEncode(pBlock, pStart, pShow->pMeta->numOfColumns);
}
2022-03-09 06:58:15 +00:00
pRsp->numOfRows = htonl(rowsRead);
pRsp->precision = TSDB_TIME_PRECISION_MILLI; // millisecond time precision
2022-05-16 06:55:31 +00:00
pReq->info.rsp = pRsp;
pReq->info.rspLen = size;
2022-03-09 06:58:15 +00:00
if (rowsRead == 0 || rowsRead < rowsToRead) {
2022-03-09 06:58:15 +00:00
pRsp->completed = 1;
mDebug("show:0x%" PRIx64 ", retrieve completed", pShow->id);
2022-04-14 10:16:02 +00:00
mndReleaseShowObj(pShow, true);
2022-03-09 06:58:15 +00:00
} else {
mDebug("show:0x%" PRIx64 ", retrieve not completed yet", pShow->id);
2022-04-14 10:16:02 +00:00
mndReleaseShowObj(pShow, false);
2022-03-09 06:58:15 +00:00
}
blockDataDestroy(pBlock);
2022-03-09 06:58:15 +00:00
return TSDB_CODE_SUCCESS;
}
2021-12-02 11:56:43 +00:00
static bool mndCheckRetrieveFinished(SShowObj *pShow) {
if (pShow->pIter == NULL && pShow->numOfRows != 0) {
2021-12-02 11:56:43 +00:00
return true;
2021-12-06 06:38:37 +00:00
}
2021-12-02 11:56:43 +00:00
return false;
}
2021-12-03 06:36:41 +00:00
void mndAddShowRetrieveHandle(SMnode *pMnode, EShowType showType, ShowRetrieveFp fp) {
2021-12-02 11:56:43 +00:00
SShowMgmt *pMgmt = &pMnode->showMgmt;
pMgmt->retrieveFps[showType] = fp;
}
2021-12-03 06:36:41 +00:00
void mndAddShowFreeIterHandle(SMnode *pMnode, EShowType showType, ShowFreeIterFp fp) {
2021-12-02 11:56:43 +00:00
SShowMgmt *pMgmt = &pMnode->showMgmt;
pMgmt->freeIterFps[showType] = fp;
}