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;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
024import org.apache.hadoop.hbase.zookeeper.ZNodePaths;
025import org.apache.yetus.audience.InterfaceAudience;
026
027import org.apache.hbase.thirdparty.com.google.protobuf.CodedOutputStream;
028
029import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
030import org.apache.hadoop.hbase.shaded.protobuf.generated.ReplicationProtos;
031
032/**
033 * This is a base class for maintaining replication related data,for example, peer, queue, etc, in
034 * zookeeper.
035 */
036@InterfaceAudience.Private
037public abstract class ZKReplicationStorageBase {
038
039  public static final String REPLICATION_ZNODE = "zookeeper.znode.replication";
040  public static final String REPLICATION_ZNODE_DEFAULT = "replication";
041
042  /** The name of the base znode that contains all replication state. */
043  protected final String replicationZNode;
044
045  protected final ZKWatcher zookeeper;
046  protected final Configuration conf;
047
048  protected ZKReplicationStorageBase(ZKWatcher zookeeper, Configuration conf) {
049    this.zookeeper = zookeeper;
050    this.conf = conf;
051
052    this.replicationZNode = ZNodePaths.joinZNode(this.zookeeper.getZNodePaths().baseZNode,
053      conf.get(REPLICATION_ZNODE, REPLICATION_ZNODE_DEFAULT));
054  }
055
056  /**
057   * Serialized protobuf of <code>state</code> with pb magic prefix prepended suitable for use as
058   * content of a peer-state znode under a peer cluster id as in
059   * /hbase/replication/peers/PEER_ID/peer-state.
060   */
061  protected static byte[] toByteArray(final ReplicationProtos.ReplicationState.State state) {
062    ReplicationProtos.ReplicationState msg =
063      ReplicationProtos.ReplicationState.newBuilder().setState(state).build();
064    // There is no toByteArray on this pb Message?
065    // 32 bytes is default which seems fair enough here.
066    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
067      CodedOutputStream cos = CodedOutputStream.newInstance(baos, 16);
068      msg.writeTo(cos);
069      cos.flush();
070      baos.flush();
071      return ProtobufUtil.prependPBMagic(baos.toByteArray());
072    } catch (IOException e) {
073      throw new RuntimeException(e);
074    }
075  }
076}