001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.replication;
019
020import java.io.ByteArrayOutputStream;
021import java.io.IOException;
022
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
025import org.apache.hadoop.hbase.zookeeper.ZNodePaths;
026import org.apache.yetus.audience.InterfaceAudience;
027
028import org.apache.hbase.thirdparty.com.google.protobuf.CodedOutputStream;
029
030import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
031import org.apache.hadoop.hbase.shaded.protobuf.generated.ReplicationProtos;
032
033/**
034 * This is a base class for maintaining replication related data,for example, peer, queue, etc, in
035 * zookeeper.
036 */
037@InterfaceAudience.Private
038public class ZKReplicationStorageBase {
039
040  public static final String REPLICATION_ZNODE = "zookeeper.znode.replication";
041  public static final String REPLICATION_ZNODE_DEFAULT = "replication";
042
043  /** The name of the base znode that contains all replication state. */
044  protected final String replicationZNode;
045
046  protected final ZKWatcher zookeeper;
047  protected final Configuration conf;
048
049  protected ZKReplicationStorageBase(ZKWatcher zookeeper, Configuration conf) {
050    this.zookeeper = zookeeper;
051    this.conf = conf;
052
053    this.replicationZNode = ZNodePaths.joinZNode(this.zookeeper.getZNodePaths().baseZNode,
054      conf.get(REPLICATION_ZNODE, REPLICATION_ZNODE_DEFAULT));
055  }
056
057  /**
058   * Serialized protobuf of <code>state</code> with pb magic prefix prepended suitable for use as
059   * content of a peer-state znode under a peer cluster id as in
060   * /hbase/replication/peers/PEER_ID/peer-state.
061   */
062  protected static byte[] toByteArray(final ReplicationProtos.ReplicationState.State state) {
063    ReplicationProtos.ReplicationState msg =
064        ReplicationProtos.ReplicationState.newBuilder().setState(state).build();
065    // There is no toByteArray on this pb Message?
066    // 32 bytes is default which seems fair enough here.
067    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
068      CodedOutputStream cos = CodedOutputStream.newInstance(baos, 16);
069      msg.writeTo(cos);
070      cos.flush();
071      baos.flush();
072      return ProtobufUtil.prependPBMagic(baos.toByteArray());
073    } catch (IOException e) {
074      throw new RuntimeException(e);
075    }
076  }
077}