001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.hbase.master;
020
021import java.util.Map;
022import java.util.concurrent.TimeUnit;
023
024import org.apache.hadoop.hbase.ScheduledChore;
025import org.apache.hadoop.hbase.TableDescriptors;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
030import org.apache.hadoop.hbase.client.TableDescriptor;
031import org.apache.hadoop.hbase.master.locking.LockManager;
032import org.apache.hadoop.hbase.mob.ExpiredMobFileCleaner;
033import org.apache.hadoop.hbase.mob.MobConstants;
034import org.apache.hadoop.hbase.mob.MobUtils;
035import org.apache.hadoop.hbase.procedure2.LockType;
036
037/**
038 * The Class ExpiredMobFileCleanerChore for running cleaner regularly to remove the expired
039 * mob files.
040 */
041@InterfaceAudience.Private
042public class ExpiredMobFileCleanerChore extends ScheduledChore {
043
044  private static final Logger LOG = LoggerFactory.getLogger(ExpiredMobFileCleanerChore.class);
045  private final HMaster master;
046  private ExpiredMobFileCleaner cleaner;
047
048  public ExpiredMobFileCleanerChore(HMaster master) {
049    super(master.getServerName() + "-ExpiredMobFileCleanerChore", master, master.getConfiguration()
050      .getInt(MobConstants.MOB_CLEANER_PERIOD, MobConstants.DEFAULT_MOB_CLEANER_PERIOD), master
051      .getConfiguration().getInt(MobConstants.MOB_CLEANER_PERIOD,
052        MobConstants.DEFAULT_MOB_CLEANER_PERIOD), TimeUnit.SECONDS);
053    this.master = master;
054    cleaner = new ExpiredMobFileCleaner();
055    cleaner.setConf(master.getConfiguration());
056  }
057
058  @Override
059  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="REC_CATCH_EXCEPTION",
060    justification="Intentional")
061  protected void chore() {
062    try {
063      TableDescriptors htds = master.getTableDescriptors();
064      Map<String, TableDescriptor> map = htds.getAll();
065      for (TableDescriptor htd : map.values()) {
066        for (ColumnFamilyDescriptor hcd : htd.getColumnFamilies()) {
067          if (hcd.isMobEnabled() && hcd.getMinVersions() == 0) {
068            // clean only for mob-enabled column.
069            // obtain a read table lock before cleaning, synchronize with MobFileCompactionChore.
070            final LockManager.MasterLock lock = master.getLockManager().createMasterLock(
071                MobUtils.getTableLockName(htd.getTableName()), LockType.SHARED,
072                this.getClass().getSimpleName() + ": Cleaning expired mob files");
073            try {
074              lock.acquire();
075              cleaner.cleanExpiredMobFiles(htd.getTableName().getNameAsString(), hcd);
076            } finally {
077              lock.release();
078            }
079          }
080        }
081      }
082    } catch (Exception e) {
083      LOG.error("Fail to clean the expired mob files", e);
084    }
085  }
086
087}