@@ -183,6 +183,64 @@ public void testTimes() throws IOException {
183183 }
184184 }
185185
186+ /**
187+ * Tests mod time change at close in DFS.
188+ */
189+ public void testTimesAtClose () throws IOException {
190+ Configuration conf = new Configuration ();
191+ final int MAX_IDLE_TIME = 2000 ; // 2s
192+ int replicas = 1 ;
193+
194+ // parameter initialization
195+ conf .setInt ("ipc.client.connection.maxidletime" , MAX_IDLE_TIME );
196+ conf .setInt ("heartbeat.recheck.interval" , 1000 );
197+ conf .setInt ("dfs.heartbeat.interval" , 1 );
198+ conf .setInt ("dfs.datanode.handler.count" , 50 );
199+ MiniDFSCluster cluster = new MiniDFSCluster (conf , numDatanodes , true , null );
200+ cluster .waitActive ();
201+ InetSocketAddress addr = new InetSocketAddress ("localhost" ,
202+ cluster .getNameNodePort ());
203+ DFSClient client = new DFSClient (addr , conf );
204+ DatanodeInfo [] info = client .datanodeReport (DatanodeReportType .LIVE );
205+ assertEquals ("Number of Datanodes " , numDatanodes , info .length );
206+ FileSystem fileSys = cluster .getFileSystem ();
207+ assertTrue (fileSys instanceof DistributedFileSystem );
208+
209+ try {
210+ // create a new file and write to it
211+ Path file1 = new Path ("/simple.dat" );
212+ FSDataOutputStream stm = writeFile (fileSys , file1 , replicas );
213+ System .out .println ("Created and wrote file simple.dat" );
214+ FileStatus statBeforeClose = fileSys .getFileStatus (file1 );
215+ long mtimeBeforeClose = statBeforeClose .getModificationTime ();
216+ String mdateBeforeClose = dateForm .format (new Date (
217+ mtimeBeforeClose ));
218+ System .out .println ("mtime on " + file1 + " before close is "
219+ + mdateBeforeClose + " (" + mtimeBeforeClose + ")" );
220+ assertTrue (mtimeBeforeClose != 0 );
221+
222+ //close file after writing
223+ stm .close ();
224+ System .out .println ("Closed file." );
225+ FileStatus statAfterClose = fileSys .getFileStatus (file1 );
226+ long mtimeAfterClose = statAfterClose .getModificationTime ();
227+ String mdateAfterClose = dateForm .format (new Date (mtimeAfterClose ));
228+ System .out .println ("mtime on " + file1 + " after close is "
229+ + mdateAfterClose + " (" + mtimeAfterClose + ")" );
230+ assertTrue (mtimeAfterClose != 0 );
231+ assertTrue (mtimeBeforeClose != mtimeAfterClose );
232+
233+ cleanupFile (fileSys , file1 );
234+ } catch (IOException e ) {
235+ info = client .datanodeReport (DatanodeReportType .ALL );
236+ printDatanodeReport (info );
237+ throw e ;
238+ } finally {
239+ fileSys .close ();
240+ cluster .shutdown ();
241+ }
242+ }
243+
186244 public static void main (String [] args ) throws Exception {
187245 new TestSetTimes ().testTimes ();
188246 }
0 commit comments