-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[vpj][changelog] Fix NPE in fetching dictionary from SOP message (#686)
- Loading branch information
1 parent
ac44e43
commit 7f82927
Showing
3 changed files
with
165 additions
and
149 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
159 changes: 159 additions & 0 deletions
159
internal/venice-common/src/test/java/com/linkedin/venice/utils/DictionaryUtilsTest.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,159 @@ | ||
package com.linkedin.venice.utils; | ||
|
||
import static org.mockito.ArgumentMatchers.anyLong; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import com.linkedin.venice.compression.CompressionStrategy; | ||
import com.linkedin.venice.kafka.protocol.ControlMessage; | ||
import com.linkedin.venice.kafka.protocol.KafkaMessageEnvelope; | ||
import com.linkedin.venice.kafka.protocol.Put; | ||
import com.linkedin.venice.kafka.protocol.StartOfPush; | ||
import com.linkedin.venice.kafka.protocol.enums.ControlMessageType; | ||
import com.linkedin.venice.kafka.protocol.enums.MessageType; | ||
import com.linkedin.venice.message.KafkaKey; | ||
import com.linkedin.venice.meta.Version; | ||
import com.linkedin.venice.pubsub.ImmutablePubSubMessage; | ||
import com.linkedin.venice.pubsub.PubSubTopicPartitionImpl; | ||
import com.linkedin.venice.pubsub.PubSubTopicRepository; | ||
import com.linkedin.venice.pubsub.api.PubSubConsumerAdapter; | ||
import com.linkedin.venice.pubsub.api.PubSubMessage; | ||
import com.linkedin.venice.pubsub.api.PubSubTopic; | ||
import com.linkedin.venice.pubsub.api.PubSubTopicPartition; | ||
import java.nio.ByteBuffer; | ||
import java.util.Collections; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
|
||
public class DictionaryUtilsTest { | ||
private final PubSubTopicRepository pubSubTopicRepository = new PubSubTopicRepository(); | ||
|
||
private PubSubTopic getTopic() { | ||
String callingFunction = Thread.currentThread().getStackTrace()[2].getMethodName(); | ||
return pubSubTopicRepository.getTopic(Version.composeKafkaTopic(Utils.getUniqueString(callingFunction), 1)); | ||
} | ||
|
||
@Test | ||
public void testGetDictionary() { | ||
PubSubTopic topic = getTopic(); | ||
byte[] dictionaryToSend = "TEST_DICT".getBytes(); | ||
|
||
PubSubConsumerAdapter pubSubConsumer = mock(PubSubConsumerAdapter.class); | ||
PubSubTopicRepository pubSubTopicRepository = mock(PubSubTopicRepository.class); | ||
PubSubTopicPartition topicPartition = new PubSubTopicPartitionImpl(topic, 0); | ||
doReturn(topic).when(pubSubTopicRepository).getTopic(topic.getName()); | ||
|
||
KafkaKey controlMessageKey = new KafkaKey(MessageType.CONTROL_MESSAGE, null); | ||
StartOfPush startOfPush = new StartOfPush(); | ||
startOfPush.compressionStrategy = CompressionStrategy.ZSTD_WITH_DICT.getValue(); | ||
startOfPush.compressionDictionary = ByteBuffer.wrap(dictionaryToSend); | ||
|
||
ControlMessage sopCM = new ControlMessage(); | ||
sopCM.controlMessageType = ControlMessageType.START_OF_PUSH.getValue(); | ||
sopCM.controlMessageUnion = startOfPush; | ||
KafkaMessageEnvelope sopWithDictionaryValue = | ||
new KafkaMessageEnvelope(MessageType.CONTROL_MESSAGE.getValue(), null, sopCM, null); | ||
PubSubMessage<KafkaKey, KafkaMessageEnvelope, Long> sopWithDictionary = | ||
new ImmutablePubSubMessage<>(controlMessageKey, sopWithDictionaryValue, topicPartition, 0L, 0L, 0); | ||
doReturn(Collections.singletonMap(topicPartition, Collections.singletonList(sopWithDictionary))) | ||
.when(pubSubConsumer) | ||
.poll(anyLong()); | ||
|
||
ByteBuffer dictionaryFromKafka = | ||
DictionaryUtils.readDictionaryFromKafka(topic.getName(), pubSubConsumer, pubSubTopicRepository); | ||
Assert.assertEquals(dictionaryFromKafka.array(), dictionaryToSend); | ||
verify(pubSubConsumer, times(1)).poll(anyLong()); | ||
} | ||
|
||
@Test | ||
public void testGetDictionaryReturnsNullWhenNoDictionary() { | ||
PubSubTopic topic = getTopic(); | ||
|
||
PubSubConsumerAdapter pubSubConsumer = mock(PubSubConsumerAdapter.class); | ||
PubSubTopicRepository pubSubTopicRepository = mock(PubSubTopicRepository.class); | ||
PubSubTopicPartition topicPartition = new PubSubTopicPartitionImpl(topic, 0); | ||
doReturn(topic).when(pubSubTopicRepository).getTopic(topic.getName()); | ||
|
||
KafkaKey controlMessageKey = new KafkaKey(MessageType.CONTROL_MESSAGE, null); | ||
StartOfPush startOfPush = new StartOfPush(); | ||
|
||
ControlMessage sopCM = new ControlMessage(); | ||
sopCM.controlMessageType = ControlMessageType.START_OF_PUSH.getValue(); | ||
sopCM.controlMessageUnion = startOfPush; | ||
KafkaMessageEnvelope sopWithDictionaryValue = | ||
new KafkaMessageEnvelope(MessageType.CONTROL_MESSAGE.getValue(), null, sopCM, null); | ||
PubSubMessage<KafkaKey, KafkaMessageEnvelope, Long> sopWithDictionary = | ||
new ImmutablePubSubMessage<>(controlMessageKey, sopWithDictionaryValue, topicPartition, 0L, 0L, 0); | ||
doReturn(Collections.singletonMap(topicPartition, Collections.singletonList(sopWithDictionary))) | ||
.when(pubSubConsumer) | ||
.poll(anyLong()); | ||
|
||
ByteBuffer dictionaryFromKafka = | ||
DictionaryUtils.readDictionaryFromKafka(topic.getName(), pubSubConsumer, pubSubTopicRepository); | ||
Assert.assertNull(dictionaryFromKafka); | ||
verify(pubSubConsumer, times(1)).poll(anyLong()); | ||
} | ||
|
||
@Test | ||
public void testGetDictionaryReturnsNullWhenNoSOP() { | ||
PubSubTopic topic = getTopic(); | ||
|
||
PubSubConsumerAdapter pubSubConsumer = mock(PubSubConsumerAdapter.class); | ||
PubSubTopicRepository pubSubTopicRepository = mock(PubSubTopicRepository.class); | ||
PubSubTopicPartition topicPartition = new PubSubTopicPartitionImpl(topic, 0); | ||
doReturn(topic).when(pubSubTopicRepository).getTopic(topic.getName()); | ||
|
||
KafkaKey dataMessageKey = new KafkaKey(MessageType.PUT, "blah".getBytes()); | ||
|
||
Put putMessage = new Put(); | ||
putMessage.putValue = ByteBuffer.wrap("blah".getBytes()); | ||
putMessage.schemaId = 1; | ||
KafkaMessageEnvelope putMessageValue = new KafkaMessageEnvelope(MessageType.PUT.getValue(), null, putMessage, null); | ||
PubSubMessage<KafkaKey, KafkaMessageEnvelope, Long> sopWithDictionary = | ||
new ImmutablePubSubMessage<>(dataMessageKey, putMessageValue, topicPartition, 0L, 0L, 0); | ||
doReturn(Collections.singletonMap(topicPartition, Collections.singletonList(sopWithDictionary))) | ||
.when(pubSubConsumer) | ||
.poll(anyLong()); | ||
|
||
ByteBuffer dictionaryFromKafka = | ||
DictionaryUtils.readDictionaryFromKafka(topic.getName(), pubSubConsumer, pubSubTopicRepository); | ||
Assert.assertNull(dictionaryFromKafka); | ||
verify(pubSubConsumer, times(1)).poll(anyLong()); | ||
} | ||
|
||
@Test | ||
public void testGetDictionaryWaitsTillTopicHasRecords() { | ||
PubSubTopic topic = getTopic(); | ||
byte[] dictionaryToSend = "TEST_DICT".getBytes(); | ||
|
||
PubSubConsumerAdapter pubSubConsumer = mock(PubSubConsumerAdapter.class); | ||
PubSubTopicRepository pubSubTopicRepository = mock(PubSubTopicRepository.class); | ||
PubSubTopicPartition topicPartition = new PubSubTopicPartitionImpl(topic, 0); | ||
doReturn(topic).when(pubSubTopicRepository).getTopic(topic.getName()); | ||
|
||
KafkaKey controlMessageKey = new KafkaKey(MessageType.CONTROL_MESSAGE, null); | ||
StartOfPush startOfPush = new StartOfPush(); | ||
startOfPush.compressionStrategy = CompressionStrategy.ZSTD_WITH_DICT.getValue(); | ||
startOfPush.compressionDictionary = ByteBuffer.wrap(dictionaryToSend); | ||
|
||
ControlMessage sopCM = new ControlMessage(); | ||
sopCM.controlMessageType = ControlMessageType.START_OF_PUSH.getValue(); | ||
sopCM.controlMessageUnion = startOfPush; | ||
KafkaMessageEnvelope sopWithDictionaryValue = | ||
new KafkaMessageEnvelope(MessageType.CONTROL_MESSAGE.getValue(), null, sopCM, null); | ||
PubSubMessage<KafkaKey, KafkaMessageEnvelope, Long> sopWithDictionary = | ||
new ImmutablePubSubMessage<>(controlMessageKey, sopWithDictionaryValue, topicPartition, 0L, 0L, 0); | ||
doReturn(Collections.emptyMap()) | ||
.doReturn(Collections.singletonMap(topicPartition, Collections.singletonList(sopWithDictionary))) | ||
.when(pubSubConsumer) | ||
.poll(anyLong()); | ||
|
||
ByteBuffer dictionaryFromKafka = | ||
DictionaryUtils.readDictionaryFromKafka(topic.getName(), pubSubConsumer, pubSubTopicRepository); | ||
Assert.assertEquals(dictionaryFromKafka.array(), dictionaryToSend); | ||
verify(pubSubConsumer, times(2)).poll(anyLong()); | ||
} | ||
} |
148 changes: 0 additions & 148 deletions
148
...e-test-common/src/integrationTest/java/com/linkedin/venice/utils/TestDictionaryUtils.java
This file was deleted.
Oops, something went wrong.