kudu-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [kudu] 01/04: [tools] Add get table statistics for CLI tools
Date Thu, 26 Sep 2019 17:11:59 GMT
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 dfd2ed2d10e354833e9438a285ce25727151b0d7
Author: triplesheep <triplesheep0419@gmail.com>
AuthorDate: Tue Sep 24 18:53:28 2019 +0800

    [tools] Add get table statistics for CLI tools
    
    Running "statistics" in CLI tools will get the table statistics.
    
    Sample output:
    $ kudu table statistics 127.0.0.1:7051 usertable
    TABLE usertable
    on disk size: 621238168
    live row count: 146183
    
    Change-Id: Ie946a29ca5731ef72270d4ef870dd5919edaf869
    Reviewed-on: http://gerrit.cloudera.org:8080/14292
    Reviewed-by: Adar Dembo <adar@cloudera.com>
    Tested-by: Andrew Wong <awong@cloudera.com>
---
 src/kudu/client/client.cc                   |  8 ++++++--
 src/kudu/client/client.h                    |  9 +++++++--
 src/kudu/client/table_statistics-internal.h | 12 ++++++++++++
 src/kudu/tools/kudu-admin-test.cc           | 19 +++++++++++++++++++
 src/kudu/tools/kudu-tool-test.cc            |  3 ++-
 src/kudu/tools/tool_action_table.cc         | 25 +++++++++++++++++++++++++
 6 files changed, 71 insertions(+), 5 deletions(-)

diff --git a/src/kudu/client/client.cc b/src/kudu/client/client.cc
index 2dd657a..28e67f9 100644
--- a/src/kudu/client/client.cc
+++ b/src/kudu/client/client.cc
@@ -885,14 +885,18 @@ KuduTableStatistics::~KuduTableStatistics() {
   delete data_;
 }
 
