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  static {
046    Configuration.addDeprecation(MobConstants.DEPRECATED_MOB_CLEANER_PERIOD,
047      MobConstants.MOB_CLEANER_PERIOD);
048  }
049
050  public MobFileCleanerChore(HMaster master) {
051    super(master.getServerName() + "-MobFileCleanerChore", master,
052      master.getConfiguration().getInt(MobConstants.MOB_CLEANER_PERIOD,
053        MobConstants.DEFAULT_MOB_CLEANER_PERIOD),
054      master.getConfiguration().getInt(MobConstants.MOB_CLEANER_PERIOD,
055        MobConstants.DEFAULT_MOB_CLEANER_PERIOD),
056      TimeUnit.SECONDS);
057    this.master = master;
058    cleaner = new ExpiredMobFileCleaner();
059    cleaner.setConf(master.getConfiguration());
060    checkObsoleteConfigurations();
061  }
062
063  private void checkObsoleteConfigurations() {
064    Configuration conf = master.getConfiguration();
065
066    if (conf.get("hbase.mob.compaction.mergeable.threshold") != null) {
067      LOG.warn("'hbase.mob.compaction.mergeable.threshold' is obsolete and not used anymore.");
068    }
069    if (conf.get("hbase.mob.delfile.max.count") != null) {
070      LOG.warn("'hbase.mob.delfile.max.count' is obsolete and not used anymore.");
071    }
072    if (conf.get("hbase.mob.compaction.threads.max") != null) {
073      LOG.warn("'hbase.mob.compaction.threads.max' is obsolete and not used anymore.");
074    }
075    if (conf.get("hbase.mob.compaction.batch.size") != null) {
076      LOG.warn("'hbase.mob.compaction.batch.size' is obsolete and not used anymore.");
077    }
078  }
079
080  @Override
081  protected void chore() {
082    TableDescriptors htds = master.getTableDescriptors();
083
084    Map<String, TableDescriptor> map = null;
085    try {
086      map = htds.getAll();
087    } catch (IOException e) {
088      LOG.error("MobFileCleanerChore failed", e);
089      return;
090    }
091    for (TableDescriptor htd : map.values()) {
092      for (ColumnFamilyDescriptor hcd : htd.getColumnFamilies()) {
093        if (hcd.isMobEnabled() && hcd.getMinVersions() == 0) {
094          try {
095            cleaner.cleanExpiredMobFiles(htd.getTableName().getNameAsString(), hcd);
096          } catch (IOException e) {
097            LOG.error("Failed to clean the expired mob files table={} family={}",
098              htd.getTableName().getNameAsString(), hcd.getNameAsString(), e);
099          }
100        }
101      }
102      try {
103        // Now clean obsolete files for a table
104        LOG.info("Cleaning obsolete MOB files from table={}", htd.getTableName());
105        try (final Admin admin = master.getConnection().getAdmin()) {
106          MobFileCleanupUtil.cleanupObsoleteMobFiles(master.getConfiguration(), htd.getTableName(),
107            admin);
108        }
109        LOG.info("Cleaning obsolete MOB files finished for table={}", htd.getTableName());
110      } catch (IOException e) {
111        LOG.error("Failed to clean the obsolete mob files for table={}", htd.getTableName(), e);
112      }
113    }
114  }
115
116}