You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by ad...@apache.org on 2016/07/14 18:03:24 UTC

[1/2] incubator-kudu git commit: env: add GetFileSizeOnDiskRecursively

Repository: incubator-kudu
Updated Branches:
  refs/heads/master 4e99544d6 -> 71a5bb507


env: add GetFileSizeOnDiskRecursively

This was originally needed by a new integration test. Since then, it's no
longer needed, but the new method might prove useful in the future.

Change-Id: Ic49629595e776ce5c755e15b04c6509053ff361f
Reviewed-on: http://gerrit.cloudera.org:8080/3644
Tested-by: Adar Dembo <ad...@cloudera.com>
Reviewed-by: Dan Burkert <da...@cloudera.com>


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

Branch: refs/heads/master
Commit: 8c68d35061aece52203c6eb0bb8de75474245a95
Parents: 4e99544
Author: Adar Dembo <ad...@cloudera.com>
Authored: Wed Jul 13 10:14:29 2016 -0700
Committer: Adar Dembo <ad...@cloudera.com>
Committed: Thu Jul 14 18:01:14 2016 +0000

----------------------------------------------------------------------
 src/kudu/tablet/compaction-test.cc | 35 ++++-----------------------------
 src/kudu/util/env.h                |  8 ++++++++
 src/kudu/util/env_posix.cc         | 32 ++++++++++++++++++++++++++++++
 3 files changed, 44 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/8c68d350/src/kudu/tablet/compaction-test.cc
----------------------------------------------------------------------
diff --git a/src/kudu/tablet/compaction-test.cc b/src/kudu/tablet/compaction-test.cc
index 3383866..695040f 100644
--- a/src/kudu/tablet/compaction-test.cc
+++ b/src/kudu/tablet/compaction-test.cc
@@ -383,13 +383,6 @@ class TestCompaction : public KuduRowSetTest {
     }
   }
 
-  Status GetDataDiskSpace(uint64_t* bytes_used) {
-    *bytes_used = 0;
-    return env_->Walk(fs_manager()->GetDataRootDirs().at(0),
-                      Env::PRE_ORDER, Bind(&TestCompaction::GetDataDiskSpaceCb,
-                                           Unretained(this), bytes_used));
-  }
-
  protected:
   OpId op_id_;
 
