Search in sources :

Example 1 with MAGIC_VALUE_V2

use of org.apache.kafka.common.record.RecordBatch.MAGIC_VALUE_V2 in project kafka by apache.

the class MemoryRecordsBuilderTest method testWriteLeaderChangeControlBatch.

@ParameterizedTest
@ArgumentsSource(MemoryRecordsBuilderArgumentsProvider.class)
public void testWriteLeaderChangeControlBatch(Args args) {
    ByteBuffer buffer = allocateBuffer(128, args);
    final int leaderId = 1;
    final int leaderEpoch = 5;
    final List<Integer> voters = Arrays.asList(2, 3);
    Supplier<MemoryRecordsBuilder> supplier = () -> new MemoryRecordsBuilder(buffer, args.magic, args.compressionType, TimestampType.CREATE_TIME, 0L, 0L, RecordBatch.NO_PRODUCER_ID, RecordBatch.NO_PRODUCER_EPOCH, RecordBatch.NO_SEQUENCE, false, true, leaderEpoch, buffer.capacity());
    if (args.magic < MAGIC_VALUE_V2) {
        assertThrows(IllegalArgumentException.class, supplier::get);
    } else {
        MemoryRecordsBuilder builder = supplier.get();
        builder.appendLeaderChangeMessage(RecordBatch.NO_TIMESTAMP, new LeaderChangeMessage().setLeaderId(leaderId).setVoters(voters.stream().map(voterId -> new Voter().setVoterId(voterId)).collect(Collectors.toList())));
        MemoryRecords built = builder.build();
        List<Record> records = TestUtils.toList(built.records());
        assertEquals(1, records.size());
        LeaderChangeMessage leaderChangeMessage = ControlRecordUtils.deserializeLeaderChangeMessage(records.get(0));
        assertEquals(leaderId, leaderChangeMessage.leaderId());
        assertEquals(voters, leaderChangeMessage.voters().stream().map(Voter::voterId).collect(Collectors.toList()));
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) MAGIC_VALUE_V2(org.apache.kafka.common.record.RecordBatch.MAGIC_VALUE_V2) MAGIC_VALUE_V1(org.apache.kafka.common.record.RecordBatch.MAGIC_VALUE_V1) BiFunction(java.util.function.BiFunction) EnumSource(org.junit.jupiter.params.provider.EnumSource) Random(java.util.Random) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) MAGIC_VALUE_V0(org.apache.kafka.common.record.RecordBatch.MAGIC_VALUE_V0) Supplier(java.util.function.Supplier) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) OptionalLong(java.util.OptionalLong) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) BufferSupplier(org.apache.kafka.common.utils.BufferSupplier) Arrays.asList(java.util.Arrays.asList) ArgumentsProvider(org.junit.jupiter.params.provider.ArgumentsProvider) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) LeaderChangeMessage(org.apache.kafka.common.message.LeaderChangeMessage) Utils(org.apache.kafka.common.utils.Utils) Time(org.apache.kafka.common.utils.Time) TestUtils(org.apache.kafka.test.TestUtils) Utils.utf8(org.apache.kafka.common.utils.Utils.utf8) Voter(org.apache.kafka.common.message.LeaderChangeMessage.Voter) Arguments(org.junit.jupiter.params.provider.Arguments) Collectors(java.util.stream.Collectors) CloseableIterator(org.apache.kafka.common.utils.CloseableIterator) Test(org.junit.jupiter.api.Test) UnsupportedCompressionTypeException(org.apache.kafka.common.errors.UnsupportedCompressionTypeException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Stream(java.util.stream.Stream) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ByteBufferOutputStream(org.apache.kafka.common.utils.ByteBufferOutputStream) ArgumentsSource(org.junit.jupiter.params.provider.ArgumentsSource) Collections(java.util.Collections) ByteBuffer(java.nio.ByteBuffer) LeaderChangeMessage(org.apache.kafka.common.message.LeaderChangeMessage) Voter(org.apache.kafka.common.message.LeaderChangeMessage.Voter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ArgumentsSource(org.junit.jupiter.params.provider.ArgumentsSource)

Aggregations

ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Arrays.asList (java.util.Arrays.asList)1 Collections (java.util.Collections)1 List (java.util.List)1 OptionalLong (java.util.OptionalLong)1 Random (java.util.Random)1 BiFunction (java.util.function.BiFunction)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 UnsupportedCompressionTypeException (org.apache.kafka.common.errors.UnsupportedCompressionTypeException)1 LeaderChangeMessage (org.apache.kafka.common.message.LeaderChangeMessage)1 Voter (org.apache.kafka.common.message.LeaderChangeMessage.Voter)1 MAGIC_VALUE_V0 (org.apache.kafka.common.record.RecordBatch.MAGIC_VALUE_V0)1 MAGIC_VALUE_V1 (org.apache.kafka.common.record.RecordBatch.MAGIC_VALUE_V1)1 MAGIC_VALUE_V2 (org.apache.kafka.common.record.RecordBatch.MAGIC_VALUE_V2)1 BufferSupplier (org.apache.kafka.common.utils.BufferSupplier)1 ByteBufferOutputStream (org.apache.kafka.common.utils.ByteBufferOutputStream)1