Java Code Examples for org.apache.hadoop.hdfs.server.namenode.INodeDirectory#replaceChild()

The following examples show how to use org.apache.hadoop.hdfs.server.namenode.INodeDirectory#replaceChild() . 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: TestRenameWithSnapshots.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * Test the undo section of rename. Before the rename, we create the renamed 
 * file/dir before taking the snapshot.
 */
@Test
public void testRenameUndo_1() throws Exception {
  final Path sdir1 = new Path("/dir1");
  final Path sdir2 = new Path("/dir2");
  hdfs.mkdirs(sdir1);
  hdfs.mkdirs(sdir2);
  final Path foo = new Path(sdir1, "foo");
  final Path bar = new Path(foo, "bar");
  DFSTestUtil.createFile(hdfs, bar, BLOCKSIZE, REPL, SEED);
  final Path dir2file = new Path(sdir2, "file");
  DFSTestUtil.createFile(hdfs, dir2file, BLOCKSIZE, REPL, SEED);
  
  SnapshotTestHelper.createSnapshot(hdfs, sdir1, "s1");
  
  INodeDirectory dir2 = fsdir.getINode4Write(sdir2.toString()).asDirectory();
  INodeDirectory mockDir2 = spy(dir2);
  doReturn(false).when(mockDir2).addChild((INode) anyObject(), anyBoolean(),
         Mockito.anyInt());
  INodeDirectory root = fsdir.getINode4Write("/").asDirectory();
  root.replaceChild(dir2, mockDir2, fsdir.getINodeMap());
  
  final Path newfoo = new Path(sdir2, "foo");
  boolean result = hdfs.rename(foo, newfoo);
  assertFalse(result);
  
  // check the current internal details
  INodeDirectory dir1Node = fsdir.getINode4Write(sdir1.toString())
      .asDirectory();
  Snapshot s1 = dir1Node.getSnapshot(DFSUtil.string2Bytes("s1"));
  ReadOnlyList<INode> dir1Children = dir1Node
      .getChildrenList(Snapshot.CURRENT_STATE_ID);
  assertEquals(1, dir1Children.size());
  assertEquals(foo.getName(), dir1Children.get(0).getLocalName());
  List<DirectoryDiff> dir1Diffs = dir1Node.getDiffs().asList();
  assertEquals(1, dir1Diffs.size());
  assertEquals(s1.getId(), dir1Diffs.get(0).getSnapshotId());
  
  // after the undo of rename, both the created and deleted list of sdir1
  // should be empty
  ChildrenDiff childrenDiff = dir1Diffs.get(0).getChildrenDiff();
  assertEquals(0, childrenDiff.getList(ListType.DELETED).size());
  assertEquals(0, childrenDiff.getList(ListType.CREATED).size());
  
  INode fooNode = fsdir.getINode4Write(foo.toString());
  assertTrue(fooNode.isDirectory() && fooNode.asDirectory().isWithSnapshot());
  List<DirectoryDiff> fooDiffs = fooNode.asDirectory().getDiffs().asList();
  assertEquals(1, fooDiffs.size());
  assertEquals(s1.getId(), fooDiffs.get(0).getSnapshotId());
  
  final Path foo_s1 = SnapshotTestHelper.getSnapshotPath(sdir1, "s1", "foo");
  INode fooNode_s1 = fsdir.getINode(foo_s1.toString());
  assertTrue(fooNode_s1 == fooNode);
  
  // check sdir2
  assertFalse(hdfs.exists(newfoo));
  INodeDirectory dir2Node = fsdir.getINode4Write(sdir2.toString())
      .asDirectory();
  assertFalse(dir2Node.isWithSnapshot());
  ReadOnlyList<INode> dir2Children = dir2Node
      .getChildrenList(Snapshot.CURRENT_STATE_ID);
  assertEquals(1, dir2Children.size());
  assertEquals(dir2file.getName(), dir2Children.get(0).getLocalName());
}
 
