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.hdfs.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    READ_ONLY
032  }
033  
034  private final String storageID;
035  private final State state;
036  private final StorageType storageType;
037
038  /**
039   * Create a storage with {@link State#NORMAL} and {@link StorageType#DEFAULT}.
040   *
041   * @param storageID
042   */
043  public DatanodeStorage(String storageID) {
044    this(storageID, State.NORMAL, StorageType.DEFAULT);
045  }
046
047  public DatanodeStorage(String sid, State s, StorageType sm) {
048    this.storageID = sid;
049    this.state = s;
050    this.storageType = sm;
051  }
052
053  public String getStorageID() {
054    return storageID;
055  }
056
057  public State getState() {
058    return state;
059  }
060
061  public StorageType getStorageType() {
062    return storageType;
063  }
064
065  /**
066   * Generate new storage ID. The format of this string can be changed
067   * in the future without requiring that old storage IDs be updated.
068   *
069   * @return unique storage ID
070   */
071  public static String generateUuid() {
072    return "DS-" + UUID.randomUUID();
073  }
074
075  @Override
076  public boolean equals(Object other){
077    if (other == this) {
078      return true;
079    }
080
081    if ((other == null) ||
082        !(other instanceof DatanodeStorage)) {
083      return false;
084    }
085    DatanodeStorage otherStorage = (DatanodeStorage) other;
086    return otherStorage.getStorageID().compareTo(getStorageID()) == 0;
087  }
088
089  @Override
090  public int hashCode() {
091    return getStorageID().hashCode();
092  }
093}