-int64_t KuduTableStatistics::on_disk_size() {
+int64_t KuduTableStatistics::on_disk_size() const {
   return data_->on_disk_size_;
 }
 
-int64_t KuduTableStatistics::live_row_count() {
+int64_t KuduTableStatistics::live_row_count() const {
   return data_->live_row_count_;
 }
 
+std::string KuduTableStatistics::ToString() const {
+  return data_->ToString();
+}
+
 ////////////////////////////////////////////////////////////
 // KuduTable
 ////////////////////////////////////////////////////////////
diff --git a/src/kudu/client/client.h b/src/kudu/client/client.h
index b64fd5c..2412f8b 100644
--- a/src/kudu/client/client.h
+++ b/src/kudu/client/client.h
@@ -964,12 +964,17 @@ class KUDU_EXPORT KuduTableStatistics {
   /// @return The table's on disk size.
   ///
   /// @note This statistic is pre-replication.
-  int64_t on_disk_size();
+  int64_t on_disk_size() const;
 
   /// @return The table's live row count.
   ///
   /// @note This statistic is pre-replication.
-  int64_t live_row_count();
+  int64_t live_row_count() const;
+
+  /// Stringify this Statistics.
+  ///
+  /// @return A string describing this statistics
+  std::string ToString() const;
 
  private:
   class KUDU_NO_EXPORT Data;
diff --git a/src/kudu/client/table_statistics-internal.h b/src/kudu/client/table_statistics-internal.h
index 54b34ea..153e9f9 100644
--- a/src/kudu/client/table_statistics-internal.h
+++ b/src/kudu/client/table_statistics-internal.h
@@ -17,13 +17,18 @@
 #pragma once
 
 #include <cstdint>
+#include <string>
 
 #include "kudu/client/client.h"
 #include "kudu/gutil/macros.h"
+#include "kudu/gutil/strings/substitute.h"
 
 namespace kudu {
 namespace client {
 
+using std::string;
+using strings::Substitute;
+
 class KuduTableStatistics::Data {
  public:
   Data(uint64_t on_disk_size, uint64_t live_row_count)
@@ -34,6 +39,13 @@ class KuduTableStatistics::Data {
   ~Data() {
   }
 
+  string ToString() const {
+    string display_string = "";
+    display_string += Substitute("on disk size: $0\n", on_disk_size_);
+    display_string += Substitute("live row count: $0\n", live_row_count_);
+    return display_string;
+  }
+
   const uint64_t on_disk_size_;
   const uint64_t live_row_count_;
 
diff --git a/src/kudu/tools/kudu-admin-test.cc b/src/kudu/tools/kudu-admin-test.cc
index 8960a21..feb4d35 100644
--- a/src/kudu/tools/kudu-admin-test.cc
+++ b/src/kudu/tools/kudu-admin-test.cc
@@ -1653,6 +1653,25 @@ TEST_F(AdminCliTest, TestListTablesDetail) {
   }
 }
 
+TEST_F(AdminCliTest, TestGetTableStatistics) {
+  vector<string> master_flags{ "--mock_table_metrics_for_testing=true",
+                               "--on_disk_size_for_testing=1024",
+                               "--live_row_count_for_testing=1000" };
+
+  NO_FATALS(BuildAndStart({}, master_flags));
+
+  string stdout, stderr;
+  Status s = RunKuduTool({
+    "table",
+    "statistics",
+    cluster_->master()->bound_rpc_addr().ToString(),
+    kTableId
+  }, &stdout, &stderr);
+  ASSERT_TRUE(s.ok()) << ToolRunInfo(s, stdout, stderr);
+  ASSERT_STR_CONTAINS(stdout, "on disk size: 1024\n"
+                              "live row count: 1000\n");
+}
+
 TEST_F(AdminCliTest, TestDescribeTable) {
   FLAGS_num_tablet_servers = 1;
   FLAGS_num_replicas = 1;
diff --git a/src/kudu/tools/kudu-tool-test.cc b/src/kudu/tools/kudu-tool-test.cc
index f44d2ab..705d1d5 100644
--- a/src/kudu/tools/kudu-tool-test.cc
+++ b/src/kudu/tools/kudu-tool-test.cc
@@ -1109,7 +1109,8 @@ TEST_F(ToolTest, TestModeHelp) {
         "scan.*Scan rows from a table",
         "copy.*Copy table data to another table",
         "set_extra_config.*Change a extra configuration value on a table",
-        "get_extra_configs.*Get the extra configuration properties for a table"
+        "get_extra_configs.*Get the extra configuration properties for a table",
+        "statistics.*Get table statistics"
     };
     NO_FATALS(RunTestHelp("table", kTableModeRegexes));
   }
diff --git a/src/kudu/tools/tool_action_table.cc b/src/kudu/tools/tool_action_table.cc
index 6fcd83c..72f55e2 100644
--- a/src/kudu/tools/tool_action_table.cc
+++ b/src/kudu/tools/tool_action_table.cc
@@ -68,6 +68,7 @@ using kudu::client::KuduSchema;
 using kudu::client::KuduTable;
 using kudu::client::KuduTableAlterer;
 using kudu::client::KuduTableCreator;
+using kudu::client::KuduTableStatistics;
 using kudu::client::KuduValue;
 using kudu::client::internal::ReplicaController;
 using std::cerr;
@@ -860,6 +861,22 @@ Status DeleteColumn(const RunnerContext& context) {
   return alterer->Alter();
 }
 
+Status GetTableStatistics(const RunnerContext& context) {
+  const string& table_name = FindOrDie(context.required_args, kTableNameArg);
+  client::sp::shared_ptr<KuduClient> client;
+  RETURN_NOT_OK(CreateKuduClient(context, &client));
+
+  unique_ptr<KuduTableStatistics> statistics;
+  KuduTableStatistics *table_statistics;
+  RETURN_NOT_OK_PREPEND(client->GetTableStatistics(table_name, &table_statistics),
+                        "failed to get table statistics.");
+  statistics.reset(table_statistics);
+  cout << "TABLE " << table_name << endl;
+  cout << statistics->ToString() << endl;
+
+  return Status::OK();
+}
+
 } // anonymous namespace
 
 unique_ptr<Mode> BuildTableMode() {
@@ -1060,6 +1077,13 @@ unique_ptr<Mode> BuildTableMode() {
       .AddRequiredParameter({ kColumnNameArg, "Name of the table column to delete" })
       .Build();
 
+  unique_ptr<Action> statistics =
+      ActionBuilder("statistics", &GetTableStatistics)
+          .Description("Get table statistics")
+          .AddRequiredParameter({ kMasterAddressesArg, kMasterAddressesArgDesc })
+          .AddRequiredParameter({ kTableNameArg, "Name of the table to get statistics" })
+          .Build();
+
   return ModeBuilder("table")
       .Description("Operate on Kudu tables")
       .AddAction(std::move(add_range_partition))
@@ -1080,6 +1104,7 @@ unique_ptr<Mode> BuildTableMode() {
       .AddAction(std::move(copy_table))
       .AddAction(std::move(set_extra_config))
       .AddAction(std::move(get_extra_configs))
+      .AddAction(std::move(statistics))
       .Build();
 }
 


Mime
View raw message