Wednesday, October 31, 2012

The Business Rules Curmudgeon

Its been a while since my last post, in that time my wife and I have had a baby boy who was born in the 9th of June.

Something that I have been pondering for a while is how to reduce the number of small business rules that seem to creep into requirements. Taken individually these seem minor but as they add up you find yourself maintaining dozens of different small rules that in aggregate are complex. This is particularly the case where you have a feature that grows over time and you steadily add more pieces to it.

The part I struggle with is how to say no to these things without sounding like a curmudgeon. The upfront development cost is usually small but when multiplied out across analysis, testing and training materials. I haven't really worked out a way around this, small tasks will disappear within estimates and people often become fixed on their vision and persuading them that it is wrong is difficult or impossible.

Perhaps its time to embrace being a curmudgeon.

Thursday, May 10, 2012

Font Crashing / Terminating jvm in Linux with X11 forwarding

I ran into an issue recently where when a certain report was run on a specific server the jvm would disappear without a trace! There were no core dumps, no errors in logs just a 500 response from Apache  and the process was gone.

I struggled with this for a while as I could regularly reproduce the issue but only on one server. By pure co-incidence I could see the terminal when the application was started and there was a cryptic X11 error message:

X connection to localhost:10.0 broken (explicit kill or server shutdown)

This server also turned out to be one where we had to sudo su to a different user to start the application and had hacked around with the Magic Cookie for X11 forwarding. This mean that although I had X11 enabled on my session it was broken.

Removing the X11 forwarding from my ssh session fixed the issue. I never managed to work out if it was a bug in the JVM or if it was my broken X11 session sending a kill signal to the process,  full details of my digging over the jump.

Saturday, February 25, 2012

Java Puzzles

In the Java Specalists newsletter they had a link to some java puzzles from http://wouter.coekaerts.be/puzzles which were rather interesting. The first one had me for a while till I read the hints about hashset and I'm still not quite sure about the second puzzle.

My original solution used Thread.sleep which did not work but after some googling I changed it to wait on the sleeper which did work. I'm not exactly sure why this is but it is to do with Thread.sleep not releasing its lock on the synchronised block whereas the sleeper.wait means that the current thread is now waiting on the original thread.

Hopefully when they publish the solution it will be a bit more clear what is going on here.

package dream;

import sleep.Sleeper;

public class Dream {

    public void dream(Sleeper s) {

        DreamThread dreamThread = new DreamThread();
        dreamThread.sleeper = s;

        new Thread(dreamThread).start();
        try {
//             Thread.sleep(100);
            s.wait(100);
        } catch (InterruptedException e) {
        }
    }

    private class DreamThread extends Dream implements Runnable {

        private Sleeper sleeper;

        @Override
        public void run() {
            sleeper.enter(this);
        }

        public void dream(Sleeper s) {
           
            try {
//                 Thread.sleep(500);
                s.wait(500);
            } catch (InterruptedException e) {
            }
        }
    }
}

Wednesday, January 25, 2012

Hibernate Sequence generation

A while ago I was working with an older version of hibernate (3.3 I think) and had to do a few thousand inserts. Whilst hibernate is not necessarily the tool for this job I was using it to do the insertion and found it spent a large amount of time fetching the primary key id one by one from the database sequence.

To get around this you can use the TableHiLoGenrator but this means that your sequence values are not what you are actually inserting into the database which means that if you wanted to use the sequence later you will need to do some stuffing around.

I had a look at implementing my own sequence generator that relied on the sequence using an increment n and then the generator is configured with this value so that it only fetches the sequence value every n times. I got this working but then we ended up not using it as we found a better way of managing the bulk insertions.

Deadlocks in oracle 11g

We ran into an issue recently where we would get deadlocks on Oracle 11g but not on 10g. After some investigation it turns out there is a change in how Oracle locks rows in 11g which means that insertions to a table with a foreign key also lock the candidate key in the referenced table!

The fix was to add indexes to the column in the referenced table so that Oracle can use this instead of locking the row.

This blog post by Richard Foote had a good explanation of the issue.

Friday, June 24, 2011

Polymorphism and if statements

This post probably falls into the category of 'stating the obvious' and has been covered many times before but I figured I'd put the thoughts rolling around my head up here anyway.

Basically whenever I see a big block of code like this:

Car car

if (car.isBlue()) {
    car.doBlueStuff();
} else if car.isRead()){
    car.doRedStuff();
}

I kind of cringe and wonder if it could be done polymorphically with something like

BlueCar extends Car {
    doStuff();
}
RedCar extends Car {
    doStuff();
}

so the if statement becomes

car.doStuff();


Polymorphism 101.


Obviously things are not always as simple or clear cut as this but big switching if blocks with several branches should at least make you pause and think if this is the best solution.

Friday, March 18, 2011

Stuffing around with LVM on ubuntu

A while ago I installed Ubuntu and went a bit nuts with my partition table with separate partitions for /var /home /tmp etc. etc. I also setup raid and LVM with the intention of having a raid 1 backup partition and a logical volume for myth tv.

One of the results of this was that I didn't put enough space on /home which I have been working around by creating symlinks and directories that live in the large LVM partition I setup for myth tv. My plan to fix this is to remove one of the physical volumes from the myth LVM, create a new volume group for home and move my data into this group. I could create one big volume group with two logical volumes for home and myth data but I wanted to try creating a group from scratch.

This has been irking me for a while so I am finally going to fix it, below is what i ended up doing:

Removing a physical volume from the lvm group

As I used JSF for the format of the logical volume I couldn't reduce the size of the file system so I ended up just running
lvreduce --size 148G /dev/mapper/mythLVMGroup-mythData
which would have the result of losing any data that happened to be in the rest of the volume, I then reformatted it with
mkfs.jfs /dev/mapper/mythLVMGroup-mythData
at this point I could then modify the logical volume by removing the device from the group:
pvmove /dev/sdb8
lvreduce --size 148G /dev/mapper/mythLVMGroup-mythData
pvmove is rather slow so I started writing this post whilst I waited for that to complete. I should have just used the -A n flag as I had deleted the data prior to doing this anyway, also as there was no data it failed. One side result of this was the UUID of the device changed (not sure if was the mkfs or the change to the lvm) so I had to update my /etc/fstab with the new id which I found by running
ls -la /dev/disk/by-uuid/
After this I remove the physical volume from the group:
vgreduce mythLVMGroup /dev/sdb8
At which point I have completed my first step of removing and resizing the mythtv volume.

Setting up the volume group
There are plenty of other guides around about setting up LVM etc. but here is what I ended up doing:
Create the volume group from two partitions:
vgcreate -s 32M homeLVMGroup /dev/sdb1 /dev/sdb8
then create the logical volume:
lvcreate --size 259G -n home_lv homeLVMGroup
now to format it
mkfs.ext4 /dev/homeLVMGroup/home_lv
at which point I mounted it, copied my old home files across and then edited fstab to mount it as home when I rebooted. I did make one stupid mistake here where I had formatted it as ext4 but then had the fstab entry as ext3!

References
I mainly used the following two pages and google to help me get this working:
http://www.walkernews.net/2007/07/02/how-to-create-linux-lvm-in-3-minutes/ 
http://tldp.org/HOWTO/LVM-HOWTO/reducelv.html