Search in sources :

Example 1 with PartitionData

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

the class KafkaConsumerTest method prepareOffsetCommitResponse.

private AtomicBoolean prepareOffsetCommitResponse(MockClient client, Node coordinator, final Map<TopicPartition, Long> partitionOffsets) {
    final AtomicBoolean commitReceived = new AtomicBoolean(true);
    Map<TopicPartition, Errors> response = new HashMap<>();
    for (TopicPartition partition : partitionOffsets.keySet()) response.put(partition, Errors.NONE);
    client.prepareResponseFrom(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            OffsetCommitRequest commitRequest = (OffsetCommitRequest) body;
            for (Map.Entry<TopicPartition, Long> partitionOffset : partitionOffsets.entrySet()) {
                OffsetCommitRequest.PartitionData partitionData = commitRequest.offsetData().get(partitionOffset.getKey());
                // verify that the expected offset has been committed
                if (partitionData.offset != partitionOffset.getValue()) {
                    commitReceived.set(false);
                    return false;
                }
            }
            return true;
        }
    }, offsetCommitResponse(response), coordinator);
    return commitReceived;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Errors(org.apache.kafka.common.protocol.Errors) OffsetCommitRequest(org.apache.kafka.common.requests.OffsetCommitRequest) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PartitionData(org.apache.kafka.common.requests.FetchResponse.PartitionData) TopicPartition(org.apache.kafka.common.TopicPartition) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) MockClient(org.apache.kafka.clients.MockClient)

Example 2 with PartitionData

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

the class KafkaConsumerTest method fetchResponse.

private FetchResponse fetchResponse(Map<TopicPartition, FetchInfo> fetches) {
    LinkedHashMap<TopicPartition, PartitionData> tpResponses = new LinkedHashMap<>();
    for (Map.Entry<TopicPartition, FetchInfo> fetchEntry : fetches.entrySet()) {
        TopicPartition partition = fetchEntry.getKey();
        long fetchOffset = fetchEntry.getValue().offset;
        int fetchCount = fetchEntry.getValue().count;
        final MemoryRecords records;
        if (fetchCount == 0) {
            records = MemoryRecords.EMPTY;
        } else {
            MemoryRecordsBuilder builder = MemoryRecords.builder(ByteBuffer.allocate(1024), CompressionType.NONE, TimestampType.CREATE_TIME, fetchOffset);
            for (int i = 0; i < fetchCount; i++) builder.append(0L, ("key-" + i).getBytes(), ("value-" + i).getBytes());
            records = builder.build();
        }
        tpResponses.put(partition, new FetchResponse.PartitionData(Errors.NONE, 0, FetchResponse.INVALID_LAST_STABLE_OFFSET, 0L, null, records));
    }
    return new FetchResponse(Errors.NONE, tpResponses, 0, INVALID_SESSION_ID);
}
Also used : PartitionData(org.apache.kafka.common.requests.FetchResponse.PartitionData) FetchResponse(org.apache.kafka.common.requests.FetchResponse) OffsetFetchResponse(org.apache.kafka.common.requests.OffsetFetchResponse) LinkedHashMap(java.util.LinkedHashMap) PartitionData(org.apache.kafka.common.requests.FetchResponse.PartitionData) TopicPartition(org.apache.kafka.common.TopicPartition) MemoryRecordsBuilder(org.apache.kafka.common.record.MemoryRecordsBuilder) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Collections.singletonMap(java.util.Collections.singletonMap) MemoryRecords(org.apache.kafka.common.record.MemoryRecords)

Aggregations

HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 PartitionData (org.apache.kafka.common.requests.FetchResponse.PartitionData)2 Collections.singletonMap (java.util.Collections.singletonMap)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 MockClient (org.apache.kafka.clients.MockClient)1 Errors (org.apache.kafka.common.protocol.Errors)1 MemoryRecords (org.apache.kafka.common.record.MemoryRecords)1 MemoryRecordsBuilder (org.apache.kafka.common.record.MemoryRecordsBuilder)1 AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)1 FetchResponse (org.apache.kafka.common.requests.FetchResponse)1 OffsetCommitRequest (org.apache.kafka.common.requests.OffsetCommitRequest)1 OffsetFetchResponse (org.apache.kafka.common.requests.OffsetFetchResponse)1