1996fanrui commented on code in PR #23599:
URL: https://github.com/apache/flink/pull/23599#discussion_r1376013241


##########
flink-runtime/src/main/java/org/apache/flink/runtime/util/DefaultGroupCache.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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.flink.runtime.util;
+
+import org.apache.flink.annotation.VisibleForTesting;
+
+import org.apache.flink.shaded.guava31.com.google.common.base.Ticker;
+import org.apache.flink.shaded.guava31.com.google.common.cache.Cache;
+import org.apache.flink.shaded.guava31.com.google.common.cache.CacheBuilder;
+import 
org.apache.flink.shaded.guava31.com.google.common.cache.RemovalNotification;
+
+import javax.annotation.concurrent.NotThreadSafe;
+
+import java.time.Duration;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+/** Default implement of {@link GroupCache}. Entries will be expired after 
timeout. */
+@NotThreadSafe
+public class DefaultGroupCache<G, K, V> implements GroupCache<G, K, V> {
+    private final Cache<CacheKey<G, K>, V> cache;
+    private final Map<G, Set<CacheKey<G, K>>> cachedBlobKeysPerJob;
+
+    private DefaultGroupCache(Duration expireTimeout, int cacheSizeLimit, 
Ticker ticker) {
+        this.cachedBlobKeysPerJob = new HashMap<>();
+        this.cache =
+                CacheBuilder.newBuilder()
+                        .concurrencyLevel(1)
+                        .maximumSize(cacheSizeLimit)
+                        .expireAfterAccess(expireTimeout)
+                        .ticker(ticker)
+                        .removalListener(this::onCacheRemoval)
+                        .build();
+    }
+
+    @Override
+    public void clear() {
+        cachedBlobKeysPerJob.clear();
+        cache.cleanUp();
+    }
+
+    @Override
+    public V get(G group, K key) {
+        return cache.getIfPresent(new CacheKey<>(group, key));
+    }
+
+    @Override
+    public void put(G group, K key, V value) {
+        CacheKey<G, K> cacheKey = new CacheKey<>(group, key);
+        cache.put(cacheKey, value);
+        cachedBlobKeysPerJob.computeIfAbsent(group, ignore -> new 
HashSet<>()).add(cacheKey);
+    }
+
+    @Override
+    public void clearCacheForGroup(G group) {
+        Set<CacheKey<G, K>> removed = cachedBlobKeysPerJob.remove(group);
+        if (removed != null) {
+            cache.invalidateAll(removed);
+        }
+    }
+
+    /**
+     * Removal listener that remove the cache key of this group .
+     *
+     * @param removalNotification of removed element.
+     */
+    private void onCacheRemoval(RemovalNotification<CacheKey<G, K>, V> 
removalNotification) {
+        CacheKey<G, K> cacheKey = removalNotification.getKey();
+        V value = removalNotification.getValue();
+        if (cacheKey != null && value != null) {
+            cachedBlobKeysPerJob.computeIfPresent(
+                    cacheKey.getGroup(),
+                    (group, keys) -> {
+                        keys.remove(cacheKey);
+                        if (keys.isEmpty()) {
+                            return null;
+                        } else {
+                            return keys;
+                        }
+                    });

Review Comment:
   I write a demo on My IDEA, it doesn't have the memory leak. When return 
null, the map will remove the key.
   
   
   <img width="1044" alt="image" 
src="https://github.com/apache/flink/assets/38427477/457dd9bb-9ea3-474f-aab4-b0c43e0b5020";>
   
   
   <img width="679" alt="image" 
src="https://github.com/apache/flink/assets/38427477/040b3831-1919-4a6e-afa0-8742e4bf200b";>



-- 
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: issues-unsubscr...@flink.apache.org

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

Reply via email to