Search in sources :

Example 1 with MockRebalanceListener

use of org.apache.kafka.clients.consumer.internals.MockRebalanceListener in project kafka by apache.

the class StreamThreadTest method shouldNotReturnDataAfterTaskMigrated.

@Test
public void shouldNotReturnDataAfterTaskMigrated() {
    final TaskManager taskManager = EasyMock.createNiceMock(TaskManager.class);
    expect(taskManager.producerClientIds()).andStubReturn(Collections.emptySet());
    final InternalTopologyBuilder internalTopologyBuilder = EasyMock.createNiceMock(InternalTopologyBuilder.class);
    expect(internalTopologyBuilder.fullSourceTopicNames()).andReturn(Collections.singletonList(topic1)).times(2);
    final MockConsumer<byte[], byte[]> consumer = new MockConsumer<>(OffsetResetStrategy.LATEST);
    consumer.subscribe(Collections.singletonList(topic1), new MockRebalanceListener());
    consumer.rebalance(Collections.singletonList(t1p1));
    consumer.updateEndOffsets(Collections.singletonMap(t1p1, 10L));
    consumer.seekToEnd(Collections.singletonList(t1p1));
    final ChangelogReader changelogReader = new MockChangelogReader() {

        @Override
        public void restore(final Map<TaskId, Task> tasks) {
            consumer.addRecord(new ConsumerRecord<>(topic1, 1, 11, new byte[0], new byte[0]));
            consumer.addRecord(new ConsumerRecord<>(topic1, 1, 12, new byte[1], new byte[0]));
            throw new TaskMigratedException("Changelog restore found task migrated", new RuntimeException("restore task migrated"));
        }
    };
    taskManager.handleLostAll();
    EasyMock.replay(taskManager, internalTopologyBuilder);
    final StreamsMetricsImpl streamsMetrics = new StreamsMetricsImpl(metrics, CLIENT_ID, StreamsConfig.METRICS_LATEST, mockTime);
    final StreamThread thread = new StreamThread(mockTime, config, null, consumer, consumer, changelogReader, null, taskManager, streamsMetrics, new TopologyMetadata(internalTopologyBuilder, config), CLIENT_ID, new LogContext(""), new AtomicInteger(), new AtomicLong(Long.MAX_VALUE), new LinkedList<>(), null, HANDLER, null).updateThreadMetadata(getSharedAdminClientId(CLIENT_ID));
    final StreamsException thrown = assertThrows(StreamsException.class, thread::run);
    verify(taskManager);
    assertThat(thrown.getCause(), isA(IllegalStateException.class));
    // The Mock consumer shall throw as the assignment has been wiped out, but records are assigned.
    assertEquals("No current assignment for partition topic1-1", thrown.getCause().getMessage());
    assertFalse(consumer.shouldRebalance());
}
Also used : StreamsException(org.apache.kafka.streams.errors.StreamsException) LogContext(org.apache.kafka.common.utils.LogContext) LinkedList(java.util.LinkedList) MockRebalanceListener(org.apache.kafka.clients.consumer.internals.MockRebalanceListener) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockConsumer(org.apache.kafka.clients.consumer.MockConsumer) StreamsMetricsImpl(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl) Utils.mkMap(org.apache.kafka.common.utils.Utils.mkMap) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) TaskMigratedException(org.apache.kafka.streams.errors.TaskMigratedException) Test(org.junit.Test)

Example 2 with MockRebalanceListener

use of org.apache.kafka.clients.consumer.internals.MockRebalanceListener in project kafka by apache.

the class KafkaConsumerTest method testEnforceRebalanceTriggersRebalanceOnNextPoll.

@Test
public void testEnforceRebalanceTriggersRebalanceOnNextPoll() {
    Time time = new MockTime(1L);
    ConsumerMetadata metadata = createMetadata(subscription);
    MockClient client = new MockClient(time, metadata);
    KafkaConsumer<String, String> consumer = newConsumer(time, client, subscription, metadata, assignor, true, groupInstanceId);
    MockRebalanceListener countingRebalanceListener = new MockRebalanceListener();
    initMetadata(client, Utils.mkMap(Utils.mkEntry(topic, 1), Utils.mkEntry(topic2, 1), Utils.mkEntry(topic3, 1)));
    consumer.subscribe(Arrays.asList(topic, topic2), countingRebalanceListener);
    Node node = metadata.fetch().nodes().get(0);
    prepareRebalance(client, node, assignor, Arrays.asList(tp0, t2p0), null);
    // a first rebalance to get the assignment, we need two poll calls since we need two round trips to finish join / sync-group
    consumer.poll(Duration.ZERO);
    consumer.poll(Duration.ZERO);
    // onPartitionsRevoked is not invoked when first joining the group
    assertEquals(countingRebalanceListener.revokedCount, 0);
    assertEquals(countingRebalanceListener.assignedCount, 1);
    consumer.enforceRebalance();
    // the next poll should trigger a rebalance
    consumer.poll(Duration.ZERO);
    assertEquals(countingRebalanceListener.revokedCount, 1);
}
Also used : ConsumerMetadata(org.apache.kafka.clients.consumer.internals.ConsumerMetadata) MockRebalanceListener(org.apache.kafka.clients.consumer.internals.MockRebalanceListener) Node(org.apache.kafka.common.Node) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.jupiter.api.Test)

Aggregations

MockRebalanceListener (org.apache.kafka.clients.consumer.internals.MockRebalanceListener)2 Collections.emptyMap (java.util.Collections.emptyMap)1 Collections.singletonMap (java.util.Collections.singletonMap)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 MockClient (org.apache.kafka.clients.MockClient)1 MockConsumer (org.apache.kafka.clients.consumer.MockConsumer)1 ConsumerMetadata (org.apache.kafka.clients.consumer.internals.ConsumerMetadata)1 Node (org.apache.kafka.common.Node)1 LogContext (org.apache.kafka.common.utils.LogContext)1 MockTime (org.apache.kafka.common.utils.MockTime)1 Time (org.apache.kafka.common.utils.Time)1 Utils.mkMap (org.apache.kafka.common.utils.Utils.mkMap)1 StreamsException (org.apache.kafka.streams.errors.StreamsException)1 TaskMigratedException (org.apache.kafka.streams.errors.TaskMigratedException)1 StreamsMetricsImpl (org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl)1 Test (org.junit.Test)1