Wednesday, December 23, 2015

Hazelcast basics and config issues

Very basics

Hazelcast is a open-source data-grid framework for java. It supports easy provisioning of distributed versions of common java data structures, locks and a simple gossip implementation.

To include it in a maven based project simply add something like the following in your pom file:
<dependency>
    <groupId>com.hazelcast</groupId>
    <artifactId>hazelcast</artifactId>
    <version>3.5.4</version>
</dependency> 
Then all that's left to do is create an instance and create and to create a simple distributed map do the following:
Config config = new Config();
HazelcastInstance h = Hazelcast.newHazelcastInstance(config);
IMap<String, String> map = h.getMap("my-map");
Basic usage of the map is the same as a ConcurrentMap with some extra functions mainly related to locking. In case of maps the hash range will be split among members and data will be divided based on keys. Default settings also sets a backup for each key. The default behaviour also requires all writes to ack from the replica for the master to commit the write.

Config issues

When first trying to use Hazelcast these are some issues that I came across that could be better documented:

1. Multicast

If you not specified Hazelcast will use Multicast for node discovery. While this wouldn't be an issue for deployments on bare servers, it would cause issues when used on VMs including AWS. To change service discovery to seed based add something like the following to the config.

    Config conf = new Config();
    JoinConfig joins = conf.getNetworkConfig().getJoin();
    joins.getMulticastConfig().setEnabled(false);
    joins.getTcpIpConfig().addMember(seed);
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(conf);

Since Hazelcast uses a gossip implementation to manage joining and leaving nodes the host passed as seed will only be used when first joining the cluster, ones in the cluster, members are handled by Hazelcast.
There is also a discovery service specifically for AWS available but I have not tried it though.

2. Listen interface

With default config I couldn't get Hazecast to listen to the network interface (both on machines with single and multiple interfaces). To get this working I explicitly set Hazelcast up to listen to 0.0.0.0 and to do that I added the following to the config factory.

    conf.getNetworkConfig()
        .getInterfaces()
        .setEnabled(true)
        .addInterface("0.0.0.0");

This will enable the listen interface and set it to listen to 0.0.0.0. This can of corse be changed to a specific ip to make it listen to only one interface.

3. Shutdown hook capturing

By default Hazelcast will capture sigterm and shutdown gracefully when possible. While this behaviour is probably preferred for most cases, for my use case however I needed more fine grained control, so I needed to add the following setting:

    conf.setProperty("hazelcast.shutdownhook.enabled", "false");

When setting this just make sure to call shutdown on the instance when shutting down to not risk dataloss.

2 comments:


  1. Excellent post!!! The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Hadoop training in chennai

    ReplyDelete