Example 2
Source File: TestRenameWithSnapshots.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * Test the undo section of rename. Before the rename, we create the renamed 
 * file/dir after taking the snapshot.
 */
@Test
public void testRenameUndo_2() throws Exception {
  final Path sdir1 = new Path("/dir1");
  final Path sdir2 = new Path("/dir2");
  hdfs.mkdirs(sdir1);
  hdfs.mkdirs(sdir2);
  final Path dir2file = new Path(sdir2, "file");
  DFSTestUtil.createFile(hdfs, dir2file, BLOCKSIZE, REPL, SEED);
  
  SnapshotTestHelper.createSnapshot(hdfs, sdir1, "s1");
  
  // create foo after taking snapshot
  final Path foo = new Path(sdir1, "foo");
  final Path bar = new Path(foo, "bar");
  DFSTestUtil.createFile(hdfs, bar, BLOCKSIZE, REPL, SEED);
  
  INodeDirectory dir2 = fsdir.getINode4Write(sdir2.toString()).asDirectory();
  INodeDirectory mockDir2 = spy(dir2);
  doReturn(false).when(mockDir2).addChild((INode) anyObject(), anyBoolean(),
          Mockito.anyInt());
  INodeDirectory root = fsdir.getINode4Write("/").asDirectory();
  root.replaceChild(dir2, mockDir2, fsdir.getINodeMap());
  
  final Path newfoo = new Path(sdir2, "foo");
  boolean result = hdfs.rename(foo, newfoo);
  assertFalse(result);
  
  // check the current internal details
  INodeDirectory dir1Node = fsdir.getINode4Write(sdir1.toString())
      .asDirectory();
  Snapshot s1 = dir1Node.getSnapshot(DFSUtil.string2Bytes("s1"));
  ReadOnlyList<INode> dir1Children = dir1Node
      .getChildrenList(Snapshot.CURRENT_STATE_ID);
  assertEquals(1, dir1Children.size());
  assertEquals(foo.getName(), dir1Children.get(0).getLocalName());
  List<DirectoryDiff> dir1Diffs = dir1Node.getDiffs().asList();
  assertEquals(1, dir1Diffs.size());
  assertEquals(s1.getId(), dir1Diffs.get(0).getSnapshotId());
  
  // after the undo of rename, the created list of sdir1 should contain 
  // 1 element
  ChildrenDiff childrenDiff = dir1Diffs.get(0).getChildrenDiff();
  assertEquals(0, childrenDiff.getList(ListType.DELETED).size());
  assertEquals(1, childrenDiff.getList(ListType.CREATED).size());
  
  INode fooNode = fsdir.getINode4Write(foo.toString());
  assertTrue(fooNode instanceof INodeDirectory);
  assertTrue(childrenDiff.getList(ListType.CREATED).get(0) == fooNode);
  
  final Path foo_s1 = SnapshotTestHelper.getSnapshotPath(sdir1, "s1", "foo");
  assertFalse(hdfs.exists(foo_s1));
  
  // check sdir2
  assertFalse(hdfs.exists(newfoo));
  INodeDirectory dir2Node = fsdir.getINode4Write(sdir2.toString())
      .asDirectory();
  assertFalse(dir2Node.isWithSnapshot());
  ReadOnlyList<INode> dir2Children = dir2Node
      .getChildrenList(Snapshot.CURRENT_STATE_ID);
  assertEquals(1, dir2Children.size());
  assertEquals(dir2file.getName(), dir2Children.get(0).getLocalName());
}
 
Example 3
Source File: TestRenameWithSnapshots.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * Test the undo section of the second-time rename.
 */
