clientMsgHandler.c 12.7 KB
Newer Older
H
Haojun Liao 已提交
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/>.
 */

L
Liu Jicong 已提交
16
#include "catalog.h"
H
Haojun Liao 已提交
17
#include "clientInt.h"
18
#include "clientLog.h"
L
Liu Jicong 已提交
19
#include "os.h"
D
dapan1121 已提交
20
#include "query.h"
L
Liu Jicong 已提交
21 22
#include "tdef.h"
#include "tname.h"
H
Haojun Liao 已提交
23

H
Haojun Liao 已提交
24 25 26 27 28
static void setErrno(SRequestObj* pRequest, int32_t code) {
  pRequest->code = code;
  terrno = code;
}

S
Shengliang Guan 已提交
29
int32_t genericRspCallback(void* param, const SDataBuf* pMsg, int32_t code) {
30
  SRequestObj* pRequest = param;
H
Haojun Liao 已提交
31 32
  setErrno(pRequest, code);

wafwerar's avatar
wafwerar 已提交
33
  taosMemoryFree(pMsg->pData);
34 35 36 37 38
  if (pRequest->body.queryFp != NULL) {
    pRequest->body.queryFp(pRequest->body.param, pRequest, code);
  } else {
    tsem_post(&pRequest->body.rspSem);
  }
H
Haojun Liao 已提交
39
  return code;
40 41
}

S
Shengliang Guan 已提交
42
int32_t processConnectRsp(void* param, const SDataBuf* pMsg, int32_t code) {
43
  SRequestObj* pRequest = param;
H
Haojun Liao 已提交
44
  if (code != TSDB_CODE_SUCCESS) {
wafwerar's avatar
wafwerar 已提交
45
    taosMemoryFree(pMsg->pData);
H
Haojun Liao 已提交
46
    setErrno(pRequest, code);
47
    tsem_post(&pRequest->body.rspSem);
H
Haojun Liao 已提交
48 49
    return code;
  }
50

S
Shengliang Guan 已提交
51
  STscObj* pTscObj = pRequest->pTscObj;
H
Haojun Liao 已提交
52

S
Shengliang Guan 已提交
53 54
  SConnectRsp connectRsp = {0};
  tDeserializeSConnectRsp(pMsg->pData, pMsg->len, &connectRsp);
L
Liu Jicong 已提交
55 56 57 58 59 60 61
  /*assert(connectRsp.epSet.numOfEps > 0);*/
  if (connectRsp.epSet.numOfEps == 0) {
    taosMemoryFree(pMsg->pData);
    setErrno(pRequest, TSDB_CODE_MND_APP_ERROR);
    tsem_post(&pRequest->body.rspSem);
    return code;
  }
H
Haojun Liao 已提交
62

D
dapan1121 已提交
63
  if (connectRsp.dnodeNum == 1) {
dengyihao's avatar
dengyihao 已提交
64 65 66 67
    SEpSet srcEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
    SEpSet dstEpSet = connectRsp.epSet;
    rpcSetDefaultAddr(pTscObj->pAppInfo->pTransporter, srcEpSet.eps[srcEpSet.inUse].fqdn,
                      dstEpSet.eps[dstEpSet.inUse].fqdn);
D
dapan1121 已提交
68
  } else if (connectRsp.dnodeNum > 1 && !isEpsetEqual(&pTscObj->pAppInfo->mgmtEp.epSet, &connectRsp.epSet)) {
D
dapan1121 已提交
69 70 71 72 73 74
    SEpSet* pOrig = &pTscObj->pAppInfo->mgmtEp.epSet;
    SEp* pOrigEp = &pOrig->eps[pOrig->inUse];
    SEp* pNewEp = &connectRsp.epSet.eps[connectRsp.epSet.inUse];
    tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in connRsp", 
        pOrig->inUse, pOrig->numOfEps, pOrigEp->fqdn, pOrigEp->port, 
        connectRsp.epSet.inUse, connectRsp.epSet.numOfEps, pNewEp->fqdn, pNewEp->port);
S
Shengliang Guan 已提交
75
    updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &connectRsp.epSet);
76 77
  }

