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  import java.util.concurrent.TimeUnit;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.HColumnDescriptor;
29  import org.apache.hadoop.hbase.HTableDescriptor;
30  import org.apache.hadoop.hbase.ScheduledChore;
31  import org.apache.hadoop.hbase.TableDescriptors;
32  import org.apache.hadoop.hbase.exceptions.LockTimeoutException;
33  import org.apache.hadoop.hbase.master.TableLockManager.TableLock;
34  import org.apache.hadoop.hbase.mob.ExpiredMobFileCleaner;
35  import org.apache.hadoop.hbase.mob.MobConstants;
36  import org.apache.hadoop.hbase.mob.MobUtils;
37  
38  import com.google.protobuf.ServiceException;
39  
40  /**
41   * The Class ExpiredMobFileCleanerChore for running cleaner regularly to remove the expired
42   * mob files.
43   */
44  @InterfaceAudience.Private
45  public class ExpiredMobFileCleanerChore extends ScheduledChore {
46  
47    private static final Log LOG = LogFactory.getLog(ExpiredMobFileCleanerChore.class);
48    private final HMaster master;
49    private TableLockManager tableLockManager;
50    private ExpiredMobFileCleaner cleaner;
51  
52    public ExpiredMobFileCleanerChore(HMaster master) {
53      super(master.getServerName() + "-ExpiredMobFileCleanerChore", master, master.getConfiguration()
54        .getInt(MobConstants.MOB_CLEANER_PERIOD, MobConstants.DEFAULT_MOB_CLEANER_PERIOD), master
55        .getConfiguration().getInt(MobConstants.MOB_CLEANER_PERIOD,
56          MobConstants.DEFAULT_MOB_CLEANER_PERIOD), TimeUnit.SECONDS);
57      this.master = master;
58      this.tableLockManager = master.getTableLockManager();
59      cleaner = new ExpiredMobFileCleaner();
60      cleaner.setConf(master.getConfiguration());
61    }
62  
63    @Override
64    protected void chore() {
65      try {
66        TableDescriptors htds = master.getTableDescriptors();
67        Map<String, HTableDescriptor> map = htds.getAll();
68        for (HTableDescriptor htd : map.values()) {
69          for (HColumnDescriptor hcd : htd.getColumnFamilies()) {
70            if (hcd.isMobEnabled() && hcd.getMinVersions() == 0) {
71              // clean only for mob-enabled column.
72              // obtain a read table lock before cleaning, synchronize with MobFileCompactionChore.
73              boolean tableLocked = false;
74              TableLock lock = null;
75              try {
76                // the tableLockManager might be null in testing. In that case, it is lock-free.
77                if (tableLockManager != null) {
78                  lock = tableLockManager.readLock(MobUtils.getTableLockName(htd.getTableName()),
79                    "Run ExpiredMobFileCleanerChore");
80                  lock.acquire();
81                }
82                tableLocked = true;
83                cleaner.cleanExpiredMobFiles(htd.getTableName().getNameAsString(), hcd);
84              } catch (LockTimeoutException e) {
85                LOG.info("Fail to acquire the lock because of timeout, maybe a"
86                  + " MobCompactor is running", e);
87              } catch (ServiceException e) {
88                LOG.error(
89                  "Fail to clean the expired mob files for the column " + hcd.getNameAsString()
90                    + " in the table " + htd.getNameAsString(), e);
91              } catch (IOException e) {
92                LOG.error(
93                  "Fail to clean the expired mob files for the column " + hcd.getNameAsString()
94                    + " in the table " + htd.getNameAsString(), e);
95              } finally {
96                if (lock != null && tableLocked) {
97                  try {
98                    lock.release();
99                  } catch (IOException e) {
100                   LOG.error(
101                     "Fail to release the read lock for the table " + htd.getNameAsString(), e);
102                 }
103               }
104             }
105           }
106         }
107       }
108     } catch (Exception e) {
109       LOG.error("Fail to clean the expired mob files", e);
110     }
111   }
112 
113 }