-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle non-partitioned Pulsar topics (#260)
Apparently, Pulsar uses a magical -1 value for the partition number when the topic is not partitioned.
- Loading branch information
Showing
6 changed files
with
110 additions
and
81 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
79 changes: 79 additions & 0 deletions
79
...age/src/test/java/com/github/bsideup/liiklus/pulsar/AbstractPulsarRecordsStorageTest.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,79 @@ | ||
package com.github.bsideup.liiklus.pulsar; | ||
|
||
import com.github.bsideup.liiklus.ApplicationRunner; | ||
import com.github.bsideup.liiklus.records.RecordStorageTests; | ||
import com.github.bsideup.liiklus.records.RecordsStorage; | ||
import com.github.bsideup.liiklus.support.DisabledUntil; | ||
import lombok.Getter; | ||
import org.apache.pulsar.client.api.PulsarClient; | ||
import org.apache.pulsar.client.impl.MessageIdImpl; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.context.ApplicationContext; | ||
import org.testcontainers.containers.PulsarContainer; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.UUID; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
abstract class AbstractPulsarRecordsStorageTest implements RecordStorageTests { | ||
|
||
static final PulsarContainer pulsar = new PulsarContainer("2.5.0") | ||
.withReuse(true); | ||
|
||
private static final ApplicationContext applicationContext; | ||
|
||
static { | ||
pulsar.start(); | ||
|
||
applicationContext = new ApplicationRunner("PULSAR", "MEMORY") | ||
.withProperty("pulsar.serviceUrl", pulsar.getPulsarBrokerUrl()) | ||
.run(); | ||
} | ||
|
||
@Getter | ||
RecordsStorage target = applicationContext.getBean(RecordsStorage.class); | ||
|
||
@Getter | ||
String topic = UUID.randomUUID().toString(); | ||
|
||
@Override | ||
@Test | ||
@DisabledUntil(value = "2020-03-01", comment = "#180 - Pulsar should fix the way seek works, not disconnecting consumers (apache/pulsar/pull/5022)") | ||
public void shouldAlwaysUseEarliestOffsetOnEmptyOffsetsInTheInitialProvider() { | ||
RecordStorageTests.super.shouldAlwaysUseEarliestOffsetOnEmptyOffsetsInTheInitialProvider(); | ||
} | ||
|
||
@Test | ||
void shouldPreferEventTimeOverPublishTime() throws Exception { | ||
var topic = getTopic(); | ||
var eventTimestamp = Instant.now().minusSeconds(1000).truncatedTo(ChronoUnit.MILLIS); | ||
|
||
int partition; | ||
try ( | ||
var pulsarClient = PulsarClient.builder() | ||
.serviceUrl(pulsar.getPulsarBrokerUrl()) | ||
.build() | ||
) { | ||
var messageId = pulsarClient.newProducer() | ||
.topic(topic) | ||
.create() | ||
.newMessage() | ||
.value("hello".getBytes()) | ||
.eventTime(eventTimestamp.toEpochMilli()) | ||
.send(); | ||
|
||
partition = ((MessageIdImpl) messageId).getPartitionIndex(); | ||
} | ||
|
||
var record = subscribeToPartition(partition) | ||
.flatMap(RecordsStorage.PartitionSource::getPublisher) | ||
.blockFirst(Duration.ofSeconds(10)); | ||
|
||
assertThat(record).satisfies(it -> { | ||
assertThat(it.getTimestamp()).isEqualTo(eventTimestamp); | ||
}); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...c/test/java/com/github/bsideup/liiklus/pulsar/NonPartitionedPulsarRecordsStorageTest.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,26 @@ | ||
package com.github.bsideup.liiklus.pulsar; | ||
|
||
import lombok.SneakyThrows; | ||
import org.apache.pulsar.client.admin.PulsarAdmin; | ||
|
||
public class NonPartitionedPulsarRecordsStorageTest extends AbstractPulsarRecordsStorageTest { | ||
|
||
@SneakyThrows | ||
public NonPartitionedPulsarRecordsStorageTest() { | ||
PulsarAdmin pulsarAdmin = PulsarAdmin.builder() | ||
.serviceHttpUrl(pulsar.getHttpServiceUrl()) | ||
.build(); | ||
|
||
pulsarAdmin.topics().createNonPartitionedTopic(topic); | ||
} | ||
|
||
@Override | ||
public String keyByPartition(int partition) { | ||
return "foo"; | ||
} | ||
|
||
@Override | ||
public int getNumberOfPartitions() { | ||
return 1; | ||
} | ||
} |
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