vnodeRead.c 11.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
H
Haojun Liao 已提交
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);
S
Shengliang Guan 已提交
52
    return TSDB_CODE_APP_NOT_READY; 
53
  }
S
slguan 已提交
54

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

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

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

70
static void vnodePutItemIntoReadQueue(SVnodeObj *pVnode, void **qhandle) {
71 72 73 74
  SReadMsg *pRead = (SReadMsg *)taosAllocateQitem(sizeof(SReadMsg));
  pRead->rpcMsg.msgType = TSDB_MSG_TYPE_QUERY;
  pRead->pCont = qhandle;
  pRead->contLen = 0;
H
Haojun Liao 已提交
75
  pRead->rpcMsg.handle = NULL;
76 77

  atomic_add_fetch_32(&pVnode->refCount, 1);
H
Haojun Liao 已提交
78

79
  vDebug("QInfo:%p add to vread queue for exec query, msg:%p", *qhandle, pRead);
80 81 82
  taosWriteQitem(pVnode->rqueue, TAOS_QTYPE_QUERY, pRead);
}

83
static int32_t vnodeDumpQueryResult(SRspRet *pRet, void* pVnode, void** handle, bool* freeHandle) {
H
Haojun Liao 已提交
84 85 86
  bool continueExec = false;

  int32_t code = TSDB_CODE_SUCCESS;
87
  if ((code = qDumpRetrieveResult(*handle, (SRetrieveTableRsp **)&pRet->rsp, &pRet->len, &continueExec)) == TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
88
    if (continueExec) {
H
Haojun Liao 已提交
89
      *freeHandle = false;
H
Haojun Liao 已提交
90
      vnodePutItemIntoReadQueue(pVnode, handle);
91
      pRet->qhandle = *handle;
H
Haojun Liao 已提交
92 93
    } else {
      *freeHandle = true;
94
      vDebug("QInfo:%p exec completed, free handle:%d", *handle, *freeHandle);
H
Haojun Liao 已提交
95 96
    }
  } else {
97
    SRetrieveTableRsp *pRsp = (SRetrieveTableRsp *)rpcMallocCont(sizeof(SRetrieveTableRsp));
98 99 100 101
    memset(pRsp, 0, sizeof(SRetrieveTableRsp));
    pRsp->completed = true;

    pRet->rsp = pRsp;
102
    pRet->len = sizeof(SRetrieveTableRsp);
H
Haojun Liao 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    *freeHandle = true;
  }

  return code;
}

static void vnodeBuildNoResultQueryRsp(SRspRet* pRet) {
  pRet->rsp = (SRetrieveTableRsp *)rpcMallocCont(sizeof(SRetrieveTableRsp));
  pRet->len = sizeof(SRetrieveTableRsp);

  memset(pRet->rsp, 0, sizeof(SRetrieveTableRsp));
  SRetrieveTableRsp* pRsp = pRet->rsp;

  pRsp->completed = true;
}

119
static int32_t vnodeProcessQueryMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) {
120
  void    *pCont = pReadMsg->pCont;
121 122 123
  int32_t  contLen = pReadMsg->contLen;
  SRspRet *pRet = &pReadMsg->rspRet;

S
slguan 已提交
124 125 126
  SQueryTableMsg* pQueryTableMsg = (SQueryTableMsg*) pCont;
  memset(pRet, 0, sizeof(SRspRet));

H
Haojun Liao 已提交
127
  // qHandle needs to be freed correctly
H
Haojun Liao 已提交
128
  if (pReadMsg->rpcMsg.code == TSDB_CODE_RPC_NETWORK_UNAVAIL) {
H
Haojun Liao 已提交
129 130 131 132
    SRetrieveTableMsg* killQueryMsg = (SRetrieveTableMsg*) pReadMsg->pCont;
    killQueryMsg->free = htons(killQueryMsg->free);
    killQueryMsg->qhandle = htobe64(killQueryMsg->qhandle);

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

136
    void** qhandle = qAcquireQInfo(pVnode->qMgmt, (uint64_t) killQueryMsg->qhandle);
137 138
    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 已提交
139
    } else {
140
      assert(*qhandle == (void*) killQueryMsg->qhandle);
H
Haojun Liao 已提交
141

142
      qKillQuery(*qhandle);
143
      qReleaseQInfo(pVnode->qMgmt, (void**) &qhandle, true);
H
Haojun Liao 已提交
144 145
    }

146
    return TSDB_CODE_TSC_QUERY_CANCELLED;
H
Haojun Liao 已提交
147 148 149
  }

  int32_t code = TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
