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

support null keys #127

Merged
merged 1 commit into from
Jul 3, 2019
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 @@ -214,20 +214,24 @@ public Flux<ReceiveReply> receive(Mono<ReceiveRequest> requestMono) {
records = records.transform(processor::postProcess);
}
return records
.map(consumerRecord -> ReceiveReply.newBuilder()
.setRecord(
ReceiveReply.Record.newBuilder()
.setOffset(consumerRecord.getOffset())
.setReplay(consumerRecord.getOffset() <= latestAckedOffsets.getOrDefault(partition, Optional.empty()).orElse(-1L))
.setKey(ByteString.copyFrom(consumerRecord.getEnvelope().getKey()))
.setValue(ByteString.copyFrom(consumerRecord.getEnvelope().getValue()))
.setTimestamp(Timestamp.newBuilder()
.setSeconds(consumerRecord.getTimestamp().getEpochSecond())
.setNanos(consumerRecord.getTimestamp().getNano())
)
)
.build()
);
.map(consumerRecord -> {
var envelope = consumerRecord.getEnvelope();

var replyBuilder = ReceiveReply.Record.newBuilder()
.setOffset(consumerRecord.getOffset())
.setReplay(consumerRecord.getOffset() <= latestAckedOffsets.getOrDefault(partition, Optional.empty()).orElse(-1L))
.setValue(ByteString.copyFrom(envelope.getValue()))
.setTimestamp(Timestamp.newBuilder()
.setSeconds(consumerRecord.getTimestamp().getEpochSecond())
.setNanos(consumerRecord.getTimestamp().getNano())
);

if (envelope.getKey() != null) {
replyBuilder.setKey(ByteString.copyFrom(envelope.getKey()));
}

return ReceiveReply.newBuilder().setRecord(replyBuilder).build();
});
});
})
.log("receive", Level.SEVERE, SignalType.ON_ERROR)
Expand Down
37 changes: 37 additions & 0 deletions app/src/test/java/com/github/bsideup/liiklus/SmokeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.bsideup.liiklus.protocol.ReceiveReply;
import com.github.bsideup.liiklus.protocol.ReceiveRequest;
import com.github.bsideup.liiklus.protocol.SubscribeRequest;
import com.github.bsideup.liiklus.records.RecordsStorage;
import com.github.bsideup.liiklus.test.AbstractIntegrationTest;
import com.google.protobuf.ByteString;
import org.assertj.core.api.Condition;
Expand All @@ -12,8 +13,11 @@
import reactor.core.publisher.Flux;
import reactor.core.publisher.SignalType;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -86,4 +90,37 @@ public boolean matches(ReceiveReply value) {
.extracting(it -> it.getRecord().getValue().toStringUtf8())
.containsSubsequence(values.toArray(new String[values.size()]));
}

@Test
public void testNullKey() throws Exception {
var subscribeAction = SubscribeRequest.newBuilder()
.setTopic(testName.getMethodName())
.setGroup(testName.getMethodName())
.setAutoOffsetReset(SubscribeRequest.AutoOffsetReset.EARLIEST)
.build();

var value = UUID.randomUUID().toString();
var recordsStorage = applicationContext.getBean(RecordsStorage.class);
recordsStorage.publish(new RecordsStorage.Envelope(
subscribeAction.getTopic(),
null, // intentionally
ByteBuffer.wrap(value.getBytes(StandardCharsets.UTF_8))
)).toCompletableFuture().join();

var record = stub
.subscribe(subscribeAction)
.flatMap(it -> stub.receive(
ReceiveRequest.newBuilder()
.setAssignment(it.getAssignment())
.build()
))
.map(ReceiveReply::getRecord)
.blockFirst(Duration.ofSeconds(10));

assertThat(record)
.isNotNull()
.satisfies(it -> {
assertThat(it.getValue().toStringUtf8()).as("value").isEqualTo(value);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -51,7 +52,9 @@ public CompletionStage<OffsetInfo> publish(Envelope envelope) {
var topic = envelope.getTopic();
var storedTopic = state.computeIfAbsent(topic, __ -> new StoredTopic(numberOfPartitions));

var partition = partitionByKey(envelope.getKey(), numberOfPartitions);
var partition = envelope.getKey() != null
? partitionByKey(envelope.getKey(), numberOfPartitions)
: ThreadLocalRandom.current().nextInt(0, numberOfPartitions);
var storedPartition = storedTopic.getPartitions().computeIfAbsent(
partition,
__ -> new StoredTopic.StoredPartition()
Expand Down Expand Up @@ -126,7 +129,7 @@ public Publisher<Record> getPublisher() {
.map(it -> new Record(
new Envelope(
topic,
it.getKey().asReadOnlyBuffer(),
it.getKey() != null ? it.getKey().asReadOnlyBuffer() : null,
it.getValue().asReadOnlyBuffer()
),
it.getTimestamp(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.junit.jupiter.api.Test;
import reactor.core.publisher.DirectProcessor;

import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -122,4 +123,25 @@ default void testInitialOffsets() throws Exception {
offsetInfos.get(9).getOffset()
);
}

@Test
default void testNullKey() throws Exception {
var topic = getTopic();
var offsetInfo = publish(new RecordsStorage.Envelope(
topic,
null,
ByteBuffer.wrap("hello".getBytes())
));
int partition = offsetInfo.getPartition();

var record = subscribeToPartition(partition)
.flatMap(RecordsStorage.PartitionSource::getPublisher)
.blockFirst(Duration.ofSeconds(10));

assertThat(record)
.isNotNull()
.satisfies(it -> {
assertThat(it.getOffset()).isEqualTo(offsetInfo.getOffset());
});
}
}