Permalink
Browse files

Added one unit test to ReadOnlyStorageEngine

  • Loading branch information...
1 parent 96232be commit dbf8071efa92ab85a18c229964b6147f761acbc1 @rsumbaly rsumbaly committed Aug 12, 2010
@@ -58,7 +58,6 @@
private static final int REPORTING_INTERVAL_BYTES = 100 * 1024 * 1024;
private static final int DEFAULT_BUFFER_SIZE = 64 * 1024;
- private File tempDir;
private final Long maxBytesPerSecond;
private final int bufferSize;
private static final AtomicInteger copyCount = new AtomicInteger(0);
@@ -69,8 +68,8 @@ public HdfsFetcher(Props props) {
this(props.containsKey("fetcher.max.bytes.per.sec") ? props.getBytes("fetcher.max.bytes.per.sec")
: null,
(int) props.getBytes("hdfs.fetcher.buffer.size", DEFAULT_BUFFER_SIZE));
- logger.info("Created hdfs fetcher with temp dir = " + tempDir.getAbsolutePath()
- + " and throttle rate " + maxBytesPerSecond + " and buffer size " + bufferSize);
+ logger.info("Created hdfs fetcher with throttle rate " + maxBytesPerSecond
+ + " and buffer size " + bufferSize);
}
public HdfsFetcher() {
@@ -98,9 +97,6 @@ public File fetch(String sourceFileUrl, String destinationFile, String storeName
ObjectName jmxName = JmxUtils.registerMbean("hdfs-copy-" + copyCount.getAndIncrement(),
stats);
try {
- File storeDir = new File(this.tempDir, storeName + "_" + System.currentTimeMillis());
- Utils.mkdirs(storeDir);
-
File destination = new File(destinationFile);
boolean result = fetch(fs, path, destination, stats);
if(result) {
@@ -174,10 +174,19 @@ public String getStoreDirPath() {
return storeDir.getAbsolutePath();
}
+ private boolean checkVersionDirName(File dir) {
+ if(dir.isDirectory() && dir.getName().contains("version-")
+ && !dir.getName().endsWith(".bak")) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
private File setMaxVersion(File[] versionDirs) {
int max = 0;
for(File versionDir: versionDirs) {
- if(versionDir.isDirectory() && versionDir.getName().contains("version-")) {
+ if(checkVersionDirName(versionDir)) {
int version = Integer.parseInt(versionDir.getName().replace("version-", ""));
if(version > max) {
max = version;
@@ -189,7 +198,7 @@ private File setMaxVersion(File[] versionDirs) {
}
private File setMaxVersion(File versionDir) {
- if(versionDir.isDirectory() && versionDir.getName().contains("version-")) {
+ if(checkVersionDirName(versionDir)) {
maxVersion = Integer.parseInt(versionDir.getName().replace("version-", ""));
}
return new File(storeDir, "version-" + maxVersion);
@@ -229,9 +238,10 @@ public void swapFiles(String newStoreDirectory) {
throw new VoldemortException("File " + newDataDir.getAbsolutePath()
+ " does not exist.");
- if(!newDataDir.getName().startsWith("version-"))
- throw new VoldemortException("Invalid version folder name '" + newDataDir.getName()
- + "'. Should be of the format 'version-n'");
+ if(!(newDataDir.getParentFile().compareTo(storeDir.getAbsoluteFile()) == 0 && checkVersionDirName(newDataDir)))
+ throw new VoldemortException("Invalid version folder name '"
+ + newDataDir
+ + "'. Either parent directory is incorrect or format(version-n) is incorrect");
logger.info("Acquiring write lock on '" + getName() + "':");
fileModificationLock.writeLock().lock();
@@ -255,17 +255,28 @@ public void testSwap() throws IOException {
assertVersionsExist(dir, 0);
}
- @Test(expected = VoldemortException.class)
+ @Test
public void testBadSwapNameThrows() throws IOException {
File versionDir = new File(dir, "version-0");
createStoreFiles(versionDir, ReadOnlyUtils.INDEX_ENTRY_SIZE * 5, 4 * 5 * 10, 2);
ReadOnlyStorageEngine engine = new ReadOnlyStorageEngine("test", strategy, dir, 2);
assertVersionsExist(dir, 0);
- // swap to a directory with bad name
+ // swap to a directory with an incorrect parent directory
File newDir = TestUtils.createTempDir();
createStoreFiles(newDir, 73, 1024, 2);
- engine.swapFiles(newDir.getAbsolutePath());
+ try {
+ engine.swapFiles(newDir.getAbsolutePath());
+ fail("Swap files should have failed since parent directory is incorrect");
+ } catch(VoldemortException e) {}
+
+ // swap to a directory with incorrect name
+ newDir = new File(dir, "blah");
+ createStoreFiles(newDir, 73, 1024, 2);
+ try {
+ engine.swapFiles(newDir.getAbsolutePath());
+ fail("Swap files should have failed since name is incorrect");
+ } catch(VoldemortException e) {}
}
@Test(expected = VoldemortException.class)

0 comments on commit dbf8071

Please sign in to comment.