Search in sources :

Example 16 with BlockStoragePolicySuite

use of org.apache.hadoop.hdfs.server.blockmanagement.BlockStoragePolicySuite in project hbase by apache.

the class HFileSystem method getStoragePolicyForOldHDFSVersion.

/**
 * Before Hadoop 2.8.0, there's no getStoragePolicy method for FileSystem interface, and we need
 * to keep compatible with it. See HADOOP-12161 for more details.
 * @param path Path to get storage policy against
 * @return the storage policy name
 */
private String getStoragePolicyForOldHDFSVersion(Path path) {
    try {
        if (this.fs instanceof DistributedFileSystem) {
            DistributedFileSystem dfs = (DistributedFileSystem) this.fs;
            HdfsFileStatus status = dfs.getClient().getFileInfo(path.toUri().getPath());
            if (null != status) {
                if (unspecifiedStoragePolicyId < 0) {
                    // Get the unspecified id field through reflection to avoid compilation error.
                    // In later version BlockStoragePolicySuite#ID_UNSPECIFIED is moved to
                    // HdfsConstants#BLOCK_STORAGE_POLICY_ID_UNSPECIFIED
                    Field idUnspecified = BlockStoragePolicySuite.class.getField("ID_UNSPECIFIED");
                    unspecifiedStoragePolicyId = idUnspecified.getByte(BlockStoragePolicySuite.class);
                }
                byte storagePolicyId = status.getStoragePolicy();
                if (storagePolicyId != unspecifiedStoragePolicyId) {
                    BlockStoragePolicy[] policies = dfs.getStoragePolicies();
                    for (BlockStoragePolicy policy : policies) {
                        if (policy.getId() == storagePolicyId) {
                            return policy.getName();
                        }
                    }
                }
            }
        }
    } catch (Throwable e) {
        LOG.warn("failed to get block storage policy of [" + path + "]", e);
    }
    return null;
}
Also used : Field(java.lang.reflect.Field) BlockStoragePolicySuite(org.apache.hadoop.hdfs.server.blockmanagement.BlockStoragePolicySuite) HdfsFileStatus(org.apache.hadoop.hdfs.protocol.HdfsFileStatus) BlockStoragePolicy(org.apache.hadoop.hdfs.protocol.BlockStoragePolicy) DistributedFileSystem(org.apache.hadoop.hdfs.DistributedFileSystem)

Aggregations

BlockStoragePolicySuite (org.apache.hadoop.hdfs.server.blockmanagement.BlockStoragePolicySuite)16 BlockStoragePolicy (org.apache.hadoop.hdfs.protocol.BlockStoragePolicy)14 Test (org.junit.Test)13 Path (org.apache.hadoop.fs.Path)7 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 LinkedHashMap (java.util.LinkedHashMap)5 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)2 Field (java.lang.reflect.Field)1 HashSet (java.util.HashSet)1 BlockStoragePolicySpi (org.apache.hadoop.fs.BlockStoragePolicySpi)1 FileAlreadyExistsException (org.apache.hadoop.fs.FileAlreadyExistsException)1 FileSystem (org.apache.hadoop.fs.FileSystem)1 ParentNotDirectoryException (org.apache.hadoop.fs.ParentNotDirectoryException)1 DistributedFileSystem (org.apache.hadoop.hdfs.DistributedFileSystem)1 HdfsAdmin (org.apache.hadoop.hdfs.client.HdfsAdmin)1 Block (org.apache.hadoop.hdfs.protocol.Block)1 HdfsFileStatus (org.apache.hadoop.hdfs.protocol.HdfsFileStatus)1 BlockInfo (org.apache.hadoop.hdfs.server.blockmanagement.BlockInfo)1