You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Trejkaz <tr...@trypticon.org> on 2016/02/05 02:48:24 UTC

Is there a way to share IndexReader data sensibly across independent callers?

Hi all.

Suppose 100 independent callers are opening the same index like this:

    try (Directory directory = FSDirectory.open(path1);
         IndexReader reader = DirectoryReader.open(directory))
    {
        // keeps reader open for a long time
    }

Someone complains that we're using a lot of memory, because each
IndexReader has its own cache.
We don't have any context that would tell us about other threads in
the same JVM that might have opened the same reader, other than it
ultimately going through our own code in both cases.

Is there a proper way to share the underlying IndexReader data? I
don't particularly care whether it's the same IndexReader but maybe it
would help if it's at least the same SegmentReaders...

Our existing attempts to use ReaderManager are not really working
because there are issues with figuring out when it's okay to close a
Directory. It seems like the answer is "never", because the caller who
passes in the first one might still be using it. But if you never
close it (which does seem to be the right way), Windows ruins your
plans by refusing to let you delete the files.

TX

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


Re: Is there a way to share IndexReader data sensibly across independent callers?

Posted by Trejkaz <tr...@trypticon.org>.
So it turns out I still have problems.

I wanted to return a proxy reader that the caller could close like
normal. I wanted to do this for two reasons:

1. This:

    try (IndexReader reader = sharer.acquireReader(...)) {
        ...
    }

    Looks much nicer than this:

    IndexReader reader = sharer.acquireReader(...));
    try {
        ...
    } finally {
        sharer.releaseReader(reader);
    }

    It isn't entirely clear from ReaderManager's docs whether you
*have* to call release or whether calling close() on the reader is
acceptable. Maybe it's fine to call close(), which removes some (but
not all) wrapping we are doing.

2. There have been bugs in the past where somehow readers got closed
more than once. I was hoping to stomp these out by giving out a
different reader to each caller so that we can track which ones have
already been closed and reject the second attempt. And indeed, since
implementing this, we haven't seen that sort of issue occur.

But supposedly the performance isn't good enough. The main cost
appears to be that creating what I thought should be a lightweight
wrapper is surprisingly expensive, because Lucene is maintaining a map
of which readers use which other reasons, which uses
System.identityHashCode, which is apparently an expensive call. (We
always see it inside this, not inside the other methods in
WeakHashMap. I guess native calls are just expensive?)

    java.lang.Thread.State: RUNNABLE
        at java.lang.System.identityHashCode(Native Method)
        at org.apache.lucene.index.IndexReader.hashCode(IndexReader.java:302)
        at java.util.WeakHashMap.hash(WeakHashMap.java:298)
        at java.util.WeakHashMap.put(WeakHashMap.java:449)
        at java.util.Collections$SetFromMap.add(Collections.java:5461)
        at java.util.Collections$SynchronizedCollection.add(Collections.java:2035)
        - locked <0x0000000084067fe8> (a java.util.Collections$SynchronizedSet)
        at org.apache.lucene.index.IndexReader.registerParentReader(IndexReader.java:138)
        at org.apache.lucene.index.BaseCompositeReader.<init>(BaseCompositeReader.java:77)
        at org.apache.lucene.index.DirectoryReader.<init>(DirectoryReader.java:310)
        at org.apache.lucene.index.FilterDirectoryReader.<init>(FilterDirectoryReader.java:83)
        at IndexReaderSharer$CloseForwardingDirectoryReader.<init>(IndexReaderSharer:184)

So I'm back to another problem where now I want to reduce the number
of times I create this wrapping reader, yet I don't want to give the
same wrapping reader object out to more than one caller because one of
them could close it twice. I guess I could add yet another layer of
wrapping, make my own IndexReader interface and implement a proxy to
that which is actually cheap, but again it's going to involve writing
hard reference counting code so I'm wondering if there is another way
to avoid this whole mess.

TX

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


Re: Is there a way to share IndexReader data sensibly across independent callers?

Posted by Trejkaz <tr...@trypticon.org>.
On Wed, Feb 10, 2016 at 3:17 AM, Michael McCandless
<lu...@mikemccandless.com> wrote:
> Why do you need to close the Directory?  It should be light weight.
> But if you really do need it, can't you subclass ReaderManager and
> override afterClose to close the directory?

