You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ozone.apache.org by ad...@apache.org on 2021/12/08 10:47:45 UTC

[ozone] branch master updated: HDDS-6071. ResourceLimitCache leaks permits (#2895)

This is an automated email from the ASF dual-hosted git repository.

adoroszlai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new 937a3d0  HDDS-6071. ResourceLimitCache leaks permits (#2895)
937a3d0 is described below

commit 937a3d0afe4099fdf2cbd8b796782552f9b90aa8
Author: Doroszlai, Attila <64...@users.noreply.github.com>
AuthorDate: Wed Dec 8 11:47:10 2021 +0100

    HDDS-6071. ResourceLimitCache leaks permits (#2895)
---
 .../hadoop/hdds/utils/ResourceLimitCache.java      |  7 +++-
 .../hadoop/hdds/utils/TestResourceLimitCache.java  | 46 +++++++++++++++++++++-
 2 files changed, 50 insertions(+), 3 deletions(-)

diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceLimitCache.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceLimitCache.java
index 5dda249..80c539d 100644
--- a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceLimitCache.java
+++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceLimitCache.java
@@ -17,7 +17,6 @@
  */
 package org.apache.hadoop.hdds.utils;
 
-
 import java.util.Objects;
 import java.util.function.BiFunction;
 import java.util.function.Predicate;
@@ -79,7 +78,11 @@ public class ResourceLimitCache<K, V> implements Cache<K, V> {
   @Override
   public void removeIf(Predicate<K> predicate) {
     Objects.requireNonNull(predicate);
-    map.keySet().removeIf(predicate);
+    for (K key : map.keySet()) {
+      if (predicate.test(key)) {
+        remove(key);
+      }
+    }
   }
 
   @Override
diff --git a/hadoop-hdds/common/src/test/java/org/apache/hadoop/hdds/utils/TestResourceLimitCache.java b/hadoop-hdds/common/src/test/java/org/apache/hadoop/hdds/utils/TestResourceLimitCache.java
index 5d7ab85..2f54922 100644
--- a/hadoop-hdds/common/src/test/java/org/apache/hadoop/hdds/utils/TestResourceLimitCache.java
+++ b/hadoop-hdds/common/src/test/java/org/apache/hadoop/hdds/utils/TestResourceLimitCache.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -25,12 +25,15 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.function.Consumer;
 
 /**
  * Test for ResourceLimitCache.
  */
 public class TestResourceLimitCache {
 
+  private static final String ANY_VALUE = "asdf";
+
   @Test
   public void testResourceLimitCache()
       throws InterruptedException, TimeoutException {
@@ -88,4 +91,45 @@ public class TestResourceLimitCache {
     resourceCache.remove(1);
     Assert.assertNull(resourceCache.get(4));
   }
+
+  @Test(timeout = 5000)
+  public void testRemove() throws Exception {
+    testRemove(cache -> cache.remove(2), 2);
+  }
+
+  @Test(timeout = 5000)
+  public void testRemoveIf() throws Exception {
+    testRemove(cache -> cache.removeIf(k -> k <= 2), 1, 2);
+  }
+
+  @Test(timeout = 5000)
+  public void testClear() throws Exception {
+    testRemove(Cache::clear, 1, 2, 3);
+  }
+
+  private static void testRemove(Consumer<Cache<Integer, String>> op,
+      int... removedKeys) throws InterruptedException {
+
+    // GIVEN
+    final int maxSize = 3;
+    Cache<Integer, String> resourceCache =
+        new ResourceLimitCache<>(new ConcurrentHashMap<>(),
+            (k, v) -> new int[] {1}, maxSize);
+    for (int i = 1; i <= maxSize; ++i) {
+      resourceCache.put(i, ANY_VALUE);
+    }
+
+    // WHEN: remove some entries
+    op.accept(resourceCache);
+
+    // THEN
+    for (Integer k : removedKeys) {
+      Assert.assertNull(resourceCache.get(k));
+    }
+    // can put new entries
+    for (int i = 1; i <= removedKeys.length; ++i) {
+      resourceCache.put(maxSize + i, ANY_VALUE);
+    }
+  }
+
 }

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@ozone.apache.org
For additional commands, e-mail: commits-help@ozone.apache.org