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 com.google.protobuf.ByteString;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.classification.InterfaceStability;
26  import org.apache.hadoop.hbase.exceptions.DeserializationException;
27  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
28  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
29  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
30  import org.apache.hadoop.hbase.util.Bytes;
31  
32  import java.io.IOException;
33  import java.util.Map;
34  
35  @InterfaceAudience.Private
36  @InterfaceStability.Stable
37  public final class ReplicationSerDeHelper {
38    private static final Log LOG = LogFactory.getLog(ReplicationSerDeHelper.class);
39  
40    private ReplicationSerDeHelper() {}
41  
42    /**
43     * @param bytes Content of a peer znode.
44     * @return ClusterKey parsed from the passed bytes.
45     * @throws DeserializationException
46     */
47    public static ReplicationPeerConfig parsePeerFrom(final byte[] bytes)
48        throws DeserializationException {
49      if (ProtobufUtil.isPBMagicPrefix(bytes)) {
50        int pblen = ProtobufUtil.lengthOfPBMagic();
51        ZooKeeperProtos.ReplicationPeer.Builder builder =
52            ZooKeeperProtos.ReplicationPeer.newBuilder();
53        ZooKeeperProtos.ReplicationPeer peer;
54        try {
55          ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen);
56          peer = builder.build();
57        } catch (IOException e) {
58          throw new DeserializationException(e);
59        }
60        return convert(peer);
61      } else {
62        if (bytes.length > 0) {
63          return new ReplicationPeerConfig().setClusterKey(Bytes.toString(bytes));
64        }
65        return new ReplicationPeerConfig().setClusterKey("");
66      }
67    }
68  
69    private static ReplicationPeerConfig convert(ZooKeeperProtos.ReplicationPeer peer) {
70      ReplicationPeerConfig peerConfig = new ReplicationPeerConfig();
71      if (peer.hasClusterkey()) {
72        peerConfig.setClusterKey(peer.getClusterkey());
73      }
74      if (peer.hasReplicationEndpointImpl()) {
75        peerConfig.setReplicationEndpointImpl(peer.getReplicationEndpointImpl());
76      }
77  
78      for (HBaseProtos.BytesBytesPair pair : peer.getDataList()) {
79        peerConfig.getPeerData().put(pair.getFirst().toByteArray(), pair.getSecond().toByteArray());
80      }
81  
82      for (HBaseProtos.NameStringPair pair : peer.getConfigurationList()) {
83        peerConfig.getConfiguration().put(pair.getName(), pair.getValue());
84      }
85      return peerConfig;
86    }
87  
88    /**
89     * @param peerConfig
90     * @return Serialized protobuf of <code>peerConfig</code> with pb magic prefix prepended suitable
91     *         for use as content of a this.peersZNode; i.e. the content of PEER_ID znode under
92     *         /hbase/replication/peers/PEER_ID
93     */
94    public static byte[] toByteArray(final ReplicationPeerConfig peerConfig) {
95      byte[] bytes = convert(peerConfig).toByteArray();
96      return ProtobufUtil.prependPBMagic(bytes);
97    }
98  
99    private static ZooKeeperProtos.ReplicationPeer convert(ReplicationPeerConfig  peerConfig) {
100     ZooKeeperProtos.ReplicationPeer.Builder builder = ZooKeeperProtos.ReplicationPeer.newBuilder();
101     if (peerConfig.getClusterKey() != null) {
102       builder.setClusterkey(peerConfig.getClusterKey());
103     }
104     if (peerConfig.getReplicationEndpointImpl() != null) {
105       builder.setReplicationEndpointImpl(peerConfig.getReplicationEndpointImpl());
106     }
107 
108     for (Map.Entry<byte[], byte[]> entry : peerConfig.getPeerData().entrySet()) {
109       builder.addData(HBaseProtos.BytesBytesPair.newBuilder()
110           .setFirst(ByteString.copyFrom(entry.getKey()))
111           .setSecond(ByteString.copyFrom(entry.getValue()))
112           .build());
113     }
114 
115     for (Map.Entry<String, String> entry : peerConfig.getConfiguration().entrySet()) {
116       builder.addConfiguration(HBaseProtos.NameStringPair.newBuilder()
117           .setName(entry.getKey())
118           .setValue(entry.getValue())
119           .build());
120     }
121 
122     return builder.build();
123   }
124 }