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

[Reader] Should set either start message id or start message from roll back duration. #6392

Merged
merged 3 commits into from
Feb 24, 2020
Merged
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 @@ -242,7 +242,7 @@ public void testReaderWithTimeLong() throws Exception {

// (3) Create reader and set position 1 hour back so, it should only read messages which are 2 hours old which
// published on step 2
Reader<byte[]> reader = pulsarClient.newReader().topic(topic).startMessageId(MessageId.earliest)
Reader<byte[]> reader = pulsarClient.newReader().topic(topic)
.startMessageFromRollbackDuration(2, TimeUnit.HOURS).create();

List<MessageId> receivedMessageIds = Lists.newArrayList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,15 @@ public CompletableFuture<Reader<T>> createAsync() {
.failedFuture(new IllegalArgumentException("Topic name must be set on the reader builder"));
}

if (conf.getStartMessageId() == null) {
if (conf.getStartMessageId() != null && conf.getStartMessageFromRollbackDurationInSec() > 0 ||
conf.getStartMessageId() == null && conf.getStartMessageFromRollbackDurationInSec() <= 0) {
return FutureUtil
.failedFuture(new IllegalArgumentException("Start message id must be set on the reader builder"));
.failedFuture(new IllegalArgumentException(
"Start message id or start message from roll back must be specified but they cannot be specified at the same time"));
}

if (conf.getStartMessageFromRollbackDurationInSec() > 0) {
conf.setStartMessageId(MessageId.earliest);
}

return client.createReaderAsync(conf, schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.pulsar.client.api.MessageId;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.Reader;
import org.apache.pulsar.client.impl.conf.ReaderConfigurationData;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -96,5 +100,26 @@ public void readerBuilderLoadConfTest() throws Exception {
assertTrue(obj instanceof ReaderConfigurationData);
assertEquals(((ReaderConfigurationData) obj).getTopicName(), topicName);
assertEquals(((ReaderConfigurationData) obj).getStartMessageId(), messageId);
client.close();
}

@Test(expectedExceptions = {PulsarClientException.class}, expectedExceptionsMessageRegExp = ".* must be specified but they cannot be specified at the same time.*")
public void shouldNotSetTwoOptAtTheSameTime() throws Exception {
PulsarClient client = PulsarClient.builder().serviceUrl("pulsar://localhost:6650").build();
try (Reader reader = client.newReader().topic("abc").startMessageId(MessageId.earliest).startMessageFromRollbackDuration(10, TimeUnit.HOURS).create()) {
// no-op
} finally {
client.close();
}
}

@Test(expectedExceptions = {PulsarClientException.class}, expectedExceptionsMessageRegExp = ".* must be specified but they cannot be specified at the same time.*")
public void shouldSetOneStartOpt() throws Exception {
PulsarClient client = PulsarClient.builder().serviceUrl("pulsar://localhost:6650").build();
try (Reader reader = client.newReader().topic("abc").create()) {
// no-op
} finally {
client.close();
}
}
}