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