@Test
public void testRenameUndo_3() throws Exception {
  final Path sdir1 = new Path("/dir1");
  final Path sdir2 = new Path("/dir2");
  final Path sdir3 = new Path("/dir3");
  hdfs.mkdirs(sdir1);
  hdfs.mkdirs(sdir2);
  hdfs.mkdirs(sdir3);
  final Path foo = new Path(sdir1, "foo");
  final Path bar = new Path(foo, "bar");
  DFSTestUtil.createFile(hdfs, bar, BLOCKSIZE, REPL, SEED);
  
  SnapshotTestHelper.createSnapshot(hdfs, sdir1, "s1");
  SnapshotTestHelper.createSnapshot(hdfs, sdir2, "s2");
  
  INodeDirectory dir3 = fsdir.getINode4Write(sdir3.toString()).asDirectory();
  INodeDirectory mockDir3 = spy(dir3);
  doReturn(false).when(mockDir3).addChild((INode) anyObject(), anyBoolean(),
          Mockito.anyInt());
  INodeDirectory root = fsdir.getINode4Write("/").asDirectory();
  root.replaceChild(dir3, mockDir3, fsdir.getINodeMap());
  
  final Path foo_dir2 = new Path(sdir2, "foo2");
  final Path foo_dir3 = new Path(sdir3, "foo3");
  hdfs.rename(foo, foo_dir2);
  boolean result = hdfs.rename(foo_dir2, foo_dir3);
  assertFalse(result);
  
  // check the current internal details
  INodeDirectory dir1Node = fsdir.getINode4Write(sdir1.toString())
      .asDirectory();
  Snapshot s1 = dir1Node.getSnapshot(DFSUtil.string2Bytes("s1"));
  INodeDirectory dir2Node = fsdir.getINode4Write(sdir2.toString())
      .asDirectory();
  Snapshot s2 = dir2Node.getSnapshot(DFSUtil.string2Bytes("s2"));
  ReadOnlyList<INode> dir2Children = dir2Node
      .getChildrenList(Snapshot.CURRENT_STATE_ID);
  assertEquals(1, dir2Children.size());
  List<DirectoryDiff> dir2Diffs = dir2Node.getDiffs().asList();
  assertEquals(1, dir2Diffs.size());
  assertEquals(s2.getId(), dir2Diffs.get(0).getSnapshotId());
  ChildrenDiff childrenDiff = dir2Diffs.get(0).getChildrenDiff();
  assertEquals(0, childrenDiff.getList(ListType.DELETED).size());
  assertEquals(1, childrenDiff.getList(ListType.CREATED).size());
  final Path foo_s2 = SnapshotTestHelper.getSnapshotPath(sdir2, "s2", "foo2");
  assertFalse(hdfs.exists(foo_s2));
  
  INode fooNode = fsdir.getINode4Write(foo_dir2.toString());
  assertTrue(childrenDiff.getList(ListType.CREATED).get(0) == fooNode);
  assertTrue(fooNode instanceof INodeReference.DstReference);
  List<DirectoryDiff> fooDiffs = fooNode.asDirectory().getDiffs().asList();
  assertEquals(1, fooDiffs.size());
  assertEquals(s1.getId(), fooDiffs.get(0).getSnapshotId());
  
  // create snapshot on sdir2 and rename again
  hdfs.createSnapshot(sdir2, "s3");
  result = hdfs.rename(foo_dir2, foo_dir3);
  assertFalse(result);

  // check internal details again
  dir2Node = fsdir.getINode4Write(sdir2.toString()).asDirectory();
  Snapshot s3 = dir2Node.getSnapshot(DFSUtil.string2Bytes("s3"));
  fooNode = fsdir.getINode4Write(foo_dir2.toString());
  dir2Children = dir2Node.getChildrenList(Snapshot.CURRENT_STATE_ID);
  assertEquals(1, dir2Children.size());
  dir2Diffs = dir2Node.getDiffs().asList();
  assertEquals(2, dir2Diffs.size());
  assertEquals(s2.getId(), dir2Diffs.get(0).getSnapshotId());
  assertEquals(s3.getId(), dir2Diffs.get(1).getSnapshotId());
  
  childrenDiff = dir2Diffs.get(0).getChildrenDiff();
  assertEquals(0, childrenDiff.getList(ListType.DELETED).size());
  assertEquals(1, childrenDiff.getList(ListType.CREATED).size());
  assertTrue(childrenDiff.getList(ListType.CREATED).get(0) == fooNode);
  
  childrenDiff = dir2Diffs.get(1).getChildrenDiff();
  assertEquals(0, childrenDiff.getList(ListType.DELETED).size());
  assertEquals(0, childrenDiff.getList(ListType.CREATED).size());
  
  final Path foo_s3 = SnapshotTestHelper.getSnapshotPath(sdir2, "s3", "foo2");
  assertFalse(hdfs.exists(foo_s2));
  assertTrue(hdfs.exists(foo_s3));
  
  assertTrue(fooNode instanceof INodeReference.DstReference);
  fooDiffs = fooNode.asDirectory().getDiffs().asList();
  assertEquals(2, fooDiffs.size());
  assertEquals(s1.getId(), fooDiffs.get(0).getSnapshotId());
  assertEquals(s3.getId(), fooDiffs.get(1).getSnapshotId());
}
 
