busbey commented on a change in pull request #921: HBASE-22749: Distributed MOB 
compactions
URL: https://github.com/apache/hbase/pull/921#discussion_r357425128
 
 

 ##########
 File path: 
hbase-server/src/main/java/org/apache/hadoop/hbase/master/MobFileCleanerChore.java
 ##########
 @@ -0,0 +1,289 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hbase.master;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.LocatedFileStatus;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.fs.RemoteIterator;
+import org.apache.hadoop.hbase.ScheduledChore;
+import org.apache.hadoop.hbase.TableDescriptors;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.backup.HFileArchiver;
+import org.apache.hadoop.hbase.client.Admin;
+import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
+import org.apache.hadoop.hbase.client.Connection;
+import org.apache.hadoop.hbase.client.ConnectionFactory;
+import org.apache.hadoop.hbase.client.TableDescriptor;
+import org.apache.hadoop.hbase.io.hfile.CacheConfig;
+import org.apache.hadoop.hbase.mob.ExpiredMobFileCleaner;
+import org.apache.hadoop.hbase.mob.MobConstants;
+import org.apache.hadoop.hbase.mob.MobUtils;
+import org.apache.hadoop.hbase.regionserver.BloomType;
+import org.apache.hadoop.hbase.regionserver.HStoreFile;
+import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
+import org.apache.hadoop.hbase.util.FSUtils;
+import org.apache.hadoop.hbase.util.HFileArchiveUtil;
+import 
org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
+import org.apache.yetus.audience.InterfaceAudience;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The Class ExpiredMobFileCleanerChore for running cleaner regularly to 
remove the expired
+ * and obsolete (files which have no active references to) mob files.
+ */
+@InterfaceAudience.Private
+public class MobFileCleanerChore extends ScheduledChore {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(MobFileCleanerChore.class);
+  private final HMaster master;
+  private ExpiredMobFileCleaner cleaner;
+  private long minAgeToArchive;
+
+  public MobFileCleanerChore(HMaster master) {
+    super(master.getServerName() + "-ExpiredMobFileCleanerChore", master, 
master.getConfiguration()
+      .getInt(MobConstants.MOB_CLEANER_PERIOD, 
MobConstants.DEFAULT_MOB_CLEANER_PERIOD), master
+      .getConfiguration().getInt(MobConstants.MOB_CLEANER_PERIOD,
+        MobConstants.DEFAULT_MOB_CLEANER_PERIOD), TimeUnit.SECONDS);
+    this.master = master;
+    cleaner = new ExpiredMobFileCleaner();
+    cleaner.setConf(master.getConfiguration());
+    checkObsoleteConfigurations();
+  }
+
+  private void checkObsoleteConfigurations() {
+    Configuration conf = master.getConfiguration();
+    if (conf.get("hbase.master.mob.ttl.cleaner.period") != null) {
+      LOG.warn("'hbase.master.mob.ttl.cleaner.period' is obsolete and not used 
anymore.");
+    }
+    if (conf.get("hbase.mob.compaction.mergeable.threshold") != null) {
+      LOG.warn("'hbase.mob.compaction.mergeable.threshold' is obsolete and not 
used anymore.");
+    }
+    if (conf.get("hbase.mob.delfile.max.count") != null) {
+      LOG.warn("'hbase.mob.delfile.max.count' is obsolete and not used 
anymore.");
+    }
+    if (conf.get("hbase.mob.compaction.threads.max") != null) {
+      LOG.warn("'hbase.mob.compaction.threads.max' is obsolete and not used 
anymore.");
+    }
+    if (conf.get("hbase.mob.compaction.batch.size") != null) {
+      LOG.warn("'hbase.mob.compaction.batch.size' is obsolete and not used 
anymore.");
+    }
+  }
+
+  @VisibleForTesting
+  public MobFileCleanerChore() {
+    this.master = null;
+  }
+
+  @Override
+  
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value="REC_CATCH_EXCEPTION",
+    justification="Intentional")
+
+  protected void chore() {
+    try {
+
+      TableDescriptors htds = master.getTableDescriptors();
+      Map<String, TableDescriptor> map = htds.getAll();
+      for (TableDescriptor htd : map.values()) {
+        for (ColumnFamilyDescriptor hcd : htd.getColumnFamilies()) {
+          if (hcd.isMobEnabled() && hcd.getMinVersions() == 0) {
+            cleaner.cleanExpiredMobFiles(htd.getTableName().getNameAsString(), 
hcd);
+          }
+        }
+        // Now clean obsolete files for a table
+        LOG.info("Cleaning obsolete MOB files ...");
+        cleanupObsoleteMobFiles(master.getConfiguration(), htd.getTableName());
+        LOG.info("Cleaning obsolete MOB files finished");
+      }
+    } catch (Exception e) {
+      LOG.error("Fail to clean the expired mob files", e);
+    }
+  }
+  /**
+   * Performs housekeeping file cleaning (called by MOB Cleaner chore)
+   * @param conf configuration
+   * @param table table name
+   * @throws IOException
+   */
+  public void cleanupObsoleteMobFiles(Configuration conf, TableName table)
+      throws IOException {
+
+    this.minAgeToArchive = conf.getLong(MobConstants.MIN_AGE_TO_ARCHIVE_KEY,
+      MobConstants.DEFAULT_MIN_AGE_TO_ARCHIVE);
+    try (final Connection conn = ConnectionFactory.createConnection(conf);
+        final Admin admin = conn.getAdmin();) {
+      TableDescriptor htd = admin.getDescriptor(table);
+      List<ColumnFamilyDescriptor> list = MobUtils.getMobColumnFamilies(htd);
+      if (list.size() == 0) {
+        LOG.info("Skipping non-MOB table [{}]",  table);
+        return;
+      }
+      // We check only those MOB files, which creation time is less
+      // than maxTimeToArchive. This is a current time - 1h. 1 hour gap
+      // gives us full confidence that all corresponding store files will
+      // exist at the time cleaning procedure begins and will be examined.
+      // So, if MOB file creation time is greater than this maxTimeToArchive,
+      // this will be skipped and won't be archived.
+      long maxCreationTimeToArchive = EnvironmentEdgeManager.currentTime() - 
minAgeToArchive;
+      LOG.info("Only MOB files whose creation time less than {} will be 
archived",
+        maxCreationTimeToArchive);
+      Path rootDir = FSUtils.getRootDir(conf);
+      Path tableDir = FSUtils.getTableDir(rootDir, table);
+      // How safe is this call?
+      List<Path> regionDirs = FSUtils.getRegionDirs(FileSystem.get(conf), 
tableDir);
+
+      Set<String> allActiveMobFileName = new HashSet<String>();
+      FileSystem fs = FileSystem.get(conf);
+      for (Path regionPath : regionDirs) {
+        for (ColumnFamilyDescriptor hcd : list) {
+          String family = hcd.getNameAsString();
+          Path storePath = new Path(regionPath, family);
+          boolean succeed = false;
+          Set<String> regionMobs = new HashSet<String>();
+          while (!succeed) {
+
+            RemoteIterator<LocatedFileStatus> rit = 
fs.listLocatedStatus(storePath);
+            List<Path> storeFiles = new ArrayList<Path>();
+            // Load list of store files first
+            while (rit.hasNext()) {
+              Path p = rit.next().getPath();
+              if (fs.isFile(p)) {
+                storeFiles.add(p);
+              }
+            }
+            try {
+              for (Path pp : storeFiles) {
+                LOG.debug("Store file: {}", pp);
+                HStoreFile sf =
+                    new HStoreFile(fs, pp, conf, CacheConfig.DISABLED, 
BloomType.NONE, true);
+                sf.initReader();
+                byte[] mobRefData = 
sf.getMetadataValue(HStoreFile.MOB_FILE_REFS);
+                byte[] bulkloadMarkerData = 
sf.getMetadataValue(HStoreFile.BULKLOAD_TASK_KEY);
+                // close store file to avoid memory leaks
+                sf.closeStoreFile(true);
+                if (mobRefData == null && bulkloadMarkerData == null) {
+                  LOG.warn("Found old store file with no MOB_FILE_REFS: {} - "
+                    + "can not proceed until all old files will be 
MOB-compacted.", pp);
+                  return;
+                } else if (mobRefData == null && bulkloadMarkerData != null) {
+                  LOG.info("Skipping file without MOB references (bulkloaded 
file):{}", pp);
+                  continue;
+                }
+                if (mobRefData.length > 1) {
+                  // if length = 1 means NULL, that there are no MOB references
+                  // in this store file, but the file was created by new MOB 
code
+                  String[] mobs = new String(mobRefData).split(",");
+                  LOG.debug("Found: {} mob refs: ", mobs.length, 
Arrays.toString(mobs));
+                  regionMobs.addAll(Arrays.asList(mobs));
+                }
+              }
+            } catch (FileNotFoundException e) {
+              LOG.warn("Starting MOB cleaning cycle from the beginning due to 
error:",e);
+              continue;
+            }
+            succeed = true;
+          }
+          // Add MOB refs for current region/family
+          allActiveMobFileName.addAll(regionMobs);
+        } // END column families
+      } // END regions
+      // Check if number of MOB files too big (over 1M)
+      if (allActiveMobFileName.size() > 1000000) {
+        LOG.warn("Found too many active MOB files: {}, this may result in high 
memory pressure.",
+          allActiveMobFileName.size());
+      }
+      LOG.debug("Found: {} active mob refs", allActiveMobFileName.size());
+      allActiveMobFileName.stream().forEach(LOG::debug);
+
+      // Now scan MOB directories and find MOB files with no references to them
+      for (ColumnFamilyDescriptor hcd : list) {
+        List<Path> toArchive = new ArrayList<Path>();
+        String family = hcd.getNameAsString();
+        Path dir = MobUtils.getMobFamilyPath(conf, table, family);
+        RemoteIterator<LocatedFileStatus> rit = fs.listLocatedStatus(dir);
+        while (rit.hasNext()) {
+          LocatedFileStatus lfs = rit.next();
+          Path p = lfs.getPath();
+          if (!allActiveMobFileName.contains(p.getName())) {
+            // MOB is not in a list of active references, but it can be too
+            // fresh, skip it in this case
+            long creationTime = fs.getFileStatus(p).getModificationTime();
+            if ( creationTime < maxCreationTimeToArchive) {
+              /* DEBUG */ LOG.info(
+                " Archiving MOB file{} creation time=" + 
(fs.getFileStatus(p).getModificationTime()), p);
+              toArchive.add(p);
+            } else {
+              LOG.info("Skipping fresh file: {}", p);
+            }
+          } else {
+            LOG.info("Keepeing active MOB file: {}", p);
+          }
+        }
+        LOG.info(" MOB Cleaner found {} files for family={}", toArchive.size() 
, family);
+        removeMobFiles(conf, table, family.getBytes(), toArchive);
+        LOG.info(" MOB Cleaner archived {} files", toArchive.size());
 
 Review comment:
   include table and family name

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to