Skip to content

Commit

Permalink
Updates to address comments, add javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
cshannon committed Jan 10, 2025
1 parent 78c8e3e commit 0e7fc81
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,62 @@

import com.google.common.base.Preconditions;

/**
* @since 4.0.0
*/
public class TabletMergeability implements Serializable {
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 static final TabletMergeability NEVER = new TabletMergeability(Duration.ofNanos(-1));
private static final TabletMergeability NOW = new TabletMergeability(Duration.ZERO);

private final Duration delay;

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

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

/**
* 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 isNow() {
return this.delay.isZero();
}

public boolean isFuture() {
/**
* Determines if the configured delay signals a tablet has a configured delay before being
* eligible to be automatically merged. (Has a positive delay)
*
* @return true if there is a configured delay, else false
*/
public boolean isDelayed() {
return delay.toNanos() > 0;
}

/**
* Returns the duration of the delay which is one of:
*
* <ul>
* <li>-1 (never)</li>
* <li>0 (now)</li>
* <li>positive delay</li>
* </ul>
*
* @return the configured mergeability delay
*/
public Duration getDelay() {
return delay;
}
Expand All @@ -73,15 +105,53 @@ public String toString() {
} else if (isNever()) {
return "TabletMergeability=NEVER";
}
return "TabletMergeability=AFTER:" + delay.toNanos();
return "TabletMergeability=AFTER:" + delay.toMillis() + "ms";
}

/**
* Signifies that a tablet is never eligible to be automatically merged.
*
* @return a {@link TabletMergeability} with a delay of -1 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 now() {
return NOW;
}

/**
* Creates a {@link TabletMergeability} from the given delay. The duration must be one of
* <ul>
* <li>-1 (never)</li>
* <li>0 (now)</li>
* <li>positive delay</li>
* </ul>
*
* @param delay the duration of the delay
*
* @return a {@link TabletMergeability} from the given delay.
*/
public static TabletMergeability from(Duration delay) {
Preconditions.checkArgument(delay.toNanos() >= -1,
"Duration of delay must be -1, 0, or a positive offset.");
"Duration of delay must be -1, 0, or a positive delay.");
return new TabletMergeability(delay);
}

