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 */
018package org.apache.hadoop.hbase.mob;
019
020import java.io.IOException;
021import java.util.Map;
022import java.util.concurrent.TimeUnit;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.ScheduledChore;
025import org.apache.hadoop.hbase.TableDescriptors;
026import org.apache.hadoop.hbase.client.Admin;
027import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
028import org.apache.hadoop.hbase.client.TableDescriptor;
029import org.apache.hadoop.hbase.master.HMaster;
030import org.apache.yetus.audience.InterfaceAudience;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034/**
035 * The class MobFileCleanerChore for running cleaner regularly to remove the expired and obsolete
036 * (files which have no active references to) mob files.
037 */
038@InterfaceAudience.Private
039public class MobFileCleanerChore extends ScheduledChore {
040
041  private static final Logger LOG = LoggerFactory.getLogger(MobFileCleanerChore.class);
042  private final HMaster master;
043  private ExpiredMobFileCleaner cleaner;
044
045  public MobFileCleanerChore(HMaster master) {
046    super(master.getServerName() + "-MobFileCleanerChore", master,
047      master.getConfiguration().getInt(MobConstants.MOB_CLEANER_PERIOD,
048        MobConstants.DEFAULT_MOB_CLEANER_PERIOD),
049      master.getConfiguration().getInt(MobConstants.MOB_CLEANER_PERIOD,
050        MobConstants.DEFAULT_MOB_CLEANER_PERIOD),
051      TimeUnit.SECONDS);
052    this.master = master;
053    cleaner = new ExpiredMobFileCleaner();
054    cleaner.setConf(master.getConfiguration());
055    checkObsoleteConfigurations();
056  }
057
058  private void checkObsoleteConfigurations() {
059    Configuration conf = master.getConfiguration();
060
061    if (conf.get("hbase.mob.compaction.mergeable.threshold") != null) {
062      LOG.warn("'hbase.mob.compaction.mergeable.threshold' is obsolete and not used anymore.");
063    }
064    if (conf.get("hbase.mob.delfile.max.count") != null) {
065      LOG.warn("'hbase.mob.delfile.max.count' is obsolete and not used anymore.");
066    }
067    if (conf.get("hbase.mob.compaction.threads.max") != null) {
068      LOG.warn("'hbase.mob.compaction.threads.max' is obsolete and not used anymore.");
069    }
070    if (conf.get("hbase.mob.compaction.batch.size") != null) {
071      LOG.warn("'hbase.mob.compaction.batch.size' is obsolete and not used anymore.");
072    }
073  }
074
075  @Override
076  protected void chore() {
077    TableDescriptors htds = master.getTableDescriptors();
078
079    Map<String, TableDescriptor> map = null;
080    try {
081      map = htds.getAll();
082    } catch (IOException e) {
083      LOG.error("MobFileCleanerChore failed", e);
084      return;
085    }
086    for (TableDescriptor htd : map.values()) {
087      for (ColumnFamilyDescriptor hcd : htd.getColumnFamilies()) {
088        if (hcd.isMobEnabled() && hcd.getMinVersions() == 0) {
089          try {
090            cleaner.cleanExpiredMobFiles(htd, hcd);
091          } catch (IOException e) {
092            LOG.error("Failed to clean the expired mob files table={} family={}",
093              htd.getTableName().getNameAsString(), hcd.getNameAsString(), e);
094          }
095        }
096      }
097      try {
098        // Now clean obsolete files for a table
099        LOG.info("Cleaning obsolete MOB files from table={}", htd.getTableName());
100        try (final Admin admin = master.getConnection().getAdmin()) {
101          MobFileCleanupUtil.cleanupObsoleteMobFiles(master.getConfiguration(), htd.getTableName(),
102            admin);
103        }
104        LOG.info("Cleaning obsolete MOB files finished for table={}", htd.getTableName());
105      } catch (IOException e) {
106        LOG.error("Failed to clean the obsolete mob files for table={}", htd.getTableName(), e);
107      }
108    }
109  }
110
111}