You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2020/02/13 05:39:59 UTC
[GitHub] [pulsar-client-go] merlimat opened a new pull request #184: Dead
letter queue implementation
merlimat opened a new pull request #184: Dead letter queue implementation
URL: https://github.com/apache/pulsar-client-go/pull/184
### Motivation
Added support for dead letter queue message routing
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [pulsar-client-go] cckellogg commented on a change in pull request
#184: Dead letter queue implementation
Posted by GitBox <gi...@apache.org>.
cckellogg commented on a change in pull request #184: Dead letter queue implementation
URL: https://github.com/apache/pulsar-client-go/pull/184#discussion_r378673396
##########
File path: pulsar/consumer_impl.go
##########
@@ -74,6 +75,11 @@ func newConsumer(client *client, options ConsumerOptions) (Consumer, error) {
messageCh = make(chan ConsumerMessage, 10)
}
+ dlq, err := newDlqRouter(client, options.DLQ)
Review comment:
what are the defaults here? will every topic by default have dlq enabled?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [pulsar-client-go] merlimat merged pull request #184: Dead letter
queue implementation
Posted by GitBox <gi...@apache.org>.
merlimat merged pull request #184: Dead letter queue implementation
URL: https://github.com/apache/pulsar-client-go/pull/184
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [pulsar-client-go] jerrypeng commented on a change in pull request
#184: Dead letter queue implementation
Posted by GitBox <gi...@apache.org>.
jerrypeng commented on a change in pull request #184: Dead letter queue implementation
URL: https://github.com/apache/pulsar-client-go/pull/184#discussion_r379010143
##########
File path: pulsar/consumer_partition.go
##########
@@ -402,23 +404,27 @@ func (pc *partitionConsumer) MessageReceived(response *pb.CommandMessage, header
var msg *message
if smm != nil {
msg = &message{
- publishTime: timeFromUnixTimestampMillis(msgMeta.GetPublishTime()),
- eventTime: timeFromUnixTimestampMillis(smm.GetEventTime()),
- key: smm.GetPartitionKey(),
- properties: internal.ConvertToStringMap(smm.GetProperties()),
- topic: pc.topic,
- msgID: msgID,
- payLoad: payload,
+ publishTime: timeFromUnixTimestampMillis(msgMeta.GetPublishTime()),
+ eventTime: timeFromUnixTimestampMillis(smm.GetEventTime()),
+ key: smm.GetPartitionKey(),
+ properties: internal.ConvertToStringMap(smm.GetProperties()),
+ topic: pc.topic,
+ msgID: msgID,
+ payLoad: payload,
+ replicationClusters: msgMeta.GetReplicateTo(),
Review comment:
why is this added?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [pulsar-client-go] merlimat commented on a change in pull request
#184: Dead letter queue implementation
Posted by GitBox <gi...@apache.org>.
merlimat commented on a change in pull request #184: Dead letter queue implementation
URL: https://github.com/apache/pulsar-client-go/pull/184#discussion_r379071753
##########
File path: pulsar/consumer_impl.go
##########
@@ -74,6 +75,11 @@ func newConsumer(client *client, options ConsumerOptions) (Consumer, error) {
messageCh = make(chan ConsumerMessage, 10)
}
+ dlq, err := newDlqRouter(client, options.DLQ)
Review comment:
By default the policy is `nil` and the it will result in no-op
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [pulsar-client-go] cckellogg commented on a change in pull request
#184: Dead letter queue implementation
Posted by GitBox <gi...@apache.org>.
cckellogg commented on a change in pull request #184: Dead letter queue implementation
URL: https://github.com/apache/pulsar-client-go/pull/184#discussion_r378673068
##########
File path: pulsar/dlq_router.go
##########
@@ -0,0 +1,147 @@
+// 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"
+ "errors"
+ "time"
+
+ "github.com/apache/pulsar-client-go/pulsar/internal"
+ log "github.com/sirupsen/logrus"
+)
+
+type dlqRouter struct {
+ client Client
+ producer Producer
+ policy *DLQPolicy
+ messageCh chan ConsumerMessage
+ closeCh chan interface{}
+ log *log.Entry
+}
+
+func newDlqRouter(client Client, policy *DLQPolicy) (*dlqRouter, error) {
+ r := &dlqRouter{
+ client: client,
+ policy: policy,
+ }
+
+ if policy != nil {
+ if policy.MaxDeliveries <= 0 {
+ return nil, errors.New("DLQPolicy.MaxDeliveries needs to be > 0")
+ }
+
+ if policy.Topic == "" {
+ return nil, errors.New("DLQPolicy.Topic needs to be set to a valid topic name")
+ }
+
+ r.messageCh = make(chan ConsumerMessage)
+ r.closeCh = make(chan interface{})
+ r.log = log.WithField("dlq-topic", policy.Topic)
+ go r.run()
+ }
+ return r, nil
+}
+
+func (r *dlqRouter) shouldSendToDlq(cm *ConsumerMessage) bool {
+ if r.policy == nil {
+ return false
+ }
+
+ msg := cm.Message.(*message)
+ r.log.WithField("count", msg.redeliveryCount).
+ WithField("max", r.policy.MaxDeliveries).
+ WithField("msgId", msg.msgID).
+ Debug("Should route to DLQ?")
+
+ // We use >= here because we're comparing the number of re-deliveries with
+ // the number of deliveries. So:
+ // * the user specifies that wants to process a message up to 10 times.
+ // * the first time, the redeliveryCount == 0, then 1 and so on
+ // * when we receive the message and redeliveryCount == 10, it means
+ // that the application has already got (and Nack()) the message 10
+ // times, so this time we should just go to DLQ.
+ return cm.Message.(*message).redeliveryCount >= r.policy.MaxDeliveries
+}
+
+func (r *dlqRouter) Chan() chan ConsumerMessage {
+ return r.messageCh
+}
+
+func (r *dlqRouter) run() {
+ for {
+ select {
+ case cm := <-r.messageCh:
+ r.log.WithField("msgID", cm.ID()).Debug("Got message for DLQ")
+ producer := r.getProducer()
+
+ msg := cm.Message.(*message)
+ msgID := msg.ID()
+ eventTime := msg.EventTime()
+ producer.SendAsync(context.Background(), &ProducerMessage{
+ Payload: msg.Payload(),
+ Key: msg.Key(),
+ Properties: msg.Properties(),
+ EventTime: &eventTime,
+ ReplicationClusters: msg.replicationClusters,
+ }, func(MessageID, *ProducerMessage, error) {
+ r.log.WithField("msgID", msgID).Debug("Sent message to DLQ")
+ cm.Consumer.AckID(msgID)
+ })
+
+ case <-r.closeCh:
+ if r.producer != nil {
+ r.producer.Close()
+ }
+ r.log.Debug("Closed DLQ router")
+ return
+ }
+ }
+}
+
+func (r *dlqRouter) close() {
+ if r.closeCh != nil {
+ r.closeCh <- nil
Review comment:
I think this could cause a deadlock for anything but a single topic because there will be multiple calls to close() and only the first call will be able to send on the closeCh because the go routine reading from the closeCh will exit.
We could do something like this:
```
select {
case r.closeCh <- nil: // this case will only occur when we can send on the channel
default:
// the closeCh is either nil or it's blocked and we can't send on it
}
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [pulsar-client-go] merlimat commented on a change in pull request
#184: Dead letter queue implementation
Posted by GitBox <gi...@apache.org>.
merlimat commented on a change in pull request #184: Dead letter queue implementation
URL: https://github.com/apache/pulsar-client-go/pull/184#discussion_r379071529
##########
File path: pulsar/dlq_router.go
##########
@@ -0,0 +1,147 @@
+// 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"
+ "errors"
+ "time"
+
+ "github.com/apache/pulsar-client-go/pulsar/internal"
+ log "github.com/sirupsen/logrus"
+)
+
+type dlqRouter struct {
+ client Client
+ producer Producer
+ policy *DLQPolicy
+ messageCh chan ConsumerMessage
+ closeCh chan interface{}
+ log *log.Entry
+}
+
+func newDlqRouter(client Client, policy *DLQPolicy) (*dlqRouter, error) {
+ r := &dlqRouter{
+ client: client,
+ policy: policy,
+ }
+
+ if policy != nil {
+ if policy.MaxDeliveries <= 0 {
+ return nil, errors.New("DLQPolicy.MaxDeliveries needs to be > 0")
+ }
+
+ if policy.Topic == "" {
+ return nil, errors.New("DLQPolicy.Topic needs to be set to a valid topic name")
+ }
+
+ r.messageCh = make(chan ConsumerMessage)
+ r.closeCh = make(chan interface{})
+ r.log = log.WithField("dlq-topic", policy.Topic)
+ go r.run()
+ }
+ return r, nil
+}
+
+func (r *dlqRouter) shouldSendToDlq(cm *ConsumerMessage) bool {
+ if r.policy == nil {
+ return false
+ }
+
+ msg := cm.Message.(*message)
+ r.log.WithField("count", msg.redeliveryCount).
+ WithField("max", r.policy.MaxDeliveries).
+ WithField("msgId", msg.msgID).
+ Debug("Should route to DLQ?")
+
+ // We use >= here because we're comparing the number of re-deliveries with
+ // the number of deliveries. So:
+ // * the user specifies that wants to process a message up to 10 times.
+ // * the first time, the redeliveryCount == 0, then 1 and so on
+ // * when we receive the message and redeliveryCount == 10, it means
+ // that the application has already got (and Nack()) the message 10
+ // times, so this time we should just go to DLQ.
+ return cm.Message.(*message).redeliveryCount >= r.policy.MaxDeliveries
+}
+
+func (r *dlqRouter) Chan() chan ConsumerMessage {
+ return r.messageCh
+}
+
+func (r *dlqRouter) run() {
+ for {
+ select {
+ case cm := <-r.messageCh:
+ r.log.WithField("msgID", cm.ID()).Debug("Got message for DLQ")
+ producer := r.getProducer()
+
+ msg := cm.Message.(*message)
+ msgID := msg.ID()
+ eventTime := msg.EventTime()
+ producer.SendAsync(context.Background(), &ProducerMessage{
+ Payload: msg.Payload(),
+ Key: msg.Key(),
+ Properties: msg.Properties(),
+ EventTime: &eventTime,
+ ReplicationClusters: msg.replicationClusters,
+ }, func(MessageID, *ProducerMessage, error) {
+ r.log.WithField("msgID", msgID).Debug("Sent message to DLQ")
+ cm.Consumer.AckID(msgID)
+ })
+
+ case <-r.closeCh:
+ if r.producer != nil {
+ r.producer.Close()
+ }
+ r.log.Debug("Closed DLQ router")
+ return
+ }
+ }
+}
+
+func (r *dlqRouter) close() {
+ if r.closeCh != nil {
+ r.closeCh <- nil
Review comment:
Added select and test case with multi topics
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [pulsar-client-go] cckellogg commented on a change in pull request
#184: Dead letter queue implementation
Posted by GitBox <gi...@apache.org>.
cckellogg commented on a change in pull request #184: Dead letter queue implementation
URL: https://github.com/apache/pulsar-client-go/pull/184#discussion_r378673068
##########
File path: pulsar/dlq_router.go
##########
@@ -0,0 +1,147 @@
+// 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"
+ "errors"
+ "time"
+
+ "github.com/apache/pulsar-client-go/pulsar/internal"
+ log "github.com/sirupsen/logrus"
+)
+
+type dlqRouter struct {
+ client Client
+ producer Producer
+ policy *DLQPolicy
+ messageCh chan ConsumerMessage
+ closeCh chan interface{}
+ log *log.Entry
+}
+
+func newDlqRouter(client Client, policy *DLQPolicy) (*dlqRouter, error) {
+ r := &dlqRouter{
+ client: client,
+ policy: policy,
+ }
+
+ if policy != nil {
+ if policy.MaxDeliveries <= 0 {
+ return nil, errors.New("DLQPolicy.MaxDeliveries needs to be > 0")
+ }
+
+ if policy.Topic == "" {
+ return nil, errors.New("DLQPolicy.Topic needs to be set to a valid topic name")
+ }
+
+ r.messageCh = make(chan ConsumerMessage)
+ r.closeCh = make(chan interface{})
+ r.log = log.WithField("dlq-topic", policy.Topic)
+ go r.run()
+ }
+ return r, nil
+}
+
+func (r *dlqRouter) shouldSendToDlq(cm *ConsumerMessage) bool {
+ if r.policy == nil {
+ return false
+ }
+
+ msg := cm.Message.(*message)
+ r.log.WithField("count", msg.redeliveryCount).
+ WithField("max", r.policy.MaxDeliveries).
+ WithField("msgId", msg.msgID).
+ Debug("Should route to DLQ?")
+
+ // We use >= here because we're comparing the number of re-deliveries with
+ // the number of deliveries. So:
+ // * the user specifies that wants to process a message up to 10 times.
+ // * the first time, the redeliveryCount == 0, then 1 and so on
+ // * when we receive the message and redeliveryCount == 10, it means
+ // that the application has already got (and Nack()) the message 10
+ // times, so this time we should just go to DLQ.
+ return cm.Message.(*message).redeliveryCount >= r.policy.MaxDeliveries
+}
+
+func (r *dlqRouter) Chan() chan ConsumerMessage {
+ return r.messageCh
+}
+
+func (r *dlqRouter) run() {
+ for {
+ select {
+ case cm := <-r.messageCh:
+ r.log.WithField("msgID", cm.ID()).Debug("Got message for DLQ")
+ producer := r.getProducer()
+
+ msg := cm.Message.(*message)
+ msgID := msg.ID()
+ eventTime := msg.EventTime()
+ producer.SendAsync(context.Background(), &ProducerMessage{
+ Payload: msg.Payload(),
+ Key: msg.Key(),
+ Properties: msg.Properties(),
+ EventTime: &eventTime,
+ ReplicationClusters: msg.replicationClusters,
+ }, func(MessageID, *ProducerMessage, error) {
+ r.log.WithField("msgID", msgID).Debug("Sent message to DLQ")
+ cm.Consumer.AckID(msgID)
+ })
+
+ case <-r.closeCh:
+ if r.producer != nil {
+ r.producer.Close()
+ }
+ r.log.Debug("Closed DLQ router")
+ return
+ }
+ }
+}
+
+func (r *dlqRouter) close() {
+ if r.closeCh != nil {
+ r.closeCh <- nil
Review comment:
Could this cause a deadlock for anything but a single topic? Could there will be multiple calls to close()?
We could do something like this:
```
select {
case r.closeCh <- nil: // this case will only occur when we can send on the channel
default:
// the closeCh is either nil or it's blocked and we can't send on it
}
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [pulsar-client-go] merlimat commented on a change in pull request
#184: Dead letter queue implementation
Posted by GitBox <gi...@apache.org>.
merlimat commented on a change in pull request #184: Dead letter queue implementation
URL: https://github.com/apache/pulsar-client-go/pull/184#discussion_r379072208
##########
File path: pulsar/consumer_partition.go
##########
@@ -402,23 +404,27 @@ func (pc *partitionConsumer) MessageReceived(response *pb.CommandMessage, header
var msg *message
if smm != nil {
msg = &message{
- publishTime: timeFromUnixTimestampMillis(msgMeta.GetPublishTime()),
- eventTime: timeFromUnixTimestampMillis(smm.GetEventTime()),
- key: smm.GetPartitionKey(),
- properties: internal.ConvertToStringMap(smm.GetProperties()),
- topic: pc.topic,
- msgID: msgID,
- payLoad: payload,
+ publishTime: timeFromUnixTimestampMillis(msgMeta.GetPublishTime()),
+ eventTime: timeFromUnixTimestampMillis(smm.GetEventTime()),
+ key: smm.GetPartitionKey(),
+ properties: internal.ConvertToStringMap(smm.GetProperties()),
+ topic: pc.topic,
+ msgID: msgID,
+ payLoad: payload,
+ replicationClusters: msgMeta.GetReplicateTo(),
Review comment:
We need to copy all properties of the original message and noticed that this was not being exposed in the internal struct.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services