Search in sources :

Example 6 with HostInfo

use of org.apache.kafka.streams.state.HostInfo in project kafka by apache.

the class StreamPartitionAssignorTest method shouldUpdateClusterMetadataOnAssignment.

@Test
public void shouldUpdateClusterMetadataOnAssignment() throws Exception {
    final TopicPartition topicOne = new TopicPartition("topic", 1);
    final TopicPartition topicTwo = new TopicPartition("topic2", 2);
    final Map<HostInfo, Set<TopicPartition>> firstHostState = Collections.singletonMap(new HostInfo("localhost", 9090), Utils.mkSet(topicOne));
    final Map<HostInfo, Set<TopicPartition>> secondHostState = Collections.singletonMap(new HostInfo("localhost", 9090), Utils.mkSet(topicOne, topicTwo));
    partitionAssignor.onAssignment(createAssignment(firstHostState));
    assertEquals(Utils.mkSet("topic"), partitionAssignor.clusterMetadata().topics());
    partitionAssignor.onAssignment(createAssignment(secondHostState));
    assertEquals(Utils.mkSet("topic", "topic2"), partitionAssignor.clusterMetadata().topics());
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) TopicPartition(org.apache.kafka.common.TopicPartition) HostInfo(org.apache.kafka.streams.state.HostInfo) Test(org.junit.Test)

Example 7 with HostInfo

use of org.apache.kafka.streams.state.HostInfo in project kafka by apache.

the class StreamPartitionAssignorTest method shouldExposeHostStateToTopicPartitionsOnAssignment.

@Test
public void shouldExposeHostStateToTopicPartitionsOnAssignment() throws Exception {
    List<TopicPartition> topic = Collections.singletonList(new TopicPartition("topic", 0));
    final Map<HostInfo, Set<TopicPartition>> hostState = Collections.singletonMap(new HostInfo("localhost", 80), Collections.singleton(new TopicPartition("topic", 0)));
    AssignmentInfo assignmentInfo = new AssignmentInfo(Collections.singletonList(new TaskId(0, 0)), Collections.<TaskId, Set<TopicPartition>>emptyMap(), hostState);
    partitionAssignor.onAssignment(new PartitionAssignor.Assignment(topic, assignmentInfo.encode()));
    assertEquals(hostState, partitionAssignor.getPartitionsByHostState());
}
Also used : AssignmentInfo(org.apache.kafka.streams.processor.internals.assignment.AssignmentInfo) HashSet(java.util.HashSet) Set(java.util.Set) TaskId(org.apache.kafka.streams.processor.TaskId) TopicPartition(org.apache.kafka.common.TopicPartition) PartitionAssignor(org.apache.kafka.clients.consumer.internals.PartitionAssignor) HostInfo(org.apache.kafka.streams.state.HostInfo) Test(org.junit.Test)

Example 8 with HostInfo

use of org.apache.kafka.streams.state.HostInfo in project kafka by apache.

the class StreamPartitionAssignorTest method shouldNotAddStandbyTaskPartitionsToPartitionsForHost.

@Test
public void shouldNotAddStandbyTaskPartitionsToPartitionsForHost() throws Exception {
    final Properties props = configProps();
    props.setProperty(StreamsConfig.NUM_STANDBY_REPLICAS_CONFIG, "1");
    final StreamsConfig config = new StreamsConfig(props);
    final KStreamBuilder builder = new KStreamBuilder();
    final String applicationId = "appId";
    builder.setApplicationId(applicationId);
    builder.stream("topic1").groupByKey().count("count");
    final UUID uuid = UUID.randomUUID();
    final String client = "client1";
    final StreamThread streamThread = new StreamThread(builder, config, mockClientSupplier, applicationId, client, uuid, new Metrics(), Time.SYSTEM, new StreamsMetadataState(builder, StreamsMetadataState.UNKNOWN_HOST), 0);
    partitionAssignor.configure(config.getConsumerConfigs(streamThread, applicationId, client));
    partitionAssignor.setInternalTopicManager(new MockInternalTopicManager(streamThread.config, mockClientSupplier.restoreConsumer));
    final Map<String, PartitionAssignor.Subscription> subscriptions = new HashMap<>();
    final Set<TaskId> emptyTasks = Collections.emptySet();
    subscriptions.put("consumer1", new PartitionAssignor.Subscription(Collections.singletonList("topic1"), new SubscriptionInfo(uuid, emptyTasks, emptyTasks, userEndPoint).encode()));
    subscriptions.put("consumer2", new PartitionAssignor.Subscription(Collections.singletonList("topic1"), new SubscriptionInfo(UUID.randomUUID(), emptyTasks, emptyTasks, "other:9090").encode()));
    final Set<TopicPartition> allPartitions = Utils.mkSet(t1p0, t1p1, t1p2);
    final Map<String, PartitionAssignor.Assignment> assign = partitionAssignor.assign(metadata, subscriptions);
    final PartitionAssignor.Assignment consumer1Assignment = assign.get("consumer1");
    final AssignmentInfo assignmentInfo = AssignmentInfo.decode(consumer1Assignment.userData());
    final Set<TopicPartition> consumer1partitions = assignmentInfo.partitionsByHost.get(new HostInfo("localhost", 2171));
    final Set<TopicPartition> consumer2Partitions = assignmentInfo.partitionsByHost.get(new HostInfo("other", 9090));
    final HashSet<TopicPartition> allAssignedPartitions = new HashSet<>(consumer1partitions);
    allAssignedPartitions.addAll(consumer2Partitions);
    assertThat(consumer1partitions, not(allPartitions));
    assertThat(consumer2Partitions, not(allPartitions));
    assertThat(allAssignedPartitions, equalTo(allPartitions));
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) HashMap(java.util.HashMap) MockInternalTopicManager(org.apache.kafka.test.MockInternalTopicManager) SubscriptionInfo(org.apache.kafka.streams.processor.internals.assignment.SubscriptionInfo) Properties(java.util.Properties) AssignmentInfo(org.apache.kafka.streams.processor.internals.assignment.AssignmentInfo) Metrics(org.apache.kafka.common.metrics.Metrics) PartitionAssignor(org.apache.kafka.clients.consumer.internals.PartitionAssignor) UUID(java.util.UUID) StreamsConfig(org.apache.kafka.streams.StreamsConfig) HashSet(java.util.HashSet) KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) TopicPartition(org.apache.kafka.common.TopicPartition) HostInfo(org.apache.kafka.streams.state.HostInfo) Test(org.junit.Test)

