2021-12-13 19:45:33 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
2021-04-19 13:47:10 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-12-13 19:45:33 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 13:47:10 +08:00
|
|
|
//
|
2021-12-13 19:45:33 +08:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2021-04-19 13:47:10 +08:00
|
|
|
|
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"
|
2020-09-16 15:21:10 +08:00
|
|
|
"time"
|
2020-11-03 14:53:36 +08:00
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/msgstream"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
2021-12-13 21:15:10 +08:00
|
|
|
"go.uber.org/zap"
|
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-03-12 14:22:09 +08:00
|
|
|
fieldStatsChan chan []*internalpb.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-12 14:22:09 +08:00
|
|
|
func newStatsService(ctx context.Context, replica ReplicaInterface, fieldStatsChan chan []*internalpb.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() {
|
2021-12-23 18:39:11 +08:00
|
|
|
sleepTimeInterval := Params.QueryNodeCfg.StatsPublishInterval
|
2020-11-16 17:01:10 +08:00
|
|
|
|
|
|
|
// start pulsar
|
2022-02-02 00:35:43 +08:00
|
|
|
producerChannels := []string{Params.MsgChannelCfg.QueryNodeStats}
|
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-12-13 21:15:10 +08:00
|
|
|
log.Debug("QueryNode statsService AsProducer succeed", zap.Strings("channels", 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():
|
2021-06-15 12:41:40 +08:00
|
|
|
log.Debug("stats service closed")
|
2020-11-05 10:52:50 +08:00
|
|
|
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-03-12 14:22:09 +08:00
|
|
|
func (sService *statsService) publicStatistic(fieldStats []*internalpb.FieldStats) {
|
2020-12-24 20:55:40 +08:00
|
|
|
segStats := sService.replica.getSegmentStatistics()
|
2020-09-16 15:21:10 +08:00
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
queryNodeStats := internalpb.QueryNodeStats{
|
2021-01-18 19:32:08 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
2021-03-10 14:45:35 +08:00
|
|
|
MsgType: commonpb.MsgType_QueryNodeStats,
|
2021-12-23 18:39:11 +08:00
|
|
|
SourceID: Params.QueryNodeCfg.QueryNodeID,
|
2021-01-18 19:32:08 +08:00
|
|
|
},
|
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-03-25 14:41:46 +08:00
|
|
|
err := sService.statsStream.Produce(&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
|
|
|
}
|