-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce capability for publishing error Records to Dead letter topic #106
Merged
nithin-pankaj
merged 7 commits into
hyperledger-labs:main
from
nithin-pankaj:deadletter-publish
Nov 20, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
847d827
Clean up bloated exception blocks for Transaction submition operations
2ac10f9
Introduce custom Error handler for handling errors during Kafka messa…
a3e09da
Attach custom error handler for Listener Containers
9836e82
Incorporate new Error code
d0d1741
Introduce RetryableServiceException
f2e14bb
Remove explicit publish of failed message to EventPublishService
f08ca4d
Update appplication yaml template with Kafka Configs
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
src/main/java/hlf/java/rest/client/config/KafkaConsumerErrorHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package hlf.java.rest.client.config; | ||
|
||
import hlf.java.rest.client.exception.FabricTransactionException; | ||
import hlf.java.rest.client.exception.RetryableServiceException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.kafka.clients.consumer.Consumer; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.apache.kafka.common.TopicPartition; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.cloud.context.config.annotation.RefreshScope; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.kafka.listener.CommonErrorHandler; | ||
import org.springframework.kafka.listener.ConsumerAwareRecordRecoverer; | ||
import org.springframework.kafka.listener.DeadLetterPublishingRecoverer; | ||
import org.springframework.kafka.listener.DefaultErrorHandler; | ||
import org.springframework.util.backoff.FixedBackOff; | ||
|
||
@Configuration | ||
@ConditionalOnProperty("kafka.integration-points[0].brokerHost") | ||
@RefreshScope | ||
@Slf4j | ||
public class KafkaConsumerErrorHandler { | ||
|
||
private static final long RETRY_INTERVAL_IN_MS = 1000L; | ||
private static final int MAX_RETRY_ATTEMPTS = 5; | ||
private static final List<Class<? extends Exception>> connectorRetryableExceptions = | ||
Arrays.asList(RetryableServiceException.class, FabricTransactionException.class); | ||
|
||
@Autowired private KafkaProperties kafkaProperties; | ||
|
||
@Autowired private KafkaProducerConfig kafkaProducerConfig; | ||
|
||
@Bean | ||
public CommonErrorHandler topicTransactionErrorHandler() { | ||
|
||
ConsumerAwareRecordRecoverer deadLetterPublishingRecoverer = null; | ||
|
||
/** | ||
* Check if the runtime config has a valid Failed Message Listener. If a dedicated Failed | ||
* message listener is available, then the dead letter configuration would push the failed | ||
* message to the exclusive dead letter topic. If a dedicated failed message listener is not | ||
* configured, check if the event publisher topic can be utilised for publishing the failed | ||
* message. | ||
*/ | ||
if (Objects.nonNull(kafkaProperties.getFailedMessageListener()) | ||
&& StringUtils.isNotBlank(kafkaProperties.getFailedMessageListener().getTopic())) { | ||
|
||
deadLetterPublishingRecoverer = | ||
generateRecordRecovererWithPublisher(kafkaProperties.getFailedMessageListener()); | ||
} else if (Objects.nonNull(kafkaProperties.getEventListener()) | ||
&& kafkaProperties.getEventListener().isListenToFailedMessages()) { | ||
|
||
deadLetterPublishingRecoverer = | ||
generateRecordRecovererWithPublisher(kafkaProperties.getEventListener()); | ||
} | ||
|
||
/* | ||
If no topics are available to publish the failed message, fall-back to a 'NO-OP' record recoverer that would simply log | ||
the message after retry attempts | ||
*/ | ||
if (Objects.isNull(deadLetterPublishingRecoverer)) { | ||
deadLetterPublishingRecoverer = | ||
new ConsumerAwareRecordRecoverer() { | ||
@Override | ||
public void accept( | ||
ConsumerRecord<?, ?> consumerRecord, Consumer<?, ?> consumer, Exception e) { | ||
log.warn( | ||
"Retries exhausted.. Committing offset. Dead letter record is not published since the configuration doesnt use subscription topic for publishing failed messages " | ||
+ "nor it has a dead letter topic configured."); | ||
} | ||
}; | ||
} | ||
|
||
DefaultErrorHandler defaultErrorHandler = | ||
new DefaultErrorHandler( | ||
deadLetterPublishingRecoverer, | ||
new FixedBackOff(RETRY_INTERVAL_IN_MS, MAX_RETRY_ATTEMPTS)); | ||
defaultErrorHandler.setCommitRecovered(true); | ||
|
||
for (Class<? extends Exception> retryableExceptionClass : connectorRetryableExceptions) { | ||
defaultErrorHandler.addRetryableExceptions(retryableExceptionClass); | ||
} | ||
|
||
return defaultErrorHandler; | ||
} | ||
|
||
private DeadLetterPublishingRecoverer generateRecordRecovererWithPublisher( | ||
KafkaProperties.Producer destination) { | ||
|
||
KafkaTemplate<String, String> deadLetterPublisherTemplate = | ||
new KafkaTemplate<>(kafkaProducerConfig.eventProducerFactory(destination)); | ||
deadLetterPublisherTemplate.setDefaultTopic(destination.getTopic()); | ||
|
||
return new DeadLetterPublishingRecoverer( | ||
deadLetterPublisherTemplate, | ||
(cr, e) -> new TopicPartition(destination.getTopic(), cr.partition())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/hlf/java/rest/client/exception/RetryableServiceException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package hlf.java.rest.client.exception; | ||
|
||
public class RetryableServiceException extends ServiceException { | ||
|
||
public RetryableServiceException(ErrorCode code, String message, Throwable cause) { | ||
super(code, message, cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add the application.yml change for this in application.template file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure!