You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by "gokaai (via GitHub)" <gi...@apache.org> on 2023/12/03 12:44:33 UTC

[PR] Enable CheckIndex to exorcise segments with missing segment infos (.si) (#7820) [lucene]

gokaai opened a new pull request, #12872:
URL: https://github.com/apache/lucene/pull/12872

   ### Description
   
   Changes: 
   
   - Allow CheckIndex to exorcise an index with missing segment info (.si) file
   - Unit tests for exorciseIndex()
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Enable CheckIndex to exorcise segments with missing segment infos (.si) (#7820) [lucene]

Posted by "mikemccand (via GitHub)" <gi...@apache.org>.
mikemccand commented on code in PR #12872:
URL: https://github.com/apache/lucene/pull/12872#discussion_r1426907008


##########
lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java:
##########
@@ -389,13 +389,39 @@ private static void parseSegmentInfos(
     }
 
     long totalDocs = 0;
+    SegmentInfo info;
     for (int seg = 0; seg < numSegments; seg++) {
       String segName = input.readString();
       byte[] segmentID = new byte[StringHelper.ID_LENGTH];
       input.readBytes(segmentID, 0, segmentID.length);
       Codec codec = readCodec(input);
-      SegmentInfo info =
-          codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      try {
+        info = codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      } catch (FileNotFoundException | NoSuchFileException e) {
+        // https://github.com/apache/lucene/issues/7820 - If we throw a CorruptIndex exception here,
+        // a missing segment info will cause parseSegmentInfos() to short-circuit and stop reading
+        // the rest of the segments. So, when encountering a segment with a missing
+        // segment info (.si) file, we initialize an empty segment info (containing 0 docs)
+        // to keep track of the segment name, so that we can continue reading other segments, and
+        // the segment with the missing .si file can be later validated or exorcised by CheckIndex()
+        info =

Review Comment:
   > * Throwing an exception in `CheckIndex` short-circuits the flow that makes it possible for `-exorcise` to do it's thing
   
   Hmm this should still work.  The exception should be thrown at a low level, when the codec is trying to read the `.si` file.  This is then caught by `CheckIndex`, and it knows which segment is affected by checking `instanceof` of the exception and then extracting the segment name.  Then it can report the root cause and/or exorcise just fine?
   
   > * Since we don't do a deeper level check on segments which aren't the latest segment, the recently added unit test where we expect prior commits to throw an exception if corrupt, does not pass
   
   Hmm why is that?  Codec should throw an exception when we try to load that (non-latest) SIS too?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Enable CheckIndex to exorcise segments with missing segment infos (.si) (#7820) [lucene]

Posted by "gokaai (via GitHub)" <gi...@apache.org>.
gokaai commented on code in PR #12872:
URL: https://github.com/apache/lucene/pull/12872#discussion_r1419442594


##########
lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java:
##########
@@ -389,13 +389,39 @@ private static void parseSegmentInfos(
     }
 
     long totalDocs = 0;
+    SegmentInfo info;
     for (int seg = 0; seg < numSegments; seg++) {
       String segName = input.readString();
       byte[] segmentID = new byte[StringHelper.ID_LENGTH];
       input.readBytes(segmentID, 0, segmentID.length);
       Codec codec = readCodec(input);
-      SegmentInfo info =
-          codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      try {
+        info = codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      } catch (FileNotFoundException | NoSuchFileException e) {

Review Comment:
   Yes - will catch all exceptions and throw a new, specific `CorruptSegmentInfoException`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Enable CheckIndex to exorcise segments with missing segment infos (.si) (#7820) [lucene]

Posted by "gokaai (via GitHub)" <gi...@apache.org>.
gokaai commented on code in PR #12872:
URL: https://github.com/apache/lucene/pull/12872#discussion_r1440324120


##########
lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java:
##########
@@ -389,13 +389,26 @@ private static void parseSegmentInfos(
     }
 
     long totalDocs = 0;
+    SegmentInfo info;
     for (int seg = 0; seg < numSegments; seg++) {
       String segName = input.readString();
       byte[] segmentID = new byte[StringHelper.ID_LENGTH];
       input.readBytes(segmentID, 0, segmentID.length);
       Codec codec = readCodec(input);
-      SegmentInfo info =
-          codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      try {
+        info = codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      } catch (Throwable t) {
+        if (infoStream != null) {
+          message(t.getMessage());
+        }
+        if (t instanceof NoSuchFileException || t instanceof FileNotFoundException) {
+          throw new CorruptSegmentInfoException(
+              "segment info file: " + segName + ".si cannot be read - it may be missing or corrupt",
+              input);
+        } else {
+          throw t;

Review Comment:
   I tried catching all Throwable t here but it causes `TestIndexWrtier#testThreadInterruptDeadlock` to fail as it doesn't throw `ThreadInterruptExceptions` 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Throw CorruptSegmentInfoException on encountering missing segment info (_N.si) file in CheckIndex [lucene]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #12872:
URL: https://github.com/apache/lucene/pull/12872#issuecomment-1925496330

   This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the dev@lucene.apache.org list. Thank you for your contribution!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Throw CorruptSegmentInfoException on encountering missing segment info (_N.si) file in CheckIndex [lucene]

Posted by "mikemccand (via GitHub)" <gi...@apache.org>.
mikemccand commented on PR #12872:
URL: https://github.com/apache/lucene/pull/12872#issuecomment-1933954630

   > > Thanks @gokaai -- I'll try to review soon!
   > > If possible please try not to force-push: it removes the history of the past commits and makes it harder to see what changed on this iteration :)
   > 
   > Sorry about th
   
   Ahh OK no worries.  I experience such `git` disasters myself far more often than I would hope :)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Enable CheckIndex to exorcise segments with missing segment infos (.si) (#7820) [lucene]

Posted by "gokaai (via GitHub)" <gi...@apache.org>.
gokaai commented on PR #12872:
URL: https://github.com/apache/lucene/pull/12872#issuecomment-1863127752

   Decided to tackle this in smaller steps - 
   - Made a new (simpler) revision where we throw a more specific error on encountering missing segment info. Created a new exception `CorruptSegmentInfoException.java`
   - Will create a new commit (or issue?) to add in fixes and unit tests for exorciseIndex


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Throw CorruptSegmentInfoException on encountering missing segment info (_N.si) file in CheckIndex [lucene]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #12872:
URL: https://github.com/apache/lucene/pull/12872#issuecomment-1960551768

   This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the dev@lucene.apache.org list. Thank you for your contribution!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Throw CorruptSegmentInfoException on encountering missing segment info (_N.si) file in CheckIndex [lucene]

Posted by "dweiss (via GitHub)" <gi...@apache.org>.
dweiss commented on PR #12872:
URL: https://github.com/apache/lucene/pull/12872#issuecomment-1934784006

   > > > Thanks @gokaai -- I'll try to review soon!
   > > > If possible please try not to force-push: it removes the history of the past commits and makes it harder to see what changed on this iteration :)
   > > 
   > > 
   > > Sorry about th
   > 
   > Ahh OK no worries. I experience such `git` disasters myself far more often than I would hope :)
   
   Look at what git reflog does and how it may help. It is really, really difficult to not be able to recover your work with git. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Throw CorruptSegmentInfoException on encountering missing segment info (_N.si) file in CheckIndex [lucene]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #12872:
URL: https://github.com/apache/lucene/pull/12872#issuecomment-3413341797

   This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the dev@lucene.apache.org list. Thank you for your contribution!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Enable CheckIndex to exorcise segments with missing segment infos (.si) (#7820) [lucene]

Posted by "mikemccand (via GitHub)" <gi...@apache.org>.
mikemccand commented on code in PR #12872:
URL: https://github.com/apache/lucene/pull/12872#discussion_r1437564242


##########
lucene/core/src/java/org/apache/lucene/index/CheckIndex.java:
##########
@@ -664,7 +667,7 @@ public int compare(String a, String b) {
     // failure (fastFail == false).  so if we get here, we should // always have a valid lastCommit:
     assert lastCommit != null;
 
-    if (lastCommit == null) {
+    if (lastCommit == null) { // TODO: Remove/move unreachable if statement?

Review Comment:
   Ahh you are right!  And looking at the code, unless there is a bug, `lastCommit` should never be null.  So I think an `assert` is appropriate and we should remove the `if`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Enable CheckIndex to exorcise segments with missing segment infos (.si) (#7820) [lucene]

Posted by "gokaai (via GitHub)" <gi...@apache.org>.
gokaai commented on code in PR #12872:
URL: https://github.com/apache/lucene/pull/12872#discussion_r1420277534


##########
lucene/core/src/java/org/apache/lucene/index/CheckIndex.java:
##########
@@ -957,6 +974,9 @@ private Status.SegmentInfoStatus testSegment(
     SegmentReader reader = null;
 
     try {
+      if (info.info.maxDoc() == 0) {
+        throw new CheckIndexException(" this segment has missing or corrupt segment info");

Review Comment:
   > Remove the extra space before this
   
   I was keeping it consistent with [this exception message](https://github.com/apache/lucene/blob/ba81826951c1f628efb60c6165add27601173bd9/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java#L949) - should I change this one too? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Throw CorruptSegmentInfoException on encountering missing segment info (_N.si) file in CheckIndex [lucene]

Posted by "mikemccand (via GitHub)" <gi...@apache.org>.
mikemccand commented on PR #12872:
URL: https://github.com/apache/lucene/pull/12872#issuecomment-1927677782

   Thanks @gokaai -- I'll try to review soon!
   
   If possible please try not to force-push: it removes the history of the past commits and makes it harder to see what changed on this iteration :)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Enable CheckIndex to exorcise segments with missing segment infos (.si) (#7820) [lucene]

Posted by "mikemccand (via GitHub)" <gi...@apache.org>.
mikemccand commented on code in PR #12872:
URL: https://github.com/apache/lucene/pull/12872#discussion_r1426901678


##########
lucene/core/src/java/org/apache/lucene/index/CheckIndex.java:
##########
@@ -957,6 +974,9 @@ private Status.SegmentInfoStatus testSegment(
     SegmentReader reader = null;
 
     try {
+      if (info.info.maxDoc() == 0) {
+        throw new CheckIndexException(" this segment has missing or corrupt segment info");

Review Comment:
   Ahh yes please?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Enable CheckIndex to exorcise segments with missing segment infos (.si) (#7820) [lucene]

Posted by "gokaai (via GitHub)" <gi...@apache.org>.
gokaai commented on code in PR #12872:
URL: https://github.com/apache/lucene/pull/12872#discussion_r1419439605


##########
lucene/core/src/java/org/apache/lucene/index/CheckIndex.java:
##########
@@ -4270,11 +4290,21 @@ public int doCheck(Options opts) throws IOException, InterruptedException {
                 + result.totLoseDocCount
                 + " documents would be lost, if -exorcise were specified\n");
       } else {
-        opts.out.println("WARNING: " + result.totLoseDocCount + " documents will be lost\n");
-        opts.out.println(
-            "NOTE: will write new segments file in 5 seconds; this will remove "
-                + result.totLoseDocCount
-                + " docs from the index. YOU WILL LOSE DATA. THIS IS YOUR LAST CHANCE TO CTRL+C!");
+        if (result.numBadSegmentsWithoutSegmentInfo > 0) {
+          opts.out.println(
+              "WARNING: " + result.totLoseDocCount + " or more documents will be lost\n");

Review Comment:
   Makes sense! Will rephrase as 'at least' in all occurrences 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Enable CheckIndex to exorcise segments with missing segment infos (.si) (#7820) [lucene]

Posted by "gokaai (via GitHub)" <gi...@apache.org>.
gokaai commented on code in PR #12872:
URL: https://github.com/apache/lucene/pull/12872#discussion_r1419454517


##########
lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java:
##########
@@ -389,13 +389,39 @@ private static void parseSegmentInfos(
     }
 
     long totalDocs = 0;
+    SegmentInfo info;
     for (int seg = 0; seg < numSegments; seg++) {
       String segName = input.readString();
       byte[] segmentID = new byte[StringHelper.ID_LENGTH];
       input.readBytes(segmentID, 0, segmentID.length);
       Codec codec = readCodec(input);
-      SegmentInfo info =
-          codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      try {
+        info = codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      } catch (FileNotFoundException | NoSuchFileException e) {
+        // https://github.com/apache/lucene/issues/7820 - If we throw a CorruptIndex exception here,
+        // a missing segment info will cause parseSegmentInfos() to short-circuit and stop reading
+        // the rest of the segments. So, when encountering a segment with a missing
+        // segment info (.si) file, we initialize an empty segment info (containing 0 docs)
+        // to keep track of the segment name, so that we can continue reading other segments, and
+        // the segment with the missing .si file can be later validated or exorcised by CheckIndex()
+        info =

Review Comment:
   Thanks for reviewing @mikemccand ! What I find tricky here are two things - 
    - Throwing an exception in `CheckIndex` short-circuits the flow that makes it possible for `-exorcise` to do it's thing
    - Since we don't do a deeper level check on segments which aren't the latest segment, the recently added unit test where we expect prior commits to throw an exception if corrupt, does not pass
   
   I'll make a new revision trying to address both of these things + your comments



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Throw CorruptSegmentInfoException on encountering missing segment info (_N.si) file in CheckIndex [lucene]

Posted by "gokaai (via GitHub)" <gi...@apache.org>.
gokaai commented on PR #12872:
URL: https://github.com/apache/lucene/pull/12872#issuecomment-1900475365

   I edited the PR description to more accurately represent what is being done. We aren't dealing with the exorcism issue yet, but throwing the right exception on encountering missing segment info. I will tackle the final step (actually making exorcism possible) in a subsequent PR


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Enable CheckIndex to exorcise segments with missing segment infos (.si) (#7820) [lucene]

Posted by "mikemccand (via GitHub)" <gi...@apache.org>.
mikemccand commented on code in PR #12872:
URL: https://github.com/apache/lucene/pull/12872#discussion_r1437565302


##########
lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java:
##########
@@ -389,13 +389,39 @@ private static void parseSegmentInfos(
     }
 
     long totalDocs = 0;
+    SegmentInfo info;
     for (int seg = 0; seg < numSegments; seg++) {
       String segName = input.readString();
       byte[] segmentID = new byte[StringHelper.ID_LENGTH];
       input.readBytes(segmentID, 0, segmentID.length);
       Codec codec = readCodec(input);
-      SegmentInfo info =
-          codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      try {
+        info = codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      } catch (FileNotFoundException | NoSuchFileException e) {

Review Comment:
   Exception handling is tricky!  I'm not sure what is the right exact set of exceptions to catch here ... but it should be broader than just the "no such file" exceptions I think.  Corruption in `.si` can result in exotic exceptions...



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Enable CheckIndex to exorcise segments with missing segment infos (.si) (#7820) [lucene]

Posted by "gokaai (via GitHub)" <gi...@apache.org>.
gokaai commented on code in PR #12872:
URL: https://github.com/apache/lucene/pull/12872#discussion_r1419455705


##########
lucene/core/src/java/org/apache/lucene/index/CheckIndex.java:
##########
@@ -664,7 +667,7 @@ public int compare(String a, String b) {
     // failure (fastFail == false).  so if we get here, we should // always have a valid lastCommit:
     assert lastCommit != null;
 
-    if (lastCommit == null) {
+    if (lastCommit == null) { // TODO: Remove/move unreachable if statement?

Review Comment:
   But we already assert `lastCommit != null` just before



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Throw CorruptSegmentInfoException on encountering missing segment info (_N.si) file in CheckIndex [lucene]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #12872:
URL: https://github.com/apache/lucene/pull/12872#issuecomment-5349615718

   This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the dev@lucene.apache.org list. Thank you for your contribution!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Enable CheckIndex to exorcise segments with missing segment infos (.si) (#7820) [lucene]

Posted by "gokaai (via GitHub)" <gi...@apache.org>.
gokaai commented on code in PR #12872:
URL: https://github.com/apache/lucene/pull/12872#discussion_r1431602197


##########
lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java:
##########
@@ -389,13 +389,39 @@ private static void parseSegmentInfos(
     }
 
     long totalDocs = 0;
+    SegmentInfo info;
     for (int seg = 0; seg < numSegments; seg++) {
       String segName = input.readString();
       byte[] segmentID = new byte[StringHelper.ID_LENGTH];
       input.readBytes(segmentID, 0, segmentID.length);
       Codec codec = readCodec(input);
-      SegmentInfo info =
-          codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      try {
+        info = codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      } catch (FileNotFoundException | NoSuchFileException e) {

Review Comment:
   How about `IOException`? Tried catching all `Throwable t` here but it causes `TestIndexWrtier#testThreadInterruptDeadlock` to fail as it catches `ThreadInterruptExceptions` when it shouldn't 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Enable CheckIndex to exorcise segments with missing segment infos (.si) (#7820) [lucene]

Posted by "mikemccand (via GitHub)" <gi...@apache.org>.
mikemccand commented on code in PR #12872:
URL: https://github.com/apache/lucene/pull/12872#discussion_r1435749878


##########
lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java:
##########
@@ -389,13 +389,26 @@ private static void parseSegmentInfos(
     }
 
     long totalDocs = 0;
+    SegmentInfo info;
     for (int seg = 0; seg < numSegments; seg++) {
       String segName = input.readString();
       byte[] segmentID = new byte[StringHelper.ID_LENGTH];
       input.readBytes(segmentID, 0, segmentID.length);
       Codec codec = readCodec(input);
-      SegmentInfo info =
-          codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      try {
+        info = codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      } catch (Throwable t) {
+        if (infoStream != null) {
+          message(t.getMessage());
+        }
+        if (t instanceof NoSuchFileException || t instanceof FileNotFoundException) {
+          throw new CorruptSegmentInfoException(
+              "segment info file: " + segName + ".si cannot be read - it may be missing or corrupt",
+              input);
+        } else {
+          throw t;

Review Comment:
   Hmm why not also `CorruptSegmentInfoException` in this case too?



##########
lucene/core/src/java/org/apache/lucene/index/CorruptSegmentInfoException.java:
##########
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.index;
+
+import org.apache.lucene.store.DataInput;
+import org.apache.lucene.store.DataOutput;
+
+/** This exception is thrown when Lucene detects an inconsistency in the index. */

Review Comment:
   Can we improve the javadoc to state when this particular class is used (to express corruption when loading a single `_N.si` file?).



##########
lucene/core/src/java/org/apache/lucene/index/CorruptSegmentInfoException.java:
##########
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.index;
+
+import org.apache.lucene.store.DataInput;
+import org.apache.lucene.store.DataOutput;
+
+/** This exception is thrown when Lucene detects an inconsistency in the index. */
+public class CorruptSegmentInfoException extends CorruptIndexException {
+
+  /** Create exception with a message only */
+  public CorruptSegmentInfoException(String message, DataInput input) {

Review Comment:
   Shouldn't this class also take the `segmentName` so `CheckIndex` can do something interesting with it (know which segment to exorcise, report which segment's `.si` is broken, etc.)?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Throw CorruptSegmentInfoException on encountering missing segment info (_N.si) file in CheckIndex [lucene]

Posted by "gokaai (via GitHub)" <gi...@apache.org>.
gokaai commented on PR #12872:
URL: https://github.com/apache/lucene/pull/12872#issuecomment-1933683163

   > Thanks @gokaai -- I'll try to review soon!
   > 
   > If possible please try not to force-push: it removes the history of the past commits and makes it harder to see what changed on this iteration :)
   
   Sorry about that @mikemccand . I messed up my workspace and couldn't salvage it - I'll keep it in mind while making any new revisions.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Throw CorruptSegmentInfoException on encountering missing segment info (_N.si) file in CheckIndex [lucene]

Posted by "serhiy-bzhezytskyy (via GitHub)" <gi...@apache.org>.
serhiy-bzhezytskyy commented on PR #12872:
URL: https://github.com/apache/lucene/pull/12872#issuecomment-5158402613

   This PR looks stalled — no longer mergeable, and the last comment from a human is from February 2024 — but the problem it fixes is still there, so I have carried it forward in #16476 rather than let it sit.
   
   Your commit is the first one there, rebased onto `main` with your authorship intact, and you are credited in `CHANGES.txt`. Two hunks needed adjusting: `codec.segmentInfoFormat().read(...)` now takes `IOContext.READONCE` on `main`, and the wildcard import had to go back to explicit ones for the ecj lint. `CheckIndex.java` and `TestTransactions.java` merged cleanly.
   
   On top of it I finished the two review points from this thread that the current diff does not cover:
   
   - The root cause was being dropped. `CorruptSegmentInfoException` has a constructor taking a `Throwable cause`, but the throw site calls the three-argument one — and that cause is the only thing naming the file (`NoSuchFileException` with the full path for a deleted `.si`, a suppressed `EOFException` naming the `MemorySegmentIndexInput` for a truncated one). It is passed now, and the catch is `Exception | AssertionError` per @mikemccand's comment about exotic exceptions.
   - `segmentName` was package-private with no accessor, so nothing outside `org.apache.lucene.index` could read the name the exception exists to carry, and no test could assert on it. There is a `getSegmentName()` now, and a test covering a deleted and a truncated `.si`.
   
   @gokaai if you would rather finish it here, say so and I will close #16476 — the point is the fix landing, not where. The `exorciseIndex` half that you deferred is #16474.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Enable CheckIndex to exorcise segments with missing segment infos (.si) (#7820) [lucene]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #12872:
URL: https://github.com/apache/lucene/pull/12872#issuecomment-1897533423

   This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the dev@lucene.apache.org list. Thank you for your contribution!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Throw CorruptSegmentInfoException on encountering missing segment info (_N.si) file in CheckIndex [lucene]

Posted by "mikemccand (via GitHub)" <gi...@apache.org>.
mikemccand commented on code in PR #12872:
URL: https://github.com/apache/lucene/pull/12872#discussion_r1478678480


##########
lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java:
##########
@@ -389,13 +386,25 @@ private static void parseSegmentInfos(
     }
 
     long totalDocs = 0;
+    SegmentInfo info;
     for (int seg = 0; seg < numSegments; seg++) {
       String segName = input.readString();
       byte[] segmentID = new byte[StringHelper.ID_LENGTH];
       input.readBytes(segmentID, 0, segmentID.length);
       Codec codec = readCodec(input);
-      SegmentInfo info =
-          codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      try {
+        info = codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      } catch (ThreadInterruptedException e) {
+        throw e;
+      } catch (Exception e) {

Review Comment:
   Thanks @gokaai!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Enable CheckIndex to exorcise segments with missing segment infos (.si) (#7820) [lucene]

Posted by "gokaai (via GitHub)" <gi...@apache.org>.
gokaai commented on code in PR #12872:
URL: https://github.com/apache/lucene/pull/12872#discussion_r1459040185


##########
lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java:
##########
@@ -389,13 +386,25 @@ private static void parseSegmentInfos(
     }
 
     long totalDocs = 0;
+    SegmentInfo info;
     for (int seg = 0; seg < numSegments; seg++) {
       String segName = input.readString();
       byte[] segmentID = new byte[StringHelper.ID_LENGTH];
       input.readBytes(segmentID, 0, segmentID.length);
       Codec codec = readCodec(input);
-      SegmentInfo info =
-          codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      try {
+        info = codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      } catch (ThreadInterruptedException e) {
+        throw e;
+      } catch (Exception e) {

Review Comment:
   Catching a general-purpose exception here was tricky !
   - `TestIndexWrtier#testThreadInterruptDeadlock` expected a ThreadInterruptedException to be thrown as is
   - `TestTransactions#testTransactions` expected an 'on-purpose' IOException to be thrown as is



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Enable CheckIndex to exorcise segments with missing segment infos (.si) (#7820) [lucene]

Posted by "mikemccand (via GitHub)" <gi...@apache.org>.
mikemccand commented on code in PR #12872:
URL: https://github.com/apache/lucene/pull/12872#discussion_r1418947126


##########
lucene/core/src/java/org/apache/lucene/index/CheckIndex.java:
##########
@@ -873,13 +876,26 @@ public int compare(String a, String b) {
     if (0 == result.numBadSegments) {
       result.clean = true;
     } else {
-      msg(
-          infoStream,
-          "WARNING: "
-              + result.numBadSegments
-              + " broken segments (containing "
-              + result.totLoseDocCount
-              + " documents) detected");
+      if (result.numBadSegmentsWithoutSegmentInfo > 0) {
+        msg(
+            infoStream,
+            "WARNING: "
+                + result.numBadSegments
+                + " broken segments (containing "

Review Comment:
   Maybe say `containing at least`?



##########
lucene/core/src/java/org/apache/lucene/index/CheckIndex.java:
##########
@@ -664,7 +667,7 @@ public int compare(String a, String b) {
     // failure (fastFail == false).  so if we get here, we should // always have a valid lastCommit:
     assert lastCommit != null;
 
-    if (lastCommit == null) {
+    if (lastCommit == null) { // TODO: Remove/move unreachable if statement?

Review Comment:
   Hmm this might still happen?  E.g. some transient `IOException` or an Access Denied or so?



##########
lucene/core/src/java/org/apache/lucene/index/CheckIndex.java:
##########
@@ -957,6 +974,9 @@ private Status.SegmentInfoStatus testSegment(
     SegmentReader reader = null;
 
     try {
+      if (info.info.maxDoc() == 0) {
+        throw new CheckIndexException(" this segment has missing or corrupt segment info");

Review Comment:
   Remove the extra space before `this`?
   
   Also, can we include specifics about which segment we were unable to read the `.si` for in the exception message?



##########
lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java:
##########
@@ -389,13 +389,39 @@ private static void parseSegmentInfos(
     }
 
     long totalDocs = 0;
+    SegmentInfo info;
     for (int seg = 0; seg < numSegments; seg++) {
       String segName = input.readString();
       byte[] segmentID = new byte[StringHelper.ID_LENGTH];
       input.readBytes(segmentID, 0, segmentID.length);
       Codec codec = readCodec(input);
-      SegmentInfo info =
-          codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      try {
+        info = codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      } catch (FileNotFoundException | NoSuchFileException e) {

Review Comment:
   Hmm can we upgrade this to catch any "normal" (not crazy things like OOME) `Throwable`?  E.g. if the `.si` file had some bits scrambled or what not we will get a more exotic exception or maybe an exception because the checksum is mismatched.
   
   Can we somehow return the root cause exception here and include it in `CheckIndexException` that we raise above?



##########
lucene/core/src/java/org/apache/lucene/index/CheckIndex.java:
##########
@@ -957,6 +974,9 @@ private Status.SegmentInfoStatus testSegment(
     SegmentReader reader = null;
 
     try {
+      if (info.info.maxDoc() == 0) {

Review Comment:
   Ahh I see, you now separately handle the `maxDoc == 0` case, here.  But, what if an index has an illegal 0 doc segment, and an intact `.si`?  We will now (incorrectly) state that it is missing its `SegmentInfo`?  Maybe we could add a separate boolean somewhere to track `segmentInfoMissing` or so?



##########
lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java:
##########
@@ -389,13 +389,39 @@ private static void parseSegmentInfos(
     }
 
     long totalDocs = 0;
+    SegmentInfo info;
     for (int seg = 0; seg < numSegments; seg++) {
       String segName = input.readString();
       byte[] segmentID = new byte[StringHelper.ID_LENGTH];
       input.readBytes(segmentID, 0, segmentID.length);
       Codec codec = readCodec(input);
-      SegmentInfo info =
-          codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      try {
+        info = codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
+      } catch (FileNotFoundException | NoSuchFileException e) {
+        // https://github.com/apache/lucene/issues/7820 - If we throw a CorruptIndex exception here,
+        // a missing segment info will cause parseSegmentInfos() to short-circuit and stop reading
+        // the rest of the segments. So, when encountering a segment with a missing
+        // segment info (.si) file, we initialize an empty segment info (containing 0 docs)
+        // to keep track of the segment name, so that we can continue reading other segments, and
+        // the segment with the missing .si file can be later validated or exorcised by CheckIndex()
+        info =

Review Comment:
   Hmm I don't like that this will suppress corruption when `SegmentInfos` is being loaded NOT from `CheckIndex`?  This is the wrong layer to have such leniency?  This method should throw the exception back up to the caller and caller should handle properly?
   
   Could we instead 1) create a subclass of `CorruptIndexException`, maybe `CorruptSegmentInfoException` or so, 2) require that caller pass in the segment name, and root cause exception, when creating it, and 3) catch any `Throwable` here and re-throw that subclass?
   
   Then we can fix `CheckIndex` to specifically catch that, pull the segment name out, and do its thing.



##########
lucene/core/src/java/org/apache/lucene/index/CheckIndex.java:
##########
@@ -948,7 +965,7 @@ private Status.SegmentInfoStatus testSegment(
     segInfoStat.maxDoc = info.info.maxDoc();
 
     final Version version = info.info.getVersion();
-    if (info.info.maxDoc() <= 0) {
+    if (info.info.maxDoc() < 0) {

Review Comment:
   Hmm why did you have to remove the `0` case?  Lucene considers this corruption (IW should never flush or merge to a 0 doc segment).



##########
lucene/core/src/java/org/apache/lucene/index/CheckIndex.java:
##########
@@ -4270,11 +4290,21 @@ public int doCheck(Options opts) throws IOException, InterruptedException {
                 + result.totLoseDocCount
                 + " documents would be lost, if -exorcise were specified\n");
       } else {
-        opts.out.println("WARNING: " + result.totLoseDocCount + " documents will be lost\n");
-        opts.out.println(
-            "NOTE: will write new segments file in 5 seconds; this will remove "
-                + result.totLoseDocCount
-                + " docs from the index. YOU WILL LOSE DATA. THIS IS YOUR LAST CHANCE TO CTRL+C!");
+        if (result.numBadSegmentsWithoutSegmentInfo > 0) {
+          opts.out.println(
+              "WARNING: " + result.totLoseDocCount + " or more documents will be lost\n");

Review Comment:
   Can we change the wording to `at least ...`?  I think that's stronger / more alarming on casual read than ` or more`.
   
   Or maybe `more than XX documents will be lost`, since we (almost certainly) know the lost segment(s) have `maxDoc > 0`?
   
   Whichever wording we use, let's be consistent in the thrown exception message above too.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org