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 org.apache.hadoop.hbase.TableName;
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.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 so
049   * 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  @Override
058  protected void metaStateUpdated(TableName tableName, State newState) throws IOException {
059    updateZooKeeper(new TableState(tableName, newState));
060  }
061
062  @Override
063  protected void metaStateDeleted(TableName tableName) throws IOException {
064    deleteZooKeeper(tableName);
065  }
066
067  private void updateZooKeeper(TableState tableState) throws IOException {
068    if (tableState == null) {
069      return;
070    }
071    String znode = ZNodePaths.joinZNode(this.master.getZooKeeper().getZNodePaths().tableZNode,
072      tableState.getTableName().getNameAsString());
073    try {
074      // Make sure znode exists.
075      if (ZKUtil.checkExists(this.master.getZooKeeper(), znode) == -1) {
076        ZKUtil.createAndFailSilent(this.master.getZooKeeper(), znode);
077      }
078      // Now set newState
079      ZooKeeperProtos.DeprecatedTableState.Builder builder =
080        ZooKeeperProtos.DeprecatedTableState.newBuilder();
081      builder.setState(
082        ZooKeeperProtos.DeprecatedTableState.State.valueOf(tableState.getState().toString()));
083      byte[] data = ProtobufUtil.prependPBMagic(builder.build().toByteArray());
084      ZKUtil.setData(this.master.getZooKeeper(), znode, data);
085    } catch (KeeperException e) {
086      // Only hbase1 clients suffer if this fails.
087      LOG.warn("Failed setting table state to zookeeper mirrored for hbase-1.x clients", e);
088    }
089  }
090
091  // This method is called by the super class on each row it finds in the hbase:meta table with
092  // table state in it.
093  @Override
094  protected void fixTableState(TableState tableState) throws IOException {
095    updateZooKeeper(tableState);
096  }
097}