1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
19 java_import org.apache.hadoop.hbase.util.Pair
21 # Wrapper for org.apache.hadoop.hbase.group.GroupAdminClient
22 # Which is an API to manage region server groups
26 include HBaseConstants
28 def initialize(connection)
29 @connection = connection
30 @admin = org.apache.hadoop.hbase.rsgroup.RSGroupAdminClient.new(connection)
31 @hb_admin = @connection.getAdmin
38 #--------------------------------------------------------------------------
39 # Returns a list of groups in hbase
44 #--------------------------------------------------------------------------
45 # get a group's information
46 def get_rsgroup(group_name)
47 group = @admin.getRSGroupInfo(group_name)
48 raise(ArgumentError, 'Group does not exist: ' + group_name) if group.nil?
52 #--------------------------------------------------------------------------
54 def add_rs_group(group_name)
55 @admin.addRSGroup(group_name)
58 #--------------------------------------------------------------------------
60 def remove_rs_group(group_name)
61 @admin.removeRSGroup(group_name)
64 #--------------------------------------------------------------------------
66 def balance_rs_group(group_name)
67 @admin.balanceRSGroup(group_name)
70 #--------------------------------------------------------------------------
71 # move server to a group
72 def move_servers(dest, *args)
73 servers = java.util.HashSet.new
75 servers.add(org.apache.hadoop.hbase.net.Address.fromString(s))
77 @admin.moveServers(servers, dest)
80 #--------------------------------------------------------------------------
81 # move tables to a group
82 def move_tables(dest, *args)
83 tables = java.util.HashSet.new
85 tables.add(org.apache.hadoop.hbase.TableName.valueOf(s))
87 @admin.moveTables(tables, dest)
90 #--------------------------------------------------------------------------
91 # move namespaces to a group
92 def move_namespaces(dest, *args)
93 tables = get_tables(args[0])
94 @admin.moveTables(tables, dest)
97 #--------------------------------------------------------------------------
99 def get_rsgroup_of_server(server)
100 res = @admin.getRSGroupOfServer(
101 org.apache.hadoop.hbase.net.Address.fromString(server)
103 raise(ArgumentError, 'Server has no group: ' + server) if res.nil?
107 #--------------------------------------------------------------------------
109 def get_rsgroup_of_table(table)
110 res = @admin.getRSGroupInfoOfTable(
111 org.apache.hadoop.hbase.TableName.valueOf(table)
113 raise(ArgumentError, 'Table has no group: ' + table) if res.nil?
117 #--------------------------------------------------------------------------
118 # move server and table to a group
119 def move_servers_tables(dest, *args)
120 servers = get_servers(args[0])
121 tables = java.util.HashSet.new
123 tables.add(org.apache.hadoop.hbase.TableName.valueOf(t))
125 @admin.moveServersAndTables(servers, tables, dest)
128 #--------------------------------------------------------------------------
129 # move server and namespace to a group
130 def move_servers_namespaces(dest, *args)
131 servers = get_servers(args[0])
132 tables = get_tables(args[1])
133 @admin.moveServersAndTables(servers, tables, dest)
136 def get_servers(servers)
137 server_set = java.util.HashSet.new
139 server_set.add(org.apache.hadoop.hbase.net.Address.fromString(s))
144 def get_tables(namespaces)
145 table_set = java.util.HashSet.new
146 error = "Can't find a namespace: "
147 namespaces.each do |ns|
148 raise(ArgumentError, "#{error}#{ns}") unless namespace_exists?(ns)
149 table_set.addAll(get_tables_by_namespace(ns))
154 # Get tables by namespace
155 def get_tables_by_namespace(ns)
156 tables = java.util.HashSet.new
157 tablelist = @hb_admin.listTableNamesByNamespace(ns).map(&:getNameAsString)
158 tablelist.each do |table|
159 tables.add(org.apache.hadoop.hbase.TableName.valueOf(table))
164 # Does Namespace exist
165 def namespace_exists?(ns)
166 return !@hb_admin.getNamespaceDescriptor(ns).nil?
167 rescue org.apache.hadoop.hbase.NamespaceNotFoundException
171 #--------------------------------------------------------------------------
172 # remove decommissioned server from rsgroup
173 def remove_servers(*args)
174 # Flatten params array
175 args = args.flatten.compact
176 servers = java.util.HashSet.new
178 servers.add(org.apache.hadoop.hbase.net.Address.fromString(s))
180 @admin.removeServers(servers)