-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-10350: add forwarding manager implementation with metrics #9580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * 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 kafka.server | ||
|
|
||
| import kafka.metrics.KafkaMetricsGroup | ||
| import kafka.network.RequestChannel | ||
| import org.apache.kafka.clients.ClientResponse | ||
| import org.apache.kafka.common.metrics.Metrics | ||
| import org.apache.kafka.common.protocol.Errors | ||
| import org.apache.kafka.common.requests.{AbstractRequest, AbstractResponse, EnvelopeRequest, EnvelopeResponse} | ||
| import org.apache.kafka.common.utils.Time | ||
|
|
||
| import scala.compat.java8.OptionConverters._ | ||
|
|
||
| class ForwardingManager(metadataCache: kafka.server.MetadataCache, | ||
| time: Time, | ||
| metrics: Metrics, | ||
| config: KafkaConfig, | ||
| threadNamePrefix: Option[String] = None) extends | ||
| BrokerToControllerChannelManagerImpl(metadataCache, time, metrics, | ||
| config, "forwardingChannel", threadNamePrefix) with KafkaMetricsGroup { | ||
|
|
||
| private val forwardingMetricName = "NumRequestsForwardingToControllerPerSec" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wdyt about also adding a "forwarded" boolean to the request log? (in Would this be useful? cc @cmccabe
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the logging in RequestChannel. |
||
|
|
||
| def forwardRequest(request: RequestChannel.Request, | ||
| responseCallback: AbstractResponse => Unit): Unit = { | ||
| val principalSerde = request.context.principalSerde.asScala.getOrElse( | ||
|
mumrah marked this conversation as resolved.
|
||
| throw new IllegalArgumentException(s"Cannot deserialize principal from request $request " + | ||
| "since there is no serde defined") | ||
| ) | ||
| val serializedPrincipal = principalSerde.serialize(request.context.principal) | ||
| val forwardRequestBuffer = request.buffer.duplicate() | ||
| forwardRequestBuffer.flip() | ||
| val envelopeRequest = new EnvelopeRequest.Builder( | ||
| forwardRequestBuffer, | ||
| serializedPrincipal, | ||
| request.context.clientAddress.getAddress | ||
| ) | ||
|
|
||
| def onClientResponse(clientResponse: ClientResponse): Unit = { | ||
| val envelopeResponse = clientResponse.responseBody.asInstanceOf[EnvelopeResponse] | ||
| val envelopeError = envelopeResponse.error() | ||
|
|
||
| val response = if (envelopeError != Errors.NONE) { | ||
| // An envelope error indicates broker misconfiguration (e.g. the principal serde | ||
| // might not be defined on the receiving broker). In this case, we do not return | ||
| // the error directly to the client since it would not be expected. Instead we | ||
| // return `UNKNOWN_SERVER_ERROR` so that the user knows that there is a problem | ||
| // on the broker. | ||
| debug(s"Forwarded request $request failed with an error in envelope response $envelopeError") | ||
| request.body[AbstractRequest].getErrorResponse(Errors.UNKNOWN_SERVER_ERROR.exception()) | ||
| } else { | ||
| AbstractResponse.deserializeBody(envelopeResponse.responseData, request.header) | ||
| } | ||
| responseCallback(response) | ||
| } | ||
|
|
||
| sendRequest(envelopeRequest, onClientResponse) | ||
| } | ||
|
|
||
| override def start(): Unit = { | ||
| super.start() | ||
| newGauge(forwardingMetricName, () => requestQueue.size()) | ||
| } | ||
|
|
||
| override def shutdown(): Unit = { | ||
| removeMetric(forwardingMetricName) | ||
|
abbccdda marked this conversation as resolved.
|
||
| super.shutdown() | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we extend the BrokerToControllerChannel here? It looks like we only ever call
forwardRequest, so maybe it's better for ForwardingManager not to extend BrokerToControllerChannel and instead take it as a dependency. Otherwise we are exposing thesendRequestmethod which we don't really want anyone to use on theforwardingManagerinstance.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually we do need
sendRequeston the forwarding manager interface in the topic creation routing (WIP here.. TheforwardRequestis serving the purpose of embedding client request inside an Envelope, while for certain cases broker would like to send direct CreateTopicsRequest on behalf of its own principal instead of the client principal. I think we could debate whether to renameforwardingManagerto a different name to also include the possibility of sending direct request, just not have yet come up a better name here.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, ok 👍 Thanks for the explanation. Another option would be to let KafkaApis have BrokerToControllerChannelManager and ForwardingManager as separate dependencies. However, we already have lots of dependencies there, so I could see us wanting to use one instance for forwarding and regular requests. I don't really have a strong opinion either way, I'm ok leaving it as-is