clientMsgHandler.c 7.3 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

S
Shengliang Guan 已提交
24
int32_t (*handleRequestRspFp[TDMT_MAX])(void*, const SDataBuf* pMsg, int32_t code);
H
Haojun Liao 已提交
25

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

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

wafwerar's avatar
wafwerar 已提交
35
  taosMemoryFree(pMsg->pData);
36
  tsem_post(&pRequest->body.rspSem);
H
Haojun Liao 已提交
37
  return code;
38 39
}

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

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

S
Shengliang Guan 已提交
51 52
  SConnectRsp connectRsp = {0};
  tDeserializeSConnectRsp(pMsg->pData, pMsg->len, &connectRsp);
L
Liu Jicong 已提交
53 54 55 56 57 58 59
  /*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 已提交
60

D
dapan1121 已提交
61 62 63 64
  if (connectRsp.dnodeNum == 1) {
    SEpSet epset = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);  
    rpcSetDefaultEpSet(pTscObj->pAppInfo->pTransporter, &epset);
  } else if (connectRsp.dnodeNum > 1 && !isEpsetEqual(&pTscObj->pAppInfo->mgmtEp.epSet, &connectRsp.epSet)) {
S
Shengliang Guan 已提交
65
    updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &connectRsp.epSet);
66 67
  }

S
Shengliang Guan 已提交
68
  for (int32_t i = 0; i < connectRsp.epSet.numOfEps; ++i) {
S
Shengliang Guan 已提交
69
    tscDebug("0x%" PRIx64 " epSet.fqdn[%d]:%s port:%d, connObj:0x%" PRIx64, pRequest->requestId, i,
S
Shengliang Guan 已提交
70
             connectRsp.epSet.eps[i].fqdn, connectRsp.epSet.eps[i].port, pTscObj->id);
H
Haojun Liao 已提交
71 72
  }

S
Shengliang Guan 已提交
73 74 75
  pTscObj->connId = connectRsp.connId;
  pTscObj->acctId = connectRsp.acctId;
  tstrncpy(pTscObj->ver, connectRsp.sVersion, tListLen(pTscObj->ver));
H
Haojun Liao 已提交
76 77

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

L
Liu Jicong 已提交
81
  pTscObj->connType = connectRsp.connType;
82

83
  hbRegisterConn(pTscObj->pAppInfo->pAppHbMgr, pTscObj->id, connectRsp.clusterId, connectRsp.connType);
L
Liu Jicong 已提交
84

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

wafwerar's avatar
wafwerar 已提交
89
  taosMemoryFree(pMsg->pData);
90
  tsem_post(&pRequest->body.rspSem);
91 92
  return 0;
}
H
Haojun Liao 已提交
93

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

H
Haojun Liao 已提交
97
  pMsgSendInfo->requestObjRefId = pRequest->self;
L
Liu Jicong 已提交
98 99 100
  pMsgSendInfo->requestId = pRequest->requestId;
  pMsgSendInfo->param = pRequest;
  pMsgSendInfo->msgType = pRequest->type;
101

H
Haojun Liao 已提交
102 103
  assert(pRequest != NULL);
  pMsgSendInfo->msgInfo = pRequest->body.requestMsg;
104

L
Liu Jicong 已提交
105 106 107
  pMsgSendInfo->fp = (handleRequestRspFp[TMSG_INDEX(pRequest->type)] == NULL)
                         ? genericRspCallback
                         : handleRequestRspFp[TMSG_INDEX(pRequest->type)];
108
  return pMsgSendInfo;
109 110
}

111
int32_t processCreateDbRsp(void* param, const SDataBuf* pMsg, int32_t code) {
H
Haojun Liao 已提交
112
  // todo rsp with the vnode id list
113
  SRequestObj* pRequest = param;
wafwerar's avatar
wafwerar 已提交
114
  taosMemoryFree(pMsg->pData);
X
Xiaoyu Wang 已提交
115 116 117
  if (code != TSDB_CODE_SUCCESS) {
    setErrno(pRequest, code);
  }
118
  tsem_post(&pRequest->body.rspSem);
X
Xiaoyu Wang 已提交
119
  return code;
H
Haojun Liao 已提交
120
}
H
Haojun Liao 已提交
121

122
int32_t processUseDbRsp(void* param, const SDataBuf* pMsg, int32_t code) {
H
Haojun Liao 已提交
123 124
  SRequestObj* pRequest = param;

D
dapan1121 已提交
125 126 127
  if (TSDB_CODE_MND_DB_NOT_EXIST == code) {
    SUseDbRsp usedbRsp = {0};
    tDeserializeSUseDbRsp(pMsg->pData, pMsg->len, &usedbRsp);
L
Liu Jicong 已提交
128
    struct SCatalog* pCatalog = NULL;
D
dapan1121 已提交
129 130

    if (usedbRsp.vgVersion >= 0) {
131 132
      uint64_t clusterId = pRequest->pTscObj->pAppInfo->clusterId;
      int32_t code1 = catalogGetHandle(clusterId, &pCatalog);
133
      if (code1 != TSDB_CODE_SUCCESS) {
134
        tscWarn("0x%" PRIx64 "catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", pRequest->requestId, clusterId, tstrerror(code1));
D
dapan1121 已提交
135 136 137 138 139
      } else {
        catalogRemoveDB(pCatalog, usedbRsp.db, usedbRsp.uid);
      }
    }

L
Liu Jicong 已提交
140
    tFreeSUsedbRsp(&usedbRsp);
D
dapan1121 已提交
141 142
  }

H
Haojun Liao 已提交
143
  if (code != TSDB_CODE_SUCCESS) {
wafwerar's avatar
wafwerar 已提交
144
    taosMemoryFree(pMsg->pData);
H
Haojun Liao 已提交
145 146 147 148 149
    setErrno(pRequest, code);
    tsem_post(&pRequest->body.rspSem);
    return code;
  }

S
Shengliang Guan 已提交
150 151 152
  SUseDbRsp usedbRsp = {0};
  tDeserializeSUseDbRsp(pMsg->pData, pMsg->len, &usedbRsp);

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

D
dapan1121 已提交
156 157 158 159 160 161
  SUseDbOutput output = {0};
  code = queryBuildUseDbOutput(&output, &usedbRsp);

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

164
    tscError("0x%" PRIx64" failed to build use db output since %s", pRequest->requestId, terrstr());
D
dapan1121 已提交
165
  } else if (output.dbVgroup) {
L
Liu Jicong 已提交
166 167
    struct SCatalog* pCatalog = NULL;

168 169
    int32_t code1 = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
    if (code1 != TSDB_CODE_SUCCESS) {
L
Liu Jicong 已提交
170
      tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", pRequest->pTscObj->pAppInfo->clusterId,
171
              tstrerror(code1));
D
dapan1121 已提交
172
      taosMemoryFreeClear(output.dbVgroup);
D
dapan1121 已提交
173 174 175 176 177
    } else {
      catalogUpdateDBVgInfo(pCatalog, output.db, output.dbId, output.dbVgroup);
    }
  }

S
Shengliang Guan 已提交
178
  tFreeSUsedbRsp(&usedbRsp);
179 180 181

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

183
  setConnectionDB(pRequest->pTscObj, db);
wafwerar's avatar
wafwerar 已提交
184
  taosMemoryFree(pMsg->pData);
185 186
  tsem_post(&pRequest->body.rspSem);
  return 0;
187 188
}

189
int32_t processCreateTableRsp(void* param, const SDataBuf* pMsg, int32_t code) {
H
Haojun Liao 已提交
190
  assert(pMsg != NULL && param != NULL);
191
  SRequestObj* pRequest = param;
H
Haojun Liao 已提交
192

wafwerar's avatar
wafwerar 已提交
193
  taosMemoryFree(pMsg->pData);
H
Haojun Liao 已提交
194 195 196 197 198 199
  if (code != TSDB_CODE_SUCCESS) {
    setErrno(pRequest, code);
    tsem_post(&pRequest->body.rspSem);
    return code;
  }

200
  tsem_post(&pRequest->body.rspSem);
H
Haojun Liao 已提交
201
  return code;
202 203
}

204 205
int32_t processDropDbRsp(void* param, const SDataBuf* pMsg, int32_t code) {
  SRequestObj* pRequest = param;
H
Haojun Liao 已提交
206 207 208 209 210 211
  if (code != TSDB_CODE_SUCCESS) {
    setErrno(pRequest, code);
    tsem_post(&pRequest->body.rspSem);
    return code;
  }

S
Shengliang Guan 已提交
212 213
  SDropDbRsp dropdbRsp = {0};
  tDeserializeSDropDbRsp(pMsg->pData, pMsg->len, &dropdbRsp);
D
dapan1121 已提交
214

S
Shengliang Guan 已提交
215
  struct SCatalog* pCatalog = NULL;
D
dapan1121 已提交
216
  catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
S
Shengliang Guan 已提交
217
  catalogRemoveDB(pCatalog, dropdbRsp.db, dropdbRsp.uid);
D
dapan1121 已提交
218

219
  tsem_post(&pRequest->body.rspSem);
H
Haojun Liao 已提交
220
  return code;
221 222
}

H
Haojun Liao 已提交
223
void initMsgHandleFp() {
S
Shengliang Guan 已提交
224 225 226
  handleRequestRspFp[TMSG_INDEX(TDMT_MND_CONNECT)] = processConnectRsp;
  handleRequestRspFp[TMSG_INDEX(TDMT_MND_CREATE_DB)] = processCreateDbRsp;
  handleRequestRspFp[TMSG_INDEX(TDMT_MND_USE_DB)] = processUseDbRsp;
227
  handleRequestRspFp[TMSG_INDEX(TDMT_MND_CREATE_STB)] = processCreateTableRsp;
S
Shengliang Guan 已提交
228
  handleRequestRspFp[TMSG_INDEX(TDMT_MND_DROP_DB)] = processDropDbRsp;
L
Liu Jicong 已提交
229
}