httpInt.h 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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/>.
 */

#ifndef TDENGINE_HTTP_INT_H
#define TDENGINE_HTTP_INT_H

19
#include "os.h"
20 21 22 23 24 25 26 27 28 29
#include <stdbool.h>
#include "pthread.h"
#include "semaphore.h"
#include "tmempool.h"
#include "taosdef.h"
#include "tutil.h"
#include "zlib.h"
#include "http.h"
#include "httpLog.h"
#include "httpJson.h"
S
Shengliang Guan 已提交
30
#include "httpParser.h"
F
freemine 已提交
31

32
#define HTTP_MAX_CMD_SIZE           1024
S
TD-1311  
Shengliang Guan 已提交
33
#define HTTP_MAX_BUFFER_SIZE        1024*1024*8
34 35
#define HTTP_LABEL_SIZE             8
#define HTTP_MAX_EVENTS             10
R
root 已提交
36 37 38
#define HTTP_BUFFER_INIT            4096
#define HTTP_BUFFER_SIZE            8388608
#define HTTP_STEP_SIZE              4096    //http message get process step by step
39
#define HTTP_METHOD_SCANNER_SIZE    7       //http method fp size
S
TD-1326  
Shengliang Guan 已提交
40
#define HTTP_GC_TARGET_SIZE         512
41 42
#define HTTP_WRITE_RETRY_TIMES      500
#define HTTP_WRITE_WAIT_TIME_MS     5
43 44
#define HTTP_PASSWORD_LEN           TSDB_UNI_LEN
#define HTTP_SESSION_ID_LEN         (TSDB_USER_LEN + HTTP_PASSWORD_LEN)
45

S
TD-1311  
Shengliang Guan 已提交
46 47 48 49 50 51 52 53
typedef enum HttpReqType {
  HTTP_REQTYPE_OTHERS = 0,
  HTTP_REQTYPE_LOGIN = 1,
  HTTP_REQTYPE_HEARTBEAT = 2,
  HTTP_REQTYPE_SINGLE_SQL = 3,
  HTTP_REQTYPE_MULTI_SQL = 4
} HttpReqType;

54 55 56 57 58 59 60 61
typedef enum {
  HTTP_SERVER_INIT,
  HTTP_SERVER_RUNNING,
  HTTP_SERVER_CLOSING,
  HTTP_SERVER_CLOSED
} HttpServerStatus;

typedef enum {
S
TD-1311  
Shengliang Guan 已提交
62 63 64 65
  HTTP_CONTEXT_STATE_READY,
  HTTP_CONTEXT_STATE_HANDLING,
  HTTP_CONTEXT_STATE_DROPPING,
  HTTP_CONTEXT_STATE_CLOSED
66 67 68 69 70 71 72 73 74 75 76 77 78
} HttpContextState;

typedef enum {
  HTTP_CMD_TYPE_UN_SPECIFIED,
  HTTP_CMD_TYPE_CREATE_DB,
  HTTP_CMD_TYPE_CREATE_STBALE,
  HTTP_CMD_TYPE_INSERT
} HttpSqlCmdType;

typedef enum { HTTP_CMD_STATE_NOT_RUN_YET, HTTP_CMD_STATE_RUN_FINISHED } HttpSqlCmdState;

typedef enum { HTTP_CMD_RETURN_TYPE_WITH_RETURN, HTTP_CMD_RETURN_TYPE_NO_RETURN } HttpSqlCmdReturnType;

S
TD-1311  
Shengliang Guan 已提交
79 80 81 82 83 84 85 86 87
struct HttpContext;
struct HttpThread;

typedef struct {
  char    id[HTTP_SESSION_ID_LEN];
  int32_t refCount;
  void *  taos;
} HttpSession;

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
typedef struct {
  // used by single cmd
  char   *nativSql;
  int32_t numOfRows;
  int32_t code;

  // these are the locations in the buffer
  int32_t tagNames[TSDB_MAX_TAGS];
  int32_t tagValues[TSDB_MAX_TAGS];
  int32_t timestamp;
  int32_t metric;
  int32_t stable;
  int32_t table;
  int32_t values;
  int32_t sql;

  // used by multi-cmd
  int8_t cmdType;
  int8_t cmdReturnType;
  int8_t cmdState;
  int8_t tagNum;
} HttpSqlCmd;