S
Shengliang Guan 已提交
78
  for (int32_t i = 0; i < connectRsp.epSet.numOfEps; ++i) {
S
Shengliang Guan 已提交
79
    tscDebug("0x%" PRIx64 " epSet.fqdn[%d]:%s port:%d, connObj:0x%" PRIx64, pRequest->requestId, i,
D
dapan1121 已提交
80
             connectRsp.epSet.eps[i].fqdn, connectRsp.epSet.eps[i].port, *(int64_t*)pTscObj->id);
H
Haojun Liao 已提交
81 82
  }

S
Shengliang Guan 已提交
83 84
  pTscObj->connId = connectRsp.connId;
  pTscObj->acctId = connectRsp.acctId;
D
dapan1121 已提交
85 86
  tstrncpy(pTscObj->sVer, connectRsp.sVer, tListLen(pTscObj->sVer));
  tstrncpy(pTscObj->sDetailVer, connectRsp.sDetailVer, tListLen(pTscObj->sDetailVer));
H
Haojun Liao 已提交
87 88

  // update the appInstInfo
S
Shengliang Guan 已提交
89
  pTscObj->pAppInfo->clusterId = connectRsp.clusterId;
H
Haojun Liao 已提交
90 91
  atomic_add_fetch_64(&pTscObj->pAppInfo->numOfConns, 1);

L
Liu Jicong 已提交
92
  pTscObj->connType = connectRsp.connType;
93

D
dapan1121 已提交
94
  hbRegisterConn(pTscObj->pAppInfo->pAppHbMgr, *(int64_t*)pTscObj->id, connectRsp.clusterId, connectRsp.connType);
L
Liu Jicong 已提交
95

96
  //  pRequest->body.resInfo.pRspMsg = pMsg->pData;
S
Shengliang Guan 已提交
97
  tscDebug("0x%" PRIx64 " clusterId:%" PRId64 ", totalConn:%" PRId64, pRequest->requestId, connectRsp.clusterId,
98
           pTscObj->pAppInfo->numOfConns);
99

wafwerar's avatar
wafwerar 已提交
100
  taosMemoryFree(pMsg->pData);
101
  tsem_post(&pRequest->body.rspSem);
102 103
  return 0;
}
H
Haojun Liao 已提交
104

L
Liu Jicong 已提交
105
SMsgSendInfo* buildMsgInfoImpl(SRequestObj* pRequest) {
wafwerar's avatar
wafwerar 已提交
106
  SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
107

H
Haojun Liao 已提交
108
  pMsgSendInfo->requestObjRefId = pRequest->self;
L
Liu Jicong 已提交
109 110 111
  pMsgSendInfo->requestId = pRequest->requestId;
  pMsgSendInfo->param = pRequest;
  pMsgSendInfo->msgType = pRequest->type;
D
dapan1121 已提交
112
  pMsgSendInfo->target.type = TARGET_TYPE_MNODE;
113

H
Haojun Liao 已提交
114 115
  assert(pRequest != NULL);
  pMsgSendInfo->msgInfo = pRequest->body.requestMsg;
H
Haojun Liao 已提交
116
  pMsgSendInfo->fp = getMsgRspHandle(pRequest->type);
117
  return pMsgSendInfo;
118 119
}

120
int32_t processCreateDbRsp(void* param, const SDataBuf* pMsg, int32_t code) {
H
Haojun Liao 已提交
121
  // todo rsp with the vnode id list
122
  SRequestObj* pRequest = param;
wafwerar's avatar
wafwerar 已提交
123
  taosMemoryFree(pMsg->pData);
X
Xiaoyu Wang 已提交
124 125 126
  if (code != TSDB_CODE_SUCCESS) {
    setErrno(pRequest, code);
  }
127 128 129 130 131 132

  if (pRequest->body.queryFp) {
    pRequest->body.queryFp(pRequest->body.param, pRequest, code);
  } else {
    tsem_post(&pRequest->body.rspSem);
  }
X
Xiaoyu Wang 已提交
133
  return code;
H
Haojun Liao 已提交
134
}
H
Haojun Liao 已提交
135

