TDengine/source/dnode/vnode/src/vnd/vnodeQuery.c

272 lines
8 KiB
C
Raw Normal View History

2021-12-24 01:41:09 +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/>.
*/
#include "vnodeQuery.h"
2022-01-14 06:53:08 +00:00
#include "vnd.h"
2021-12-24 01:41:09 +00:00
2021-12-30 09:13:02 +00:00
static int32_t vnodeGetTableList(SVnode *pVnode, SRpcMsg *pMsg);
2022-01-25 06:20:52 +00:00
static int vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg);
2021-12-30 09:13:02 +00:00
2022-01-21 10:19:40 +00:00
int vnodeQueryOpen(SVnode *pVnode) {
return qWorkerInit(NODE_TYPE_VNODE, pVnode->vgId, NULL, (void **)&pVnode->pQuery, pVnode,
2022-01-25 07:18:56 +00:00
(putReqToQueryQFp)vnodePutReqToVQueryQ, (sendReqToDnodeFp)vnodeSendReqToDnode);
2022-01-21 10:19:40 +00:00
}
2021-12-24 01:41:09 +00:00
2022-03-09 08:13:46 +00:00
void vnodeQueryClose(SVnode *pVnode) {
qWorkerDestroy((void **)&pVnode->pQuery);
}
2022-01-25 06:20:52 +00:00
int vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2022-01-21 11:52:01 +00:00
vTrace("message in query queue is processing");
2022-01-28 02:44:02 +00:00
SReadHandle handle = {.reader = pVnode->pTsdb, .meta = pVnode->pMeta};
2022-01-12 10:40:22 +00:00
switch (pMsg->msgType) {
2022-03-08 09:22:21 +00:00
case TDMT_VND_QUERY: {
2022-01-28 02:44:02 +00:00
return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg);
}
2022-01-12 10:40:22 +00:00
case TDMT_VND_QUERY_CONTINUE:
2022-01-28 02:44:02 +00:00
return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg);
2022-01-12 10:40:22 +00:00
default:
vError("unknown msg type:%d in query queue", pMsg->msgType);
return TSDB_CODE_VND_APP_ERROR;
}
2021-12-24 01:41:09 +00:00
}
2022-01-25 06:20:52 +00:00
int vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2022-01-21 11:52:01 +00:00
vTrace("message in fetch queue is processing");
2021-12-27 10:42:33 +00:00
switch (pMsg->msgType) {
case TDMT_VND_FETCH:
return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg);
2022-01-25 07:18:56 +00:00
case TDMT_VND_FETCH_RSP:
return qWorkerProcessFetchRsp(pVnode, pVnode->pQuery, pMsg);
2021-12-27 10:42:33 +00:00
case TDMT_VND_RES_READY:
return qWorkerProcessReadyMsg(pVnode, pVnode->pQuery, pMsg);
case TDMT_VND_TASKS_STATUS:
return qWorkerProcessStatusMsg(pVnode, pVnode->pQuery, pMsg);
case TDMT_VND_CANCEL_TASK:
return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg);
case TDMT_VND_DROP_TASK:
return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg);
2021-12-29 15:07:01 +00:00
case TDMT_VND_SHOW_TABLES:
return qWorkerProcessShowMsg(pVnode, pVnode->pQuery, pMsg);
case TDMT_VND_SHOW_TABLES_FETCH:
2021-12-30 09:13:02 +00:00
return vnodeGetTableList(pVnode, pMsg);
2021-12-31 06:02:48 +00:00
// return qWorkerProcessShowFetchMsg(pVnode->pMeta, pVnode->pQuery, pMsg);
2021-12-31 05:34:38 +00:00
case TDMT_VND_TABLE_META:
2022-01-25 06:20:52 +00:00
return vnodeGetTableMeta(pVnode, pMsg);
2022-01-20 02:45:15 +00:00
case TDMT_VND_CONSUME:
2022-03-08 09:22:21 +00:00
return tqProcessPollReq(pVnode->pTq, pMsg);
2022-03-09 08:13:46 +00:00
case TDMT_VND_QUERY_HEARTBEAT:
return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg);
2021-12-27 10:42:33 +00:00
default:
vError("unknown msg type:%d in fetch queue", pMsg->msgType);
return TSDB_CODE_VND_APP_ERROR;
}
2021-12-24 01:41:09 +00:00
}
2022-01-25 06:20:52 +00:00
static int vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg) {
2022-03-08 09:22:21 +00:00
STbCfg *pTbCfg = NULL;
STbCfg *pStbCfg = NULL;
2021-12-30 07:05:49 +00:00
tb_uid_t uid;
int32_t nCols;
int32_t nTagCols;
2022-01-25 05:42:33 +00:00
SSchemaWrapper *pSW = NULL;
STableMetaRsp *pTbMetaMsg = NULL;
2022-02-15 08:46:21 +00:00
STableMetaRsp metaRsp = {0};
SSchema *pTagSchema;
2021-12-31 06:02:48 +00:00
SRpcMsg rpcMsg;
2021-12-31 06:43:13 +00:00
int msgLen = 0;
int32_t code = TSDB_CODE_VND_APP_ERROR;
2021-12-28 05:58:53 +00:00
2022-02-15 08:46:21 +00:00
STableInfoReq infoReq = {0};
if (tDeserializeSTableInfoReq(pMsg->pCont, pMsg->contLen, &infoReq) != 0) {
terrno = TSDB_CODE_INVALID_MSG;
goto _exit;
}
pTbCfg = metaGetTbInfoByName(pVnode->pMeta, infoReq.tbName, &uid);
2021-12-30 07:05:49 +00:00
if (pTbCfg == NULL) {
2022-01-12 07:51:34 +00:00
code = TSDB_CODE_VND_TB_NOT_EXIST;
2021-12-31 06:02:48 +00:00
goto _exit;
2021-12-30 07:05:49 +00:00
}
if (pTbCfg->type == META_CHILD_TABLE) {
pStbCfg = metaGetTbInfoByUid(pVnode->pMeta, pTbCfg->ctbCfg.suid);
if (pStbCfg == NULL) {
2022-02-11 11:52:07 +00:00
code = TSDB_CODE_VND_TB_NOT_EXIST;
2021-12-31 06:02:48 +00:00
goto _exit;
2021-12-30 07:05:49 +00:00
}
pSW = metaGetTableSchema(pVnode->pMeta, pTbCfg->ctbCfg.suid, 0, true);
} else {
pSW = metaGetTableSchema(pVnode->pMeta, uid, 0, true);
}
nCols = pSW->nCols;
if (pTbCfg->type == META_SUPER_TABLE) {
nTagCols = pTbCfg->stbCfg.nTagCols;
pTagSchema = pTbCfg->stbCfg.pTagSchema;
2022-01-04 07:39:08 +00:00
} else if (pTbCfg->type == META_CHILD_TABLE) {
2021-12-30 07:05:49 +00:00
nTagCols = pStbCfg->stbCfg.nTagCols;
pTagSchema = pStbCfg->stbCfg.pTagSchema;
} else {
nTagCols = 0;
pTagSchema = NULL;
2021-12-28 05:58:53 +00:00
}
2022-02-15 08:46:21 +00:00
metaRsp.pSchemas = calloc(nCols + nTagCols, sizeof(SSchema));
2022-02-15 07:24:27 +00:00
if (metaRsp.pSchemas == NULL) {
2022-02-11 11:52:07 +00:00
code = TSDB_CODE_VND_OUT_OF_MEMORY;
2021-12-31 06:02:48 +00:00
goto _exit;
2021-12-28 05:58:53 +00:00
}
2022-02-22 03:22:13 +00:00
metaRsp.dbId = pVnode->config.dbId;
2022-02-15 08:46:21 +00:00
memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
strcpy(metaRsp.tbName, infoReq.tbName);
2021-12-30 07:05:49 +00:00
if (pTbCfg->type == META_CHILD_TABLE) {
2022-02-15 07:24:27 +00:00
strcpy(metaRsp.stbName, pStbCfg->name);
metaRsp.suid = pTbCfg->ctbCfg.suid;
2022-01-07 01:56:11 +00:00
} else if (pTbCfg->type == META_SUPER_TABLE) {
2022-02-15 07:24:27 +00:00
strcpy(metaRsp.stbName, pTbCfg->name);
metaRsp.suid = uid;
2021-12-30 07:05:49 +00:00
}
2022-02-15 07:24:27 +00:00
metaRsp.numOfTags = nTagCols;
metaRsp.numOfColumns = nCols;
metaRsp.tableType = pTbCfg->type;
metaRsp.tuid = uid;
metaRsp.vgId = pVnode->vgId;
2021-12-30 07:05:49 +00:00
2022-02-15 07:24:27 +00:00
memcpy(metaRsp.pSchemas, pSW->pSchema, sizeof(SSchema) * pSW->nCols);
2021-12-30 07:05:49 +00:00
if (nTagCols) {
2022-02-15 07:24:27 +00:00
memcpy(POINTER_SHIFT(metaRsp.pSchemas, sizeof(SSchema) * pSW->nCols), pTagSchema, sizeof(SSchema) * nTagCols);
2021-12-30 07:05:49 +00:00
}
2022-02-15 07:24:27 +00:00
int32_t rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
if (rspLen < 0) {
code = TSDB_CODE_INVALID_MSG;
goto _exit;
}
2022-02-15 08:46:21 +00:00
void *pRsp = rpcMallocCont(rspLen);
2022-02-15 07:24:27 +00:00
if (pRsp == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _exit;
2021-12-30 07:05:49 +00:00
}
2022-02-15 08:46:21 +00:00
tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
2021-12-28 05:58:53 +00:00
2021-12-31 06:43:13 +00:00
code = 0;
2021-12-31 06:02:48 +00:00
_exit:
2022-02-15 07:24:27 +00:00
tFreeSTableMetaRsp(&metaRsp);
2022-01-25 05:42:33 +00:00
if (pSW != NULL) {
tfree(pSW->pSchema);
tfree(pSW);
2022-01-24 06:44:19 +00:00
}
2022-01-25 05:42:33 +00:00
if (pTbCfg) {
tfree(pTbCfg->name);
if (pTbCfg->type == META_SUPER_TABLE) {
free(pTbCfg->stbCfg.pTagSchema);
} else if (pTbCfg->type == META_SUPER_TABLE) {
kvRowFree(pTbCfg->ctbCfg.pTag);
}
tfree(pTbCfg);
}
2021-12-31 06:43:13 +00:00
rpcMsg.handle = pMsg->handle;
rpcMsg.ahandle = pMsg->ahandle;
2022-02-15 07:24:27 +00:00
rpcMsg.pCont = pRsp;
rpcMsg.contLen = rspLen;
2021-12-31 06:43:13 +00:00
rpcMsg.code = code;
2021-12-31 05:34:38 +00:00
rpcSendResponse(&rpcMsg);
2022-02-15 07:24:27 +00:00
return code;
2021-12-30 09:13:02 +00:00
}
2022-01-24 06:44:19 +00:00
static void freeItemHelper(void *pItem) {
char *p = *(char **)pItem;
2022-01-22 06:24:09 +00:00
free(p);
}
2021-12-30 09:13:02 +00:00
/**
* @param pVnode
* @param pMsg
* @param pRsp
*/
static int32_t vnodeGetTableList(SVnode *pVnode, SRpcMsg *pMsg) {
2021-12-31 06:02:48 +00:00
SMTbCursor *pCur = metaOpenTbCursor(pVnode->pMeta);
2022-03-08 09:22:21 +00:00
SArray *pArray = taosArrayInit(10, POINTER_BYTES);
2021-12-30 09:13:02 +00:00
2022-03-08 09:22:21 +00:00
char *name = NULL;
2021-12-30 09:13:02 +00:00
int32_t totalLen = 0;
int32_t numOfTables = 0;
2021-12-30 09:13:02 +00:00
while ((name = metaTbCursorNext(pCur)) != NULL) {
2022-01-22 06:24:09 +00:00
if (numOfTables < 10000) { // TODO: temp get tables of vnode, and should del when show tables commad ok.
taosArrayPush(pArray, &name);
totalLen += strlen(name);
2022-01-22 06:24:09 +00:00
} else {
tfree(name);
}
2022-01-22 06:24:09 +00:00
numOfTables++;
}
// TODO: temp debug, and should del when show tables command ok
2022-01-22 06:24:09 +00:00
vInfo("====vgId:%d, numOfTables: %d", pVnode->vgId, numOfTables);
if (numOfTables > 10000) {
2022-01-24 06:44:19 +00:00
numOfTables = 10000;
2021-12-30 09:13:02 +00:00
}
metaCloseTbCursor(pCur);
2021-12-31 06:02:48 +00:00
int32_t rowLen =
(TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE) + 8 + 2 + (TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE) + 8 + 4;
2022-01-24 06:44:19 +00:00
// int32_t numOfTables = (int32_t)taosArrayGetSize(pArray);
2021-12-30 09:13:02 +00:00
int32_t payloadLen = rowLen * numOfTables;
2021-12-31 06:02:48 +00:00
// SVShowTablesFetchReq *pFetchReq = pMsg->pCont;
2021-12-30 09:13:02 +00:00
SVShowTablesFetchRsp *pFetchRsp = (SVShowTablesFetchRsp *)rpcMallocCont(sizeof(SVShowTablesFetchRsp) + payloadLen);
2022-01-10 12:44:11 +00:00
memset(pFetchRsp, 0, sizeof(SVShowTablesFetchRsp) + payloadLen);
2021-12-30 09:13:02 +00:00
2021-12-31 06:02:48 +00:00
char *p = pFetchRsp->data;
for (int32_t i = 0; i < numOfTables; ++i) {
char *n = taosArrayGetP(pArray, i);
2021-12-30 09:13:02 +00:00
STR_TO_VARSTR(p, n);
2021-12-31 03:18:59 +00:00
p += (TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE);
2022-01-24 07:45:49 +00:00
// free(n);
2021-12-30 09:13:02 +00:00
}
pFetchRsp->numOfRows = htonl(numOfTables);
pFetchRsp->precision = 0;
SRpcMsg rpcMsg = {
2021-12-31 06:02:48 +00:00
.handle = pMsg->handle,
2021-12-30 09:13:02 +00:00
.ahandle = pMsg->ahandle,
2021-12-31 06:02:48 +00:00
.pCont = pFetchRsp,
2021-12-30 09:13:02 +00:00
.contLen = sizeof(SVShowTablesFetchRsp) + payloadLen,
2021-12-31 06:02:48 +00:00
.code = 0,
2021-12-30 09:13:02 +00:00
};
rpcSendResponse(&rpcMsg);
2022-01-24 06:44:19 +00:00
2022-01-22 06:24:09 +00:00
taosArrayDestroyEx(pArray, freeItemHelper);
2021-12-28 05:35:21 +00:00
return 0;
2021-12-31 05:34:38 +00:00
}