Skip to content
Merged
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() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is very common for API details to change during implementation. Once we're satisfied and ready to merge, we should update the KIP. Typically we would also send a message to the vote thread in case there are any concerns.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update the KIP and the thread once this PR is approved to reflect the changes here.

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,24 @@
/*
* 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 Admin#describeMetadataQuorum(DescribeMetadataQuorumOptions)}
*/
public class DescribeMetadataQuorumOptions extends AbstractOptions<DescribeMetadataQuorumOptions> {

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

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

private final KafkaFuture<QuorumInfo> quorumInfo;

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

/**
* Returns a future containing the QuorumInfo
*/
public KafkaFuture<QuorumInfo> quorumInfo() {
return quorumInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,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.DescribeQuorumResponseData;
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 +209,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 +261,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 +273,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.apache.kafka.common.internals.Topic.METADATA_TOPIC_NAME;
import static org.apache.kafka.common.internals.Topic.METADATA_TOPIC_PARTITION;
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 +4328,84 @@ 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(
"describeMetadataQuorum", calcDeadlineMs(now, options.timeoutMs()), provider) {

private QuorumInfo.ReplicaState translateReplicaState(DescribeQuorumResponseData.ReplicaState replica) {
return new QuorumInfo.ReplicaState(
replica.replicaId(),
replica.logEndOffset(),
replica.lastFetchTimestamp() == -1 ? OptionalLong.empty() : OptionalLong.of(replica.lastFetchTimestamp()),
replica.lastCaughtUpTimestamp() == -1 ? OptionalLong.empty() : OptionalLong.of(replica.lastCaughtUpTimestamp()));
}

private QuorumInfo createQuorumResult(final DescribeQuorumResponseData.PartitionData partition) {
return new QuorumInfo(
partition.leaderId(),
partition.currentVoters().stream().map(v -> translateReplicaState(v)).collect(Collectors.toList()),
partition.observers().stream().map(o -> translateReplicaState(o)).collect(Collectors.toList()));
}

@Override
DescribeQuorumRequest.Builder createRequest(int timeoutMs) {
Comment thread
hachikuji marked this conversation as resolved.
Outdated
return new Builder(DescribeQuorumRequest.singletonRequest(
new TopicPartition(METADATA_TOPIC_NAME, METADATA_TOPIC_PARTITION.partition())));
}

@Override
void handleResponse(AbstractResponse response) {
final DescribeQuorumResponse quorumResponse = (DescribeQuorumResponse) response;
if (quorumResponse.data().errorCode() != Errors.NONE.code()) {
throw Errors.forCode(quorumResponse.data().errorCode()).exception();
Comment thread
dajac marked this conversation as resolved.
Outdated
}
if (quorumResponse.data().topics().size() != 1) {
String msg = String.format("DescribeMetadataQuorum received %d topics when 1 was expected",
quorumResponse.data().topics().size());
log.debug(msg);
throw new UnknownServerException(msg);
}
DescribeQuorumResponseData.TopicData topic = quorumResponse.data().topics().get(0);
if (!topic.topicName().equals(METADATA_TOPIC_NAME)) {
String msg = String.format("DescribeMetadataQuorum received a topic with name %s when %s was expected",
topic.topicName(), METADATA_TOPIC_NAME);
log.debug(msg);
throw new UnknownServerException(msg);
}
if (topic.partitions().size() != 1) {
String msg = String.format("DescribeMetadataQuorum received a topic %s with %d partitions when 1 was expected",
topic.topicName(), topic.partitions().size());
log.debug(msg);
throw new UnknownServerException(msg);
}
DescribeQuorumResponseData.PartitionData partition = topic.partitions().get(0);
if (partition.partitionIndex() != METADATA_TOPIC_PARTITION.partition()) {
String msg = String.format("DescribeMetadataQuorum received a single partition with index %d when %d was expected",
partition.partitionIndex(), METADATA_TOPIC_PARTITION.partition());
log.debug(msg);
throw new UnknownServerException(msg);
}
if (partition.errorCode() != Errors.NONE.code()) {
throw Errors.forCode(partition.errorCode()).exception();
}
future.complete(createQuorumResult(partition));
}

@Override
void handleFailure(Throwable throwable) {
future.completeExceptionally(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
153 changes: 153 additions & 0 deletions clients/src/main/java/org/apache/kafka/clients/admin/QuorumInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* 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 java.util.List;
import java.util.Objects;
import java.util.OptionalLong;

/**
* This class is used to describe the state of the quorum received in DescribeQuorumResponse.
*/
public class QuorumInfo {
private final Integer leaderId;
private final List<ReplicaState> voters;
private final List<ReplicaState> observers;

QuorumInfo(Integer leaderId, List<ReplicaState> voters, List<ReplicaState> observers) {
this.leaderId = leaderId;
this.voters = voters;
this.observers = observers;
}

public Integer leaderId() {
return leaderId;
}

public List<ReplicaState> voters() {
return voters;
}

public List<ReplicaState> observers() {
return observers;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
QuorumInfo that = (QuorumInfo) o;
return leaderId.equals(that.leaderId)
&& voters.equals(that.voters)
&& observers.equals(that.observers);
}

@Override
public int hashCode() {
return Objects.hash(leaderId, voters, observers);
}

@Override
public String toString() {
return "QuorumInfo(" +
"leaderId=" + leaderId +
", voters=" + voters +
", observers=" + observers +
')';
}

public static class ReplicaState {
private final int replicaId;
private final long logEndOffset;
private final OptionalLong lastFetchTimeMs;
private final OptionalLong lastCaughtUpTimeMs;

ReplicaState() {
this(0, 0, OptionalLong.empty(), OptionalLong.empty());
}

ReplicaState(
int replicaId,
long logEndOffset,
OptionalLong lastFetchTimeMs,
OptionalLong lastCaughtUpTimeMs
) {
this.replicaId = replicaId;
this.logEndOffset = logEndOffset;
this.lastFetchTimeMs = lastFetchTimeMs;
this.lastCaughtUpTimeMs = lastCaughtUpTimeMs;
}

/**
* Return the ID for this replica.
* @return The ID for this replica
*/
public int replicaId() {
Comment thread
dajac marked this conversation as resolved.
Outdated
return replicaId;
}

/**
* Return the logEndOffset known by the leader for this replica.
* @return The logEndOffset for this replica
*/
public long logEndOffset() {
return logEndOffset;
}

/**
* Return the lastFetchTime in milliseconds for this replica.
* @return The value of the lastFetchTime if known, empty otherwise
*/
public OptionalLong lastFetchTimeMs() {
return lastFetchTimeMs;
}

/**
* Return the lastCaughtUpTime in milliseconds for this replica.
* @return The value of the lastCaughtUpTime if known, empty otherwise
*/
public OptionalLong lastCaughtUpTimeMs() {
return lastCaughtUpTimeMs;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ReplicaState that = (ReplicaState) o;
return replicaId == that.replicaId
&& logEndOffset == that.logEndOffset
&& lastFetchTimeMs.equals(that.lastFetchTimeMs)
&& lastCaughtUpTimeMs.equals(that.lastCaughtUpTimeMs);
}

@Override
public int hashCode() {
return Objects.hash(replicaId, logEndOffset, lastFetchTimeMs, lastCaughtUpTimeMs);
}

@Override
public String toString() {
return "ReplicaState(" +
"replicaId=" + replicaId +
", logEndOffset=" + logEndOffset +
", lastFetchTimeMs=" + lastFetchTimeMs +
", lastCaughtUpTimeMs=" + lastCaughtUpTimeMs +
')';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.kafka.common.internals;

import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.errors.InvalidTopicException;
import org.apache.kafka.common.utils.Utils;

Expand All @@ -27,6 +28,8 @@ 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 TopicPartition METADATA_TOPIC_PARTITION = new TopicPartition(METADATA_TOPIC_NAME, 0);
public static final String LEGAL_CHARS = "[a-zA-Z0-9._-]";

private static final Set<String> INTERNAL_TOPICS = Collections.unmodifiableSet(
Expand Down
Loading