<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
    <channel>
        <title>Aaron McCurry - Blogs at Near Infinity</title>


        <link>http://www.nearinfinity.com/blogs/</link>
        <description>Employee Blogs</description>
        <language>en</language>
        <copyright>Copyright 2010</copyright>
        <lastBuildDate>Tue, 19 Jan 2010 21:25:35 -0500</lastBuildDate>
        <generator>http://www.sixapart.com/movabletype/</generator>
        <docs>http://www.rssboard.org/rss-specification</docs>
        
        <item>
            <title>Low Memory Patch For Lucene</title>
            <description><![CDATA[While I'm working on getting my code into lucene. &nbsp;I have patched 3.0.0 and 2.9.1 with my <a href="http://www.nearinfinity.com/blogs/aaron_mccurry/my_first_lucene_patch.html">low memory patch</a>.<div><br /></div><div>By default the small memory footprint is enabled to change back to the default implementation set the following system property.</div><div><br /></div><div>-Dorg.apache.lucene.index.TermInfosReader=default<div><br /></div><div>Have fun! &nbsp;If you have any problems or questions please let me know or add to this <a href="https://issues.apache.org/jira/browse/LUCENE-2205">LUCENE-2205</a>.</div><div><br /></div><div><a href="http://www.nearinfinity.com/blogs/assets/amccurry/lucene-core-3.0.0-nic.jar">lucene-core-3.0.0-nic.jar</a></div><div><br /></div><div><a href="http://www.nearinfinity.com/blogs/assets/amccurry/lucene-core-2.9.1-nic.jar">lucene-core-2.9.1-nic.jar</a></div><div><br /></div></div>]]></description>
            <link>http://www.nearinfinity.com/blogs/aaron_mccurry/low_memory_patch_for_lucene.html</link>
            <guid>http://www.nearinfinity.com/blogs/aaron_mccurry/low_memory_patch_for_lucene.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">Java</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Lucene</category>
            
            
            <pubDate>Tue, 19 Jan 2010 21:25:35 -0500</pubDate>
        </item>
        
        <item>
            <title>Lucene Hit Paging Made Easier</title>
            <description><![CDATA[Lucene gives users the ability to search massive amounts of data in a very short amount of time. &nbsp;However allowing users to page through the entire result set of their search can be difficult and risky depending on how many users are performing searches and how many of those users are paging through 100's if not 1,000's of hits per page.<div><br /></div><div>Problem Scenario:</div><div><br /></div><div><ul><li>Each of your indexes contains 100,000,000's documents.</li><li>You have 500 users on your system&nbsp;actively&nbsp;performing searches.</li><li>You have 100 search results per page.</li><li>And, your typical user pages through the first 10 pages of results. &nbsp;(Normal&nbsp;occurrence&nbsp;on some systems)</li></ul></div><div><br /></div><div>So for the 10th page you will have to collect 1,000 hits, at a cost of a float plus an int plus some object overhead per hit. &nbsp;So let's say 20 bytes per hit. &nbsp;So you have 500 users * 1,000 hits * 20 bytes = 10,000,000 bytes or 10M. &nbsp;Easy, no problem, right?</div><div><br /></div><div>Well what if you also give the users an easy way to move to the end of the result set. &nbsp;Hmm... &nbsp;Well for a result set size of 10,000 it's no big deal. &nbsp;But what if you hand out result sets in the order of a 1,000,000 or even 10,000,000.</div><div><br /></div><div>At this point you really just want to prevent the system from running out of memory. &nbsp;Because if you have 25 users getting 10,000,000 results each and they all click last page at the same time. &nbsp;That's going to cost you 5 Gig of heap! &nbsp;At least. &nbsp;Some might say that it won't ever happen, but in my experience, if it can happen, it will.</div><div><br /></div><div>So I created a Paging Hit Collector, that windows the hits to the users. &nbsp;It's uses the last hit collected from the previous search pass, to feed the next search pass. &nbsp;So yes if a user clicks the last page, it might perform multiple searches but, the system won't run out of memory.</div><div><br /></div><div>The user's will get there answer&nbsp;eventually, and if your system gives them some feedback as it searches and pages, they will probably sit and wait for it to come back. &nbsp;Instead of giving up and hitting cancel and search and cancel and search, and making the system worse and worse.</div><div><br /></div><b>

The Simple Example:</b><div><b><br /></b><div><div>
<pre class="prettyprint">IndexSearcher searcher = new IndexSearcher(reader);<br />
TermQuery query = new TermQuery(new Term("f1", "value"));
IterablePaging paging = new IterablePaging(searcher, query, 100);<br />
for (ScoreDoc sd : paging.skipTo(90)) {
&nbsp;&nbsp;System.out.println("doc id [" + sd.doc + "] " + 
&nbsp;&nbsp;&nbsp;&nbsp;"score [" + sd.score + "]");
}
</pre>
</div></div><b><div><b><br /></b></div>

The More Advanced Example:</b></div><div><b><br /></b><div><div>
<pre class="prettyprint">IndexSearcher searcher = new IndexSearcher(reader);<br />
TotalHitsRef totalHitsRef = new TotalHitsRef();
ProgressRef progressRef = new ProgressRef();<br />
TermQuery query = new TermQuery(new Term("f1", "value"));
IterablePaging paging = new IterablePaging(searcher, query, 100);<br />
for (ScoreDoc sd : paging.skipTo(90).
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gather(20).
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;totalHits(totalHitsRef).
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;progress(progressRef)) {<br />
&nbsp;&nbsp;System.out.println("time [" + progressRef.queryTime() + "] " + 
&nbsp;&nbsp;&nbsp;&nbsp;"total hits [" + totalHitsRef.totalHits() + "] " + 
&nbsp;&nbsp;&nbsp;&nbsp;"searches [" + progressRef.searchesPerformed() + "] " + 
&nbsp;&nbsp;&nbsp;&nbsp;"position [" + progressRef.currentHitPosition() + "] " + 
&nbsp;&nbsp;&nbsp;&nbsp;"doc id [" + sd.doc + "] " + 
&nbsp;&nbsp;&nbsp;&nbsp;"score [" + sd.score + "]");
}
</pre>
</div></div><div><br /></div><div>Here's a link to the code <a href="https://issues.apache.org/jira/browse/LUCENE-2215">LUCENE-2215</a>.</div><div><br /></div></div>]]></description>
            <link>http://www.nearinfinity.com/blogs/aaron_mccurry/making_lucene_hit_results_pagi.html</link>
            <guid>http://www.nearinfinity.com/blogs/aaron_mccurry/making_lucene_hit_results_pagi.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">Java</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Lucene</category>
            
            
            <pubDate>Mon, 18 Jan 2010 16:16:59 -0500</pubDate>
        </item>
        
        <item>
            <title>My First Lucene Patch - Making Lucene Do More With Less</title>
            <description><![CDATA[I've been using Lucene for the better part of 2 years, from initial playing around, to prototyping to production application. &nbsp;It's an impressive library and it has come along way in the past couple of years.<div><br /></div><div>When I first started playing around with it the version was 2.1 and the search times were so much faster than what we were trying to use at the time (Oracle Text). &nbsp;The first test was indexing a monster dataset and searching it quickly. &nbsp;It passed with flying colors!</div><div><br /></div><div>Next was to add in record level access control. &nbsp;<a href="http://java.dzone.com/articles/how-implement-row-level-access">Easy</a>&nbsp;and extremely fast.</div><div><br /></div><div>Next was to add in all the other data needed for our application. &nbsp;That was a little bit harder, considering that we have close to 150 fields in our index and well into the billion record range (growing everyday).</div><div><br /></div><div>The problem was that we needed more memory and there was no extra money for any more servers (or upgrades). &nbsp;So there we were, stuck. &nbsp;So I decided to start poking around using <a href="https://visualvm.dev.java.net/">visualvm</a>&nbsp;to see if there were any places in our application or in Lucene to save some memory.</div><div><br /></div><div>We had already disabled norms on all our fields (we really didn't need norms for our data nor did we have the resources). &nbsp;Took a long look at all our fields that we were indexing to see if there were any we didn't need, but we really did need them all. &nbsp;Then I stumbled across the TermInfosReader class in Lucene.</div><div><br /></div><div>This is where Lucene really gets it speed, but also uses quite a bit memory to do it. &nbsp;And this is where I wrote my first Lucene patch.</div><div><br /></div><div>In TermInfosReader there is a bunch stuff but the big memory hogs are in three arrays.</div><div><br /></div><div><ul><li>Terms[]</li><li>TermInfos[]</li><li>long[]</li></ul></div><div>Basically Lucene does a binary search across the Terms array (that by default contains every 128th Term in the index) with a given Term to find where on disk the exact Term needed lives. &nbsp;There's a little bit more going on in the class than that, but that's basically what it's doing.</div><div><br /></div><div>So, I started this patch with the need to save memory. &nbsp;So how in the world do you do that in java when everything is already in basic arrays and everything is needed in memory. &nbsp;Well you have to save it another way, references. &nbsp;References are a hidden cost in Java, every single reference in 32-bit JVM costs you 4 bytes, and 64-bit JVM it's 8 (assuming that you don't have compressed references).</div><div><br /></div><div>Let's count the references.</div><div><br /></div><div><ul><li>Terms[] length * 3, 1 reference for the Term and 2 references for the two Strings inside the Term</li><li>TermInfo[] length * 1</li><li>long[] = 1 reference total<br /></li></ul><div>So, let's talk numbers. &nbsp;If you have a billion terms in your index, that's 125 MB (1,000,000,000 / 128 * (3 + 1 references) * 4 bytes for every ref) bytes of memory for the references. &nbsp;In a 64-bit JVM that doubled 250 MB. &nbsp;Not to mention the object overhead for every one of those Term and TermInfos objects. Wow that's a lot!</div><div><br /></div><div>So I decided to remove nearly all of those references by using a byte array and an int array as an offset index.</div><div><br /></div><div>The results were impressive!</div><div><br /></div><div>Given an index of 6.2 GB size 1,010,000&nbsp;number of documents with 179,822,683 number of terms the default implementation uses 292,235,512 bytes to just get the index usable.</div><div><br /></div><div>My no-ref implementation of the same index uses only 49,849,744 bytes get the index usable. &nbsp;That a 17% of the original size, that's an 83% savings!</div><div><br /></div><div>And the best part is, that it loads the segments faster into memory. &nbsp;So those real-time updates will be online faster. &nbsp;The run-time performance is slightly faster as well. &nbsp;But the huge performance saving is in garbage collection. &nbsp;Over 7 times faster for full GC's on my Macbook Pro. &nbsp;Wow!</div><div><br /></div><div>I think that the results speak for themselves, and I hope that the Lucene folks will accept my patch. &nbsp;That way I won't have to continue patching each version after the fact. &nbsp;Also removing references can be great, but the code required to do it, and maintain the same level of performance, is ugly! &nbsp;So don't try this at home!</div><div><br /></div><div><a href="https://issues.apache.org/jira/browse/LUCENE-2205">LUCENE-2205</a></div><div><br /></div></div>]]></description>
            <link>http://www.nearinfinity.com/blogs/aaron_mccurry/my_first_lucene_patch.html</link>
            <guid>http://www.nearinfinity.com/blogs/aaron_mccurry/my_first_lucene_patch.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">Java</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Lucene</category>
            
            
            <pubDate>Mon, 11 Jan 2010 20:06:21 -0500</pubDate>
        </item>
        
        <item>
            <title>Using HBase-dsl</title>
            <description><![CDATA[At the beginning of last month I started prototyping various solutions for a customer using HBase. &nbsp;However I found myself writing tons of code to perform some fairly simple tasks. &nbsp;So I set out to simply my HBase code and ended up writing a Java <a href="http://wiki.github.com/nearinfinity/hbase-dsl" target="_blank">HBase DSL</a>. &nbsp;It's still fairly rough around the edges but it does allow the use of standard Java types and it's extensible.<div><br /><font class="Apple-style-span" style="font-size: 1.25em; "><font class="Apple-style-span" style="font-size: 1.25em; ">

Simple Put and Get Example</font></font><br /><br /><b>

Direct HBase API:</b><br />

<br />
<pre class="prettyprint">public class PutAndGet {
   public static void main(String[] args) throws IOException {
      HTable hTable = new HTable("test");

      byte[] rowId = Bytes.toBytes("abcd");
      byte[] famA = Bytes.toBytes("famA");
      byte[] col1 = Bytes.toBytes("col1");
      Put put = new Put(rowId).
         add(famA, col1, Bytes.toBytes("hello world!"));
      hTable.put(put);
      Get get = new Get(rowId);
      Result result = hTable.get(get);
      byte[] value = result.getValue(famA, col1);
      System.out.println(Bytes.toString(value));
   }
}
</pre><b>HBase-dsl API:</b><br /><br />
<pre class="prettyprint">public class PutAndGetWithDsl { 
   public static void main(String[] args) throws IOException { 
      HBase&lt;QueryOps, String&gt; hBase = new HBase&lt;QueryOps&lt;String&gt;, String&gt;(String.class);

      hBase.save("test").  
         row("abcd"). 
            family("famA"). 
               col("col1", "hello world!"); 
      String value = hBase.fetch("test"). 
         row("abcd").
            family("famA"). 
               value("col1", String.class)
      System.out.println(value);
   }
 }</pre>

Now this is where the dsl becomes more powerful!<div><br /><font class="Apple-style-span" style="font-size: 1.25em; "><font class="Apple-style-span" style="font-size: 1.25em; ">

Scanner Example</font></font><br /><br /><b>

Direct HBase API:</b><br /><br />

<pre class="prettyprint">public class Scanner {
   public static void main(String[] args) throws IOException {
      byte[] famA = Bytes.toBytes("famA");
      byte[] col1 = Bytes.toBytes("col1");  

      HTable hTable = new HTable("test");  

      Scan scan = new Scan(Bytes.toBytes("a"), Bytes.toBytes("z"));
      scan.addColumn(famA, col1);  

      SingleColumnValueFilter singleColumnValueFilterA = new SingleColumnValueFilter(
           famA, col1, CompareOp.EQUAL, Bytes.toBytes("hello world!"));
      singleColumnValueFilterA.setFilterIfMissing(true);  

      SingleColumnValueFilter singleColumnValueFilterB = new SingleColumnValueFilter(
           famA, col1, CompareOp.EQUAL, Bytes.toBytes("hello hbase!"));
      singleColumnValueFilterB.setFilterIfMissing(true);  

      FilterList filter = new FilterList(Operator.MUST_PASS_ONE, Arrays
           .asList((Filter) singleColumnValueFilterA,
                singleColumnValueFilterB));  

      scan.setFilter(filter);  

      ResultScanner scanner = hTable.getScanner(scan);  

      for (Result result : scanner) {
         System.out.println(Bytes.toString(result.getValue(famA, col1)));
      }
   }
}</pre>
<b>HBase-dsl API:</b><br /><br />

<pre class="prettyprint">public class ScannerWithDsl {
   public static void main(String[] args) throws IOException {
      HBase&lt;QueryOps, String&gt; hBase = new HBase&lt;QueryOps&lt;String&gt;, String&gt;(String.class);

      hBase.scan("test","a","z").
         select().
            family("famA").
               col("col1").
         where().
            family("famA").
               col("col1").eq("hello world!","hello hbase!").
         foreach(new ForEach<row>() {
            @Override
            public void process(Row row) {
               System.out.println(row.value("famA", "col1", String.class));
            }
         });
  }
}</row></pre><br />
See the unit tests, for more examples.<br /><br /></div></div>]]></description>
            <link>http://www.nearinfinity.com/blogs/aaron_mccurry/using_hbase-dsl.html</link>
            <guid>http://www.nearinfinity.com/blogs/aaron_mccurry/using_hbase-dsl.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">Database</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Hadoop</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Java</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Persistence</category>
            
            
                <category domain="http://www.sixapart.com/ns/types#tag">hadoop</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">hbase</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">hbase-dsl</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">java</category>
            
            <pubDate>Tue, 05 Jan 2010 22:34:20 -0500</pubDate>
        </item>
        
        <item>
            <title>Installing Go on OSX Snow Leopard</title>
            <description><![CDATA[ Go is a new experimental language that has been released by Google that is suppose to have nearly the speed of C while executing, without a lot of the normal headaches of C/C++ coding.&nbsp; For example Go has garbage collection, dynamic types, reflections, etc. see <a href="http://www.infoq.com/news/2009/11/Go-Google-New-Language">Google Experiments With A New Language, Go</a> for a good overview.&nbsp; So after reading a few things about it, I wanted to try it out, and here is how I got it working on my 64-bit OSX machine.<br /><br />It's pretty basic, all I did was follow the instructions <a href="http://golang.org/doc/install.html">here</a>.<br /><br />First I added:<br /><br /><pre class="prettyprint">export GOROOT=$HOME/go<br />export GOOS=darwin<br />export GOARCH=amd64<br />export GOBIN=$HOME/gobin<br />export PATH=$PATH:$GOBIN</pre>
to ~/.bash_profile<br /><br />Run:<br /><br />
<pre class="prettyprint">. ~/.bash_profile</pre>
Next:<br /><br />
<pre class="prettyprint">sudo easy_install mercurial</pre>
This took a while for me to pull mercurial down, maybe their server was having problems. <br /><br />Note: I had to install the Xcode.mpkg from the Optional Installs on my Snow Leopard DVD to get gcc-4.3 installed.<br /><br />Next:<br /><br />

<pre class="prettyprint">hg clone -r release https://go.googlecode.com/hg/ $GOROOT</pre>
Next:<br /><br />
<pre class="prettyprint">cd $GOROOT/src<br />./all.bash</pre>
Note: One of the http tests goes out and touches google.com, I had to drop the firewall while building everything.&nbsp; After the build and tests were complete I turned it back on.<br /><br />And that's it! Time to make a hello world program.<br /><br />Create hello.go:<br /><br />
<pre class="prettyprint">package main<br /><br />import fmt "fmt"<br /><br />func main() {<br />&nbsp;&nbsp; fmt.Printf("hello world!\n");<br />}</pre>
Compile and Link:<br /><br />
<pre class="prettyprint">6g hello.go<br />6l hello.6</pre>
Run:<br /><br />
<pre class="prettyprint">./6.out</pre>
Output:<br /><br />
<pre class="prettyprint">hello world!</pre>
I am very excited about Go can't wait to write something real with it!<br /><br />]]></description>
            <link>http://www.nearinfinity.com/blogs/aaron_mccurry/installing_go_on_osx_snow_leop.html</link>
            <guid>http://www.nearinfinity.com/blogs/aaron_mccurry/installing_go_on_osx_snow_leop.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">General</category>
            
            
                <category domain="http://www.sixapart.com/ns/types#tag">c</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">go</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">google</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">language</category>
            
            <pubDate>Wed, 11 Nov 2009 14:52:23 -0500</pubDate>
        </item>
        
        <item>
            <title>Hive - The next great data warehouse</title>
            <description><![CDATA[In the past few weeks I have been spending more and more time working with Hadoop and Hive.&nbsp; For those of you that don't know what Hadoop is check out what <a href="http://en.wikipedia.org/wiki/Hadoop">wikipedia</a> has to say.&nbsp; Hive is built on top of Hadoop, simply stated is it a SQL engine that submits <a href="http://en.wikipedia.org/wiki/Map_Reduce">map/reduce</a> jobs to Hadoop for execution.<br /><br />So next you ask yourself, "why do I care"?&nbsp; Well with Hive using Hadoop for all the heavy lifting, the amount of data that you can process is only limited by the amount of hardware you have in your cluster.&nbsp; Hive is used for data warehousing which means that it is designed to work on huge datasets, huge joins, huge data loads, huge query results, etc.&nbsp; However before you start thinking about getting rid of that MySQL database, think again.&nbsp; Hive is not and never will be low latency.&nbsp; All queries submit map/reduce jobs to Hadoop which then operates on files stored in HDFS.<br /><br />Hive has a lot of nice features built in, like:<br /><ul><li>It can operate on <i>raw</i> files located in HDFS, like logs from you application, like csv files from your database(s).&nbsp; So this can reduce your load time, because you don't have to actually load it into a database before you can use it.</li><li>It can operate on compressed files.&nbsp; I started using this feature last week because I am getting a 4 to 1 compression ratio with no different in performance (I am using sequence files with block compression).</li><li>In your SQL statements you can actually use the Hadoop streaming api to build your own mapper and reducers, and they don't even have to be written in Java!</li><li>You can also create your own user defined functions, so when you have to do something crazy with the data, you can!</li></ul><br />And there are lots more, so go check it out!<br /><br /><a href="http://wiki.apache.org/hadoop/Hive">Hive</a>, the real Netezza killer.<br />]]></description>
            <link>http://www.nearinfinity.com/blogs/aaron_mccurry/hive_-_the_next_great_data_war.html</link>
            <guid>http://www.nearinfinity.com/blogs/aaron_mccurry/hive_-_the_next_great_data_war.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">Hadoop</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Java</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">SQL</category>
            
            
                <category domain="http://www.sixapart.com/ns/types#tag">hadoop</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">hdfs</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">hive</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">java</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">sql</category>
            
            <pubDate>Sun, 04 Oct 2009 13:18:55 -0500</pubDate>
        </item>
        
        <item>
            <title>Tuning the IBM JVM for large heaps</title>
            <description><![CDATA[
 
 
 
Recently I have been rewriting a lot of our lucene search engine code
for a web application that I'm currently supporting.&nbsp; Our physical
environment is a little different than most, we have a single very
large computer (a left over from a previous project) running several
virtual machines.&nbsp; The virtual machine that we are currently using for
our searching duties is a 64 cpu 128G box.<br />

<br />
Before I go into the tuning saga, I would like to say that in a
previous version of the search engine we ran many more (16), smaller
jvms all on the same box.&nbsp; They work together through a main controller
module that coordinates calls to all the searching jvms.&nbsp; After the
rewrite of the
searchers, there was a lot of pressure to get the new code out the
door.&nbsp; The new code partitions the data and searching duties in a very
similar way to the previous version, however I haven't had time to
focus
on getting all the parts broken up across all the jvms.&nbsp; It's very
doable, but do to customer priorities it hasn't been developed.&nbsp; But I
was in
luck (kinda), our environment was a single computer, so I thought I
will just run one large jvm.&nbsp; I knew going in that garbage collection
(GC) was going to be a
problem, I just didn't realize how much.<br />

<br />
Currently, we are running the 64-bit IBM java 6 jvm, the IBM jvm is very
different from the Sun jvm, but no more so than in the GC tuning
department.&nbsp; In the Sun jvm you have all kinds of settings to tweak,
algorithms to use etc.&nbsp; The IBM jvm just isn't as advanced, you have 4
different GC policies, and sizing for various parts of the internals.&nbsp;
And that's it!&nbsp; Great, it should be simple right?&nbsp; Well sorta.<br />

<br />

First try, bring the system online with a 50G (yes that's gigabytes)
heap, with all the default settings.&nbsp; Run some load tests and see where
we are.<br />

<br />

Everything is running great right up to the first full GC, 25 seconds
doesn't sound that long but when you are waiting for a computer to
return results, it's an eternity.&nbsp; For those that don't know, when a
full GC occurs (some of the newer Sun algorithms are different) it
stops the world (STW).&nbsp; This means the JVM is frozen until the GC is
complete.&nbsp; No good.<br />

<br />

So the default policy is the optthruput policy (-Xgcpolicy:optthruput).&nbsp; After digging through the IBM documentation,
this type of policy should be used for maximum throughput, but at the
expense of pauses during GC.&nbsp; They also mention that this should be
used for batch processing when pauses are really a problem.<br />

<br />

Next I tried the optavgpause policy (-Xgcpolicy:optavgpause), this is
suppose to smooth out the GCs by kicking off the mark phase early.&nbsp; I'm
not going to talk about mark, sweep, and compaction, you can find it
here (<a href="http://www.ibm.com/developerworks/ibm/library/i-incrcomp/">http://www.ibm.com/developerworks/ibm/library/i-incrcomp/</a>).&nbsp; But
basically it's suppose to run a concurrent parallel mark phase before
the jvm runs out of heap space and performs a full GC that STW.&nbsp; This did help,
got us down in the 10-15 second range on average, but the problem was
that the mark phase was too slow to start under heavy load and didn't
give us a whole lot of concurrent marking before the STW.&nbsp; I found this
out by adding -verbose:gc, this adds a lot of debugging information to the
standard out.&nbsp; You should just run this all the time, it provides a lot
of useful information about your application.<br />

<br />

Next I tried subpool (-Xgcpolicy:subpool), it worked fine but was
plagued by the same problem, slow full GCs.&nbsp; Subpool is suppose to work
better on large SMP machines like ours, but in the end it was more of
the same.&nbsp; It's full GCs were in the 10-12 second range, plus the application seemed
to run slower, by about 10%.<br />

<br />
And last I tried gencon (-Xgcpolicy:gencon), the newest of all
their
policies.&nbsp; Gencon is suppose to be used on "transactional systems",
systems that create a lot of short lived objects.&nbsp; Isn't that what most
java applications do?&nbsp; When I started it up, it seemed to be faster,
and our load test confirmed that, 30% more throughput.&nbsp; But the amazing
thing was that the full GCs were fast, really fast, in the 200-400ms
range, for a 50G heap!&nbsp; But wait, it wasn't using most of the heap, and
it was GCing all the time.&nbsp; Back to the verbose:gc log, AHA!&nbsp; The
nursery was too small, it was about 10% of the heap, and because our
application is almost all short lived objects I decided to increase the
size of the nursery.&nbsp; I gave it 50% of the heap, and the time between
GCs slowed down, but the time to perform the GC was still in the 350ms
range.&nbsp; Awesome!<br />

<br />

I finally settled on 100G heap with 50% a nursery, and the full GCs are
now in the 400-600ms range.&nbsp; I can live with that, because this gives
us a huge ceiling for load, and capacity.<br />

<br />

So to summarize, if you application needs to run a huge heap size, and
you are using the IBM jvm, I would start by using the gencon policy.&nbsp;
It seems to be the most modern of all their policies, and it seems to
work the best.<br /><br />Good luck!<br />

]]></description>
            <link>http://www.nearinfinity.com/blogs/aaron_mccurry/tuning_the_ibm_jvm_for_large_h.html</link>
            <guid>http://www.nearinfinity.com/blogs/aaron_mccurry/tuning_the_ibm_jvm_for_large_h.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">General</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Java</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Lucene</category>
            
            
            <pubDate>Fri, 24 Apr 2009 20:53:36 -0500</pubDate>
        </item>
        
        <item>
            <title>A little tuning can go a long way...</title>
            <description><![CDATA[The application that I'm current developing uses Lucene for searching
and data retrieval.&nbsp; We recently had a rather special need to use one
of the utility classes in Lucene, OpenBitSet.&nbsp; OpenBitSet is not unlike
the regular BitSet provided by the java.util package.&nbsp; The biggest
difference is that the class is NOT final.&nbsp; So for someone like me,
that tinkers and optimizes to get every last bit of performance out of
my application, final classes are a major PITA.<br /><br />The problem:<br /><br />OpenBitSet
provides an optimized "find the next set bit starting here" method
called nextSetBit(long index).&nbsp; However I needed to find the previous
set bit, so naturally I started by using the built-in api.&nbsp; Iterate backwards over each bit starting at given position until a bit that it is set to true is found.&nbsp; Probably not the fastest way, but it
works.&nbsp; I thought that if I wrote my own "find the previous
set bit starting here" method it could be potently a lot faster.&nbsp; And because the class is not final and the internals are not private I can extend it, YAY!<br /><br />So after some work and some tests I came up with this:<br /><br />
<pre class="prettyprint">&nbsp;&nbsp;&nbsp; /**<br />&nbsp;&nbsp;&nbsp; &nbsp;* Finds the previous set bit in the bitset, start looking at the index position.<br />&nbsp;&nbsp;&nbsp; &nbsp;* @param index the index to start looking.<br />&nbsp;&nbsp;&nbsp; &nbsp;* @return the previous position in the bitset that is set.<br />&nbsp;&nbsp;&nbsp; &nbsp;*/<br />&nbsp;&nbsp;&nbsp; public long prevSetBit(long index) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //get index of the word to start with<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int i = (int) (index &gt;&gt;&gt; 6); //div by 64<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (i &gt;= wlen) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //if the index requested is greater than the actual<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //max word, just start with the last word<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; i = wlen - 1;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //loop backwards over the bits[] until a non-zero word is found<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while (i &gt;= 0) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; long word = bits[i];<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (word != 0) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //find the position of the most significant bit<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int pomsb = getPositionOfMostSigBit(word);<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //multiply by 64<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; long l = ((long) i) &lt;&lt; 6;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //add together to find the actual position<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return l + pomsb;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; i--;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return -1;<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; /**<br />&nbsp;&nbsp;&nbsp; &nbsp;* The word passed in, should not be 0.<br />&nbsp;&nbsp;&nbsp; &nbsp;* @param word the word to find the position of the most significant bit.<br />&nbsp;&nbsp;&nbsp; &nbsp;* @return the position in the word.<br />&nbsp;&nbsp;&nbsp; &nbsp;*/<br />&nbsp;&nbsp;&nbsp; private int getPositionOfMostSigBit(long word) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (word == 1l) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return 0;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int i = 0;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while (word != 1) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; word = word &gt;&gt;&gt; 1;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; i++;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return i;<br />&nbsp;&nbsp;&nbsp; }<br /><br /></pre>
And of course I had to write a performance test to make sure that my work had produced something useful.&nbsp; Here's the test:<br /><br />

<pre class="prettyprint">&nbsp;&nbsp;&nbsp; public static void main(String[] args) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; CustomOpenBitSet bitSet = new CustomOpenBitSet(Integer.MAX_VALUE);<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int maxCount = 1000000;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fillBitSet(bitSet, maxCount);<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; 10; i++) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; double s1 = System.nanoTime();<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int slow = runSlowReverseSeeksToSetBits(bitSet, maxCount);<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; double e1 = System.nanoTime();<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; double s2 = System.nanoTime();<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int fast = runFastReverseSeeksToSetBits(bitSet, maxCount);<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; double e2 = System.nanoTime();<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; assert (slow == fast);<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("The slow time took " + ((e1-s1) / ONE_BILLION) + " seconds");<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("The fast time took " + ((e2-s2) / ONE_BILLION) + " seconds");<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; }<br /><br />&nbsp;&nbsp;&nbsp; private static int runFastReverseSeeksToSetBits(CustomOpenBitSet bitSet, int maxCount) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int count = 0;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; long start = Integer.MAX_VALUE;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while (count &lt; maxCount - 1) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; long position = bitSet.prevSetBit(start);<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; count++;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; start = position - 1;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return count;<br />&nbsp;&nbsp;&nbsp; }<br /><br />&nbsp;&nbsp;&nbsp; private static int runSlowReverseSeeksToSetBits(CustomOpenBitSet bitSet, int maxCount) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int count = 0;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; long start = Integer.MAX_VALUE;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while (count &lt; maxCount - 1) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; long position = slowSeek(bitSet,start);<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; count++;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; start = position - 1;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return count;<br />&nbsp;&nbsp;&nbsp; }<br /><br />&nbsp;&nbsp;&nbsp; private static long slowSeek(CustomOpenBitSet bitSet, long position) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while (!bitSet.get(position)) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; position--;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (position &lt; 0) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return -1;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return position;<br />&nbsp;&nbsp;&nbsp; }<br /><br />&nbsp;&nbsp;&nbsp; private static void fillBitSet(CustomOpenBitSet bitSet, int numberOfBitsToSet) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Random random = new Random();<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int nextInt = -1;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; numberOfBitsToSet; i++) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; do {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; nextInt = random.nextInt(Integer.MAX_VALUE);<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } while (bitSet.fastGet(nextInt));<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; bitSet.set(nextInt);<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; }
</pre>
The
test creates a BitSet that is Integer.MAX_VALUE long and randomly
populates it with 1 million bits set to true.&nbsp; Then starting at
Integer.MAX_VALUE, find all the bits that are set to true, moving from
the greatest position to the smallest position.&nbsp; Do this 10 times and
print off the time taken to perform both algorithms. <br /><br />
<pre class="prettyprint">Pass [0], the slow time took 7.867899136 seconds<br />Pass [0], the fast time took 0.059851008 seconds<br />Pass [1], the slow time took 7.84461184 seconds<br />Pass [1], the fast time took 0.051954944 seconds<br />Pass [2], the slow time took 7.820731904 seconds<br />Pass [2], the fast time took 0.07801088 seconds<br />Pass [3], the slow time took 7.81953408 seconds<br />Pass [3], the fast time took 0.074951168 seconds<br />Pass [4], the slow time took 7.798106112 seconds<br />Pass [4], the fast time took 0.07792384 seconds<br />Pass [5], the slow time took 7.742498048 seconds<br />Pass [5], the fast time took 0.067644928 seconds<br />Pass [6], the slow time took 7.664754944 seconds<br />Pass [6], the fast time took 0.07476992 seconds<br />Pass [7], the slow time took 7.841472768 seconds<br />Pass [7], the fast time took 0.073187072 seconds<br />Pass [8], the slow time took 7.927188992 seconds<br />Pass [8], the fast time took 0.07574912 seconds<br />Pass [9], the slow time took 7.85273088 seconds<br />Pass [9], the fast time took 0.076514048 seconds
</pre>So as you can see the new method is about 100 times faster than just iterating backwards over the bitset, given this sample data.&nbsp; I would assume that if the sample size of 1 million were to increase, the gap between the 2 methods would narrow.<br />]]></description>
            <link>http://www.nearinfinity.com/blogs/aaron_mccurry/a_little_tuning_can_go_a_long.html</link>
            <guid>http://www.nearinfinity.com/blogs/aaron_mccurry/a_little_tuning_can_go_a_long.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">Java</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Lucene</category>
            
            
                <category domain="http://www.sixapart.com/ns/types#tag">java</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">lucene</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">openbitset</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">performance</category>
            
            <pubDate>Thu, 02 Apr 2009 21:50:52 -0500</pubDate>
        </item>
        
        <item>
            <title>How to implement row level access control in Lucene</title>
            <description><![CDATA[Below I have written some fully functionally code that shows how you could implement row level access control in Lucene (2.3.2).  Basically you have to index enough information to be able to search (in a single query) and find all documents that a given user has access to read.<br /><br />

In the below example there are two fields:<br /><br />

DATA: Which contains any data that you want your users to be able to search.  NOTE: You can have as many data fields as you like.<br /><br />

ACL_FIELD: The field used to determine what users have access to this document.  Note: You can have as many access control fields as you like.<br /><br />

All you have to do is built the access control query for each user and submit your user's query unchanged.<br /><br />

<pre class="prettyprint">public class TestIndexerSearcher {

   public static void main(String[] args) throws Exception {
      Directory directory = new RAMDirectory();
      IndexWriter indexWriter = new IndexWriter(directory, new StandardAnalyzer());
      indexWriter.addDocument(buildDocument("DATA:sametoken","ACL_FIELD:access"));
      indexWriter.addDocument(buildDocument("DATA:sametoken","ACL_FIELD:noaccess"));
      indexWriter.optimize();
      indexWriter.close();

      IndexSearcher indexSearcher = new IndexSearcher(directory);

      QueryParser parser = new QueryParser("DATA", new StandardAnalyzer());
      Query query = parser.parse("sametoken");
		
      //This is all you have to add to your existing code.
      Filter aclFilter = applyAccessControl(new TermQuery(
         new Term("ACL_FIELD","access")));

      Hits hits = indexSearcher.search(query, aclFilter);
      System.out.println("Hits[" + hits.length() + "]");
      for (int i = 0; i &lt; hits.length(); i++) {
         Document doc = hits.doc(i);
         System.out.println("DATA [" + doc.get("DATA") + 
            "] ACL_FIELD [" + doc.get("ACL_FIELD") + "]");
      }
      indexSearcher.close();	
   }

   private static Filter applyAccessControl(Query aclQuery) {
      return new CachedQueryFilter(aclQuery.toString(), 
         new QueryWrapperFilter(aclQuery));
   }

   private static Document buildDocument(String... fieldInfo) {
      Document document = new Document();
      for (int i = 0; i &lt; fieldInfo.length; i++) {
         String[] split = fieldInfo[i].split(":");
         String fieldName = split[0];
         String fieldValue = split[1];
         document.add(new Field(fieldName,fieldValue,
            Field.Store.YES,Field.Index.TOKENIZED));
      }
      return document;
   }	
}

</pre>

<br />After you run this code, you will get a single hit, not the two that you would normally get if the access control filter wasn't in place.<br /><br />

<pre class="prettyprint">public class CachedQueryFilter extends Filter {
   private static final long serialVersionUID = 6797293376134753695L;
   private Filter filter;
   private String key;
   private static transient Map&lt;String, BitSetCache&gt; filterCache = 
      new ConcurrentHashMap&lt;String, BitSetCache&gt;();

   public CachedQueryFilter(String key, Filter filter) {
      this.filter = filter;
      this.key = key;
   }

   public BitSet bits(IndexReader reader) throws IOException {
      BitSetCache cachedBitSet = (BitSetCache) filterCache.get(key);
      if (cachedBitSet != null) {
         BitSet bitSet = cachedBitSet.bitSet.get();
         if (bitSet != null &amp;&amp; cachedBitSet.indexReaderVersion == reader.getVersion()) {
            return bitSet;
         }
      }
      BitSet bits = filter.bits(reader);
      BitSetCache bitSetCache = new BitSetCache();
      bitSetCache.indexReaderVersion = reader.getVersion();
      bitSetCache.bitSet = new SoftReference&lt;BitSet&gt;(bits);
      filterCache.put(key, bitSetCache);
      return bits;
   }
	
   private class BitSetCache {
      long indexReaderVersion;
      SoftReference&lt;BitSet&gt; bitSet;
   }
}
</pre>


There are two additional features that this query filter doesn't implements that you may want to consider.<br /><br />

1st - Provide per query locking around the bitset creation code.  This would allow multiple bitset creation calls to occur at once, but the same access control query would block.  Therefore we would only have to build it once, even if multiple user queries with the same access control hit the query filter at once.<br /><br />

2nd - Persist the bitsets.  In the past I have used the same directory as the index, but you may want to use a database, or something else.<br /><br />]]></description>
            <link>http://www.nearinfinity.com/blogs/aaron_mccurry/how_to_implement_row_level.html</link>
            <guid>http://www.nearinfinity.com/blogs/aaron_mccurry/how_to_implement_row_level.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">Java</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Lucene</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Security</category>
            
            
            <pubDate>Sat, 27 Sep 2008 21:08:42 -0500</pubDate>
        </item>
        
        <item>
            <title>Lucene is a memory hog!</title>
            <description><![CDATA[<p><span style="font-family: Arial;">Let me start by saying that, I like Lucene, I have used it to solve many technical problems on my current project. But one aspect of Lucene that I have had issues with is with its memory footprint.</span></p>

<p><span style="font-family: Arial;">Currently we index 38 fields across 1.5 billion documents, and we have implemented a fair similarity object (see <a href="http://lucene.apache.org/java/docs/scoring.html">Lucene Scoring Documentation</a>) for normalized scoring. We have no use for index time field boosting or for any type of Norms (see <a href="http://lucene.apache.org/java/docs/scoring.html">Lucene Scoring Documentation</a>). However Lucene reads all of the Norms into memory for fast scoring. </span></p>

<p><span style="font-family: Arial;">So let's do the math:</span></p>

<p><span style="font-family: Arial;">1 byte per Norm value * 38 fields * 1,500,000,000 = <b><i>57,000,000,000</i></b></span></p>

<p><span style="font-family: Arial;">That's +/- 57 Gigs of heap space!</span></p>

<p><span style="font-family: Arial;">That's quiet a bit of memory usage for something that we don't even use. I have since patched Lucene, so that the indexed Norms have a much better memory footprint, something around 1.5 Gigs. Not great, but livable.</span></p>

<p><span style="font-family: Arial;">I havenÃ•t posted my patch, because itÃ•s all or nothing, I havenÃ•t implemented a way to turn it on and off. But if there is anyone else out there, with an application that is running out of memory as users use more and more indexed fields in Lucene, take a look at the <i>SegmentReader</i> class. There's a byte array in there that you should take a look at. Happy hunting!</span></p>]]></description>
            <link>http://www.nearinfinity.com/blogs/aaron_mccurry/lucene_is_a_memory_hog.html</link>
            <guid>http://www.nearinfinity.com/blogs/aaron_mccurry/lucene_is_a_memory_hog.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">Java</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Lucene</category>
            
            
            <pubDate>Wed, 03 Sep 2008 22:29:41 -0500</pubDate>
        </item>
        
        <item>
            <title>What Happens To Lucene When You Go Big...  Part 1</title>
            <description><![CDATA[<p style="margin-bottom: 0in;">My current project has some unique
searching requirements.</p>
<p style="margin-bottom: 0in;">
</p>
<h2>Requirements</h2>
<ul>
	<li><p style="margin-bottom: 0in;">Fuzzy searching is a must
	(Soundex, Levenshtein, etc.)</p>
	</li><li><p style="margin-bottom: 0in;">Has to be fast, a must with any
	searching solution</p>
	</li><li><p style="margin-bottom: 0in;">Has to provide access control</p>
	</li><li><p style="margin-bottom: 0in;">Full data load indexing needs to
	be completed in a reasonable amount of time</p>
	</li><li><p style="margin-bottom: 0in;">Scoring needs to be a custom
	implementation</p>
	</li><li><p style="margin-bottom: 0in;">Needs to run on a predetermined
	environment, meaning that new hardware purchases are not going to
	happen any time soon</p>
	</li><li><p style="margin-bottom: 0in;">And last but not least is ability
	do all these things on a dataset that exceeds a billion records</p>
</li></ul>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;">So we have had a lot of constraints to
deal with, the hardest one by far is the last one.</p>
<p style="margin-bottom: 0in;">
</p>
<h2>The Data</h2>
<p style="margin-bottom: 0in;">
</p>
<ul>
	<li><p style="margin-bottom: 0in;">1 billion
	plus records</p>
	</li><li><p style="margin-bottom: 0in;">Over 30
	million unique terms</p>
</li></ul>
<p style="margin-bottom: 0in;">
</p>
<h2>Indexing and Searching Server Specs</h2>
<p style="margin-bottom: 0in;">
</p>
<ul>
	<li><p style="margin-bottom: 0in;">20 CPUs</p>
	</li><li><p style="margin-bottom: 0in;">32 Gig of ram</p>
	</li><li><p style="margin-bottom: 0in;">Dedicated SAN
	storage</p>
</li></ul>
<p style="margin-bottom: 0in;">
</p>
<h2>First Searching Experiences</h2>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;">After getting the
index built in multiple partitions, I fired up a simple Lucene
console to do some simple searches with a Lucene multi searcher.  Ran
out of memory with 2 Gig heap, tried the maximum heap size for the 32
bit JVM we were using, 3.3 Gig, and that ran out of memory as well. 
So, initial tries to just run one search were unsuccessful.</p>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;">Then we installed
a 64-bit JVM and tried an 8 Gig heap, and it worked!  I could run
searches and after the first couple of warm up searches it was
getting 20 - 80 ms responses on single term searches.  Great,
but then we tried a Fuzzy search, which uses a Levenshtein algorithm
to calculate matches, 2 minutes 45 seconds, this was unacceptable.</p>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;">Next we wrote our
own Levenshtein Lucene query and got the 2 minutes plus search down
to about one second.  We found that the built in Lucene Fuzzy query
was taking 85-95% of the time to find the terms to search.  Then
after those terms were found the actual search with those expanded
terms only took a second to two depending on how many terms were
found.  So we replaced the built in Fuzzy query with a custom one
that gets near instantaneous results on Levenshtein fuzzy matches. 
Problem solved.</p>
<p style="margin-bottom: 0in;">
</p>
<h2>Indexing Time</h2>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;">After our initial
proof of concept was complete, we needed to improve the indexing time
down to something more reasonable.  The index creation from scratch
was taking 36 - 48 hours to build with 20 CPUs running at 100%
utilization.  Which means that the machine was indexing about 9,000
records a second.  Not bad for Lucene 2.2, but not that great.</p>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;">First we stopped
merging the indexes after we created them, that by itself was taking
about 12 hours.  At this point we also started searching these
multiple indexes in parallel, and we are seeing modest increases in
query performance.</p>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;">Second, we
upgraded to Lucene 2.3, this provided a huge increase in indexing
speed.  Our index creation time went from 36 - 48 hours
(depending on if we merged indexes or not) down to 3-4 hours.  The
indexing process is now indexing around 125,000+ records a second. 
Huge improvement, if you haven't upgraded to 2.3, you should!</p>
<p style="margin-bottom: 0in;">
</p>
<h2>Current Development</h2>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;">We are in the
process of adding access control to Lucene as well as adding new
custom queries and scoring.  So far Lucene has performed better than
any of the competition that it has come up against, and with it's
price point it seems to have won acceptance on our project.</p>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;">In upcoming parts
I will go into more details about the technical solutions that we
have developed to solve these problems, as well others that I haven't
mentioned yet.</p>
<p style="margin-bottom: 0in;">
</p>]]></description>
            <link>http://www.nearinfinity.com/blogs/aaron_mccurry/what_happens_to_lucene_when.html</link>
            <guid>http://www.nearinfinity.com/blogs/aaron_mccurry/what_happens_to_lucene_when.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">Java</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Lucene</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Security</category>
            
            
            <pubDate>Wed, 30 Apr 2008 13:30:00 -0500</pubDate>
        </item>
        
        <item>
            <title>Column Oriented Databases</title>
            <description><![CDATA[<h2><font face="Arial, sans-serif">What is it?</font></h2>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;"><font face="Arial, sans-serif">A column
oriented database (or a column store database) is a database that
stores it's information in a column oriented manor, instead of a row
oriented manor.</font></p>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;"><font face="Arial, sans-serif">Take for
example this simple table of information.</font></p>
<p style="margin-bottom: 0in;">
</p>
<table border="1" bordercolor="#000000" cellpadding="4" cellspacing="0">
	<tbody><tr valign="top">
		<td bgcolor="#e6e6e6">
			<p><font face="Arial, sans-serif"><b>Id</b></font></p>
		</td>
		<td bgcolor="#e6e6e6">
			<p><font face="Arial, sans-serif"><b>FirstName</b></font></p>
		</td>
		<td bgcolor="#e6e6e6">
			<p><font face="Arial, sans-serif"><b>LastName</b></font></p>
		</td>
		<td bgcolor="#e6e6e6">
			<p><font face="Arial, sans-serif"><b>Gender</b></font></p>
		</td>
		<td bgcolor="#e6e6e6">
			<p><font face="Arial, sans-serif"><b>BirthDate</b></font></p>
		</td>
	</tr>
	<tr valign="top">
		<td>
			<p><font face="Arial, sans-serif">1</font></p>
		</td>
		<td>
			<p><font face="Arial, sans-serif">John</font></p>
		</td>
		<td>
			<p><font face="Arial, sans-serif">Doe</font></p>
		</td>
		<td>
			<p><font face="Arial, sans-serif">M</font></p>
		</td>
		<td>
			<p><font face="Arial, sans-serif">1/1/1980</font></p>
		</td>
	</tr>
	<tr valign="top">
		<td>
			<p><font face="Arial, sans-serif">2</font></p>
		</td>
		<td>
			<p><font face="Arial, sans-serif">Jane</font></p>
		</td>
		<td>
			<p><font face="Arial, sans-serif">Doe</font></p>
		</td>
		<td>
			<p><font face="Arial, sans-serif">F</font></p>
		</td>
		<td>
			<p><font face="Arial, sans-serif">2/2/1981</font></p>
		</td>
	</tr>
	<tr valign="top">
		<td>
			<p><font face="Arial, sans-serif">3</font></p>
		</td>
		<td>
			<p><font face="Arial, sans-serif">John</font></p>
		</td>
		<td>
			<p><font face="Arial, sans-serif">Smith</font></p>
		</td>
		<td>
			<p><font face="Arial, sans-serif">M</font></p>
		</td>
		<td>
			<p><font face="Arial, sans-serif">3/3/1979</font></p>
		</td>
	</tr>
</tbody></table>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;"><font face="Arial, sans-serif">It is
very natural for a row oriented database to store the following
information as sequential appending rows on disk.  This makes them
very good for inserting and updating data (depending on
implementation).</font></p>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;">

<font face="Arial, sans-serif">


<table border="1" bordercolor="#000000" cellpadding="4" cellspacing="0">
<tbody><tr><td>
1,John,Doe,M,1/1/1980~2,Jane,Doe,F,2/2/1981~3,John,Smith,M,3/3/1979
</td></tr>
</tbody></table>


</font>
</p>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;"><font face="Arial, sans-serif">A column
oriented database might store the data like this:</font></p>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;"><font face="Arial, sans-serif">

<table border="1" bordercolor="#000000" cellpadding="4" cellspacing="0">
<tbody><tr><td>
1,2,3~John,Jane,John~Doe,Doe,Smith~M,F,M~1/1/1980,2/2/1981,3/3/1979
</td></tr>
</tbody></table>

</font></p>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;"><font face="Arial, sans-serif">As you
can imagine this type of structure would not perform well if you were
making lots of changes to the data.  But if you are scanning through
a column looking for values, the column oriented database groups all
the values of a column together as opposed to spreading them out like
the row store database would do.</font></p>
<p style="margin-bottom: 0in;">
</p>
<h2><font face="Arial, sans-serif">Why do I care?</font></h2>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;"><font face="Arial, sans-serif">Database
vendors would argue that indexing the columns solves all the
performance problems of a row store database.  And for most
applications they are right, but one benefit that most column store
databases provide that may not be apparent, is their ability to
compress the data.  Take my example:</font></p>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;"><font face="Arial, sans-serif">

<table border="1" bordercolor="#000000" cellpadding="4" cellspacing="0">
<tbody><tr><td>
1,2,3~<span style="background: rgb(255, 255, 0) none repeat scroll 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">John</span>,Jane,<span style="background: rgb(255, 255, 0) none repeat scroll 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">John</span>~<span style="background: rgb(61, 235, 61) none repeat scroll 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">Doe</span>,<span style="background: rgb(61, 235, 61) none repeat scroll 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">Doe</span>,Smith~<span style="background: rgb(255, 0, 0) none repeat scroll 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">M</span>,F,<span style="background: rgb(255, 0, 0) none repeat scroll 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">M</span>~1/1/1980,2/2/1981,3/3/1979
</td></tr>
</tbody></table>


</font></p>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;"><font face="Arial, sans-serif">Because
the type of each column is consistent, compression of the entire
column can be a lot greater than compressing each row in a row store
database (some newer RDBMs are adding compression features). 
<a href="http://www.vertica.com/">Vertica</a>, a commercial column
store database boasts up to 90% reduction in storage compared with a
traditional row store databases.</font></p>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;"><font face="Arial, sans-serif">Query
speed can also be greatly improved on a column store database versus
a row store database, although that feature is more dependent on
implementation then on the structure of the data.</font></p>
<p style="margin-bottom: 0in;">
</p>
<h2><font face="Arial, sans-serif">Conclusion</font></h2>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;"><font face="Arial, sans-serif">For
problems where you are writing and updating a lot (OLTP in database
talk) stick with a row store database.  But if you are reading more
than you are writing (OLAP) a column store database might be
something worth considering.</font></p>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;"><font face="Arial, sans-serif">Here are
some column store databases that I have used or heard of:</font></p>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;"><font face="Arial, sans-serif">Open
source:</font></p>
<ul>
	<li><p style="margin-bottom: 0in;"><font face="Arial, sans-serif"><a href="http://www.luciddb.org/">LucidDb</a>
	- Java / JNI implementation</font></p>
	</li><li><p style="margin-bottom: 0in;"><font face="Arial, sans-serif"><a href="http://monetdb.cwi.nl/">MonetDb</a></font></p>
	</li><li><p style="margin-bottom: 0in;"><font face="Arial, sans-serif"><a href="http://db.csail.mit.edu/projects/cstore/">C-Store</a></font></p>
</li></ul>
<p style="margin-bottom: 0in;">
</p>
<p style="margin-bottom: 0in;"><font face="Arial, sans-serif">Commercial:</font></p>
<ul>
	<li><p style="margin-bottom: 0in;"><font face="Arial, sans-serif"><a href="http://www.vertica.com/">Vertica</a>
	- Grid based column store database</font></p>
</li></ul>
<p style="margin-bottom: 0in;">
</p>]]></description>
            <link>http://www.nearinfinity.com/blogs/aaron_mccurry/column_oriented_databases.html</link>
            <guid>http://www.nearinfinity.com/blogs/aaron_mccurry/column_oriented_databases.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">General</category>
            
            
            <pubDate>Wed, 30 Apr 2008 12:00:00 -0500</pubDate>
        </item>
        
    </channel>
</rss>
