Release It!

Michael T. Nygard

Mentioned 12

Provides information on ways to effectively design and release an application.

More on Amazon.com

Mentioned in questions and answers.

I need some code samples (and I also really curious about them) of Scala and Java code which show that Scala code is more simple and concise then code written in Java (of course both samples should solve the same problem).

If there is only Scala sample with comment like "this is abstract factory in Scala, in Java it will look much more cumbersome" then this is also acceptable.

Thanks!

I like most of all accepted and this answers

The Circuit Breaker pattern from Michael Nygard's Release It in FaKods (link to code)

implementation looks like this in Scala:

. . .
addCircuitBreaker("test", CircuitBreakerConfiguration(100,10))
. . .


class Test extends UsingCircuitBreaker {
  def myMethodWorkingFine = {
    withCircuitBreaker("test") {
      . . .
    }
  }

  def myMethodDoingWrong = {
    withCircuitBreaker("test") {
      require(false,"FUBAR!!!")
    }
  }
}

Which I think is super nice. It looks just as a pice of the language but it is a simple mixin in the CircuitBreaker Object doing all work.

/**
 * Basic MixIn for using CircuitBreaker Scope method
 *
 * @author Christopher Schmidt
 */
trait UsingCircuitBreaker {
  def withCircuitBreaker[T](name: String)(f: => T): T = {
    CircuitBreaker(name).invoke(f)
  }
}

Reference in other languages Google for "Circuit breaker" + your language.

Yesterday I was having a heated debate with a coworker on what would be the preferred error reporting method. Mainly we were discussing the usage of exceptions or error codes for reporting errors between application layers or modules.

What rules do you use to decide if you throw exceptions or return error codes for error reporting?

In high-level stuff, exceptions; in low-level stuff, error codes.

The default behaviour of an exception is to unwind the stack and stop the program, if I'm writing a script an and I go for a key that's not in a dictionary it's probably an error, and I want the program to halt and let me know all about that.

If, however, I'm writing a piece of code which I must know the behaviour of in every possible situation, then I want error codes. Otherwise I have to know every exception that can be thrown by every line in my function to know what it will do (Read The Exception That Grounded an Airline to get an idea of how tricky this is). It's tedious and hard to write code that reacts appropriately to every situation (including the unhappy ones), but that's because writing error-free code is tedious and hard, not because you're passing error codes.

Both Raymond Chen and Joel have made some eloquent arguments against using exceptions for everything.

Is it advisable to zero-in on design of a component or architecture of a software with performance in mind? What I mean is, how ready should the design/architecture be to used in a performance-intensive environment?

While designing components, should we just follow good OO principles and just ensure that the component is 'extendable'. This way we tweak the design a bit here and a bit there as and when we run into performance issues. Although this way, we would often land up in performance issues where tweaking software a little bit might not help.

Alternatively, should we come up with a design, although complex, makes performance issues a cakewalk. We would still need to tweak the software, but the tweaking is often very simple as the design is performance-oriented.

Note: In neither of the cases listed above am I trying to tune the performance of the software before running into performance issues. To re-phrase the question, should the design of the software be performance-oriented?

Please do not answer me saying that it all depends on the environment in which a software is expected to run. The reason being that clients of any industrial-grade software just seem to want more and more and more all the time. You may not plan your software to be constantly running in performance-intensive environments, but what if it has to? Should we then re-design the software when we sense that?

This question has been troubling me from a week and I don't have an answer yet. What is your take on this?

Do not make any optimiyation in advance. But the ARCHITECTURE ha a huge impact on the potential performance of the system.

I sugges rading the following book: Release It!: Design and Deploy Production-Ready Software

I have been programming for a couple of years and am now studying computer-science at university. Every time I have coded something, I did so by starting up my editor and improvise. Writing methods, classes as I go. Off course, I think about it beforehand, I take out sketch paper and write my ideas down, do a little list of things to do, etc. This can work for writing little bits of code or simple and relatively small software, but how about complex ones?

When you want to make a complex piece of software, how do you blueprint it? Architects blueprints a building before starting any real work on it, and they do so by following standards. I expect programmers do to, but I have never heard of it. What are the methods, the tools and the steps that are being used to do so.

