Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mergeability column to support automatic merges #5187

Merged
merged 8 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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 com.google.common.base.Preconditions;

public class TabletMergeability implements Serializable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need @since tags in javadoc since this class is in a public API package.

private static final long serialVersionUID = 1L;

public static final TabletMergeability NEVER = new TabletMergeability(Duration.ofNanos(-1));
public static final TabletMergeability NOW = new TabletMergeability(Duration.ZERO);

private final Duration delay;

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

public boolean isNever() {
return this.delay.isNegative();
}

public boolean isNow() {
return this.delay.isZero();
}

public boolean isFuture() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to ignore this suggestion. Was thinking this name went better w/ the getDelay() method.

Suggested change
public boolean isFuture() {
public boolean isDelayed() {

Copy link
Contributor Author

@cshannon cshannon Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think isDelayed() is probably better and will rename it.

I kept going back and forth with trying to decide on the correct names and I think this may change anyways. I've been wondering if instead of NOW it should be ALWAYS or IMMEDIATE etc as those names could align better with NEVER. I will probably leave the name alone for now because I think the exact naming is going to be how we intent to use this going forward and if we want to rename it and TabletMergeability itself could possibly be renamed in a future PR.

Right now the name is TabletMereability but as @ctubbsii had pointed out in a previous conversation, the current goal for this is really tablet automatic mergeability so we can use it to determine if we can run an automatic merge on a tablet range just like we do automatic splits.

However, there has been some talk of making it more generic to mergeabilty in general (Even regular user triggered merges) which may also impact how we name things but not sure we will or need to. Either way, I expect in future PRs as this evolves the naming is going to be tweaked a bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keith-turner - The more I think about it, the more I'm not sure I like using NOW because it's a bit misleading. A tablet could be eligible to be merged now even if a delay is set if enough time has passed. So the value isn't really signaling that it's eligible now so much as that it's always eligible no matter what because the delay is 0 so I'm back to thinking maybe we rename it to something like ALWAYS or IMMEDIATELY.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah NOW is kinda tricky. I like ALWAYS. Maybe could have two states instead of three by making delay be >=0 instead of >0. Then its just NEVER or DELAYED. NEVER is the one that is nice to specifically designate intention. From an encoding standpoint that would be a boolean and a long.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting idea, I think that makes sense to allow a delay of >= 0 because it fits in nicely as part of the regular delay logic but NEVER is the edge case as you said. For the json encoding we keep the boolean and long but inside the TabletMergeability class maybe we can just represent "NEVER" with a null delay. I think we should still keep the always() method shortcut I think because it's going to be a commonly used value.

return delay.toNanos() > 0;
}

public Duration getDelay() {
return delay;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could do the following to give more flexibility w/ changing how now and never are represented, would need add javadoc if doing so.

Suggested change
public Duration getDelay() {
return delay;
public Duration getDelay() {
Preconditions.checkstate(isDelayed());
return delay;

Copy link
Contributor Author

@cshannon cshannon Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't end up making this change yet because it would make things a bit tricky for things like json serialization. It would make the serialization harder because the json code would now have to duplicate business logic and know to translate NEVER to -1 and NOW to 0 for the delay. This is the spot I am talking about.

That code would have to make sure to not call getDelay() unless it was positive and we would introduce multiple locations in the code that need to be updated if we wanted to change the meaning of -1 and 0. They are different packages so we can't make it package scope for the private delay either.

All that being said, I think this is doable and not a bad idea but if we do this I think it probably makes sense to completely decouple the serialization as well if the intent is to be more flexible in the future and no other code should really know what -1 and 0 mean outside of the internals of TabletMergeability. This would mean only allowing publicly accessible methods that create a TabletMergeability to set a delay of > 0 and requiring the use of TabletMergeability.now() or TabletMergeability.never() for all other cases so this method would go away.

For serialization, I think the json format in TabletMergeabilityMetadata would need to be changed to something like the following:

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

and we would just have to set steadyTime and delay to null values if either never or now was true.

@keith-turner - thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking this one step further you could even just require the delay in TabletMergeability to always be positive and never < 0 and add a couple boolean flags to signal NOW and NEVER and just make the delay null in that case vs using 0 or -1. It probably doesn't matter either way though if we keep using 0 and -1 as it would be hidden internally to the class and not exposed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now its documented on the javadoc what -1,0, and >0 mean, which seems good for now. Its nice to structure the API so that the -1 case does not cause bugs in code calling getDelay AND to make it more flexible to change, but not really sure of the best way to achieve those goals. The javadoc lets someone know hey you need to handle this -1 case, which helps w/ the usage bug case. Making the API more flexible is tricky and as you mentioned this will probably evolve as more of the feature is implemented also, so it would be good to see what is learned from that.

If the code is released w/ that javadoc then its behavior is set and it would be ok for the serialization code to be tightly coupled to the behavior in the javadoc.

}

@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 (isNow()) {
return "TabletMergeability=NOW";
} else if (isNever()) {
return "TabletMergeability=NEVER";
}
return "TabletMergeability=AFTER:" + delay.toNanos();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to convert this to ms or s in the toString method for ease of understanding.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not a bad idea, nanoseconds would be pretty tough to understand in a log.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could convert to ms and include the unit at the end of the string.

}

public static TabletMergeability from(Duration delay) {
Preconditions.checkArgument(delay.toNanos() >= -1,
"Duration of delay must be -1, 0, or a positive offset.");
return new TabletMergeability(delay);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method needs javadoc outlining the acceptable input values for duration. Since this is a public API class wondering if the following would be better instead, however those methods would still need javadoc. Could make those constants private if adding these also, thinking the methods give a bit more flexibility to change.

Suggested change
public static TabletMergeability from(Duration delay) {
Preconditions.checkArgument(delay.toNanos() >= -1,
"Duration of delay must be -1, 0, or a positive offset.");
return new TabletMergeability(delay);
}
public static TabletMergeability now(){
return NOW:
}
public static TabletMergeability never(){
return NEVER:
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good idea and I may do this as well in TabletMergeabilityMetadata. The reason is I have been thinking that the NOW, NEVER etc constants that I defined in TabletMergeability and TabletMergeabilityMetadata could lead to future mistakes if developers forget they are not enums and incorrectly use an identity check for equality. At first glance TabletMergeability.NOW looks like an enum value when it isn't. In theory if the code always uses the constant then an identity check for equality would be true when checked, but it would still be safer and more correct to use .equals() and not == since it isn't an enum and both TabletMergeability and TabletMergeabilityMetadata have equals/hashcode defined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I will add some javadocs and we can tweak them in future PRs if things evolve/change which should be fine as long as we do it before we release 4.0.0


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 @@ -392,6 +392,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,132 @@
/*
* 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;

public static final TabletMergeabilityMetadata NEVER =
new TabletMergeabilityMetadata(TabletMergeability.NEVER);
public static final TabletMergeabilityMetadata NOW =
new TabletMergeabilityMetadata(TabletMergeability.NOW);

private final TabletMergeability tabletMergeability;
private final SteadyTime steadyTime;

private TabletMergeabilityMetadata(TabletMergeability tabletMergeability, SteadyTime steadyTime) {
this.tabletMergeability = Objects.requireNonNull(tabletMergeability);
this.steadyTime = steadyTime;
Preconditions.checkArgument(tabletMergeability.isFuture() == (steadyTime != null),
"SteadyTime must be set if and only if TabletMergeability delay is greater than 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;
}
return currentTime.getDuration().compareTo(totalDelay()) >= 0;
}

private Duration totalDelay() {
return steadyTime != null ? steadyTime.getDuration().plus(tabletMergeability.getDelay())
: tabletMergeability.getDelay();
}

private static class GSonData {
long delay;
Long steadyTime;
}

String toJson() {
GSonData jData = new GSonData();
jData.delay = tabletMergeability.getDelay().toNanos();
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);
TabletMergeability tabletMergeability = TabletMergeability.from(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 after(Duration delay, SteadyTime currentTime) {
return from(TabletMergeability.after(delay), currentTime);
}

public static TabletMergeabilityMetadata from(TabletMergeability tm, SteadyTime currentTime) {
return new TabletMergeabilityMetadata(tm, 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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType.LOADED;
import static org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType.LOCATION;
import static org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType.LOGS;
import static org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType.MERGEABILITY;
import static org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType.MERGED;
import static org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType.OPID;
import static org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType.PREV_ROW;
Expand Down Expand Up @@ -312,6 +313,14 @@ public TabletMetadataBuilder putCloned() {
return this;
}

@Override
public TabletMetadataBuilder
putTabletMergeability(TabletMergeabilityMetadata tabletMergeability) {
fetched.add(MERGEABILITY);
internalBuilder.putTabletMergeability(tabletMergeability);
return this;
}

@Override
public TabletMetadataBuilder automaticallyPutServerLock(boolean b) {
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,13 @@ public T automaticallyPutServerLock(boolean b) {
return getThis();
}

@Override
public T putTabletMergeability(TabletMergeabilityMetadata tabletMergeability) {
TabletColumnFamily.MERGEABILITY_COLUMN.put(mutation,
TabletMergeabilityMetadata.toValue(tabletMergeability));
return getThis();
}

public void setCloseAfterMutate(AutoCloseable closeable) {
this.closeAfterMutate = closeable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.ScanFileColumnFamily;
import org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.SplitColumnFamily;
import org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.SuspendLocationColumn;
import org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.TabletColumnFamily;
import org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.UserCompactionRequestedColumnFamily;
import org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType;
import org.apache.accumulo.core.metadata.schema.filters.TabletMetadataFilter;
Expand Down Expand Up @@ -394,6 +395,9 @@ public Options fetch(ColumnType... colsToFetch) {
case UNSPLITTABLE:
qualifiers.add(SplitColumnFamily.UNSPLITTABLE_COLUMN);
break;
case MERGEABILITY:
qualifiers.add(TabletColumnFamily.MERGEABILITY_COLUMN);
break;
default:
throw new IllegalArgumentException("Unknown col type " + colToFetch);
}
Expand Down
Loading
Loading