AWS CloudFormation Introduction

Posted in amazon, cloud by pedro | Sunday, February 27th, 2011 at 11:52 pm

AWS CloudFormation solves the complexity of creating collections of AWS Services and Resources (RDS, EC2, etc). We can use existing Templates or create our own. From chosen Template we can easily deploy our Stack, without worries about dependency resolution of ours Services. Read more on Amazon Blog: Create Your AWS Stack From a Recipe.

CloudFormation comes for free and is fully integrated into AWS Management Console. Unfortunately you have to sing up even if you already have active AWS Account. In my case it takes few seconds and I’ve have response in my inbox.

Amazon provides article how to setup step by step WordPress blog: AWS CloudFormation in the AWS Management Console. Today this resources are supported:

  • Auto Scaling Groups
  • AWS Elastic Beanstalk
  • CloudWatch Alarms
  • EBS Volumes
  • EC2 Instances
  • EC2 Security Groups
  • Elastic IP Addresses
  • Elastic Load Balancers
  • RDS Database Instances
  • RDS Security Groups
  • SimpleDB Domains
  • SNS Topics
  • SNS Subscriptions
  • SQS Queues

We can easily start with CloudFormation by choosing template from this list and than customize it. Template is text resource in JSON format which describes our resources and from template we can create our stack. Today list of templates:

  • WordPress (blog)
  • TextPattern (content management)
  • MoinMoin (wiki)
  • Tracks (project tracking)
  • Gollum (wiki used by GitHub)
  • Drupal (content management)
  • ReviewBoard (code review tool)
  • Movable Type Open Source (blog)
  • MantisBT (bug tracker)
  • Hibari (collaboration, wiki)
  • Joomla (content management)
  • Insoshi (social apps)
  • Redmine (project mgmt)

We may also create our template. Every template may have this sections:

  • AWSTemplateFormatVersion (required): 2010-09-09
  • Description (optional): We can provide comments about our template.
  • Parameters (optional): We can define parameter of String or CommaSeparatedList type with default value. This values may be changed during stack creation.
  • Mappings (optional): We can specify conditional parameter values. This is very similar to dictionary. To get value we use Fn::FindInMap function.
  • Resources (required): Here we define resources we need with resource parameters.
  • Outputs (optional): We can define information passed back to template user.

Additionally we have pseudo parameters and functions:

  • AWS::Region parameter returns AWS Region in which the resource is created.
  • AWS::StackName parameter returns name of the stack
  • Fn::Base64 function returns Base64 encoding of input string.
  • Fn::FindInMap function returns the value of a key from a mapping declared in the Mappings.
  • Fn::GetAtt function returns the value of an attribute from a resource.
  • Fn::GetAZs function returns comma-separated list of Availability Zones for the specified region.
  • Fn::Join function appends a set of values into a single value, separated by the specified delimiter.
  • Ref function allow us to use the logical name of any resource or parameter.

See WordPress template example for easy start.

 

Conclusions

 

Amazon is lider in public cloud computing. By introducing AWS CloudFormation Amazon makes entry into cloud computing easier. The cost is also important and AWS CloudFormation comes for free, we are only paying for Resources we are using. There is limit to 20 stacks we may create, but it seams reasonable and of course we may contact with Amazon to rise this limit. Amazon also provide detailed documentation: API Reference, Getting Started Guide, User Guide and a bonus Command Line Quick Reference Card.

All that make start with cloud computing is piece of cake!


JPA2 Metamodel – how to manage that.

Posted in java by pedro | Tuesday, February 22nd, 2011 at 4:49 pm

JPA2 added new typesafe Criteria API. This allows us to build query in strongly-typed manner instead of string based. To provide such functionality JPA2 use Metamodel API described in JSR-317: Chapter 5.

When entity manager factory is bootstrapping for persistence unit, then persistence provider have to initialize metamodel classes so this classes have to be accessible. For application developers it is important to easy generate them.

We can automatically generate metamodel classes we use Annotation Processing API. The structure of metamodel classes are described in JSR-317: Chapter 6.2.1.1. Briefly for every managed class there will be similar class with name of metamodel class plus “_”, and for every property there will be SingularAttribute for non-collection and
Attribute for Collection|Set|List|Map attribute.

Thanks to that metamodel classes we can write a query, which is type safe and probably more important we can easy do refactorings. That’s in theory, because IDE have to have access to metamodel classes, so the question arise how to manage this classes. I was wonder about three option here:

  1. Keep generated metamodel as normal code and commit into repository.
  2. Use external tool to generate metamodel and include results as IDE sources.
  3. Use IDE support, if exists.

First solution is portable, no IDE configuration, no problems between different IDE’s and so on. The disadvantage of this solution is that we have to remember about generating metamodel classes after we change our code and than commit metamodel into repository. Obviously we break the version control system rule, “don’t commit generated files“.