Example 4
Source File: TestRenameWithSnapshots.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * Test undo where dst node being overwritten is a reference node
 */
@Test
public void testRenameUndo_4() throws Exception {
  final Path sdir1 = new Path("/dir1");
  final Path sdir2 = new Path("/dir2");
  final Path sdir3 = new Path("/dir3");
  hdfs.mkdirs(sdir1);
  hdfs.mkdirs(sdir2);
  hdfs.mkdirs(sdir3);
  
  final Path foo = new Path(sdir1, "foo");
  final Path bar = new Path(foo, "bar");
  DFSTestUtil.createFile(hdfs, bar, BLOCKSIZE, REPL, SEED);
  
  final Path foo2 = new Path(sdir2, "foo2");
  hdfs.mkdirs(foo2);
  
  SnapshotTestHelper.createSnapshot(hdfs, sdir1, "s1");
  SnapshotTestHelper.createSnapshot(hdfs, sdir2, "s2");
  
  // rename foo2 to foo3, so that foo3 will be a reference node
  final Path foo3 = new Path(sdir3, "foo3");
  hdfs.rename(foo2, foo3);
  
  INode foo3Node = fsdir.getINode4Write(foo3.toString());
  assertTrue(foo3Node.isReference());
  
  INodeDirectory dir3 = fsdir.getINode4Write(sdir3.toString()).asDirectory();
  INodeDirectory mockDir3 = spy(dir3);
  // fail the rename but succeed in undo
  doReturn(false).when(mockDir3).addChild((INode) Mockito.isNull(),
      anyBoolean(), Mockito.anyInt());
  Mockito.when(mockDir3.addChild((INode) Mockito.isNotNull(), anyBoolean(), 
      Mockito.anyInt())).thenReturn(false).thenCallRealMethod();
  INodeDirectory root = fsdir.getINode4Write("/").asDirectory();
  root.replaceChild(dir3, mockDir3, fsdir.getINodeMap());
  foo3Node.setParent(mockDir3);
  
  try {
    hdfs.rename(foo, foo3, Rename.OVERWRITE);
    fail("the rename from " + foo + " to " + foo3 + " should fail");
  } catch (IOException e) {
    GenericTestUtils.assertExceptionContains("rename from " + foo + " to "
        + foo3 + " failed.", e);
  }
  
  // make sure the undo is correct
  final INode foo3Node_undo = fsdir.getINode4Write(foo3.toString());
  assertSame(foo3Node, foo3Node_undo);
  INodeReference.WithCount foo3_wc = (WithCount) foo3Node.asReference()
      .getReferredINode();
  assertEquals(2, foo3_wc.getReferenceCount());
  assertSame(foo3Node, foo3_wc.getParentReference());
}
 
