HBASE-23232 Remove rsgroup profile from pom.xml of hbase-assembly (#779)
[hbase.git] / hbase-shell / src / main / ruby / hbase / rsgroup_admin.rb
blob4e32ea4447b4a65a3e0257ccaf6c7501637d1eb5
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.
18 include Java
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
24 module Hbase
25   class RSGroupAdmin
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
32     end
34     def close
35       @admin.close
36     end
38     #--------------------------------------------------------------------------
39     # Returns a list of groups in hbase
40     def list_rs_groups
41       @admin.listRSGroups
42     end
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?
49       group
50     end
52     #--------------------------------------------------------------------------
53     # add a group
54     def add_rs_group(group_name)
55       @admin.addRSGroup(group_name)
56     end
58     #--------------------------------------------------------------------------
59     # remove a group
60     def remove_rs_group(group_name)
61       @admin.removeRSGroup(group_name)
62     end
64     #--------------------------------------------------------------------------
65     # balance a group
66     def balance_rs_group(group_name)
67       @admin.balanceRSGroup(group_name)
68     end
70     #--------------------------------------------------------------------------
71     # move server to a group
72     def move_servers(dest, *args)
73       servers = java.util.HashSet.new
74       args[0].each do |s|
75         servers.add(org.apache.hadoop.hbase.net.Address.fromString(s))
76       end
77       @admin.moveServers(servers, dest)
78     end
80     #--------------------------------------------------------------------------
81     # move tables to a group
82     def move_tables(dest, *args)
83       tables = java.util.HashSet.new
84       args[0].each do |s|
85         tables.add(org.apache.hadoop.hbase.TableName.valueOf(s))
86       end
87       @admin.moveTables(tables, dest)
88     end
90     #--------------------------------------------------------------------------
91     # move namespaces to a group
92     def move_namespaces(dest, *args)
93       tables = get_tables(args[0])
94       @admin.moveTables(tables, dest)
95     end
97     #--------------------------------------------------------------------------
98     # get group of server
99     def get_rsgroup_of_server(server)
100       res = @admin.getRSGroupOfServer(
101         org.apache.hadoop.hbase.net.Address.fromString(server)
102       )
103       raise(ArgumentError, 'Server has no group: ' + server) if res.nil?
104       res
105     end
107     #--------------------------------------------------------------------------
108     # get group of table
109     def get_rsgroup_of_table(table)
110       res = @admin.getRSGroupInfoOfTable(
111         org.apache.hadoop.hbase.TableName.valueOf(table)
112       )
113       raise(ArgumentError, 'Table has no group: ' + table) if res.nil?
114       res
115     end
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
122       args[1].each do |t|
123         tables.add(org.apache.hadoop.hbase.TableName.valueOf(t))
124       end
125       @admin.moveServersAndTables(servers, tables, dest)
126     end
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)
134     end
136     def get_servers(servers)
137       server_set = java.util.HashSet.new
138       servers.each do |s|
139         server_set.add(org.apache.hadoop.hbase.net.Address.fromString(s))
140       end
141       server_set
142     end
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))
150       end
151       table_set
152     end
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))
160       end
161       tables
162     end
164     # Does Namespace exist
165     def namespace_exists?(ns)
166       return !@hb_admin.getNamespaceDescriptor(ns).nil?
167     rescue org.apache.hadoop.hbase.NamespaceNotFoundException
168       return false
169     end
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
177       args.each do |s|
178         servers.add(org.apache.hadoop.hbase.net.Address.fromString(s))
179       end
180       @admin.removeServers(servers)
181     end
182   end