Skip to content

Commit

Permalink
HDFS-8169. Move LocatedBlocks and related classes to hdfs-client. Con…
Browse files Browse the repository at this point in the history
…tributed by Haohui Mai.
  • Loading branch information
Haohui Mai committed Apr 20, 2015
1 parent 8511d80 commit 5c97db0
Show file tree
Hide file tree
Showing 43 changed files with 299 additions and 235 deletions.
@@ -1,2 +1,10 @@
<FindBugsFilter> <FindBugsFilter>
<Match>
<Or>
<Class name="org.apache.hadoop.hdfs.protocol.HdfsFileStatus"/>
<Class name="org.apache.hadoop.hdfs.protocol.LocatedBlock"/>
<Class name="org.apache.hadoop.hdfs.security.token.block.BlockTokenIdentifier"/>
</Or>
<Bug pattern="EI_EXPOSE_REP,EI_EXPOSE_REP2" />
</Match>
</FindBugsFilter> </FindBugsFilter>
@@ -0,0 +1,65 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hdfs;

import org.apache.hadoop.util.StringUtils;

import java.io.UnsupportedEncodingException;

public class DFSUtilClient {
/**
* Converts a byte array to a string using UTF8 encoding.
*/
public static String bytes2String(byte[] bytes) {
return bytes2String(bytes, 0, bytes.length);
}

/** Return used as percentage of capacity */
public static float getPercentUsed(long used, long capacity) {
return capacity <= 0 ? 100 : (used * 100.0f)/capacity;
}

/** Return remaining as percentage of capacity */
public static float getPercentRemaining(long remaining, long capacity) {
return capacity <= 0 ? 0 : (remaining * 100.0f)/capacity;
}

/** Convert percentage to a string. */
public static String percent2String(double percentage) {
return StringUtils.format("%.2f%%", percentage);
}

/**
* Decode a specific range of bytes of the given byte array to a string
* using UTF8.
*
* @param bytes The bytes to be decoded into characters
* @param offset The index of the first byte to decode
* @param length The number of bytes to decode
* @return The decoded string
*/
private static String bytes2String(byte[] bytes, int offset, int length) {
try {
return new String(bytes, offset, length, "UTF8");
} catch(UnsupportedEncodingException e) {
assert false : "UTF8 encoding is not supported ";
}
return null;
}

}
Expand Up @@ -26,7 +26,7 @@
import org.apache.hadoop.io.*; import org.apache.hadoop.io.*;


