2020-11-26 16:01:31 +08:00
|
|
|
package querynode
|
2020-09-16 15:21:10 +08:00
|
|
|
|
|
|
|
import (
|
2020-11-05 10:52:50 +08:00
|
|
|
"context"
|
2020-09-16 15:21:10 +08:00
|
|
|
"fmt"
|
2020-11-16 17:01:10 +08:00
|
|
|
"log"
|
2020-09-16 15:21:10 +08:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2020-11-03 14:53:36 +08:00
|
|
|
|
2020-11-05 10:52:50 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream"
|
2020-11-03 14:53:36 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb"
|
2020-09-16 15:21:10 +08:00
|
|
|
)
|
|
|
|
|
2020-11-09 16:27:11 +08:00
|
|
|
type statsService struct {
|
2020-12-24 16:19:42 +08:00
|
|
|
ctx context.Context
|
|
|
|
statsStream msgstream.MsgStream
|
|
|
|
replica collectionReplica
|
2020-11-05 10:52:50 +08:00
|
|
|
}
|
|
|
|
|
2020-12-24 16:19:42 +08:00
|
|
|
func newStatsService(ctx context.Context, replica collectionReplica) *statsService {
|
2020-10-24 10:45:57 +08:00
|
|
|
|
2020-11-09 16:27:11 +08:00
|
|
|
return &statsService{
|
2020-12-24 16:19:42 +08:00
|
|
|
ctx: ctx,
|
|
|
|
statsStream: nil,
|
|
|
|
replica: replica,
|
2020-11-05 10:52:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 16:27:11 +08:00
|
|
|
func (sService *statsService) start() {
|
2020-12-10 16:31:09 +08:00
|
|
|
sleepTimeInterval := Params.StatsPublishInterval
|
|
|
|
receiveBufSize := Params.StatsReceiveBufSize
|
2020-11-16 17:01:10 +08:00
|
|
|
|
|
|
|
// start pulsar
|
2020-12-10 16:31:09 +08:00
|
|
|
msgStreamURL := Params.PulsarAddress
|
|
|
|
producerChannels := []string{Params.StatsChannelName}
|
2020-11-16 17:01:10 +08:00
|
|
|
|
|
|
|
statsStream := msgstream.NewPulsarMsgStream(sService.ctx, receiveBufSize)
|
2020-11-18 19:46:18 +08:00
|
|
|
statsStream.SetPulsarClient(msgStreamURL)
|
2020-11-16 17:01:10 +08:00
|
|
|
statsStream.CreatePulsarProducers(producerChannels)
|
|
|
|
|
|
|
|
var statsMsgStream msgstream.MsgStream = statsStream
|
|
|
|
|
2020-12-08 14:41:04 +08:00
|
|
|
sService.statsStream = statsMsgStream
|
|
|
|
sService.statsStream.Start()
|
2020-11-16 17:01:10 +08:00
|
|
|
|
|
|
|
// start service
|
2020-11-18 17:32:52 +08:00
|
|
|
fmt.Println("do segments statistic in ", strconv.Itoa(sleepTimeInterval), "ms")
|
2020-11-05 10:52:50 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sService.ctx.Done():
|
|
|
|
return
|
2020-11-18 17:32:52 +08:00
|
|
|
case <-time.After(time.Duration(sleepTimeInterval) * time.Millisecond):
|
2020-12-24 16:19:42 +08:00
|
|
|
sService.sendSegmentStatistic()
|
2020-11-05 10:52:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-16 15:21:10 +08:00
|
|
|
|
2020-11-24 16:12:39 +08:00
|
|
|
func (sService *statsService) close() {
|
2020-12-08 14:41:04 +08:00
|
|
|
if sService.statsStream != nil {
|
|
|
|
sService.statsStream.Close()
|
|
|
|
}
|
2020-11-24 16:12:39 +08:00
|
|
|
}
|
|
|
|
|
2020-12-24 16:19:42 +08:00
|
|
|
func (sService *statsService) sendSegmentStatistic() {
|
|
|
|
statisticData := sService.replica.getSegmentStatistics()
|
2020-09-16 15:21:10 +08:00
|
|
|
|
2020-12-24 16:19:42 +08:00
|
|
|
// fmt.Println("Publish segment statistic")
|
|
|
|
// fmt.Println(statisticData)
|
|
|
|
sService.publicStatistic(statisticData)
|
|
|
|
}
|
2020-09-16 15:21:10 +08:00
|
|
|
|
2020-12-24 16:19:42 +08:00
|
|
|
func (sService *statsService) publicStatistic(statistic *internalpb.QueryNodeStats) {
|
2020-12-11 11:29:54 +08:00
|
|
|
var msg msgstream.TsMsg = &msgstream.QueryNodeStatsMsg{
|
2020-11-16 17:01:10 +08:00
|
|
|
BaseMsg: msgstream.BaseMsg{
|
2020-11-30 19:38:23 +08:00
|
|
|
HashValues: []uint32{0},
|
2020-11-16 17:01:10 +08:00
|
|
|
},
|
2020-12-24 16:19:42 +08:00
|
|
|
QueryNodeStats: *statistic,
|
2020-11-16 17:01:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var msgPack = msgstream.MsgPack{
|
2020-11-17 14:10:07 +08:00
|
|
|
Msgs: []msgstream.TsMsg{msg},
|
2020-11-16 17:01:10 +08:00
|
|
|
}
|
2020-12-08 14:41:04 +08:00
|
|
|
err := sService.statsStream.Produce(&msgPack)
|
2020-11-16 17:01:10 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2020-09-16 15:21:10 +08:00
|
|
|
}
|