150
  void**  handle = NULL;
H
Haojun Liao 已提交
151

S
slguan 已提交
152
  if (contLen != 0) {
H
Haojun Liao 已提交
153
    qinfo_t pQInfo = NULL;
154
    code = qCreateQueryInfo(pVnode->tsdb, pVnode->vgId, pQueryTableMsg, &pQInfo);
155

S
slguan 已提交
156
    SQueryTableRsp *pRsp = (SQueryTableRsp *) rpcMallocCont(sizeof(SQueryTableRsp));
157 158
    pRsp->code    = code;
    pRsp->qhandle = 0;
159

S
slguan 已提交
160 161
    pRet->len = sizeof(SQueryTableRsp);
    pRet->rsp = pRsp;
B
Bomin Zhang 已提交
162
    int32_t vgId = pVnode->vgId;
H
Haojun Liao 已提交
163

164
    // current connect is broken
H
Haojun Liao 已提交
165
    if (code == TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
166
      handle = qRegisterQInfo(pVnode->qMgmt, (uint64_t) pQInfo);
167
      if (handle == NULL) {  // failed to register qhandle, todo add error test case
168 169
        vError("vgId:%d QInfo:%p register qhandle failed, return to app, code:%s", pVnode->vgId, (void *)pQInfo,
               tstrerror(pRsp->code));
170
        pRsp->code = TSDB_CODE_QRY_INVALID_QHANDLE;
171
        qDestroyQueryInfo(pQInfo);  // destroy it directly
172 173
      } else {
        assert(*handle == pQInfo);
174
        pRsp->qhandle = htobe64((uint64_t) pQInfo);
175 176
      }

177
      if (handle != NULL && vnodeNotifyCurrentQhandle(pReadMsg->rpcMsg.handle, *handle, pVnode->vgId) != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
178
        vError("vgId:%d, QInfo:%p, query discarded since link is broken, %p", pVnode->vgId, *handle, pReadMsg->rpcMsg.handle);
179
        pRsp->code = TSDB_CODE_RPC_NETWORK_UNAVAIL;
H
Haojun Liao 已提交
180
        qReleaseQInfo(pVnode->qMgmt, (void**) &handle, true);
H
Haojun Liao 已提交
181 182 183 184
        return pRsp->code;
      }
    } else {
      assert(pQInfo == NULL);
185
    }
H
Haojun Liao 已提交
186

dengyihao's avatar
dengyihao 已提交
187
    if (handle != NULL) {
188
      vDebug("vgId:%d, QInfo:%p, dnode query msg disposed, create qhandle and returns to app", vgId, *handle);
189
      vnodePutItemIntoReadQueue(pVnode, handle);
dengyihao's avatar
dengyihao 已提交
190
    }
S
slguan 已提交
191
  } else {
192
    assert(pCont != NULL);
193
    void** qhandle = (void**) pCont;
H
Haojun Liao 已提交
194

H
Haojun Liao 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208
    vDebug("vgId:%d, QInfo:%p, dnode continues to exec query", pVnode->vgId, *qhandle);

    bool freehandle = false;
    bool buildRes = qTableQuery(*qhandle); // do execute query

    // build query rsp, the retrieve request has reached here already
    if (buildRes) {
      // update the connection info according to the retrieve connection
      pReadMsg->rpcMsg.handle = qGetResultRetrieveMsg(*qhandle);
      assert(pReadMsg->rpcMsg.handle != NULL);

      vDebug("vgId:%d, QInfo:%p, start to build retrieval rsp after query paused, %p", pVnode->vgId, *qhandle,
             pReadMsg->rpcMsg.handle);

209 210 211 212 213
      // set the real rsp error code
      pReadMsg->rpcMsg.code = vnodeDumpQueryResult(&pReadMsg->rspRet, pVnode, qhandle, &freehandle);

      // NOTE: set return code to be TSDB_CODE_QRY_HAS_RSP to notify dnode to return msg to client
      code = TSDB_CODE_QRY_HAS_RSP;
H
Haojun Liao 已提交
214 215 216 217 218
    } else {
      freehandle = qQueryCompleted(*qhandle);
    }

    // NOTE: if the qhandle is not put into vread queue or query is completed, free the qhandle.
219
    // If the building of result is not required, simply free it. Otherwise, mandatorily free the qhandle
H
Haojun Liao 已提交
220 221 222
    if (freehandle || (!buildRes)) {
      qReleaseQInfo(pVnode->qMgmt, (void **)&qhandle, freehandle);
    }
223
  }
H
Haojun Liao 已提交
224

S
slguan 已提交
225 226 227
  return code;
}

