From 1be4d75c4201b5ebae3b77b06f907856d44bfb63 Mon Sep 17 00:00:00 2001 From: Prudhvi Godithi Date: Mon, 1 Dec 2025 14:43:59 -0800 Subject: [PATCH 1/5] Add support for SuppressedFormats Signed-off-by: Prudhvi Godithi --- .../codecs/asserting/AssertingCodec.java | 31 +++++++++++++++---- .../lucene/tests/util/LuceneTestCase.java | 14 +++++++++ .../util/TestRuleSetupAndRestoreClassEnv.java | 9 ++++++ 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingCodec.java b/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingCodec.java index 0aded31855e6..661590c3afa1 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingCodec.java +++ b/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingCodec.java @@ -16,6 +16,9 @@ */ package org.apache.lucene.tests.codecs.asserting; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; import org.apache.lucene.codecs.DocValuesFormat; import org.apache.lucene.codecs.FilterCodec; import org.apache.lucene.codecs.KnnVectorsFormat; @@ -33,6 +36,22 @@ /** Acts like the default codec but with additional asserts. */ public class AssertingCodec extends FilterCodec { + private static volatile Set suppressedFormats = Collections.emptySet(); + + /** Set the formats to suppress. Use simple class names like "AssertingStoredFieldsFormat". */ + public static void setSuppressedFormats(Set formats) { + suppressedFormats = new HashSet<>(formats); + } + + /** Clear all suppressed formats. */ + public static void clearSuppressedFormats() { + suppressedFormats = Collections.emptySet(); + } + + private static boolean isSuppressed(String formatName) { + return suppressedFormats.contains(formatName); + } + static void assertThread(String object, Thread creationThread) { if (creationThread != Thread.currentThread()) { throw new AssertionError( @@ -90,12 +109,12 @@ public PostingsFormat postingsFormat() { @Override public TermVectorsFormat termVectorsFormat() { - return vectors; + return isSuppressed("AssertingTermVectorsFormat") ? delegate.termVectorsFormat() : vectors; } @Override public StoredFieldsFormat storedFieldsFormat() { - return storedFields; + return isSuppressed("AssertingStoredFieldsFormat") ? delegate.storedFieldsFormat() : storedFields; } @Override @@ -105,22 +124,22 @@ public DocValuesFormat docValuesFormat() { @Override public NormsFormat normsFormat() { - return norms; + return isSuppressed("AssertingNormsFormat") ? delegate.normsFormat() : norms; } @Override public LiveDocsFormat liveDocsFormat() { - return liveDocs; + return isSuppressed("AssertingLiveDocsFormat") ? delegate.liveDocsFormat() : liveDocs; } @Override public PointsFormat pointsFormat() { - return pointsFormat; + return isSuppressed("AssertingPointsFormat") ? delegate.pointsFormat() : pointsFormat; } @Override public KnnVectorsFormat knnVectorsFormat() { - return knnVectorsFormat; + return isSuppressed("AssertingKnnVectorsFormat") ? delegate.knnVectorsFormat() : knnVectorsFormat; } @Override diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java index 00177cb2300c..be851f5e6c33 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java +++ b/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java @@ -350,6 +350,20 @@ public abstract class LuceneTestCase extends Assert { String[] value(); } + /** + * Annotation for test classes that should avoid specific asserting formats within the Asserting + * codec (e.g., AssertingStoredFieldsFormat) while keeping other asserting formats active. + * + *

Use the simple class name of the format to suppress, e.g., "AssertingStoredFieldsFormat". + */ + @Documented + @Inherited + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.TYPE) + public @interface SuppressAssertingFormats { + String[] value(); + } + /** * Annotation for test classes that should avoid mock filesystem types (because they test a bug * that only happens on linux, for example). diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java b/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java index e4c0625e7c55..e47b96d44a0c 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java +++ b/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java @@ -51,6 +51,7 @@ import org.apache.lucene.tests.search.similarities.AssertingSimilarity; import org.apache.lucene.tests.search.similarities.RandomSimilarity; import org.apache.lucene.tests.util.LuceneTestCase.LiveIWCFlushMode; +import org.apache.lucene.tests.util.LuceneTestCase.SuppressAssertingFormats; import org.apache.lucene.tests.util.LuceneTestCase.SuppressCodecs; import org.apache.lucene.util.InfoStream; import org.apache.lucene.util.PrintStreamInfoStream; @@ -127,6 +128,14 @@ protected void before() throws Exception { avoidCodecs.addAll(Arrays.asList(a.value())); } + // Process SuppressAssertingFormats annotation + if (targetClass.isAnnotationPresent(SuppressAssertingFormats.class)) { + SuppressAssertingFormats a = targetClass.getAnnotation(SuppressAssertingFormats.class); + AssertingCodec.setSuppressedFormats(new HashSet<>(Arrays.asList(a.value()))); + } else { + AssertingCodec.clearSuppressedFormats(); + } + savedCodec = Codec.getDefault(); int randomVal = random.nextInt(11); if ("default".equals(TEST_CODEC)) { From db82f8a5e03dc5f95f49f92b9c7ef0eae8ae71b9 Mon Sep 17 00:00:00 2001 From: Prudhvi Godithi Date: Mon, 1 Dec 2025 14:53:48 -0800 Subject: [PATCH 2/5] Fix assertions Signed-off-by: Prudhvi Godithi --- .../tests/codecs/asserting/AssertingCodec.java | 9 +++------ .../util/TestRuleSetupAndRestoreClassEnv.java | 14 ++++++++++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingCodec.java b/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingCodec.java index 661590c3afa1..4ef15ad4b923 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingCodec.java +++ b/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingCodec.java @@ -40,12 +40,9 @@ public class AssertingCodec extends FilterCodec { /** Set the formats to suppress. Use simple class names like "AssertingStoredFieldsFormat". */ public static void setSuppressedFormats(Set formats) { - suppressedFormats = new HashSet<>(formats); - } - - /** Clear all suppressed formats. */ - public static void clearSuppressedFormats() { - suppressedFormats = Collections.emptySet(); + suppressedFormats = formats == null || formats.isEmpty() + ? Collections.emptySet() + : new HashSet<>(formats); } private static boolean isSuppressed(String formatName) { diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java b/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java index e47b96d44a0c..287960aabeff 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java +++ b/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java @@ -77,6 +77,11 @@ final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule { */ HashSet avoidCodecs; + /** + * @see SuppressAssertingFormats + */ + HashSet avoidAssertingFormats; + static class ThreadNameFixingPrintStreamInfoStream extends PrintStreamInfoStream { public ThreadNameFixingPrintStreamInfoStream(PrintStream out) { super(out); @@ -128,14 +133,15 @@ protected void before() throws Exception { avoidCodecs.addAll(Arrays.asList(a.value())); } - // Process SuppressAssertingFormats annotation + avoidAssertingFormats = new HashSet<>(); if (targetClass.isAnnotationPresent(SuppressAssertingFormats.class)) { SuppressAssertingFormats a = targetClass.getAnnotation(SuppressAssertingFormats.class); - AssertingCodec.setSuppressedFormats(new HashSet<>(Arrays.asList(a.value()))); - } else { - AssertingCodec.clearSuppressedFormats(); + avoidAssertingFormats.addAll(Arrays.asList(a.value())); } + // Set suppressed asserting formats before codec creation + AssertingCodec.setSuppressedFormats(avoidAssertingFormats); + savedCodec = Codec.getDefault(); int randomVal = random.nextInt(11); if ("default".equals(TEST_CODEC)) { From 55c60858f20f04c4b146776b74ea9511b903ad3a Mon Sep 17 00:00:00 2001 From: Prudhvi Godithi Date: Tue, 2 Dec 2025 14:15:15 -0800 Subject: [PATCH 3/5] Add support for SuppressedFormats Signed-off-by: Prudhvi Godithi --- .../tests/codecs/asserting/AssertingCodec.java | 13 ++++++++----- .../tests/util/TestRuleSetupAndRestoreClassEnv.java | 1 - 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingCodec.java b/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingCodec.java index 4ef15ad4b923..19184f9e1ca0 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingCodec.java +++ b/lucene/test-framework/src/java/org/apache/lucene/tests/codecs/asserting/AssertingCodec.java @@ -40,9 +40,8 @@ public class AssertingCodec extends FilterCodec { /** Set the formats to suppress. Use simple class names like "AssertingStoredFieldsFormat". */ public static void setSuppressedFormats(Set formats) { - suppressedFormats = formats == null || formats.isEmpty() - ? Collections.emptySet() - : new HashSet<>(formats); + suppressedFormats = + formats == null || formats.isEmpty() ? Collections.emptySet() : new HashSet<>(formats); } private static boolean isSuppressed(String formatName) { @@ -111,7 +110,9 @@ public TermVectorsFormat termVectorsFormat() { @Override public StoredFieldsFormat storedFieldsFormat() { - return isSuppressed("AssertingStoredFieldsFormat") ? delegate.storedFieldsFormat() : storedFields; + return isSuppressed("AssertingStoredFieldsFormat") + ? delegate.storedFieldsFormat() + : storedFields; } @Override @@ -136,7 +137,9 @@ public PointsFormat pointsFormat() { @Override public KnnVectorsFormat knnVectorsFormat() { - return isSuppressed("AssertingKnnVectorsFormat") ? delegate.knnVectorsFormat() : knnVectorsFormat; + return isSuppressed("AssertingKnnVectorsFormat") + ? delegate.knnVectorsFormat() + : knnVectorsFormat; } @Override diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java b/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java index 287960aabeff..5319dc5a72c5 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java +++ b/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java @@ -139,7 +139,6 @@ protected void before() throws Exception { avoidAssertingFormats.addAll(Arrays.asList(a.value())); } - // Set suppressed asserting formats before codec creation AssertingCodec.setSuppressedFormats(avoidAssertingFormats); savedCodec = Codec.getDefault(); From a046547ac4567f1323f0ea1425aea84ced30cc80 Mon Sep 17 00:00:00 2001 From: Prudhvi Godithi Date: Tue, 2 Dec 2025 14:48:52 -0800 Subject: [PATCH 4/5] Code cleanup Signed-off-by: Prudhvi Godithi --- .../lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java | 1 - 1 file changed, 1 deletion(-) diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java b/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java index 5319dc5a72c5..506cb210ba92 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java +++ b/lucene/test-framework/src/java/org/apache/lucene/tests/util/TestRuleSetupAndRestoreClassEnv.java @@ -138,7 +138,6 @@ protected void before() throws Exception { SuppressAssertingFormats a = targetClass.getAnnotation(SuppressAssertingFormats.class); avoidAssertingFormats.addAll(Arrays.asList(a.value())); } - AssertingCodec.setSuppressedFormats(avoidAssertingFormats); savedCodec = Codec.getDefault(); From 794f208e5b707c57366194ac90f0cd8717f1a03f Mon Sep 17 00:00:00 2001 From: Prudhvi Godithi Date: Tue, 2 Dec 2025 17:04:29 -0800 Subject: [PATCH 5/5] Update changes.txt Signed-off-by: Prudhvi Godithi --- lucene/CHANGES.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 918901d2c773..57f2d8df5e58 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -186,6 +186,8 @@ New Features * GITHUB#15415: Add fallback support to Lucene104ScalarQuantizedVectorsFormat getFloatVectorValues when there are no full-precision vectors present (Pulkit Gupta) + * GITHUB#15468: Add support for `@SuppressAssertingFormats` annotation for fine-grained control over `AssertingCodec` formats (Prudhvi Godithi) + Improvements --------------------- * GITHUB#15148: Add support uint8 distance and allow 8 bit scalar quantization (Trevor McCulloch)