use of org.apache.kafka.streams.ThreadMetadata in project kafka by apache.
the class AdjustStreamThreadCountTest method shouldAddAndRemoveStreamThreadsWhileKeepingNamesCorrect.
@Test
public void shouldAddAndRemoveStreamThreadsWhileKeepingNamesCorrect() throws Exception {
try (final KafkaStreams kafkaStreams = new KafkaStreams(builder.build(), properties)) {
addStreamStateChangeListener(kafkaStreams);
startStreamsAndWaitForRunning(kafkaStreams);
int oldThreadCount = kafkaStreams.metadataForLocalThreads().size();
stateTransitionHistory.clear();
assertThat(kafkaStreams.metadataForLocalThreads().stream().map(t -> t.threadName().split("-StreamThread-")[1]).sorted().toArray(), equalTo(new String[] { "1", "2" }));
final Optional<String> name = kafkaStreams.addStreamThread();
assertThat("New thread has index 3", "3".equals(name.get().split("-StreamThread-")[1]));
TestUtils.waitForCondition(() -> kafkaStreams.metadataForLocalThreads().stream().sequential().map(ThreadMetadata::threadName).anyMatch(t -> t.equals(name.get())), "Stream thread has not been added");
assertThat(kafkaStreams.metadataForLocalThreads().size(), equalTo(oldThreadCount + 1));
assertThat(kafkaStreams.metadataForLocalThreads().stream().map(t -> t.threadName().split("-StreamThread-")[1]).sorted().toArray(), equalTo(new String[] { "1", "2", "3" }));
waitForTransitionFromRebalancingToRunning();
oldThreadCount = kafkaStreams.metadataForLocalThreads().size();
stateTransitionHistory.clear();
final Optional<String> removedThread = kafkaStreams.removeStreamThread();
assertThat(removedThread, not(Optional.empty()));
assertThat(kafkaStreams.metadataForLocalThreads().size(), equalTo(oldThreadCount - 1));
waitForTransitionFromRebalancingToRunning();
stateTransitionHistory.clear();
final Optional<String> name2 = kafkaStreams.addStreamThread();
assertThat(name2, not(Optional.empty()));
TestUtils.waitForCondition(() -> kafkaStreams.metadataForLocalThreads().stream().sequential().map(ThreadMetadata::threadName).anyMatch(t -> t.equals(name2.orElse(""))), "Wait for the thread to be added");
assertThat(kafkaStreams.metadataForLocalThreads().size(), equalTo(oldThreadCount));
assertThat(kafkaStreams.metadataForLocalThreads().stream().map(t -> t.threadName().split("-StreamThread-")[1]).sorted().toArray(), equalTo(new String[] { "1", "2", "3" }));
assertThat("the new thread should have received the old threads name", name2.equals(removedThread));
waitForTransitionFromRebalancingToRunning();
}
}
use of org.apache.kafka.streams.ThreadMetadata in project kafka by apache.
the class StreamThreadTest method shouldReturnActiveTaskMetadataWhileRunningState.
@Test
public void shouldReturnActiveTaskMetadataWhileRunningState() {
internalTopologyBuilder.addSource(null, "source", null, null, null, topic1);
clientSupplier.setCluster(createCluster());
final StreamsMetricsImpl streamsMetrics = new StreamsMetricsImpl(metrics, APPLICATION_ID, config.getString(StreamsConfig.BUILT_IN_METRICS_VERSION_CONFIG), mockTime);
final TopologyMetadata topologyMetadata = new TopologyMetadata(internalTopologyBuilder, config);
topologyMetadata.buildAndRewriteTopology();
final StreamThread thread = StreamThread.create(topologyMetadata, config, clientSupplier, clientSupplier.getAdmin(config.getAdminConfigs(CLIENT_ID)), PROCESS_ID, CLIENT_ID, streamsMetrics, mockTime, streamsMetadataState, 0, stateDirectory, new MockStateRestoreListener(), threadIdx, null, HANDLER);
thread.setState(StreamThread.State.STARTING);
thread.rebalanceListener().onPartitionsRevoked(Collections.emptySet());
final Map<TaskId, Set<TopicPartition>> activeTasks = new HashMap<>();
final List<TopicPartition> assignedPartitions = new ArrayList<>();
// assign single partition
assignedPartitions.add(t1p1);
activeTasks.put(task1, Collections.singleton(t1p1));
thread.taskManager().handleAssignment(activeTasks, emptyMap());
final MockConsumer<byte[], byte[]> mockConsumer = (MockConsumer<byte[], byte[]>) thread.mainConsumer();
mockConsumer.assign(assignedPartitions);
mockConsumer.updateBeginningOffsets(Collections.singletonMap(t1p1, 0L));
thread.rebalanceListener().onPartitionsAssigned(assignedPartitions);
thread.runOnce();
final ThreadMetadata metadata = thread.threadMetadata();
assertEquals(StreamThread.State.RUNNING.name(), metadata.threadState());
assertTrue(metadata.activeTasks().contains(new TaskMetadataImpl(task1, Utils.mkSet(t1p1), new HashMap<>(), new HashMap<>(), Optional.empty())));
assertTrue(metadata.standbyTasks().isEmpty());
assertTrue("#threadState() was: " + metadata.threadState() + "; expected either RUNNING, STARTING, PARTITIONS_REVOKED, PARTITIONS_ASSIGNED, or CREATED", Arrays.asList("RUNNING", "STARTING", "PARTITIONS_REVOKED", "PARTITIONS_ASSIGNED", "CREATED").contains(metadata.threadState()));
final String threadName = metadata.threadName();
assertThat(threadName, startsWith(CLIENT_ID + "-StreamThread-" + threadIdx));
assertEquals(threadName + "-consumer", metadata.consumerClientId());
assertEquals(threadName + "-restore-consumer", metadata.restoreConsumerClientId());
assertEquals(Collections.singleton(threadName + "-producer"), metadata.producerClientIds());
assertEquals(CLIENT_ID + "-admin", metadata.adminClientId());
}
use of org.apache.kafka.streams.ThreadMetadata in project kafka by apache.
the class StreamThreadTest method shouldReturnStandbyTaskMetadataWhileRunningState.
@Test
public void shouldReturnStandbyTaskMetadataWhileRunningState() {
internalStreamsBuilder.stream(Collections.singleton(topic1), consumed).groupByKey().count(Materialized.as("count-one"));
internalStreamsBuilder.buildAndOptimizeTopology();
final StreamThread thread = createStreamThread(CLIENT_ID, config, false);
final MockConsumer<byte[], byte[]> restoreConsumer = clientSupplier.restoreConsumer;
restoreConsumer.updatePartitions("stream-thread-test-count-one-changelog", Collections.singletonList(new PartitionInfo("stream-thread-test-count-one-changelog", 0, null, new Node[0], new Node[0])));
final HashMap<TopicPartition, Long> offsets = new HashMap<>();
offsets.put(new TopicPartition("stream-thread-test-count-one-changelog", 1), 0L);
restoreConsumer.updateEndOffsets(offsets);
restoreConsumer.updateBeginningOffsets(offsets);
thread.setState(StreamThread.State.STARTING);
thread.rebalanceListener().onPartitionsRevoked(Collections.emptySet());
final Map<TaskId, Set<TopicPartition>> standbyTasks = new HashMap<>();
// assign single partition
standbyTasks.put(task1, Collections.singleton(t1p1));
thread.taskManager().handleAssignment(emptyMap(), standbyTasks);
thread.rebalanceListener().onPartitionsAssigned(Collections.emptyList());
thread.runOnce();
final ThreadMetadata threadMetadata = thread.threadMetadata();
assertEquals(StreamThread.State.RUNNING.name(), threadMetadata.threadState());
assertTrue(threadMetadata.standbyTasks().contains(new TaskMetadataImpl(task1, Utils.mkSet(t1p1), new HashMap<>(), new HashMap<>(), Optional.empty())));
assertTrue(threadMetadata.activeTasks().isEmpty());
thread.taskManager().shutdown(true);
}
use of org.apache.kafka.streams.ThreadMetadata in project kafka by apache.
the class ThreadMetadataImplTest method shouldBeEqualIfSameObject.
@Test
public void shouldBeEqualIfSameObject() {
final ThreadMetadata same = new ThreadMetadataImpl(THREAD_NAME, THREAD_STATE, MAIN_CONSUMER_CLIENT_ID, RESTORE_CONSUMER_CLIENT_ID, PRODUCER_CLIENT_IDS, ADMIN_CLIENT_ID, ACTIVE_TASKS, STANDBY_TASKS);
assertThat(threadMetadata, equalTo(same));
assertThat(threadMetadata.hashCode(), equalTo(same.hashCode()));
}
use of org.apache.kafka.streams.ThreadMetadata in project kafka by apache.
the class ThreadMetadataImplTest method shouldNotBeEqualIfDifferInThreadName.
@Test
public void shouldNotBeEqualIfDifferInThreadName() {
final ThreadMetadata differThreadName = new ThreadMetadataImpl("different", THREAD_STATE, MAIN_CONSUMER_CLIENT_ID, RESTORE_CONSUMER_CLIENT_ID, PRODUCER_CLIENT_IDS, ADMIN_CLIENT_ID, ACTIVE_TASKS, STANDBY_TASKS);
assertThat(threadMetadata, not(equalTo(differThreadName)));
assertThat(threadMetadata.hashCode(), not(equalTo(differThreadName.hashCode())));
}
Aggregations