1414import org .apache .lucene .store .FSDirectory ;
1515import org .apache .lucene .store .FileSwitchDirectory ;
1616import org .apache .lucene .store .FilterDirectory ;
17+ import org .apache .lucene .store .FilterIndexInput ;
1718import org .apache .lucene .store .IOContext ;
1819import org .apache .lucene .store .IndexInput ;
1920import org .apache .lucene .store .LockFactory ;
3536import org .elasticsearch .plugins .IndexStorePlugin ;
3637
3738import java .io .IOException ;
39+ import java .io .UncheckedIOException ;
3840import java .nio .file .Files ;
3941import java .nio .file .Path ;
4042import java .util .HashSet ;
@@ -171,6 +173,40 @@ protected boolean useDirectIO(String name, IOContext context, OptionalLong fileL
171173
172174 @ Override
173175 public IndexInput openInput (String name , IOContext context ) throws IOException {
176+ // Force normal read advice for stored field temp fdt files:
177+ // (tmp fdt files should only exist when index sorting is enabled)
178+ if (LuceneFilesExtensions .TMP .getExtension ().equals (getExtension (name )) && name .contains ("fdt" )) {
179+ ensureOpen ();
180+ ensureCanRead (name );
181+ var niofsDelegate = super .openInput (name , context );
182+ var ioContext = context ;
183+ return new FilterIndexInput (niofsDelegate .toString (), niofsDelegate ) {
184+
185+ IndexInput directIOInput ;
186+
187+ @ Override
188+ public IndexInput clone () {
189+ // HACK: only StoredFieldsWriter#checkIntegrity() will invoking this clone method for fdt tmp file.
190+ if (directIOInput == null ) {
191+ try {
192+ directIOInput = directIODelegate .openInput (name , ioContext );
193+ } catch (IOException e ) {
194+ throw new UncheckedIOException (e );
195+ }
196+ }
197+ return directIOInput .clone ();
198+ }
199+
200+ @ Override
201+ public void close () throws IOException {
202+ super .close ();
203+ if (directIOInput != null ) {
204+ directIOInput .close ();
205+ }
206+ }
207+ };
208+ }
209+
174210 if (useDelegate (name , context )) {
175211 // we need to do these checks on the outer directory since the inner doesn't know about pending deletes
176212 ensureOpen ();
@@ -181,13 +217,6 @@ public IndexInput openInput(String name, IOContext context) throws IOException {
181217 // we might run into trouble with files that are pendingDelete in one directory but still
182218 // listed in listAll() from the other. We on the other hand don't want to list files from both dirs
183219 // and intersect for perf reasons.
184-
185- // Force normal read advice for stored field temp fdt files:
186- // (tmp fdt files should only exist when index sorting is enabled)
187- if (LuceneFilesExtensions .TMP .getExtension ().equals (getExtension (name )) && name .contains ("fdt" )) {
188- context = context .withReadAdvice (ReadAdvice .NORMAL );
189- }
190-
191220 return delegate .openInput (name , context );
192221 } else {
193222 return super .openInput (name , context );
0 commit comments