Example 9 with HostInfo

use of org.apache.kafka.streams.state.HostInfo in project kafka by apache.

the class StreamPartitionAssignorTest method shouldSetClusterMetadataOnAssignment.

@Test
public void shouldSetClusterMetadataOnAssignment() throws Exception {
    final List<TopicPartition> topic = Collections.singletonList(new TopicPartition("topic", 0));
    final Map<HostInfo, Set<TopicPartition>> hostState = Collections.singletonMap(new HostInfo("localhost", 80), Collections.singleton(new TopicPartition("topic", 0)));
    final AssignmentInfo assignmentInfo = new AssignmentInfo(Collections.singletonList(new TaskId(0, 0)), Collections.<TaskId, Set<TopicPartition>>emptyMap(), hostState);
    partitionAssignor.onAssignment(new PartitionAssignor.Assignment(topic, assignmentInfo.encode()));
    final Cluster cluster = partitionAssignor.clusterMetadata();
    final List<PartitionInfo> partitionInfos = cluster.partitionsForTopic("topic");
    final PartitionInfo partitionInfo = partitionInfos.get(0);
    assertEquals(1, partitionInfos.size());
    assertEquals("topic", partitionInfo.topic());
    assertEquals(0, partitionInfo.partition());
}
Also used : AssignmentInfo(org.apache.kafka.streams.processor.internals.assignment.AssignmentInfo) HashSet(java.util.HashSet) Set(java.util.Set) TaskId(org.apache.kafka.streams.processor.TaskId) TopicPartition(org.apache.kafka.common.TopicPartition) Cluster(org.apache.kafka.common.Cluster) PartitionAssignor(org.apache.kafka.clients.consumer.internals.PartitionAssignor) PartitionInfo(org.apache.kafka.common.PartitionInfo) HostInfo(org.apache.kafka.streams.state.HostInfo) Test(org.junit.Test)

Example 10 with HostInfo

use of org.apache.kafka.streams.state.HostInfo in project kafka by apache.

the class AssignmentInfoTest method testEncodeDecode.

@Test
public void testEncodeDecode() {
    List<TaskId> activeTasks = Arrays.asList(new TaskId(0, 0), new TaskId(0, 0), new TaskId(0, 1), new TaskId(1, 0));
    Map<TaskId, Set<TopicPartition>> standbyTasks = new HashMap<>();
    standbyTasks.put(new TaskId(1, 1), Utils.mkSet(new TopicPartition("t1", 1), new TopicPartition("t2", 1)));
    standbyTasks.put(new TaskId(2, 0), Utils.mkSet(new TopicPartition("t3", 0), new TopicPartition("t3", 0)));
    AssignmentInfo info = new AssignmentInfo(activeTasks, standbyTasks, new HashMap<HostInfo, Set<TopicPartition>>());
    AssignmentInfo decoded = AssignmentInfo.decode(info.encode());
    assertEquals(info, decoded);
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) Set(java.util.Set) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) HostInfo(org.apache.kafka.streams.state.HostInfo) Test(org.junit.Test)

Aggregations

HostInfo (org.apache.kafka.streams.state.HostInfo)14 TopicPartition (org.apache.kafka.common.TopicPartition)13 HashSet (java.util.HashSet)10 Set (java.util.Set)10 TaskId (org.apache.kafka.streams.processor.TaskId)9 Test (org.junit.Test)9 HashMap (java.util.HashMap)8 AssignmentInfo (org.apache.kafka.streams.processor.internals.assignment.AssignmentInfo)6 PartitionAssignor (org.apache.kafka.clients.consumer.internals.PartitionAssignor)5 UUID (java.util.UUID)4 PartitionInfo (org.apache.kafka.common.PartitionInfo)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 Metrics (org.apache.kafka.common.metrics.Metrics)3 SubscriptionInfo (org.apache.kafka.streams.processor.internals.assignment.SubscriptionInfo)3 IOException (java.io.IOException)2 Properties (java.util.Properties)2 Cluster (org.apache.kafka.common.Cluster)2 Node (org.apache.kafka.common.Node)2 StreamsConfig (org.apache.kafka.streams.StreamsConfig)2