View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.master;
20  
21  import java.io.IOException;
22  import java.util.Map;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.ScheduledChore;
28  import org.apache.hadoop.hbase.HColumnDescriptor;
29  import org.apache.hadoop.hbase.HTableDescriptor;
30  import org.apache.hadoop.hbase.TableDescriptors;
31  import org.apache.hadoop.hbase.exceptions.LockTimeoutException;
32  import org.apache.hadoop.hbase.master.TableLockManager.TableLock;
33  import org.apache.hadoop.hbase.mob.ExpiredMobFileCleaner;
34  import org.apache.hadoop.hbase.mob.MobConstants;
35  import org.apache.hadoop.hbase.mob.MobUtils;
36  
37  /**
38   * The Class ExpiredMobFileCleanerChore for running cleaner regularly to remove the expired
39   * mob files.
40   */
41  @InterfaceAudience.Private
42  public class ExpiredMobFileCleanerChore extends ScheduledChore {
43  
44    private static final Log LOG = LogFactory.getLog(ExpiredMobFileCleanerChore.class);
45    private final HMaster master;
46    private TableLockManager tableLockManager;
47    private ExpiredMobFileCleaner cleaner;
48  
49    public ExpiredMobFileCleanerChore(HMaster master) {
50      super(master.getServerName() + "-ExpiredMobFileCleanerChore", master,
51        master.getConfiguration().getInt(MobConstants.MOB_CLEANER_PERIOD,
52          MobConstants.DEFAULT_MOB_CLEANER_PERIOD));
53  
54      this.master = master;
55      this.tableLockManager = master.getTableLockManager();
56      cleaner = new ExpiredMobFileCleaner();
57      cleaner.setConf(master.getConfiguration());
58    }
59  
60    @Override
61    protected void chore() {
62      try {
63        TableDescriptors htds = master.getTableDescriptors();
64        Map<String, HTableDescriptor> map = htds.getAll();
65        for (HTableDescriptor htd : map.values()) {
66          for (HColumnDescriptor hcd : htd.getColumnFamilies()) {
67            if (hcd.isMobEnabled() && hcd.getMinVersions() == 0) {
68              // clean only for mob-enabled column.
69              // obtain a read table lock before cleaning, synchronize with MobFileCompactionChore.
70              boolean tableLocked = false;
71              TableLock lock = null;
72              try {
73                // the tableLockManager might be null in testing. In that case, it is lock-free.
74                if (tableLockManager != null) {
75                  lock = tableLockManager.readLock(MobUtils.getTableLockName(htd.getTableName()),
76                    "Run ExpiredMobFileCleanerChore");
77                  lock.acquire();
78                }
79                tableLocked = true;
80                cleaner.cleanExpiredMobFiles(htd.getTableName().getNameAsString(), hcd);
81              } catch (LockTimeoutException e) {
82                LOG.info("Fail to acquire the lock because of timeout, maybe a"
83                  + " MobFileCompactor is running", e);
84              } catch (Exception e) {
85                LOG.error(
86                  "Fail to clean the expired mob files for the column " + hcd.getNameAsString()
87                    + " in the table " + htd.getNameAsString(), e);
88              } finally {
89                if (lock != null && tableLocked) {
90                  try {
91                    lock.release();
92                  } catch (IOException e) {
93                    LOG.error(
94                      "Fail to release the write lock for the table " + htd.getNameAsString(), e);
95                  }
96                }
97              }
98            }
99          }
100       }
101     } catch (Exception e) {
102       LOG.error("Fail to clean the expired mob files", e);
103     }
104   }
105 
106 }