keith-turner commented on code in PR #5353:
URL: https://github.com/apache/accumulo/pull/5353#discussion_r1994364312


##########
server/manager/src/main/java/org/apache/accumulo/manager/merge/FindMergeableRangeTask.java:
##########
@@ -0,0 +1,287 @@
+/*
+ * 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;
+
+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();
+
+      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 {}, 
TABLE_MAX_MERGEABILITY_THRESHOLD is set to {}",
+            tableName, 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.debug("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(e -> {

Review Comment:
   Renaming e would align w/ the wording in the comment
   
   ```suggestion
               current.add(tm).ifPresent(error -> {
   ```



##########
server/manager/src/main/java/org/apache/accumulo/manager/Manager.java:
##########
@@ -1309,6 +1310,13 @@ boolean canSuspendTablets() {
     ThreadPools.watchCriticalScheduledTask(context.getScheduledExecutor()
         .scheduleWithFixedDelay(() -> 
ScanServerMetadataEntries.clean(context), 10, 10, MINUTES));
 
+    // TODO - create new threadpool?

Review Comment:
   Need to remove this TODO



##########
server/manager/src/main/java/org/apache/accumulo/manager/merge/FindMergeableRangeTask.java:
##########
@@ -0,0 +1,287 @@
+/*
+ * 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;
+
+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();
+
+      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 {}, 
TABLE_MAX_MERGEABILITY_THRESHOLD is set to {}",
+            tableName, 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.debug("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(e -> {
+              submit(current, type, table, namespaceId);
+              current.resetAndAdd(tm);
+            });
+          }
+
+          // Try and submit an outstanding mergeable tablets

Review Comment:
   ```suggestion
             // Try and submit any outstanding mergeable tablets
   ```



##########
test/src/main/java/org/apache/accumulo/test/functional/TabletMergeabilityIT.java:
##########
@@ -0,0 +1,406 @@
+/*
+ * 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.test.functional;
+
+import static org.apache.accumulo.test.TestIngest.generateRow;
+import static 
org.apache.accumulo.test.compaction.ExternalCompactionTestUtils.countTablets;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.time.Duration;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+import org.apache.accumulo.core.client.Accumulo;
+import org.apache.accumulo.core.client.AccumuloClient;
+import org.apache.accumulo.core.client.BatchWriter;
+import org.apache.accumulo.core.client.admin.CompactionConfig;
+import org.apache.accumulo.core.client.admin.NewTableConfiguration;
+import org.apache.accumulo.core.client.admin.TabletAvailability;
+import org.apache.accumulo.core.client.admin.TabletMergeability;
+import org.apache.accumulo.core.clientImpl.TabletMergeabilityUtil;
+import org.apache.accumulo.core.conf.Property;
+import org.apache.accumulo.core.data.Mutation;
+import org.apache.accumulo.core.data.Range;
+import org.apache.accumulo.core.data.TableId;
+import org.apache.accumulo.core.data.Value;
+import org.apache.accumulo.core.dataImpl.KeyExtent;
+import org.apache.accumulo.core.metadata.schema.TabletMetadata;
+import org.apache.accumulo.core.security.Authorizations;
+import org.apache.accumulo.harness.MiniClusterConfigurationCallback;
+import org.apache.accumulo.harness.SharedMiniClusterBase;
+import org.apache.accumulo.miniclusterImpl.MiniAccumuloConfigImpl;
+import org.apache.accumulo.server.ServerContext;
+import org.apache.accumulo.test.TestIngest;
+import org.apache.accumulo.test.VerifyIngest;
+import org.apache.accumulo.test.VerifyIngest.VerifyParams;
+import org.apache.accumulo.test.util.Wait;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.io.Text;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+public class TabletMergeabilityIT extends SharedMiniClusterBase {
+
+  @Override
+  protected Duration defaultTimeout() {
+    return Duration.ofMinutes(5);
+  }
+
+  @BeforeAll
+  public static void setup() throws Exception {
+    SharedMiniClusterBase.startMiniClusterWithConfig(new Callback());
+  }
+
+  @AfterAll
+  public static void teardown() {
+    SharedMiniClusterBase.stopMiniCluster();
+  }
+
+  private static class Callback implements MiniClusterConfigurationCallback {
+    @Override
+    public void configureMiniCluster(MiniAccumuloConfigImpl cfg, Configuration 
coreSite) {
+      // Configure a short period of time to run the auto merge thread for 
testing
+      cfg.setProperty(Property.MANAGER_TABLET_MERGEABILITY_INTERVAL, "3s");
+    }
+  }
+
+  @Test
+  public void testMergeabilityAll() throws Exception {
+    String tableName = getUniqueNames(1)[0];
+    try (AccumuloClient c = 
Accumulo.newClient().from(getClientProps()).build()) {
+      c.tableOperations().create(tableName);
+      var tableId = 
TableId.of(c.tableOperations().tableIdMap().get(tableName));
+
+      TreeSet<Text> splits = new TreeSet<>();
+      splits.add(new Text(String.format("%09d", 333)));
+      splits.add(new Text(String.format("%09d", 666)));
+      splits.add(new Text(String.format("%09d", 999)));
+
+      // create splits with mergeabilty disabled so the task does not merge 
them away
+      // The default tablet is always mergeable, but it is currently the only 
one that is mergeable,
+      // so nothing will merge
+      c.tableOperations().putSplits(tableName, 
TabletMergeabilityUtil.userDefaultSplits(splits));
+      Wait.waitFor(() -> countTablets(getCluster().getServerContext(), 
tableName, tm -> true) == 4,
+          5000, 200);
+
+      // update to always mergeable so the task can now merge tablets
+      c.tableOperations().putSplits(tableName, 
TabletMergeabilityUtil.systemDefaultSplits(splits));
+
+      // Wait for merge, we should have one tablet
+      Wait.waitFor(() -> hasExactTablets(getCluster().getServerContext(), 
tableId,
+          Set.of(new KeyExtent(tableId, null, null))), 10000, 200);
+
+    }
+  }
+
+  @Test
+  public void testMergeabilityMultipleRanges() throws Exception {
+    String tableName = getUniqueNames(1)[0];
+    try (AccumuloClient c = 
Accumulo.newClient().from(getClientProps()).build()) {
+      c.tableOperations().create(tableName);
+      var tableId = 
TableId.of(c.tableOperations().tableIdMap().get(tableName));
+
+      SortedMap<Text,TabletMergeability> splits = new TreeMap<>();
+      splits.put(new Text(String.format("%09d", 333)), 
TabletMergeability.never());
+      splits.put(new Text(String.format("%09d", 555)), 
TabletMergeability.never());
+      splits.put(new Text(String.format("%09d", 666)), 
TabletMergeability.never());
+      splits.put(new Text(String.format("%09d", 999)), 
TabletMergeability.never());
+
+      c.tableOperations().putSplits(tableName, splits);
+      Wait.waitFor(() -> countTablets(getCluster().getServerContext(), 
tableName, tm -> true) == 5,
+          5000, 500);
+
+      splits.put(new Text(String.format("%09d", 333)), 
TabletMergeability.always());
+      splits.put(new Text(String.format("%09d", 555)), 
TabletMergeability.always());
+      // Keep tablet 666 as never, this should cause two fate jobs for merging
+      splits.put(new Text(String.format("%09d", 999)), 
TabletMergeability.always());
+      c.tableOperations().putSplits(tableName, splits);
+
+      // Wait for merge, we should have 3 tablets
+      // 333 and 555 should be merged into 555
+      // 666
+      // 999 and default merged into default
+      Wait.waitFor(() -> hasExactTablets(getCluster().getServerContext(), 
tableId,
+          Set.of(new KeyExtent(tableId, new Text(String.format("%09d", 555)), 
null),
+              new KeyExtent(tableId, new Text(String.format("%09d", 666)),
+                  new Text(String.format("%09d", 555))),
+              new KeyExtent(tableId, null, new Text(String.format("%09d", 
666))))),
+          10000, 200);
+
+    }
+  }
+
+  @Test
+  public void testMergeabilityThresholdMultipleRanges() throws Exception {
+    String tableName = getUniqueNames(1)[0];
+    try (AccumuloClient c = 
Accumulo.newClient().from(getClientProps()).build()) {
+      Map<String,String> props = new HashMap<>();
+      props.put(Property.TABLE_SPLIT_THRESHOLD.getKey(), "32K");
+      props.put(Property.TABLE_MAX_MERGEABILITY_THRESHOLD.getKey(), ".5");
+      c.tableOperations().create(tableName, new NewTableConfiguration()
+          
.withInitialTabletAvailability(TabletAvailability.HOSTED).setProperties(props));
+      var tableId = 
TableId.of(c.tableOperations().tableIdMap().get(tableName));
+
+      SortedMap<Text,TabletMergeability> splits = new TreeMap<>();
+      // Create new tablets that won't merge automatically
+      for (int i = 10000; i <= 90000; i += 10000) {
+        splits.put(row(i), TabletMergeability.never());
+      }
+
+      c.tableOperations().putSplits(tableName, splits);
+      // Verify we now have 10 tablets
+      // [row_0000010000, row_0000020000, row_0000030000, row_0000040000, 
row_0000050000,
+      // row_0000060000, row_0000070000, row_0000080000, row_0000090000, 
default]
+      Wait.waitFor(() -> countTablets(getCluster().getServerContext(), 
tableName, tm -> true) == 10,
+          5000, 500);
+
+      // Insert rows into each tablet with different numbers of rows
+      // Tablets with end rows row_0000020000 - row_0000040000, row_0000060000 
- row_0000080000,
+      // default will have 1000 rows
+      // Tablets with end rows row_0000010000, row_0000050000, row_0000090000 
will have 5000 rows
+      try (BatchWriter bw = c.createBatchWriter(tableName)) {
+        final var value = StringUtils.repeat("a", 1024);
+        for (int i = 0; i < 100000; i += 10000) {
+          var rows = 1000;
+          if (i % 40000 == 0) {
+            rows = 5000;
+          }
+          for (int j = 0; j < rows; j++) {
+            Mutation m = new Mutation(row(i + j));
+            m.put(new Text("cf1"), new Text("cq1"), new Value(value));
+            bw.addMutation(m);
+          }
+        }
+      }
+      c.tableOperations().flush(tableName, null, null, true);
+
+      // Set all 10 tablets to be auto-mergeable
+      for (int i = 10000; i <= 90000; i += 10000) {
+        splits.put(row(i), TabletMergeability.always());
+      }
+      c.tableOperations().putSplits(tableName, splits);
+
+      // With the mergeability threshold set to 50% of 32KB we should be able 
to merge together
+      // the tablets with 1000 rows, but not 5000 rows. This should produce 
the following
+      // 6 tablets after merger.
+      Wait.waitFor(() -> hasExactTablets(getCluster().getServerContext(), 
tableId,
+          Set.of(new KeyExtent(tableId, row(10000), null),
+              new KeyExtent(tableId, row(40000), row(10000)),
+              new KeyExtent(tableId, row(50000), row(40000)),
+              new KeyExtent(tableId, row(80000), row(50000)),
+              new KeyExtent(tableId, row(90000), row(80000)),
+              new KeyExtent(tableId, null, row(90000)))),
+          10000, 200);
+    }
+  }
+
+  @Test
+  public void testSplitAndMergeAll() throws Exception {
+    String tableName = getUniqueNames(1)[0];
+    try (AccumuloClient c = 
Accumulo.newClient().from(getClientProps()).build()) {
+      Map<String,String> props = new HashMap<>();
+      props.put(Property.TABLE_SPLIT_THRESHOLD.getKey(), "16K");
+      props.put(Property.TABLE_FILE_COMPRESSED_BLOCK_SIZE.getKey(), "1K");
+      c.tableOperations().create(tableName, new 
NewTableConfiguration().setProperties(props)
+          .withInitialTabletAvailability(TabletAvailability.HOSTED));
+      var tableId = 
TableId.of(c.tableOperations().tableIdMap().get(tableName));
+
+      // Ingest data so tablet will split
+      VerifyParams params = new VerifyParams(getClientProps(), tableName, 
5_000);
+      TestIngest.ingest(c, params);
+      c.tableOperations().flush(tableName);
+      VerifyIngest.verifyIngest(c, params);
+
+      // Wait for table to split, should be more than 10 tablets
+      Wait.waitFor(() -> c.tableOperations().listSplits(tableName).size() > 
10, 30000, 200);
+
+      // Delete all the data - We can't use deleteRows() as that would merge 
empty tablets
+      // Instead, we want the mergeability thread to merge so use a batch 
deleter and
+      // compact away the deleted data
+      var bd = c.createBatchDeleter(tableName, Authorizations.EMPTY, 1);
+      bd.setRanges(List.of(new Range()));
+      bd.delete();
+      c.tableOperations().compact(tableName, new 
CompactionConfig().setFlush(true));
+
+      // Wait for merge back to default tablet
+      Wait.waitFor(() -> hasExactTablets(getCluster().getServerContext(), 
tableId,
+          Set.of(new KeyExtent(tableId, null, null))), 30000, 200);
+    }
+  }
+
+  @Test
+  public void testMergeabilityThreshold() throws Exception {
+    String tableName = getUniqueNames(1)[0];
+    try (AccumuloClient c = 
Accumulo.newClient().from(getClientProps()).build()) {
+      Map<String,String> props = new HashMap<>();
+      props.put(Property.TABLE_SPLIT_THRESHOLD.getKey(), "16K");
+      props.put(Property.TABLE_FILE_COMPRESSED_BLOCK_SIZE.getKey(), "1K");
+      // Set a low threshold to 1% of the split threshold
+      props.put(Property.TABLE_MAX_MERGEABILITY_THRESHOLD.getKey(), ".01");
+      c.tableOperations().create(tableName, new 
NewTableConfiguration().setProperties(props)
+          .withInitialTabletAvailability(TabletAvailability.HOSTED));
+      var tableId = 
TableId.of(c.tableOperations().tableIdMap().get(tableName));
+
+      // Ingest data so tablet will split
+      VerifyParams params = new VerifyParams(getClientProps(), tableName, 
5_000);
+      TestIngest.ingest(c, params);
+      c.tableOperations().flush(tableName);
+      VerifyIngest.verifyIngest(c, params);
+
+      // Wait for table to split, should be more than 10 tablets
+      Wait.waitFor(() -> c.tableOperations().listSplits(tableName).size() > 
10, 10000, 200);
+
+      // Set the split threshold back to the default of 5 MB. There's not a 
lot of data so normally
+      // we could merge back to 1 tablet, but the threshold is too low at 1% 
so it should not merge
+      // yet.
+      c.tableOperations().setProperty(tableName, 
Property.TABLE_SPLIT_THRESHOLD.getKey(), "5m");
+
+      // Should not merge so make sure it throws IllegalStateException
+      assertThrows(IllegalStateException.class,
+          () -> Wait.waitFor(() -> 
hasExactTablets(getCluster().getServerContext(), tableId,
+              Set.of(new KeyExtent(tableId, null, null))), 5000, 500));
+      // Make sure we failed because of exact tablets and not a different 
IllegalStateException
+      assertFalse(hasExactTablets(getCluster().getServerContext(), tableId,
+          Set.of(new KeyExtent(tableId, null, null))));
+
+      // With a 10% threshold we should be able to merge
+      c.tableOperations().setProperty(tableName, 
Property.TABLE_MAX_MERGEABILITY_THRESHOLD.getKey(),
+          ".1");
+
+      // Wait for merge back to default tablet
+      Wait.waitFor(() -> hasExactTablets(getCluster().getServerContext(), 
tableId,
+          Set.of(new KeyExtent(tableId, null, null))), 10000, 200);
+    }
+  }
+
+  @Test
+  public void testMergeAfter() throws Exception {
+    String tableName = getUniqueNames(1)[0];
+    try (AccumuloClient c = 
Accumulo.newClient().from(getClientProps()).build()) {
+      c.tableOperations().create(tableName);
+      var tableId = 
TableId.of(c.tableOperations().tableIdMap().get(tableName));
+
+      TreeSet<Text> splits = new TreeSet<>();
+      splits.add(new Text(String.format("%09d", 333)));
+      splits.add(new Text(String.format("%09d", 666)));
+      splits.add(new Text(String.format("%09d", 999)));
+
+      var delay = Duration.ofSeconds(5);
+      var startTime = c.instanceOperations().getManagerTime();
+      c.tableOperations().putSplits(tableName, 
TabletMergeabilityUtil.splitsWithDefault(splits,
+          TabletMergeability.after(Duration.ofSeconds(5))));
+
+      Wait.waitFor(() -> countTablets(getCluster().getServerContext(), 
tableName, tm -> true) == 4,
+          5000, 200);
+
+      // Wait for merge back to default tablet
+      Wait.waitFor(() -> hasExactTablets(getCluster().getServerContext(), 
tableId,
+          Set.of(new KeyExtent(tableId, null, null))), 10000, 200);
+
+      var elapsed = c.instanceOperations().getManagerTime().minus(startTime);
+      assertTrue(elapsed.compareTo(delay) > 0);
+    }
+  }
+
+  @Test
+  public void testMergeabilityMaxFiles() throws Exception {
+    String tableName = getUniqueNames(1)[0];
+    try (AccumuloClient c = 
Accumulo.newClient().from(getClientProps()).build()) {
+      Map<String,String> props = new HashMap<>();
+      // disable compactions and set a low merge file max
+      props.put(Property.TABLE_MAJC_RATIO.getKey(), "9999");
+      props.put(Property.TABLE_MERGE_FILE_MAX.getKey(), "3");
+      c.tableOperations().create(tableName, new 
NewTableConfiguration().setProperties(props)
+          .withInitialTabletAvailability(TabletAvailability.HOSTED));
+      var tableId = 
TableId.of(c.tableOperations().tableIdMap().get(tableName));
+
+      // Create new tablets that won't merge automatically
+      SortedMap<Text,TabletMergeability> splits = new TreeMap<>();
+      for (int i = 500; i < 5000; i += 500) {
+        splits.put(row(i), TabletMergeability.never());
+      }
+      c.tableOperations().putSplits(tableName, splits);
+
+      // Verify we now have 10 tablets
+      Wait.waitFor(() -> countTablets(getCluster().getServerContext(), 
tableName, tm -> true) == 10,
+          5000, 500);
+
+      // Ingest data so tablet will split, each tablet will have several files 
because
+      // of the flush setting
+      VerifyParams params = new VerifyParams(getClientProps(), tableName, 
5_000);
+      params.startRow = 0;
+      params.flushAfterRows = 100;
+      TestIngest.ingest(c, params);
+      VerifyIngest.verifyIngest(c, params);
+

Review Comment:
   Could add a check of the number of files here.  Maybe it should do a waitFor 
instead of assertTrue though.
   
   ```
   assertTrue(FileMetadataUtil.countFiles(getCluster().getServerContext(), 
tableName) > 3);
   ```



##########
test/src/main/java/org/apache/accumulo/test/functional/TabletMergeabilityIT.java:
##########
@@ -0,0 +1,406 @@
+/*
+ * 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.test.functional;
+
+import static org.apache.accumulo.test.TestIngest.generateRow;
+import static 
org.apache.accumulo.test.compaction.ExternalCompactionTestUtils.countTablets;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.time.Duration;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+import org.apache.accumulo.core.client.Accumulo;
+import org.apache.accumulo.core.client.AccumuloClient;
+import org.apache.accumulo.core.client.BatchWriter;
+import org.apache.accumulo.core.client.admin.CompactionConfig;
+import org.apache.accumulo.core.client.admin.NewTableConfiguration;
+import org.apache.accumulo.core.client.admin.TabletAvailability;
+import org.apache.accumulo.core.client.admin.TabletMergeability;
+import org.apache.accumulo.core.clientImpl.TabletMergeabilityUtil;
+import org.apache.accumulo.core.conf.Property;
+import org.apache.accumulo.core.data.Mutation;
+import org.apache.accumulo.core.data.Range;
+import org.apache.accumulo.core.data.TableId;
+import org.apache.accumulo.core.data.Value;
+import org.apache.accumulo.core.dataImpl.KeyExtent;
+import org.apache.accumulo.core.metadata.schema.TabletMetadata;
+import org.apache.accumulo.core.security.Authorizations;
+import org.apache.accumulo.harness.MiniClusterConfigurationCallback;
+import org.apache.accumulo.harness.SharedMiniClusterBase;
+import org.apache.accumulo.miniclusterImpl.MiniAccumuloConfigImpl;
+import org.apache.accumulo.server.ServerContext;
+import org.apache.accumulo.test.TestIngest;
+import org.apache.accumulo.test.VerifyIngest;
+import org.apache.accumulo.test.VerifyIngest.VerifyParams;
+import org.apache.accumulo.test.util.Wait;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.io.Text;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+public class TabletMergeabilityIT extends SharedMiniClusterBase {
+
+  @Override
+  protected Duration defaultTimeout() {
+    return Duration.ofMinutes(5);
+  }
+
+  @BeforeAll
+  public static void setup() throws Exception {
+    SharedMiniClusterBase.startMiniClusterWithConfig(new Callback());
+  }
+
+  @AfterAll
+  public static void teardown() {
+    SharedMiniClusterBase.stopMiniCluster();
+  }
+
+  private static class Callback implements MiniClusterConfigurationCallback {
+    @Override
+    public void configureMiniCluster(MiniAccumuloConfigImpl cfg, Configuration 
coreSite) {
+      // Configure a short period of time to run the auto merge thread for 
testing
+      cfg.setProperty(Property.MANAGER_TABLET_MERGEABILITY_INTERVAL, "3s");
+    }
+  }
+
+  @Test
+  public void testMergeabilityAll() throws Exception {
+    String tableName = getUniqueNames(1)[0];
+    try (AccumuloClient c = 
Accumulo.newClient().from(getClientProps()).build()) {
+      c.tableOperations().create(tableName);
+      var tableId = 
TableId.of(c.tableOperations().tableIdMap().get(tableName));
+
+      TreeSet<Text> splits = new TreeSet<>();
+      splits.add(new Text(String.format("%09d", 333)));
+      splits.add(new Text(String.format("%09d", 666)));
+      splits.add(new Text(String.format("%09d", 999)));
+
+      // create splits with mergeabilty disabled so the task does not merge 
them away
+      // The default tablet is always mergeable, but it is currently the only 
one that is mergeable,
+      // so nothing will merge
+      c.tableOperations().putSplits(tableName, 
TabletMergeabilityUtil.userDefaultSplits(splits));
+      Wait.waitFor(() -> countTablets(getCluster().getServerContext(), 
tableName, tm -> true) == 4,
+          5000, 200);
+
+      // update to always mergeable so the task can now merge tablets
+      c.tableOperations().putSplits(tableName, 
TabletMergeabilityUtil.systemDefaultSplits(splits));
+
+      // Wait for merge, we should have one tablet
+      Wait.waitFor(() -> hasExactTablets(getCluster().getServerContext(), 
tableId,
+          Set.of(new KeyExtent(tableId, null, null))), 10000, 200);
+
+    }
+  }
+
+  @Test
+  public void testMergeabilityMultipleRanges() throws Exception {
+    String tableName = getUniqueNames(1)[0];
+    try (AccumuloClient c = 
Accumulo.newClient().from(getClientProps()).build()) {
+      c.tableOperations().create(tableName);
+      var tableId = 
TableId.of(c.tableOperations().tableIdMap().get(tableName));
+
+      SortedMap<Text,TabletMergeability> splits = new TreeMap<>();
+      splits.put(new Text(String.format("%09d", 333)), 
TabletMergeability.never());
+      splits.put(new Text(String.format("%09d", 555)), 
TabletMergeability.never());
+      splits.put(new Text(String.format("%09d", 666)), 
TabletMergeability.never());
+      splits.put(new Text(String.format("%09d", 999)), 
TabletMergeability.never());
+
+      c.tableOperations().putSplits(tableName, splits);
+      Wait.waitFor(() -> countTablets(getCluster().getServerContext(), 
tableName, tm -> true) == 5,
+          5000, 500);
+
+      splits.put(new Text(String.format("%09d", 333)), 
TabletMergeability.always());
+      splits.put(new Text(String.format("%09d", 555)), 
TabletMergeability.always());
+      // Keep tablet 666 as never, this should cause two fate jobs for merging
+      splits.put(new Text(String.format("%09d", 999)), 
TabletMergeability.always());
+      c.tableOperations().putSplits(tableName, splits);
+
+      // Wait for merge, we should have 3 tablets
+      // 333 and 555 should be merged into 555
+      // 666
+      // 999 and default merged into default
+      Wait.waitFor(() -> hasExactTablets(getCluster().getServerContext(), 
tableId,
+          Set.of(new KeyExtent(tableId, new Text(String.format("%09d", 555)), 
null),
+              new KeyExtent(tableId, new Text(String.format("%09d", 666)),
+                  new Text(String.format("%09d", 555))),
+              new KeyExtent(tableId, null, new Text(String.format("%09d", 
666))))),
+          10000, 200);
+
+    }
+  }
+
+  @Test
+  public void testMergeabilityThresholdMultipleRanges() throws Exception {
+    String tableName = getUniqueNames(1)[0];
+    try (AccumuloClient c = 
Accumulo.newClient().from(getClientProps()).build()) {
+      Map<String,String> props = new HashMap<>();
+      props.put(Property.TABLE_SPLIT_THRESHOLD.getKey(), "32K");
+      props.put(Property.TABLE_MAX_MERGEABILITY_THRESHOLD.getKey(), ".5");
+      c.tableOperations().create(tableName, new NewTableConfiguration()
+          
.withInitialTabletAvailability(TabletAvailability.HOSTED).setProperties(props));
+      var tableId = 
TableId.of(c.tableOperations().tableIdMap().get(tableName));
+
+      SortedMap<Text,TabletMergeability> splits = new TreeMap<>();
+      // Create new tablets that won't merge automatically
+      for (int i = 10000; i <= 90000; i += 10000) {
+        splits.put(row(i), TabletMergeability.never());
+      }
+
+      c.tableOperations().putSplits(tableName, splits);
+      // Verify we now have 10 tablets
+      // [row_0000010000, row_0000020000, row_0000030000, row_0000040000, 
row_0000050000,
+      // row_0000060000, row_0000070000, row_0000080000, row_0000090000, 
default]
+      Wait.waitFor(() -> countTablets(getCluster().getServerContext(), 
tableName, tm -> true) == 10,
+          5000, 500);
+
+      // Insert rows into each tablet with different numbers of rows
+      // Tablets with end rows row_0000020000 - row_0000040000, row_0000060000 
- row_0000080000,
+      // default will have 1000 rows
+      // Tablets with end rows row_0000010000, row_0000050000, row_0000090000 
will have 5000 rows
+      try (BatchWriter bw = c.createBatchWriter(tableName)) {
+        final var value = StringUtils.repeat("a", 1024);
+        for (int i = 0; i < 100000; i += 10000) {
+          var rows = 1000;
+          if (i % 40000 == 0) {
+            rows = 5000;
+          }
+          for (int j = 0; j < rows; j++) {
+            Mutation m = new Mutation(row(i + j));
+            m.put(new Text("cf1"), new Text("cq1"), new Value(value));
+            bw.addMutation(m);
+          }
+        }
+      }
+      c.tableOperations().flush(tableName, null, null, true);
+
+      // Set all 10 tablets to be auto-mergeable
+      for (int i = 10000; i <= 90000; i += 10000) {
+        splits.put(row(i), TabletMergeability.always());
+      }
+      c.tableOperations().putSplits(tableName, splits);
+
+      // With the mergeability threshold set to 50% of 32KB we should be able 
to merge together
+      // the tablets with 1000 rows, but not 5000 rows. This should produce 
the following
+      // 6 tablets after merger.
+      Wait.waitFor(() -> hasExactTablets(getCluster().getServerContext(), 
tableId,
+          Set.of(new KeyExtent(tableId, row(10000), null),
+              new KeyExtent(tableId, row(40000), row(10000)),
+              new KeyExtent(tableId, row(50000), row(40000)),
+              new KeyExtent(tableId, row(80000), row(50000)),
+              new KeyExtent(tableId, row(90000), row(80000)),
+              new KeyExtent(tableId, null, row(90000)))),
+          10000, 200);
+    }
+  }
+
+  @Test
+  public void testSplitAndMergeAll() throws Exception {
+    String tableName = getUniqueNames(1)[0];
+    try (AccumuloClient c = 
Accumulo.newClient().from(getClientProps()).build()) {
+      Map<String,String> props = new HashMap<>();
+      props.put(Property.TABLE_SPLIT_THRESHOLD.getKey(), "16K");
+      props.put(Property.TABLE_FILE_COMPRESSED_BLOCK_SIZE.getKey(), "1K");
+      c.tableOperations().create(tableName, new 
NewTableConfiguration().setProperties(props)
+          .withInitialTabletAvailability(TabletAvailability.HOSTED));
+      var tableId = 
TableId.of(c.tableOperations().tableIdMap().get(tableName));
+
+      // Ingest data so tablet will split
+      VerifyParams params = new VerifyParams(getClientProps(), tableName, 
5_000);
+      TestIngest.ingest(c, params);
+      c.tableOperations().flush(tableName);
+      VerifyIngest.verifyIngest(c, params);
+
+      // Wait for table to split, should be more than 10 tablets
+      Wait.waitFor(() -> c.tableOperations().listSplits(tableName).size() > 
10, 30000, 200);
+
+      // Delete all the data - We can't use deleteRows() as that would merge 
empty tablets
+      // Instead, we want the mergeability thread to merge so use a batch 
deleter and
+      // compact away the deleted data
+      var bd = c.createBatchDeleter(tableName, Authorizations.EMPTY, 1);
+      bd.setRanges(List.of(new Range()));
+      bd.delete();
+      c.tableOperations().compact(tableName, new 
CompactionConfig().setFlush(true));
+
+      // Wait for merge back to default tablet
+      Wait.waitFor(() -> hasExactTablets(getCluster().getServerContext(), 
tableId,
+          Set.of(new KeyExtent(tableId, null, null))), 30000, 200);
+    }
+  }
+
+  @Test
+  public void testMergeabilityThreshold() throws Exception {
+    String tableName = getUniqueNames(1)[0];
+    try (AccumuloClient c = 
Accumulo.newClient().from(getClientProps()).build()) {
+      Map<String,String> props = new HashMap<>();
+      props.put(Property.TABLE_SPLIT_THRESHOLD.getKey(), "16K");
+      props.put(Property.TABLE_FILE_COMPRESSED_BLOCK_SIZE.getKey(), "1K");
+      // Set a low threshold to 1% of the split threshold
+      props.put(Property.TABLE_MAX_MERGEABILITY_THRESHOLD.getKey(), ".01");
+      c.tableOperations().create(tableName, new 
NewTableConfiguration().setProperties(props)
+          .withInitialTabletAvailability(TabletAvailability.HOSTED));
+      var tableId = 
TableId.of(c.tableOperations().tableIdMap().get(tableName));
+
+      // Ingest data so tablet will split
+      VerifyParams params = new VerifyParams(getClientProps(), tableName, 
5_000);
+      TestIngest.ingest(c, params);
+      c.tableOperations().flush(tableName);
+      VerifyIngest.verifyIngest(c, params);
+
+      // Wait for table to split, should be more than 10 tablets
+      Wait.waitFor(() -> c.tableOperations().listSplits(tableName).size() > 
10, 10000, 200);
+
+      // Set the split threshold back to the default of 5 MB. There's not a 
lot of data so normally
+      // we could merge back to 1 tablet, but the threshold is too low at 1% 
so it should not merge
+      // yet.
+      c.tableOperations().setProperty(tableName, 
Property.TABLE_SPLIT_THRESHOLD.getKey(), "5m");
+
+      // Should not merge so make sure it throws IllegalStateException
+      assertThrows(IllegalStateException.class,
+          () -> Wait.waitFor(() -> 
hasExactTablets(getCluster().getServerContext(), tableId,
+              Set.of(new KeyExtent(tableId, null, null))), 5000, 500));
+      // Make sure we failed because of exact tablets and not a different 
IllegalStateException
+      assertFalse(hasExactTablets(getCluster().getServerContext(), tableId,
+          Set.of(new KeyExtent(tableId, null, null))));
+
+      // With a 10% threshold we should be able to merge
+      c.tableOperations().setProperty(tableName, 
Property.TABLE_MAX_MERGEABILITY_THRESHOLD.getKey(),
+          ".1");
+
+      // Wait for merge back to default tablet
+      Wait.waitFor(() -> hasExactTablets(getCluster().getServerContext(), 
tableId,
+          Set.of(new KeyExtent(tableId, null, null))), 10000, 200);
+    }
+  }
+
+  @Test
+  public void testMergeAfter() throws Exception {
+    String tableName = getUniqueNames(1)[0];
+    try (AccumuloClient c = 
Accumulo.newClient().from(getClientProps()).build()) {
+      c.tableOperations().create(tableName);
+      var tableId = 
TableId.of(c.tableOperations().tableIdMap().get(tableName));
+
+      TreeSet<Text> splits = new TreeSet<>();
+      splits.add(new Text(String.format("%09d", 333)));
+      splits.add(new Text(String.format("%09d", 666)));
+      splits.add(new Text(String.format("%09d", 999)));
+
+      var delay = Duration.ofSeconds(5);
+      var startTime = c.instanceOperations().getManagerTime();
+      c.tableOperations().putSplits(tableName, 
TabletMergeabilityUtil.splitsWithDefault(splits,
+          TabletMergeability.after(Duration.ofSeconds(5))));
+
+      Wait.waitFor(() -> countTablets(getCluster().getServerContext(), 
tableName, tm -> true) == 4,
+          5000, 200);
+
+      // Wait for merge back to default tablet
+      Wait.waitFor(() -> hasExactTablets(getCluster().getServerContext(), 
tableId,
+          Set.of(new KeyExtent(tableId, null, null))), 10000, 200);
+
+      var elapsed = c.instanceOperations().getManagerTime().minus(startTime);
+      assertTrue(elapsed.compareTo(delay) > 0);
+    }
+  }
+
+  @Test
+  public void testMergeabilityMaxFiles() throws Exception {
+    String tableName = getUniqueNames(1)[0];
+    try (AccumuloClient c = 
Accumulo.newClient().from(getClientProps()).build()) {
+      Map<String,String> props = new HashMap<>();
+      // disable compactions and set a low merge file max
+      props.put(Property.TABLE_MAJC_RATIO.getKey(), "9999");
+      props.put(Property.TABLE_MERGE_FILE_MAX.getKey(), "3");
+      c.tableOperations().create(tableName, new 
NewTableConfiguration().setProperties(props)
+          .withInitialTabletAvailability(TabletAvailability.HOSTED));
+      var tableId = 
TableId.of(c.tableOperations().tableIdMap().get(tableName));
+
+      // Create new tablets that won't merge automatically
+      SortedMap<Text,TabletMergeability> splits = new TreeMap<>();
+      for (int i = 500; i < 5000; i += 500) {
+        splits.put(row(i), TabletMergeability.never());
+      }
+      c.tableOperations().putSplits(tableName, splits);
+
+      // Verify we now have 10 tablets
+      Wait.waitFor(() -> countTablets(getCluster().getServerContext(), 
tableName, tm -> true) == 10,
+          5000, 500);
+
+      // Ingest data so tablet will split, each tablet will have several files 
because
+      // of the flush setting
+      VerifyParams params = new VerifyParams(getClientProps(), tableName, 
5_000);
+      params.startRow = 0;
+      params.flushAfterRows = 100;
+      TestIngest.ingest(c, params);
+      VerifyIngest.verifyIngest(c, params);
+
+      // Mark all tablets as mergeable
+      for (int i = 500; i < 5000; i += 500) {
+        splits.put(row(i), TabletMergeability.always());
+      }
+      c.tableOperations().putSplits(tableName, splits);
+
+      // Should not merge as we set max file count to only 3 and there are 
more files than that
+      // per tablet, so make sure it throws IllegalStateException
+      assertThrows(IllegalStateException.class,
+          () -> Wait.waitFor(() -> 
hasExactTablets(getCluster().getServerContext(), tableId,
+              Set.of(new KeyExtent(tableId, null, null))), 5000, 500));
+      // Make sure tablets is still 10, not merged
+      assertEquals(10, countTablets(getCluster().getServerContext(), 
tableName, tm -> true));
+
+      // Set max merge file count back to default of 10k
+      c.tableOperations().setProperty(tableName, 
Property.TABLE_MERGE_FILE_MAX.getKey(), "10000");
+
+      // Should merge back to 1 tablet
+      Wait.waitFor(() -> hasExactTablets(getCluster().getServerContext(), 
tableId,
+          Set.of(new KeyExtent(tableId, null, null))), 10000, 200);

Review Comment:
   Can re-verify the data after merge.  There are a few other test were the 
data could be verified again after the merges.
   
   ```suggestion
             Set.of(new KeyExtent(tableId, null, null))), 10000, 200);
             
         VerifyIngest.verifyIngest(c, params);
   ```



##########
server/manager/src/main/java/org/apache/accumulo/manager/tableOps/merge/VerifyMergeability.java:
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.tableOps.merge;
+
+import static 
org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType.FILES;
+import static 
org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType.MERGEABILITY;
+
+import org.apache.accumulo.core.conf.Property;
+import org.apache.accumulo.core.fate.FateId;
+import org.apache.accumulo.core.fate.Repo;
+import org.apache.accumulo.manager.Manager;
+import org.apache.accumulo.manager.tableOps.ManagerRepo;
+import org.apache.accumulo.manager.tableOps.merge.MergeInfo.Operation;
+import org.apache.accumulo.manager.tableOps.merge.UnreserveAndError.Reason;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Preconditions;
+
+public class VerifyMergeability extends ManagerRepo {
+  private static final Logger log = 
LoggerFactory.getLogger(VerifyMergeability.class);
+  private static final long serialVersionUID = 1L;
+  private final MergeInfo data;
+
+  public VerifyMergeability(MergeInfo mergeInfo) {
+    this.data = mergeInfo;
+    Preconditions.checkArgument(data.op == Operation.SYSTEM_MERGE, "Must be a 
System Merge");
+  }
+
+  @Override
+  public Repo<Manager> call(FateId fateId, Manager env) throws Exception {
+
+    var range = data.getReserveExtent();
+
+    var currentTime = env.getSteadyTime();
+    var context = env.getContext();
+    var tableConf = context.getTableConfiguration(data.tableId);
+    var splitThreshold = tableConf.getAsBytes(Property.TABLE_SPLIT_THRESHOLD);
+    var maxMergeabilityThreshold = 
tableConf.getFraction(Property.TABLE_MAX_MERGEABILITY_THRESHOLD);
+
+    // max percentage of split threshold
+    long maxTotalSize = (long) (splitThreshold * maxMergeabilityThreshold);
+
+    long maxFiles = 
env.getContext().getTableConfiguration(data.getOriginalExtent().tableId())
+        .getCount(Property.TABLE_MERGE_FILE_MAX);
+
+    log.debug("Validating system merge for {} with range {}", fateId, range);
+
+    try (var tablets = 
env.getContext().getAmple().readTablets().forTable(data.tableId)
+        .overlapping(range.prevEndRow(), range.endRow()).fetch(FILES, 
MERGEABILITY)
+        .checkConsistency().build()) {
+
+      long totalSize = 0;
+      long totalFiles = 0;
+      int totalUnMergeable = 0;
+
+      for (var tabletMetadata : tablets) {

Review Comment:
   That turned out nicely.



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