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.protocol;
019
020import org.apache.hadoop.fs.StorageType;
021
022import java.util.UUID;
023
024/**
025 * Class captures information of a storage in Datanode.
026 */
027public class DatanodeStorage {
028  /** The state of the storage. */
029  public enum State {
030    NORMAL,
031    
032    /**
033     * A storage that represents a read-only path to replicas stored on a shared storage device.
034     * Replicas on {@link #READ_ONLY_SHARED} storage are not counted towards live replicas.
035     * 
036     * <p>
037     * In certain implementations, a {@link #READ_ONLY_SHARED} storage may be correlated to 
038     * its {@link #NORMAL} counterpart using the {@link DatanodeStorage#storageID}.  This
039     * property should be used for debugging purposes only.
040     * </p> 
041     */
042    READ_ONLY_SHARED,
043
044    FAILED;
045  }
046  
047  private final String storageID;
048  private final State state;
049  private final StorageType storageType;
050  private static final String STORAGE_ID_PREFIX = "DS-";
051
052  /**
053   * Create a storage with {@link State#NORMAL} and {@link StorageType#DEFAULT}.
054   */
055  public DatanodeStorage(String storageID) {
056    this(storageID, State.NORMAL, StorageType.DEFAULT);
057  }
058
059  public DatanodeStorage(String sid, State s, StorageType sm) {
060    this.storageID = sid;
061    this.state = s;
062    this.storageType = sm;
063  }
064
065  public String getStorageID() {
066    return storageID;
067  }
068
069  public State getState() {
070    return state;
071  }
072
073  public StorageType getStorageType() {
074    return storageType;
075  }
076
077  /**
078   * Generate new storage ID. The format of this string can be changed
079   * in the future without requiring that old storage IDs be updated.
080   *
081   * @return unique storage ID
082   */
083  public static String generateUuid() {
084    return STORAGE_ID_PREFIX + UUID.randomUUID();
085  }
086
087  /**
088   * Verify that a given string is a storage ID in the "DS-..uuid.." format.
089   */
090  public static boolean isValidStorageId(final String storageID) {
091    try {
092      // Attempt to parse the UUID.
093      if (storageID != null && storageID.indexOf(STORAGE_ID_PREFIX) == 0) {
094        UUID.fromString(storageID.substring(STORAGE_ID_PREFIX.length()));
095        return true;
096      }
097    } catch (IllegalArgumentException iae) {
098    }
099
100    return false;
101  }
102
103  @Override
104  public String toString() {
105    return "DatanodeStorage["+ storageID + "," + storageType + "," + state +"]";
106  }
107  
108  @Override
109  public boolean equals(Object other){
110    if (other == this) {
111      return true;
112    }
113
114    if ((other == null) ||
115        !(other instanceof DatanodeStorage)) {
116      return false;
117    }
118    DatanodeStorage otherStorage = (DatanodeStorage) other;
119    return otherStorage.getStorageID().compareTo(getStorageID()) == 0;
120  }
121
122  @Override
123  public int hashCode() {
124    return getStorageID().hashCode();
125  }
126}