3

I am using hazelcast using the client and server setup (not peer to peer). Should I create an instance of hazelcast client and instance using a singleton pattern (I am using lazy idiom which probably needs another look in any case). Should I use a singleton or just call HazelcastInstance hz = Hazelcast . newHazelcastInstance (); to get an instance whenever I need one?

Hazelcast accessor used in server implementation:

public class HCastAccessor {

    private static final Logger Logger = LoggerFactory.getLogger(HCastAccessor.class.getName());

//  private volatile static HCastAccessor instance = null;  
    private HCastAccessor() {
    }

    //lazy loaded holder idiom
    private static class LazyHolder {
        private static final HCastAccessor INSTANCE = new HCastAccessor();
    }

    public static HCastAccessor getAccessorInstance() {
        return LazyHolder.INSTANCE;
    }

    public HazelcastInstance getHCastInstance() {
        HazelcastInstance hcast = null;
        try {
            final Config config = new FileSystemXmlConfig("resources/hazelcast.xml");  
            hcast = Hazelcast.newHazelcastInstance(config);  
        }
        catch (FileNotFoundException fe) {
            Logger.error("File not found for Hazelcast:{}" + fe.getMessage());
        }
        catch (Exception e) {
            Logger.error("Exception while starting Hazelcast server:{}" + e.getMessage());
        }
        return hcast;
    }
}

reference for hazelcast retrieved:

    final HCastAccessor hcastAccessor = HCastAccessor.getAccessorInstance();
    HazelcastInstance hcast = hcastAccessor.getHCastInstance(); // handle shutdown as well

I am accessing multiple IMaps after that but in the same class.

log statements:

2014-09-05 05:15:29,473 INFO c.a.w.StartHcastServer [main] Starting Hazelcast Server on 2014/09/05 05:15:29
2014-09-05 05:15:30,105 INFO c.h.i.DefaultAddressPicker [main] null [dev] [3.2.4] Interfaces is disabled, trying to pick one address from TCP-IP config addresses: [127.0.0.1]
2014-09-05 05:15:30,113 INFO c.h.i.DefaultAddressPicker [main] null [dev] [3.2.4] Picked Address[127.0.0.1]:5701, using socket ServerSocket[addr=/0.0.0.0,localport=5701], bind any local is true
2014-09-05 05:15:30,336 INFO c.h.system [main] [127.0.0.1]:5701 [dev] [3.2.4] Hazelcast 3.2.4 (20140721) starting at Address[127.0.0.1]:5701
2014-09-05 05:15:30,336 INFO c.h.system [main] [127.0.0.1]:5701 [dev] [3.2.4] Copyright (C) 2008-2014 Hazelcast.com
2014-09-05 05:15:30,336 INFO c.h.i.Node [main] [127.0.0.1]:5701 [dev] [3.2.4] Creating TcpIpJoiner
2014-09-05 05:15:30,343 INFO c.h.c.LifecycleService [main] [127.0.0.1]:5701 [dev] [3.2.4] Address[127.0.0.1]:5701 is STARTING
2014-09-05 05:15:30,446 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5701 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5703
2014-09-05 05:15:30,449 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5701 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5702
2014-09-05 05:15:30,454 INFO c.h.n.SocketConnector [hz._hzInstance_1_dev.cached.thread-2] [127.0.0.1]:5701 [dev] [3.2.4] Connecting to /127.0.0.1:5703, timeout: 0, bind-any: true
2014-09-05 05:15:30,456 INFO c.h.n.SocketConnector [hz._hzInstance_1_dev.cached.thread-2] [127.0.0.1]:5701 [dev] [3.2.4] Could not connect to: /127.0.0.1:5703. Reason: SocketException[Connection re
fused to address /127.0.0.1:5703]
2014-09-05 05:15:30,456 INFO c.h.n.SocketConnector [hz._hzInstance_1_dev.cached.thread-3] [127.0.0.1]:5701 [dev] [3.2.4] Connecting to /127.0.0.1:5702, timeout: 0, bind-any: true
2014-09-05 05:15:30,464 INFO c.h.n.SocketConnector [hz._hzInstance_1_dev.cached.thread-3] [127.0.0.1]:5701 [dev] [3.2.4] Could not connect to: /127.0.0.1:5702. Reason: SocketException[Connection re
fused to address /127.0.0.1:5702]
2014-09-05 05:15:31,450 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5701 [dev] [3.2.4]


