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.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.Abortable;
26  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
27  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
28  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
29  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
30  import org.apache.zookeeper.KeeperException;
31  
32  
33  /**
34   * This is a base class for maintaining replication state in zookeeper.
35   */
36  @InterfaceAudience.Private
37  public abstract class ReplicationStateZKBase {
38  
39    /**
40     * The name of the znode that contains the replication status of a remote slave (i.e. peer)
41     * cluster.
42     */
43    protected final String peerStateNodeName;
44    /** The name of the base znode that contains all replication state. */
45    protected final String replicationZNode;
46    /** The name of the znode that contains a list of all remote slave (i.e. peer) clusters. */
47    protected final String peersZNode;
48    /** The name of the znode that contains all replication queues */
49    protected final String queuesZNode;
50    /** The cluster key of the local cluster */
51    protected final String ourClusterKey;
52    protected final ZooKeeperWatcher zookeeper;
53    protected final Configuration conf;
54    protected final Abortable abortable;
55  
56    // Public for testing
57    public static final byte[] ENABLED_ZNODE_BYTES =
58        toByteArray(ZooKeeperProtos.ReplicationState.State.ENABLED);
59    public static final byte[] DISABLED_ZNODE_BYTES =
60        toByteArray(ZooKeeperProtos.ReplicationState.State.DISABLED);
61  
62    public ReplicationStateZKBase(ZooKeeperWatcher zookeeper, Configuration conf,
63        Abortable abortable) {
64      this.zookeeper = zookeeper;
65      this.conf = conf;
66      this.abortable = abortable;
67  
68      String replicationZNodeName = conf.get("zookeeper.znode.replication", "replication");
69      String peersZNodeName = conf.get("zookeeper.znode.replication.peers", "peers");
70      String queuesZNodeName = conf.get("zookeeper.znode.replication.rs", "rs");
71      this.peerStateNodeName = conf.get("zookeeper.znode.replication.peers.state", "peer-state");
72      this.ourClusterKey = ZKUtil.getZooKeeperClusterKey(this.conf);
73      this.replicationZNode = ZKUtil.joinZNode(this.zookeeper.baseZNode, replicationZNodeName);
74      this.peersZNode = ZKUtil.joinZNode(replicationZNode, peersZNodeName);
75      this.queuesZNode = ZKUtil.joinZNode(replicationZNode, queuesZNodeName);
76    }
77  
78    public List<String> getListOfReplicators() {
79      List<String> result = null;
80      try {
81        result = ZKUtil.listChildrenNoWatch(this.zookeeper, this.queuesZNode);
82      } catch (KeeperException e) {
83        this.abortable.abort("Failed to get list of replicators", e);
84      }
85      return result;
86    }
87  
88    /**
89     * @param state
90     * @return Serialized protobuf of <code>state</code> with pb magic prefix prepended suitable for
91     *         use as content of a peer-state znode under a peer cluster id as in
92     *         /hbase/replication/peers/PEER_ID/peer-state.
93     */
94    protected static byte[] toByteArray(final ZooKeeperProtos.ReplicationState.State state) {
95      byte[] bytes =
96          ZooKeeperProtos.ReplicationState.newBuilder().setState(state).build().toByteArray();
97      return ProtobufUtil.prependPBMagic(bytes);
98    }
99  
100   protected boolean peerExists(String id) throws KeeperException {
101     return ZKUtil.checkExists(this.zookeeper, ZKUtil.joinZNode(this.peersZNode, id)) >= 0;
102   }
103 
104   /**
105    * Determine if a ZK path points to a peer node.
106    * @param path path to be checked
107    * @return true if the path points to a peer node, otherwise false
108    */
109   protected boolean isPeerPath(String path) {
110     return path.split("/").length == peersZNode.split("/").length + 1;
111   }
112 }