Example 5
Source File: TestRenameWithSnapshots.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * Test the undo section of rename. Before the rename, we create the renamed 
 * file/dir before taking the snapshot.
 */
@Test
public void testRenameUndo_1() throws Exception {
  final Path sdir1 = new Path("/dir1");
  final Path sdir2 = new Path("/dir2");
  hdfs.mkdirs(sdir1);
  hdfs.mkdirs(sdir2);
  final Path foo = new Path(sdir1, "foo");
  final Path bar = new Path(foo, "bar");
  DFSTestUtil.createFile(hdfs, bar, BLOCKSIZE, REPL, SEED);
  final Path dir2file = new Path(sdir2, "file");
  DFSTestUtil.createFile(hdfs, dir2file, BLOCKSIZE, REPL, SEED);
  
  SnapshotTestHelper.createSnapshot(hdfs, sdir1, "s1");
  
  INodeDirectory dir2 = fsdir.getINode4Write(sdir2.toString()).asDirectory();
  INodeDirectory mockDir2 = spy(dir2);
  doReturn(false).when(mockDir2).addChild((INode) anyObject(), anyBoolean(),
         Mockito.anyInt());
  INodeDirectory root = fsdir.getINode4Write("/").asDirectory();
  root.replaceChild(dir2, mockDir2, fsdir.getINodeMap());
  
  final Path newfoo = new Path(sdir2, "foo");
  boolean result = hdfs.rename(foo, newfoo);
  assertFalse(result);
  
  // check the current internal details
  INodeDirectory dir1Node = fsdir.getINode4Write(sdir1.toString())
      .asDirectory();
  Snapshot s1 = dir1Node.getSnapshot(DFSUtil.string2Bytes("s1"));
  ReadOnlyList<INode> dir1Children = dir1Node
      .getChildrenList(Snapshot.CURRENT_STATE_ID);
  assertEquals(1, dir1Children.size());
  assertEquals(foo.getName(), dir1Children.get(0).getLocalName());
  List<DirectoryDiff> dir1Diffs = dir1Node.getDiffs().asList();
  assertEquals(1, dir1Diffs.size());
  assertEquals(s1.getId(), dir1Diffs.get(0).getSnapshotId());
  
  // after the undo of rename, both the created and deleted list of sdir1
  // should be empty
  ChildrenDiff childrenDiff = dir1Diffs.get(0).getChildrenDiff();
  assertEquals(0, childrenDiff.getList(ListType.DELETED).size());
  assertEquals(0, childrenDiff.getList(ListType.CREATED).size());
  
  INode fooNode = fsdir.getINode4Write(foo.toString());
  assertTrue(fooNode.isDirectory() && fooNode.asDirectory().isWithSnapshot());
  List<DirectoryDiff> fooDiffs = fooNode.asDirectory().getDiffs().asList();
  assertEquals(1, fooDiffs.size());
  assertEquals(s1.getId(), fooDiffs.get(0).getSnapshotId());
  
  final Path foo_s1 = SnapshotTestHelper.getSnapshotPath(sdir1, "s1", "foo");
  INode fooNode_s1 = fsdir.getINode(foo_s1.toString());
  assertTrue(fooNode_s1 == fooNode);
  
  // check sdir2
  assertFalse(hdfs.exists(newfoo));
  INodeDirectory dir2Node = fsdir.getINode4Write(sdir2.toString())
      .asDirectory();
  assertFalse(dir2Node.isWithSnapshot());
  ReadOnlyList<INode> dir2Children = dir2Node
      .getChildrenList(Snapshot.CURRENT_STATE_ID);
  assertEquals(1, dir2Children.size());
  assertEquals(dir2file.getName(), dir2Children.get(0).getLocalName());
}
 
