Skip to content
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

Change EventHubsTemplate to not use block() for async #40772

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,52 +101,43 @@ private Mono<Void> doSend(String destination, List<EventData> events, PartitionS
EventHubProducerAsyncClient producer = producerFactory.createProducer(destination);
CreateBatchOptions options = buildCreateBatchOptions(partitionSupplier);

EventDataBatch eventDataBatch = null;
try {
eventDataBatch = producer.createBatch(options).block();
} catch (Exception e) {
LOGGER.error("EventDataBatch create error.", e);
return Mono.error(e);
}
AtomicReference<EventDataBatch> currentBatch = new AtomicReference<>(eventDataBatch);

Flux.fromIterable(events).flatMap(event -> {
final EventDataBatch batch = currentBatch.get();
try {
if (batch.tryAdd(event)) {
AtomicReference<EventDataBatch> currentBatch = new AtomicReference<>();
return producer.createBatch(options)
.doOnSuccess(eventDataBatch -> {
currentBatch.set(eventDataBatch);
})
.then(Flux.fromIterable(events).flatMap(event -> {
final EventDataBatch batch = currentBatch.get();
try {
if (batch.tryAdd(event)) {
return Mono.empty();
} else {
LOGGER.warn("EventDataBatch is full in the collect process or the first event is "
+ "too large to fit in an empty batch! Max size: {}", batch.getMaxSizeInBytes());
}
} catch (AmqpException e) {
LOGGER.error("Event is larger than maximum allowed size.", e);
return Mono.empty();
} else {
LOGGER.warn("EventDataBatch is full in the collect process or the first event is "
+ "too large to fit in an empty batch! Max size: {}", batch.getMaxSizeInBytes());
}
} catch (AmqpException e) {
LOGGER.error("Event is larger than maximum allowed size.", e);
return Mono.empty();
}

return Mono.when(
producer.send(batch),
producer.createBatch(options).map(newBatch -> {
currentBatch.set(newBatch);
// Add the event that did not fit in the previous batch.
try {
if (!newBatch.tryAdd(event)) {
LOGGER.error("Event was too large to fit in an empty batch. Max size:{} ",
newBatch.getMaxSizeInBytes());
}
} catch (AmqpException e) {
LOGGER.error("Event was too large to fit in an empty batch. Max size:{}",
newBatch.getMaxSizeInBytes(), e);
}

return newBatch;
}));
})
.then()
.block();
return Mono.when(
producer.send(batch),
producer.createBatch(options).map(newBatch -> {
currentBatch.set(newBatch);
// Add the event that did not fit in the previous batch.
try {
if (!newBatch.tryAdd(event)) {
LOGGER.error("Event was too large to fit in an empty batch. Max size:{} ",
newBatch.getMaxSizeInBytes());
}
} catch (AmqpException e) {
LOGGER.error("Event was too large to fit in an empty batch. Max size:{}",
newBatch.getMaxSizeInBytes(), e);
}

final EventDataBatch batch = currentBatch.getAndSet(null);
return producer.send(batch);
return newBatch;
}));
}).then(Mono.defer(() -> producer.send(currentBatch.getAndSet(null)))));
}

private CreateBatchOptions buildCreateBatchOptions(PartitionSupplier partitionSupplier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import org.junit.jupiter.api.Test;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import reactor.test.StepVerifier;

import java.util.ArrayList;
Expand Down Expand Up @@ -78,6 +80,35 @@ void testSendAsyncForMessagesWithThreeBatch() {
verify(this.mockProducerClient, times(3)).send(any(EventDataBatch.class));
}

/**
* test the three batches case in parallel
*/
@Test
void testSendAsyncForMessagesWithThreeBatchParallel() {
EventDataBatch eventDataBatch = mock(EventDataBatch.class);

when(this.mockProducerClient.createBatch(any(CreateBatchOptions.class)))
.thenReturn(Mono.just(eventDataBatch));
when(eventDataBatch.tryAdd(any(EventData.class))).thenReturn(true, true, false, true, true,
false, true);
when(eventDataBatch.getCount()).thenReturn(2, 2, 1);
List<String> messagesList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
messagesList.add("abcde");
}
List<Message<String>> messages =
messagesList.stream().map((Function<String, GenericMessage<String>>) GenericMessage::new).collect(Collectors.toList());

Mono<Void> mono = Flux.just(messages)
.parallel()
.runOn(Schedulers.parallel())
.flatMap(msgs -> this.eventHubsTemplate.sendAsync(this.destination, messages, null))
.then();
StepVerifier.create(mono)
.verifyComplete();
verify(this.mockProducerClient, times(3)).send(any(EventDataBatch.class));
}

/**
* test the normal one batch case
*/
Expand Down
Loading