Search in sources :

Example 11 with CreateTopicsRequestData

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

the class RequestResponseTest method createCreateTopicRequest.

private CreateTopicsRequest createCreateTopicRequest(short version, boolean validateOnly) {
    CreateTopicsRequestData data = new CreateTopicsRequestData().setTimeoutMs(123).setValidateOnly(validateOnly);
    data.topics().add(new CreatableTopic().setNumPartitions(3).setReplicationFactor((short) 5));
    CreatableTopic topic2 = new CreatableTopic();
    data.topics().add(topic2);
    topic2.assignments().add(new CreatableReplicaAssignment().setPartitionIndex(0).setBrokerIds(asList(1, 2, 3)));
    topic2.assignments().add(new CreatableReplicaAssignment().setPartitionIndex(1).setBrokerIds(asList(2, 3, 4)));
    topic2.configs().add(new CreateableTopicConfig().setName("config1").setValue("value1"));
    return new CreateTopicsRequest.Builder(data).build(version);
}
Also used : CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) CreatableReplicaAssignment(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignment) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) CreateableTopicConfig(org.apache.kafka.common.message.CreateTopicsRequestData.CreateableTopicConfig) Builder(org.apache.kafka.common.requests.CreateTopicsRequest.Builder)

Example 12 with CreateTopicsRequestData

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

the class RequestResponseTest method testCreateTopicRequestV3FailsIfNoPartitionsOrReplicas.

@Test
public void testCreateTopicRequestV3FailsIfNoPartitionsOrReplicas() {
    final UnsupportedVersionException exception = assertThrows(UnsupportedVersionException.class, () -> {
        CreateTopicsRequestData data = new CreateTopicsRequestData().setTimeoutMs(123).setValidateOnly(false);
        data.topics().add(new CreatableTopic().setName("foo").setNumPartitions(CreateTopicsRequest.NO_NUM_PARTITIONS).setReplicationFactor((short) 1));
        data.topics().add(new CreatableTopic().setName("bar").setNumPartitions(1).setReplicationFactor(CreateTopicsRequest.NO_REPLICATION_FACTOR));
        new Builder(data).build((short) 3);
    });
    assertTrue(exception.getMessage().contains("supported in CreateTopicRequest version 4+"));
    assertTrue(exception.getMessage().contains("[foo, bar]"));
}
Also used : CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) Builder(org.apache.kafka.common.requests.CreateTopicsRequest.Builder) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException) Test(org.junit.jupiter.api.Test)

Example 13 with CreateTopicsRequestData

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

the class QuorumControllerTest method testSnapshotConfiguration.

@Test
public void testSnapshotConfiguration() throws Throwable {
    final int numBrokers = 4;
    final int maxNewRecordBytes = 4;
    Map<Integer, Long> brokerEpochs = new HashMap<>();
    Uuid fooId;
    try (LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv(3, Optional.empty())) {
        try (QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv(logEnv, builder -> {
            builder.setConfigDefs(CONFIGS).setSnapshotMaxNewRecordBytes(maxNewRecordBytes);
        })) {
            QuorumController active = controlEnv.activeController();
            for (int i = 0; i < numBrokers; i++) {
                BrokerRegistrationReply reply = active.registerBroker(new BrokerRegistrationRequestData().setBrokerId(i).setRack(null).setClusterId(active.clusterId()).setIncarnationId(Uuid.fromString("kxAT73dKQsitIedpiPtwB" + i)).setListeners(new ListenerCollection(Arrays.asList(new Listener().setName("PLAINTEXT").setHost("localhost").setPort(9092 + i)).iterator()))).get();
                brokerEpochs.put(i, reply.epoch());
            }
            for (int i = 0; i < numBrokers - 1; i++) {
                assertEquals(new BrokerHeartbeatReply(true, false, false, false), active.processBrokerHeartbeat(new BrokerHeartbeatRequestData().setWantFence(false).setBrokerEpoch(brokerEpochs.get(i)).setBrokerId(i).setCurrentMetadataOffset(100000L)).get());
            }
            CreateTopicsResponseData fooData = active.createTopics(new CreateTopicsRequestData().setTopics(new CreatableTopicCollection(Collections.singleton(new CreatableTopic().setName("foo").setNumPartitions(-1).setReplicationFactor((short) -1).setAssignments(new CreatableReplicaAssignmentCollection(Arrays.asList(new CreatableReplicaAssignment().setPartitionIndex(0).setBrokerIds(Arrays.asList(0, 1, 2)), new CreatableReplicaAssignment().setPartitionIndex(1).setBrokerIds(Arrays.asList(1, 2, 0))).iterator()))).iterator()))).get();
            fooId = fooData.topics().find("foo").topicId();
            active.allocateProducerIds(new AllocateProducerIdsRequestData().setBrokerId(0).setBrokerEpoch(brokerEpochs.get(0))).get();
            SnapshotReader<ApiMessageAndVersion> snapshot = createSnapshotReader(logEnv.waitForLatestSnapshot());
            checkSnapshotSubcontent(expectedSnapshotContent(fooId, brokerEpochs), snapshot);
        }
    }
}
Also used : BrokerHeartbeatReply(org.apache.kafka.metadata.BrokerHeartbeatReply) ListenerCollection(org.apache.kafka.common.message.BrokerRegistrationRequestData.ListenerCollection) LocalLogManagerTestEnv(org.apache.kafka.metalog.LocalLogManagerTestEnv) Listener(org.apache.kafka.common.message.BrokerRegistrationRequestData.Listener) HashMap(java.util.HashMap) BrokerRegistrationRequestData(org.apache.kafka.common.message.BrokerRegistrationRequestData) BrokerRegistrationReply(org.apache.kafka.metadata.BrokerRegistrationReply) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData) BrokerEndpoint(org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerEndpoint) CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) Uuid(org.apache.kafka.common.Uuid) BrokerHeartbeatRequestData(org.apache.kafka.common.message.BrokerHeartbeatRequestData) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) CreatableReplicaAssignment(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignment) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) AllocateProducerIdsRequestData(org.apache.kafka.common.message.AllocateProducerIdsRequestData) CreatableReplicaAssignmentCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignmentCollection) Test(org.junit.jupiter.api.Test)

