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