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