You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by Matt Tucker <ma...@jivesoftware.com> on 2003/02/16 23:35:49 UTC

FSDirectory patch for file renaming

Hello all,

For quite some time, we've been experiencing intermittent indexing failures
that look similar to the following:

java.io.IOException: couldn't rename segments.new to segments 
     at
org.apache.lucene.store.FSDirectory.renameFile(Ljava.lang.String;Ljava.lang.
String)V(Unknown Source) 
     at
org.apache.lucene.index.SegmentInfos.write(Lorg.apache.lucene.store.Director
y)V(Unknown Source)

This is caused by File.renameTo failing. From previous run-ins with that
method, it's actually fairly flaky, especially under various Windows JVMs.
The fix is pretty easy -- if File.renameTo fails, manually copy the file
from the old to new destination.

Old code (FSDirectory.renameFile): 

------------------------------
    if (!old.renameTo(nu))
      throw new IOException("couldn't rename " + from + " to " + to);
------------------------------

New code ("buf" variable is declared at top of file as null):

------------------------------
    // Rename the old file to the new one. Unfortunately, the renameTo()
method doesn't
    // work reliably under some JVMs. Therefore, if the rename fails, we
manually rename by
    // copying the old file to the new one.
    if (!old.renameTo(nu)) {
      try {
        java.io.InputStream in = new FileInputStream(old);
        java.io.OutputStream out = new FileOutputStream(nu);
        int len;
        // See if the buf needs to be initialized. Initialization is only
done on-demand
        // since many VM's will never run into the renameTo bug and hence
shouldn't waste
        // 1K of mem for no reason.
        if (buf == null) {
          buf = new byte[1024];
        }
        while ((len = in.read(buf)) >= 0) {
           out.write(buf, 0, len);
        }
        in.close();
        out.close();

        // Delete the old file.
        old.delete();
      }
      catch (IOException ioe) {
        throw new IOException("couldn't rename " + from + " to " + to);
      }
    }
------------------------------

Advantages to this fix:

 * Makes indexing more reliable, especially under VM's where the renameTo
method doesn't always work.
 * Uses no additional memory or resources when File.renameTo works normally.

Disadvantages:

 * It always sucks to add workarounds to bugs in other libraries, but what
is one to do?
 * We've run into File.renameTo issues on multiple machines and VM versions,
but it would be good if others could confirm they've had problems as well. 

I'd suggest this fix as a good candidate for Lucene 1.3 unless others see
problems with it.

Regards,
Matt


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


RE: FSDirectory patch for file renaming

Posted by Matt Tucker <ma...@jivesoftware.com>.
Dmitry,

> Is there a possible other disadvantage that would result in a 
> legitimate 
> error condition being ignored? For instance,
> if there are multiple threads indexing, could they step on each other 
> when they try to update the segments file?

