use of org.apache.kafka.server.common.ApiMessageAndVersion in project kafka by apache.
the class ReplicationControlManagerTest method testElectUncleanLeaders.
@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testElectUncleanLeaders(boolean electAllPartitions) throws Exception {
ReplicationControlTestContext ctx = new ReplicationControlTestContext();
ReplicationControlManager replication = ctx.replicationControl;
ctx.registerBrokers(0, 1, 2, 3, 4);
ctx.unfenceBrokers(0, 1, 2, 3, 4);
Uuid fooId = ctx.createTestTopic("foo", new int[][] { new int[] { 1, 2, 3 }, new int[] { 2, 3, 4 }, new int[] { 0, 2, 1 } }).topicId();
TopicIdPartition partition0 = new TopicIdPartition(fooId, 0);
TopicIdPartition partition1 = new TopicIdPartition(fooId, 1);
TopicIdPartition partition2 = new TopicIdPartition(fooId, 2);
ctx.fenceBrokers(Utils.mkSet(2, 3));
ctx.fenceBrokers(Utils.mkSet(1, 2, 3));
assertLeaderAndIsr(replication, partition0, NO_LEADER, new int[] { 1 });
assertLeaderAndIsr(replication, partition1, 4, new int[] { 4 });
assertLeaderAndIsr(replication, partition2, 0, new int[] { 0 });
ElectLeadersRequestData request = buildElectLeadersRequest(ElectionType.UNCLEAN, electAllPartitions ? null : singletonMap("foo", asList(0, 1, 2)));
// No election can be done yet because no replicas are available for partition 0
ControllerResult<ElectLeadersResponseData> result1 = replication.electLeaders(request);
assertEquals(Collections.emptyList(), result1.records());
ElectLeadersResponseData expectedResponse1 = buildElectLeadersResponse(NONE, electAllPartitions, Utils.mkMap(Utils.mkEntry(new TopicPartition("foo", 0), new ApiError(ELIGIBLE_LEADERS_NOT_AVAILABLE)), Utils.mkEntry(new TopicPartition("foo", 1), new ApiError(ELECTION_NOT_NEEDED)), Utils.mkEntry(new TopicPartition("foo", 2), new ApiError(ELECTION_NOT_NEEDED))));
assertElectLeadersResponse(expectedResponse1, result1.response());
// Now we bring 2 back online which should allow the unclean election of partition 0
ctx.unfenceBrokers(Utils.mkSet(2));
// Bring 2 back into the ISR for partition 1. This allows us to verify that
// preferred election does not occur as a result of the unclean election request.
ctx.alterIsr(partition1, 4, asList(2, 4));
ControllerResult<ElectLeadersResponseData> result = replication.electLeaders(request);
assertEquals(1, result.records().size());
ApiMessageAndVersion record = result.records().get(0);
assertTrue(record.message() instanceof PartitionChangeRecord);
PartitionChangeRecord partitionChangeRecord = (PartitionChangeRecord) record.message();
assertEquals(0, partitionChangeRecord.partitionId());
assertEquals(2, partitionChangeRecord.leader());
assertEquals(singletonList(2), partitionChangeRecord.isr());
ctx.replay(result.records());
assertLeaderAndIsr(replication, partition0, 2, new int[] { 2 });
assertLeaderAndIsr(replication, partition1, 4, new int[] { 2, 4 });
assertLeaderAndIsr(replication, partition2, 0, new int[] { 0 });
ElectLeadersResponseData expectedResponse = buildElectLeadersResponse(NONE, electAllPartitions, Utils.mkMap(Utils.mkEntry(new TopicPartition("foo", 0), ApiError.NONE), Utils.mkEntry(new TopicPartition("foo", 1), new ApiError(ELECTION_NOT_NEEDED)), Utils.mkEntry(new TopicPartition("foo", 2), new ApiError(ELECTION_NOT_NEEDED))));
assertElectLeadersResponse(expectedResponse, result.response());
}
use of org.apache.kafka.server.common.ApiMessageAndVersion in project kafka by apache.
the class SnapshotGeneratorTest method testGenerateBatches.
@Test
public void testGenerateBatches() throws Exception {
SnapshotWriter<ApiMessageAndVersion> writer = createSnapshotWriter(123, 0);
List<Section> sections = Arrays.asList(new Section("replication", Arrays.asList(BATCHES.get(0), BATCHES.get(1), BATCHES.get(2)).iterator()), new Section("configuration", Arrays.asList(BATCHES.get(3), BATCHES.get(4)).iterator()));
SnapshotGenerator generator = new SnapshotGenerator(new LogContext(), writer, 2, sections);
assertFalse(writer.isFrozen());
assertEquals(123L, generator.lastContainedLogOffset());
assertEquals(writer, generator.writer());
assertEquals(OptionalLong.of(0L), generator.generateBatches());
assertEquals(OptionalLong.of(0L), generator.generateBatches());
assertFalse(writer.isFrozen());
assertEquals(OptionalLong.empty(), generator.generateBatches());
assertTrue(writer.isFrozen());
}
use of org.apache.kafka.server.common.ApiMessageAndVersion in project kafka by apache.
the class TopicsImageTest method testLocalReassignmentChanges.
@Test
public void testLocalReassignmentChanges() {
int localId = 3;
Uuid zooId = Uuid.fromString("0hHJ3X5ZQ-CFfQ5xgpj90w");
List<TopicImage> topics = new ArrayList<>();
topics.add(newTopicImage("zoo", zooId, newPartition(new int[] { 0, 1, localId }), newPartition(new int[] { localId, 1, 2 }), newPartition(new int[] { 0, 1, localId }), newPartition(new int[] { localId, 1, 2 }), newPartition(new int[] { 0, 1, 2 }), newPartition(new int[] { 0, 1, 2 })));
TopicsImage image = new TopicsImage(newTopicsByIdMap(topics), newTopicsByNameMap(topics));
List<ApiMessageAndVersion> topicRecords = new ArrayList<>();
// zoo-0 - follower to leader
topicRecords.add(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(zooId).setPartitionId(0).setLeader(localId), PARTITION_CHANGE_RECORD.highestSupportedVersion()));
// zoo-1 - leader to follower
topicRecords.add(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(zooId).setPartitionId(1).setLeader(1), PARTITION_CHANGE_RECORD.highestSupportedVersion()));
// zoo-2 - follower to removed
topicRecords.add(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(zooId).setPartitionId(2).setIsr(Arrays.asList(0, 1, 2)).setReplicas(Arrays.asList(0, 1, 2)), PARTITION_CHANGE_RECORD.highestSupportedVersion()));
// zoo-3 - leader to removed
topicRecords.add(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(zooId).setPartitionId(3).setLeader(0).setIsr(Arrays.asList(0, 1, 2)).setReplicas(Arrays.asList(0, 1, 2)), PARTITION_CHANGE_RECORD.highestSupportedVersion()));
// zoo-4 - not replica to leader
topicRecords.add(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(zooId).setPartitionId(4).setLeader(localId).setIsr(Arrays.asList(localId, 1, 2)).setReplicas(Arrays.asList(localId, 1, 2)), PARTITION_CHANGE_RECORD.highestSupportedVersion()));
// zoo-5 - not replica to follower
topicRecords.add(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(zooId).setPartitionId(5).setIsr(Arrays.asList(0, 1, localId)).setReplicas(Arrays.asList(0, 1, localId)), PARTITION_CHANGE_RECORD.highestSupportedVersion()));
TopicsDelta delta = new TopicsDelta(image);
RecordTestUtils.replayAll(delta, topicRecords);
LocalReplicaChanges changes = delta.localChanges(localId);
assertEquals(new HashSet<>(Arrays.asList(new TopicPartition("zoo", 2), new TopicPartition("zoo", 3))), changes.deletes());
assertEquals(new HashSet<>(Arrays.asList(new TopicPartition("zoo", 0), new TopicPartition("zoo", 4))), changes.leaders().keySet());
assertEquals(new HashSet<>(Arrays.asList(new TopicPartition("zoo", 1), new TopicPartition("zoo", 5))), changes.followers().keySet());
}
use of org.apache.kafka.server.common.ApiMessageAndVersion in project kafka by apache.
the class TopicsImageTest method testBasicLocalChanges.
@Test
public void testBasicLocalChanges() {
int localId = 3;
/* Changes already include in DELTA1_RECORDS and IMAGE1:
* foo - topic id deleted
* bar-0 - stay as follower with different partition epoch
* baz-0 - new topic to leader
*/
List<ApiMessageAndVersion> topicRecords = new ArrayList<>(DELTA1_RECORDS);
// Create a new foo topic with a different id
Uuid newFooId = Uuid.fromString("b66ybsWIQoygs01vdjH07A");
topicRecords.add(new ApiMessageAndVersion(new TopicRecord().setName("foo").setTopicId(newFooId), TOPIC_RECORD.highestSupportedVersion()));
topicRecords.add(newPartitionRecord(newFooId, 0, Arrays.asList(0, 1, 2)));
topicRecords.add(newPartitionRecord(newFooId, 1, Arrays.asList(0, 1, localId)));
// baz-1 - new partition to follower
topicRecords.add(new ApiMessageAndVersion(new PartitionRecord().setPartitionId(1).setTopicId(BAZ_UUID).setReplicas(Arrays.asList(4, 2, localId)).setIsr(Arrays.asList(4, 2, localId)).setLeader(4).setLeaderEpoch(2).setPartitionEpoch(1), PARTITION_RECORD.highestSupportedVersion()));
TopicsDelta delta = new TopicsDelta(IMAGE1);
RecordTestUtils.replayAll(delta, topicRecords);
LocalReplicaChanges changes = delta.localChanges(localId);
assertEquals(new HashSet<>(Arrays.asList(new TopicPartition("foo", 0), new TopicPartition("foo", 1))), changes.deletes());
assertEquals(new HashSet<>(Arrays.asList(new TopicPartition("baz", 0))), changes.leaders().keySet());
assertEquals(new HashSet<>(Arrays.asList(new TopicPartition("baz", 1), new TopicPartition("bar", 0), new TopicPartition("foo", 1))), changes.followers().keySet());
}
use of org.apache.kafka.server.common.ApiMessageAndVersion in project kafka by apache.
the class RecordTestUtils method sizeInBytes.
private static int sizeInBytes(List<ApiMessageAndVersion> records) {
int size = 0;
for (ApiMessageAndVersion record : records) {
ObjectSerializationCache cache = new ObjectSerializationCache();
size += MetadataRecordSerde.INSTANCE.recordSize(record, cache);
}
return size;
}
Aggregations