2021-01-22 19:43:27 +08:00
|
|
|
package dataservice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
|
|
|
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/datapb"
|
|
|
|
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
dataNode struct {
|
|
|
|
id int64
|
|
|
|
address struct {
|
|
|
|
ip string
|
|
|
|
port int64
|
|
|
|
}
|
2021-01-28 11:24:41 +08:00
|
|
|
client DataNodeClient
|
2021-01-22 19:43:27 +08:00
|
|
|
channelNum int
|
|
|
|
}
|
|
|
|
dataNodeCluster struct {
|
2021-02-03 17:30:10 +08:00
|
|
|
mu sync.RWMutex
|
|
|
|
finishCh chan struct{}
|
|
|
|
nodes []*dataNode
|
2021-01-22 19:43:27 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func newDataNodeCluster(finishCh chan struct{}) *dataNodeCluster {
|
|
|
|
return &dataNodeCluster{
|
2021-02-03 17:30:10 +08:00
|
|
|
finishCh: finishCh,
|
|
|
|
nodes: make([]*dataNode, 0),
|
2021-01-22 19:43:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 11:24:41 +08:00
|
|
|
func (c *dataNodeCluster) Register(dataNode *dataNode) {
|
2021-01-22 19:43:27 +08:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2021-01-28 11:24:41 +08:00
|
|
|
if c.checkDataNodeNotExist(dataNode.address.ip, dataNode.address.port) {
|
|
|
|
c.nodes = append(c.nodes, dataNode)
|
2021-01-27 14:41:56 +08:00
|
|
|
if len(c.nodes) == Params.DataNodeNum {
|
|
|
|
close(c.finishCh)
|
|
|
|
}
|
2021-01-22 19:43:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dataNodeCluster) checkDataNodeNotExist(ip string, port int64) bool {
|
|
|
|
for _, node := range c.nodes {
|
2021-02-02 18:53:10 +08:00
|
|
|
if node.address.ip == ip && node.address.port == port {
|
2021-01-22 19:43:27 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dataNodeCluster) GetNumOfNodes() int {
|
|
|
|
return len(c.nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dataNodeCluster) GetNodeIDs() []int64 {
|
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
|
|
|
ret := make([]int64, len(c.nodes))
|
2021-01-29 17:08:31 +08:00
|
|
|
for i, node := range c.nodes {
|
|
|
|
ret[i] = node.id
|
2021-01-22 19:43:27 +08:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2021-02-03 17:30:10 +08:00
|
|
|
func (c *dataNodeCluster) WatchInsertChannels(channels []string) {
|
2021-01-22 19:43:27 +08:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2021-02-02 18:53:10 +08: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 19:43:27 +08:00
|
|
|
for i, group := range groups {
|
2021-02-02 18:53:10 +08:00
|
|
|
resp, err := c.nodes[i].client.WatchDmChannels(&datapb.WatchDmChannelRequest{
|
2021-01-22 19:43:27 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_kDescribeCollection,
|
|
|
|
MsgID: -1, // todo
|
|
|
|
Timestamp: 0, // todo
|
2021-01-26 15:14:49 +08:00
|
|
|
SourceID: Params.NodeID,
|
2021-01-22 19:43:27 +08:00
|
|
|
},
|
|
|
|
ChannelNames: group,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
continue
|
|
|
|
}
|
2021-02-02 18:53:10 +08:00
|
|
|
if resp.ErrorCode != commonpb.ErrorCode_SUCCESS {
|
|
|
|
log.Println(resp.Reason)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.nodes[i].channelNum += len(group)
|
2021-01-22 19:43:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dataNodeCluster) GetDataNodeStates() ([]*internalpb2.ComponentInfo, error) {
|
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
|
|
|
ret := make([]*internalpb2.ComponentInfo, 0)
|
|
|
|
for _, node := range c.nodes {
|
2021-01-26 15:14:49 +08:00
|
|
|
states, err := node.client.GetComponentStates(&commonpb.Empty{})
|
2021-01-22 19:43:27 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret = append(ret, states.State)
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dataNodeCluster) FlushSegment(request *datapb.FlushSegRequest) {
|
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
|
|
|
for _, node := range c.nodes {
|
2021-01-24 21:20:11 +08:00
|
|
|
if _, err := node.client.FlushSegments(request); err != nil {
|
2021-01-22 19:43:27 +08:00
|
|
|
log.Println(err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-28 11:24:41 +08:00
|
|
|
|
|
|
|
func (c *dataNodeCluster) ShutDownClients() {
|
|
|
|
for _, node := range c.nodes {
|
|
|
|
if err := node.client.Stop(); err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-02 18:53:10 +08:00
|
|
|
|
|
|
|
// Clear only for test
|
|
|
|
func (c *dataNodeCluster) Clear() {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
c.finishCh = make(chan struct{})
|
|
|
|
c.nodes = make([]*dataNode, 0)
|
|
|
|
}
|