clientMsgHandler.c 6.9 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 17 18
#include "os.h"
#include "tdef.h"
#include "tname.h"
H
Haojun Liao 已提交
19
#include "clientInt.h"
20
#include "clientLog.h"
D
dapan1121 已提交
21
#include "catalog.h"
D
dapan1121 已提交
22
#include "query.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 53
  SConnectRsp connectRsp = {0};
  tDeserializeSConnectRsp(pMsg->pData, pMsg->len, &connectRsp);
  assert(connectRsp.epSet.numOfEps > 0);
H
Haojun Liao 已提交
54

S
Shengliang Guan 已提交
55 56
  if (!isEpsetEqual(&pTscObj->pAppInfo->mgmtEp.epSet, &connectRsp.epSet)) {
    updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, &connectRsp.epSet);
57 58
  }

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

S
Shengliang Guan 已提交
64 65 66
  pTscObj->connId = connectRsp.connId;
  pTscObj->acctId = connectRsp.acctId;
  tstrncpy(pTscObj->ver, connectRsp.sVersion, tListLen(pTscObj->ver));
H
Haojun Liao 已提交
67 68

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

L
Liu Jicong 已提交
72
  pTscObj->connType = connectRsp.connType;
73

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

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

wafwerar's avatar
wafwerar 已提交
80
  taosMemoryFree(pMsg->pData);
81
  tsem_post(&pRequest->body.rspSem);
82 83
  return 0;
}
H
Haojun Liao 已提交
84

85
SMsgSendInfo* buildMsgInfoImpl(SRequestObj *pRequest) {
wafwerar's avatar
wafwerar 已提交
86
  SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
87

H
Haojun Liao 已提交
88 89 90 91
  pMsgSendInfo->requestObjRefId = pRequest->self;
  pMsgSendInfo->requestId       = pRequest->requestId;
  pMsgSendInfo->param           = pRequest;
  pMsgSendInfo->msgType         = pRequest->type;
92

H
Haojun Liao 已提交
93 94
  assert(pRequest != NULL);
  pMsgSendInfo->msgInfo = pRequest->body.requestMsg;
95

H
Haojun Liao 已提交
96
  pMsgSendInfo->fp = (handleRequestRspFp[TMSG_INDEX(pRequest->type)] == NULL)? genericRspCallback:handleRequestRspFp[TMSG_INDEX(pRequest->type)];
97
  return pMsgSendInfo;
98 99
}

100
int32_t processCreateDbRsp(void* param, const SDataBuf* pMsg, int32_t code) {
H
Haojun Liao 已提交
101
  // todo rsp with the vnode id list
102
  SRequestObj* pRequest = param;
wafwerar's avatar
wafwerar 已提交
103
  taosMemoryFree(pMsg->pData);
X
Xiaoyu Wang 已提交
104 105 106
  if (code != TSDB_CODE_SUCCESS) {
    setErrno(pRequest, code);
  }
107
  tsem_post(&pRequest->body.rspSem);
X
Xiaoyu Wang 已提交
108
  return code;
H
Haojun Liao 已提交
109
}
H
Haojun Liao 已提交
110

111
int32_t processUseDbRsp(void* param, const SDataBuf* pMsg, int32_t code) {
H
Haojun Liao 已提交
112 113
  SRequestObj* pRequest = param;

D
dapan1121 已提交
114 115 116 117 118 119
  if (TSDB_CODE_MND_DB_NOT_EXIST == code) {
    SUseDbRsp usedbRsp = {0};
    tDeserializeSUseDbRsp(pMsg->pData, pMsg->len, &usedbRsp);
    struct SCatalog *pCatalog = NULL;

    if (usedbRsp.vgVersion >= 0) {
120 121
      int32_t code1 = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
      if (code1 != TSDB_CODE_SUCCESS) {
L
Liu Jicong 已提交
122
        tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", pRequest->pTscObj->pAppInfo->clusterId,
123
                tstrerror(code1));
D
dapan1121 已提交
124 125 126 127 128
      } else {
        catalogRemoveDB(pCatalog, usedbRsp.db, usedbRsp.uid);
      }
    }

L
Liu Jicong 已提交
129
    tFreeSUsedbRsp(&usedbRsp);
D
dapan1121 已提交
130 131
  }

