From 91b01a1db794581c67e1ccf9aabaa7cafd686c48 Mon Sep 17 00:00:00 2001 From: Ayush Saxena Date: Tue, 3 Sep 2019 18:08:42 +0530 Subject: [PATCH] HDFS-14807. SetTimes updates all negative values apart from -1. Contributed by Ayush Saxena. --- .../org/apache/hadoop/hdfs/protocol/ClientProtocol.java | 6 +++--- .../apache/hadoop/hdfs/server/namenode/FSDirAttrOp.java | 4 ++-- .../src/test/java/org/apache/hadoop/hdfs/TestSetTimes.java | 7 +++++++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/protocol/ClientProtocol.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/protocol/ClientProtocol.java index da93707551..35969a384e 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/protocol/ClientProtocol.java +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/protocol/ClientProtocol.java @@ -1145,11 +1145,11 @@ void fsync(String src, long inodeId, String client, long lastBlockLength) * Sets the modification and access time of the file to the specified time. * @param src The string representation of the path * @param mtime The number of milliseconds since Jan 1, 1970. - * Setting mtime to -1 means that modification time should not + * Setting negative mtime means that modification time should not * be set by this call. * @param atime The number of milliseconds since Jan 1, 1970. - * Setting atime to -1 means that access time should not be set - * by this call. + * Setting negative atime means that access time should not be + * set by this call. * * @throws org.apache.hadoop.security.AccessControlException permission denied * @throws java.io.FileNotFoundException file src is not found diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirAttrOp.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirAttrOp.java index b0a36b9204..65b528a22a 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirAttrOp.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirAttrOp.java @@ -475,14 +475,14 @@ static boolean unprotectedSetTimes( boolean status = false; INode inode = iip.getLastINode(); int latest = iip.getLatestSnapshotId(); - if (mtime != -1) { + if (mtime >= 0) { inode = inode.setModificationTime(mtime, latest); status = true; } // if the last access time update was within the last precision interval, // then no need to store access time - if (atime != -1 && (status || force + if (atime >= 0 && (status || force || atime > inode.getAccessTime() + fsd.getAccessTimePrecision())) { inode.setAccessTime(atime, latest, fsd.getFSNamesystem().getSnapshotManager(). diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestSetTimes.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestSetTimes.java index a90d139ab6..74cde2a2a9 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestSetTimes.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestSetTimes.java @@ -139,6 +139,13 @@ public void testTimes() throws IOException { " (" + mtime1 + ")"); assertTrue(atime1 != 0); + // check setting negative value for atime and mtime. + fileSys.setTimes(file1, -2, -2); + // The values shouldn't change. + stat = fileSys.getFileStatus(file1); + assertEquals(mtime1, stat.getModificationTime()); + assertEquals(atime1, stat.getAccessTime()); + // // record dir times //