3131import org .apache .hadoop .HadoopIllegalArgumentException ;
3232import org .apache .hadoop .classification .InterfaceAudience ;
3333import org .apache .hadoop .fs .permission .AclEntry ;
34- import org .apache .hadoop .fs .permission .AclEntryScope ;
35- import org .apache .hadoop .fs .permission .AclEntryType ;
36- import org .apache .hadoop .fs .permission .FsAction ;
37- import org .apache .hadoop .fs .permission .FsPermission ;
3834import org .apache .hadoop .fs .permission .PermissionStatus ;
3935import org .apache .hadoop .fs .StorageType ;
4036import org .apache .hadoop .fs .XAttr ;
6056import org .apache .hadoop .hdfs .server .namenode .FsImageProto .INodeSection .XAttrFeatureProto ;
6157import org .apache .hadoop .hdfs .server .namenode .FsImageProto .INodeSection .QuotaByStorageTypeEntryProto ;
6258import org .apache .hadoop .hdfs .server .namenode .FsImageProto .INodeSection .QuotaByStorageTypeFeatureProto ;
59+ import org .apache .hadoop .hdfs .server .namenode .INodeWithAdditionalFields .PermissionStatusFormat ;
60+ import org .apache .hadoop .hdfs .server .namenode .SerialNumberManager .StringTable ;
6361import org .apache .hadoop .hdfs .server .namenode .snapshot .Snapshot ;
6462import org .apache .hadoop .hdfs .server .namenode .startupprogress .Phase ;
6563import org .apache .hadoop .hdfs .server .namenode .startupprogress .StartupProgress ;
7472
7573@ InterfaceAudience .Private
7674public final class FSImageFormatPBINode {
77- private final static long USER_GROUP_STRID_MASK = (1 << 24 ) - 1 ;
78- private final static int USER_STRID_OFFSET = 40 ;
79- private final static int GROUP_STRID_OFFSET = 16 ;
80-
8175 public static final int ACL_ENTRY_NAME_MASK = (1 << 24 ) - 1 ;
8276 public static final int ACL_ENTRY_NAME_OFFSET = 6 ;
8377 public static final int ACL_ENTRY_TYPE_OFFSET = 3 ;
8478 public static final int ACL_ENTRY_SCOPE_OFFSET = 5 ;
8579 public static final int ACL_ENTRY_PERM_MASK = 7 ;
86- private static final int ACL_ENTRY_TYPE_MASK = 3 ;
87- private static final int ACL_ENTRY_SCOPE_MASK = 1 ;
88- private static final FsAction [] FSACTION_VALUES = FsAction .values ();
89- private static final AclEntryScope [] ACL_ENTRY_SCOPE_VALUES = AclEntryScope
90- .values ();
91- private static final AclEntryType [] ACL_ENTRY_TYPE_VALUES = AclEntryType
92- .values ();
9380
9481 public static final int XATTR_NAMESPACE_MASK = 3 ;
9582 public static final int XATTR_NAMESPACE_OFFSET = 30 ;
@@ -100,56 +87,36 @@ public final class FSImageFormatPBINode {
10087 public static final int XATTR_NAMESPACE_EXT_OFFSET = 5 ;
10188 public static final int XATTR_NAMESPACE_EXT_MASK = 1 ;
10289
103- private static final XAttr .NameSpace [] XATTR_NAMESPACE_VALUES =
104- XAttr .NameSpace .values ();
105-
106-
10790 private static final Logger LOG =
10891 LoggerFactory .getLogger (FSImageFormatPBINode .class );
10992
93+ // the loader must decode all fields referencing serial number based fields
94+ // via to<Item> methods with the string table.
11095 public final static class Loader {
11196 public static PermissionStatus loadPermission (long id ,
112- final String [] stringTable ) {
113- short perm = (short ) (id & ((1 << GROUP_STRID_OFFSET ) - 1 ));
114- int gsid = (int ) ((id >> GROUP_STRID_OFFSET ) & USER_GROUP_STRID_MASK );
115- int usid = (int ) ((id >> USER_STRID_OFFSET ) & USER_GROUP_STRID_MASK );
116- return new PermissionStatus (stringTable [usid ], stringTable [gsid ],
117- new FsPermission (perm ));
97+ final StringTable stringTable ) {
98+ return PermissionStatusFormat .toPermissionStatus (id , stringTable );
11899 }
119100
120101 public static ImmutableList <AclEntry > loadAclEntries (
121- AclFeatureProto proto , final String [] stringTable ) {
102+ AclFeatureProto proto , final StringTable stringTable ) {
122103 ImmutableList .Builder <AclEntry > b = ImmutableList .builder ();
123104 for (int v : proto .getEntriesList ()) {
124- int p = v & ACL_ENTRY_PERM_MASK ;
125- int t = (v >> ACL_ENTRY_TYPE_OFFSET ) & ACL_ENTRY_TYPE_MASK ;
126- int s = (v >> ACL_ENTRY_SCOPE_OFFSET ) & ACL_ENTRY_SCOPE_MASK ;
127- int nid = (v >> ACL_ENTRY_NAME_OFFSET ) & ACL_ENTRY_NAME_MASK ;
128- String name = stringTable [nid ];
129- b .add (new AclEntry .Builder ().setName (name )
130- .setPermission (FSACTION_VALUES [p ])
131- .setScope (ACL_ENTRY_SCOPE_VALUES [s ])
132- .setType (ACL_ENTRY_TYPE_VALUES [t ]).build ());
105+ b .add (AclEntryStatusFormat .toAclEntry (v , stringTable ));
133106 }
134107 return b .build ();
135108 }
136109
137110 public static List <XAttr > loadXAttrs (
138- XAttrFeatureProto proto , final String [] stringTable ) {
111+ XAttrFeatureProto proto , final StringTable stringTable ) {
139112 List <XAttr > b = new ArrayList <>();
140113 for (XAttrCompactProto xAttrCompactProto : proto .getXAttrsList ()) {
141114 int v = xAttrCompactProto .getName ();
142- int nid = (v >> XATTR_NAME_OFFSET ) & XATTR_NAME_MASK ;
143- int ns = (v >> XATTR_NAMESPACE_OFFSET ) & XATTR_NAMESPACE_MASK ;
144- ns |=
145- ((v >> XATTR_NAMESPACE_EXT_OFFSET ) & XATTR_NAMESPACE_EXT_MASK ) << 2 ;
146- String name = stringTable [nid ];
147115 byte [] value = null ;
148116 if (xAttrCompactProto .getValue () != null ) {
149117 value = xAttrCompactProto .getValue ().toByteArray ();
150118 }
151- b .add (new XAttr .Builder ().setNameSpace (XATTR_NAMESPACE_VALUES [ns ])
152- .setName (name ).setValue (value ).build ());
119+ b .add (XAttrFormat .toXAttr (v , value , stringTable ));
153120 }
154121
155122 return b ;
@@ -439,46 +406,30 @@ private void loadRootINode(INodeSection.INode p) {
439406 }
440407 }
441408
409+ // the saver can directly write out fields referencing serial numbers.
410+ // the serial number maps will be compacted when loading.
442411 public final static class Saver {
443412 private long numImageErrors ;
444413
445- private static long buildPermissionStatus (INodeAttributes n ,
446- final SaverContext .DeduplicationMap <String > stringMap ) {
447- long userId = stringMap .getId (n .getUserName ());
448- long groupId = stringMap .getId (n .getGroupName ());
449- return ((userId & USER_GROUP_STRID_MASK ) << USER_STRID_OFFSET )
450- | ((groupId & USER_GROUP_STRID_MASK ) << GROUP_STRID_OFFSET )
451- | n .getFsPermissionShort ();
414+ private static long buildPermissionStatus (INodeAttributes n ) {
415+ return n .getPermissionLong ();
452416 }
453417
454- private static AclFeatureProto .Builder buildAclEntries (AclFeature f ,
455- final SaverContext .DeduplicationMap <String > map ) {
418+ private static AclFeatureProto .Builder buildAclEntries (AclFeature f ) {
456419 AclFeatureProto .Builder b = AclFeatureProto .newBuilder ();
457420 for (int pos = 0 , e ; pos < f .getEntriesSize (); pos ++) {
458421 e = f .getEntryAt (pos );
459- int nameId = map .getId (AclEntryStatusFormat .getName (e ));
460- int v = ((nameId & ACL_ENTRY_NAME_MASK ) << ACL_ENTRY_NAME_OFFSET )
461- | (AclEntryStatusFormat .getType (e ).ordinal () << ACL_ENTRY_TYPE_OFFSET )
462- | (AclEntryStatusFormat .getScope (e ).ordinal () << ACL_ENTRY_SCOPE_OFFSET )
463- | (AclEntryStatusFormat .getPermission (e ).ordinal ());
464- b .addEntries (v );
422+ b .addEntries (e );
465423 }
466424 return b ;
467425 }
468-
469- private static XAttrFeatureProto .Builder buildXAttrs (XAttrFeature f ,
470- final SaverContext .DeduplicationMap <String > stringMap ) {
426+
427+ private static XAttrFeatureProto .Builder buildXAttrs (XAttrFeature f ) {
471428 XAttrFeatureProto .Builder b = XAttrFeatureProto .newBuilder ();
472429 for (XAttr a : f .getXAttrs ()) {
473430 XAttrCompactProto .Builder xAttrCompactBuilder = XAttrCompactProto .
474431 newBuilder ();
475- int nsOrd = a .getNameSpace ().ordinal ();
476- Preconditions .checkArgument (nsOrd < 8 , "Too many namespaces." );
477- int v = ((nsOrd & XATTR_NAMESPACE_MASK ) << XATTR_NAMESPACE_OFFSET )
478- | ((stringMap .getId (a .getName ()) & XATTR_NAME_MASK ) <<
479- XATTR_NAME_OFFSET );
480- v |= (((nsOrd >> 2 ) & XATTR_NAMESPACE_EXT_MASK ) <<
481- XATTR_NAMESPACE_EXT_OFFSET );
432+ int v = XAttrFormat .toInt (a );
482433 xAttrCompactBuilder .setName (v );
483434 if (a .getValue () != null ) {
484435 xAttrCompactBuilder .setValue (PBHelperClient .getByteString (a .getValue ()));
@@ -510,7 +461,7 @@ public static INodeSection.INodeFile.Builder buildINodeFile(
510461 INodeSection .INodeFile .Builder b = INodeSection .INodeFile .newBuilder ()
511462 .setAccessTime (file .getAccessTime ())
512463 .setModificationTime (file .getModificationTime ())
513- .setPermission (buildPermissionStatus (file , state . getStringMap () ))
464+ .setPermission (buildPermissionStatus (file ))
514465 .setPreferredBlockSize (file .getPreferredBlockSize ())
515466 .setStoragePolicyID (file .getLocalStoragePolicyID ())
516467 .setBlockType (PBHelperClient .convert (file .getBlockType ()));
@@ -523,11 +474,11 @@ public static INodeSection.INodeFile.Builder buildINodeFile(
523474
524475 AclFeature f = file .getAclFeature ();
525476 if (f != null ) {
526- b .setAcl (buildAclEntries (f , state . getStringMap () ));
477+ b .setAcl (buildAclEntries (f ));
527478 }
528479 XAttrFeature xAttrFeature = file .getXAttrFeature ();
529480 if (xAttrFeature != null ) {
530- b .setXAttrs (buildXAttrs (xAttrFeature , state . getStringMap () ));
481+ b .setXAttrs (buildXAttrs (xAttrFeature ));
531482 }
532483 return b ;
533484 }
@@ -539,19 +490,19 @@ public static INodeSection.INodeDirectory.Builder buildINodeDirectory(
539490 .newBuilder ().setModificationTime (dir .getModificationTime ())
540491 .setNsQuota (quota .getNameSpace ())
541492 .setDsQuota (quota .getStorageSpace ())
542- .setPermission (buildPermissionStatus (dir , state . getStringMap () ));
493+ .setPermission (buildPermissionStatus (dir ));
543494
544495 if (quota .getTypeSpaces ().anyGreaterOrEqual (0 )) {
545496 b .setTypeQuotas (buildQuotaByStorageTypeEntries (quota ));
546497 }
547498
548499 AclFeature f = dir .getAclFeature ();
549500 if (f != null ) {
550- b .setAcl (buildAclEntries (f , state . getStringMap () ));
501+ b .setAcl (buildAclEntries (f ));
551502 }
552503 XAttrFeature xAttrFeature = dir .getXAttrFeature ();
553504 if (xAttrFeature != null ) {
554- b .setXAttrs (buildXAttrs (xAttrFeature , state . getStringMap () ));
505+ b .setXAttrs (buildXAttrs (xAttrFeature ));
555506 }
556507 return b ;
557508 }
@@ -712,7 +663,7 @@ private void save(OutputStream out, INodeSymlink n) throws IOException {
712663 SaverContext state = parent .getSaverContext ();
713664 INodeSection .INodeSymlink .Builder b = INodeSection .INodeSymlink
714665 .newBuilder ()
715- .setPermission (buildPermissionStatus (n , state . getStringMap () ))
666+ .setPermission (buildPermissionStatus (n ))
716667 .setTarget (ByteString .copyFrom (n .getSymlink ()))
717668 .setModificationTime (n .getModificationTime ())
718669 .setAccessTime (n .getAccessTime ());
0 commit comments