Skip to content

Commit

Permalink
HDFS-8237. Move all protocol classes used by ClientProtocol to hdfs-c…
Browse files Browse the repository at this point in the history
…lient. Contributed by Haohui Mai.
  • Loading branch information
Haohui Mai committed May 4, 2015
1 parent bf70c5a commit 0d6aa5d
Show file tree
Hide file tree
Showing 21 changed files with 116 additions and 93 deletions.
Expand Up @@ -11,6 +11,9 @@
<Class name="org.apache.hadoop.hdfs.protocol.DirectoryListing"/> <Class name="org.apache.hadoop.hdfs.protocol.DirectoryListing"/>
<Class name="org.apache.hadoop.hdfs.security.token.block.BlockTokenIdentifier"/> <Class name="org.apache.hadoop.hdfs.security.token.block.BlockTokenIdentifier"/>
<Class name="org.apache.hadoop.hdfs.security.token.block.DataEncryptionKey"/> <Class name="org.apache.hadoop.hdfs.security.token.block.DataEncryptionKey"/>
<Class name="org.apache.hadoop.hdfs.protocol.SnapshotDiffReport$DiffReportEntry"/>
<Class name="org.apache.hadoop.hdfs.protocol.SnapshottableDirectoryStatus"/>
<Class name="org.apache.hadoop.hdfs.server.protocol.DatanodeStorageReport"/>
</Or> </Or>
<Bug pattern="EI_EXPOSE_REP,EI_EXPOSE_REP2" /> <Bug pattern="EI_EXPOSE_REP,EI_EXPOSE_REP2" />
</Match> </Match>
Expand Down
Expand Up @@ -19,6 +19,7 @@


import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.primitives.SignedBytes;
import org.apache.commons.io.Charsets; import org.apache.commons.io.Charsets;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation; import org.apache.hadoop.fs.BlockLocation;
Expand All @@ -36,15 +37,19 @@


import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.text.SimpleDateFormat;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;


import static org.apache.hadoop.hdfs.client.HdfsClientConfigKeys.DFS_HA_NAMENODES_KEY_PREFIX; import static org.apache.hadoop.hdfs.client.HdfsClientConfigKeys.DFS_HA_NAMENODES_KEY_PREFIX;
import static org.apache.hadoop.hdfs.client.HdfsClientConfigKeys.DFS_NAMESERVICES; import static org.apache.hadoop.hdfs.client.HdfsClientConfigKeys.DFS_NAMESERVICES;


