The intersection of technology and leadership

Category: Development (Page 14 of 18)

Declaring Spring Programmatically

I’ve always been a big fan of Picocontainer because it has a lovely programmatic interface as an IoC container, and I generally value programmatic APIs over external representations, particularly for config files (note that it doesn’t mean external representations aren’t useful).

Unfortunately the followers of Spring love writing their code in XML, and many of their examples don’t show what you need to do to wire up all their examples declaratively in code. I spent some time trying to work out how to do some things.

Bean with an id ‘myBean’

<bean id="myBean" class="com.thekua.example.SomeClass">

in code:

GenericBeanDefinition someClassDefinition = BeanDefinitionBuilder.rootBeanDefinition(SomeClass.class).getBeanDefinition();
getDefaultListableBeanFactory().register("myBean", someClassDefinition);

Guided by this article: Programmatically Build A Spring Application Context

I’ll add more examples for the more complicated items later.

Coding styles leads to (or prevents) certain classes of bugs

I fixed a bug yesterday caused by a loop that terminated too early. It took me a while to understand the fact that it was terminating too early, and I realised that I’ve never written this type of bug because of the way that I tend to write code. Basically, the method looked like this:

public void someMethod() {
   for ( String key : keys ) {
       if (someCondition()) {
         return;
       }
       doSomeProcessingForThe(key);
   }
}

As you can see, the innocent looking return statement breaks out of the entire method, and what they really wanted was a continue statement instead. Note that TDD had been applied to this codebase, and it’s just another reminder that driving your code with tests isn’t enough (you also want to think of ensuring you write more comprehensive tests, particularly for loops).

I’m writing this post because I though it’s interesting that I realised a certain coding style removes (and possibly adds) a whole class of bugs. Had I been writing the code to start with, I probably would have avoided the use of the return (and/or continue) statement resulting in the following code:

public void someMethod() {
   for ( String key : keys ) {
       if (!someCondition()) {
         doSomeProcessingForThe(key);
       }       
   }
}

Prevention is easy with tools like checkstyle, PMD or findbugs (and maybe some of them already check for this). If not, I think it’d be easy to add.

Validating JVM Arguments in Code

I tried looking around on the Internet for a way from code, to get the arguments passed to the JVM itself, rather than environment properties, system properties, and program arguments. As of Java 5, apparently it got a whole lot easy in code than before with the java.lang.management.ManagementFactory.

Testing to see if you have a particular JVM argument is easy with a function like:

public boolean hasJvmArgument(String argument) {
  List arguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
  return arguments.contains(argument);
}

Why would you want to do that?
For some systems, it’s critical the system operates under a specially configured environment. In our current system, for example, we have a health check that runs a series of tests to validate those assumptions still hold true. To achieve our performance targets, it’s integral our application starts with the correct JVM settings, and even with automation in place, we potentially have mistakes. Fortunately with the code above, we can simply add a JvmHasCorrectSettingsCheck to guarantee the application never gets deployed under the wrong circumstances.

Thanks to Saager Mhatre for pointing me in the right direction!

Application logging principles

A friend asked me if I had any good links about logging in applications and I realised that I haven’t blogged about it. I think logging is an important part to any good application, so I thought I’d emphasis some principles I tend to follow when applying logging.

Logging cannot be considered in isolation of the exception handling strategies

Often I hear about teams who think that logging is a completely isolated concern for a system and exception handling is another. Though they seem like it, systems where exception handling strategies that don’t consider logging strategies and vice versa end up in a very confused state.

As a rule of thumb, my default position on exception handling looks like this:

  • If you can handle an exception, recover from it and continue
  • If you can’t handle an exception, but you can translate it, then do so and continue.
  • If you can’t handle an exception and you won’t translate it, then propagate it to someone who can.

Weaving in logging, it becomes a little bit more complicated.

  • If you’re going to handle an exception, you may choose to log the details. Ideally you might choose to log them at a finer level if it’s not a critical exception.
  • If you translate an exception and still propogate that, log the exception only if you aren’t going to propogate it (previous rule applies here to what level).
  • If you can’t handle an exception and you won’t translate it, don’t bother logging it. Ensure that someone higher up is going to apply the first two rules. There’s nothing more horrid than seeing multiple stack traces, reporting the same problem and there’s too much noise for the signal to be of any use.

Defend the user against stack traces

Applying the previous set of guidelines, then I always try to have one place in my application (at the outer most level possible) to prevent stack traces from appearing out of nowhere to the user. You might still choose to display it to a user for them to submit it back to a support team, or to simply log it to a file to be sent off. What should be avoided at all costs is crashing the application in spectacular fashion with only a stack trace as evidence.

Handling this at the outer most layer, hopefully from a single location, gives a few more benefits. You might choose to log additional information about the application, or render it in a different manner that proves useful for fixing problems.

Think of what would information would help you at 3am in the morning

The reason we log things is to have additional information about what is going on, when things aren’t going as expected. I like to think about what it would be like, to be called at 3am, only to diagnose an issue.

