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

S
Shengliang Guan 已提交
61 62
  if (!isEpsetEqual(&pTscObj->pAppInfo->mgmtEp.epSet, &connectRsp.epSet)) {
    updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &connectRsp.epSet);
63 64
  }

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

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

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

L
Liu Jicong 已提交
78
  pTscObj->connType = connectRsp.connType;
79

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

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

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

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

H
Haojun Liao 已提交
94
  pMsgSendInfo->requestObjRefId = pRequest->self;
L
Liu Jicong 已提交
95 96 97
  pMsgSendInfo->requestId = pRequest->requestId;
  pMsgSendInfo->param = pRequest;
  pMsgSendInfo->msgType = pRequest->type;
98

H
Haojun Liao 已提交
99 100
  assert(pRequest != NULL);
  pMsgSendInfo->msgInfo = pRequest->body.requestMsg;
101

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

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

119
int32_t processUseDbRsp(void* param, const SDataBuf* pMsg, int32_t code) {
H
Haojun Liao 已提交
120 121
  SRequestObj* pRequest = param;

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

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

L
Liu Jicong 已提交
137
    tFreeSUsedbRsp(&usedbRsp);
D
dapan1121 已提交
138 139
  }

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

S
Shengliang Guan 已提交
147 148 149
  SUseDbRsp usedbRsp = {0};
  tDeserializeSUseDbRsp(pMsg->pData, pMsg->len, &usedbRsp);

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

D
dapan1121 已提交
153 154 155 156 157 158
  SUseDbOutput output = {0};
  code = queryBuildUseDbOutput(&output, &usedbRsp);

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

    tscError("failed to build use db output since %s", terrstr());
D
dapan1121 已提交
162
  } else if (output.dbVgroup) {
L
Liu Jicong 已提交
163 164
    struct SCatalog* pCatalog = NULL;

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

S
Shengliang Guan 已提交
175
  tFreeSUsedbRsp(&usedbRsp);
176 177 178

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

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

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

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

197
  tsem_post(&pRequest->body.rspSem);
H
Haojun Liao 已提交
198
  return code;
199 200
}

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

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

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

216
  tsem_post(&pRequest->body.rspSem);
H
Haojun Liao 已提交
217
  return code;
218 219
}

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