clientMsgHandler.c 7.5 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
  if (connectRsp.dnodeNum == 1) {
dengyihao's avatar
dengyihao 已提交
62 63 64 65
    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 已提交
66
  } else if (connectRsp.dnodeNum > 1 && !isEpsetEqual(&pTscObj->pAppInfo->mgmtEp.epSet, &connectRsp.epSet)) {
S
Shengliang Guan 已提交
67
    updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &connectRsp.epSet);
68 69
  }

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

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

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

L
Liu Jicong 已提交
83
  pTscObj->connType = connectRsp.connType;
84

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

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

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

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

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

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

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

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

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

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

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

L
Liu Jicong 已提交
143
    tFreeSUsedbRsp(&usedbRsp);
D
dapan1121 已提交
144 145
  }

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

S
Shengliang Guan 已提交
153 154 155
  SUseDbRsp usedbRsp = {0};
  tDeserializeSUseDbRsp(pMsg->pData, pMsg->len, &usedbRsp);

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

D
dapan1121 已提交
159 160 161 162 163 164
  SUseDbOutput output = {0};
  code = queryBuildUseDbOutput(&output, &usedbRsp);

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

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

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

S
Shengliang Guan 已提交
181
  tFreeSUsedbRsp(&usedbRsp);
182 183 184

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

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

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

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

203
  tsem_post(&pRequest->body.rspSem);
H
Haojun Liao 已提交
204
  return code;
205 206
}

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

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

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

222
  tsem_post(&pRequest->body.rspSem);
H
Haojun Liao 已提交
223
  return code;
224 225
}

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