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 all 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,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 {
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;

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
Loading