From 5fdd646843ed36b30c4b21e8e1720e090c9dd24d Mon Sep 17 00:00:00 2001 From: Sakthi Date: Wed, 9 Jan 2019 01:37:37 -0800 Subject: [PATCH] HBASE-21634: Print error message when user uses unacceptable values for LIMIT while setting quotas. --- hbase-shell/src/main/ruby/hbase/quotas.rb | 14 +-- .../src/test/ruby/hbase/quotas_test.rb | 86 ++++++++++++++++++- 2 files changed, 92 insertions(+), 8 deletions(-) diff --git a/hbase-shell/src/main/ruby/hbase/quotas.rb b/hbase-shell/src/main/ruby/hbase/quotas.rb index 38cb3e334dd0279e0522af4dfad188daac2a7feb..5c6431a771914d56f8e9c5a19646271e20e2f2be 100644 --- a/hbase-shell/src/main/ruby/hbase/quotas.rb +++ b/hbase-shell/src/main/ruby/hbase/quotas.rb @@ -126,6 +126,9 @@ module Hbase # Parse a string a 1K, 2G, etc. _parse_size(args[LIMIT]) end + if limit <= 0 + raise(ArgumentError, 'Invalid space limit, must be greater than 0') + end # Extract the policy, failing if something bogus was provided policy = SpaceViolationPolicy.valueOf(args[POLICY]) # Create a table or namespace quota @@ -249,7 +252,7 @@ module Hbase def _parse_size(str_limit) str_limit = str_limit.downcase - match = /(\d+)([bkmgtp%]*)/.match(str_limit) + match = /^(\d+)([bkmgtp%]?)$/.match(str_limit) if match if match[2] == '%' return match[1].to_i @@ -263,21 +266,20 @@ module Hbase def _parse_limit(str_limit, type_cls, type) str_limit = str_limit.downcase - match = /(\d+)(req|cu|[bkmgtp])\/(sec|min|hour|day)/.match(str_limit) + match = /^(\d+)(req|cu|[bkmgtp])\/(sec|min|hour|day)$/.match(str_limit) if match + limit = match[1].to_i if match[2] == 'req' - limit = match[1].to_i type = type_cls.valueOf(type + '_NUMBER') elsif match[2] == 'cu' - limit = match[1].to_i type = type_cls.valueOf(type + '_CAPACITY_UNIT') else - limit = _size_from_str(match[1].to_i, match[2]) + limit = _size_from_str(limit, match[2]) type = type_cls.valueOf(type + '_SIZE') end if limit <= 0 - raise(ArgumentError, 'Invalid throttle limit, must be greater then 0') + raise(ArgumentError, 'Invalid throttle limit, must be greater than 0') end case match[3] diff --git a/hbase-shell/src/test/ruby/hbase/quotas_test.rb b/hbase-shell/src/test/ruby/hbase/quotas_test.rb index 981001a693db7420cf9f1014159ea36da47e7cd1..1dd215dee515092324ead12c947049090c9ebf1b 100644 --- a/hbase-shell/src/test/ruby/hbase/quotas_test.rb +++ b/hbase-shell/src/test/ruby/hbase/quotas_test.rb @@ -26,6 +26,7 @@ require 'hbase/table' include HBaseConstants module Hbase + # rubocop:disable Metrics/ClassLength class SpaceQuotasTest < Test::Unit::TestCase include TestHelpers @@ -60,11 +61,91 @@ module Hbase end end - define_test 'set quota with a non-numeric limit fails' do + # rubocop:disable Metrics/BlockLength + define_test 'set quota with an invalid limit fails' do + # Space Quota assert_raise(ArgumentError) do - command(:set_quota, TYPE => SPACE, LIMIT => 'asdf', POLICY => NO_INSERTS, TABLE => @test_name) + command(:set_quota, + TYPE => SPACE, + LIMIT => 'asdf', + POLICY => NO_INSERTS, + TABLE => @test_name) + end + assert_raise(ArgumentError) do + command(:set_quota, + TYPE => SPACE, + LIMIT => '1.3G', + POLICY => NO_INSERTS, + TABLE => @test_name) + end + assert_raise(ArgumentError) do + command(:set_quota, + TYPE => SPACE, + LIMIT => 'G1G', + POLICY => NO_INSERTS, + TABLE => @test_name) + end + assert_raise(ArgumentError) do + command(:set_quota, + TYPE => SPACE, + LIMIT => '1GG', + POLICY => NO_INSERTS, + TABLE => @test_name) + end + assert_raise(ArgumentError) do + command(:set_quota, + TYPE => SPACE, + LIMIT => '1H', + POLICY => NO_INSERTS, + TABLE => @test_name) + end + assert_raise(ArgumentError) do + command(:set_quota, + TYPE => SPACE, + LIMIT => '0G', + POLICY => NO_INSERTS, + TABLE => @test_name) + end + + # Throttle Quota + assert_raise(ArgumentError) do + command(:set_quota, + TYPE => THROTTLE, + LIMIT => 'asdf', + TABLE => @test_name) + end + assert_raise(ArgumentError) do + command(:set_quota, + TYPE => THROTTLE, + LIMIT => '1.3G/hour', + TABLE => @test_name) + end + assert_raise(ArgumentError) do + command(:set_quota, + TYPE => THROTTLE, + LIMIT => 'G1G/hour', + TABLE => @test_name) + end + assert_raise(ArgumentError) do + command(:set_quota, + TYPE => THROTTLE, + LIMIT => '1GG/hour', + TABLE => @test_name) + end + assert_raise(ArgumentError) do + command(:set_quota, + TYPE => THROTTLE, + LIMIT => '1H/hour', + TABLE => @test_name) + end + assert_raise(ArgumentError) do + command(:set_quota, + TYPE => THROTTLE, + LIMIT => '0G/hour', + TABLE => @test_name) end end + # rubocop:enable Metrics/BlockLength define_test 'set quota without a limit fails' do assert_raise(ArgumentError) do @@ -171,4 +252,5 @@ module Hbase assert(output.include?('Previous rpc throttle state : false')) end end + # rubocop:enable Metrics/ClassLength end -- 2.17.2 (Apple Git-113)