2021-06-04 15:00:34 +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-06-18 21:30:08 +08:00
|
|
|
package rootcoord
|
2021-06-04 15:00:34 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/msgstream"
|
2021-08-31 11:07:58 +08:00
|
|
|
"go.uber.org/zap"
|
2021-06-04 15:00:34 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type dmlChannels struct {
|
2021-08-31 11:07:58 +08:00
|
|
|
core *Core
|
|
|
|
lock sync.RWMutex
|
|
|
|
dml map[string]msgstream.MsgStream
|
2021-06-04 15:00:34 +08:00
|
|
|
}
|
|
|
|
|
2021-08-31 11:07:58 +08:00
|
|
|
func newDMLChannels(c *Core) *dmlChannels {
|
|
|
|
return &dmlChannels{
|
|
|
|
core: c,
|
|
|
|
lock: sync.RWMutex{},
|
|
|
|
dml: make(map[string]msgstream.MsgStream),
|
2021-06-04 15:00:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 11:07:58 +08:00
|
|
|
// GetNumChannels get current dml channel count
|
|
|
|
func (d *dmlChannels) GetNumChannels() int {
|
|
|
|
d.lock.RLock()
|
|
|
|
defer d.lock.RUnlock()
|
|
|
|
return len(d.dml)
|
2021-06-11 16:39:29 +08:00
|
|
|
}
|
|
|
|
|
2021-08-19 15:06:12 +08:00
|
|
|
// ListChannels lists all dml channel names
|
2021-08-03 09:59:24 +08:00
|
|
|
func (d *dmlChannels) ListChannels() []string {
|
2021-08-31 11:07:58 +08:00
|
|
|
d.lock.RLock()
|
|
|
|
defer d.lock.RUnlock()
|
|
|
|
|
|
|
|
ret := make([]string, 0, len(d.dml))
|
|
|
|
for n := range d.dml {
|
|
|
|
ret = append(ret, n)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
|
2021-08-03 09:59:24 +08:00
|
|
|
}
|
|
|
|
|
2021-08-31 11:07:58 +08:00
|
|
|
// Produce produces msg pack into specified channel
|
|
|
|
func (d *dmlChannels) Produce(name string, pack *msgstream.MsgPack) error {
|
|
|
|
d.lock.Lock()
|
|
|
|
defer d.lock.Unlock()
|
|
|
|
|
|
|
|
ds, ok := d.dml[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("channel %s not exist", name)
|
|
|
|
}
|
|
|
|
if err := ds.Produce(pack); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2021-06-04 15:00:34 +08:00
|
|
|
}
|
|
|
|
|
2021-08-19 15:06:12 +08:00
|
|
|
// Broadcast broadcasts msg pack into specified channel
|
2021-08-31 11:07:58 +08:00
|
|
|
func (d *dmlChannels) Broadcast(name string, pack *msgstream.MsgPack) error {
|
|
|
|
d.lock.Lock()
|
|
|
|
defer d.lock.Unlock()
|
|
|
|
|
|
|
|
ds, ok := d.dml[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("channel %s not exist", name)
|
|
|
|
}
|
|
|
|
if err := ds.Broadcast(pack); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BroadcastAll invoke broadcast with provided msg pack in all channels specified
|
|
|
|
func (d *dmlChannels) BroadcastAll(channels []string, pack *msgstream.MsgPack) error {
|
|
|
|
d.lock.Lock()
|
|
|
|
defer d.lock.Unlock()
|
|
|
|
|
|
|
|
for _, ch := range channels {
|
|
|
|
ds, ok := d.dml[ch]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("channel %s not exist", ch)
|
2021-06-15 21:11:58 +08:00
|
|
|
}
|
2021-08-31 11:07:58 +08:00
|
|
|
if err := ds.Broadcast(pack); err != nil {
|
2021-06-15 21:11:58 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-06-04 15:00:34 +08:00
|
|
|
}
|
2021-06-15 21:11:58 +08:00
|
|
|
return nil
|
2021-06-04 15:00:34 +08:00
|
|
|
}
|
|
|
|
|
2021-08-19 15:06:12 +08:00
|
|
|
// AddProducerChannels add named channels as producer
|
2021-06-08 19:25:37 +08:00
|
|
|
func (d *dmlChannels) AddProducerChannels(names ...string) {
|
2021-08-31 11:07:58 +08:00
|
|
|
d.lock.Lock()
|
|
|
|
defer d.lock.Unlock()
|
|
|
|
|
2021-06-04 15:00:34 +08:00
|
|
|
for _, name := range names {
|
2021-08-31 11:07:58 +08:00
|
|
|
log.Debug("add dml channel", zap.String("channel name", name))
|
|
|
|
_, ok := d.dml[name]
|
|
|
|
if !ok {
|
|
|
|
ms, err := d.core.msFactory.NewMsgStream(d.core.ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("add msgstream failed", zap.String("name", name), zap.Error(err))
|
|
|
|
continue
|
2021-06-04 15:00:34 +08:00
|
|
|
}
|
2021-08-31 11:07:58 +08:00
|
|
|
ms.AsProducer([]string{name})
|
|
|
|
d.dml[name] = ms
|
2021-06-04 15:00:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 15:06:12 +08:00
|
|
|
// RemoveProducerChannels removes specified channels
|
2021-06-04 15:00:34 +08:00
|
|
|
func (d *dmlChannels) RemoveProducerChannels(names ...string) {
|
2021-08-31 11:07:58 +08:00
|
|
|
d.lock.Lock()
|
|
|
|
defer d.lock.Unlock()
|
|
|
|
|
2021-06-04 15:00:34 +08:00
|
|
|
for _, name := range names {
|
2021-08-31 11:07:58 +08:00
|
|
|
if ds, ok := d.dml[name]; ok {
|
|
|
|
ds.Close()
|
|
|
|
delete(d.dml, name)
|
2021-06-08 19:25:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|