2021-01-22 11:43:27 +00:00
|
|
|
package dataservice
|
|
|
|
|
|
|
|
import (
|
2021-02-26 09:44:24 +00:00
|
|
|
"context"
|
2021-04-13 01:47:02 +00:00
|
|
|
"errors"
|
2021-02-23 01:58:06 +00:00
|
|
|
"fmt"
|
2021-01-22 11:43:27 +00:00
|
|
|
"sync"
|
|
|
|
|
2021-02-23 01:58:06 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/log"
|
2021-03-05 12:41:34 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/types"
|
2021-01-22 11:43:27 +00:00
|
|
|
|
2021-03-05 12:41:34 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
2021-01-22 11:43:27 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/datapb"
|
2021-03-12 06:22:09 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb"
|
2021-01-22 11:43:27 +00:00
|
|
|
)
|
|
|
|
|
2021-03-05 12:41:34 +00:00
|
|
|
type dataNode struct {
|
|
|
|
id int64
|
|
|
|
address struct {
|
|
|
|
ip string
|
|
|
|
port int64
|
2021-01-22 11:43:27 +00:00
|
|
|
}
|
2021-03-05 12:41:34 +00:00
|
|
|
client types.DataNode
|
|
|
|
channelNum int
|
|
|
|
}
|
|
|
|
type dataNodeCluster struct {
|
2021-04-08 07:08:34 +00:00
|
|
|
sync.RWMutex
|
2021-04-13 01:47:02 +00:00
|
|
|
nodes []*dataNode
|
2021-03-05 12:41:34 +00:00
|
|
|
}
|
2021-01-22 11:43:27 +00:00
|
|
|
|
2021-02-23 01:58:06 +00:00
|
|
|
func (node *dataNode) String() string {
|
|
|
|
return fmt.Sprintf("id: %d, address: %s:%d", node.id, node.address.ip, node.address.port)
|
|
|
|
}
|
|
|
|
|
2021-04-13 01:47:02 +00:00
|
|
|
func newDataNodeCluster() *dataNodeCluster {
|
2021-01-22 11:43:27 +00:00
|
|
|
return &dataNodeCluster{
|
2021-04-13 01:47:02 +00:00
|
|
|
nodes: make([]*dataNode, 0),
|
2021-01-22 11:43:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 01:47:02 +00:00
|
|
|
func (c *dataNodeCluster) Register(dataNode *dataNode) error {
|
2021-04-08 07:08:34 +00:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2021-01-28 03:24:41 +00:00
|
|
|
if c.checkDataNodeNotExist(dataNode.address.ip, dataNode.address.port) {
|
|
|
|
c.nodes = append(c.nodes, dataNode)
|
2021-04-13 01:47:02 +00:00
|
|
|
return nil
|
2021-01-22 11:43:27 +00:00
|
|
|
}
|
2021-04-13 01:47:02 +00:00
|
|
|
return errors.New("datanode already exist")
|
2021-01-22 11:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dataNodeCluster) checkDataNodeNotExist(ip string, port int64) bool {
|
|
|
|
for _, node := range c.nodes {
|
2021-02-02 10:53:10 +00:00
|
|
|
if node.address.ip == ip && node.address.port == port {
|
2021-01-22 11:43:27 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dataNodeCluster) GetNumOfNodes() int {
|
2021-04-08 07:08:34 +00:00
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
2021-01-22 11:43:27 +00:00
|
|
|
return len(c.nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dataNodeCluster) GetNodeIDs() []int64 {
|
2021-04-08 07:08:34 +00:00
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
ret := make([]int64, 0, len(c.nodes))
|
|
|
|
for _, node := range c.nodes {
|
|
|
|
ret = append(ret, node.id)
|
2021-01-22 11:43:27 +00:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2021-02-03 09:30:10 +00:00
|
|
|
func (c *dataNodeCluster) WatchInsertChannels(channels []string) {
|
2021-02-26 09:44:24 +00:00
|
|
|
ctx := context.TODO()
|
2021-04-08 07:08:34 +00:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2021-02-02 10:53:10 +00:00
|
|
|
var groups [][]string
|
|
|
|
if len(channels) < len(c.nodes) {
|
|
|
|
groups = make([][]string, len(channels))
|
|
|
|
} else {
|
|
|
|
groups = make([][]string, len(c.nodes))
|
|
|
|
}
|
|
|
|
length := len(groups)
|
|
|
|
for i, channel := range channels {
|
|
|
|
groups[i%length] = append(groups[i%length], channel)
|
|
|
|
}
|
2021-01-22 11:43:27 +00:00
|
|
|
for i, group := range groups {
|
2021-03-12 06:22:09 +00:00
|
|
|
resp, err := c.nodes[i].client.WatchDmChannels(ctx, &datapb.WatchDmChannelsRequest{
|
2021-01-22 11:43:27 +00:00
|
|
|
Base: &commonpb.MsgBase{
|
2021-03-10 06:45:35 +00:00
|
|
|
MsgType: commonpb.MsgType_DescribeCollection,
|
2021-01-22 11:43:27 +00:00
|
|
|
MsgID: -1, // todo
|
|
|
|
Timestamp: 0, // todo
|
2021-01-26 07:14:49 +00:00
|
|
|
SourceID: Params.NodeID,
|
2021-01-22 11:43:27 +00:00
|
|
|
},
|
|
|
|
ChannelNames: group,
|
|
|
|
})
|
2021-02-23 01:58:06 +00:00
|
|
|
if err = VerifyResponse(resp, err); err != nil {
|
|
|
|
log.Error("watch dm channels error", zap.Stringer("dataNode", c.nodes[i]), zap.Error(err))
|
2021-02-02 10:53:10 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.nodes[i].channelNum += len(group)
|
2021-01-22 11:43:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 06:22:09 +00:00
|
|
|
func (c *dataNodeCluster) GetDataNodeStates(ctx context.Context) ([]*internalpb.ComponentInfo, error) {
|
2021-04-08 07:08:34 +00:00
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
2021-03-12 06:22:09 +00:00
|
|
|
ret := make([]*internalpb.ComponentInfo, 0)
|
2021-01-22 11:43:27 +00:00
|
|
|
for _, node := range c.nodes {
|
2021-03-05 12:41:34 +00:00
|
|
|
states, err := node.client.GetComponentStates(ctx)
|
2021-01-22 11:43:27 +00:00
|
|
|
if err != nil {
|
2021-02-23 01:58:06 +00:00
|
|
|
log.Error("get component states error", zap.Stringer("dataNode", node), zap.Error(err))
|
2021-01-22 11:43:27 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret = append(ret, states.State)
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2021-03-12 06:22:09 +00:00
|
|
|
func (c *dataNodeCluster) FlushSegment(request *datapb.FlushSegmentsRequest) {
|
2021-02-26 09:44:24 +00:00
|
|
|
ctx := context.TODO()
|
2021-04-08 07:08:34 +00:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2021-01-22 11:43:27 +00:00
|
|
|
for _, node := range c.nodes {
|
2021-02-26 09:44:24 +00:00
|
|
|
if _, err := node.client.FlushSegments(ctx, request); err != nil {
|
2021-02-23 01:58:06 +00:00
|
|
|
log.Error("flush segment err", zap.Stringer("dataNode", node), zap.Error(err))
|
2021-01-22 11:43:27 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-28 03:24:41 +00:00
|
|
|
|
|
|
|
func (c *dataNodeCluster) ShutDownClients() {
|
2021-04-08 07:08:34 +00:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2021-01-28 03:24:41 +00:00
|
|
|
for _, node := range c.nodes {
|
|
|
|
if err := node.client.Stop(); err != nil {
|
2021-02-23 01:58:06 +00:00
|
|
|
log.Error("stop client error", zap.Stringer("dataNode", node), zap.Error(err))
|
2021-01-28 03:24:41 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-02 10:53:10 +00:00
|
|
|
|
|
|
|
// Clear only for test
|
|
|
|
func (c *dataNodeCluster) Clear() {
|
2021-04-08 07:08:34 +00:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2021-02-02 10:53:10 +00:00
|
|
|
c.nodes = make([]*dataNode, 0)
|
|
|
|
}
|