I don't know of any problems like this. Ideally, the file rename would be
atomic (hence Doug's comments to that effect in FSDirectory). However, it's
impossible to make this operation atomic under Java, and the only way to
make the operation robust is to add the manual file copy if renameTo fails.
Also, it's worth noting that FSDirectory.renameFile is synchronized and that
the lock files should take care of multiple VM's trying to do indexing. So,
there really shouldn't be any real error cases we're covering up with the
extra logic.

> Also, what VMs have you experienced these kinds of failures with?

Most often Windows VM's. However, we've seen problems with renameTo on
almost every platform and VM combination (but usually only when renameTo is
being called more often than Lucene generally does). I know that others are
running into these issues from a search through lucene-user
(http://nagoya.apache.org/eyebrowse/ReadMsg?listName=lucene-user@jakarta.apa
che.org&msgId=116687).

Regards,
Matt


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


Re: FSDirectory patch for file renaming

Posted by Dmitry Serebrennikov <dm...@earthlink.net>.
Matt Tucker wrote:

>Advantages to this fix:
>
> * Makes indexing more reliable, especially under VM's where the renameTo
>method doesn't always work.
> * Uses no additional memory or resources when File.renameTo works normally.
>
>Disadvantages:
>
> * It always sucks to add workarounds to bugs in other libraries, but what
>is one to do?
> * We've run into File.renameTo issues on multiple machines and VM versions,
>but it would be good if others could confirm they've had problems as well. 
>
Is there a possible other disadvantage that would result in a legitimate 
error condition being ignored? For instance,
if there are multiple threads indexing, could they step on each other 
when they try to update the segments file?
Also, what VMs have you experienced these kinds of failures with?

>
>I'd suggest this fix as a good candidate for Lucene 1.3 unless others see
>problems with it.
>
>Regards,
>Matt
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: lucene-dev-help@jakarta.apache.org
>
>
>  
>



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


Re: FSDirectory patch for file renaming

Posted by Otis Gospodnetic <ot...@yahoo.com>.
Yeah, I already changed that.  Thanks for checking!

Otis

--- Scott Ganyo <sc...@etapestry.com> wrote:
> I'm ok with this, given the lack of a better option.  Please ensure
> that 
> the input and output streams are closed in a finally block, though.
> 
> Scott
> 
> Otis Gospodnetic wrote:
> 
> >I hate having to make the suggested change for the reasons Matt
> >mentioned as disadvantages below....but what is one to do?
> >I'll change the code shortly.
> >
> >If any other developers are against this change, speak up.
> >
> >Otis
> >
> >
> >--- Matt Tucker <ma...@jivesoftware.com> wrote:
> >  
> >
> >>Hello all,
> >>
> >>For quite some time, we've been experiencing intermittent indexing
> >>failures
> >>that look similar to the following:
> >>
> >>java.io.IOException: couldn't rename segments.new to segments 
> >>     at
> >>
> >>    
> >>
>
>org.apache.lucene.store.FSDirectory.renameFile(Ljava.lang.String;Ljava.lang.
> >  
> >
> >>String)V(Unknown Source) 
> >>     at
> >>
> >>    
> >>
>
>org.apache.lucene.index.SegmentInfos.write(Lorg.apache.lucene.store.Director
> >  
> >
> >>y)V(Unknown Source)
> >>
> >>This is caused by File.renameTo failing. From previous run-ins with
> >>that
> >>method, it's actually fairly flaky, especially under various
> Windows
> >>JVMs.
> >>The fix is pretty easy -- if File.renameTo fails, manually copy the
> >>file
> >>from the old to new destination.
> >>
> >>Old code (FSDirectory.renameFile): 
> >>
> >>------------------------------
> >>    if (!old.renameTo(nu))
> >>      throw new IOException("couldn't rename " + from + " to " +
> to);
> >>------------------------------
> >>
> >>New code ("buf" variable is declared at top of file as null):
> >>
> >>------------------------------
> >>    // Rename the old file to the new one. Unfortunately, the
> >>renameTo()
> >>method doesn't
> >>    // work reliably under some JVMs. Therefore, if the rename
> fails,
> >>we
> >>manually rename by
> >>    // copying the old file to the new one.
> >>    if (!old.renameTo(nu)) {
> >>      try {
> >>        java.io.InputStream in = new FileInputStream(old);
> >>        java.io.OutputStream out = new FileOutputStream(nu);
> >>        int len;
> >>        // See if the buf needs to be initialized. Initialization
> is
> >>only
> >>done on-demand
> >>        // since many VM's will never run into the renameTo bug and
> >>hence
> >>shouldn't waste
> >>        // 1K of mem for no reason.
> >>        if (buf == null) {
> >>          buf = new byte[1024];
> >>        }
> >>        while ((len = in.read(buf)) >= 0) {
> >>           out.write(buf, 0, len);
> >>        }
> >>        in.close();
> >>        out.close();
> >>
> >>        // Delete the old file.
> >>        old.delete();
> >>      }
> >>      catch (IOException ioe) {
> >>        throw new IOException("couldn't rename " + from + " to " +
> >>to);
> >>      }
> >>    }
> >>------------------------------
> >>
> >>Advantages to this fix:
> >>
> >> * Makes indexing more reliable, especially under VM's where the
> >>renameTo
> >>method doesn't always work.
> >> * Uses no additional memory or resources when File.renameTo works
> >>normally.
> >>
> >>Disadvantages:
> >>
> >> * It always sucks to add workarounds to bugs in other libraries,
> but
> >>what
> >>is one to do?
> >> * We've run into File.renameTo issues on multiple machines and VM
> >>versions,
> >>but it would be good if others could confirm they've had problems
> as
> >>well. 
> >>
> >>I'd suggest this fix as a good candidate for Lucene 1.3 unless
> others
> >>see
> >>problems with it.
> >>
> >>Regards,
> >>Matt
> >>
> >>
>
>>---------------------------------------------------------------------
> >>To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
> >>For additional commands, e-mail: lucene-dev-help@jakarta.apache.org
> >>
> >>    
> >>
> >
> >
> >__________________________________________________
> >Do you Yahoo!?
> >Yahoo! Tax Center - forms, calculators, tips, more
> >http://taxes.yahoo.com/
> >
>
>---------------------------------------------------------------------
> >To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
> >For additional commands, e-mail: lucene-dev-help@jakarta.apache.org
> >  
> >
> 
> -- 
> "Perfection in design is not achieved when there is nothing more to
> add, but when there is nothing more to remove." - Antoine de
> Saint-Exup�ry
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: lucene-dev-help@jakarta.apache.org
> 


__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, more
http://taxes.yahoo.com/

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


Re: FSDirectory patch for file renaming

Posted by Scott Ganyo <sc...@etapestry.com>.
I'm ok with this, given the lack of a better option.  Please ensure that 
the input and output streams are closed in a finally block, though.

Scott

Otis Gospodnetic wrote:

>I hate having to make the suggested change for the reasons Matt
>mentioned as disadvantages below....but what is one to do?
>I'll change the code shortly.
>
>If any other developers are against this change, speak up.
>
>Otis
>
>
>--- Matt Tucker <ma...@jivesoftware.com> wrote:
>  
>
>>Hello all,
>>
>>For quite some time, we've been experiencing intermittent indexing
>>failures
>>that look similar to the following:
>>
>>java.io.IOException: couldn't rename segments.new to segments 
>>     at
>>
>>    
>>
>org.apache.lucene.store.FSDirectory.renameFile(Ljava.lang.String;Ljava.lang.
>  
>
>>String)V(Unknown Source) 
>>     at
>>
>>    
>>
>org.apache.lucene.index.SegmentInfos.write(Lorg.apache.lucene.store.Director
>  
>
>>y)V(Unknown Source)
>>
>>This is caused by File.renameTo failing. From previous run-ins with
>>that
>>method, it's actually fairly flaky, especially under various Windows
>>JVMs.
>>The fix is pretty easy -- if File.renameTo fails, manually copy the
>>file
>>from the old to new destination.
>>
>>Old code (FSDirectory.renameFile): 
>>
>>------------------------------
>>    if (!old.renameTo(nu))
>>      throw new IOException("couldn't rename " + from + " to " + to);
>>------------------------------
>>
>>New code ("buf" variable is declared at top of file as null):
>>
>>------------------------------
>>    // Rename the old file to the new one. Unfortunately, the
>>renameTo()
>>method doesn't
>>    // work reliably under some JVMs. Therefore, if the rename fails,
>>we
>>manually rename by
>>    // copying the old file to the new one.
>>    if (!old.renameTo(nu)) {
>>      try {
>>        java.io.InputStream in = new FileInputStream(old);
>>        java.io.OutputStream out = new FileOutputStream(nu);
>>        int len;
>>        // See if the buf needs to be initialized. Initialization is
>>only
>>done on-demand
>>        // since many VM's will never run into the renameTo bug and
>>hence
>>shouldn't waste
>>        // 1K of mem for no reason.
>>        if (buf == null) {
>>          buf = new byte[1024];
>>        }
>>        while ((len = in.read(buf)) >= 0) {
>>           out.write(buf, 0, len);
>>        }
>>        in.close();
>>        out.close();
>>
>>        // Delete the old file.
>>        old.delete();
>>      }
>>      catch (IOException ioe) {
>>        throw new IOException("couldn't rename " + from + " to " +
>>to);
>>      }
>>    }
>>------------------------------
>>
>>Advantages to this fix:
>>
>> * Makes indexing more reliable, especially under VM's where the
>>renameTo
>>method doesn't always work.
>> * Uses no additional memory or resources when File.renameTo works
>>normally.
>>
>>Disadvantages:
>>
>> * It always sucks to add workarounds to bugs in other libraries, but
>>what
>>is one to do?
>> * We've run into File.renameTo issues on multiple machines and VM
>>versions,
>>but it would be good if others could confirm they've had problems as
>>well. 
>>
>>I'd suggest this fix as a good candidate for Lucene 1.3 unless others
>>see
>>problems with it.
>>
>>Regards,
>>Matt
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: lucene-dev-help@jakarta.apache.org
>>
>>    
>>
>
>
>__________________________________________________
>Do you Yahoo!?
>Yahoo! Tax Center - forms, calculators, tips, more
>http://taxes.yahoo.com/
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: lucene-dev-help@jakarta.apache.org
>  
>

-- 
"Perfection in design is not achieved when there is nothing more to add, but when there is nothing more to remove." - Antoine de Saint-Exupéry



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


Re: FSDirectory patch for file renaming

Posted by Otis Gospodnetic <ot...@yahoo.com>.
I hate having to make the suggested change for the reasons Matt
mentioned as disadvantages below....but what is one to do?
I'll change the code shortly.

If any other developers are against this change, speak up.

Otis


--- Matt Tucker <ma...@jivesoftware.com> wrote:
> Hello all,
> 
> For quite some time, we've been experiencing intermittent indexing
> failures
> that look similar to the following:
> 
> java.io.IOException: couldn't rename segments.new to segments 
>      at
>
org.apache.lucene.store.FSDirectory.renameFile(Ljava.lang.String;Ljava.lang.
> String)V(Unknown Source) 
>      at
>
org.apache.lucene.index.SegmentInfos.write(Lorg.apache.lucene.store.Director
> y)V(Unknown Source)
> 
> This is caused by File.renameTo failing. From previous run-ins with
> that
> method, it's actually fairly flaky, especially under various Windows
> JVMs.
> The fix is pretty easy -- if File.renameTo fails, manually copy the
> file
> from the old to new destination.
> 
> Old code (FSDirectory.renameFile): 
> 
> ------------------------------
>     if (!old.renameTo(nu))
>       throw new IOException("couldn't rename " + from + " to " + to);
> ------------------------------
> 
> New code ("buf" variable is declared at top of file as null):
> 
> ------------------------------
>     // Rename the old file to the new one. Unfortunately, the
> renameTo()
> method doesn't
>     // work reliably under some JVMs. Therefore, if the rename fails,
> we
> manually rename by
>     // copying the old file to the new one.
>     if (!old.renameTo(nu)) {
>       try {
>         java.io.InputStream in = new FileInputStream(old);
>         java.io.OutputStream out = new FileOutputStream(nu);
>         int len;
>         // See if the buf needs to be initialized. Initialization is
> only
> done on-demand
>         // since many VM's will never run into the renameTo bug and
> hence
> shouldn't waste
>         // 1K of mem for no reason.
>         if (buf == null) {
>           buf = new byte[1024];
>         }
>         while ((len = in.read(buf)) >= 0) {
>            out.write(buf, 0, len);
>         }
>         in.close();
>         out.close();
> 
>         // Delete the old file.
>         old.delete();
>       }
>       catch (IOException ioe) {
>         throw new IOException("couldn't rename " + from + " to " +
> to);
>       }
>     }
> ------------------------------
> 
> Advantages to this fix:
> 
>  * Makes indexing more reliable, especially under VM's where the
> renameTo
> method doesn't always work.
>  * Uses no additional memory or resources when File.renameTo works
> normally.
> 
> Disadvantages:
> 
>  * It always sucks to add workarounds to bugs in other libraries, but
> what
> is one to do?
>  * We've run into File.renameTo issues on multiple machines and VM
> versions,
> but it would be good if others could confirm they've had problems as
> well. 
> 
> I'd suggest this fix as a good candidate for Lucene 1.3 unless others
> see
> problems with it.
> 
> Regards,
> Matt
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: lucene-dev-help@jakarta.apache.org
> 


__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, more
http://taxes.yahoo.com/

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


Re: FSDirectory patch for file renaming

Posted by Tatu Saloranta <ta...@hypermall.net>.
On Monday 17 February 2003 23:12, Matt Tucker wrote:
> Tatu,
>
> > The only suggestion I have is to encapsulate this workaround
> > in one place, perhaps to a utility class file (util/FileUtil.java?), and
> > only called from the other place(s) (right now that would be just one
>
> place?).
>
> > This way workaround code wouldn't add code clutter to actual
> > functionality, and could
> > be properly commented in utility class itself.
>
> Personally, this seems like overkill to me. There is only a single use of
> File.renameTo in the entire Lucene codebase, so why not keep the workaround

Matter of taste, I guess... and also depends on whether this is ever likely to 
be done from any other place, ever. I usually move things like this to a 
separate class second time I need them. :-)
But that's easier if I'm only person working on certain piece of 
functionality.

