Myasuka commented on a change in pull request #10449: 
[FLINK-7289][state-backend] Integrate RocksDB memory use with managed memory
URL: https://github.com/apache/flink/pull/10449#discussion_r354657527
 
 

 ##########
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/memory/SharedResources.java
 ##########
 @@ -0,0 +1,172 @@
+/*
+ * 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.memory;
+
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.util.function.LongFunctionWithException;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * A map that keeps track of acquired shared resources and handles their 
allocation disposal.
+ */
+final class SharedResources {
+
+       private final ReentrantLock lock = new ReentrantLock();
+
+       private final HashMap<String, LeasedResource<?>> reservedResources = 
new HashMap<>();
+
+       /**
+        * Gets the shared memory resource for the given owner and registers a 
lease. If the resource
+        * does not yet exist, it will be created via the given initializer 
function.
+        *
+        * <p>The resource must be released when no longer used. That releases 
the lease. When all leases are
+        * released, the resource is disposed.
+        */
+       <T extends AutoCloseable> ResourceAndSize<T> 
getOrAllocateSharedResource(
+                       String type,
+                       Object leaseHolder,
+                       LongFunctionWithException<T, Exception> initializer,
+                       long sizeForInitialization) throws Exception {
+
+               // We could be stuck on this lock for a while, in cases where 
another initialization is currently
+               // happening and the initialization is expensive.
+               // We lock interruptibly here to allow for faster exit in case 
of cancellation errors.
+               try {
+                       lock.lockInterruptibly();
 
 Review comment:
   Wonder if it could be better when using CAS to ensure only initialized once 
from previous PR #10416 which refer to the idea of 
[AtomicSafeInitializer](https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/concurrent/AtomicSafeInitializer.java).

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to