vnodeQuery.c 4.8 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

H
Hongze Cheng 已提交
16
#include "vnodeInt.h"
D
dapan1121 已提交
17

S
Shengliang Guan 已提交
18
int vnodeQueryOpen(SVnode *pVnode) {
S
Shengliang Guan 已提交
19
  return qWorkerInit(NODE_TYPE_VNODE, pVnode->vgId, NULL, (void **)&pVnode->pQuery, &pVnode->msgCb);
S
Shengliang Guan 已提交
20
}
D
dapan1121 已提交
21

L
Liu Jicong 已提交
22
void vnodeQueryClose(SVnode *pVnode) { qWorkerDestroy((void **)&pVnode->pQuery); }
D
dapan1121 已提交
23

H
Hongze Cheng 已提交
24
int vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg) {
L
Liu Jicong 已提交
25 26
  STbCfg         *pTbCfg = NULL;
  STbCfg         *pStbCfg = NULL;
H
Hongze Cheng 已提交
27 28 29
  tb_uid_t        uid;
  int32_t         nCols;
  int32_t         nTagCols;
H
Haojun Liao 已提交
30 31
  SSchemaWrapper *pSW = NULL;
  STableMetaRsp  *pTbMetaMsg = NULL;
S
Shengliang Guan 已提交
32 33
  STableMetaRsp   metaRsp = {0};
  SSchema        *pTagSchema;
H
more  
Hongze Cheng 已提交
34
  SRpcMsg         rpcMsg;
D
dapan1121 已提交
35
  int             msgLen = 0;
D
dapan1121 已提交
36
  int32_t         code = 0;
D
dapan1121 已提交
37
  char            tableFName[TSDB_TABLE_FNAME_LEN];
D
dapan1121 已提交
38 39
  int32_t         rspLen = 0;
  void           *pRsp = NULL;
H
more  
Hongze Cheng 已提交
40

S
Shengliang Guan 已提交
41 42
  STableInfoReq infoReq = {0};
  if (tDeserializeSTableInfoReq(pMsg->pCont, pMsg->contLen, &infoReq) != 0) {
D
dapan1121 已提交
43
    code = TSDB_CODE_INVALID_MSG;
S
Shengliang Guan 已提交
44 45 46
    goto _exit;
  }

D
dapan1121 已提交
47 48 49 50 51 52 53 54 55 56
  metaRsp.dbId = pVnode->config.dbId;
  memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName));
  strcpy(metaRsp.tbName, infoReq.tbName);

  sprintf(tableFName, "%s.%s", infoReq.dbFName, infoReq.tbName);
  code = vnodeValidateTableHash(&pVnode->config, tableFName);
  if (code) {
    goto _exit;
  }

S
Shengliang Guan 已提交
57
  pTbCfg = metaGetTbInfoByName(pVnode->pMeta, infoReq.tbName, &uid);
H
Hongze Cheng 已提交
58
  if (pTbCfg == NULL) {
H
Haojun Liao 已提交
59
    code = TSDB_CODE_VND_TB_NOT_EXIST;
H
more  
Hongze Cheng 已提交
60
    goto _exit;
H
Hongze Cheng 已提交
61 62 63 64 65
  }

  if (pTbCfg->type == META_CHILD_TABLE) {
    pStbCfg = metaGetTbInfoByUid(pVnode->pMeta, pTbCfg->ctbCfg.suid);
    if (pStbCfg == NULL) {
D
dapan1121 已提交
66
      code = TSDB_CODE_VND_TB_NOT_EXIST;
H
more  
Hongze Cheng 已提交
67
      goto _exit;
H
Hongze Cheng 已提交
68 69 70 71 72 73 74 75 76 77 78
    }

    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;
H
Hongze Cheng 已提交
79
  } else if (pTbCfg->type == META_CHILD_TABLE) {
H
Hongze Cheng 已提交
80 81 82 83 84
    nTagCols = pStbCfg->stbCfg.nTagCols;
    pTagSchema = pStbCfg->stbCfg.pTagSchema;
  } else {
    nTagCols = 0;
    pTagSchema = NULL;
H
more  
Hongze Cheng 已提交
85 86
  }

wafwerar's avatar
wafwerar 已提交
87
  metaRsp.pSchemas = taosMemoryCalloc(nCols + nTagCols, sizeof(SSchema));
S
Shengliang Guan 已提交
88
  if (metaRsp.pSchemas == NULL) {
D
dapan1121 已提交
89
    code = TSDB_CODE_VND_OUT_OF_MEMORY;
H
more  
Hongze Cheng 已提交
90
    goto _exit;
H
more  
Hongze Cheng 已提交
91 92
  }

