You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by da...@apache.org on 2016/12/06 01:22:17 UTC

[2/2] kudu git commit: Add file modified time to Env

Add file modified time to Env

Change-Id: I90b3e759a4dbb352a0c09dc726b428c9dcea5595
Reviewed-on: http://gerrit.cloudera.org:8080/5339
Reviewed-by: Adar Dembo <ad...@cloudera.com>
Tested-by: Kudu Jenkins


Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/44c73d5b
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/44c73d5b
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/44c73d5b

Branch: refs/heads/master
Commit: 44c73d5b685b43f7dc938eb7196127ff3c397970
Parents: 8264417
Author: Dan Burkert <da...@apache.org>
Authored: Thu Dec 1 17:51:11 2016 -0800
Committer: Dan Burkert <da...@apache.org>
Committed: Tue Dec 6 01:21:05 2016 +0000

----------------------------------------------------------------------
 .../external_mini_cluster_fs_inspector.cc         | 11 +++--------
 src/kudu/util/env-test.cc                         | 18 ++++++++++++++++++
 src/kudu/util/env.h                               |  9 +++++++++
 src/kudu/util/env_posix.cc                        | 16 ++++++++++++++++
 4 files changed, 46 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/44c73d5b/src/kudu/integration-tests/external_mini_cluster_fs_inspector.cc
----------------------------------------------------------------------
diff --git a/src/kudu/integration-tests/external_mini_cluster_fs_inspector.cc b/src/kudu/integration-tests/external_mini_cluster_fs_inspector.cc
index b9667f0..00441b1 100644
--- a/src/kudu/integration-tests/external_mini_cluster_fs_inspector.cc
+++ b/src/kudu/integration-tests/external_mini_cluster_fs_inspector.cc
@@ -186,14 +186,9 @@ Status ExternalMiniClusterFsInspector::ReadTabletSuperBlockOnTS(int index,
 
 int64_t ExternalMiniClusterFsInspector::GetTabletSuperBlockMTimeOrDie(int ts_index,
                                                                       const string& tablet_id) {
-  const auto& sb_path = GetTabletSuperBlockPathOnTS(ts_index, tablet_id);
-  struct stat s;
-  CHECK_ERR(stat(sb_path.c_str(), &s)) << "failed to stat: " << sb_path;
-#ifdef __APPLE__
-  return s.st_mtimespec.tv_sec * 1e6 + s.st_mtimespec.tv_nsec / 1000;
-#else
-  return s.st_mtim.tv_sec * 1e6 + s.st_mtim.tv_nsec / 1000;
-#endif
+  int64_t timestamp;
+  CHECK_OK(env_->GetFileModifiedTime(GetTabletSuperBlockPathOnTS(ts_index, tablet_id), &timestamp));
+  return timestamp;
 }
 
 string ExternalMiniClusterFsInspector::GetConsensusMetadataPathOnTS(int index,

http://git-wip-us.apache.org/repos/asf/kudu/blob/44c73d5b/src/kudu/util/env-test.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/env-test.cc b/src/kudu/util/env-test.cc
index 7d0cd78..f3c41d3 100644
--- a/src/kudu/util/env-test.cc
+++ b/src/kudu/util/env-test.cc
@@ -681,6 +681,24 @@ TEST_F(TestEnv, TestGetBlockSize) {
   ASSERT_GT(block_size, 0);
 }
 
+TEST_F(TestEnv, TestGetFileModifiedTime) {
+  string path = GetTestPath("mtime");
+  unique_ptr<WritableFile> writer;
+  ASSERT_OK(env_->NewWritableFile(path, &writer));
+
+  int64_t initial_time;
+  ASSERT_OK(env_->GetFileModifiedTime(writer->filename(), &initial_time));
+
+  // HFS has 1 second mtime granularity.
+  AssertEventually([&] {
+    int64_t after_time;
+    writer->Append(" ");
+    writer->Sync();
+    ASSERT_OK(env_->GetFileModifiedTime(writer->filename(), &after_time));
+    ASSERT_LT(initial_time, after_time);
+  }, MonoDelta::FromSeconds(5));
+}
+
 TEST_F(TestEnv, TestRWFile) {
   // Create the file.
   unique_ptr<RWFile> file;

http://git-wip-us.apache.org/repos/asf/kudu/blob/44c73d5b/src/kudu/util/env.h
----------------------------------------------------------------------
diff --git a/src/kudu/util/env.h b/src/kudu/util/env.h
index 21617c5..adc2508 100644
--- a/src/kudu/util/env.h
+++ b/src/kudu/util/env.h
@@ -171,6 +171,15 @@ class Env {
   // as reported by GetFileSizeOnDisk(), storing the grand total in 'bytes_used'.
   virtual Status GetFileSizeOnDiskRecursively(const std::string& root, uint64_t* bytes_used) = 0;
 
+  // Returns the modified time of the file in microseconds.
+  //
+  // The timestamp is a 'system' timestamp, and is not guaranteed to be
+  // monotonic, or have any other consistency properties. The granularity of the
+  // timestamp is not guaranteed, and may be as high as 1 second on some
+  // platforms. The timestamp is not guaranteed to be anchored to any particular
+  // epoch.
+  virtual Status GetFileModifiedTime(const std::string& fname, int64_t* timestamp) = 0;
+
   // Store the block size of the filesystem where fname resides in
   // *block_size. fname must exist but it may be a file or a directory.
   virtual Status GetBlockSize(const std::string& fname, uint64_t* block_size) = 0;

http://git-wip-us.apache.org/repos/asf/kudu/blob/44c73d5b/src/kudu/util/env_posix.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/env_posix.cc b/src/kudu/util/env_posix.cc
index d24de69..33bfcac 100644
--- a/src/kudu/util/env_posix.cc
+++ b/src/kudu/util/env_posix.cc
@@ -940,6 +940,22 @@ class PosixEnv : public Env {
     return s;
   }
 
+  virtual Status GetFileModifiedTime(const string& fname, int64_t* timestamp) override {
+    TRACE_EVENT1("io", "PosixEnv::GetFileModifiedTime", "fname", fname);
+    ThreadRestrictions::AssertIOAllowed();
+
+    struct stat s;
+    if (stat(fname.c_str(), &s) != 0) {
+      return IOError(fname, errno);
+    }
+#ifdef __APPLE__
+    *timestamp = s.st_mtimespec.tv_sec * 1e6 + s.st_mtimespec.tv_nsec / 1e3;
+#else
+    *timestamp = s.st_mtim.tv_sec * 1e6 + s.st_mtim.tv_nsec / 1e3;
+#endif
+    return Status::OK();
+  }
+
   // Local convenience function for safely running statvfs().
   static Status StatVfs(const string& path, struct statvfs* buf) {
     ThreadRestrictions::AssertIOAllowed();