Search in sources :

Example 6 with CreatableTopicResult

use of org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult in project kafka by apache.

the class KafkaAdminClientTest method prepareCreateTopicsResponse.

private static CreateTopicsResponse prepareCreateTopicsResponse(String topicName, Errors error) {
    CreateTopicsResponseData data = new CreateTopicsResponseData();
    data.topics().add(new CreatableTopicResult().setName(topicName).setErrorCode(error.code()));
    return new CreateTopicsResponse(data);
}
Also used : CreateTopicsResponse(org.apache.kafka.common.requests.CreateTopicsResponse) CreatableTopicResult(org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData)

Example 7 with CreatableTopicResult

use of org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult in project kafka by apache.

the class ReplicationControlManagerTest method testDeleteTopics.

@Test
public void testDeleteTopics() throws Exception {
    ReplicationControlTestContext ctx = new ReplicationControlTestContext();
    ReplicationControlManager replicationControl = ctx.replicationControl;
    CreateTopicsRequestData request = new CreateTopicsRequestData();
    CreateTopicsRequestData.CreateableTopicConfigCollection requestConfigs = new CreateTopicsRequestData.CreateableTopicConfigCollection();
    requestConfigs.add(new CreateTopicsRequestData.CreateableTopicConfig().setName("cleanup.policy").setValue("compact"));
    requestConfigs.add(new CreateTopicsRequestData.CreateableTopicConfig().setName("min.cleanable.dirty.ratio").setValue("0.1"));
    request.topics().add(new CreatableTopic().setName("foo").setNumPartitions(3).setReplicationFactor((short) 2).setConfigs(requestConfigs));
    ctx.registerBrokers(0, 1);
    ctx.unfenceBrokers(0, 1);
    ControllerResult<CreateTopicsResponseData> createResult = replicationControl.createTopics(request);
    CreateTopicsResponseData expectedResponse = new CreateTopicsResponseData();
    Uuid topicId = createResult.response().topics().find("foo").topicId();
    expectedResponse.topics().add(new CreatableTopicResult().setName("foo").setNumPartitions(3).setReplicationFactor((short) 2).setErrorMessage(null).setErrorCode((short) 0).setTopicId(topicId));
    assertEquals(expectedResponse, createResult.response());
    // Until the records are replayed, no changes are made
    assertNull(replicationControl.getPartition(topicId, 0));
    assertEmptyTopicConfigs(ctx, "foo");
    ctx.replay(createResult.records());
    assertNotNull(replicationControl.getPartition(topicId, 0));
    assertNotNull(replicationControl.getPartition(topicId, 1));
    assertNotNull(replicationControl.getPartition(topicId, 2));
    assertNull(replicationControl.getPartition(topicId, 3));
    assertCreatedTopicConfigs(ctx, "foo", requestConfigs);
    assertEquals(singletonMap(topicId, new ResultOrError<>("foo")), replicationControl.findTopicNames(Long.MAX_VALUE, Collections.singleton(topicId)));
    assertEquals(singletonMap("foo", new ResultOrError<>(topicId)), replicationControl.findTopicIds(Long.MAX_VALUE, Collections.singleton("foo")));
    Uuid invalidId = new Uuid(topicId.getMostSignificantBits() + 1, topicId.getLeastSignificantBits());
    assertEquals(singletonMap(invalidId, new ResultOrError<>(new ApiError(UNKNOWN_TOPIC_ID))), replicationControl.findTopicNames(Long.MAX_VALUE, Collections.singleton(invalidId)));
    assertEquals(singletonMap("bar", new ResultOrError<>(new ApiError(UNKNOWN_TOPIC_OR_PARTITION))), replicationControl.findTopicIds(Long.MAX_VALUE, Collections.singleton("bar")));
    ControllerResult<Map<Uuid, ApiError>> invalidDeleteResult = replicationControl.deleteTopics(Collections.singletonList(invalidId));
    assertEquals(0, invalidDeleteResult.records().size());
    assertEquals(singletonMap(invalidId, new ApiError(UNKNOWN_TOPIC_ID, null)), invalidDeleteResult.response());
    ControllerResult<Map<Uuid, ApiError>> deleteResult = replicationControl.deleteTopics(Collections.singletonList(topicId));
    assertTrue(deleteResult.isAtomic());
    assertEquals(singletonMap(topicId, new ApiError(NONE, null)), deleteResult.response());
    assertEquals(1, deleteResult.records().size());
    ctx.replay(deleteResult.records());
    assertNull(replicationControl.getPartition(topicId, 0));
    assertNull(replicationControl.getPartition(topicId, 1));
    assertNull(replicationControl.getPartition(topicId, 2));
    assertNull(replicationControl.getPartition(topicId, 3));
    assertEquals(singletonMap(topicId, new ResultOrError<>(new ApiError(UNKNOWN_TOPIC_ID))), replicationControl.findTopicNames(Long.MAX_VALUE, Collections.singleton(topicId)));
    assertEquals(singletonMap("foo", new ResultOrError<>(new ApiError(UNKNOWN_TOPIC_OR_PARTITION))), replicationControl.findTopicIds(Long.MAX_VALUE, Collections.singleton("foo")));
    assertEmptyTopicConfigs(ctx, "foo");
}
Also used : CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData) Uuid(org.apache.kafka.common.Uuid) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) CreatableTopicResult(org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult) ApiError(org.apache.kafka.common.requests.ApiError) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 8 with CreatableTopicResult

