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.datanode;
019
020import java.io.File;
021import java.io.FileNotFoundException;
022import java.io.IOException;
023
024import org.apache.hadoop.hdfs.protocol.Block;
025import org.apache.hadoop.hdfs.server.common.HdfsServerConstants.ReplicaState;
026import org.apache.hadoop.hdfs.server.datanode.fsdataset.FsVolumeSpi;
027
028/**
029 * This class describes a replica that has been finalized.
030 */
031public class FinalizedReplica extends ReplicaInfo {
032
033  /**
034   * Constructor
035   * @param blockId block id
036   * @param len replica length
037   * @param genStamp replica generation stamp
038   * @param vol volume where replica is located
039   * @param dir directory path where block and meta files are located
040   */
041  public FinalizedReplica(long blockId, long len, long genStamp,
042      FsVolumeSpi vol, File dir) {
043    super(blockId, len, genStamp, vol, dir);
044  }
045  
046  /**
047   * Constructor
048   * @param block a block
049   * @param vol volume where replica is located
050   * @param dir directory path where block and meta files are located
051   */
052  public FinalizedReplica(Block block, FsVolumeSpi vol, File dir) {
053    super(block, vol, dir);
054  }
055
056  /**
057   * Copy constructor.
058   * @param from where to copy construct from
059   */
060  public FinalizedReplica(FinalizedReplica from) {
061    super(from);
062  }
063
064  @Override  // ReplicaInfo
065  public ReplicaState getState() {
066    return ReplicaState.FINALIZED;
067  }
068  
069  @Override
070  public long getVisibleLength() {
071    return getNumBytes();       // all bytes are visible
072  }
073
074  @Override
075  public long getBytesOnDisk() {
076    return getNumBytes();
077  }
078
079  @Override  // Object
080  public boolean equals(Object o) {
081    return super.equals(o);
082  }
083  
084  @Override  // Object
085  public int hashCode() {
086    return super.hashCode();
087  }
088  
089  @Override
090  public String toString() {
091    return super.toString();
092  }
093
094  /**
095   * gets the last chunk checksum and the length of the block corresponding
096   * to that checksum.
097   * Note, need to be called with the FsDataset lock acquired. May improve to
098   * lock only the FsVolume in the future.
099   * @throws IOException
100   */
101  public ChunkChecksum getLastChecksumAndDataLen() throws IOException {
102    ChunkChecksum chunkChecksum = null;
103    try {
104      byte[] lastChecksum = getVolume().loadLastPartialChunkChecksum(
105          getBlockFile(), getMetaFile());
106      if (lastChecksum != null) {
107        chunkChecksum =
108            new ChunkChecksum(getVisibleLength(), lastChecksum);
109      }
110    } catch (FileNotFoundException e) {
111      // meta file is lost. Try to continue anyway.
112      DataNode.LOG.warn("meta file " + getMetaFile() +
113          " is missing!");
114    } catch (IOException ioe) {
115      DataNode.LOG.warn("Unable to read checksum from meta file " +
116          getMetaFile(), ioe);
117    }
118    return chunkChecksum;
119  }
120}