Example 6
Source File: TestRenameWithSnapshots.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * Test the undo section of rename. Before the rename, we create the renamed 
 * file/dir after taking the snapshot.
 */
@Test
public void testRenameUndo_2() throws Exception {
  final Path sdir1 = new Path("/dir1");
  final Path sdir2 = new Path("/dir2");
  hdfs.mkdirs(sdir1);
  hdfs.mkdirs(sdir2);
  final Path dir2file = new Path(sdir2, "file");
  DFSTestUtil.createFile(hdfs, dir2file, BLOCKSIZE, REPL, SEED);
  
  SnapshotTestHelper.createSnapshot(hdfs, sdir1, "s1");
  
  // create foo after taking snapshot
  final Path foo = new Path(sdir1, "foo");
  final Path bar = new Path(foo, "bar");
  DFSTestUtil.createFile(hdfs, bar, BLOCKSIZE, REPL, SEED);
  
  INodeDirectory dir2 = fsdir.getINode4Write(sdir2.toString()).asDirectory();
  INodeDirectory mockDir2 = spy(dir2);
  doReturn(false).when(mockDir2).addChild((INode) anyObject(), anyBoolean(),
          Mockito.anyInt());
  INodeDirectory root = fsdir.getINode4Write("/").asDirectory();
  root.replaceChild(dir2, mockDir2, fsdir.getINodeMap());
  
  final Path newfoo = new Path(sdir2, "foo");
  boolean result = hdfs.rename(foo, newfoo);
  assertFalse(result);
  
  // check the current internal details
  INodeDirectory dir1Node = fsdir.getINode4Write(sdir1.toString())
      .asDirectory();
  Snapshot s1 = dir1Node.getSnapshot(DFSUtil.string2Bytes("s1"));
  ReadOnlyList<INode> dir1Children = dir1Node
      .getChildrenList(Snapshot.CURRENT_STATE_ID);
  assertEquals(1, dir1Children.size());
  assertEquals(foo.getName(), dir1Children.get(0).getLocalName());
  List<DirectoryDiff> dir1Diffs = dir1Node.getDiffs().asList();
  assertEquals(1, dir1Diffs.size());
  assertEquals(s1.getId(), dir1Diffs.get(0).getSnapshotId());
  
  // after the undo of rename, the created list of sdir1 should contain 
  // 1 element
  ChildrenDiff childrenDiff = dir1Diffs.get(0).getChildrenDiff();
  assertEquals(0, childrenDiff.getList(ListType.DELETED).size());
  assertEquals(1, childrenDiff.getList(ListType.CREATED).size());
  
  INode fooNode = fsdir.getINode4Write(foo.toString());
  assertTrue(fooNode instanceof INodeDirectory);
  assertTrue(childrenDiff.getList(ListType.CREATED).get(0) == fooNode);
  
  final Path foo_s1 = SnapshotTestHelper.getSnapshotPath(sdir1, "s1", "foo");
  assertFalse(hdfs.exists(foo_s1));
  
  // check sdir2
  assertFalse(hdfs.exists(newfoo));
  INodeDirectory dir2Node = fsdir.getINode4Write(sdir2.toString())
      .asDirectory();
  assertFalse(dir2Node.isWithSnapshot());
  ReadOnlyList<INode> dir2Children = dir2Node
      .getChildrenList(Snapshot.CURRENT_STATE_ID);
  assertEquals(1, dir2Children.size());
  assertEquals(dir2file.getName(), dir2Children.get(0).getLocalName());
}
 
Example 7
Source File: TestRenameWithSnapshots.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * Test the undo section of the second-time rename.
 */
