Skip to content

Commit 9b250d7

Browse files
committed
HDFS-6847. Support storage policy on directories and include storage policy in HdfsFileStatus. Contributed by Jing Zhao
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/HDFS-6584@1618416 13f79535-47bb-0310-9956-ffa450edef68
1 parent 37207b7 commit 9b250d7

27 files changed

+338
-100
lines changed

hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ HDFS-6584: Archival Storage
2020
HDFS-6686. Change BlockPlacementPolicy to use fallback when some storage
2121
types are unavailable. (szetszwo)
2222

23+
HDFS-6847. Support storage policy on directories and include storage policy
24+
in HdfsFileStatus. (Jing Zhao via szetszwo)
25+
2326
Trunk (Unreleased)
2427

2528
INCOMPATIBLE CHANGES

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/BlockStoragePolicy.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.apache.commons.logging.LogFactory;
2828
import org.apache.hadoop.classification.InterfaceAudience;
2929
import org.apache.hadoop.conf.Configuration;
30+
import org.apache.hadoop.fs.XAttr;
31+
import org.apache.hadoop.fs.XAttr.NameSpace;
3032

3133
/**
3234
* A block storage policy describes how to select the storage types
@@ -44,9 +46,13 @@ public class BlockStoragePolicy {
4446
= "dfs.block.storage.policy.creation-fallback.";
4547
public static final String DFS_BLOCK_STORAGE_POLICY_REPLICATION_FALLBACK_KEY_PREFIX
4648
= "dfs.block.storage.policy.replication-fallback.";
49+
public static final String STORAGE_POLICY_XATTR_NAME = "bsp";
50+
/** set the namespace to TRUSTED so that only privilege users can access */
51+
public static final NameSpace XAttrNS = NameSpace.TRUSTED;
4752

4853
public static final int ID_BIT_LENGTH = 4;
4954
public static final int ID_MAX = (1 << ID_BIT_LENGTH) - 1;
55+
public static final byte ID_UNSPECIFIED = 0;
5056

