Search in sources :

Example 36 with AbstractRequest

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

the class AbstractCoordinatorTest method prepareFirstHeartbeat.

private AtomicBoolean prepareFirstHeartbeat() {
    final AtomicBoolean heartbeatReceived = new AtomicBoolean(false);
    mockClient.prepareResponse(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            boolean isHeartbeatRequest = body instanceof HeartbeatRequest;
            if (isHeartbeatRequest)
                heartbeatReceived.set(true);
            return isHeartbeatRequest;
        }
    }, heartbeatResponse(Errors.UNKNOWN_SERVER_ERROR));
    return heartbeatReceived;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) MockClient(org.apache.kafka.clients.MockClient) HeartbeatRequest(org.apache.kafka.common.requests.HeartbeatRequest)

Example 37 with AbstractRequest

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

the class AbstractCoordinatorTest method testWakeupAfterSyncGroupSent.

@Test
public void testWakeupAfterSyncGroupSent() throws Exception {
    setupCoordinator(RETRY_BACKOFF_MS);
    mockClient.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    mockClient.prepareResponse(joinGroupFollowerResponse(1, "memberId", "leaderId", Errors.NONE));
    mockClient.prepareResponse(new MockClient.RequestMatcher() {

        private int invocations = 0;

        @Override
        public boolean matches(AbstractRequest body) {
            invocations++;
            boolean isSyncGroupRequest = body instanceof SyncGroupRequest;
            if (isSyncGroupRequest && invocations == 1)
                // simulate wakeup after the request sent
                throw new WakeupException();
            return isSyncGroupRequest;
        }
    }, syncGroupResponse(Errors.NONE));
    AtomicBoolean heartbeatReceived = prepareFirstHeartbeat();
    try {
        coordinator.ensureActiveGroup();
        fail("Should have woken up from ensureActiveGroup()");
    } catch (WakeupException e) {
    }
    assertEquals(1, coordinator.onJoinPrepareInvokes);
    assertEquals(0, coordinator.onJoinCompleteInvokes);
    assertFalse(heartbeatReceived.get());
    coordinator.ensureActiveGroup();
    assertEquals(1, coordinator.onJoinPrepareInvokes);
    assertEquals(1, coordinator.onJoinCompleteInvokes);
    awaitFirstHeartbeat(heartbeatReceived);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SyncGroupRequest(org.apache.kafka.common.requests.SyncGroupRequest) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) WakeupException(org.apache.kafka.common.errors.WakeupException) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Example 38 with AbstractRequest

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

the class AbstractCoordinatorTest method testWakeupAfterSyncGroupReceivedExternalCompletion.

@Test
public void testWakeupAfterSyncGroupReceivedExternalCompletion() throws Exception {
    setupCoordinator(RETRY_BACKOFF_MS);
    mockClient.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    mockClient.prepareResponse(joinGroupFollowerResponse(1, "memberId", "leaderId", Errors.NONE));
    mockClient.prepareResponse(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            boolean isSyncGroupRequest = body instanceof SyncGroupRequest;
            if (isSyncGroupRequest)
                // wakeup after the request returns
                consumerClient.wakeup();
            return isSyncGroupRequest;
        }
    }, syncGroupResponse(Errors.NONE));
    AtomicBoolean heartbeatReceived = prepareFirstHeartbeat();
    try {
        coordinator.ensureActiveGroup();
        fail("Should have woken up from ensureActiveGroup()");
    } catch (WakeupException e) {
    }
    assertEquals(1, coordinator.onJoinPrepareInvokes);
    assertEquals(0, coordinator.onJoinCompleteInvokes);
    assertFalse(heartbeatReceived.get());
    coordinator.ensureActiveGroup();
    assertEquals(1, coordinator.onJoinPrepareInvokes);
    assertEquals(1, coordinator.onJoinCompleteInvokes);
    awaitFirstHeartbeat(heartbeatReceived);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SyncGroupRequest(org.apache.kafka.common.requests.SyncGroupRequest) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) WakeupException(org.apache.kafka.common.errors.WakeupException) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Example 39 with AbstractRequest

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

the class ConsumerCoordinatorTest method testLeaveGroupOnClose.

@Test
public void testLeaveGroupOnClose() {
    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.close(0);
    assertTrue(received.get());
}
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 40 with AbstractRequest

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

the class ConsumerCoordinatorTest method testUpdateMetadataDuringRebalance.

@Test
public void testUpdateMetadataDuringRebalance() {
    final String topic1 = "topic1";
    final String topic2 = "topic2";
    TopicPartition tp1 = new TopicPartition(topic1, 0);
    TopicPartition tp2 = new TopicPartition(topic2, 0);
    final String consumerId = "leader";
    List<String> topics = Arrays.asList(topic1, topic2);
    subscriptions.subscribe(new HashSet<>(topics), rebalanceListener);
    metadata.setTopics(topics);
    // we only have metadata for one topic initially
    metadata.update(TestUtils.singletonCluster(topic1, 1), Collections.<String>emptySet(), time.milliseconds());
    client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady();
    // prepare initial rebalance
    Map<String, List<String>> memberSubscriptions = singletonMap(consumerId, topics);
    partitionAssignor.prepare(singletonMap(consumerId, Collections.singletonList(tp1)));
    client.prepareResponse(joinGroupLeaderResponse(1, consumerId, memberSubscriptions, Errors.NONE));
    client.prepareResponse(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            SyncGroupRequest sync = (SyncGroupRequest) body;
            if (sync.memberId().equals(consumerId) && sync.generationId() == 1 && sync.groupAssignment().containsKey(consumerId)) {
                // trigger the metadata update including both topics after the sync group request has been sent
                Map<String, Integer> topicPartitionCounts = new HashMap<>();
                topicPartitionCounts.put(topic1, 1);
                topicPartitionCounts.put(topic2, 1);
                metadata.update(TestUtils.singletonCluster(topicPartitionCounts), Collections.<String>emptySet(), time.milliseconds());
                return true;
            }
            return false;
        }
    }, syncGroupResponse(Collections.singletonList(tp1), Errors.NONE));
    // the metadata update should trigger a second rebalance
    client.prepareResponse(joinGroupLeaderResponse(2, consumerId, memberSubscriptions, Errors.NONE));
    client.prepareResponse(syncGroupResponse(Arrays.asList(tp1, tp2), Errors.NONE));
    coordinator.poll(time.milliseconds(), Long.MAX_VALUE);
    assertFalse(coordinator.needRejoin());
    assertEquals(new HashSet<>(Arrays.asList(tp1, tp2)), subscriptions.assignedPartitions());
}
Also used : AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) SyncGroupRequest(org.apache.kafka.common.requests.SyncGroupRequest) TopicPartition(org.apache.kafka.common.TopicPartition) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

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