Skip to content

[WIP] KAFKA-19123 the buffer of forwarded request should be released after receiving the reponse from controller #19566

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

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion core/src/main/scala/kafka/network/SocketServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,12 @@ private[kafka] class Processor(
tryUnmuteChannel(channelId)

case response: SendResponse =>
sendResponse(response, response.responseSend)
try
sendResponse(response, response.responseSend)
finally {
if (response.request.header.apiKey().forwardable)
response.request.releaseBuffer()
}
case response: CloseConnectionResponse =>
updateRequestMetrics(response)
trace("Closing socket connection actively according to the response code.")
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/scala/kafka/server/KafkaRequestHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ class KafkaRequestHandler(
if (originalRequest.callbackRequestCompleteTimeNanos.isEmpty)
originalRequest.callbackRequestCompleteTimeNanos = Some(time.nanoseconds())
threadCurrentRequest.remove()
if (!originalRequest.header.apiKey().forwardable)
originalRequest.releaseBuffer()
}

case request: RequestChannel.Request =>
Expand All @@ -163,7 +165,8 @@ class KafkaRequestHandler(
case e: Throwable => error("Exception when handling request", e)
} finally {
threadCurrentRequest.remove()
request.releaseBuffer()
if (!request.header.apiKey().forwardable)
request.releaseBuffer()
}

case RequestChannel.WakeupRequest =>
Expand Down
43 changes: 39 additions & 4 deletions core/src/test/scala/kafka/server/KafkaRequestHandlerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,23 @@ class KafkaRequestHandlerTest {
})
}

def makeRequest(time: Time, metrics: RequestChannelMetrics): RequestChannel.Request = {
def makeRequest(
time: Time,
metrics: RequestChannelMetrics,
apiKeys: ApiKeys = ApiKeys.API_VERSIONS,
version: Short = 0,
buffer: ByteBuffer = ByteBuffer.allocate(0),
memoryPool: MemoryPool = mock(classOf[MemoryPool])
): RequestChannel.Request = {
// Make unsupported API versions request to avoid having to parse a real request
val requestHeader = mock(classOf[RequestHeader])
when(requestHeader.apiKey()).thenReturn(ApiKeys.API_VERSIONS)
when(requestHeader.apiVersion()).thenReturn(0.toShort)
when(requestHeader.apiKey()).thenReturn(apiKeys)
when(requestHeader.apiVersion()).thenReturn(version)

val context = new RequestContext(requestHeader, "0", mock(classOf[InetAddress]), new KafkaPrincipal("", ""),
new ListenerName(""), SecurityProtocol.PLAINTEXT, mock(classOf[ClientInformation]), false)
new RequestChannel.Request(0, context, time.nanoseconds(),
mock(classOf[MemoryPool]), ByteBuffer.allocate(0), metrics)
memoryPool, buffer, metrics)
}

def setupBrokerTopicMetrics(systemRemoteStorageEnabled: Boolean = true): BrokerTopicMetrics = {
Expand Down Expand Up @@ -699,4 +706,32 @@ class KafkaRequestHandlerTest {
// cleanup
brokerTopicStats.close()
}

@Test
def testRequestBufferRelease(): Unit = {
val time = new MockTime()
val metrics = new RequestChannelMetrics(Collections.emptySet[ApiKeys])
val requestChannel = new RequestChannel(10, time, metrics)
val apiHandler = mock(classOf[ApiRequestHandler])
val memoryPool = mock(classOf[MemoryPool])
val buffer = ByteBuffer.allocate(1024)

val handler = new KafkaRequestHandler(0, 0, mock(classOf[Meter]), new AtomicInteger(1), requestChannel, apiHandler, time)

val request = makeRequest(time, metrics, ApiKeys.PRODUCE, 3, buffer, memoryPool)
requestChannel.sendRequest(request)

val shutdownThread = new Thread(() => {
try {
Thread.sleep(1000)
requestChannel.sendShutdownRequest()
} catch {
case _: InterruptedException =>
}
})

shutdownThread.start()
handler.run()
verify(memoryPool, times(1)).release(buffer)
}
}