2021-07-12 11:03:52 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// 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-09-10 11:26:00 +08:00
|
|
|
|
2021-07-12 11:03:52 +08:00
|
|
|
package datacoord
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/milvus-io/milvus/internal/metrics"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
|
|
|
)
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// ClusterStore is the interface stores DataNodes information
|
2021-07-12 11:03:52 +08:00
|
|
|
type ClusterStore interface {
|
|
|
|
GetNodes() []*NodeInfo
|
|
|
|
SetNode(nodeID UniqueID, node *NodeInfo)
|
|
|
|
DeleteNode(nodeID UniqueID)
|
|
|
|
GetNode(nodeID UniqueID) *NodeInfo
|
|
|
|
SetClient(nodeID UniqueID, client types.DataNode)
|
|
|
|
SetWatched(nodeID UniqueID, channelsName []string)
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// NodeInfo wrapper struct for storing DataNode information
|
|
|
|
// and related controlling struct
|
2021-07-12 11:03:52 +08:00
|
|
|
type NodeInfo struct {
|
2021-07-15 16:38:31 +08:00
|
|
|
Info *datapb.DataNodeInfo
|
2021-07-12 11:03:52 +08:00
|
|
|
eventCh chan *NodeEvent
|
|
|
|
client types.DataNode
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// eventChBuffer magic number for channel buffer size in NodeInfo
|
2021-07-12 11:03:52 +08:00
|
|
|
const eventChBuffer = 1024
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// NodeEvent data node event struct
|
2021-07-12 11:03:52 +08:00
|
|
|
type NodeEvent struct {
|
|
|
|
Type NodeEventType
|
|
|
|
Req interface{}
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// NewNodeInfo helper function to create a NodeInfo from provided datapb.DataNodeInfo
|
2021-07-12 11:03:52 +08:00
|
|
|
func NewNodeInfo(ctx context.Context, info *datapb.DataNodeInfo) *NodeInfo {
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
return &NodeInfo{
|
2021-07-15 16:38:31 +08:00
|
|
|
Info: info,
|
2021-07-12 11:03:52 +08:00
|
|
|
eventCh: make(chan *NodeEvent, eventChBuffer),
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// ShadowClone shadow clones a NodeInfo
|
|
|
|
// note that info, eventCh, etc is not created again
|
2021-07-12 11:03:52 +08:00
|
|
|
func (n *NodeInfo) ShadowClone(opts ...NodeOpt) *NodeInfo {
|
|
|
|
cloned := &NodeInfo{
|
2021-07-15 16:38:31 +08:00
|
|
|
Info: n.Info,
|
2021-07-12 11:03:52 +08:00
|
|
|
eventCh: n.eventCh,
|
|
|
|
client: n.client,
|
|
|
|
ctx: n.ctx,
|
|
|
|
cancel: n.cancel,
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(cloned)
|
|
|
|
}
|
|
|
|
return cloned
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// Clone "deep" clone a NodeInfo
|
|
|
|
// note that ONLY `info` field is deep copied
|
|
|
|
// parameter opts is applied in sequence to clone NodeInfo
|
2021-07-12 11:03:52 +08:00
|
|
|
func (n *NodeInfo) Clone(opts ...NodeOpt) *NodeInfo {
|
2021-07-15 16:38:31 +08:00
|
|
|
info := proto.Clone(n.Info).(*datapb.DataNodeInfo)
|
2021-07-12 11:03:52 +08:00
|
|
|
cloned := &NodeInfo{
|
2021-07-15 16:38:31 +08:00
|
|
|
Info: info,
|
2021-07-12 11:03:52 +08:00
|
|
|
eventCh: n.eventCh,
|
|
|
|
client: n.client,
|
|
|
|
ctx: n.ctx,
|
|
|
|
cancel: n.cancel,
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(cloned)
|
|
|
|
}
|
|
|
|
return cloned
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// GetEventChannel returns event channel
|
2021-07-12 11:03:52 +08:00
|
|
|
func (n *NodeInfo) GetEventChannel() chan *NodeEvent {
|
|
|
|
return n.eventCh
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// GetClient returns client
|
2021-07-12 11:03:52 +08:00
|
|
|
func (n *NodeInfo) GetClient() types.DataNode {
|
|
|
|
return n.client
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// Dispose stops the data node client and calls cancel
|
2021-07-12 11:03:52 +08:00
|
|
|
func (n *NodeInfo) Dispose() {
|
|
|
|
defer n.cancel()
|
|
|
|
if n.client != nil {
|
|
|
|
n.client.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 23:48:54 +08:00
|
|
|
// make sure NodesInfo implements ClusterStore
|
|
|
|
var _ ClusterStore = (*NodesInfo)(nil)
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// NodesInfo wraps a map UniqueID -> NodeInfo
|
|
|
|
// implements ClusterStore interface
|
|
|
|
// not lock related field is required so all operations shall be protected outside
|
2021-07-12 11:03:52 +08:00
|
|
|
type NodesInfo struct {
|
|
|
|
nodes map[UniqueID]*NodeInfo
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// NewNodesInfo helper function creates a NodesInfo
|
2021-07-12 11:03:52 +08:00
|
|
|
func NewNodesInfo() *NodesInfo {
|
|
|
|
c := &NodesInfo{
|
|
|
|
nodes: make(map[UniqueID]*NodeInfo),
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// GetNodes returns nodes list in NodesInfo
|
2021-07-12 11:03:52 +08:00
|
|
|
func (c *NodesInfo) GetNodes() []*NodeInfo {
|
|
|
|
nodes := make([]*NodeInfo, 0, len(c.nodes))
|
|
|
|
for _, node := range c.nodes {
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// SetNode sets a NodeInfo with provided UniqueID
|
2021-07-12 11:03:52 +08:00
|
|
|
func (c *NodesInfo) SetNode(nodeID UniqueID, node *NodeInfo) {
|
|
|
|
c.nodes[nodeID] = node
|
|
|
|
metrics.DataCoordDataNodeList.WithLabelValues("online").Inc()
|
|
|
|
metrics.DataCoordDataNodeList.WithLabelValues("offline").Dec()
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// DeleteNode deletes a NodeInfo with provided UniqueID
|
2021-07-12 11:03:52 +08:00
|
|
|
func (c *NodesInfo) DeleteNode(nodeID UniqueID) {
|
|
|
|
delete(c.nodes, nodeID)
|
|
|
|
metrics.DataCoordDataNodeList.WithLabelValues("online").Dec()
|
|
|
|
metrics.DataCoordDataNodeList.WithLabelValues("offline").Inc()
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// GetNode get NodeInfo binding to the specified UniqueID
|
|
|
|
// returns nil if no Info is found
|
2021-07-12 11:03:52 +08:00
|
|
|
func (c *NodesInfo) GetNode(nodeID UniqueID) *NodeInfo {
|
|
|
|
node, ok := c.nodes[nodeID]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// SetClient set DataNode client to specified UniqueID
|
|
|
|
// do nothing if no Info is found
|
2021-07-12 11:03:52 +08:00
|
|
|
func (c *NodesInfo) SetClient(nodeID UniqueID, client types.DataNode) {
|
|
|
|
if node, ok := c.nodes[nodeID]; ok {
|
|
|
|
c.nodes[nodeID] = node.ShadowClone(SetClient(client))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// SetWatched set specified channels watch state from Uncomplete to Complete
|
|
|
|
// do nothing if no Info is found
|
2021-07-12 11:03:52 +08:00
|
|
|
func (c *NodesInfo) SetWatched(nodeID UniqueID, channelsName []string) {
|
|
|
|
if node, ok := c.nodes[nodeID]; ok {
|
|
|
|
c.nodes[nodeID] = node.Clone(SetWatched(channelsName))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// NodeOpt helper functions updating NodeInfo properties
|
2021-07-12 11:03:52 +08:00
|
|
|
type NodeOpt func(n *NodeInfo)
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// SetWatched returns a NodeOpt updating specified channels watch states from Uncomplete to Complete
|
2021-07-12 11:03:52 +08:00
|
|
|
func SetWatched(channelsName []string) NodeOpt {
|
|
|
|
return func(n *NodeInfo) {
|
|
|
|
channelsMap := make(map[string]struct{})
|
|
|
|
for _, channelName := range channelsName {
|
|
|
|
channelsMap[channelName] = struct{}{}
|
|
|
|
}
|
2021-07-15 16:38:31 +08:00
|
|
|
for _, ch := range n.Info.Channels {
|
2021-07-12 11:03:52 +08:00
|
|
|
_, ok := channelsMap[ch.GetName()]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ch.State == datapb.ChannelWatchState_Uncomplete {
|
|
|
|
ch.State = datapb.ChannelWatchState_Complete
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// SetClient returns NodeOpt update DataNode client
|
2021-07-12 11:03:52 +08:00
|
|
|
func SetClient(client types.DataNode) NodeOpt {
|
|
|
|
return func(n *NodeInfo) {
|
|
|
|
n.client = client
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// AddChannels returns NodeOpt adding specified channels to assigned list
|
2021-07-12 11:03:52 +08:00
|
|
|
func AddChannels(channels []*datapb.ChannelStatus) NodeOpt {
|
|
|
|
return func(n *NodeInfo) {
|
2021-07-15 16:38:31 +08:00
|
|
|
n.Info.Channels = append(n.Info.Channels, channels...)
|
2021-07-12 11:03:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:59:55 +08:00
|
|
|
// SetChannels returns NodeOpt updating assigned channels
|
2021-07-12 11:03:52 +08:00
|
|
|
func SetChannels(channels []*datapb.ChannelStatus) NodeOpt {
|
|
|
|
return func(n *NodeInfo) {
|
2021-07-15 16:38:31 +08:00
|
|
|
n.Info.Channels = channels
|
2021-07-12 11:03:52 +08:00
|
|
|
}
|
|
|
|
}
|