Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DiDi
nightingale
提交
8f40b87b
N
nightingale
项目概览
DiDi
/
nightingale
12 个月 前同步成功
通知
46
Star
7053
Fork
1161
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
nightingale
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
8f40b87b
编写于
4月 09, 2020
作者:
U
Ulric Qin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
collector call transfer rpc use long connection
上级
a74016f7
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
116 addition
and
23 deletion
+116
-23
src/modules/collector/collector.go
src/modules/collector/collector.go
+1
-0
src/modules/collector/sys/funcs/clients.go
src/modules/collector/sys/funcs/clients.go
+45
-0
src/modules/collector/sys/funcs/push.go
src/modules/collector/sys/funcs/push.go
+70
-23
未找到文件。
src/modules/collector/collector.go
浏览文件 @
8f40b87b
...
...
@@ -73,6 +73,7 @@ func main() {
funcs
.
BuildMappers
()
funcs
.
Collect
()
funcs
.
InitRpcClients
()
//插件采集
plugins
.
Detect
()
...
...
src/modules/collector/sys/funcs/clients.go
0 → 100644
浏览文件 @
8f40b87b
package
funcs
import
(
"net/rpc"
"sync"
)
type
RpcClientContainer
struct
{
M
map
[
string
]
*
rpc
.
Client
sync
.
RWMutex
}
var
rpcClients
*
RpcClientContainer
func
InitRpcClients
()
{
rpcClients
=
&
RpcClientContainer
{
M
:
make
(
map
[
string
]
*
rpc
.
Client
),
}
}
func
(
rcc
*
RpcClientContainer
)
Get
(
addr
string
)
*
rpc
.
Client
{
rcc
.
RLock
()
defer
rcc
.
RUnlock
()
client
,
has
:=
rcc
.
M
[
addr
]
if
!
has
{
return
nil
}
return
client
}
// Put 返回的bool表示affected,确实把自己塞进去了
func
(
rcc
*
RpcClientContainer
)
Put
(
addr
string
,
client
*
rpc
.
Client
)
bool
{
rcc
.
Lock
()
defer
rcc
.
Unlock
()
oc
,
has
:=
rcc
.
M
[
addr
]
if
has
&&
oc
!=
nil
{
return
false
}
rcc
.
M
[
addr
]
=
client
return
true
}
src/modules/collector/sys/funcs/push.go
浏览文件 @
8f40b87b
...
...
@@ -44,45 +44,25 @@ func Push(metricItems []*dataobj.MetricValue) error {
items
=
append
(
items
,
item
)
}
var
mh
codec
.
MsgpackHandle
mh
.
MapType
=
reflect
.
TypeOf
(
map
[
string
]
interface
{}(
nil
))
addrs
:=
address
.
GetRPCAddresses
(
"transfer"
)
count
:=
len
(
addrs
)
retry
:=
0
for
{
for
_
,
i
:=
range
rand
.
Perm
(
count
)
{
addr
:=
addrs
[
i
]
var
conn
net
.
Conn
conn
,
err
=
net
.
DialTimeout
(
"tcp"
,
addr
,
time
.
Millisecond
*
3000
)
if
err
!=
nil
{
logger
.
Error
(
"dial transfer err:"
,
err
)
continue
}
var
bufconn
=
struct
{
// bufconn here is a buffered io.ReadWriteCloser
io
.
Closer
*
bufio
.
Reader
*
bufio
.
Writer
}{
conn
,
bufio
.
NewReader
(
conn
),
bufio
.
NewWriter
(
conn
)}
rpcCodec
:=
codec
.
MsgpackSpecRpc
.
ClientCodec
(
bufconn
,
&
mh
)
client
:=
rpc
.
NewClientWithCodec
(
rpcCodec
)
var
reply
dataobj
.
TransferResp
err
=
client
.
Call
(
"Transfer.Push"
,
items
,
&
reply
)
client
.
Close
()
reply
,
err
:=
rpcCall
(
addr
,
items
)
if
err
!=
nil
{
logger
.
Error
(
err
)
continue
}
else
{
if
reply
.
Msg
!=
"ok"
{
err
=
fmt
.
Errorf
(
"some item push err
"
,
reply
)
err
=
fmt
.
Errorf
(
"some item push err
: %s"
,
reply
.
Msg
)
logger
.
Error
(
err
)
}
return
err
}
}
time
.
Sleep
(
time
.
Millisecond
*
500
)
retry
+=
1
...
...
@@ -94,6 +74,73 @@ func Push(metricItems []*dataobj.MetricValue) error {
return
err
}
func
rpcCall
(
addr
string
,
items
[]
*
dataobj
.
MetricValue
)
(
dataobj
.
TransferResp
,
error
)
{
var
reply
dataobj
.
TransferResp
var
err
error
affected
:=
false
client
:=
rpcClients
.
Get
(
addr
)
if
client
==
nil
{
client
,
err
=
rpcClient
(
addr
)
if
err
!=
nil
{
return
reply
,
err
}
else
{
affected
=
rpcClients
.
Put
(
addr
,
client
)
}
}
timeout
:=
time
.
Duration
(
8
)
*
time
.
Second
done
:=
make
(
chan
error
,
1
)
go
func
()
{
err
:=
client
.
Call
(
"Transfer.Push"
,
items
,
&
reply
)
done
<-
err
}()
select
{
case
<-
time
.
After
(
timeout
)
:
logger
.
Warningf
(
"rpc call timeout, transfer addr: %s"
,
addr
)
rpcClients
.
Put
(
addr
,
nil
)
client
.
Close
()
return
reply
,
fmt
.
Errorf
(
"%s rpc call timeout"
,
addr
)
case
err
:=
<-
done
:
if
err
!=
nil
{
rpcClients
.
Put
(
addr
,
nil
)
client
.
Close
()
return
reply
,
err
}
}
if
!
affected
{
// 我尝试把自己这个client塞进map失败,说明已经有一个client塞进去了,那我自己用完了就关闭
client
.
Close
()
}
return
reply
,
nil
}
func
rpcClient
(
addr
string
)
(
*
rpc
.
Client
,
error
)
{
conn
,
err
:=
net
.
DialTimeout
(
"tcp"
,
addr
,
time
.
Second
*
3
)
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"dial transfer %s fail: %v"
,
addr
,
err
)
logger
.
Error
(
err
)
return
nil
,
err
}
var
bufConn
=
struct
{
io
.
Closer
*
bufio
.
Reader
*
bufio
.
Writer
}{
conn
,
bufio
.
NewReader
(
conn
),
bufio
.
NewWriter
(
conn
)}
var
mh
codec
.
MsgpackHandle
mh
.
MapType
=
reflect
.
TypeOf
(
map
[
string
]
interface
{}(
nil
))
rpcCodec
:=
codec
.
MsgpackSpecRpc
.
ClientCodec
(
bufConn
,
&
mh
)
client
:=
rpc
.
NewClientWithCodec
(
rpcCodec
)
return
client
,
nil
}
func
CounterToGauge
(
item
*
dataobj
.
MetricValue
)
error
{
key
:=
item
.
PK
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录