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/09 01:38:56 UTC
[GitHub] [kafka] tusharg1993 commented on a change in pull request #11204: KAFKA-13188 - Release the memory back into MemoryPool
tusharg1993 commented on a change in pull request #11204:
URL: https://github.com/apache/kafka/pull/11204#discussion_r802200036
##########
File path: clients/src/main/java/org/apache/kafka/clients/ClientResponseWithFinalize.java
##########
@@ -0,0 +1,67 @@
+/*
+ * 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;
+
+import java.nio.ByteBuffer;
+import org.apache.kafka.common.errors.AuthenticationException;
+import org.apache.kafka.common.errors.UnsupportedVersionException;
+import org.apache.kafka.common.memory.MemoryPool;
+import org.apache.kafka.common.requests.AbstractResponse;
+import org.apache.kafka.common.requests.RequestHeader;
+import org.apache.kafka.common.utils.LogContext;
+import org.slf4j.Logger;
+
+
+/**
+ * This is a decorator for ClientResponse used to verify (at finalization time) that any underlying memory for this
+ * response has been returned to the pool. To be used only as a debugging aide.
+ */
+public class ClientResponseWithFinalize extends ClientResponse {
+ private final LogContext logContext;
+
+ public ClientResponseWithFinalize(RequestHeader requestHeader, RequestCompletionHandler callback,
+ String destination, long createdTimeMs, long receivedTimeMs, boolean disconnected,
+ UnsupportedVersionException versionMismatch, AuthenticationException authenticationException,
+ AbstractResponse responseBody, MemoryPool memoryPool, ByteBuffer responsePayload, LogContext logContext) {
+ super(requestHeader, callback, destination, createdTimeMs, receivedTimeMs, disconnected, versionMismatch,
+ authenticationException, responseBody, memoryPool, responsePayload);
+ this.logContext = logContext;
+ }
+
+ private Logger getLogger() {
+ if (logContext != null) {
+ return logContext.logger(ClientResponseWithFinalize.class);
+ }
+ return log;
+ }
+
+ protected void checkAndForceBufferRelease() {
+ if (memoryPool != null && responsePayload != null) {
+ getLogger().error("ByteBuffer[{}] not released. Ref Count: {}. RequestType: {}", responsePayload.position(),
+ refCount.get(), this.requestHeader.apiKey());
+ memoryPool.release(responsePayload);
+ responsePayload = null;
+ }
+ }
+
+ @Override
+ protected void finalize() throws Throwable {
+ super.finalize();
+ checkAndForceBufferRelease();
+ }
Review comment:
The finalizer here is not supposed to be used in production. Rather it's a debugging tool in cases issues arise
>This PR also adds the ClientResponseWithFinalize class for debugging purposes as well protected by linkedin.enable.client.resonse.leakcheck flag. The class uses finalizer to check whether there is some issue in code due to which the buffer wasn't released back to the pool yet. However, during in an actual production setting, the finalizers are costly and hence, not enabled by default.
--
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