2022-01-07 18:49:25 +08:00
|
|
|
// 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
|
2021-04-19 13:42:47 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2022-01-07 18:49:25 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 13:42:47 +08:00
|
|
|
//
|
2022-01-07 18:49:25 +08:00
|
|
|
// 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-19 13:42:47 +08:00
|
|
|
|
2022-03-03 21:57:56 +08:00
|
|
|
package rmq
|
2021-03-26 20:10:11 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
2022-03-03 21:57:56 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/mq/mqimpl/rocksmq/server"
|
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/internal/mq/mqimpl/rocksmq/client"
|
|
|
|
"github.com/milvus-io/milvus/internal/mq/msgstream/mqwrapper"
|
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"
|
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 {
|
2022-03-03 21:57:56 +08:00
|
|
|
client client.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientWithDefaultOptions() (*rmqClient, error) {
|
|
|
|
option := client.Options{Server: server.Rmq}
|
|
|
|
return NewClient(option)
|
2021-03-26 20:10:11 +08:00
|
|
|
}
|
|
|
|
|
2022-03-03 21:57:56 +08:00
|
|
|
// NewClient returns a new rmqClient object
|
|
|
|
func NewClient(opts client.Options) (*rmqClient, error) {
|
|
|
|
c, err := client.NewClient(opts)
|
2021-03-26 20:10:11 +08:00
|
|
|
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
|
2022-03-03 21:57:56 +08:00
|
|
|
func (rc *rmqClient) CreateProducer(options mqwrapper.ProducerOptions) (mqwrapper.Producer, error) {
|
|
|
|
rmqOpts := client.ProducerOptions{Topic: options.Topic}
|
2021-03-26 20:10:11 +08:00
|
|
|
pp, err := rc.client.CreateProducer(rmqOpts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rp := rmqProducer{p: pp}
|
|
|
|
return &rp, nil
|
|
|
|
}
|
|
|
|
|
2021-10-07 21:21:05 +08:00
|
|
|
// Subscribe subscribes a consumer in rmq client
|
2022-03-03 21:57:56 +08:00
|
|
|
func (rc *rmqClient) Subscribe(options mqwrapper.ConsumerOptions) (mqwrapper.Consumer, error) {
|
|
|
|
receiveChannel := make(chan client.Message, options.BufSize)
|
2021-03-26 20:10:11 +08:00
|
|
|
|
2022-03-03 21:57:56 +08:00
|
|
|
cli, err := rc.client.Subscribe(client.ConsumerOptions{
|
2021-09-30 20:51:40 +08:00
|
|
|
Topic: options.Topic,
|
|
|
|
SubscriptionName: options.SubscriptionName,
|
|
|
|
MessageChannel: receiveChannel,
|
2022-10-25 13:23:30 +08:00
|
|
|
SubscriptionInitialPosition: options.SubscriptionInitialPosition,
|
2021-03-26 20:10:11 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-03 21:57:56 +08:00
|
|
|
rConsumer := &Consumer{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
|
2022-03-03 21:57:56 +08:00
|
|
|
func (rc *rmqClient) EarliestMessageID() mqwrapper.MessageID {
|
|
|
|
rID := client.EarliestMessageID()
|
2021-03-26 20:10:11 +08:00
|
|
|
return &rmqID{messageID: rID}
|
|
|
|
}
|
|
|
|
|
2021-10-09 00:07:52 +08:00
|
|
|
// StringToMsgID converts string id to MessageID
|
2022-03-03 21:57:56 +08:00
|
|
|
func (rc *rmqClient) StringToMsgID(id string) (mqwrapper.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
|
2022-03-03 21:57:56 +08:00
|
|
|
func (rc *rmqClient) BytesToMsgID(id []byte) (mqwrapper.MessageID, error) {
|
2022-03-15 14:45:22 +08:00
|
|
|
rID := DeserializeRmqID(id)
|
2021-03-27 09:46:54 +08:00
|
|
|
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
|
|
|
}
|