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 2019/03/07 04:46:10 UTC

[kudu] 02/02: tools: fix output on 'kudu table scan' to add proper locking

This is an automated email from the ASF dual-hosted git repository.

adar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git

commit 0a05f613e44885ad1be2759ad4944233e5e6df74
Author: Todd Lipcon <to...@apache.org>
AuthorDate: Tue Feb 26 16:28:07 2019 -0800

    tools: fix output on 'kudu table scan' to add proper locking
    
    The 'kudu table scan' tool is multi-threaded, but no lock was protecting
    output. This would cause output to occasionally interleave like:
    
      (int64 l_orderkey=380530182)
      (int64 l_orderkey=352994055)(int64 l_orderkey=380680647)
      (int64 l_orderkey=353120262)
    
      (int64 l_orderkey=353314147)
    
    (i.e. two rows would print on the same line, and an extra newline would
    be inserted later).
    
    This adds a mutex to protect the output.
    
    Change-Id: I9d7f936d9beaf7eedba46477bf7da2ebdb800eb1
    Reviewed-on: http://gerrit.cloudera.org:8080/12617
    Reviewed-by: Yingchun Lai <40...@qq.com>
    Reviewed-by: Grant Henke <gr...@apache.org>
    Tested-by: Kudu Jenkins
---
 src/kudu/tools/table_scanner.cc | 8 ++++++--
 src/kudu/tools/table_scanner.h  | 5 ++++-
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/kudu/tools/table_scanner.cc b/src/kudu/tools/table_scanner.cc
index a2f2e7b..c8c5525 100644
--- a/src/kudu/tools/table_scanner.cc
+++ b/src/kudu/tools/table_scanner.cc
@@ -303,6 +303,7 @@ void TableScanner::ScannerTask(const vector<KuduScanToken *>& tokens) {
       count += batch.NumRows();
       total_count_.IncrementBy(batch.NumRows());
       if (FLAGS_show_value) {
+        MutexLock l(output_lock_);
         for (const auto& row : batch) {
           cout << row.ToString() << endl;
         }
@@ -311,8 +312,11 @@ void TableScanner::ScannerTask(const vector<KuduScanToken *>& tokens) {
     delete scanner;
 
     sw.stop();
-    cout << "T " << token->tablet().id() << " scanned count " << count
-        << " cost " << sw.elapsed().wall_seconds() << " seconds" << endl;
+    {
+      MutexLock l(output_lock_);
+      cout << "T " << token->tablet().id() << " scanned count " << count
+           << " cost " << sw.elapsed().wall_seconds() << " seconds" << endl;
+    }
   }
 }
 
diff --git a/src/kudu/tools/table_scanner.h b/src/kudu/tools/table_scanner.h
index c9589c5..2165390 100644
--- a/src/kudu/tools/table_scanner.h
+++ b/src/kudu/tools/table_scanner.h
@@ -26,6 +26,7 @@
 #include "kudu/client/shared_ptr.h"
 #include "kudu/gutil/gscoped_ptr.h"
 #include "kudu/util/atomic.h"
+#include "kudu/util/mutex.h"
 #include "kudu/util/status.h"
 #include "kudu/util/threadpool.h"
 
@@ -57,11 +58,13 @@ private:
   void ScannerTask(const vector<KuduScanToken *>& tokens);
   void MonitorTask();
 
-private:
   AtomicInt<uint64_t> total_count_;
   client::sp::shared_ptr<KuduClient> client_;
   std::string table_name_;
   gscoped_ptr<ThreadPool> thread_pool_;
+
+  // Protects output to stdout so that rows don't get interleaved.
+  Mutex output_lock_;
 };
 } // namespace tools
 } // namespace kudu