Search in sources :

Example 1 with LeaderChangeMessage

use of org.apache.kafka.common.message.LeaderChangeMessage in project kafka by apache.

the class MemoryRecordsBuilderTest method testWriteLeaderChangeControlBatchWithoutLeaderEpoch.

@ParameterizedTest
@ArgumentsSource(MemoryRecordsBuilderArgumentsProvider.class)
public void testWriteLeaderChangeControlBatchWithoutLeaderEpoch(Args args) {
    ByteBuffer buffer = allocateBuffer(128, args);
    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, RecordBatch.NO_PARTITION_LEADER_EPOCH, buffer.capacity());
    if (args.magic < MAGIC_VALUE_V2) {
        assertThrows(IllegalArgumentException.class, supplier::get);
    } else {
        final int leaderId = 1;
        MemoryRecordsBuilder builder = supplier.get();
        assertThrows(IllegalArgumentException.class, () -> builder.appendLeaderChangeMessage(RecordBatch.NO_TIMESTAMP, new LeaderChangeMessage().setLeaderId(leaderId).setVoters(Collections.emptyList())));
    }
}
Also used : LeaderChangeMessage(org.apache.kafka.common.message.LeaderChangeMessage) ByteBuffer(java.nio.ByteBuffer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ArgumentsSource(org.junit.jupiter.params.provider.ArgumentsSource)

Example 2 with LeaderChangeMessage

use of org.apache.kafka.common.message.LeaderChangeMessage in project kafka by apache.

the class ControlRecordUtilsTest method testDeserializeRecord.

private void testDeserializeRecord(ControlRecordType controlRecordType) {
    final int leaderId = 1;
    final int voterId = 2;
    LeaderChangeMessage data = new LeaderChangeMessage().setLeaderId(leaderId).setVoters(Collections.singletonList(new Voter().setVoterId(voterId)));
    ByteBuffer valueBuffer = ByteBuffer.allocate(256);
    data.write(new ByteBufferAccessor(valueBuffer), new ObjectSerializationCache(), data.highestSupportedVersion());
    valueBuffer.flip();
    byte[] keyData = new byte[] { 0, 0, 0, (byte) controlRecordType.type };
    DefaultRecord record = new DefaultRecord(256, (byte) 0, 0, 0L, 0, ByteBuffer.wrap(keyData), valueBuffer, null);
    LeaderChangeMessage deserializedData = ControlRecordUtils.deserializeLeaderChangeMessage(record);
    assertEquals(leaderId, deserializedData.leaderId());
    assertEquals(Collections.singletonList(new Voter().setVoterId(voterId)), deserializedData.voters());
}
Also used : LeaderChangeMessage(org.apache.kafka.common.message.LeaderChangeMessage) ObjectSerializationCache(org.apache.kafka.common.protocol.ObjectSerializationCache) Voter(org.apache.kafka.common.message.LeaderChangeMessage.Voter) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer)

Example 3 with LeaderChangeMessage

use of org.apache.kafka.common.message.LeaderChangeMessage in project kafka by apache.

the class BatchAccumulatorTest method testLeaderChangeMessageWritten.

@Test
public void testLeaderChangeMessageWritten() {
    int leaderEpoch = 17;
    long baseOffset = 0;
    int lingerMs = 50;
    int maxBatchSize = 512;
    ByteBuffer buffer = ByteBuffer.allocate(256);
    Mockito.when(memoryPool.tryAllocate(256)).thenReturn(buffer);
    BatchAccumulator<String> acc = buildAccumulator(leaderEpoch, baseOffset, lingerMs, maxBatchSize);
    acc.appendLeaderChangeMessage(new LeaderChangeMessage(), time.milliseconds());
    assertTrue(acc.needsDrain(time.milliseconds()));
    List<BatchAccumulator.CompletedBatch<String>> batches = acc.drain();
    assertEquals(1, batches.size());
    BatchAccumulator.CompletedBatch<String> batch = batches.get(0);
    batch.release();
    Mockito.verify(memoryPool).release(buffer);
}
Also used : LeaderChangeMessage(org.apache.kafka.common.message.LeaderChangeMessage) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 4 with LeaderChangeMessage