@@ -398,28 +391,6 @@ class TestCompaction : public KuduRowSetTest {
   MvccManager mvcc_;
 
   scoped_refptr<LogAnchorRegistry> log_anchor_registry_;
-
- private:
-
-  Status GetDataDiskSpaceCb(uint64_t* bytes_used,
-                            Env::FileType type,
-                            const string& dirname, const string& basename) {
-    uint64_t file_bytes_used = 0;
-    switch (type) {
-      case Env::FILE_TYPE:
-        RETURN_NOT_OK(env_->GetFileSizeOnDisk(
-            JoinPathSegments(dirname, basename), &file_bytes_used));
-        *bytes_used += file_bytes_used;
-        break;
-      case Env::DIRECTORY_TYPE:
-        // Ignore directory space consumption; it varies from filesystem to
-        // filesystem and isn't interesting for this test.
-        break;
-      default:
-        LOG(FATAL) << "Unknown file type: " << type;
-    }
-    return Status::OK();
-  }
 };
 
 TEST_F(TestCompaction, TestMemRowSetInput) {
@@ -771,7 +742,8 @@ TEST_F(TestCompaction, TestCompactionFreesDiskSpace) {
   }
 
   uint64_t bytes_before;
-  ASSERT_NO_FATAL_FAILURE(GetDataDiskSpace(&bytes_before));
+  ASSERT_OK(env_->GetFileSizeOnDiskRecursively(
+      fs_manager()->GetDataRootDirs().at(0), &bytes_before));
 
   ASSERT_OK(tablet()->Compact(Tablet::FORCE_COMPACT_ALL));
 
@@ -781,7 +753,8 @@ TEST_F(TestCompaction, TestCompactionFreesDiskSpace) {
   deadline.AddDelta(MonoDelta::FromSeconds(30));
   while (true) {
     uint64_t bytes_after;
-    ASSERT_NO_FATAL_FAILURE(GetDataDiskSpace(&bytes_after));
+    ASSERT_OK(env_->GetFileSizeOnDiskRecursively(
+        fs_manager()->GetDataRootDirs().at(0), &bytes_after));
     LOG(INFO) << Substitute("Data disk space: $0 (before), $1 (after) ",
                             bytes_before, bytes_after);
     if (bytes_after < bytes_before) {

http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/8c68d350/src/kudu/util/env.h
----------------------------------------------------------------------
diff --git a/src/kudu/util/env.h b/src/kudu/util/env.h
index f62f85b..7eb97e7 100644
--- a/src/kudu/util/env.h
+++ b/src/kudu/util/env.h
@@ -165,6 +165,10 @@ class Env {
   // of space consumed by the file, not the user-facing file size.
   virtual Status GetFileSizeOnDisk(const std::string& fname, uint64_t* file_size) = 0;
 
+  // Walk 'root' recursively, looking up the amount of space used by each file
+  // as reported by GetFileSizeOnDisk(), storing the grand total in 'bytes_used'.
+  virtual Status GetFileSizeOnDiskRecursively(const std::string& root, uint64_t* bytes_used) = 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;
@@ -264,6 +268,7 @@ class Env {
 
   // Get the total amount of RAM installed on this machine.
   virtual Status GetTotalRAMBytes(int64_t* ram) = 0;
+
  private:
   // No copying allowed
   Env(const Env&);
@@ -585,6 +590,9 @@ class EnvWrapper : public Env {
   Status GetFileSizeOnDisk(const std::string& f, uint64_t* s) OVERRIDE {
     return target_->GetFileSizeOnDisk(f, s);
   }
+  Status GetFileSizeOnDiskRecursively(const std::string& root, uint64_t* bytes_used) OVERRIDE {
+    return target_->GetFileSizeOnDiskRecursively(root, bytes_used);
+  }
   Status GetBlockSize(const std::string& f, uint64_t* s) OVERRIDE {
     return target_->GetBlockSize(f, s);
   }

http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/8c68d350/src/kudu/util/env_posix.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/env_posix.cc b/src/kudu/util/env_posix.cc
index 465e7aa..773971f 100644
--- a/src/kudu/util/env_posix.cc
+++ b/src/kudu/util/env_posix.cc
@@ -898,6 +898,17 @@ class PosixEnv : public Env {
     return s;
   }
 
+  virtual Status GetFileSizeOnDiskRecursively(const string& root,
+                                              uint64_t* bytes_used) OVERRIDE {
+    TRACE_EVENT1("io", "PosixEnv::GetFileSizeOnDiskRecursively", "path", root);
+    uint64_t total = 0;
+    RETURN_NOT_OK(Walk(root, Env::PRE_ORDER,
+                       Bind(&PosixEnv::GetFileSizeOnDiskRecursivelyCb,
+                            Unretained(this), &total)));
+    *bytes_used = total;
+    return Status::OK();
+  }
+
   virtual Status GetBlockSize(const string& fname, uint64_t* block_size) OVERRIDE {
     TRACE_EVENT1("io", "PosixEnv::GetBlockSize", "path", fname);
     ThreadRestrictions::AssertIOAllowed();
@@ -1207,6 +1218,27 @@ class PosixEnv : public Env {
         return Status::OK();
     }
   }
+
+  Status GetFileSizeOnDiskRecursivelyCb(uint64_t* bytes_used,
+                                        Env::FileType type,
+                                        const string& dirname,
+                                        const string& basename) {
+    uint64_t file_bytes_used = 0;
+    switch (type) {
+      case Env::FILE_TYPE:
+        RETURN_NOT_OK(GetFileSizeOnDisk(
+            JoinPathSegments(dirname, basename), &file_bytes_used));
+        *bytes_used += file_bytes_used;
+        break;
+      case Env::DIRECTORY_TYPE:
+        // Ignore directory space consumption as it varies from filesystem to
+        // filesystem.
+        break;
+      default:
+        LOG(FATAL) << "Unknown file type: " << type;
+    }
+    return Status::OK();
+  }
 };
 
 PosixEnv::PosixEnv() {}


[2/2] incubator-kudu git commit: rw_mutex: add configurable priority

Posted by ad...@apache.org.
rw_mutex: add configurable priority

The glibc implementation of pthread rwlocks exposes priorities that can help
if avoiding reader or writer starvation is desirable. I have a use case for
the latter, so let's expose the priorities in our wrapper.

Note: pthread rwlock priorities don't exist on macOS, which is why they're a
"best effort".

Change-Id: I16ba6cd041f126c94e63fa07a1e84c88db6778d7
Reviewed-on: http://gerrit.cloudera.org:8080/3603
Tested-by: Kudu Jenkins
Reviewed-by: Todd Lipcon <to...@apache.org>
Reviewed-by: Dan Burkert <da...@cloudera.com>


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

Branch: refs/heads/master
Commit: 71a5bb5070f144f8df356f5e28dd4e063de3df5e
Parents: 8c68d35
Author: Adar Dembo <ad...@cloudera.com>
Authored: Fri Jul 8 18:51:56 2016 -0700
Committer: Adar Dembo <ad...@cloudera.com>
Committed: Thu Jul 14 18:02:27 2016 +0000

----------------------------------------------------------------------
 src/kudu/util/rw_mutex.cc | 32 ++++++++++++++++++++++++++++++++
 src/kudu/util/rw_mutex.h  | 22 ++++++++++++++++++++++
 2 files changed, 54 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/71a5bb50/src/kudu/util/rw_mutex.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/rw_mutex.cc b/src/kudu/util/rw_mutex.cc
index b47d04f..d9473bb 100644
--- a/src/kudu/util/rw_mutex.cc
+++ b/src/kudu/util/rw_mutex.cc
@@ -31,8 +31,40 @@ void unlock_rwlock(pthread_rwlock_t* rwlock) {
 namespace kudu {
 
 RWMutex::RWMutex() {
+  Init(Priority::PREFER_READING);
+}
+
+RWMutex::RWMutex(Priority prio) {
+  Init(prio);
+}
+
+void RWMutex::Init(Priority prio) {
+#ifdef __linux__
+  // Adapt from priority to the pthread type.
+  int kind = PTHREAD_RWLOCK_PREFER_READER_NP;
+  switch (prio) {
+    case Priority::PREFER_READING:
+      kind = PTHREAD_RWLOCK_PREFER_READER_NP;
+      break;
+    case Priority::PREFER_WRITING:
+      kind = PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP;
+      break;
+  }
+
+  // Initialize the new rwlock with the user's preference.
+  pthread_rwlockattr_t attr;
+  int rv = pthread_rwlockattr_init(&attr);
+  DCHECK_EQ(0, rv) << strerror(rv);
+  rv = pthread_rwlockattr_setkind_np(&attr, kind);
+  DCHECK_EQ(0, rv) << strerror(rv);
+  rv = pthread_rwlock_init(&native_handle_, &attr);
+  DCHECK_EQ(0, rv) << strerror(rv);
+  rv = pthread_rwlockattr_destroy(&attr);
+  DCHECK_EQ(0, rv) << strerror(rv);
+#else
   int rv = pthread_rwlock_init(&native_handle_, NULL);
   DCHECK_EQ(0, rv) << strerror(rv);
+#endif
 }
 
 RWMutex::~RWMutex() {

http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/71a5bb50/src/kudu/util/rw_mutex.h
----------------------------------------------------------------------
diff --git a/src/kudu/util/rw_mutex.h b/src/kudu/util/rw_mutex.h
index 0153556..64a88d1 100644
--- a/src/kudu/util/rw_mutex.h
+++ b/src/kudu/util/rw_mutex.h
@@ -28,7 +28,27 @@ namespace kudu {
 // Implemented as a thin wrapper around pthread_rwlock_t.
 class RWMutex {
  public:
+
+  // Possible fairness policies for the RWMutex.
+  enum class Priority {
+    // The lock will prioritize readers at the expense of writers.
+    PREFER_READING,
+
+    // The lock will prioritize writers at the expense of readers.
+    //
+    // Care should be taken when using this fairness policy, as it can lead to
+    // unexpected deadlocks (e.g. a writer waiting on the lock will prevent
+    // additional readers from acquiring it).
+    PREFER_WRITING,
+  };
+
+  // Create an RWMutex that prioritizes readers.
   RWMutex();
+
+  // Create an RWMutex with customized priority. This is a best effort; the
+  // underlying platform may not support custom priorities.
+  explicit RWMutex(Priority prio);
+
   ~RWMutex();
 
   void ReadLock();
@@ -48,6 +68,8 @@ class RWMutex {
   bool try_lock_shared() { return TryReadLock(); }
 
  private:
+  void Init(Priority prio);
+
   pthread_rwlock_t native_handle_;
 
   DISALLOW_COPY_AND_ASSIGN(RWMutex);