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

2022-02-26 Thread GitBox


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



##
File path: 
clients/src/main/java/org/apache/kafka/clients/admin/internals/FenceProducersHandler.java
##
@@ -76,6 +75,10 @@ public String apiName() {
 " when building `InitProducerId` request");
 }
 InitProducerIdRequestData data = new InitProducerIdRequestData()
+// Because we never include a producer epoch or ID in this 
request, we expect that some errors
+// (such as PRODUCER_FENCED) will never be returned in the 
corresponding broker response.
+// If we ever modify this logic to include an epoch or 
producer ID, we will need to update the
+// error handling logic for this handler to accommodate these 
new errors.

Review comment:
   Looks good.




-- 
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] showuon commented on a change in pull request #11777: KAFKA-10000: Add producer fencing API to admin client (KIP-618)

2022-02-24 Thread GitBox


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



##
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 {
+private final Logger log;
+private final AdminApiLookupStrategy lookupStrategy;
+
+public FenceProducersHandler(
+LogContext logContext
+) {
+this.log = logContext.logger(DescribeTransactionsHandler.class);
+this.lookupStrategy = new 
CoordinatorStrategy(FindCoordinatorRequest.CoordinatorType.TRANSACTION, 
logContext);
+}
+
+public static AdminApiFuture.SimpleAdminApiFuture newFuture(
+Collection transactionalIds
+) {
+return AdminApiFuture.forKeys(buildKeySet(transactionalIds));
+}
+
+private static Set buildKeySet(Collection 
transactionalIds) {
+return transactionalIds.stream()
+.map(CoordinatorKey::byTransactionalId)
+.collect(Collectors.toSet());
+}
+
+@Override
+public String apiName() {
+return "fenceProducer";
+}
+
+@Override
+public AdminApiLookupStrategy 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 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 completed = 
Collections.singletonMap(key, new ProducerIdAndEpoch(
+response.data().producerId(),
+response.data().producerEpoch()
+));
+
+return new ApiResult<>(completed, Collections.emptyMap(), 
Collections.emptyList());
+}
+
+private ApiResult 
handleError(CoordinatorKey transactionalIdKey, Errors error) {
+switch (error) {

Review comment:
   I agree with you that since we always sent without producerID and epoch, 
the error should

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

2022-02-24 Thread GitBox


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



##
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 {
+private final Logger log;
+private final AdminApiLookupStrategy lookupStrategy;
+
+public FenceProducersHandler(
+LogContext logContext
+) {
+this.log = logContext.logger(DescribeTransactionsHandler.class);
+this.lookupStrategy = new 
CoordinatorStrategy(FindCoordinatorRequest.CoordinatorType.TRANSACTION, 
logContext);
+}
+
+public static AdminApiFuture.SimpleAdminApiFuture newFuture(
+Collection transactionalIds
+) {
+return AdminApiFuture.forKeys(buildKeySet(transactionalIds));
+}
+
+private static Set buildKeySet(Collection 
transactionalIds) {
+return transactionalIds.stream()
+.map(CoordinatorKey::byTransactionalId)
+.collect(Collectors.toSet());
+}
+
+@Override
+public String apiName() {
+return "fenceProducer";
+}
+
+@Override
+public AdminApiLookupStrategy 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 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 completed = 
Collections.singletonMap(key, new ProducerIdAndEpoch(
+response.data().producerId(),
+response.data().producerEpoch()
+));
+
+return new ApiResult<>(completed, Collections.emptyMap(), 
Collections.emptyList());
+}
+
+private ApiResult 
handleError(CoordinatorKey transactionalIdKey, Errors error) {
+switch (error) {

Review comment:
   I agree with you that since we always sent without producerID and epoch, 
the error should

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

2022-02-24 Thread GitBox


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 {
+public final AbstractRequest.Builder request;
+public final Set keys;
+
+public RequestAndKeys(AbstractRequest.Builder request, Set 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 {
+private final Logger log;
+private final AdminApiLookupStrategy 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;
+impor