You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by zg...@apache.org on 2019/01/31 04:16:30 UTC

[hbase] branch branch-2.0 updated: HBASE-21634: Print error message when user uses unacceptable values for LIMIT while setting quotas.

This is an automated email from the ASF dual-hosted git repository.

zghao pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/hbase.git


The following commit(s) were added to refs/heads/branch-2.0 by this push:
     new 46101cc  HBASE-21634: Print error message when user uses unacceptable values for LIMIT while setting quotas.
46101cc is described below

commit 46101cc2bccdfbaf437bef40eddf4523fb583b7f
Author: Sakthi <sa...@gmail.com>
AuthorDate: Wed Jan 30 14:45:10 2019 -0800

    HBASE-21634: Print error message when user uses unacceptable values for LIMIT while setting quotas.
    
    Signed-off-by: Guanghao Zhang <zg...@apache.org>
---
 hbase-shell/src/main/ruby/hbase/quotas.rb      | 26 +++++---
 hbase-shell/src/test/ruby/hbase/quotas_test.rb | 86 +++++++++++++++++++++++++-
 2 files changed, 101 insertions(+), 11 deletions(-)

diff --git a/hbase-shell/src/main/ruby/hbase/quotas.rb b/hbase-shell/src/main/ruby/hbase/quotas.rb
index 054b57a..900d176 100644
--- a/hbase-shell/src/main/ruby/hbase/quotas.rb
+++ b/hbase-shell/src/main/ruby/hbase/quotas.rb
@@ -45,6 +45,7 @@ module HBaseQuotasConstants
 end
 
 module Hbase
+  # rubocop:disable Metrics/ClassLength
   class QuotasAdmin
     def initialize(admin)
       @admin = admin
@@ -117,6 +118,8 @@ module Hbase
       @admin.setQuota(settings)
     end
 
+    # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
+    # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
     def limit_space(args)
       raise(ArgumentError, 'Argument should be a Hash') unless !args.nil? && args.is_a?(Hash)
       # Let the user provide a raw number
@@ -126,6 +129,10 @@ 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
@@ -145,6 +152,8 @@ module Hbase
       # Apply the quota
       @admin.setQuota(settings)
     end
+    # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
+    # rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
 
     def remove_space_limit(args)
       raise(ArgumentError, 'Argument should be a Hash') unless !args.nil? && args.is_a?(Hash)
@@ -243,13 +252,9 @@ module Hbase
       QuotaTableUtil.getObservedSnapshotSizes(@admin.getConnection)
     end
 
-    def list_snapshot_sizes()
-      QuotaTableUtil.getObservedSnapshotSizes(@admin.getConnection())
-    end
-
     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
@@ -261,20 +266,21 @@ module Hbase
       end
     end
 
+    # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
     def _parse_limit(str_limit, type_cls, type)
       str_limit = str_limit.downcase
-      match = /(\d+)(req|[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')
         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]
@@ -289,6 +295,7 @@ module Hbase
         raise(ArgumentError, 'Invalid throttle limit syntax')
       end
     end
+    # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
 
     def _size_from_str(value, suffix)
       case suffix
@@ -301,4 +308,5 @@ module Hbase
       value
     end
   end
+  # rubocop:enable Metrics/ClassLength
 end
diff --git a/hbase-shell/src/test/ruby/hbase/quotas_test.rb b/hbase-shell/src/test/ruby/hbase/quotas_test.rb
index fe4fb28..1956ba5 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
@@ -136,4 +217,5 @@ module Hbase
       assert(output.include? snapshot2)
     end
   end
+  # rubocop:enable Metrics/ClassLength
 end