I don't want opinions.

I'd like something specific: Charts? Maps? Tools? Techniques? Step-by-steps process? ...

My "step-by-step" process would be this:

  1. Create a prototype. It doesn't have to do much, in many cases dragging a few controls on the form designer will do. Then go through the prototype with the customer/end-user. (This is the important part - if you just create the prototype and throw it over the wall, it wouldn't do any good.) Explain how you think the software would work when you show the prototype and listen to what the customer says. Again, listening is more important than explaining. After that, you should have a solid understanding of what the customer needs.
  2. Now I usually take out paper and pencil and start sketching the high-level module structure. Maybe also important classes, but not every last implementation detail. After this step, I know what modules I need, what the responsibility of each module is, how I can test each module.
  3. Implementation is done the way you know it (at least, I do it more or less the way you described it). But you only implement the basic core functionality at first. Otherwise you are bound to miss critical functionality because you're too busy adding bells and whistles.
  4. Rinse and repeat: You now have a program with limited functionality, go back to your customer, show it, let them play with it. Look at how they try to use it: Where do they fail? What are they looking for and can't find?

Book tip: Release It

I'm going to be starting a large scale software project involving a cross-platform GUI and heavy number crunching. I plan on writing most of the application back-end in C++ and CUDA, and the GUI in Qt4. I plan on using Make as my build system. This will be a project that will only have two developers, and will probably end up being an open source project once I get relatively far with it.

My main question is, does anyone have any tips/resources for developing the architecture behind the software.

  • What would be a good way to structure your files? (number crunching code in a separate folder than the gui code, which is separate from the compiled executables, or something else?)
  • How would the build system work for cross-platform software? Would I need to set-up a different build system for each platform, or could I create a common "makefile" that would build differently depending on which platform invoked it?
  • What kinds of modifications would I need to do to my code to ensure it will build properly on all platforms (pre-processor directives, etc...)?
  • How does someone handle revision control for large projects and integrate it into the build system so I get Version.Revision.Build numbers that update with each time I build my project?

I've been disheartened by the fact that my college that I'm about to graduate from covers almost none of this. At this point, I've done a lot of Google searches with little luck on what I need. I'm not asking to be spoon fed everything, I'd be perfectly content with links to books or websites relating to this.

I've done most of this stuff separately, but I've never tried to combine everything into one big package.

Would anyone recommend Release It!: Design and Deploy Production-Ready Software for what I'm inquiring about?

1) The structure can be the same, but if you're including files in subdirectories, make sure you use '/ and not \. The former will also work on windows.

2) I'd check out cmake ( http://www.cmake.org/ ) for managing building across multiple platforms. This will take a lot of pain out of the process for you.

3) Use STL and Boost as much as you can. These libraries have been designed with this very problem in mind. This will cover a huge portion of what you might need to do. You've already chosen the excellent Qt library, so your GUI is not an issue either. With these 3 combined, you're already most of the way there. That said, you might have to do something that is platform independent and not covered by any of these. For this I suggest you write a class that exposes a consistent interface across all platforms, and hide the nitty-gritty with preprocessor directives in the source file. It is imperative to expose a consistent interface.

4) Use SVN. I don't know how to do it on other compilers, but it is trivial in Visual Studio to get the latest revision and insert it in to some file that then gets included in your project. You can use build events to do this. Sorry I can't elaborate on other platforms.

In addition, if you have to head outside of STL, Boost, Qt etceteras, be mindful of the documentation of the API calls on all platforms. I have been bitten in the past by the behavior of a fseek() API call that wrote bytes to pad out to the destination on one implementation, but not the other. These can be a major cause of headache and another reason to try and stick to tried-and-tested libraries as mentioned above.

What I was taught at school is all about programming languages, software design, but hardly anything about how to automatically build a software, probably with something like unit testing integrated.

Please tell me how do one start learning to set up a build process for his project.

If this is too abstract to make any sense, I would add that I use Python and do web programming with Django.

Thanks in advance.

I like a couple of Pragmatic Programmers' books on this subject, Ship it! and Release it!. Together, they teach a lot of real-world, pragmatic stuff about such things as build systems and how to design well-deployable programs.

