Skip to content

Commit

Permalink
Add Mergeability column to support automatic merges (#5187)
Browse files Browse the repository at this point in the history
Add Mergeability column to support automatic merges

This adds a new Mergeability column for marking if/when tablets are
eligible to me merged by the system based on a threshold. The column 
stores two values, a duration which is a delay for when a tablet can be 
merged that is relative to the time the Manager uses, Steady time. 
It also stores the current Steady time value when inserted so that later 
we can add the delay plus the original time when inserted to see if 
enough time has passed. The Steady Time value will only be stored if 
the delay is >= 0, otherwise it will be null as it won't be used. The Steady
Time value isn't technically needed for a delay of 0 because 0 means
it's eligible to merge always, but it could be useful if we wanted to
change the value for some reason and it would allow logging when it
was stored.

There are 2 possible states for the delay value:

1) null : This means a tablet will never automatically merge
2) duration >= 0 : Tablet is eligible to merge after the given delay
 (stored as a duration), relative to the current system Steady time. Ie. the
 tablet can be merged if the current manager time is later than the delay 
value + the steady time value when inserted. If the duration is 0 then it 
means it can merge always (now). 

This change only adds the new column itself and populates it. The default
is to never merge automatically for all cases except for when the system 
automatically splits tablets. In that case the newly split tablets are marked
as being eligible to merge always (duration of 0).

Future updates will add API enhancements to allow setting/viewing the
mergeability setting as well as to enable automatic merging by the system 
that is based on this new column value.

When automatic merging is enabled, if a user wants to make a tablet 
eligible to be merged in the future they would do so by setting a delay
that is positive. For example, to make a tablet eligible to be merged 
3 days from now the user set a duration of 3 days in the API (future PR)
and when the system inserts the value into metadata it will also include 
the current SteadyTime on creation. Later when the the current steady time 
passes that set delay + original stored steady time value the tablet would be
eligible to be merged. To enable merging immediately they can set the 
duration to 0 or use the TabletMergeability.always() helper which is just a 
shortcut to a duration of 0.
  • Loading branch information
cshannon authored Jan 10, 2025
1 parent 09dc3ce commit 7701e59
Show file tree
Hide file tree
Showing 30 changed files with 525 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* 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.core.client.admin;

import java.io.Serializable;
import java.time.Duration;
import java.util.Objects;
import java.util.Optional;

import com.google.common.base.Preconditions;

