This commit is contained in:
Shengliang Guan 2021-01-22 11:59:14 +08:00
parent 2079533b50
commit fa7e9bd6d2
19 changed files with 247 additions and 238 deletions

View File

@ -100,9 +100,7 @@ const char *httpContextStateStr(HttpContextState state) {
} }
} }
void httpNotifyContextClose(HttpContext *pContext) { void httpNotifyContextClose(HttpContext *pContext) { shutdown(pContext->fd, SHUT_WR); }
shutdown(pContext->fd, SHUT_WR);
}
bool httpAlterContextState(HttpContext *pContext, HttpContextState srcState, HttpContextState destState) { bool httpAlterContextState(HttpContext *pContext, HttpContextState srcState, HttpContextState destState) {
return (atomic_val_compare_exchange_32(&pContext->state, srcState, destState) == srcState); return (atomic_val_compare_exchange_32(&pContext->state, srcState, destState) == srcState);
@ -174,7 +172,6 @@ bool httpInitContext(HttpContext *pContext) {
pContext->encodeMethod = NULL; pContext->encodeMethod = NULL;
memset(&pContext->singleCmd, 0, sizeof(HttpSqlCmd)); memset(&pContext->singleCmd, 0, sizeof(HttpSqlCmd));
httpTrace("context:%p, fd:%d, parsed:%d", pContext, pContext->fd, pContext->parsed); httpTrace("context:%p, fd:%d, parsed:%d", pContext, pContext->fd, pContext->parsed);
return true; return true;
} }

View File

@ -168,7 +168,6 @@ bool gcBuildQueryJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result,
if (i < num_fields - 1) { if (i < num_fields - 1) {
len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, ", "); len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, ", ");
} }
} }
len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, "}"); len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, "}");

View File

