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  private boolean unlinked;      // copy-on-write done for block
033
034  /**
035   * Constructor
036   * @param blockId block id
037   * @param len replica length
038   * @param genStamp replica generation stamp
039   * @param vol volume where replica is located
040   * @param dir directory path where block and meta files are located
041   */
042  public FinalizedReplica(long blockId, long len, long genStamp,
043      FsVolumeSpi vol, File dir) {
044    super(blockId, len, genStamp, vol, dir);
045  }
046  
047  /**
048   * Constructor
049   * @param block a block
050   * @param vol volume where replica is located
051   * @param dir directory path where block and meta files are located
052   */
053  public FinalizedReplica(Block block, FsVolumeSpi vol, File dir) {
054    super(block, vol, dir);
055  }
056
057  /**
058   * Copy constructor.
059   * @param from where to copy construct from
060   */
061  public FinalizedReplica(FinalizedReplica from) {
062    super(from);
063    this.unlinked = from.isUnlinked();
064  }
065
066  @Override  // ReplicaInfo
067  public ReplicaState getState() {
068    return ReplicaState.FINALIZED;
069  }
070  
071  @Override // ReplicaInfo
072  public boolean isUnlinked() {
073    return unlinked;
074  }
075
076  @Override  // ReplicaInfo
077  public void setUnlinked() {
078    unlinked = true;
079  }
080  
081  @Override
082  public long getVisibleLength() {
083    return getNumBytes();       // all bytes are visible
084  }
085
086  @Override
087  public long getBytesOnDisk() {
088    return getNumBytes();
089  }
090
091  @Override  // Object
092  public boolean equals(Object o) {
093    return super.equals(o);
094  }
095  
096  @Override  // Object
097  public int hashCode() {
098    return super.hashCode();
099  }
100  
101  @Override
102  public String toString() {
103    return super.toString()
104        + "\n  unlinked          =" + unlinked;
105  }
106
107  /**
108   * gets the last chunk checksum and the length of the block corresponding
109   * to that checksum.
110   * Note, need to be called with the FsDataset lock acquired. May improve to
111   * lock only the FsVolume in the future.
112   * @throws IOException
113   */
114  public ChunkChecksum getLastChecksumAndDataLen() throws IOException {
115    ChunkChecksum chunkChecksum = null;
116    try {
117      byte[] lastChecksum = getVolume().loadLastPartialChunkChecksum(
118          getBlockFile(), getMetaFile());
119      if (lastChecksum != null) {
120        chunkChecksum =
121            new ChunkChecksum(getVisibleLength(), lastChecksum);
122      }
123    } catch (FileNotFoundException e) {
124      // meta file is lost. Try to continue anyway.
125      DataNode.LOG.warn("meta file " + getMetaFile() +
126          " is missing!");
127    } catch (IOException ioe) {
128      DataNode.LOG.warn("Unable to read checksum from meta file " +
129          getMetaFile(), ioe);
130    }
131    return chunkChecksum;
132  }
133}