2424import static org .mockito .Mockito .spy ;
2525
2626import org .apache .hadoop .conf .Configuration ;
27+ import org .apache .hadoop .hdfs .DFSConfigKeys ;
2728import org .apache .hadoop .hdfs .protocol .SnapshotException ;
2829import org .apache .hadoop .hdfs .server .namenode .FSDirectory ;
2930import org .apache .hadoop .hdfs .server .namenode .INode ;
3536import org .junit .Assert ;
3637import org .junit .Test ;
3738
39+ import java .io .IOException ;
40+
3841
3942/**
4043 * Testing snapshot manager functionality.
4144 */
4245public class TestSnapshotManager {
43- private static final int testMaxSnapshotLimit = 7 ;
46+ private static final int testMaxSnapshotIDLimit = 7 ;
4447
4548 /**
46- * Test that the global limit on snapshots is honored.
49+ * Test that the global limit on snapshot Ids is honored.
4750 */
4851 @ Test (timeout =10000 )
49- public void testSnapshotLimits () throws Exception {
50- // Setup mock objects for SnapshotManager.createSnapshot.
51- //
52+ public void testSnapshotIDLimits () throws Exception {
53+ testMaxSnapshotLimit (testMaxSnapshotIDLimit , "rollover" ,
54+ new Configuration (), testMaxSnapshotIDLimit );
55+ }
56+
57+ /**
58+ * Tests that the global limit on snapshots is honored.
59+ */
60+ @ Test (timeout =10000 )
61+ public void testMaxSnapshotLimit () throws Exception {
62+ Configuration conf = new Configuration ();
63+ conf .setInt (DFSConfigKeys .DFS_NAMENODE_SNAPSHOT_FILESYSTEM_LIMIT ,
64+ testMaxSnapshotIDLimit );
65+ conf .setInt (DFSConfigKeys .
66+ DFS_NAMENODE_SNAPSHOT_MAX_LIMIT ,
67+ testMaxSnapshotIDLimit );
68+ testMaxSnapshotLimit (testMaxSnapshotIDLimit ,"max snapshot limit" ,
69+ conf , testMaxSnapshotIDLimit * 2 );
70+ }
71+
72+ private void testMaxSnapshotLimit (int maxSnapshotLimit , String errMsg ,
73+ Configuration conf , int maxSnapID )
74+ throws IOException {
5275 LeaseManager leaseManager = mock (LeaseManager .class );
5376 INodeDirectory ids = mock (INodeDirectory .class );
5477 FSDirectory fsdir = mock (FSDirectory .class );
5578 INodesInPath iip = mock (INodesInPath .class );
5679
57- SnapshotManager sm = spy (new SnapshotManager (new Configuration () , fsdir ));
80+ SnapshotManager sm = spy (new SnapshotManager (conf , fsdir ));
5881 doReturn (ids ).when (sm ).getSnapshottableRoot (any ());
59- doReturn (testMaxSnapshotLimit ).when (sm ).getMaxSnapshotID ();
82+ doReturn (maxSnapID ).when (sm ).getMaxSnapshotID ();
6083
6184 // Create testMaxSnapshotLimit snapshots. These should all succeed.
6285 //
63- for (Integer i = 0 ; i < testMaxSnapshotLimit ; ++i ) {
86+ for (Integer i = 0 ; i < maxSnapshotLimit ; ++i ) {
6487 sm .createSnapshot (leaseManager , iip , "dummy" , i .toString (), Time .now ());
6588 }
6689
@@ -73,7 +96,7 @@ public void testSnapshotLimits() throws Exception {
7396 Assert .fail ("Expected SnapshotException not thrown" );
7497 } catch (SnapshotException se ) {
7598 Assert .assertTrue (
76- StringUtils .toLowerCase (se .getMessage ()).contains ("rollover" ));
99+ StringUtils .toLowerCase (se .getMessage ()).contains (errMsg ));
77100 }
78101
79102 // Delete a snapshot to free up a slot.
@@ -83,22 +106,26 @@ public void testSnapshotLimits() throws Exception {
83106 // Attempt to create a snapshot again. It should still fail due
84107 // to snapshot ID rollover.
85108 //
109+
86110 try {
87111 sm .createSnapshot (leaseManager , iip , "dummy" , "shouldFailSnapshot2" ,
88112 Time .now ());
89- Assert .fail ("Expected SnapshotException not thrown" );
113+ // in case the snapshot ID limit is hit, further creation of snapshots
114+ // even post deletions of snapshots won't succeed
115+ if (maxSnapID < maxSnapshotLimit ) {
116+ Assert .fail ("CreateSnapshot should succeed" );
117+ }
90118 } catch (SnapshotException se ) {
91119 Assert .assertTrue (
92- StringUtils .toLowerCase (se .getMessage ()).contains ("rollover" ));
120+ StringUtils .toLowerCase (se .getMessage ()).contains (errMsg ));
93121 }
94122 }
95-
96123 /**
97124 * Snapshot is identified by INODE CURRENT_STATE_ID.
98125 * So maximum allowable snapshotID should be less than CURRENT_STATE_ID
99126 */
100127 @ Test
101- public void testValidateSnapshotIDWidth () {
128+ public void testValidateSnapshotIDWidth () throws Exception {
102129 FSDirectory fsdir = mock (FSDirectory .class );
103130 SnapshotManager snapshotManager = new SnapshotManager (new Configuration (),
104131 fsdir );
0 commit comments