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 com.google.common.annotations.VisibleForTesting;
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.conf.Configuration;
26  import org.apache.hadoop.fs.FileStatus;
27  import org.apache.hadoop.hbase.Abortable;
28  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.master.cleaner.BaseLogCleanerDelegate;
31  import org.apache.hadoop.hbase.replication.ReplicationException;
32  import org.apache.hadoop.hbase.replication.ReplicationFactory;
33  import org.apache.hadoop.hbase.replication.ReplicationQueuesClient;
34  import org.apache.hadoop.hbase.replication.ReplicationQueuesClientArguments;
35  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
36  import java.io.IOException;
37  import java.util.Collections;
38  import java.util.List;
39  import java.util.Set;
40
41  import com.google.common.base.Predicate;
42  import com.google.common.collect.ImmutableSet;
43  import com.google.common.collect.Iterables;
44  import com.google.common.collect.Sets;
45  import org.apache.zookeeper.KeeperException;
46
47  /**
48   * Implementation of a log cleaner that checks if a log is still scheduled for
49   * replication before deleting it when its TTL is over.
50   */
51  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
52  public class ReplicationLogCleaner extends BaseLogCleanerDelegate {
53    private static final Log LOG = LogFactory.getLog(ReplicationLogCleaner.class);
54    private ZooKeeperWatcher zkw;
55    private ReplicationQueuesClient replicationQueues;
56    private boolean stopped = false;
57  
58
59    @Override
60    public Iterable<FileStatus> getDeletableFiles(Iterable<FileStatus> files) {
61     // all members of this class are null if replication is disabled,
62     // so we cannot filter the files
63      if (this.getConf() == null) {
64        return files;
65      }
66
67      final Set<String> wals;
68      try {
69        // The concurrently created new WALs may not be included in the return list,
70        // but they won't be deleted because they're not in the checking set.
71        wals = replicationQueues.getAllWALs();
72      } catch (KeeperException e) {
73        LOG.warn("Failed to read zookeeper, skipping checking deletable files");
74        return Collections.emptyList();
75      }
76      return Iterables.filter(files, new Predicate<FileStatus>() {
77        @Override
78        public boolean apply(FileStatus file) {
79          String wal = file.getPath().getName();
80          boolean logInReplicationQueue = wals.contains(wal);
81          if (LOG.isDebugEnabled()) {
82            if (logInReplicationQueue) {
83              LOG.debug("Found log in ZK, keeping: " + wal);
84            } else {
85              LOG.debug("Didn't find this log in ZK, deleting: " + wal);
86            }
87          }
88         return !logInReplicationQueue;
89        }});
90    }
91
92    @Override
93    public void setConf(Configuration config) {
94      // Make my own Configuration.  Then I'll have my own connection to zk that
95      // I can close myself when comes time.
96      Configuration conf = new Configuration(config);
97      try {
98        setConf(conf, new ZooKeeperWatcher(conf, "replicationLogCleaner", null));
99      } catch (IOException e) {
100       LOG.error("Error while configuring " + this.getClass().getName(), e);
101     }
102   }
103
104   @VisibleForTesting
105   public void setConf(Configuration conf, ZooKeeperWatcher zk) {
106     super.setConf(conf);
107     try {
108       this.zkw = zk;
109       this.replicationQueues = ReplicationFactory.getReplicationQueuesClient(
110           new ReplicationQueuesClientArguments(conf, new WarnOnlyAbortable(), zkw));
111       this.replicationQueues.init();
112     } catch (Exception e) {
113       LOG.error("Error while configuring " + this.getClass().getName(), e);
114     }
115   }
116
117   @Override
118   public void stop(String why) {
119     if (this.stopped) return;
120     this.stopped = true;
121     if (this.zkw != null) {
122       LOG.info("Stopping " + this.zkw);
123       this.zkw.close();
124     }
125   }
126
127   @Override
128   public boolean isStopped() {
129     return this.stopped;
130   }
131
132   private static class WarnOnlyAbortable implements Abortable {
133
134     @Override
135     public void abort(String why, Throwable e) {
136       LOG.warn("ReplicationLogCleaner received abort, ignoring.  Reason: " + why);
137       if (LOG.isDebugEnabled()) {
138         LOG.debug(e);
139       }
140     }
141
142     @Override
143     public boolean isAborted() {
144       return false;
145     }
146   }
147 }