Skip to content

Commit fe59fde

Browse files
authored
Backport #14607 to branch_10x (Index open performs version check on each segment, ignores indexCreatedVersionMajor) (#15431)
1 parent bec61ca commit fe59fde

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

lucene/CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ Other
129129
Applications using SecurityManager now need to grant SerializablePermission("serialFilter")
130130
to the analysis-smartcn module. (Uwe Schindler, Isaac David)
131131

132+
* GITHUB#15431: Index open performs version check on each segment, ignores indexCreatedVersionMajor (Rahul Goswami, Mike Sokolov)
133+
132134
Build
133135
---------------------
134136
* Upgrade forbiddenapis to version 3.10. (Uwe Schindler)

lucene/backward-codecs/src/test/org/apache/lucene/backward_index/TestBasicBackwardsCompatibility.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,10 @@ public void testFailOpenOldIndex() throws IOException {
861861
() -> StandardDirectoryReader.open(commit, Version.LATEST.major, null));
862862
assertTrue(
863863
ex.getMessage()
864-
.contains("only supports reading from version " + Version.LATEST.major + " upwards."));
864+
.contains(
865+
"This Lucene version only supports indexes with major version "
866+
+ Version.LATEST.major
867+
+ " or later"));
865868
// now open with allowed min version
866869
StandardDirectoryReader.open(commit, Version.MIN_SUPPORTED_MAJOR, null).close();
867870
}

lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -346,25 +346,12 @@ public static final SegmentInfos readCommit(
346346
input);
347347
}
348348

349-
if (indexCreatedVersion < minSupportedMajorVersion) {
350-
throw new IndexFormatTooOldException(
351-
input,
352-
"This index was initially created with Lucene "
353-
+ indexCreatedVersion
354-
+ ".x while the current version is "
355-
+ Version.LATEST
356-
+ " and Lucene only supports reading"
357-
+ (minSupportedMajorVersion == Version.MIN_SUPPORTED_MAJOR
358-
? " the current and previous major versions"
359-
: " from version " + minSupportedMajorVersion + " upwards"));
360-
}
361-
362349
SegmentInfos infos = new SegmentInfos(indexCreatedVersion);
363350
infos.id = id;
364351
infos.generation = generation;
365352
infos.lastGeneration = generation;
366353
infos.luceneVersion = luceneVersion;
367-
parseSegmentInfos(directory, input, infos, format);
354+
parseSegmentInfos(directory, input, infos, format, minSupportedMajorVersion);
368355
return infos;
369356

370357
} catch (Throwable t) {
@@ -380,7 +367,12 @@ public static final SegmentInfos readCommit(
380367
}
381368

382369
private static void parseSegmentInfos(
383-
Directory directory, DataInput input, SegmentInfos infos, int format) throws IOException {
370+
Directory directory,
371+
DataInput input,
372+
SegmentInfos infos,
373+
int format,
374+
int minSupportedMajorVersion)
375+
throws IOException {
384376
infos.version = CodecUtil.readBELong(input);
385377
// System.out.println("READ sis version=" + infos.version);
386378
infos.counter = input.readVLong();
@@ -397,6 +389,7 @@ private static void parseSegmentInfos(
397389
}
398390

399391
long totalDocs = 0;
392+
400393
for (int seg = 0; seg < numSegments; seg++) {
401394
String segName = input.readString();
402395
byte[] segmentID = new byte[StringHelper.ID_LENGTH];
@@ -490,6 +483,30 @@ private static void parseSegmentInfos(
490483
+ infos.indexCreatedVersionMajor,
491484
input);
492485
}
486+
487+
int createdOrSegmentMinVersion =
488+
info.getMinVersion() == null
489+
? infos.indexCreatedVersionMajor
490+
: info.getMinVersion().major;
491+
492+
// version >=7 are expected to record minVersion
493+
if (info.getMinVersion() == null || info.getMinVersion().major < minSupportedMajorVersion) {
494+
throw new IndexFormatTooOldException(
495+
input,
496+
"Index has segments derived from Lucene version "
497+
+ createdOrSegmentMinVersion
498+
+ ".x and is not supported by Lucene "
499+
+ Version.LATEST
500+
+ ". This Lucene version only supports indexes with major version "
501+
+ minSupportedMajorVersion
502+
+ " or later (found: "
503+
+ createdOrSegmentMinVersion
504+
+ ", minimum supported: "
505+
+ minSupportedMajorVersion
506+
+ "). To resolve this issue re-index your data using Lucene "
507+
+ minSupportedMajorVersion
508+
+ ".x or later.");
509+
}
493510
}
494511

495512
infos.userData = input.readMapOfStrings();
@@ -512,11 +529,13 @@ private static Codec readCodec(DataInput input) throws IOException {
512529
} catch (IllegalArgumentException e) {
513530
// maybe it's an old default codec that moved
514531
if (name.startsWith("Lucene")) {
515-
throw new IllegalArgumentException(
532+
throw new IndexFormatTooOldException(
533+
input,
516534
"Could not load codec '"
517535
+ name
518-
+ "'. Did you forget to add lucene-backward-codecs.jar?",
519-
e);
536+
+ "'. "
537+
+ e.getMessage()
538+
+ ". Did you forget to add lucene-backward-codecs.jar?");
520539
}
521540
throw e;
522541
}

0 commit comments

Comments
 (0)