vnodeRead.c 9.4 KB
Newer Older
S
slguan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
17
#include <dnode.h>
S
slguan 已提交
18
#include "os.h"
H
Haojun Liao 已提交
19 20

#include "tglobal.h"
S
slguan 已提交
21
#include "taoserror.h"
H
Haojun Liao 已提交
22 23 24
#include "taosmsg.h"
#include "tcache.h"
#include "query.h"
S
slguan 已提交
25 26 27 28
#include "trpc.h"
#include "tsdb.h"
#include "vnode.h"
#include "vnodeInt.h"
29
#include "tqueue.h"
S
slguan 已提交
30

31 32 33
static int32_t (*vnodeProcessReadMsgFp[TSDB_MSG_TYPE_MAX])(SVnodeObj *pVnode, SReadMsg *pReadMsg);
static int32_t  vnodeProcessQueryMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg);
static int32_t  vnodeProcessFetchMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg);
H
Haojun Liao 已提交
34
static int32_t  vnodeNotifyCurrentQhandle(void* handle, void* qhandle, int32_t vgId);
S
slguan 已提交
35 36

void vnodeInitReadFp(void) {
J
jtao1735 已提交
37 38
  vnodeProcessReadMsgFp[TSDB_MSG_TYPE_QUERY] = vnodeProcessQueryMsg;
  vnodeProcessReadMsgFp[TSDB_MSG_TYPE_FETCH] = vnodeProcessFetchMsg;
S
slguan 已提交
39 40
}

41
int32_t vnodeProcessRead(void *param, SReadMsg *pReadMsg) {
S
slguan 已提交
42
  SVnodeObj *pVnode = (SVnodeObj *)param;
43
  int msgType = pReadMsg->rpcMsg.msgType;
S
slguan 已提交
44

45
  if (vnodeProcessReadMsgFp[msgType] == NULL) {
46
    vDebug("vgId:%d, msgType:%s not processed, no handle", pVnode->vgId, taosMsg[msgType]);
47
    return TSDB_CODE_VND_MSG_NOT_PROCESSED;
48
  }
S
slguan 已提交
49

50
  if (pVnode->status != TAOS_VN_STATUS_READY) {
51
    vDebug("vgId:%d, msgType:%s not processed, vnode status is %d", pVnode->vgId, taosMsg[msgType], pVnode->status);
52
    return TSDB_CODE_VND_INVALID_STATUS; 
53
  }
S
slguan 已提交
54

55 56
  // tsdb may be in reset state  
  if (pVnode->tsdb == NULL) return TSDB_CODE_RPC_NOT_READY;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
57
  if (pVnode->status == TAOS_VN_STATUS_CLOSING)
58 59
    return TSDB_CODE_RPC_NOT_READY;

H
Hui Li 已提交
60
  // TODO: Later, let slave to support query
61
  if (pVnode->syncCfg.replica > 1 && pVnode->role != TAOS_SYNC_ROLE_MASTER) {
62
    vDebug("vgId:%d, msgType:%s not processed, replica:%d role:%d", pVnode->vgId, taosMsg[msgType], pVnode->syncCfg.replica, pVnode->role);
H
Hui Li 已提交
63
    return TSDB_CODE_RPC_NOT_READY;
64
  }
H
Hui Li 已提交
65

66
  return (*vnodeProcessReadMsgFp[msgType])(pVnode, pReadMsg);
S
slguan 已提交
67 68
}

69 70 71 72 73 74 75 76 77 78
static void vnodePutItemIntoReadQueue(SVnodeObj *pVnode, void *qhandle) {
  SReadMsg *pRead = (SReadMsg *)taosAllocateQitem(sizeof(SReadMsg));
  pRead->rpcMsg.msgType = TSDB_MSG_TYPE_QUERY;
  pRead->pCont = qhandle;
  pRead->contLen = 0;

  atomic_add_fetch_32(&pVnode->refCount, 1);
  taosWriteQitem(pVnode->rqueue, TAOS_QTYPE_QUERY, pRead);
}

