HBASE-12833 [shell] table.rb leaks connections (Solomon Duskis)

Conflicts:
	hbase-shell/src/main/ruby/hbase/hbase.rb
This commit is contained in:
Enis Soztutar 2015-01-16 11:35:27 -08:00
parent 37a2774c6f
commit e64c400835
8 changed files with 76 additions and 26 deletions

View File

@ -32,15 +32,16 @@ module Hbase
class Admin class Admin
include HBaseConstants include HBaseConstants
def initialize(configuration, formatter) def initialize(admin, formatter)
# @admin = org.apache.hadoop.hbase.client.HBaseAdmin.new(configuration) @admin = admin
@conn = org.apache.hadoop.hbase.client.ConnectionFactory.createConnection(configuration)
@admin = @conn.getAdmin()
connection = @admin.getConnection() connection = @admin.getConnection()
@conf = configuration
@formatter = formatter @formatter = formatter
end end
def close
@admin.close
end
#---------------------------------------------------------------------------------------------- #----------------------------------------------------------------------------------------------
# Returns a list of tables in hbase # Returns a list of tables in hbase
def list(regex = ".*") def list(regex = ".*")
@ -196,7 +197,8 @@ module Hbase
#---------------------------------------------------------------------------------------------- #----------------------------------------------------------------------------------------------
# Returns ZooKeeper status dump # Returns ZooKeeper status dump
def zk_dump def zk_dump
@zk_wrapper = org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher.new(@conf, @zk_wrapper = org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher.new(
@admin.getConfiguration(),
"admin", "admin",
nil) nil)
zk = @zk_wrapper.getRecoverableZooKeeper().getZooKeeper() zk = @zk_wrapper.getRecoverableZooKeeper().getZooKeeper()
@ -397,10 +399,14 @@ module Hbase
#---------------------------------------------------------------------------------------------- #----------------------------------------------------------------------------------------------
# Truncates table while maintaing region boundaries (deletes all records by recreating the table) # Truncates table while maintaing region boundaries (deletes all records by recreating the table)
def truncate_preserve(table_name, conf = @conf) def truncate_preserve(table_name, conf = @conf)
h_table = @conn.getTable(table_name) h_table = @conn.getTable(table_name)
splits = h_table.getRegionLocations().keys().map{|i| Bytes.toStringBinary(i.getStartKey)}.delete_if{|k| k == ""}.to_java :String locator = @conn.getRegionLocator(table_name)
splits = org.apache.hadoop.hbase.util.Bytes.toBinaryByteArrays(splits) splits = locator.getAllRegionLocations().
table_description = h_table.getTableDescriptor() map{|i| Bytes.toString(i.getRegionInfo().getStartKey)}.
delete_if{|k| k == ""}.to_java :String
locator.close()
table_description = @admin.getTableDescriptor(table_name)
yield 'Disabling table...' if block_given? yield 'Disabling table...' if block_given?
disable(table_name) disable(table_name)

View File

@ -38,15 +38,17 @@ module Hbase
configuration.setInt("hbase.client.retries.number", 7) configuration.setInt("hbase.client.retries.number", 7)
configuration.setInt("hbase.ipc.client.connect.max.retries", 3) configuration.setInt("hbase.ipc.client.connect.max.retries", 3)
end end
@connection = org.apache.hadoop.hbase.client.ConnectionFactory.createConnection(
self.configuration)
end end
def admin(formatter) def admin(formatter)
::Hbase::Admin.new(configuration, formatter) ::Hbase::Admin.new(@connection.getAdmin, formatter)
end end
# Create new one each time # Create new one each time
def table(table, shell) def table(table, shell)
::Hbase::Table.new(configuration, table, shell) ::Hbase::Table.new(@connection.getTable(table), shell)
end end
def replication_admin(formatter) def replication_admin(formatter)
@ -60,5 +62,9 @@ module Hbase
def visibility_labels_admin(formatter) def visibility_labels_admin(formatter)
::Hbase::VisibilityLabelsAdmin.new(configuration, formatter) ::Hbase::VisibilityLabelsAdmin.new(configuration, formatter)
end end
def shutdown
@connection.close
end
end end
end end

View File

@ -111,23 +111,17 @@ EOF
# let external objects read the table name # let external objects read the table name
attr_reader :name attr_reader :name
def initialize(configuration, table_name, shell) def initialize(table, shell)
if @@thread_pool then @table = table
@connection = org.apache.hadoop.hbase.client.ConnectionFactory.createConnection( @name = @table.getName().getNameAsString()
configuration, @@thread_pool)
@table = @connection.getTable(org.apache.hadoop.hbase.TableName.valueOf(table_name))
else
@connection = org.apache.hadoop.hbase.client.ConnectionFactory.createConnection(
configuration)
@table = @connection.getTable(org.apache.hadoop.hbase.TableName.valueOf(table_name))
@@thread_pool = @table.getPool()
end
@name = table_name
@shell = shell @shell = shell
@converters = Hash.new() @converters = Hash.new()
end end
def close()
@table.close()
end
# Note the below methods are prefixed with '_' to hide them from the average user, as # Note the below methods are prefixed with '_' to hide them from the average user, as
# they will be much less likely to tab complete to the 'dangerous' internal method # they will be much less likely to tab complete to the 'dangerous' internal method
#---------------------------------------------------------------------------------------------- #----------------------------------------------------------------------------------------------

