Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions clients/src/main/java/org/apache/kafka/clients/admin/Admin.java
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,35 @@ default DescribeFeaturesResult describeFeatures() {
*/
UpdateFeaturesResult updateFeatures(Map<String, FeatureUpdate> featureUpdates, UpdateFeaturesOptions options);

/**
* Describes the state of the metadata quorum.
* <p>
* This is a convenience method for {@link #describeMetadataQuorum(DescribeMetadataQuorumOptions)} with default options.
* See the overload for more details.
*
* @return the {@link DescribeMetadataQuorumResult} containing the result
*/
default DescribeMetadataQuorumResult describeMetadataQuorum() {
return describeMetadataQuorum(new DescribeMetadataQuorumOptions());
}

/**
* Describes the state of the metadata quorum.
* <p>
* The following exceptions can be anticipated when calling {@code get()} on the futures obtained from
* the returned {@code DescribeMetadataQuorumResult}:
* <ul>
* <li>{@link org.apache.kafka.common.errors.ClusterAuthorizationException}
* If the authenticated user didn't have {@code DESCRIBE} access to the cluster.</li>
* <li>{@link org.apache.kafka.common.errors.TimeoutException}
* If the request timed out before the controller could list the cluster links.</li>
* </ul>
*
* @param options The {@link DescribeMetadataQuorumOptions} to use when describing the quorum.
* @return the {@link DescribeMetadataQuorumResult} containing the result
*/
DescribeMetadataQuorumResult describeMetadataQuorum(DescribeMetadataQuorumOptions options);

/**
* Unregister a broker.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.admin;

/**
* Options for {@link ConfluentAdmin#describeQuorum(DescribeMetadataQuorumOptions)}.
*
*/
public class DescribeMetadataQuorumOptions extends AbstractOptions<DescribeMetadataQuorumOptions> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.admin;

import org.apache.kafka.common.KafkaFuture;
import org.apache.kafka.common.QuorumInfo;

/**
* The result of {@link Admin#describeMetadataQuorum(DescribeMetadataQuorumOptions)}
*
*/
public class DescribeMetadataQuorumResult {

private final KafkaFuture<QuorumInfo> quorumInfo;

public DescribeMetadataQuorumResult(KafkaFuture<QuorumInfo> quorumInfo) {
this.quorumInfo = quorumInfo;
}

/**
* Returns a future QuorumInfo
*/
public KafkaFuture<QuorumInfo> quorumInfo() {
return quorumInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
import org.apache.kafka.common.MetricName;
import org.apache.kafka.common.Node;
import org.apache.kafka.common.PartitionInfo;
import org.apache.kafka.common.QuorumInfo;
import org.apache.kafka.common.QuorumInfo.ReplicaState;
import org.apache.kafka.common.TopicCollection;
import org.apache.kafka.common.TopicCollection.TopicIdCollection;
import org.apache.kafka.common.TopicCollection.TopicNameCollection;
Expand Down Expand Up @@ -135,6 +137,7 @@
import org.apache.kafka.common.message.DescribeLogDirsRequestData;
import org.apache.kafka.common.message.DescribeLogDirsRequestData.DescribableLogDirTopic;
import org.apache.kafka.common.message.DescribeLogDirsResponseData;
import org.apache.kafka.common.message.DescribeQuorumRequestData;
import org.apache.kafka.common.message.DescribeUserScramCredentialsRequestData;
import org.apache.kafka.common.message.DescribeUserScramCredentialsRequestData.UserName;
import org.apache.kafka.common.message.DescribeUserScramCredentialsResponseData;
Expand Down Expand Up @@ -208,6 +211,9 @@
import org.apache.kafka.common.requests.DescribeLogDirsResponse;
import org.apache.kafka.common.requests.DescribeUserScramCredentialsRequest;
import org.apache.kafka.common.requests.DescribeUserScramCredentialsResponse;
import org.apache.kafka.common.requests.DescribeQuorumRequest;
import org.apache.kafka.common.requests.DescribeQuorumRequest.Builder;
import org.apache.kafka.common.requests.DescribeQuorumResponse;
import org.apache.kafka.common.requests.ElectLeadersRequest;
import org.apache.kafka.common.requests.ElectLeadersResponse;
import org.apache.kafka.common.requests.ExpireDelegationTokenRequest;
Expand Down Expand Up @@ -257,6 +263,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalLong;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
Expand All @@ -268,6 +275,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Collections.singletonList;
import static org.apache.kafka.common.internals.Topic.METADATA_TOPIC_NAME;
import static org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignablePartition;
import static org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignablePartitionResponse;
import static org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignableTopicResponse;
Expand Down Expand Up @@ -4321,6 +4330,61 @@ void handleFailure(Throwable throwable) {
return new UpdateFeaturesResult(new HashMap<>(updateFutures));
}

@Override
public DescribeMetadataQuorumResult describeMetadataQuorum(DescribeMetadataQuorumOptions options) {
NodeProvider provider = new LeastLoadedNodeProvider();

final KafkaFutureImpl<QuorumInfo> future = new KafkaFutureImpl<>();
final long now = time.milliseconds();
final Call call = new Call(
"describeQuorum", calcDeadlineMs(now, options.timeoutMs()), provider) {

private QuorumInfo createQuorumResult(final DescribeQuorumResponse response) {
Integer partition = 0;
String topicName = response.getTopicNameByIndex(partition);
Integer leaderId = response.getPartitionLeaderId(topicName, partition);
List<ReplicaState> voters = new ArrayList<>();
for (Map.Entry<Integer, Long> entry: response.getVoterOffsets(topicName, partition).entrySet()) {
voters.add(new ReplicaState(entry.getKey(), entry.getValue(), OptionalLong.empty(), OptionalLong.empty()));
}
List<ReplicaState> observers = new ArrayList<>();
for (Map.Entry<Integer, Long> entry: response.getObserverOffsets(topicName, partition).entrySet()) {
observers.add(new ReplicaState(entry.getKey(), entry.getValue(), OptionalLong.empty(), OptionalLong.empty()));
}
QuorumInfo info = new QuorumInfo(topicName, leaderId, voters, observers);
return info;
}

@Override
DescribeQuorumRequest.Builder createRequest(int timeoutMs) {
DescribeQuorumRequestData data = new DescribeQuorumRequestData()
.setTopics(singletonList(new DescribeQuorumRequestData.TopicData()
.setPartitions(singletonList(new DescribeQuorumRequestData.PartitionData()
.setPartitionIndex(0)))
.setTopicName(METADATA_TOPIC_NAME)));
return new Builder(data);
}

@Override
void handleResponse(AbstractResponse response) {
final DescribeQuorumResponse quorumResponse = (DescribeQuorumResponse) response;
if (quorumResponse.data().errorCode() == Errors.NONE.code()) {
future.complete(createQuorumResult(quorumResponse));
} else {
future.completeExceptionally(Errors.forCode(quorumResponse.data().errorCode()).exception());
}
}

@Override
void handleFailure(Throwable throwable) {
completeAllExceptionally(Collections.singletonList(future), throwable);
}
};

runnable.call(call, now);
return new DescribeMetadataQuorumResult(future);
}

@Override
public UnregisterBrokerResult unregisterBroker(int brokerId, UnregisterBrokerOptions options) {
final KafkaFutureImpl<Void> future = new KafkaFutureImpl<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class Topic {

public static final String GROUP_METADATA_TOPIC_NAME = "__consumer_offsets";
public static final String TRANSACTION_STATE_TOPIC_NAME = "__transaction_state";
public static final String METADATA_TOPIC_NAME = "__cluster_metadata";
public static final String LEGAL_CHARS = "[a-zA-Z0-9._-]";

private static final Set<String> INTERNAL_TOPICS = Collections.unmodifiableSet(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.message.DescribeQuorumResponseData;
import org.apache.kafka.common.message.DescribeQuorumResponseData.ReplicaState;
import org.apache.kafka.common.message.DescribeQuorumResponseData.TopicData;
import org.apache.kafka.common.protocol.ApiKeys;
import org.apache.kafka.common.protocol.ByteBufferAccessor;
import org.apache.kafka.common.protocol.Errors;
Expand Down Expand Up @@ -93,4 +94,51 @@ public static DescribeQuorumResponseData singletonResponse(TopicPartition topicP
public static DescribeQuorumResponse parse(ByteBuffer buffer, short version) {
return new DescribeQuorumResponse(new DescribeQuorumResponseData(new ByteBufferAccessor(buffer), version));
}

public String getTopicNameByIndex(Integer index) {
return data.topics().get(index).topicName();
}

public Integer getPartitionLeaderId(String topicName, Integer partition) {
Integer leaderId = -1;
TopicData topic = data.topics().stream()
.filter(t -> t.topicName().equals(topicName))
.findFirst()
.orElse(null);
if (topic != null) {
leaderId = Integer.valueOf(topic.partitions().get(partition).leaderId());
}
return leaderId;
}

public Map<Integer, Long> getVoterOffsets(String topicName, Integer partition) {
Map<Integer, Long> voterOffsets = new HashMap<>();
TopicData topic = data.topics().stream()
.filter(t -> t.topicName().equals(topicName))
.findFirst()
.orElse(null);
if (topic != null) {
topic.partitions().get(partition).currentVoters().forEach(
v -> {
voterOffsets.put(v.replicaId(), v.logEndOffset());
}
);
}
return voterOffsets;
}

public Map<Integer, Long> getObserverOffsets(String topicName, Integer partition) {
Map<Integer, Long> observerOffsets = new HashMap<>();
TopicData topic = data.topics().stream()
.filter(t -> t.topicName().equals(topicName))
.findFirst()
.orElse(null);
topic.partitions().get(partition).observers().forEach(
o -> {
observerOffsets.put(o.replicaId(), o.logEndOffset());
}
);
return observerOffsets;
}

}
Loading