Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
慢慢CG
TDengine
提交
11353abc
T
TDengine
项目概览
慢慢CG
/
TDengine
与 Fork 源项目一致
Fork自
taosdata / TDengine
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
11353abc
编写于
5月 01, 2020
作者:
J
jtao1735
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
draft
上级
f0bbe277
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
376 addition
and
0 deletion
+376
-0
src/cq/src/cqMain.c
src/cq/src/cqMain.c
+167
-0
src/cq/src/vnodeStream.c
src/cq/src/vnodeStream.c
+209
-0
未找到文件。
src/cq/src/cqMain.c
0 → 100644
浏览文件 @
11353abc
/*
* 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
#include "taosmsg.h"
#include "vnode.h"
/* static TAOS *dbConn = NULL; */
void
vnodeCloseStreamCallback
(
void
*
param
);
void
cqOpen
(
void
*
param
,
void
*
tmrId
)
{
SVnodeObj
*
pVnode
=
(
SVnodeObj
*
)
param
;
SMeterObj
*
pObj
;
if
(
pVnode
->
streamRole
==
TSDB_VN_STREAM_STATUS_STOP
)
return
;
if
(
pVnode
->
meterList
==
NULL
)
return
;
taosTmrStopA
(
&
pVnode
->
streamTimer
);
pVnode
->
streamTimer
=
NULL
;
for
(
int
sid
=
0
;
sid
<
pVnode
->
cfg
.
maxSessions
;
++
sid
)
{
pObj
=
pVnode
->
meterList
[
sid
];
if
(
pObj
==
NULL
||
pObj
->
sqlLen
==
0
||
vnodeIsMeterState
(
pObj
,
TSDB_METER_STATE_DROPPING
))
continue
;
dTrace
(
"vid:%d sid:%d id:%s, open stream:%s"
,
pObj
->
vnode
,
sid
,
pObj
->
meterId
,
pObj
->
pSql
);
if
(
pVnode
->
dbConn
==
NULL
)
{
char
db
[
64
]
=
{
0
};
char
user
[
64
]
=
{
0
};
vnodeGetDBFromMeterId
(
pObj
,
db
);
sprintf
(
user
,
"_%s"
,
pVnode
->
cfg
.
acct
);
pVnode
->
dbConn
=
taos_connect
(
NULL
,
user
,
tsInternalPass
,
db
,
0
);
}
if
(
pVnode
->
dbConn
==
NULL
)
{
dError
(
"vid:%d, failed to connect to mgmt node"
,
pVnode
->
vnode
);
taosTmrReset
(
vnodeOpenStreams
,
1000
,
param
,
vnodeTmrCtrl
,
&
pVnode
->
streamTimer
);
return
;
}
if
(
pObj
->
pStream
==
NULL
)
{
pObj
->
pStream
=
taos_open_stream
(
pVnode
->
dbConn
,
pObj
->
pSql
,
vnodeProcessStreamRes
,
pObj
->
lastKey
,
pObj
,
vnodeCloseStreamCallback
);
if
(
pObj
->
pStream
)
pVnode
->
numOfStreams
++
;
}
}
}
// Close all streams in a vnode
void
cqClose
(
SVnodeObj
*
pVnode
)
{
SMeterObj
*
pObj
;
dPrint
(
"vid:%d, stream is closed, old role %s"
,
pVnode
->
vnode
,
taosGetVnodeStreamStatusStr
(
pVnode
->
streamRole
));
// stop stream computing
for
(
int
sid
=
0
;
sid
<
pVnode
->
cfg
.
maxSessions
;
++
sid
)
{
pObj
=
pVnode
->
meterList
[
sid
];
if
(
pObj
==
NULL
)
continue
;
if
(
pObj
->
sqlLen
>
0
&&
pObj
->
pStream
)
{
taos_close_stream
(
pObj
->
pStream
);
pVnode
->
numOfStreams
--
;
}
pObj
->
pStream
=
NULL
;
}
}
void
cqCreate
(
SMeterObj
*
pObj
)
{
if
(
pObj
->
sqlLen
<=
0
)
return
;
SVnodeObj
*
pVnode
=
vnodeList
+
pObj
->
vnode
;
if
(
pVnode
->
streamRole
==
TSDB_VN_STREAM_STATUS_STOP
)
return
;
if
(
pObj
->
pStream
)
return
;
dTrace
(
"vid:%d sid:%d id:%s stream:%s is created"
,
pObj
->
vnode
,
pObj
->
sid
,
pObj
->
meterId
,
pObj
->
pSql
);
if
(
pVnode
->
dbConn
==
NULL
)
{
if
(
pVnode
->
streamTimer
==
NULL
)
taosTmrReset
(
vnodeOpenStreams
,
1000
,
pVnode
,
vnodeTmrCtrl
,
&
pVnode
->
streamTimer
);
}
else
{
pObj
->
pStream
=
taos_open_stream
(
pVnode
->
dbConn
,
pObj
->
pSql
,
vnodeProcessStreamRes
,
pObj
->
lastKey
,
pObj
,
vnodeCloseStreamCallback
);
if
(
pObj
->
pStream
)
pVnode
->
numOfStreams
++
;
}
}
// Close only one stream
void
cqDrop
(
SMeterObj
*
pObj
)
{
SVnodeObj
*
pVnode
=
vnodeList
+
pObj
->
vnode
;
if
(
pObj
->
sqlLen
<=
0
)
return
;
if
(
pObj
->
pStream
)
{
taos_close_stream
(
pObj
->
pStream
);
pVnode
->
numOfStreams
--
;
}
pObj
->
pStream
=
NULL
;
if
(
pVnode
->
numOfStreams
==
0
)
{
taos_close
(
pVnode
->
dbConn
);
pVnode
->
dbConn
=
NULL
;
}
dTrace
(
"vid:%d sid:%d id:%d stream is removed"
,
pObj
->
vnode
,
pObj
->
sid
,
pObj
->
meterId
);
}
void
cqProcessStreamRes
(
void
*
param
,
TAOS_RES
*
tres
,
TAOS_ROW
row
)
{
SMeterObj
*
pObj
=
(
SMeterObj
*
)
param
;
dTrace
(
"vid:%d sid:%d id:%s, stream result is ready"
,
pObj
->
vnode
,
pObj
->
sid
,
pObj
->
meterId
);
// construct data
int32_t
contLen
=
pObj
->
bytesPerPoint
;
char
*
pTemp
=
calloc
(
1
,
sizeof
(
SSubmitMsg
)
+
pObj
->
bytesPerPoint
+
sizeof
(
SVMsgHeader
));
SSubmitMsg
*
pMsg
=
(
SSubmitMsg
*
)(
pTemp
+
sizeof
(
SVMsgHeader
));
pMsg
->
numOfRows
=
htons
(
1
);
char
ncharBuf
[
TSDB_MAX_BYTES_PER_ROW
]
=
{
0
};
int32_t
offset
=
0
;
for
(
int32_t
i
=
0
;
i
<
pObj
->
numOfColumns
;
++
i
)
{
char
*
dst
=
row
[
i
];
if
(
dst
==
NULL
)
{
setNull
(
pMsg
->
payLoad
+
offset
,
pObj
->
schema
[
i
].
type
,
pObj
->
schema
[
i
].
bytes
);
}
else
{
// here, we need to transfer nchar(utf8) to unicode(ucs-4)
if
(
pObj
->
schema
[
i
].
type
==
TSDB_DATA_TYPE_NCHAR
)
{
taosMbsToUcs4
(
row
[
i
],
pObj
->
schema
[
i
].
bytes
,
ncharBuf
,
TSDB_MAX_BYTES_PER_ROW
);
dst
=
ncharBuf
;
}
memcpy
(
pMsg
->
payLoad
+
offset
,
dst
,
pObj
->
schema
[
i
].
bytes
);
}
offset
+=
pObj
->
schema
[
i
].
bytes
;
}
contLen
+=
sizeof
(
SSubmitMsg
);
int32_t
numOfPoints
=
0
;
int32_t
code
=
vnodeInsertPoints
(
pObj
,
(
char
*
)
pMsg
,
contLen
,
TSDB_DATA_SOURCE_SHELL
,
NULL
,
pObj
->
sversion
,
&
numOfPoints
,
taosGetTimestamp
(
vnodeList
[
pObj
->
vnode
].
cfg
.
precision
));
if
(
code
!=
TSDB_CODE_SUCCESS
)
{
dError
(
"vid:%d sid:%d id:%s, failed to insert continuous query results"
,
pObj
->
vnode
,
pObj
->
sid
,
pObj
->
meterId
);
}
assert
(
numOfPoints
>=
0
&&
numOfPoints
<=
1
);
tfree
(
pTemp
);
}
static
void
vnodeGetDBFromMeterId
(
SMeterObj
*
pObj
,
char
*
db
)
{
char
*
st
=
strstr
(
pObj
->
meterId
,
"."
);
char
*
end
=
strstr
(
st
+
1
,
"."
);
memcpy
(
db
,
st
+
1
,
end
-
(
st
+
1
));
}
src/cq/src/vnodeStream.c
0 → 100644
浏览文件 @
11353abc
/*
* 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
#include "taosmsg.h"
#include "vnode.h"
#include "vnodeUtil.h"
#include "vnodeStatus.h"
/* static TAOS *dbConn = NULL; */
void
vnodeCloseStreamCallback
(
void
*
param
);
void
vnodeProcessStreamRes
(
void
*
param
,
TAOS_RES
*
tres
,
TAOS_ROW
row
)
{
SMeterObj
*
pObj
=
(
SMeterObj
*
)
param
;
dTrace
(
"vid:%d sid:%d id:%s, stream result is ready"
,
pObj
->
vnode
,
pObj
->
sid
,
pObj
->
meterId
);
// construct data
int32_t
contLen
=
pObj
->
bytesPerPoint
;
char
*
pTemp
=
calloc
(
1
,
sizeof
(
SSubmitMsg
)
+
pObj
->
bytesPerPoint
+
sizeof
(
SVMsgHeader
));
SSubmitMsg
*
pMsg
=
(
SSubmitMsg
*
)(
pTemp
+
sizeof
(
SVMsgHeader
));
pMsg
->
numOfRows
=
htons
(
1
);
char
ncharBuf
[
TSDB_MAX_BYTES_PER_ROW
]
=
{
0
};
int32_t
offset
=
0
;
for
(
int32_t
i
=
0
;
i
<
pObj
->
numOfColumns
;
++
i
)
{
char
*
dst
=
row
[
i
];
if
(
dst
==
NULL
)
{
setNull
(
pMsg
->
payLoad
+
offset
,
pObj
->
schema
[
i
].
type
,
pObj
->
schema
[
i
].
bytes
);
}
else
{
// here, we need to transfer nchar(utf8) to unicode(ucs-4)
if
(
pObj
->
schema
[
i
].
type
==
TSDB_DATA_TYPE_NCHAR
)
{
taosMbsToUcs4
(
row
[
i
],
pObj
->
schema
[
i
].
bytes
,
ncharBuf
,
TSDB_MAX_BYTES_PER_ROW
);
dst
=
ncharBuf
;
}
memcpy
(
pMsg
->
payLoad
+
offset
,
dst
,
pObj
->
schema
[
i
].
bytes
);
}
offset
+=
pObj
->
schema
[
i
].
bytes
;
}
contLen
+=
sizeof
(
SSubmitMsg
);
int32_t
numOfPoints
=
0
;
int32_t
code
=
vnodeInsertPoints
(
pObj
,
(
char
*
)
pMsg
,
contLen
,
TSDB_DATA_SOURCE_SHELL
,
NULL
,
pObj
->
sversion
,
&
numOfPoints
,
taosGetTimestamp
(
vnodeList
[
pObj
->
vnode
].
cfg
.
precision
));
if
(
code
!=
TSDB_CODE_SUCCESS
)
{
dError
(
"vid:%d sid:%d id:%s, failed to insert continuous query results"
,
pObj
->
vnode
,
pObj
->
sid
,
pObj
->
meterId
);
}
assert
(
numOfPoints
>=
0
&&
numOfPoints
<=
1
);
tfree
(
pTemp
);
}
static
void
vnodeGetDBFromMeterId
(
SMeterObj
*
pObj
,
char
*
db
)
{
char
*
st
=
strstr
(
pObj
->
meterId
,
"."
);
char
*
end
=
strstr
(
st
+
1
,
"."
);
memcpy
(
db
,
st
+
1
,
end
-
(
st
+
1
));
}
void
vnodeOpenStreams
(
void
*
param
,
void
*
tmrId
)
{
SVnodeObj
*
pVnode
=
(
SVnodeObj
*
)
param
;
SMeterObj
*
pObj
;
if
(
pVnode
->
streamRole
==
TSDB_VN_STREAM_STATUS_STOP
)
return
;
if
(
pVnode
->
meterList
==
NULL
)
return
;
taosTmrStopA
(
&
pVnode
->
streamTimer
);
pVnode
->
streamTimer
=
NULL
;
for
(
int
sid
=
0
;
sid
<
pVnode
->
cfg
.
maxSessions
;
++
sid
)
{
pObj
=
pVnode
->
meterList
[
sid
];
if
(
pObj
==
NULL
||
pObj
->
sqlLen
==
0
||
vnodeIsMeterState
(
pObj
,
TSDB_METER_STATE_DROPPING
))
continue
;
dTrace
(
"vid:%d sid:%d id:%s, open stream:%s"
,
pObj
->
vnode
,
sid
,
pObj
->
meterId
,
pObj
->
pSql
);
if
(
pVnode
->
dbConn
==
NULL
)
{
char
db
[
64
]
=
{
0
};
char
user
[
64
]
=
{
0
};
vnodeGetDBFromMeterId
(
pObj
,
db
);
sprintf
(
user
,
"_%s"
,
pVnode
->
cfg
.
acct
);
pVnode
->
dbConn
=
taos_connect
(
NULL
,
user
,
tsInternalPass
,
db
,
0
);
}
if
(
pVnode
->
dbConn
==
NULL
)
{
dError
(
"vid:%d, failed to connect to mgmt node"
,
pVnode
->
vnode
);
taosTmrReset
(
vnodeOpenStreams
,
1000
,
param
,
vnodeTmrCtrl
,
&
pVnode
->
streamTimer
);
return
;
}
if
(
pObj
->
pStream
==
NULL
)
{
pObj
->
pStream
=
taos_open_stream
(
pVnode
->
dbConn
,
pObj
->
pSql
,
vnodeProcessStreamRes
,
pObj
->
lastKey
,
pObj
,
vnodeCloseStreamCallback
);
if
(
pObj
->
pStream
)
pVnode
->
numOfStreams
++
;
}
}
}
void
vnodeCreateStream
(
SMeterObj
*
pObj
)
{
if
(
pObj
->
sqlLen
<=
0
)
return
;
SVnodeObj
*
pVnode
=
vnodeList
+
pObj
->
vnode
;
if
(
pVnode
->
streamRole
==
TSDB_VN_STREAM_STATUS_STOP
)
return
;
if
(
pObj
->
pStream
)
return
;
dTrace
(
"vid:%d sid:%d id:%s stream:%s is created"
,
pObj
->
vnode
,
pObj
->
sid
,
pObj
->
meterId
,
pObj
->
pSql
);
if
(
pVnode
->
dbConn
==
NULL
)
{
if
(
pVnode
->
streamTimer
==
NULL
)
taosTmrReset
(
vnodeOpenStreams
,
1000
,
pVnode
,
vnodeTmrCtrl
,
&
pVnode
->
streamTimer
);
}
else
{
pObj
->
pStream
=
taos_open_stream
(
pVnode
->
dbConn
,
pObj
->
pSql
,
vnodeProcessStreamRes
,
pObj
->
lastKey
,
pObj
,
vnodeCloseStreamCallback
);
if
(
pObj
->
pStream
)
pVnode
->
numOfStreams
++
;
}
}
// Close only one stream
void
vnodeRemoveStream
(
SMeterObj
*
pObj
)
{
SVnodeObj
*
pVnode
=
vnodeList
+
pObj
->
vnode
;
if
(
pObj
->
sqlLen
<=
0
)
return
;
if
(
pObj
->
pStream
)
{
taos_close_stream
(
pObj
->
pStream
);
pVnode
->
numOfStreams
--
;
}
pObj
->
pStream
=
NULL
;
if
(
pVnode
->
numOfStreams
==
0
)
{
taos_close
(
pVnode
->
dbConn
);
pVnode
->
dbConn
=
NULL
;
}
dTrace
(
"vid:%d sid:%d id:%d stream is removed"
,
pObj
->
vnode
,
pObj
->
sid
,
pObj
->
meterId
);
}
// Close all streams in a vnode
void
vnodeCloseStream
(
SVnodeObj
*
pVnode
)
{
SMeterObj
*
pObj
;
dPrint
(
"vid:%d, stream is closed, old role %s"
,
pVnode
->
vnode
,
taosGetVnodeStreamStatusStr
(
pVnode
->
streamRole
));
// stop stream computing
for
(
int
sid
=
0
;
sid
<
pVnode
->
cfg
.
maxSessions
;
++
sid
)
{
pObj
=
pVnode
->
meterList
[
sid
];
if
(
pObj
==
NULL
)
continue
;
if
(
pObj
->
sqlLen
>
0
&&
pObj
->
pStream
)
{
taos_close_stream
(
pObj
->
pStream
);
pVnode
->
numOfStreams
--
;
}
pObj
->
pStream
=
NULL
;
}
}
void
vnodeUpdateStreamRole
(
SVnodeObj
*
pVnode
)
{
/* SMeterObj *pObj; */
int
newRole
=
(
pVnode
->
vnodeStatus
==
TSDB_VN_STATUS_MASTER
)
?
TSDB_VN_STREAM_STATUS_START
:
TSDB_VN_STREAM_STATUS_STOP
;
if
(
newRole
!=
pVnode
->
streamRole
)
{
dPrint
(
"vid:%d, stream role is changed from %s to %s"
,
pVnode
->
vnode
,
taosGetVnodeStreamStatusStr
(
pVnode
->
streamRole
),
taosGetVnodeStreamStatusStr
(
newRole
));
pVnode
->
streamRole
=
newRole
;
if
(
newRole
==
TSDB_VN_STREAM_STATUS_START
)
{
vnodeOpenStreams
(
pVnode
,
NULL
);
}
else
{
vnodeCloseStream
(
pVnode
);
}
}
else
{
dPrint
(
"vid:%d, stream role is keep to %s"
,
pVnode
->
vnode
,
taosGetVnodeStreamStatusStr
(
pVnode
->
streamRole
));
}
}
// Callback function called from client
void
vnodeCloseStreamCallback
(
void
*
param
)
{
SMeterObj
*
pMeter
=
(
SMeterObj
*
)
param
;
SVnodeObj
*
pVnode
=
NULL
;
if
(
pMeter
==
NULL
||
pMeter
->
sqlLen
==
0
)
return
;
pVnode
=
vnodeList
+
pMeter
->
vnode
;
pMeter
->
sqlLen
=
0
;
pMeter
->
pSql
=
NULL
;
pMeter
->
pStream
=
NULL
;
pVnode
->
numOfStreams
--
;
if
(
pVnode
->
numOfStreams
==
0
)
{
taos_close
(
pVnode
->
dbConn
);
pVnode
->
dbConn
=
NULL
;
}
vnodeSaveMeterObjToFile
(
pMeter
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录