5157
/** A block storage policy suite. */
5258
public static class Suite {
@@ -299,6 +305,20 @@ public static Suite readBlockStorageSuite(Configuration conf) {
299305
return new Suite(firstID, policies);
300306
}
301307

308+
public static String buildXAttrName() {
309+
return XAttrNS.toString().toLowerCase() + "." + STORAGE_POLICY_XATTR_NAME;
310+
}
311+
312+
public static XAttr buildXAttr(byte policyId) {
313+
final String name = buildXAttrName();
314+
return XAttrHelper.buildXAttr(name, new byte[] { policyId });
315+
}
316+
317+
public static boolean isStoragePolicyXAttr(XAttr xattr) {
318+
return xattr != null && xattr.getNameSpace() == BlockStoragePolicy.XAttrNS
319+
&& xattr.getName().equals(BlockStoragePolicy.STORAGE_POLICY_XATTR_NAME);
320+
}
321+
302322
private static void throwIllegalArgumentException(String message,
303323
Configuration conf) {
304324
throw new IllegalArgumentException(message + " in "

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,8 +1646,8 @@ public boolean setReplication(String src, short replication)
16461646
}
16471647

16481648
/**
1649-
* Set storage policy for an existing file
1650-
* @param src file name
1649+
* Set storage policy for an existing file/directory
1650+
* @param src file/directory name
16511651
* @param policyName name of the storage policy
16521652
*/
16531653
public void setStoragePolicy(String src, String policyName)

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/protocol/ClientProtocol.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ public boolean setReplication(String src, short replication)
255255
SnapshotAccessControlException, IOException;
256256

257257
/**
258-
* Set the storage policy for an existing file
259-
* @param src Path of an existing file.
258+
* Set the storage policy for a file/directory
259+
* @param src Path of an existing file/directory.
260260
* @param policyName The name of the storage policy
261261
* @throws SnapshotAccessControlException If access is denied
262262
* @throws UnresolvedLinkException if <code>src</code> contains a symlink

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/protocol/HdfsFileStatus.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ public class HdfsFileStatus {
4444
private final String owner;
4545
private final String group;
4646
private final long fileId;
47-
47+
4848
// Used by dir, not including dot and dotdot. Always zero for a regular file.
4949
private final int childrenNum;
50+
private final byte storagePolicy;
5051

5152
public static final byte[] EMPTY_NAME = new byte[0];
5253

@@ -65,9 +66,9 @@ public class HdfsFileStatus {
6566
* @param fileId the file id
6667
*/
6768
public HdfsFileStatus(long length, boolean isdir, int block_replication,
68-
long blocksize, long modification_time, long access_time,
69-
FsPermission permission, String owner, String group,
70-
byte[] symlink, byte[] path, long fileId, int childrenNum) {
69+
long blocksize, long modification_time, long access_time,
70+
FsPermission permission, String owner, String group, byte[] symlink,
71+
byte[] path, long fileId, int childrenNum, byte storagePolicy) {
7172
this.length = length;
7273
this.isdir = isdir;
7374
this.block_replication = (short)block_replication;
@@ -85,6 +86,7 @@ public HdfsFileStatus(long length, boolean isdir, int block_replication,
8586
this.path = path;
8687
this.fileId = fileId;
8788
this.childrenNum = childrenNum;
89+
this.storagePolicy = storagePolicy;
8890
}
8991

9092
/**
@@ -242,6 +244,11 @@ final public int getChildrenNum() {
242244
return childrenNum;
243245
}
244246

247+
/** @return the storage policy id */
248+
public final byte getStoragePolicy() {
249+
return storagePolicy;
250+
}
251+
245252
final public FileStatus makeQualified(URI defaultUri, Path path) {
246253
return new FileStatus(getLen(), isDir(), getReplication(),
247254
getBlockSize(), getModificationTime(),

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/protocol/HdfsLocatedFileStatus.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
@InterfaceStability.Evolving
3535
public class HdfsLocatedFileStatus extends HdfsFileStatus {
3636
private final LocatedBlocks locations;
37-
37+
3838
/**
3939
* Constructor
4040
*
@@ -56,13 +56,13 @@ public HdfsLocatedFileStatus(long length, boolean isdir,
5656
int block_replication, long blocksize, long modification_time,
5757
long access_time, FsPermission permission, String owner, String group,
5858
byte[] symlink, byte[] path, long fileId, LocatedBlocks locations,
59-
int childrenNum) {
59+
int childrenNum, byte storagePolicy) {
6060
super(length, isdir, block_replication, blocksize, modification_time,
6161
access_time, permission, owner, group, symlink, path, fileId,
62-
childrenNum);
62+
childrenNum, storagePolicy);
6363
this.locations = locations;
6464
}
65-
65+
6666
public LocatedBlocks getBlockLocations() {
6767
return locations;
6868
}

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/protocol/SnapshottableDirectoryStatus.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.apache.hadoop.fs.Path;
2626
import org.apache.hadoop.fs.permission.FsPermission;
27+
import org.apache.hadoop.hdfs.BlockStoragePolicy;
2728
import org.apache.hadoop.hdfs.DFSUtil;
2829

2930
/**
@@ -61,7 +62,7 @@ public SnapshottableDirectoryStatus(long modification_time, long access_time,
6162
int snapshotNumber, int snapshotQuota, byte[] parentFullPath) {
6263
this.dirStatus = new HdfsFileStatus(0, true, 0, 0, modification_time,
6364
access_time, permission, owner, group, null, localName, inodeId,
64-
childrenNum);
65+
childrenNum, BlockStoragePolicy.ID_UNSPECIFIED);
6566
this.snapshotNumber = snapshotNumber;
6667
this.snapshotQuota = snapshotQuota;
6768
this.parentFullPath = parentFullPath;

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/protocolPB/PBHelper.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.apache.hadoop.fs.permission.FsPermission;
4343
import org.apache.hadoop.ha.HAServiceProtocol.HAServiceState;
4444
import org.apache.hadoop.ha.proto.HAServiceProtocolProtos;
45+
import org.apache.hadoop.hdfs.BlockStoragePolicy;
4546
import org.apache.hadoop.hdfs.DFSUtil;
4647
import org.apache.hadoop.hdfs.StorageType;
4748
import org.apache.hadoop.hdfs.protocol.Block;
@@ -1315,7 +1316,9 @@ public static HdfsFileStatus convert(HdfsFileStatusProto fs) {
13151316
fs.getPath().toByteArray(),
13161317
fs.hasFileId()? fs.getFileId(): INodeId.GRANDFATHER_INODE_ID,
13171318
fs.hasLocations() ? PBHelper.convert(fs.getLocations()) : null,
1318-
fs.hasChildrenNum() ? fs.getChildrenNum() : -1);
1319+
fs.hasChildrenNum() ? fs.getChildrenNum() : -1,
1320+
fs.hasStoragePolicy() ? (byte) fs.getStoragePolicy()
1321+
: BlockStoragePolicy.ID_UNSPECIFIED);
13191322
}
13201323

13211324
public static SnapshottableDirectoryStatus convert(
@@ -1361,12 +1364,14 @@ public static HdfsFileStatusProto convert(HdfsFileStatus fs) {
13611364
setGroup(fs.getGroup()).
13621365
setFileId(fs.getFileId()).
13631366
setChildrenNum(fs.getChildrenNum()).
1364-
setPath(ByteString.copyFrom(fs.getLocalNameInBytes()));
1367+
setPath(ByteString.copyFrom(fs.getLocalNameInBytes())).
1368+
setStoragePolicy(fs.getStoragePolicy());
13651369
if (fs.isSymlink()) {
13661370
builder.setSymlink(ByteString.copyFrom(fs.getSymlinkInBytes()));
13671371
}
13681372
if (fs instanceof HdfsLocatedFileStatus) {
1369-
LocatedBlocks locations = ((HdfsLocatedFileStatus)fs).getBlockLocations();
1373+
final HdfsLocatedFileStatus lfs = (HdfsLocatedFileStatus) fs;
1374+
LocatedBlocks locations = lfs.getBlockLocations();
13701375
if (locations != null) {
13711376
builder.setLocations(PBHelper.convert(locations));
13721377
}

0 commit comments

Comments
 (0)