@Test
public void testRenameUndo_3() throws Exception {
  final Path sdir1 = new Path("/dir1");
  final Path sdir2 = new Path("/dir2");
  final Path sdir3 = new Path("/dir3");
  hdfs.mkdirs(sdir1);
  hdfs.mkdirs(sdir2);
  hdfs.mkdirs(sdir3);
  final Path foo = new Path(sdir1, "foo");
  final Path bar = new Path(foo, "bar");
  DFSTestUtil.createFile(hdfs, bar, BLOCKSIZE, REPL, SEED);
  
  SnapshotTestHelper.createSnapshot(hdfs, sdir1, "s1");
  SnapshotTestHelper.createSnapshot(hdfs, sdir2, "s2");
  
  INodeDirectory dir3 = fsdir.getINode4Write(sdir3.toString()).asDirectory();
  INodeDirectory mockDir3 = spy(dir3);
  doReturn(false).when(mockDir3).addChild((INode) anyObject(), anyBoolean(),
          Mockito.anyInt());
  INodeDirectory root = fsdir.getINode4Write("/").asDirectory();
  root.replaceChild(dir3, mockDir3, fsdir.getINodeMap());
  
  final Path foo_dir2 = new Path(sdir2, "foo2");
  final Path foo_dir3 = new Path(sdir3, "foo3");
  hdfs.rename(foo, foo_dir2);
  boolean result = hdfs.rename(foo_dir2, foo_dir3);
  assertFalse(result);
  
  // check the current internal details
  INodeDirectory dir1Node = fsdir.getINode4Write(sdir1.toString())
      .asDirectory();
  Snapshot s1 = dir1Node.getSnapshot(DFSUtil.string2Bytes("s1"));
  INodeDirectory dir2Node = fsdir.getINode4Write(sdir2.toString())
      .asDirectory();
  Snapshot s2 = dir2Node.getSnapshot(DFSUtil.string2Bytes("s2"));
  ReadOnlyList<INode> dir2Children = dir2Node
      .getChildrenList(Snapshot.CURRENT_STATE_ID);
  assertEquals(1, dir2Children.size());
  List<DirectoryDiff> dir2Diffs = dir2Node.getDiffs().asList();
  assertEquals(1, dir2Diffs.size());
  assertEquals(s2.getId(), dir2Diffs.get(0).getSnapshotId());
  ChildrenDiff childrenDiff = dir2Diffs.get(0).getChildrenDiff();
  assertEquals(0, childrenDiff.getList(ListType.DELETED).size());
  assertEquals(1, childrenDiff.getList(ListType.CREATED).size());
  final Path foo_s2 = SnapshotTestHelper.getSnapshotPath(sdir2, "s2", "foo2");
  assertFalse(hdfs.exists(foo_s2));
  
  INode fooNode = fsdir.getINode4Write(foo_dir2.toString());
  assertTrue(childrenDiff.getList(ListType.CREATED).get(0) == fooNode);
  assertTrue(fooNode instanceof INodeReference.DstReference);
  List<DirectoryDiff> fooDiffs = fooNode.asDirectory().getDiffs().asList();
  assertEquals(1, fooDiffs.size());
  assertEquals(s1.getId(), fooDiffs.get(0).getSnapshotId());
  
  // create snapshot on sdir2 and rename again
  hdfs.createSnapshot(sdir2, "s3");
  result = hdfs.rename(foo_dir2, foo_dir3);
  assertFalse(result);

  // check internal details again
  dir2Node = fsdir.getINode4Write(sdir2.toString()).asDirectory();
  Snapshot s3 = dir2Node.getSnapshot(DFSUtil.string2Bytes("s3"));
  fooNode = fsdir.getINode4Write(foo_dir2.toString());
  dir2Children = dir2Node.getChildrenList(Snapshot.CURRENT_STATE_ID);
  assertEquals(1, dir2Children.size());
  dir2Diffs = dir2Node.getDiffs().asList();
  assertEquals(2, dir2Diffs.size());
  assertEquals(s2.getId(), dir2Diffs.get(0).getSnapshotId());
  assertEquals(s3.getId(), dir2Diffs.get(1).getSnapshotId());
  
  childrenDiff = dir2Diffs.get(0).getChildrenDiff();
  assertEquals(0, childrenDiff.getList(ListType.DELETED).size());
  assertEquals(1, childrenDiff.getList(ListType.CREATED).size());
  assertTrue(childrenDiff.getList(ListType.CREATED).get(0) == fooNode);
  
  childrenDiff = dir2Diffs.get(1).getChildrenDiff();
  assertEquals(0, childrenDiff.getList(ListType.DELETED).size());
  assertEquals(0, childrenDiff.getList(ListType.CREATED).size());
  
  final Path foo_s3 = SnapshotTestHelper.getSnapshotPath(sdir2, "s3", "foo2");
  assertFalse(hdfs.exists(foo_s2));
  assertTrue(hdfs.exists(foo_s3));
  
  assertTrue(fooNode instanceof INodeReference.DstReference);
  fooDiffs = fooNode.asDirectory().getDiffs().asList();
  assertEquals(2, fooDiffs.size());
  assertEquals(s1.getId(), fooDiffs.get(0).getSnapshotId());
  assertEquals(s3.getId(), fooDiffs.get(1).getSnapshotId());
}
 
