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