Second one assumes that you are using some kind of build tool (eg. maven). Then we can setup generation step into that tool (for maven it is maven-processor-plugin) and we setup our IDE to point to that sources. The bad thing about that is that we have to run the tool, otherwise we have not actual metamodel.

Third one is use your IDE for that. Sadly configuration isn’t automatic and every IDE has it’s own way to do this. JPA 2.0 Typesafe Criteria API and Annotation Processing Howto article describes how to do this in IntelliJ IDEA.

Both second and third solution has one additional flaw. You have to specify which annotation generator you want to use. That ties you to JPA provider implementation.

Here you find annotation processor names:

EclipseLink
org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor
Hibernate
org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor
OpenJPA
org.apache.openjpa.persistence.meta.AnnotationProcessor6

We can find processor parameters and switches in javadocs, in EclipseLink case, processor is in internal package, that makes finding parameters and switches a little bit harder, but we can check source code, or use Google.

I try to use third one and second one as fallback for people which use different IDE than IntelliJ, but I’m not happy with that solution.

How do you process  JPA2 metamodel classes?


Spring Template Objects Testing

Posted in java, spring, springframework, testing by pedro | Monday, February 21st, 2011 at 12:24 pm

My friend asked me, how we should test this piece of code

public void fire(final NotificationMessage message) {
  jmsTemplate.send(new MessageCreator() {
    @Override
    public Message createMessage(Session session) throws JMSException {
      return session.createObjectMessage(message);
    }
  });
}

Mock, stubs, arguments matchers are not successful here. Of course we can mock jmsTemplate, verify if send method was executed, but this way we only test if springframework works properly. Should we leave this to integration test?

Of course not, and our business part is: session.createObjectMessage(message). We have to change our code. First at all we provide second method.

public void fire(final NotificationMessage message) {
  this.fire(message, getMessageCreator(message));
}
public|protected| void fire(final NotificationMessage message, MessageCreator mc) {
  jmsTemplate.send(mc);
}

This two methods are easily testable by using mocks and verifying object interactions. We can decide about method modifier:

  • public – if we want second function to be available in our API.
  • protected – for future subclass usage.
  • package public – for use in package.
  • or even private

Finally we have decide about getMessageCreator(message).

  1. Provide Factory Method for it: MessageCrator getMessageCreator(NotificationMessage message);
    MessageCreator getMessageCreator(final NotificationMessage message) {
      return new MessageCreator() {
        @Override
        public Message createMessage(Session session) throws JMSException {
          return session.createObjectMessage(message);
        }
      };
    }
    
  2. Create Class which implements MessageCreator interface
    public class NotificationMessageCreator implements MessageCreator {
        private final NotificationMessage message;
    
        public NotificationMessageCreator(NotificationMessage message) {
            this.message = message;
        }
        @Override
        public Message createMessage(Session session) throws JMSException {
            return session.createObjectMessage(message);
        }
    }
    

I think both approaches are ok, so choose better one for you.

Of course you may argue that you don’t need unit test, you will have integration test for that part of the code. Unfortunately integration tests are slow and from time to time they fail, so for simple check I prefer unit test.

Summary

We use a lot of Spring Template Object helpers, and most of the time we are tempted to create anonymous class in place. This way we make our code hard to unit test. Consider this approach to make your code easier to test and maintain. What do you think about that?


Amazon Services for iOS and Android

Posted in amazon, ios, ipad by pedro | Tuesday, February 15th, 2011 at 7:34 pm

Every time we write an application which uses Amazon Web Services, we have to write our Web Services client with http connection processing and errors handling. Amazon gives us two SDKs to simplify this tasks:

Both SDKs comes with code samples, so we may start really quickly. Currently these services are supported:

I’m iPhone fan so I will show you an usage example by building simple application and deploying it on an iPad device.

Our first step is to download AWS SDK for iOS aws-ios-sdk-0.1.0.2.zip. Next we have to unpack zip file. Inside zip file we will find:

  • src directory with SDK sources.
  • samples directory with awsBrowser sample application.
  • Documentation directory with API documentation.
  • AWSiOSSDK.framework directory with ready to use AWS iOS SDK.
  • README.html file with description of zip content
  • NOTICE.txt – information about 3rd-party code (GTMLogger)
  • LICENSE.txt – Apache License version 2.0

Unfortunately latest version has errors in framework directory (instead of soft links we have files with link as a content). We have to change this:

-rwxr–r– 1 pedro staff 26 Jan 26 09:50 Resources
-rwxr–r– 1 pedro staff 24 Jan 26 09:50 Headers
-rwxr–r– 1 pedro staff 26 Jan 26 09:50 AWSiOSSDK
drwxr-xr-x 4 pedro staff 136 Feb 2 15:05 Versions

