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.protocol;
019    
020    import org.apache.hadoop.hdfs.StorageType;
021    
022    import java.util.UUID;
023    
024    /**
025     * Class captures information of a storage in Datanode.
026     */
027    public 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    
051      /**
052       * Create a storage with {@link State#NORMAL} and {@link StorageType#DEFAULT}.
053       */
054      public DatanodeStorage(String storageID) {
055        this(storageID, State.NORMAL, StorageType.DEFAULT);
056      }
057    
058      public DatanodeStorage(String sid, State s, StorageType sm) {
059        this.storageID = sid;
060        this.state = s;
061        this.storageType = sm;
062      }
063    
064      public String getStorageID() {
065        return storageID;
066      }
067    
068      public State getState() {
069        return state;
070      }
071    
072      public StorageType getStorageType() {
073        return storageType;
074      }
075    
076      /**
077       * Generate new storage ID. The format of this string can be changed
078       * in the future without requiring that old storage IDs be updated.
079       *
080       * @return unique storage ID
081       */
082      public static String generateUuid() {
083        return "DS-" + UUID.randomUUID();
084      }
085    
086      @Override
087      public String toString() {
088        return "DatanodeStorage["+ storageID + "," + storageType + "," + state +"]";
089      }
090      
091      @Override
092      public boolean equals(Object other){
093        if (other == this) {
094          return true;
095        }
096    
097        if ((other == null) ||
098            !(other instanceof DatanodeStorage)) {
099          return false;
100        }
101        DatanodeStorage otherStorage = (DatanodeStorage) other;
102        return otherStorage.getStorageID().compareTo(getStorageID()) == 0;
103      }
104    
105      @Override
106      public int hashCode() {
107        return getStorageID().hashCode();
108      }
109    }