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

pulsar connector produces message asynchronously #22026

Closed
wants to merge 1 commit into from
Closed
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 @@ -22,16 +22,25 @@
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

@DoFn.UnboundedPerElement
@SuppressWarnings({"rawtypes", "nullness"})
public class WriteToPulsarDoFn extends DoFn<byte[], Void> {

private static final Logger LOG = LoggerFactory.getLogger(WriteToPulsarDoFn.class);

private Producer<byte[]> producer;
private PulsarClient client;
private String clientUrl;
private String topic;

private transient Exception sendException = null;
private transient long numSendFailures = 0;

WriteToPulsarDoFn(PulsarIO.Write transform) {
this.clientUrl = transform.getClientUrl();
this.topic = transform.getTopic();
Expand All @@ -45,12 +54,48 @@ public void setup() throws PulsarClientException {

@ProcessElement
public void processElement(@Element byte[] messageToSend) throws Exception {
producer.send(messageToSend);
producer.sendAsync(messageToSend)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any concern of data loss here? Typically Beam is supposed to handle any threading or async itself. I'm thinking of the case where async is delayed, and Beam thinks a given element is completed, but it hasn't actually been sent to pulsar.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the concerns here, I've seen very troublesome async writers in Beam that were prone to loosing data. I suppose in this case it depends on the behavior of producer.flush() in finishBundle(). Does it block until all pending messages are send? If so, I'd suggest to add some comments to clarify.

.whenComplete((mid, exception) -> {
if (exception == null) {
return;
}

synchronized (WriteToPulsarDoFn.this) {
if (sendException == null) {
sendException = (Exception) exception;
}
numSendFailures++;
}
// don't log exception stacktrace here, exception will be propagated up.
LOG.warn("send failed : '{}'", exception.getMessage());
});
}

@FinishBundle
public void finishBundle() throws IOException {
producer.flush();
checkForFailures();
}

@Teardown
public void teardown() throws PulsarClientException {
producer.close();
client.close();
}

private synchronized void checkForFailures() throws IOException {
if (numSendFailures == 0) {
return;
}

String msg = String.format(
"Pulsar Write DoFn: failed to send %d records (since last report)", numSendFailures);

Exception e = sendException;
sendException = null;
numSendFailures = 0;

LOG.warn(msg);
throw new IOException(msg, e);
}
}