@ -31,19 +31,18 @@ struct ehttp_gzip_s {
z_stream * gzip; z_stream * gzip;
gz_header * header; gz_header * header;
char * chunk; char * chunk;
int32_t state; int32_t state;
}; };
static void dummy_on_data(ehttp_gzip_t *gzip, void *arg, const char *buf, int32_t len) { static void dummy_on_data(ehttp_gzip_t *gzip, void *arg, const char *buf, int32_t len) {}
}
static void ehttp_gzip_cleanup(ehttp_gzip_t *gzip) { static void ehttp_gzip_cleanup(ehttp_gzip_t *gzip) {
switch (gzip->state) { switch (gzip->state) {
case EHTTP_GZIP_READY: { case EHTTP_GZIP_READY: {
inflateEnd(gzip->gzip); inflateEnd(gzip->gzip);
} break; } break;
default: break; default:
break;
} }
if (gzip->gzip) { if (gzip->gzip) {
free(gzip->gzip); free(gzip->gzip);
@ -164,4 +163,3 @@ int32_t ehttp_gzip_finish(ehttp_gzip_t *gzip) {
return 0; return 0;
} }

View File

@ -46,18 +46,19 @@ int32_t httpWriteBufByFd(struct HttpContext* pContext, const char* buf, int32_t
do { do {
if (pContext->fd > 2) { if (pContext->fd > 2) {
len = (int32_t)taosSend(pContext->fd, buf + writeLen, (size_t)(sz - writeLen), MSG_NOSIGNAL); len = (int32_t)taosSend(pContext->fd, buf + writeLen, (size_t)(sz - writeLen), MSG_NOSIGNAL);
} } else {
else {
return sz; return sz;
} }
if (len < 0) { if (len < 0) {
httpDebug("context:%p, fd:%d, socket write errno:%d:%s, times:%d", pContext, pContext->fd, errno, strerror(errno), countWait); httpDebug("context:%p, fd:%d, socket write errno:%d:%s, times:%d", pContext, pContext->fd, errno, strerror(errno),
countWait);
if (++countWait > HTTP_WRITE_RETRY_TIMES) break; if (++countWait > HTTP_WRITE_RETRY_TIMES) break;
taosMsleep(HTTP_WRITE_WAIT_TIME_MS); taosMsleep(HTTP_WRITE_WAIT_TIME_MS);
continue; continue;
} else if (len == 0) { } else if (len == 0) {
httpDebug("context:%p, fd:%d, socket write errno:%d:%s, connect already closed", pContext, pContext->fd, errno, strerror(errno)); httpDebug("context:%p, fd:%d, socket write errno:%d:%s, connect already closed", pContext, pContext->fd, errno,
strerror(errno));
break; break;
} else { } else {
countWait = 0; countWait = 0;
@ -113,9 +114,9 @@ int32_t httpWriteJsonBufBody(JsonBuf* buf, bool isTheLast) {
httpTrace("context:%p, fd:%d, no data need dump", buf->pContext, buf->pContext->fd); httpTrace("context:%p, fd:%d, no data need dump", buf->pContext, buf->pContext->fd);
return 0; // there is no data to dump. return 0; // there is no data to dump.
} else { } else {
int32_t len = sprintf(sLen, "%d\r\n", srcLen); int32_t len = sprintf(sLen, "%x\r\n", srcLen);
httpTrace("context:%p, fd:%d, write body, chunkSize:%d, response:\n%s", buf->pContext, buf->pContext->fd, httpTrace("context:%p, fd:%d, write body, chunkSize:%d, response:\n%s", buf->pContext, buf->pContext->fd, srcLen,
srcLen, buf->buf); buf->buf);
httpWriteBufNoTrace(buf->pContext, sLen, len); httpWriteBufNoTrace(buf->pContext, sLen, len);
remain = httpWriteBufNoTrace(buf->pContext, buf->buf, srcLen); remain = httpWriteBufNoTrace(buf->pContext, buf->buf, srcLen);
} }
@ -126,8 +127,8 @@ int32_t httpWriteJsonBufBody(JsonBuf* buf, bool isTheLast) {
if (ret == 0) { if (ret == 0) {
if (compressBufLen > 0) { if (compressBufLen > 0) {
int32_t len = sprintf(sLen, "%x\r\n", compressBufLen); int32_t len = sprintf(sLen, "%x\r\n", compressBufLen);
httpTrace("context:%p, fd:%d, write body, chunkSize:%d, compressSize:%d, last:%d, response:\n%s", httpTrace("context:%p, fd:%d, write body, chunkSize:%d, compressSize:%d, last:%d, response:\n%s", buf->pContext,
buf->pContext, buf->pContext->fd, srcLen, compressBufLen, isTheLast, buf->buf); buf->pContext->fd, srcLen, compressBufLen, isTheLast, buf->buf);
httpWriteBufNoTrace(buf->pContext, sLen, len); httpWriteBufNoTrace(buf->pContext, sLen, len);
remain = httpWriteBufNoTrace(buf->pContext, (const char*)compressBuf, compressBufLen); remain = httpWriteBufNoTrace(buf->pContext, (const char*)compressBuf, compressBufLen);
} else { } else {

View File

@ -237,7 +237,6 @@ static int32_t httpOnParseHeaderField(HttpParser *parser, const char *key, const
} }
httpTrace("context:%p, fd:%d, keepAlive:%d", pContext, pContext->fd, pContext->parser->keepAlive); httpTrace("context:%p, fd:%d, keepAlive:%d", pContext, pContext->fd, pContext->parser->keepAlive);
} }
#if 0 #if 0
else if (0 == strcasecmp(key, "Content-Encoding")) { else if (0 == strcasecmp(key, "Content-Encoding")) {
if (0 == strcmp(val, "gzip")) { if (0 == strcmp(val, "gzip")) {
@ -410,9 +409,7 @@ static int32_t httpPopStack(HttpParser *parser) {
return 0; return 0;
} }
static void httpClearStack(HttpStack *stack) { static void httpClearStack(HttpStack *stack) { stack->pos = 0; }
stack->pos = 0;
}
static int32_t httpCleanupStack(HttpStack *stack) { static int32_t httpCleanupStack(HttpStack *stack) {
free(stack->stacks); free(stack->stacks);
@ -523,15 +520,26 @@ char *httpDecodeUrl(const char *enc) {
if (!p) break; if (!p) break;
int32_t hex, cnt; int32_t hex, cnt;
int32_t n = sscanf(p + 1, "%2x%n", &hex, &cnt); int32_t n = sscanf(p + 1, "%2x%n", &hex, &cnt);
if (n!=1 && cnt !=2) { ok = 0; break; } if (n != 1 && cnt != 2) {
if (httpAppendString(&str, enc, (int32_t)(p-enc))) { ok = 0; break; } ok = 0;
break;
}
if (httpAppendString(&str, enc, (int32_t)(p - enc))) {
ok = 0;
break;
}
char c = (char)hex; char c = (char)hex;
if (httpAppendString(&str, &c, 1)) { ok = 0; break; } if (httpAppendString(&str, &c, 1)) {
ok = 0;
break;
}
enc = p + 3; enc = p + 3;
} }
char *dec = NULL; char *dec = NULL;
if (ok && *enc) { if (ok && *enc) {
if (httpAppendString(&str, enc, (int32_t)strlen(enc))) { ok = 0; } if (httpAppendString(&str, enc, (int32_t)strlen(enc))) {
ok = 0;
}
} }
if (ok) { if (ok) {
dec = str.str; dec = str.str;

View File

@ -161,7 +161,8 @@ void httpSendTaosdInvalidSqlErrorResp(HttpContext *pContext, char *errMsg) {
temp[i] = '\''; temp[i] = '\'';
} else if (temp[i] == '\n') { } else if (temp[i] == '\n') {
temp[i] = ' '; temp[i] = ' ';
} else {} } else {
}
} }
httpSendErrorRespImp(pContext, httpCode, "Bad Request", TSDB_CODE_TSC_INVALID_SQL & 0XFFFF, temp); httpSendErrorRespImp(pContext, httpCode, "Bad Request", TSDB_CODE_TSC_INVALID_SQL & 0XFFFF, temp);

View File

@ -95,7 +95,6 @@ bool restProcessSqlRequest(HttpContext* pContext, int32_t timestampFmt) {
return false; return false;
} }
/* /*
* for async test * for async test
* *

View File

@ -83,7 +83,8 @@ void restStartSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result)
httpJsonToken(jsonBuf, JsonArrStt); httpJsonToken(jsonBuf, JsonArrStt);
} }
bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int32_t numOfRows, int32_t timestampFormat) { bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int32_t numOfRows,
int32_t timestampFormat) {
JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); JsonBuf *jsonBuf = httpMallocJsonBuf(pContext);
if (jsonBuf == NULL) return false; if (jsonBuf == NULL) return false;
@ -135,11 +136,13 @@ bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result,
break; break;
case TSDB_DATA_TYPE_TIMESTAMP: case TSDB_DATA_TYPE_TIMESTAMP:
if (timestampFormat == REST_TIMESTAMP_FMT_LOCAL_STRING) { if (timestampFormat == REST_TIMESTAMP_FMT_LOCAL_STRING) {
httpJsonTimestamp(jsonBuf, *((int64_t *)row[i]), taos_result_precision(result) == TSDB_TIME_PRECISION_MICRO); httpJsonTimestamp(jsonBuf, *((int64_t *)row[i]),
taos_result_precision(result) == TSDB_TIME_PRECISION_MICRO);
} else if (timestampFormat == REST_TIMESTAMP_FMT_TIMESTAMP) { } else if (timestampFormat == REST_TIMESTAMP_FMT_TIMESTAMP) {
httpJsonInt64(jsonBuf, *((int64_t *)row[i])); httpJsonInt64(jsonBuf, *((int64_t *)row[i]));
} else { } else {
httpJsonUtcTimestamp(jsonBuf, *((int64_t *)row[i]), taos_result_precision(result) == TSDB_TIME_PRECISION_MICRO); httpJsonUtcTimestamp(jsonBuf, *((int64_t *)row[i]),
taos_result_precision(result) == TSDB_TIME_PRECISION_MICRO);
} }
break; break;
default: default:

View File

@ -35,11 +35,13 @@ static void httpStopThread(HttpThread* pThread) {
struct epoll_event event = {.events = EPOLLIN}; struct epoll_event event = {.events = EPOLLIN};
eventfd_t fd = eventfd(1, 0); eventfd_t fd = eventfd(1, 0);
if (fd == -1) { if (fd == -1) {
httpError("%s, failed to create eventfd, will call pthread_cancel instead, which may result in data corruption: %s", pThread->label, strerror(errno)); httpError("%s, failed to create eventfd, will call pthread_cancel instead, which may result in data corruption: %s",
pThread->label, strerror(errno));
pThread->stop = true; pThread->stop = true;
pthread_cancel(pThread->thread); pthread_cancel(pThread->thread);
} else if (epoll_ctl(pThread->pollFd, EPOLL_CTL_ADD, fd, &event) < 0) { } else if (epoll_ctl(pThread->pollFd, EPOLL_CTL_ADD, fd, &event) < 0) {
httpError("%s, failed to call epoll_ctl, will call pthread_cancel instead, which may result in data corruption: %s", pThread->label, strerror(errno)); httpError("%s, failed to call epoll_ctl, will call pthread_cancel instead, which may result in data corruption: %s",
pThread->label, strerror(errno));
pthread_cancel(pThread->thread); pthread_cancel(pThread->thread);
} }
@ -210,8 +212,8 @@ static void *httpAcceptHttpConnection(void *arg) {
pContext = httpCreateContext(connFd); pContext = httpCreateContext(connFd);
if (pContext == NULL) { if (pContext == NULL) {
httpError("fd:%d, ip:%s:%u, no enough resource to allocate http context", connFd, taosInetNtoa(clientAddr.sin_addr), httpError("fd:%d, ip:%s:%u, no enough resource to allocate http context", connFd,
htons(clientAddr.sin_port)); taosInetNtoa(clientAddr.sin_addr), htons(clientAddr.sin_port));
taosCloseSocket(connFd); taosCloseSocket(connFd);
continue; continue;
} }

View File

@ -35,7 +35,8 @@ void httpCreateSession(HttpContext *pContext, void *taos) {
session.refCount = 1; session.refCount = 1;
int32_t len = snprintf(session.id, HTTP_SESSION_ID_LEN, "%s.%s", pContext->user, pContext->pass); int32_t len = snprintf(session.id, HTTP_SESSION_ID_LEN, "%s.%s", pContext->user, pContext->pass);
pContext->session = taosCachePut(server->sessionCache, session.id, len, &session, sizeof(HttpSession), tsHttpSessionExpire * 1000); pContext->session =
taosCachePut(server->sessionCache, session.id, len, &session, sizeof(HttpSession), tsHttpSessionExpire * 1000);
// void *temp = pContext->session; // void *temp = pContext->session;
// taosCacheRelease(server->sessionCache, (void **)&temp, false); // taosCacheRelease(server->sessionCache, (void **)&temp, false);

View File

@ -269,8 +269,8 @@ void httpProcessSingleSqlCallBackImp(void *param, TAOS_RES *result, int32_t code
pContext->user, tstrerror(code), pObj, taos_errstr(pObj)); pContext->user, tstrerror(code), pObj, taos_errstr(pObj));
httpSendTaosdInvalidSqlErrorResp(pContext, taos_errstr(pObj)); httpSendTaosdInvalidSqlErrorResp(pContext, taos_errstr(pObj));
} else { } else {
httpError("context:%p, fd:%d, user:%s, query error, code:%s, sqlObj:%p", pContext, pContext->fd, httpError("context:%p, fd:%d, user:%s, query error, code:%s, sqlObj:%p", pContext, pContext->fd, pContext->user,
pContext->user, tstrerror(code), pObj); tstrerror(code), pObj);
httpSendErrorResp(pContext, code); httpSendErrorResp(pContext, code);
} }
taos_free_result(result); taos_free_result(result);

View File

@ -114,6 +114,4 @@ void httpCleanUpSystem() {
tsHttpServer.status = HTTP_SERVER_CLOSED; tsHttpServer.status = HTTP_SERVER_CLOSED;
} }
int32_t httpGetReqCount() { int32_t httpGetReqCount() { return atomic_exchange_32(&tsHttpServer.requestNum, 0); }
return atomic_exchange_32(&tsHttpServer.requestNum, 0);
}

View File

@ -308,9 +308,7 @@ void tgInitHandle(HttpServer *pServer) {
httpAddMethod(pServer, &tgDecodeMethod); httpAddMethod(pServer, &tgDecodeMethod);
} }
void tgCleanupHandle() { void tgCleanupHandle() { tgFreeSchemas(); }
tgFreeSchemas();
}
bool tgGetUserFromUrl(HttpContext *pContext) { bool tgGetUserFromUrl(HttpContext *pContext) {
HttpParser *pParser = pContext->parser; HttpParser *pParser = pContext->parser;
@ -603,7 +601,8 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) {
if (tsTelegrafUseFieldNum == 0) { if (tsTelegrafUseFieldNum == 0) {
table_cmd->stable = stable_cmd->stable = httpAddToSqlCmdBuffer(pContext, "%s", stname); table_cmd->stable = stable_cmd->stable = httpAddToSqlCmdBuffer(pContext, "%s", stname);
} else { } else {
table_cmd->stable = stable_cmd->stable = httpAddToSqlCmdBuffer(pContext, "%s_%d_%d", stname, fieldsSize, orderTagsLen); table_cmd->stable = stable_cmd->stable =
httpAddToSqlCmdBuffer(pContext, "%s_%d_%d", stname, fieldsSize, orderTagsLen);
} }
table_cmd->stable = stable_cmd->stable = table_cmd->stable = stable_cmd->stable =
httpShrinkTableName(pContext, table_cmd->stable, httpGetCmdsString(pContext, table_cmd->stable)); httpShrinkTableName(pContext, table_cmd->stable, httpGetCmdsString(pContext, table_cmd->stable));
@ -627,9 +626,11 @@ bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) {
// table name // table name
if (tsTelegrafUseFieldNum == 0) { if (tsTelegrafUseFieldNum == 0) {
table_cmd->table = stable_cmd->table = httpAddToSqlCmdBufferNoTerminal(pContext, "%s_%s", stname, host->valuestring); table_cmd->table = stable_cmd->table =
httpAddToSqlCmdBufferNoTerminal(pContext, "%s_%s", stname, host->valuestring);
} else { } else {
table_cmd->table = stable_cmd->table = httpAddToSqlCmdBufferNoTerminal(pContext, "%s_%d_%d_%s", stname, fieldsSize, orderTagsLen, host->valuestring); table_cmd->table = stable_cmd->table =
httpAddToSqlCmdBufferNoTerminal(pContext, "%s_%d_%d_%s", stname, fieldsSize, orderTagsLen, host->valuestring);
} }
for (int32_t i = 0; i < orderTagsLen; ++i) { for (int32_t i = 0; i < orderTagsLen; ++i) {
cJSON *tag = orderedTags[i]; cJSON *tag = orderedTags[i];

View File

@ -160,8 +160,7 @@ bool httpMallocMultiCmds(HttpContext *pContext, int32_t cmdSize, int32_t bufferS
free(multiCmds->cmds); free(multiCmds->cmds);
multiCmds->cmds = (HttpSqlCmd *)malloc((size_t)cmdSize * sizeof(HttpSqlCmd)); multiCmds->cmds = (HttpSqlCmd *)malloc((size_t)cmdSize * sizeof(HttpSqlCmd));
if (multiCmds->cmds == NULL) { if (multiCmds->cmds == NULL) {
httpError("context:%p, fd:%d, user:%s, malloc cmds:%d error", pContext, pContext->fd, httpError("context:%p, fd:%d, user:%s, malloc cmds:%d error", pContext, pContext->fd, pContext->user, cmdSize);
pContext->user, cmdSize);
return false; return false;
} }
multiCmds->maxSize = (int16_t)cmdSize; multiCmds->maxSize = (int16_t)cmdSize;
@ -381,7 +380,9 @@ int32_t httpGzipDeCompress(char *srcData, int32_t nSrcData, char *destData, int3
if ((err = inflate(&gzipStream, Z_NO_FLUSH)) != Z_OK) { if ((err = inflate(&gzipStream, Z_NO_FLUSH)) != Z_OK) {
return -2; return -2;
} }
} else return -3; } else {
return -3;
}
} }
} }
@ -397,14 +398,16 @@ int32_t httpGzipCompressInit(HttpContext *pContext) {
pContext->gzipStream.zalloc = (alloc_func)0; pContext->gzipStream.zalloc = (alloc_func)0;
pContext->gzipStream.zfree = (free_func)0; pContext->gzipStream.zfree = (free_func)0;
pContext->gzipStream.opaque = (voidpf)0; pContext->gzipStream.opaque = (voidpf)0;
if (deflateInit2(&pContext->gzipStream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) { if (deflateInit2(&pContext->gzipStream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) !=
Z_OK) {
return -1; return -1;
} }
return 0; return 0;
} }
int32_t httpGzipCompress(HttpContext *pContext, char *srcData, int32_t nSrcData, char *destData, int32_t *nDestData, bool isTheLast) { int32_t httpGzipCompress(HttpContext *pContext, char *srcData, int32_t nSrcData, char *destData, int32_t *nDestData,
bool isTheLast) {
int32_t err = 0; int32_t err = 0;
int32_t lastTotalLen = (int32_t)(pContext->gzipStream.total_out); int32_t lastTotalLen = (int32_t)(pContext->gzipStream.total_out);
pContext->gzipStream.next_in = (Bytef *)srcData; pContext->gzipStream.next_in = (Bytef *)srcData;

View File

@ -1,12 +1,10 @@
system sh/stop_dnodes.sh system sh/stop_dnodes.sh
sleep 2000
system sh/deploy.sh -n dnode1 -i 1 system sh/deploy.sh -n dnode1 -i 1
system sh/cfg.sh -n dnode1 -c wallevel -v 0 system sh/cfg.sh -n dnode1 -c wallevel -v 0
system sh/cfg.sh -n dnode1 -c http -v 1 system sh/cfg.sh -n dnode1 -c http -v 1
system sh/cfg.sh -n dnode1 -c httpEnableRecordSql -v 1 system sh/cfg.sh -n dnode1 -c httpEnableRecordSql -v 1
system sh/exec.sh -n dnode1 -s start system sh/exec.sh -n dnode1 -s start
sleep 2000
sql connect sql connect
print ============================ dnode1 start print ============================ dnode1 start