-
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
846824b
commit f3e0fa4
Showing
19 changed files
with
383 additions
and
40 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
150 changes: 150 additions & 0 deletions
150
...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,150 @@ | ||
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.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 | ||
.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
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.