/**
* @since 4.0.0
*/
public class TabletMergeability implements Serializable {
private static final long serialVersionUID = 1L;

private static final TabletMergeability NEVER = new TabletMergeability();
private static final TabletMergeability ALWAYS = new TabletMergeability(Duration.ZERO);

private final Duration delay;

private TabletMergeability(Duration delay) {
this.delay = Objects.requireNonNull(delay);
}

// Edge case for NEVER
private TabletMergeability() {
this.delay = null;
}

/**
* Determines if the configured delay signals a tablet is never eligible to be automatically
* merged.
*
* @return true if never mergeable, else false
*/
public boolean isNever() {
return this.delay == null;
}

/**
* Determines if the configured delay signals a tablet is always eligible to be automatically
* merged now. (Has a delay of 0)
*
* @return true if always mergeable now, else false
*/
public boolean isAlways() {
return delay != null && this.delay.isZero();
}

/**
* Returns an Optional duration of the delay which is one of:
*
* <ul>
* <li>empty (never)</li>
* <li>0 (now)</li>
* <li>positive delay</li>
* </ul>
*
* @return the configured mergeability delay
*/
public Optional<Duration> getDelay() {
return Optional.ofNullable(delay);
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
TabletMergeability that = (TabletMergeability) o;
return Objects.equals(delay, that.delay);
}

@Override
public int hashCode() {
return Objects.hashCode(delay);
}

@Override
public String toString() {
if (delay == null) {
return "TabletMergeability=NEVER";
}
return "TabletMergeability=AFTER:" + delay.toMillis() + "ms";
}

/**
* Signifies that a tablet is never eligible to be automatically merged.
*
* @return a {@link TabletMergeability} with an empty delay signaling never merge
*/
public static TabletMergeability never() {
return NEVER;
}

/**
* Signifies that a tablet is eligible now to be automatically merged
*
* @return a {@link TabletMergeability} with a delay of 0 signaling never merge
*/
public static TabletMergeability always() {
return ALWAYS;
}

/**
* Creates a {@link TabletMergeability} that signals a tablet has a delay to a point in the future
* before it is automatically eligible to be merged. The duration must be positive value.
*
* @param delay the duration of the delay
*
* @return a {@link TabletMergeability} from the given delay.
*/
public static TabletMergeability after(Duration delay) {
Preconditions.checkArgument(delay.toNanos() >= 0, "Duration of delay must be greater than 0.");
return new TabletMergeability(delay);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ interface TabletUpdates<T> {

T putCloned();

T putTabletMergeability(TabletMergeabilityMetadata tabletMergeability);

/**
* By default the server lock is automatically added to mutations unless this method is set to
* false.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ public static class TabletColumnFamily {
public static final String REQUESTED_QUAL = "requestToHost";
public static final ColumnFQ REQUESTED_COLUMN = new ColumnFQ(NAME, new Text(REQUESTED_QUAL));

public static final String MERGEABILITY_QUAL = "mergeability";
public static final ColumnFQ MERGEABILITY_COLUMN =
new ColumnFQ(NAME, new Text(MERGEABILITY_QUAL));

public static Value encodePrevEndRow(Text per) {
if (per == null) {
return new Value(new byte[] {0});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* 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.core.metadata.schema;

import static org.apache.accumulo.core.util.LazySingletons.GSON;

import java.io.Serializable;
import java.time.Duration;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import org.apache.accumulo.core.client.admin.TabletMergeability;
import org.apache.accumulo.core.data.Value;
import org.apache.accumulo.core.util.time.SteadyTime;

import com.google.common.base.Preconditions;

public class TabletMergeabilityMetadata implements Serializable {
private static final long serialVersionUID = 1L;

private static final TabletMergeabilityMetadata NEVER =
new TabletMergeabilityMetadata(TabletMergeability.never());;

private final TabletMergeability tabletMergeability;
private final SteadyTime steadyTime;

private TabletMergeabilityMetadata(TabletMergeability tabletMergeability, SteadyTime steadyTime) {
this.tabletMergeability = Objects.requireNonNull(tabletMergeability);
this.steadyTime = steadyTime;
// This makes sure that SteadyTime is set if TabletMergeability has a delay, and is null
// if TabletMergeability is NEVER as we don't need to store it in that case
Preconditions.checkArgument(tabletMergeability.isNever() == (steadyTime == null),
"SteadyTime must be set if and only if TabletMergeability delay is >= 0");
}

private TabletMergeabilityMetadata(TabletMergeability tabletMergeability) {
this(tabletMergeability, null);
}

public TabletMergeability getTabletMergeability() {
return tabletMergeability;
}

public Optional<SteadyTime> getSteadyTime() {
return Optional.ofNullable(steadyTime);
}

public boolean isMergeable(SteadyTime currentTime) {
if (tabletMergeability.isNever()) {
return false;
}
// Steady time should never be null unless TabletMergeability is NEVER
Preconditions.checkState(steadyTime != null, "SteadyTime should be set");
var totalDelay = steadyTime.getDuration().plus(tabletMergeability.getDelay().orElseThrow());
return currentTime.getDuration().compareTo(totalDelay) >= 0;
}

private static class GSonData {
boolean never;
Long delay;
Long steadyTime;
}

String toJson() {
GSonData jData = new GSonData();
jData.never = tabletMergeability.isNever();
jData.delay = tabletMergeability.getDelay().map(Duration::toNanos).orElse(null);
jData.steadyTime = steadyTime != null ? steadyTime.getNanos() : null;
return GSON.get().toJson(jData);
}

static TabletMergeabilityMetadata fromJson(String json) {
GSonData jData = GSON.get().fromJson(json, GSonData.class);
if (jData.never) {
Preconditions.checkArgument(jData.delay == null && jData.steadyTime == null,
"delay and steadyTime should be null if mergeability 'never' is true");
} else {
Preconditions.checkArgument(jData.delay != null && jData.steadyTime != null,
"delay and steadyTime should both be set if mergeability 'never' is false");
}
TabletMergeability tabletMergeability = jData.never ? TabletMergeability.never()
: TabletMergeability.after(Duration.ofNanos(jData.delay));
SteadyTime steadyTime =
jData.steadyTime != null ? SteadyTime.from(jData.steadyTime, TimeUnit.NANOSECONDS) : null;
return new TabletMergeabilityMetadata(tabletMergeability, steadyTime);
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
TabletMergeabilityMetadata that = (TabletMergeabilityMetadata) o;
return Objects.equals(tabletMergeability, that.tabletMergeability)
&& Objects.equals(steadyTime, that.steadyTime);
}

@Override
public int hashCode() {
return Objects.hash(tabletMergeability, steadyTime);
}

@Override
public String toString() {
return "TabletMergeabilityMetadata{" + tabletMergeability + ", " + steadyTime + '}';
}

public static TabletMergeabilityMetadata never() {
return NEVER;
}

public static TabletMergeabilityMetadata always(SteadyTime currentTime) {
return new TabletMergeabilityMetadata(TabletMergeability.always(), currentTime);
}

public static TabletMergeabilityMetadata after(Duration delay, SteadyTime currentTime) {
return new TabletMergeabilityMetadata(TabletMergeability.after(delay), currentTime);
}

public static Value toValue(TabletMergeabilityMetadata tmm) {
return new Value(tmm.toJson());
}

public static TabletMergeabilityMetadata fromValue(Value value) {
return TabletMergeabilityMetadata.fromJson(value.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.ServerColumnFamily.SELECTED_QUAL;
import static org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.ServerColumnFamily.TIME_QUAL;
import static org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.TabletColumnFamily.AVAILABILITY_QUAL;
import static org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.TabletColumnFamily.MERGEABILITY_QUAL;
import static org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.TabletColumnFamily.PREV_ROW_QUAL;
import static org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.TabletColumnFamily.REQUESTED_QUAL;

Expand Down Expand Up @@ -123,6 +124,7 @@ public class TabletMetadata {
private final Set<FateId> compacted;
private final Set<FateId> userCompactionsRequested;
private final UnSplittableMetadata unSplittableMetadata;
private final TabletMergeabilityMetadata mergeability;
private final Supplier<Long> fileSize;

private TabletMetadata(Builder tmBuilder) {
Expand Down Expand Up @@ -155,6 +157,7 @@ private TabletMetadata(Builder tmBuilder) {
this.compacted = tmBuilder.compacted.build();
this.userCompactionsRequested = tmBuilder.userCompactionsRequested.build();
this.unSplittableMetadata = tmBuilder.unSplittableMetadata;
this.mergeability = Objects.requireNonNull(tmBuilder.mergeability);
this.fileSize = Suppliers.memoize(() -> {
// This code was using a java stream. While profiling SplitMillionIT, the stream was showing
// up as hot when scanning 1 million tablets. Converted to a for loop to improve performance.
Expand Down Expand Up @@ -198,7 +201,8 @@ public enum ColumnType {
SELECTED,
COMPACTED,
USER_COMPACTION_REQUESTED,
UNSPLITTABLE
UNSPLITTABLE,
MERGEABILITY
}

public static class Location {
Expand Down Expand Up @@ -439,6 +443,11 @@ public UnSplittableMetadata getUnSplittable() {
return unSplittableMetadata;
}

public TabletMergeabilityMetadata getTabletMergeability() {
ensureFetched(ColumnType.MERGEABILITY);
return mergeability;
}

@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("tableId", tableId)
Expand All @@ -453,7 +462,8 @@ public String toString() {
.append("operationId", operationId).append("selectedFiles", selectedFiles)
.append("futureAndCurrentLocationSet", futureAndCurrentLocationSet)
.append("userCompactionsRequested", userCompactionsRequested)
.append("unSplittableMetadata", unSplittableMetadata).toString();
.append("unSplittableMetadata", unSplittableMetadata).append("mergeability", mergeability)
.toString();
}

public List<Entry<Key,Value>> getKeyValues() {
Expand Down Expand Up @@ -527,6 +537,9 @@ public static <E extends Entry<Key,Value>> TabletMetadata convertRow(Iterator<E>
case REQUESTED_QUAL:
tmBuilder.onDemandHostingRequested(true);
break;
case MERGEABILITY_QUAL:
tmBuilder.mergeability(TabletMergeabilityMetadata.fromValue(kv.getValue()));
break;
default:
throw new IllegalStateException("Unexpected TabletColumnFamily qualifier: " + qual);
}
Expand Down Expand Up @@ -689,7 +702,7 @@ static class Builder {
private final ImmutableSet.Builder<FateId> compacted = ImmutableSet.builder();
private final ImmutableSet.Builder<FateId> userCompactionsRequested = ImmutableSet.builder();
private UnSplittableMetadata unSplittableMetadata;
// private Supplier<Long> fileSize;
private TabletMergeabilityMetadata mergeability = TabletMergeabilityMetadata.never();

void table(TableId tableId) {
this.tableId = tableId;
Expand Down Expand Up @@ -799,6 +812,10 @@ void unSplittableMetadata(UnSplittableMetadata unSplittableMetadata) {
this.unSplittableMetadata = unSplittableMetadata;
}

void mergeability(TabletMergeabilityMetadata mergeability) {
this.mergeability = mergeability;
}

void keyValue(Entry<Key,Value> kv) {
if (this.keyValues == null) {
this.keyValues = ImmutableList.builder();
Expand Down
Loading

0 comments on commit 7701e59

Please sign in to comment.