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 String name = null;
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        public String toString() {
145          return description;
146        }
147      }
148    
149      /**
150       * Block replica states, which it can go through while being constructed.
151       */
152      static public enum ReplicaState {
153        /** Replica is finalized. The state when replica is not modified. */
154        FINALIZED(0),
155        /** Replica is being written to. */
156        RBW(1),
157        /** Replica is waiting to be recovered. */
158        RWR(2),
159        /** Replica is under recovery. */
160        RUR(3),
161        /** Temporary replica: created for replication and relocation only. */
162        TEMPORARY(4);
163    
164        private int value;
165    
166        private ReplicaState(int v) {
167          value = v;
168        }
169    
170        public int getValue() {
171          return value;
172        }
173    
174        public static ReplicaState getState(int v) {
175          return ReplicaState.values()[v];
176        }
177    
178        /** Read from in */
179        public static ReplicaState read(DataInput in) throws IOException {
180          return values()[in.readByte()];
181        }
182    
183        /** Write to out */
184        public void write(DataOutput out) throws IOException {
185          out.writeByte(ordinal());
186        }
187      }
188    
189      /**
190       * States, which a block can go through while it is under construction.
191       */
192      static public enum BlockUCState {
193        /**
194         * Block construction completed.<br>
195         * The block has at least one {@link ReplicaState#FINALIZED} replica,
196         * and is not going to be modified.
197         */
198        COMPLETE,
199        /**
200         * The block is under construction.<br>
201         * It has been recently allocated for write or append.
202         */
203        UNDER_CONSTRUCTION,
204        /**
205         * The block is under recovery.<br>
206         * When a file lease expires its last block may not be {@link #COMPLETE}
207         * and needs to go through a recovery procedure, 
208         * which synchronizes the existing replicas contents.
209         */
210        UNDER_RECOVERY,
211        /**
212         * The block is committed.<br>
213         * The client reported that all bytes are written to data-nodes
214         * with the given generation stamp and block length, but no 
215         * {@link ReplicaState#FINALIZED} 
216         * replicas has yet been reported by data-nodes themselves.
217         */
218        COMMITTED;
219      }
220      
221      public static final String NAMENODE_LEASE_HOLDER = "HDFS_NameNode";
222      public static final long NAMENODE_LEASE_RECHECK_INTERVAL = 2000;
223    }
224