into this (There is one more in Version directory, it is called Current and should point to A directory):

drwxr-xr-x 5 pedro staff 170 Feb 2 14:41 Versions
lrwxr-xr-x 1 pedro staff 26 Feb 2 14:42 AWSiOSSDK -> Versions/Current/AWSiOSSDK
lrwxr-xr-x 1 pedro staff 24 Feb 2 14:42 Headers -> Versions/Current/Headers
lrwxr-xr-x 1 pedro staff 26 Feb 2 14:43 Resources -> Versions/Current/Resources

Library API

Actually AWS SDK supports Amazon S3, Amazon SimpleDB, Amazon SQS and Amazon SNS.

If you want to use other Amazon services you should subclass AmazonWebSerciceClient class and use invoke: method with AmazonServiceRequest parameter (the best way is to create subclass). The method will return AmazonServiceResponse (of course subclassing for easier access to specific information will be the best choice).

Every service has own client with specific methods for service (eg. S3 client has listBuckets: and SQS client has listQueues: method):

API is very intuitive and has very good documentation, so let us move forward to the example.

Example

OK, now we are ready to start our project.

  1. Start Xcode.
  2. Choose: Create a new Xcode project.
  3. Choose: Split View-base Application.
  4. Give name to your project.
  5. Attach AWSiOSSDK.framework to project.

RootViewController is UITableViewController, which is displayed in right master part of the split view. We will use it to show our Amazon S3 bucket list (listBuckets message). The main part will be used to display list of objects stored in bucket (listObjectsInBucket message) with additional information for current context, and that’s all.

Here you will find code: awsSimpleExample.

Summary

This library simplify integration iPhone and Android application with Amazon services. The latest iOS SDK API is described on this site docs.amazonwebservices.com/AWSiOSSDK/latest. I hope Amazon developers will add other services soon.


Spring Data – Redis – tutorial

Posted in java, spring, springframework by pedro | Sunday, January 30th, 2011 at 2:49 pm

Spring Data project provides a solution to access data stored in new emerging technologies like NoSQL databases, cloud based services etc. When we look into SpringSource git repository we see a lot of spring-data projects:

  • spring-data-commons: common interfaces and utility class for other spring-data projects.
  • spring-data-column: support for column based databases. It has not started yet, but there will be support for Cassandra and HBase
  • spring-data-document: support for document databases. Currently MongoDB and CouchDB are supported.
  • spring-data-graph: support for graph based databases. Currently Neo4j is supported.
  • spring-data-keyvalue: support for key-value databases. Currently Redis and Riak are supported and probably Membase will be supported in future.
  • spring-data-jdbc-ext: JDBC extensions, as example Oracle RAC connection failover is implemented.
  • spring-data-jpa: simplifies JPA based data access layer.

I would like to share with you how you can use Redis. First step is to  download it from redis.io web page. Here is useful site try.redis-db.com where we can run Redis commands and also read step by step tutorial. This tutorial shows us all structures Redis supports (list,set,sorted set and hashes) and some useful commands. A lot of reputable sites use Redis today .

After download and unpacking we should compile Redis (version 2.2 (it is release candidate) is preferable one to use since some commands do not work in version 2.0.4).

> make

> sudo make install

Once we run this commands we are all set to run the following five commands:

  • redis-benchmark – for benchmarking Redis server
  • redis-check-aof – check the AOF (Aggregate Objective Function), and it can repair that.
  • redis-check-dump – check rdb files for unprocessable opcodes.
  • redis-cli – Redis client.
  • redis-server – Redis server.

We can test Redis server.

>redis-server

[1055] 06 Jan 18:19:15 # Warning: no config file specified, using the default config. In order to specify a config file use ‘redis-server /path/to/redis.conf’

[1055] 06 Jan 18:19:15 * Server started, Redis version 2.0.4

[1055] 06 Jan 18:19:15 * The server is now ready to accept connections on port 6379

[1055] 06 Jan 18:19:15 – 0 clients connected (0 slaves), 1074272 bytes in use

and Redis client.

> redis-cli

redis> set my-super-key “my-super-value”

OK

Now we create a simple Java project in order to show how simple a spring-data-redis module essentially is.

> mvn archetype:create -DgroupId=info.pietrowski -DpackageName=info.pietrowski.redis -DartifactId=spring-data-redis -Dpackage=jar

Next we have to add in pom.xml milestone spring repository, and add spring-data-redis as dependence, after that all needed dependencies will be fetched.

Next we are creating resources folder under main folder, and create application.xml which will have all the configuration.

We can configure the JedisConnectionFactory,  in two different ways, One – we can provide JedisShardInfo object in shardInfo property or second – we can provide host (default localhost), port (default 6379), password (default empty) and timeout (default 2000) properties. One think to keep in mind is  that JedisShardInfo object has precedence and allows to setup weight, but only allows constructor injection.