79
static int32_t vnodeProcessQueryMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) {
80
  void    *pCont = pReadMsg->pCont;
81 82 83
  int32_t  contLen = pReadMsg->contLen;
  SRspRet *pRet = &pReadMsg->rspRet;

S
slguan 已提交
84 85 86
  SQueryTableMsg* pQueryTableMsg = (SQueryTableMsg*) pCont;
  memset(pRet, 0, sizeof(SRspRet));

H
Haojun Liao 已提交
87
  // qHandle needs to be freed correctly
H
Haojun Liao 已提交
88
  if (pReadMsg->rpcMsg.code == TSDB_CODE_RPC_NETWORK_UNAVAIL) {
H
Haojun Liao 已提交
89 90 91 92
    SRetrieveTableMsg* killQueryMsg = (SRetrieveTableMsg*) pReadMsg->pCont;
    killQueryMsg->free = htons(killQueryMsg->free);
    killQueryMsg->qhandle = htobe64(killQueryMsg->qhandle);

93
    vWarn("QInfo:%p connection %p broken, kill query", (void*) killQueryMsg->qhandle, pReadMsg->rpcMsg.handle);
H
Haojun Liao 已提交
94
    assert(pReadMsg->rpcMsg.contLen > 0 && killQueryMsg->free == 1);
95

96
    void** qhandle = qAcquireQInfo(pVnode->qMgmt, (uint64_t) killQueryMsg->qhandle);
97 98
    if (qhandle == NULL || *qhandle == NULL) {
      vWarn("QInfo:%p invalid qhandle, no matched query handle, conn:%p", (void*) killQueryMsg->qhandle, pReadMsg->rpcMsg.handle);
H
Haojun Liao 已提交
99
    } else {
100
      assert(*qhandle == (void*) killQueryMsg->qhandle);
101
      qKillQuery(*qhandle);
102
      qReleaseQInfo(pVnode->qMgmt, (void**) &qhandle, true);
H
Haojun Liao 已提交
103 104
    }

105
    return TSDB_CODE_TSC_QUERY_CANCELLED;
H
Haojun Liao 已提交
106 107 108
  }

  int32_t code = TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
109
  void**  handle = NULL;
H
Haojun Liao 已提交
110

S
slguan 已提交
111
  if (contLen != 0) {
H
Haojun Liao 已提交
112
    qinfo_t pQInfo = NULL;
113
    code = qCreateQueryInfo(pVnode->tsdb, pVnode->vgId, pQueryTableMsg, pVnode, &pQInfo);
114

S
slguan 已提交
115
    SQueryTableRsp *pRsp = (SQueryTableRsp *) rpcMallocCont(sizeof(SQueryTableRsp));
116 117
    pRsp->code    = code;
    pRsp->qhandle = 0;
118

S
slguan 已提交
119 120
    pRet->len = sizeof(SQueryTableRsp);
    pRet->rsp = pRsp;
B
Bomin Zhang 已提交
121
    int32_t vgId = pVnode->vgId;
H
Haojun Liao 已提交
122

123
    // current connect is broken
H
Haojun Liao 已提交
124
    if (code == TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
125 126
//      handle = qRegisterQInfo(pVnode->qMgmt, (uint64_t) pQInfo);
      handle = &pQInfo;
127
      if (handle == NULL) {  // failed to register qhandle
128 129
        vError("vgId:%d QInfo:%p register qhandle failed, return to app, code:%s", pVnode->vgId, (void *)pQInfo,
               tstrerror(pRsp->code));
130
        pRsp->code = TSDB_CODE_QRY_INVALID_QHANDLE;
131
        qDestroyQueryInfo(pQInfo);  // destroy it directly
132 133
      } else {
        assert(*handle == pQInfo);
134
        pRsp->qhandle = htobe64((uint64_t) pQInfo);
135 136
      }

H
Haojun Liao 已提交
137
//      pQInfo = NULL;
138
      if (handle != NULL && vnodeNotifyCurrentQhandle(pReadMsg->rpcMsg.handle, *handle, pVnode->vgId) != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
139
        vError("vgId:%d, QInfo:%p, query discarded since link is broken, %p", pVnode->vgId, *handle, pReadMsg->rpcMsg.handle);
140
        pRsp->code = TSDB_CODE_RPC_NETWORK_UNAVAIL;
H
Haojun Liao 已提交
141
//        qReleaseQInfo(pVnode->qMgmt, (void**) &handle, true);
H
Haojun Liao 已提交
142 143 144 145
        return pRsp->code;
      }
    } else {
      assert(pQInfo == NULL);
146
    }
H
Haojun Liao 已提交
147

dengyihao's avatar
dengyihao 已提交
148
    if (handle != NULL) {
H
Haojun Liao 已提交
149 150
      vDebug("vgId:%d, QInfo:%p, dnode query msg disposed, register qhandle and return to app", vgId, *handle);

151
      vnodePutItemIntoReadQueue(pVnode, *handle);
H
Haojun Liao 已提交
152
//      qReleaseQInfo(pVnode->qMgmt, (void**) &handle, false);
dengyihao's avatar
dengyihao 已提交
153
    }
154

S
slguan 已提交
155
  } else {
156
    assert(pCont != NULL);
H
Haojun Liao 已提交
157 158 159
    void* p = (void*) pCont;
    handle = &p;
//    handle = qAcquireQInfo(pVnode->qMgmt, (uint64_t) pCont);
H
Haojun Liao 已提交
160
    if (handle == NULL) {
161
      vWarn("QInfo:%p invalid qhandle in continuing exec query, conn:%p", (void*) pCont, pReadMsg->rpcMsg.handle);
H
Haojun Liao 已提交
162 163
      code = TSDB_CODE_QRY_INVALID_QHANDLE;
    } else {
H
Haojun Liao 已提交
164
      vDebug("vgId:%d, QInfo:%p, dnode continue exec query", pVnode->vgId, (void*) pCont);
H
Haojun Liao 已提交
165
      code = TSDB_CODE_VND_ACTION_IN_PROGRESS;
dengyihao's avatar
dengyihao 已提交
166
      qTableQuery(*handle); // do execute query
H
Haojun Liao 已提交
167
    }
H
Haojun Liao 已提交
168
//    qReleaseQInfo(pVnode->qMgmt, (void**) &handle, false);
169
  }
H
Haojun Liao 已提交
170

S
slguan 已提交
171 172 173
  return code;
}

