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