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; 021 022import org.apache.hadoop.hbase.client.TableState; 023import org.apache.hadoop.hbase.protobuf.ProtobufUtil; 024import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos; 025import org.apache.hadoop.hbase.zookeeper.ZKUtil; 026import org.apache.hadoop.hbase.zookeeper.ZNodePaths; 027import org.apache.hadoop.hbase.TableName; 028import org.apache.yetus.audience.InterfaceAudience; 029import org.apache.zookeeper.KeeperException; 030import org.slf4j.Logger; 031import org.slf4j.LoggerFactory; 032 033/** 034 * A subclass of TableStateManager that mirrors change in state out to zookeeper for hbase-1.x 035 * clients to pick up; hbase-1.x clients read table state of zookeeper rather than from hbase:meta 036 * as hbase-2.x clients do. Set "hbase.mirror.table.state.to.zookeeper" to false to disable 037 * mirroring. See in HMaster where we make the choice. The below does zk updates on a best-effort 038 * basis only. If we fail updating zk we keep going because only hbase1 clients suffer; we'll just 039 * log at WARN level. 040 * @deprecated Since 2.0.0. To be removed in 3.0.0. 041 */ 042@Deprecated 043@InterfaceAudience.Private 044public class MirroringTableStateManager extends TableStateManager { 045 private static final Logger LOG = LoggerFactory.getLogger(MirroringTableStateManager.class); 046 047 /** 048 * Set this key to true in Configuration to enable mirroring of table state out to zookeeper 049 * so hbase-1.x clients can pick-up table state. 050 */ 051 static final String MIRROR_TABLE_STATE_TO_ZK_KEY = "hbase.mirror.table.state.to.zookeeper"; 052 053 public MirroringTableStateManager(MasterServices master) { 054 super(master); 055 } 056 057 protected void updateMetaState(TableName tableName, TableState.State newState) 058 throws IOException { 059 // Take the lock. Its reentrant. Calls to super will take same lock. 060 lock.writeLock().lock(); 061 try { 062 super.updateMetaState(tableName, newState); 063 updateZooKeeper(new TableState(tableName, newState)); 064 } finally { 065 lock.writeLock().unlock(); 066 } 067 } 068 069 public void setDeletedTable(TableName tableName) throws IOException { 070 lock.writeLock().lock(); 071 try { 072 super.setDeletedTable(tableName); 073 deleteZooKeeper(tableName); 074 } finally { 075 lock.writeLock().unlock(); 076 } 077 } 078 079 private void updateZooKeeper(TableState tableState) throws IOException { 080 if (tableState == null) { 081 return; 082 } 083 String znode = ZNodePaths.joinZNode(this.master.getZooKeeper().getZNodePaths().tableZNode, 084 tableState.getTableName().getNameAsString()); 085 try { 086 // Make sure znode exists. 087 if (ZKUtil.checkExists(this.master.getZooKeeper(), znode) == -1) { 088 ZKUtil.createAndFailSilent(this.master.getZooKeeper(), znode); 089 } 090 // Now set newState 091 ZooKeeperProtos.DeprecatedTableState.Builder builder = 092 ZooKeeperProtos.DeprecatedTableState.newBuilder(); 093 builder.setState(ZooKeeperProtos.DeprecatedTableState.State. 094 valueOf(tableState.getState().toString())); 095 byte [] data = ProtobufUtil.prependPBMagic(builder.build().toByteArray()); 096 ZKUtil.setData(this.master.getZooKeeper(), znode, data); 097 } catch (KeeperException e) { 098 // Only hbase1 clients suffer if this fails. 099 LOG.warn("Failed setting table state to zookeeper mirrored for hbase-1.x clients", e); 100 } 101 } 102 103 // This method is called by the super class on each row it finds in the hbase:meta table with 104 // table state in it. 105 @Override 106 protected void fixTableState(TableState tableState) throws IOException { 107 updateZooKeeper(tableState); 108 } 109}