提交 5ed28feb 编写于 作者: S Shengliang Guan

[TD-464] show queries and show streams

上级 ebc8c4d8
...@@ -26,7 +26,7 @@ void tscAddIntoSqlList(SSqlObj *pSql); ...@@ -26,7 +26,7 @@ void tscAddIntoSqlList(SSqlObj *pSql);
void tscRemoveFromSqlList(SSqlObj *pSql); void tscRemoveFromSqlList(SSqlObj *pSql);
void tscAddIntoStreamList(SSqlStream *pStream); void tscAddIntoStreamList(SSqlStream *pStream);
void tscRemoveFromStreamList(SSqlStream *pStream, SSqlObj *pSqlObj); void tscRemoveFromStreamList(SSqlStream *pStream, SSqlObj *pSqlObj);
int tscBuildQueryStreamDesc(char *pMsg, STscObj *pObj); int tscBuildQueryStreamDesc(void *pMsg, STscObj *pObj);
void tscKillQuery(STscObj *pObj, uint32_t killId); void tscKillQuery(STscObj *pObj, uint32_t killId);
void tscKillStream(STscObj *pObj, uint32_t killId); void tscKillStream(STscObj *pObj, uint32_t killId);
void tscKillConnection(STscObj *pObj); void tscKillConnection(STscObj *pObj);
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "ttime.h" #include "ttime.h"
#include "ttimer.h" #include "ttimer.h"
#include "tutil.h" #include "tutil.h"
#include "taosmsg.h"
void tscSaveSlowQueryFp(void *handle, void *tmrId); void tscSaveSlowQueryFp(void *handle, void *tmrId);
void *tscSlowQueryConn = NULL; void *tscSlowQueryConn = NULL;
...@@ -96,9 +97,9 @@ void tscSaveSlowQuery(SSqlObj *pSql) { ...@@ -96,9 +97,9 @@ void tscSaveSlowQuery(SSqlObj *pSql) {
char *sql = malloc(200); char *sql = malloc(200);
int len = snprintf(sql, 200, "insert into %s.slowquery values(now, '%s', %" PRId64 ", %" PRId64 ", '", tsMonitorDbName, int len = snprintf(sql, 200, "insert into %s.slowquery values(now, '%s', %" PRId64 ", %" PRId64 ", '", tsMonitorDbName,
pSql->pTscObj->user, pSql->stime, pSql->res.useconds); pSql->pTscObj->user, pSql->stime, pSql->res.useconds);
int sqlLen = snprintf(sql + len, TSDB_SHOW_SQL_LEN, "%s", pSql->sqlstr); int sqlLen = snprintf(sql + len, TSDB_SLOW_QUERY_SQL_LEN, "%s", pSql->sqlstr);
if (sqlLen > TSDB_SHOW_SQL_LEN - 1) { if (sqlLen > TSDB_SLOW_QUERY_SQL_LEN - 1) {
sqlLen = len + TSDB_SHOW_SQL_LEN - 1; sqlLen = len + TSDB_SLOW_QUERY_SQL_LEN - 1;
} else { } else {
sqlLen += len; sqlLen += len;
} }
...@@ -206,72 +207,60 @@ void tscKillStream(STscObj *pObj, uint32_t killId) { ...@@ -206,72 +207,60 @@ void tscKillStream(STscObj *pObj, uint32_t killId) {
taos_close_stream(pStream); taos_close_stream(pStream);
} }
int tscBuildQueryStreamDesc(char *pMsg, STscObj *pObj) { int tscBuildQueryStreamDesc(void *pMsg, STscObj *pObj) {
char * pStart = pMsg; SCMHeartBeatMsg *pHeartbeat = pMsg;
char * pMax = pMsg + TSDB_PAYLOAD_SIZE - 256; pHeartbeat->connId = htonl(pObj->connId);
SQqueryList *pQList = (SQqueryList *)pMsg;
pQList->numOfQueries = 0;
SQueryDesc *pQdesc = (SQueryDesc*)(pMsg + sizeof(SQqueryList)); pHeartbeat->numOfQueries = 0;
SQueryDesc *pQdesc = (SQueryDesc *)pHeartbeat->pData;
// We extract the lock to tscBuildHeartBeatMsg function. // We extract the lock to tscBuildHeartBeatMsg function.
/* pthread_mutex_lock (&pObj->mutex); */
pMsg += sizeof(SQqueryList); //SSqlObj *pSql = pObj->sqlList;
SSqlObj *pSql = pObj->sqlList; //while (pSql) {
while (pSql) {
/* /*
* avoid sqlobj may not be correctly removed from sql list * avoid sqlobj may not be correctly removed from sql list
* e.g., forgetting to free the sql result may cause the sql object still in sql list * e.g., forgetting to free the sql result may cause the sql object still in sql list
*/ */
if (pSql->sqlstr == NULL) { // if (pSql->sqlstr == NULL) {
pSql = pSql->next; // pSql = pSql->next;
continue; // continue;
} // }
strncpy(pQdesc->sql, pSql->sqlstr, TSDB_SHOW_SQL_LEN - 1); strncpy(pQdesc->sql, "select * from d1.t1", TSDB_SHOW_SQL_LEN - 1);
pQdesc->sql[TSDB_SHOW_SQL_LEN - 1] = 0; pQdesc->sql[TSDB_SHOW_SQL_LEN - 1] = 0;
pQdesc->stime = pSql->stime; pQdesc->stime = htobe64(taosGetTimestampMs());
pQdesc->queryId = pSql->queryId; pQdesc->queryId = htonl(12);
pQdesc->useconds = pSql->res.useconds; pQdesc->useconds = htonl(34567);
pQList->numOfQueries++; pHeartbeat->numOfQueries++;
pQdesc++; pQdesc++;
pSql = pSql->next; //pSql = pSql->next;
pMsg += sizeof(SQueryDesc); //}
if (pMsg > pMax) break;
}
SStreamList *pSList = (SStreamList *)pMsg; pHeartbeat->numOfStreams = 0;
pSList->numOfStreams = 0; SStreamDesc *pSdesc = (SStreamDesc *)pQdesc;
SStreamDesc *pSdesc = (SStreamDesc*) (pMsg + sizeof(SStreamList));
pMsg += sizeof(SStreamList); //SSqlStream *pStream = pObj->streamList;
SSqlStream *pStream = pObj->streamList; //while (pStream) {
while (pStream) { strncpy(pSdesc->sql, "select * from d1.s1", TSDB_SHOW_SQL_LEN - 1);
strncpy(pSdesc->sql, pStream->pSql->sqlstr, TSDB_SHOW_SQL_LEN - 1);
pSdesc->sql[TSDB_SHOW_SQL_LEN - 1] = 0; pSdesc->sql[TSDB_SHOW_SQL_LEN - 1] = 0;
pSdesc->streamId = pStream->streamId; pSdesc->streamId = htonl(98);
pSdesc->num = pStream->num; pSdesc->num = htobe64(76543);
pSdesc->useconds = pStream->useconds; pSdesc->useconds = htobe64(21);
pSdesc->stime = pStream->stime - pStream->interval; pSdesc->stime = htobe64(taosGetTimestampMs()-1000);
pSdesc->ctime = pStream->ctime; pSdesc->ctime = htobe64(taosGetTimestampMs());
pSdesc->slidingTime = pStream->slidingTime; pSdesc->slidingTime = htobe64(567);
pSdesc->interval = pStream->interval; pSdesc->interval = htobe64(89);
pSList->numOfStreams++; pHeartbeat->numOfStreams++;
pSdesc++; pSdesc++;
pStream = pStream->next; //pStream = pStream->next;
pMsg += sizeof(SStreamDesc); //}
if (pMsg > pMax) break;
}
/* pthread_mutex_unlock (&pObj->mutex); */
return pMsg - pStart; return pHeartbeat->numOfQueries * sizeof(SQueryDesc) + pHeartbeat->numOfStreams * sizeof(SStreamDesc) + sizeof(SCMHeartBeatMsg);
} }
void tscKillConnection(STscObj *pObj) { void tscKillConnection(STscObj *pObj) {
......
...@@ -1747,52 +1747,45 @@ int tscBuildSTableVgroupMsg(SSqlObj *pSql, SSqlInfo *pInfo) { ...@@ -1747,52 +1747,45 @@ int tscBuildSTableVgroupMsg(SSqlObj *pSql, SSqlInfo *pInfo) {
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
int tscEstimateHeartBeatMsgLength(SSqlObj *pSql) { int tscBuildHeartBeatMsg(SSqlObj *pSql, SSqlInfo *pInfo) {
int size = 0; SSqlCmd *pCmd = &pSql->cmd;
STscObj *pObj = pSql->pTscObj; STscObj *pObj = pSql->pTscObj;
size += tsRpcHeadSize; pthread_mutex_lock(&pObj->mutex);
size += sizeof(SQqueryList);
int32_t numOfQueries = 0;
SSqlObj *tpSql = pObj->sqlList; SSqlObj *tpSql = pObj->sqlList;
while (tpSql) { while (tpSql) {
size += sizeof(SQueryDesc);
tpSql = tpSql->next; tpSql = tpSql->next;
numOfQueries++;
} }
size += sizeof(SStreamList); int32_t numOfStreams = 0;
SSqlStream *pStream = pObj->streamList; SSqlStream *pStream = pObj->streamList;
while (pStream) { while (pStream) {
size += sizeof(SStreamDesc);
pStream = pStream->next; pStream = pStream->next;
numOfStreams++;
} }
return size + TSDB_EXTRA_PAYLOAD_SIZE; // ==>
} numOfQueries = 1;
numOfStreams = 1;
int tscBuildHeartBeatMsg(SSqlObj *pSql, SSqlInfo *pInfo) { int size = numOfQueries * sizeof(SQueryDesc) + numOfStreams * sizeof(SStreamDesc) + sizeof(SCMHeartBeatMsg) + 100;
SSqlCmd *pCmd = &pSql->cmd;
STscObj *pObj = pSql->pTscObj;
pthread_mutex_lock(&pObj->mutex);
int size = tscEstimateHeartBeatMsgLength(pSql);
if (TSDB_CODE_SUCCESS != tscAllocPayload(pCmd, size)) { if (TSDB_CODE_SUCCESS != tscAllocPayload(pCmd, size)) {
pthread_mutex_unlock(&pObj->mutex); pthread_mutex_unlock(&pObj->mutex);
tscError("%p failed to malloc for heartbeat msg", pSql); tscError("%p failed to malloc for heartbeat msg", pSql);
return -1; return -1;
} }
SCMHeartBeatMsg *pHeartbeat = (SCMHeartBeatMsg*)pCmd->payload; SCMHeartBeatMsg *pHeartbeat = (SCMHeartBeatMsg *)pCmd->payload;
pHeartbeat->connId = htonl(pSql->pTscObj->connId); int msgLen = tscBuildQueryStreamDesc(pHeartbeat, pObj);
int msgLen = tscBuildQueryStreamDesc((char*)pHeartbeat + sizeof(pHeartbeat->connId), pObj);
pthread_mutex_unlock(&pObj->mutex); pthread_mutex_unlock(&pObj->mutex);
pCmd->payloadLen = msgLen + sizeof(pHeartbeat->connId); pCmd->payloadLen = msgLen;
pCmd->msgType = TSDB_MSG_TYPE_CM_HEARTBEAT; pCmd->msgType = TSDB_MSG_TYPE_CM_HEARTBEAT;
assert(msgLen + minMsgSize() <= size);
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
......
...@@ -231,7 +231,8 @@ void tsDataSwap(void *pLeft, void *pRight, int32_t type, int32_t size); ...@@ -231,7 +231,8 @@ void tsDataSwap(void *pLeft, void *pRight, int32_t type, int32_t size);
#define TSDB_SHELL_VNODE_BITS 24 #define TSDB_SHELL_VNODE_BITS 24
#define TSDB_SHELL_SID_MASK 0xFF #define TSDB_SHELL_SID_MASK 0xFF
#define TSDB_HTTP_TOKEN_LEN 20 #define TSDB_HTTP_TOKEN_LEN 20
#define TSDB_SHOW_SQL_LEN 512 #define TSDB_SHOW_SQL_LEN 64
#define TSDB_SLOW_QUERY_SQL_LEN 512
#define TSDB_METER_STATE_OFFLINE 0 #define TSDB_METER_STATE_OFFLINE 0
#define TSDB_METER_STATE_ONLLINE 1 #define TSDB_METER_STATE_ONLLINE 1
......
...@@ -707,18 +707,11 @@ typedef struct { ...@@ -707,18 +707,11 @@ typedef struct {
int64_t interval; int64_t interval;
} SStreamDesc; } SStreamDesc;
typedef struct {
int32_t numOfQueries;
} SQqueryList;
typedef struct {
int32_t numOfStreams;
} SStreamList;
typedef struct { typedef struct {
uint32_t connId; uint32_t connId;
SQqueryList qlist; int32_t numOfQueries;
SStreamList slist; int32_t numOfStreams;
char pData[];
} SCMHeartBeatMsg; } SCMHeartBeatMsg;
typedef struct { typedef struct {
......
...@@ -182,8 +182,6 @@ typedef struct SUserObj { ...@@ -182,8 +182,6 @@ typedef struct SUserObj {
int8_t updateEnd[1]; int8_t updateEnd[1];
int32_t refCount; int32_t refCount;
struct SAcctObj * pAcct; struct SAcctObj * pAcct;
SQqueryList * pQList; // query list
SStreamList * pSList; // stream list
} SUserObj; } SUserObj;
typedef struct { typedef struct {
......
...@@ -29,6 +29,12 @@ typedef struct { ...@@ -29,6 +29,12 @@ typedef struct {
uint32_t connId; uint32_t connId;
uint64_t stime; uint64_t stime;
uint64_t lastAccess; uint64_t lastAccess;
uint32_t queryId;
uint32_t streamId;
int32_t numOfQueries;
int32_t numOfStreams;
SStreamDesc *pStreams;
SQueryDesc * pQueries;
} SConnObj; } SConnObj;
int32_t mnodeInitProfile(); int32_t mnodeInitProfile();
...@@ -37,6 +43,7 @@ void mnodeCleanupProfile(); ...@@ -37,6 +43,7 @@ void mnodeCleanupProfile();
SConnObj *mnodeCreateConn(char *user, uint32_t ip, uint16_t port); SConnObj *mnodeCreateConn(char *user, uint32_t ip, uint16_t port);
SConnObj *mnodeAccquireConn(uint32_t connId, char *user, uint32_t ip, uint16_t port); SConnObj *mnodeAccquireConn(uint32_t connId, char *user, uint32_t ip, uint16_t port);
void mnodeReleaseConn(SConnObj *pConn); void mnodeReleaseConn(SConnObj *pConn);
int32_t mnodeSaveQueryStreamList(SConnObj *pConn, SCMHeartBeatMsg *pHBMsg);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
此差异已折叠。
...@@ -243,23 +243,27 @@ static int32_t mnodeProcessHeartBeatMsg(SMnodeMsg *pMsg) { ...@@ -243,23 +243,27 @@ static int32_t mnodeProcessHeartBeatMsg(SMnodeMsg *pMsg) {
// pHBRsp->killConnection = 1; // pHBRsp->killConnection = 1;
} else { } else {
pHBRsp->connId = htonl(pConn->connId); pHBRsp->connId = htonl(pConn->connId);
mnodeSaveQueryStreamList(pConn, pHBMsg);
if (pConn->killed != 0) { if (pConn->killed != 0) {
pHBRsp->killConnection = 1; pHBRsp->killConnection = 1;
} }
if (pConn->streamId != 0) {
pHBRsp->streamId = htonl(pConn->streamId);
pConn->streamId = 0;
}
if (pConn->queryId != 0) {
pHBRsp->queryId = htonl(pConn->queryId);
pConn->queryId = 0;
}
} }
pHBRsp->onlineDnodes = htonl(mnodeGetOnlinDnodesNum()); pHBRsp->onlineDnodes = htonl(mnodeGetOnlinDnodesNum());
pHBRsp->totalDnodes = htonl(mnodeGetDnodesNum()); pHBRsp->totalDnodes = htonl(mnodeGetDnodesNum());
mnodeGetMnodeIpSetForShell(&pHBRsp->ipList); mnodeGetMnodeIpSetForShell(&pHBRsp->ipList);
/*
* TODO
* Dispose kill stream or kill query message
*/
// pHBRsp->queryId = 0;
// pHBRsp->streamId = 0;
// pHBRsp->killConnection = 0;
pMsg->rpcRsp.rsp = pHBRsp; pMsg->rpcRsp.rsp = pHBRsp;
pMsg->rpcRsp.len = sizeof(SCMHeartBeatRsp); pMsg->rpcRsp.len = sizeof(SCMHeartBeatRsp);
......
...@@ -183,7 +183,7 @@ static void dnodeBuildMonitorSql(char *sql, int32_t cmd) { ...@@ -183,7 +183,7 @@ static void dnodeBuildMonitorSql(char *sql, int32_t cmd) {
snprintf(sql, SQL_LENGTH, snprintf(sql, SQL_LENGTH,
"create table if not exists %s.slowquery(ts timestamp, username " "create table if not exists %s.slowquery(ts timestamp, username "
"binary(%d), created_time timestamp, time bigint, sql binary(%d))", "binary(%d), created_time timestamp, time bigint, sql binary(%d))",
tsMonitorDbName, TSDB_TABLE_ID_LEN, TSDB_SHOW_SQL_LEN); tsMonitorDbName, TSDB_TABLE_ID_LEN, TSDB_SLOW_QUERY_SQL_LEN);
} else if (cmd == MONITOR_CMD_CREATE_TB_LOG) { } else if (cmd == MONITOR_CMD_CREATE_TB_LOG) {
snprintf(sql, SQL_LENGTH, snprintf(sql, SQL_LENGTH,
"create table if not exists %s.log(ts timestamp, level tinyint, " "create table if not exists %s.log(ts timestamp, level tinyint, "
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册