Search in sources :

Example 46 with HostInfo

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

the class StreamsPartitionAssignorTest method testAssignWithStandbyReplicas.

@Test
public void testAssignWithStandbyReplicas() {
    builder.addSource(null, "source1", null, null, null, "topic1");
    builder.addSource(null, "source2", null, null, null, "topic2");
    builder.addProcessor("processor", new MockApiProcessorSupplier<>(), "source1", "source2");
    builder.addStateStore(new MockKeyValueStoreBuilder("store1", false), "processor");
    final List<String> topics = asList("topic1", "topic2");
    final Set<TopicPartition> allTopicPartitions = topics.stream().map(topic -> asList(new TopicPartition(topic, 0), new TopicPartition(topic, 1), new TopicPartition(topic, 2))).flatMap(Collection::stream).collect(Collectors.toSet());
    final Set<TaskId> allTasks = mkSet(TASK_0_0, TASK_0_1, TASK_0_2);
    final Set<TaskId> prevTasks00 = mkSet(TASK_0_0);
    final Set<TaskId> prevTasks01 = mkSet(TASK_0_1);
    final Set<TaskId> prevTasks02 = mkSet(TASK_0_2);
    final Set<TaskId> standbyTasks00 = mkSet(TASK_0_0);
    final Set<TaskId> standbyTasks01 = mkSet(TASK_0_1);
    final Set<TaskId> standbyTasks02 = mkSet(TASK_0_2);
    createMockTaskManager(prevTasks00, standbyTasks01);
    adminClient = createMockAdminClientForAssignor(getTopicPartitionOffsetsMap(singletonList(APPLICATION_ID + "-store1-changelog"), singletonList(3)));
    configurePartitionAssignorWith(Collections.singletonMap(StreamsConfig.NUM_STANDBY_REPLICAS_CONFIG, 1));
    subscriptions.put("consumer10", new Subscription(topics, getInfo(UUID_1, prevTasks00, EMPTY_TASKS, USER_END_POINT).encode()));
    subscriptions.put("consumer11", new Subscription(topics, getInfo(UUID_1, prevTasks01, standbyTasks02, USER_END_POINT).encode()));
    subscriptions.put("consumer20", new Subscription(topics, getInfo(UUID_2, prevTasks02, standbyTasks00, OTHER_END_POINT).encode()));
    final Map<String, Assignment> assignments = partitionAssignor.assign(metadata, new GroupSubscription(subscriptions)).groupAssignment();
    // the first consumer
    final AssignmentInfo info10 = checkAssignment(allTopics, assignments.get("consumer10"));
    final Set<TaskId> allActiveTasks = new HashSet<>(info10.activeTasks());
    final Set<TaskId> allStandbyTasks = new HashSet<>(info10.standbyTasks().keySet());
    // the second consumer
    final AssignmentInfo info11 = checkAssignment(allTopics, assignments.get("consumer11"));
    allActiveTasks.addAll(info11.activeTasks());
    allStandbyTasks.addAll(info11.standbyTasks().keySet());
    assertNotEquals("same processId has same set of standby tasks", info11.standbyTasks().keySet(), info10.standbyTasks().keySet());
    // check active tasks assigned to the first client
    assertEquals(mkSet(TASK_0_0, TASK_0_1), new HashSet<>(allActiveTasks));
    assertEquals(mkSet(TASK_0_2), new HashSet<>(allStandbyTasks));
    // the third consumer
    final AssignmentInfo info20 = checkAssignment(allTopics, assignments.get("consumer20"));
    allActiveTasks.addAll(info20.activeTasks());
    allStandbyTasks.addAll(info20.standbyTasks().keySet());
    // all task ids are in the active tasks and also in the standby tasks
    assertEquals(3, allActiveTasks.size());
    assertEquals(allTasks, allActiveTasks);
    assertEquals(3, allStandbyTasks.size());
    assertEquals(allTasks, allStandbyTasks);
    // Check host partition assignments
    final Map<HostInfo, Set<TopicPartition>> partitionsByHost = info10.partitionsByHost();
    assertEquals(2, partitionsByHost.size());
    assertEquals(allTopicPartitions, partitionsByHost.values().stream().flatMap(Collection::stream).collect(Collectors.toSet()));
    final Map<HostInfo, Set<TopicPartition>> standbyPartitionsByHost = info10.standbyPartitionByHost();
    assertEquals(2, standbyPartitionsByHost.size());
    assertEquals(allTopicPartitions, standbyPartitionsByHost.values().stream().flatMap(Collection::stream).collect(Collectors.toSet()));
    for (final HostInfo hostInfo : partitionsByHost.keySet()) {
        assertTrue(Collections.disjoint(partitionsByHost.get(hostInfo), standbyPartitionsByHost.get(hostInfo)));
    }
    // All consumers got the same host info
    assertEquals(partitionsByHost, info11.partitionsByHost());
    assertEquals(partitionsByHost, info20.partitionsByHost());
    assertEquals(standbyPartitionsByHost, info11.standbyPartitionByHost());
    assertEquals(standbyPartitionsByHost, info20.standbyPartitionByHost());
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) Utils.mkSet(org.apache.kafka.common.utils.Utils.mkSet) Set(java.util.Set) Utils.mkSortedSet(org.apache.kafka.common.utils.Utils.mkSortedSet) SortedSet(java.util.SortedSet) HashSet(java.util.HashSet) Collections.emptySet(java.util.Collections.emptySet) Assignment(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Assignment) AssignmentInfo(org.apache.kafka.streams.processor.internals.assignment.AssignmentInfo) TopicPartition(org.apache.kafka.common.TopicPartition) Collection(java.util.Collection) GroupSubscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.GroupSubscription) GroupSubscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.GroupSubscription) Subscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription) HostInfo(org.apache.kafka.streams.state.HostInfo) MockKeyValueStoreBuilder(org.apache.kafka.test.MockKeyValueStoreBuilder) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 47 with HostInfo

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

