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>]
27 usage = 'Usage : ./hbase org.jruby.Main region_status.rb [wait]' \
28 '[--table <table_name>]\n'
29 OptionParser.new do |o|
31 o.on('-t', '--table TABLENAME', 'Only process TABLENAME') do |tablename|
32 $tablename = tablename
34 o.on('-h', '--help', 'Display help message') { puts o; exit }
38 SHOULD_WAIT = ARGV[0] == 'wait'
39 if ARGV[0] && !SHOULD_WAIT
46 java_import org.apache.hadoop.hbase.HBaseConfiguration
47 java_import org.apache.hadoop.hbase.TableName
48 java_import org.apache.hadoop.hbase.HConstants
49 java_import org.apache.hadoop.hbase.MasterNotRunningException
50 java_import org.apache.hadoop.hbase.client.HBaseAdmin
51 java_import org.apache.hadoop.hbase.client.Table
52 java_import org.apache.hadoop.hbase.client.Scan
53 java_import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter
54 java_import org.apache.hadoop.hbase.util.Bytes
55 java_import org.apache.hadoop.hbase.HRegionInfo
56 java_import org.apache.hadoop.hbase.MetaTableAccessor
57 java_import org.apache.hadoop.hbase.HTableDescriptor
58 java_import org.apache.hadoop.hbase.client.ConnectionFactory
60 # disable debug logging on this script for clarity
61 log_level = org.apache.log4j.Level::ERROR
62 org.apache.log4j.Logger.getLogger('org.apache.zookeeper').setLevel(log_level)
63 org.apache.log4j.Logger.getLogger('org.apache.hadoop.hbase').setLevel(log_level)
65 config = HBaseConfiguration.create
66 config.set 'fs.defaultFS', config.get(HConstants::HBASE_DIR)
67 connection = ConnectionFactory.createConnection(config)
68 # wait until the master is running
72 admin = connection.getAdmin
74 rescue MasterNotRunningException => e
75 print 'Waiting for master to start...\n'
83 # scan META to see how many regions we should have
87 tableNameMetaPrefix = $tablename + HConstants::META_ROW_DELIMITER.chr
89 (tableNameMetaPrefix + HConstants::META_ROW_DELIMITER.chr).to_java_bytes
92 scan.setCacheBlocks(false)
94 scan.setFilter(FirstKeyOnlyFilter.new)
95 INFO = 'info'.to_java_bytes
96 REGION_INFO = 'regioninfo'.to_java_bytes
97 scan.addColumn INFO, REGION_INFO
102 table = connection.getTable(TableName.valueOf('hbase:meta'))
103 scanner = table.getScanner(scan)
104 iter = scanner.iterator
106 rescue IOException => ioe
107 print "Exception trying to scan META: #{ioe}"
113 rowid = Bytes.toString(result.getRow)
114 rowidStr = java.lang.String.new(rowid)
115 if !$tablename.nil? && !rowidStr.startsWith(tableNameMetaPrefix)
116 # Gone too far, break
120 region = MetaTableAccessor.getHRegionInfo(result)
121 unless region.isOffline
122 # only include regions that should be online
127 # If we're trying to see the status of all HBase tables, we need to include the
128 # hbase:meta table, that is not included in our scan
129 meta_count += 1 if $tablename.nil?
131 # query the master to see how many regions are on region servers
132 $TableName = TableName.valueOf($tablename.to_java_bytes) unless $tablename.nil?
135 server_count = admin.getClusterStatus.getRegionsCount
137 connection = ConnectionFactory.createConnection(config)
138 server_count = MetaTableAccessor.allTableRegions(connection, $TableName).size
140 print "Region Status: #{server_count} / #{meta_count}\n"
141 if SHOULD_WAIT && server_count < meta_count
142 # continue this loop until server & meta count match
151 exit server_count == meta_count ? 0 : 1