I guess that's the next thing I'll try out. I am already subclassing
ReaderManager to wrap the readers after discovering that reader caches
don't work if I wrap from outside it.

As for the need to close it, it's true, in production we don't really
need to. But we happen to be using a Directory implementation that
checks that callers close things, and that check is only triggered
when we close the Directory at the moment. So it's just something that
adds diagnostics to help resolve other warnings about files not being
closed.

> So you essentially need to "lazy close" your ReaderManager, when there
> are no searches currently needing it?

That's more or less the right way to think about it. Actually each
session might make more than one search using the same reader and
reuse the same one for those, but when no sessions are running for
that index anymore I wanted to close it because Windows has annoying
file locking for read operations. If it weren't for Windows...

> Why not have a sync'd block, with a reference to the ReaderManager.
> Inside that block, if the reference is null, that means it's closed,
> and you open a new one.  Else, use the existing one.  Won't that do
> what you need w/o requiring full fledged reference counts?  Yes, it is
> a sync'd block around acquire/release, but I don't see how that can be
> avoided, and it'd be fast when the ReaderManager is already opened.

This is roughly what I currently have. At the moment I lock my entire
acquire() / release() methods, which maybe it's possible to reduce,
although I'm not entirely sure.

Concurrency is fiddly...

TX

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


Re: Is there a way to share IndexReader data sensibly across independent callers?

Posted by Michael McCandless <lu...@mikemccandless.com>.
Why do you need to close the Directory?  It should be light weight.
But if you really do need it, can't you subclass ReaderManager and
override afterClose to close the directory?

So you essentially need to "lazy close" your ReaderManager, when there
are no searches currently needing it?

Why not have a sync'd block, with a reference to the ReaderManager.
Inside that block, if the reference is null, that means it's closed,
and you open a new one.  Else, use the existing one.  Won't that do
what you need w/o requiring full fledged reference counts?  Yes, it is
a sync'd block around acquire/release, but I don't see how that can be
avoided, and it'd be fast when the ReaderManager is already opened.

Mike McCandless

http://blog.mikemccandless.com


On Tue, Feb 9, 2016 at 6:39 AM, Trejkaz <tr...@trypticon.org> wrote:
> On Tue, Feb 9, 2016 at 7:59 PM, Michael McCandless
> <lu...@mikemccandless.com> wrote:
>> Can't you just call ReaderManager.close?
>>
>> All in-flight operations with that RM will keep working, and the
>> underlying reader will only finally close once they have all finished.
>
> I guess that has the caveat that it would be possible to have two
> readers open on the same directory, which is mostly what I was trying
> to avoid. My current solution absolutely prevents that, at the cost of
> having to synchronise when acquiring or releasing, although I can
> probably use double-checked locking to reduce the impact of that.
>
> Really what would be handy is something that resembles ReaderManager
> but takes Path for every method and also opens and closes the
> Directory...
>
> TX
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>

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


Re: Is there a way to share IndexReader data sensibly across independent callers?

Posted by Trejkaz <tr...@trypticon.org>.
On Tue, Feb 9, 2016 at 7:59 PM, Michael McCandless
<lu...@mikemccandless.com> wrote:
> Can't you just call ReaderManager.close?
>
> All in-flight operations with that RM will keep working, and the
> underlying reader will only finally close once they have all finished.

I guess that has the caveat that it would be possible to have two
readers open on the same directory, which is mostly what I was trying
to avoid. My current solution absolutely prevents that, at the cost of
having to synchronise when acquiring or releasing, although I can
probably use double-checked locking to reduce the impact of that.

Really what would be handy is something that resembles ReaderManager
but takes Path for every method and also opens and closes the
Directory...

TX

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


Re: Is there a way to share IndexReader data sensibly across independent callers?

Posted by Michael McCandless <lu...@mikemccandless.com>.
Can't you just call ReaderManager.close?

All in-flight operations with that RM will keep working, and the
underlying reader will only finally close once they have all finished.

Mike McCandless

http://blog.mikemccandless.com


