|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.lucene.backward_index; |
| 18 | + |
| 19 | +import static org.apache.lucene.util.Version.MIN_SUPPORTED_MAJOR; |
| 20 | + |
| 21 | +import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.List; |
| 24 | +import java.util.stream.Collectors; |
| 25 | +import org.apache.lucene.document.Document; |
| 26 | +import org.apache.lucene.document.Field; |
| 27 | +import org.apache.lucene.document.TextField; |
| 28 | +import org.apache.lucene.index.DirectoryReader; |
| 29 | +import org.apache.lucene.index.IndexFormatTooOldException; |
| 30 | +import org.apache.lucene.index.IndexWriter; |
| 31 | +import org.apache.lucene.index.IndexWriterConfig; |
| 32 | +import org.apache.lucene.index.SegmentInfos; |
| 33 | +import org.apache.lucene.index.Term; |
| 34 | +import org.apache.lucene.search.IndexSearcher; |
| 35 | +import org.apache.lucene.search.TermQuery; |
| 36 | +import org.apache.lucene.search.TopDocs; |
| 37 | +import org.apache.lucene.store.Directory; |
| 38 | +import org.apache.lucene.tests.analysis.MockAnalyzer; |
| 39 | +import org.apache.lucene.tests.util.TestUtil; |
| 40 | +import org.apache.lucene.util.Version; |
| 41 | + |
| 42 | +/** |
| 43 | + * Tests for the MIN_SUPPORTED_MAJOR backwards compatibility policy. Verifies that indexes created |
| 44 | + * with major version numbers >= MIN_SUPPORTED_MAJOR can be opened, while older indexes throw |
| 45 | + * IndexFormatTooOldException. |
| 46 | + * |
| 47 | + * <p>This test uses the precreated index archives to validate the policy against real historical |
| 48 | + * indexes. The test automatically adapts when MIN_SUPPORTED_MAJOR changes by using the existing |
| 49 | + * backwards compatibility testing infrastructure. |
| 50 | + */ |
| 51 | +public class TestMinSupportedMajorBackwardsCompatibility extends BackwardsCompatibilityTestBase { |
| 52 | + static final String INDEX_NAME = "index"; |
| 53 | + static final String SUFFIX = "-nocfs"; |
| 54 | + |
| 55 | + public TestMinSupportedMajorBackwardsCompatibility(Version version, String pattern) { |
| 56 | + super(version, pattern); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected void createIndex(Directory directory) throws IOException { |
| 61 | + IndexWriterConfig conf = |
| 62 | + new IndexWriterConfig(new MockAnalyzer(random())) |
| 63 | + .setUseCompoundFile(false) |
| 64 | + .setCodec(TestUtil.getDefaultCodec()); |
| 65 | + try (IndexWriter writer = new IndexWriter(directory, conf)) { |
| 66 | + Document doc = new Document(); |
| 67 | + doc.add(new TextField("content", "test document for version compatibility", Field.Store.YES)); |
| 68 | + writer.addDocument(doc); |
| 69 | + writer.commit(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Uses the standard allVersion() method to dynamically test all available versions. The |
| 75 | + * supportsVersion() override controls which versions are actually tested. |
| 76 | + */ |
| 77 | + @ParametersFactory(argumentFormatting = "Lucene-Version:%1$s; Pattern: %2$s") |
| 78 | + public static Iterable<Object[]> testVersionsFactory() { |
| 79 | + return allVersion(INDEX_NAME, SUFFIX); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + protected boolean supportsVersion(Version version) { |
| 84 | + // Test only versions that should be supported according to MIN_SUPPORTED_MAJOR |
| 85 | + return version.major >= MIN_SUPPORTED_MAJOR; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Test that indexes created with versions >= MIN_SUPPORTED_MAJOR can be opened and searched. This |
| 90 | + * validates that the relaxed policy correctly allows older but supported indexes. |
| 91 | + */ |
| 92 | + public void testSupportedVersionCanBeOpened() throws IOException { |
| 93 | + // The base class setup ensures we only get here for supported versions |
| 94 | + assertTrue("Version should be supported", version.major >= MIN_SUPPORTED_MAJOR); |
| 95 | + |
| 96 | + // This should succeed for any version >= MIN_SUPPORTED_MAJOR |
| 97 | + try (DirectoryReader reader = DirectoryReader.open(directory)) { |
| 98 | + // Basic validation that the index can be read |
| 99 | + assertTrue("Index should be readable", reader.numDocs() >= 0); |
| 100 | + |
| 101 | + // Verify we can read the SegmentInfos |
| 102 | + SegmentInfos sis = SegmentInfos.readLatestCommit(directory); |
| 103 | + assertTrue( |
| 104 | + "Creation version should be >= MIN_SUPPORTED_MAJOR", |
| 105 | + sis.getIndexCreatedVersionMajor() >= MIN_SUPPORTED_MAJOR); |
| 106 | + |
| 107 | + // Test basic search functionality if there are documents |
| 108 | + if (reader.numDocs() > 0) { |
| 109 | + IndexSearcher searcher = new IndexSearcher(reader); |
| 110 | + TopDocs hits = searcher.search(new TermQuery(new Term("content", "test")), 10); |
| 111 | + // Note: we can't guarantee the specific content, but should be able to search |
| 112 | + assertNotNull("Search should return results", hits); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Test unsupported versions by dynamically finding versions below MIN_SUPPORTED_MAJOR. This |
| 119 | + * validates that both DirectoryReader and IndexWriter properly throw IndexFormatTooOldException |
| 120 | + * for versions below MIN_SUPPORTED_MAJOR. |
| 121 | + */ |
| 122 | + public void testUnsupportedVersionsThrowException() throws IOException { |
| 123 | + // Find all versions that should be unsupported |
| 124 | + List<Version> allVersions = getAllCurrentReleasedVersionsAndCurrent(); |
| 125 | + List<Version> unsupportedVersions = |
| 126 | + allVersions.stream() |
| 127 | + .filter(v -> v.major < MIN_SUPPORTED_MAJOR) |
| 128 | + .collect(Collectors.toList()); |
| 129 | + |
| 130 | + if (unsupportedVersions.isEmpty()) { |
| 131 | + // If there are no unsupported versions (e.g., MIN_SUPPORTED_MAJOR is very low), |
| 132 | + // we can't test this behavior |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + // Test each unsupported version |
| 137 | + for (Version unsupportedVersion : unsupportedVersions) { |
| 138 | + String indexName = |
| 139 | + String.format( |
| 140 | + java.util.Locale.ROOT, createPattern(INDEX_NAME, SUFFIX), unsupportedVersion); |
| 141 | + java.io.InputStream resource = |
| 142 | + TestAncientIndicesCompatibility.class.getResourceAsStream(indexName); |
| 143 | + |
| 144 | + if (resource == null) { |
| 145 | + // Skip versions that don't have archives (this is expected for some older versions) |
| 146 | + continue; |
| 147 | + } |
| 148 | + |
| 149 | + java.nio.file.Path tempDir = createTempDir("unsupported-" + unsupportedVersion); |
| 150 | + TestUtil.unzip(resource, tempDir); |
| 151 | + |
| 152 | + try (org.apache.lucene.store.Directory dir = newFSDirectory(tempDir)) { |
| 153 | + // Test DirectoryReader fails for unsupported versions |
| 154 | + IndexFormatTooOldException readerException = |
| 155 | + expectThrows( |
| 156 | + IndexFormatTooOldException.class, |
| 157 | + () -> { |
| 158 | + DirectoryReader.open(dir); |
| 159 | + }); |
| 160 | + |
| 161 | + // Verify the exception message contains useful information |
| 162 | + String readerMessage = readerException.getMessage(); |
| 163 | + assertNotNull( |
| 164 | + "Reader exception message should not be null for version " + unsupportedVersion, |
| 165 | + readerMessage); |
| 166 | + assertTrue( |
| 167 | + "Reader exception message should contain version information for " |
| 168 | + + unsupportedVersion |
| 169 | + + ": " |
| 170 | + + readerMessage, |
| 171 | + readerMessage.length() > 0); |
| 172 | + |
| 173 | + // Test IndexWriter creation also fails appropriately |
| 174 | + IndexWriterConfig config = new IndexWriterConfig(); |
| 175 | + IndexFormatTooOldException writerException = |
| 176 | + expectThrows( |
| 177 | + IndexFormatTooOldException.class, |
| 178 | + () -> { |
| 179 | + new IndexWriter(dir, config); |
| 180 | + }); |
| 181 | + |
| 182 | + String writerMessage = writerException.getMessage(); |
| 183 | + assertNotNull( |
| 184 | + "Writer exception message should not be null for version " + unsupportedVersion, |
| 185 | + writerMessage); |
| 186 | + assertTrue( |
| 187 | + "Writer exception message should contain version information for " |
| 188 | + + unsupportedVersion |
| 189 | + + ": " |
| 190 | + + writerMessage, |
| 191 | + writerMessage.length() > 0); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Test that validates the constant MIN_SUPPORTED_MAJOR has the expected value. This test will |
| 198 | + * fail if someone accidentally changes the constant without considering the implications. |
| 199 | + */ |
| 200 | + public void testMinSupportedMajorConstantValue() { |
| 201 | + // This test validates that MIN_SUPPORTED_MAJOR is set to the expected value |
| 202 | + // If this test fails, it likely means the constant was changed and the |
| 203 | + // implications need to be considered (tests updated, documentation updated, etc.) |
| 204 | + assertEquals( |
| 205 | + "MIN_SUPPORTED_MAJOR should be 10 for the relaxed upgrade policy", 10, MIN_SUPPORTED_MAJOR); |
| 206 | + |
| 207 | + // Additional validation: MIN_SUPPORTED_MAJOR should be <= current major |
| 208 | + assertTrue( |
| 209 | + "MIN_SUPPORTED_MAJOR should not exceed current major version", |
| 210 | + MIN_SUPPORTED_MAJOR <= Version.LATEST.major); |
| 211 | + |
| 212 | + assertTrue("MIN_SUPPORTED_MAJOR should be positive", MIN_SUPPORTED_MAJOR > 0); |
| 213 | + } |
| 214 | +} |
0 commit comments