Search in sources :

Example 41 with AbstractRequest

use of org.apache.kafka.common.requests.AbstractRequest in project apache-kafka-on-k8s by banzaicloud.

the class ConsumerCoordinatorTest method testMaybeLeaveGroup.

@Test
public void testMaybeLeaveGroup() {
    final String consumerId = "consumer";
    subscriptions.subscribe(singleton(topic1), rebalanceListener);
    joinAsFollowerAndReceiveAssignment(consumerId, coordinator, singletonList(t1p));
    final AtomicBoolean received = new AtomicBoolean(false);
    client.prepareResponse(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            received.set(true);
            LeaveGroupRequest leaveRequest = (LeaveGroupRequest) body;
            return leaveRequest.memberId().equals(consumerId) && leaveRequest.groupId().equals(groupId);
        }
    }, new LeaveGroupResponse(Errors.NONE));
    coordinator.maybeLeaveGroup();
    assertTrue(received.get());
    AbstractCoordinator.Generation generation = coordinator.generation();
    assertNull(generation);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LeaveGroupResponse(org.apache.kafka.common.requests.LeaveGroupResponse) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) LeaveGroupRequest(org.apache.kafka.common.requests.LeaveGroupRequest) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Example 42 with AbstractRequest

use of org.apache.kafka.common.requests.AbstractRequest in project apache-kafka-on-k8s by banzaicloud.

the class ConsumerCoordinatorTest method testCommitAfterLeaveGroup.

@Test
public void testCommitAfterLeaveGroup() {
    // enable auto-assignment
    subscriptions.subscribe(singleton(topic1), rebalanceListener);
    joinAsFollowerAndReceiveAssignment("consumer", coordinator, singletonList(t1p));
    // now switch to manual assignment
    client.prepareResponse(new LeaveGroupResponse(Errors.NONE));
    subscriptions.unsubscribe();
    coordinator.maybeLeaveGroup();
    subscriptions.assignFromUser(singleton(t1p));
    // the client should not reuse generation/memberId from auto-subscribed generation
    client.prepareResponse(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            OffsetCommitRequest commitRequest = (OffsetCommitRequest) body;
            return commitRequest.memberId().equals(OffsetCommitRequest.DEFAULT_MEMBER_ID) && commitRequest.generationId() == OffsetCommitRequest.DEFAULT_GENERATION_ID;
        }
    }, offsetCommitResponse(singletonMap(t1p, Errors.NONE)));
    AtomicBoolean success = new AtomicBoolean(false);
    coordinator.commitOffsetsAsync(singletonMap(t1p, new OffsetAndMetadata(100L)), callback(success));
    coordinator.invokeCompletedOffsetCommitCallbacks();
    assertTrue(success.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LeaveGroupResponse(org.apache.kafka.common.requests.LeaveGroupResponse) OffsetCommitRequest(org.apache.kafka.common.requests.OffsetCommitRequest) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Example 43 with AbstractRequest

use of org.apache.kafka.common.requests.AbstractRequest in project apache-kafka-on-k8s by banzaicloud.

the class FetcherTest method testReturnCommittedTransactions.

@Test
public void testReturnCommittedTransactions() {
    Fetcher<byte[], byte[]> fetcher = createFetcher(subscriptions, new Metrics(), new ByteArrayDeserializer(), new ByteArrayDeserializer(), Integer.MAX_VALUE, IsolationLevel.READ_COMMITTED);
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    int currentOffset = 0;
    currentOffset += appendTransactionalRecords(buffer, 1L, currentOffset, new SimpleRecord(time.milliseconds(), "key".getBytes(), "value".getBytes()), new SimpleRecord(time.milliseconds(), "key".getBytes(), "value".getBytes()));
    currentOffset += commitTransaction(buffer, 1L, currentOffset);
    buffer.flip();
    List<FetchResponse.AbortedTransaction> abortedTransactions = new ArrayList<>();
    MemoryRecords records = MemoryRecords.readableRecords(buffer);
    subscriptions.assignFromUser(singleton(tp0));
    subscriptions.seek(tp0, 0);
    // normal fetch
    assertEquals(1, fetcher.sendFetches());
    assertFalse(fetcher.hasCompletedFetches());
    client.prepareResponse(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            FetchRequest request = (FetchRequest) body;
            assertEquals(IsolationLevel.READ_COMMITTED, request.isolationLevel());
            return true;
        }
    }, fullFetchResponseWithAbortedTransactions(records, abortedTransactions, Errors.NONE, 100L, 100L, 0));
    consumerClient.poll(0);
    assertTrue(fetcher.hasCompletedFetches());
    Map<TopicPartition, List<ConsumerRecord<byte[], byte[]>>> fetchedRecords = fetcher.fetchedRecords();
    assertTrue(fetchedRecords.containsKey(tp0));
    assertEquals(fetchedRecords.get(tp0).size(), 2);
}
Also used : AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Metrics(org.apache.kafka.common.metrics.Metrics) TopicPartition(org.apache.kafka.common.TopicPartition) SimpleRecord(org.apache.kafka.common.record.SimpleRecord) FetchRequest(org.apache.kafka.common.requests.FetchRequest) List(java.util.List) ArrayList(java.util.ArrayList) ByteArrayDeserializer(org.apache.kafka.common.serialization.ByteArrayDeserializer) MemoryRecords(org.apache.kafka.common.record.MemoryRecords) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Example 44 with AbstractRequest