typedef struct {
  HttpSqlCmd *cmds;
  int16_t     pos;
  int16_t     size;
  int16_t     maxSize;
  int32_t     bufferPos;
  int32_t     bufferSize;
  char *      buffer;
} HttpSqlCmds;

typedef struct {
  char *module;
S
TD-2046  
Shengliang Guan 已提交
123
  bool (*fpDecode)(struct HttpContext *pContext);
124 125 126
} HttpDecodeMethod;

typedef struct {
127
  void (*startJsonFp)(struct HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result);
128
  void (*stopJsonFp)(struct HttpContext *pContext, HttpSqlCmd *cmd);
129
  bool (*buildQueryJsonFp)(struct HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int numOfRows);
130 131 132 133 134 135 136
  void (*buildAffectRowJsonFp)(struct HttpContext *pContext, HttpSqlCmd *cmd, int affectRows);
  void (*initJsonFp)(struct HttpContext *pContext);
  void (*cleanJsonFp)(struct HttpContext *pContext);
  bool (*checkFinishedFp)(struct HttpContext *pContext, HttpSqlCmd *cmd, int code);
  void (*setNextCmdFp)(struct HttpContext *pContext, HttpSqlCmd *cmd, int code);
} HttpEncodeMethod;

F
freemine 已提交
137 138 139 140 141
typedef enum {
  EHTTP_CONTEXT_PROCESS_FAILED = 0x01,
  EHTTP_CONTEXT_PARSER_FAILED  = 0x02
} EHTTP_CONTEXT_FAILED_CAUSE;

142 143
typedef struct HttpContext {
  int32_t      refCount;
144
  SOCKET       fd;
145 146 147 148 149
  uint32_t     accessTimes;
  uint32_t     lastAccessTime;
  int32_t      state;
  uint8_t      reqType;
  uint8_t      parsed;
150
  uint8_t      error;
151 152
  char         ipstr[22];
  char         user[TSDB_USER_LEN];  // parsed from auth token or login message
153
  char         pass[HTTP_PASSWORD_LEN];
154
  char         db[/*TSDB_ACCT_ID_LEN + */TSDB_DB_NAME_LEN];
155
  TAOS *       taos;
156
  void *       ppContext;
157 158
  HttpSession *session;
  z_stream     gzipStream;
S
TD-1311  
Shengliang Guan 已提交
159
  HttpParser  *parser;
160 161 162
  HttpSqlCmd   singleCmd;
  HttpSqlCmds *multiCmds;
  JsonBuf *    jsonBuf;
S
TD-1311  
Shengliang Guan 已提交
163 164
  HttpEncodeMethod *encodeMethod;
  HttpDecodeMethod *decodeMethod;
165 166 167 168 169 170 171 172
  struct HttpThread *pThread;
} HttpContext;

typedef struct HttpThread {
  pthread_t       thread;
  HttpContext *   pHead;
  pthread_mutex_t threadMutex;
  bool            stop;
173
  EpollFd         pollFd;
S
TD-1311  
Shengliang Guan 已提交
174 175
  int32_t         numOfContexts;
  int32_t         threadId;
L
liuyq-617 已提交
176
  char            label[HTTP_LABEL_SIZE << 1];
177 178 179 180 181 182 183
  bool (*processData)(HttpContext *pContext);
} HttpThread;

typedef struct HttpServer {
  char              label[HTTP_LABEL_SIZE];
  uint32_t          serverIp;
  uint16_t          serverPort;
S
TD-1207  
Shengliang Guan 已提交
184 185
  int8_t            stop;
  int8_t            reserve;
186
  SOCKET            fd;
S
TD-1311  
Shengliang Guan 已提交
187 188
  int32_t           numOfThreads;
  int32_t           methodScannerLen;
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
  int32_t           requestNum;
  int32_t           status;
  pthread_t         thread;
  HttpThread *      pThreads;
  void *            contextCache;
  void *            sessionCache;
  pthread_mutex_t   serverMutex;
  HttpDecodeMethod *methodScanner[HTTP_METHOD_SCANNER_SIZE];
  bool (*processData)(HttpContext *pContext);
} HttpServer;

extern const char *httpKeepAliveStr[];
extern const char *httpVersionStr[];
extern HttpServer  tsHttpServer;

#endif