From b85e2c296affdb96f0cccb97faeb48314fb4bdb0 Mon Sep 17 00:00:00 2001 From: Guangxu Cheng Date: Tue, 30 May 2017 22:51:25 +0800 Subject: [PATCH] HBASE-18129 truncate_preserve fails when the truncate method doesn't exists on the master --- hbase-shell/src/main/ruby/hbase/admin.rb | 13 ++++++++++++- hbase-shell/src/test/ruby/hbase/admin_test.rb | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletions(-) diff --git a/hbase-shell/src/main/ruby/hbase/admin.rb b/hbase-shell/src/main/ruby/hbase/admin.rb index 749b2e8..ef0cbda 100644 --- a/hbase-shell/src/main/ruby/hbase/admin.rb +++ b/hbase-shell/src/main/ruby/hbase/admin.rb @@ -521,8 +521,9 @@ module Hbase locator = @connection.getRegionLocator(table_name) begin splits = locator.getAllRegionLocations(). - map{|i| Bytes.toString(i.getRegionInfo().getStartKey)}. + map{|i| Bytes.toStringBinary(i.getRegionInfo().getStartKey)}. delete_if{|k| k == ""}.to_java :String + splits = org.apache.hadoop.hbase.util.Bytes.toBinaryByteArrays(splits) ensure locator.close() end @@ -533,6 +534,10 @@ module Hbase begin puts 'Truncating table...' + #just for test + unless conf.getBoolean("hbase.client.truncatetable.support", true) + raise UnSupportMethodException + end @admin.truncateTable(table_name, true) rescue => e # Handle the compatibility case, where the truncate method doesn't exists on the Master @@ -551,6 +556,12 @@ module Hbase end end + class UnSupportMethodException < StandardError + def cause + return org.apache.hadoop.hbase.DoNotRetryIOException.new("truncateTable is not support") + end + end + #---------------------------------------------------------------------------------------------- # Check the status of alter command (number of regions reopened) def alter_status(table_name) diff --git a/hbase-shell/src/test/ruby/hbase/admin_test.rb b/hbase-shell/src/test/ruby/hbase/admin_test.rb index de2891b..46e56ca 100644 --- a/hbase-shell/src/test/ruby/hbase/admin_test.rb +++ b/hbase-shell/src/test/ruby/hbase/admin_test.rb @@ -272,6 +272,23 @@ module Hbase output = capture_stdout { command(:truncate, @test_name) } assert(!output.empty?) end + + define_test "truncate_preserve should maintain the previous region boundaries" do + drop_test_table(@create_test_name) + admin.create(@create_test_name, 'a', {NUMREGIONS => 10, SPLITALGO => 'HexStringSplit'}) + splits = table(@create_test_name)._get_splits_internal() + admin.truncate_preserve(@create_test_name, $TEST_CLUSTER.getConfiguration) + assert_equal(splits, table(@create_test_name)._get_splits_internal()) + end + + define_test "truncate_preserve should be fine when truncateTable method doesn't support" do + drop_test_table(@create_test_name) + admin.create(@create_test_name, 'a', {NUMREGIONS => 10, SPLITALGO => 'HexStringSplit'}) + splits = table(@create_test_name)._get_splits_internal() + $TEST_CLUSTER.getConfiguration.setBoolean("hbase.client.truncatetable.support", false) + admin.truncate_preserve(@create_test_name, $TEST_CLUSTER.getConfiguration) + assert_equal(splits, table(@create_test_name)._get_splits_internal()) + end end # Simple administration methods tests -- 1.7.1