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.
17 # View the current status of all regions on an HBase cluster. This is
18 # predominantly used to determined if all the regions in META have been
19 # onlined yet on startup.
21 # To use this script, run:
23 # ${HBASE_HOME}/bin/hbase org.jruby.Main region_status.rb [wait] [--table <table_name>]
28 usage = 'Usage : ./hbase org.jruby.Main region_status.rb [wait]' +
29 '[--table <table_name>]\n'
30 OptionParser.new do |o|
32 o.on('-t', '--table TABLENAME', 'Only process TABLENAME') do |tablename|
33 $tablename = tablename
35 o.on('-h', '--help', 'Display help message') { puts o; exit }
39 SHOULD_WAIT = ARGV[0] == 'wait'
40 if ARGV[0] and not SHOULD_WAIT
48 import org.apache.hadoop.hbase.HBaseConfiguration
49 import org.apache.hadoop.hbase.TableName
50 import org.apache.hadoop.hbase.HConstants
51 import org.apache.hadoop.hbase.MasterNotRunningException
52 import org.apache.hadoop.hbase.client.HBaseAdmin
53 import org.apache.hadoop.hbase.client.Table
54 import org.apache.hadoop.hbase.client.Scan
55 import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter
56 import org.apache.hadoop.hbase.util.Bytes
57 import org.apache.hadoop.hbase.HRegionInfo
58 import org.apache.hadoop.hbase.MetaTableAccessor
59 import org.apache.hadoop.hbase.HTableDescriptor
60 import org.apache.hadoop.hbase.client.ConnectionFactory
62 # disable debug logging on this script for clarity
63 log_level = org.apache.log4j.Level::ERROR
64 org.apache.log4j.Logger.getLogger("org.apache.zookeeper").setLevel(log_level)
65 org.apache.log4j.Logger.getLogger("org.apache.hadoop.hbase").setLevel(log_level)
67 config = HBaseConfiguration.create
68 config.set 'fs.defaultFS', config.get(HConstants::HBASE_DIR)
69 connection = ConnectionFactory.createConnection(config)
70 # wait until the master is running
74 admin = connection.getAdmin()
76 rescue MasterNotRunningException => e
77 print 'Waiting for master to start...\n'
85 # scan META to see how many regions we should have
89 tableNameMetaPrefix = $tablename + HConstants::META_ROW_DELIMITER.chr
91 (tableNameMetaPrefix + HConstants::META_ROW_DELIMITER.chr).to_java_bytes
94 scan.setCacheBlocks(false)
96 scan.setFilter(FirstKeyOnlyFilter.new)
97 INFO = 'info'.to_java_bytes
98 REGION_INFO = 'regioninfo'.to_java_bytes
99 scan.addColumn INFO, REGION_INFO
104 table = connection.getTable(TableName.valueOf('hbase:meta'))
105 scanner = table.getScanner(scan)
106 iter = scanner.iterator
108 rescue IOException => ioe
109 print "Exception trying to scan META: #{ioe}"
115 rowid = Bytes.toString(result.getRow())
116 rowidStr = java.lang.String.new(rowid)
117 if not $tablename.nil? and not rowidStr.startsWith(tableNameMetaPrefix)
118 # Gone too far, break
121 region = MetaTableAccessor::getHRegionInfo(result)
122 if not region.isOffline
123 # only include regions that should be online
128 # If we're trying to see the status of all HBase tables, we need to include the
129 # hbase:meta table, that is not included in our scan
134 # query the master to see how many regions are on region servers
135 if not $tablename.nil?
136 $TableName = TableName.valueOf($tablename.to_java_bytes)
140 server_count = admin.getClusterStatus().getRegionsCount()
142 connection = ConnectionFactory::createConnection(config);
143 server_count = MetaTableAccessor::allTableRegions(connection, $TableName).size()
145 print "Region Status: #{server_count} / #{meta_count}\n"
146 if SHOULD_WAIT and server_count < meta_count
147 #continue this loop until server & meta count match
156 exit server_count == meta_count ? 0 : 1