Members [1] {
        Member [127.0.0.1]:5701 this
}

2014-09-05 05:15:31,482 INFO c.h.c.LifecycleService [main] [127.0.0.1]:5701 [dev] [3.2.4] Address[127.0.0.1]:5701 is STARTED
2014-09-05 05:15:31,488 INFO c.a.c.n.SendServerEmail [main] hazelcast server email notification for server 192.168.110.154 WildMetrix Hazelcast server launched at 192.168.110.154
2014-09-05 05:15:32,421 INFO c.h.p.InternalPartitionService [hz._hzInstance_1_dev.cached.thread-1] [127.0.0.1]:5701 [dev] [3.2.4] Initializing cluster partition table first arrangement...
2014-09-05 05:15:32,615 INFO c.h.i.DefaultAddressPicker [main] null [dev] [3.2.4] Interfaces is disabled, trying to pick one address from TCP-IP config addresses: [127.0.0.1]
2014-09-05 05:15:32,616 INFO c.h.i.DefaultAddressPicker [main] null [dev] [3.2.4] Picked Address[127.0.0.1]:5702, using socket ServerSocket[addr=/0.0.0.0,localport=5702], bind any local is true
2014-09-05 05:15:32,690 INFO c.h.system [main] [127.0.0.1]:5702 [dev] [3.2.4] Hazelcast 3.2.4 (20140721) starting at Address[127.0.0.1]:5702
2014-09-05 05:15:32,690 INFO c.h.system [main] [127.0.0.1]:5702 [dev] [3.2.4] Copyright (C) 2008-2014 Hazelcast.com
2014-09-05 05:15:32,691 INFO c.h.i.Node [main] [127.0.0.1]:5702 [dev] [3.2.4] Creating TcpIpJoiner
2014-09-05 05:15:32,692 INFO c.h.c.LifecycleService [main] [127.0.0.1]:5702 [dev] [3.2.4] Address[127.0.0.1]:5702 is STARTING
2014-09-05 05:15:32,701 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5703
2014-09-05 05:15:32,701 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5701
2014-09-05 05:15:32,702 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to /127.0.0.1:5703, timeout: 0, bind-any: true
2014-09-05 05:15:32,702 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Could not connect to: /127.0.0.1:5703. Reason: SocketException[Connection re
fused to address /127.0.0.1:5703]
2014-09-05 05:15:32,702 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to /127.0.0.1:5701, timeout: 0, bind-any: true
2014-09-05 05:15:32,708 INFO c.h.n.SocketAcceptor [hz._hzInstance_1_dev.IO.thread-Acceptor] [127.0.0.1]:5701 [dev] [3.2.4] Accepting socket connection from /127.0.0.1:38086
2014-09-05 05:15:32,722 INFO c.h.n.TcpIpConnectionManager [hz._hzInstance_1_dev.IO.thread-Acceptor] [127.0.0.1]:5701 [dev] [3.2.4] 5701 accepted socket connection from /127.0.0.1:38086
2014-09-05 05:15:32,722 INFO c.h.n.TcpIpConnectionManager [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] 38086 accepted socket connection from /127.0.0.1:5701
2014-09-05 05:16:09,722 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Couldn't join to the master : Address[127.0.0.1]:5701
2014-09-05 05:16:09,722 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Failed to connect, node joined= false, allConnected= false to all other members after 0 seconds.
2014-09-05 05:16:09,723 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Rebooting after 10 seconds.
2014-09-05 05:16:19,724 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5703
2014-09-05 05:16:19,724 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5701
2014-09-05 05:16:19,724 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to /127.0.0.1:5703, timeout: 0, bind-any: true
2014-09-05 05:16:19,724 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Could not connect to: /127.0.0.1:5703. Reason: SocketException[Connection re
fused to address /127.0.0.1:5703]
2014-09-05 05:16:45,732 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Failed to connect, node joined= false, allConnected= false to all other members after 0 seconds.
2014-09-05 05:16:45,732 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Rebooting after 10 seconds.
2014-09-05 05:16:55,733 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5703
2014-09-05 05:16:55,733 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5701
2014-09-05 05:16:55,733 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-3] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to /127.0.0.1:5703, timeout: 0, bind-any: true
2014-09-05 05:16:55,734 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-3] [127.0.0.1]:5702 [dev] [3.2.4] Could not connect to: /127.0.0.1:5703. Reason: SocketException[Connection re
fused to address /127.0.0.1:5703]
2014-09-05 05:17:21,740 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Failed to connect, node joined= false, allConnected= false to all other members after 0 seconds.
2014-09-05 05:17:21,740 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Rebooting after 10 seconds.
2014-09-05 05:17:31,741 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5703
2014-09-05 05:17:31,741 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5701
2014-09-05 05:17:31,742 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to /127.0.0.1:5703, timeout: 0, bind-any: true
2014-09-05 05:17:31,742 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Could not connect to: /127.0.0.1:5703. Reason: SocketException[Connection re
fused to address /127.0.0.1:5703]
2014-09-05 05:17:31,743 WARN c.h.n.ConnectionMonitor [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Removing connection to endpoint Address[127.0.0.1]:5703 Cause => java.net.
SocketException {Connection refused to address /127.0.0.1:5703}, Error-Count: 5
2014-09-05 05:17:57,749 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Failed to connect, node joined= false, allConnected= false to all other members after 0 seconds.
2014-09-05 05:17:57,749 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Rebooting after 10 seconds.
2014-09-05 05:18:07,750 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5703
2014-09-05 05:18:07,750 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5701
2014-09-05 05:18:07,751 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to /127.0.0.1:5703, timeout: 0, bind-any: true
2014-09-05 05:18:07,751 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Could not connect to: /127.0.0.1:5703. Reason: SocketException[Connection re
fused to address /127.0.0.1:5703]
2014-09-05 05:18:07,751 WARN c.h.n.ConnectionMonitor [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Removing connection to endpoint Address[127.0.0.1]:5703 Cause => java.net.
SocketException {Connection refused to address /127.0.0.1:5703}, Error-Count: 6
2014-09-05 05:18:33,760 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Join try count exceed limit, setting this node as master!
2014-09-05 05:18:33,760 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4]


Members [1] {
        Member [127.0.0.1]:5702 this
}

2014-09-05 05:18:33,760 WARN c.h.i.Node [main] [127.0.0.1]:5702 [dev] [3.2.4] Config seed port is 5701 and cluster size is 1. Some of the ports seem occupied!
2014-09-05 05:18:33,761 INFO c.h.c.LifecycleService [main] [127.0.0.1]:5702 [dev] [3.2.4] Address[127.0.0.1]:5702 is STARTED
2014-09-05 05:18:33,777 INFO c.h.c.ClusterService [hz._hzInstance_1_dev.global-operation.thread-4] [127.0.0.1]:5701 [dev] [3.2.4]

Members [2] {
        Member [127.0.0.1]:5701 this
        Member [127.0.0.1]:5702
}

2014-09-05 05:18:33,784 WARN c.h.p.InternalPartitionService [hz._hzInstance_2_dev.global-operation.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] This is the master node and received a PartitionRuntimeSt
ate from Address[127.0.0.1]:5701. Ignoring incoming state!
2014-09-05 05:18:33,826 WARN c.h.p.InternalPartitionService [hz._hzInstance_2_dev.global-operation.thread-1] [127.0.0.1]:5702 [dev] [3.2.4] This is the master node and received a PartitionRuntimeSt
ate from Address[127.0.0.1]:5701. Ignoring incoming state!
2014-09-05 05:18:33,827 INFO c.h.p.InternalPartitionService [hz._hzInstance_1_dev.migration] [127.0.0.1]:5701 [dev] [3.2.4] Re-partitioning cluster data... Migration queue size: 135
2014-09-05 05:18:34,132 INFO c.h.i.DefaultAddressPicker [main] null [dev] [3.2.4] Interfaces is disabled, trying to pick one address from TCP-IP config addresses: [127.0.0.1]
2014-09-05 05:18:34,133 INFO c.h.i.DefaultAddressPicker [main] null [dev] [3.2.4] Picked Address[127.0.0.1]:5703, using socket ServerSocket[addr=/0.0.0.0,localport=5703], bind any local is true
2014-09-05 05:18:34,207 INFO c.h.system [main] [127.0.0.1]:5703 [dev] [3.2.4] Hazelcast 3.2.4 (20140721) starting at Address[127.0.0.1]:5703
2014-09-05 05:18:34,207 INFO c.h.system [main] [127.0.0.1]:5703 [dev] [3.2.4] Copyright (C) 2008-2014 Hazelcast.com
2014-09-05 05:18:34,207 INFO c.h.i.Node [main] [127.0.0.1]:5703 [dev] [3.2.4] Creating TcpIpJoiner
2014-09-05 05:18:34,208 INFO c.h.c.LifecycleService [main] [127.0.0.1]:5703 [dev] [3.2.4] Address[127.0.0.1]:5703 is STARTING
2014-09-05 05:18:34,228 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5703 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5702

1 Answers1

4

What you need to understand is that each invocation of

HazelcastInstance hz = Hazelcast.newHazelcastInstance ();

Will create exactly that - a new hazelcast node in your JVM (yes, you can have multiple hazelcast nodes inside one JVM), which will have to join the cluster etc. Unless you have a use case for that (for instance this is very useful during unit tests where you can set up your in-memory cluster on a single node! I guess another use case would be if you need to change your configuration, from what I know it still cannot be changed in runtime) you shouldn't call it too often and simply cache it (for instance in that singleton of yours).

@Edit: you are creating multiple instances of HazelcastInstance. You will have indeed only one instance of LazyHolder.INSTANCE but you don't store hcast in HCastAccessor so every time you call HCastAccessor.getAccessorInstance().getHCastInstance() it will create a new object. You need to put hcast inside HCastAccessor and create a new hazelcast instance only if hcast is null, otherwise you return it.

Take care that you either need to synchronize the whole getHCastInstance() method or do a double nullcheck with synchronization (check for null, synchronize only the construction part, inside that synchronize check for null once again before doing the actual creation) or it won't be thread safe. Something like:

if(hcas == null) {
  synchronize(this) {
    if(hcas == null) {
      // create the instance here
    }
  }
}
Mateusz Dymczyk
  • 14,969
  • 10
  • 59
  • 94
  • thanks for sharing - I misquoted or worded it incorrectly. In my singleton implementation, I return the reference to the newHazelcastInstance - what I'm wondering is whether I need that or not. My singleton implementation is probably flawed since I see multiple connections (unless its the xml config file that I need to change) –  Sep 05 '14 at 01:35
  • I agree with the statement that I should use the singleton - i am thinking that perhaps its the config file that is misonfigured. What I see when I run the server is that eventually multiple ports are bound based on the hazelcast log output. –  Sep 05 '14 at 01:37
  • @user3813256 can you add the config, logs where you see your multiple ports being bound to the question? And briefly show how you are creating those hazelcast instances. – Mateusz Dymczyk Sep 05 '14 at 02:00
  • thanks - copying my singleton which I am looking at again as well –  Sep 05 '14 at 02:09
  • I will change the implementation - however, I am calling this only once: HazelcastInstance hcast = hcastAccessor.getHCastInstance(); –  Sep 05 '14 at 09:11
  • I changed my implementation for the singleton to hold the hazelcast instance (I'll try with lazy loaded holder idiom as well). However, my server implementation is not stable - I need to troubleshoot more. thank you for your help –  Sep 05 '14 at 15:40
  • [Double-checked locking is not safe in Java unless the member is marked `volatile` in Java 1.5+.](https://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html) – stiemannkj1 Feb 28 '19 at 15:47