View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.replication;
20  
21  import java.util.List;
22  
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.Abortable;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
27  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
28  import org.apache.hadoop.hbase.zookeeper.ZKConfig;
29  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
30  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
31  import org.apache.zookeeper.KeeperException;
32  
33  
34  /**
35   * This is a base class for maintaining replication state in zookeeper.
36   */
37  @InterfaceAudience.Private
38  public abstract class ReplicationStateZKBase {
39  
40    /**
41     * The name of the znode that contains the replication status of a remote slave (i.e. peer)
42     * cluster.
43     */
44    protected final String peerStateNodeName;
45    /** The name of the base znode that contains all replication state. */
46    protected final String replicationZNode;
47    /** The name of the znode that contains a list of all remote slave (i.e. peer) clusters. */
48    protected final String peersZNode;
49    /** The name of the znode that contains all replication queues */
50    protected final String queuesZNode;
51    /** The name of the znode that contains queues of hfile references to be replicated */
52    protected final String hfileRefsZNode;
53    /** The cluster key of the local cluster */
54    protected final String ourClusterKey;
55    protected final ZooKeeperWatcher zookeeper;
56    protected final Configuration conf;
57    protected final Abortable abortable;
58  
59    // Public for testing
60    public static final byte[] ENABLED_ZNODE_BYTES =
61        toByteArray(ZooKeeperProtos.ReplicationState.State.ENABLED);
62    public static final byte[] DISABLED_ZNODE_BYTES =
63        toByteArray(ZooKeeperProtos.ReplicationState.State.DISABLED);
64    public static final String ZOOKEEPER_ZNODE_REPLICATION_HFILE_REFS_KEY =
65        "zookeeper.znode.replication.hfile.refs";
66    public static final String ZOOKEEPER_ZNODE_REPLICATION_HFILE_REFS_DEFAULT = "hfile-refs";
67  
68    public ReplicationStateZKBase(ZooKeeperWatcher zookeeper, Configuration conf,
69        Abortable abortable) {
70      this.zookeeper = zookeeper;
71      this.conf = conf;
72      this.abortable = abortable;
73  
74      String replicationZNodeName = conf.get("zookeeper.znode.replication", "replication");
75      String peersZNodeName = conf.get("zookeeper.znode.replication.peers", "peers");
76      String queuesZNodeName = conf.get("zookeeper.znode.replication.rs", "rs");
77      String hfileRefsZNodeName = conf.get(ZOOKEEPER_ZNODE_REPLICATION_HFILE_REFS_KEY,
78        ZOOKEEPER_ZNODE_REPLICATION_HFILE_REFS_DEFAULT);
79      this.peerStateNodeName = conf.get("zookeeper.znode.replication.peers.state", "peer-state");
80      this.ourClusterKey = ZKConfig.getZooKeeperClusterKey(this.conf);
81      this.replicationZNode = ZKUtil.joinZNode(this.zookeeper.baseZNode, replicationZNodeName);
82      this.peersZNode = ZKUtil.joinZNode(replicationZNode, peersZNodeName);
83      this.queuesZNode = ZKUtil.joinZNode(replicationZNode, queuesZNodeName);
84      this.hfileRefsZNode = ZKUtil.joinZNode(replicationZNode, hfileRefsZNodeName);
85    }
86  
87    public List<String> getListOfReplicators() {
88      List<String> result = null;
89      try {
90        result = ZKUtil.listChildrenNoWatch(this.zookeeper, this.queuesZNode);
91      } catch (KeeperException e) {
92        this.abortable.abort("Failed to get list of replicators", e);
93      }
94      return result;
95    }
96  
97    /**
98     * @param state
99     * @return Serialized protobuf of <code>state</code> with pb magic prefix prepended suitable for
100    *         use as content of a peer-state znode under a peer cluster id as in
101    *         /hbase/replication/peers/PEER_ID/peer-state.
102    */
103   protected static byte[] toByteArray(final ZooKeeperProtos.ReplicationState.State state) {
104     byte[] bytes =
105         ZooKeeperProtos.ReplicationState.newBuilder().setState(state).build().toByteArray();
106     return ProtobufUtil.prependPBMagic(bytes);
107   }
108 
109   protected boolean peerExists(String id) throws KeeperException {
110     return ZKUtil.checkExists(this.zookeeper, ZKUtil.joinZNode(this.peersZNode, id)) >= 0;
111   }
112 
113   /**
114    * Determine if a ZK path points to a peer node.
115    * @param path path to be checked
116    * @return true if the path points to a peer node, otherwise false
117    */
118   protected boolean isPeerPath(String path) {
119     return path.split("/").length == peersZNode.split("/").length + 1;
120   }
121 }