2021-04-19 13:42:47 +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-04-02 10:01:11 +08:00
|
|
|
package mqclient
|
2021-03-26 20:10:11 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
2021-03-27 09:46:54 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/rocksmq/client/rocksmq"
|
2021-03-26 20:10:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type rmqClient struct {
|
|
|
|
client rocksmq.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRmqClient(opts rocksmq.ClientOptions) (*rmqClient, error) {
|
|
|
|
c, err := rocksmq.NewClient(opts)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Set rmq client failed, error", zap.Error(err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &rmqClient{client: c}, nil
|
|
|
|
}
|
|
|
|
|
2021-04-02 10:01:11 +08:00
|
|
|
func (rc *rmqClient) CreateProducer(options ProducerOptions) (Producer, error) {
|
2021-03-26 20:10:11 +08:00
|
|
|
rmqOpts := rocksmq.ProducerOptions{Topic: options.Topic}
|
|
|
|
pp, err := rc.client.CreateProducer(rmqOpts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rp := rmqProducer{p: pp}
|
|
|
|
return &rp, nil
|
|
|
|
}
|
|
|
|
|
2021-04-02 10:01:11 +08:00
|
|
|
func (rc *rmqClient) Subscribe(options ConsumerOptions) (Consumer, error) {
|
2021-03-26 20:10:11 +08:00
|
|
|
receiveChannel := make(chan rocksmq.ConsumerMessage, options.BufSize)
|
|
|
|
|
|
|
|
cli, err := rc.client.Subscribe(rocksmq.ConsumerOptions{
|
|
|
|
Topic: options.Topic,
|
|
|
|
SubscriptionName: options.SubscriptionName,
|
|
|
|
MessageChannel: receiveChannel,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-08 21:49:59 +08:00
|
|
|
rConsumer := &RmqConsumer{c: cli, closeCh: make(chan struct{})}
|
2021-03-26 20:10:11 +08:00
|
|
|
|
|
|
|
return rConsumer, nil
|
|
|
|
}
|
|
|
|
|
2021-04-02 10:01:11 +08:00
|
|
|
func (rc *rmqClient) EarliestMessageID() MessageID {
|
2021-03-26 20:10:11 +08:00
|
|
|
rID := rocksmq.EarliestMessageID()
|
|
|
|
return &rmqID{messageID: rID}
|
|
|
|
}
|
|
|
|
|
2021-04-02 10:01:11 +08:00
|
|
|
func (rc *rmqClient) StringToMsgID(id string) (MessageID, error) {
|
2021-03-26 20:10:11 +08:00
|
|
|
rID, err := strconv.ParseInt(id, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &rmqID{messageID: rID}, nil
|
|
|
|
}
|
|
|
|
|
2021-04-02 10:01:11 +08:00
|
|
|
func (rc *rmqClient) BytesToMsgID(id []byte) (MessageID, error) {
|
|
|
|
rID, err := DeserializeRmqID(id)
|
2021-03-27 09:46:54 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &rmqID{messageID: rID}, nil
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:10:11 +08:00
|
|
|
func (rc *rmqClient) Close() {
|
2021-09-22 17:21:00 +08:00
|
|
|
// TODO(yukun): What to do here?
|
|
|
|
// rc.client.Close()
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|