in reply to Re: (mem)caching of ip prefixes?
in thread (mem)caching of ip prefixes?

Ok, I've been thinking about a daemon, but I thought maybe it is possible to cache the thing somehow and do without establishing another daemon. I considered daemon approach a little desperate, but I'm beginning to think maybe it wouldn't be that bad.

I understand that hash with all possible answers wouldn't be that big, but it's questions that are making the whole thing impossible, and storing IPs in some memory efficient OR cachable way that provides lightning fast lookup speed is what I seek. I want to supply a single IP and get an answer containing it's AS number. I just can't construct a hash of all ips, it's ~4GB. So I need prefixes, thus I need a method for a really quick lookup of an individual IP in prefix list.

My data right now is 120000 prefixes. My first (simple) approach was to construct a hash like this:

%iphash = ( 192 => { 192.168.0.0/24 => 23244, 192.168.1.0/24 => 4324, }, 193 => { 193.1.0.0/16 => 2452 }, );
and so on, these are of course just some stupid values. Then I would look up the first octet of an ip address, taking the keys of a subhash (which are all the prefixes that start from this octet), iterate through them using Net:IP overlaps and most probably - get an answer finally. But it was more than 3 seconds, and it's way too much. I guess it was pretty much memory consuming too, but since it was too slow I didn't even get to checking memory.

So I found a Net::Patricia module, fed it with the prefix list and assigned "user_data" which were the AS numbers. Now, using match_string method I get what I want in less than a second. Perfect! However the memory problem occurred and I don't know how to cache it, share it, and so on.

my $pt = new Net::Patricia; (fill it) my $memd = new Cache::Memcached {(usual stuff)}; $memd->set( 'pt', $pt ); (...) my $ptm = $memd->get('pt'); my $asn = $ptm->match_string($given_ip);

And what I get is:

perl in free(): warning: chunk is already free perl in free(): warning: page is already free Assertion failed: (prefix->ref_count > 0), function Deref_Prefix, file + patricia.c, line 362. zsh: abort (core dumped) perl ipmemcached_00.pl

I'm not saying it "doesn't work", I know I might have just gotten it wrong, although I tried various approaches and all I got where core dumps (which surprised me a lot, I must say) or undefs, hence this thread.

Replies are listed 'Best First'.
Re^3: (mem)caching of ip prefixes?
by BrowserUk (Patriarch) on Jun 28, 2007 at 08:40 UTC

    Well, from what I can see, Cache::Memcached only allows you to store/share simple key/values pairs, not complex objects or data structures. So I don't see any way to use Net::Patricia in conjuction with C::MC.

    If NET::Patricia is fast enough for your purpose, then I see no benefit of further 'caching', and no efficient way to 'share' the N::P object between processes. The next obvious option is to set up the N::P object as server.

    There are all sorts of server types that you could use, and you could write it in Perl as a forking, pre-forking, threaded or select-driven. But using perl to develop a server for high-speed, short transaction services using a single, large, shared data object, the select-model is the only game in town.

    To that end, POE::Component::Server::TCP or even POE::Component::Server::SimpleHTTP would probably serve [sic] your purposes with minimal programming cost.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.