001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.hadoop.hdfs.protocol;
019    
020    import java.util.EnumSet;
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    import org.apache.hadoop.classification.InterfaceAudience;
025    
026    /**
027     * This class tracks changes in the layout version of HDFS.
028     * 
029     * Layout version is changed for following reasons:
030     * <ol>
031     * <li>The layout of how namenode or datanode stores information 
032     * on disk changes.</li>
033     * <li>A new operation code is added to the editlog.</li>
034     * <li>Modification such as format of a record, content of a record 
035     * in editlog or fsimage.</li>
036     * </ol>
037     * <br>
038     * <b>How to update layout version:<br></b>
039     * When a change requires new layout version, please add an entry into
040     * {@link Feature} with a short enum name, new layout version and description
041     * of the change. Please see {@link Feature} for further details.
042     * <br>
043     */
044    @InterfaceAudience.Private
045    public class LayoutVersion {
046     
047      /**
048       * Version in which HDFS-2991 was fixed. This bug caused OP_ADD to
049       * sometimes be skipped for append() calls. If we see such a case when
050       * loading the edits, but the version is known to have that bug, we
051       * workaround the issue. Otherwise we should consider it a corruption
052       * and bail.
053       */
054      public static final int BUGFIX_HDFS_2991_VERSION = -40;
055    
056      /**
057       * Enums for features that change the layout version.
058       * <br><br>
059       * To add a new layout version:
060       * <ul>
061       * <li>Define a new enum constant with a short enum name, the new layout version 
062       * and description of the added feature.</li>
063       * <li>When adding a layout version with an ancestor that is not same as
064       * its immediate predecessor, use the constructor where a spacific ancestor
065       * can be passed.
066       * </li>
067       * </ul>
068       */
069      public static enum Feature {
070        NAMESPACE_QUOTA(-16, "Support for namespace quotas"),
071        FILE_ACCESS_TIME(-17, "Support for access time on files"),
072        DISKSPACE_QUOTA(-18, "Support for disk space quotas"),
073        STICKY_BIT(-19, "Support for sticky bits"),
074        APPEND_RBW_DIR(-20, "Datanode has \"rbw\" subdirectory for append"),
075        ATOMIC_RENAME(-21, "Support for atomic rename"),
076        CONCAT(-22, "Support for concat operation"),
077        SYMLINKS(-23, "Support for symbolic links"),
078        DELEGATION_TOKEN(-24, "Support for delegation tokens for security"),
079        FSIMAGE_COMPRESSION(-25, "Support for fsimage compression"),
080        FSIMAGE_CHECKSUM(-26, "Support checksum for fsimage"),
081        REMOVE_REL13_DISK_LAYOUT_SUPPORT(-27, "Remove support for 0.13 disk layout"),
082        EDITS_CHESKUM(-28, "Support checksum for editlog"),
083        UNUSED(-29, "Skipped version"),
084        FSIMAGE_NAME_OPTIMIZATION(-30, "Store only last part of path in fsimage"),
085        RESERVED_REL20_203(-31, -19, "Reserved for release 0.20.203"),
086        RESERVED_REL20_204(-32, "Reserved for release 0.20.204"),
087        RESERVED_REL22(-33, -27, "Reserved for release 0.22"),
088        RESERVED_REL23(-34, -30, "Reserved for release 0.23"),
089        FEDERATION(-35, "Support for namenode federation"),
090        LEASE_REASSIGNMENT(-36, "Support for persisting lease holder reassignment"),
091        STORED_TXIDS(-37, "Transaction IDs are stored in edits log and image files"),
092        TXID_BASED_LAYOUT(-38, "File names in NN Storage are based on transaction IDs"), 
093        EDITLOG_OP_OPTIMIZATION(-39,
094            "Use LongWritable and ShortWritable directly instead of ArrayWritable of UTF8");
095        
096        final int lv;
097        final int ancestorLV;
098        final String description;
099        
100        /**
101         * Feature that is added at {@code currentLV}. 
102         * @param lv new layout version with the addition of this feature
103         * @param description description of the feature
104         */
105        Feature(final int lv, final String description) {
106          this(lv, lv + 1, description);
107        }
108    
109        /**
110         * Feature that is added at {@code currentLV}.
111         * @param lv new layout version with the addition of this feature
112         * @param ancestorLV layout version from which the new lv is derived
113         *          from.
114         * @param description description of the feature
115         */
116        Feature(final int lv, final int ancestorLV,
117            final String description) {
118          this.lv = lv;
119          this.ancestorLV = ancestorLV;
120          this.description = description;
121        }
122        
123        /** 
124         * Accessor method for feature layout version 
125         * @return int lv value
126         */
127        public int getLayoutVersion() {
128          return lv;
129        }
130    
131        /** 
132         * Accessor method for feature ancestor layout version 
133         * @return int ancestor LV value
134         */
135        public int getAncestorLayoutVersion() {
136          return ancestorLV;
137        }
138    
139        /** 
140         * Accessor method for feature description 
141         * @return String feature description 
142         */
143        public String getDescription() {
144          return description;
145        }
146      }
147      
148      // Build layout version and corresponding feature matrix
149      static final Map<Integer, EnumSet<Feature>>map = 
150        new HashMap<Integer, EnumSet<Feature>>();
151      
152      // Static initialization 
153      static {
154        initMap();
155      }
156      
157      /**
158       * Initialize the map of a layout version and EnumSet of {@link Feature}s 
159       * supported.
160       */
161      private static void initMap() {
162        // Go through all the enum constants and build a map of
163        // LayoutVersion <-> EnumSet of all supported features in that LayoutVersion
164        for (Feature f : Feature.values()) {
165          EnumSet<Feature> ancestorSet = map.get(f.ancestorLV);
166          if (ancestorSet == null) {
167            ancestorSet = EnumSet.noneOf(Feature.class); // Empty enum set
168            map.put(f.ancestorLV, ancestorSet);
169          }
170          EnumSet<Feature> featureSet = EnumSet.copyOf(ancestorSet);
171          featureSet.add(f);
172          map.put(f.lv, featureSet);
173        }
174        
175        // Special initialization for 0.20.203 and 0.20.204
176        // to add Feature#DELEGATION_TOKEN
177        specialInit(Feature.RESERVED_REL20_203.lv, Feature.DELEGATION_TOKEN);
178        specialInit(Feature.RESERVED_REL20_204.lv, Feature.DELEGATION_TOKEN);
179      }
180      
181      private static void specialInit(int lv, Feature f) {
182        EnumSet<Feature> set = map.get(lv);
183        set.add(f);
184      }
185      
186      /**
187       * Gets formatted string that describes {@link LayoutVersion} information.
188       */
189      public static String getString() {
190        final StringBuilder buf = new StringBuilder();
191        buf.append("Feature List:\n");
192        for (Feature f : Feature.values()) {
193          buf.append(f).append(" introduced in layout version ")
194              .append(f.lv).append(" (").
195          append(f.description).append(")\n");
196        }
197        
198        buf.append("\n\nLayoutVersion and supported features:\n");
199        for (Feature f : Feature.values()) {
200          buf.append(f.lv).append(": ").append(map.get(f.lv))
201              .append("\n");
202        }
203        return buf.toString();
204      }
205      
206      /**
207       * Returns true if a given feature is supported in the given layout version
208       * @param f Feature
209       * @param lv LayoutVersion
210       * @return true if {@code f} is supported in layout version {@code lv}
211       */
212      public static boolean supports(final Feature f, final int lv) {
213        final EnumSet<Feature> set =  map.get(lv);
214        return set != null && set.contains(f);
215      }
216      
217      /**
218       * Get the current layout version
219       */
220      public static int getCurrentLayoutVersion() {
221        Feature[] values = Feature.values();
222        return values[values.length - 1].lv;
223      }
224    }