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
019package org.apache.hadoop.hdfs.server.datanode;
020
021import java.io.IOException;
022
023import org.apache.hadoop.fs.StorageType;
024import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
025import org.apache.hadoop.hdfs.protocol.ExtendedBlock;
026import org.apache.hadoop.hdfs.protocol.LocatedBlock;
027import org.apache.hadoop.hdfs.protocolPB.DatanodeProtocolClientSideTranslatorPB;
028import org.apache.hadoop.hdfs.server.protocol.DatanodeRegistration;
029
030/**
031 * ReportBadBlockAction is an instruction issued by {{BPOfferService}} to
032 * {{BPServiceActor}} to report bad block to namenode
033 *
034 */
035public class ReportBadBlockAction implements BPServiceActorAction {
036
037  private final ExtendedBlock block;
038  private final String storageUuid;
039  private final StorageType storageType;
040
041  public ReportBadBlockAction(ExtendedBlock block, String storageUuid, 
042      StorageType storageType) {
043    this.block = block;
044    this.storageUuid = storageUuid;
045    this.storageType = storageType;
046  }
047
048  @Override
049  public void reportTo(DatanodeProtocolClientSideTranslatorPB bpNamenode, 
050    DatanodeRegistration bpRegistration) throws BPServiceActorActionException {
051    if (bpRegistration == null) {
052      return;
053    }
054    DatanodeInfo[] dnArr = { new DatanodeInfo(bpRegistration) };
055    String[] uuids = { storageUuid };
056    StorageType[] types = { storageType };
057    LocatedBlock[] locatedBlock = { new LocatedBlock(block,
058        dnArr, uuids, types) };
059
060    try {
061      bpNamenode.reportBadBlocks(locatedBlock);  
062    } catch (IOException e){
063      throw new BPServiceActorActionException("Failed to report bad block "
064          + block + " to namenode: ");
065    }
066  }
067
068  @Override
069  public int hashCode() {
070    final int prime = 31;
071    int result = 1;
072    result = prime * result + ((block == null) ? 0 : block.hashCode());
073    result = prime * result
074        + ((storageType == null) ? 0 : storageType.hashCode());
075    result = prime * result
076        + ((storageUuid == null) ? 0 : storageUuid.hashCode());
077    return result;
078  }
079
080  @Override
081  public boolean equals(Object obj) {
082    if (this == obj) {
083      return true;
084    }
085    if (obj == null || !(obj instanceof ReportBadBlockAction)) {
086      return false;
087    }
088    ReportBadBlockAction other = (ReportBadBlockAction) obj;
089    if (block == null) {
090      if (other.block != null) {
091        return false;
092      }
093    } else if (!block.equals(other.block)) {
094      return false;
095    }
096    if (storageType != other.storageType) {
097      return false;
098    }
099    if (storageUuid == null) {
100      if (other.storageUuid != null) {
101        return false;
102      }
103    } else if (!storageUuid.equals(other.storageUuid)) {
104      return false;
105    }
106    return true;
107  }
108}