diff --git a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/KeyManagerImpl.java b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/KeyManagerImpl.java index b8857fdb63ea..109ce23941b8 100644 --- a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/KeyManagerImpl.java +++ b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/KeyManagerImpl.java @@ -1397,13 +1397,20 @@ public void refresh(OmKeyInfo key) throws IOException { refreshPipeline(Arrays.asList(key)); } + private boolean isKeyDeleted(String key, Table keyTable) { + CacheValue omKeyInfoCacheValue + = keyTable.getCacheValue(new CacheKey(key)); + return omKeyInfoCacheValue != null + && omKeyInfoCacheValue.getCacheValue() == null; + } + /** * Helper function for listStatus to find key in TableCache. */ private void listStatusFindKeyInTableCache( Iterator, CacheValue>> cacheIter, String keyArgs, String startCacheKey, boolean recursive, - TreeMap cacheKeyMap, Set deletedKeySet) { + TreeMap cacheKeyMap) { while (cacheIter.hasNext()) { Map.Entry, CacheValue> entry = @@ -1414,23 +1421,20 @@ private void listStatusFindKeyInTableCache( } OmKeyInfo cacheOmKeyInfo = entry.getValue().getCacheValue(); // cacheOmKeyInfo is null if an entry is deleted in cache - if (cacheOmKeyInfo != null) { - if (cacheKey.startsWith(startCacheKey) && - cacheKey.compareTo(startCacheKey) >= 0) { - if (!recursive) { - String remainingKey = StringUtils.stripEnd(cacheKey.substring( - startCacheKey.length()), OZONE_URI_DELIMITER); - // For non-recursive, the remaining part of key can't have '/' - if (remainingKey.contains(OZONE_URI_DELIMITER)) { - continue; - } + if (cacheOmKeyInfo != null + && cacheKey.startsWith(startCacheKey) + && cacheKey.compareTo(startCacheKey) >= 0) { + if (!recursive) { + String remainingKey = StringUtils.stripEnd(cacheKey.substring( + startCacheKey.length()), OZONE_URI_DELIMITER); + // For non-recursive, the remaining part of key can't have '/' + if (remainingKey.contains(OZONE_URI_DELIMITER)) { + continue; } - OzoneFileStatus fileStatus = new OzoneFileStatus( - cacheOmKeyInfo, scmBlockSize, !OzoneFSUtils.isFile(cacheKey)); - cacheKeyMap.put(cacheKey, fileStatus); } - } else { - deletedKeySet.add(cacheKey); + OzoneFileStatus fileStatus = new OzoneFileStatus( + cacheOmKeyInfo, scmBlockSize, !OzoneFSUtils.isFile(cacheKey)); + cacheKeyMap.put(cacheKey, fileStatus); } } } @@ -1488,8 +1492,6 @@ public List listStatus(OmKeyArgs args, boolean recursive, String keyName = args.getKeyName(); // A map sorted by OmKey to combine results from TableCache and DB. TreeMap cacheKeyMap = new TreeMap<>(); - // A set to keep track of keys deleted in cache but not flushed to DB. - Set deletedKeySet = new TreeSet<>(); if (Strings.isNullOrEmpty(startKey)) { OzoneFileStatus fileStatus = getFileStatus(args, clientAddress); @@ -1519,7 +1521,7 @@ public List listStatus(OmKeyArgs args, boolean recursive, // First, find key in TableCache listStatusFindKeyInTableCache(cacheIter, keyArgs, startCacheKey, - recursive, cacheKeyMap, deletedKeySet); + recursive, cacheKeyMap); iterator = keyTable.iterator(); } finally { metadataManager.getLock().releaseReadLock(BUCKET_LOCK, volumeName, @@ -1545,7 +1547,8 @@ public List listStatus(OmKeyArgs args, boolean recursive, String entryKeyName = omKeyInfo.getKeyName(); if (recursive) { // for recursive list all the entries - if (!deletedKeySet.contains(entryInDb)) { + + if (!isKeyDeleted(entryInDb, keyTable)) { cacheKeyMap.put(entryInDb, new OzoneFileStatus(omKeyInfo, scmBlockSize, !OzoneFSUtils.isFile(entryKeyName))); countEntries++; @@ -1559,14 +1562,14 @@ public List listStatus(OmKeyArgs args, boolean recursive, .getImmediateChild(entryKeyName, keyName); boolean isFile = OzoneFSUtils.isFile(immediateChild); if (isFile) { - if (!deletedKeySet.contains(entryInDb)) { + if (!isKeyDeleted(entryInDb, keyTable)) { cacheKeyMap.put(entryInDb, new OzoneFileStatus(omKeyInfo, scmBlockSize, !isFile)); countEntries++; } } else { // if entry is a directory - if (!deletedKeySet.contains(entryInDb)) { + if (!isKeyDeleted(entryInDb, keyTable)) { if (!entryKeyName.equals(immediateChild)) { OmKeyInfo fakeDirEntry = createDirectoryKey( omKeyInfo.getVolumeName(), omKeyInfo.getBucketName(), @@ -1605,7 +1608,6 @@ public List listStatus(OmKeyArgs args, boolean recursive, } // Clean up temp map and set cacheKeyMap.clear(); - deletedKeySet.clear(); List keyInfoList = new ArrayList<>(fileStatusList.size()); fileStatusList.stream().map(s -> s.getKeyInfo()).forEach(keyInfoList::add);