use of org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult in project kafka by apache.

the class ReplicationControlManagerTest method testCreateTopics.

@Test
public void testCreateTopics() throws Exception {
    ReplicationControlTestContext ctx = new ReplicationControlTestContext();
    ReplicationControlManager replicationControl = ctx.replicationControl;
    CreateTopicsRequestData request = new CreateTopicsRequestData();
    request.topics().add(new CreatableTopic().setName("foo").setNumPartitions(-1).setReplicationFactor((short) -1));
    ControllerResult<CreateTopicsResponseData> result = replicationControl.createTopics(request);
    CreateTopicsResponseData expectedResponse = new CreateTopicsResponseData();
    expectedResponse.topics().add(new CreatableTopicResult().setName("foo").setErrorCode(Errors.INVALID_REPLICATION_FACTOR.code()).setErrorMessage("Unable to replicate the partition 3 time(s): All " + "brokers are currently fenced."));
    assertEquals(expectedResponse, result.response());
    ctx.registerBrokers(0, 1, 2);
    ctx.unfenceBrokers(0, 1, 2);
    ControllerResult<CreateTopicsResponseData> result2 = replicationControl.createTopics(request);
    CreateTopicsResponseData expectedResponse2 = new CreateTopicsResponseData();
    expectedResponse2.topics().add(new CreatableTopicResult().setName("foo").setNumPartitions(1).setReplicationFactor((short) 3).setErrorMessage(null).setErrorCode((short) 0).setTopicId(result2.response().topics().find("foo").topicId()));
    assertEquals(expectedResponse2, result2.response());
    ctx.replay(result2.records());
    assertEquals(new PartitionRegistration(new int[] { 1, 2, 0 }, new int[] { 1, 2, 0 }, Replicas.NONE, Replicas.NONE, 1, 0, 0), replicationControl.getPartition(((TopicRecord) result2.records().get(0).message()).topicId(), 0));
    ControllerResult<CreateTopicsResponseData> result3 = replicationControl.createTopics(request);
    CreateTopicsResponseData expectedResponse3 = new CreateTopicsResponseData();
    expectedResponse3.topics().add(new CreatableTopicResult().setName("foo").setErrorCode(Errors.TOPIC_ALREADY_EXISTS.code()).setErrorMessage("Topic 'foo' already exists."));
    assertEquals(expectedResponse3, result3.response());
    Uuid fooId = result2.response().topics().find("foo").topicId();
    RecordTestUtils.assertBatchIteratorContains(asList(asList(new ApiMessageAndVersion(new PartitionRecord().setPartitionId(0).setTopicId(fooId).setReplicas(asList(1, 2, 0)).setIsr(asList(1, 2, 0)).setRemovingReplicas(Collections.emptyList()).setAddingReplicas(Collections.emptyList()).setLeader(1).setLeaderEpoch(0).setPartitionEpoch(0), (short) 0), new ApiMessageAndVersion(new TopicRecord().setTopicId(fooId).setName("foo"), (short) 0))), ctx.replicationControl.iterator(Long.MAX_VALUE));
}
Also used : PartitionRegistration(org.apache.kafka.metadata.PartitionRegistration) TopicRecord(org.apache.kafka.common.metadata.TopicRecord) Uuid(org.apache.kafka.common.Uuid) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) PartitionRecord(org.apache.kafka.common.metadata.PartitionRecord) CreatableTopicResult(org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 9 with CreatableTopicResult

use of org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult in project kafka by apache.

the class ReplicationControlManagerTest method testShrinkAndExpandIsr.

@Test
public void testShrinkAndExpandIsr() throws Exception {
    ReplicationControlTestContext ctx = new ReplicationControlTestContext();
    ReplicationControlManager replicationControl = ctx.replicationControl;
    ctx.registerBrokers(0, 1, 2);
    ctx.unfenceBrokers(0, 1, 2);
    CreatableTopicResult createTopicResult = ctx.createTestTopic("foo", new int[][] { new int[] { 0, 1, 2 } });
    TopicIdPartition topicIdPartition = new TopicIdPartition(createTopicResult.topicId(), 0);
    TopicPartition topicPartition = new TopicPartition("foo", 0);
    assertEquals(OptionalInt.of(0), ctx.currentLeader(topicIdPartition));
    long brokerEpoch = ctx.currentBrokerEpoch(0);
    PartitionData shrinkIsrRequest = newAlterIsrPartition(replicationControl, topicIdPartition, asList(0, 1));
    ControllerResult<AlterIsrResponseData> shrinkIsrResult = sendAlterIsr(replicationControl, 0, brokerEpoch, "foo", shrinkIsrRequest);
    AlterIsrResponseData.PartitionData shrinkIsrResponse = assertAlterIsrResponse(shrinkIsrResult, topicPartition, NONE);
    assertConsistentAlterIsrResponse(replicationControl, topicIdPartition, shrinkIsrResponse);
    PartitionData expandIsrRequest = newAlterIsrPartition(replicationControl, topicIdPartition, asList(0, 1, 2));
    ControllerResult<AlterIsrResponseData> expandIsrResult = sendAlterIsr(replicationControl, 0, brokerEpoch, "foo", expandIsrRequest);
    AlterIsrResponseData.PartitionData expandIsrResponse = assertAlterIsrResponse(expandIsrResult, topicPartition, NONE);
    assertConsistentAlterIsrResponse(replicationControl, topicIdPartition, expandIsrResponse);
}
Also used : PartitionData(org.apache.kafka.common.message.AlterIsrRequestData.PartitionData) TopicPartition(org.apache.kafka.common.TopicPartition) AlterIsrResponseData(org.apache.kafka.common.message.AlterIsrResponseData) CreatableTopicResult(org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult) TopicIdPartition(org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 10 with CreatableTopicResult

use of org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult in project kafka by apache.

the class ReplicationControlManagerTest method testInvalidAlterIsrRequests.

@Test
public void testInvalidAlterIsrRequests() throws Exception {
    ReplicationControlTestContext ctx = new ReplicationControlTestContext();
    ReplicationControlManager replicationControl = ctx.replicationControl;
    ctx.registerBrokers(0, 1, 2);
    ctx.unfenceBrokers(0, 1, 2);
    CreatableTopicResult createTopicResult = ctx.createTestTopic("foo", new int[][] { new int[] { 0, 1, 2 } });
    TopicIdPartition topicIdPartition = new TopicIdPartition(createTopicResult.topicId(), 0);
    TopicPartition topicPartition = new TopicPartition("foo", 0);
    assertEquals(OptionalInt.of(0), ctx.currentLeader(topicIdPartition));
    long brokerEpoch = ctx.currentBrokerEpoch(0);
    // Invalid leader
    PartitionData invalidLeaderRequest = newAlterIsrPartition(replicationControl, topicIdPartition, asList(0, 1));
    ControllerResult<AlterIsrResponseData> invalidLeaderResult = sendAlterIsr(replicationControl, 1, ctx.currentBrokerEpoch(1), "foo", invalidLeaderRequest);
    assertAlterIsrResponse(invalidLeaderResult, topicPartition, Errors.INVALID_REQUEST);
    // Stale broker epoch
    PartitionData invalidBrokerEpochRequest = newAlterIsrPartition(replicationControl, topicIdPartition, asList(0, 1));
    assertThrows(StaleBrokerEpochException.class, () -> sendAlterIsr(replicationControl, 0, brokerEpoch - 1, "foo", invalidBrokerEpochRequest));
    // Invalid leader epoch
    PartitionData invalidLeaderEpochRequest = newAlterIsrPartition(replicationControl, topicIdPartition, asList(0, 1));
    invalidLeaderEpochRequest.setLeaderEpoch(500);
    ControllerResult<AlterIsrResponseData> invalidLeaderEpochResult = sendAlterIsr(replicationControl, 1, ctx.currentBrokerEpoch(1), "foo", invalidLeaderEpochRequest);
    assertAlterIsrResponse(invalidLeaderEpochResult, topicPartition, FENCED_LEADER_EPOCH);
    // Invalid ISR (3 is not a valid replica)
    PartitionData invalidIsrRequest1 = newAlterIsrPartition(replicationControl, topicIdPartition, asList(0, 1));
    invalidIsrRequest1.setNewIsr(asList(0, 1, 3));
    ControllerResult<AlterIsrResponseData> invalidIsrResult1 = sendAlterIsr(replicationControl, 1, ctx.currentBrokerEpoch(1), "foo", invalidIsrRequest1);
    assertAlterIsrResponse(invalidIsrResult1, topicPartition, Errors.INVALID_REQUEST);
    // Invalid ISR (does not include leader 0)
    PartitionData invalidIsrRequest2 = newAlterIsrPartition(replicationControl, topicIdPartition, asList(0, 1));
    invalidIsrRequest2.setNewIsr(asList(1, 2));
    ControllerResult<AlterIsrResponseData> invalidIsrResult2 = sendAlterIsr(replicationControl, 1, ctx.currentBrokerEpoch(1), "foo", invalidIsrRequest2);
    assertAlterIsrResponse(invalidIsrResult2, topicPartition, Errors.INVALID_REQUEST);
}
Also used : PartitionData(org.apache.kafka.common.message.AlterIsrRequestData.PartitionData) TopicPartition(org.apache.kafka.common.TopicPartition) AlterIsrResponseData(org.apache.kafka.common.message.AlterIsrResponseData) CreatableTopicResult(org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult) TopicIdPartition(org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Aggregations

CreatableTopicResult (org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult)15 CreateTopicsResponseData (org.apache.kafka.common.message.CreateTopicsResponseData)9 Test (org.junit.jupiter.api.Test)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 CreatableTopic (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic)7 CreateTopicsRequestData (org.apache.kafka.common.message.CreateTopicsRequestData)6 ArrayList (java.util.ArrayList)5 Uuid (org.apache.kafka.common.Uuid)5 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)5 HashMap (java.util.HashMap)4 ApiError (org.apache.kafka.common.requests.ApiError)4 TopicIdPartition (org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition)4 List (java.util.List)3 AlterIsrResponseData (org.apache.kafka.common.message.AlterIsrResponseData)3 TopicRecord (org.apache.kafka.common.metadata.TopicRecord)3 PartitionRegistration (org.apache.kafka.metadata.PartitionRegistration)3 Map (java.util.Map)2 OptionalInt (java.util.OptionalInt)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 InvalidReplicationFactorException (org.apache.kafka.common.errors.InvalidReplicationFactorException)2