This is particularly difficult to do on iterative projects because you need to balance out enough logging with the cost of changing that logging. I try to add logging to parts of code that are fairly stable and whose implementation patterns will unlikely change.

If you TDD your code, and you reach for the debugger, you need to add some logging

TDD reduces the need to debug, however it never removes the need entirely. Debugging is a useful tool during development that lets us discover information our logs fail to tell us. Unfortunately it’s rare that we can apply this technique when our code is in production, therefore notice what information helps you when you debug and add its equivalent to a log somewhere.

If information logged is part of a fundamental business process, add some tests

I don’t necessarily test all log statements I add to code, however the cost of adding tests around some log statements if often worth it, especially if it’s critical information supporting a business process.

The pattern I use to test loggers is:

  1. Get a reference to the logger under test
  2. Save the logging level for that logger (to restore after the test runs)
  3. Turn up the logging level to the level you expect
  4. Execute the code that should trigger the logging
  5. Verify the correct message and information was logged
  6. (After the test – i.e. teardown) restore the logging level to the original value

Organise sets of information into groups and associate those groups with different log levels

Logging everything to a single level can often be too overwhelming. The trick to getting the right level of logging and information is to have a clear, shared understanding with the team about what information should be logged at what level.

Performance tuning garbage collection

Our current project has a heavy emphasis on throughput and latency, and our team spent a good amount of time building up the infrastructure to be able to performance test our application. We’re currently in the middle of performance tuning, and got to a point where garbage collection is a significant enough problem for us to start looking at options. I’ve never really had to do this low level tuning before and it’s amazing to see how many options you can tweak. Here’s a few links I’ve found that have been helpful. Please leave a comment if you think there’s more that would be of use and I’ll update the list.

  • Tuning Garbage Collection for Java 5 – A good background and explanation about some of the basic concepts and terms you need to understand before tuning.
  • Hotspot FAQ – A long list of questions (some of them quite funny too), with some decent answers.
  • HotSpot Options – A list of all (?) configurable parameters related to behaviour, performance and debugging.
  • BEA recommendations for garbage collection – A simple example of what values you might consider setting for a production application server.
  • A summary for JDK1.4.2 options – Providing a brief summary for an older version of the JVM.
  • List of all JVM options for Java 6 – Apparently a more complete list of options for Java 6.
  • Another list of JVM options – Though I’m not sure how up to date this one is. The author also makes mention that the list was compiled with Solaris OS in mind.
  • Blog entires about tuning – A blog detailing greater performance tuning options and details about some of them.

Please note that we’re using Sun’s JVM in production, and hence performance tuning against that JVM.

Scriptable vs Visual?

Most programs tend to follow two schools of philosophy in their design, one that I’m going to call ‘scriptable’, the other, ‘visual’. Those in the ‘scriptable’ camp tend to come from a unix background where being able to use pipes to direct input and output into different programs provides a simple, yet very powerful paradigm. Those found in the ‘visual’ camp tend to come from a windows background where almost everything has a GUI, and people mostly use a mouse to execute the functions.

Of coures there are different advantages and disadvantages to both approaches.

For example, discovering functionality in a ‘scriptable’ program is often not obvious simply by executing it. Without the program providing well written, detailed documentation and addtional examples, an end user can spend considerable time discovering how to do something trivial, rather than simply do it. Remembering special codes or parmaters, or the order it expects them in can be considered especially tediuous. Of course, the benefits of the having this interface allows the program to be automated as part of a larger job. The power of this approach is particularly useful for system administrators who may need to do something as part of a common job, or for developers who don’t want to reinvent the wheel.

The ‘visual’ approach, on the other hand, requires less documentation as long as the interface is easy to use and intuitive to the user (although this is often not the case). For people from a less technical background, using a ‘visual’ manner for discovering behaviour is a powerful paradigm and sometimes the user’s only option given their individual set of knowledge. Unfortunately, using this approach in isolation also means excluding the possibility of automating any functionality provided (or at least making it significantly more difficult).

I see people unconsciously make the choice between these two modes all the time. If I was to generalise, Java and Rubyists tend to gravitate towards the ‘scriptable’ interfaces (providing libraries, jars) while a lot of .Net people tend to gravitate towards the ‘visual’ (providing exes).

My preference for writing applications obviously leans towards a mixed approach where I would always want to prioritise the ‘scriptable’ interface above the ‘visual’ one. My thinking here is that the ‘scriptable’ interface should give enough separation between the functionality and the way an end user interacts with it that the ‘visual’ interface should be easy to drive out. In my experience, doing it the other way around leaves people coupling a lot more of their functionality into the graphical user interface, often making the ‘scriptable’ interface more difficult to extract.

Don’t get me wrong, as an end user, I almost always want a user interface for things that I’m going to do very infrequently. The developer in me also wants assurance that, in case that infrequent becomes frequent, I have the easy option of automating it later.

