View Javadoc

1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.replication;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.concurrent.atomic.AtomicBoolean;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.Abortable;
31  import org.apache.hadoop.hbase.ServerName;
32  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
33  
34  /**
35   * This class acts as a wrapper for all the objects used to identify and
36   * communicate with remote peers and is responsible for answering to expired
37   * sessions and re-establishing the ZK connections.
38   */
39  public class ReplicationPeer implements Abortable {
40    private static final Log LOG = LogFactory.getLog(ReplicationPeer.class);
41  
42    private final String clusterKey;
43    private final String id;
44    private List<ServerName> regionServers = new ArrayList<ServerName>(0);
45    private final AtomicBoolean peerEnabled = new AtomicBoolean();
46    // Cannot be final since a new object needs to be recreated when session fails
47    private ZooKeeperWatcher zkw;
48    private final Configuration conf;
49  
50    /**
51     * Constructor that takes all the objects required to communicate with the
52     * specified peer, except for the region server addresses.
53     * @param conf configuration object to this peer
54     * @param key cluster key used to locate the peer
55     * @param id string representation of this peer's identifier
56     */
57    public ReplicationPeer(Configuration conf, String key,
58        String id) throws IOException {
59      this.conf = conf;
60      this.clusterKey = key;
61      this.id = id;
62      this.reloadZkWatcher();
63    }
64  
65    /**
66     * Get the cluster key of that peer
67     * @return string consisting of zk ensemble addresses, client port
68     * and root znode
69     */
70    public String getClusterKey() {
71      return clusterKey;
72    }
73  
74    /**
75     * Get the state of this peer
76     * @return atomic boolean that holds the status
77     */
78    public AtomicBoolean getPeerEnabled() {
79      return peerEnabled;
80    }
81  
82    /**
83     * Get a list of all the addresses of all the region servers
84     * for this peer cluster
85     * @return list of addresses
86     */
87    public List<ServerName> getRegionServers() {
88      return regionServers;
89    }
90  
91    /**
92     * Set the list of region servers for that peer
93     * @param regionServers list of addresses for the region servers
94     */
95    public void setRegionServers(List<ServerName> regionServers) {
96      this.regionServers = regionServers;
97    }
98  
99    /**
100    * Get the ZK connection to this peer
101    * @return zk connection
102    */
103   public ZooKeeperWatcher getZkw() {
104     return zkw;
105   }
106 
107   /**
108    * Get the identifier of this peer
109    * @return string representation of the id (short)
110    */
111   public String getId() {
112     return id;
113   }
114 
115   /**
116    * Get the configuration object required to communicate with this peer
117    * @return configuration object
118    */
119   public Configuration getConfiguration() {
120     return conf;
121   }
122 
123   @Override
124   public void abort(String why, Throwable e) {
125     LOG.warn("The ReplicationPeer coresponding to peer " + clusterKey
126         + " was aborted for the following reason(s):" + why, e);
127   }
128 
129   /**
130    * Closes the current ZKW (if not null) and creates a new one
131    * @throws IOException If anything goes wrong connecting
132    */
133   public void reloadZkWatcher() throws IOException {
134     if (zkw != null) zkw.close();
135     zkw = new ZooKeeperWatcher(conf,
136         "connection to cluster: " + id, this);    
137   }
138 
139   @Override
140   public boolean isAborted() {
141     // Currently the replication peer is never "Aborted", we just log when the
142     // abort method is called.
143     return false;
144   }
145 }