You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by GitBox <gi...@apache.org> on 2022/05/23 02:03:01 UTC

[GitHub] [ozone] mukul1987 opened a new pull request, #3444: HDDS-6788. Sort Ozone list Status output for FSO buckets.

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

   ## What changes were proposed in this pull request?
   With FSO buckets, S3 list keys output is not sorted.
   
   Amazon S3 list Objects returning the output sorted.
   https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html
   Since listKeys relies on listStatus for FSO buckets, this jira proposes to make listStatus output sorted
   
   
   ## What is the link to the Apache JIRA
   https://issues.apache.org/jira/browse/HDDS-6788
   
   ## How was this patch tested?
   Added a new test to check for sorted output
   


-- 
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


[GitHub] [ozone] mukul1987 commented on a diff in pull request #3444: HDDS-6788. Sort Ozone list Status output for FSO buckets.

Posted by GitBox <gi...@apache.org>.
mukul1987 commented on code in PR #3444:
URL: https://github.com/apache/ozone/pull/3444#discussion_r881707690


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,487 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.TreeMap;
+import java.util.Iterator;
+import java.util.PriorityQueue;
+import java.util.NoSuchElementException;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+
+public class OzoneListStatusHelper {
+
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize, GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public List<OzoneFileStatus> listStatusFSO(OmKeyArgs args, boolean recursive,
+                                             String startKey, long numEntries, String clientAddress)
+      throws IOException {
+    Preconditions.checkArgument(!recursive);
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    if (numEntries <= 0) {
+      return new ArrayList<>();
+    }
+
+    final ArrayList<OzoneFileStatus> fileStatuses = new ArrayList<>();
+
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    final String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    /**
+     * a) If the keyname is a file just return one entry
+     * b) if the keyname is root, then return the value of the bucket
+     * c) if the keyname is a different bucket than root, fetch the direcoty parent id
+     *
+     * if the startkey exists
+     * a) check the start key is a child ot key, else return emptry list
+     * b) chekc if the start key is a child chil of keynae, then reset the key to parent of start key
+     * c) if start key is non existent then seek to the neatest key
+     * d) if the keyname is not a dir or a file, it can either be invalid name or a prefix path
+     *     in case, this is called as part of listStatus fail as the real dir/file should exist
+     *     else, try to find the parent of keyname and use that as the prefix, use the rest of the path to construct prefix path
+     */
+
+
+    if (StringUtils.isNotBlank(keyName) && StringUtils.isNotBlank(startKey) &&
+        !OzoneFSUtils.isImmediateChild(keyName, startKey)) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+            "Returns empty list", startKey, keyName);
+      }
+      return Collections.emptyList();
+    }
+
+    String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
+    OmBucketInfo omBucketInfo =
+        metadataManager.getBucketTable().get(bucketKey);
+    if (omBucketInfo == null) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+            "Returns empty list", startKey, keyName);
+      }
+      return Collections.emptyList();
+    }
+
+    BucketLayout bucketLayout = omBucketInfo.getBucketLayout();
+
+    OzoneFileStatus fileStatus = getStatusHelper.apply(args, clientAddress, true);
+
+    String dbPrefixKey;
+    if (fileStatus == null) {
+      dbPrefixKey = getDbKey(keyName, args, omBucketInfo);
+      prefixKey = OzoneFSUtils.getParentDir(keyName);
+    } else {
+      if (fileStatus.isFile()) {
+        return Collections.singletonList(fileStatus);
+      }
+      long id = getId(fileStatus, omBucketInfo);
+      dbPrefixKey = metadataManager.getOzonePathKey(id, "");
+    }
+
+    String startKeyPrefix = Strings.isNullOrEmpty(startKey) ? "" : getDbKey(startKey, args, omBucketInfo);
+
+
+    try (MinHeapIterator heapIterator =
+             new MinHeapIterator(metadataManager, dbPrefixKey, bucketLayout,
+                 startKeyPrefix, volumeName, bucketName)) {
+
+      while (fileStatuses.size() < numEntries && heapIterator.hasNext()) {
+        HeapEntry entry = heapIterator.next();
+        OzoneFileStatus status = entry.getStatus(prefixKey, scmBlockSize, volumeName, bucketName);
+        LOG.info("returning status:{} keyname:{} startkey:{} numEntries:{}", status, prefixKey, startKey, numEntries);
+        fileStatuses.add(status);
+      }
+    }
+
+    return fileStatuses;
+  }
+
+
+  private String getDbKey(String key, OmKeyArgs args, OmBucketInfo omBucketInfo) throws IOException {
+    long startKeyParentId;
+    java.nio.file.Path file = Paths.get(key);
+    String parent = OzoneFSUtils.getParentDir(key);
+
+    OmKeyArgs startKeyArgs = args.toBuilder()
+        .setKeyName(parent)
+        .setSortDatanodesInPipeline(false)
+        .build();
+    OzoneFileStatus fileStatusInfo = getStatusHelper.apply(startKeyArgs,
+        null, true);
+    Preconditions.checkNotNull(fileStatusInfo);
+    startKeyParentId = getId(fileStatusInfo, omBucketInfo);
+    return metadataManager.getOzonePathKey(startKeyParentId, file.getFileName().toString());
+  }
+
+  private long getId(OzoneFileStatus fileStatus, OmBucketInfo omBucketInfo) {
+    if (fileStatus.getKeyInfo() != null) {
+      return fileStatus.getKeyInfo().getObjectID();
+    } else {
+      // assert root is null
+      // list root directory.
+      return omBucketInfo.getObjectID();
+    }
+  }
+  public enum EntryType {
+    DIR_CACHE,
+    FILE_CACHE,
+    RAW_FILE_DB,
+    RAW_DIR_DB;
+
+    public boolean isDir() {
+      switch (this) {
+        case DIR_CACHE:
+        case RAW_DIR_DB:
+          return true;
+        case FILE_CACHE:
+        case RAW_FILE_DB:
+          return false;
+        default:
+          throw new IllegalArgumentException();
+      }
+
+    }
+  }
+  public static class HeapEntry<T extends WithParentObjectId> implements Comparable<HeapEntry> {
+    private final EntryType entryType;
+    private final String key;
+    T value;
+
+    HeapEntry(EntryType entryType, String key, T value) {
+      this.entryType = entryType;
+      this.key = key;
+      this.value = value;
+    }
+
+    public int compareTo(HeapEntry other) {
+      return this.key.compareTo(other.key);
+    }
+
+    public boolean equals(Object other) {
+      HeapEntry that = (HeapEntry) other;
+      return this.key.equals(that.key);
+    }
+
+    public String getKey() {
+      return key;
+    }
+
+    public OzoneFileStatus getStatus(String prefixPath, long scmBlockSize, String volumeName, String bucketName) {
+      OmKeyInfo keyInfo;
+      if (entryType.isDir()) {
+        OmDirectoryInfo dirInfo = (OmDirectoryInfo) value;
+        String dirName = OMFileRequest.getAbsolutePath(prefixPath,
+            dirInfo.getName());
+        keyInfo = OMFileRequest.getOmKeyInfo(volumeName,
+            bucketName, dirInfo, dirName);
+      } else {
+        keyInfo = (OmKeyInfo) value;
+        keyInfo.setFileName(keyInfo.getKeyName());
+        String fullKeyPath = OMFileRequest.getAbsolutePath(prefixPath,
+            keyInfo.getKeyName());
+        keyInfo.setKeyName(fullKeyPath);
+      }
+      return new OzoneFileStatus(keyInfo, scmBlockSize, entryType.isDir());
+    }
+  }
+
+  public interface OzoneKeyIterator extends Iterator<HeapEntry<? extends WithParentObjectId>>, Closeable {
+
+  }
+
+  private static class RawIter<T extends WithParentObjectId>
+      implements OzoneKeyIterator {
+
+    private final EntryType iterType;
+    private final String prefixKey;
+
+    private final TableIterator<String,
+        ? extends Table.KeyValue<String, T>> tableIterator;
+    private final Set<String> cacheDeletedKeySet;
+    private HeapEntry currentKey;
+
+    RawIter(EntryType iterType, TableIterator<String,
+        ? extends Table.KeyValue<String, T>> tableIterator,
+            String prefixKey, String startKey,
+            Set<String> cacheDeletedKeySet) throws IOException {
+      this.iterType = iterType;
+      this.tableIterator = tableIterator;
+      this.prefixKey = prefixKey;
+      this.cacheDeletedKeySet = cacheDeletedKeySet;
+      this.currentKey = null;
+      if (!StringUtils.isBlank(prefixKey)) {
+        tableIterator.seek(prefixKey);
+      }
+
+      if (!StringUtils.isBlank(startKey)) {
+        tableIterator.seek(startKey);
+      }
+
+      getNextKey();
+    }
+
+    private void getNextKey() throws IOException {
+      while (tableIterator.hasNext() && currentKey == null) {
+        Table.KeyValue<String, T> entry = tableIterator.next();
+        String entryKey = entry.getKey();
+        if (entryKey.startsWith(prefixKey)) {
+          if (!cacheDeletedKeySet.contains(entryKey)) {
+            currentKey = new HeapEntry(iterType, entryKey, entry.getValue());
+          }
+        } else {
+          break;
+        }
+      }
+    }
+
+    public boolean hasNext() {
+      try {
+        getNextKey();
+      } catch (Throwable t) {
+        throw new NoSuchElementException();
+      }
+      return currentKey != null;
+    }
+
+    public HeapEntry next() {
+      try {
+        getNextKey();
+      } catch (Throwable t) {
+        throw new NoSuchElementException();
+      }
+      HeapEntry ret = currentKey;
+      currentKey = null;
+
+      return ret;
+    }
+
+    public void close() {
+
+    }
+  }
+
+  private static class CacheIter< T extends WithParentObjectId>
+      implements OzoneKeyIterator {
+    private final Set<String> cacheDeletedKeySet;
+    private final Map<String, T> cacheKeyMap;
+
+    private final Iterator<Map.Entry<String, T>>
+        cacheCreatedKeyIter;
+
+    private final Iterator<Map.Entry<CacheKey<String>, CacheValue<T>>>
+        cacheIter;
+
+    private final String prefixKey;
+    private final String startKey;
+
+
+    private final EntryType entryType;
+
+    public CacheIter(EntryType entryType, Iterator<Map.Entry<CacheKey<String>,
+        CacheValue<T>>> cacheIter, String startKey, String prefixKey) {
+      this.cacheDeletedKeySet = new TreeSet<>();
+      this.cacheKeyMap = new TreeMap<>();
+
+      this.cacheIter = cacheIter;
+      this.startKey = startKey;
+      this.prefixKey = prefixKey;
+      this.entryType = entryType;
+
+      getCacheValues();
+
+      cacheCreatedKeyIter = cacheKeyMap.entrySet().iterator();
+    }
+
+    private void getCacheValues() {
+      while (cacheIter.hasNext()) {
+        Map.Entry<CacheKey<String>, CacheValue<T>> entry =
+            cacheIter.next();
+        String cacheKey = entry.getKey().getCacheKey();
+        T cacheOmInfo = entry.getValue().getCacheValue();
+        // cacheOmKeyInfo is null if an entry is deleted in cache
+        if (cacheOmInfo == null) {
+          cacheDeletedKeySet.add(cacheKey);

Review Comment:
   done, using existing method in KeyManagerImpl to check for deleted keys



-- 
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


[GitHub] [ozone] mukul1987 commented on a diff in pull request #3444: HDDS-6788. Sort Ozone list Status output for FSO buckets.

Posted by GitBox <gi...@apache.org>.
mukul1987 commented on code in PR #3444:
URL: https://github.com/apache/ozone/pull/3444#discussion_r881979296


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,560 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+
+  /**
+   * Interface to get the File Status.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      boolean recursive, String startKey, long numEntries,
+      String clientAddress)
+      throws IOException {
+    Preconditions.checkArgument(!recursive);
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    if (numEntries <= 0) {
+      return new ArrayList<>();
+    }
+
+    boolean listKeysMode = false;
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    /**
+     * a) If the keyname is a file just return one entry
+     * b) if the keyname is root, then return the value of the bucket
+     * c) if the keyname is a different bucket than root,
+     * fetch the direcoty parent id
+     *
+     * if the startkey exists
+     * a) check the start key is a child ot key, else return emptry list
+     * b) chekc if the start key is a child chil of keynae,
+     * then reset the key to parent of start key
+     * c) if start key is non existent then seek to the neatest key
+     * d) if the keyname is not a dir or a file, it can either be
+     * invalid name or a prefix path
+     *     in case, this is called as part of listStatus fail as the
+     *     real dir/file should exist
+     *     else, try to find the parent of keyname and use that as the prefix,
+     *     use the rest of the path to construct prefix path
+     */
+
+    String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
+    OmBucketInfo omBucketInfo =
+        metadataManager.getBucketTable().get(bucketKey);
+    if (omBucketInfo == null) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+            "Returns empty list", startKey, keyName);
+      }
+      return new ArrayList<>();
+    }
+
+    boolean pathNameChanged = false;
+    if (StringUtils.isNotBlank(startKey)) {
+      if (StringUtils.isNotBlank(keyName)) {
+        if (!listKeysMode && !OzoneFSUtils.isImmediateChild(keyName, startKey)) {

Review Comment:
   This will be taken care of the in the next patch



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,560 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+
+  /**
+   * Interface to get the File Status.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      boolean recursive, String startKey, long numEntries,
+      String clientAddress)
+      throws IOException {
+    Preconditions.checkArgument(!recursive);
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    if (numEntries <= 0) {
+      return new ArrayList<>();
+    }
+
+    boolean listKeysMode = false;
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    /**
+     * a) If the keyname is a file just return one entry
+     * b) if the keyname is root, then return the value of the bucket
+     * c) if the keyname is a different bucket than root,
+     * fetch the direcoty parent id
+     *
+     * if the startkey exists
+     * a) check the start key is a child ot key, else return emptry list
+     * b) chekc if the start key is a child chil of keynae,
+     * then reset the key to parent of start key
+     * c) if start key is non existent then seek to the neatest key
+     * d) if the keyname is not a dir or a file, it can either be
+     * invalid name or a prefix path
+     *     in case, this is called as part of listStatus fail as the
+     *     real dir/file should exist
+     *     else, try to find the parent of keyname and use that as the prefix,
+     *     use the rest of the path to construct prefix path
+     */
+
+    String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
+    OmBucketInfo omBucketInfo =
+        metadataManager.getBucketTable().get(bucketKey);
+    if (omBucketInfo == null) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("StartKey {} is not an immediate child of keyName {}. " +

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


[GitHub] [ozone] mukul1987 commented on a diff in pull request #3444: HDDS-6788. Sort Ozone list Status output for FSO buckets.

Posted by GitBox <gi...@apache.org>.
mukul1987 commented on code in PR #3444:
URL: https://github.com/apache/ozone/pull/3444#discussion_r881967611


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,560 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+
+  /**
+   * Interface to get the File Status.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      boolean recursive, String startKey, long numEntries,
+      String clientAddress)
+      throws IOException {
+    Preconditions.checkArgument(!recursive);
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    if (numEntries <= 0) {
+      return new ArrayList<>();
+    }
+
+    boolean listKeysMode = false;
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    /**
+     * a) If the keyname is a file just return one entry
+     * b) if the keyname is root, then return the value of the bucket
+     * c) if the keyname is a different bucket than root,
+     * fetch the direcoty parent id
+     *
+     * if the startkey exists
+     * a) check the start key is a child ot key, else return emptry list
+     * b) chekc if the start key is a child chil of keynae,
+     * then reset the key to parent of start key
+     * c) if start key is non existent then seek to the neatest key
+     * d) if the keyname is not a dir or a file, it can either be
+     * invalid name or a prefix path
+     *     in case, this is called as part of listStatus fail as the
+     *     real dir/file should exist
+     *     else, try to find the parent of keyname and use that as the prefix,
+     *     use the rest of the path to construct prefix path
+     */
+
+    String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
+    OmBucketInfo omBucketInfo =
+        metadataManager.getBucketTable().get(bucketKey);
+    if (omBucketInfo == null) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+            "Returns empty list", startKey, keyName);
+      }
+      return new ArrayList<>();
+    }
+
+    boolean pathNameChanged = false;
+    if (StringUtils.isNotBlank(startKey)) {
+      if (StringUtils.isNotBlank(keyName)) {
+        if (!listKeysMode && !OzoneFSUtils.isImmediateChild(keyName, startKey)) {
+          if (LOG.isDebugEnabled()) {
+            LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+                "Returns empty list", startKey, keyName);
+          }
+          return new ArrayList<>();
+        }
+      } else {
+        keyName = OzoneFSUtils.getParentDir(startKey);
+        prefixKey = keyName;
+        pathNameChanged = true;
+      }
+    }
+
+    OzoneFileStatus fileStatus = null;
+    if (pathNameChanged) {
+      OmKeyArgs startKeyArgs = args.toBuilder()
+          .setKeyName(keyName)
+          .setSortDatanodesInPipeline(false)
+          .build();
+       fileStatus = getStatusHelper.apply(startKeyArgs,
+           null, true);
+    } else {
+      fileStatus =
+          getStatusHelper.apply(args, clientAddress, listKeysMode);
+    }
+
+    String dbPrefixKey;
+    if (fileStatus == null) {
+      dbPrefixKey = getDbKey(keyName, args, omBucketInfo);
+      prefixKey = OzoneFSUtils.getParentDir(keyName);
+    } else {
+      if (fileStatus.isFile()) {
+        return Collections.singletonList(fileStatus);
+      }
+
+      long id = getId(fileStatus, omBucketInfo);
+      dbPrefixKey = metadataManager.getOzonePathKey(id, "");
+    }
+
+    String startKeyPrefix =
+        Strings.isNullOrEmpty(startKey) ? "" :
+            getDbKey(startKey, args, omBucketInfo);
+
+    TreeMap<String, OzoneFileStatus> map = new TreeMap<>();
+
+    BucketLayout bucketLayout = omBucketInfo.getBucketLayout();
+    try (MinHeapIterator heapIterator =
+             new MinHeapIterator(metadataManager, dbPrefixKey, bucketLayout,
+                 startKeyPrefix, volumeName, bucketName)) {
+
+      while (map.size() < numEntries && heapIterator.hasNext()) {
+        HeapEntry entry = heapIterator.next();
+        OzoneFileStatus status = entry.getStatus(prefixKey,
+            scmBlockSize, volumeName, bucketName, args);
+        LOG.info("returning status:{} keyname:{} startkey:{} numEntries:{}",
+            status, prefixKey, startKey, numEntries);
+        map.put(entry.key, status);
+      }
+    }
+
+    return map.values();
+  }
+
+
+  private String getDbKey(String key, OmKeyArgs args,
+                          OmBucketInfo omBucketInfo) throws IOException {
+    long startKeyParentId;
+    String parent = OzoneFSUtils.getParentDir(key);
+
+    OmKeyArgs startKeyArgs = args.toBuilder()
+        .setKeyName(parent)
+        .setSortDatanodesInPipeline(false)
+        .build();
+    OzoneFileStatus fileStatusInfo = getStatusHelper.apply(startKeyArgs,
+        null, true);
+    Preconditions.checkNotNull(fileStatusInfo);
+    startKeyParentId = getId(fileStatusInfo, omBucketInfo);
+    return metadataManager.
+        getOzonePathKey(startKeyParentId, OzoneFSUtils.getFileName(key));
+  }
+
+  private long getId(OzoneFileStatus fileStatus, OmBucketInfo omBucketInfo) {
+    if (fileStatus.getKeyInfo() != null) {
+      return fileStatus.getKeyInfo().getObjectID();
+    } else {
+      // assert root is null
+      // list root directory.
+      return omBucketInfo.getObjectID();
+    }
+  }
+
+  /**
+   * Enum of types of entries.
+   */
+  public enum EntryType {
+    DIR_CACHE,
+    FILE_CACHE,
+    RAW_FILE_DB,
+    RAW_DIR_DB;
+
+    public boolean isDir() {
+      switch (this) {
+      case DIR_CACHE:
+      case RAW_DIR_DB:
+        return true;
+      case FILE_CACHE:
+      case RAW_FILE_DB:
+        return false;
+      default:
+        throw new IllegalArgumentException();
+      }
+    }
+  }
+
+  /**
+   * Entry which is added to heap.
+   * @param <T>
+   */
+  private static class HeapEntry<T extends WithParentObjectId>
+      implements Comparable<HeapEntry> {
+    private final EntryType entryType;
+    private final String key;
+    private final T value;
+
+    HeapEntry(EntryType entryType, String key, T value) {
+      this.entryType = entryType;
+      this.key = key;
+      this.value = value;
+    }
+
+    public int compareTo(HeapEntry other) {
+      return this.key.compareTo(other.key);
+    }
+
+    public boolean equals(Object other) {
+      if (other == null) {
+        return false;
+      }
+
+      if (!(other instanceof HeapEntry)) {
+        return false;
+      }
+
+
+      HeapEntry that = (HeapEntry) other;
+      return this.key.equals(that.key);
+    }
+
+    public int hashCode() {
+      return key.hashCode();
+    }
+
+    public String getKey() {
+      return key;
+    }
+
+    public OzoneFileStatus getStatus(String prefixPath, long scmBlockSize,
+                                     String volumeName, String bucketName,
+                                     OmKeyArgs args) {
+      OmKeyInfo keyInfo;
+      if (entryType.isDir()) {
+        OmDirectoryInfo dirInfo = (OmDirectoryInfo) value;
+        String dirName = OMFileRequest.getAbsolutePath(prefixPath,
+            dirInfo.getName());
+        keyInfo = OMFileRequest.getOmKeyInfo(volumeName,
+            bucketName, dirInfo, dirName);
+      } else {
+        keyInfo = (OmKeyInfo) value;
+        keyInfo.setFileName(keyInfo.getKeyName());
+        String fullKeyPath = OMFileRequest.getAbsolutePath(prefixPath,
+            keyInfo.getKeyName());
+        keyInfo.setKeyName(fullKeyPath);
+      }
+      return new OzoneFileStatus(keyInfo, scmBlockSize, entryType.isDir());
+    }
+  }
+
+  /**
+   * Iterator class for Ozone keys.
+   */
+  public interface OzoneKeyIterator extends
+      Iterator<HeapEntry<? extends WithParentObjectId>>, Closeable {
+  }
+
+  /**
+   * Raw iterator over db tables.
+   * @param <T>
+   */
+  private static class RawIter<T extends WithParentObjectId>
+      implements OzoneKeyIterator {
+
+    private final EntryType iterType;
+    private final String prefixKey;
+
+    private final TableIterator<String,
+        ? extends Table.KeyValue<String, T>> tableIterator;
+    private final Set<String> cacheDeletedKeySet;
+    private HeapEntry currentKey;
+
+    RawIter(EntryType iterType, TableIterator<String,
+        ? extends Table.KeyValue<String, T>> tableIterator,
+            String prefixKey, String startKey,
+            Set<String> cacheDeletedKeySet) throws IOException {
+      this.iterType = iterType;
+      this.tableIterator = tableIterator;
+      this.prefixKey = prefixKey;
+      this.cacheDeletedKeySet = cacheDeletedKeySet;
+      this.currentKey = null;
+      if (!StringUtils.isBlank(prefixKey)) {
+        tableIterator.seek(prefixKey);
+      }
+
+      if (!StringUtils.isBlank(startKey)) {
+        tableIterator.seek(startKey);
+      }
+
+      getNextKey();
+    }
+
+    private void getNextKey() throws IOException {
+      while (tableIterator.hasNext() && currentKey == null) {
+        Table.KeyValue<String, T> entry = tableIterator.next();
+        String entryKey = entry.getKey();
+        if (entryKey.startsWith(prefixKey)) {
+          if (!cacheDeletedKeySet.contains(entryKey)) {
+            currentKey = new HeapEntry(iterType, entryKey, entry.getValue());
+          }
+        } else {
+          break;
+        }
+      }
+    }
+
+    public boolean hasNext() {
+      try {
+        getNextKey();
+      } catch (Throwable t) {
+        throw new NoSuchElementException();
+      }
+      return currentKey != null;
+    }
+
+    public HeapEntry next() {
+      try {
+        getNextKey();
+      } catch (Throwable t) {
+        throw new NoSuchElementException();
+      }
+      HeapEntry ret = currentKey;
+      currentKey = null;
+
+      return ret;
+    }
+
+    public void close() {
+
+    }
+  }
+
+  /**
+   * Iterator for Cache for the database.
+   * @param <T>
+   */
+  private static class CacheIter< T extends WithParentObjectId>
+      implements OzoneKeyIterator {
+    private final Set<String> cacheDeletedKeySet;
+    private final Map<String, T> cacheKeyMap;
+
+    private final Iterator<Map.Entry<String, T>>
+        cacheCreatedKeyIter;
+
+    private final Iterator<Map.Entry<CacheKey<String>, CacheValue<T>>>
+        cacheIter;
+
+    private final String prefixKey;
+    private final String startKey;
+
+
+    private final EntryType entryType;
+
+    CacheIter(EntryType entryType, Iterator<Map.Entry<CacheKey<String>,
+        CacheValue<T>>> cacheIter, String startKey, String prefixKey) {
+      this.cacheDeletedKeySet = new TreeSet<>();
+      this.cacheKeyMap = new TreeMap<>();
+
+      this.cacheIter = cacheIter;
+      this.startKey = startKey;
+      this.prefixKey = prefixKey;
+      this.entryType = entryType;
+
+      getCacheValues();
+
+      cacheCreatedKeyIter = cacheKeyMap.entrySet().iterator();
+    }
+
+    private void getCacheValues() {
+      while (cacheIter.hasNext()) {
+        Map.Entry<CacheKey<String>, CacheValue<T>> entry =
+            cacheIter.next();
+        String cacheKey = entry.getKey().getCacheKey();
+        T cacheOmInfo = entry.getValue().getCacheValue();
+        // cacheOmKeyInfo is null if an entry is deleted in cache
+        if (cacheOmInfo == null) {
+          cacheDeletedKeySet.add(cacheKey);
+          continue;
+        }
+
+        if (StringUtils.isBlank(startKey)) {
+          // startKey is null or empty, then the seekKeyInDB="1024/"
+          if (cacheKey.startsWith(prefixKey)) {
+            cacheKeyMap.put(cacheKey, cacheOmInfo);
+          }
+        } else {
+          // startKey not empty, then the seekKeyInDB="1024/b" and
+          // seekKeyInDBWithOnlyParentID = "1024/". This is to avoid case of
+          // parentID with "102444" cache entries.
+          // Here, it has to list all the keys after "1024/b" and requires >=0
+          // string comparison.
+          if (cacheKey.startsWith(prefixKey) &&
+              cacheKey.compareTo(startKey) >= 0) {
+            cacheKeyMap.put(cacheKey, cacheOmInfo);
+          }
+        }
+      }
+    }
+
+    public boolean hasNext() {
+      return cacheCreatedKeyIter.hasNext();
+    }
+
+    public HeapEntry next() {
+      Map.Entry<String, T> entry = cacheCreatedKeyIter.next();
+      return new HeapEntry(entryType, entry.getKey(), entry.getValue());
+    }
+
+    public void close() {
+
+    }
+
+    public Set<String> getDeletedKeySet() {
+      return cacheDeletedKeySet;
+    }
+  }
+
+  /**
+   * Implement a min heap iterator to find the smaller
+   * lexicographically sorted string.
+   */
+  private static class MinHeapIterator implements
+      Iterator<HeapEntry<? extends WithParentObjectId>>, Closeable {
+
+    private final PriorityQueue<HeapEntry
+        <? extends WithParentObjectId>> minHeap = new PriorityQueue<>();
+    private final ArrayList<Iterator<HeapEntry
+        <? extends WithParentObjectId>>> iterators = new ArrayList<>();
+
+    private final RawIter<OmDirectoryInfo> rawDirIter;
+    private final RawIter<OmKeyInfo> rawFileIter;
+
+    private final CacheIter<OmKeyInfo> cacheFileIter;
+    private final CacheIter<OmDirectoryInfo> cacheDirIter;
+
+    MinHeapIterator(OMMetadataManager omMetadataManager, String prefixKey,
+                    BucketLayout bucketLayout, String startKey,
+                    String volumeName, String bucketName) throws IOException {
+
+      omMetadataManager.getLock().acquireReadLock(BUCKET_LOCK, volumeName,
+          bucketName);
+      cacheDirIter =
+          new CacheIter<>(EntryType.DIR_CACHE,
+              omMetadataManager.getDirectoryTable().cacheIterator(),
+              startKey, prefixKey);
+
+      cacheFileIter =
+          new CacheIter<>(EntryType.FILE_CACHE,
+              omMetadataManager.getKeyTable(bucketLayout).cacheIterator(),
+              startKey, prefixKey);
+
+      rawDirIter =
+          new RawIter<>(EntryType.RAW_DIR_DB,
+              omMetadataManager.getDirectoryTable().iterator(),
+              prefixKey, startKey, cacheDirIter.getDeletedKeySet());
+
+      rawFileIter =
+          new RawIter<>(EntryType.RAW_FILE_DB,
+              omMetadataManager.getKeyTable(bucketLayout).iterator(),
+              prefixKey, startKey, cacheFileIter.getDeletedKeySet());
+
+      omMetadataManager.getLock().releaseReadLock(BUCKET_LOCK, volumeName,
+          bucketName);
+
+      iterators.add(EntryType.DIR_CACHE.ordinal(), cacheDirIter);
+      iterators.add(EntryType.FILE_CACHE.ordinal(), cacheFileIter);
+      iterators.add(EntryType.RAW_FILE_DB.ordinal(), rawFileIter);
+      iterators.add(EntryType.RAW_DIR_DB.ordinal(), rawDirIter);
+      insertFirstElement();
+
+    }
+
+    public void insertFirstElement() {
+      for (Iterator<HeapEntry<? extends WithParentObjectId>> iter :
+          iterators) {
+        if (iter.hasNext()) {
+          minHeap.add(iter.next());
+        }
+      }
+    }
+
+    public boolean hasNext() {
+      return !minHeap.isEmpty();
+    }
+
+    public HeapEntry<? extends WithParentObjectId> next() {
+      HeapEntry<? extends WithParentObjectId> heapEntry = minHeap.remove();
+      Iterator<HeapEntry<? extends WithParentObjectId>> iter =
+          iterators.get(heapEntry.entryType.ordinal());
+      if (iter.hasNext()) {
+        minHeap.add(iter.next());
+      }
+
+      return heapEntry;
+    }
+
+    public void close() throws IOException {
+    }

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


[GitHub] [ozone] mukul1987 commented on a diff in pull request #3444: HDDS-6788. Sort Ozone list Status output for FSO buckets.

Posted by GitBox <gi...@apache.org>.
mukul1987 commented on code in PR #3444:
URL: https://github.com/apache/ozone/pull/3444#discussion_r881710462


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,560 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+
+  /**
+   * Interface to get the File Status.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      boolean recursive, String startKey, long numEntries,
+      String clientAddress)
+      throws IOException {
+    Preconditions.checkArgument(!recursive);
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    if (numEntries <= 0) {

Review Comment:
   done



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,560 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+
+  /**
+   * Interface to get the File Status.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      boolean recursive, String startKey, long numEntries,

Review Comment:
   done



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/KeyManagerImpl.java:
##########
@@ -1500,9 +1500,22 @@ public List<OzoneFileStatus> listStatus(OmKeyArgs args, boolean recursive,
     if (numEntries <= 0) {
       return fileStatusList;
     }
+
+    boolean useNewIterator = true;

Review Comment:
   I want to do a simple cleanup patch where the old code can be removed easily.



-- 
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


[GitHub] [ozone] kerneltime commented on a diff in pull request #3444: HDDS-6788. Sort Ozone list Status output for FSO buckets.

Posted by GitBox <gi...@apache.org>.
kerneltime commented on code in PR #3444:
URL: https://github.com/apache/ozone/pull/3444#discussion_r879695941


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,487 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.TreeMap;
+import java.util.Iterator;
+import java.util.PriorityQueue;
+import java.util.NoSuchElementException;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+
+public class OzoneListStatusHelper {
+
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize, GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public List<OzoneFileStatus> listStatusFSO(OmKeyArgs args, boolean recursive,
+                                             String startKey, long numEntries, String clientAddress)
+      throws IOException {
+    Preconditions.checkArgument(!recursive);
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    if (numEntries <= 0) {
+      return new ArrayList<>();
+    }
+
+    final ArrayList<OzoneFileStatus> fileStatuses = new ArrayList<>();
+
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    final String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    /**
+     * a) If the keyname is a file just return one entry
+     * b) if the keyname is root, then return the value of the bucket
+     * c) if the keyname is a different bucket than root, fetch the direcoty parent id
+     *
+     * if the startkey exists
+     * a) check the start key is a child ot key, else return emptry list
+     * b) chekc if the start key is a child chil of keynae, then reset the key to parent of start key
+     * c) if start key is non existent then seek to the neatest key
+     * d) if the keyname is not a dir or a file, it can either be invalid name or a prefix path
+     *     in case, this is called as part of listStatus fail as the real dir/file should exist
+     *     else, try to find the parent of keyname and use that as the prefix, use the rest of the path to construct prefix path
+     */
+
+
+    if (StringUtils.isNotBlank(keyName) && StringUtils.isNotBlank(startKey) &&
+        !OzoneFSUtils.isImmediateChild(keyName, startKey)) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+            "Returns empty list", startKey, keyName);
+      }
+      return Collections.emptyList();
+    }
+
+    String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
+    OmBucketInfo omBucketInfo =
+        metadataManager.getBucketTable().get(bucketKey);
+    if (omBucketInfo == null) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+            "Returns empty list", startKey, keyName);
+      }
+      return Collections.emptyList();
+    }
+
+    BucketLayout bucketLayout = omBucketInfo.getBucketLayout();
+
+    OzoneFileStatus fileStatus = getStatusHelper.apply(args, clientAddress, true);
+
+    String dbPrefixKey;
+    if (fileStatus == null) {
+      dbPrefixKey = getDbKey(keyName, args, omBucketInfo);
+      prefixKey = OzoneFSUtils.getParentDir(keyName);
+    } else {
+      if (fileStatus.isFile()) {
+        return Collections.singletonList(fileStatus);
+      }
+      long id = getId(fileStatus, omBucketInfo);
+      dbPrefixKey = metadataManager.getOzonePathKey(id, "");
+    }
+
+    String startKeyPrefix = Strings.isNullOrEmpty(startKey) ? "" : getDbKey(startKey, args, omBucketInfo);
+
+
+    try (MinHeapIterator heapIterator =
+             new MinHeapIterator(metadataManager, dbPrefixKey, bucketLayout,
+                 startKeyPrefix, volumeName, bucketName)) {
+
+      while (fileStatuses.size() < numEntries && heapIterator.hasNext()) {
+        HeapEntry entry = heapIterator.next();
+        OzoneFileStatus status = entry.getStatus(prefixKey, scmBlockSize, volumeName, bucketName);
+        LOG.info("returning status:{} keyname:{} startkey:{} numEntries:{}", status, prefixKey, startKey, numEntries);
+        fileStatuses.add(status);
+      }
+    }
+
+    return fileStatuses;
+  }
+
+
+  private String getDbKey(String key, OmKeyArgs args, OmBucketInfo omBucketInfo) throws IOException {
+    long startKeyParentId;
+    java.nio.file.Path file = Paths.get(key);
+    String parent = OzoneFSUtils.getParentDir(key);
+
+    OmKeyArgs startKeyArgs = args.toBuilder()
+        .setKeyName(parent)
+        .setSortDatanodesInPipeline(false)
+        .build();
+    OzoneFileStatus fileStatusInfo = getStatusHelper.apply(startKeyArgs,
+        null, true);
+    Preconditions.checkNotNull(fileStatusInfo);
+    startKeyParentId = getId(fileStatusInfo, omBucketInfo);
+    return metadataManager.getOzonePathKey(startKeyParentId, file.getFileName().toString());
+  }
+
+  private long getId(OzoneFileStatus fileStatus, OmBucketInfo omBucketInfo) {
+    if (fileStatus.getKeyInfo() != null) {
+      return fileStatus.getKeyInfo().getObjectID();
+    } else {
+      // assert root is null
+      // list root directory.
+      return omBucketInfo.getObjectID();
+    }
+  }
+  public enum EntryType {
+    DIR_CACHE,
+    FILE_CACHE,
+    RAW_FILE_DB,
+    RAW_DIR_DB;
+
+    public boolean isDir() {
+      switch (this) {
+        case DIR_CACHE:
+        case RAW_DIR_DB:
+          return true;
+        case FILE_CACHE:
+        case RAW_FILE_DB:
+          return false;
+        default:
+          throw new IllegalArgumentException();
+      }
+
+    }
+  }
+  public static class HeapEntry<T extends WithParentObjectId> implements Comparable<HeapEntry> {
+    private final EntryType entryType;
+    private final String key;
+    T value;
+
+    HeapEntry(EntryType entryType, String key, T value) {
+      this.entryType = entryType;
+      this.key = key;
+      this.value = value;
+    }
+
+    public int compareTo(HeapEntry other) {
+      return this.key.compareTo(other.key);
+    }
+
+    public boolean equals(Object other) {
+      HeapEntry that = (HeapEntry) other;
+      return this.key.equals(that.key);
+    }
+
+    public String getKey() {
+      return key;
+    }
+
+    public OzoneFileStatus getStatus(String prefixPath, long scmBlockSize, String volumeName, String bucketName) {
+      OmKeyInfo keyInfo;
+      if (entryType.isDir()) {
+        OmDirectoryInfo dirInfo = (OmDirectoryInfo) value;
+        String dirName = OMFileRequest.getAbsolutePath(prefixPath,
+            dirInfo.getName());
+        keyInfo = OMFileRequest.getOmKeyInfo(volumeName,
+            bucketName, dirInfo, dirName);
+      } else {
+        keyInfo = (OmKeyInfo) value;
+        keyInfo.setFileName(keyInfo.getKeyName());
+        String fullKeyPath = OMFileRequest.getAbsolutePath(prefixPath,
+            keyInfo.getKeyName());
+        keyInfo.setKeyName(fullKeyPath);
+      }
+      return new OzoneFileStatus(keyInfo, scmBlockSize, entryType.isDir());
+    }
+  }
+
+  public interface OzoneKeyIterator extends Iterator<HeapEntry<? extends WithParentObjectId>>, Closeable {
+
+  }
+
+  private static class RawIter<T extends WithParentObjectId>
+      implements OzoneKeyIterator {
+
+    private final EntryType iterType;
+    private final String prefixKey;
+
+    private final TableIterator<String,
+        ? extends Table.KeyValue<String, T>> tableIterator;
+    private final Set<String> cacheDeletedKeySet;
+    private HeapEntry currentKey;
+
+    RawIter(EntryType iterType, TableIterator<String,
+        ? extends Table.KeyValue<String, T>> tableIterator,
+            String prefixKey, String startKey,
+            Set<String> cacheDeletedKeySet) throws IOException {
+      this.iterType = iterType;
+      this.tableIterator = tableIterator;
+      this.prefixKey = prefixKey;
+      this.cacheDeletedKeySet = cacheDeletedKeySet;
+      this.currentKey = null;
+      if (!StringUtils.isBlank(prefixKey)) {
+        tableIterator.seek(prefixKey);
+      }
+
+      if (!StringUtils.isBlank(startKey)) {
+        tableIterator.seek(startKey);
+      }
+
+      getNextKey();
+    }
+
+    private void getNextKey() throws IOException {
+      while (tableIterator.hasNext() && currentKey == null) {
+        Table.KeyValue<String, T> entry = tableIterator.next();
+        String entryKey = entry.getKey();
+        if (entryKey.startsWith(prefixKey)) {
+          if (!cacheDeletedKeySet.contains(entryKey)) {
+            currentKey = new HeapEntry(iterType, entryKey, entry.getValue());
+          }
+        } else {
+          break;
+        }
+      }
+    }
+
+    public boolean hasNext() {
+      try {
+        getNextKey();
+      } catch (Throwable t) {
+        throw new NoSuchElementException();
+      }
+      return currentKey != null;
+    }
+
+    public HeapEntry next() {
+      try {
+        getNextKey();
+      } catch (Throwable t) {
+        throw new NoSuchElementException();
+      }
+      HeapEntry ret = currentKey;
+      currentKey = null;
+
+      return ret;
+    }
+
+    public void close() {
+
+    }
+  }
+
+  private static class CacheIter< T extends WithParentObjectId>
+      implements OzoneKeyIterator {
+    private final Set<String> cacheDeletedKeySet;
+    private final Map<String, T> cacheKeyMap;
+
+    private final Iterator<Map.Entry<String, T>>
+        cacheCreatedKeyIter;
+
+    private final Iterator<Map.Entry<CacheKey<String>, CacheValue<T>>>
+        cacheIter;
+
+    private final String prefixKey;
+    private final String startKey;
+
+
+    private final EntryType entryType;
+
+    public CacheIter(EntryType entryType, Iterator<Map.Entry<CacheKey<String>,
+        CacheValue<T>>> cacheIter, String startKey, String prefixKey) {
+      this.cacheDeletedKeySet = new TreeSet<>();
+      this.cacheKeyMap = new TreeMap<>();
+
+      this.cacheIter = cacheIter;
+      this.startKey = startKey;
+      this.prefixKey = prefixKey;
+      this.entryType = entryType;
+
+      getCacheValues();
+
+      cacheCreatedKeyIter = cacheKeyMap.entrySet().iterator();
+    }
+
+    private void getCacheValues() {
+      while (cacheIter.hasNext()) {
+        Map.Entry<CacheKey<String>, CacheValue<T>> entry =
+            cacheIter.next();
+        String cacheKey = entry.getKey().getCacheKey();
+        T cacheOmInfo = entry.getValue().getCacheValue();
+        // cacheOmKeyInfo is null if an entry is deleted in cache
+        if (cacheOmInfo == null) {
+          cacheDeletedKeySet.add(cacheKey);

Review Comment:
   Will this run into the same issue as the other commands that needed to be fixed where the memory can grow unbounded? 



-- 
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


[GitHub] [ozone] mukul1987 merged pull request #3444: HDDS-6788. Sort Ozone list Status output for FSO buckets.

Posted by GitBox <gi...@apache.org>.
mukul1987 merged PR #3444:
URL: https://github.com/apache/ozone/pull/3444


-- 
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


[GitHub] [ozone] mukul1987 commented on a diff in pull request #3444: HDDS-6788. Sort Ozone list Status output for FSO buckets.

Posted by GitBox <gi...@apache.org>.
mukul1987 commented on code in PR #3444:
URL: https://github.com/apache/ozone/pull/3444#discussion_r882304520


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,534 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.PriorityQueue;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.
+    OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+  /**
+   * Interface to get the File Status for a path.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  /**
+   * Interface for iteration of Heap Entries.
+   */
+  public interface ClosableIterator extends Iterator<HeapEntry>, Closeable {
+
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      String startKey, long numEntries, String clientAddress)
+      throws IOException {
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    boolean listKeysMode = false;
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
+    OmBucketInfo omBucketInfo =
+        metadataManager.getBucketTable().get(bucketKey);
+    if (omBucketInfo == null) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("Volume:{} Bucket:{} does not exist",
+            volumeName, bucketName);
+      }
+      return new ArrayList<>();
+    }
+
+    // Determine if the prefixKey is determined from the startKey
+    // if the keyName is null
+    if (StringUtils.isNotBlank(startKey)) {
+      if (StringUtils.isNotBlank(keyName)) {
+        if (!listKeysMode &&
+            !OzoneFSUtils.arePathsSame(keyName, startKey) &&
+            !OzoneFSUtils.isImmediateChild(keyName, startKey)) {

Review Comment:
   ok 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


[GitHub] [ozone] rakeshadr commented on a diff in pull request #3444: HDDS-6788. Sort Ozone list Status output for FSO buckets.

Posted by GitBox <gi...@apache.org>.
rakeshadr commented on code in PR #3444:
URL: https://github.com/apache/ozone/pull/3444#discussion_r882321732


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,534 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.PriorityQueue;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.
+    OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+  /**
+   * Interface to get the File Status for a path.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  /**
+   * Interface for iteration of Heap Entries.
+   */
+  public interface ClosableIterator extends Iterator<HeapEntry>, Closeable {
+
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      String startKey, long numEntries, String clientAddress)
+      throws IOException {
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    boolean listKeysMode = false;
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
+    OmBucketInfo omBucketInfo =
+        metadataManager.getBucketTable().get(bucketKey);
+    if (omBucketInfo == null) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("Volume:{} Bucket:{} does not exist",
+            volumeName, bucketName);
+      }
+      return new ArrayList<>();
+    }
+
+    // Determine if the prefixKey is determined from the startKey
+    // if the keyName is null
+    if (StringUtils.isNotBlank(startKey)) {
+      if (StringUtils.isNotBlank(keyName)) {
+        if (!listKeysMode &&
+            !StringUtils.startsWith(startKey, keyName) &&
+            !OzoneFSUtils.isImmediateChild(keyName, startKey)) {
+          if (LOG.isDebugEnabled()) {
+            LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+                "Returns empty list", startKey, keyName);
+          }
+          return new ArrayList<>();
+        }
+      } else {
+        keyName = OzoneFSUtils.getParentDir(startKey);
+        prefixKey = keyName;
+        args = args.toBuilder()
+            .setKeyName(keyName)
+            .setSortDatanodesInPipeline(false)
+            .build();
+      }
+    }
+
+    OzoneFileStatus fileStatus =
+        getStatusHelper.apply(args, clientAddress, listKeysMode);
+
+    String dbPrefixKey;
+    if (fileStatus == null) {
+      // if the file status is null, prefix is a not a valid filesystem path
+      // this should only work in list keys mode.
+      // fetch the db key based on the prefix path.
+      dbPrefixKey = getDbKey(keyName, args, omBucketInfo);
+      prefixKey = OzoneFSUtils.getParentDir(keyName);
+    } else {
+      // If the keyname is a file just return one entry
+      if (fileStatus.isFile()) {
+        return Collections.singletonList(fileStatus);
+      }
+
+      // fetch the db key based on parent prefix id.
+      long id = getId(fileStatus, omBucketInfo);
+      dbPrefixKey = metadataManager.getOzonePathKey(id, "");
+    }
+
+    // Determine startKeyPrefix for DB iteration
+    String startKeyPrefix =
+        Strings.isNullOrEmpty(startKey) ? "" :
+            getDbKey(startKey, args, omBucketInfo);
+
+    TreeMap<String, OzoneFileStatus> map = new TreeMap<>();
+
+    BucketLayout bucketLayout = omBucketInfo.getBucketLayout();
+
+    // fetch the sorted output using a min heap iterator where
+    // every remove from the heap will give the smallest entry.
+    try (MinHeapIterator heapIterator = new MinHeapIterator(metadataManager,
+        dbPrefixKey, bucketLayout, startKeyPrefix, volumeName, bucketName)) {
+
+      while (map.size() < numEntries && heapIterator.hasNext()) {
+        HeapEntry entry = heapIterator.next();
+        OzoneFileStatus status = entry.getStatus(prefixKey,
+            scmBlockSize, volumeName, bucketName);
+        map.put(entry.key, status);
+      }
+    }
+
+    return map.values();
+  }
+
+  private String getDbKey(String key, OmKeyArgs args,
+                          OmBucketInfo omBucketInfo) throws IOException {
+    long startKeyParentId;
+    OzoneFileStatus fileStatusInfo = getFileStatus(key, args);
+    if (fileStatusInfo == null || fileStatusInfo.isFile()) {
+      String parent = OzoneFSUtils.getParentDir(key);
+
+      fileStatusInfo = getFileStatus(parent, args);
+
+      // the keyname is not a valid filesystem path.
+      // determine the parent prefix by fetching the
+      // prefix id of the parent directory
+      Preconditions.checkNotNull(fileStatusInfo);

Review Comment:
   checkNotNull can throw NPE, which is a RuntimeException and can leads to retries. Instead of that, can you throw OMException explicitly.
   
   ```
   2022-05-26 10:37:31,235 [Time-limited test] INFO  retry.RetryInvocationHandler (RetryInvocationHandler.java:log(411)) - com.google.protobuf.ServiceException: org.apache.hadoop.ipc.RemoteException(java.lang.NullPointerException): java.lang.NullPointerException
   	at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:889)
   	at org.apache.hadoop.ozone.om.OzoneListStatusHelper.getDbKey(OzoneListStatusHelper.java:)
   ```
   
   
   ```
     public static <T> T checkNotNull(@CheckForNull T reference) {
       if (reference == null) {
         throw new NullPointerException();
       } else {
         return reference;
       }
     }
   ```



-- 
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


[GitHub] [ozone] rakeshadr commented on a diff in pull request #3444: HDDS-6788. Sort Ozone list Status output for FSO buckets.

Posted by GitBox <gi...@apache.org>.
rakeshadr commented on code in PR #3444:
URL: https://github.com/apache/ozone/pull/3444#discussion_r881869258


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,560 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+
+  /**
+   * Interface to get the File Status.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      boolean recursive, String startKey, long numEntries,
+      String clientAddress)
+      throws IOException {
+    Preconditions.checkArgument(!recursive);
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    if (numEntries <= 0) {
+      return new ArrayList<>();
+    }
+
+    boolean listKeysMode = false;
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    /**
+     * a) If the keyname is a file just return one entry
+     * b) if the keyname is root, then return the value of the bucket
+     * c) if the keyname is a different bucket than root,
+     * fetch the direcoty parent id
+     *
+     * if the startkey exists
+     * a) check the start key is a child ot key, else return emptry list
+     * b) chekc if the start key is a child chil of keynae,
+     * then reset the key to parent of start key
+     * c) if start key is non existent then seek to the neatest key
+     * d) if the keyname is not a dir or a file, it can either be
+     * invalid name or a prefix path
+     *     in case, this is called as part of listStatus fail as the
+     *     real dir/file should exist
+     *     else, try to find the parent of keyname and use that as the prefix,
+     *     use the rest of the path to construct prefix path
+     */
+
+    String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
+    OmBucketInfo omBucketInfo =
+        metadataManager.getBucketTable().get(bucketKey);
+    if (omBucketInfo == null) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+            "Returns empty list", startKey, keyName);
+      }
+      return new ArrayList<>();
+    }
+
+    boolean pathNameChanged = false;
+    if (StringUtils.isNotBlank(startKey)) {
+      if (StringUtils.isNotBlank(keyName)) {
+        if (!listKeysMode && !OzoneFSUtils.isImmediateChild(keyName, startKey)) {
+          if (LOG.isDebugEnabled()) {
+            LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+                "Returns empty list", startKey, keyName);
+          }
+          return new ArrayList<>();
+        }
+      } else {
+        keyName = OzoneFSUtils.getParentDir(startKey);
+        prefixKey = keyName;
+        pathNameChanged = true;
+      }
+    }
+
+    OzoneFileStatus fileStatus = null;
+    if (pathNameChanged) {
+      OmKeyArgs startKeyArgs = args.toBuilder()
+          .setKeyName(keyName)
+          .setSortDatanodesInPipeline(false)
+          .build();
+       fileStatus = getStatusHelper.apply(startKeyArgs,
+           null, true);
+    } else {
+      fileStatus =
+          getStatusHelper.apply(args, clientAddress, listKeysMode);
+    }
+
+    String dbPrefixKey;
+    if (fileStatus == null) {
+      dbPrefixKey = getDbKey(keyName, args, omBucketInfo);
+      prefixKey = OzoneFSUtils.getParentDir(keyName);
+    } else {
+      if (fileStatus.isFile()) {
+        return Collections.singletonList(fileStatus);
+      }
+
+      long id = getId(fileStatus, omBucketInfo);
+      dbPrefixKey = metadataManager.getOzonePathKey(id, "");
+    }
+
+    String startKeyPrefix =
+        Strings.isNullOrEmpty(startKey) ? "" :
+            getDbKey(startKey, args, omBucketInfo);
+
+    TreeMap<String, OzoneFileStatus> map = new TreeMap<>();
+
+    BucketLayout bucketLayout = omBucketInfo.getBucketLayout();
+    try (MinHeapIterator heapIterator =
+             new MinHeapIterator(metadataManager, dbPrefixKey, bucketLayout,
+                 startKeyPrefix, volumeName, bucketName)) {
+
+      while (map.size() < numEntries && heapIterator.hasNext()) {
+        HeapEntry entry = heapIterator.next();
+        OzoneFileStatus status = entry.getStatus(prefixKey,
+            scmBlockSize, volumeName, bucketName, args);
+        LOG.info("returning status:{} keyname:{} startkey:{} numEntries:{}",

Review Comment:
   Please change `LOG.info` to `LOG.debug`



-- 
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


[GitHub] [ozone] mukul1987 commented on a diff in pull request #3444: HDDS-6788. Sort Ozone list Status output for FSO buckets.

Posted by GitBox <gi...@apache.org>.
mukul1987 commented on code in PR #3444:
URL: https://github.com/apache/ozone/pull/3444#discussion_r881979652


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,560 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+
+  /**
+   * Interface to get the File Status.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      boolean recursive, String startKey, long numEntries,
+      String clientAddress)
+      throws IOException {
+    Preconditions.checkArgument(!recursive);
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    if (numEntries <= 0) {
+      return new ArrayList<>();
+    }
+
+    boolean listKeysMode = false;
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    /**
+     * a) If the keyname is a file just return one entry
+     * b) if the keyname is root, then return the value of the bucket
+     * c) if the keyname is a different bucket than root,
+     * fetch the direcoty parent id
+     *
+     * if the startkey exists
+     * a) check the start key is a child ot key, else return emptry list
+     * b) chekc if the start key is a child chil of keynae,

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


[GitHub] [ozone] mukul1987 commented on a diff in pull request #3444: HDDS-6788. Sort Ozone list Status output for FSO buckets.

Posted by GitBox <gi...@apache.org>.
mukul1987 commented on code in PR #3444:
URL: https://github.com/apache/ozone/pull/3444#discussion_r881978832


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,560 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+
+  /**
+   * Interface to get the File Status.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      boolean recursive, String startKey, long numEntries,
+      String clientAddress)
+      throws IOException {
+    Preconditions.checkArgument(!recursive);
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    if (numEntries <= 0) {
+      return new ArrayList<>();
+    }
+
+    boolean listKeysMode = false;
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    /**
+     * a) If the keyname is a file just return one entry
+     * b) if the keyname is root, then return the value of the bucket
+     * c) if the keyname is a different bucket than root,
+     * fetch the direcoty parent id
+     *
+     * if the startkey exists
+     * a) check the start key is a child ot key, else return emptry list
+     * b) chekc if the start key is a child chil of keynae,
+     * then reset the key to parent of start key
+     * c) if start key is non existent then seek to the neatest key
+     * d) if the keyname is not a dir or a file, it can either be
+     * invalid name or a prefix path
+     *     in case, this is called as part of listStatus fail as the
+     *     real dir/file should exist
+     *     else, try to find the parent of keyname and use that as the prefix,
+     *     use the rest of the path to construct prefix path
+     */
+
+    String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
+    OmBucketInfo omBucketInfo =
+        metadataManager.getBucketTable().get(bucketKey);
+    if (omBucketInfo == null) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+            "Returns empty list", startKey, keyName);
+      }
+      return new ArrayList<>();
+    }
+
+    boolean pathNameChanged = false;
+    if (StringUtils.isNotBlank(startKey)) {
+      if (StringUtils.isNotBlank(keyName)) {
+        if (!listKeysMode && !OzoneFSUtils.isImmediateChild(keyName, startKey)) {
+          if (LOG.isDebugEnabled()) {
+            LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+                "Returns empty list", startKey, keyName);
+          }
+          return new ArrayList<>();
+        }
+      } else {
+        keyName = OzoneFSUtils.getParentDir(startKey);
+        prefixKey = keyName;
+        pathNameChanged = true;
+      }
+    }
+
+    OzoneFileStatus fileStatus = null;
+    if (pathNameChanged) {
+      OmKeyArgs startKeyArgs = args.toBuilder()
+          .setKeyName(keyName)
+          .setSortDatanodesInPipeline(false)
+          .build();
+       fileStatus = getStatusHelper.apply(startKeyArgs,
+           null, true);
+    } else {
+      fileStatus =
+          getStatusHelper.apply(args, clientAddress, listKeysMode);
+    }
+
+    String dbPrefixKey;
+    if (fileStatus == null) {
+      dbPrefixKey = getDbKey(keyName, args, omBucketInfo);
+      prefixKey = OzoneFSUtils.getParentDir(keyName);
+    } else {
+      if (fileStatus.isFile()) {
+        return Collections.singletonList(fileStatus);
+      }
+
+      long id = getId(fileStatus, omBucketInfo);
+      dbPrefixKey = metadataManager.getOzonePathKey(id, "");
+    }
+
+    String startKeyPrefix =
+        Strings.isNullOrEmpty(startKey) ? "" :
+            getDbKey(startKey, args, omBucketInfo);
+
+    TreeMap<String, OzoneFileStatus> map = new TreeMap<>();
+
+    BucketLayout bucketLayout = omBucketInfo.getBucketLayout();
+    try (MinHeapIterator heapIterator =
+             new MinHeapIterator(metadataManager, dbPrefixKey, bucketLayout,
+                 startKeyPrefix, volumeName, bucketName)) {
+
+      while (map.size() < numEntries && heapIterator.hasNext()) {
+        HeapEntry entry = heapIterator.next();
+        OzoneFileStatus status = entry.getStatus(prefixKey,
+            scmBlockSize, volumeName, bucketName, args);
+        LOG.info("returning status:{} keyname:{} startkey:{} numEntries:{}",

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


[GitHub] [ozone] rakeshadr commented on a diff in pull request #3444: HDDS-6788. Sort Ozone list Status output for FSO buckets.

Posted by GitBox <gi...@apache.org>.
rakeshadr commented on code in PR #3444:
URL: https://github.com/apache/ozone/pull/3444#discussion_r882303852


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,534 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.PriorityQueue;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.
+    OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+  /**
+   * Interface to get the File Status for a path.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  /**
+   * Interface for iteration of Heap Entries.
+   */
+  public interface ClosableIterator extends Iterator<HeapEntry>, Closeable {
+
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      String startKey, long numEntries, String clientAddress)
+      throws IOException {
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    boolean listKeysMode = false;
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
+    OmBucketInfo omBucketInfo =
+        metadataManager.getBucketTable().get(bucketKey);
+    if (omBucketInfo == null) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("Volume:{} Bucket:{} does not exist",
+            volumeName, bucketName);
+      }
+      return new ArrayList<>();
+    }
+
+    // Determine if the prefixKey is determined from the startKey
+    // if the keyName is null
+    if (StringUtils.isNotBlank(startKey)) {
+      if (StringUtils.isNotBlank(keyName)) {
+        if (!listKeysMode &&
+            !OzoneFSUtils.arePathsSame(keyName, startKey) &&
+            !OzoneFSUtils.isImmediateChild(keyName, startKey)) {

Review Comment:
   StringUtils.startsWith(startKey, keyPrefix) then should proceed. Now it returns saying startKey is not an immediate child.
   
   For ex: listStatus("a1", "a1/b2/d2/d21.tx");



-- 
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


[GitHub] [ozone] rakeshadr commented on a diff in pull request #3444: HDDS-6788. Sort Ozone list Status output for FSO buckets.

Posted by GitBox <gi...@apache.org>.
rakeshadr commented on code in PR #3444:
URL: https://github.com/apache/ozone/pull/3444#discussion_r882321732


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,534 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.PriorityQueue;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.
+    OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+  /**
+   * Interface to get the File Status for a path.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  /**
+   * Interface for iteration of Heap Entries.
+   */
+  public interface ClosableIterator extends Iterator<HeapEntry>, Closeable {
+
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      String startKey, long numEntries, String clientAddress)
+      throws IOException {
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    boolean listKeysMode = false;
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
+    OmBucketInfo omBucketInfo =
+        metadataManager.getBucketTable().get(bucketKey);
+    if (omBucketInfo == null) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("Volume:{} Bucket:{} does not exist",
+            volumeName, bucketName);
+      }
+      return new ArrayList<>();
+    }
+
+    // Determine if the prefixKey is determined from the startKey
+    // if the keyName is null
+    if (StringUtils.isNotBlank(startKey)) {
+      if (StringUtils.isNotBlank(keyName)) {
+        if (!listKeysMode &&
+            !StringUtils.startsWith(startKey, keyName) &&
+            !OzoneFSUtils.isImmediateChild(keyName, startKey)) {
+          if (LOG.isDebugEnabled()) {
+            LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+                "Returns empty list", startKey, keyName);
+          }
+          return new ArrayList<>();
+        }
+      } else {
+        keyName = OzoneFSUtils.getParentDir(startKey);
+        prefixKey = keyName;
+        args = args.toBuilder()
+            .setKeyName(keyName)
+            .setSortDatanodesInPipeline(false)
+            .build();
+      }
+    }
+
+    OzoneFileStatus fileStatus =
+        getStatusHelper.apply(args, clientAddress, listKeysMode);
+
+    String dbPrefixKey;
+    if (fileStatus == null) {
+      // if the file status is null, prefix is a not a valid filesystem path
+      // this should only work in list keys mode.
+      // fetch the db key based on the prefix path.
+      dbPrefixKey = getDbKey(keyName, args, omBucketInfo);
+      prefixKey = OzoneFSUtils.getParentDir(keyName);
+    } else {
+      // If the keyname is a file just return one entry
+      if (fileStatus.isFile()) {
+        return Collections.singletonList(fileStatus);
+      }
+
+      // fetch the db key based on parent prefix id.
+      long id = getId(fileStatus, omBucketInfo);
+      dbPrefixKey = metadataManager.getOzonePathKey(id, "");
+    }
+
+    // Determine startKeyPrefix for DB iteration
+    String startKeyPrefix =
+        Strings.isNullOrEmpty(startKey) ? "" :
+            getDbKey(startKey, args, omBucketInfo);
+
+    TreeMap<String, OzoneFileStatus> map = new TreeMap<>();
+
+    BucketLayout bucketLayout = omBucketInfo.getBucketLayout();
+
+    // fetch the sorted output using a min heap iterator where
+    // every remove from the heap will give the smallest entry.
+    try (MinHeapIterator heapIterator = new MinHeapIterator(metadataManager,
+        dbPrefixKey, bucketLayout, startKeyPrefix, volumeName, bucketName)) {
+
+      while (map.size() < numEntries && heapIterator.hasNext()) {
+        HeapEntry entry = heapIterator.next();
+        OzoneFileStatus status = entry.getStatus(prefixKey,
+            scmBlockSize, volumeName, bucketName);
+        map.put(entry.key, status);
+      }
+    }
+
+    return map.values();
+  }
+
+  private String getDbKey(String key, OmKeyArgs args,
+                          OmBucketInfo omBucketInfo) throws IOException {
+    long startKeyParentId;
+    OzoneFileStatus fileStatusInfo = getFileStatus(key, args);
+    if (fileStatusInfo == null || fileStatusInfo.isFile()) {
+      String parent = OzoneFSUtils.getParentDir(key);
+
+      fileStatusInfo = getFileStatus(parent, args);
+
+      // the keyname is not a valid filesystem path.
+      // determine the parent prefix by fetching the
+      // prefix id of the parent directory
+      Preconditions.checkNotNull(fileStatusInfo);

Review Comment:
   checkNotNull can throw NPE, which is a RuntimeException and can leads to retries. Instead of that, can you throw OMException explicitly. Another idea is, return empty string to the caller and the caller can handle this special case.
   
   ```
   2022-05-26 10:37:31,235 [Time-limited test] INFO  retry.RetryInvocationHandler (RetryInvocationHandler.java:log(411)) - com.google.protobuf.ServiceException: org.apache.hadoop.ipc.RemoteException(java.lang.NullPointerException): java.lang.NullPointerException
   	at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:889)
   	at org.apache.hadoop.ozone.om.OzoneListStatusHelper.getDbKey(OzoneListStatusHelper.java:)
   ```
   
   
   ```
     public static <T> T checkNotNull(@CheckForNull T reference) {
       if (reference == null) {
         throw new NullPointerException();
       } else {
         return reference;
       }
     }
   ```



-- 
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


[GitHub] [ozone] rakeshadr commented on a diff in pull request #3444: HDDS-6788. Sort Ozone list Status output for FSO buckets.

Posted by GitBox <gi...@apache.org>.
rakeshadr commented on code in PR #3444:
URL: https://github.com/apache/ozone/pull/3444#discussion_r881256917


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,560 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+
+  /**
+   * Interface to get the File Status.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      boolean recursive, String startKey, long numEntries,
+      String clientAddress)
+      throws IOException {
+    Preconditions.checkArgument(!recursive);
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    if (numEntries <= 0) {

Review Comment:
   Its duplicate condition and can be removed.
   Refer condition: [KeyManagerImpl.java#L1501](https://github.com/apache/ozone/blob/master/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/KeyManagerImpl.java#L1501)



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,560 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+
+  /**
+   * Interface to get the File Status.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      boolean recursive, String startKey, long numEntries,

Review Comment:
   `recursive` argument can be removed, its being validated as false.  
   `Preconditions.checkArgument(!recursive);`
   
   Please remove the argument.



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/KeyManagerImpl.java:
##########
@@ -1500,9 +1500,22 @@ public List<OzoneFileStatus> listStatus(OmKeyArgs args, boolean recursive,
     if (numEntries <= 0) {
       return fileStatusList;
     }
+
+    boolean useNewIterator = true;

Review Comment:
   `useNewIterator` is always true. Please remove it as there is no logic around it.



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,560 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+
+  /**
+   * Interface to get the File Status.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      boolean recursive, String startKey, long numEntries,
+      String clientAddress)
+      throws IOException {
+    Preconditions.checkArgument(!recursive);
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    if (numEntries <= 0) {
+      return new ArrayList<>();
+    }
+
+    boolean listKeysMode = false;
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    /**
+     * a) If the keyname is a file just return one entry
+     * b) if the keyname is root, then return the value of the bucket
+     * c) if the keyname is a different bucket than root,
+     * fetch the direcoty parent id
+     *
+     * if the startkey exists
+     * a) check the start key is a child ot key, else return emptry list
+     * b) chekc if the start key is a child chil of keynae,

Review Comment:
   Typo:  Please fix it.
   ```
   child ot key
   chekc if the start key is a child chil of keynae,
   neatest key
   ```



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,560 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+
+  /**
+   * Interface to get the File Status.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      boolean recursive, String startKey, long numEntries,
+      String clientAddress)
+      throws IOException {
+    Preconditions.checkArgument(!recursive);
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    if (numEntries <= 0) {
+      return new ArrayList<>();
+    }
+
+    boolean listKeysMode = false;
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    /**
+     * a) If the keyname is a file just return one entry
+     * b) if the keyname is root, then return the value of the bucket
+     * c) if the keyname is a different bucket than root,
+     * fetch the direcoty parent id
+     *
+     * if the startkey exists
+     * a) check the start key is a child ot key, else return emptry list
+     * b) chekc if the start key is a child chil of keynae,
+     * then reset the key to parent of start key
+     * c) if start key is non existent then seek to the neatest key
+     * d) if the keyname is not a dir or a file, it can either be
+     * invalid name or a prefix path
+     *     in case, this is called as part of listStatus fail as the
+     *     real dir/file should exist
+     *     else, try to find the parent of keyname and use that as the prefix,
+     *     use the rest of the path to construct prefix path
+     */
+
+    String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
+    OmBucketInfo omBucketInfo =
+        metadataManager.getBucketTable().get(bucketKey);
+    if (omBucketInfo == null) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+            "Returns empty list", startKey, keyName);
+      }
+      return new ArrayList<>();
+    }
+
+    boolean pathNameChanged = false;
+    if (StringUtils.isNotBlank(startKey)) {
+      if (StringUtils.isNotBlank(keyName)) {
+        if (!listKeysMode && !OzoneFSUtils.isImmediateChild(keyName, startKey)) {

Review Comment:
   `listKeysMode` - describe the purpose of this flag. If not required please remove it.



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,560 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+
+  /**
+   * Interface to get the File Status.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      boolean recursive, String startKey, long numEntries,
+      String clientAddress)
+      throws IOException {
+    Preconditions.checkArgument(!recursive);
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    if (numEntries <= 0) {
+      return new ArrayList<>();
+    }
+
+    boolean listKeysMode = false;
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    /**
+     * a) If the keyname is a file just return one entry
+     * b) if the keyname is root, then return the value of the bucket
+     * c) if the keyname is a different bucket than root,
+     * fetch the direcoty parent id
+     *
+     * if the startkey exists
+     * a) check the start key is a child ot key, else return emptry list
+     * b) chekc if the start key is a child chil of keynae,
+     * then reset the key to parent of start key
+     * c) if start key is non existent then seek to the neatest key
+     * d) if the keyname is not a dir or a file, it can either be
+     * invalid name or a prefix path
+     *     in case, this is called as part of listStatus fail as the
+     *     real dir/file should exist
+     *     else, try to find the parent of keyname and use that as the prefix,
+     *     use the rest of the path to construct prefix path
+     */
+
+    String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
+    OmBucketInfo omBucketInfo =
+        metadataManager.getBucketTable().get(bucketKey);
+    if (omBucketInfo == null) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("StartKey {} is not an immediate child of keyName {}. " +

Review Comment:
   Log message is not reflecting the null bucket info condition. Please modify the message.



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,560 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+
+  /**
+   * Interface to get the File Status.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      boolean recursive, String startKey, long numEntries,
+      String clientAddress)
+      throws IOException {
+    Preconditions.checkArgument(!recursive);
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    if (numEntries <= 0) {
+      return new ArrayList<>();
+    }
+
+    boolean listKeysMode = false;
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    /**
+     * a) If the keyname is a file just return one entry
+     * b) if the keyname is root, then return the value of the bucket
+     * c) if the keyname is a different bucket than root,
+     * fetch the direcoty parent id
+     *
+     * if the startkey exists
+     * a) check the start key is a child ot key, else return emptry list
+     * b) chekc if the start key is a child chil of keynae,
+     * then reset the key to parent of start key
+     * c) if start key is non existent then seek to the neatest key
+     * d) if the keyname is not a dir or a file, it can either be
+     * invalid name or a prefix path
+     *     in case, this is called as part of listStatus fail as the
+     *     real dir/file should exist
+     *     else, try to find the parent of keyname and use that as the prefix,
+     *     use the rest of the path to construct prefix path
+     */
+
+    String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
+    OmBucketInfo omBucketInfo =
+        metadataManager.getBucketTable().get(bucketKey);
+    if (omBucketInfo == null) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+            "Returns empty list", startKey, keyName);
+      }
+      return new ArrayList<>();
+    }
+
+    boolean pathNameChanged = false;
+    if (StringUtils.isNotBlank(startKey)) {
+      if (StringUtils.isNotBlank(keyName)) {
+        if (!listKeysMode && !OzoneFSUtils.isImmediateChild(keyName, startKey)) {
+          if (LOG.isDebugEnabled()) {
+            LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+                "Returns empty list", startKey, keyName);
+          }
+          return new ArrayList<>();
+        }
+      } else {
+        keyName = OzoneFSUtils.getParentDir(startKey);
+        prefixKey = keyName;
+        pathNameChanged = true;
+      }
+    }
+
+    OzoneFileStatus fileStatus = null;
+    if (pathNameChanged) {
+      OmKeyArgs startKeyArgs = args.toBuilder()
+          .setKeyName(keyName)
+          .setSortDatanodesInPipeline(false)
+          .build();
+       fileStatus = getStatusHelper.apply(startKeyArgs,
+           null, true);
+    } else {
+      fileStatus =
+          getStatusHelper.apply(args, clientAddress, listKeysMode);
+    }
+
+    String dbPrefixKey;
+    if (fileStatus == null) {
+      dbPrefixKey = getDbKey(keyName, args, omBucketInfo);
+      prefixKey = OzoneFSUtils.getParentDir(keyName);
+    } else {
+      if (fileStatus.isFile()) {
+        return Collections.singletonList(fileStatus);
+      }
+
+      long id = getId(fileStatus, omBucketInfo);
+      dbPrefixKey = metadataManager.getOzonePathKey(id, "");
+    }
+
+    String startKeyPrefix =
+        Strings.isNullOrEmpty(startKey) ? "" :
+            getDbKey(startKey, args, omBucketInfo);
+
+    TreeMap<String, OzoneFileStatus> map = new TreeMap<>();
+
+    BucketLayout bucketLayout = omBucketInfo.getBucketLayout();
+    try (MinHeapIterator heapIterator =
+             new MinHeapIterator(metadataManager, dbPrefixKey, bucketLayout,
+                 startKeyPrefix, volumeName, bucketName)) {
+
+      while (map.size() < numEntries && heapIterator.hasNext()) {
+        HeapEntry entry = heapIterator.next();
+        OzoneFileStatus status = entry.getStatus(prefixKey,
+            scmBlockSize, volumeName, bucketName, args);
+        LOG.info("returning status:{} keyname:{} startkey:{} numEntries:{}",
+            status, prefixKey, startKey, numEntries);
+        map.put(entry.key, status);
+      }
+    }
+
+    return map.values();
+  }
+
+
+  private String getDbKey(String key, OmKeyArgs args,
+                          OmBucketInfo omBucketInfo) throws IOException {
+    long startKeyParentId;
+    String parent = OzoneFSUtils.getParentDir(key);
+
+    OmKeyArgs startKeyArgs = args.toBuilder()
+        .setKeyName(parent)
+        .setSortDatanodesInPipeline(false)
+        .build();
+    OzoneFileStatus fileStatusInfo = getStatusHelper.apply(startKeyArgs,
+        null, true);
+    Preconditions.checkNotNull(fileStatusInfo);
+    startKeyParentId = getId(fileStatusInfo, omBucketInfo);
+    return metadataManager.
+        getOzonePathKey(startKeyParentId, OzoneFSUtils.getFileName(key));
+  }
+
+  private long getId(OzoneFileStatus fileStatus, OmBucketInfo omBucketInfo) {
+    if (fileStatus.getKeyInfo() != null) {
+      return fileStatus.getKeyInfo().getObjectID();
+    } else {
+      // assert root is null
+      // list root directory.
+      return omBucketInfo.getObjectID();
+    }
+  }
+
+  /**
+   * Enum of types of entries.
+   */
+  public enum EntryType {
+    DIR_CACHE,
+    FILE_CACHE,
+    RAW_FILE_DB,
+    RAW_DIR_DB;
+
+    public boolean isDir() {
+      switch (this) {
+      case DIR_CACHE:
+      case RAW_DIR_DB:
+        return true;
+      case FILE_CACHE:
+      case RAW_FILE_DB:
+        return false;
+      default:
+        throw new IllegalArgumentException();
+      }
+    }
+  }
+
+  /**
+   * Entry which is added to heap.
+   * @param <T>
+   */
+  private static class HeapEntry<T extends WithParentObjectId>
+      implements Comparable<HeapEntry> {
+    private final EntryType entryType;
+    private final String key;
+    private final T value;
+
+    HeapEntry(EntryType entryType, String key, T value) {
+      this.entryType = entryType;
+      this.key = key;
+      this.value = value;
+    }
+
+    public int compareTo(HeapEntry other) {
+      return this.key.compareTo(other.key);
+    }
+
+    public boolean equals(Object other) {
+      if (other == null) {
+        return false;
+      }
+
+      if (!(other instanceof HeapEntry)) {
+        return false;
+      }
+
+
+      HeapEntry that = (HeapEntry) other;
+      return this.key.equals(that.key);
+    }
+
+    public int hashCode() {
+      return key.hashCode();
+    }
+
+    public String getKey() {
+      return key;
+    }
+
+    public OzoneFileStatus getStatus(String prefixPath, long scmBlockSize,
+                                     String volumeName, String bucketName,
+                                     OmKeyArgs args) {

Review Comment:
   Remove `OmKeyArgs args`, which is unused.



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,560 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+
+  /**
+   * Interface to get the File Status.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      boolean recursive, String startKey, long numEntries,
+      String clientAddress)
+      throws IOException {
+    Preconditions.checkArgument(!recursive);
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    if (numEntries <= 0) {
+      return new ArrayList<>();
+    }
+
+    boolean listKeysMode = false;
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    /**
+     * a) If the keyname is a file just return one entry
+     * b) if the keyname is root, then return the value of the bucket
+     * c) if the keyname is a different bucket than root,
+     * fetch the direcoty parent id
+     *
+     * if the startkey exists
+     * a) check the start key is a child ot key, else return emptry list
+     * b) chekc if the start key is a child chil of keynae,
+     * then reset the key to parent of start key
+     * c) if start key is non existent then seek to the neatest key
+     * d) if the keyname is not a dir or a file, it can either be
+     * invalid name or a prefix path
+     *     in case, this is called as part of listStatus fail as the
+     *     real dir/file should exist
+     *     else, try to find the parent of keyname and use that as the prefix,
+     *     use the rest of the path to construct prefix path
+     */
+
+    String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
+    OmBucketInfo omBucketInfo =
+        metadataManager.getBucketTable().get(bucketKey);
+    if (omBucketInfo == null) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+            "Returns empty list", startKey, keyName);
+      }
+      return new ArrayList<>();
+    }
+
+    boolean pathNameChanged = false;
+    if (StringUtils.isNotBlank(startKey)) {
+      if (StringUtils.isNotBlank(keyName)) {
+        if (!listKeysMode && !OzoneFSUtils.isImmediateChild(keyName, startKey)) {
+          if (LOG.isDebugEnabled()) {
+            LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+                "Returns empty list", startKey, keyName);
+          }
+          return new ArrayList<>();
+        }
+      } else {
+        keyName = OzoneFSUtils.getParentDir(startKey);
+        prefixKey = keyName;
+        pathNameChanged = true;
+      }
+    }
+
+    OzoneFileStatus fileStatus = null;
+    if (pathNameChanged) {
+      OmKeyArgs startKeyArgs = args.toBuilder()
+          .setKeyName(keyName)
+          .setSortDatanodesInPipeline(false)
+          .build();
+       fileStatus = getStatusHelper.apply(startKeyArgs,
+           null, true);
+    } else {
+      fileStatus =
+          getStatusHelper.apply(args, clientAddress, listKeysMode);
+    }
+
+    String dbPrefixKey;
+    if (fileStatus == null) {
+      dbPrefixKey = getDbKey(keyName, args, omBucketInfo);
+      prefixKey = OzoneFSUtils.getParentDir(keyName);
+    } else {
+      if (fileStatus.isFile()) {
+        return Collections.singletonList(fileStatus);
+      }
+
+      long id = getId(fileStatus, omBucketInfo);
+      dbPrefixKey = metadataManager.getOzonePathKey(id, "");
+    }
+
+    String startKeyPrefix =
+        Strings.isNullOrEmpty(startKey) ? "" :
+            getDbKey(startKey, args, omBucketInfo);
+
+    TreeMap<String, OzoneFileStatus> map = new TreeMap<>();
+
+    BucketLayout bucketLayout = omBucketInfo.getBucketLayout();
+    try (MinHeapIterator heapIterator =
+             new MinHeapIterator(metadataManager, dbPrefixKey, bucketLayout,
+                 startKeyPrefix, volumeName, bucketName)) {
+
+      while (map.size() < numEntries && heapIterator.hasNext()) {
+        HeapEntry entry = heapIterator.next();
+        OzoneFileStatus status = entry.getStatus(prefixKey,
+            scmBlockSize, volumeName, bucketName, args);
+        LOG.info("returning status:{} keyname:{} startkey:{} numEntries:{}",
+            status, prefixKey, startKey, numEntries);
+        map.put(entry.key, status);
+      }
+    }
+
+    return map.values();
+  }
+
+
+  private String getDbKey(String key, OmKeyArgs args,
+                          OmBucketInfo omBucketInfo) throws IOException {
+    long startKeyParentId;
+    String parent = OzoneFSUtils.getParentDir(key);
+
+    OmKeyArgs startKeyArgs = args.toBuilder()
+        .setKeyName(parent)
+        .setSortDatanodesInPipeline(false)
+        .build();
+    OzoneFileStatus fileStatusInfo = getStatusHelper.apply(startKeyArgs,
+        null, true);
+    Preconditions.checkNotNull(fileStatusInfo);
+    startKeyParentId = getId(fileStatusInfo, omBucketInfo);
+    return metadataManager.
+        getOzonePathKey(startKeyParentId, OzoneFSUtils.getFileName(key));
+  }
+
+  private long getId(OzoneFileStatus fileStatus, OmBucketInfo omBucketInfo) {
+    if (fileStatus.getKeyInfo() != null) {
+      return fileStatus.getKeyInfo().getObjectID();
+    } else {
+      // assert root is null
+      // list root directory.
+      return omBucketInfo.getObjectID();
+    }
+  }
+
+  /**
+   * Enum of types of entries.
+   */
+  public enum EntryType {
+    DIR_CACHE,
+    FILE_CACHE,
+    RAW_FILE_DB,
+    RAW_DIR_DB;
+
+    public boolean isDir() {
+      switch (this) {
+      case DIR_CACHE:
+      case RAW_DIR_DB:
+        return true;
+      case FILE_CACHE:
+      case RAW_FILE_DB:
+        return false;
+      default:
+        throw new IllegalArgumentException();
+      }
+    }
+  }
+
+  /**
+   * Entry which is added to heap.
+   * @param <T>
+   */
+  private static class HeapEntry<T extends WithParentObjectId>
+      implements Comparable<HeapEntry> {
+    private final EntryType entryType;
+    private final String key;
+    private final T value;
+
+    HeapEntry(EntryType entryType, String key, T value) {
+      this.entryType = entryType;
+      this.key = key;
+      this.value = value;
+    }
+
+    public int compareTo(HeapEntry other) {
+      return this.key.compareTo(other.key);
+    }
+
+    public boolean equals(Object other) {
+      if (other == null) {
+        return false;
+      }
+
+      if (!(other instanceof HeapEntry)) {
+        return false;
+      }
+
+
+      HeapEntry that = (HeapEntry) other;
+      return this.key.equals(that.key);
+    }
+
+    public int hashCode() {
+      return key.hashCode();
+    }
+
+    public String getKey() {
+      return key;
+    }
+
+    public OzoneFileStatus getStatus(String prefixPath, long scmBlockSize,
+                                     String volumeName, String bucketName,
+                                     OmKeyArgs args) {
+      OmKeyInfo keyInfo;
+      if (entryType.isDir()) {
+        OmDirectoryInfo dirInfo = (OmDirectoryInfo) value;
+        String dirName = OMFileRequest.getAbsolutePath(prefixPath,
+            dirInfo.getName());
+        keyInfo = OMFileRequest.getOmKeyInfo(volumeName,
+            bucketName, dirInfo, dirName);
+      } else {
+        keyInfo = (OmKeyInfo) value;
+        keyInfo.setFileName(keyInfo.getKeyName());
+        String fullKeyPath = OMFileRequest.getAbsolutePath(prefixPath,
+            keyInfo.getKeyName());
+        keyInfo.setKeyName(fullKeyPath);
+      }
+      return new OzoneFileStatus(keyInfo, scmBlockSize, entryType.isDir());
+    }
+  }
+
+  /**
+   * Iterator class for Ozone keys.
+   */
+  public interface OzoneKeyIterator extends
+      Iterator<HeapEntry<? extends WithParentObjectId>>, Closeable {
+  }
+
+  /**
+   * Raw iterator over db tables.
+   * @param <T>
+   */
+  private static class RawIter<T extends WithParentObjectId>
+      implements OzoneKeyIterator {
+
+    private final EntryType iterType;
+    private final String prefixKey;
+
+    private final TableIterator<String,
+        ? extends Table.KeyValue<String, T>> tableIterator;
+    private final Set<String> cacheDeletedKeySet;
+    private HeapEntry currentKey;
+
+    RawIter(EntryType iterType, TableIterator<String,
+        ? extends Table.KeyValue<String, T>> tableIterator,
+            String prefixKey, String startKey,
+            Set<String> cacheDeletedKeySet) throws IOException {
+      this.iterType = iterType;
+      this.tableIterator = tableIterator;
+      this.prefixKey = prefixKey;
+      this.cacheDeletedKeySet = cacheDeletedKeySet;
+      this.currentKey = null;
+      if (!StringUtils.isBlank(prefixKey)) {
+        tableIterator.seek(prefixKey);
+      }
+
+      if (!StringUtils.isBlank(startKey)) {
+        tableIterator.seek(startKey);
+      }
+
+      getNextKey();
+    }
+
+    private void getNextKey() throws IOException {
+      while (tableIterator.hasNext() && currentKey == null) {
+        Table.KeyValue<String, T> entry = tableIterator.next();
+        String entryKey = entry.getKey();
+        if (entryKey.startsWith(prefixKey)) {
+          if (!cacheDeletedKeySet.contains(entryKey)) {
+            currentKey = new HeapEntry(iterType, entryKey, entry.getValue());
+          }
+        } else {
+          break;
+        }
+      }
+    }
+
+    public boolean hasNext() {
+      try {
+        getNextKey();
+      } catch (Throwable t) {
+        throw new NoSuchElementException();
+      }
+      return currentKey != null;
+    }
+
+    public HeapEntry next() {
+      try {
+        getNextKey();
+      } catch (Throwable t) {
+        throw new NoSuchElementException();
+      }
+      HeapEntry ret = currentKey;
+      currentKey = null;
+
+      return ret;
+    }
+
+    public void close() {
+
+    }
+  }
+
+  /**
+   * Iterator for Cache for the database.
+   * @param <T>
+   */
+  private static class CacheIter< T extends WithParentObjectId>
+      implements OzoneKeyIterator {
+    private final Set<String> cacheDeletedKeySet;
+    private final Map<String, T> cacheKeyMap;
+
+    private final Iterator<Map.Entry<String, T>>
+        cacheCreatedKeyIter;
+
+    private final Iterator<Map.Entry<CacheKey<String>, CacheValue<T>>>
+        cacheIter;
+
+    private final String prefixKey;
+    private final String startKey;
+
+
+    private final EntryType entryType;
+
+    CacheIter(EntryType entryType, Iterator<Map.Entry<CacheKey<String>,
+        CacheValue<T>>> cacheIter, String startKey, String prefixKey) {
+      this.cacheDeletedKeySet = new TreeSet<>();
+      this.cacheKeyMap = new TreeMap<>();
+
+      this.cacheIter = cacheIter;
+      this.startKey = startKey;
+      this.prefixKey = prefixKey;
+      this.entryType = entryType;
+
+      getCacheValues();
+
+      cacheCreatedKeyIter = cacheKeyMap.entrySet().iterator();
+    }
+
+    private void getCacheValues() {
+      while (cacheIter.hasNext()) {
+        Map.Entry<CacheKey<String>, CacheValue<T>> entry =
+            cacheIter.next();
+        String cacheKey = entry.getKey().getCacheKey();
+        T cacheOmInfo = entry.getValue().getCacheValue();
+        // cacheOmKeyInfo is null if an entry is deleted in cache
+        if (cacheOmInfo == null) {
+          cacheDeletedKeySet.add(cacheKey);
+          continue;
+        }
+
+        if (StringUtils.isBlank(startKey)) {
+          // startKey is null or empty, then the seekKeyInDB="1024/"
+          if (cacheKey.startsWith(prefixKey)) {
+            cacheKeyMap.put(cacheKey, cacheOmInfo);
+          }
+        } else {
+          // startKey not empty, then the seekKeyInDB="1024/b" and
+          // seekKeyInDBWithOnlyParentID = "1024/". This is to avoid case of
+          // parentID with "102444" cache entries.
+          // Here, it has to list all the keys after "1024/b" and requires >=0
+          // string comparison.
+          if (cacheKey.startsWith(prefixKey) &&
+              cacheKey.compareTo(startKey) >= 0) {
+            cacheKeyMap.put(cacheKey, cacheOmInfo);
+          }
+        }
+      }
+    }
+
+    public boolean hasNext() {
+      return cacheCreatedKeyIter.hasNext();
+    }
+
+    public HeapEntry next() {
+      Map.Entry<String, T> entry = cacheCreatedKeyIter.next();
+      return new HeapEntry(entryType, entry.getKey(), entry.getValue());
+    }
+
+    public void close() {
+
+    }
+
+    public Set<String> getDeletedKeySet() {
+      return cacheDeletedKeySet;
+    }
+  }
+
+  /**
+   * Implement a min heap iterator to find the smaller
+   * lexicographically sorted string.
+   */
+  private static class MinHeapIterator implements
+      Iterator<HeapEntry<? extends WithParentObjectId>>, Closeable {
+
+    private final PriorityQueue<HeapEntry
+        <? extends WithParentObjectId>> minHeap = new PriorityQueue<>();
+    private final ArrayList<Iterator<HeapEntry
+        <? extends WithParentObjectId>>> iterators = new ArrayList<>();
+
+    private final RawIter<OmDirectoryInfo> rawDirIter;
+    private final RawIter<OmKeyInfo> rawFileIter;
+
+    private final CacheIter<OmKeyInfo> cacheFileIter;
+    private final CacheIter<OmDirectoryInfo> cacheDirIter;
+
+    MinHeapIterator(OMMetadataManager omMetadataManager, String prefixKey,
+                    BucketLayout bucketLayout, String startKey,
+                    String volumeName, String bucketName) throws IOException {
+
+      omMetadataManager.getLock().acquireReadLock(BUCKET_LOCK, volumeName,
+          bucketName);
+      cacheDirIter =
+          new CacheIter<>(EntryType.DIR_CACHE,
+              omMetadataManager.getDirectoryTable().cacheIterator(),
+              startKey, prefixKey);
+
+      cacheFileIter =
+          new CacheIter<>(EntryType.FILE_CACHE,
+              omMetadataManager.getKeyTable(bucketLayout).cacheIterator(),
+              startKey, prefixKey);
+
+      rawDirIter =
+          new RawIter<>(EntryType.RAW_DIR_DB,
+              omMetadataManager.getDirectoryTable().iterator(),
+              prefixKey, startKey, cacheDirIter.getDeletedKeySet());
+
+      rawFileIter =
+          new RawIter<>(EntryType.RAW_FILE_DB,
+              omMetadataManager.getKeyTable(bucketLayout).iterator(),
+              prefixKey, startKey, cacheFileIter.getDeletedKeySet());
+
+      omMetadataManager.getLock().releaseReadLock(BUCKET_LOCK, volumeName,
+          bucketName);
+
+      iterators.add(EntryType.DIR_CACHE.ordinal(), cacheDirIter);
+      iterators.add(EntryType.FILE_CACHE.ordinal(), cacheFileIter);
+      iterators.add(EntryType.RAW_FILE_DB.ordinal(), rawFileIter);
+      iterators.add(EntryType.RAW_DIR_DB.ordinal(), rawDirIter);
+      insertFirstElement();
+
+    }
+
+    public void insertFirstElement() {
+      for (Iterator<HeapEntry<? extends WithParentObjectId>> iter :
+          iterators) {
+        if (iter.hasNext()) {
+          minHeap.add(iter.next());
+        }
+      }
+    }
+
+    public boolean hasNext() {
+      return !minHeap.isEmpty();
+    }
+
+    public HeapEntry<? extends WithParentObjectId> next() {
+      HeapEntry<? extends WithParentObjectId> heapEntry = minHeap.remove();
+      Iterator<HeapEntry<? extends WithParentObjectId>> iter =
+          iterators.get(heapEntry.entryType.ordinal());
+      if (iter.hasNext()) {
+        minHeap.add(iter.next());
+      }
+
+      return heapEntry;
+    }
+
+    public void close() throws IOException {
+    }

Review Comment:
   Please close all the iterators and which inturn should close `tableIterator.close();`



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,560 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+
+  /**
+   * Interface to get the File Status.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      boolean recursive, String startKey, long numEntries,
+      String clientAddress)
+      throws IOException {
+    Preconditions.checkArgument(!recursive);
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    if (numEntries <= 0) {
+      return new ArrayList<>();
+    }
+
+    boolean listKeysMode = false;
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    /**
+     * a) If the keyname is a file just return one entry
+     * b) if the keyname is root, then return the value of the bucket
+     * c) if the keyname is a different bucket than root,
+     * fetch the direcoty parent id
+     *
+     * if the startkey exists
+     * a) check the start key is a child ot key, else return emptry list
+     * b) chekc if the start key is a child chil of keynae,
+     * then reset the key to parent of start key
+     * c) if start key is non existent then seek to the neatest key
+     * d) if the keyname is not a dir or a file, it can either be
+     * invalid name or a prefix path
+     *     in case, this is called as part of listStatus fail as the
+     *     real dir/file should exist
+     *     else, try to find the parent of keyname and use that as the prefix,
+     *     use the rest of the path to construct prefix path
+     */
+
+    String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
+    OmBucketInfo omBucketInfo =
+        metadataManager.getBucketTable().get(bucketKey);
+    if (omBucketInfo == null) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+            "Returns empty list", startKey, keyName);
+      }
+      return new ArrayList<>();
+    }
+
+    boolean pathNameChanged = false;
+    if (StringUtils.isNotBlank(startKey)) {
+      if (StringUtils.isNotBlank(keyName)) {
+        if (!listKeysMode && !OzoneFSUtils.isImmediateChild(keyName, startKey)) {

Review Comment:
   Need to handle a special case, where both keyPrefix && startKey are same.
   
   For ex: #listStatus("a1/a11", "a1/a11");
   Since `
   OzoneFSUtils.isImmediateChild(keyName, startKey)` is false, will get empty list. Instead it should return all the keys after "a1/a11"
   



-- 
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


[GitHub] [ozone] mukul1987 commented on a diff in pull request #3444: HDDS-6788. Sort Ozone list Status output for FSO buckets.

Posted by GitBox <gi...@apache.org>.
mukul1987 commented on code in PR #3444:
URL: https://github.com/apache/ozone/pull/3444#discussion_r881711200


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,560 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+
+  /**
+   * Interface to get the File Status.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      boolean recursive, String startKey, long numEntries,
+      String clientAddress)
+      throws IOException {
+    Preconditions.checkArgument(!recursive);
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    if (numEntries <= 0) {
+      return new ArrayList<>();
+    }
+
+    boolean listKeysMode = false;
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    /**
+     * a) If the keyname is a file just return one entry
+     * b) if the keyname is root, then return the value of the bucket
+     * c) if the keyname is a different bucket than root,
+     * fetch the direcoty parent id
+     *
+     * if the startkey exists
+     * a) check the start key is a child ot key, else return emptry list
+     * b) chekc if the start key is a child chil of keynae,
+     * then reset the key to parent of start key
+     * c) if start key is non existent then seek to the neatest key
+     * d) if the keyname is not a dir or a file, it can either be
+     * invalid name or a prefix path
+     *     in case, this is called as part of listStatus fail as the
+     *     real dir/file should exist
+     *     else, try to find the parent of keyname and use that as the prefix,
+     *     use the rest of the path to construct prefix path
+     */
+
+    String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
+    OmBucketInfo omBucketInfo =
+        metadataManager.getBucketTable().get(bucketKey);
+    if (omBucketInfo == null) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+            "Returns empty list", startKey, keyName);
+      }
+      return new ArrayList<>();
+    }
+
+    boolean pathNameChanged = false;
+    if (StringUtils.isNotBlank(startKey)) {
+      if (StringUtils.isNotBlank(keyName)) {
+        if (!listKeysMode && !OzoneFSUtils.isImmediateChild(keyName, startKey)) {
+          if (LOG.isDebugEnabled()) {
+            LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+                "Returns empty list", startKey, keyName);
+          }
+          return new ArrayList<>();
+        }
+      } else {
+        keyName = OzoneFSUtils.getParentDir(startKey);
+        prefixKey = keyName;
+        pathNameChanged = true;
+      }
+    }
+
+    OzoneFileStatus fileStatus = null;
+    if (pathNameChanged) {
+      OmKeyArgs startKeyArgs = args.toBuilder()
+          .setKeyName(keyName)
+          .setSortDatanodesInPipeline(false)
+          .build();
+       fileStatus = getStatusHelper.apply(startKeyArgs,
+           null, true);
+    } else {
+      fileStatus =
+          getStatusHelper.apply(args, clientAddress, listKeysMode);
+    }
+
+    String dbPrefixKey;
+    if (fileStatus == null) {
+      dbPrefixKey = getDbKey(keyName, args, omBucketInfo);
+      prefixKey = OzoneFSUtils.getParentDir(keyName);
+    } else {
+      if (fileStatus.isFile()) {
+        return Collections.singletonList(fileStatus);
+      }
+
+      long id = getId(fileStatus, omBucketInfo);
+      dbPrefixKey = metadataManager.getOzonePathKey(id, "");
+    }
+
+    String startKeyPrefix =
+        Strings.isNullOrEmpty(startKey) ? "" :
+            getDbKey(startKey, args, omBucketInfo);
+
+    TreeMap<String, OzoneFileStatus> map = new TreeMap<>();
+
+    BucketLayout bucketLayout = omBucketInfo.getBucketLayout();
+    try (MinHeapIterator heapIterator =
+             new MinHeapIterator(metadataManager, dbPrefixKey, bucketLayout,
+                 startKeyPrefix, volumeName, bucketName)) {
+
+      while (map.size() < numEntries && heapIterator.hasNext()) {
+        HeapEntry entry = heapIterator.next();
+        OzoneFileStatus status = entry.getStatus(prefixKey,
+            scmBlockSize, volumeName, bucketName, args);
+        LOG.info("returning status:{} keyname:{} startkey:{} numEntries:{}",
+            status, prefixKey, startKey, numEntries);
+        map.put(entry.key, status);
+      }
+    }
+
+    return map.values();
+  }
+
+
+  private String getDbKey(String key, OmKeyArgs args,
+                          OmBucketInfo omBucketInfo) throws IOException {
+    long startKeyParentId;
+    String parent = OzoneFSUtils.getParentDir(key);
+
+    OmKeyArgs startKeyArgs = args.toBuilder()
+        .setKeyName(parent)
+        .setSortDatanodesInPipeline(false)
+        .build();
+    OzoneFileStatus fileStatusInfo = getStatusHelper.apply(startKeyArgs,
+        null, true);
+    Preconditions.checkNotNull(fileStatusInfo);
+    startKeyParentId = getId(fileStatusInfo, omBucketInfo);
+    return metadataManager.
+        getOzonePathKey(startKeyParentId, OzoneFSUtils.getFileName(key));
+  }
+
+  private long getId(OzoneFileStatus fileStatus, OmBucketInfo omBucketInfo) {
+    if (fileStatus.getKeyInfo() != null) {
+      return fileStatus.getKeyInfo().getObjectID();
+    } else {
+      // assert root is null
+      // list root directory.
+      return omBucketInfo.getObjectID();
+    }
+  }
+
+  /**
+   * Enum of types of entries.
+   */
+  public enum EntryType {
+    DIR_CACHE,
+    FILE_CACHE,
+    RAW_FILE_DB,
+    RAW_DIR_DB;
+
+    public boolean isDir() {
+      switch (this) {
+      case DIR_CACHE:
+      case RAW_DIR_DB:
+        return true;
+      case FILE_CACHE:
+      case RAW_FILE_DB:
+        return false;
+      default:
+        throw new IllegalArgumentException();
+      }
+    }
+  }
+
+  /**
+   * Entry which is added to heap.
+   * @param <T>
+   */
+  private static class HeapEntry<T extends WithParentObjectId>
+      implements Comparable<HeapEntry> {
+    private final EntryType entryType;
+    private final String key;
+    private final T value;
+
+    HeapEntry(EntryType entryType, String key, T value) {
+      this.entryType = entryType;
+      this.key = key;
+      this.value = value;
+    }
+
+    public int compareTo(HeapEntry other) {
+      return this.key.compareTo(other.key);
+    }
+
+    public boolean equals(Object other) {
+      if (other == null) {
+        return false;
+      }
+
+      if (!(other instanceof HeapEntry)) {
+        return false;
+      }
+
+
+      HeapEntry that = (HeapEntry) other;
+      return this.key.equals(that.key);
+    }
+
+    public int hashCode() {
+      return key.hashCode();
+    }
+
+    public String getKey() {
+      return key;
+    }
+
+    public OzoneFileStatus getStatus(String prefixPath, long scmBlockSize,
+                                     String volumeName, String bucketName,
+                                     OmKeyArgs args) {

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


[GitHub] [ozone] mukul1987 commented on a diff in pull request #3444: HDDS-6788. Sort Ozone list Status output for FSO buckets.

Posted by GitBox <gi...@apache.org>.
mukul1987 commented on code in PR #3444:
URL: https://github.com/apache/ozone/pull/3444#discussion_r882302295


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneListStatusHelper.java:
##########
@@ -0,0 +1,560 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.ozone.om;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.hdds.utils.db.TableIterator;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
+import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils;
+import org.apache.hadoop.ozone.om.helpers.OmKeyArgs;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.BucketLayout;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.WithParentObjectId;
+import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.NoSuchElementException;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Helper class for fetching List Status for a path.
+ */
+public class OzoneListStatusHelper {
+
+  /**
+   * Interface to get the File Status.
+   */
+  @FunctionalInterface
+  public interface GetFileStatusHelper {
+    OzoneFileStatus apply(OmKeyArgs args, String clientAddress,
+                          boolean skipFileNotFoundError) throws IOException;
+  }
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneListStatusHelper.class);
+
+  private final OMMetadataManager metadataManager;
+  private final long scmBlockSize;
+
+  private final GetFileStatusHelper getStatusHelper;
+
+  OzoneListStatusHelper(OMMetadataManager metadataManager, long scmBlockSize,
+                        GetFileStatusHelper func) {
+    this.metadataManager = metadataManager;
+    this.scmBlockSize = scmBlockSize;
+    this.getStatusHelper = func;
+  }
+
+  public Collection<OzoneFileStatus> listStatusFSO(OmKeyArgs args,
+      boolean recursive, String startKey, long numEntries,
+      String clientAddress)
+      throws IOException {
+    Preconditions.checkArgument(!recursive);
+    Preconditions.checkNotNull(args, "Key args can not be null");
+
+    if (numEntries <= 0) {
+      return new ArrayList<>();
+    }
+
+    boolean listKeysMode = false;
+    final String volumeName = args.getVolumeName();
+    final String bucketName = args.getBucketName();
+    String keyName = args.getKeyName();
+    String prefixKey = keyName;
+
+    /**
+     * a) If the keyname is a file just return one entry
+     * b) if the keyname is root, then return the value of the bucket
+     * c) if the keyname is a different bucket than root,
+     * fetch the direcoty parent id
+     *
+     * if the startkey exists
+     * a) check the start key is a child ot key, else return emptry list
+     * b) chekc if the start key is a child chil of keynae,
+     * then reset the key to parent of start key
+     * c) if start key is non existent then seek to the neatest key
+     * d) if the keyname is not a dir or a file, it can either be
+     * invalid name or a prefix path
+     *     in case, this is called as part of listStatus fail as the
+     *     real dir/file should exist
+     *     else, try to find the parent of keyname and use that as the prefix,
+     *     use the rest of the path to construct prefix path
+     */
+
+    String bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
+    OmBucketInfo omBucketInfo =
+        metadataManager.getBucketTable().get(bucketKey);
+    if (omBucketInfo == null) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("StartKey {} is not an immediate child of keyName {}. " +
+            "Returns empty list", startKey, keyName);
+      }
+      return new ArrayList<>();
+    }
+
+    boolean pathNameChanged = false;
+    if (StringUtils.isNotBlank(startKey)) {
+      if (StringUtils.isNotBlank(keyName)) {
+        if (!listKeysMode && !OzoneFSUtils.isImmediateChild(keyName, startKey)) {

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