You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "RobertIndie (via GitHub)" <gi...@apache.org> on 2024/06/11 10:00:24 UTC
Re: [PR] [Issue 1223] Support ZeroQueueConsumer [pulsar-client-go]
RobertIndie commented on code in PR #1225:
URL: https://github.com/apache/pulsar-client-go/pull/1225#discussion_r1634564333
##########
pulsar/consumer_zero_queue_test.go:
##########
@@ -0,0 +1,443 @@
+// Licensed to the Apache Software Foundation (ASF) 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 pulsar
+
+import (
+ "context"
+ "fmt"
+ "log"
+ "testing"
+ "time"
+
+ "github.com/apache/pulsar-client-go/pulsar/internal"
+ "github.com/apache/pulsar-client-go/pulsaradmin"
+ "github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/config"
+ "github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils"
+ "github.com/stretchr/testify/assert"
+)
+
+func TestNormalZeroQueueConsumer(t *testing.T) {
+ client, err := NewClient(ClientOptions{
+ URL: lookupURL,
+ })
+
+ assert.Nil(t, err)
+ defer client.Close()
+
+ topic := newTopicName()
+ ctx := context.Background()
+
+ // create consumer
+ consumer, err := client.Subscribe(ConsumerOptions{
Review Comment:
Can we check the type for the consumer?
##########
pulsar/consumer_impl.go:
##########
@@ -236,7 +240,19 @@ func newConsumer(client *client, options ConsumerOptions) (Consumer, error) {
}
func newInternalConsumer(client *client, options ConsumerOptions, topic string,
- messageCh chan ConsumerMessage, dlq *dlqRouter, rlq *retryRouter, disableForceTopicCreation bool) (*consumer, error) {
+ messageCh chan ConsumerMessage, dlq *dlqRouter, rlq *retryRouter, disableForceTopicCreation bool) (Consumer, error) {
+ partitions, err := client.TopicPartitions(topic)
+ if err != nil {
+ return nil, err
+ }
+
+ if len(partitions) > 1 && options.EnableZeroQueueConsumer {
+ return nil, pkgerrors.New("ZeroQueueConsumer is not supported for partitioned topics")
+ }
+
+ if len(partitions) == 1 && options.EnableZeroQueueConsumer {
Review Comment:
We should also prevent creating a zero queue consumer for a partitioned topic that has only one partition.
##########
pulsar/consumer_zero_queue.go:
##########
@@ -0,0 +1,328 @@
+// Licensed to the Apache Software Foundation (ASF) 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 pulsar
+
+import (
+ "context"
+ "fmt"
+ "sync"
+ "time"
+
+ "github.com/apache/pulsar-client-go/pulsar/internal"
+ "github.com/apache/pulsar-client-go/pulsar/log"
+ "github.com/pkg/errors"
+)
+
+type zeroQueueConsumer struct {
+ sync.Mutex
+ topic string
+ client *client
+ options ConsumerOptions
+ pc *partitionConsumer
+ consumerName string
+ disableForceTopicCreation bool
+
+ messageCh chan ConsumerMessage
+
+ dlq *dlqRouter
+ rlq *retryRouter
+ closeOnce sync.Once
+ closeCh chan struct{}
+ errorCh chan error
+
+ log log.Logger
+ metrics *internal.LeveledMetrics
+}
+
+func newZeroConsumer(client *client, options ConsumerOptions, topic string,
+ messageCh chan ConsumerMessage, dlq *dlqRouter,
+ rlq *retryRouter, disableForceTopicCreation bool) (*zeroQueueConsumer, error) {
+ zc := &zeroQueueConsumer{
+ topic: topic,
+ client: client,
+ options: options,
+ disableForceTopicCreation: disableForceTopicCreation,
+ messageCh: messageCh,
+ closeCh: make(chan struct{}),
+ errorCh: make(chan error),
+ dlq: dlq,
+ rlq: rlq,
+ log: client.log.SubLogger(log.Fields{"topic": topic}),
+ consumerName: options.Name,
+ metrics: client.metrics.GetLeveledMetrics(topic),
+ }
+
+ var nackRedeliveryDelay time.Duration
+ if zc.options.NackRedeliveryDelay == 0 {
+ nackRedeliveryDelay = defaultNackRedeliveryDelay
+ } else {
+ nackRedeliveryDelay = zc.options.NackRedeliveryDelay
+ }
+ opts := &partitionConsumerOpts{
+ topic: zc.topic,
+ consumerName: zc.consumerName,
+ subscription: zc.options.SubscriptionName,
+ subscriptionType: zc.options.Type,
+ subscriptionInitPos: zc.options.SubscriptionInitialPosition,
+ partitionIdx: 0,
+ receiverQueueSize: zc.options.ReceiverQueueSize,
+ nackRedeliveryDelay: nackRedeliveryDelay,
+ nackBackoffPolicy: zc.options.NackBackoffPolicy,
+ metadata: zc.options.Properties,
+ subProperties: zc.options.SubscriptionProperties,
+ replicateSubscriptionState: zc.options.ReplicateSubscriptionState,
+ startMessageID: zc.options.startMessageID,
+ startMessageIDInclusive: zc.options.StartMessageIDInclusive,
+ subscriptionMode: zc.options.SubscriptionMode,
+ readCompacted: zc.options.ReadCompacted,
+ interceptors: zc.options.Interceptors,
+ maxReconnectToBroker: zc.options.MaxReconnectToBroker,
+ backoffPolicy: zc.options.BackoffPolicy,
+ keySharedPolicy: zc.options.KeySharedPolicy,
+ schema: zc.options.Schema,
+ decryption: zc.options.Decryption,
+ ackWithResponse: zc.options.AckWithResponse,
+ maxPendingChunkedMessage: zc.options.MaxPendingChunkedMessage,
+ expireTimeOfIncompleteChunk: zc.options.ExpireTimeOfIncompleteChunk,
+ autoAckIncompleteChunk: zc.options.AutoAckIncompleteChunk,
+ consumerEventListener: zc.options.EventListener,
+ enableBatchIndexAck: zc.options.EnableBatchIndexAcknowledgment,
+ ackGroupingOptions: zc.options.AckGroupingOptions,
+ autoReceiverQueueSize: zc.options.EnableAutoScaledReceiverQueueSize,
+ }
Review Comment:
Could we abstract this logic so that we can reuse the code? https://github.com/apache/pulsar-client-go/blob/fc15995c1a180a151aca58c478bc40843120783e/pulsar/consumer_impl.go#L390-L420
##########
pulsar/consumer_zero_queue.go:
##########
@@ -0,0 +1,328 @@
+// Licensed to the Apache Software Foundation (ASF) 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 pulsar
+
+import (
+ "context"
+ "fmt"
+ "sync"
+ "time"
+
+ "github.com/apache/pulsar-client-go/pulsar/internal"
+ "github.com/apache/pulsar-client-go/pulsar/log"
+ "github.com/pkg/errors"
+)
+
+type zeroQueueConsumer struct {
+ sync.Mutex
+ topic string
+ client *client
+ options ConsumerOptions
+ pc *partitionConsumer
+ consumerName string
+ disableForceTopicCreation bool
+
+ messageCh chan ConsumerMessage
+
+ dlq *dlqRouter
+ rlq *retryRouter
+ closeOnce sync.Once
+ closeCh chan struct{}
+ errorCh chan error
+
+ log log.Logger
+ metrics *internal.LeveledMetrics
+}
+
+func newZeroConsumer(client *client, options ConsumerOptions, topic string,
+ messageCh chan ConsumerMessage, dlq *dlqRouter,
+ rlq *retryRouter, disableForceTopicCreation bool) (*zeroQueueConsumer, error) {
+ zc := &zeroQueueConsumer{
+ topic: topic,
+ client: client,
+ options: options,
+ disableForceTopicCreation: disableForceTopicCreation,
+ messageCh: messageCh,
+ closeCh: make(chan struct{}),
+ errorCh: make(chan error),
+ dlq: dlq,
+ rlq: rlq,
+ log: client.log.SubLogger(log.Fields{"topic": topic}),
+ consumerName: options.Name,
+ metrics: client.metrics.GetLeveledMetrics(topic),
+ }
+
+ var nackRedeliveryDelay time.Duration
+ if zc.options.NackRedeliveryDelay == 0 {
+ nackRedeliveryDelay = defaultNackRedeliveryDelay
+ } else {
+ nackRedeliveryDelay = zc.options.NackRedeliveryDelay
+ }
+ opts := &partitionConsumerOpts{
+ topic: zc.topic,
+ consumerName: zc.consumerName,
+ subscription: zc.options.SubscriptionName,
+ subscriptionType: zc.options.Type,
+ subscriptionInitPos: zc.options.SubscriptionInitialPosition,
+ partitionIdx: 0,
+ receiverQueueSize: zc.options.ReceiverQueueSize,
+ nackRedeliveryDelay: nackRedeliveryDelay,
+ nackBackoffPolicy: zc.options.NackBackoffPolicy,
+ metadata: zc.options.Properties,
+ subProperties: zc.options.SubscriptionProperties,
+ replicateSubscriptionState: zc.options.ReplicateSubscriptionState,
+ startMessageID: zc.options.startMessageID,
+ startMessageIDInclusive: zc.options.StartMessageIDInclusive,
+ subscriptionMode: zc.options.SubscriptionMode,
+ readCompacted: zc.options.ReadCompacted,
+ interceptors: zc.options.Interceptors,
+ maxReconnectToBroker: zc.options.MaxReconnectToBroker,
+ backoffPolicy: zc.options.BackoffPolicy,
+ keySharedPolicy: zc.options.KeySharedPolicy,
+ schema: zc.options.Schema,
+ decryption: zc.options.Decryption,
+ ackWithResponse: zc.options.AckWithResponse,
+ maxPendingChunkedMessage: zc.options.MaxPendingChunkedMessage,
+ expireTimeOfIncompleteChunk: zc.options.ExpireTimeOfIncompleteChunk,
+ autoAckIncompleteChunk: zc.options.AutoAckIncompleteChunk,
+ consumerEventListener: zc.options.EventListener,
+ enableBatchIndexAck: zc.options.EnableBatchIndexAcknowledgment,
+ ackGroupingOptions: zc.options.AckGroupingOptions,
+ autoReceiverQueueSize: zc.options.EnableAutoScaledReceiverQueueSize,
+ }
+ conn, err := newPartitionConsumer(zc, zc.client, opts, zc.messageCh, zc.dlq, zc.metrics)
+ if err != nil {
+ return nil, err
+ }
+ zc.pc = conn
+
+ return zc, nil
+}
+
+func (z *zeroQueueConsumer) Subscription() string {
+ return z.options.SubscriptionName
+}
+
+func (z *zeroQueueConsumer) Unsubscribe() error {
+ return z.unsubscribe(false)
+}
+
+func (z *zeroQueueConsumer) UnsubscribeForce() error {
+ return z.unsubscribe(true)
+}
+
+func (z *zeroQueueConsumer) unsubscribe(force bool) error {
+ z.Lock()
+ defer z.Unlock()
+
+ if err := z.pc.unsubscribe(force); err != nil {
+ return errors.Errorf("topic %s, subscription %s: %v", z.topic, z.Subscription(), err)
+ }
+
+ return nil
+}
+
+func (z *zeroQueueConsumer) GetLastMessageIDs() ([]TopicMessageID, error) {
+ id, err := z.pc.getLastMessageID()
+ if err != nil {
+ return nil, err
+ }
+ tm := &topicMessageID{topic: z.pc.topic, track: id}
+ return []TopicMessageID{tm}, nil
+}
+
+func (z *zeroQueueConsumer) Receive(ctx context.Context) (Message, error) {
+ if state := z.pc.getConsumerState(); state == consumerClosed || state == consumerClosing {
+ z.log.WithField("state", state).Error("Failed to ack by closing or closed consumer")
+ return nil, errors.New("consumer state is closed")
+ }
+ z.Lock()
+ defer z.Unlock()
+ z.pc.availablePermits.inc()
+ for {
+ select {
+ case <-z.closeCh:
+ return nil, newError(ConsumerClosed, "consumer closed")
+ case cm, ok := <-z.messageCh:
+ if !ok {
+ return nil, newError(ConsumerClosed, "consumer closed")
+ }
+ return cm.Message, nil
+ case <-ctx.Done():
+ return nil, ctx.Err()
+ }
+ }
+
+}
+
+func (z *zeroQueueConsumer) Chan() <-chan ConsumerMessage {
+ z.log.Warnf("zeroQueueConsumer cannot support Chan method")
Review Comment:
Better to use error level log here.
What about panicking here?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org