Skip to content

Commit 4bc66e4

Browse files
committed
added validate method into TabletMetadata
1 parent 8557f44 commit 4bc66e4

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

core/src/main/java/org/apache/accumulo/core/metadata/schema/TabletMetadata.java

+54
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import org.apache.accumulo.core.clientImpl.ClientContext;
4343
import org.apache.accumulo.core.data.ByteSequence;
4444
import org.apache.accumulo.core.data.Key;
45+
import org.apache.accumulo.core.data.PartialKey;
46+
import org.apache.accumulo.core.data.Range;
4547
import org.apache.accumulo.core.data.TableId;
4648
import org.apache.accumulo.core.data.Value;
4749
import org.apache.accumulo.core.dataImpl.KeyExtent;
@@ -558,6 +560,58 @@ private static Optional<TServerInstance> checkServer(ClientContext context, Stri
558560
.map(address -> new TServerInstance(address, stat.getEphemeralOwner()));
559561
}
560562

563+
public static void validate(TabletMetadata tm) {
564+
if (tm == null) {
565+
throw new IllegalStateException("TabletMetadata cannot be null");
566+
}
567+
568+
Text prevEndRowText = tm.getPrevEndRow();
569+
Text endRowText = tm.getEndRow();
570+
571+
// Allow validation even if prevEndRow is missing, as long as endRowText exists
572+
Key prevEndRowKey = (prevEndRowText != null) ? new Key(prevEndRowText) : null;
573+
Key endRowKey = (endRowText != null) ? new Key(endRowText) : null;
574+
575+
Collection<StoredTabletFile> files = tm.getFiles();
576+
if (files == null || files.isEmpty()) {
577+
return;
578+
}
579+
580+
for (StoredTabletFile file : files) {
581+
if (!isFileRangeValid(file, prevEndRowKey, endRowKey)) {
582+
throw new IllegalStateException("File range " + file.getRange()
583+
+ " is inconsistent with tablet range [" + prevEndRowText + ", " + endRowText + "]");
584+
}
585+
}
586+
}
587+
588+
private static boolean isFileRangeValid(StoredTabletFile file, Key prevEndRowKey, Key endRowKey) {
589+
Range fileRange = file.getRange();
590+
591+
if (fileRange == null) {
592+
return false;
593+
}
594+
595+
Key fileStartKey = fileRange.getStartKey();
596+
Key fileEndKey = fileRange.getEndKey();
597+
598+
// If start key is null, assume it starts at the beginning of the tablet
599+
if (fileStartKey == null) {
600+
fileStartKey = prevEndRowKey != null ? prevEndRowKey.followingKey(PartialKey.ROW) : null;
601+
}
602+
603+
if (fileEndKey == null) {
604+
fileEndKey = endRowKey;
605+
}
606+
607+
if (prevEndRowKey != null
608+
&& fileStartKey.compareTo(prevEndRowKey.followingKey(PartialKey.ROW)) < 0) {
609+
return false; // File starts before the valid range
610+
}
611+
return endRowKey == null || fileEndKey.compareTo(endRowKey) <= 0; // File extends beyond the
612+
// tablet's end row
613+
}
614+
561615
static class Builder {
562616
private TableId tableId;
563617
private Text prevEndRow;

0 commit comments

Comments
 (0)