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