Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DiDi
nightingale
提交
7a1a65c3
N
nightingale
项目概览
DiDi
/
nightingale
10 个月 前同步成功
通知
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,体验更适合开发者的 AI 搜索 >>
提交
7a1a65c3
编写于
4月 21, 2022
作者:
U
Ulric Qin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add queue count control chan number
上级
808fa583
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
23 addition
and
52 deletion
+23
-52
etc/server.conf
etc/server.conf
+3
-1
src/server/writer/writer.go
src/server/writer/writer.go
+20
-51
未找到文件。
etc/server.conf
浏览文件 @
7a1a65c3
...
...
@@ -180,8 +180,10 @@ MaxIdleConns = 100
MaxIdleConnsPerHost
=
10
[
WriterOpt
]
# queue channel count
QueueCount
=
100
# queue max size
QueueMaxSize
=
1
00000
QueueMaxSize
=
2
00000
# once pop samples number from queue
QueuePopSize
=
2000
...
...
src/server/writer/writer.go
浏览文件 @
7a1a65c3
...
...
@@ -4,13 +4,11 @@ import (
"bytes"
"context"
"fmt"
"hash/crc32"
"net"
"net/http"
"sync"
"time"
cmap
"github.com/orcaman/concurrent-map"
"github.com/golang/protobuf/proto"
"github.com/golang/snappy"
"github.com/prometheus/client_golang/api"
...
...
@@ -36,6 +34,7 @@ type Options struct {
}
type
GlobalOpt
struct
{
QueueCount
int
QueueMaxSize
int
QueuePopSize
int
}
...
...
@@ -105,8 +104,7 @@ func (w WriterType) Post(req []byte, headers ...map[string]string) error {
type
WritersType
struct
{
globalOpt
GlobalOpt
backends
map
[
string
]
WriterType
chans
cmap
.
ConcurrentMap
sync
.
RWMutex
chans
map
[
int
]
chan
*
prompb
.
TimeSeries
}
func
(
ws
*
WritersType
)
Put
(
name
string
,
writer
WriterType
)
{
...
...
@@ -116,51 +114,36 @@ func (ws *WritersType) Put(name string, writer WriterType) {
// PushSample Push one sample to chan, hash by ident
// @Author: quzhihao
func
(
ws
*
WritersType
)
PushSample
(
ident
string
,
v
interface
{})
{
if
!
ws
.
chans
.
Has
(
ident
)
{
ws
.
Lock
()
// important: check twice
if
!
ws
.
chans
.
Has
(
ident
)
{
c
:=
make
(
chan
*
prompb
.
TimeSeries
,
Writers
.
globalOpt
.
QueueMaxSize
)
ws
.
chans
.
Set
(
ident
,
c
)
go
ws
.
StartConsumer
(
ident
,
c
)
}
ws
.
Unlock
()
}
hashkey
:=
crc32
.
ChecksumIEEE
([]
byte
(
ident
))
%
uint32
(
ws
.
globalOpt
.
QueueCount
)
c
,
ok
:=
ws
.
chans
.
Get
(
ident
)
c
,
ok
:=
ws
.
chans
[
int
(
hashkey
)]
if
ok
{
ch
:=
c
.
(
chan
*
prompb
.
TimeSeries
)
select
{
case
c
h
<-
v
.
(
*
prompb
.
TimeSeries
)
:
case
c
<-
v
.
(
*
prompb
.
TimeSeries
)
:
default
:
logger
.
Warningf
(
"Write channel(%s) full, current channel size: %d"
,
ident
,
len
(
c
h
))
logger
.
Warningf
(
"Write channel(%s) full, current channel size: %d"
,
ident
,
len
(
c
))
}
}
}
// StartConsumer every ident channel has a consumer, start it
// @Author: quzhihao
func
(
ws
*
WritersType
)
StartConsumer
(
ident
string
,
ch
chan
*
prompb
.
TimeSeries
)
{
func
(
ws
*
WritersType
)
StartConsumer
(
ch
chan
*
prompb
.
TimeSeries
)
{
var
(
batch
=
ws
.
globalOpt
.
QueuePopSize
max
=
ws
.
globalOpt
.
QueueMaxSize
batchCounter
int
closeCounter
int
series
=
make
([]
*
prompb
.
TimeSeries
,
0
,
batch
)
batchCounter
int
)
logger
.
Infof
(
"Starting channel(%s) consumer, max size:%d, batch:%d"
,
ident
,
max
,
batch
)
for
{
select
{
case
item
:=
<-
ch
:
// has data, no need to close
closeCounter
=
0
series
=
append
(
series
,
item
)
batchCounter
++
if
batchCounter
>=
ws
.
globalOpt
.
QueuePopSize
{
ws
.
post
(
ident
,
series
)
ws
.
post
(
series
)
// reset
batchCounter
=
0
...
...
@@ -168,29 +151,11 @@ func (ws *WritersType) StartConsumer(ident string, ch chan *prompb.TimeSeries) {
}
case
<-
time
.
After
(
time
.
Second
)
:
if
len
(
series
)
>
0
{
// has data, no need to close
closeCounter
=
0
ws
.
post
(
ident
,
series
)
ws
.
post
(
series
)
// reset
batchCounter
=
0
series
=
make
([]
*
prompb
.
TimeSeries
,
0
,
batch
)
}
else
{
closeCounter
++
}
if
closeCounter
>
3600
{
logger
.
Infof
(
"Closing channel(%s) reason: no data for an hour"
,
ident
)
ws
.
Lock
()
close
(
ch
)
ws
.
chans
.
Remove
(
ident
)
ws
.
Unlock
()
logger
.
Infof
(
"Closed channel(%s) reason: no data for an hour"
,
ident
)
return
}
}
}
...
...
@@ -198,11 +163,9 @@ func (ws *WritersType) StartConsumer(ident string, ch chan *prompb.TimeSeries) {
// post post series to TSDB
// @Author: quzhihao
func
(
ws
*
WritersType
)
post
(
ident
string
,
series
[]
*
prompb
.
TimeSeries
)
{
// maybe as backend hashstring
headers
:=
map
[
string
]
string
{
"ident"
:
ident
}
func
(
ws
*
WritersType
)
post
(
series
[]
*
prompb
.
TimeSeries
)
{
for
key
:=
range
ws
.
backends
{
go
ws
.
backends
[
key
]
.
Write
(
series
,
headers
)
go
ws
.
backends
[
key
]
.
Write
(
series
)
}
}
...
...
@@ -216,7 +179,13 @@ var Writers = NewWriters()
func
Init
(
opts
[]
Options
,
globalOpt
GlobalOpt
)
error
{
Writers
.
globalOpt
=
globalOpt
Writers
.
chans
=
cmap
.
New
()
Writers
.
chans
=
make
(
map
[
int
]
chan
*
prompb
.
TimeSeries
)
// init channels
for
i
:=
0
;
i
<
globalOpt
.
QueueCount
;
i
++
{
Writers
.
chans
[
i
]
=
make
(
chan
*
prompb
.
TimeSeries
,
Writers
.
globalOpt
.
QueueMaxSize
)
go
Writers
.
StartConsumer
(
Writers
.
chans
[
i
])
}
for
i
:=
0
;
i
<
len
(
opts
);
i
++
{
cli
,
err
:=
api
.
NewClient
(
api
.
Config
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录