C0urante commented on a change in pull request #11777:
URL: https://github.com/apache/kafka/pull/11777#discussion_r814480835



##########
File path: 
clients/src/main/java/org/apache/kafka/clients/admin/internals/FenceProducersHandler.java
##########
@@ -0,0 +1,146 @@
+/*
+ * 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 org.apache.kafka.common.Node;
+import org.apache.kafka.common.errors.InvalidProducerEpochException;
+import org.apache.kafka.common.errors.TransactionalIdAuthorizationException;
+import org.apache.kafka.common.errors.TransactionalIdNotFoundException;
+import org.apache.kafka.common.message.InitProducerIdRequestData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.AbstractResponse;
+import org.apache.kafka.common.requests.FindCoordinatorRequest;
+import org.apache.kafka.common.requests.InitProducerIdRequest;
+import org.apache.kafka.common.requests.InitProducerIdResponse;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.ProducerIdAndEpoch;
+import org.slf4j.Logger;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class FenceProducersHandler extends 
AdminApiHandler.Unbatched<CoordinatorKey, ProducerIdAndEpoch> {
+    private final Logger log;
+    private final AdminApiLookupStrategy<CoordinatorKey> lookupStrategy;
+
+    public FenceProducersHandler(
+            LogContext logContext
+    ) {
+        this.log = logContext.logger(DescribeTransactionsHandler.class);
+        this.lookupStrategy = new 
CoordinatorStrategy(FindCoordinatorRequest.CoordinatorType.TRANSACTION, 
logContext);
+    }
+
+    public static AdminApiFuture.SimpleAdminApiFuture<CoordinatorKey, 
ProducerIdAndEpoch> newFuture(
+            Collection<String> transactionalIds
+    ) {
+        return AdminApiFuture.forKeys(buildKeySet(transactionalIds));
+    }
+
+    private static Set<CoordinatorKey> buildKeySet(Collection<String> 
transactionalIds) {
+        return transactionalIds.stream()
+                .map(CoordinatorKey::byTransactionalId)
+                .collect(Collectors.toSet());
+    }
+
+    @Override
+    public String apiName() {
+        return "fenceProducer";
+    }
+
+    @Override
+    public AdminApiLookupStrategy<CoordinatorKey> lookupStrategy() {
+        return lookupStrategy;
+    }
+
+    @Override
+    InitProducerIdRequest.Builder buildSingleRequest(int brokerId, 
CoordinatorKey key) {
+        if (key.type != FindCoordinatorRequest.CoordinatorType.TRANSACTION) {
+            throw new IllegalArgumentException("Invalid group coordinator key 
" + key +
+                    " when building `InitProducerId` request");
+        }
+        InitProducerIdRequestData data = new InitProducerIdRequestData()
+                .setProducerEpoch(ProducerIdAndEpoch.NONE.epoch)
+                .setProducerId(ProducerIdAndEpoch.NONE.producerId)
+                .setTransactionalId(key.idValue)
+                // Set transaction timeout to 1 since it's only being 
initialized to fence out older producers with the same transactional ID,
+                // and shouldn't be used for any actual record writes
+                .setTransactionTimeoutMs(1);
+        return new InitProducerIdRequest.Builder(data);
+    }
+
+    @Override
+    public ApiResult<CoordinatorKey, ProducerIdAndEpoch> handleSingleResponse(
+            Node broker,
+            CoordinatorKey key,
+            AbstractResponse abstractResponse
+    ) {
+        InitProducerIdResponse response = (InitProducerIdResponse) 
abstractResponse;
+
+        Errors error = Errors.forCode(response.data().errorCode());
+        if (error != Errors.NONE) {
+            return handleError(key, error);
+        }
+
+        Map<CoordinatorKey, ProducerIdAndEpoch> completed = 
Collections.singletonMap(key, new ProducerIdAndEpoch(
+                response.data().producerId(),
+                response.data().producerEpoch()
+        ));
+
+        return new ApiResult<>(completed, Collections.emptyMap(), 
Collections.emptyList());
+    }
+
+    private ApiResult<CoordinatorKey, ProducerIdAndEpoch> 
handleError(CoordinatorKey transactionalIdKey, Errors error) {
+        switch (error) {

Review comment:
       This shouldn't happen if we don't include a producer epoch/ID in the 
`InitProducerIdRequest`, right? If so, I could see there still being a case for 
adding coverage for `PRODUCER_FENCED` just in order to provide a better 
user-facing error message, but it's unclear what else we could really say 
beyond "unexpected error."
   
   Thoughts? 




-- 
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


Reply via email to