You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2022/02/25 03:59:00 UTC

[GitHub] [kafka] showuon commented on a change in pull request #11777: KAFKA-10000: Add producer fencing API to admin client (KIP-618)

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



##########
File path: clients/src/main/java/org/apache/kafka/clients/admin/internals/AdminApiHandler.java
##########
@@ -122,4 +126,54 @@ public ApiResult(
         }
     }
 
+    class RequestAndKeys<K> {
+        public final AbstractRequest.Builder<?> request;
+        public final Set<K> keys;
+
+        public RequestAndKeys(AbstractRequest.Builder<?> request, Set<K> keys) {
+            this.request = request;
+            this.keys = keys;
+        }
+    }
+
+    /**
+     * An {@link AdminApiHandler} that will group multiple keys into a single request when possible.
+     * Keys will be grouped together whenever they target the same broker. This type of handler
+     * should be used when when interacting with broker APIs that can act on multiple keys at once,

Review comment:
       nit: double `when`

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

Review comment:
       nit: indent

##########
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);

Review comment:
       wrong class name

##########
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:
       There's also an expected error: `PRODUCER_FENCED` which should be handled here.




-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org