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 (
|
2021-11-21 07:33:14 +08:00
|
|
|
"errors"
|
2021-03-26 20:10:11 +08:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2021-12-24 10:47:56 +08:00
|
|
|
// rmqClient contains a rocksmq client
|
2021-03-26 20:10:11 +08:00
|
|
|
type rmqClient struct {
|
|
|
|
client rocksmq.Client
|
|
|
|
}
|
|
|
|
|
2021-10-04 23:48:10 +08:00
|
|
|
// NewRmqClient returns a new rmqClient object
|
2021-03-26 20:10:11 +08:00
|
|
|
func NewRmqClient(opts rocksmq.ClientOptions) (*rmqClient, error) {
|
|
|
|
c, err := rocksmq.NewClient(opts)
|
|
|
|
if err != nil {
|
2021-10-20 10:56:35 +08:00
|
|
|
log.Error("Failed to set rmq client: ", zap.Error(err))
|
2021-03-26 20:10:11 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &rmqClient{client: c}, nil
|
|
|
|
}
|
|
|
|
|
2021-10-09 00:03:51 +08:00
|
|
|
// CreateProducer creates a producer for rocksmq client
|
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-12-17 22:42:42 +08:00
|
|
|
// CreateReader creates a rocksmq reader from reader options
|
2021-11-19 15:57:12 +08:00
|
|
|
func (rc *rmqClient) CreateReader(options ReaderOptions) (Reader, error) {
|
2021-11-21 07:33:14 +08:00
|
|
|
opts := rocksmq.ReaderOptions{
|
|
|
|
Topic: options.Topic,
|
|
|
|
StartMessageID: options.StartMessageID.(*rmqID).messageID,
|
|
|
|
StartMessageIDInclusive: options.StartMessageIDInclusive,
|
2021-11-24 17:47:15 +08:00
|
|
|
SubscriptionRolePrefix: options.SubscriptionRolePrefix,
|
2021-11-21 07:33:14 +08:00
|
|
|
}
|
|
|
|
pr, err := rc.client.CreateReader(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if pr == nil {
|
|
|
|
return nil, errors.New("pulsar is not ready, producer is nil")
|
|
|
|
}
|
|
|
|
reader := &rmqReader{r: pr}
|
|
|
|
return reader, nil
|
2021-11-19 15:57:12 +08:00
|
|
|
}
|
|
|
|
|
2021-10-07 21:21:05 +08:00
|
|
|
// Subscribe subscribes a consumer in rmq client
|
2021-04-02 10:01:11 +08:00
|
|
|
func (rc *rmqClient) Subscribe(options ConsumerOptions) (Consumer, error) {
|
2021-11-17 14:11:11 +08:00
|
|
|
receiveChannel := make(chan rocksmq.Message, options.BufSize)
|
2021-03-26 20:10:11 +08:00
|
|
|
|
|
|
|
cli, err := rc.client.Subscribe(rocksmq.ConsumerOptions{
|
2021-09-30 20:51:40 +08:00
|
|
|
Topic: options.Topic,
|
|
|
|
SubscriptionName: options.SubscriptionName,
|
|
|
|
MessageChannel: receiveChannel,
|
|
|
|
SubscriptionInitialPosition: rocksmq.SubscriptionInitialPosition(options.SubscriptionInitialPosition),
|
2021-03-26 20:10:11 +08:00
|
|
|
})
|
|
|
|
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-10-07 21:18:52 +08:00
|
|
|
// EarliestMessageID returns the earliest message ID for rmq client
|
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-10-09 00:07:52 +08:00
|
|
|
// StringToMsgID converts string id to MessageID
|
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-11-04 20:49:20 +08:00
|
|
|
// BytesToMsgID converts a byte array to messageID
|
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() {
|
2022-01-04 14:45:19 +08:00
|
|
|
rc.client.Close()
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|