From 9a05628313ac3b9e22924b472a71da72f6fcae65 Mon Sep 17 00:00:00 2001 From: xuqinya Date: Thu, 21 Feb 2019 13:04:12 +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 | 22 ++++++++++++++++++++ hbase-shell/src/main/ruby/hbase_constants.rb | 1 + .../ruby/shell/commands/list_quota_snapshots.rb | 16 ++++++++++++- .../ruby/shell/commands/list_quota_table_sizes.rb | 12 ++++++++++- 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/hbase-shell/src/main/ruby/hbase/quotas.rb b/hbase-shell/src/main/ruby/hbase/quotas.rb index 4023aed..4017900 100644 --- a/hbase-shell/src/main/ruby/hbase/quotas.rb +++ b/hbase-shell/src/main/ruby/hbase/quotas.rb @@ -309,6 +309,28 @@ module Hbase raise(ArgumentError, 'Invalid throttle limit syntax') end end + + def size_to_str(value) + p = 1 << 50 + t = 1 << 40 + g = 1 << 30 + m = 1 << 20 + k = 1 << 10 + if value >= p + size = (value / p).to_s << 'P' + elsif value >= t + size = (value / t).to_s << 'T' + elsif value >= g + size = (value / g).to_s << 'G' + elsif value >= m + size = (value / m).to_s << 'M' + elsif value >= k + size = (value / k).to_s << 'K' + else + size = value.to_s << 'B' + end + 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..5676275 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..65ae2eb 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,6 +36,7 @@ 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'}) @@ -43,11 +44,14 @@ For example: EOF end + # rubocop:disable Metrics/AbcSize + # rubocop:disable Metrics/MethodLength 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,12 +59,20 @@ 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) end + # rubocop:enable Metrics/AbcSize + # rubocop:enable Metrics/MethodLength def get_policy(status) # Unwrap the violation policy if it exists 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..0c0c935 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,18 +30,28 @@ 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 + # rubocop:disable Metrics/AbcSize + # rubocop:disable Metrics/MethodLength 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) end + # rubocop:enable Metrics/AbcSize + # rubocop:enable Metrics/MethodLength end end end -- 1.7.1