174 175 176 177
static int32_t vnodeProcessFetchMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) {
  void *   pCont = pReadMsg->pCont;
  SRspRet *pRet = &pReadMsg->rspRet;

S
slguan 已提交
178
  SRetrieveTableMsg *pRetrieve = pCont;
H
Haojun Liao 已提交
179
  pRetrieve->qhandle = htobe64(pRetrieve->qhandle);
H
Haojun Liao 已提交
180 181
  pRetrieve->free = htons(pRetrieve->free);

H
Hui Li 已提交
182
  vDebug("vgId:%d, QInfo:%p, retrieve msg is disposed", pVnode->vgId, (void*) pRetrieve->qhandle);
183

S
slguan 已提交
184
  memset(pRet, 0, sizeof(SRspRet));
H
Haojun Liao 已提交
185

H
Haojun Liao 已提交
186
  int32_t code = TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
187 188 189 190 191
  void** handle = NULL;
  void* p1 = (void*) pRetrieve->qhandle;
  handle = &p1;

//  void** handle = qAcquireQInfo(pVnode->qMgmt, pRetrieve->qhandle);
192
  if (handle == NULL || (*handle) != (void*) pRetrieve->qhandle) {
H
Haojun Liao 已提交
193
    code = TSDB_CODE_QRY_INVALID_QHANDLE;
194
    vDebug("vgId:%d, invalid qhandle in fetch result, QInfo:%p", pVnode->vgId, (void*) pRetrieve->qhandle);
H
Haojun Liao 已提交
195 196 197 198 199 200 201 202

    pRet->rsp = (SRetrieveTableRsp *)rpcMallocCont(sizeof(SRetrieveTableRsp));
    pRet->len = sizeof(SRetrieveTableRsp);

    memset(pRet->rsp, 0, sizeof(SRetrieveTableRsp));
    SRetrieveTableRsp* pRsp = pRet->rsp;
    pRsp->numOfRows = 0;
    pRsp->useconds  = 0;
203
    pRsp->completed = true;
H
Haojun Liao 已提交
204 205

    return code;
H
Haojun Liao 已提交
206
  }
S
slguan 已提交
207

H
Haojun Liao 已提交
208
  if (pRetrieve->free == 1) {
H
Haojun Liao 已提交
209
    vDebug("vgId:%d, QInfo:%p, retrieve msg received to kill query and free qhandle", pVnode->vgId, *handle);
210
    qKillQuery(*handle);
H
Haojun Liao 已提交
211
//    qReleaseQInfo(pVnode->qMgmt, (void**) &handle, true);
H
Haojun Liao 已提交
212

S
slguan 已提交
213
    pRet->rsp = (SRetrieveTableRsp *)rpcMallocCont(sizeof(SRetrieveTableRsp));
H
Haojun Liao 已提交
214 215
    pRet->len = sizeof(SRetrieveTableRsp);

S
slguan 已提交
216
    memset(pRet->rsp, 0, sizeof(SRetrieveTableRsp));
H
Haojun Liao 已提交
217 218 219 220
    SRetrieveTableRsp* pRsp = pRet->rsp;
    pRsp->numOfRows = 0;
    pRsp->completed = true;
    pRsp->useconds  = 0;
221

H
Haojun Liao 已提交
222 223 224 225 226 227 228 229 230 231 232 233
    return code;
  }

  bool freeHandle = true;
  code = qRetrieveQueryResultInfo(*handle);
  if (code != TSDB_CODE_SUCCESS) {
    //TODO handle malloc failure
    pRet->rsp = (SRetrieveTableRsp *)rpcMallocCont(sizeof(SRetrieveTableRsp));
    memset(pRet->rsp, 0, sizeof(SRetrieveTableRsp));
  } else { // if failed to dump result, free qhandle immediately
    if ((code = qDumpRetrieveResult(*handle, (SRetrieveTableRsp **)&pRet->rsp, &pRet->len)) == TSDB_CODE_SUCCESS) {
      if (qHasMoreResultsToRetrieve(*handle)) {
234
        vnodePutItemIntoReadQueue(pVnode, *handle);
235
        pRet->qhandle = *handle;
H
Haojun Liao 已提交
236
        freeHandle = false;
237 238 239
      } else {
        qKillQuery(*handle);
        freeHandle = true;
H
Haojun Liao 已提交
240
      }
S
slguan 已提交
241 242
    }
  }
243

H
Haojun Liao 已提交
244 245
  UNUSED(freeHandle);
//  qReleaseQInfo(pVnode->qMgmt, (void**) &handle, freeHandle);
S
slguan 已提交
246 247
  return code;
}
H
Haojun Liao 已提交
248 249 250 251 252 253 254 255 256 257 258 259

// notify connection(handle) that current qhandle is created, if current connection from
// client is broken, the query needs to be killed immediately.
int32_t vnodeNotifyCurrentQhandle(void* handle, void* qhandle, int32_t vgId) {
  SRetrieveTableMsg* killQueryMsg = rpcMallocCont(sizeof(SRetrieveTableMsg));
  killQueryMsg->qhandle = htobe64((uint64_t) qhandle);
  killQueryMsg->free = htons(1);
  killQueryMsg->header.vgId = htonl(vgId);
  killQueryMsg->header.contLen = htonl(sizeof(SRetrieveTableMsg));

  vDebug("QInfo:%p register qhandle to connect:%p", qhandle, handle);
  return rpcReportProgress(handle, (char*) killQueryMsg, sizeof(SRetrieveTableMsg));
260
}