H
Haojun Liao 已提交
132
  if (code != TSDB_CODE_SUCCESS) {
wafwerar's avatar
wafwerar 已提交
133
    taosMemoryFree(pMsg->pData);
H
Haojun Liao 已提交
134 135 136 137 138
    setErrno(pRequest, code);
    tsem_post(&pRequest->body.rspSem);
    return code;
  }

S
Shengliang Guan 已提交
139 140 141
  SUseDbRsp usedbRsp = {0};
  tDeserializeSUseDbRsp(pMsg->pData, pMsg->len, &usedbRsp);

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

D
dapan1121 已提交
145 146 147 148 149 150
  SUseDbOutput output = {0};
  code = queryBuildUseDbOutput(&output, &usedbRsp);

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

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

157 158
    int32_t code1 = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
    if (code1 != TSDB_CODE_SUCCESS) {
L
Liu Jicong 已提交
159
      tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", pRequest->pTscObj->pAppInfo->clusterId,
160
              tstrerror(code1));
D
dapan1121 已提交
161
      taosMemoryFreeClear(output.dbVgroup);
D
dapan1121 已提交
162 163 164 165 166
    } else {
      catalogUpdateDBVgInfo(pCatalog, output.db, output.dbId, output.dbVgroup);
    }
  }

S
Shengliang Guan 已提交
167
  tFreeSUsedbRsp(&usedbRsp);
168 169 170

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

172
  setConnectionDB(pRequest->pTscObj, db);
wafwerar's avatar
wafwerar 已提交
173
  taosMemoryFree(pMsg->pData);
174 175
  tsem_post(&pRequest->body.rspSem);
  return 0;
176 177
}

178
int32_t processCreateTableRsp(void* param, const SDataBuf* pMsg, int32_t code) {
H
Haojun Liao 已提交
179
  assert(pMsg != NULL && param != NULL);
180
  SRequestObj* pRequest = param;
H
Haojun Liao 已提交
181

wafwerar's avatar
wafwerar 已提交
182
  taosMemoryFree(pMsg->pData);
H
Haojun Liao 已提交
183 184 185 186 187 188
  if (code != TSDB_CODE_SUCCESS) {
    setErrno(pRequest, code);
    tsem_post(&pRequest->body.rspSem);
    return code;
  }

189
  tsem_post(&pRequest->body.rspSem);
H
Haojun Liao 已提交
190
  return code;
191 192
}

193 194
int32_t processDropDbRsp(void* param, const SDataBuf* pMsg, int32_t code) {
  SRequestObj* pRequest = param;
H
Haojun Liao 已提交
195 196 197 198 199 200
  if (code != TSDB_CODE_SUCCESS) {
    setErrno(pRequest, code);
    tsem_post(&pRequest->body.rspSem);
    return code;
  }

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

S
Shengliang Guan 已提交
204
  struct SCatalog* pCatalog = NULL;
D
dapan1121 已提交
205
  catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
S
Shengliang Guan 已提交
206
  catalogRemoveDB(pCatalog, dropdbRsp.db, dropdbRsp.uid);
D
dapan1121 已提交
207

208
  tsem_post(&pRequest->body.rspSem);
H
Haojun Liao 已提交
209
  return code;
210 211
}

H
Haojun Liao 已提交
212
void initMsgHandleFp() {
S
Shengliang Guan 已提交
213 214 215
  handleRequestRspFp[TMSG_INDEX(TDMT_MND_CONNECT)] = processConnectRsp;
  handleRequestRspFp[TMSG_INDEX(TDMT_MND_CREATE_DB)] = processCreateDbRsp;
  handleRequestRspFp[TMSG_INDEX(TDMT_MND_USE_DB)] = processUseDbRsp;
216
  handleRequestRspFp[TMSG_INDEX(TDMT_MND_CREATE_STB)] = processCreateTableRsp;
S
Shengliang Guan 已提交
217
  handleRequestRspFp[TMSG_INDEX(TDMT_MND_DROP_DB)] = processDropDbRsp;
L
Liu Jicong 已提交
218
}