<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dev.davidsoergel.com</title>
	<atom:link href="http://dev.davidsoergel.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://dev.davidsoergel.com</link>
	<description>various computing projects</description>
	<lastBuildDate>Fri, 22 Aug 2008 23:01:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Contract Tests with TestNG: unit testing against interfaces and abstract classes</title>
		<link>http://dev.davidsoergel.com/2008/08/22/contract-tests-with-testng/</link>
		<comments>http://dev.davidsoergel.com/2008/08/22/contract-tests-with-testng/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 22:11:31 +0000</pubDate>
		<dc:creator>David Soergel</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://dev.davidsoergel.com/?p=12</guid>
		<description><![CDATA[An aspect of testing Java programs that seems to me fairly neglected is testing conformance to interfaces, and (nearly identically) testing that functionality of abstract classes works properly in all concrete subclasses.  Certainly this has been mentioned before, generally under the name Abstract Tests or Contract Tests.  Also, the idea seems to me [...]]]></description>
			<content:encoded><![CDATA[<p>An aspect of testing Java programs that seems to me fairly neglected is testing conformance to interfaces, and (nearly identically) testing that functionality of abstract classes works properly in all concrete subclasses.  Certainly this has <a href="http://blog.paulmoser.co.uk/index.php/2007/02/25/unit-testing-interface-implementations/">been</a> <a href="http://www.ddj.com/184405269">mentioned</a> <a href="http://groboutils.sourceforge.net">before</a>, generally under the name <strong>Abstract Tests</strong> or <strong>Contract Tests</strong>.  Also, the idea seems to me very much in keeping with <a href="http://en.wikipedia.org/wiki/Behavior_driven_development">Behaviour Driven Development</a> (BDD) and the <a href="http://en.wikipedia.org/wiki/Design_by_contract">Design by Contract</a> (DbC) philosophy.</p>

<p>TestNG does not explicitly support Contract Tests, as far as I can tell, but it’s fairly easy to make it work using the little trick I describe below.</p>

<p><span id="more-12"></span>For an interface or abstract class called FooBar, make a test class called FooBarInterfaceTest or FooBarAbstractTest or whatever.</p>

<p>For an implementation FooBarImpl, we could make a test FooBarImplTest that just inherits from FooBarInterfaceTest. But what if FooBarImpl implements multiple interfaces, or extends an abstract class in addition to implementing one or more interfaces?</p>

<p>I thought about making one inner class (inside FooBarImplTest) per interface, each inheriting from the appropriate InterfaceTest. But, that doesn’t work because TestNG doesn’t recognize tests inside inner classes (or even static inner classes).</p>

<p>My solution is to use the TestNG @Factory annotation, which marks a method that returns a bunch of test cases in an object array. We can put such a @Factory in the implementation test class, and use it to return Contract Tests for each of the implemented interfaces or extended abstract classes.</p>

<p>Since the abstract tests will need to create instances of the concrete implementation being tested, we’ll need to provide the abstract test case with a factory for the test instances. I use a simple generic interface to describe the factory:</p>

<pre><code>public interface TestInstanceFactory&lt;T&gt;
    {
    T createInstance() throws Exception;
    }
</code></pre>

<p>Then the Contract Test looks like this:</p>

<pre><code>public abstract class FooBarInterfaceTest
    {
    private TestInstanceFactory&lt;? extends FooBar&gt; tif;

    public FooBarInterfaceTest(TestInstanceFactory&lt;? extends FooBar&gt; tif)
        {
        this.tif = tif;
        }

    @Test
    public void someFooBarMethodTest
        {
        FooBar testInstance = tif.createInstance();

        ... 
        }
    }
</code></pre>

<p>That creates the problem that the only constructor for the FooBarInterfaceTest requires a TestInstanceFactory argument. If FooBarInterfaceTest were a regular class, then TestNG (or at least the IntelliJ IDEA plugin) would try to instantiate it as a regular test, failing because there’s no zero-arg constructor. The trick there is to make the Contract Test class actually abstract, as indicated above, and to concretize it inline in the @Factory method in the implementation test (see below).</p>

<p>I put some logic for collecting all the Contract Tests relevant to a given implementation test into an abstract class (from which the implementation test will inherit):</p>

<pre><code>public abstract class ContractTestAware&lt;T&gt;
    {
    public abstract void addContractTestsToQueue(Queue&lt;Object&gt; theContractTests);

    @Factory
    public Object[] instantiateAllContractTests()
        {
        Set&lt;Object&gt; result = new HashSet&lt;Object&gt;();
        Queue&lt;Object&gt; queue = new LinkedList&lt;Object&gt;();

        addContractTestsToQueue(queue);

        // recursively find all applicable contract tests up the tree
        while (!queue.isEmpty())
            {
            Object contractTest = queue.remove();
            result.add(contractTest);
            if (contractTest instanceof ContractTestAware)
                {
                ((ContractTestAware) contractTest).addContractTestsToQueue(queue);
                }
            }

        return result.toArray();
        }
    }
</code></pre>

<p>And finally we make the implementation test, thus:</p>

<pre><code>public class FooBarImplTest extends ContractTestAware&lt;FooBarImpl&gt;
        implements TestInstanceFactory&lt;FooBarImpl&gt;
    {
    public FooBarImpl createInstance() throws Exception
        {
        return new FooBarImpl();
        }

    public void addContractTestsToQueue(Queue&lt;Object&gt; theContractTests)
        {
        theContractTests.add(new FooBarInterfaceTest(this){});  // this is the trick
        }
    }
</code></pre>

<p>Two problems remain:</p>

<ol>
<li><p>although <code>FooBarImplTest</code> extends <code>ContractTestAware</code>, TestNG <a href="http://code.google.com/p/testng/issues/detail?id=28">doesn&#8217;t find the inherited <code>@Factory</code> method</a>.  So we have to override it in <code>FooBarImplTest</code>:</p>

<pre><code>@Factory
public Object[] instantiateAllContractTests()
    {
    return super.instantiateAllContractTests();
    }
</code></pre></li>
<li><p>As written above, the <code>FooBarImplTest</code> <code>@Factory</code> method still <a href="http://code.google.com/p/testng/issues/detail?id=29">isn&#8217;t found</a> because there is no <code>@Test</code> method present.  So, in the unfortunate case that you don&#8217;t have any real tests for the implementation class, you can just do this:</p>

<pre><code>@Test
public void bogusTest()
    {
    }
</code></pre>

<p>that will cause the <code>@Factory</code> method to be found so all the Contract Tests will be run.</p></li>
</ol>

<p>That’s it! Note you can add as many Contract Tests as you want for each implementation test.</p>

<p>Also, although I’ve emphasized using this approach to test interfaces and abstract classes, there’s no reason you couldn’t use it for regular classes as well. You could do this if you’re in a situation where a subclass ought to pass all of the tests for its superclass (perhaps in addition to tests specific to the subclass).</p>

<p>Note too that this solution is chainable, so the test interface/class hierarchy can mirror the real interface/class hierarchy. For instance, if the <code>FooBar</code> interface extends another interface, say <code>Baz</code>, then <code>FooBarInterfaceTest</code> can itself extend <code>ContractTestAware</code> in order to provide a <code>BazInterfaceTest</code> (initialized with the provided concrete factory).</p>

<pre><code>public abstract class FooBarInterfaceTest extends ContractTestAware&lt;FooBar&gt;
    {
    private TestInstanceFactory&lt;? extends FooBar&gt; tif;

    public FooBarInterfaceTest(TestInstanceFactory&lt;? extends FooBar&gt; tif)
        {
        this.tif = tif;
        }

    public void addContractTestsToQueue(Queue&lt;Object&gt; theContractTests)
        {
        theContractTests.add(new BazInterfaceTest(tif){});
        }
    }
</code></pre>

<p>Obviously, any change to a Contract Test will be automatically applied wherever it&#8217;s appropriate.  That is, by following this methodology, all concrete classes implementing the interface will be always be tested with the current version of the Contract Test.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.davidsoergel.com/2008/08/22/contract-tests-with-testng/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>s3napback: Cycling, Incremental, Compressed, Encrypted Backups to Amazon S3</title>
		<link>http://dev.davidsoergel.com/2008/05/07/s3napback-cycling-incremental-compressed-encrypted-backups-to-amazon-s3/</link>
		<comments>http://dev.davidsoergel.com/2008/05/07/s3napback-cycling-incremental-compressed-encrypted-backups-to-amazon-s3/#comments</comments>
		<pubDate>Thu, 08 May 2008 03:17:33 +0000</pubDate>
		<dc:creator>David Soergel</dc:creator>
				<category><![CDATA[Sysadmin]]></category>

		<guid isPermaLink="false">http://dev.davidsoergel.com/?p=7</guid>
		<description><![CDATA[The problem

In searching for a way to back up one of my Linux boxes to Amazon S3, I was surprised to find that none of the many backup methods and scripts I found on the net did what I wanted, so I wrote yet another one.



The design requirements were:


Occasional full backups, and daily incremental backups
Stream [...]]]></description>
			<content:encoded><![CDATA[<h2>The problem</h2>

<p>In searching for a way to back up one of my Linux boxes to Amazon S3, I was surprised to find that none of the many backup methods and scripts I found on the net did what I wanted, so <a href="http://dev.davidsoergel.com/trac/s3napback/">I wrote yet another one</a>.</p>

<p><span id="more-7"></span></p>

<p>The design requirements were:</p>

<ul>
<li>Occasional full backups, and daily incremental backups</li>
<li>Stream data to S3, rather than making a local temp file first (i.e., if I want to archive all of /home at once, there&#8217;s no point in making huge local tarball, doing lots of disk access in the process)</li>
<li>Break up large archives into manageable chunks</li>
<li>Encryption</li>
</ul>

<p>As far as I could tell, no available backup script (including, e.g. s3sync, backup-manager, s3backup, etc. etc.) met all four requirements.</p>

<p>The closest thing is <a href="http://js3tream.sourceforge.net">js3tream</a>, which handles streaming and splitting, but not incrementalness or encryption.  Those are both fairly easy to add, though, using tar and gpg, as <a href="http://js3tream.sourceforge.net/linux_tar.html">suggested</a> by the js3tream author.  However, the s3backup.sh script he provides uses temp files (unnecessarily), and does not encrypt.  So I modified it a bit to produce <a href="http://dev.davidsoergel.com/trac/s3napback/browser/trunk/s3backup-gpg-streaming.sh">s3backup-gpg-streaming.sh</a>.</p>

<p>That&#8217;s not the end of the story, though, since it leaves open the problem of managing the backup rotation.  I found the explicit cron jobs suggested on the js3tream site too messy, especially since I sometimes want to back up a lot of different directories.  Some other available solutions will send incremental backups to S3, but never purge the old ones, and so use ever more storage.</p>

<p>Finally, I wanted to easily deal with MySQL and Subversion dumps.</p>

<h2>The solution</h2>

<p>I wrote s3napback, which wraps js3tream and solves all of the above issues by providing:</p>

<ul>
<li>Dead-simple configuration</li>
<li>Automatic rotation of backup sets</li>
<li>Alternation of full and incremental backups (using &#8220;tar -g&#8221;)</li>
<li>Integrated GPG encryption</li>
<li>No temporary files used anywhere, only pipes and TCP streams (optionally, uses smallish temp files to save memory)</li>
<li>Integrated handling of MySQL dumps</li>
<li>Integrated handling of Subversion repositories, and of directories containing multiple Subversion repositories.</li>
</ul>

<p>It&#8217;s not rocket science, just a wrapper that makes things a bit easier.</p>

<p>Check out the <a href="http://dev.davidsoergel.com/trac/s3napback/">project page</a> for more info and to download it!</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.davidsoergel.com/2008/05/07/s3napback-cycling-incremental-compressed-encrypted-backups-to-amazon-s3/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>High iowait, high load on a 3ware 8506-4 solved</title>
		<link>http://dev.davidsoergel.com/2006/03/14/high-iowait-high-load-on-a-3ware-8506-4-solved/</link>
		<comments>http://dev.davidsoergel.com/2006/03/14/high-iowait-high-load-on-a-3ware-8506-4-solved/#comments</comments>
		<pubDate>Wed, 15 Mar 2006 03:36:08 +0000</pubDate>
		<dc:creator>David Soergel</dc:creator>
				<category><![CDATA[Sysadmin]]></category>

		<guid isPermaLink="false">http://dev.davidsoergel.com/2008/01/22/high-iowait-high-load-on-a-3ware-8506-4-solved/</guid>
		<description><![CDATA[The Symptoms

I have two 3ware 8506-4 controllers with three disks each connected to them.  I&#8217;ve been getting high iowait on my box whenever there&#8217;s an even slightly disk-intensive task going on.  The weird thing is that even processes that don&#8217;t use the high-usage disk get stalled in iowait.  As a result, load [...]]]></description>
			<content:encoded><![CDATA[<h2>The Symptoms</h2>

<p>I have two 3ware 8506-4 controllers with three disks each connected to them.  I&#8217;ve been getting high iowait on my box whenever there&#8217;s an even slightly disk-intensive task going on.  The weird thing is that even processes that don&#8217;t use the high-usage disk get stalled in iowait.  As a result, load skyrockets and the system becomes unresponsive.</p>

<p>Inducing disk load with bonnie++ and watching things with &#8220;iostat -x&#8221; revealed that even disks with extremely low bandwidth usage and very few i/o requests end up with 100% utilization.  However, only those disks on the same controller as the high-usage disk are affected.  The observed total throughput is around 50 Mb/sec, e.g., the limit of the high-usage disk.  It&#8217;s nowhere near the bandwidth limit of the controller, the SATA channel, or the PCI bus.  So, there&#8217;s something wrong with the controller that effectively limits it to control one disk at a time with reasonable performance.</p>

<p>The kernel is the current CentOS 3.6 one, 2.4.21-37.0.1.ELsmp.</p>

<p>The write cache is enabled on the controllers.</p>

<p>This seems to be related to at least some of the posts on the infamous <a href="https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=121434">RHEL vs. 3ware performance bug</a>.</p>

<h2>The Diagnosis</h2>

<p>Apparently, what is happening is that the four SATA channels on the controller share a single request queue, with a depth of 256 requests.  When requests to one disk arrive at a high rate, they fill the queue, thereby starving the other disks.</p>

<p>Controllers from other manufacturers typically limit the number of requests to a single disk to some number smaller than the total queue depth for the controller.  Thus, the queue for a single disk can get full without blocking access to the other disks.  Why 3ware made the default per-disk queue depth the same as the depth for the whole controller is beyond me.  Fortunately, this is correctable.</p>

<p><span id="more-6"></span></p>

<h2>The Solution</h2>

<p>I googled around for <a href="http://www.uwsg.iu.edu/hypermail/linux/kernel/0409.0/0327.html">some</a> <a href="http://www.ussg.iu.edu/hypermail/linux/kernel/0309.3/0316.html">hints</a>.</p>

<p>In 2.6 kernels, the queue depth can be adjusted using the sysfs mechanism (though that may require a <a href="http://www.uwsg.iu.edu/hypermail/linux/kernel/0409.0/0074.html">patch</a> to the 3w-xxxx driver).  However, I need to continue running a 2.4 kernel for a while.  So, the solution is to rebuild the driver with a different queue depth (and the kernel image, since I need to boot from these disks).</p>

<p>First I ran a bunch of tests to get a performance baseline using various readahead and elvtune settings:</p>

<pre><code>3wareIssueTests.sh
</code></pre>

<p>Then I downloaded the kernel source, configured it, and added</p>

<pre><code>CONFIG_3W_XXXX_CMD_PER_LUN=32
</code></pre>

<p>to <code>/usr/src/linux-2.4/.config</code></p>

<p>Then, bypassing much of the standard stuff for a custom kernel since this is a simple change:</p>

<pre><code>rm -f drivers/scsi/3w-xxxx.o
make modules
mv /lib/modules/2.4.21-37.0.1.ELsmp/kernel/drivers/scsi/3w-xxxx.o /lib/modules/2.4.21-37.0.1.ELsmp/kernel/drivers/scsi/3w-xxxx.o.orig
cp /usr/src/linux-2.4/drivers/scsi/3w-xxxx.o /lib/modules/2.4.21-37.0.1.ELsmp/kernel/drivers/scsi
mkinitrd /boot/initrd-2.4.21-37.0.1.ELsmp-3ware32b.img 2.4.21-37.0.1.ELsmp 
</code></pre>

<p>Then add a config with the new image to /etc/grub.conf, using the old image as a fallback:</p>

<pre><code>default 0
fallback 1
</code></pre>

<p>And reboot.</p>

<p>Then run 3wareIssueTests.sh again and watch the load average.  Hmm, that didn&#8217;t work !?  The original symptoms persist, and /proc/scsi/3w-xxxx/1 shows <code>Max commands posted: 254</code>, which shouldn&#8217;t happen.</p>

<p>Added</p>

<pre><code>printk(KERN_WARNING "3w-xxxx: set cmd_per_lun to %d.\n", tw_host-&gt;cmd_per_lun);
</code></pre>

<p>at line 1177 of 3w-xxxx.c</p>

<p>Aha, it&#8217;s still 254.  The .config change didn&#8217;t get picked up (maybe I needed <code>make dep</code> or something) so we can just add the compile flag manually:</p>

<pre><code>gcc -D__KERNEL__ -I/usr/src/linux-2.4.21-37.0.1.EL/include -Wall -Wstrict-prototypes -Wno-trigraphs -O2 -fno-strict-aliasing -fno-common  -Wno-unused -fomit-frame-pointer -pipe -freorder-blocks -mpreferred-stack-boundary=2 -march=i686 -DMODULE -DMODVERSIONS -include /usr/src/linux-2.4.21-37.0.1.EL/include/linux/modversions.h  -nostdinc -iwithprefix include -DKBUILD_BASENAME=3w_xxxx -DCONFIG_3W_XXXX_CMD_PER_LUN=32 -c -o 3w-xxxx.o 3w-xxxx.c
</code></pre>

<p>Then build a new image, reboot, and run the tests again as above.</p>

<p>Awesome, that worked.  iowait is still kinda high during bonnie, but that&#8217;s to be expected.  Load goes to 3, but not 12.  Disks on the same controller as the test disk appear to be unaffected.</p>

<p>The various readahead and elvtune settings seem to have minimal effect; I&#8217;ll just leave them as:</p>

<pre><code>blockdev --setra 120 /dev/sde
elvtune -r 2048 -w 8192 /dev/sde
</code></pre>

<p>Finally, other i/o performance tidbits:</p>

<ul>
<li>The disks are mounted with noatime</li>
<li>Apache uses a single access log file</li>
<li>Added
<code>echo 100 5000 640 2560 150 30000 5000 1884 2 &gt; /proc/sys/vm/bdflush</code>
to <code>/etc/rc.d/rc.local</code>, as proposed on <a href="http://people.redhat.com/alikins/system_tuning.html#fs">this handy tuning page</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://dev.davidsoergel.com/2006/03/14/high-iowait-high-load-on-a-3ware-8506-4-solved/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Powerbook performance overhaul</title>
		<link>http://dev.davidsoergel.com/2006/03/14/powerbook-performance-overhaul/</link>
		<comments>http://dev.davidsoergel.com/2006/03/14/powerbook-performance-overhaul/#comments</comments>
		<pubDate>Wed, 15 Mar 2006 03:26:44 +0000</pubDate>
		<dc:creator>David Soergel</dc:creator>
				<category><![CDATA[Mac]]></category>

		<guid isPermaLink="false">http://dev.davidsoergel.com/2008/01/22/powerbook-performance-overhaul/</guid>
		<description><![CDATA[My Powerbook G4 has been running way slow lately, and I finally have a little breathing room to address the problem.  My impression is that a) my disk is really full, b) my disk is really fragmented, and c) this especially impacts virtual memory.

So my solution is:


Confirm that my latest daily Retrospect backup worked.
Delete [...]]]></description>
			<content:encoded><![CDATA[<p>My Powerbook G4 has been running way slow lately, and I finally have a little breathing room to address the problem.  My impression is that a) my disk is really full, b) my disk is really fragmented, and c) this especially impacts virtual memory.</p>

<p>So my solution is:</p>

<ol>
<li>Confirm that my latest daily Retrospect backup worked.</li>
<li>Delete old &amp; large files; <a href="http://www.apple.com/downloads/macosx/system_disk_utilities/diskinventoryx.html">DiskInventoryX</a> helps a lot to identify them.</li>
<li>Quick Defragment in <a href="http://www.coriolis-systems.com/iDefrag-2.php">iDefrag</a> (oops, superfluous given later steps).
Holy crap, it crashed in the middle of defragmenting!  How nervewracking is that!?  OK, no more iDefrag. Sheesh.</li>
<li>Optimize/repair in Disk Utility</li>
<li><a href="http://www.coriolis-systems.com/iPartition.php">Carbon Copy Cloner</a> the disk to an image on another drive (using Target Disk Mode so the drive is not the boot disk)</li>
<li>Repartition to make a 2Gb Swap partition at the beginning of the disk  (Maybe I could have done this without the cloning step using <a href="http://www.bombich.com/software/ccc.html">iPartition</a>, but that&#8217;s expensive).</li>
<li><a href="http://www.coriolis-systems.com/iPartition.php">Carbon Copy Cloner</a> from the image back to the (now slightly smaller) main partition.  This should automatically defrag everything too.</li>
<li>Actually move the swap files to that partition using <a href="http://www.computer-support.ch/">Xupport</a></li>
<li>Quit Mail.app, Delete ~/Mail/Envelope Index, start it up again and rebuild</li>
<li>Full Spotlight reindex of the disk (automatic, since CCC deletes the index)</li>
<li>Disable unneeded services&#8230; e.g., I had MySQL running but I don&#8217;t really use it</li>
<li>Mail.app prefs: delete messages immediately; etc.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://dev.davidsoergel.com/2006/03/14/powerbook-performance-overhaul/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
