-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-12709; Add Admin API for ListTransactions
#10616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
clients/src/main/java/org/apache/kafka/clients/admin/ListTransactionsOptions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * 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.annotation.InterfaceStability; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Options for {@link Admin#listTransactions()}. | ||
| * | ||
| * The API of this class is evolving, see {@link Admin} for details. | ||
| */ | ||
| @InterfaceStability.Evolving | ||
| public class ListTransactionsOptions extends AbstractOptions<ListTransactionsOptions> { | ||
| private Set<TransactionState> filteredStates = Collections.emptySet(); | ||
| private Set<Long> filteredProducerIds = Collections.emptySet(); | ||
|
|
||
| /** | ||
| * Filter only the transactions that are in a specific set of states. If no filter | ||
| * is specified or if the passed set of states is empty, then transactions in all | ||
| * states will be returned. | ||
| * | ||
| * @param states the set of states to filter by | ||
| * @return this object | ||
| */ | ||
| public ListTransactionsOptions filterStates(Collection<TransactionState> states) { | ||
| this.filteredStates = new HashSet<>(states); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Filter only the transactions from producers in a specific set of producerIds. | ||
| * If no filter is specified or if the passed collection of producerIds is empty, | ||
| * then the transactions of all producerIds will be returned. | ||
| * | ||
| * @param producerIdFilters the set of producerIds to filter by | ||
| * @return this object | ||
| */ | ||
| public ListTransactionsOptions filterProducerIds(Collection<Long> producerIdFilters) { | ||
| this.filteredProducerIds = new HashSet<>(producerIdFilters); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the set of states to be filtered or empty if no states have been specified. | ||
| * | ||
| * @return the current set of filtered states (empty means that no states are filtered and all | ||
| * all transactions will be returned) | ||
| */ | ||
| public Set<TransactionState> filteredStates() { | ||
| return filteredStates; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the set of producerIds that are being filtered or empty if none have been specified. | ||
| * | ||
| * @return the current set of filtered states (empty means that no producerIds are filtered and | ||
| * all transactions will be returned) | ||
| */ | ||
| public Set<Long> filteredProducerIds() { | ||
| return filteredProducerIds; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "ListTransactionsOptions(" + | ||
| "filteredStates=" + filteredStates + | ||
| ", filteredProducerIds=" + filteredProducerIds + | ||
| ", timeoutMs=" + timeoutMs + | ||
| ')'; | ||
| } | ||
| } | ||
125 changes: 125 additions & 0 deletions
125
clients/src/main/java/org/apache/kafka/clients/admin/ListTransactionsResult.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* | ||
| * 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.annotation.InterfaceStability; | ||
| import org.apache.kafka.common.internals.KafkaFutureImpl; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * The result of the {@link Admin#listTransactions()} call. | ||
| * <p> | ||
| * The API of this class is evolving, see {@link Admin} for details. | ||
| */ | ||
| @InterfaceStability.Evolving | ||
| public class ListTransactionsResult { | ||
| private final KafkaFutureImpl<Map<Integer, KafkaFutureImpl<Collection<TransactionListing>>>> future; | ||
|
|
||
| ListTransactionsResult(KafkaFutureImpl<Map<Integer, KafkaFutureImpl<Collection<TransactionListing>>>> future) { | ||
| this.future = future; | ||
| } | ||
|
|
||
| /** | ||
| * Get all transaction listings. If any of the underlying requests fail, then the future | ||
| * returned from this method will also fail with the first encountered error. | ||
| * | ||
| * @return A future containing the collection of transaction listings. The future completes | ||
| * when all transaction listings are available and fails after any non-retriable error. | ||
| */ | ||
| public KafkaFuture<Collection<TransactionListing>> all() { | ||
| return allByBrokerId().thenApply(map -> { | ||
| List<TransactionListing> allListings = new ArrayList<>(); | ||
| for (Collection<TransactionListing> listings : map.values()) { | ||
| allListings.addAll(listings); | ||
| } | ||
| return allListings; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Get a future which returns a map containing the underlying listing future for each broker | ||
| * in the cluster. This is useful, for example, if a partial listing of transactions is | ||
| * sufficient, or if you want more granular error details. | ||
| * | ||
| * @return A future containing a map of futures by broker which complete individually when | ||
| * their respective transaction listings are available. The top-level future returned | ||
| * from this method may fail if the admin client is unable to lookup the available | ||
| * brokers in the cluster. | ||
| */ | ||
| public KafkaFuture<Map<Integer, KafkaFuture<Collection<TransactionListing>>>> byBrokerId() { | ||
| KafkaFutureImpl<Map<Integer, KafkaFuture<Collection<TransactionListing>>>> result = new KafkaFutureImpl<>(); | ||
| future.whenComplete((brokerFutures, exception) -> { | ||
| if (brokerFutures != null) { | ||
| Map<Integer, KafkaFuture<Collection<TransactionListing>>> brokerFuturesCopy = | ||
| new HashMap<>(brokerFutures.size()); | ||
| brokerFuturesCopy.putAll(brokerFutures); | ||
| result.complete(brokerFuturesCopy); | ||
| } else { | ||
| result.completeExceptionally(exception); | ||
| } | ||
| }); | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Get all transaction listings in a map which is keyed by the ID of respective broker | ||
| * that is currently managing them. If any of the underlying requests fail, then the future | ||
| * returned from this method will also fail with the first encountered error. | ||
| * | ||
| * @return A future containing a map from the broker ID to the transactions hosted by that | ||
| * broker respectively. This future completes when all transaction listings are | ||
| * available and fails after any non-retriable error. | ||
| */ | ||
| public KafkaFuture<Map<Integer, Collection<TransactionListing>>> allByBrokerId() { | ||
| KafkaFutureImpl<Map<Integer, Collection<TransactionListing>>> allFuture = new KafkaFutureImpl<>(); | ||
| Map<Integer, Collection<TransactionListing>> allListingsMap = new HashMap<>(); | ||
|
|
||
| future.whenComplete((map, topLevelException) -> { | ||
| if (topLevelException != null) { | ||
| allFuture.completeExceptionally(topLevelException); | ||
| return; | ||
| } | ||
|
|
||
| Set<Integer> remainingResponses = new HashSet<>(map.keySet()); | ||
| map.forEach((brokerId, future) -> { | ||
| future.whenComplete((listings, brokerException) -> { | ||
| if (brokerException != null) { | ||
| allFuture.completeExceptionally(brokerException); | ||
| } else if (!allFuture.isDone()) { | ||
| allListingsMap.put(brokerId, listings); | ||
| remainingResponses.remove(brokerId); | ||
|
|
||
| if (remainingResponses.isEmpty()) { | ||
| allFuture.complete(allListingsMap); | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| return allFuture; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.