136
int32_t processUseDbRsp(void* param, const SDataBuf* pMsg, int32_t code) {
H
Haojun Liao 已提交
137 138
  SRequestObj* pRequest = param;

D
dapan1121 已提交
139 140 141
  if (TSDB_CODE_MND_DB_NOT_EXIST == code) {
    SUseDbRsp usedbRsp = {0};
    tDeserializeSUseDbRsp(pMsg->pData, pMsg->len, &usedbRsp);
L
Liu Jicong 已提交
142
    struct SCatalog* pCatalog = NULL;
D
dapan1121 已提交
143 144

    if (usedbRsp.vgVersion >= 0) {
145
      uint64_t clusterId = pRequest->pTscObj->pAppInfo->clusterId;
dengyihao's avatar
dengyihao 已提交
146
      int32_t  code1 = catalogGetHandle(clusterId, &pCatalog);
147
      if (code1 != TSDB_CODE_SUCCESS) {
dengyihao's avatar
dengyihao 已提交
148 149
        tscWarn("0x%" PRIx64 "catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", pRequest->requestId, clusterId,
                tstrerror(code1));
D
dapan1121 已提交
150 151 152 153 154
      } else {
        catalogRemoveDB(pCatalog, usedbRsp.db, usedbRsp.uid);
      }
    }

L
Liu Jicong 已提交
155
    tFreeSUsedbRsp(&usedbRsp);
D
dapan1121 已提交
156 157
  }

H
Haojun Liao 已提交
158
  if (code != TSDB_CODE_SUCCESS) {
wafwerar's avatar
wafwerar 已提交
159
    taosMemoryFree(pMsg->pData);
H
Haojun Liao 已提交
160
    setErrno(pRequest, code);
161 162 163 164 165 166 167

    if (pRequest->body.queryFp != NULL) {
      pRequest->body.queryFp(pRequest->body.param, pRequest, pRequest->code);
    } else {
      tsem_post(&pRequest->body.rspSem);
    }

H
Haojun Liao 已提交
168 169 170
    return code;
  }

S
Shengliang Guan 已提交
171 172 173
  SUseDbRsp usedbRsp = {0};
  tDeserializeSUseDbRsp(pMsg->pData, pMsg->len, &usedbRsp);

174
  SName name = {0};
L
Liu Jicong 已提交
175
  tNameFromString(&name, usedbRsp.db, T_NAME_ACCT | T_NAME_DB);
S
Shengliang Guan 已提交
176

D
dapan1121 已提交
177 178 179 180 181 182
  SUseDbOutput output = {0};
  code = queryBuildUseDbOutput(&output, &usedbRsp);

  if (code != 0) {
    terrno = code;
    if (output.dbVgroup) taosHashCleanup(output.dbVgroup->vgHash);
wafwerar's avatar
wafwerar 已提交
183
    taosMemoryFreeClear(output.dbVgroup);
D
dapan1121 已提交
184

dengyihao's avatar
dengyihao 已提交
185
    tscError("0x%" PRIx64 " failed to build use db output since %s", pRequest->requestId, terrstr());
D
dapan1121 已提交
186
  } else if (output.dbVgroup && output.dbVgroup->vgHash) {
L
Liu Jicong 已提交
187 188
    struct SCatalog* pCatalog = NULL;

189 190
    int32_t code1 = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
    if (code1 != TSDB_CODE_SUCCESS) {
L
Liu Jicong 已提交
191
      tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", pRequest->pTscObj->pAppInfo->clusterId,
192
              tstrerror(code1));
D
dapan1121 已提交
193
      taosMemoryFreeClear(output.dbVgroup);
D
dapan1121 已提交
194 195 196 197 198
    } else {
      catalogUpdateDBVgInfo(pCatalog, output.db, output.dbId, output.dbVgroup);
    }
  }

S
Shengliang Guan 已提交
199
  tFreeSUsedbRsp(&usedbRsp);
200 201 202

  char db[TSDB_DB_NAME_LEN] = {0};
  tNameGetDbName(&name, db);
203

204
  setConnectionDB(pRequest->pTscObj, db);
wafwerar's avatar
wafwerar 已提交
205
  taosMemoryFree(pMsg->pData);
206 207 208 209 210 211

  if (pRequest->body.queryFp != NULL) {
    pRequest->body.queryFp(pRequest->body.param, pRequest, pRequest->code);
  } else {
    tsem_post(&pRequest->body.rspSem);
  }
