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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
