-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e76d5cf
commit b2fee86
Showing
11 changed files
with
226 additions
and
18 deletions.
There are no files selected for viewing
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
151 changes: 151 additions & 0 deletions
151
...afka/src/test/java/io/smallrye/reactive/messaging/kafka/commit/StreamAckStrategyTest.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,151 @@ | ||
package io.smallrye.reactive.messaging.kafka.commit; | ||
|
||
import static org.awaitility.Awaitility.await; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import java.util.UUID; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
|
||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.apache.kafka.common.serialization.IntegerDeserializer; | ||
import org.apache.kafka.common.serialization.IntegerSerializer; | ||
import org.apache.kafka.common.serialization.StringSerializer; | ||
import org.eclipse.microprofile.reactive.messaging.Channel; | ||
import org.eclipse.microprofile.reactive.messaging.Incoming; | ||
import org.eclipse.microprofile.reactive.messaging.Message; | ||
import org.eclipse.microprofile.reactive.messaging.Outgoing; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.smallrye.mutiny.Multi; | ||
import io.smallrye.mutiny.Uni; | ||
import io.smallrye.mutiny.infrastructure.Infrastructure; | ||
import io.smallrye.reactive.messaging.MutinyEmitter; | ||
import io.smallrye.reactive.messaging.annotations.Broadcast; | ||
import io.smallrye.reactive.messaging.kafka.KafkaRecord; | ||
import io.smallrye.reactive.messaging.kafka.base.KafkaCompanionTestBase; | ||
import io.smallrye.reactive.messaging.providers.locals.ContextOperator; | ||
import io.smallrye.reactive.messaging.test.common.config.MapBasedConfig; | ||
import io.vertx.core.Context; | ||
import io.vertx.core.Vertx; | ||
|
||
public class StreamAckStrategyTest extends KafkaCompanionTestBase { | ||
|
||
@Test | ||
public void testWithPartitions() { | ||
Infrastructure.setOperatorLogger(new Infrastructure.OperatorLogger() { | ||
@Override | ||
public void log(String identifier, String event, Object value, Throwable failure) { | ||
String message = "[--> " + identifier + " | " + event; | ||
if (failure == null) { | ||
if (value != null) { | ||
message = message + "(" + value + ")"; | ||
} else { | ||
message = message + "()"; | ||
} | ||
} else { | ||
message = message + "(" + failure.getClass().getName() + "(\"" + failure.getMessage() + "\"))"; | ||
} | ||
Context context = Vertx.currentContext(); | ||
if (context != null) { | ||
message = message + " " + context; | ||
} | ||
System.out.println(message + " " + Thread.currentThread().getName()); | ||
} | ||
}); | ||
companion.topics().createAndWait(topic, 3); | ||
String sinkTopic = topic + "-sink"; | ||
companion.topics().createAndWait(sinkTopic, 3); | ||
String groupId = UUID.randomUUID().toString(); | ||
|
||
MapBasedConfig config = kafkaConfig("mp.messaging.incoming.kafka") | ||
.with("group.id", groupId) | ||
.with("topic", topic) | ||
.with("partitions", 3) | ||
.with("auto.offset.reset", "earliest") | ||
.with("commit-strategy", "throttled") | ||
.with("value.deserializer", IntegerDeserializer.class.getName()) | ||
.withPrefix("mp.messaging.outgoing.sink") | ||
.with("connector", "smallrye-kafka") | ||
.with("topic", sinkTopic) | ||
.with("value.serializer", IntegerSerializer.class.getName()) | ||
.withPrefix("mp.messaging.outgoing.out") | ||
.with("connector", "smallrye-kafka") | ||
.with("topic", topic + "-out") | ||
.with("value.serializer", StringSerializer.class.getName()); | ||
|
||
DoubleAckBean application = runApplication(config, DoubleAckBean.class); | ||
|
||
int expected = 3000; | ||
Random random = new Random(); | ||
companion.produceIntegers().usingGenerator(i -> { | ||
int p = random.nextInt(3); | ||
return new ProducerRecord<>(topic, p, Integer.toString(p), i); | ||
}, expected).awaitCompletion(Duration.ofMinutes(1)); | ||
|
||
await().atMost(1, TimeUnit.MINUTES) | ||
.until(() -> application.count() >= expected); | ||
|
||
companion.consumeIntegers().fromTopics(sinkTopic, expected) | ||
.awaitCompletion(Duration.ofMinutes(1)); | ||
} | ||
|
||
@ApplicationScoped | ||
public static class DoubleAckBean { | ||
private final AtomicLong count = new AtomicLong(); | ||
private final Map<String, List<Integer>> received = new ConcurrentHashMap<>(); | ||
|
||
private final AtomicLong parallel = new AtomicLong(); | ||
|
||
@Inject | ||
@Channel("out") | ||
MutinyEmitter<String> emitter; | ||
|
||
@Incoming("kafka") | ||
@Outgoing("sink") | ||
@Broadcast | ||
public Multi<Message<Integer>> consume(Multi<KafkaRecord<String, Integer>> records) { | ||
return records.plug(ContextOperator::apply) | ||
.log("upstream") | ||
.call(m -> emitter.send(m.getPayload() + "")) | ||
// .call(m -> Uni.createFrom().voidItem().emitOn(Infrastructure.getDefaultExecutor())) | ||
.invoke(msg -> { | ||
String k = Thread.currentThread().getName(); | ||
System.out.println(parallel.incrementAndGet() + " " + k + " " + Vertx.currentContext() + " / " | ||
+ msg.getContextMetadata().get().context()); | ||
List<Integer> list = received.computeIfAbsent(k, s -> new CopyOnWriteArrayList<>()); | ||
list.add(msg.getPayload()); | ||
count.incrementAndGet(); | ||
}) | ||
.group().by(msg -> msg.getPartition()) | ||
.flatMap(group -> { | ||
System.out.println("key " + group.key()); | ||
return group.onItem().scan((l, r) -> { | ||
return l; | ||
}); | ||
}) | ||
.log("downstream") | ||
// .call(m -> Uni.createFrom().voidItem().emitOn(Infrastructure.getDefaultExecutor())) | ||
.call(m -> Uni.createFrom().completionStage(m::ack)) | ||
.invoke(() -> parallel.decrementAndGet()) | ||
.map(msg -> msg.withPayload(msg.getPayload() + 1)); | ||
} | ||
|
||
public Map<String, List<Integer>> getReceived() { | ||
return received; | ||
} | ||
|
||
public long count() { | ||
return count.get(); | ||
} | ||
} | ||
|
||
} |
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
28 changes: 28 additions & 0 deletions
28
...vider/src/main/java/io/smallrye/reactive/messaging/providers/locals/ContextDecorator.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,28 @@ | ||
package io.smallrye.reactive.messaging.providers.locals; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
|
||
import org.eclipse.microprofile.reactive.messaging.Message; | ||
|
||
import io.smallrye.mutiny.Multi; | ||
import io.smallrye.reactive.messaging.PublisherDecorator; | ||
|
||
/** | ||
* Decorator to dispatch messages on the Vert.x context attached to the message via {@link LocalContextMetadata}. | ||
* Low priority to be called before other decorators. | ||
*/ | ||
@ApplicationScoped | ||
public class ContextDecorator implements PublisherDecorator { | ||
|
||
@Override | ||
public int getPriority() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public Multi<? extends Message<?>> decorate(Multi<? extends Message<?>> publisher, String channelName, | ||
boolean isConnector) { | ||
return ContextOperator.apply(publisher); | ||
} | ||
|
||
} |
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
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