Skip to content

Commit a1ec78b

Browse files
petersomogyirobertkerek-cldr
authored andcommitted
HBASE-23046 Remove compatibility case from truncate command (apache#638)
Author: Peter Somogyi Reason: Bug Ref: CDH-82052 Signed-off-by: Sean Busbey <busbey@apache.org> (cherry picked from commit 08b82c5) Change-Id: I9a748ea60a2e6e1b3bb17c52892909b694bab07e
1 parent 1eb77fe commit a1ec78b

File tree

2 files changed

+32
-85
lines changed

2 files changed

+32
-85
lines changed

hbase-shell/src/main/ruby/hbase/admin.rb

Lines changed: 14 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -517,83 +517,30 @@ def get_table_attributes(table_name)
517517
def truncate(table_name_str)
518518
puts "Truncating '#{table_name_str}' table (it may take a while):"
519519
table_name = TableName.valueOf(table_name_str)
520-
table_description = @admin.getTableDescriptor(table_name)
521-
raise ArgumentError, "Table #{table_name_str} is not enabled. Enable it first." unless
522-
enabled?(table_name_str)
523-
puts 'Disabling table...'
524-
@admin.disableTable(table_name)
525520

526-
begin
527-
puts 'Truncating table...'
528-
@admin.truncateTable(table_name, false)
529-
rescue => e
530-
# Handle the compatibility case, where the truncate method doesn't exists on the Master
531-
raise e unless e.respond_to?(:cause) && !e.cause.nil?
532-
rootCause = e.cause
533-
if rootCause.is_a?(org.apache.hadoop.hbase.DoNotRetryIOException)
534-
# Handle the compatibility case, where the truncate method doesn't exists on the Master
535-
puts 'Dropping table...'
536-
@admin.deleteTable(table_name)
537-
538-
puts 'Creating table...'
539-
@admin.createTable(table_description)
540-
else
541-
raise e
542-
end
521+
if enabled?(table_name_str)
522+
puts 'Disabling table...'
523+
disable(table_name_str)
543524
end
525+
526+
puts 'Truncating table...'
527+
@admin.truncateTable(table_name, false)
544528
end
545529

546530
#----------------------------------------------------------------------------------------------
547-
# Truncates table while maintaing region boundaries (deletes all records by recreating the table)
548-
def truncate_preserve(table_name_str, conf = @conf)
531+
# Truncates table while maintaining region boundaries
532+
# (deletes all records by recreating the table)
533+
def truncate_preserve(table_name_str)
549534
puts "Truncating '#{table_name_str}' table (it may take a while):"
550535
table_name = TableName.valueOf(table_name_str)
551-
locator = @connection.getRegionLocator(table_name)
552-
begin
553-
splits = locator.getAllRegionLocations
554-
.map { |i| Bytes.toStringBinary(i.getRegionInfo.getStartKey) }
555-
.delete_if { |k| k == '' }.to_java :String
556-
splits = org.apache.hadoop.hbase.util.Bytes.toBinaryByteArrays(splits)
557-
ensure
558-
locator.close
559-
end
560-
561-
table_description = @admin.getTableDescriptor(table_name)
562-
puts 'Disabling table...'
563-
disable(table_name_str)
564536

565-
begin
566-
puts 'Truncating table...'
567-
# just for test
568-
unless conf.getBoolean('hbase.client.truncatetable.support', true)
569-
raise UnsupportedMethodException, 'truncateTable'
570-
end
571-
@admin.truncateTable(table_name, true)
572-
rescue => e
573-
# Handle the compatibility case, where the truncate method doesn't exists on the Master
574-
raise e unless e.respond_to?(:cause) && !e.cause.nil?
575-
rootCause = e.cause
576-
if rootCause.is_a?(org.apache.hadoop.hbase.DoNotRetryIOException)
577-
# Handle the compatibility case, where the truncate method doesn't exists on the Master
578-
puts 'Dropping table...'
579-
@admin.deleteTable(table_name)
580-
581-
puts 'Creating table with region boundaries...'
582-
@admin.createTable(table_description, splits)
583-
else
584-
raise e
585-
end
586-
end
587-
end
588-
589-
class UnsupportedMethodException < StandardError
590-
def initialize(name)
591-
@method_name = name
537+
if enabled?(table_name_str)
538+
puts 'Disabling table...'
539+
disable(table_name_str)
592540
end
593541

594-
def cause
595-
org.apache.hadoop.hbase.DoNotRetryIOException.new("#{@method_name} is not support")
596-
end
542+
puts 'Truncating table...'
543+
@admin.truncateTable(table_name, true)
597544
end
598545

599546
#----------------------------------------------------------------------------------------------

hbase-shell/src/test/ruby/hbase/admin_test.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -336,53 +336,53 @@ def teardown
336336

337337
#-------------------------------------------------------------------------------
338338

339-
define_test "truncate should empty a table" do
340-
table(@test_name).put(1, "x:a", 1)
341-
table(@test_name).put(2, "x:a", 2)
339+
define_test 'truncate should empty a table' do
340+
table(@test_name).put(1, 'x:a', 1)
341+
table(@test_name).put(2, 'x:a', 2)
342342
assert_equal(2, table(@test_name)._count_internal)
343343
# This is hacky. Need to get the configuration into admin instance
344344
command(:truncate, @test_name)
345345
assert_equal(0, table(@test_name)._count_internal)
346346
end
347347

348-
define_test "truncate should yield log records" do
348+
define_test 'truncate should yield log records' do
349349
output = capture_stdout { command(:truncate, @test_name) }
350350
assert(!output.empty?)
351351
end
352352

353+
define_test 'truncate should work on disabled table' do
354+
table(@test_name).put(1, 'x:a', 1)
355+
table(@test_name).put(2, 'x:a', 2)
356+
assert_equal(2, table(@test_name)._count_internal)
357+
command(:disable, @test_name)
358+
command(:truncate, @test_name)
359+
assert_equal(0, table(@test_name)._count_internal)
360+
end
361+
353362
#-------------------------------------------------------------------------------
354363

355-
define_test "truncate_preserve should empty a table" do
356-
table(@test_name).put(1, "x:a", 1)
357-
table(@test_name).put(2, "x:a", 2)
364+
define_test 'truncate_preserve should empty a table' do
365+
table(@test_name).put(1, 'x:a', 1)
366+
table(@test_name).put(2, 'x:a', 2)
358367
assert_equal(2, table(@test_name)._count_internal)
359368
# This is hacky. Need to get the configuration into admin instance
360369
command(:truncate_preserve, @test_name)
361370
assert_equal(0, table(@test_name)._count_internal)
362371
end
363372

364-
define_test "truncate_preserve should yield log records" do
373+
define_test 'truncate_preserve should yield log records' do
365374
output = capture_stdout { command(:truncate_preserve, @test_name) }
366375
assert(!output.empty?)
367376
end
368377

369-
define_test "truncate_preserve should maintain the previous region boundaries" do
378+
define_test 'truncate_preserve should maintain the previous region boundaries' do
370379
drop_test_table(@create_test_name)
371380
admin.create(@create_test_name, 'a', {NUMREGIONS => 10, SPLITALGO => 'HexStringSplit'})
372381
splits = table(@create_test_name)._get_splits_internal()
373382
command(:truncate_preserve, @create_test_name)
374383
assert_equal(splits, table(@create_test_name)._get_splits_internal())
375384
end
376385

377-
define_test "truncate_preserve should be fine when truncateTable method doesn't support" do
378-
drop_test_table(@create_test_name)
379-
admin.create(@create_test_name, 'a', {NUMREGIONS => 10, SPLITALGO => 'HexStringSplit'})
380-
splits = table(@create_test_name)._get_splits_internal()
381-
$TEST_CLUSTER.getConfiguration.setBoolean("hbase.client.truncatetable.support", false)
382-
admin.truncate_preserve(@create_test_name, $TEST_CLUSTER.getConfiguration)
383-
assert_equal(splits, table(@create_test_name)._get_splits_internal())
384-
end
385-
386386
#-------------------------------------------------------------------------------
387387

388388
define_test "list_regions should fail for disabled table" do

0 commit comments

Comments
 (0)