2021-01-16 10:12:14 +08:00
|
|
|
package querynode
|
2020-09-16 15:21:10 +08:00
|
|
|
|
|
|
|
import (
|
2020-11-05 10:52:50 +08:00
|
|
|
"context"
|
2021-03-05 18:16:50 +08:00
|
|
|
"strings"
|
2020-09-16 15:21:10 +08:00
|
|
|
"time"
|
2020-11-03 14:53:36 +08:00
|
|
|
|
2021-03-05 09:21:35 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/log"
|
2020-11-05 10:52:50 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream"
|
2021-01-18 19:32:08 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
|
2020-09-16 15:21:10 +08:00
|
|
|
)
|
|
|
|
|
2020-11-09 16:27:11 +08:00
|
|
|
type statsService struct {
|
2020-12-24 20:55:40 +08:00
|
|
|
ctx context.Context
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
replica ReplicaInterface
|
2020-12-24 20:55:40 +08:00
|
|
|
|
2021-01-18 19:32:08 +08:00
|
|
|
fieldStatsChan chan []*internalpb2.FieldStats
|
2020-12-24 20:55:40 +08:00
|
|
|
statsStream msgstream.MsgStream
|
2021-02-08 14:30:54 +08:00
|
|
|
msFactory msgstream.Factory
|
2020-11-05 10:52:50 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func newStatsService(ctx context.Context, replica ReplicaInterface, fieldStatsChan chan []*internalpb2.FieldStats, factory msgstream.Factory) *statsService {
|
2020-10-24 10:45:57 +08:00
|
|
|
|
2020-11-09 16:27:11 +08:00
|
|
|
return &statsService{
|
2020-12-24 20:55:40 +08:00
|
|
|
ctx: ctx,
|
|
|
|
|
|
|
|
replica: replica,
|
|
|
|
|
|
|
|
fieldStatsChan: fieldStatsChan,
|
|
|
|
statsStream: nil,
|
2021-02-08 14:30:54 +08:00
|
|
|
|
|
|
|
msFactory: factory,
|
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
|
2020-11-16 17:01:10 +08:00
|
|
|
|
|
|
|
// start pulsar
|
2020-12-10 16:31:09 +08:00
|
|
|
producerChannels := []string{Params.StatsChannelName}
|
2020-11-16 17:01:10 +08:00
|
|
|
|
2021-02-08 14:30:54 +08:00
|
|
|
statsStream, _ := sService.msFactory.NewMsgStream(sService.ctx)
|
2021-02-04 14:37:12 +08:00
|
|
|
statsStream.AsProducer(producerChannels)
|
2021-03-05 18:16:50 +08:00
|
|
|
log.Debug("querynode AsProducer: " + strings.Join(producerChannels, ", "))
|
2020-11-16 17:01:10 +08:00
|
|
|
|
|
|
|
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-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 20:55:40 +08:00
|
|
|
sService.publicStatistic(nil)
|
|
|
|
case fieldStats := <-sService.fieldStatsChan:
|
|
|
|
sService.publicStatistic(fieldStats)
|
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
|
|
|
}
|
|
|
|
|
2021-01-18 19:32:08 +08:00
|
|
|
func (sService *statsService) publicStatistic(fieldStats []*internalpb2.FieldStats) {
|
2020-12-24 20:55:40 +08:00
|
|
|
segStats := sService.replica.getSegmentStatistics()
|
2020-09-16 15:21:10 +08:00
|
|
|
|
2021-01-18 19:32:08 +08:00
|
|
|
queryNodeStats := internalpb2.QueryNodeStats{
|
|
|
|
Base: &commonpb.MsgBase{
|
2021-03-10 14:45:35 +08:00
|
|
|
MsgType: commonpb.MsgType_QueryNodeStats,
|
2021-01-18 19:32:08 +08:00
|
|
|
SourceID: Params.QueryNodeID,
|
|
|
|
},
|
2020-12-24 20:55:40 +08:00
|
|
|
SegStats: segStats,
|
|
|
|
FieldStats: fieldStats,
|
|
|
|
}
|
2020-09-16 15:21:10 +08:00
|
|
|
|
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 20:55:40 +08:00
|
|
|
QueryNodeStats: queryNodeStats,
|
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
|
|
|
}
|
2021-02-24 09:48:17 +08:00
|
|
|
err := sService.statsStream.Produce(context.TODO(), &msgPack)
|
2020-11-16 17:01:10 +08:00
|
|
|
if err != nil {
|
2021-03-05 09:21:35 +08:00
|
|
|
log.Error(err.Error())
|
2020-11-16 17:01:10 +08:00
|
|
|
}
|
2020-09-16 15:21:10 +08:00
|
|
|
}
|