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.backup.master;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.fs.FileStatus;
29  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
30  import org.apache.hadoop.hbase.HConstants;
31  import org.apache.hadoop.hbase.backup.impl.BackupManager;
32  import org.apache.hadoop.hbase.backup.impl.BackupSystemTable;
33  import org.apache.hadoop.hbase.classification.InterfaceAudience;
34  import org.apache.hadoop.hbase.classification.InterfaceStability;
35  import org.apache.hadoop.hbase.client.Connection;
36  import org.apache.hadoop.hbase.client.ConnectionFactory;
37  import org.apache.hadoop.hbase.master.cleaner.BaseLogCleanerDelegate;
38  
39  /**
40   * Implementation of a log cleaner that checks if a log is still scheduled for
41   * incremental backup before deleting it when its TTL is over.
42   */
43  @InterfaceStability.Evolving
44  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
45  public class BackupLogCleaner extends BaseLogCleanerDelegate {
46    private static final Log LOG = LogFactory.getLog(BackupLogCleaner.class);
47  
48    private boolean stopped = false;
49  
50    public BackupLogCleaner() {
51    }
52  
53    @Override
54    public Iterable<FileStatus> getDeletableFiles(Iterable<FileStatus> files) {
55      // all members of this class are null if backup is disabled,
56      // so we cannot filter the files
57      if (this.getConf() == null|| !BackupManager.isBackupEnabled(getConf())) {
58        if (LOG.isDebugEnabled()) {
59          LOG.debug("Backup is not enabled. Check your "
60              + HConstants.BACKUP_ENABLE_KEY + " setting");
61        }
62        return files;
63      }
64      
65      List<FileStatus> list = new ArrayList<FileStatus>();
66      // TODO: LogCleaners do not have a way to get the Connection from Master. We should find a
67      // way to pass it down here, so that this connection is not re-created every time.
68      // It is expensive
69      try (final Connection conn = ConnectionFactory.createConnection(getConf());
70          final BackupSystemTable table = new BackupSystemTable(conn)) {
71        // If we do not have recorded backup sessions
72        if (!table.hasBackupSessions()) {
73          LOG.debug("BackupLogCleaner has no backup sessions");
74          return files;
75        }
76        
77        for(FileStatus file: files){
78          String wal = file.getPath().toString();
79          boolean logInSystemTable = table.isWALFileDeletable(wal);
80          if(LOG.isDebugEnabled()) {
81            if(logInSystemTable) {
82              LOG.debug("Found log file in hbase:backup, deleting: " + wal);
83              list.add(file);
84            } else {
85              LOG.debug("Didn't find this log in hbase:backup, keeping: " + wal);
86            }
87          }
88        }
89        return list;  
90      } catch (IOException e) {
91        LOG.error("Failed to get hbase:backup table, therefore will keep all files", e);
92        // nothing to delete
93        return new ArrayList<FileStatus>();
94      }
95    }
96  
97    @Override
98    public void setConf(Configuration config) {
99      super.setConf(config);
100     // If backup is disabled, keep all members null
101     if (!config.getBoolean(HConstants.BACKUP_ENABLE_KEY, HConstants.BACKUP_ENABLE_DEFAULT)) {
102       LOG.warn("Backup is disabled - allowing all wals to be deleted");
103       return;
104     }
105   }
106 
107   @Override
108   public void stop(String why) {
109     if (this.stopped) {
110       return;
111     }
112     this.stopped = true;
113     LOG.info("Stopping BackupLogCleaner");
114   }
115 
116   @Override
117   public boolean isStopped() {
118     return this.stopped;
119   }
120 
121 }