Monthly Archives: March 2011

Amazon EC2 flexibility: replacing a root volume in an instance

Time for some Amazon EC2 love.

Currently, our main servers are running on Amazon EC2. This last week, for a reason still unknown, we ran into a lack of disk space problem. The solution is to replace the disk, which in normal circumstances could be really bad… But with EC2, after understanding a little better how it works, this was simple and quick. And application downtime could be reduced to a minimum – around 20 minutes if I remember correctly – but this depends totally on the volume size.

These are the steps I had to take:

  • Stop the EC2 instance;
  • Take a snapshot of its volume;
  • Create a new volume, based on that snapshot, but with the desired new size;
  • Detach the old volume from the instance, attach the new, bigger one;
  • Start the instance again;
  • Execute ‘resize2fs /dev/sda1′ to make the linux installation aware of the new space.

Just remember to do all of this in the same availability zone, you cannot attach a volume from one zone into an instance from another one. You can use the command ‘df’ to check if the resize was successful.

Finally, the last step was to re-associate the correct IP address, so that everything works again as if nothing had happened.


Matching a range of numbers in Scala

Hi everybody!

Yesterday I did a presentation about Scala at Globalcode. It was very nice, thank you all who attended!

Now, one of the things I liked the most was the final part when people started making questions. I must admit I didn’t have answers to some of them, and this post is about one of those.

The question was something like: “can I pattern match against a range of numbers, like, say, 1 to 10?”

The answer is yes, although with a little more code than I thought necessary – yet, the solution is simple. First, if we want to match a simple number, we could write something like this:

myNumber match {
  case 10 => println("a ten")
  case num: Int => println("a number")
  case _ => println("something else")
}

In terms of matching numbers, we have two cases here. In one of them, we match directly a ’10′. In the other, we match any number. But how do we match a range of numbers? By adding something extra in the ‘any number’ case:

myNumber match {
  case 10 => println("a ten")
  case num if 0 until 100 contains num => println("a number between 0 and 100")
  case _ => println("something else")
}

This ‘something extra’ is called a ‘guard’. Guards allow us to add more verifications to values we match. With this, we can have any kind of restriction in any matched value. Also, we could have a ‘case’ for numbers between 0 and 100, and another one for any other number, like this:

myNumber match {
  case 10 => println("a ten")
  case num if 0 until 100 contains num => println("a number between 0 and 100")
  case num: Int => println("a number")
  case _ => println("something else")
}

Finally, please mind the order of the cases. Notice how we go from the more specific numbers, to the broader matching. This is not optional. you cannot put the broader case before the other ones – the code won’t even compile. In summary, this won’t work:

myNumber match {
  case num: Int => println("a number")
  case 10 => println("a ten")
  case num if 0 until 100 contains num => println("a number between 0 and 100")
  case _ => println("something else")
}

Now lets talks about how this work. Like a lot of things in Scala, ’0 until 100′ is not a special construct. It is implemented as library – which means you could have implemented this yourself. The steps that the scala compiler has to take to make this magic happen is something similar to the following:

  • 0 is an Int, so the compiler tries to find an ‘until’ method in the Int class, but it doesn’t exist;
  • the compiler finds out that the ‘until’ method exists in the ‘RichInt’ class;
  • it would be perfect if 0 was a RichInt… so the compiler now searches for a way to transform an Int in a RichInt;
  • luckily, it finds this: ‘implicit def intWrapper (x: Int) : RichInt’ in the Predef object;
  • 0 is then wrapped in a RichInt, and the compiler calls its until method, with ’100′ as the parameter;
  • the ‘until’ call results in a Range object and the compiler calls its ‘contains’ method, passing ‘num’ as the parameter;
  • the result of ‘contains’ is a boolean, which will decide if the number matches or not which what we want.

That’s quite a lot of stuff happening, even though there is nothing too complex in there. To close, one more last bit of information. Predef is an object that contains a lot of helper implicit methods, which are in the classpath of any scala application by default. Very handy.

I would like to thank Henrique Prage, who was in the Scala presentation, and sent me this link this morning: http://stackoverflow.com/questions/1346127/can-a-range-be-matched-in-scala – it really helped me find out quickly the answer to this question =)


sbt and maven

A few months ago I talked about my first impressions about sbt (simple build tool) here. My conclusion at the time was that NetBeans + Maven combination would suit me better. This changed a little bit, so I’m revisiting this topic to explain why.

First, I explored sbt a little more. I certainly still have a lot to learn, but I found out one thing that is making a lot of difference: you can use a maven pom file to handle your dependencies. This is great for a few reasons:

  • you can leverage your existing knowledge of pom files;
  • I’m not comfortable (yet) with the sbt own way of handing dependencies;
  • you can use tools that doesn’t support sbt directly – actually I don’t know of any that does that.

Lets elaborate some more. First, lets say you have a project that already uses maven. The first point means you can just type ‘sbt’, create an sbt project, and the tool will be able use the pom.xml file as a source for the project dependencies configuration. You will just have to use the sbt command ‘update’ after each time you change the pom file – which is very reasonable.

Second, the way sbt handles dependencies. Code. Basically, you configure the dependencies in a scala file, that gets compiled with the rest of the project. I don’t find this bad nor good. For now, I don’t have a strong opinion about this – but its still not comfortable.

Finally, the third point means you can use, say, NetBeans, with its great maven support, while using sbt as well. For a small personal project I’m currently working on, this is being very useful. My current development setup consists of NetBeans as the IDE, with sbt running in a console window in the ‘~test’ state. Every time I hit save on a source file inside NetBeans, sbt gets it, compiles and runs the tests. Yet, because I’m using an IDE instead of simple a code editor, a have full source code completion – which is really nice.

Of course this doesn’t work perfectly all the time – NetBeans gets confused from time to time and you might have to ignore some editor warnings. Nothing too bad though.


Follow

Get every new post delivered to your Inbox.