HDFS-9252. Change TestFileTruncate to use FsDatasetTestUtils to get block file size and genstamp. (Lei (Eddy) Xu via cmccabe)

(cherry picked from commit dfbde3fc51)
This commit is contained in:
Colin Patrick Mccabe 2015-11-17 10:48:15 -08:00
parent 2babf12810
commit 0f9f9ba3cb
5 changed files with 55 additions and 20 deletions

View File

@ -795,6 +795,9 @@ Release 2.8.0 - UNRELEASED
HDFS-9369. Use ctest to run tests for hadoop-hdfs-native-client. (wheat9) HDFS-9369. Use ctest to run tests for hadoop-hdfs-native-client. (wheat9)
HDFS-9252. Change TestFileTruncate to use FsDatasetTestUtils to get block
file size and genstamp. (Lei (Eddy) Xu via cmccabe)
OPTIMIZATIONS OPTIMIZATIONS
HDFS-8026. Trace FSOutputSummer#writeChecksumChunks rather than HDFS-8026. Trace FSOutputSummer#writeChecksumChunks rather than

View File

@ -75,14 +75,15 @@ public class FsDatasetUtil {
* Find the meta-file for the specified block file * Find the meta-file for the specified block file
* and then return the generation stamp from the name of the meta-file. * and then return the generation stamp from the name of the meta-file.
*/ */
static long getGenerationStampFromFile(File[] listdir, File blockFile) { static long getGenerationStampFromFile(File[] listdir, File blockFile)
throws IOException {
String blockName = blockFile.getName(); String blockName = blockFile.getName();
for (int j = 0; j < listdir.length; j++) { for (int j = 0; j < listdir.length; j++) {
String path = listdir[j].getName(); String path = listdir[j].getName();
if (!path.startsWith(blockName)) { if (!path.startsWith(blockName)) {
continue; continue;
} }
if (blockFile == listdir[j]) { if (blockFile.getCanonicalPath().equals(listdir[j].getCanonicalPath())) {
continue; continue;
} }
return Block.getGenerationStamp(listdir[j].getName()); return Block.getGenerationStamp(listdir[j].getName());

View File

@ -232,4 +232,14 @@ public interface FsDatasetTestUtils {
* Obtain the raw capacity of underlying storage per DataNode. * Obtain the raw capacity of underlying storage per DataNode.
*/ */
long getRawCapacity() throws IOException; long getRawCapacity() throws IOException;
/**
* Get the persistently stored length of the block.
*/
long getStoredDataLength(ExtendedBlock block) throws IOException;
/**
* Get the persistently stored generation stamp.
*/
long getStoredGenerationStamp(ExtendedBlock block) throws IOException;
} }

View File

@ -26,6 +26,7 @@ import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.fs.DF; import org.apache.hadoop.fs.DF;
import org.apache.hadoop.hdfs.protocol.Block; import org.apache.hadoop.hdfs.protocol.Block;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.hdfs.protocol.ExtendedBlock; import org.apache.hadoop.hdfs.protocol.ExtendedBlock;
import org.apache.hadoop.hdfs.server.datanode.DataNode; import org.apache.hadoop.hdfs.server.datanode.DataNode;
import org.apache.hadoop.hdfs.server.datanode.FinalizedReplica; import org.apache.hadoop.hdfs.server.datanode.FinalizedReplica;
@ -175,6 +176,10 @@ public class FsDatasetImplTestUtils implements FsDatasetTestUtils {
dataset = (FsDatasetImpl) datanode.getFSDataset(); dataset = (FsDatasetImpl) datanode.getFSDataset();
} }
private File getBlockFile(ExtendedBlock eb) throws IOException {
return dataset.getBlockFile(eb.getBlockPoolId(), eb.getBlockId());
}
/** /**
* Return a materialized replica from the FsDatasetImpl. * Return a materialized replica from the FsDatasetImpl.
*/ */
@ -235,7 +240,6 @@ public class FsDatasetImplTestUtils implements FsDatasetTestUtils {
return rip; return rip;
} }
@Override @Override
public Replica createRBW(ExtendedBlock eb) throws IOException { public Replica createRBW(ExtendedBlock eb) throws IOException {
try (FsVolumeReferences volumes = dataset.getFsVolumeReferences()) { try (FsVolumeReferences volumes = dataset.getFsVolumeReferences()) {
@ -343,4 +347,20 @@ public class FsDatasetImplTestUtils implements FsDatasetTestUtils {
return df.getCapacity(); return df.getCapacity();
} }
} }
@Override
public long getStoredDataLength(ExtendedBlock block) throws IOException {
File f = getBlockFile(block);
try (RandomAccessFile raf = new RandomAccessFile(f, "r")) {
return raf.length();
}
}
@Override
public long getStoredGenerationStamp(ExtendedBlock block) throws IOException {
File f = getBlockFile(block);
File dir = f.getParentFile();
File[] files = FileUtil.listFiles(dir);
return FsDatasetUtil.getGenerationStampFromFile(files, f);
}
} }

View File

@ -58,6 +58,7 @@ import org.apache.hadoop.hdfs.protocol.LocatedBlock;
import org.apache.hadoop.hdfs.protocol.LocatedBlocks; import org.apache.hadoop.hdfs.protocol.LocatedBlocks;
import org.apache.hadoop.hdfs.server.common.HdfsServerConstants; import org.apache.hadoop.hdfs.server.common.HdfsServerConstants;
import org.apache.hadoop.hdfs.server.common.HdfsServerConstants.StartupOption; import org.apache.hadoop.hdfs.server.common.HdfsServerConstants.StartupOption;
import org.apache.hadoop.hdfs.server.datanode.FsDatasetTestUtils;
import org.apache.hadoop.net.ServerSocketUtil; import org.apache.hadoop.net.ServerSocketUtil;
import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.test.GenericTestUtils; import org.apache.hadoop.test.GenericTestUtils;
@ -698,11 +699,11 @@ public class TestFileTruncate {
// Wait replicas come to 3 // Wait replicas come to 3
DFSTestUtil.waitReplication(fs, p, REPLICATION); DFSTestUtil.waitReplication(fs, p, REPLICATION);
// Old replica is disregarded and replaced with the truncated one // Old replica is disregarded and replaced with the truncated one
assertEquals(cluster.getBlockFile(dn, newBlock.getBlock()).length(), FsDatasetTestUtils utils = cluster.getFsDatasetTestUtils(dn);
assertEquals(utils.getStoredDataLength(newBlock.getBlock()),
newBlock.getBlockSize()); newBlock.getBlockSize());
assertTrue(cluster.getBlockMetadataFile(dn, assertEquals(utils.getStoredGenerationStamp(newBlock.getBlock()),
newBlock.getBlock()).getName().endsWith( newBlock.getBlock().getGenerationStamp());
newBlock.getBlock().getGenerationStamp() + ".meta"));
// Validate the file // Validate the file
FileStatus fileStatus = fs.getFileStatus(p); FileStatus fileStatus = fs.getFileStatus(p);
@ -752,15 +753,15 @@ public class TestFileTruncate {
// Wait replicas come to 3 // Wait replicas come to 3
DFSTestUtil.waitReplication(fs, p, REPLICATION); DFSTestUtil.waitReplication(fs, p, REPLICATION);
FsDatasetTestUtils utils = cluster.getFsDatasetTestUtils(dn);
// New block is replicated to dn1 // New block is replicated to dn1
assertEquals(cluster.getBlockFile(dn, newBlock.getBlock()).length(), assertEquals(utils.getStoredDataLength(newBlock.getBlock()),
newBlock.getBlockSize()); newBlock.getBlockSize());
// Old replica exists too since there is snapshot // Old replica exists too since there is snapshot
assertEquals(cluster.getBlockFile(dn, oldBlock.getBlock()).length(), assertEquals(utils.getStoredDataLength(oldBlock.getBlock()),
oldBlock.getBlockSize()); oldBlock.getBlockSize());
assertTrue(cluster.getBlockMetadataFile(dn, assertEquals(utils.getStoredGenerationStamp(oldBlock.getBlock()),
oldBlock.getBlock()).getName().endsWith( oldBlock.getBlock().getGenerationStamp());
oldBlock.getBlock().getGenerationStamp() + ".meta"));
// Validate the file // Validate the file
FileStatus fileStatus = fs.getFileStatus(p); FileStatus fileStatus = fs.getFileStatus(p);
@ -812,18 +813,18 @@ public class TestFileTruncate {
// Wait replicas come to 3 // Wait replicas come to 3
DFSTestUtil.waitReplication(fs, p, REPLICATION); DFSTestUtil.waitReplication(fs, p, REPLICATION);
// Old replica is disregarded and replaced with the truncated one on dn0 // Old replica is disregarded and replaced with the truncated one on dn0
assertEquals(cluster.getBlockFile(dn0, newBlock.getBlock()).length(), FsDatasetTestUtils utils = cluster.getFsDatasetTestUtils(dn0);
assertEquals(utils.getStoredDataLength(newBlock.getBlock()),
newBlock.getBlockSize()); newBlock.getBlockSize());
assertTrue(cluster.getBlockMetadataFile(dn0, assertEquals(utils.getStoredGenerationStamp(newBlock.getBlock()),
newBlock.getBlock()).getName().endsWith( newBlock.getBlock().getGenerationStamp());
newBlock.getBlock().getGenerationStamp() + ".meta"));
// Old replica is disregarded and replaced with the truncated one on dn1 // Old replica is disregarded and replaced with the truncated one on dn1
assertEquals(cluster.getBlockFile(dn1, newBlock.getBlock()).length(), utils = cluster.getFsDatasetTestUtils(dn1);
assertEquals(utils.getStoredDataLength(newBlock.getBlock()),
newBlock.getBlockSize()); newBlock.getBlockSize());
assertTrue(cluster.getBlockMetadataFile(dn1, assertEquals(utils.getStoredGenerationStamp(newBlock.getBlock()),
newBlock.getBlock()).getName().endsWith( newBlock.getBlock().getGenerationStamp());
newBlock.getBlock().getGenerationStamp() + ".meta"));
// Validate the file // Validate the file
FileStatus fileStatus = fs.getFileStatus(p); FileStatus fileStatus = fs.getFileStatus(p);