use of org.apache.kafka.common.requests.AbstractRequest in project apache-kafka-on-k8s by banzaicloud.

the class FetcherTest method testListOffsetsSendsIsolationLevel.

@Test
public void testListOffsetsSendsIsolationLevel() {
    for (final IsolationLevel isolationLevel : IsolationLevel.values()) {
        Fetcher<byte[], byte[]> fetcher = createFetcher(subscriptions, new Metrics(), new ByteArrayDeserializer(), new ByteArrayDeserializer(), Integer.MAX_VALUE, isolationLevel);
        subscriptions.assignFromUser(singleton(tp0));
        subscriptions.requestOffsetReset(tp0, OffsetResetStrategy.LATEST);
        client.prepareResponse(new MockClient.RequestMatcher() {

            @Override
            public boolean matches(AbstractRequest body) {
                ListOffsetRequest request = (ListOffsetRequest) body;
                return request.isolationLevel() == isolationLevel;
            }
        }, listOffsetResponse(Errors.NONE, 1L, 5L));
        fetcher.resetOffsetsIfNeeded();
        consumerClient.pollNoWakeup();
        assertFalse(subscriptions.isOffsetResetNeeded(tp0));
        assertTrue(subscriptions.isFetchable(tp0));
        assertEquals(5, subscriptions.position(tp0).longValue());
    }
}
Also used : Metrics(org.apache.kafka.common.metrics.Metrics) IsolationLevel(org.apache.kafka.common.requests.IsolationLevel) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) ListOffsetRequest(org.apache.kafka.common.requests.ListOffsetRequest) ByteArrayDeserializer(org.apache.kafka.common.serialization.ByteArrayDeserializer) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Example 45 with AbstractRequest

use of org.apache.kafka.common.requests.AbstractRequest in project apache-kafka-on-k8s by banzaicloud.

the class TransactionManagerTest method endTxnMatcher.

private MockClient.RequestMatcher endTxnMatcher(final TransactionResult result, final long pid, final short epoch) {
    return new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            EndTxnRequest endTxnRequest = (EndTxnRequest) body;
            assertEquals(transactionalId, endTxnRequest.transactionalId());
            assertEquals(pid, endTxnRequest.producerId());
            assertEquals(epoch, endTxnRequest.producerEpoch());
            assertEquals(result, endTxnRequest.command());
            return true;
        }
    };
}
Also used : EndTxnRequest(org.apache.kafka.common.requests.EndTxnRequest) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest)

Aggregations

AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)48 MockClient (org.apache.kafka.clients.MockClient)38 Test (org.junit.Test)27 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)20 WakeupException (org.apache.kafka.common.errors.WakeupException)12 Node (org.apache.kafka.common.Node)11 ProduceRequest (org.apache.kafka.common.requests.ProduceRequest)9 SyncGroupRequest (org.apache.kafka.common.requests.SyncGroupRequest)9 JoinGroupRequest (org.apache.kafka.common.requests.JoinGroupRequest)8 HashMap (java.util.HashMap)7 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)7 TopicPartition (org.apache.kafka.common.TopicPartition)6 Map (java.util.Map)5 MemoryRecords (org.apache.kafka.common.record.MemoryRecords)5 LeaveGroupResponse (org.apache.kafka.common.requests.LeaveGroupResponse)4 Collections.singletonMap (java.util.Collections.singletonMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Metrics (org.apache.kafka.common.metrics.Metrics)3 MutableRecordBatch (org.apache.kafka.common.record.MutableRecordBatch)3 FetchRequest (org.apache.kafka.common.requests.FetchRequest)3