Skip to content

Commit 7b27d09

Browse files
authored
HBASE-30036 Skip redundant delete markers during flush and minor compaction (#7993)
Add DeleteTracker.isRedundantDelete() to detect when a delete marker is already covered by a previously tracked delete of equal or broader scope. ScanDeleteTracker implements this for all four delete types: - DeleteFamily/DeleteFamilyVersion: covered by a tracked DeleteFamily - DeleteColumn/Delete: covered by a tracked DeleteFamily or DeleteColumn MinorCompactionScanQueryMatcher calls this check before including a delete marker, returning SEEK_NEXT_COL to skip past all remaining cells covered by the previously tracked delete. Compatible with KEEP_DELETED_CELLS. When set to TRUE, trackDelete() does not populate the delete tracker, so isRedundantDelete() always returns false and all markers are retained. Signed-off-by: Charles Connell <cconnell@apache.org>
1 parent 39c7455 commit 7b27d09

5 files changed

Lines changed: 175 additions & 2 deletions

File tree

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/querymatcher/DeleteTracker.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ enum DeleteResult {
8383
// deleted in strong semantics of versions(See MvccTracker)
8484
}
8585

86+
/**
87+
* Check if the given delete marker is redundant, i.e., it is already covered by a previously
88+
* tracked delete of equal or broader scope. A DeleteFamily is redundant if a DeleteFamily with a
89+
* higher timestamp was already seen. A DeleteColumn is redundant if a DeleteColumn for the same
90+
* qualifier with a higher timestamp, or a DeleteFamily with a higher timestamp, was already seen.
91+
* <p>
92+
* This is a read-only check with no side effects on tracker state.
93+
* @param cell the delete marker cell to check
94+
* @return true if the delete marker is redundant and can be skipped
95+
*/
96+
default boolean isRedundantDelete(ExtendedCell cell) {
97+
return false;
98+
}
99+
86100
/**
87101
* Return the comparator passed to this delete tracker
88102
* @return the cell comparator

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/querymatcher/MinorCompactionScanQueryMatcher.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.io.IOException;
2121
import org.apache.hadoop.hbase.ExtendedCell;
22+
import org.apache.hadoop.hbase.KeyValue;
2223
import org.apache.hadoop.hbase.PrivateCellUtil;
2324
import org.apache.hadoop.hbase.regionserver.ScanInfo;
2425
import org.apache.yetus.audience.InterfaceAudience;
@@ -47,6 +48,19 @@ public MatchCode match(ExtendedCell cell) throws IOException {
4748
// we should not use this delete marker to mask any cell yet.
4849
return MatchCode.INCLUDE;
4950
}
51+
// Check before tracking: an older DeleteColumn or DeleteFamily is redundant if a newer
52+
// one of equal or broader scope was already seen. Must check before trackDelete() since
53+
// that overwrites tracker state. Seek past remaining cells for this column/row since
54+
// they are all covered by the previously tracked delete.
55+
if (deletes.isRedundantDelete(cell)) {
56+
// Skip seeking for deletes with empty qualifier, not to skip a subsequent
57+
// DeleteFamily marker that covers other qualifiers. DeleteFamily itself can seek
58+
// safely because all remaining empty-qualifier cells are redundant under it.
59+
if (cell.getQualifierLength() == 0 && typeByte != KeyValue.Type.DeleteFamily.getCode()) {
60+
return MatchCode.SKIP;
61+
}
62+
return columns.getNextRowOrNextColumn(cell);
63+
}
5064
trackDelete(cell);
5165
return MatchCode.INCLUDE;
5266
}

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/querymatcher/ScanDeleteTracker.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,28 @@ public DeleteResult isDeleted(ExtendedCell cell) {
142142
return DeleteResult.NOT_DELETED;
143143
}
144144

145+
@Override
146+
public boolean isRedundantDelete(ExtendedCell cell) {
147+
byte type = cell.getTypeByte();
148+
boolean coveredByFamily = hasFamilyStamp && cell.getTimestamp() <= familyStamp;
149+
150+
if (
151+
type == KeyValue.Type.DeleteFamily.getCode()
152+
|| type == KeyValue.Type.DeleteFamilyVersion.getCode()
153+
) {
154+
return coveredByFamily;
155+
}
156+
157+
boolean coveredByColumn =
158+
deleteCell != null && deleteType == KeyValue.Type.DeleteColumn.getCode()
159+
&& CellUtil.matchingQualifier(cell, deleteCell) && cell.getTimestamp() <= deleteTimestamp;
160+
161+
if (type == KeyValue.Type.DeleteColumn.getCode() || type == KeyValue.Type.Delete.getCode()) {
162+
return coveredByFamily || coveredByColumn;
163+
}
164+
return false;
165+
}
166+
145167
@Override
146168
public boolean isEmpty() {
147169
return deleteCell == null && !hasFamilyStamp && familyVersionStamps.isEmpty();

hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestStoreFileWriter.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,15 @@ public void testCompactedFiles() throws Exception {
177177
stores[0].getStorefilesCount());
178178

179179
regions[1].compact(false);
180-
assertEquals(flushCount - stores[1].getCompactedFiles().size() + 2,
181-
stores[1].getStorefilesCount());
180+
// HBASE-30036 skips redundant delete markers during minor compaction, so the historical
181+
// file may end up empty and not be created. The count can be +1 or +2.
182+
int minorCompactedCount = stores[1].getStorefilesCount();
183+
int expectedMin = flushCount - stores[1].getCompactedFiles().size() + 1;
184+
int expectedMax = flushCount - stores[1].getCompactedFiles().size() + 2;
185+
assertTrue(
186+
"Expected store file count between " + expectedMin + " and " + expectedMax + " but was "
187+
+ minorCompactedCount,
188+
minorCompactedCount >= expectedMin && minorCompactedCount <= expectedMax);
182189

183190
verifyCells();
184191

hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/querymatcher/TestCompactionScanQueryMatcher.java

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.hadoop.hbase.regionserver.querymatcher;
1919

2020
import static org.apache.hadoop.hbase.regionserver.querymatcher.ScanQueryMatcher.MatchCode.INCLUDE;
21+
import static org.apache.hadoop.hbase.regionserver.querymatcher.ScanQueryMatcher.MatchCode.SEEK_NEXT_COL;
2122
import static org.apache.hadoop.hbase.regionserver.querymatcher.ScanQueryMatcher.MatchCode.SKIP;
2223
import static org.junit.Assert.assertEquals;
2324

@@ -75,6 +76,121 @@ public void testMatch_PartialRangeDropDeletes() throws Exception {
7576
testDropDeletes(row2, row3, new byte[][] { row1, row1 }, INCLUDE, INCLUDE);
7677
}
7778

79+
/**
80+
* Test redundant delete marker handling with COMPACT_RETAIN_DELETES. Cells are auto-generated
81+
* from the given types with decrementing timestamps.
82+
*/
83+
@Test
84+
public void testSkipsRedundantDeleteMarkers() throws IOException {
85+
// Interleaved DeleteColumn + Put. First DC included, put triggers SEEK_NEXT_COL.
86+
assertRetainDeletes(new Type[] { Type.DeleteColumn, Type.Put, Type.DeleteColumn }, INCLUDE,
87+
SEEK_NEXT_COL);
88+
89+
// Contiguous DeleteColumn. First included, rest redundant.
90+
assertRetainDeletes(new Type[] { Type.DeleteColumn, Type.DeleteColumn, Type.DeleteColumn },
91+
INCLUDE, SEEK_NEXT_COL, SEEK_NEXT_COL);
92+
93+
// Contiguous DeleteFamily. First included, rest redundant.
94+
assertRetainDeletes(new Type[] { Type.DeleteFamily, Type.DeleteFamily, Type.DeleteFamily },
95+
INCLUDE, SEEK_NEXT_COL, SEEK_NEXT_COL);
96+
97+
// DF + DFV interleaved. DF included, DFV redundant (SKIP because empty qualifier),
98+
// older DF redundant (SEEK_NEXT_COL), older DFV redundant (SKIP).
99+
assertRetainDeletes(new Type[] { Type.DeleteFamily, Type.DeleteFamilyVersion, Type.DeleteFamily,
100+
Type.DeleteFamilyVersion }, INCLUDE, SKIP, SEEK_NEXT_COL, SKIP);
101+
102+
// Delete (version) covered by DeleteColumn.
103+
assertRetainDeletes(new Type[] { Type.DeleteColumn, Type.Delete, Type.Delete, Type.Delete },
104+
INCLUDE, SEEK_NEXT_COL, SEEK_NEXT_COL, SEEK_NEXT_COL);
105+
106+
// KEEP_DELETED_CELLS=TRUE: all markers retained.
107+
assertRetainDeletes(KeepDeletedCells.TRUE,
108+
new Type[] { Type.DeleteColumn, Type.DeleteColumn, Type.DeleteColumn }, INCLUDE, INCLUDE,
109+
INCLUDE);
110+
}
111+
112+
/**
113+
* Redundant column-level deletes with empty qualifier must not seek past a subsequent
114+
* DeleteFamily. getKeyForNextColumn treats empty qualifier as "no column" and returns
115+
* SEEK_NEXT_ROW, which would skip the DF and all remaining cells in the row.
116+
*/
117+
@Test
118+
public void testEmptyQualifierDeleteDoesNotSkipDeleteFamily() throws IOException {
119+
byte[] emptyQualifier = HConstants.EMPTY_BYTE_ARRAY;
120+
121+
// DC(empty) + DC(empty) redundant + DF must still be reachable.
122+
assertRetainDeletes(emptyQualifier,
123+
new Type[] { Type.DeleteColumn, Type.DeleteColumn, Type.DeleteFamily }, INCLUDE, SKIP,
124+
INCLUDE);
125+
126+
// DC(empty) + Delete(empty) redundant + DF must still be reachable.
127+
assertRetainDeletes(emptyQualifier,
128+
new Type[] { Type.DeleteColumn, Type.Delete, Type.DeleteFamily }, INCLUDE, SKIP, INCLUDE);
129+
}
130+
131+
private void assertRetainDeletes(Type[] types, MatchCode... expected) throws IOException {
132+
assertRetainDeletes(KeepDeletedCells.FALSE, types, expected);
133+
}
134+
135+
private void assertRetainDeletes(byte[] qualifier, Type[] types, MatchCode... expected)
136+
throws IOException {
137+
assertRetainDeletes(KeepDeletedCells.FALSE, qualifier, types, expected);
138+
}
139+
140+
/**
141+
* Build cells from the given types with decrementing timestamps (same ts for adjacent
142+
* family-level and column-level types at the same position). Family-level types (DeleteFamily,
143+
* DeleteFamilyVersion) use empty qualifier; others use col1.
144+
*/
145+
private void assertRetainDeletes(KeepDeletedCells keepDeletedCells, Type[] types,
146+
MatchCode... expected) throws IOException {
147+
assertRetainDeletes(keepDeletedCells, null, types, expected);
148+
}
149+
150+
/**
151+
* Build cells from the given types with decrementing timestamps. If qualifier is null,
152+
* family-level types use empty qualifier and others use col1. If qualifier is specified, all
153+
* types use that qualifier.
154+
*/
155+
private void assertRetainDeletes(KeepDeletedCells keepDeletedCells, byte[] qualifier,
156+
Type[] types, MatchCode... expected) throws IOException {
157+
long now = EnvironmentEdgeManager.currentTime();
158+
ScanInfo scanInfo = new ScanInfo(this.conf, fam1, 0, 1, ttl, keepDeletedCells,
159+
HConstants.DEFAULT_BLOCKSIZE, 0, rowComparator, false);
160+
CompactionScanQueryMatcher qm = CompactionScanQueryMatcher.create(scanInfo,
161+
ScanType.COMPACT_RETAIN_DELETES, 0L, PrivateConstants.OLDEST_TIMESTAMP,
162+
PrivateConstants.OLDEST_TIMESTAMP, now, null, null, null);
163+
qm.setToNewRow(KeyValueUtil.createFirstOnRow(row1));
164+
165+
long ts = now;
166+
List<MatchCode> actual = new ArrayList<>(expected.length);
167+
for (int i = 0; i < types.length; i++) {
168+
byte[] qual;
169+
if (qualifier != null) {
170+
qual = qualifier;
171+
} else {
172+
boolean familyLevel = types[i] == Type.DeleteFamily || types[i] == Type.DeleteFamilyVersion;
173+
qual = familyLevel ? HConstants.EMPTY_BYTE_ARRAY : col1;
174+
}
175+
KeyValue kv = types[i] == Type.Put
176+
? new KeyValue(row1, fam1, qual, ts, types[i], data)
177+
: new KeyValue(row1, fam1, qual, ts, types[i]);
178+
actual.add(qm.match(kv));
179+
if (actual.size() >= expected.length) {
180+
break;
181+
}
182+
// Decrement ts for next cell, but keep same ts when the next type has lower type code
183+
// at the same logical position (e.g. DF then DFV at the same timestamp).
184+
if (i + 1 < types.length && types[i + 1].getCode() < types[i].getCode()) {
185+
continue;
186+
}
187+
ts--;
188+
}
189+
for (int i = 0; i < expected.length; i++) {
190+
assertEquals("Mismatch at index " + i, expected[i], actual.get(i));
191+
}
192+
}
193+
78194
private void testDropDeletes(byte[] from, byte[] to, byte[][] rows, MatchCode... expected)
79195
throws IOException {
80196
long now = EnvironmentEdgeManager.currentTime();

0 commit comments

Comments
 (0)