212
  return 0;
213 214
}

H
Haojun Liao 已提交
215
int32_t processCreateSTableRsp(void* param, const SDataBuf* pMsg, int32_t code) {
H
Haojun Liao 已提交
216
  assert(pMsg != NULL && param != NULL);
217
  SRequestObj* pRequest = param;
H
Haojun Liao 已提交
218

wafwerar's avatar
wafwerar 已提交
219
  taosMemoryFree(pMsg->pData);
H
Haojun Liao 已提交
220 221 222 223
  if (code != TSDB_CODE_SUCCESS) {
    setErrno(pRequest, code);
  }

224
  if (pRequest->body.queryFp != NULL) {
225
    removeMeta(pRequest->pTscObj, pRequest->tableList);
226 227 228 229
    pRequest->body.queryFp(pRequest->body.param, pRequest, code);
  } else {
    tsem_post(&pRequest->body.rspSem);
  }
H
Haojun Liao 已提交
230
  return code;
231 232
}

233 234
int32_t processDropDbRsp(void* param, const SDataBuf* pMsg, int32_t code) {
  SRequestObj* pRequest = param;
H
Haojun Liao 已提交
235 236
  if (code != TSDB_CODE_SUCCESS) {
    setErrno(pRequest, code);
237 238 239
  } else {
    SDropDbRsp dropdbRsp = {0};
    tDeserializeSDropDbRsp(pMsg->pData, pMsg->len, &dropdbRsp);
D
dapan1121 已提交
240

241 242 243 244
    struct SCatalog* pCatalog = NULL;
    catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
    catalogRemoveDB(pCatalog, dropdbRsp.db, dropdbRsp.uid);
  }
D
dapan1121 已提交
245

246 247 248 249 250
  if (pRequest->body.queryFp != NULL) {
    pRequest->body.queryFp(pRequest->body.param, pRequest, code);
  } else {
    tsem_post(&pRequest->body.rspSem);
  }
H
Haojun Liao 已提交
251
  return code;
252 253
}

D
dapan1121 已提交
254 255 256 257
int32_t processAlterStbRsp(void* param, const SDataBuf* pMsg, int32_t code) {
  SRequestObj* pRequest = param;
  if (code != TSDB_CODE_SUCCESS) {
    setErrno(pRequest, code);
258 259 260 261 262 263 264 265 266
  } else {
    SMAlterStbRsp alterRsp = {0};
    SDecoder      coder = {0};
    tDecoderInit(&coder, pMsg->pData, pMsg->len);
    tDecodeSMAlterStbRsp(&coder, &alterRsp);
    tDecoderClear(&coder);

    pRequest->body.resInfo.execRes.msgType = TDMT_MND_ALTER_STB;
    pRequest->body.resInfo.execRes.res = alterRsp.pMeta;
D
dapan1121 已提交
267 268
  }

269
  if (pRequest->body.queryFp != NULL) {
270 271 272 273 274 275 276 277 278 279 280 281 282 283
    SQueryExecRes* pRes = &pRequest->body.resInfo.execRes;

    if (code == TSDB_CODE_SUCCESS) {
      SCatalog* pCatalog = NULL;
      int32_t   ret = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
      if (pRes->res != NULL) {
        ret = handleAlterTbExecRes(pRes->res, pCatalog);
      }

      if (ret != TSDB_CODE_SUCCESS) {
        code = ret;
      }
    }

284 285 286 287
    pRequest->body.queryFp(pRequest->body.param, pRequest, code);
  } else {
    tsem_post(&pRequest->body.rspSem);
  }
D
dapan1121 已提交
288 289 290
  return code;
}

