I am trying to write unit tests for my Hazelcast cluster - According to this doc https://hazelcast.zendesk.com/hc/en-us/articles/115004425243-How-do-I-test-my-Hazelcast-cluster-
i wrote my code in this way -
public void test(){
HazelcastInstance instance = Hazelcast.newHazelcastInstance();
HazelcastInstance instance2 = Hazelcast.newHazelcastInstance();
}
My console shows -
Members [1] {
Member [MY IP]:5701 - this
}
and
Members [1] {
Member [MY IP]:5702 - this
}
The port number changes, IP stays the same but they are not in the same cluster. What do I do to get them to be a part of the same cluster (on a single JVM)? UPDATE
More console data -
Jul 31, 2018 3:21:00 PM com.hazelcast.config.XmlConfigLocator
INFO: Loading 'hazelcast-default.xml' from classpath.
Jul 31, 2018 3:21:01 PM com.hazelcast.instance.DefaultAddressPicker
INFO: [LOCAL] [dev] [3.7.4] Prefer IPv4 stack is true.
Jul 31, 2018 3:21:01 PM com.hazelcast.instance.DefaultAddressPicker
INFO: [LOCAL] [dev] [3.7.4] Picked [MY_IP]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true
Jul 31, 2018 3:21:01 PM com.hazelcast.system
INFO: [MY_IP]:5701 [dev] [3.7.4] Hazelcast 3.7.4 (20161209 - 3df1bb5) starting at [MY_IP]:5701
Jul 31, 2018 3:21:01 PM com.hazelcast.system
INFO: [MY_IP]:5701 [dev] [3.7.4] Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
Jul 31, 2018 3:21:01 PM com.hazelcast.system
INFO: [MY_IP]:5701 [dev] [3.7.4] Configured Hazelcast Serialization version : 1
Jul 31, 2018 3:21:01 PM com.hazelcast.spi.impl.operationservice.impl.BackpressureRegulator
INFO: [MY_IP]:5701 [dev] [3.7.4] Backpressure is disabled
Jul 31, 2018 3:21:01 PM com.hazelcast.instance.Node
INFO: [MY_IP]:5701 [dev] [3.7.4] Creating MulticastJoiner
Jul 31, 2018 3:21:01 PM com.hazelcast.core.LifecycleService
INFO: [MY_IP]:5701 [dev] [3.7.4] [MY_IP]:5701 is STARTING
Jul 31, 2018 3:21:02 PM com.hazelcast.spi.impl.operationexecutor.impl.OperationExecutorImpl
INFO: [MY_IP]:5701 [dev] [3.7.4] Starting 4 partition threads
Jul 31, 2018 3:21:02 PM com.hazelcast.spi.impl.operationexecutor.impl.OperationExecutorImpl
INFO: [MY_IP]:5701 [dev] [3.7.4] Starting 3 generic threads (1 dedicated for priority tasks)
Jul 31, 2018 3:21:02 PM com.hazelcast.nio.tcp.nonblocking.NonBlockingIOThreadingModel
INFO: [MY_IP]:5701 [dev] [3.7.4] TcpIpConnectionManager configured with Non Blocking IO- threading model: 3 input threads and 3 output threads
Jul 31, 2018 3:21:05 PM com.hazelcast.internal.cluster.impl.MulticastJoiner
INFO: [MY_IP]:5701 [dev] [3.7.4]
Members [1] {
Member [MY_IP]:5701 -
this
}
Jul 31, 2018 3:21:05 PM com.hazelcast.core.LifecycleService
INFO: [MY_IP]:5701 [dev] [3.7.4] [MY_IP]:5701 is
STARTED
Jul 31, 2018 3:21:05 PM com.hazelcast.config.XmlConfigLocator
INFO: Loading 'hazelcast-default.xml' from classpath.
Jul 31, 2018 3:21:05 PM com.hazelcast.instance.DefaultAddressPicker
INFO: [LOCAL] [dev] [3.7.4] Prefer IPv4 stack is true.
Jul 31, 2018 3:21:05 PM com.hazelcast.instance.DefaultAddressPicker
INFO: [LOCAL] [dev] [3.7.4] Picked [MY_IP]:5702, using socket
ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5702], bind any local is
true
Jul 31, 2018 3:21:05 PM com.hazelcast.system
INFO: [MY_IP]:5702 [dev] [3.7.4] Hazelcast 3.7.4 (20161209 -
3df1bb5) starting at [MY_IP]:5702
Jul 31, 2018 3:21:05 PM com.hazelcast.system
INFO: [MY_IP]:5702 [dev] [3.7.4] Copyright (c) 2008-2016,
Hazelcast, Inc. All Rights Reserved.
Jul 31, 2018 3:21:05 PM com.hazelcast.system
INFO: [MY_IP]:5702 [dev] [3.7.4] Configured Hazelcast
Serialization version : 1
Jul 31, 2018 3:21:05 PM
com.hazelcast.spi.impl.operationservice.impl.BackpressureRegulator
INFO: [MY_IP]:5702 [dev] [3.7.4] Backpressure is disabled
Jul 31, 2018 3:21:05 PM com.hazelcast.instance.Node
INFO: [MY_IP]:5702 [dev] [3.7.4] Creating MulticastJoiner
Jul 31, 2018 3:21:05 PM com.hazelcast.core.LifecycleService
INFO: [MY_IP]:5702 [dev] [3.7.4] [MY_IP]:5702 is
Jul 31, 2018 3:21:05 PM
com.hazelcast.spi.impl.operationexecutor.impl.OperationExecutorImpl
INFO: [MY_IP]:5702 [dev] [3.7.4] Starting 4 partition threads
Jul 31, 2018 3:21:05 PM
com.hazelcast.spi.impl.operationexecutor.impl.OperationExecutorImpl
INFO: [MY_IP]:5702 [dev] [3.7.4] Starting 3 generic threads (1
dedicated for priority tasks)
Jul 31, 2018 3:21:05 PM
com.hazelcast.nio.tcp.nonblocking.NonBlockingIOThreadingModel
INFO: [MY_IP]:5702 [dev] [3.7.4] TcpIpConnectionManager
configured with Non Blocking IO-threading model: 3 input threads and 3
output threads
Jul 31, 2018 3:21:08 PM
com.hazelcast.internal.cluster.impl.MulticastJoiner
INFO: [MY_IP]:5702 [dev] [3.7.4]
Members [1] {
Member [MY_IP]:5702 -
this
}
Jul 31, 2018 3:21:08 PM com.hazelcast.instance.Node
WARNING: [MY_IP]:5702 [dev] [3.7.4] Config seed port is 5701
and cluster size is 1. Some of the ports seem occupied!
Jul 31, 2018 3:21:08 PM com.hazelcast.core.LifecycleService
INFO: [MY_IP]:5702 [dev] [3.7.4] [MY_IP]:5702 is
STARTED
As you noted, you're using the default Hazelcast configuration, as shown in the logs
INFO: Loading 'hazelcast-default.xml' from classpath.
The default Hazelcast configuration is for multicast. This is ideal for getting started quickly with multiple machines, not so good for one machine.
The first Hazelcast.newHazelcastInstance()
will start a multicast group on port 5701.
The second Hazelcast.newHazelcastInstance()
will find port 5701 in use, will take the next port 5702, and start a multicast group on 5702.
In other words, the second instance can be on the same port so can't be in the same group (as the group is based on the port). So you end up with two clusters which isn't what you want.
There are four steps to achieve what you want.
hazelcast.xml
file in your test classpath. Turning off multicast does not turn on TCP. You could chose to have no discovery (useful for some tests).
So you need a hazelcast.xml
that looks like this perhaps:
<?xml version="1.0" encoding="UTF-8"?>
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.10.xsd"
xmlns="http://www.hazelcast.com/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<network>
<join>
<multicast enabled="false"/>
<tcp-ip enabled="true">
<interface>127.0.0.1</interface>
</tcp-ip>
</join>
</network>
</hazelcast>