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.io.Closeable;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.concurrent.atomic.AtomicBoolean;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.classification.InterfaceAudience;
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.hbase.Abortable;
33  import org.apache.hadoop.hbase.ServerName;
34  import org.apache.hadoop.hbase.exceptions.DeserializationException;
35  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
36  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
37  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
38  import org.apache.hadoop.hbase.zookeeper.ZooKeeperNodeTracker;
39  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
40  import org.apache.zookeeper.KeeperException;
41  import org.apache.zookeeper.KeeperException.NodeExistsException;
42  
43  import com.google.protobuf.InvalidProtocolBufferException;
44  
45  /**
46   * This class acts as a wrapper for all the objects used to identify and
47   * communicate with remote peers and is responsible for answering to expired
48   * sessions and re-establishing the ZK connections.
49   */
50  @InterfaceAudience.Private
51  public class ReplicationPeer implements Abortable, Closeable {
52    private static final Log LOG = LogFactory.getLog(ReplicationPeer.class);
53  
54    private final String clusterKey;
55    private final String id;
56    private List<ServerName> regionServers = new ArrayList<ServerName>(0);
57    private final AtomicBoolean peerEnabled = new AtomicBoolean();
58    // Cannot be final since a new object needs to be recreated when session fails
59    private ZooKeeperWatcher zkw;
60    private final Configuration conf;
61    private long lastRegionserverUpdate;
62  
63    private PeerStateTracker peerStateTracker;
64  
65  
66    /**
67     * Constructor that takes all the objects required to communicate with the
68     * specified peer, except for the region server addresses.
69     * @param conf configuration object to this peer
70     * @param key cluster key used to locate the peer
71     * @param id string representation of this peer's identifier
72     */
73    public ReplicationPeer(Configuration conf, String key,
74        String id) throws IOException {
75      this.conf = conf;
76      this.clusterKey = key;
77      this.id = id;
78      this.reloadZkWatcher();
79    }
80  
81    /**
82     * start a state tracker to check whether this peer is enabled or not
83     *
84     * @param zookeeper zk watcher for the local cluster
85     * @param peerStateNode path to zk node which stores peer state
86     * @throws KeeperException
87     */
88    public void startStateTracker(ZooKeeperWatcher zookeeper, String peerStateNode)
89        throws KeeperException {
90      ensurePeerEnabled(zookeeper, peerStateNode);
91      this.peerStateTracker = new PeerStateTracker(peerStateNode, zookeeper, this);
92      this.peerStateTracker.start();
93      try {
94        this.readPeerStateZnode();
95      } catch (DeserializationException e) {
96        throw ZKUtil.convert(e);
97      }
98    }
99  
100   private void readPeerStateZnode() throws DeserializationException {
101     this.peerEnabled.set(isStateEnabled(this.peerStateTracker.getData(false)));
102   }
103 
104   /**
105    * Get the cluster key of that peer
106    * @return string consisting of zk ensemble addresses, client port
107    * and root znode
108    */
109   public String getClusterKey() {
110     return clusterKey;
111   }
112 
113   /**
114    * Get the state of this peer
115    * @return atomic boolean that holds the status
116    */
117   public AtomicBoolean getPeerEnabled() {
118     return peerEnabled;
119   }
120 
121   /**
122    * Get a list of all the addresses of all the region servers
123    * for this peer cluster
124    * @return list of addresses
125    */
126   public List<ServerName> getRegionServers() {
127     return regionServers;
128   }
129 
130   /**
131    * Set the list of region servers for that peer
132    * @param regionServers list of addresses for the region servers
133    */
134   public void setRegionServers(List<ServerName> regionServers) {
135     this.regionServers = regionServers;
136     lastRegionserverUpdate = System.currentTimeMillis();
137   }
138 
139   /**
140    * Get the ZK connection to this peer
141    * @return zk connection
142    */
143   public ZooKeeperWatcher getZkw() {
144     return zkw;
145   }
146 
147   /**
148    * Get the timestamp at which the last change occurred to the list of region servers to replicate
149    * to.
150    * @return The System.currentTimeMillis at the last time the list of peer region servers changed.
151    */
152   public long getLastRegionserverUpdate() {
153     return lastRegionserverUpdate;
154   }
155 
156   /**
157    * Get the identifier of this peer
158    * @return string representation of the id (short)
159    */
160   public String getId() {
161     return id;
162   }
163 
164   /**
165    * Get the configuration object required to communicate with this peer
166    * @return configuration object
167    */
168   public Configuration getConfiguration() {
169     return conf;
170   }
171 
172   @Override
173   public void abort(String why, Throwable e) {
174     LOG.fatal("The ReplicationPeer coresponding to peer " + clusterKey
175         + " was aborted for the following reason(s):" + why, e);
176   }
177 
178   /**
179    * Closes the current ZKW (if not null) and creates a new one
180    * @throws IOException If anything goes wrong connecting
181    */
182   public void reloadZkWatcher() throws IOException {
183     if (zkw != null) zkw.close();
184     zkw = new ZooKeeperWatcher(conf,
185         "connection to cluster: " + id, this);
186   }
187 
188   @Override
189   public boolean isAborted() {
190     // Currently the replication peer is never "Aborted", we just log when the
191     // abort method is called.
192     return false;
193   }
194 
195   @Override
196   public void close() throws IOException {
197     if (zkw != null){
198       zkw.close();
199     }
200   }
201 
202   /**
203    * Parse the raw data from ZK to get a peer's state
204    * @param bytes raw ZK data
205    * @return True if the passed in <code>bytes</code> are those of a pb serialized ENABLED state.
206    * @throws DeserializationException
207    */
208   public static boolean isStateEnabled(final byte[] bytes) throws DeserializationException {
209     ZooKeeperProtos.ReplicationState.State state = parseStateFrom(bytes);
210     return ZooKeeperProtos.ReplicationState.State.ENABLED == state;
211   }
212 
213   /**
214    * @param bytes Content of a state znode.
215    * @return State parsed from the passed bytes.
216    * @throws DeserializationException
217    */
218   private static ZooKeeperProtos.ReplicationState.State parseStateFrom(final byte[] bytes)
219       throws DeserializationException {
220     ProtobufUtil.expectPBMagicPrefix(bytes);
221     int pblen = ProtobufUtil.lengthOfPBMagic();
222     ZooKeeperProtos.ReplicationState.Builder builder =
223         ZooKeeperProtos.ReplicationState.newBuilder();
224     ZooKeeperProtos.ReplicationState state;
225     try {
226       state = builder.mergeFrom(bytes, pblen, bytes.length - pblen).build();
227       return state.getState();
228     } catch (InvalidProtocolBufferException e) {
229       throw new DeserializationException(e);
230     }
231   }
232 
233   /**
234    * Utility method to ensure an ENABLED znode is in place; if not present, we create it.
235    * @param zookeeper
236    * @param path Path to znode to check
237    * @return True if we created the znode.
238    * @throws NodeExistsException
239    * @throws KeeperException
240    */
241   private static boolean ensurePeerEnabled(final ZooKeeperWatcher zookeeper, final String path)
242       throws NodeExistsException, KeeperException {
243     if (ZKUtil.checkExists(zookeeper, path) == -1) {
244       // There is a race b/w PeerWatcher and ReplicationZookeeper#add method to create the
245       // peer-state znode. This happens while adding a peer.
246       // The peer state data is set as "ENABLED" by default.
247       ZKUtil.createNodeIfNotExistsAndWatch(zookeeper, path,
248         ReplicationStateZKBase.ENABLED_ZNODE_BYTES);
249       return true;
250     }
251     return false;
252   }
253 
254   /**
255    * Tracker for state of this peer
256    */
257   public class PeerStateTracker extends ZooKeeperNodeTracker {
258 
259     public PeerStateTracker(String peerStateZNode, ZooKeeperWatcher watcher,
260         Abortable abortable) {
261       super(watcher, peerStateZNode, abortable);
262     }
263 
264     @Override
265     public synchronized void nodeDataChanged(String path) {
266       if (path.equals(node)) {
267         super.nodeDataChanged(path);
268         try {
269           readPeerStateZnode();
270         } catch (DeserializationException e) {
271           LOG.warn("Failed deserializing the content of " + path, e);
272         }
273       }
274     }
275   }
276 }