View File

@ -36,6 +36,10 @@ module Hbase
create_test_table(@test_name) create_test_table(@test_name)
end end
def teardown
shutdown
end
define_test "exists? should return true when a table exists" do define_test "exists? should return true when a table exists" do
assert(admin.exists?('hbase:meta')) assert(admin.exists?('hbase:meta'))
end end
@ -69,6 +73,10 @@ module Hbase
@create_test_name = 'hbase_create_table_test_table' @create_test_name = 'hbase_create_table_test_table'
end end
def teardown
shutdown
end
define_test "list should return a list of tables" do define_test "list should return a list of tables" do
assert(admin.list.member?(@test_name)) assert(admin.list.member?(@test_name))
end end
@ -241,6 +249,10 @@ module Hbase
create_test_table(@test_name) create_test_table(@test_name)
end end
def teardown
shutdown
end
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
define_test "alter should fail with non-string table names" do define_test "alter should fail with non-string table names" do
@ -342,6 +354,7 @@ module Hbase
table = table(@test_name) table = table(@test_name)
assert_not_equal(nil, table) assert_not_equal(nil, table)
table.close
end end
end end
end end

View File

@ -40,6 +40,10 @@ module Hbase
@create_test_name = 'hbase_create_table_test_table' @create_test_name = 'hbase_create_table_test_table'
end end
def teardown
shutdown
end
define_test "Revoke should rid access rights appropriately" do define_test "Revoke should rid access rights appropriately" do
drop_test_table(@test_name) drop_test_table(@test_name)
create_test_table(@test_name) create_test_table(@test_name)

View File

@ -29,9 +29,13 @@ module Hbase
setup_hbase setup_hbase
end end
def teardown
shutdown
end
define_test "Hbase::Table constructor should not fail for existent tables" do define_test "Hbase::Table constructor should not fail for existent tables" do
assert_nothing_raised do assert_nothing_raised do
table('hbase:meta') table('hbase:meta').close()
end end
end end
end end
@ -48,6 +52,11 @@ module Hbase
@test_table = table(@test_name) @test_table = table(@test_name)
end end
def tearDown
@test_table.close()
shutdown
end
define_test "is_meta_table? method should return true for the meta table" do define_test "is_meta_table? method should return true for the meta table" do
assert(table('hbase:meta').is_meta_table?) assert(table('hbase:meta').is_meta_table?)
end end
@ -113,6 +122,11 @@ module Hbase
@test_table.put(105, "x:a", "4") @test_table.put(105, "x:a", "4")
end end
def teardown
@test_table.close
shutdown
end
define_test "put should work without timestamp" do define_test "put should work without timestamp" do
@test_table.put("123", "x:a", "1") @test_table.put("123", "x:a", "1")
end end
@ -203,7 +217,11 @@ module Hbase
@test_table.put(3, "x:a", 21, {ATTRIBUTES=>{'mykey'=>'myvalue'}}) @test_table.put(3, "x:a", 21, {ATTRIBUTES=>{'mykey'=>'myvalue'}})
@test_table.put(3, "x:b", 22, @test_ts, {ATTRIBUTES=>{'mykey'=>'myvalue'}}) @test_table.put(3, "x:b", 22, @test_ts, {ATTRIBUTES=>{'mykey'=>'myvalue'}})
end
def teardown
@test_table.close
shutdown
end end
define_test "count should work w/o a block passed" do define_test "count should work w/o a block passed" do

View File

@ -38,6 +38,11 @@ module Hbase
create_test_table(@test_name) create_test_table(@test_name)
end end
def teardown
@test_table.close
shutdown
end
define_test "Labels should be created as specified" do define_test "Labels should be created as specified" do
label = 'TEST_LABELS' label = 'TEST_LABELS'
count = table('hbase:labels')._count_internal count = table('hbase:labels')._count_internal

View File

@ -47,6 +47,10 @@ module Hbase
hbase = ::Hbase::Hbase.new($TEST_CLUSTER.getConfiguration) hbase = ::Hbase::Hbase.new($TEST_CLUSTER.getConfiguration)
@shell = ::Shell::Shell.new(hbase, formatter) @shell = ::Shell::Shell.new(hbase, formatter)
end end
def shutdown
@shell.hbase.shutdown
end
def table(table) def table(table)
@shell.hbase_table(table) @shell.hbase_table(table)