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 org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.conf.Configured;
023import org.apache.hadoop.fs.FileSystem;
024import org.apache.hadoop.hbase.HBaseConfiguration;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.Admin;
028import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
029import org.apache.hadoop.hbase.client.Connection;
030import org.apache.hadoop.hbase.client.ConnectionFactory;
031import org.apache.hadoop.hbase.client.HBaseAdmin;
032import org.apache.hadoop.hbase.client.TableDescriptor;
033import org.apache.hadoop.hbase.io.hfile.CacheConfig;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
036import org.apache.hadoop.util.Tool;
037import org.apache.hadoop.util.ToolRunner;
038import org.apache.yetus.audience.InterfaceAudience;
039import org.slf4j.Logger;
040import org.slf4j.LoggerFactory;
041
042/**
043 * The cleaner to delete the expired MOB files.
044 */
045@InterfaceAudience.Private
046public class ExpiredMobFileCleaner extends Configured implements Tool {
047
048  private static final Logger LOG = LoggerFactory.getLogger(ExpiredMobFileCleaner.class);
049
050  /**
051   * Cleans the MOB files when they're expired and their min versions are 0. If the latest timestamp
052   * of Cells in a MOB file is older than the TTL in the column family, it's regarded as expired.
053   * This cleaner deletes them. At a time T0, the cells in a mob file M0 are expired. If a user
054   * starts a scan before T0, those mob cells are visible, this scan still runs after T0. At that
055   * time T1, this mob file M0 is expired, meanwhile a cleaner starts, the M0 is archived and can be
056   * read in the archive directory.
057   * @param tableName The current table name.
058   * @param family    The current family.
059   */
060  public void cleanExpiredMobFiles(String tableName, ColumnFamilyDescriptor family)
061    throws IOException {
062    Configuration conf = getConf();
063    TableName tn = TableName.valueOf(tableName);
064    FileSystem fs = FileSystem.get(conf);
065    LOG.info("Cleaning the expired MOB files of " + family.getNameAsString() + " in " + tableName);
066    // disable the block cache.
067    Configuration copyOfConf = new Configuration(conf);
068    copyOfConf.setFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, 0f);
069    CacheConfig cacheConfig = new CacheConfig(copyOfConf);
070    MobUtils.cleanExpiredMobFiles(fs, conf, tn, family, cacheConfig,
071      EnvironmentEdgeManager.currentTime());
072  }
073
074  public static void main(String[] args) throws Exception {
075    Configuration conf = HBaseConfiguration.create();
076    ToolRunner.run(conf, new ExpiredMobFileCleaner(), args);
077  }
078
079  private void printUsage() {
080    System.err.println("Usage:\n" + "--------------------------\n"
081      + ExpiredMobFileCleaner.class.getName() + " tableName familyName");
082    System.err.println(" tableName        The table name");
083    System.err.println(" familyName       The column family name");
084  }
085
086  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "REC_CATCH_EXCEPTION",
087      justification = "Intentional")
088  @Override
089  public int run(String[] args) throws Exception {
090    if (args.length != 2) {
091      printUsage();
092      return 1;
093    }
094    String tableName = args[0];
095    String familyName = args[1];
096    TableName tn = TableName.valueOf(tableName);
097    HBaseAdmin.available(getConf());
098    Connection connection = ConnectionFactory.createConnection(getConf());
099    Admin admin = connection.getAdmin();
100    try {
101      TableDescriptor htd = admin.getDescriptor(tn);
102      ColumnFamilyDescriptor family = htd.getColumnFamily(Bytes.toBytes(familyName));
103      if (family == null || !family.isMobEnabled()) {
104        throw new IOException("Column family " + familyName + " is not a MOB column family");
105      }
106      if (family.getMinVersions() > 0) {
107        throw new IOException(
108          "The minVersions of the column family is not 0, could not be handled by this cleaner");
109      }
110      cleanExpiredMobFiles(tableName, family);
111      return 0;
112    } finally {
113      try {
114        admin.close();
115      } catch (IOException e) {
116        LOG.error("Failed to close the HBaseAdmin.", e);
117      }
118      try {
119        connection.close();
120      } catch (IOException e) {
121        LOG.error("Failed to close the connection.", e);
122      }
123    }
124  }
125}