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 2021/08/13 15:29:20 UTC

[GitHub] [kafka] smccauliff commented on a change in pull request #11204: KAFKA-13188 - Release the memory back into MemoryPool

smccauliff commented on a change in pull request #11204:
URL: https://github.com/apache/kafka/pull/11204#discussion_r688600716



##########
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:
       I realize the super class does not define a finalize method, but it might be nice to have correct ordering of calls to super class finalize in the code base.  See https://stackoverflow.com/questions/11379115/do-you-call-super-finalize-within-a-subclass/11379181
   
   




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