-
Notifications
You must be signed in to change notification settings - Fork 453
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
+525
−38
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
346f936
Add Mergeability column to support automatic merges
cshannon 10830fe
Merge branch 'main' into accumulo-5014
cshannon 408a941
Merge branch 'main' into accumulo-5014
cshannon ca60fe5
Store mergeability delay and steady time separate
cshannon 78c8e3e
Merge branch 'main' into accumulo-5014
cshannon 0e7fc81
Updates to address comments, add javadocs
cshannon bf951c4
use 2 states (never and delay >= 0)
cshannon 97aa2a5
fix test
cshannon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
core/src/main/java/org/apache/accumulo/core/client/admin/TabletMergeability.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
core/src/main/java/org/apache/accumulo/core/metadata/schema/TabletMergeabilityMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.