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())));
}
}
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());
}
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);
}
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));
}
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()));
}
Aggregations