On Tue, Feb 9, 2016 at 12:12 AM, Trejkaz <tr...@trypticon.org> wrote:
> On Tue, Feb 9, 2016 at 2:10 AM, Sanne Grinovero
> <sa...@gmail.com> wrote:
>> Hi,
>> you should really try to reuse the same opened Directory, like you
>> suggest without closing it until your application is "done" with it in
>> all its threads (normally on application shutdown).
>> Keeping a Directory open will not lead to have open files, that is
>> probably caused by not closing the instances of IndexReader.
>>
>> I'd highly recommend to use the ReaderManager for these reasons,
>> especially because handling these details across different threads
>> both correctly and efficiently can be tricky - I've learned that
>> myself when implementing similar things before the ReaderManager was
>> created.
>
> I'm already using ReaderManager, but there are issues.
>
> I want to close it when the last acquired index has been released, but
> no thread knows anything about what indexes other threads could be
> using, yet we still want indexes to be closed once nobody is using
> them. So I end up having to reference count the ReaderManager, which
> seems to defeat the purpose of using it in the first place since I
> could just reference count the reader itself. I wish it could handle
> automatically closing and reopening the index by itself, but I don't
> think it can.
>
> At the moment I have bolted this additional level of reference
> counting around ReaderManager and it just creates a new ReaderManager
> when the reference count goes back up to 1 and closes it when it goes
> back to 0. But this blob has to be synchronised to implement it safely
> and the map for looking these things up can never clean out entries,
> because I couldn't find a safe way to do that even using
> ConcurrentMap.
>
> TX
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>

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


Re: Is there a way to share IndexReader data sensibly across independent callers?

Posted by Trejkaz <tr...@trypticon.org>.
On Tue, Feb 9, 2016 at 2:10 AM, Sanne Grinovero
<sa...@gmail.com> wrote:
> Hi,
> you should really try to reuse the same opened Directory, like you
> suggest without closing it until your application is "done" with it in
> all its threads (normally on application shutdown).
> Keeping a Directory open will not lead to have open files, that is
> probably caused by not closing the instances of IndexReader.
>
> I'd highly recommend to use the ReaderManager for these reasons,
> especially because handling these details across different threads
> both correctly and efficiently can be tricky - I've learned that
> myself when implementing similar things before the ReaderManager was
> created.

I'm already using ReaderManager, but there are issues.

I want to close it when the last acquired index has been released, but
no thread knows anything about what indexes other threads could be
using, yet we still want indexes to be closed once nobody is using
them. So I end up having to reference count the ReaderManager, which
seems to defeat the purpose of using it in the first place since I
could just reference count the reader itself. I wish it could handle
automatically closing and reopening the index by itself, but I don't
think it can.

At the moment I have bolted this additional level of reference
counting around ReaderManager and it just creates a new ReaderManager
when the reference count goes back up to 1 and closes it when it goes
back to 0. But this blob has to be synchronised to implement it safely
and the map for looking these things up can never clean out entries,
because I couldn't find a safe way to do that even using
ConcurrentMap.

TX

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


Re: Is there a way to share IndexReader data sensibly across independent callers?

Posted by Sanne Grinovero <sa...@gmail.com>.
Hi,
you should really try to reuse the same opened Directory, like you
suggest without closing it until your application is "done" with it in
all its threads (normally on application shutdown).
Keeping a Directory open will not lead to have open files, that is
probably caused by not closing the instances of IndexReader.

I'd highly recommend to use the ReaderManager for these reasons,
especially because handling these details across different threads
both correctly and efficiently can be tricky - I've learned that
myself when implementing similar things before the ReaderManager was
created.

HTH,
Sanne

On 5 February 2016 at 01:48, Trejkaz <tr...@trypticon.org> wrote:
> Hi all.
>
> Suppose 100 independent callers are opening the same index like this:
>
>     try (Directory directory = FSDirectory.open(path1);
>          IndexReader reader = DirectoryReader.open(directory))
>     {
>         // keeps reader open for a long time
>     }
>
> Someone complains that we're using a lot of memory, because each
> IndexReader has its own cache.
> We don't have any context that would tell us about other threads in
> the same JVM that might have opened the same reader, other than it
> ultimately going through our own code in both cases.
>
> Is there a proper way to share the underlying IndexReader data? I
> don't particularly care whether it's the same IndexReader but maybe it
> would help if it's at least the same SegmentReaders...
>
> Our existing attempts to use ReaderManager are not really working
> because there are issues with figuring out when it's okay to close a
> Directory. It seems like the answer is "never", because the caller who
> passes in the first one might still be using it. But if you never
> close it (which does seem to be the right way), Windows ruins your
> plans by refusing to let you delete the files.
>
> TX
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>

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