cshannon commented on code in PR #5353: URL: https://github.com/apache/accumulo/pull/5353#discussion_r1975848148
########## server/manager/src/main/java/org/apache/accumulo/manager/merge/FindMergeableRangeTask.java: ########## @@ -0,0 +1,233 @@ +/* + * 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 java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.Set; +import java.util.function.Predicate; + +import org.apache.accumulo.core.clientImpl.thrift.TableOperation; +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.fate.Fate.FateOperation; +import org.apache.accumulo.core.fate.FateInstanceType; +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 maxFileCount = + context.getTableConfiguration(tableId).getCount(Property.TABLE_MERGE_FILE_MAX); + long threshold = + context.getTableConfiguration(tableId).getAsBytes(Property.TABLE_SPLIT_THRESHOLD); + double mergeabilityThreshold = context.getTableConfiguration(tableId) + .getFraction(Property.TABLE_MAX_MERGEABILITY_THRESHOLD); + 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(manager.getSteadyTime(), maxFileCount, maxTotalSize); + + for (var tm : tablets) { + log.trace("Checking tablet {}, {}", tm.getExtent(), tm.getTabletMergeability()); + if (!current.add(tm)) { + submit(current, type, table, namespaceId); + current.resetAndAdd(tm); + } + } + + submit(current, type, table, namespaceId); + } + + } catch (Exception e) { + log.error("Failed to generate system merges for {}", tableName, e); + } + } + + } + + void submit(MergeableRange range, FateInstanceType type, Entry<TableId,String> table, + NamespaceId namespaceId) { + if (range.tabletCount < 2) { + return; + } + + log.debug("Table {} found {} tablets that can be merged for table", table.getValue(), + range.tabletCount); + + TableId tableId = table.getKey(); + String tableName = table.getValue(); + + final Text startRow = range.startRow != null ? range.startRow : new Text(""); + final Text endRow = range.endRow != null ? range.endRow : new Text(""); + + String startRowStr = StringUtils.defaultIfBlank(startRow.toString(), "-inf"); + String endRowStr = StringUtils.defaultIfBlank(endRow.toString(), "+inf"); + log.debug("FindMergeableRangeTask: Creating merge op: {} from startRow: {} to endRow: {}", + tableId, startRowStr, endRowStr); + var fateId = manager.fate(type).startTransaction(); + String goalMessage = TableOperation.MERGE + " Merge table " + tableName + "(" + tableId + + ") splits from " + startRowStr + " to " + endRowStr; + + manager.fate(type).seedTransaction(FateOperation.SYSTEM_MERGE, fateId, Review Comment: This is a really good idea, I will make this change for sure. -- 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]