Example 14 with CreateTopicsRequestData

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

the class ReplicationControlManagerTest method testGlobalTopicAndPartitionMetrics.

@Test
public void testGlobalTopicAndPartitionMetrics() 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));
    ctx.registerBrokers(0, 1, 2);
    ctx.unfenceBrokers(0, 1, 2);
    List<Uuid> topicsToDelete = new ArrayList<>();
    ControllerResult<CreateTopicsResponseData> result = replicationControl.createTopics(request);
    topicsToDelete.add(result.response().topics().find("foo").topicId());
    RecordTestUtils.replayAll(replicationControl, result.records());
    assertEquals(1, ctx.metrics.globalTopicsCount());
    request = new CreateTopicsRequestData();
    request.topics().add(new CreatableTopic().setName("bar").setNumPartitions(1).setReplicationFactor((short) -1));
    request.topics().add(new CreatableTopic().setName("baz").setNumPartitions(2).setReplicationFactor((short) -1));
    result = replicationControl.createTopics(request);
    RecordTestUtils.replayAll(replicationControl, result.records());
    assertEquals(3, ctx.metrics.globalTopicsCount());
    assertEquals(4, ctx.metrics.globalPartitionCount());
    topicsToDelete.add(result.response().topics().find("baz").topicId());
    ControllerResult<Map<Uuid, ApiError>> deleteResult = replicationControl.deleteTopics(topicsToDelete);
    RecordTestUtils.replayAll(replicationControl, deleteResult.records());
    assertEquals(1, ctx.metrics.globalTopicsCount());
    assertEquals(1, ctx.metrics.globalPartitionCount());
    Uuid topicToDelete = result.response().topics().find("bar").topicId();
    deleteResult = replicationControl.deleteTopics(Collections.singletonList(topicToDelete));
    RecordTestUtils.replayAll(replicationControl, deleteResult.records());
    assertEquals(0, ctx.metrics.globalTopicsCount());
    assertEquals(0, ctx.metrics.globalPartitionCount());
}
Also used : Uuid(org.apache.kafka.common.Uuid) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) ArrayList(java.util.ArrayList) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData) 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 15 with CreateTopicsRequestData

use of org.apache.kafka.common.message.CreateTopicsRequestData 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)

Aggregations

CreateTopicsRequestData (org.apache.kafka.common.message.CreateTopicsRequestData)23 CreatableTopic (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic)19 Test (org.junit.jupiter.api.Test)19 CreateTopicsResponseData (org.apache.kafka.common.message.CreateTopicsResponseData)13 CreatableTopicCollection (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection)12 HashMap (java.util.HashMap)11 Uuid (org.apache.kafka.common.Uuid)10 LocalLogManagerTestEnv (org.apache.kafka.metalog.LocalLogManagerTestEnv)9 List (java.util.List)6 Map (java.util.Map)6 BrokerHeartbeatRequestData (org.apache.kafka.common.message.BrokerHeartbeatRequestData)6 BrokerRegistrationRequestData (org.apache.kafka.common.message.BrokerRegistrationRequestData)6 Listener (org.apache.kafka.common.message.BrokerRegistrationRequestData.Listener)6 ListenerCollection (org.apache.kafka.common.message.BrokerRegistrationRequestData.ListenerCollection)6 BrokerHeartbeatReply (org.apache.kafka.metadata.BrokerHeartbeatReply)6 BrokerRegistrationReply (org.apache.kafka.metadata.BrokerRegistrationReply)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 CreatableReplicaAssignment (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignment)5 CreatableTopicResult (org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult)5 Builder (org.apache.kafka.common.requests.CreateTopicsRequest.Builder)5