sodonnel commented on a change in pull request #1147: URL: https://github.com/apache/hadoop-ozone/pull/1147#discussion_r446880416
########## File path: hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/utils/ContainerCache.java ########## @@ -117,28 +117,47 @@ public ReferenceCountedDB getDB(long containerID, String containerDBType, throws IOException { Preconditions.checkState(containerID >= 0, "Container ID cannot be negative."); + ReferenceCountedDB db; lock.lock(); try { - ReferenceCountedDB db = (ReferenceCountedDB) this.get(containerDBPath); - - if (db == null) { - MetadataStore metadataStore = - MetadataStoreBuilder.newBuilder() - .setDbFile(new File(containerDBPath)) - .setCreateIfMissing(false) - .setConf(conf) - .setDBType(containerDBType) - .build(); - db = new ReferenceCountedDB(metadataStore, containerDBPath); - this.put(containerDBPath, db); + db = (ReferenceCountedDB) this.get(containerDBPath); + if (db != null) { + db.incrementReference(); + return db; } - // increment the reference before returning the object - db.incrementReference(); - return db; + } finally { + lock.unlock(); + } + + try { + MetadataStore metadataStore = + MetadataStoreBuilder.newBuilder() + .setDbFile(new File(containerDBPath)) + .setCreateIfMissing(false) + .setConf(conf) + .setDBType(containerDBType) + .build(); + db = new ReferenceCountedDB(metadataStore, containerDBPath); } catch (Exception e) { LOG.error("Error opening DB. Container:{} ContainerPath:{}", containerID, containerDBPath, e); throw e; + } + + lock.lock(); + try { + ReferenceCountedDB currentDB = + (ReferenceCountedDB) this.get(containerDBPath); + if (currentDB != null) { + // increment the reference before returning the object + currentDB.incrementReference(); + return currentDB; Review comment: What happens to the ReferenceCountedDB instance created at line 140 if we return here? Do we need to call db.cleanup() on the newly created instance, and if we don't could this potentially leak db objects? I note that the remove() call in ContainerCache calls db.cleanup when removing the entry, which makes we suspect this is important. If we do need to call cleanup() on it, we can probably capture the return value inside the lock and then call cleanup() from outside the lock and then return? ---------------------------------------------------------------- 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 --------------------------------------------------------------------- To unsubscribe, e-mail: ozone-issues-unsubscr...@hadoop.apache.org For additional commands, e-mail: ozone-issues-h...@hadoop.apache.org