the class StreamsMetadataStateTest method before.

@Before
public void before() {
    builder = new StreamsBuilder();
    final KStream<Object, Object> one = builder.stream("topic-one");
    one.groupByKey().count(Materialized.as("table-one"));
    final KStream<Object, Object> two = builder.stream("topic-two");
    two.groupByKey().count(Materialized.as("table-two"));
    builder.stream("topic-three").groupByKey().count(Materialized.as("table-three"));
    one.merge(two).groupByKey().count(Materialized.as("merged-table"));
    builder.stream("topic-four").mapValues(value -> value);
    builder.globalTable("global-topic", Consumed.with(null, null), Materialized.as(globalTable));
    TopologyWrapper.getInternalTopologyBuilder(builder.build()).setApplicationId("appId");
    topic1P0 = new TopicPartition("topic-one", 0);
    topic1P1 = new TopicPartition("topic-one", 1);
    topic2P0 = new TopicPartition("topic-two", 0);
    topic2P1 = new TopicPartition("topic-two", 1);
    topic3P0 = new TopicPartition("topic-three", 0);
    topic4P0 = new TopicPartition("topic-four", 0);
    hostOne = new HostInfo("host-one", 8080);
    hostTwo = new HostInfo("host-two", 9090);
    hostThree = new HostInfo("host-three", 7070);
    hostToActivePartitions = new HashMap<>();
    hostToActivePartitions.put(hostOne, mkSet(topic1P0, topic2P1, topic4P0));
    hostToActivePartitions.put(hostTwo, mkSet(topic2P0, topic1P1));
    hostToActivePartitions.put(hostThree, Collections.singleton(topic3P0));
    hostToStandbyPartitions = new HashMap<>();
    hostToStandbyPartitions.put(hostThree, mkSet(topic1P0, topic2P1, topic4P0));
    hostToStandbyPartitions.put(hostOne, mkSet(topic2P0, topic1P1));
    hostToStandbyPartitions.put(hostTwo, Collections.singleton(topic3P0));
    final List<PartitionInfo> partitionInfos = Arrays.asList(new PartitionInfo("topic-one", 0, null, null, null), new PartitionInfo("topic-one", 1, null, null, null), new PartitionInfo("topic-two", 0, null, null, null), new PartitionInfo("topic-two", 1, null, null, null), new PartitionInfo("topic-three", 0, null, null, null), new PartitionInfo("topic-four", 0, null, null, null));
    cluster = new Cluster(null, Collections.<Node>emptyList(), partitionInfos, Collections.<String>emptySet(), Collections.<String>emptySet());
    final TopologyMetadata topologyMetadata = new TopologyMetadata(TopologyWrapper.getInternalTopologyBuilder(builder.build()), new DummyStreamsConfig());
    topologyMetadata.buildAndRewriteTopology();
    metadataState = new StreamsMetadataState(topologyMetadata, hostOne, logContext);
    metadataState.onChange(hostToActivePartitions, hostToStandbyPartitions, cluster);
    partitioner = (topic, key, value, numPartitions) -> 1;
    storeNames = mkSet("table-one", "table-two", "merged-table", globalTable);
}
Also used : DummyStreamsConfig(org.apache.kafka.streams.processor.internals.testutil.DummyStreamsConfig) Node(org.apache.kafka.common.Node) Cluster(org.apache.kafka.common.Cluster) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) TopicPartition(org.apache.kafka.common.TopicPartition) PartitionInfo(org.apache.kafka.common.PartitionInfo) HostInfo(org.apache.kafka.streams.state.HostInfo) Before(org.junit.Before)

