View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.util;
19  
20  import java.io.IOException;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.TableName;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.client.TableState;
30  import org.apache.hadoop.hbase.exceptions.DeserializationException;
31  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
32  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
33  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
34  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
35  import org.apache.zookeeper.KeeperException;
36  
37  /**
38   * utlity method to migrate zookeeper data across HBase versions.
39   */
40  @InterfaceAudience.Private
41  public class ZKDataMigrator {
42  
43    private static final Log LOG = LogFactory.getLog(ZKDataMigrator.class);
44  
45    /**
46     * Method for table states migration.
47     * Used when upgrading from pre-2.0 to 2.0
48     * Reading state from zk, applying them to internal state
49     * and delete.
50     * Used by master to clean migration from zk based states to
51     * table descriptor based states.
52     */
53    @Deprecated
54    public static Map<TableName, TableState.State> queryForTableStates(ZooKeeperWatcher zkw)
55        throws KeeperException, InterruptedException {
56      Map<TableName, TableState.State> rv = new HashMap<>();
57      List<String> children = ZKUtil.listChildrenNoWatch(zkw, zkw.tableZNode);
58      if (children == null)
59        return rv;
60      for (String child: children) {
61        TableName tableName = TableName.valueOf(child);
62        ZooKeeperProtos.DeprecatedTableState.State state = getTableState(zkw, tableName);
63        TableState.State newState = TableState.State.ENABLED;
64        if (state != null) {
65          switch (state) {
66          case ENABLED:
67            newState = TableState.State.ENABLED;
68            break;
69          case DISABLED:
70            newState = TableState.State.DISABLED;
71            break;
72          case DISABLING:
73            newState = TableState.State.DISABLING;
74            break;
75          case ENABLING:
76            newState = TableState.State.ENABLING;
77            break;
78          default:
79          }
80        }
81        rv.put(tableName, newState);
82      }
83      return rv;
84    }
85  
86    /**
87     * Gets table state from ZK.
88     * @param zkw ZooKeeperWatcher instance to use
89     * @param tableName table we're checking
90     * @return Null or {@link ZooKeeperProtos.DeprecatedTableState.State} found in znode.
91     * @throws KeeperException
92     */
93    @Deprecated
94    private static  ZooKeeperProtos.DeprecatedTableState.State getTableState(
95        final ZooKeeperWatcher zkw, final TableName tableName)
96        throws KeeperException, InterruptedException {
97      String znode = ZKUtil.joinZNode(zkw.tableZNode, tableName.getNameAsString());
98      byte [] data = ZKUtil.getData(zkw, znode);
99      if (data == null || data.length <= 0) return null;
100     try {
101       ProtobufUtil.expectPBMagicPrefix(data);
102       ZooKeeperProtos.DeprecatedTableState.Builder builder =
103           ZooKeeperProtos.DeprecatedTableState.newBuilder();
104       int magicLen = ProtobufUtil.lengthOfPBMagic();
105       ProtobufUtil.mergeFrom(builder, data, magicLen, data.length - magicLen);
106       return builder.getState();
107     } catch (IOException e) {
108       KeeperException ke = new KeeperException.DataInconsistencyException();
109       ke.initCause(e);
110       throw ke;
111     } catch (DeserializationException e) {
112       throw ZKUtil.convert(e);
113     }
114   }
115 
116 }