H
Hongze Cheng 已提交
93
  if (pTbCfg->type == META_CHILD_TABLE) {
S
Shengliang Guan 已提交
94 95
    strcpy(metaRsp.stbName, pStbCfg->name);
    metaRsp.suid = pTbCfg->ctbCfg.suid;
D
dapan1121 已提交
96
  } else if (pTbCfg->type == META_SUPER_TABLE) {
S
Shengliang Guan 已提交
97 98
    strcpy(metaRsp.stbName, pTbCfg->name);
    metaRsp.suid = uid;
H
Hongze Cheng 已提交
99
  }
S
Shengliang Guan 已提交
100 101 102 103 104
  metaRsp.numOfTags = nTagCols;
  metaRsp.numOfColumns = nCols;
  metaRsp.tableType = pTbCfg->type;
  metaRsp.tuid = uid;
  metaRsp.vgId = pVnode->vgId;
H
Hongze Cheng 已提交
105

S
Shengliang Guan 已提交
106
  memcpy(metaRsp.pSchemas, pSW->pSchema, sizeof(SSchema) * pSW->nCols);
H
Hongze Cheng 已提交
107
  if (nTagCols) {
S
Shengliang Guan 已提交
108
    memcpy(POINTER_SHIFT(metaRsp.pSchemas, sizeof(SSchema) * pSW->nCols), pTagSchema, sizeof(SSchema) * nTagCols);
H
Hongze Cheng 已提交
109 110
  }

D
dapan1121 已提交
111 112 113
_exit:

  rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp);
S
Shengliang Guan 已提交
114 115 116 117 118
  if (rspLen < 0) {
    code = TSDB_CODE_INVALID_MSG;
    goto _exit;
  }

D
dapan1121 已提交
119
  pRsp = rpcMallocCont(rspLen);
S
Shengliang Guan 已提交
120 121 122
  if (pRsp == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
H
Hongze Cheng 已提交
123
  }
S
Shengliang Guan 已提交
124
  tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp);
H
more  
Hongze Cheng 已提交
125

S
Shengliang Guan 已提交
126
  tFreeSTableMetaRsp(&metaRsp);
H
Haojun Liao 已提交
127
  if (pSW != NULL) {
wafwerar's avatar
wafwerar 已提交
128 129
    taosMemoryFreeClear(pSW->pSchema);
    taosMemoryFreeClear(pSW);
H
Hongze Cheng 已提交
130
  }
H
Haojun Liao 已提交
131 132

  if (pTbCfg) {
wafwerar's avatar
wafwerar 已提交
133
    taosMemoryFreeClear(pTbCfg->name);
H
Haojun Liao 已提交
134
    if (pTbCfg->type == META_SUPER_TABLE) {
wafwerar's avatar
wafwerar 已提交
135
      taosMemoryFree(pTbCfg->stbCfg.pTagSchema);
H
Haojun Liao 已提交
136 137 138 139
    } else if (pTbCfg->type == META_SUPER_TABLE) {
      kvRowFree(pTbCfg->ctbCfg.pTag);
    }

wafwerar's avatar
wafwerar 已提交
140
    taosMemoryFreeClear(pTbCfg);
H
Haojun Liao 已提交
141 142
  }

D
dapan1121 已提交
143 144
  rpcMsg.handle = pMsg->handle;
  rpcMsg.ahandle = pMsg->ahandle;
S
Shengliang Guan 已提交
145 146
  rpcMsg.pCont = pRsp;
  rpcMsg.contLen = rspLen;
D
dapan1121 已提交
147
  rpcMsg.code = code;
D
dapan1121 已提交
148

S
shm  
Shengliang Guan 已提交
149
  tmsgSendRsp(&rpcMsg);
D
dapan1121 已提交
150
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
151
}
H
Hongze Cheng 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {
  pLoad->vgId = pVnode->vgId;
  pLoad->role = TAOS_SYNC_STATE_LEADER;
  pLoad->numOfTables = metaGetTbNum(pVnode->pMeta);
  pLoad->numOfTimeSeries = 400;
  pLoad->totalStorage = 300;
  pLoad->compStorage = 200;
  pLoad->pointsWritten = 100;
  pLoad->numOfSelectReqs = 1;
  pLoad->numOfInsertReqs = 3;
  pLoad->numOfInsertSuccessReqs = 2;
  pLoad->numOfBatchInsertReqs = 5;
  pLoad->numOfBatchInsertSuccessReqs = 4;
  return 0;
}