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.util;
019
020import java.io.IOException;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.TableState;
026import org.apache.hadoop.hbase.exceptions.DeserializationException;
027import org.apache.hadoop.hbase.zookeeper.ZKUtil;
028import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
029import org.apache.hadoop.hbase.zookeeper.ZNodePaths;
030import org.apache.yetus.audience.InterfaceAudience;
031import org.apache.zookeeper.KeeperException;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
036import org.apache.hadoop.hbase.shaded.protobuf.generated.ZooKeeperProtos;
037
038/**
039 * Utlity method to migrate zookeeper data across HBase versions.
040 * @deprecated Since 2.0.0. To be removed in hbase-3.0.0.
041 */
042@Deprecated
043@InterfaceAudience.Private
044public class ZKDataMigrator {
045  private static final Logger LOG = LoggerFactory.getLogger(ZKDataMigrator.class);
046
047  // Shutdown constructor.
048  private ZKDataMigrator() {
049  }
050
051  /**
052   * Method for table states migration. Used when upgrading from pre-2.0 to 2.0 Reading state from
053   * zk, applying them to internal state and delete. Used by master to clean migration from zk based
054   * states to table descriptor based states.
055   * @deprecated Since 2.0.0. To be removed in hbase-3.0.0.
056   */
057  @Deprecated
058  public static Map<TableName, TableState.State> queryForTableStates(ZKWatcher zkw)
059    throws KeeperException, InterruptedException {
060    Map<TableName, TableState.State> rv = new HashMap<>();
061    List<String> children = ZKUtil.listChildrenNoWatch(zkw, zkw.getZNodePaths().tableZNode);
062    if (children == null) return rv;
063    for (String child : children) {
064      TableName tableName = TableName.valueOf(child);
065      ZooKeeperProtos.DeprecatedTableState.State state = getTableState(zkw, tableName);
066      TableState.State newState = TableState.State.ENABLED;
067      if (state != null) {
068        switch (state) {
069          case ENABLED:
070            newState = TableState.State.ENABLED;
071            break;
072          case DISABLED:
073            newState = TableState.State.DISABLED;
074            break;
075          case DISABLING:
076            newState = TableState.State.DISABLING;
077            break;
078          case ENABLING:
079            newState = TableState.State.ENABLING;
080            break;
081          default:
082        }
083      }
084      rv.put(tableName, newState);
085    }
086    return rv;
087  }
088
089  /**
090   * Gets table state from ZK.
091   * @param zkw       ZKWatcher instance to use
092   * @param tableName table we're checking
093   * @return Null or
094   *         {@link org.apache.hadoop.hbase.shaded.protobuf.generated.ZooKeeperProtos.DeprecatedTableState.State}
095   *         found in znode. n * @deprecated Since 2.0.0. To be removed in hbase-3.0.0.
096   */
097  @Deprecated
098  private static ZooKeeperProtos.DeprecatedTableState.State getTableState(final ZKWatcher zkw,
099    final TableName tableName) throws KeeperException, InterruptedException {
100    String znode =
101      ZNodePaths.joinZNode(zkw.getZNodePaths().tableZNode, tableName.getNameAsString());
102    byte[] data = ZKUtil.getData(zkw, znode);
103    if (data == null || data.length <= 0) return null;
104    try {
105      ProtobufUtil.expectPBMagicPrefix(data);
106      ZooKeeperProtos.DeprecatedTableState.Builder builder =
107        ZooKeeperProtos.DeprecatedTableState.newBuilder();
108      int magicLen = ProtobufUtil.lengthOfPBMagic();
109      ProtobufUtil.mergeFrom(builder, data, magicLen, data.length - magicLen);
110      return builder.getState();
111    } catch (IOException e) {
112      KeeperException ke = new KeeperException.DataInconsistencyException();
113      ke.initCause(e);
114      throw ke;
115    } catch (DeserializationException e) {
116      throw ZKUtil.convert(e);
117    }
118  }
119}