Skip to content

Commit 1d3b089

Browse files
author
Dhruba Borthakur
committed
HDFS-504. Update the modification time of a file when the file
is closed. (Chun Zhang via dhruba) git-svn-id: https://svn.apache.org/repos/asf/hadoop/hdfs/trunk@800238 13f79535-47bb-0310-9956-ffa450edef68
1 parent 627f89b commit 1d3b089

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ Trunk (unreleased changes)
6767

6868
HDFS-511. Remove redundant block searches in BlockManager. (shv)
6969

70+
HDFS-504. Update the modification time of a file when the file
71+
is closed. (Chun Zhang via dhruba)
72+
7073
BUG FIXES
7174
HDFS-76. Better error message to users when commands fail because of
7275
lack of quota. Allow quota to be set even if the limit is lower than

src/java/org/apache/hadoop/hdfs/server/namenode/FSDirectory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,10 @@ void persistBlocks(String path, INodeFileUnderConstruction file) {
306306
*/
307307
void closeFile(String path, INodeFile file) {
308308
waitForReady();
309+
long now = FSNamesystem.now();
309310
synchronized (rootDir) {
310311
// file is closed
312+
file.setModificationTimeForce(now);
311313
fsImage.getEditLog().logCloseFile(path, file);
312314
if (NameNode.stateChangeLog.isDebugEnabled()) {
313315
NameNode.stateChangeLog.debug("DIR* FSDirectory.closeFile: "

src/test/hdfs/org/apache/hadoop/hdfs/TestSetTimes.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)