cshannon commented on code in PR #5353:
URL: https://github.com/apache/accumulo/pull/5353#discussion_r2007443539


##########
server/manager/src/main/java/org/apache/accumulo/manager/merge/FindMergeableRangeTask.java:
##########
@@ -0,0 +1,293 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.manager.merge;
+
+import static 
org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType.FILES;
+import static 
org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType.MERGEABILITY;
+import static 
org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType.PREV_ROW;
+import static 
org.apache.accumulo.manager.merge.FindMergeableRangeTask.UnmergeableReason.MAX_FILE_COUNT;
+import static 
org.apache.accumulo.manager.merge.FindMergeableRangeTask.UnmergeableReason.MAX_TOTAL_SIZE;
+import static 
org.apache.accumulo.manager.merge.FindMergeableRangeTask.UnmergeableReason.NOT_CONTIGUOUS;
+import static 
org.apache.accumulo.manager.merge.FindMergeableRangeTask.UnmergeableReason.TABLET_MERGEABILITY;
+
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.function.Predicate;
+
+import org.apache.accumulo.core.conf.Property;
+import org.apache.accumulo.core.data.NamespaceId;
+import org.apache.accumulo.core.data.TableId;
+import org.apache.accumulo.core.dataImpl.KeyExtent;
+import org.apache.accumulo.core.fate.Fate.FateOperation;
+import org.apache.accumulo.core.fate.FateInstanceType;
+import org.apache.accumulo.core.fate.FateKey;
+import org.apache.accumulo.core.metadata.schema.TabletMetadata;
+import org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType;
+import org.apache.accumulo.core.metadata.schema.filters.TabletMetadataFilter;
+import org.apache.accumulo.core.util.time.SteadyTime;
+import org.apache.accumulo.manager.Manager;
+import org.apache.accumulo.manager.tableOps.TraceRepo;
+import org.apache.accumulo.manager.tableOps.merge.MergeInfo.Operation;
+import org.apache.accumulo.manager.tableOps.merge.TableRangeOp;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.io.Text;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Sets;
+
+/**
+ * This task is used to scan tables to find ranges of tablets that can be 
merged together
+ * automatically.
+ */
+public class FindMergeableRangeTask implements Runnable {
+
+  private static final Logger log = 
LoggerFactory.getLogger(FindMergeableRangeTask.class);
+
+  private static final TabletMergeabilityFilter FILTER = new 
TabletMergeabilityFilter();
+
+  private final Manager manager;
+
+  public FindMergeableRangeTask(Manager manager) {
+    this.manager = Objects.requireNonNull(manager);
+    log.debug("Creating FindMergeableRangeTask");
+  }
+
+  @Override
+  public void run() {
+    var context = manager.getContext();
+    Map<TableId,String> tables = context.getTableIdToNameMap();
+
+    log.debug("Starting FindMergeableRangeTask");
+
+    for (Entry<TableId,String> table : tables.entrySet()) {
+      TableId tableId = table.getKey();
+      String tableName = table.getValue();
+
+      // Read the table configuration to compute the max total file size of a 
mergeable range.
+      // The max size is a percentage of the configured split threshold and we 
do not want
+      // to exceed this limit.
+      long threshold =
+          
context.getTableConfiguration(tableId).getAsBytes(Property.TABLE_SPLIT_THRESHOLD);
+      double mergeabilityThreshold = context.getTableConfiguration(tableId)
+          .getFraction(Property.TABLE_MAX_MERGEABILITY_THRESHOLD);
+      if (mergeabilityThreshold <= 0) {
+        log.trace("Skipping FindMergeableRangeTask for table {}, {}} is set to 
{}", tableName,
+            Property.TABLE_MAX_MERGEABILITY_THRESHOLD.getKey(), 
mergeabilityThreshold);
+        continue;
+      }
+
+      long maxFileCount =
+          
context.getTableConfiguration(tableId).getCount(Property.TABLE_MERGE_FILE_MAX);
+      long maxTotalSize = (long) (threshold * mergeabilityThreshold);
+
+      log.debug("Checking {} for tablets that can be merged", tableName);
+      log.trace("maxFileCount: {}, maxTotalSize:{}, splitThreshold:{}, 
mergeabilityThreshold:{}",
+          maxFileCount, maxTotalSize, threshold, mergeabilityThreshold);
+      try {
+        NamespaceId namespaceId = context.getNamespaceId(tableId);
+        var type = FateInstanceType.fromTableId(tableId);
+
+        try (var tablets = context.getAmple().readTablets().forTable(tableId)
+            .fetch(PREV_ROW, FILES, MERGEABILITY).filter(FILTER).build()) {
+
+          final MergeableRange current =
+              new MergeableRange(tableId, manager.getSteadyTime(), 
maxFileCount, maxTotalSize);
+
+          for (var tm : tablets) {
+            log.trace("Checking tablet {}, {}", tm.getExtent(), 
tm.getTabletMergeability());
+            // If there was an error adding the next tablet to the range then
+            // the existing range is complete as we can't add more tablets so
+            // submit a merge fate op and reset to find more merge ranges
+            current.add(tm).ifPresent(error -> {
+              submit(current, type, table, namespaceId);
+              current.resetAndAdd(tm);
+            });
+          }
+
+          // Try and submit any outstanding mergeable tablets
+          submit(current, type, table, namespaceId);
+        }
+
+      } catch (Exception e) {
+        log.error("Failed to generate system merges for {}", tableName, e);
+      }

Review Comment:
   I pushed an update to main with this change.



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to