VladRodionov commented on a change in pull request #623: HBASE-22749: 
Distributed MOB compactions
URL: https://github.com/apache/hbase/pull/623#discussion_r336206677
 
 

 ##########
 File path: hbase-server/src/main/java/org/apache/hadoop/hbase/mob/MobUtils.java
 ##########
 @@ -907,6 +789,143 @@ public static boolean hasMobColumns(TableDescriptor htd) 
{
     return false;
   }
 
+  /**
+   * Get list of Mob column families (if any exists)
+   * @param htd table descriptor
+   * @return list of Mob column families
+   */
+  public static List<ColumnFamilyDescriptor> 
getMobColumnFamilies(TableDescriptor htd){
+
+    List<ColumnFamilyDescriptor> fams = new 
ArrayList<ColumnFamilyDescriptor>();
+    ColumnFamilyDescriptor[] hcds = htd.getColumnFamilies();
+    for (ColumnFamilyDescriptor hcd : hcds) {
+      if (hcd.isMobEnabled()) {
+        fams.add(hcd);
+      }
+    }
+    return fams;
+  }
+
+  /**
+   * Performs housekeeping file cleaning (called by MOB Cleaner chore)
+   * @param conf configuration
+   * @param table table name
+   * @throws IOException
+   */
+  public static void cleanupObsoleteMobFiles(Configuration conf, TableName 
table)
+      throws IOException {
+
+    try (final Connection conn = ConnectionFactory.createConnection(conf);
+        final Admin admin = conn.getAdmin();) {
+      TableDescriptor htd = admin.getDescriptor(table);
+      List<ColumnFamilyDescriptor> list = getMobColumnFamilies(htd);
+      if (list.size() == 0) {
+        LOG.info("Skipping non-MOB table [" + table + "]");
+        return;
+      }
+      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) {
+            //TODO handle FNFE
+            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) {
+                HStoreFile sf = new HStoreFile(fs, pp, conf, 
CacheConfig.DISABLED,
+                  BloomType.NONE, true);
+                sf.initReader();
+                byte[] mobRefData = 
sf.getMetadataValue(HStoreFile.MOB_FILE_REFS);
+                byte[] mobCellCountData = 
sf.getMetadataValue(HStoreFile.MOB_CELLS_COUNT);
+                byte[] bulkloadMarkerData = 
sf.getMetadataValue(HStoreFile.BULKLOAD_TASK_KEY);
+                if (mobRefData == null && (mobCellCountData != null ||
+                    bulkloadMarkerData == null)) {
+                  LOG.info("Found old store file with no MOB_FILE_REFS: " + pp
+                    +" - can not proceed until all old files will be 
MOB-compacted");
+                  return;
+                } else if (mobRefData == null) {
+                  LOG.info("Skipping file without MOB references (can be 
bulkloaded file):"+ pp);
+                  continue;
+                }
+                String[] mobs = new String(mobRefData).split(",");
+                regionMobs.addAll(Arrays.asList(mobs));
+              }
+            } catch (FileNotFoundException e) {
+              //TODO
+              LOG.warn(e.getMessage());
+              continue;
+            }
+            succeed = true;
+          }
+          // Add MOB refs for current region/family
+          allActiveMobFileName.addAll(regionMobs);
+        } // END column families
+      }//END regions
+
+      // Now scan MOB directories and find MOB files with no references to them
+      long now = System.currentTimeMillis();
+      long minAgeToArchive = 
conf.getLong(MobConstants.MOB_MINIMUM_FILE_AGE_TO_ARCHIVE_KEY,
+                              
MobConstants.DEFAULT_MOB_MINIMUM_FILE_AGE_TO_ARCHIVE);
+      for (ColumnFamilyDescriptor hcd: list) {
+          List<Path> toArchive = new ArrayList<Path>();
+          String family = hcd.getNameAsString();
+          Path dir = 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
+              /*DEBUG*/ LOG.debug(" Age=" + (now - 
fs.getFileStatus(p).getModificationTime()) +
+                " MOB file="+ p);
+              if (now - fs.getFileStatus(p).getModificationTime() > 
minAgeToArchive) {
+                toArchive.add(p);
+              } else {
+                LOG.debug(" Skipping fresh file: " + p);
+              }
+            }
+          }
+          LOG.info(" MOB Cleaner found "+ toArchive.size()+" files for 
family="+family);
+          removeMobFiles(conf, table, family.getBytes(), toArchive);
+          LOG.info(" MOB Cleaner archived "+ toArchive.size()+" files");
+      }
+    }
+  }
+
+
+
+
+  public static long getNumberOfMobFiles(Configuration conf, TableName 
tableName, String family)
 
 Review comment:
   Fixed.

----------------------------------------------------------------
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