use of org.apache.kafka.common.message.LeaderChangeMessage in project kafka by apache.

the class MockLogTest method testAppendControlRecord.

@Test
public void testAppendControlRecord() {
    final long initialOffset = 0;
    final int currentEpoch = 3;
    LeaderChangeMessage messageData = new LeaderChangeMessage().setLeaderId(0);
    ByteBuffer buffer = ByteBuffer.allocate(256);
    log.appendAsLeader(MemoryRecords.withLeaderChangeMessage(initialOffset, 0L, 2, buffer, messageData), currentEpoch);
    assertEquals(0, log.startOffset());
    assertEquals(1, log.endOffset().offset);
    assertEquals(currentEpoch, log.lastFetchedEpoch());
    Records records = log.read(0, Isolation.UNCOMMITTED).records;
    for (RecordBatch batch : records.batches()) {
        assertTrue(batch.isControlBatch());
    }
    List<ByteBuffer> extractRecords = new ArrayList<>();
    for (Record record : records.records()) {
        LeaderChangeMessage deserializedData = ControlRecordUtils.deserializeLeaderChangeMessage(record);
        assertEquals(deserializedData, messageData);
        extractRecords.add(record.value());
    }
    assertEquals(1, extractRecords.size());
    assertEquals(new OffsetAndEpoch(1, currentEpoch), log.endOffsetForEpoch(currentEpoch));
}
Also used : LeaderChangeMessage(org.apache.kafka.common.message.LeaderChangeMessage) RecordBatch(org.apache.kafka.common.record.RecordBatch) ArrayList(java.util.ArrayList) Record(org.apache.kafka.common.record.Record) SimpleRecord(org.apache.kafka.common.record.SimpleRecord) ByteBuffer(java.nio.ByteBuffer) Records(org.apache.kafka.common.record.Records) MemoryRecords(org.apache.kafka.common.record.MemoryRecords) Test(org.junit.jupiter.api.Test)

Example 5 with LeaderChangeMessage

use of org.apache.kafka.common.message.LeaderChangeMessage in project kafka by apache.

the class RaftClientTestContext method verifyLeaderChangeMessage.

static void verifyLeaderChangeMessage(int leaderId, List<Integer> voters, List<Integer> grantingVoters, ByteBuffer recordKey, ByteBuffer recordValue) {
    assertEquals(ControlRecordType.LEADER_CHANGE, ControlRecordType.parse(recordKey));
    LeaderChangeMessage leaderChangeMessage = ControlRecordUtils.deserializeLeaderChangeMessage(recordValue);
    assertEquals(leaderId, leaderChangeMessage.leaderId());
    assertEquals(voters.stream().map(voterId -> new Voter().setVoterId(voterId)).collect(Collectors.toList()), leaderChangeMessage.voters());
    assertEquals(grantingVoters.stream().map(voterId -> new Voter().setVoterId(voterId)).collect(Collectors.toSet()), new HashSet<>(leaderChangeMessage.grantingVoters()));
}
Also used : LeaderChangeMessage(org.apache.kafka.common.message.LeaderChangeMessage) Voter(org.apache.kafka.common.message.LeaderChangeMessage.Voter)

Aggregations

LeaderChangeMessage (org.apache.kafka.common.message.LeaderChangeMessage)10 ByteBuffer (java.nio.ByteBuffer)6 Voter (org.apache.kafka.common.message.LeaderChangeMessage.Voter)5 Test (org.junit.jupiter.api.Test)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 ArrayList (java.util.ArrayList)2 ByteBufferAccessor (org.apache.kafka.common.protocol.ByteBufferAccessor)2 Record (org.apache.kafka.common.record.Record)2 ArgumentsSource (org.junit.jupiter.params.provider.ArgumentsSource)2 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