From daf2d53c13a9e252b73b753d59ab662f2a5d8ba2 Mon Sep 17 00:00:00 2001 From: xuqinya Date: Wed, 20 Feb 2019 20:25:59 +0800 Subject: [PATCH] HBASE-21768 list_quota_table_sizes/list_quota_snapshots should print human readable values for size --- hbase-shell/src/main/ruby/hbase/quotas.rb | 17 +++++++++++++++++ hbase-shell/src/main/ruby/hbase_constants.rb | 1 + .../ruby/shell/commands/list_quota_snapshots.rb | 14 +++++++++++--- .../ruby/shell/commands/list_quota_table_sizes.rb | 8 +++++++- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/hbase-shell/src/main/ruby/hbase/quotas.rb b/hbase-shell/src/main/ruby/hbase/quotas.rb index 4023aed..d3a7975 100644 --- a/hbase-shell/src/main/ruby/hbase/quotas.rb +++ b/hbase-shell/src/main/ruby/hbase/quotas.rb @@ -309,6 +309,23 @@ module Hbase raise(ArgumentError, 'Invalid throttle limit syntax') end end + + def size_to_str(value) + if value >= (1 << 50) + size = (value / (1 << 50)).to_s << 'P' + elsif value >= (1 << 40) + size = (value / (1 << 40)).to_s << 'T' + elsif value >= (1 << 30) + size = (value / (1 << 30)).to_s << 'G' + elsif value >= (1 << 20) + size = (value / (1 << 20)).to_s << 'M' + elsif value >= (1 << 10) + size = (value / (1 << 10)).to_s << 'K' + else + size = value.to_s << 'B' + end + return size + end # rubocop:enable Metrics/AbcSize, Metrics/MethodLength def _size_from_str(value, suffix) diff --git a/hbase-shell/src/main/ruby/hbase_constants.rb b/hbase-shell/src/main/ruby/hbase_constants.rb index 5c8ab87..5163f50 100644 --- a/hbase-shell/src/main/ruby/hbase_constants.rb +++ b/hbase-shell/src/main/ruby/hbase_constants.rb @@ -98,6 +98,7 @@ module HBaseConstants FORMATTER_CLASS = 'FORMATTER_CLASS'.freeze POLICY = 'POLICY'.freeze REGIONSERVER = 'REGIONSERVER'.freeze + HUMANREADABLE='HUMANREADABLE'.freeze # Load constants from hbase java API def self.promote_constants(constants) diff --git a/hbase-shell/src/main/ruby/shell/commands/list_quota_snapshots.rb b/hbase-shell/src/main/ruby/shell/commands/list_quota_snapshots.rb index 5cb01a9..6949348 100644 --- a/hbase-shell/src/main/ruby/shell/commands/list_quota_snapshots.rb +++ b/hbase-shell/src/main/ruby/shell/commands/list_quota_snapshots.rb @@ -36,18 +36,20 @@ be retreived from that RegionServer instead of the quota table. For example: hbase> list_quota_snapshots + hbase> list_quota_snapshots({HUMANREADABLE=>'true'}) hbase> list_quota_snapshots({TABLE => 'table1'}) hbase> list_quota_snapshots({NAMESPACE => 'org1'}) hbase> list_quota_snapshots({REGIONSERVER => 'server1.domain,16020,1483482894742'}) hbase> list_quota_snapshots({NAMESPACE => 'org1', REGIONSERVER => 'server1.domain,16020,1483482894742'}) EOF end - + def command(args = {}) # All arguments may be nil desired_table = args[TABLE] desired_namespace = args[NAMESPACE] desired_regionserver = args[REGIONSERVER] + desired_humanreadable = args[HUMANREADABLE] formatter.header(%w[TABLE USAGE LIMIT IN_VIOLATION POLICY]) count = 0 quotas_admin.get_quota_snapshots(desired_regionserver).each do |table_name, snapshot| @@ -55,8 +57,14 @@ EOF next unless accept? table_name, desired_table, desired_namespace status = snapshot.getQuotaStatus policy = get_policy(status) - formatter.row([table_name.to_s, snapshot.getUsage.to_s, snapshot.getLimit.to_s, - status.isInViolation.to_s, policy]) + if desired_humanreadable == 'true' + formatter.row([table_name.to_s, quotas_admin.size_to_str(snapshot.getUsage), + quotas_admin.size_to_str(snapshot.getLimit), + status.isInViolation.to_s, policy]) + else + formatter.row([table_name.to_s, snapshot.getUsage.to_s, + snapshot.getLimit.to_s,status.isInViolation.to_s, policy]) + end count += 1 end formatter.footer(count) diff --git a/hbase-shell/src/main/ruby/shell/commands/list_quota_table_sizes.rb b/hbase-shell/src/main/ruby/shell/commands/list_quota_table_sizes.rb index ef7505e..4c52a59 100644 --- a/hbase-shell/src/main/ruby/shell/commands/list_quota_table_sizes.rb +++ b/hbase-shell/src/main/ruby/shell/commands/list_quota_table_sizes.rb @@ -30,14 +30,20 @@ provides a higher-level of insight than this command. For example: hbase> list_quota_table_sizes + hbase> list_quota_table_sizes({HUMANREADABLE=>'true'}) EOF end def command(_args = {}) + desired_humanreadable = args[HUMANREADABLE] formatter.header(%w[TABLE SIZE]) count = 0 quotas_admin.get_master_table_sizes.each do |tableName, size| - formatter.row([tableName.to_s, size.to_s]) + if desired_humanreadable == 'true' + formatter.row([tableName.to_s, quotas_admin.size_to_str(size)]) + else + formatter.row([tableName.to_s, size.to_s]) + end count += 1 end formatter.footer(count) -- 1.7.1