Perhaps suggestion would make more sense if I had said that the whole "delete 
old file, rename another file to take its place" operation should be 
refactored out, as it seems to be 'atomic' operation indexer uses.

> code local to that usage? Additionally, we're only talking about a few
> lines of code. Is it really better to add a whole new class?

At this point it'd be a whole new class, but other self-contained file 
operations could easily be moved there as well.

Anyway, it was just a suggestion, so feel free to ignore it if you think it 
doesn't make sense.

-+ Tatu +-


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


RE: FSDirectory patch for file renaming

Posted by Matt Tucker <ma...@jivesoftware.com>.
Tatu,

> The only suggestion I have is to encapsulate this workaround 
> in one place, perhaps to a utility class file (util/FileUtil.java?), and 
> only called from the other place(s) (right now that would be just one
place?). 
> This way workaround code wouldn't add code clutter to actual 
> functionality, and could 
> be properly commented in utility class itself.

Personally, this seems like overkill to me. There is only a single use of
File.renameTo in the entire Lucene codebase, so why not keep the workaround
code local to that usage? Additionally, we're only talking about a few lines
of code. Is it really better to add a whole new class?

Regards,
Matt


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


Re: FSDirectory patch for file renaming

Posted by Tatu Saloranta <ta...@hypermall.net>.
On Monday 17 February 2003 13:18, Otis Gospodnetic wrote:
> Hello Matt,
>
> Is there any way you can make this reproducable consistenly?
> You said renamaTo is 'fairly flaky' - can you make something that will
> always fail?
> I have never seen that fail, but I rarely use (Lucene on) Winblows.
>
> Adding your patch would not be a big problem, but I'd be much happier
> if we could find out what's causing this to fail.
...
> Do other developers have suggestions and/or opionions regarding this?