I need to find ways to add retry mechanism to my DB calls in case of timeouts, LINQ to SQL is used to call some sprocs in my code...

using (MyDataContext dc = new MyDataContext())
{
    int result = -1; //denote failure
    int count = 0;

    while ((result < 0) && (count < MAX_RETRIES))
    {
        result = dc.myStoredProc1(...);
        count++;
    }

    result = -1;
    count  = 0;
    while ((result < 0) && (count < MAX_RETRIES))
    {
        result = dc.myStoredProc2(...);
        count++;
    }

    ...

    ...
}

Not sure if the code above is right or posed any complications.

It'll be nice to throw an exception after MAX_RETRIES has reached, but I dunno how and where to throw them appropriately :-)

Any helps appreciated.

If you get a timeout from your database, it's not very likely that it's going to respond in a timely fashion a few miliseconds later.

Retrying in a tight loop as you suggest is likely to make a bad situation worse because you would be placing an undue burden on the database server, as well as tying up a thread in the calling code. It would be safer to introduce a wait time between each retry.

For more advanced scenarios, you could consider a progressive wait pattern where you retry more frequently in the beginning and then with longer and longer intervals if you still get timeouts.

You may also want to look into the Circuit Breaker design pattern from the book Release It!, as well as many of the other patterns and anti-patterns described in that book.

The State pattern is a good fit for implementing Circuit Breaker.

The Circuit Breaker pattern, from the book Release It!, protects a remote service from requests while it is failing (or recovering) and helps the client manage repeated remote service failure. I like Davy Brion’s stateful circuit breaker and Ayende’s lazy timeout fix is very clean.

However, I have not seen a lot of implementations of filtering which exceptions will cause an increase in the failure count of a circuit breaker.


Don't worry about showing locking, unless your implementation is particularly dependent on clever locking. FYI, Phil Haack appears to have the latest version of TimedLock, used in Davy Brion's articles.

Filter by Predicate

A predicate can provide extended criteria and filtering logic.

public void AttemptCall(Action action, Predicate<Exception> match)
{
    try
    {
        action();
    }
    catch(Exception e)
    {
        if(match(e))
            state.ActUponException(e);

        throw;
    }
}

For example, you may want to increase the circuit breaker only on a WebException caused by a timeout.

circuitBreaker.AttemptCall(() => service.DoWork(), e =>
    {
        WebException local = e as WebException;
        if(local == null)
            return false;

        return local.Status == WebExceptionStatus.Timeout;
    });

Does anyone know of excellent (in your opinion) books/websites that go over in details how to pragmatically use a bug/task/project management tool to help improve software development process? Ideally, I would like "from-the-trench" kind of resource, not theory.

Examples of such mgmt tools are: JIRA/Confluence combo, Visual Studio Team System / Sharepoint, FogBugz, etc.

I think I'm looking for books that are like the 3 Pragmatic book series (source control, unit testing, and build automation) Come to think of it, I wish the pragmatic guys also released a book on how to integrate project mgmt tools to their software development process.

I know of a few that might fit as the answers to my question:

Painless Project Management with FogBugz, Second Edition Visual Studio Team System: Better Software Development for Agile Teams Software Engineering with Microsoft Visual Studio Team System

Much like how we learn different programming languages to broaden our thought process, I dont really mind what tool the book/website uses. What I'm most interested in is HOW the tool is integrated into software development process to help improve it.

I tried to close the DB connection.But had a bit of confusion, say

ResultSet rs = null 

Whether I have to it close by

rs.close();

Or

DatabaseUtil.closeResultSet(rs);

What is difference between those?

Closing the resultSet doesn't close the database connection. You need to do that separately.

Typically you would want to close a resource like this:

if (resultSet != null) {
     try {
         resultSet.close();
     } catch (SQLException e) {
         log.info("closing resultset caused exception", e);
     }
}

If that is what your DatabaseUtil does, then that would be preferable.

Using try-with-resources results in prettier code, but be aware that the behavior for one edge case is very different. If an exception is thrown while closing the resultSet (after everything else has proceeded without a problem) then with the above code it is not propagated, so it doesn't stop the rest of the transaction from completing. However, if you use try-with-resources, the exception thrown by the close method will get propagated. The exception thrown while closing a resultSet or statement is only about freeing database resources (that will get freed eventually anyway, it's not a crisis), it doesn't indicate any data problems and it doesn't seem like a reason to torpedo the database operation.

