|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.kafka.clients.admin; |
| 18 | + |
| 19 | +import org.apache.kafka.common.KafkaFuture; |
| 20 | +import org.apache.kafka.common.TopicPartition; |
| 21 | +import org.apache.kafka.common.annotation.InterfaceStability; |
| 22 | +import org.apache.kafka.common.errors.ApiException; |
| 23 | +import org.apache.kafka.common.internals.KafkaFutureImpl; |
| 24 | + |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Set; |
| 27 | + |
| 28 | +/** |
| 29 | + * The result of the {@link Admin#deleteShareGroupOffsets(String, Set, DeleteShareGroupOffsetsOptions)} call. |
| 30 | + * <p> |
| 31 | + * The API of this class is evolving, see {@link Admin} for details. |
| 32 | + */ |
| 33 | +@InterfaceStability.Evolving |
| 34 | +public class DeleteShareGroupOffsetsResult { |
| 35 | + |
| 36 | + private final KafkaFuture<Map<TopicPartition, ApiException>> future; |
| 37 | + private final Set<TopicPartition> partitions; |
| 38 | + |
| 39 | + DeleteShareGroupOffsetsResult(KafkaFuture<Map<TopicPartition, ApiException>> future, Set<TopicPartition> partitions) { |
| 40 | + this.future = future; |
| 41 | + this.partitions = partitions; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Return a future which succeeds only if all the deletions succeed. |
| 46 | + * If not, the first partition error shall be returned. |
| 47 | + */ |
| 48 | + public KafkaFuture<Void> all() { |
| 49 | + final KafkaFutureImpl<Void> result = new KafkaFutureImpl<>(); |
| 50 | + |
| 51 | + this.future.whenComplete((topicPartitions, throwable) -> { |
| 52 | + if (throwable != null) { |
| 53 | + result.completeExceptionally(throwable); |
| 54 | + } else { |
| 55 | + for (TopicPartition partition : partitions) { |
| 56 | + if (maybeCompleteExceptionally(topicPartitions, partition, result)) { |
| 57 | + return; |
| 58 | + } |
| 59 | + } |
| 60 | + result.complete(null); |
| 61 | + } |
| 62 | + }); |
| 63 | + return result; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Return a future which can be used to check the result for a given partition. |
| 68 | + */ |
| 69 | + public KafkaFuture<Void> partitionResult(final TopicPartition partition) { |
| 70 | + if (!partitions.contains(partition)) { |
| 71 | + throw new IllegalArgumentException("Partition " + partition + " was not included in the original request"); |
| 72 | + } |
| 73 | + final KafkaFutureImpl<Void> result = new KafkaFutureImpl<>(); |
| 74 | + |
| 75 | + this.future.whenComplete((topicPartitions, throwable) -> { |
| 76 | + if (throwable != null) { |
| 77 | + result.completeExceptionally(throwable); |
| 78 | + } else if (!maybeCompleteExceptionally(topicPartitions, partition, result)) { |
| 79 | + result.complete(null); |
| 80 | + } |
| 81 | + }); |
| 82 | + return result; |
| 83 | + } |
| 84 | + |
| 85 | + private boolean maybeCompleteExceptionally(Map<TopicPartition, ApiException> partitionLevelErrors, |
| 86 | + TopicPartition partition, |
| 87 | + KafkaFutureImpl<Void> result) { |
| 88 | + Throwable exception; |
| 89 | + if (!partitionLevelErrors.containsKey(partition)) { |
| 90 | + exception = new IllegalArgumentException("Offset deletion result for partition \"" + partition + "\" was not included in the response"); |
| 91 | + } else { |
| 92 | + exception = partitionLevelErrors.get(partition); |
| 93 | + } |
| 94 | + |
| 95 | + if (exception != null) { |
| 96 | + result.completeExceptionally(exception); |
| 97 | + return true; |
| 98 | + } else { |
| 99 | + return false; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments