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.server.common;
019    
020    import java.io.DataInput;
021    import java.io.DataOutput;
022    import java.io.IOException;
023    
024    import org.apache.hadoop.classification.InterfaceAudience;
025    import org.apache.hadoop.hdfs.server.namenode.MetaRecoveryContext;
026    
027    /************************************
028     * Some handy internal HDFS constants
029     *
030     ************************************/
031    
032    @InterfaceAudience.Private
033    public final class HdfsServerConstants {
034      /* Hidden constructor */
035      private HdfsServerConstants() { }
036      
037      /**
038       * Type of the node
039       */
040      static public enum NodeType {
041        NAME_NODE,
042        DATA_NODE;
043      }
044    
045      /** Startup options */
046      static public enum StartupOption{
047        FORMAT  ("-format"),
048        CLUSTERID ("-clusterid"),
049        GENCLUSTERID ("-genclusterid"),
050        REGULAR ("-regular"),
051        BACKUP  ("-backup"),
052        CHECKPOINT("-checkpoint"),
053        UPGRADE ("-upgrade"),
054        ROLLBACK("-rollback"),
055        FINALIZE("-finalize"),
056        IMPORT  ("-importCheckpoint"),
057        BOOTSTRAPSTANDBY("-bootstrapStandby"),
058        INITIALIZESHAREDEDITS("-initializeSharedEdits"),
059        RECOVER  ("-recover"),
060        FORCE("-force"),
061        NONINTERACTIVE("-nonInteractive");
062        
063        private final String name;
064        
065        // Used only with format and upgrade options
066        private String clusterId = null;
067        
068        // Used only with format option
069        private boolean isForceFormat = false;
070        private boolean isInteractiveFormat = true;
071        
072        // Used only with recovery option
073        private int force = 0;
074    
075        private StartupOption(String arg) {this.name = arg;}
076        public String getName() {return name;}
077        public NamenodeRole toNodeRole() {
078          switch(this) {
079          case BACKUP: 
080            return NamenodeRole.BACKUP;
081          case CHECKPOINT: 
082            return NamenodeRole.CHECKPOINT;
083          default:
084            return NamenodeRole.NAMENODE;
085          }
086        }
087        
088        public void setClusterId(String cid) {
089          clusterId = cid;
090        }
091    
092        public String getClusterId() {
093          return clusterId;
094        }
095    
096        public MetaRecoveryContext createRecoveryContext() {
097          if (!name.equals(RECOVER.name))
098            return null;
099          return new MetaRecoveryContext(force);
100        }
101    
102        public void setForce(int force) {
103          this.force = force;
104        }
105        
106        public int getForce() {
107          return this.force;
108        }
109        
110        public boolean getForceFormat() {
111          return isForceFormat;
112        }
113        
114        public void setForceFormat(boolean force) {
115          isForceFormat = force;
116        }
117        
118        public boolean getInteractiveFormat() {
119          return isInteractiveFormat;
120        }
121        
122        public void setInteractiveFormat(boolean interactive) {
123          isInteractiveFormat = interactive;
124        }
125      }
126    
127      // Timeouts for communicating with DataNode for streaming writes/reads
128      public static int READ_TIMEOUT = 60 * 1000;
129      public static int READ_TIMEOUT_EXTENSION = 5 * 1000;
130      public static int WRITE_TIMEOUT = 8 * 60 * 1000;
131      public static int WRITE_TIMEOUT_EXTENSION = 5 * 1000; //for write pipeline
132    
133      /**
134       * Defines the NameNode role.
135       */
136      static public enum NamenodeRole {
137        NAMENODE  ("NameNode"),
138        BACKUP    ("Backup Node"),
139        CHECKPOINT("Checkpoint Node");
140    
141        private String description = null;
142        private NamenodeRole(String arg) {this.description = arg;}
143      
144        @Override
145        public String toString() {
146          return description;
147        }
148      }
149    
150      /**
151       * Block replica states, which it can go through while being constructed.
152       */
153      static public enum ReplicaState {
154        /** Replica is finalized. The state when replica is not modified. */
155        FINALIZED(0),
156        /** Replica is being written to. */
157        RBW(1),
158        /** Replica is waiting to be recovered. */
159        RWR(2),
160        /** Replica is under recovery. */
161        RUR(3),
162        /** Temporary replica: created for replication and relocation only. */
163        TEMPORARY(4);
164    
165        private int value;
166    
167        private ReplicaState(int v) {
168          value = v;
169        }
170    
171        public int getValue() {
172          return value;
173        }
174    
175        public static ReplicaState getState(int v) {
176          return ReplicaState.values()[v];
177        }
178    
179        /** Read from in */
180        public static ReplicaState read(DataInput in) throws IOException {
181          return values()[in.readByte()];
182        }
183    
184        /** Write to out */
185        public void write(DataOutput out) throws IOException {
186          out.writeByte(ordinal());
187        }
188      }
189    
190      /**
191       * States, which a block can go through while it is under construction.
192       */
193      static public enum BlockUCState {
194        /**
195         * Block construction completed.<br>
196         * The block has at least one {@link ReplicaState#FINALIZED} replica,
197         * and is not going to be modified.
198         */
199        COMPLETE,
200        /**
201         * The block is under construction.<br>
202         * It has been recently allocated for write or append.
203         */
204        UNDER_CONSTRUCTION,
205        /**
206         * The block is under recovery.<br>
207         * When a file lease expires its last block may not be {@link #COMPLETE}
208         * and needs to go through a recovery procedure, 
209         * which synchronizes the existing replicas contents.
210         */
211        UNDER_RECOVERY,
212        /**
213         * The block is committed.<br>
214         * The client reported that all bytes are written to data-nodes
215         * with the given generation stamp and block length, but no 
216         * {@link ReplicaState#FINALIZED} 
217         * replicas has yet been reported by data-nodes themselves.
218         */
219        COMMITTED;
220      }
221      
222      public static final String NAMENODE_LEASE_HOLDER = "HDFS_NameNode";
223      public static final long NAMENODE_LEASE_RECHECK_INTERVAL = 2000;
224    }
225