Java Code Examples for org.apache.ratis.util.FileUtils#move()

The following examples show how to use org.apache.ratis.util.FileUtils#move() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestRaftStorage.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
/**
 * check if RaftStorage deletes tmp metafile when startup
 */
@Test
public void testCleanMetaTmpFile() throws Exception {
  RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR);
  Assert.assertEquals(StorageState.NORMAL, storage.getState());
  storage.close();

  RaftStorageDirectory sd = new RaftStorageDirectory(storageDir);
  File metaFile = sd.getMetaFile();
  FileUtils.move(metaFile, sd.getMetaTmpFile());

  Assert.assertEquals(StorageState.NOT_FORMATTED, sd.analyzeStorage(false));

  try {
    new RaftStorage(storageDir, StartupOption.REGULAR);
    Assert.fail("should throw IOException since storage dir is not formatted");
  } catch (IOException e) {
    Assert.assertTrue(
        e.getMessage().contains(StorageState.NOT_FORMATTED.name()));
  }

  // let the storage dir contain both raft-meta and raft-meta.tmp
  new RaftStorage(storageDir, StartupOption.FORMAT).close();
  Assert.assertTrue(sd.getMetaFile().exists());
  Assert.assertTrue(sd.getMetaTmpFile().createNewFile());
  Assert.assertTrue(sd.getMetaTmpFile().exists());
  try {
    storage = new RaftStorage(storageDir, StartupOption.REGULAR);
    Assert.assertEquals(StorageState.NORMAL, storage.getState());
    Assert.assertFalse(sd.getMetaTmpFile().exists());
    Assert.assertTrue(sd.getMetaFile().exists());
  } finally {
    storage.close();
  }
}
 
Example 2
Source File: TestRaftStorage.java    From ratis with Apache License 2.0 5 votes vote down vote up
/**
 * check if RaftStorage deletes tmp metafile when startup
 */
@Test
public void testCleanMetaTmpFile() throws Exception {
  RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR);
  Assert.assertEquals(StorageState.NORMAL, storage.getState());
  storage.close();

  RaftStorageDirectory sd = new RaftStorageDirectory(storageDir);
  File metaFile = sd.getMetaFile();
  FileUtils.move(metaFile, sd.getMetaTmpFile());

  Assert.assertEquals(StorageState.NOT_FORMATTED, sd.analyzeStorage(false));

  try {
    new RaftStorage(storageDir, StartupOption.REGULAR);
    Assert.fail("should throw IOException since storage dir is not formatted");
  } catch (IOException e) {
    Assert.assertTrue(
        e.getMessage().contains(StorageState.NOT_FORMATTED.name()));
  }

  // let the storage dir contain both raft-meta and raft-meta.tmp
  new RaftStorage(storageDir, StartupOption.FORMAT).close();
  Assert.assertTrue(sd.getMetaFile().exists());
  Assert.assertTrue(sd.getMetaTmpFile().createNewFile());
  Assert.assertTrue(sd.getMetaTmpFile().exists());
  try {
    storage = new RaftStorage(storageDir, StartupOption.REGULAR);
    Assert.assertEquals(StorageState.NORMAL, storage.getState());
    Assert.assertFalse(sd.getMetaTmpFile().exists());
    Assert.assertTrue(sd.getMetaFile().exists());
  } finally {
    storage.close();
  }
}