-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unsubscribe channel after query node down (#15230)
Signed-off-by: xige-16 <[email protected]>
- Loading branch information
Showing
17 changed files
with
832 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
// 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 | ||
// 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. | ||
|
||
package querycoord | ||
|
||
import ( | ||
"container/list" | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/golang/protobuf/proto" | ||
"go.uber.org/zap" | ||
|
||
etcdkv "github.com/milvus-io/milvus/internal/kv/etcd" | ||
"github.com/milvus-io/milvus/internal/log" | ||
"github.com/milvus-io/milvus/internal/msgstream" | ||
"github.com/milvus-io/milvus/internal/proto/querypb" | ||
"github.com/milvus-io/milvus/internal/util/funcutil" | ||
) | ||
|
||
const ( | ||
unsubscribeChannelInfoPrefix = "queryCoord-unsubscribeChannelInfo" | ||
unsubscribeChannelCheckInterval = time.Second | ||
) | ||
|
||
type channelUnsubscribeHandler struct { | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
kvClient *etcdkv.EtcdKV | ||
factory msgstream.Factory | ||
|
||
channelInfos *list.List | ||
downNodeChan chan int64 | ||
|
||
wg sync.WaitGroup | ||
} | ||
|
||
// newChannelUnsubscribeHandler create a new handler service to unsubscribe channels | ||
func newChannelUnsubscribeHandler(ctx context.Context, kv *etcdkv.EtcdKV, factory msgstream.Factory) (*channelUnsubscribeHandler, error) { | ||
childCtx, cancel := context.WithCancel(ctx) | ||
handler := &channelUnsubscribeHandler{ | ||
ctx: childCtx, | ||
cancel: cancel, | ||
kvClient: kv, | ||
factory: factory, | ||
|
||
channelInfos: list.New(), | ||
//TODO:: if the query nodes that are down exceed 1024, query coord will not be able to restart | ||
downNodeChan: make(chan int64, 1024), | ||
} | ||
|
||
err := handler.reloadFromKV() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return handler, nil | ||
} | ||
|
||
// reloadFromKV reload unsolved channels to unsubscribe | ||
func (csh *channelUnsubscribeHandler) reloadFromKV() error { | ||
log.Debug("start reload unsubscribe channelInfo from kv") | ||
_, channelInfoValues, err := csh.kvClient.LoadWithPrefix(unsubscribeChannelInfoPrefix) | ||
if err != nil { | ||
return err | ||
} | ||
for _, value := range channelInfoValues { | ||
channelInfo := &querypb.UnsubscribeChannelInfo{} | ||
err = proto.Unmarshal([]byte(value), channelInfo) | ||
if err != nil { | ||
return err | ||
} | ||
csh.channelInfos.PushBack(channelInfo) | ||
csh.downNodeChan <- channelInfo.NodeID | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// addUnsubscribeChannelInfo add channel info to handler service, and persistent to etcd | ||
func (csh *channelUnsubscribeHandler) addUnsubscribeChannelInfo(info *querypb.UnsubscribeChannelInfo) { | ||
nodeID := info.NodeID | ||
channelInfoValue, err := proto.Marshal(info) | ||
if err != nil { | ||
panic(err) | ||
} | ||
// when queryCoord is restarted multiple times, the nodeID of added channelInfo may be the same | ||
hasEnqueue := false | ||
for e := csh.channelInfos.Back(); e != nil; e = e.Prev() { | ||
if e.Value.(*querypb.UnsubscribeChannelInfo).NodeID == nodeID { | ||
hasEnqueue = true | ||
} | ||
} | ||
|
||
if !hasEnqueue { | ||
channelInfoKey := fmt.Sprintf("%s/%d", unsubscribeChannelInfoPrefix, nodeID) | ||
err = csh.kvClient.Save(channelInfoKey, string(channelInfoValue)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
csh.channelInfos.PushBack(info) | ||
csh.downNodeChan <- info.NodeID | ||
log.Debug("add unsubscribeChannelInfo to handler", zap.Int64("nodeID", info.NodeID)) | ||
} | ||
} | ||
|
||
// handleChannelUnsubscribeLoop handle the unsubscription of channels which query node has watched | ||
func (csh *channelUnsubscribeHandler) handleChannelUnsubscribeLoop() { | ||
defer csh.wg.Done() | ||
for { | ||
select { | ||
case <-csh.ctx.Done(): | ||
log.Debug("channelUnsubscribeHandler ctx done, handleChannelUnsubscribeLoop end") | ||
return | ||
case <-csh.downNodeChan: | ||
channelInfo := csh.channelInfos.Front().Value.(*querypb.UnsubscribeChannelInfo) | ||
nodeID := channelInfo.NodeID | ||
for _, collectionChannels := range channelInfo.CollectionChannels { | ||
collectionID := collectionChannels.CollectionID | ||
subName := funcutil.GenChannelSubName(Params.QueryNodeCfg.MsgChannelSubName, collectionID, nodeID) | ||
err := unsubscribeChannels(csh.ctx, csh.factory, subName, collectionChannels.Channels) | ||
if err != nil { | ||
log.Debug("unsubscribe channels failed", zap.Int64("nodeID", nodeID)) | ||
panic(err) | ||
} | ||
} | ||
|
||
channelInfoKey := fmt.Sprintf("%s/%d", unsubscribeChannelInfoPrefix, nodeID) | ||
err := csh.kvClient.Remove(channelInfoKey) | ||
if err != nil { | ||
log.Error("remove unsubscribe channelInfo from etcd failed", zap.Int64("nodeID", nodeID)) | ||
panic(err) | ||
} | ||
log.Debug("unsubscribe channels success", zap.Int64("nodeID", nodeID)) | ||
} | ||
} | ||
} | ||
|
||
func (csh *channelUnsubscribeHandler) start() { | ||
csh.wg.Add(1) | ||
go csh.handleChannelUnsubscribeLoop() | ||
} | ||
|
||
func (csh *channelUnsubscribeHandler) close() { | ||
csh.cancel() | ||
csh.wg.Wait() | ||
} | ||
|
||
// unsubscribeChannels create consumer fist, and unsubscribe channel through msgStream.close() | ||
func unsubscribeChannels(ctx context.Context, factory msgstream.Factory, subName string, channels []string) error { | ||
msgStream, err := factory.NewMsgStream(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
msgStream.AsConsumer(channels, subName) | ||
msgStream.Close() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// 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 | ||
// 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. | ||
|
||
package querycoord | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/golang/protobuf/proto" | ||
"github.com/stretchr/testify/assert" | ||
|
||
etcdkv "github.com/milvus-io/milvus/internal/kv/etcd" | ||
"github.com/milvus-io/milvus/internal/msgstream" | ||
"github.com/milvus-io/milvus/internal/proto/querypb" | ||
"github.com/milvus-io/milvus/internal/util/etcd" | ||
) | ||
|
||
func Test_HandlerReloadFromKV(t *testing.T) { | ||
refreshParams() | ||
baseCtx, cancel := context.WithCancel(context.Background()) | ||
etcdCli, err := etcd.GetEtcdClient(&Params.BaseParams) | ||
assert.Nil(t, err) | ||
defer etcdCli.Close() | ||
kv := etcdkv.NewEtcdKV(etcdCli, Params.BaseParams.MetaRootPath) | ||
|
||
channelInfoKey := fmt.Sprintf("%s/%d", unsubscribeChannelInfoPrefix, defaultQueryNodeID) | ||
unsubscribeChannelInfo := &querypb.UnsubscribeChannelInfo{ | ||
NodeID: defaultQueryNodeID, | ||
} | ||
channelInfoBytes, err := proto.Marshal(unsubscribeChannelInfo) | ||
assert.Nil(t, err) | ||
|
||
err = kv.Save(channelInfoKey, string(channelInfoBytes)) | ||
assert.Nil(t, err) | ||
|
||
factory := msgstream.NewPmsFactory() | ||
handler, err := newChannelUnsubscribeHandler(baseCtx, kv, factory) | ||
assert.Nil(t, err) | ||
assert.Equal(t, 1, len(handler.downNodeChan)) | ||
|
||
cancel() | ||
} | ||
|
||
func Test_AddUnsubscribeChannelInfo(t *testing.T) { | ||
refreshParams() | ||
baseCtx, cancel := context.WithCancel(context.Background()) | ||
etcdCli, err := etcd.GetEtcdClient(&Params.BaseParams) | ||
assert.Nil(t, err) | ||
defer etcdCli.Close() | ||
kv := etcdkv.NewEtcdKV(etcdCli, Params.BaseParams.MetaRootPath) | ||
factory := msgstream.NewPmsFactory() | ||
handler, err := newChannelUnsubscribeHandler(baseCtx, kv, factory) | ||
assert.Nil(t, err) | ||
|
||
collectionChannels := &querypb.UnsubscribeChannels{ | ||
CollectionID: defaultCollectionID, | ||
Channels: []string{"test-channel"}, | ||
} | ||
unsubscribeChannelInfo := &querypb.UnsubscribeChannelInfo{ | ||
NodeID: defaultQueryNodeID, | ||
CollectionChannels: []*querypb.UnsubscribeChannels{collectionChannels}, | ||
} | ||
|
||
handler.addUnsubscribeChannelInfo(unsubscribeChannelInfo) | ||
frontValue := handler.channelInfos.Front() | ||
assert.NotNil(t, frontValue) | ||
assert.Equal(t, defaultQueryNodeID, frontValue.Value.(*querypb.UnsubscribeChannelInfo).NodeID) | ||
|
||
// repeat nodeID which has down | ||
handler.addUnsubscribeChannelInfo(unsubscribeChannelInfo) | ||
assert.Equal(t, 1, len(handler.downNodeChan)) | ||
|
||
cancel() | ||
} | ||
|
||
func Test_HandleChannelUnsubscribeLoop(t *testing.T) { | ||
refreshParams() | ||
baseCtx, cancel := context.WithCancel(context.Background()) | ||
etcdCli, err := etcd.GetEtcdClient(&Params.BaseParams) | ||
assert.Nil(t, err) | ||
defer etcdCli.Close() | ||
kv := etcdkv.NewEtcdKV(etcdCli, Params.BaseParams.MetaRootPath) | ||
factory := msgstream.NewPmsFactory() | ||
m := map[string]interface{}{ | ||
"PulsarAddress": Params.PulsarCfg.Address, | ||
"ReceiveBufSize": 1024, | ||
"PulsarBufSize": 1024} | ||
factory.SetParams(m) | ||
handler, err := newChannelUnsubscribeHandler(baseCtx, kv, factory) | ||
assert.Nil(t, err) | ||
|
||
collectionChannels := &querypb.UnsubscribeChannels{ | ||
CollectionID: defaultCollectionID, | ||
Channels: []string{"test-channel"}, | ||
} | ||
unsubscribeChannelInfo := &querypb.UnsubscribeChannelInfo{ | ||
NodeID: defaultQueryNodeID, | ||
CollectionChannels: []*querypb.UnsubscribeChannels{collectionChannels}, | ||
} | ||
|
||
handler.addUnsubscribeChannelInfo(unsubscribeChannelInfo) | ||
channelInfoKey := fmt.Sprintf("%s/%d", unsubscribeChannelInfoPrefix, defaultQueryNodeID) | ||
_, err = kv.Load(channelInfoKey) | ||
assert.Nil(t, err) | ||
|
||
handler.start() | ||
|
||
for { | ||
_, err = kv.Load(channelInfoKey) | ||
if err != nil { | ||
break | ||
} | ||
} | ||
|
||
cancel() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.