We can set factory to use connection pooling by setting the value of propery  pooling to ‘true’ (default).

See application.xml comments to see three different way of configuration.

Note: There are two different libraries supported Jedis and JRedis, there have very similar names and both have the same factory name. See the difference (two r isn’t enough for me):

  • org.springframework.data.keyvalue.redis.connection.jedis.JedisConnectionFactory
  • org.springframework.data.keyvalue.redis.connection.jredis.JredisConnectionFactory

Similar to what we do in Spring, we configure template object providing it with connection factory. We will perform all the operations through this template object. By default we need to provide only Connection Factory, but there are more properties we can provide:

  • exposeConnection (default false) – if we return real connection or proxy object.
  • keySerializer, hashKeySerializer, valueSerializer, hashValueSerializer (default JdkSerializationRedisSerializer) which delegates serialization to default Java serialization mechanism.
  • stringSerializer (default StringRedisSerializer) which is simple String to byte[] (and back) serializer with UTF8 encoding.

We are ready to execute some code which will be cooperating with Redis instance. Spring-Data provide us with two ways of interaction, First is by using execute method and providing RedisCallback object. Second is by using *Operations helpers (it will be explained later)

When we are using RedisCallback we have access to low level Redis commands, see this list of interface (I won’t put all the method here because it is huge list):

Check RedisCallbackExample class, this was the hard way and the problem is we have to convert our objects into byte arrays in both directions, the second way is easier. Spring Data provides for us Operations objects, than we have much more simpler API and all byte<->object conversion is made by serializer we setup (or the default one). Higher level API (you will easily recognize *Operation *Commands equivalents):

Most of methods get key as first parameters so we have even better API for multiple operation on the same key:

Check RedisCallbackExample class to see some easy examples of *Operations usage. One important thing to mention is that you should use stringSerializers for keys, otherwise you will have problems from other clients, because standard serialization adds class information. Otherwise you end with such keys:

  1. “\xac\xed\x00\x05t\x00\x05atInt”
  2. “\xac\xed\x00\x05t\x00\nmySuperKey”
  3. “\xac\xed\x00\x05t\x00\bsuperKey”

Till now we just check API for Redis, but Spring Data offers more for us. All cool stuff is in org.springframework.data.keyvalue.redis.support package and all sub-packages. We have than:

  • RedisAtomicInteger – Atomic integer (CAS operation) backed by Redis.
  • RedisAtomicLong – Same as previous for Long.
  • RedisList – Redis extension for List, Queue, Deque, BlockingDeque and BlockingQueue with two additional methods List range(start, end) and RedisList trim(start, end).
  • RedisSet – Redis extension for Set with additional methods: diff, diffAndStore, intersect, intersectAndStore, union, unionAndStore.
  • RedisZSet – Redis extension for SortedSet. Note that Comparator is not applicable here so this interface extends normal Set and provide proper methods similar to SortedSet.
  • RedisMap – Redis extension for Map with additional Long increment(key, delta) method

Every interface currently have one Default implementation. Check application-support.xml for examples of configuration and RedisSupportClassesExample for examples of use. There is lot of useful information in the comments as well.

Summary

The library is first milestone release so there are minor bugs, documentation isn’t as perfect as we used to and current version needs no stable Redis server but this is definitely a great library which allow us to use all this cool NoSQL stuff in a “standard” Spring Data Access manner.

Awesome job!

This post is only useful if you checkout the code: from bitbucket , for the lazy ones here is spring-data-redis zip file as well.


about me

My name is Sebastian Pietrowski. I've finished Warsaw University as Master degree. During my studies I started work for merlin.pl. The primary language I use is Java but I have also programmed in Python, Ruby and Scala. I worked as a technical solution architect at merlin.pl. infrastructure when we were moving from PL/SQL to J2EE. I engineering a great performance optimized solution that made the application 10 times faster than requirements and 85 times faster as original solution.

Currently, I am working as a Senior Expert at F.Hoffmann-La Roche to help define future roadmap in design and development of Enterprise software at Roche and Genentech and build adoption for new technologies. I'm continuously mentoring new developers, helping them understand how important test driven development is and empowering them to get better at their daily job. I'm involved in many activities which brings new technologies for better and faster development. You can find more details on my LinkedIn profile.

But don’t get me wrong, I am not your typical nerd. I'm a pleasant guy that you can drink a glass of wine with me and talk about a range of topics with. My leisure activities include playing basketball, soccer and listening to music. I try to be pragmatic while staying focused on application performance and tuning with success in my daily work.

My favorite quote from Yoda's and my life’s motto is: Do, or do not. There is no try.