Skip to content

Commit

Permalink
KAFKA-16730: Initial version of share group consumer client code (apa…
Browse files Browse the repository at this point in the history
…che#16461)

This is the initial version of the share group consumer client code. It implements the complete ShareConsumer interface.

There are unit tests, but not integration tests yet since those would depend upon complete broker code, which is not available at this point.

Reviewers: Apoorv Mittal <[email protected]>, Manikumar Reddy <[email protected]>,  Lianet Magrans <[email protected]>
  • Loading branch information
AndrewJSchofield authored and abhi-ksolves committed Jul 31, 2024
1 parent 3790a50 commit cf3d5d6
Show file tree
Hide file tree
Showing 54 changed files with 12,932 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public class GroupRebalanceConfig {

public enum ProtocolType {
CONSUMER,
CONNECT;
CONNECT,
SHARE;

@Override
public String toString() {
Expand All @@ -50,7 +51,7 @@ public GroupRebalanceConfig(AbstractConfig config, ProtocolType protocolType) {
this.sessionTimeoutMs = config.getInt(CommonClientConfigs.SESSION_TIMEOUT_MS_CONFIG);

// Consumer and Connect use different config names for defining rebalance timeout
if (protocolType == ProtocolType.CONSUMER) {
if ((protocolType == ProtocolType.CONSUMER) || (protocolType == ProtocolType.SHARE)) {
this.rebalanceTimeoutMs = config.getInt(CommonClientConfigs.MAX_POLL_INTERVAL_MS_CONFIG);
} else {
this.rebalanceTimeoutMs = config.getInt(CommonClientConfigs.REBALANCE_TIMEOUT_MS_CONFIG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.kafka.clients.consumer.internals.ShareConsumerDelegate;
import org.apache.kafka.clients.consumer.internals.ShareConsumerDelegateCreator;
import org.apache.kafka.clients.consumer.internals.SubscriptionState;
import org.apache.kafka.clients.consumer.internals.metrics.KafkaShareConsumerMetrics;
import org.apache.kafka.common.KafkaException;
import org.apache.kafka.common.Metric;
import org.apache.kafka.common.MetricName;
Expand Down Expand Up @@ -49,11 +50,11 @@
/**
* A client that consumes records from a Kafka cluster using a share group.
* <p>
* <em>This is an early access feature introduced by KIP-932. It is not suitable for production use until it is
* fully implemented and released.</em>
* <em>This is an early access feature under development which is introduced by KIP-932.
* It is not suitable for production use until it is fully implemented and released.</em>
*
* <h3>Cross-Version Compatibility</h3>
* This client can communicate with brokers that are version 4.0.0 or newer. You will receive an
* This client can communicate with brokers that are a version that supports share groups. You will receive an
* {@link org.apache.kafka.common.errors.UnsupportedVersionException} when invoking an API that is not
* available on the running broker version.
*
Expand Down Expand Up @@ -697,4 +698,8 @@ String clientId() {
Metrics metricsRegistry() {
return delegate.metricsRegistry();
}

KafkaShareConsumerMetrics kafkaShareConsumerMetrics() {
return delegate.kafkaShareConsumerMetrics();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.kafka.clients.consumer.internals;

import org.apache.kafka.common.message.ShareAcknowledgeRequestData;
import org.apache.kafka.common.message.ShareFetchRequestData;
import org.apache.kafka.common.protocol.MessageUtil;

import java.util.ArrayList;
import java.util.List;

public class AcknowledgementBatch {
private long firstOffset;
private long lastOffset;
private List<Byte> acknowledgeTypes;

public AcknowledgementBatch() {
this.firstOffset = 0L;
this.lastOffset = 0L;
this.acknowledgeTypes = new ArrayList<>(0);
}

public long firstOffset() {
return this.firstOffset;
}

public long lastOffset() {
return this.lastOffset;
}

public List<Byte> acknowledgeTypes() {
return this.acknowledgeTypes;
}

public AcknowledgementBatch setFirstOffset(long v) {
this.firstOffset = v;
return this;
}

public AcknowledgementBatch setLastOffset(long v) {
this.lastOffset = v;
return this;
}

public AcknowledgementBatch setAcknowledgeTypes(List<Byte> v) {
this.acknowledgeTypes = v;
return this;
}

public ShareAcknowledgeRequestData.AcknowledgementBatch toShareAcknowledgeRequest() {
return new ShareAcknowledgeRequestData.AcknowledgementBatch()
.setFirstOffset(firstOffset)
.setLastOffset(lastOffset)
.setAcknowledgeTypes(acknowledgeTypes);
}

public ShareFetchRequestData.AcknowledgementBatch toShareFetchRequest() {
return new ShareFetchRequestData.AcknowledgementBatch()
.setFirstOffset(firstOffset)
.setLastOffset(lastOffset)
.setAcknowledgeTypes(acknowledgeTypes);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof AcknowledgementBatch)) return false;
AcknowledgementBatch other = (AcknowledgementBatch) obj;
if (firstOffset != other.firstOffset) return false;
if (lastOffset != other.lastOffset) return false;
if (this.acknowledgeTypes == null) {
return other.acknowledgeTypes == null;
} else {
return this.acknowledgeTypes.equals(other.acknowledgeTypes);
}
}

@Override
public int hashCode() {
int hashCode = 0;
hashCode = 31 * hashCode + ((int) (firstOffset >> 32) ^ (int) firstOffset);
hashCode = 31 * hashCode + ((int) (lastOffset >> 32) ^ (int) lastOffset);
hashCode = 31 * hashCode + (acknowledgeTypes == null ? 0 : acknowledgeTypes.hashCode());
return hashCode;
}

@Override
public String toString() {
return "AcknowledgementBatch("
+ "firstOffset=" + firstOffset
+ ", lastOffset=" + lastOffset
+ ", acknowledgeTypes=" + MessageUtil.deepToString(acknowledgeTypes.iterator())
+ ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.kafka.clients.consumer.internals;

import org.apache.kafka.clients.consumer.AcknowledgementCommitCallback;
import org.apache.kafka.common.TopicIdPartition;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class AcknowledgementCommitCallbackHandler {

private static final Logger LOG = LoggerFactory.getLogger(AcknowledgementCommitCallbackHandler.class);
private final AcknowledgementCommitCallback acknowledgementCommitCallback;
private boolean enteredCallback = false;

AcknowledgementCommitCallbackHandler(AcknowledgementCommitCallback acknowledgementCommitCallback) {
this.acknowledgementCommitCallback = acknowledgementCommitCallback;
}

public boolean hasEnteredCallback() {
return enteredCallback;
}

void onComplete(List<Map<TopicIdPartition, Acknowledgements>> acknowledgementsMapList) {
final ArrayList<Throwable> exceptions = new ArrayList<>();
acknowledgementsMapList.forEach(acknowledgementsMap -> acknowledgementsMap.forEach((partition, acknowledgements) -> {
Exception exception = null;
if (acknowledgements.getAcknowledgeErrorCode() != null) {
exception = acknowledgements.getAcknowledgeErrorCode().exception();
}
Set<Long> offsets = acknowledgements.getAcknowledgementsTypeMap().keySet();
Set<Long> offsetsCopy = Collections.unmodifiableSet(offsets);
enteredCallback = true;
try {
acknowledgementCommitCallback.onComplete(Collections.singletonMap(partition, offsetsCopy), exception);
} catch (Throwable e) {
LOG.error("Exception thrown by acknowledgement commit callback", e);
exceptions.add(e);
} finally {
enteredCallback = false;
}
}));
if (!exceptions.isEmpty()) {
throw ConsumerUtils.maybeWrapAsKafkaException(exceptions.get(0), "Exception thrown by acknowledgement commit callback");
}
}
}
Loading

0 comments on commit cf3d5d6

Please sign in to comment.