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.master;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.fs.Path;
26  import org.apache.hadoop.hbase.master.HMaster;
27  import org.apache.hadoop.hbase.master.LogCleanerDelegate;
28  import org.apache.hadoop.hbase.replication.ReplicationZookeeperWrapper;
29  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWrapper;
30  import org.apache.zookeeper.WatchedEvent;
31  import org.apache.zookeeper.Watcher;
32  
33  import java.io.IOException;
34  import java.util.HashSet;
35  import java.util.List;
36  import java.util.Set;
37  import java.util.concurrent.atomic.AtomicBoolean;
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  public class ReplicationLogCleaner implements LogCleanerDelegate, Watcher {
44  
45    private static final Log LOG =
46      LogFactory.getLog(ReplicationLogCleaner.class);
47    private Configuration conf;
48    private ReplicationZookeeperWrapper zkHelper;
49    private Set<String> hlogs = new HashSet<String>();
50  
51    /**
52     * Instantiates the cleaner, does nothing more.
53     */
54    public ReplicationLogCleaner() {}
55  
56    @Override
57    public boolean isLogDeletable(Path filePath) {
58      String log = filePath.getName();
59      // If we saw the hlog previously, let's consider it's still used
60      // At some point in the future we will refresh the list and it will be gone
61      if (this.hlogs.contains(log)) {
62        return false;
63      }
64  
65      // Let's see it's still there
66      // This solution makes every miss very expensive to process since we
67      // almost completely refresh the cache each time
68      return !refreshHLogsAndSearch(log);
69    }
70  
71    /**
72     * Search through all the hlogs we have in ZK to refresh the cache
73     * If a log is specified and found, then we early out and return true
74     * @param searchedLog log we are searching for, pass null to cache everything
75     *                    that's in zookeeper.
76     * @return false until a specified log is found.
77     */
78    private boolean refreshHLogsAndSearch(String searchedLog) {
79      this.hlogs.clear();
80      final boolean lookForLog = searchedLog != null;
81      List<String> rss = zkHelper.getListOfReplicators(this);
82      if (rss == null) {
83        LOG.debug("Didn't find any region server that replicates, deleting: " +
84            searchedLog);
85        return false;
86      }
87      for (String rs: rss) {
88        List<String> listOfPeers = zkHelper.getListPeersForRS(rs, this);
89        // if rs just died, this will be null
90        if (listOfPeers == null) {
91          continue;
92        }
93        for (String id : listOfPeers) {
94          List<String> peersHlogs = zkHelper.getListHLogsForPeerForRS(rs, id, this);
95          if (peersHlogs != null) {
96            this.hlogs.addAll(peersHlogs);
97          }
98          // early exit if we found the log
99          if(lookForLog && this.hlogs.contains(searchedLog)) {
100           LOG.debug("Found log in ZK, keeping: " + searchedLog);
101           return true;
102         }
103       }
104     }
105     LOG.debug("Didn't find this log in ZK, deleting: " + searchedLog);
106     return false;
107   }
108 
109   @Override
110   public void setConf(Configuration conf) {
111     this.conf = conf;
112     try {
113       this.zkHelper = new ReplicationZookeeperWrapper(
114           ZooKeeperWrapper.createInstance(this.conf,
115               HMaster.class.getName()),
116           this.conf, new AtomicBoolean(true), null);
117     } catch (IOException e) {
118       LOG.error(e);
119     }
120     refreshHLogsAndSearch(null);
121   }
122 
123   @Override
124   public Configuration getConf() {
125     return conf;
126   }
127 
128   @Override
129   public void process(WatchedEvent watchedEvent) {}
130 }