<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">&gt; Okay, that makes sense. Would that be a server-side cache, or a client-side<br>

&gt; cache?<br>
<br>
</div></div>There are no server-side caches (other than the OS disk cache).<br></blockquote><div><br></div><div>Ok, that&#39;s what I gathered before, was just checking. </div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">
&gt; I believe I&#39;ve already succeeded in getting a client-side persistent<br>
&gt; disk-based cache to work (my zodb_indexdb_uri is<br>
&gt; &quot;zeo://%(here)s/zeo_indexdb.sock?cache_size=2000MB&amp;connection_cache_size=500000&amp;connection_pool_size=5&amp;var=zeocache&amp;client=index&quot;),<br>
<br>
</div>This configuration syntax isn&#39;t part of ZODB.  I&#39;m not familiar with<br>
the options there.</blockquote><div><div><br></div><div>Ah yes it&#39;s a part of repoze - <a href="http://docs.repoze.org/zodbconn/narr.html#zeo-uri-scheme">http://docs.repoze.org/zodbconn/narr.html#zeo-uri-scheme</a> . I looked into this, and the following mappings from uri-syntax to xml-syntax hold true:</div>
<div><br></div><div>  cache_size --&gt; zodb/zeoclient/cache-size</div><div>  connection_cache_size --&gt; zodb/cache-size</div><div>  connection_pool_size --&gt; zodb/pool-size</div></div><div>  var --&gt; zodb/zeoclient/var</div>
<div>  client --&gt; zodb/zeoclient/client</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">&gt; but this doesn&#39;t seem to be what you&#39;re referring to as that is exactly the<br>
&gt; same size as the in-memory cache.<br>
<br>
</div>I doubt it, but who knows?<br></blockquote><div><br></div><div>I meant that I have only one cache-size option in terms of bytes, and the cache made on the disk is exactly that size (rather, it reserves all the space on disk instantly, even if it isn&#39;t all used).</div>
<div><br></div><div>----------------------</div><div><br>Here&#39;s a detailed description of the issues I&#39;m having.</div><div><br></div><div>I wrote the following code to preload the indices:</div><div><br></div><div>
<div><font size="1" face="courier new, monospace">    def preload_index_btree(index_name, index_type, btree):</font></div><div><font size="1" face="courier new, monospace">        print &quot;((Preloading &#39;%s&#39; %s index btree...))&quot; % (index_name, index_type)</font></div>
<div><font size="1" face="courier new, monospace">        start = last_print = time.time()</font></div><div><font size="1" face="courier new, monospace">        for i, item in enumerate(btree.items()):</font></div><div><font size="1" face="courier new, monospace">            item</font></div>
<div><font size="1" face="courier new, monospace">        print &quot;((Preloaded &#39;%s&#39; %s index btree (%d items in %.2fs)))&quot; % (</font></div><div><font size="1" face="courier new, monospace">            index_name, index_type, i, time.time() - start,</font></div>
<div><font size="1" face="courier new, monospace">        )</font></div><div><font size="1" face="courier new, monospace">    def preload_catalog(catalog):</font></div><div><font size="1" face="courier new, monospace">        &quot;&quot;&quot;Given a catalog, touch every persistent object we can find to force</font></div>
<div><font size="1" face="courier new, monospace">        them to go into the cache.&quot;&quot;&quot;</font></div><div><font size="1" face="courier new, monospace">        start = time.time()</font></div><div><font size="1" face="courier new, monospace">        num_indices = len(catalog.items())</font></div>
<div><font size="1" face="courier new, monospace">        for i, (index_name, index) in enumerate(catalog.items()):</font></div><div><font size="1" face="courier new, monospace">            print &quot;((Preloading index %2d/%2d &#39;%s&#39;...))&quot; % (i+1, num_indices, index_name,)</font></div>
<div><font size="1" face="courier new, monospace">            preload_index_btree(index_name, &#39;fwd&#39;, index._fwd_index)</font></div><div><font size="1" face="courier new, monospace">            preload_index_btree(index_name, &#39;rev&#39;, index._rev_index)</font></div>
<div><font size="1" face="courier new, monospace">        print &quot;((Preloaded catalog! Took %.2fs))&quot; % (time.time() - start)</font></div></div><div><br></div><div>And I run it on server start as follows (modified for the relevant parts; I tried to make the example simple but it ended up needing a lot of parts). This runs in a thread:</div>
<div><br></div><div><div><font size="1" face="courier new, monospace">    from util import zodb as Z</font></div><div><font size="1" face="courier new, monospace">    from util import zodb_query as ZQ</font></div><div><div>
<font size="1" face="courier new, monospace">    for i in xrange(3):</font></div><div><font size="1" face="courier new, monospace">        connwrap = Z.ConnWrapper(&#39;index&#39;)</font></div><div><font size="1" face="courier new, monospace">        print &quot;((Preload #%d...))&quot; % (i+1)</font></div>
<div><font size="1" face="courier new, monospace">        with connwrap as index_root:</font></div><div><font size="1" face="courier new, monospace">            ZQ.preload_catalog(index_root.index.catalog)</font></div><div>
<font size="1" face="courier new, monospace">        connwrap.close()</font></div></div></div><div><br></div><div>Z.ConnWrapper is something that uses my config to return connections such that I only have one DB instance for the whole server process:</div>
<div><br></div><div><div><font size="1" face="courier new, monospace">  class ConnWrapper(object):</font></div><div><font size="1" face="courier new, monospace">    def __init__(self, db_name):</font></div><div><font size="1" face="courier new, monospace">        global_config = appconfig.get_config()</font></div>
<div><font size="1" face="courier new, monospace">        db_conf = global_config[&#39;dbs&#39;][db_name]</font></div><div><font size="1" face="courier new, monospace">        </font></div><div><font size="1" face="courier new, monospace">        db = db_conf[&#39;db&#39;]</font></div>
<div><font size="1" face="courier new, monospace">        self.appmaker = db_conf[&#39;appmaker&#39;]</font></div><div><font size="1" face="courier new, monospace">        </font></div><div><font size="1" face="courier new, monospace">        conn = db.open()</font></div>
<div><font size="1" face="courier new, monospace">        self.conn = conn</font></div></div><div><font size="1" face="courier new, monospace">        self.cur_t = None</font></div><div><font size="1" face="courier new, monospace">    #...</font></div>
<div><div><font size="1" face="courier new, monospace">    def get_approot(self):</font></div><div><font size="1" face="courier new, monospace">        return self.appmaker(self.conn.root())</font></div><div><font size="1" face="courier new, monospace">    def __enter__(self):</font></div>
<div><font size="1" face="courier new, monospace">        &quot;&quot;&quot;.begin() transaction and return the app_root&quot;&quot;&quot;</font></div><div><font size="1" face="courier new, monospace">        if self.cur_t:</font></div>
<div><font size="1" face="courier new, monospace">            raise ValueError(&quot;transaction already in progres&quot;)</font></div><div><font size="1" face="courier new, monospace">        self.cur_t = self.conn.transaction_manager.begin()</font></div>
<div><font size="1" face="courier new, monospace">        return self.get_approot()</font></div></div><div><div><font size="1" face="courier new, monospace">    def __exit__(self, typ, value, tb):</font></div><div><font size="1" face="courier new, monospace">        if typ is None:</font></div>
<div><font size="1" face="courier new, monospace">            try:</font></div><div><font size="1" face="courier new, monospace">                self.cur_t.commit()</font></div><div><font size="1" face="courier new, monospace">            except:</font></div>
<div><font size="1" face="courier new, monospace">                self.cur_t = None</font></div><div><font size="1" face="courier new, monospace">                raise</font></div><div><font size="1" face="courier new, monospace">                </font></div>
<div><font size="1" face="courier new, monospace">            self.cur_t = None</font></div></div><div><div><font size="1" face="courier new, monospace">        else:</font></div><div><font size="1" face="courier new, monospace">            self.cur_t.abort()</font></div>
<div><font size="1" face="courier new, monospace">            self.cur_t = None</font></div></div><div><br></div><div>The relevant part of the global config setup is:</div><div><br></div><div><font size="1" face="courier new, monospace">    from repoze.zodbconn.uri import db_from_uri</font></div>
<div><font size="1" face="courier new, monospace">    from indexdb.models import appmaker as indexdb_appmaker</font></div><div><font size="1" face="courier new, monospace">    #...</font></div><div><div><font size="1" face="courier new, monospace">    zodb_indexdb_uri = global_config.get(&#39;zodb_indexdb_uri&#39;)</font></div>
</div><div><font size="1" face="courier new, monospace">    index_db = db_from_uri(zodb_indexdb_uri)</font></div><div><font size="1" face="courier new, monospace">    global_config[&#39;dbs&#39;] = {</font></div><div><font size="1" face="courier new, monospace">        &#39;index&#39;: {</font></div>
<div><font size="1" face="courier new, monospace">            &#39;db&#39;: index_db,</font></div><div><font size="1" face="courier new, monospace">            &#39;appmaker&#39;: indexdb_appmaker,</font></div><div><font size="1" face="courier new, monospace">        },</font></div>
<div><font size="1" face="courier new, monospace">    }</font></div><div><br></div><div>`zodb_indexdb_uri` is in my .ini file as mentioned above:</div><div><br></div><div><font size="1" face="courier new, monospace">    zodb_indexdb_uri = zeo://%(here)s/zeo_indexdb.sock?cache_size=3000MB&amp;connection_cache_size=5000000&amp;connection_pool_size=5&amp;var=zeocache&amp;client=index</font></div>
<div><br></div><div>The preloading seems to accomplish its purpose. When I restart the server, it takes a while to run through all the indices the first time over and the memory usage grows as this is happening, e.g.:</div>
<div><br></div><div><div><font size="1" face="courier new, monospace">    ((Preloading index  3/17 &#39;account&#39;...))</font></div><div><font size="1" face="courier new, monospace">    ((Preloading &#39;account&#39; fwd index btree...))</font></div>
<div><font size="1" face="courier new, monospace">    ((Preloaded &#39;account&#39; fwd index btree (37 items in 0.00s)))</font></div><div><font size="1" face="courier new, monospace">    ((Preloading &#39;account&#39; rev index btree...))</font></div>
</div><div><font size="1" face="courier new, monospace">    ((Preloaded &#39;account&#39; rev index btree (346786 items in 69.72s)))</font></div><div><br></div><div>And the subsequent attempts are quite rapid:</div><div><br>
</div><div><div><font size="1" face="courier new, monospace">    ((Preloading index  3/17 &#39;account&#39;...))</font></div><div><span style="font-family:&#39;courier new&#39;,monospace;font-size:x-small">   </span><span style="font-family:&#39;courier new&#39;,monospace;font-size:x-small"> </span><font size="1" face="courier new, monospace">((Preloading &#39;account&#39; fwd index btree...))</font></div>
<div><span style="font-family:&#39;courier new&#39;,monospace;font-size:x-small">   </span><span style="font-family:&#39;courier new&#39;,monospace;font-size:x-small"> </span><font size="1" face="courier new, monospace">((Preloaded &#39;account&#39; fwd index btree (37 items in 0.00s)))</font></div>
<div><span style="font-family:&#39;courier new&#39;,monospace;font-size:x-small">   </span><span style="font-family:&#39;courier new&#39;,monospace;font-size:x-small"> </span><font size="1" face="courier new, monospace">((Preloading &#39;account&#39; rev index btree...))</font></div>
<div><span style="font-family:&#39;courier new&#39;,monospace;font-size:x-small">   </span><span style="font-family:&#39;courier new&#39;,monospace;font-size:x-small"> </span><font size="1" face="courier new, monospace">((Preloaded &#39;account&#39; rev index btree (346903 items in 0.08s)))</font></div>
</div><div><span style="font-family:&#39;courier new&#39;,monospace;font-size:x-small">   </span><span style="font-family:&#39;courier new&#39;,monospace;font-size:x-small"> </span><font size="1" face="courier new, monospace">...</font></div>
<div><span style="font-family:&#39;courier new&#39;,monospace;font-size:x-small">   </span><span style="font-family:&#39;courier new&#39;,monospace;font-size:x-small"> </span><font size="1" face="courier new, monospace">((Preloaded catalog! Took 1.58s)) #(for the entire catalog)</font></div>
<div><br></div><div>What I don&#39;t understand is that this doesn&#39;t seem to work in the long run. Just before writing this email I ran a view that required a simple query after not having restarted the server in a while, and it took a minute or two to complete. Running the view again, it took only a few seconds. So it seems something had been moved out of the cache, which makes no sense to me as the server has plenty of RAM and the cache size is plenty large. </div>
<div><br></div><div>Further, after having preloaded the indices once, shouldn&#39;t it preload quite rapidly upon further server restarts, if it&#39;s all in the cache and the cache is persisted? After the above preload ran, I restarted the server, and although the first few indices did indeed load more quickly: </div>
<div><br></div><div><font size="1" face="courier new, monospace">    ((Preloading index  3/17 &#39;account&#39;...))</font></div><div><font size="1" face="courier new, monospace">    ((Preloading &#39;account&#39; fwd index btree...))</font></div>
<div><font size="1" face="courier new, monospace">    ((Preloaded &#39;account&#39; fwd index btree (37 items in 0.00s)))</font></div><div><font size="1" face="courier new, monospace">    ((Preloading &#39;account&#39; rev index btree...))</font></div>
<div><div><font size="1" face="courier new, monospace">    ((Preloaded &#39;account&#39; rev index btree (346905 items in 8.37s)))</font></div></div><div>Some took just as long:</div><div><div><font size="1" face="courier new, monospace">    ((Preloaded &#39;timestamp&#39; fwd index btree (348333 items in 90.69s)))</font></div>
</div><div>And the whole catalog took a good amount yet: </div><div><span style="font-family:&#39;courier new&#39;,monospace;font-size:x-small">    (Preloaded catalog! Took 199.03s))</span></div><div>Granted, if it took 8-11 seconds per index instead of 60-90 seconds per index, that&#39;s not such a bad improvement. This is why I was considering memcachedb, by the way - something that would work well in between restarts of my server. Another option I guess would be to have a whole other server instance running just for the caching that would be always on to avoid these issues.</div>
<div><br></div><div>It seems like something isn&#39;t working with the caching, but I can&#39;t figure out why... any pointers? It seems I don&#39;t understand the cache mechanism very well as I did everything I thought should work according to my understanding.</div>
<div><br></div><div>One potential thing is this: after a zeopack the index database .fs file is about 400 megabytes, so I figure a cache of 3000 megabytes should more than cover it. Before a zeopack, though - I do one every 3 hours - the file grows to 7.6 gigabytes. Shouldn&#39;t the relevant objects - the entire set of latest versions of the objects - be the ones in the cache, thus it doesn&#39;t matter that the .fs file is 7.6gb as the actual used bits of it are only 400mb or so? Another question is, does zeopacking destroy the cache? If so then that would make sense. I&#39;ll have to preload upon every zeopack. If it&#39;s not that, then I&#39;m not sure what it could be.</div>
<div><br></div><div>Thanks,</div><div>- Claudiu</div></div>