Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HBASE-23046 Remove compatibility case from truncate command #638

Merged
merged 2 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 13 additions & 66 deletions hbase-shell/src/main/ruby/hbase/admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -576,83 +576,30 @@ def snapshot_cleanup_enabled?
def truncate(table_name_str)
puts "Truncating '#{table_name_str}' table (it may take a while):"
table_name = TableName.valueOf(table_name_str)
table_description = @admin.getDescriptor(table_name)
raise ArgumentError, "Table #{table_name_str} is not enabled. Enable it first." unless
enabled?(table_name_str)
puts 'Disabling table...'
@admin.disableTable(table_name)

begin
puts 'Truncating table...'
@admin.truncateTable(table_name, false)
rescue => e
# Handle the compatibility case, where the truncate method doesn't exists on the Master
raise e unless e.respond_to?(:cause) && !e.cause.nil?
rootCause = e.cause
if rootCause.is_a?(org.apache.hadoop.hbase.DoNotRetryIOException)
# Handle the compatibility case, where the truncate method doesn't exists on the Master
puts 'Dropping table...'
@admin.deleteTable(table_name)

puts 'Creating table...'
@admin.createTable(table_description)
else
raise e
end
if enabled?(table_name_str)
puts 'Disabling table...'
disable(table_name_str)
end

puts 'Truncating table...'
@admin.truncateTable(table_name, false)
end

#----------------------------------------------------------------------------------------------
# Truncates table while maintaing region boundaries (deletes all records by recreating the table)
# Truncates table while maintaining region boundaries
# (deletes all records by recreating the table)
def truncate_preserve(table_name_str, conf = @conf)
puts "Truncating '#{table_name_str}' table (it may take a while):"
table_name = TableName.valueOf(table_name_str)
locator = @connection.getRegionLocator(table_name)
begin
splits = locator.getAllRegionLocations
.map { |i| Bytes.toStringBinary(i.getRegion.getStartKey) }
.delete_if { |k| k == '' }.to_java :String
splits = org.apache.hadoop.hbase.util.Bytes.toBinaryByteArrays(splits)
ensure
locator.close
end

table_description = @admin.getDescriptor(table_name)
puts 'Disabling table...'
disable(table_name_str)

begin
puts 'Truncating table...'
# just for test
unless conf.getBoolean('hbase.client.truncatetable.support', true)
raise UnsupportedMethodException, 'truncateTable'
end
@admin.truncateTable(table_name, true)
rescue => e
# Handle the compatibility case, where the truncate method doesn't exists on the Master
raise e unless e.respond_to?(:cause) && !e.cause.nil?
rootCause = e.cause
if rootCause.is_a?(org.apache.hadoop.hbase.DoNotRetryIOException)
# Handle the compatibility case, where the truncate method doesn't exists on the Master
puts 'Dropping table...'
@admin.deleteTable(table_name)

puts 'Creating table with region boundaries...'
@admin.createTable(table_description, splits)
else
raise e
end
end
end

class UnsupportedMethodException < StandardError
def initialize(name)
@method_name = name
if enabled?(table_name_str)
puts 'Disabling table...'
disable(table_name_str)
end

def cause
org.apache.hadoop.hbase.DoNotRetryIOException.new("#{@method_name} is not support")
end
puts 'Truncating table...'
@admin.truncateTable(table_name, true)
end

#----------------------------------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions hbase-shell/src/test/ruby/hbase/admin_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,15 @@ def teardown
assert(!output.empty?)
end

define_test "truncate should work on disabled table" do
table(@test_name).put(1, "x:a", 1)
table(@test_name).put(2, "x:a", 2)
assert_equal(2, table(@test_name)._count_internal)
command(:disable, @test_name)
command(:truncate, @test_name)
assert_equal(0, table(@test_name)._count_internal)
end

#-------------------------------------------------------------------------------

define_test "truncate_preserve should empty a table" do
Expand Down