Example 8
Source File: TestRenameWithSnapshots.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * Test undo where dst node being overwritten is a reference node
 */
@Test
public void testRenameUndo_4() throws Exception {
  final Path sdir1 = new Path("/dir1");
  final Path sdir2 = new Path("/dir2");
  final Path sdir3 = new Path("/dir3");
  hdfs.mkdirs(sdir1);
  hdfs.mkdirs(sdir2);
  hdfs.mkdirs(sdir3);
  
  final Path foo = new Path(sdir1, "foo");
  final Path bar = new Path(foo, "bar");
  DFSTestUtil.createFile(hdfs, bar, BLOCKSIZE, REPL, SEED);
  
  final Path foo2 = new Path(sdir2, "foo2");
  hdfs.mkdirs(foo2);
  
  SnapshotTestHelper.createSnapshot(hdfs, sdir1, "s1");
  SnapshotTestHelper.createSnapshot(hdfs, sdir2, "s2");
  
  // rename foo2 to foo3, so that foo3 will be a reference node
  final Path foo3 = new Path(sdir3, "foo3");
  hdfs.rename(foo2, foo3);
  
  INode foo3Node = fsdir.getINode4Write(foo3.toString());
  assertTrue(foo3Node.isReference());
  
  INodeDirectory dir3 = fsdir.getINode4Write(sdir3.toString()).asDirectory();
  INodeDirectory mockDir3 = spy(dir3);
  // fail the rename but succeed in undo
  doReturn(false).when(mockDir3).addChild((INode) Mockito.isNull(),
      anyBoolean(), Mockito.anyInt());
  Mockito.when(mockDir3.addChild((INode) Mockito.isNotNull(), anyBoolean(), 
      Mockito.anyInt())).thenReturn(false).thenCallRealMethod();
  INodeDirectory root = fsdir.getINode4Write("/").asDirectory();
  root.replaceChild(dir3, mockDir3, fsdir.getINodeMap());
  foo3Node.setParent(mockDir3);
  
  try {
    hdfs.rename(foo, foo3, Rename.OVERWRITE);
    fail("the rename from " + foo + " to " + foo3 + " should fail");
  } catch (IOException e) {
    GenericTestUtils.assertExceptionContains("rename from " + foo + " to "
        + foo3 + " failed.", e);
  }
  
  // make sure the undo is correct
  final INode foo3Node_undo = fsdir.getINode4Write(foo3.toString());
  assertSame(foo3Node, foo3Node_undo);
  INodeReference.WithCount foo3_wc = (WithCount) foo3Node.asReference()
      .getReferredINode();
  assertEquals(2, foo3_wc.getReferenceCount());
  assertSame(foo3Node, foo3_wc.getParentReference());
}