Sounds like a legitimate workaround to me, esp. since this has been reported 
as a bug to Javasoft by others earlier. It's a shame they can not fix the 
underlying problem, but unfortunately that's a common gripe about Sun's JVM 
development.

The only suggestion I have is to encapsulate this workaround in one place, 
perhaps to a utility class file (util/FileUtil.java?), and only called from 
the other place(s) (right now that would be just one place?). This way 
workaround code wouldn't add code clutter to actual functionality, and could 
be properly commented in utility class itself.

-+ Tatu +-


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


Re: FSDirectory patch for file renaming

Posted by John L Cwikla <cw...@biz360.com>.
>From the java bug parade:

http://developer.java.sun.com/developer/bugParade/bugs/4017593.html


Other points of interest:

I believe some *nix's fail if you try to renameTo across partitions (which
is
theoretically correct since it's not possible, but most mv's use cp/delete
to make up for it)

In Win 9*, renameTo is not atomic like in WinNT variants.


----- Original Message -----
From: "Tatu Saloranta" <ta...@hypermall.net>
To: "Lucene Developers List" <lu...@jakarta.apache.org>
Sent: Tuesday, February 18, 2003 7:54 PM
Subject: Re: FSDirectory patch for file renaming


> This may be getting bit off-topic, but since I'm currently using
renameTo() on
> a project, I was wondering what are exact conditions under which people
have
> seen this problem (I got the impression it's much more common on certain
> platforms etc?).
> So is true that the problem mostly (only?) occurs  when following
conditions
> are met:
>
> (a) A file with name renameTo() would rename another file existed earlier
> (b) 'Old' file is deleted
> (c) renameTo() tries to rename new file to 'old' file name, and native
file
> system for some reason still thinks that file exists?
>
> or are there other cases where same problem occurs? And also, is this more
> frequent on Windows platform? (I haven't seen this on Solaris or Linux,
but I
> haven't had to use renameTo() heavily).
>
> And finally, has anyone seen same problem when renaming directories?
> I'm basically implementing a "real time" (ie. almost instant access to
current
> data) background reindexing with Lucene, and need renameTo() only for
> directory renaming, and was wondering if I should add similar workaround
to my
> code, even before hitting the problem (better safe than sorry).
>
> To make search available without interruptions I'm copying existing index
> directory to a work dir, do updates (for all changed docs remove from +
add
> to index), remove old index and rename working dir to index. During first
> steps old index is still available, and during last steps I'm just
> temporarily sync'ing search engine object to prevent new queries from
using
> that index (that's the main idea -- implementation is bit more complicated
as
> it's possible some searches have already started using that index).
>
> -+ Tatu +-
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: lucene-dev-help@jakarta.apache.org
>



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


Re: FSDirectory patch for file renaming

Posted by Tatu Saloranta <ta...@hypermall.net>.
This may be getting bit off-topic, but since I'm currently using renameTo() on 
a project, I was wondering what are exact conditions under which people have 
seen this problem (I got the impression it's much more common on certain 
platforms etc?).
So is true that the problem mostly (only?) occurs  when following conditions 
are met:

(a) A file with name renameTo() would rename another file existed earlier
(b) 'Old' file is deleted
(c) renameTo() tries to rename new file to 'old' file name, and native file 
system for some reason still thinks that file exists?

or are there other cases where same problem occurs? And also, is this more 
frequent on Windows platform? (I haven't seen this on Solaris or Linux, but I 
haven't had to use renameTo() heavily).

And finally, has anyone seen same problem when renaming directories?
I'm basically implementing a "real time" (ie. almost instant access to current 
data) background reindexing with Lucene, and need renameTo() only for
directory renaming, and was wondering if I should add similar workaround to my 
code, even before hitting the problem (better safe than sorry).

To make search available without interruptions I'm copying existing index 
directory to a work dir, do updates (for all changed docs remove from + add 
to index), remove old index and rename working dir to index. During first 
steps old index is still available, and during last steps I'm just 
temporarily sync'ing search engine object to prevent new queries from using 
that index (that's the main idea -- implementation is bit more complicated as 
it's possible some searches have already started using that index).

-+ Tatu +-


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


RE: FSDirectory patch for file renaming

Posted by Matt Tucker <ma...@jivesoftware.com>.
Dmitry,

> We've also seen errors related to the rename failures, 
> especially when 
> the Windows machine was heavily loaded (a lot of virtual memory use, 
> swapping, etc.). Java bug database has numerous reports about 
> this issue 
> as well, and the general resolution (last time I looked at 
> these bugs) was "too bad".

Yeah, it's an issue that irks me a lot. I've wasted quite a few hours on
it... :( 

> So, I would agree that the patch makes sense, 
> as long as it does not result in strange behavior when the error condition
is 
> legitimate.

I can't think of any strange behavior it would cause. If File.renameTo
fails, it seems pretty natural to just try it another way.

Regards,
Matt


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


Re: FSDirectory patch for file renaming

Posted by Dmitry Serebrennikov <dm...@earthlink.net>.
Matt Tucker wrote:

>Otis,
>
>Yesterday, we had a data set on a particular Windows box that would
>consistently bomb out with the rename error. That's what finally motivated
>me to make the patch. After applying the patch the rebuild worked perfectly.
>I also removed the renameTo method call and tested with my manual copy code
>being used all the time (that also worked perfectly).
>
>The first time we ran into File.renameTo issues was with the config files in
>our product, Jive Forums. The algorithm was:
>
> * Write out new config file as jive_config.tmp
> * Delete old jive_config.xml
> * Rename jive_config.tmp to jive_config.xml using File.renameTo
>
>The rename would bomb out about once very two months for some customers, and
>much more frequently for others. At that point, we wrote a small testing app
>that performed renames again and again, and we were easily able to see the
>error consistently. Since we added the manual rename logic to our own code,
>we haven't seen a problem.
>
>The fact that another user had the error and bothered to email the list
>indicates to me that at least a few people are seeing the issue. Therefore,
>applying the patch seems like a very good idea, especially since it won't
>affect users that don't run into File.renameTo issues at all.
>
We've also seen errors related to the rename failures, especially when 
the Windows machine was heavily loaded (a lot of virtual memory use, 
swapping, etc.). Java bug database has numerous reports about this issue 
as well, and the general resolution (last time I looked at these bugs) 
was "too bad". So, I would agree that the patch makes sense, as long as 
it does not result in strange behavior when the error condition is 
legitimate.

>
>Regards,
>Matt
>
>  
>
>>-----Original Message-----
>>From: Otis Gospodnetic [mailto:otis_gospodnetic@yahoo.com] 
>>Sent: Monday, February 17, 2003 3:19 PM
>>To: Lucene Developers List
>>Subject: Re: FSDirectory patch for file renaming
>>
>>
>>Hello Matt,
>>
>>Is there any way you can make this reproducable consistenly? 
>>You said renamaTo is 'fairly flaky' - can you make something 
>>that will always fail? I have never seen that fail, but I 
>>rarely use (Lucene on) Winblows.
>>
>>Adding your patch would not be a big problem, but I'd be much 
>>happier if we could find out what's causing this to fail. 
>>Does this ever fail in a single-threaded application? (yes, 
>>Lucene's locks and the fact renameFile method in FSDirectory 
>>is synchronized should prevent multiple threads from stepping 
>>at each other toes,
>>but...)
>>
>>Do other developers have suggestions and/or opionions regarding this?
>>
>>Thanks,
>>Otis
>>
>>
>>--- Matt Tucker <ma...@jivesoftware.com> wrote:
>>    
>>
>>>Hello all,
>>>
>>>For quite some time, we've been experiencing intermittent indexing 
>>>failures that look similar to the following:
>>>
>>>java.io.IOException: couldn't rename segments.new to segments 
>>>     at
>>>
>>>      
>>>
>>org.apache.lucene.store.FSDirectory.renameFile(Ljava.lang.Stri
>>ng;Ljava.lang.
>>    
>>
>>>String)V(Unknown Source) 
>>>     at
>>>
>>>      
>>>
>>org.apache.lucene.index.SegmentInfos.write(Lorg.apache.lucene.
>>store.Director
>>    
>>
>>>y)V(Unknown Source)
>>>
>>>This is caused by File.renameTo failing. From previous run-ins with 
>>>that method, it's actually fairly flaky, especially under various 
>>>Windows JVMs.
>>>The fix is pretty easy -- if File.renameTo fails, manually copy the
>>>file
>>>from the old to new destination.
>>>
>>>Old code (FSDirectory.renameFile):
>>>
>>>------------------------------
>>>    if (!old.renameTo(nu))
>>>      throw new IOException("couldn't rename " + from + " 
>>>      
>>>
>>to " + to);
>>    
>>
>>>------------------------------
>>>
>>>New code ("buf" variable is declared at top of file as null):
>>>
>>>------------------------------
>>>    // Rename the old file to the new one. Unfortunately, the
>>>renameTo()
>>>method doesn't
>>>    // work reliably under some JVMs. Therefore, if the 
>>>      
>>>
>>rename fails, 
>>    
>>
>>>we manually rename by
>>>    // copying the old file to the new one.
>>>    if (!old.renameTo(nu)) {
>>>      try {
>>>        java.io.InputStream in = new FileInputStream(old);
>>>        java.io.OutputStream out = new FileOutputStream(nu);
>>>        int len;
>>>        // See if the buf needs to be initialized. Initialization is
>>>only
>>>done on-demand
>>>        // since many VM's will never run into the renameTo bug and
>>>hence
>>>shouldn't waste
>>>        // 1K of mem for no reason.
>>>        if (buf == null) {
>>>          buf = new byte[1024];
>>>        }
>>>        while ((len = in.read(buf)) >= 0) {
>>>           out.write(buf, 0, len);
>>>        }
>>>        in.close();
>>>        out.close();
>>>
>>>        // Delete the old file.
>>>        old.delete();
>>>      }
>>>      catch (IOException ioe) {
>>>        throw new IOException("couldn't rename " + from + " to " + 
>>>to);
>>>      }
>>>    }
>>>------------------------------
>>>
>>>Advantages to this fix:
>>>
>>> * Makes indexing more reliable, especially under VM's where the 
>>>renameTo method doesn't always work.
>>> * Uses no additional memory or resources when File.renameTo works
>>>normally.
>>>
>>>Disadvantages:
>>>
>>> * It always sucks to add workarounds to bugs in other 
>>>      
>>>
>>libraries, but 
>>    
>>
>>>what is one to do?
>>> * We've run into File.renameTo issues on multiple machines and VM
>>>versions,
>>>but it would be good if others could confirm they've had problems as
>>>well. 
>>>
>>>I'd suggest this fix as a good candidate for Lucene 1.3 
>>>      
>>>
>>unless others 
>>    
>>
>>>see problems with it.
>>>
>>>Regards,
>>>Matt
>>>
>>>
>>>
>>>      
>>>
>>---------------------------------------------------------------------
>>    
>>
>>>To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
>>>For additional commands, e-mail: lucene-dev-help@jakarta.apache.org
>>>
>>>      
>>>
>>__________________________________________________
>>Do you Yahoo!?
>>Yahoo! Shopping - Send Flowers for Valentine's Day 
>>    
>>
>http://shopping.yahoo.com
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: lucene-dev-help@jakarta.apache.org
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: lucene-dev-help@jakarta.apache.org
>
>
>  
>



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


Re: FSDirectory patch for file renaming

Posted by Andrzej Bialecki <ab...@getopt.org>.
Matt Tucker wrote:
> Otis,
> 
> Yesterday, we had a data set on a particular Windows box that would
> consistently bomb out with the rename error. That's what finally motivated
> me to make the patch. After applying the patch the rebuild worked perfectly.
> I also removed the renameTo method call and tested with my manual copy code
> being used all the time (that also worked perfectly).

Interesting... One of the causes for renameTo failing is when the other 
file already exists. Could you insert a check for nu.exists() right 
after nu.delete()? Perhaps this check will be sufficient for the problem 
to disappear, because this behaviour looks to me like a timing problem...

This could be a shot in the wild, but could it be caused by bad 
timing/bad use of native API within JVM (e.g. a filesystem caching the 
requests)? Does it behave differently if run on FAT vs. NTFS filesystem? 
This would of course mean a gross violation of File.delete() contract, 
but JVM is just a program and it may contain bugs... Or maybe it's 
Windows that contains bugs, I don't remember... ;-) Does it behave the 
same way in JDK 1.3.x as in JDK 1.4.x?


-- 
Best regards,
Andrzej Bialecki

-------------------------------------------------
Software Architect, System Integration Specialist
-------------------------------------------------
FreeBSD developer (http://www.freebsd.org)



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


RE: FSDirectory patch for file renaming

Posted by Matt Tucker <ma...@jivesoftware.com>.
Otis,

Yesterday, we had a data set on a particular Windows box that would
consistently bomb out with the rename error. That's what finally motivated
me to make the patch. After applying the patch the rebuild worked perfectly.
I also removed the renameTo method call and tested with my manual copy code
being used all the time (that also worked perfectly).

The first time we ran into File.renameTo issues was with the config files in
our product, Jive Forums. The algorithm was:

 * Write out new config file as jive_config.tmp
 * Delete old jive_config.xml
 * Rename jive_config.tmp to jive_config.xml using File.renameTo

The rename would bomb out about once very two months for some customers, and
much more frequently for others. At that point, we wrote a small testing app
that performed renames again and again, and we were easily able to see the
error consistently. Since we added the manual rename logic to our own code,
we haven't seen a problem.

The fact that another user had the error and bothered to email the list
indicates to me that at least a few people are seeing the issue. Therefore,
applying the patch seems like a very good idea, especially since it won't
affect users that don't run into File.renameTo issues at all.

Regards,
Matt

> -----Original Message-----
> From: Otis Gospodnetic [mailto:otis_gospodnetic@yahoo.com] 
> Sent: Monday, February 17, 2003 3:19 PM
> To: Lucene Developers List
> Subject: Re: FSDirectory patch for file renaming
> 
> 
> Hello Matt,
> 
> Is there any way you can make this reproducable consistenly? 
> You said renamaTo is 'fairly flaky' - can you make something 
> that will always fail? I have never seen that fail, but I 
> rarely use (Lucene on) Winblows.
> 
> Adding your patch would not be a big problem, but I'd be much 
> happier if we could find out what's causing this to fail. 
> Does this ever fail in a single-threaded application? (yes, 
> Lucene's locks and the fact renameFile method in FSDirectory 
> is synchronized should prevent multiple threads from stepping 
> at each other toes,
> but...)
> 
> Do other developers have suggestions and/or opionions regarding this?
> 
> Thanks,
> Otis
> 
> 
> --- Matt Tucker <ma...@jivesoftware.com> wrote:
> > Hello all,
> > 
> > For quite some time, we've been experiencing intermittent indexing 
> > failures that look similar to the following:
> > 
> > java.io.IOException: couldn't rename segments.new to segments 
> >      at
> >
> org.apache.lucene.store.FSDirectory.renameFile(Ljava.lang.Stri
> ng;Ljava.lang.
> > String)V(Unknown Source) 
> >      at
> >
> org.apache.lucene.index.SegmentInfos.write(Lorg.apache.lucene.
> store.Director
> > y)V(Unknown Source)
> > 
> > This is caused by File.renameTo failing. From previous run-ins with 
> > that method, it's actually fairly flaky, especially under various 
> > Windows JVMs.
> > The fix is pretty easy -- if File.renameTo fails, manually copy the
> > file
> > from the old to new destination.
> > 
> > Old code (FSDirectory.renameFile):
> > 
> > ------------------------------
> >     if (!old.renameTo(nu))
> >       throw new IOException("couldn't rename " + from + " 
> to " + to);
> > ------------------------------
> > 
> > New code ("buf" variable is declared at top of file as null):
> > 
> > ------------------------------
> >     // Rename the old file to the new one. Unfortunately, the
> > renameTo()
> > method doesn't
> >     // work reliably under some JVMs. Therefore, if the 
> rename fails, 
> > we manually rename by
> >     // copying the old file to the new one.
> >     if (!old.renameTo(nu)) {
> >       try {
> >         java.io.InputStream in = new FileInputStream(old);
> >         java.io.OutputStream out = new FileOutputStream(nu);
> >         int len;
> >         // See if the buf needs to be initialized. Initialization is
> > only
> > done on-demand
> >         // since many VM's will never run into the renameTo bug and
> > hence
> > shouldn't waste
> >         // 1K of mem for no reason.
> >         if (buf == null) {
> >           buf = new byte[1024];
> >         }
> >         while ((len = in.read(buf)) >= 0) {
> >            out.write(buf, 0, len);
> >         }
> >         in.close();
> >         out.close();
> > 
> >         // Delete the old file.
> >         old.delete();
> >       }
> >       catch (IOException ioe) {
> >         throw new IOException("couldn't rename " + from + " to " + 
> > to);
> >       }
> >     }
> > ------------------------------
> > 
> > Advantages to this fix:
> > 
> >  * Makes indexing more reliable, especially under VM's where the 
> > renameTo method doesn't always work.
> >  * Uses no additional memory or resources when File.renameTo works
> > normally.
> > 
> > Disadvantages:
> > 
> >  * It always sucks to add workarounds to bugs in other 
> libraries, but 
> > what is one to do?
> >  * We've run into File.renameTo issues on multiple machines and VM
> > versions,
> > but it would be good if others could confirm they've had problems as
> > well. 
> > 
> > I'd suggest this fix as a good candidate for Lucene 1.3 
> unless others 
> > see problems with it.
> > 
> > Regards,
> > Matt
> > 
> > 
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: lucene-dev-help@jakarta.apache.org
> > 
> 
> 
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Shopping - Send Flowers for Valentine's Day 
http://shopping.yahoo.com

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


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


Re: FSDirectory patch for file renaming

Posted by Otis Gospodnetic <ot...@yahoo.com>.
Hello Matt,

Is there any way you can make this reproducable consistenly?
You said renamaTo is 'fairly flaky' - can you make something that will
always fail?
I have never seen that fail, but I rarely use (Lucene on) Winblows.

Adding your patch would not be a big problem, but I'd be much happier
if we could find out what's causing this to fail.
Does this ever fail in a single-threaded application? (yes, Lucene's
locks and the fact renameFile method in FSDirectory is synchronized
should prevent multiple threads from stepping at each other toes,
but...)

Do other developers have suggestions and/or opionions regarding this?

Thanks,
Otis


--- Matt Tucker <ma...@jivesoftware.com> wrote:
> Hello all,
> 
> For quite some time, we've been experiencing intermittent indexing
> failures
> that look similar to the following:
> 
> java.io.IOException: couldn't rename segments.new to segments 
>      at
>
org.apache.lucene.store.FSDirectory.renameFile(Ljava.lang.String;Ljava.lang.
> String)V(Unknown Source) 
>      at
>
org.apache.lucene.index.SegmentInfos.write(Lorg.apache.lucene.store.Director
> y)V(Unknown Source)
> 
> This is caused by File.renameTo failing. From previous run-ins with
> that
> method, it's actually fairly flaky, especially under various Windows
> JVMs.
> The fix is pretty easy -- if File.renameTo fails, manually copy the
> file
> from the old to new destination.
> 
> Old code (FSDirectory.renameFile): 
> 
> ------------------------------
>     if (!old.renameTo(nu))
>       throw new IOException("couldn't rename " + from + " to " + to);
> ------------------------------
> 
> New code ("buf" variable is declared at top of file as null):
> 
> ------------------------------
>     // Rename the old file to the new one. Unfortunately, the
> renameTo()
> method doesn't
>     // work reliably under some JVMs. Therefore, if the rename fails,
> we
> manually rename by
>     // copying the old file to the new one.
>     if (!old.renameTo(nu)) {
>       try {
>         java.io.InputStream in = new FileInputStream(old);
>         java.io.OutputStream out = new FileOutputStream(nu);
>         int len;
>         // See if the buf needs to be initialized. Initialization is
> only
> done on-demand
>         // since many VM's will never run into the renameTo bug and
> hence
> shouldn't waste
>         // 1K of mem for no reason.
>         if (buf == null) {
>           buf = new byte[1024];
>         }
>         while ((len = in.read(buf)) >= 0) {
>            out.write(buf, 0, len);
>         }
>         in.close();
>         out.close();
> 
>         // Delete the old file.
>         old.delete();
>       }
>       catch (IOException ioe) {
>         throw new IOException("couldn't rename " + from + " to " +
> to);
>       }
>     }
> ------------------------------
> 
> Advantages to this fix:
> 
>  * Makes indexing more reliable, especially under VM's where the
> renameTo
> method doesn't always work.
>  * Uses no additional memory or resources when File.renameTo works
> normally.
> 
> Disadvantages:
> 
>  * It always sucks to add workarounds to bugs in other libraries, but
> what
> is one to do?
>  * We've run into File.renameTo issues on multiple machines and VM
> versions,
> but it would be good if others could confirm they've had problems as
> well. 
> 
> I'd suggest this fix as a good candidate for Lucene 1.3 unless others
> see
> problems with it.
> 
> Regards,
> Matt
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: lucene-dev-help@jakarta.apache.org
> 


__________________________________________________
Do you Yahoo!?
Yahoo! Shopping - Send Flowers for Valentine's Day
http://shopping.yahoo.com

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