Example 48 with HostInfo

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

the class ServerUtilTest method shouldReturnServerPortWithTrailingSlash.

@Test
public void shouldReturnServerPortWithTrailingSlash() {
    // When:
    final HostInfo hostInfo = ServerUtil.parseHostInfo("http://localhost:8088/");
    // Then:
    assertThat(hostInfo.port(), Matchers.is(8088));
}
Also used : HostInfo(org.apache.kafka.streams.state.HostInfo) Test(org.junit.Test)

Example 49 with HostInfo

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

the class ActiveHostFilterTest method setUp.

@Before
public void setUp() {
    activeHost = new KsqlHostInfo("activeHost", 2345);
    HostInfo activeHostInfo = new HostInfo("activeHost", 2345);
    standByHost = new KsqlHostInfo("standby1", 1234);
    activeHostFilter = new ActiveHostFilter(activeHostInfo);
}
Also used : KsqlHostInfo(io.confluent.ksql.util.KsqlHostInfo) HostInfo(org.apache.kafka.streams.state.HostInfo) KsqlHostInfo(io.confluent.ksql.util.KsqlHostInfo) Before(org.junit.Before)

Example 50 with HostInfo

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

the class KsqlResource method configure.

@Override
public void configure(final KsqlConfig config) {
    if (!config.getKsqlStreamConfigProps().containsKey(StreamsConfig.APPLICATION_SERVER_CONFIG)) {
        throw new IllegalArgumentException("Need KS application server set");
    }
    final String applicationServer = (String) config.getKsqlStreamConfigProps().get(StreamsConfig.APPLICATION_SERVER_CONFIG);
    final HostInfo hostInfo = ServerUtil.parseHostInfo(applicationServer);
    this.localHost = new KsqlHostInfo(hostInfo.host(), hostInfo.port());
    try {
        this.localUrl = new URL(applicationServer);
    } catch (final Exception e) {
        throw new IllegalStateException("Failed to convert remote host info to URL." + " remoteInfo: " + localHost.host() + ":" + localHost.host());
    }
    this.validator = new RequestValidator(CustomValidators.VALIDATOR_MAP, injectorFactory, ksqlEngine::createSandbox, new ValidatedCommandFactory());
    this.handler = new RequestHandler(customExecutors.EXECUTOR_MAP, new DistributingExecutor(config, commandRunner.getCommandQueue(), distributedCmdResponseTimeout, injectorFactory, authorizationValidator, new ValidatedCommandFactory(), errorHandler, commandRunnerWarning), ksqlEngine, new DefaultCommandQueueSync(commandRunner.getCommandQueue(), this::shouldSynchronize, distributedCmdResponseTimeout));
}
Also used : DistributingExecutor(io.confluent.ksql.rest.server.computation.DistributingExecutor) RequestValidator(io.confluent.ksql.rest.server.validation.RequestValidator) RequestHandler(io.confluent.ksql.rest.server.execution.RequestHandler) KsqlHostInfo(io.confluent.ksql.util.KsqlHostInfo) DefaultCommandQueueSync(io.confluent.ksql.rest.server.execution.DefaultCommandQueueSync) ValidatedCommandFactory(io.confluent.ksql.rest.server.computation.ValidatedCommandFactory) HostInfo(org.apache.kafka.streams.state.HostInfo) KsqlHostInfo(io.confluent.ksql.util.KsqlHostInfo) URL(java.net.URL) PatternSyntaxException(java.util.regex.PatternSyntaxException) KsqlException(io.confluent.ksql.util.KsqlException) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException)

Aggregations

HostInfo (org.apache.kafka.streams.state.HostInfo)57 TopicPartition (org.apache.kafka.common.TopicPartition)31 HashSet (java.util.HashSet)30 Test (org.junit.Test)27 Set (java.util.Set)25 HashMap (java.util.HashMap)22 TaskId (org.apache.kafka.streams.processor.TaskId)18 AssignmentInfo (org.apache.kafka.streams.processor.internals.assignment.AssignmentInfo)16 KsqlHostInfo (io.confluent.ksql.util.KsqlHostInfo)12 Map (java.util.Map)12 PartitionInfo (org.apache.kafka.common.PartitionInfo)11 ArrayList (java.util.ArrayList)10 UUID (java.util.UUID)9 Cluster (org.apache.kafka.common.Cluster)9 PartitionAssignor (org.apache.kafka.clients.consumer.internals.PartitionAssignor)8 StreamsMetadata (org.apache.kafka.streams.StreamsMetadata)7 SubscriptionInfo (org.apache.kafka.streams.processor.internals.assignment.SubscriptionInfo)7 List (java.util.List)6 Node (org.apache.kafka.common.Node)6 KsqlNode (io.confluent.ksql.execution.streams.materialization.Locator.KsqlNode)5