szetszwo commented on PR #4731: URL: https://github.com/apache/ozone/pull/4731#issuecomment-1553289523
@adoroszlai , a standard solution is to use [Double-checked locking](https://en.wikipedia.org/wiki/Double-checked_locking) or a `MemoizedSupplier` as shown below. ```java @@ -25,8 +25,10 @@ import org.apache.hadoop.hdds.utils.db.managed.ManagedColumnFamilyOptions; import org.apache.hadoop.hdds.utils.db.managed.ManagedDBOptions; import org.apache.hadoop.hdds.utils.db.managed.ManagedLRUCache; +import org.apache.ratis.util.MemoizedSupplier; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Supplier; import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_DATANODE_METADATA_ROCKSDB_CACHE_SIZE; import static org.apache.hadoop.ozone.OzoneConfigKeys.HDDS_DATANODE_METADATA_ROCKSDB_CACHE_SIZE_DEFAULT; @@ -107,8 +109,8 @@ public ManagedColumnFamilyOptions getColumnFamilyOptions( * Base profile for datanode storage disks. */ private static final class StorageBasedProfile { - private final AtomicReference<ManagedColumnFamilyOptions> cfOptions = - new AtomicReference<>(); + private final AtomicReference<Supplier<ManagedColumnFamilyOptions>> + cfOptions = new AtomicReference<>(); private final DBProfile baseProfile; private StorageBasedProfile(DBProfile profile) { @@ -121,9 +123,10 @@ private ManagedDBOptions getDBOptions() { private ManagedColumnFamilyOptions getColumnFamilyOptions( ConfigurationSource config) { - cfOptions.updateAndGet(op -> op != null ? op : - createColumnFamilyOptions(config)); - return cfOptions.get(); + final Supplier<ManagedColumnFamilyOptions> options + = MemoizedSupplier.valueOf(() -> createColumnFamilyOptions(config)); + cfOptions.compareAndSet(null, options); + return cfOptions.get().get(); } ``` -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