public class DFSUtilClient { public class DFSUtilClient {
public static final byte[] EMPTY_BYTES = {};
private static final Logger LOG = LoggerFactory.getLogger( private static final Logger LOG = LoggerFactory.getLogger(
DFSUtilClient.class); DFSUtilClient.class);
/** /**
Expand Down Expand Up @@ -184,6 +189,48 @@ public static BlockLocation[] locatedBlocks2Locations(
return blkLocations; return blkLocations;
} }


/** Compare two byte arrays by lexicographical order. */
public static int compareBytes(byte[] left, byte[] right) {
if (left == null) {
left = EMPTY_BYTES;
}
if (right == null) {
right = EMPTY_BYTES;
}
return SignedBytes.lexicographicalComparator().compare(left, right);
}

/**
* Given a list of path components returns a byte array
*/
public static byte[] byteArray2bytes(byte[][] pathComponents) {
if (pathComponents.length == 0) {
return EMPTY_BYTES;
} else if (pathComponents.length == 1
&& (pathComponents[0] == null || pathComponents[0].length == 0)) {
return new byte[]{(byte) Path.SEPARATOR_CHAR};
}
int length = 0;
for (int i = 0; i < pathComponents.length; i++) {
length += pathComponents[i].length;
if (i < pathComponents.length - 1) {
length++; // for SEPARATOR
}
}
byte[] path = new byte[length];
int index = 0;
for (int i = 0; i < pathComponents.length; i++) {
System.arraycopy(pathComponents[i], 0, path, index,
pathComponents[i].length);
index += pathComponents[i].length;
if (i < pathComponents.length - 1) {
path[index] = (byte) Path.SEPARATOR_CHAR;
index++;
}
}
return path;
}

/** /**
* Decode a specific range of bytes of the given byte array to a string * Decode a specific range of bytes of the given byte array to a string
* using UTF8. * using UTF8.
Expand Down Expand Up @@ -343,4 +390,42 @@ public static boolean isValidName(String src) {
} }
return true; return true;
} }

/**
* Converts a time duration in milliseconds into DDD:HH:MM:SS format.
*/
public static String durationToString(long durationMs) {
boolean negative = false;
if (durationMs < 0) {
negative = true;
durationMs = -durationMs;
}
// Chop off the milliseconds
long durationSec = durationMs / 1000;
final int secondsPerMinute = 60;
final int secondsPerHour = 60*60;
final int secondsPerDay = 60*60*24;
final long days = durationSec / secondsPerDay;
durationSec -= days * secondsPerDay;
final long hours = durationSec / secondsPerHour;
durationSec -= hours * secondsPerHour;
final long minutes = durationSec / secondsPerMinute;
durationSec -= minutes * secondsPerMinute;
final long seconds = durationSec;
final long milliseconds = durationMs % 1000;
String format = "%03d:%02d:%02d:%02d.%03d";
if (negative) {
format = "-" + format;
}
return String.format(format, days, hours, minutes, seconds, milliseconds);
}

/**
* Converts a Date into an ISO-8601 formatted datetime string.
*/
public static String dateToIso8601String(Date date) {
SimpleDateFormat df =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.ENGLISH);
return df.format(date);
}
} }
Expand Up @@ -38,6 +38,7 @@ public interface HdfsClientConfigKeys {
int DFS_NAMENODE_HTTPS_PORT_DEFAULT = 50470; int DFS_NAMENODE_HTTPS_PORT_DEFAULT = 50470;
String DFS_NAMENODE_HTTPS_ADDRESS_KEY = "dfs.namenode.https-address"; String DFS_NAMENODE_HTTPS_ADDRESS_KEY = "dfs.namenode.https-address";
String DFS_HA_NAMENODES_KEY_PREFIX = "dfs.ha.namenodes"; String DFS_HA_NAMENODES_KEY_PREFIX = "dfs.ha.namenodes";
int DFS_NAMENODE_RPC_PORT_DEFAULT = 8020;


/** dfs.client.retry configuration properties */ /** dfs.client.retry configuration properties */
interface Retry { interface Retry {
Expand Down
Expand Up @@ -24,9 +24,9 @@
import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DFSUtil;


import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import org.apache.hadoop.hdfs.DFSUtilClient;


/** /**
* Describes a path-based cache directive. * Describes a path-based cache directive.
Expand Down Expand Up @@ -244,9 +244,9 @@ public long getAbsoluteMillis() {
@Override @Override
public String toString() { public String toString() {
if (isRelative) { if (isRelative) {
return DFSUtil.durationToString(ms); return DFSUtilClient.durationToString(ms);
} }
return DFSUtil.dateToIso8601String(new Date(ms)); return DFSUtilClient.dateToIso8601String(new Date(ms));
} }
} }


Expand Down
Expand Up @@ -22,9 +22,9 @@
import java.util.List; import java.util.List;


import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DFSUtil;


import com.google.common.base.Objects; import com.google.common.base.Objects;
import org.apache.hadoop.hdfs.DFSUtilClient;


/** /**
* This class represents to end users the difference between two snapshots of * This class represents to end users the difference between two snapshots of
Expand Down Expand Up @@ -102,8 +102,8 @@ public DiffReportEntry(DiffType type, byte[] sourcePath, byte[] targetPath) {
public DiffReportEntry(DiffType type, byte[][] sourcePathComponents, public DiffReportEntry(DiffType type, byte[][] sourcePathComponents,
byte[][] targetPathComponents) { byte[][] targetPathComponents) {
this.type = type; this.type = type;
this.sourcePath = DFSUtil.byteArray2bytes(sourcePathComponents); this.sourcePath = DFSUtilClient.byteArray2bytes(sourcePathComponents);
this.targetPath = targetPathComponents == null ? null : DFSUtil this.targetPath = targetPathComponents == null ? null : DFSUtilClient
.byteArray2bytes(targetPathComponents); .byteArray2bytes(targetPathComponents);
} }


Expand All @@ -121,7 +121,7 @@ public DiffType getType() {
} }


static String getPathString(byte[] path) { static String getPathString(byte[] path) {
String pathStr = DFSUtil.bytes2String(path); String pathStr = DFSUtilClient.bytes2String(path);
if (pathStr.isEmpty()) { if (pathStr.isEmpty()) {
return Path.CUR_DIR; return Path.CUR_DIR;
} else { } else {
Expand Down
Expand Up @@ -24,7 +24,7 @@


import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.permission.FsPermission; import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.hdfs.DFSUtil; import org.apache.hadoop.hdfs.DFSUtilClient;


/** /**
* Metadata about a snapshottable directory * Metadata about a snapshottable directory
Expand All @@ -36,9 +36,9 @@ public class SnapshottableDirectoryStatus {
@Override @Override
public int compare(SnapshottableDirectoryStatus left, public int compare(SnapshottableDirectoryStatus left,
SnapshottableDirectoryStatus right) { SnapshottableDirectoryStatus right) {
int d = DFSUtil.compareBytes(left.parentFullPath, right.parentFullPath); int d = DFSUtilClient.compareBytes(left.parentFullPath, right.parentFullPath);
return d != 0? d return d != 0? d
: DFSUtil.compareBytes(left.dirStatus.getLocalNameInBytes(), : DFSUtilClient.compareBytes(left.dirStatus.getLocalNameInBytes(),
right.dirStatus.getLocalNameInBytes()); right.dirStatus.getLocalNameInBytes());
} }
}; };
Expand Down Expand Up @@ -101,7 +101,7 @@ public HdfsFileStatus getDirStatus() {
public Path getFullPath() { public Path getFullPath() {
String parentFullPathStr = String parentFullPathStr =
(parentFullPath == null || parentFullPath.length == 0) ? (parentFullPath == null || parentFullPath.length == 0) ?
null : DFSUtil.bytes2String(parentFullPath); null : DFSUtilClient.bytes2String(parentFullPath);
if (parentFullPathStr == null if (parentFullPathStr == null
&& dirStatus.getLocalNameInBytes().length == 0) { && dirStatus.getLocalNameInBytes().length == 0) {
// root // root
Expand Down
Expand Up @@ -22,7 +22,7 @@


import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdfs.server.namenode.NameNode; import org.apache.hadoop.hdfs.client.HdfsClientConfigKeys;
import org.apache.hadoop.io.Text; import org.apache.hadoop.io.Text;
import org.apache.hadoop.net.NetUtils; import org.apache.hadoop.net.NetUtils;
import org.apache.hadoop.security.SecurityUtil; import org.apache.hadoop.security.SecurityUtil;
Expand Down Expand Up @@ -57,7 +57,7 @@ public Token<DelegationTokenIdentifier> selectToken(
Text serviceName = SecurityUtil.buildTokenService(nnUri); Text serviceName = SecurityUtil.buildTokenService(nnUri);
final String nnServiceName = conf.get(SERVICE_NAME_KEY + serviceName); final String nnServiceName = conf.get(SERVICE_NAME_KEY + serviceName);


int nnRpcPort = NameNode.DEFAULT_PORT; int nnRpcPort = HdfsClientConfigKeys.DFS_NAMENODE_RPC_PORT_DEFAULT;
if (nnServiceName != null) { if (nnServiceName != null) {
nnRpcPort = NetUtils.createSocketAddr(nnServiceName, nnRpcPort).getPort(); nnRpcPort = NetUtils.createSocketAddr(nnServiceName, nnRpcPort).getPort();
} }
Expand Down
3 changes: 3 additions & 0 deletions hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt
Expand Up @@ -504,6 +504,9 @@ Release 2.8.0 - UNRELEASED
"dfs.client.read.shortcircuit.streams.cache.size" (Brahma Reddy Battula via "dfs.client.read.shortcircuit.streams.cache.size" (Brahma Reddy Battula via
Colin P. McCabe) Colin P. McCabe)


HDFS-8237. Move all protocol classes used by ClientProtocol to hdfs-client.
(wheat9)

OPTIMIZATIONS OPTIMIZATIONS


HDFS-8026. Trace FSOutputSummer#writeChecksumChunks rather than HDFS-8026. Trace FSOutputSummer#writeChecksumChunks rather than
Expand Down
Expand Up @@ -43,14 +43,12 @@
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
Expand Down Expand Up @@ -97,26 +95,12 @@
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.primitives.SignedBytes;
import com.google.protobuf.BlockingService; import com.google.protobuf.BlockingService;


@InterfaceAudience.Private @InterfaceAudience.Private
public class DFSUtil { public class DFSUtil {
public static final Log LOG = LogFactory.getLog(DFSUtil.class.getName()); public static final Log LOG = LogFactory.getLog(DFSUtil.class.getName());


public static final byte[] EMPTY_BYTES = {};

/** Compare two byte arrays by lexicographical order. */
public static int compareBytes(byte[] left, byte[] right) {
if (left == null) {
left = EMPTY_BYTES;
}
if (right == null) {
right = EMPTY_BYTES;
}
return SignedBytes.lexicographicalComparator().compare(left, right);
}

private DFSUtil() { /* Hidden constructor */ } private DFSUtil() { /* Hidden constructor */ }
private static final ThreadLocal<Random> RANDOM = new ThreadLocal<Random>() { private static final ThreadLocal<Random> RANDOM = new ThreadLocal<Random>() {
@Override @Override
Expand Down Expand Up @@ -345,37 +329,6 @@ public static String strings2PathString(String[] components) {
return Joiner.on(Path.SEPARATOR).join(components); return Joiner.on(Path.SEPARATOR).join(components);
} }


/**
* Given a list of path components returns a byte array
*/
public static byte[] byteArray2bytes(byte[][] pathComponents) {
if (pathComponents.length == 0) {
return EMPTY_BYTES;
} else if (pathComponents.length == 1
&& (pathComponents[0] == null || pathComponents[0].length == 0)) {
return new byte[]{(byte) Path.SEPARATOR_CHAR};
}
int length = 0;
for (int i = 0; i < pathComponents.length; i++) {
length += pathComponents[i].length;
if (i < pathComponents.length - 1) {
length++; // for SEPARATOR
}
}
byte[] path = new byte[length];
int index = 0;
for (int i = 0; i < pathComponents.length; i++) {
System.arraycopy(pathComponents[i], 0, path, index,
pathComponents[i].length);
index += pathComponents[i].length;
if (i < pathComponents.length - 1) {
path[index] = (byte) Path.SEPARATOR_CHAR;
index++;
}
}
return path;
}

/** Convert an object representing a path to a string. */ /** Convert an object representing a path to a string. */
public static String path2String(final Object path) { public static String path2String(final Object path) {
return path == null? null return path == null? null
Expand Down Expand Up @@ -1377,38 +1330,14 @@ static String getPassword(Configuration conf, String alias) {
* Converts a Date into an ISO-8601 formatted datetime string. * Converts a Date into an ISO-8601 formatted datetime string.
*/ */
public static String dateToIso8601String(Date date) { public static String dateToIso8601String(Date date) {
SimpleDateFormat df = return DFSUtilClient.dateToIso8601String(date);
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.ENGLISH);
return df.format(date);
} }


/** /**
* Converts a time duration in milliseconds into DDD:HH:MM:SS format. * Converts a time duration in milliseconds into DDD:HH:MM:SS format.
*/ */
public static String durationToString(long durationMs) { public static String durationToString(long durationMs) {
boolean negative = false; return DFSUtilClient.durationToString(durationMs);
if (durationMs < 0) {
negative = true;
durationMs = -durationMs;
}
// Chop off the milliseconds
long durationSec = durationMs / 1000;
final int secondsPerMinute = 60;
final int secondsPerHour = 60*60;
final int secondsPerDay = 60*60*24;
final long days = durationSec / secondsPerDay;
durationSec -= days * secondsPerDay;
final long hours = durationSec / secondsPerHour;
durationSec -= hours * secondsPerHour;
final long minutes = durationSec / secondsPerMinute;
durationSec -= minutes * secondsPerMinute;
final long seconds = durationSec;
final long milliseconds = durationMs % 1000;
String format = "%03d:%02d:%02d:%02d.%03d";
if (negative) {
format = "-" + format;
}
return String.format(format, days, hours, minutes, seconds, milliseconds);
} }


/** /**
Expand Down

0 comments on commit 0d6aa5d

Please sign in to comment.