228 229 230 231
static int32_t vnodeProcessFetchMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) {
  void *   pCont = pReadMsg->pCont;
  SRspRet *pRet = &pReadMsg->rspRet;

S
slguan 已提交
232
  SRetrieveTableMsg *pRetrieve = pCont;
H
Haojun Liao 已提交
233
  pRetrieve->free = htons(pRetrieve->free);
234
  pRetrieve->qhandle = htobe64(pRetrieve->qhandle);
H
Haojun Liao 已提交
235

236
  vDebug("vgId:%d, QInfo:%p, retrieve msg is disposed, free:%d, conn:%p", pVnode->vgId, (void*) pRetrieve->qhandle, pRetrieve->free, pReadMsg->rpcMsg.handle);
237

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

H
Haojun Liao 已提交
240
  int32_t code = TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
241
  void** handle = qAcquireQInfo(pVnode->qMgmt, pRetrieve->qhandle);
242
  if (handle == NULL || (*handle) != (void*) pRetrieve->qhandle) {
H
Haojun Liao 已提交
243
    code = TSDB_CODE_QRY_INVALID_QHANDLE;
244
    vDebug("vgId:%d, invalid qhandle in retrieving result, QInfo:%p", pVnode->vgId, (void*) pRetrieve->qhandle);
H
Haojun Liao 已提交
245 246
    
    vnodeBuildNoResultQueryRsp(pRet);
H
Haojun Liao 已提交
247
    return code;
H
Haojun Liao 已提交
248
  }
S
slguan 已提交
249

H
Haojun Liao 已提交
250
  if (pRetrieve->free == 1) {
251
    vWarn("vgId:%d, QInfo:%p, retrieve msg received to kill query and free qhandle", pVnode->vgId, *handle);
252
    qKillQuery(*handle);
H
Haojun Liao 已提交
253
    qReleaseQInfo(pVnode->qMgmt, (void**) &handle, true);
H
Haojun Liao 已提交
254

H
Haojun Liao 已提交
255
    vnodeBuildNoResultQueryRsp(pRet);
256
    code = TSDB_CODE_TSC_QUERY_CANCELLED;
H
Haojun Liao 已提交
257 258 259
    return code;
  }

260
  // register the qhandle to connect to quit query immediate if connection is broken
261 262 263 264 265 266
  if (vnodeNotifyCurrentQhandle(pReadMsg->rpcMsg.handle, *handle, pVnode->vgId) != TSDB_CODE_SUCCESS) {
    vError("vgId:%d, QInfo:%p, retrieve discarded since link is broken, %p", pVnode->vgId, *handle, pReadMsg->rpcMsg.handle);
    code = TSDB_CODE_RPC_NETWORK_UNAVAIL;
    qReleaseQInfo(pVnode->qMgmt, (void**) &handle, true);
    return code;
  }
267

H
Haojun Liao 已提交
268
  bool freeHandle = true;
269 270
  bool buildRes   = false;

271
  code = qRetrieveQueryResultInfo(*handle, &buildRes, pReadMsg->rpcMsg.handle);
H
Haojun Liao 已提交
272 273 274
  if (code != TSDB_CODE_SUCCESS) {
    //TODO handle malloc failure
    pRet->rsp = (SRetrieveTableRsp *)rpcMallocCont(sizeof(SRetrieveTableRsp));
275
    pRet->len = sizeof(SRetrieveTableRsp);
H
Haojun Liao 已提交
276
    memset(pRet->rsp, 0, sizeof(SRetrieveTableRsp));
H
Haojun Liao 已提交
277
    freeHandle = true;
H
Haojun Liao 已提交
278
  } else { // result is not ready, return immediately
279
    if (!buildRes) {
H
Haojun Liao 已提交
280
      qReleaseQInfo(pVnode->qMgmt, (void**) &handle, false);
281 282 283
      return TSDB_CODE_QRY_NOT_READY;
    }

284
    code = vnodeDumpQueryResult(pRet, pVnode, handle, &freeHandle);
H
Haojun Liao 已提交
285
  }
H
Haojun Liao 已提交
286

287 288
  // If qhandle is not added into vread queue, the query should be completed already or paused with error.
  // Here free qhandle immediately
H
Haojun Liao 已提交
289 290
  if (freeHandle) {
    qReleaseQInfo(pVnode->qMgmt, (void**) &handle, true);
S
slguan 已提交
291
  }
292

S
slguan 已提交
293 294
  return code;
}
H
Haojun Liao 已提交
295 296 297 298 299 300 301 302 303 304 305 306

// 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));
307
}