You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by "sumitagrawl (via GitHub)" <gi...@apache.org> on 2023/10/16 06:05:46 UTC

[PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

sumitagrawl opened a new pull request, #5446:
URL: https://github.com/apache/ozone/pull/5446

   ## What changes were proposed in this pull request?
   
   It makes use of ratis resources to be acquired to control resource usages. And ozone cache can be non-blocking.
   
   Cache:
   Eviction policy with FIFO
   Queue length: 1024
   Queue data size: 1GB (but considering actual data size as happening for ratis)
   
   Parent JIRA: https://issues.apache.org/jira/browse/HDDS-9222
   
   ## What is the link to the Apache JIRA
   
   https://issues.apache.org/jira/browse/HDDS-9456
   
   ## How was this patch tested?
   
   1. UT case for ResourceCache
   2. Existing integration test
   


-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "sumitagrawl (via GitHub)" <gi...@apache.org>.
sumitagrawl commented on code in PR #5446:
URL: https://github.com/apache/ozone/pull/5446#discussion_r1363385608


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceCache.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.Weigher;
+import java.util.Objects;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import org.apache.commons.lang3.tuple.Pair;
+
+/**
+ * Cache with FIFO functionality with limit. If resource usages crosses the
+ * limit, first entry will be removed. This does not ensure meeting exact limit
+ * as first entry removal may not meet the limit.
+ */
+public class ResourceCache<K, V> implements Cache<K, V> {
+  private final com.google.common.cache.Cache<K, V> cache;
+
+  public ResourceCache(
+      Weigher weigher, int limits,

Review Comment:
   Guava cache support int as weigher, so going with integer limit with granularity of KB



-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "szetszwo (via GitHub)" <gi...@apache.org>.
szetszwo commented on code in PR #5446:
URL: https://github.com/apache/ozone/pull/5446#discussion_r1372086472


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceCache.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.Weigher;
+import java.util.Objects;
+import java.util.function.Predicate;
+
+/**
+ * Cache with FIFO functionality with limit. If resource usages crosses the
+ * limit, first entry will be removed. This does not ensure meeting exact limit
+ * as first entry removal may not meet the limit.
+ */
+public class ResourceCache<K, V> implements Cache<K, V> {
+  private final com.google.common.cache.Cache<K, V> cache;
+
+  public ResourceCache(
+      Weigher weigher, long limits,
+      RemovalListener<K, V> listener) {
+    Objects.requireNonNull(weigher);
+    if (listener == null) {
+      cache = CacheBuilder.newBuilder()
+          .maximumWeight(limits).weigher(weigher).build();
+    } else {
+      cache = CacheBuilder.newBuilder()
+          .maximumWeight(limits).weigher(weigher)
+          .removalListener(listener).build();
+    }
+  }
+
+  @Override
+  public V get(K key) {
+    Objects.requireNonNull(key);
+    return cache.getIfPresent(key);
+  }
+
+  @Override
+  public void put(K key, V value) throws InterruptedException {
+    Objects.requireNonNull(key);
+    Objects.requireNonNull(value);
+    cache.put(key, value);
+  }
+
+  @Override
+  public V remove(K key) {
+    Objects.requireNonNull(key);
+    V value = cache.getIfPresent(key);

Review Comment:
   Remove the return type `V` and don't call `getIfPresent`.



##########
hadoop-hdds/common/src/test/java/org/apache/hadoop/hdds/utils/TestResourceLimitCache.java:
##########
@@ -51,7 +51,7 @@ public void testResourceLimitCache()
     // permits out of 10
     CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
       try {
-        return resourceCache.put(1, "a");
+        resourceCache.put(1, "a");

Review Comment:
   Remove `TestResourceLimitCache.java`.



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceCache.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.Weigher;
+import java.util.Objects;
+import java.util.function.Predicate;
+
+/**
+ * Cache with FIFO functionality with limit. If resource usages crosses the
+ * limit, first entry will be removed. This does not ensure meeting exact limit
+ * as first entry removal may not meet the limit.
+ */
+public class ResourceCache<K, V> implements Cache<K, V> {
+  private final com.google.common.cache.Cache<K, V> cache;
+
+  public ResourceCache(
+      Weigher weigher, long limits,

Review Comment:
   Generic types are missing.



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceLimitCache.java:
##########
@@ -49,20 +49,19 @@ public V get(K key) {
   }
 
   @Override
-  public V put(K key, V value) throws InterruptedException {
+  public void put(K key, V value) throws InterruptedException {

Review Comment:
   Remove `ResourceLimitCache.java`.  It is not used anywhere.



##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/ContainerStateMachine.java:
##########
@@ -197,18 +196,20 @@ public ContainerStateMachine(RaftGroupId gid, ContainerDispatcher dispatcher,
     metrics = CSMMetrics.create(gid);
     this.writeChunkFutureMap = new ConcurrentHashMap<>();
     applyTransactionCompletionMap = new ConcurrentHashMap<>();
-    int numPendingRequests = conf
-        .getObject(DatanodeRatisServerConfig.class)
-        .getLeaderNumPendingRequests();
     long pendingRequestsBytesLimit = (long)conf.getStorageSize(
         OzoneConfigKeys.DFS_CONTAINER_RATIS_LEADER_PENDING_BYTES_LIMIT,
         OzoneConfigKeys.DFS_CONTAINER_RATIS_LEADER_PENDING_BYTES_LIMIT_DEFAULT,
         StorageUnit.BYTES);
-    int pendingRequestsMegaBytesLimit =
-        HddsUtils.roundupMb(pendingRequestsBytesLimit);
-    stateMachineDataCache = new ResourceLimitCache<>(new ConcurrentHashMap<>(),
-        (index, data) -> new int[] {1, HddsUtils.roundupMb(data.size())},
-        numPendingRequests, pendingRequestsMegaBytesLimit);
+    // cache with FIFO eviction, and if element not found, this needs
+    // to be obtained from disk for slow follower
+    stateMachineDataCache = new ResourceCache<>(
+        (index, data) -> ((ByteString)data).size(),
+        pendingRequestsBytesLimit,
+        (P) -> {

Review Comment:
   Use lower case for variable.



-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "nandakumar131 (via GitHub)" <gi...@apache.org>.
nandakumar131 commented on PR #5446:
URL: https://github.com/apache/ozone/pull/5446#issuecomment-1765711694

   @sumitagrawl, can you fix the checkstyle and the findbug issues?


-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "sumitagrawl (via GitHub)" <gi...@apache.org>.
sumitagrawl commented on code in PR #5446:
URL: https://github.com/apache/ozone/pull/5446#discussion_r1363404864


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceCache.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.Weigher;
+import java.util.Objects;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import org.apache.commons.lang3.tuple.Pair;
+
+/**
+ * Cache with FIFO functionality with limit. If resource usages crosses the
+ * limit, first entry will be removed. This does not ensure meeting exact limit
+ * as first entry removal may not meet the limit.
+ */
+public class ResourceCache<K, V> implements Cache<K, V> {
+  private final com.google.common.cache.Cache<K, V> cache;
+
+  public ResourceCache(
+      Weigher weigher, int limits,
+      BiFunction<Pair<K, V>, Boolean, Void> evictNotifier) {

Review Comment:
   done



-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "sumitagrawl (via GitHub)" <gi...@apache.org>.
sumitagrawl commented on code in PR #5446:
URL: https://github.com/apache/ozone/pull/5446#discussion_r1372525623


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceCache.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.Weigher;
+import java.util.Objects;
+import java.util.function.Predicate;
+
+/**
+ * Cache with FIFO functionality with limit. If resource usages crosses the
+ * limit, first entry will be removed. This does not ensure meeting exact limit
+ * as first entry removal may not meet the limit.
+ */
+public class ResourceCache<K, V> implements Cache<K, V> {
+  private final com.google.common.cache.Cache<K, V> cache;
+
+  public ResourceCache(
+      Weigher weigher, long limits,

Review Comment:
   done



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceCache.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.Weigher;
+import java.util.Objects;
+import java.util.function.Predicate;
+
+/**
+ * Cache with FIFO functionality with limit. If resource usages crosses the
+ * limit, first entry will be removed. This does not ensure meeting exact limit
+ * as first entry removal may not meet the limit.
+ */
+public class ResourceCache<K, V> implements Cache<K, V> {
+  private final com.google.common.cache.Cache<K, V> cache;
+
+  public ResourceCache(
+      Weigher weigher, long limits,
+      RemovalListener<K, V> listener) {
+    Objects.requireNonNull(weigher);
+    if (listener == null) {
+      cache = CacheBuilder.newBuilder()
+          .maximumWeight(limits).weigher(weigher).build();
+    } else {
+      cache = CacheBuilder.newBuilder()
+          .maximumWeight(limits).weigher(weigher)
+          .removalListener(listener).build();
+    }
+  }
+
+  @Override
+  public V get(K key) {
+    Objects.requireNonNull(key);
+    return cache.getIfPresent(key);
+  }
+
+  @Override
+  public void put(K key, V value) throws InterruptedException {
+    Objects.requireNonNull(key);
+    Objects.requireNonNull(value);
+    cache.put(key, value);
+  }
+
+  @Override
+  public V remove(K key) {
+    Objects.requireNonNull(key);
+    V value = cache.getIfPresent(key);

Review Comment:
   done



-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "szetszwo (via GitHub)" <gi...@apache.org>.
szetszwo commented on code in PR #5446:
URL: https://github.com/apache/ozone/pull/5446#discussion_r1362521728


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceCache.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.Weigher;
+import java.util.Objects;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import org.apache.commons.lang3.tuple.Pair;
+
+/**
+ * Cache with FIFO functionality with limit. If resource usages crosses the
+ * limit, first entry will be removed. This does not ensure meeting exact limit
+ * as first entry removal may not meet the limit.
+ */
+public class ResourceCache<K, V> implements Cache<K, V> {
+  private final com.google.common.cache.Cache<K, V> cache;
+
+  public ResourceCache(
+      Weigher weigher, int limits,
+      BiFunction<Pair<K, V>, Boolean, Void> evictNotifier) {
+    Objects.requireNonNull(weigher);
+    Objects.requireNonNull(limits);
+    RemovalListener<K, V> listener = ele -> {
+      if (null != evictNotifier) {
+        evictNotifier.apply(Pair.of(ele.getKey(), ele.getValue()),
+            ele.wasEvicted());
+      }
+    };
+    cache = CacheBuilder.newBuilder()
+        .maximumWeight(limits)
+        .weigher(weigher)
+        .removalListener(listener)
+        .build();
+  }
+
+  @Override
+  public V get(K key) {
+    Objects.requireNonNull(key);
+    return cache.getIfPresent(key);
+  }
+
+  @Override
+  public V put(K key, V value) throws InterruptedException {
+    Objects.requireNonNull(key);
+    Objects.requireNonNull(value);
+    V oldValue = cache.getIfPresent(key);

Review Comment:
   Remove the return value and don't call `getIfPresent`.



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceCache.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.Weigher;
+import java.util.Objects;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import org.apache.commons.lang3.tuple.Pair;
+
+/**
+ * Cache with FIFO functionality with limit. If resource usages crosses the
+ * limit, first entry will be removed. This does not ensure meeting exact limit
+ * as first entry removal may not meet the limit.
+ */
+public class ResourceCache<K, V> implements Cache<K, V> {
+  private final com.google.common.cache.Cache<K, V> cache;
+
+  public ResourceCache(
+      Weigher weigher, int limits,
+      BiFunction<Pair<K, V>, Boolean, Void> evictNotifier) {
+    Objects.requireNonNull(weigher);
+    Objects.requireNonNull(limits);

Review Comment:
   How could `limits` be null?



##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/ContainerStateMachine.java:
##########
@@ -197,18 +196,21 @@ public ContainerStateMachine(RaftGroupId gid, ContainerDispatcher dispatcher,
     metrics = CSMMetrics.create(gid);
     this.writeChunkFutureMap = new ConcurrentHashMap<>();
     applyTransactionCompletionMap = new ConcurrentHashMap<>();
-    int numPendingRequests = conf
-        .getObject(DatanodeRatisServerConfig.class)
-        .getLeaderNumPendingRequests();
     long pendingRequestsBytesLimit = (long)conf.getStorageSize(
         OzoneConfigKeys.DFS_CONTAINER_RATIS_LEADER_PENDING_BYTES_LIMIT,
         OzoneConfigKeys.DFS_CONTAINER_RATIS_LEADER_PENDING_BYTES_LIMIT_DEFAULT,
         StorageUnit.BYTES);
-    int pendingRequestsMegaBytesLimit =
-        HddsUtils.roundupMb(pendingRequestsBytesLimit);
-    stateMachineDataCache = new ResourceLimitCache<>(new ConcurrentHashMap<>(),
-        (index, data) -> new int[] {1, HddsUtils.roundupMb(data.size())},
-        numPendingRequests, pendingRequestsMegaBytesLimit);
+    // cache with FIFO eviction, and if element not found, this needs
+    // to be obtained from disk for slow follower
+    stateMachineDataCache = new ResourceCache<>(
+        (index, data) -> HddsUtils.roundupKb(((ByteString)data).size()),

Review Comment:
   Use `long` and don't round.



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceCache.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.Weigher;
+import java.util.Objects;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import org.apache.commons.lang3.tuple.Pair;
+
+/**
+ * Cache with FIFO functionality with limit. If resource usages crosses the
+ * limit, first entry will be removed. This does not ensure meeting exact limit
+ * as first entry removal may not meet the limit.
+ */
+public class ResourceCache<K, V> implements Cache<K, V> {
+  private final com.google.common.cache.Cache<K, V> cache;
+
+  public ResourceCache(
+      Weigher weigher, int limits,
+      BiFunction<Pair<K, V>, Boolean, Void> evictNotifier) {
+    Objects.requireNonNull(weigher);
+    Objects.requireNonNull(limits);
+    RemovalListener<K, V> listener = ele -> {
+      if (null != evictNotifier) {
+        evictNotifier.apply(Pair.of(ele.getKey(), ele.getValue()),
+            ele.wasEvicted());
+      }
+    };
+    cache = CacheBuilder.newBuilder()

Review Comment:
   We may add `maximumSize(..)` as well.



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceCache.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.Weigher;
+import java.util.Objects;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import org.apache.commons.lang3.tuple.Pair;
+
+/**
+ * Cache with FIFO functionality with limit. If resource usages crosses the
+ * limit, first entry will be removed. This does not ensure meeting exact limit
+ * as first entry removal may not meet the limit.
+ */
+public class ResourceCache<K, V> implements Cache<K, V> {
+  private final com.google.common.cache.Cache<K, V> cache;
+
+  public ResourceCache(
+      Weigher weigher, int limits,
+      BiFunction<Pair<K, V>, Boolean, Void> evictNotifier) {

Review Comment:
   Pass `RemovalListener<K, V>` instead.



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceCache.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.Weigher;
+import java.util.Objects;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import org.apache.commons.lang3.tuple.Pair;
+
+/**
+ * Cache with FIFO functionality with limit. If resource usages crosses the
+ * limit, first entry will be removed. This does not ensure meeting exact limit
+ * as first entry removal may not meet the limit.
+ */
+public class ResourceCache<K, V> implements Cache<K, V> {
+  private final com.google.common.cache.Cache<K, V> cache;
+
+  public ResourceCache(
+      Weigher weigher, int limits,

Review Comment:
   Use `long` for `limits`.



-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "sumitagrawl (via GitHub)" <gi...@apache.org>.
sumitagrawl commented on code in PR #5446:
URL: https://github.com/apache/ozone/pull/5446#discussion_r1363161660


##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/ContainerStateMachine.java:
##########
@@ -197,18 +196,21 @@ public ContainerStateMachine(RaftGroupId gid, ContainerDispatcher dispatcher,
     metrics = CSMMetrics.create(gid);
     this.writeChunkFutureMap = new ConcurrentHashMap<>();
     applyTransactionCompletionMap = new ConcurrentHashMap<>();
-    int numPendingRequests = conf
-        .getObject(DatanodeRatisServerConfig.class)
-        .getLeaderNumPendingRequests();
     long pendingRequestsBytesLimit = (long)conf.getStorageSize(
         OzoneConfigKeys.DFS_CONTAINER_RATIS_LEADER_PENDING_BYTES_LIMIT,
         OzoneConfigKeys.DFS_CONTAINER_RATIS_LEADER_PENDING_BYTES_LIMIT_DEFAULT,
         StorageUnit.BYTES);
-    int pendingRequestsMegaBytesLimit =
-        HddsUtils.roundupMb(pendingRequestsBytesLimit);
-    stateMachineDataCache = new ResourceLimitCache<>(new ConcurrentHashMap<>(),
-        (index, data) -> new int[] {1, HddsUtils.roundupMb(data.size())},
-        numPendingRequests, pendingRequestsMegaBytesLimit);
+    // cache with FIFO eviction, and if element not found, this needs
+    // to be obtained from disk for slow follower
+    stateMachineDataCache = new ResourceCache<>(
+        (index, data) -> HddsUtils.roundupKb(((ByteString)data).size()),

Review Comment:
   Weigher functional interface of guava cache supports only "int" type as max weight (size). This can reach to max 2GB, so roundupKb is used and keeping consistent to "int" type.



-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "szetszwo (via GitHub)" <gi...@apache.org>.
szetszwo commented on code in PR #5446:
URL: https://github.com/apache/ozone/pull/5446#discussion_r1364561872


##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/ContainerStateMachine.java:
##########
@@ -197,18 +196,21 @@ public ContainerStateMachine(RaftGroupId gid, ContainerDispatcher dispatcher,
     metrics = CSMMetrics.create(gid);
     this.writeChunkFutureMap = new ConcurrentHashMap<>();
     applyTransactionCompletionMap = new ConcurrentHashMap<>();
-    int numPendingRequests = conf
-        .getObject(DatanodeRatisServerConfig.class)
-        .getLeaderNumPendingRequests();
     long pendingRequestsBytesLimit = (long)conf.getStorageSize(
         OzoneConfigKeys.DFS_CONTAINER_RATIS_LEADER_PENDING_BYTES_LIMIT,
         OzoneConfigKeys.DFS_CONTAINER_RATIS_LEADER_PENDING_BYTES_LIMIT_DEFAULT,
         StorageUnit.BYTES);
-    int pendingRequestsMegaBytesLimit =
-        HddsUtils.roundupMb(pendingRequestsBytesLimit);
-    stateMachineDataCache = new ResourceLimitCache<>(new ConcurrentHashMap<>(),
-        (index, data) -> new int[] {1, HddsUtils.roundupMb(data.size())},
-        numPendingRequests, pendingRequestsMegaBytesLimit);
+    // cache with FIFO eviction, and if element not found, this needs
+    // to be obtained from disk for slow follower
+    stateMachineDataCache = new ResourceCache<>(
+        (index, data) -> HddsUtils.roundupKb(((ByteString)data).size()),

Review Comment:
   `ByteString.size()` returns `int`.



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceCache.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.Weigher;
+import java.util.Objects;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import org.apache.commons.lang3.tuple.Pair;
+
+/**
+ * Cache with FIFO functionality with limit. If resource usages crosses the
+ * limit, first entry will be removed. This does not ensure meeting exact limit
+ * as first entry removal may not meet the limit.
+ */
+public class ResourceCache<K, V> implements Cache<K, V> {
+  private final com.google.common.cache.Cache<K, V> cache;
+
+  public ResourceCache(
+      Weigher weigher, int limits,

Review Comment:
   The parameter in `maximumWeight(long maximumWeight)` is long.



-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "sumitagrawl (via GitHub)" <gi...@apache.org>.
sumitagrawl commented on code in PR #5446:
URL: https://github.com/apache/ozone/pull/5446#discussion_r1363385938


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceCache.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.Weigher;
+import java.util.Objects;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import org.apache.commons.lang3.tuple.Pair;
+
+/**
+ * Cache with FIFO functionality with limit. If resource usages crosses the
+ * limit, first entry will be removed. This does not ensure meeting exact limit
+ * as first entry removal may not meet the limit.
+ */
+public class ResourceCache<K, V> implements Cache<K, V> {
+  private final com.google.common.cache.Cache<K, V> cache;
+
+  public ResourceCache(
+      Weigher weigher, int limits,
+      BiFunction<Pair<K, V>, Boolean, Void> evictNotifier) {
+    Objects.requireNonNull(weigher);
+    Objects.requireNonNull(limits);

Review Comment:
   done



-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "sumitagrawl (via GitHub)" <gi...@apache.org>.
sumitagrawl commented on code in PR #5446:
URL: https://github.com/apache/ozone/pull/5446#discussion_r1366796052


##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/ContainerStateMachine.java:
##########
@@ -197,18 +196,21 @@ public ContainerStateMachine(RaftGroupId gid, ContainerDispatcher dispatcher,
     metrics = CSMMetrics.create(gid);
     this.writeChunkFutureMap = new ConcurrentHashMap<>();
     applyTransactionCompletionMap = new ConcurrentHashMap<>();
-    int numPendingRequests = conf
-        .getObject(DatanodeRatisServerConfig.class)
-        .getLeaderNumPendingRequests();
     long pendingRequestsBytesLimit = (long)conf.getStorageSize(
         OzoneConfigKeys.DFS_CONTAINER_RATIS_LEADER_PENDING_BYTES_LIMIT,
         OzoneConfigKeys.DFS_CONTAINER_RATIS_LEADER_PENDING_BYTES_LIMIT_DEFAULT,
         StorageUnit.BYTES);
-    int pendingRequestsMegaBytesLimit =
-        HddsUtils.roundupMb(pendingRequestsBytesLimit);
-    stateMachineDataCache = new ResourceLimitCache<>(new ConcurrentHashMap<>(),
-        (index, data) -> new int[] {1, HddsUtils.roundupMb(data.size())},
-        numPendingRequests, pendingRequestsMegaBytesLimit);
+    // cache with FIFO eviction, and if element not found, this needs
+    // to be obtained from disk for slow follower
+    stateMachineDataCache = new ResourceCache<>(
+        (index, data) -> HddsUtils.roundupKb(((ByteString)data).size()),

Review Comment:
   ok, removed roundup



-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "sumitagrawl (via GitHub)" <gi...@apache.org>.
sumitagrawl commented on code in PR #5446:
URL: https://github.com/apache/ozone/pull/5446#discussion_r1372526097


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceLimitCache.java:
##########
@@ -49,20 +49,19 @@ public V get(K key) {
   }
 
   @Override
-  public V put(K key, V value) throws InterruptedException {
+  public void put(K key, V value) throws InterruptedException {

Review Comment:
   done



##########
hadoop-hdds/common/src/test/java/org/apache/hadoop/hdds/utils/TestResourceLimitCache.java:
##########
@@ -51,7 +51,7 @@ public void testResourceLimitCache()
     // permits out of 10
     CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
       try {
-        return resourceCache.put(1, "a");
+        resourceCache.put(1, "a");

Review Comment:
   done



##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/ContainerStateMachine.java:
##########
@@ -197,18 +196,20 @@ public ContainerStateMachine(RaftGroupId gid, ContainerDispatcher dispatcher,
     metrics = CSMMetrics.create(gid);
     this.writeChunkFutureMap = new ConcurrentHashMap<>();
     applyTransactionCompletionMap = new ConcurrentHashMap<>();
-    int numPendingRequests = conf
-        .getObject(DatanodeRatisServerConfig.class)
-        .getLeaderNumPendingRequests();
     long pendingRequestsBytesLimit = (long)conf.getStorageSize(
         OzoneConfigKeys.DFS_CONTAINER_RATIS_LEADER_PENDING_BYTES_LIMIT,
         OzoneConfigKeys.DFS_CONTAINER_RATIS_LEADER_PENDING_BYTES_LIMIT_DEFAULT,
         StorageUnit.BYTES);
-    int pendingRequestsMegaBytesLimit =
-        HddsUtils.roundupMb(pendingRequestsBytesLimit);
-    stateMachineDataCache = new ResourceLimitCache<>(new ConcurrentHashMap<>(),
-        (index, data) -> new int[] {1, HddsUtils.roundupMb(data.size())},
-        numPendingRequests, pendingRequestsMegaBytesLimit);
+    // cache with FIFO eviction, and if element not found, this needs
+    // to be obtained from disk for slow follower
+    stateMachineDataCache = new ResourceCache<>(
+        (index, data) -> ((ByteString)data).size(),
+        pendingRequestsBytesLimit,
+        (P) -> {

Review Comment:
   done



-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "szetszwo (via GitHub)" <gi...@apache.org>.
szetszwo commented on PR #5446:
URL: https://github.com/apache/ozone/pull/5446#issuecomment-1779731148

   There are some unrelated, invalid findbugs warnings.  It look like are bugs in findbugs.  The change here somehow triggering them.  


-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "szetszwo (via GitHub)" <gi...@apache.org>.
szetszwo commented on code in PR #5446:
URL: https://github.com/apache/ozone/pull/5446#discussion_r1373361671


##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/ContainerStateMachine.java:
##########
@@ -197,18 +196,20 @@ public ContainerStateMachine(RaftGroupId gid, ContainerDispatcher dispatcher,
     metrics = CSMMetrics.create(gid);
     this.writeChunkFutureMap = new ConcurrentHashMap<>();
     applyTransactionCompletionMap = new ConcurrentHashMap<>();
-    int numPendingRequests = conf
-        .getObject(DatanodeRatisServerConfig.class)
-        .getLeaderNumPendingRequests();
     long pendingRequestsBytesLimit = (long)conf.getStorageSize(
         OzoneConfigKeys.DFS_CONTAINER_RATIS_LEADER_PENDING_BYTES_LIMIT,
         OzoneConfigKeys.DFS_CONTAINER_RATIS_LEADER_PENDING_BYTES_LIMIT_DEFAULT,
         StorageUnit.BYTES);
-    int pendingRequestsMegaBytesLimit =
-        HddsUtils.roundupMb(pendingRequestsBytesLimit);
-    stateMachineDataCache = new ResourceLimitCache<>(new ConcurrentHashMap<>(),
-        (index, data) -> new int[] {1, HddsUtils.roundupMb(data.size())},
-        numPendingRequests, pendingRequestsMegaBytesLimit);
+    // cache with FIFO eviction, and if element not found, this needs
+    // to be obtained from disk for slow follower
+    stateMachineDataCache = new ResourceCache<>(
+        (index, data) -> ((ByteString)data).size(),

Review Comment:
   The casting seems not needed.



-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "sumitagrawl (via GitHub)" <gi...@apache.org>.
sumitagrawl commented on code in PR #5446:
URL: https://github.com/apache/ozone/pull/5446#discussion_r1363379265


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceCache.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.Weigher;
+import java.util.Objects;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import org.apache.commons.lang3.tuple.Pair;
+
+/**
+ * Cache with FIFO functionality with limit. If resource usages crosses the
+ * limit, first entry will be removed. This does not ensure meeting exact limit
+ * as first entry removal may not meet the limit.
+ */
+public class ResourceCache<K, V> implements Cache<K, V> {
+  private final com.google.common.cache.Cache<K, V> cache;
+
+  public ResourceCache(
+      Weigher weigher, int limits,
+      BiFunction<Pair<K, V>, Boolean, Void> evictNotifier) {
+    Objects.requireNonNull(weigher);
+    Objects.requireNonNull(limits);
+    RemovalListener<K, V> listener = ele -> {
+      if (null != evictNotifier) {
+        evictNotifier.apply(Pair.of(ele.getKey(), ele.getValue()),
+            ele.wasEvicted());
+      }
+    };
+    cache = CacheBuilder.newBuilder()

Review Comment:
   Guava cache do not allow setting maximumSize with maximumWeight, support only one strategy at a time,
   `
   java.lang.IllegalStateException: maximum size was already set to 2
   	at com.google.common.base.Preconditions.checkState(Preconditions.java:586)
   	at com.google.common.cache.CacheBuilder.maximumWeight(CacheBuilder.java:528)
   	at org.apache.hadoop.hdds.utils.ResourceCache.<init>(ResourceCache.java:48)`



-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "sumitagrawl (via GitHub)" <gi...@apache.org>.
sumitagrawl commented on PR #5446:
URL: https://github.com/apache/ozone/pull/5446#issuecomment-1765604405

   @szetszwo Updated using Guava cache


-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "sumitagrawl (via GitHub)" <gi...@apache.org>.
sumitagrawl commented on code in PR #5446:
URL: https://github.com/apache/ozone/pull/5446#discussion_r1363384600


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceCache.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.Weigher;
+import java.util.Objects;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import org.apache.commons.lang3.tuple.Pair;
+
+/**
+ * Cache with FIFO functionality with limit. If resource usages crosses the
+ * limit, first entry will be removed. This does not ensure meeting exact limit
+ * as first entry removal may not meet the limit.
+ */
+public class ResourceCache<K, V> implements Cache<K, V> {
+  private final com.google.common.cache.Cache<K, V> cache;
+
+  public ResourceCache(
+      Weigher weigher, int limits,
+      BiFunction<Pair<K, V>, Boolean, Void> evictNotifier) {
+    Objects.requireNonNull(weigher);
+    Objects.requireNonNull(limits);
+    RemovalListener<K, V> listener = ele -> {
+      if (null != evictNotifier) {
+        evictNotifier.apply(Pair.of(ele.getKey(), ele.getValue()),
+            ele.wasEvicted());
+      }
+    };
+    cache = CacheBuilder.newBuilder()
+        .maximumWeight(limits)
+        .weigher(weigher)
+        .removalListener(listener)
+        .build();
+  }
+
+  @Override
+  public V get(K key) {
+    Objects.requireNonNull(key);
+    return cache.getIfPresent(key);
+  }
+
+  @Override
+  public V put(K key, V value) throws InterruptedException {
+    Objects.requireNonNull(key);
+    Objects.requireNonNull(value);
+    V oldValue = cache.getIfPresent(key);

Review Comment:
   done, ResourceLimitCache will be removed later with cleanup



-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "sumitagrawl (via GitHub)" <gi...@apache.org>.
sumitagrawl commented on PR #5446:
URL: https://github.com/apache/ozone/pull/5446#issuecomment-1772500774

   @szetszwo all comments are handled


-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "sumitagrawl (via GitHub)" <gi...@apache.org>.
sumitagrawl commented on code in PR #5446:
URL: https://github.com/apache/ozone/pull/5446#discussion_r1366794102


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceCache.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.Weigher;
+import java.util.Objects;
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+import org.apache.commons.lang3.tuple.Pair;
+
+/**
+ * Cache with FIFO functionality with limit. If resource usages crosses the
+ * limit, first entry will be removed. This does not ensure meeting exact limit
+ * as first entry removal may not meet the limit.
+ */
+public class ResourceCache<K, V> implements Cache<K, V> {
+  private final com.google.common.cache.Cache<K, V> cache;
+
+  public ResourceCache(
+      Weigher weigher, int limits,

Review Comment:
   done



-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "szetszwo (via GitHub)" <gi...@apache.org>.
szetszwo commented on code in PR #5446:
URL: https://github.com/apache/ozone/pull/5446#discussion_r1372658170


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceCache.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.Weigher;
+import java.util.Objects;
+import java.util.function.Predicate;
+
+/**
+ * Cache with FIFO functionality with limit. If resource usages crosses the
+ * limit, first entry will be removed. This does not ensure meeting exact limit
+ * as first entry removal may not meet the limit.
+ */
+public class ResourceCache<K, V> implements Cache<K, V> {
+  private final com.google.common.cache.Cache<K, V> cache;
+
+  public ResourceCache(
+      Weigher weigher, long limits,
+      RemovalListener<K, V> listener) {
+    Objects.requireNonNull(weigher);
+    if (listener == null) {
+      cache = CacheBuilder.newBuilder()
+          .maximumWeight(limits).weigher(weigher).build();
+    } else {
+      cache = CacheBuilder.newBuilder()
+          .maximumWeight(limits).weigher(weigher)
+          .removalListener(listener).build();
+    }
+  }
+
+  @Override
+  public V get(K key) {
+    Objects.requireNonNull(key);
+    return cache.getIfPresent(key);
+  }
+
+  @Override
+  public void put(K key, V value) throws InterruptedException {
+    Objects.requireNonNull(key);
+    Objects.requireNonNull(value);
+    cache.put(key, value);
+  }
+
+  @Override
+  public V remove(K key) {
+    Objects.requireNonNull(key);
+    V value = cache.getIfPresent(key);

Review Comment:
   Have you push your commit?   `remove` still return `V`.



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/ResourceCache.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.hadoop.hdds.utils;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.Weigher;
+import java.util.Objects;
+import java.util.function.Predicate;
+
+/**
+ * Cache with FIFO functionality with limit. If resource usages crosses the
+ * limit, first entry will be removed. This does not ensure meeting exact limit
+ * as first entry removal may not meet the limit.
+ */
+public class ResourceCache<K, V> implements Cache<K, V> {
+  private final com.google.common.cache.Cache<K, V> cache;
+
+  public ResourceCache(
+      Weigher weigher, long limits,

Review Comment:
   @sumitagrawl , the `Weigher` still does not have any generic types.



-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "sumitagrawl (via GitHub)" <gi...@apache.org>.
sumitagrawl commented on PR #5446:
URL: https://github.com/apache/ozone/pull/5446#issuecomment-1781159586

   @szetszwo missed to push, now done


-- 
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-unsubscribe@ozone.apache.org

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


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


Re: [PR] HDDS-9456. Non-blocking cache for Container State Machine [ozone]

Posted by "szetszwo (via GitHub)" <gi...@apache.org>.
szetszwo merged PR #5446:
URL: https://github.com/apache/ozone/pull/5446


-- 
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-unsubscribe@ozone.apache.org

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


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