[GitHub] [kafka] dengziming commented on a diff in pull request #13432: KAFKA-14821 Implement the listOffsets API with AdminApiDriver
dengziming commented on code in PR #13432: URL: https://github.com/apache/kafka/pull/13432#discussion_r1159516278 ## clients/src/main/java/org/apache/kafka/clients/admin/internals/ListOffsetsHandler.java: ## @@ -0,0 +1,229 @@ +/* + * 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.internals; + +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.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.kafka.clients.admin.ListOffsetsOptions; +import org.apache.kafka.clients.admin.ListOffsetsResult.ListOffsetsResultInfo; +import org.apache.kafka.clients.admin.OffsetSpec; +import org.apache.kafka.clients.admin.OffsetSpec.TimestampSpec; +import org.apache.kafka.clients.admin.internals.AdminApiFuture.SimpleAdminApiFuture; +import org.apache.kafka.clients.admin.internals.AdminApiHandler.Batched; +import org.apache.kafka.common.Node; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.errors.ApiException; +import org.apache.kafka.common.errors.InvalidMetadataException; +import org.apache.kafka.common.errors.RetriableException; +import org.apache.kafka.common.errors.UnsupportedVersionException; +import org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsPartition; +import org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic; +import org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse; +import org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsTopicResponse; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.requests.AbstractResponse; +import org.apache.kafka.common.requests.ListOffsetsRequest; +import org.apache.kafka.common.requests.ListOffsetsResponse; +import org.apache.kafka.common.utils.CollectionUtils; +import org.apache.kafka.common.utils.LogContext; +import org.slf4j.Logger; + +public final class ListOffsetsHandler extends Batched { + +private final Map offsetSpecsByPartition; +private final ListOffsetsOptions options; +private final Logger log; +private final AdminApiLookupStrategy lookupStrategy; + +public ListOffsetsHandler( +Map offsetSpecsByPartition, +ListOffsetsOptions options, +LogContext logContext +) { +this.offsetSpecsByPartition = offsetSpecsByPartition; +this.options = options; +this.log = logContext.logger(ListOffsetsHandler.class); +this.lookupStrategy = new PartitionLeaderStrategy(logContext); +} + +@Override +public String apiName() { +return "listOffsets"; +} + +@Override +public AdminApiLookupStrategy lookupStrategy() { +return this.lookupStrategy; +} + +@Override +ListOffsetsRequest.Builder buildBatchedRequest(int brokerId, Set keys) { +Map topicsByName = CollectionUtils.groupPartitionsByTopic( +keys, +topicName -> new ListOffsetsTopic().setName(topicName), +(listOffsetsTopic, partitionId) -> { +TopicPartition topicPartition = new TopicPartition(listOffsetsTopic.name(), partitionId); +OffsetSpec offsetSpec = offsetSpecsByPartition.get(topicPartition); +long offsetQuery = getOffsetFromSpec(offsetSpec); +listOffsetsTopic.partitions().add( +new ListOffsetsPartition() +.setPartitionIndex(partitionId) +.setTimestamp(offsetQuery)); +}); +boolean supportsMaxTimestamp = keys Review Comment: I think we should make `supportsMaxTimestamp` a class field and init it in construct method, then in `handleUnsupportedVersion` we will change it to false. This is what we did in the past in `KafkaAdminClient.getListOffsetsCalls`, right? https://github.com/apache/kafka/blob/9b409de1e2719f72e3c17663ce131e40dc459361/clients/src/main/java/org/apache/kafka/clients/admin/KafkaAdminClient.java#L3922-L3944 ## clients/src/main/java/org/apache/kafka/clients/admin/intern
[GitHub] [kafka] dengziming commented on a diff in pull request #13432: KAFKA-14821 Implement the listOffsets API with AdminApiDriver
dengziming commented on code in PR #13432: URL: https://github.com/apache/kafka/pull/13432#discussion_r1159538238 ## clients/src/main/java/org/apache/kafka/clients/admin/internals/ListOffsetsHandler.java: ## @@ -0,0 +1,229 @@ +/* + * 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.internals; + +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.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.kafka.clients.admin.ListOffsetsOptions; +import org.apache.kafka.clients.admin.ListOffsetsResult.ListOffsetsResultInfo; +import org.apache.kafka.clients.admin.OffsetSpec; +import org.apache.kafka.clients.admin.OffsetSpec.TimestampSpec; +import org.apache.kafka.clients.admin.internals.AdminApiFuture.SimpleAdminApiFuture; +import org.apache.kafka.clients.admin.internals.AdminApiHandler.Batched; +import org.apache.kafka.common.Node; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.errors.ApiException; +import org.apache.kafka.common.errors.InvalidMetadataException; +import org.apache.kafka.common.errors.RetriableException; +import org.apache.kafka.common.errors.UnsupportedVersionException; +import org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsPartition; +import org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic; +import org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse; +import org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsTopicResponse; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.requests.AbstractResponse; +import org.apache.kafka.common.requests.ListOffsetsRequest; +import org.apache.kafka.common.requests.ListOffsetsResponse; +import org.apache.kafka.common.utils.CollectionUtils; +import org.apache.kafka.common.utils.LogContext; +import org.slf4j.Logger; + +public final class ListOffsetsHandler extends Batched { + +private final Map offsetSpecsByPartition; +private final ListOffsetsOptions options; +private final Logger log; +private final AdminApiLookupStrategy lookupStrategy; + +public ListOffsetsHandler( +Map offsetSpecsByPartition, +ListOffsetsOptions options, +LogContext logContext +) { +this.offsetSpecsByPartition = offsetSpecsByPartition; +this.options = options; +this.log = logContext.logger(ListOffsetsHandler.class); +this.lookupStrategy = new PartitionLeaderStrategy(logContext); +} + +@Override +public String apiName() { +return "listOffsets"; +} + +@Override +public AdminApiLookupStrategy lookupStrategy() { +return this.lookupStrategy; +} + +@Override +ListOffsetsRequest.Builder buildBatchedRequest(int brokerId, Set keys) { +Map topicsByName = CollectionUtils.groupPartitionsByTopic( +keys, +topicName -> new ListOffsetsTopic().setName(topicName), +(listOffsetsTopic, partitionId) -> { +TopicPartition topicPartition = new TopicPartition(listOffsetsTopic.name(), partitionId); +OffsetSpec offsetSpec = offsetSpecsByPartition.get(topicPartition); +long offsetQuery = getOffsetFromSpec(offsetSpec); +listOffsetsTopic.partitions().add( +new ListOffsetsPartition() +.setPartitionIndex(partitionId) +.setTimestamp(offsetQuery)); +}); +boolean supportsMaxTimestamp = keys +.stream() +.anyMatch(key -> getOffsetFromSpec(offsetSpecsByPartition.get(key)) == ListOffsetsRequest.MAX_TIMESTAMP); + +return ListOffsetsRequest.Builder +.forConsumer(true, options.isolationLevel(), supportsMaxTimestamp) +.setTargetTimes(new ArrayList<>(topicsByName.values())); +} + +@Override +public ApiResult handleResponse( +Node broker, +Set keys, +AbstractResponse abstractResponse +) { +Li
[GitHub] [kafka] dengziming commented on a diff in pull request #13432: KAFKA-14821 Implement the listOffsets API with AdminApiDriver
dengziming commented on code in PR #13432: URL: https://github.com/apache/kafka/pull/13432#discussion_r1167684205 ## clients/src/main/java/org/apache/kafka/clients/admin/KafkaAdminClient.java: ## @@ -4579,7 +4374,7 @@ void maybeRetry(long currentTimeMs, Throwable throwable) { }; } -private long getOffsetFromOffsetSpec(OffsetSpec offsetSpec) { +private static long getOffsetFromSpec(OffsetSpec offsetSpec) { Review Comment: nit: this change seems unnecessary. ## clients/src/main/java/org/apache/kafka/clients/admin/internals/AdminApiDriver.java: ## @@ -260,12 +261,29 @@ public void onFailure( .filter(future.lookupKeys()::contains) .collect(Collectors.toSet()); retryLookup(keysToUnmap); +} else if (t instanceof UnsupportedVersionException) { Review Comment: We should add 2 test cases in AdminApiDriverTest to test this: 1. testFulfillmentUnsupportedVersion 2. testFulfillmentRetribleUnsupportedVersion The first is to verify unsupportedVersionException will return in failed, the code like this: ``` ctx.poll(emptyMap(), map(mkSet("foo"), failed("foo", new UnsupportedVersionException("unsupported api"; ctx.poll(emptyMap(), emptyMap()); ``` The second is more complex, you should implement `handleUnsupportedVersion` in `MockAdminApiHandler` to support retry on unsupported, the code may like this: ``` ctx.handler.addRetriableUnsupportedVersionKey("foo"); ctx.handler.expectRequest(mkSet("foo"), failed("foo", new UnsupportedVersionException("unsupported api"))); ctx.handler.expectRequest(mkSet("bar"), failed("foo", new UnsupportedVersionException("unsupported api"))); // verify retry on foo and not retry on bar. List> requestSpecs = ctx.driver.poll(); requestSpecs.forEach(requestSpec -> { ctx.driver.onFailure(ctx.time.milliseconds(), requestSpec, new UnsupportedVersionException("unsupported api")); }); ctx.poll(emptyMap(), map( mkSet("foo"), failed("foo", new UnsupportedVersionException("unsupported api")) )); ``` ## clients/src/main/java/org/apache/kafka/clients/admin/internals/ListOffsetsHandler.java: ## @@ -0,0 +1,229 @@ +/* + * 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.internals; + +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.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.kafka.clients.admin.ListOffsetsOptions; +import org.apache.kafka.clients.admin.ListOffsetsResult.ListOffsetsResultInfo; +import org.apache.kafka.clients.admin.OffsetSpec; +import org.apache.kafka.clients.admin.OffsetSpec.TimestampSpec; +import org.apache.kafka.clients.admin.internals.AdminApiFuture.SimpleAdminApiFuture; +import org.apache.kafka.clients.admin.internals.AdminApiHandler.Batched; +import org.apache.kafka.common.Node; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.errors.ApiException; +import org.apache.kafka.common.errors.InvalidMetadataException; +import org.apache.kafka.common.errors.RetriableException; +import org.apache.kafka.common.errors.UnsupportedVersionException; +import org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsPartition; +import org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic; +import org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse; +import org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsTopicResponse; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.requests.AbstractResponse; +import org.apache.kafka.common.requests.ListOffsetsRequest; +import org.apache.kafka.common.requests.ListOffsetsResponse; +import org.apache.kafka.common.utils.CollectionUtils; +import org.apache.kafka.common.utils.LogContext; +import org.slf4j.Logger; + +public final class ListOffsetsHandler extends Batched { + +pr
[GitHub] [kafka] dengziming commented on a diff in pull request #13432: KAFKA-14821 Implement the listOffsets API with AdminApiDriver
dengziming commented on code in PR #13432: URL: https://github.com/apache/kafka/pull/13432#discussion_r1167773347 ## clients/src/main/java/org/apache/kafka/clients/admin/internals/AdminApiDriver.java: ## @@ -260,12 +261,29 @@ public void onFailure( .filter(future.lookupKeys()::contains) .collect(Collectors.toSet()); retryLookup(keysToUnmap); +} else if (t instanceof UnsupportedVersionException) { Review Comment: We should add 2 test cases in AdminApiDriverTest to test this: 1. testFulfillmentUnsupportedVersion 2. testFulfillmentRetribleUnsupportedVersion The first is to verify unsupportedVersionException will return in failed, the code like this: ``` ctx.poll(emptyMap(), map(mkSet("foo"), failed("foo", new UnsupportedVersionException("unsupported api"; ctx.poll(emptyMap(), emptyMap()); ``` The second is more complex, you should implement `handleUnsupportedVersion` in `MockAdminApiHandler` to support retry on unsupported, the code may like this: ``` ctx.handler.addRetriableUnsupportedVersionKey("foo"); ctx.handler.expectRequest(mkSet("foo"), failed("foo", new UnsupportedVersionException("unsupported api"))); ctx.handler.expectRequest(mkSet("bar"), failed("foo", new UnsupportedVersionException("unsupported api"))); // verify retry on foo and not retry on bar. List> requestSpecs = ctx.driver.poll(); requestSpecs.forEach(requestSpec -> { ctx.driver.onFailure(ctx.time.milliseconds(), requestSpec, new UnsupportedVersionException("unsupported api")); }); ctx.poll(emptyMap(), map( mkSet("foo"), failed("foo", new UnsupportedVersionException("unsupported api")) )); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [kafka] dengziming commented on a diff in pull request #13432: KAFKA-14821 Implement the listOffsets API with AdminApiDriver
dengziming commented on code in PR #13432: URL: https://github.com/apache/kafka/pull/13432#discussion_r1170758199 ## clients/src/main/java/org/apache/kafka/clients/admin/internals/ListOffsetsHandler.java: ## @@ -0,0 +1,209 @@ +/* + * 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.internals; + +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.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.kafka.clients.admin.ListOffsetsOptions; +import org.apache.kafka.clients.admin.ListOffsetsResult.ListOffsetsResultInfo; +import org.apache.kafka.clients.admin.internals.AdminApiFuture.SimpleAdminApiFuture; +import org.apache.kafka.clients.admin.internals.AdminApiHandler.Batched; +import org.apache.kafka.common.Node; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.errors.ApiException; +import org.apache.kafka.common.errors.InvalidMetadataException; +import org.apache.kafka.common.errors.RetriableException; +import org.apache.kafka.common.errors.UnsupportedVersionException; +import org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsPartition; +import org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic; +import org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse; +import org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsTopicResponse; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.requests.AbstractResponse; +import org.apache.kafka.common.requests.ListOffsetsRequest; +import org.apache.kafka.common.requests.ListOffsetsResponse; +import org.apache.kafka.common.utils.CollectionUtils; +import org.apache.kafka.common.utils.LogContext; +import org.slf4j.Logger; + +public final class ListOffsetsHandler extends Batched { + +private final Map offsetTimestampsByPartition; +private final ListOffsetsOptions options; +private final Logger log; +private final AdminApiLookupStrategy lookupStrategy; + +public ListOffsetsHandler( +Map offsetTimestampsByPartition, +ListOffsetsOptions options, +LogContext logContext +) { +this.offsetTimestampsByPartition = offsetTimestampsByPartition; +this.options = options; +this.log = logContext.logger(ListOffsetsHandler.class); +this.lookupStrategy = new PartitionLeaderStrategy(logContext); +} + +@Override +public String apiName() { +return "listOffsets"; +} + +@Override +public AdminApiLookupStrategy lookupStrategy() { +return this.lookupStrategy; +} + +@Override +ListOffsetsRequest.Builder buildBatchedRequest(int brokerId, Set keys) { +Map topicsByName = CollectionUtils.groupPartitionsByTopic( +keys, +topicName -> new ListOffsetsTopic().setName(topicName), +(listOffsetsTopic, partitionId) -> { +TopicPartition topicPartition = new TopicPartition(listOffsetsTopic.name(), partitionId); +long offsetTimestamp = offsetTimestampsByPartition.get(topicPartition); +listOffsetsTopic.partitions().add( +new ListOffsetsPartition() +.setPartitionIndex(partitionId) +.setTimestamp(offsetTimestamp)); +}); +boolean supportsMaxTimestamp = keys +.stream() +.anyMatch(key -> offsetTimestampsByPartition.get(key) == ListOffsetsRequest.MAX_TIMESTAMP); + +return ListOffsetsRequest.Builder +.forConsumer(true, options.isolationLevel(), supportsMaxTimestamp) +.setTargetTimes(new ArrayList<>(topicsByName.values())); +} + +@Override +public ApiResult handleResponse( +Node broker, +Set keys, +AbstractResponse abstractResponse +) { +ListOffsetsResponse response = (ListOffsetsResponse) abstractResponse; +Map completed = new HashMap<>(); +Map failed = new HashMap<>(); +List unmapp
[GitHub] [kafka] dengziming commented on a diff in pull request #13432: KAFKA-14821 Implement the listOffsets API with AdminApiDriver
dengziming commented on code in PR #13432: URL: https://github.com/apache/kafka/pull/13432#discussion_r1171232959 ## clients/src/main/java/org/apache/kafka/clients/admin/internals/ListOffsetsHandler.java: ## @@ -0,0 +1,212 @@ +/* + * 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.internals; + +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.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.kafka.clients.admin.ListOffsetsOptions; +import org.apache.kafka.clients.admin.ListOffsetsResult.ListOffsetsResultInfo; +import org.apache.kafka.clients.admin.internals.AdminApiFuture.SimpleAdminApiFuture; +import org.apache.kafka.clients.admin.internals.AdminApiHandler.Batched; +import org.apache.kafka.common.Node; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.errors.ApiException; +import org.apache.kafka.common.errors.LeaderNotAvailableException; +import org.apache.kafka.common.errors.NotLeaderOrFollowerException; +import org.apache.kafka.common.errors.RetriableException; +import org.apache.kafka.common.errors.UnsupportedVersionException; +import org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsPartition; +import org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic; +import org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse; +import org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsTopicResponse; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.requests.AbstractResponse; +import org.apache.kafka.common.requests.ListOffsetsRequest; +import org.apache.kafka.common.requests.ListOffsetsResponse; +import org.apache.kafka.common.utils.CollectionUtils; +import org.apache.kafka.common.utils.LogContext; +import org.slf4j.Logger; + +public final class ListOffsetsHandler extends Batched { + +private final Map offsetTimestampsByPartition; +private final ListOffsetsOptions options; +private final Logger log; +private final AdminApiLookupStrategy lookupStrategy; + +public ListOffsetsHandler( +Map offsetTimestampsByPartition, +ListOffsetsOptions options, +LogContext logContext +) { +this.offsetTimestampsByPartition = offsetTimestampsByPartition; +this.options = options; +this.log = logContext.logger(ListOffsetsHandler.class); +this.lookupStrategy = new PartitionLeaderStrategy(logContext); +} + +@Override +public String apiName() { +return "listOffsets"; +} + +@Override +public AdminApiLookupStrategy lookupStrategy() { +return this.lookupStrategy; +} + +@Override +ListOffsetsRequest.Builder buildBatchedRequest(int brokerId, Set keys) { +Map topicsByName = CollectionUtils.groupPartitionsByTopic( +keys, +topicName -> new ListOffsetsTopic().setName(topicName), +(listOffsetsTopic, partitionId) -> { +TopicPartition topicPartition = new TopicPartition(listOffsetsTopic.name(), partitionId); +long offsetTimestamp = offsetTimestampsByPartition.get(topicPartition); +listOffsetsTopic.partitions().add( +new ListOffsetsPartition() +.setPartitionIndex(partitionId) +.setTimestamp(offsetTimestamp)); +}); +boolean supportsMaxTimestamp = keys +.stream() +.anyMatch(key -> offsetTimestampsByPartition.get(key) == ListOffsetsRequest.MAX_TIMESTAMP); + +return ListOffsetsRequest.Builder +.forConsumer(true, options.isolationLevel(), supportsMaxTimestamp) +.setTargetTimes(new ArrayList<>(topicsByName.values())); +} + +@Override +public ApiResult handleResponse( +Node broker, +Set keys, +AbstractResponse abstractResponse +) { +ListOffsetsResponse response = (ListOffsetsResponse) abstractResponse; +Map completed = new
[GitHub] [kafka] dengziming commented on a diff in pull request #13432: KAFKA-14821 Implement the listOffsets API with AdminApiDriver
dengziming commented on code in PR #13432: URL: https://github.com/apache/kafka/pull/13432#discussion_r1147319995 ## clients/src/main/java/org/apache/kafka/clients/admin/internals/AdminApiHandler.java: ## @@ -70,6 +71,22 @@ */ ApiResult handleResponse(Node broker, Set keys, AbstractResponse response); +/** + * Callback that is invoked when a request hits an UnsupportedVersionException. If the + * exception can be handled and the request retried, the appropriate changes to the request + * should be applied and that should be indicated via the returned boolean value. If the + * exception is thrown during the lookup stage the handler should delegate the handling to the + * lookup strategy. + * + * @return True if the exception can be handled; false otherwise. + */ +default boolean handleUnsupportedVersionException( Review Comment: I forgot the details about the whole call chain, but I think we should decide which keys (if any) can retry with lower version and which keys can not, keys which are not retriable should be put into the result, and others are left out of the result which will be retried automatically, similar to what we do in `handleResponse`. ## clients/src/main/java/org/apache/kafka/clients/admin/internals/ListOffsetsHandler.java: ## @@ -0,0 +1,256 @@ +/* + * 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.internals; + +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.Optional; +import java.util.Set; +import java.util.function.Predicate; +import org.apache.kafka.clients.admin.ListOffsetsOptions; +import org.apache.kafka.clients.admin.ListOffsetsResult.ListOffsetsResultInfo; +import org.apache.kafka.clients.admin.OffsetSpec; +import org.apache.kafka.clients.admin.OffsetSpec.TimestampSpec; +import org.apache.kafka.clients.admin.internals.AdminApiFuture.SimpleAdminApiFuture; +import org.apache.kafka.clients.admin.internals.AdminApiHandler.Batched; +import org.apache.kafka.common.Node; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.errors.ApiException; +import org.apache.kafka.common.errors.InvalidMetadataException; +import org.apache.kafka.common.errors.RetriableException; +import org.apache.kafka.common.errors.UnsupportedVersionException; +import org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsPartition; +import org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsTopic; +import org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse; +import org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsTopicResponse; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.requests.AbstractResponse; +import org.apache.kafka.common.requests.ListOffsetsRequest; +import org.apache.kafka.common.requests.ListOffsetsResponse; +import org.apache.kafka.common.utils.LogContext; +import org.slf4j.Logger; + +public final class ListOffsetsHandler extends Batched { + +private final ListOffsetsOptions options; +private final Map offsetSpecsByPartition; +private final Logger log; +private final AdminApiLookupStrategy lookupStrategy; + +private volatile boolean skipMaxTimestampRequests = false; + +public ListOffsetsHandler( +Map offsetSpecsByPartition, +ListOffsetsOptions options, +LogContext logContext +) { +this.offsetSpecsByPartition = offsetSpecsByPartition; +this.options = options; +this.log = logContext.logger(ListOffsetsHandler.class); +this.lookupStrategy = new PartitionLeaderStrategy(logContext); +} + +@Override +public String apiName() { +return "listOffsets"; +} + +@Override +public AdminApiLookupStrategy lookupStrategy() { +return this.lookupStrategy; +} + +@Override +ListOffsetsRequest.Builder buildBatchedRequest(int brokerId, Set keys) { +final Map topicsByName = new HashMap<>(); +boolean supportsMaxTimestamp =