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.master;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.fs.Path;
26  import org.apache.hadoop.hbase.Abortable;
27  import org.apache.hadoop.hbase.HConstants;
28  import org.apache.hadoop.hbase.client.HConnectionManager;
29  import org.apache.hadoop.hbase.master.cleaner.BaseLogCleanerDelegate;
30  import org.apache.hadoop.hbase.replication.ReplicationZookeeper;
31  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
32  import org.apache.zookeeper.KeeperException;
33  
34  import java.io.IOException;
35  import java.util.HashSet;
36  import java.util.List;
37  import java.util.Set;
38  
39  /**
40   * Implementation of a log cleaner that checks if a log is still scheduled for
41   * replication before deleting it when its TTL is over.
42   */
43  @InterfaceAudience.Private
44  public class ReplicationLogCleaner extends BaseLogCleanerDelegate implements Abortable {
45    private static final Log LOG = LogFactory.getLog(ReplicationLogCleaner.class);
46    private ReplicationZookeeper zkHelper;
47    private Set<String> hlogs = new HashSet<String>();
48    private boolean stopped = false;
49    private boolean aborted;
50  
51  
52    @Override
53    public boolean isLogDeletable(Path filePath) {
54  
55      try {
56        if (!zkHelper.getReplication()) {
57          return false;
58        }
59      } catch (KeeperException e) {
60        abort("Cannot get the state of replication", e);
61        return false;
62      }
63  
64      // all members of this class are null if replication is disabled, and we
65      // return true since false would render the LogsCleaner useless
66      if (this.getConf() == null) {
67        return true;
68      }
69      String log = filePath.getName();
70      // If we saw the hlog previously, let's consider it's still used
71      // At some point in the future we will refresh the list and it will be gone
72      if (this.hlogs.contains(log)) {
73        return false;
74      }
75  
76      // Let's see it's still there
77      // This solution makes every miss very expensive to process since we
78      // almost completely refresh the cache each time
79      return !refreshHLogsAndSearch(log);
80    }
81  
82    /**
83     * Search through all the hlogs we have in ZK to refresh the cache
84     * If a log is specified and found, then we early out and return true
85     * @param searchedLog log we are searching for, pass null to cache everything
86     *                    that's in zookeeper.
87     * @return false until a specified log is found.
88     */
89    private boolean refreshHLogsAndSearch(String searchedLog) {
90      this.hlogs.clear();
91      final boolean lookForLog = searchedLog != null;
92      List<String> rss = zkHelper.getListOfReplicators();
93      if (rss == null) {
94        LOG.debug("Didn't find any region server that replicates, deleting: " +
95            searchedLog);
96        return false;
97      }
98      for (String rs: rss) {
99        List<String> listOfPeers = zkHelper.getListPeersForRS(rs);
100       // if rs just died, this will be null
101       if (listOfPeers == null) {
102         continue;
103       }
104       for (String id : listOfPeers) {
105         List<String> peersHlogs = zkHelper.getListHLogsForPeerForRS(rs, id);
106         if (peersHlogs != null) {
107           this.hlogs.addAll(peersHlogs);
108         }
109         // early exit if we found the log
110         if(lookForLog && this.hlogs.contains(searchedLog)) {
111           LOG.debug("Found log in ZK, keeping: " + searchedLog);
112           return true;
113         }
114       }
115     }
116     LOG.debug("Didn't find this log in ZK, deleting: " + searchedLog);
117     return false;
118   }
119 
120   @Override
121   public void setConf(Configuration config) {
122     // If replication is disabled, keep all members null
123     if (!config.getBoolean(HConstants.REPLICATION_ENABLE_KEY, false)) {
124       return;
125     }
126     // Make my own Configuration.  Then I'll have my own connection to zk that
127     // I can close myself when comes time.
128     Configuration conf = new Configuration(config);
129     super.setConf(conf);
130     try {
131       ZooKeeperWatcher zkw = new ZooKeeperWatcher(conf, "replicationLogCleaner", null);
132       this.zkHelper = new ReplicationZookeeper(this, conf, zkw);
133     } catch (KeeperException e) {
134       LOG.error("Error while configuring " + this.getClass().getName(), e);
135     } catch (IOException e) {
136       LOG.error("Error while configuring " + this.getClass().getName(), e);
137     }
138     refreshHLogsAndSearch(null);
139   }
140 
141 
142   @Override
143   public void stop(String why) {
144     if (this.stopped) return;
145     this.stopped = true;
146     if (this.zkHelper != null) {
147       LOG.info("Stopping " + this.zkHelper.getZookeeperWatcher());
148       this.zkHelper.getZookeeperWatcher().close();
149     }
150     // Not sure why we're deleting a connection that we never acquired or used
151     HConnectionManager.deleteConnection(this.getConf());
152   }
153 
154   @Override
155   public boolean isStopped() {
156     return this.stopped;
157   }
158 
159   @Override
160   public void abort(String why, Throwable e) {
161     LOG.warn("Aborting ReplicationLogCleaner because " + why, e);
162     this.aborted = true;
163     stop(why);
164   }
165 
166   @Override
167   public boolean isAborted() {
168     return this.aborted;
169   }
170 }