D
dapan1121 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
static int32_t buildShowVariablesBlock(SArray* pVars, SSDataBlock** block) {
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
  pBlock->info.hasVarCol = true;

  pBlock->pDataBlock = taosArrayInit(SHOW_VARIABLES_RESULT_COLS, sizeof(SColumnInfoData));

  SColumnInfoData infoData = {0};
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
  infoData.info.bytes = SHOW_VARIABLES_RESULT_FIELD1_LEN;

  taosArrayPush(pBlock->pDataBlock, &infoData);

  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
  infoData.info.bytes = SHOW_VARIABLES_RESULT_FIELD2_LEN;
  taosArrayPush(pBlock->pDataBlock, &infoData);

  int32_t numOfCfg = taosArrayGetSize(pVars);
  blockDataEnsureCapacity(pBlock, numOfCfg);

  for (int32_t i = 0, c = 0; i < numOfCfg; ++i, c = 0) {
    SVariablesInfo *pInfo = taosArrayGet(pVars, i);

    char name[TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE] = {0};
    STR_WITH_MAXSIZE_TO_VARSTR(name, pInfo->name, TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE);
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, c++);
    colDataAppend(pColInfo, i, name, false);
    
    char value[TSDB_CONFIG_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
    STR_WITH_MAXSIZE_TO_VARSTR(value, pInfo->value, TSDB_CONFIG_VALUE_LEN + VARSTR_HEADER_SIZE);
    pColInfo = taosArrayGet(pBlock->pDataBlock, c++);
    colDataAppend(pColInfo, i, value, false);
  }

  pBlock->info.rows = numOfCfg;

  *block = pBlock;
  
  return TSDB_CODE_SUCCESS;
}


static int32_t buildShowVariablesRsp(SArray* pVars, SRetrieveTableRsp** pRsp) {
  SSDataBlock* pBlock = NULL;
  int32_t code = buildShowVariablesBlock(pVars, &pBlock);
  if (code) {
    return code;
  }

  size_t rspSize = sizeof(SRetrieveTableRsp) + blockGetEncodeSize(pBlock);
  *pRsp = taosMemoryCalloc(1, rspSize);
  if (NULL == *pRsp) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  (*pRsp)->useconds = 0;
  (*pRsp)->completed = 1;
  (*pRsp)->precision = 0;
  (*pRsp)->compressed = 0;
  (*pRsp)->compLen = 0;
  (*pRsp)->numOfRows = htonl(pBlock->info.rows);
  (*pRsp)->numOfCols = htonl(SHOW_VARIABLES_RESULT_COLS);

  int32_t len = 0;
  blockCompressEncode(pBlock, (*pRsp)->data, &len, SHOW_VARIABLES_RESULT_COLS, false);
  ASSERT(len == rspSize - sizeof(SRetrieveTableRsp));

  blockDataDestroy(pBlock);
  return TSDB_CODE_SUCCESS;
}

int32_t processShowVariablesRsp(void* param, const SDataBuf* pMsg, int32_t code) {
  SRequestObj* pRequest = param;
  if (code != TSDB_CODE_SUCCESS) {
    setErrno(pRequest, code);
  } else {
    SShowVariablesRsp rsp = {0};
    SRetrieveTableRsp* pRes = NULL;
    code = tDeserializeSShowVariablesRsp(pMsg->pData, pMsg->len, &rsp);
    if (TSDB_CODE_SUCCESS == code) {
      code = buildShowVariablesRsp(rsp.variables, &pRes);
    }
    if (TSDB_CODE_SUCCESS == code) {
      code = setQueryResultFromRsp(&pRequest->body.resInfo, pRes, false, false);
    }
    
    tFreeSShowVariablesRsp(&rsp);
  }

  if (pRequest->body.queryFp != NULL) {
    pRequest->body.queryFp(pRequest->body.param, pRequest, code);
  } else {
    tsem_post(&pRequest->body.rspSem);
  }
  return code;
}


H
Haojun Liao 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401
__async_send_cb_fn_t getMsgRspHandle(int32_t msgType) {
  switch (msgType) {
    case TDMT_MND_CONNECT:
      return processConnectRsp;
    case TDMT_MND_CREATE_DB:
      return processCreateDbRsp;
    case TDMT_MND_USE_DB:
      return processUseDbRsp;
    case TDMT_MND_CREATE_STB:
      return processCreateSTableRsp;
    case TDMT_MND_DROP_DB:
      return processDropDbRsp;
    case TDMT_MND_ALTER_STB:
      return processAlterStbRsp;
D
dapan1121 已提交
402 403
    case TDMT_MND_SHOW_VARIABLES:
      return processShowVariablesRsp;
H
Haojun Liao 已提交
404 405 406
    default:
      return genericRspCallback;
  }
L
Liu Jicong 已提交
407
}