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  import java.util.concurrent.TimeUnit;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.HColumnDescriptor;
29  import org.apache.hadoop.hbase.HTableDescriptor;
30  import org.apache.hadoop.hbase.ScheduledChore;
31  import org.apache.hadoop.hbase.TableDescriptors;
32  import org.apache.hadoop.hbase.mob.MobUtils;
33  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
34  
35  /**
36   * The Class MobCompactChore for running compaction regularly to merge small mob files.
37   */
38  @InterfaceAudience.Private
39  public class MobCompactionChore extends ScheduledChore {
40  
41    private static final Log LOG = LogFactory.getLog(MobCompactionChore.class);
42    private HMaster master;
43    private TableLockManager tableLockManager;
44    private ExecutorService pool;
45  
46    public MobCompactionChore(HMaster master, int period) {
47      // use the period as initial delay.
48      super(master.getServerName() + "-MobCompactionChore", master, period, period, TimeUnit.SECONDS);
49      this.master = master;
50      this.tableLockManager = master.getTableLockManager();
51      this.pool = MobUtils.createMobCompactorThreadPool(master.getConfiguration());
52    }
53  
54    @Override
55    protected void chore() {
56      try {
57        TableDescriptors htds = master.getTableDescriptors();
58        Map<String, HTableDescriptor> map = htds.getAll();
59        for (HTableDescriptor htd : map.values()) {
60          if (!master.getAssignmentManager().getTableStateManager().isTableState(htd.getTableName(),
61            ZooKeeperProtos.Table.State.ENABLED)) {
62            continue;
63          }
64          boolean reported = false;
65          try {
66            for (HColumnDescriptor hcd : htd.getColumnFamilies()) {
67              if (!hcd.isMobEnabled()) {
68                continue;
69              }
70              if (!reported) {
71                master.reportMobCompactionStart(htd.getTableName());
72                reported = true;
73              }
74              MobUtils.doMobCompaction(master.getConfiguration(), master.getFileSystem(),
75                htd.getTableName(), hcd, pool, tableLockManager, false);
76            }
77          } finally {
78            if (reported) {
79              master.reportMobCompactionEnd(htd.getTableName());
80            }
81          }
82        }
83      } catch (Exception e) {
84        LOG.error("Failed to compact mob files", e);
85      }
86    }
87  
88    @Override
89    protected synchronized void cleanup() {
90      super.cleanup();
91      pool.shutdown();
92    }
93  }