Death by a thousand differences

One of the most common smells I see on projects is the desire to do things slightly differently for no good reason. Triggered by many different reasons such as new people joining a project, or when new functionality is being added, or when bugs need to be fixed. Each individual difference isn’t a problem on its own, rather it’s their accumulative effect that has a significant negative impact on projects that force developers to continually context switch when trying to match their mental model against the code or to navigate their way around a new area in the system. I find the death by a thousand differences is especially a significant problem on iterative projects where change happens rapidly such as the project Frankie describes.

Code isn’t the only thing that suffers from this condition as well with many unwarranted differences built into configuration and build and deployment scripts (ant, nant and msbuild).

What to do about it?
In many ways Rails had a very strong opinion that I agree with, following convention over configuration, with a core principle that every difference should exist for a good reason. Projects need code standards to help maintain consistency, and needs to be updated to reflect the current state of the system. New team members need to understand what the standard is, why it exists and the whole team needs to be vigilant against the additional context-switch each new difference adds.

Sometimes differences must exist, but always apply the 5 whys to question if the difference is really needed. Sometimes they exist as a means to an end (a transition to a better implementation pattern) yet projects must be wary that not too many differences exist at the same time without a good reason.

Sprouting inner classses

Since returning to a more hands-on technical role, I’ve noticed a few habits that I didn’t realise I had, or have more recently acquired. One of these particular habits is (credit to Michael Feathers for the pattern name) is my tendency to sprout tiny classes.

Perhaps it’s my aversion to writing too much procedural code in an object-oriented language or it’s my preference to write small, well encapsulated objects. So far, my general approach seems to be:

I first notice a particular class’ responsibility has grown too much. An easy temptation is to move it to a function, and if I need to share it, might even be tempted to make it static. Instead, I sprout a static inner class who now owns that responsibility. I move any state needed for that responsibility to the class, keeping the tell, don’t ask principle in tact as much as possible. As I interact with the object more, I refine the class name, seeking to understand its responsibilities in different contexts. I might push more responsibilities into it, or move some responsibilities away from it.

I like to try to keep the class private until I’m happy that the class makes sense and all the responsibilities relate to each other in some logical manner. When I’m confident that the class is mature enough, I elevate it to a top level class.

What approaches do other people tend to favour? What can be improved? What doesn’t make sense?

WordPress 2.6.2 interoperability with Apache 2 Mod Proxy

I’ve had some issues upgrading to WordPress 2.6.2. Until recently I had been running 2.5 because I continuously received the error message, “Firefox has detected that the server is redirecting the request for this address in a way that will never complete.” I tried all the suggestions on the forums, yet nothing seemed to help with that. Both WordPress 2.6 and 2.6.1 exhibited this behaviour. Fortunately 2.6.2 never gave me this problem, instead giving me other problems. Looking at the source code, I was suspicious when the install.php had references to localhost:11008 when I viewed the source.

<link rel="stylesheet" 
href="http://localhost/trial/wp-admin/css/install.css?ver=20080708" 
type="text/css" media="all"/>

When I ran the installer, it also put that value in for the siteurl option. I looked at the source code and became suspicious of the code putting it in there, specifically in the function wp_guess_url():

$_SERVER["HTTP_HOST"]

I ran a test php page, and found that it was returning the localhost reference. I did some more searching, and found the following comments,

  • Be careful with HTTP_HOST behind a proxy server

I believe my server is running Apache Mod Proxy, where my virtual host is forwarded on from the main apache server. I asked my system administrator to play around with the ProxyPreserveHost configuration described here but unfortunately it didn’t seem to have much effect on the $_SERVER['HTT_HOST'] value.

I did a little bit more reading, and tried using a total wide fix described here, effectively executing some code to reset the HTTP_HOST to the HTTP_X_FORWARDED_HOST value yet that also didn’t seem to work.

In my .htaccess file, I had added a line:

php_value auto_prepend_file wordpress_fixes.php

and in a wordpress_fixes.php file, I had:

<?php
if(isset($_SERVER["HTTP_X_FORWARDED_HOST"]))
        $_SERVER["HTTP_HOST"] = $_SERVER["HTTP_X_FORWARDED_HOST"];
?>

yet it didn’t seem to do anything. Instead, I reverted back to changing all the $_SERVER['HTTP_HOST'] values into $_SERVER['HTTP_X_FORWARDED_HOST']

For everyone’s reference, here are all the files and the line numbers I had to change:

  • wp-includes/canonical.php (line 48)
  • wp-includes/cron.php (line 103)
  • wp-includes/feed.php (line 500)
  • wp-includes/functions.php (line 2327)
  • wp-includes/pluggable.php (line 669, line 685)
  • wp-login.php (line 20, line 274, line 275)

It also looks like it fixed the cron issue of publishing future posts, and trackbacks, so apologies if anyone got flooded with those recently.

« Older posts Newer posts »

© 2024 patkua@work

Theme by Anders NorenUp ↑