All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.hadoop.hbase.replication.ReplicationStateZKBase Maven / Gradle / Ivy

There is a newer version: 3.0.0-beta-1
Show newest version
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.hadoop.hbase.replication;

import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Abortable;
import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
import org.apache.hadoop.hbase.zookeeper.ZKUtil;
import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
import org.apache.zookeeper.KeeperException;


/**
 * This is a base class for maintaining replication state in zookeeper.
 */
public abstract class ReplicationStateZKBase {

  /**
   * The name of the znode that contains the replication status of a remote slave (i.e. peer)
   * cluster.
   */
  protected final String peerStateNodeName;
  /** The name of the znode that contains the replication status of the local cluster. */
  protected final String stateZNode;
  /** The name of the base znode that contains all replication state. */
  protected final String replicationZNode;
  /** The name of the znode that contains a list of all remote slave (i.e. peer) clusters. */
  protected final String peersZNode;
  /** The name of the znode that contains all replication queues */
  protected final String queuesZNode;
  /** The cluster key of the local cluster */
  protected final String ourClusterKey;
  protected final ZooKeeperWatcher zookeeper;
  protected final Configuration conf;
  protected final Abortable abortable;

  // Public for testing
  public static final byte[] ENABLED_ZNODE_BYTES =
      toByteArray(ZooKeeperProtos.ReplicationState.State.ENABLED);
  protected static final byte[] DISABLED_ZNODE_BYTES =
      toByteArray(ZooKeeperProtos.ReplicationState.State.DISABLED);

  public ReplicationStateZKBase(ZooKeeperWatcher zookeeper, Configuration conf,
      Abortable abortable) {
    this.zookeeper = zookeeper;
    this.conf = conf;
    this.abortable = abortable;

    String replicationZNodeName = conf.get("zookeeper.znode.replication", "replication");
    String peersZNodeName = conf.get("zookeeper.znode.replication.peers", "peers");
    String queuesZNodeName = conf.get("zookeeper.znode.replication.rs", "rs");
    String stateZNodeName = conf.get("zookeeper.znode.replication.state", "state");
    this.peerStateNodeName = conf.get("zookeeper.znode.replication.peers.state", "peer-state");
    this.ourClusterKey = ZKUtil.getZooKeeperClusterKey(this.conf);
    this.replicationZNode = ZKUtil.joinZNode(this.zookeeper.baseZNode, replicationZNodeName);
    this.stateZNode = ZKUtil.joinZNode(replicationZNode, stateZNodeName);
    this.peersZNode = ZKUtil.joinZNode(replicationZNode, peersZNodeName);
    this.queuesZNode = ZKUtil.joinZNode(replicationZNode, queuesZNodeName);
  }

  public List getListOfReplicators() {
    List result = null;
    try {
      result = ZKUtil.listChildrenNoWatch(this.zookeeper, this.queuesZNode);
    } catch (KeeperException e) {
      this.abortable.abort("Failed to get list of replicators", e);
    }
    return result;
  }

  /**
   * @param state
   * @return Serialized protobuf of state with pb magic prefix prepended suitable for
   *         use as content of either the cluster state znode -- whether or not we should be
   *         replicating kept in /hbase/replication/state -- or as content of a peer-state znode
   *         under a peer cluster id as in /hbase/replication/peers/PEER_ID/peer-state.
   */
  protected static byte[] toByteArray(final ZooKeeperProtos.ReplicationState.State state) {
    byte[] bytes =
        ZooKeeperProtos.ReplicationState.newBuilder().setState(state).build().toByteArray();
    return ProtobufUtil.prependPBMagic(bytes);
  }

  public boolean peerExists(String id) throws KeeperException {
    return ZKUtil.checkExists(this.zookeeper, ZKUtil.joinZNode(this.peersZNode, id)) >= 0;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy