001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.master; 019 020import java.io.IOException; 021import java.util.HashMap; 022import java.util.Map; 023import java.util.Set; 024import java.util.concurrent.locks.ReadWriteLock; 025import java.util.concurrent.locks.ReentrantReadWriteLock; 026 027import org.apache.hadoop.hbase.client.TableDescriptor; 028import org.apache.hadoop.hbase.exceptions.IllegalArgumentIOException; 029import org.apache.hadoop.hbase.util.ZKDataMigrator; 030import org.apache.hadoop.hbase.zookeeper.ZKUtil; 031import org.apache.hadoop.hbase.zookeeper.ZNodePaths; 032import org.apache.hbase.thirdparty.com.google.common.collect.Sets; 033import edu.umd.cs.findbugs.annotations.NonNull; 034import edu.umd.cs.findbugs.annotations.Nullable; 035 036import org.apache.hadoop.hbase.MetaTableAccessor; 037import org.apache.hadoop.hbase.TableDescriptors; 038import org.apache.hadoop.hbase.TableName; 039import org.apache.hadoop.hbase.TableNotFoundException; 040import org.apache.yetus.audience.InterfaceAudience; 041import org.apache.zookeeper.KeeperException; 042import org.slf4j.Logger; 043import org.slf4j.LoggerFactory; 044import org.apache.hadoop.hbase.client.Connection; 045import org.apache.hadoop.hbase.client.Result; 046import org.apache.hadoop.hbase.client.TableState; 047 048/** 049 * This is a helper class used to manage table states. 050 * This class uses hbase:meta as its store for table state so hbase:meta must be online before 051 * {@link #start()} is called. 052 * TODO: Cache state. Cut down on meta looksups. 053 */ 054// TODO: Make this a guava Service 055@InterfaceAudience.Private 056public class TableStateManager { 057 private static final Logger LOG = LoggerFactory.getLogger(TableStateManager.class); 058 /** 059 * Set this key to false in Configuration to disable migrating table state from zookeeper 060 * so hbase:meta table. 061 */ 062 static final String MIGRATE_TABLE_STATE_FROM_ZK_KEY = "hbase.migrate.table.state.from.zookeeper"; 063 064 final ReadWriteLock lock = new ReentrantReadWriteLock(); 065 final MasterServices master; 066 067 public TableStateManager(MasterServices master) { 068 this.master = master; 069 } 070 071 /** 072 * Set table state to provided. 073 * Caller should lock table on write. 074 * @param tableName table to change state for 075 * @param newState new state 076 * @throws IOException 077 */ 078 public void setTableState(TableName tableName, TableState.State newState) throws IOException { 079 lock.writeLock().lock(); 080 try { 081 updateMetaState(tableName, newState); 082 } finally { 083 lock.writeLock().unlock(); 084 } 085 } 086 087 /** 088 * Set table state to provided but only if table in specified states 089 * Caller should lock table on write. 090 * @param tableName table to change state for 091 * @param newState new state 092 * @param states states to check against 093 * @return null if succeed or table state if failed 094 * @throws IOException 095 */ 096 public TableState setTableStateIfInStates(TableName tableName, 097 TableState.State newState, 098 TableState.State... states) 099 throws IOException { 100 lock.writeLock().lock(); 101 try { 102 TableState currentState = readMetaState(tableName); 103 if (currentState == null) { 104 throw new TableNotFoundException(tableName); 105 } 106 if (currentState.inStates(states)) { 107 updateMetaState(tableName, newState); 108 return null; 109 } else { 110 return currentState; 111 } 112 } finally { 113 lock.writeLock().unlock(); 114 } 115 } 116 117 /** 118 * Set table state to provided but only if table not in specified states 119 * Caller should lock table on write. 120 * @param tableName table to change state for 121 * @param newState new state 122 * @param states states to check against 123 * @throws IOException 124 */ 125 public boolean setTableStateIfNotInStates(TableName tableName, 126 TableState.State newState, 127 TableState.State... states) 128 throws IOException { 129 TableState currentState = readMetaState(tableName); 130 if (currentState == null) { 131 throw new TableNotFoundException(tableName); 132 } 133 if (!currentState.inStates(states)) { 134 updateMetaState(tableName, newState); 135 return true; 136 } else { 137 return false; 138 } 139 } 140 141 public boolean isTableState(TableName tableName, TableState.State... states) { 142 try { 143 TableState tableState = getTableState(tableName); 144 return tableState.isInStates(states); 145 } catch (IOException e) { 146 LOG.error("Unable to get table " + tableName + " state", e); 147 return false; 148 } 149 } 150 151 public void setDeletedTable(TableName tableName) throws IOException { 152 if (tableName.equals(TableName.META_TABLE_NAME)) { 153 return; 154 } 155 MetaTableAccessor.deleteTableState(master.getConnection(), tableName); 156 } 157 158 public boolean isTablePresent(TableName tableName) throws IOException { 159 return readMetaState(tableName) != null; 160 } 161 162 /** 163 * Return all tables in given states. 164 * 165 * @param states filter by states 166 * @return tables in given states 167 * @throws IOException 168 */ 169 public Set<TableName> getTablesInStates(final TableState.State... states) throws IOException { 170 final Set<TableName> rv = Sets.newHashSet(); 171 MetaTableAccessor.fullScanTables(master.getConnection(), new MetaTableAccessor.Visitor() { 172 @Override 173 public boolean visit(Result r) throws IOException { 174 TableState tableState = MetaTableAccessor.getTableState(r); 175 if (tableState != null && tableState.inStates(states)) 176 rv.add(tableState.getTableName()); 177 return true; 178 } 179 }); 180 return rv; 181 } 182 183 public static class TableStateNotFoundException extends TableNotFoundException { 184 TableStateNotFoundException(TableName tableName) { 185 super(tableName.getNameAsString()); 186 } 187 } 188 189 @NonNull 190 public TableState getTableState(TableName tableName) throws IOException { 191 TableState currentState = readMetaState(tableName); 192 if (currentState == null) { 193 throw new TableStateNotFoundException(tableName); 194 } 195 return currentState; 196 } 197 198 protected void updateMetaState(TableName tableName, TableState.State newState) 199 throws IOException { 200 if (tableName.equals(TableName.META_TABLE_NAME)) { 201 if (TableState.State.DISABLING.equals(newState) || 202 TableState.State.DISABLED.equals(newState)) { 203 throw new IllegalArgumentIOException("Cannot disable the meta table; " + newState); 204 } 205 // Otherwise, just return; no need to set ENABLED on meta -- it is always ENABLED. 206 return; 207 } 208 MetaTableAccessor.updateTableState(master.getConnection(), tableName, newState); 209 } 210 211 @Nullable 212 protected TableState readMetaState(TableName tableName) throws IOException { 213 return MetaTableAccessor.getTableState(master.getConnection(), tableName); 214 } 215 216 public void start() throws IOException { 217 TableDescriptors tableDescriptors = master.getTableDescriptors(); 218 migrateZooKeeper(); 219 Connection connection = master.getConnection(); 220 fixTableStates(tableDescriptors, connection); 221 } 222 223 private void fixTableStates(TableDescriptors tableDescriptors, Connection connection) 224 throws IOException { 225 final Map<String, TableDescriptor> allDescriptors = tableDescriptors.getAll(); 226 final Map<String, TableState> states = new HashMap<>(); 227 // NOTE: Ful hbase:meta table scan! 228 MetaTableAccessor.fullScanTables(connection, new MetaTableAccessor.Visitor() { 229 @Override 230 public boolean visit(Result r) throws IOException { 231 TableState state = MetaTableAccessor.getTableState(r); 232 states.put(state.getTableName().getNameAsString(), state); 233 return true; 234 } 235 }); 236 for (Map.Entry<String, TableDescriptor> entry: allDescriptors.entrySet()) { 237 String table = entry.getKey(); 238 if (table.equals(TableName.META_TABLE_NAME.getNameAsString())) { 239 // This table is always enabled. No fixup needed. No entry in hbase:meta needed. 240 // Call through to fixTableState though in case a super class wants to do something. 241 fixTableState(new TableState(TableName.valueOf(table), TableState.State.ENABLED)); 242 continue; 243 } 244 TableState tableState = states.get(table); 245 if (tableState == null) { 246 LOG.warn(table + " has no table state in hbase:meta, assuming ENABLED"); 247 MetaTableAccessor.updateTableState(connection, TableName.valueOf(table), 248 TableState.State.ENABLED); 249 fixTableState(new TableState(TableName.valueOf(table), TableState.State.ENABLED)); 250 } else { 251 fixTableState(tableState); 252 } 253 } 254 } 255 256 /** 257 * For subclasses in case they want to do fixup post hbase:meta. 258 */ 259 protected void fixTableState(TableState tableState) throws IOException {} 260 261 /** 262 * This code is for case where a hbase2 Master is starting for the first time. ZooKeeper is 263 * where we used to keep table state. On first startup, read zookeeper and update hbase:meta 264 * with the table states found in zookeeper. This is tricky as we'll do this check every time we 265 * startup until mirroring is disabled. See the {@link #MIGRATE_TABLE_STATE_FROM_ZK_KEY} flag. 266 * Original form of this migration came in with HBASE-13032. It deleted all znodes when done. 267 * We can't do that if we want to support hbase-1.x clients who need to be able to read table 268 * state out of zk. See {@link MirroringTableStateManager}. 269 * @deprecated Since 2.0.0. Remove in hbase-3.0.0. 270 */ 271 @Deprecated 272 private void migrateZooKeeper() throws IOException { 273 if (!this.master.getConfiguration().getBoolean(MIGRATE_TABLE_STATE_FROM_ZK_KEY, 274 true)) { 275 return; 276 } 277 try { 278 for (Map.Entry<TableName, TableState.State> entry: 279 ZKDataMigrator.queryForTableStates(this.master.getZooKeeper()).entrySet()) { 280 if (this.master.getTableDescriptors().get(entry.getKey()) == null) { 281 deleteZooKeeper(entry.getKey()); 282 LOG.info("Purged table state entry from zookeepr for table not in hbase:meta: " + 283 entry.getKey()); 284 continue; 285 } 286 TableState ts = null; 287 try { 288 ts = getTableState(entry.getKey()); 289 } catch (TableStateNotFoundException e) { 290 // This can happen; table exists but no TableState. 291 } 292 if (ts == null) { 293 TableState.State zkstate = entry.getValue(); 294 // Only migrate if it is an enable or disabled table. If in-between -- ENABLING or 295 // DISABLING then we have a problem; we are starting up an hbase-2 on a cluster with 296 // RIT. It is going to be rough! 297 if (zkstate.equals(TableState.State.ENABLED) || 298 zkstate.equals(TableState.State.DISABLED)) { 299 LOG.info("Migrating table state from zookeeper to hbase:meta; tableName=" + 300 entry.getKey() + ", state=" + entry.getValue()); 301 updateMetaState(entry.getKey(), entry.getValue()); 302 } else { 303 LOG.warn("Table={} has no state and zookeeper state is in-between={} (neither " + 304 "ENABLED or DISABLED); NOT MIGRATING table state", entry.getKey(), zkstate); 305 } 306 } 307 // What if the table states disagree? Defer to the hbase:meta setting rather than have the 308 // hbase-1.x support prevail. 309 } 310 } catch (KeeperException |InterruptedException e) { 311 LOG.warn("Failed reading table state from zookeeper", e); 312 } 313 } 314 315 /** 316 * Utility method that knows how to delete the old hbase-1.x table state znode. 317 * Used also by the Mirroring subclass. 318 * @deprecated Since 2.0.0. To be removed in hbase-3.0.0. 319 */ 320 @Deprecated 321 protected void deleteZooKeeper(TableName tableName) { 322 try { 323 // Delete from ZooKeeper 324 String znode = ZNodePaths.joinZNode(this.master.getZooKeeper().getZNodePaths().tableZNode, 325 tableName.getNameAsString()); 326 ZKUtil.deleteNodeFailSilent(this.master.getZooKeeper(), znode); 327 } catch (KeeperException e) { 328 LOG.warn("Failed deleting table state from zookeeper", e); 329 } 330 } 331}