/************************************************** /**************************************************
* A Block is a Hadoop FS primitive, identified by a * A Block is a Hadoop FS primitive, identified by a
* long. * long.
* *
**************************************************/ **************************************************/
Expand Down Expand Up @@ -124,7 +124,7 @@ public void set(long blkid, long len, long genStamp) {
public long getBlockId() { public long getBlockId() {
return blockId; return blockId;
} }

public void setBlockId(long bid) { public void setBlockId(long bid) {
blockId = bid; blockId = bid;
} }
Expand All @@ -147,7 +147,7 @@ public void setNumBytes(long len) {
public long getGenerationStamp() { public long getGenerationStamp() {
return generationStamp; return generationStamp;
} }

public void setGenerationStamp(long stamp) { public void setGenerationStamp(long stamp) {
generationStamp = stamp; generationStamp = stamp;
} }
Expand Down Expand Up @@ -179,13 +179,13 @@ public void write(DataOutput out) throws IOException {
public void readFields(DataInput in) throws IOException { public void readFields(DataInput in) throws IOException {
readHelper(in); readHelper(in);
} }

final void writeHelper(DataOutput out) throws IOException { final void writeHelper(DataOutput out) throws IOException {
out.writeLong(blockId); out.writeLong(blockId);
out.writeLong(numBytes); out.writeLong(numBytes);
out.writeLong(generationStamp); out.writeLong(generationStamp);
} }

final void readHelper(DataInput in) throws IOException { final void readHelper(DataInput in) throws IOException {
this.blockId = in.readLong(); this.blockId = in.readLong();
this.numBytes = in.readLong(); this.numBytes = in.readLong();
Expand All @@ -194,7 +194,7 @@ final void readHelper(DataInput in) throws IOException {
throw new IOException("Unexpected block size: " + numBytes); throw new IOException("Unexpected block size: " + numBytes);
} }
} }

// write only the identifier part of the block // write only the identifier part of the block
public void writeId(DataOutput out) throws IOException { public void writeId(DataOutput out) throws IOException {
out.writeLong(blockId); out.writeLong(blockId);
Expand Down Expand Up @@ -223,7 +223,7 @@ public boolean equals(Object o) {
} }
return compareTo((Block)o) == 0; return compareTo((Block)o) == 0;
} }

/** /**
* @return true if the two blocks have the same block ID and the same * @return true if the two blocks have the same block ID and the same
* generation stamp, or if both blocks are null. * generation stamp, or if both blocks are null.
Expand Down
Expand Up @@ -50,7 +50,7 @@ public class DatanodeID implements Comparable<DatanodeID> {


/** /**
* UUID identifying a given datanode. For upgraded Datanodes this is the * UUID identifying a given datanode. For upgraded Datanodes this is the
* same as the StorageID that was previously used by this Datanode. * same as the StorageID that was previously used by this Datanode.
* For newly formatted Datanodes it is a UUID. * For newly formatted Datanodes it is a UUID.
*/ */
private final String datanodeUuid; private final String datanodeUuid;
Expand Down Expand Up @@ -80,7 +80,7 @@ public DatanodeID(String datanodeUuid, DatanodeID from) {
* e.g. if this is a new datanode. A new UUID will * e.g. if this is a new datanode. A new UUID will
* be assigned by the namenode. * be assigned by the namenode.
* @param xferPort data transfer port * @param xferPort data transfer port
* @param infoPort info server port * @param infoPort info server port
* @param ipcPort ipc server port * @param ipcPort ipc server port
*/ */
public DatanodeID(String ipAddr, String hostName, String datanodeUuid, public DatanodeID(String ipAddr, String hostName, String datanodeUuid,
Expand All @@ -92,7 +92,7 @@ public DatanodeID(String ipAddr, String hostName, String datanodeUuid,
this.infoSecurePort = infoSecurePort; this.infoSecurePort = infoSecurePort;
this.ipcPort = ipcPort; this.ipcPort = ipcPort;
} }

public void setIpAddr(String ipAddr) { public void setIpAddr(String ipAddr) {
//updated during registration, preserve former xferPort //updated during registration, preserve former xferPort
setIpAndXferPort(ipAddr, xferPort); setIpAndXferPort(ipAddr, xferPort);
Expand All @@ -108,7 +108,7 @@ private void setIpAndXferPort(String ipAddr, int xferPort) {
public void setPeerHostName(String peerHostName) { public void setPeerHostName(String peerHostName) {
this.peerHostName = peerHostName; this.peerHostName = peerHostName;
} }

/** /**
* @return data node ID. * @return data node ID.
*/ */
Expand Down Expand Up @@ -139,12 +139,12 @@ public String getHostName() {
} }


/** /**
* @return hostname from the actual connection * @return hostname from the actual connection
*/ */
public String getPeerHostName() { public String getPeerHostName() {
return peerHostName; return peerHostName;
} }

/** /**
* @return IP:xferPort string * @return IP:xferPort string
*/ */
Expand Down Expand Up @@ -242,17 +242,17 @@ public boolean equals(Object to) {
return (getXferAddr().equals(((DatanodeID)to).getXferAddr()) && return (getXferAddr().equals(((DatanodeID)to).getXferAddr()) &&
datanodeUuid.equals(((DatanodeID)to).getDatanodeUuid())); datanodeUuid.equals(((DatanodeID)to).getDatanodeUuid()));
} }

@Override @Override
public int hashCode() { public int hashCode() {
return datanodeUuid.hashCode(); return datanodeUuid.hashCode();
} }

@Override @Override
public String toString() { public String toString() {
return getXferAddr(); return getXferAddr();
} }

/** /**
* Update fields when a new registration request comes in. * Update fields when a new registration request comes in.
* Note that this does not update storageID. * Note that this does not update storageID.
Expand All @@ -265,7 +265,7 @@ public void updateRegInfo(DatanodeID nodeReg) {
infoSecurePort = nodeReg.getInfoSecurePort(); infoSecurePort = nodeReg.getInfoSecurePort();
ipcPort = nodeReg.getIpcPort(); ipcPort = nodeReg.getIpcPort();
} }

/** /**
* Compare based on data transfer address. * Compare based on data transfer address.
* *
Expand Down

0 comments on commit 5c97db0

Please sign in to comment.