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 2020/10/13 22:48:46 UTC

[GitHub] [kafka] hachikuji commented on a change in pull request #9103: KAFKA-10181: Use Envelope RPC to do redirection for (Incremental)AlterConfig, AlterClientQuota and CreateTopics

hachikuji commented on a change in pull request #9103:
URL: https://github.com/apache/kafka/pull/9103#discussion_r504286524



##########
File path: clients/src/main/java/org/apache/kafka/common/requests/EnvelopeRequest.java
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.common.requests;
+
+import org.apache.kafka.common.message.DefaultPrincipalData;
+import org.apache.kafka.common.message.EnvelopeRequestData;
+import org.apache.kafka.common.message.EnvelopeResponseData;
+import org.apache.kafka.common.protocol.ApiKeys;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.protocol.types.Struct;
+import org.apache.kafka.common.security.auth.KafkaPrincipal;
+import org.apache.kafka.common.security.auth.KafkaPrincipalSerde;
+
+import java.nio.ByteBuffer;
+
+public class EnvelopeRequest extends AbstractRequest {
+
+    public static class Builder extends AbstractRequest.Builder<EnvelopeRequest> {
+
+        private final EnvelopeRequestData data;
+
+        public Builder(ByteBuffer embedData, String clientHostName) {
+            this(embedData, null, clientHostName);
+        }
+
+        public Builder(ByteBuffer embedData,

Review comment:
       nit: why don't we call it `requestData` to be consistent with the name used in the api spec?

##########
File path: clients/src/main/java/org/apache/kafka/common/requests/CreateTopicsResponse.java
##########
@@ -70,9 +70,12 @@ public int throttleTimeMs() {
     @Override
     public Map<Errors, Integer> errorCounts() {
         HashMap<Errors, Integer> counts = new HashMap<>();
-        data.topics().forEach(result ->
-            updateErrorCounts(counts, Errors.forCode(result.errorCode()))
-        );
+        for (CreateTopicsResponseData.CreatableTopicResult result : data.topics()) {
+            Errors error = Errors.forCode(result.errorCode());
+            if (error != Errors.NONE) {

Review comment:
       Not sure why we need this change. I think the convention is to include `NONE` in error counts.

##########
File path: clients/src/main/java/org/apache/kafka/common/requests/EnvelopeRequest.java
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.common.requests;
+
+import org.apache.kafka.common.message.DefaultPrincipalData;
+import org.apache.kafka.common.message.EnvelopeRequestData;
+import org.apache.kafka.common.message.EnvelopeResponseData;
+import org.apache.kafka.common.protocol.ApiKeys;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.protocol.types.Struct;
+import org.apache.kafka.common.security.auth.KafkaPrincipal;
+import org.apache.kafka.common.security.auth.KafkaPrincipalSerde;
+
+import java.nio.ByteBuffer;
+
+public class EnvelopeRequest extends AbstractRequest {
+
+    public static class Builder extends AbstractRequest.Builder<EnvelopeRequest> {
+
+        private final EnvelopeRequestData data;
+
+        public Builder(ByteBuffer embedData, String clientHostName) {
+            this(embedData, null, clientHostName);
+        }
+
+        public Builder(ByteBuffer embedData,
+                       ByteBuffer serializedPrincipal,
+                       String clientHostName) {
+            super(ApiKeys.ENVELOPE);
+            this.data = new EnvelopeRequestData()
+                            .setRequestData(embedData)
+                            .setRequestPrincipal(serializedPrincipal)
+                            .setClientHostName(clientHostName);
+        }
+
+        @Override
+        public EnvelopeRequest build(short version) {
+            return new EnvelopeRequest(data, version);
+        }
+
+        @Override
+        public String toString() {
+            return data.toString();
+        }
+    }
+
+    private final EnvelopeRequestData data;
+
+    public EnvelopeRequest(EnvelopeRequestData data, short version) {
+        super(ApiKeys.ENVELOPE, version);
+        this.data = data;
+    }
+
+    public EnvelopeRequest(Struct struct, short version) {
+        super(ApiKeys.ENVELOPE, version);
+        this.data = new EnvelopeRequestData(struct, version);
+    }
+
+    public ByteBuffer requestData() {
+        return data.requestData();
+    }
+
+    public String clientHostName() {
+        return data.clientHostName();
+    }
+
+    public KafkaPrincipal requestPrincipal(KafkaPrincipalSerde principalSerde) {

Review comment:
       nit: I think it might be better to pull this out of the request class. The direction we're moving is toward dumber request/response classes. Eventually `EnvelopeRequest` will go away and we'll just use `EnvelopeRequestData`.

##########
File path: clients/src/main/java/org/apache/kafka/common/requests/ApiVersionsResponse.java
##########
@@ -43,15 +43,43 @@
  */
 public class ApiVersionsResponse extends AbstractResponse {
 
+    public static final int MIN_CONSTRAINT_IBP_VERSION = 31;

Review comment:
       I'm wondering if we really need the IBP to leak into the common library. It should really only be a broker concern. Seems like the only point is so that we can continue to use the factory methods defined below from the broker code. Is that right? Could we instead move the factories to the broker?

##########
File path: clients/src/main/java/org/apache/kafka/common/requests/RequestContext.java
##########
@@ -78,8 +101,7 @@ public RequestAndSize parseRequest(ByteBuffer buffer) {
                         ", connectionId: " + connectionId +
                         ", listenerName: " + listenerName +
                         ", principal: " + principal +
-                        ", initialPrincipal: " + initialPrincipalName() +
-                        ", initialClientId: " + header.initialClientId(), ex);
+                        ", forwardingPrincipal: " + forwardingPrincipal, ex);

Review comment:
       nit: maybe print `forwardingPrincipal` only if it is defined

##########
File path: clients/src/main/java/org/apache/kafka/common/security/auth/KafkaPrincipalSerde.java
##########
@@ -0,0 +1,29 @@
+/*
+ * 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.common.security.auth;
+
+import java.nio.ByteBuffer;
+
+/**
+ * Serializer/Deserializer interface for {@link KafkaPrincipal} for the forwarding purpose.
+ */
+public interface KafkaPrincipalSerde {
+
+    ByteBuffer serialize(KafkaPrincipal principal, short version);

Review comment:
       It is strange to couple the serialization of the principal with the version of the envelope request. This might help us in the case of default principal builder, but users with their own custom builder are on their own, right? I think it is better to be consistent and leave versioning to the principal builder as well. 

##########
File path: clients/src/main/java/org/apache/kafka/common/requests/EnvelopeResponse.java
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.common.requests;
+
+import org.apache.kafka.common.message.EnvelopeResponseData;
+import org.apache.kafka.common.protocol.ApiKeys;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.protocol.types.Struct;
+
+import java.nio.ByteBuffer;
+import java.util.Map;
+
+public class EnvelopeResponse extends AbstractResponse {
+
+    private final EnvelopeResponseData data;
+
+    public EnvelopeResponse(int throttleTimeMs, AbstractResponse innerResponse, short innerApiVersion) {

Review comment:
       In a similar vein, I think it's better to not include serialization logic in the response object. It tends to hide some of the details like byte buffer allocation that we might want to control at another level. 

##########
File path: clients/src/main/java/org/apache/kafka/common/requests/EnvelopeResponse.java
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.common.requests;
+
+import org.apache.kafka.common.message.EnvelopeResponseData;
+import org.apache.kafka.common.protocol.ApiKeys;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.protocol.types.Struct;
+
+import java.nio.ByteBuffer;
+import java.util.Map;
+
+public class EnvelopeResponse extends AbstractResponse {
+
+    private final EnvelopeResponseData data;
+
+    public EnvelopeResponse(int throttleTimeMs, AbstractResponse innerResponse, short innerApiVersion) {
+        Struct dataStruct = innerResponse.toStruct(innerApiVersion);
+        ByteBuffer buffer = ByteBuffer.allocate(dataStruct.sizeOf());
+        dataStruct.writeTo(buffer);
+        buffer.flip();
+
+        this.data = new EnvelopeResponseData()
+                        .setThrottleTimeMs(throttleTimeMs)
+                        .setResponseData(buffer);
+    }
+
+    public EnvelopeResponse(EnvelopeResponseData data) {
+        this.data = data;
+    }
+
+    public AbstractResponse embedResponse(RequestHeader originalHeader) {

Review comment:
       Same here. We can return `ByteBuffer` and leave parsing to higher layers.

##########
File path: clients/src/main/resources/common/message/EnvelopeRequest.json
##########
@@ -0,0 +1,33 @@
+// 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.
+
+{
+  "apiKey": 58,
+  "type": "request",
+  "name": "EnvelopeRequest",
+  // Request struct for redirection.
+  "validVersions": "0",
+  "flexibleVersions": "0+",
+  "fields": [
+    { "name": "RequestData", "type": "bytes", "versions": "0+", "zeroCopy": true,
+      "about": "The embedded request header and data."},
+    { "name": "PrincipalIdToken", "type": "bytes", "tag": 0, "taggedVersions": "0+" },

Review comment:
       Do we have a use case for this yet? I don't see that it gets used anywhere.




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

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