You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@lucy.apache.org by goran kent <go...@gmail.com> on 2011/11/07 09:36:23 UTC
[lucy-user] Splicing in a bit of caching in remote searcher
Hi,
I'm considering changing our established caching mechanism to allow
for more nimble cache refreshing (ie, when the backend indexes change
beyond threshold X). Instead of caching using our reverse-proxy
cluster, I'd like to cache the $response on each remote searcher node.
My idea is to splice into LucyX/Remote/SearchServer.pm's sub serve():
# Process the method call.
read( $client_sock, $buf, 4 );
$len = unpack( 'N', $buf );
read( $client_sock, $buf, $len );
my $response = $dispatch{$method}->( $self, thaw($buf) );
my $frozen = nfreeze($response);
my $packed_len = pack( 'N', bytes::length($frozen) );
print $client_sock $packed_len . $frozen;
becomes,
# Process the method call.
read( $client_sock, $buf, 4 );
$len = unpack( 'N', $buf );
read( $client_sock, $buf, $len );
#---------incision start----------
my $response;
my $cached_object_id = md5sum($buf); # TODO: check if $buf is the search string
if (is_cached($cached_object_id)) {
$response = read_cached_object($cached_object_id);
}
else {
$response = $dispatch{$method}->( $self, thaw($buf) );
}
#---------incision end----------
my $frozen = nfreeze($response);
my $packed_len = pack( 'N', bytes::length($frozen) );
print $client_sock $packed_len . $frozen;
....
I seem to recall though that the typical search is not an atomic
transaction: ie, the remote search protocol is broken up into
discrete request/response chunks:
my $hits = $poly_searcher->hits(
query => $parsed_query,
sort_spec => $sort_spec,
offset => 0, # or 10, 20, etc
num_wanted => 10,
);
is processed roughly as:
doc_max/response
doc_freq/response x 31
...
top_docs/response
fetch_doc/response x 10
...
done
So, my question is basically: which parts do I cache and what's the
best way to identify those parts? I have a feeling I'm going to have
to package a group of request/responses to cache it in it's
entirety,... or something. --or maybe this is not feasible within
the given framework.
I essentially need a better understanding of the client/server
interaction process so I can formulate an approach to achieve
remote-end caching of search queries (in Perl of course, since that's
what's being used here).
Comments?
thanks
Re: [lucy-user] Splicing in a bit of caching in remote searcher
Posted by goran kent <go...@gmail.com>.
On Mon, Nov 7, 2011 at 11:16 PM, Marvin Humphrey <ma...@rectangular.com> wrote:
> On Mon, Nov 07, 2011 at 10:48:19PM +0200, goran kent wrote:
>> Can you think of any unexpected issues which might arise from caching
>> top_docs() and not other elements of a search transaction?
>
> The top_docs() method is where the search happens. The results for a given
> set of arguments should be deterministic for the life of a Searcher.
>
> It occurs to me now that top_docs() also takes two other params in addition to
> the query: sort_spec and num_wanted. Clearly, num_wanted must be part of the
> cache key as well. So too must the sort_spec if it is changing, which may
> complicate matters as SortSpec does not presently have a useful to_string()
> method.
great - my sortspec never changes, so that's no problem.
Thanks!
Re: [lucy-user] Splicing in a bit of caching in remote searcher
Posted by goran kent <go...@gmail.com>.
Speaking about caching - what else can be cached? Highlighted
excerpts? ...or is retrieving that info from the indexes faster than
reading from a nfreeze'd file could ever be?
Re: [lucy-user] Splicing in a bit of caching in remote searcher
Posted by goran kent <go...@gmail.com>.
On Wed, Nov 9, 2011 at 8:09 PM, Marvin Humphrey <ma...@rectangular.com> wrote:
> Brainstorming... perhaps SearchServer should offer a pluggable caching
> mechanism for top_docs().... or maybe subclassing would work, so that there's
> no need to monkey with the current implementation?
Subclassing sounds ideal (especially adding the howto in the
cookbook). I'm no Perl pro, but I know enough to know that doing it
by subclassing is ideal -- I ended up hacking the code and adding some
procedural (non-oop) subroutine calls. Dirty as all hell, but my
patch file is easy to apply and best of all, it works.
short of time, hands, patience, blah blah
Re: [lucy-user] Splicing in a bit of caching in remote searcher
Posted by Marvin Humphrey <ma...@rectangular.com>.
On Wed, Nov 09, 2011 at 07:41:39PM +0200, goran kent wrote:
> On Mon, Nov 7, 2011 at 11:16 PM, Marvin Humphrey <ma...@rectangular.com> wrote:
> > The top_docs() method is where the search happens. The results for a given
> > set of arguments should be deterministic for the life of a Searcher.
>
> Just a quick positive note: caching top_docs on all the search nodes
> works extremely well. This simple approach gives us granular control
> over what's cached, and where (as opposed to a monolithic cache which
> is difficult to manage when it comes to refreshing views of indexes).
Thanks for passing that along! I would not necessarily have thought to take
this approach, and we should remember that it worked out well when it comes
time to expose a caching API for our clustering support.
Brainstorming... perhaps SearchServer should offer a pluggable caching
mechanism for top_docs().... or maybe subclassing would work, so that there's
no need to monkey with the current implementation?
package MySearchServer;
use base qw( LucyX::Remote::SearchServer );
...
sub top_docs {
my ($self, %args) = @_;
my $key = $args{num_wanted} . $args{query}->to_string;
# Enable this when SortSpec gains a meaningful to_string() method.
#$key .= $args{sort_spec}->to_string;
my $result = $self->get_cache->lookup($key);
if (!$result) {
$result = $self->SUPER::top_docs(%args);
$self->store_in_cache($key, $result);
}
return $result;
}
That might make for a good Lucy::Docs::Cookbook entry.
Marvin Humphrey
Re: [lucy-user] Splicing in a bit of caching in remote searcher
Posted by goran kent <go...@gmail.com>.
On Mon, Nov 7, 2011 at 11:16 PM, Marvin Humphrey <ma...@rectangular.com> wrote:
> The top_docs() method is where the search happens. The results for a given
> set of arguments should be deterministic for the life of a Searcher.
Just a quick positive note: caching top_docs on all the search nodes
works extremely well. This simple approach gives us granular control
over what's cached, and where (as opposed to a monolithic cache which
is difficult to manage when it comes to refreshing views of indexes).
Re: [lucy-user] Splicing in a bit of caching in remote searcher
Posted by Marvin Humphrey <ma...@rectangular.com>.
On Mon, Nov 07, 2011 at 10:48:19PM +0200, goran kent wrote:
> Can you think of any unexpected issues which might arise from caching
> top_docs() and not other elements of a search transaction?
The top_docs() method is where the search happens. The results for a given
set of arguments should be deterministic for the life of a Searcher.
It occurs to me now that top_docs() also takes two other params in addition to
the query: sort_spec and num_wanted. Clearly, num_wanted must be part of the
cache key as well. So too must the sort_spec if it is changing, which may
complicate matters as SortSpec does not presently have a useful to_string()
method.
Marvin Humphrey
Re: [lucy-user] Splicing in a bit of caching in remote searcher
Posted by goran kent <go...@gmail.com>.
On Mon, Nov 7, 2011 at 8:45 PM, Marvin Humphrey <ma...@rectangular.com> wrote:
> However, Query objects have a to_string() method you may be able to make use
> of:
>
> if ($method eq 'top_docs') {
> my $args = thaw($buf)
> my $key = $args->{query}->to_string;
> if (is_cached($key)) {
> $response = read_cached_object($key);
> }
> else {
> $response = $dispatch{$method}->( $self, $args );
> }
> }
...
> The only individual task which it could conceivably make sense to cache would
> be top_docs().
...
> I understand why you want to do this: it allows you to invalidate chunks of the
> cache piecemeal as individual nodes move forwards, rather than invalidate the
> whole cache whenever any one of the nodes changes. Hopefully caching
> top_docs() alone will help.
Thanks for the considered response. The detail you have provided is
very useful. I'll give it a go and see how it goes.
Can you think of any unexpected issues which might arise from caching
top_docs() and not other elements of a search transaction?
thanks
Re: [lucy-user] Splicing in a bit of caching in remote searcher
Posted by Marvin Humphrey <ma...@rectangular.com>.
On Mon, Nov 07, 2011 at 10:36:23AM +0200, goran kent wrote:
> #---------incision start----------
> my $response;
> my $cached_object_id = md5sum($buf); # TODO: check if $buf is the search string
>
> if (is_cached($cached_object_id)) {
> $response = read_cached_object($cached_object_id);
> }
> else {
> $response = $dispatch{$method}->( $self, thaw($buf) );
> }
> #---------incision end----------
$buf is never the search string. It's this, from SearchClient.pm:
my $serialized = nfreeze($args);
my $packed_len = pack( 'N', length($serialized) );
print $sock "$method\n$packed_len$serialized";
$method is the name of the method to invoke on the SearchServer's local
Searcher object. $args is a Perl hashref containing the arguments to pass to
that method, which is subsequently serialized using Storable's nfreeze()
function and becomes the scalar $serialized.
When the method is "top_docs", then $args will contain a Lucy::Search::Query
*object*. The query string has already been parsed at this point, back in the
SearchClient; at no time does the raw query string ever get sent over the wire
to the SearchServer.
However, Query objects have a to_string() method you may be able to make use
of:
if ($method eq 'top_docs') {
my $args = thaw($buf)
my $key = $args->{query}->to_string;
if (is_cached($key)) {
$response = read_cached_object($key);
}
else {
$response = $dispatch{$method}->( $self, $args );
}
}
> I seem to recall though that the typical search is not an atomic
> transaction: ie, the remote search protocol is broken up into
> discrete request/response chunks:
Correct.
> my $hits = $poly_searcher->hits(
> query => $parsed_query,
> sort_spec => $sort_spec,
> offset => 0, # or 10, 20, etc
> num_wanted => 10,
> );
>
>
> is processed roughly as:
>
> doc_max/response
> doc_freq/response x 31
> ...
> top_docs/response
> fetch_doc/response x 10
> ...
> done
>
> So, my question is basically: which parts do I cache and what's the
> best way to identify those parts?
The only individual task which it could conceivably make sense to cache would
be top_docs().
All those calls to doc_freq() are part of the weighting process. The
behavior is not ideal, but changing it is a bit of a can of worms and
server-side caching won't help, as the calls are all fast lookups.
> I have a feeling I'm going to have
> to package a group of request/responses to cache it in it's
> entirety,... or something. --or maybe this is not feasible within
> the given framework.
I can't think of a way to bundle things up without significant refactoring of
how Lucy's searching works or rearchitecting of SearchClient.
I understand why you want to do this: it allows you to invalidate chunks of the
cache piecemeal as individual nodes move forwards, rather than invalidate the
whole cache whenever any one of the nodes changes. Hopefully caching
top_docs() alone will help.
> I essentially need a better understanding of the client/server
> interaction process so I can formulate an approach to achieve
> remote-end caching of search queries (in Perl of course, since that's
> what's being used here).
Understanding how Query objects are compiled to Matcher objects would help, so
maybe check out Lucy::Docs::Cookbook::CustomQuery. Those doc_freq calls
happen during the weighting stage, and are used to power IDF.
http://incubator.apache.org/lucy/docs/perl/Lucy/Docs/Cookbook/CustomQuery.html
http://incubator.apache.org/lucy/docs/perl/Lucy/Docs/IRTheory.html#TF-IDF-ranking-algorithm
Marvin Humphrey