You should close the database connection after closing the resultSet. Make sure you use a finally block so that closing the connection happens regardless of what exceptions are thrown (including exceptions thrown while closing statements or resultSets).

The code in this question is one example of how not to do it (it's likely the OP wanted to minimize the boilerplate in the example, since closing things is not the focus of the question, so it may be this way in order to be kept short, not because the OP doesn't know not to do this), it demonstrates a common error that happens in real life:

Class Class1<T>
{
  public T getColumn(DataSource ds)
  {
    T value = null;
    Connection con = null;
    Statement st = null;

    try
    {
      con = ds.getConnection();
      st = con.createStatement();
      ResultSet rs = st.executeQuery("select 1 from dual");
      rs.next();
      Object o = rs.getObject(1); // I want an Integer but a BigDecimal is created!
      value = (T) o; // ClassCastException here!
    }
    finally
    {
      if (st != null) { st.close(); }
      if (con != null) { con.close(); }
    }

    return i;
  }
}

If the statement.close throws a SQLException, then the following line in the finally block never gets called, the connection will not get closed, and the database connection will hang around unused until it times out. This may not seem bad since it's only one connection, but every other connection may see the same behavior and under the right circumstances you can bring your whole application down this way (throwing away all your database connections). See Michael Nygard's book Release It! for a story describing a very expensive and painful enterprise application outage that was traced back to code exactly like this.

Having to cut-and-paste this resource-closing code is ugly, it would be an improvement to introduce something like a template method that passes in a resultset mapper, so that the closing is done in only one place. But that puts you on the road to reinventing spring-jdbc, which has already done all this for you.

I want my python script to simultaneously accept POST variables and query string variables from the web address.

The script has code :

form = cgi.FieldStorage()
print form

However, this only captures the post variables and no query variables from the web address. Is there a way to do this?

Thanks,

Ali

cgi.parse_qsl (in any Python 2.*; urlparse.parse_qsl in 2.6 or better) take a query string and return a list of name, value pairs. Use os.environ['QUERY_STRING'] to get the query string part of the URL your CGI script was reached at (everything after the ? in the URL, if any).

We are experiencing socket time out exceptions with increased load on our IBM Websphere application server. We were trying to see if the timeouts are caused by slow speed on the application server. Or, is the timeout caused solely based on socket data transfer activity to our SOAP web-service. Basically, would increase load on the client machine cause a socket timeout? Or is the timeout solely caused by slow response time on the server end. Or both? My first thought is that the time out is caused because of slow response time from the server.

Here is an example error. This is Java web app (client) connecting to a windows machine, C# web-service.

WebServicesFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.generalException
faultString: java.net.SocketTimeoutException: Async operation timed out
faultActor: null
faultDetail: 

java.net.SocketTimeoutException: Async operation timed out
                at com.ibm.ws.webservices.engine.WebServicesFault.makeFault(WebServicesFault.java:283)
                at com.ibm.ws.webservices.engine.transport.http.HTTPSender.invoke(HTTPSender.java:725)
                at com.ibm.ws.webservices.engine.PivotHandlerWrapper.invoke(PivotHandlerWrapper.java:263)
                at com.ibm.ws.webservices.engine.PivotHandlerWrapper.invoke(PivotHandlerWrapper.java:263)
                at com.ibm.ws.webservices.engine.PivotHandlerWrapper.invoke(PivotHandlerWrapper.java:263)

IBM's documentation for Async operation timed out blames the server:

WSWS3228E exception occurs in WebService client (JAX-RPC) when invoking WebService call and WebService provider is taking more than default timeout (300 secs) to process the request.

The server is taking more than 5 minutes (300 seconds) to respond to the request. The most likely scenario is heavy load on the server. The other likely culprit is a slow/unresponsive backing service to the server (e.g. a database or another web service). There is a possibility it is the link between the client and server. You will need to get some metrics out of the server to determine where the requests are getting caught up.

Could I suggest reading Release It!. It lays out the reasons why distributed systems go bad and suggests ways to fix them.