View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.master;
20  
21  import java.util.Map;
22  import java.util.concurrent.ExecutorService;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.classification.InterfaceAudience;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.hbase.ScheduledChore;
30  import org.apache.hadoop.hbase.HColumnDescriptor;
31  import org.apache.hadoop.hbase.HTableDescriptor;
32  import org.apache.hadoop.hbase.TableDescriptors;
33  import org.apache.hadoop.hbase.mob.MobConstants;
34  import org.apache.hadoop.hbase.mob.MobUtils;
35  
36  /**
37   * The Class MobFileCompactChore for running compaction regularly to merge small mob files.
38   */
39  @InterfaceAudience.Private
40  public class MobFileCompactionChore extends ScheduledChore {
41  
42    private static final Log LOG = LogFactory.getLog(MobFileCompactionChore.class);
43    private HMaster master;
44    private TableLockManager tableLockManager;
45    private ExecutorService pool;
46  
47    public MobFileCompactionChore(HMaster master) {
48      super(master.getServerName() + "-MobFileCompactChore", master,
49        master.getConfiguration().getInt(
50          MobConstants.MOB_FILE_COMPACTION_CHORE_PERIOD,
51            MobConstants.DEFAULT_MOB_FILE_COMPACTION_CHORE_PERIOD));
52      this.master = master;
53      this.tableLockManager = master.getTableLockManager();
54      this.pool = MobUtils.createMobFileCompactorThreadPool(master.getConfiguration());
55    }
56  
57    @Override
58    protected void chore() {
59      try {
60        TableDescriptors htds = master.getTableDescriptors();
61        Map<String, HTableDescriptor> map = htds.getAll();
62        for (HTableDescriptor htd : map.values()) {
63          if (!master.getConnection().isTableEnabled(htd.getTableName())) {
64            continue;
65          }
66          boolean reported = false;
67          try {
68            for (HColumnDescriptor hcd : htd.getColumnFamilies()) {
69              if (!hcd.isMobEnabled()) {
70                continue;
71              }
72              if (!reported) {
73                master.reportMobFileCompactionStart(htd.getTableName());
74                reported = true;
75              }
76              MobUtils.doMobFileCompaction(master.getConfiguration(), master.getFileSystem(),
77                htd.getTableName(), hcd, pool, tableLockManager, false);
78            }
79          } finally {
80            if (reported) {
81              master.reportMobFileCompactionEnd(htd.getTableName());
82            }
83          }
84        }
85      } catch (Exception e) {
86        LOG.error("Fail to clean the expired mob files", e);
87      }
88    }
89  
90    @Override
91    protected void cleanup() {
92      super.cleanup();
93      pool.shutdown();
94    }
95  }