From d0a31283873d68dc6025e7e2028223aba0b38896 Mon Sep 17 00:00:00 2001 From: Toshihiro Suzuki Date: Wed, 18 Apr 2018 14:47:04 +0900 Subject: [PATCH] HBASE-20293 get_splits returns duplicate split points when region replication is on --- hbase-shell/src/main/ruby/hbase/table.rb | 8 +++++--- hbase-shell/src/main/ruby/shell/commands/get_splits.rb | 3 +-- hbase-shell/src/test/ruby/hbase/table_test.rb | 15 +++++++++++++++ hbase-shell/src/test/ruby/test_helper.rb | 11 +++++++++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/hbase-shell/src/main/ruby/hbase/table.rb b/hbase-shell/src/main/ruby/hbase/table.rb index 3e3fb8e..90284ea 100644 --- a/hbase-shell/src/main/ruby/hbase/table.rb +++ b/hbase-shell/src/main/ruby/hbase/table.rb @@ -20,6 +20,7 @@ include Java java_import org.apache.hadoop.hbase.util.Bytes +java_import org.apache.hadoop.hbase.client.RegionReplicaUtil # Wrapper for org.apache.hadoop.hbase.client.Table @@ -720,9 +721,10 @@ EOF # Get the split points for the table def _get_splits_internal() locator = @table.getRegionLocator() - locator.getAllRegionLocations() - .map { |i| Bytes.toStringBinary(i.getRegionInfo().getStartKey) } - .delete_if { |k| k == "" } + locator.getAllRegionLocations(). + select { |s| RegionReplicaUtil.isDefaultReplica(s.getRegionInfo) }. + map { |i| Bytes.toStringBinary(i.getRegionInfo().getStartKey) }. + delete_if { |k| k == "" } ensure locator.close() end diff --git a/hbase-shell/src/main/ruby/shell/commands/get_splits.rb b/hbase-shell/src/main/ruby/shell/commands/get_splits.rb index 26be15f..d13219d 100644 --- a/hbase-shell/src/main/ruby/shell/commands/get_splits.rb +++ b/hbase-shell/src/main/ruby/shell/commands/get_splits.rb @@ -38,8 +38,7 @@ EOF def get_splits(table) splits = table._get_splits_internal() - puts(format('Total number of splits = %d', - numsplits: (splits.size + 1))) + puts(format('Total number of splits = %d', splits.size + 1)) splits end end diff --git a/hbase-shell/src/test/ruby/hbase/table_test.rb b/hbase-shell/src/test/ruby/hbase/table_test.rb index 6ffdf89..3ac101c 100644 --- a/hbase-shell/src/test/ruby/hbase/table_test.rb +++ b/hbase-shell/src/test/ruby/hbase/table_test.rb @@ -188,6 +188,7 @@ module Hbase end # Complex data management methods tests + # rubocop:disable Metrics/ClassLength class TableComplexMethodsTest < Test::Unit::TestCase include TestHelpers @@ -635,5 +636,19 @@ module Hbase assert_equal(0, splits.size) assert_equal([], splits) end + + define_test 'Split count for a table with region replicas' do + @test_table_name = 'tableWithRegionReplicas' + create_test_table_with_region_replicas(@test_table_name, 3, + SPLITS => ['10']) + @table = table(@test_table_name) + splits = @table._get_splits_internal + # In this case, total splits should be 1 even if the number of region + # replicas is 3. + assert_equal(1, splits.size) + assert_equal(['10'], splits) + drop_test_table(@test_table_name) + end end + # rubocop:enable Metrics/ClassLength end diff --git a/hbase-shell/src/test/ruby/test_helper.rb b/hbase-shell/src/test/ruby/test_helper.rb index b4bec90..c947439 100644 --- a/hbase-shell/src/test/ruby/test_helper.rb +++ b/hbase-shell/src/test/ruby/test_helper.rb @@ -107,6 +107,17 @@ module Hbase end end + def create_test_table_with_region_replicas(name, num_of_replicas, splits) + # Create the table if needed + unless admin.exists?(name) + admin.create name, 'f1', { REGION_REPLICATION => num_of_replicas }, + splits + end + + # Enable the table if needed + admin.enable(name) unless admin.enabled?(name) + end + def drop_test_table(name) return unless admin.exists?(name) begin -- 2.10.1 (Apple Git-78)