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
The following commit(s) were added to refs/heads/master by this push:
new bf1a07e Add get table statistics interface for cpp client
bf1a07e is described below
commit bf1a07e8557e09cda8a7fddf4930d0bc2ce8efa9
Author: triplesheep <[email protected]>
AuthorDate: Wed Sep 11 20:43:20 2019 +0800
Add get table statistics interface for cpp client
Change-Id: Ic00f46fd49c258f46a178a92d142d4d93d615d82
Reviewed-on: http://gerrit.cloudera.org:8080/14218
Tested-by: Kudu Jenkins
Reviewed-by: Adar Dembo <[email protected]>
---
src/kudu/client/client-test.cc | 17 +++++++++++
src/kudu/client/client.cc | 47 +++++++++++++++++++++++++++++
src/kudu/client/client.h | 38 +++++++++++++++++++++++
src/kudu/client/master_proxy_rpc.cc | 3 ++
src/kudu/client/table_statistics-internal.h | 45 +++++++++++++++++++++++++++
5 files changed, 150 insertions(+)
diff --git a/src/kudu/client/client-test.cc b/src/kudu/client/client-test.cc
index bc0de7e..ff62b52 100644
--- a/src/kudu/client/client-test.cc
+++ b/src/kudu/client/client-test.cc
@@ -118,6 +118,7 @@ DECLARE_bool(fail_dns_resolution);
DECLARE_bool(location_mapping_by_uuid);
DECLARE_bool(log_inject_latency);
DECLARE_bool(master_support_connect_to_master_rpc);
+DECLARE_bool(mock_table_metrics_for_testing);
DECLARE_bool(rpc_trace_negotiation);
DECLARE_bool(scanner_inject_service_unavailable_on_continue_scan);
DECLARE_int32(flush_threshold_mb);
@@ -136,6 +137,8 @@ DECLARE_int32(scanner_inject_latency_on_each_batch_ms);
DECLARE_int32(scanner_max_batch_size_bytes);
DECLARE_int32(scanner_ttl_ms);
DECLARE_int32(table_locations_ttl_ms);
+DECLARE_int64(live_row_count_for_testing);
+DECLARE_int64(on_disk_size_for_testing);
DECLARE_string(location_mapping_cmd);
DECLARE_string(superuser_acl);
DECLARE_string(user_acl);
@@ -781,6 +784,20 @@ TEST_F(ClientTest, TestListTabletServers) {
tss[0]->hostname());
}
+TEST_F(ClientTest, TestGetTableStatistics) {
+ unique_ptr<KuduTableStatistics> statistics;
+ FLAGS_mock_table_metrics_for_testing = true;
+ FLAGS_on_disk_size_for_testing = 1024;
+ FLAGS_live_row_count_for_testing = 1000;
+ {
+ KuduTableStatistics *table_statistics;
+ ASSERT_OK(client_->GetTableStatistics(kTableName, &table_statistics));
+ statistics.reset(table_statistics);
+ }
+ ASSERT_EQ(FLAGS_on_disk_size_for_testing, statistics->on_disk_size());
+ ASSERT_EQ(FLAGS_live_row_count_for_testing, statistics->live_row_count());
+}
+
TEST_F(ClientTest, TestBadTable) {
shared_ptr<KuduTable> t;
Status s = client_->OpenTable("xxx-does-not-exist", &t);
diff --git a/src/kudu/client/client.cc b/src/kudu/client/client.cc
index fcdb78e..2dd657a 100644
--- a/src/kudu/client/client.cc
+++ b/src/kudu/client/client.cc
@@ -51,6 +51,7 @@
#include "kudu/client/table-internal.h"
#include "kudu/client/table_alterer-internal.h"
#include "kudu/client/table_creator-internal.h"
+#include "kudu/client/table_statistics-internal.h"
#include "kudu/client/tablet-internal.h"
#include "kudu/client/tablet_server-internal.h"
#include "kudu/client/value.h"
@@ -109,6 +110,8 @@ using kudu::master::DeleteTableRequestPB;
using kudu::master::DeleteTableResponsePB;
using kudu::master::GetTableSchemaRequestPB;
using kudu::master::GetTableSchemaResponsePB;
+using kudu::master::GetTableStatisticsRequestPB;
+using kudu::master::GetTableStatisticsResponsePB;
using kudu::master::GetTabletLocationsRequestPB;
using kudu::master::GetTabletLocationsResponsePB;
using kudu::master::ListTablesRequestPB;
@@ -590,6 +593,31 @@ Status KuduClient::GetTablet(const string& tablet_id,
KuduTablet** tablet) {
return Status::OK();
}
+Status KuduClient::GetTableStatistics(const string& table_name,
+ KuduTableStatistics** statistics) {
+ GetTableStatisticsRequestPB req;
+ GetTableStatisticsResponsePB resp;
+ TableIdentifierPB* table = req.mutable_table();
+ table->set_table_name(table_name);
+ MonoTime deadline = MonoTime::Now() + default_admin_operation_timeout();
+ Synchronizer sync;
+ AsyncLeaderMasterRpc<GetTableStatisticsRequestPB,
GetTableStatisticsResponsePB> rpc(
+ deadline, this, BackoffType::EXPONENTIAL, req, &resp,
+ &MasterServiceProxy::GetTableStatisticsAsync, "GetTableStatistics",
+ sync.AsStatusCallback(), {});
+ rpc.SendRpc();
+ RETURN_NOT_OK(sync.Wait());
+ if (resp.has_error()) {
+ return StatusFromPB(resp.error().status());
+ }
+ unique_ptr<KuduTableStatistics> table_statistics(new KuduTableStatistics);
+ table_statistics->data_ = new KuduTableStatistics::Data(resp.on_disk_size(),
+ resp.live_row_count());
+
+ *statistics = table_statistics.release();
+ return Status::OK();
+}
+
string KuduClient::GetMasterAddresses() const {
return HostPort::ToCommaSeparatedString(data_->master_hostports());
}
@@ -847,6 +875,25 @@ Status KuduTableCreator::Create() {
}
////////////////////////////////////////////////////////////
+// KuduTableStatistics
+////////////////////////////////////////////////////////////
+
+KuduTableStatistics::KuduTableStatistics() : data_(nullptr) {
+}
+
+KuduTableStatistics::~KuduTableStatistics() {
+ delete data_;
+}
+
+int64_t KuduTableStatistics::on_disk_size() {
+ return data_->on_disk_size_;
+}
+
+int64_t KuduTableStatistics::live_row_count() {
+ return data_->live_row_count_;
+}
+
+////////////////////////////////////////////////////////////
// KuduTable
////////////////////////////////////////////////////////////
diff --git a/src/kudu/client/client.h b/src/kudu/client/client.h
index 513aa36..b64fd5c 100644
--- a/src/kudu/client/client.h
+++ b/src/kudu/client/client.h
@@ -78,6 +78,7 @@ class KuduSession;
class KuduStatusCallback;
class KuduTableAlterer;
class KuduTableCreator;
+class KuduTableStatistics;
class KuduTablet;
class KuduTabletServer;
class KuduUpdate;
@@ -458,6 +459,16 @@ class KUDU_EXPORT KuduClient : public
sp::enable_shared_from_this<KuduClient> {
Status GetTablet(const std::string& tablet_id,
KuduTablet** tablet) KUDU_NO_EXPORT;
+ /// Get the table statistics by table name.
+ ///
+ /// @param [in] table_name
+ /// Name of the table.
+ /// @param [out] statistics
+ /// Table statistics. The caller takes ownership of the statistics.
+ /// @return Operation status.
+ Status GetTableStatistics(const std::string& table_name,
+ KuduTableStatistics** statistics);
+
/// Get the master RPC addresses as configured on the last leader master this
/// client connected to, as a CSV. If the client has not connected to a
leader
/// master, an empty string is returned.
@@ -944,6 +955,33 @@ class KUDU_EXPORT KuduTableCreator {
DISALLOW_COPY_AND_ASSIGN(KuduTableCreator);
};
+/// @brief In-memory statistics of table.
+class KUDU_EXPORT KuduTableStatistics {
+ public:
+ KuduTableStatistics();
+ ~KuduTableStatistics();
+
+ /// @return The table's on disk size.
+ ///
+ /// @note This statistic is pre-replication.
+ int64_t on_disk_size();
+
+ /// @return The table's live row count.
+ ///
+ /// @note This statistic is pre-replication.
+ int64_t live_row_count();
+
+ private:
+ class KUDU_NO_EXPORT Data;
+
+ friend class KuduClient;
+
+ // Owned.
+ Data* data_;
+
+ DISALLOW_COPY_AND_ASSIGN(KuduTableStatistics);
+};
+
/// @brief A representation of a table on a particular cluster.
///
/// A KuduTable holds the current schema of the table. Any given KuduTable
diff --git a/src/kudu/client/master_proxy_rpc.cc
b/src/kudu/client/master_proxy_rpc.cc
index b873f1d..5f63d82 100644
--- a/src/kudu/client/master_proxy_rpc.cc
+++ b/src/kudu/client/master_proxy_rpc.cc
@@ -64,6 +64,8 @@ using master::GetTableLocationsRequestPB;
using master::GetTableLocationsResponsePB;
using master::GetTableSchemaRequestPB;
using master::GetTableSchemaResponsePB;
+using master::GetTableStatisticsRequestPB;
+using master::GetTableStatisticsResponsePB;
using master::ListMastersRequestPB;
using master::ListMastersResponsePB;
using master::ListTablesRequestPB;
@@ -291,6 +293,7 @@ template class
AsyncLeaderMasterRpc<IsAlterTableDoneRequestPB, IsAlterTableDoneR
template class AsyncLeaderMasterRpc<GetTableSchemaRequestPB,
GetTableSchemaResponsePB>;
template class AsyncLeaderMasterRpc<GetTableLocationsRequestPB,
GetTableLocationsResponsePB>;
template class AsyncLeaderMasterRpc<GetTabletLocationsRequestPB,
GetTabletLocationsResponsePB>;
+template class AsyncLeaderMasterRpc<GetTableStatisticsRequestPB,
GetTableStatisticsResponsePB>;
template class AsyncLeaderMasterRpc<ListTablesRequestPB, ListTablesResponsePB>;
template class AsyncLeaderMasterRpc<ListTabletServersRequestPB,
ListTabletServersResponsePB>;
template class AsyncLeaderMasterRpc<ListMastersRequestPB,
ListMastersResponsePB>;
diff --git a/src/kudu/client/table_statistics-internal.h
b/src/kudu/client/table_statistics-internal.h
new file mode 100644
index 0000000..54b34ea
--- /dev/null
+++ b/src/kudu/client/table_statistics-internal.h
@@ -0,0 +1,45 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+#pragma once
+
+#include <cstdint>
+
+#include "kudu/client/client.h"
+#include "kudu/gutil/macros.h"
+
+namespace kudu {
+namespace client {
+
+class KuduTableStatistics::Data {
+ public:
+ Data(uint64_t on_disk_size, uint64_t live_row_count)
+ : on_disk_size_(on_disk_size),
+ live_row_count_(live_row_count) {
+ }
+
+ ~Data() {
+ }
+
+ const uint64_t on_disk_size_;
+ const uint64_t live_row_count_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(Data);
+};
+
+} // namespace client
+} // namespace kudu