2021-12-13 11:45:33 +00: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 05:47:10 +00:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-12-13 11:45:33 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 05:47:10 +00:00
|
|
|
//
|
2021-12-13 11:45:33 +00: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 05:47:10 +00:00
|
|
|
|
2021-01-16 02:12:14 +00:00
|
|
|
package querynode
|
2020-09-16 07:21:10 +00:00
|
|
|
|
|
|
|
import (
|
2020-11-05 02:52:50 +00:00
|
|
|
"context"
|
2020-09-16 07:21:10 +00:00
|
|
|
"time"
|
2020-11-03 06:53:36 +00:00
|
|
|
|
2021-04-22 06:45:57 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
2022-03-03 13:57:56 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/mq/msgstream"
|
2021-04-22 06:45:57 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
2021-12-13 13:15:10 +00:00
|
|
|
"go.uber.org/zap"
|
2020-09-16 07:21:10 +00:00
|
|
|
)
|
|
|
|
|
2020-11-09 08:27:11 +00:00
|
|
|
type statsService struct {
|
2020-12-24 12:55:40 +00:00
|
|
|
ctx context.Context
|
|
|
|
|
2021-03-05 08:52:45 +00:00
|
|
|
replica ReplicaInterface
|
2020-12-24 12:55:40 +00:00
|
|
|
|
2022-02-08 13:57:46 +00:00
|
|
|
statsStream msgstream.MsgStream
|
|
|
|
msFactory msgstream.Factory
|
2020-11-05 02:52:50 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 13:57:46 +00:00
|
|
|
func newStatsService(ctx context.Context, replica ReplicaInterface, factory msgstream.Factory) *statsService {
|
2020-10-24 02:45:57 +00:00
|
|
|
|
2020-11-09 08:27:11 +00:00
|
|
|
return &statsService{
|
2022-02-08 13:57:46 +00:00
|
|
|
ctx: ctx,
|
|
|
|
replica: replica,
|
|
|
|
statsStream: nil,
|
|
|
|
msFactory: factory,
|
2020-11-05 02:52:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 08:27:11 +00:00
|
|
|
func (sService *statsService) start() {
|
2021-12-23 10:39:11 +00:00
|
|
|
sleepTimeInterval := Params.QueryNodeCfg.StatsPublishInterval
|
2020-11-16 09:01:10 +00:00
|
|
|
|
|
|
|
// start pulsar
|
2022-03-04 03:17:56 +00:00
|
|
|
producerChannels := []string{Params.CommonCfg.QueryNodeStats}
|
2020-11-16 09:01:10 +00:00
|
|
|
|
2021-02-08 06:30:54 +00:00
|
|
|
statsStream, _ := sService.msFactory.NewMsgStream(sService.ctx)
|
2021-02-04 06:37:12 +00:00
|
|
|
statsStream.AsProducer(producerChannels)
|
2021-12-13 13:15:10 +00:00
|
|
|
log.Debug("QueryNode statsService AsProducer succeed", zap.Strings("channels", producerChannels))
|
2020-11-16 09:01:10 +00:00
|
|
|
|
|
|
|
var statsMsgStream msgstream.MsgStream = statsStream
|
|
|
|
|
2020-12-08 06:41:04 +00:00
|
|
|
sService.statsStream = statsMsgStream
|
|
|
|
sService.statsStream.Start()
|
2020-11-16 09:01:10 +00:00
|
|
|
|
|
|
|
// start service
|
2020-11-05 02:52:50 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sService.ctx.Done():
|
2021-06-15 04:41:40 +00:00
|
|
|
log.Debug("stats service closed")
|
2020-11-05 02:52:50 +00:00
|
|
|
return
|
2020-11-18 09:32:52 +00:00
|
|
|
case <-time.After(time.Duration(sleepTimeInterval) * time.Millisecond):
|
2020-12-24 12:55:40 +00:00
|
|
|
sService.publicStatistic(nil)
|
2020-11-05 02:52:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-16 07:21:10 +00:00
|
|
|
|
2020-11-24 08:12:39 +00:00
|
|
|
func (sService *statsService) close() {
|
2020-12-08 06:41:04 +00:00
|
|
|
if sService.statsStream != nil {
|
|
|
|
sService.statsStream.Close()
|
|
|
|
}
|
2020-11-24 08:12:39 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 06:22:09 +00:00
|
|
|
func (sService *statsService) publicStatistic(fieldStats []*internalpb.FieldStats) {
|
2020-12-24 12:55:40 +00:00
|
|
|
segStats := sService.replica.getSegmentStatistics()
|
2020-09-16 07:21:10 +00:00
|
|
|
|
2021-03-12 06:22:09 +00:00
|
|
|
queryNodeStats := internalpb.QueryNodeStats{
|
2021-01-18 11:32:08 +00:00
|
|
|
Base: &commonpb.MsgBase{
|
2021-03-10 06:45:35 +00:00
|
|
|
MsgType: commonpb.MsgType_QueryNodeStats,
|
2021-12-23 10:39:11 +00:00
|
|
|
SourceID: Params.QueryNodeCfg.QueryNodeID,
|
2021-01-18 11:32:08 +00:00
|
|
|
},
|
2020-12-24 12:55:40 +00:00
|
|
|
SegStats: segStats,
|
|
|
|
FieldStats: fieldStats,
|
|
|
|
}
|
2020-09-16 07:21:10 +00:00
|
|
|
|
2020-12-11 03:29:54 +00:00
|
|
|
var msg msgstream.TsMsg = &msgstream.QueryNodeStatsMsg{
|
2020-11-16 09:01:10 +00:00
|
|
|
BaseMsg: msgstream.BaseMsg{
|
2020-11-30 11:38:23 +00:00
|
|
|
HashValues: []uint32{0},
|
2020-11-16 09:01:10 +00:00
|
|
|
},
|
2020-12-24 12:55:40 +00:00
|
|
|
QueryNodeStats: queryNodeStats,
|
2020-11-16 09:01:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var msgPack = msgstream.MsgPack{
|
2020-11-17 06:10:07 +00:00
|
|
|
Msgs: []msgstream.TsMsg{msg},
|
2020-11-16 09:01:10 +00:00
|
|
|
}
|
2021-03-25 06:41:46 +00:00
|
|
|
err := sService.statsStream.Produce(&msgPack)
|
2020-11-16 09:01:10 +00:00
|
|
|
if err != nil {
|
2021-03-05 01:21:35 +00:00
|
|
|
log.Error(err.Error())
|
2020-11-16 09:01:10 +00:00
|
|
|
}
|
2020-09-16 07:21:10 +00:00
|
|
|
}
|