/**
* 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@
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 static final TabletMergeabilityMetadata NEVER =
new TabletMergeabilityMetadata(TabletMergeability.never());
private 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),
Preconditions.checkArgument(tabletMergeability.isDelayed() == (steadyTime != null),
"SteadyTime must be set if and only if TabletMergeability delay is greater than 0.");
}

Expand Down Expand Up @@ -114,6 +114,14 @@ public String toString() {
return "TabletMergeabilityMetadata{" + tabletMergeability + ", " + steadyTime + '}';
}

public static TabletMergeabilityMetadata now() {
return NOW;
}

public static TabletMergeabilityMetadata never() {
return NEVER;
}

public static TabletMergeabilityMetadata after(Duration delay, SteadyTime currentTime) {
return from(TabletMergeability.after(delay), currentTime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -702,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 TabletMergeabilityMetadata mergeability = TabletMergeabilityMetadata.NEVER;
private TabletMergeabilityMetadata mergeability = TabletMergeabilityMetadata.never();

void table(TableId tableId) {
this.tableId = tableId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ public void testBuilder() {
.putFile(sf1, dfv1).putFile(sf2, dfv2).putBulkFile(rf1, loadedFateId1)
.putBulkFile(rf2, loadedFateId2).putFlushId(27).putDirName("dir1").putScan(sf3).putScan(sf4)
.putCompacted(compactFateId1).putCompacted(compactFateId2).putCloned()
.putTabletMergeability(TabletMergeabilityMetadata.NOW)
.putTabletMergeability(TabletMergeabilityMetadata.now())
.build(ECOMP, HOSTING_REQUESTED, MERGED, USER_COMPACTION_REQUESTED, UNSPLITTABLE);

assertEquals(extent, tm.getExtent());
Expand All @@ -664,7 +664,7 @@ public void testBuilder() {
assertFalse(tm.hasMerged());
assertNull(tm.getUnSplittable());
assertEquals("OK", tm.getCloned());
assertEquals(TabletMergeabilityMetadata.NOW, tm.getTabletMergeability());
assertEquals(TabletMergeabilityMetadata.now(), tm.getTabletMergeability());
assertThrows(IllegalStateException.class, tm::getOperationId);
assertThrows(IllegalStateException.class, tm::getSuspend);
assertThrows(IllegalStateException.class, tm::getTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private Map<Key,Value> createEntries() {
var builder = TabletMetadata.builder(keyExtent).putDirName(dirName)
.putTime(new MetadataTime(0, TimeType.LOGICAL))
.putTabletAvailability(TabletAvailability.HOSTED)
.putTabletMergeability(TabletMergeabilityMetadata.NEVER).putPrevEndRow(prevEndRow);
.putTabletMergeability(TabletMergeabilityMetadata.never()).putPrevEndRow(prevEndRow);
for (String file : files) {
builder.putFile(new ReferencedTabletFile(new Path(file)).insert(), new DataFileValue(0, 0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,13 +664,13 @@ public void testMergeabilityColumn() {

m = new Mutation(new Text("0;foo"));
TabletColumnFamily.MERGEABILITY_COLUMN.put(m,
TabletMergeabilityMetadata.toValue(TabletMergeabilityMetadata.NEVER));
TabletMergeabilityMetadata.toValue(TabletMergeabilityMetadata.never()));
violations = mc.check(createEnv(), m);
assertTrue(violations.isEmpty());

m = new Mutation(new Text("0;foo"));
TabletColumnFamily.MERGEABILITY_COLUMN.put(m,
TabletMergeabilityMetadata.toValue(TabletMergeabilityMetadata.NOW));
TabletMergeabilityMetadata.toValue(TabletMergeabilityMetadata.now()));
violations = mc.check(createEnv(), m);
assertTrue(violations.isEmpty());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public void executeFateOperation(TInfo tinfo, TCredentials c, TFateId opid, TFat
manager.fate(type).seedTransaction(op, fateId,
new TraceRepo<>(new CreateTable(c.getPrincipal(), tableName, timeType, options,
splitsPath, splitCount, splitsDirsPath, initialTableState,
initialTabletAvailability, namespaceId, TabletMergeabilityMetadata.NEVER)),
initialTabletAvailability, namespaceId, TabletMergeabilityMetadata.never())),
autoCleanup, goalMessage);

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ private void addNewTablets(FateId fateId, Manager manager, TabletMetadata tablet
.debug("{} copying compacted marker to new child tablet {}", fateId, compactedFateId));

mutator.putTabletAvailability(tabletMetadata.getTabletAvailability());
mutator.putTabletMergeability(splitInfo.isSystemCreated() ? TabletMergeabilityMetadata.NOW
: TabletMergeabilityMetadata.NEVER);
mutator.putTabletMergeability(splitInfo.isSystemCreated() ? TabletMergeabilityMetadata.now()
: TabletMergeabilityMetadata.never());
tabletMetadata.getLoaded().forEach((k, v) -> mutator.putBulkFile(k.getTabletFile(), v));

newTabletsFiles.get(newExtent).forEach(mutator::putFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void testManyColumns() throws Exception {
var availability = TabletAvailability.HOSTED;
var lastLocation = TabletMetadata.Location.last("1.2.3.4:1234", "123456789");
var suspendingTServer = SuspendingTServer.fromValue(new Value("1.2.3.4:5|56"));
var mergeability = TabletMergeabilityMetadata.NOW;
var mergeability = TabletMergeabilityMetadata.now();

var tablet1 =
TabletMetadata.builder(ke1).putOperation(opid).putDirName("td1").putFile(file3, dfv3)
Expand Down Expand Up @@ -234,7 +234,7 @@ public void testManyColumns() throws Exception {
EasyMock.expect(tabletMutator.deleteSuspension()).andReturn(tabletMutator);
EasyMock.expect(tabletMutator.deleteLocation(lastLocation)).andReturn(tabletMutator);
EasyMock.expect(tabletMutator.deleteUnSplittable()).andReturn(tabletMutator);
EasyMock.expect(tabletMutator.putTabletMergeability(TabletMergeabilityMetadata.NOW))
EasyMock.expect(tabletMutator.putTabletMergeability(TabletMergeabilityMetadata.now()))
.andReturn(tabletMutator).once();

});
Expand Down Expand Up @@ -405,7 +405,7 @@ public void testTime() throws Exception {
.once();
EasyMock.expect(tabletMutator.setMerged()).andReturn(tabletMutator).once();
// Current default if not set is NEVER
EasyMock.expect(tabletMutator.putTabletMergeability(TabletMergeabilityMetadata.NEVER))
EasyMock.expect(tabletMutator.putTabletMergeability(TabletMergeabilityMetadata.never()))
.andReturn(tabletMutator).once();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public void testManyColumns() throws Exception {
.andReturn(tablet1Mutator);
EasyMock.expect(tablet1Mutator.putFile(file2, dfv2)).andReturn(tablet1Mutator);
// SplitInfo marked as system generated so should be set to NOW
EasyMock.expect(tablet1Mutator.putTabletMergeability(TabletMergeabilityMetadata.NOW))
EasyMock.expect(tablet1Mutator.putTabletMergeability(TabletMergeabilityMetadata.now()))
.andReturn(tablet1Mutator);
tablet1Mutator.submit(EasyMock.anyObject());
EasyMock.expectLastCall().once();
Expand All @@ -315,7 +315,7 @@ public void testManyColumns() throws Exception {
EasyMock.expect(tablet2Mutator.putCompacted(ucfid3)).andReturn(tablet2Mutator);
EasyMock.expect(tablet2Mutator.putTabletAvailability(availability)).andReturn(tablet2Mutator);
// SplitInfo marked as system generated so should be set to NOW
EasyMock.expect(tablet2Mutator.putTabletMergeability(TabletMergeabilityMetadata.NOW))
EasyMock.expect(tablet2Mutator.putTabletMergeability(TabletMergeabilityMetadata.now()))
.andReturn(tablet2Mutator);
EasyMock.expect(tablet2Mutator.putBulkFile(loaded1.getTabletFile(), flid1))
.andReturn(tablet2Mutator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void createMetadata(TableId tableId) {
tabletMutator.putDirName(dirName);
tabletMutator.putTime(new MetadataTime(0, TimeType.MILLIS));
tabletMutator.putTabletAvailability(TabletAvailability.HOSTED);
tabletMutator.putTabletMergeability(TabletMergeabilityMetadata.NEVER);
tabletMutator.putTabletMergeability(TabletMergeabilityMetadata.never());
tabletMutator.mutate();
} catch (Exception e) {
throw new IllegalStateException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void addSplitTest() throws Exception {
TableId id = TableId.of(c.tableOperations().tableIdMap().get(tableName));
try (TabletsMetadata tm = getServerContext().getAmple().readTablets().forTable(id).build()) {
// Default for user created tablets should be mergeability set to NEVER
tm.stream().forEach(tablet -> assertEquals(TabletMergeabilityMetadata.NEVER,
tm.stream().forEach(tablet -> assertEquals(TabletMergeabilityMetadata.never(),
tablet.getTabletMergeability()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private void testCommonSystemTableConfig(ClientContext client, TableId tableId,
assertTrue(
tablets.stream().allMatch(tm -> tm.getTabletAvailability() == TabletAvailability.HOSTED));
assertTrue(tablets.stream()
.allMatch(tm -> tm.getTabletMergeability().equals(TabletMergeabilityMetadata.NEVER)));
.allMatch(tm -> tm.getTabletMergeability().equals(TabletMergeabilityMetadata.never())));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ public void tabletShouldSplit() throws Exception {
.equals(entry.getKey().getColumnQualifier())) {
// Default tablet should be set to NEVER, all newly generated system splits should be
// set to NOW
var mergeability = extent.endRow() == null ? TabletMergeabilityMetadata.NEVER
: TabletMergeabilityMetadata.NOW;
var mergeability = extent.endRow() == null ? TabletMergeabilityMetadata.never()
: TabletMergeabilityMetadata.now();
assertEquals(mergeability, TabletMergeabilityMetadata.fromValue(entry.getValue()));
}
count++;
Expand Down

0 comments on commit 0e7fc81

Please sign in to comment.