Skip to content

Commit

Permalink
DltAwareProcessor improvements
Browse files Browse the repository at this point in the history
 - Instead of using a BiFunction as a delegate, use standard Function that takes the full record
 - Remove Supplier<Long> that was used to handle record time stamps since this is no longer needed
 - Docs cleanup
  • Loading branch information
sobychacko committed Sep 27, 2023
1 parent ca2d304 commit bca3dd5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
package org.springframework.cloud.stream.binder.kafka.streams;

import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Supplier;
import java.util.function.Function;

import org.apache.kafka.streams.KeyValue;
import org.apache.kafka.streams.processor.api.Processor;
import org.apache.kafka.streams.processor.api.ProcessorContext;
import org.apache.kafka.streams.processor.api.Record;
Expand All @@ -39,14 +37,9 @@
public class DltAwareProcessor<KIn, VIn, KOut, VOut> implements Processor<KIn, VIn, KOut, VOut> {

/**
* Delegate {@link BiFunction} that is responsible for processing the data.
* Delegate {@link Function} that is responsible for processing the data.
*/
private final BiFunction<KIn, VIn, KeyValue<KOut, VOut>> delegateFunction;

/**
* Event time for the forwarded downstream record.
*/
private final Supplier<Long> recordTimeSupplier;
private final Function<Record<KIn, VIn>, Record<KOut, VOut>> delegateFunction;

/**
* DLT destination.
Expand All @@ -70,26 +63,13 @@ public class DltAwareProcessor<KIn, VIn, KOut, VOut> implements Processor<KIn, V

/**
*
* @param delegateFunction {@link BiFunction} to process the data
* @param delegateFunction {@link Function} to process the data
* @param dltDestination DLT destination
* @param dltPublishingContext {@link DltPublishingContext}
*/
public DltAwareProcessor(BiFunction<KIn, VIn, KeyValue<KOut, VOut>> delegateFunction, String dltDestination,
public DltAwareProcessor(Function<Record<KIn, VIn>, Record<KOut, VOut>> delegateFunction, String dltDestination,
DltPublishingContext dltPublishingContext) {
this(delegateFunction, dltDestination, dltPublishingContext, System::currentTimeMillis);
}

/**
*
* @param delegateFunction {@link BiFunction} to process the data
* @param dltDestination DLT destination
* @param dltPublishingContext {@link DltPublishingContext}
* @param recordTimeSupplier Supplier for downstream record timestamp
*/
public DltAwareProcessor(BiFunction<KIn, VIn, KeyValue<KOut, VOut>> delegateFunction, String dltDestination,
DltPublishingContext dltPublishingContext, Supplier<Long> recordTimeSupplier) {
this.delegateFunction = delegateFunction;
this.recordTimeSupplier = recordTimeSupplier;
Assert.isTrue(StringUtils.hasText(dltDestination), "DLT Destination topic must be provided.");
this.dltDestination = dltDestination;
Assert.notNull(dltPublishingContext, "DltSenderContext cannot be null");
Expand All @@ -98,24 +78,12 @@ public DltAwareProcessor(BiFunction<KIn, VIn, KeyValue<KOut, VOut>> delegateFunc

/**
*
* @param delegateFunction {@link BiFunction} to process the data
* @param delegateFunction {@link Function} to process the data
* @param processorRecordRecoverer {@link BiConsumer} that recovers failed records
*/
public DltAwareProcessor(BiFunction<KIn, VIn, KeyValue<KOut, VOut>> delegateFunction,
public DltAwareProcessor(Function<Record<KIn, VIn>, Record<KOut, VOut>> delegateFunction,
BiConsumer<Record<KIn, VIn>, Exception> processorRecordRecoverer) {
this(delegateFunction, processorRecordRecoverer, System::currentTimeMillis);
}

/**
*
* @param delegateFunction {@link BiFunction} to process the data
* @param processorRecordRecoverer {@link BiConsumer} that recovers failed records
* @param recordTimeSupplier Supplier for downstream record timestamp
*/
public DltAwareProcessor(BiFunction<KIn, VIn, KeyValue<KOut, VOut>> delegateFunction,
BiConsumer<Record<KIn, VIn>, Exception> processorRecordRecoverer, Supplier<Long> recordTimeSupplier) {
this.delegateFunction = delegateFunction;
this.recordTimeSupplier = recordTimeSupplier;
Assert.notNull(processorRecordRecoverer, "You must provide a valid processor recoverer");
this.processorRecordRecoverer = processorRecordRecoverer;
}
Expand All @@ -129,8 +97,7 @@ public void init(ProcessorContext<KOut, VOut> context) {
@Override
public void process(Record<KIn, VIn> record) {
try {
KeyValue<KOut, VOut> keyValue = this.delegateFunction.apply(record.key(), record.value());
Record<KOut, VOut> downstreamRecord = new Record<>(keyValue.key, keyValue.value, recordTimeSupplier.get(), record.headers());
Record<KOut, VOut> downstreamRecord = this.delegateFunction.apply(record);
this.context.forward(downstreamRecord);
}
catch (Exception exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ static class PublishToDltOnErrorApplication {
@Bean
public java.util.function.Consumer<KStream<String, String>> errorStream(DltPublishingContext dltSenderContext) {
return input -> input
.process(() -> new DltAwareProcessor<>((k, v) -> {
.process(() -> new DltAwareProcessor<>(rec -> {
throw new RuntimeException("error");
}, "hello-dlt-1", dltSenderContext));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,30 +158,28 @@ Here is how you can do that.
@Bean
public java.util.function.Function<KStream<String, String>, KStream<String, String>> process(DltPublishingContext dltSenderContext) {
return input -> input
.process(() -> new DltAwareProcessor<>((k, v) -> {
.process(() -> new DltAwareProcessor<>(record -> {
throw new RuntimeException("error");
}, "hello-dlt-1", dltPublishingContext));
}
```

The business logic code from the original `map` call now has been moved as part of `KStream#process` method call, which takes a `ProcessorSupplier`.
We, then, pass in the custom `DltAwareProcessor,` which is capable to publishing to a DLT.
The constructor for `DltAwareProcessor` above takes three parameters - a `BiFunction` that takes the key and value of the input record and then the business logic operation as part of the `BiFunction` body, the DLT topic, and finally a `DltPublishingContext`. When the `BiFunction`'s lambda expression throws an exception, the `DltAwareProcessor` will send the input record to a DLT. The `DltPublishingContext` provides `DltAwareProcessor` the necessary publishing infrastructure beans.
The constructor for `DltAwareProcessor` above takes three parameters - a `Function` that takes the input record and then the business logic operation as part of the `Function` body, the DLT topic, and finally a `DltPublishingContext`. When the `Function`'s lambda expression throws an exception, the `DltAwareProcessor` will send the input record to a DLT. The `DltPublishingContext` provides `DltAwareProcessor` the necessary publishing infrastructure beans.
The `DltPublishingContext` is autoconfigured by the binder, so that you can inject directly this into the application.

If you want to provide a custom timestamp on the record that gets published to the DLT, then you can provide an optional fourth constructor argument which is a `Supplier<Long>`.
If this value is provided, then this `Supplier` is invoked each time `DltAwareProcessor` publishes to the DLT.

If you do not want the binder to publish failed records to a DLT, then you can provide your own recoverer as a `BiConsumer`.
If you do not want the binder to publish failed records to a DLT, then you can provide your own recoverer as a `BiConsumer` that takes the input `Record` and the exception as arguments.
Assume a scenario, in which you do not want to send the record to the DLT, but simply log the message and move on.
For this, it is convenient, if we can override the recovery process in `DltAwareProcessor`.
Here is an example of how you do that.
It is convenient, if we can override the default recovery mechanism provided by the `DltAwareProcessor`.

Here is an example.

```
@Bean
public java.util.function.Function<KStream<String, String>, KStream<String, String>> process() {
return input -> input
.process(() -> new DltAwareProcessor<>((k, v) -> {
.process(() -> new DltAwareProcessor<>(record -> {
throw new RuntimeException("error");
},
(record, exception) -> {
Expand All @@ -191,5 +189,3 @@ public java.util.function.Function<KStream<String, String>, KStream<String, Stri
```

In this case, when the record fails, the `DltAwareProcessor`, instead of using its built-in recoverer which publishes to a DLT, uses the user provided recoverer which is a `BiConsumer` that takes the failed record and the exception thrown as arguments.
In this case also, you can provide an optional `Supplier<Long>` to dictate the timestamp used in the record passed in to the `BiConsumer` recoverer.

0 comments on commit bca3dd5

Please sign in to comment.