Search in sources :

Example 1 with NodeContext

use of com.hazelcast.instance.impl.NodeContext in project hazelcast by hazelcast.

the class TestHazelcastInstanceFactory method newHazelcastInstance.

/**
 * Creates a new test Hazelcast instance which is only allowed to connect to specified addresses:
 * <ul>
 * <li>{@code blockedAddresses} are blacklisted in its {@code MockJoiner}</li>
 * <li>connections to {@code blockedAddresses} are blocked by its {@code FirewallingConnectionManager}</li>
 * </ul>
 * This is handy in split-brain tests, when a new instance should be started on a specific network partition
 * of the split brain.
 *
 * @param address          the address to use as Member's address; if {@code null}, then uses the next address
 * @param config           the config to use; use {@code null} to get the default config
 * @param blockedAddresses addresses to which the new instance is allowed to communicate
 */
public HazelcastInstance newHazelcastInstance(Address address, Config config, Address[] blockedAddresses) {
    final String instanceName = config != null ? config.getInstanceName() : null;
    if (isMockNetwork) {
        config = initOrCreateConfig(config);
        Address thisAddress = address != null ? address : nextAddress(config.getNetworkConfig().getPort());
        NodeContext nodeContext = registry.createNodeContext(thisAddress, blockedAddresses == null ? Collections.emptySet() : new HashSet<>(asList(blockedAddresses)));
        HazelcastInstance hazelcastInstance = HazelcastInstanceFactory.newHazelcastInstance(config, instanceName, nodeContext);
        registerTestMetricsPublisher(hazelcastInstance);
        return hazelcastInstance;
    }
    throw new UnsupportedOperationException("Explicit address is only available for mock network setup!");
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Address(com.hazelcast.cluster.Address) Accessors.getAddress(com.hazelcast.test.Accessors.getAddress) DefaultNodeContext(com.hazelcast.instance.impl.DefaultNodeContext) NodeContext(com.hazelcast.instance.impl.NodeContext) HashSet(java.util.HashSet)

Example 2 with NodeContext

use of com.hazelcast.instance.impl.NodeContext in project hazelcast by hazelcast.

the class CustomNodeExtensionTestInstanceFactory method newHazelcastInstance.

@Override
public HazelcastInstance newHazelcastInstance(Config config) {
    String instanceName = config != null ? config.getInstanceName() : null;
    NodeContext nodeContext;
    if (TestEnvironment.isMockNetwork()) {
        config = initOrCreateConfig(config);
        nodeContext = this.registry.createNodeContext(this.nextAddress(config.getNetworkConfig().getPort()));
    } else {
        nodeContext = new DefaultNodeContext();
    }
    return HazelcastInstanceFactory.newHazelcastInstance(config, instanceName, new WanServiceMockingNodeContext(nodeContext, nodeExtensionFn));
}
Also used : NodeContext(com.hazelcast.instance.impl.NodeContext) DefaultNodeContext(com.hazelcast.instance.impl.DefaultNodeContext) DefaultNodeContext(com.hazelcast.instance.impl.DefaultNodeContext)

Example 3 with NodeContext

use of com.hazelcast.instance.impl.NodeContext in project hazelcast by hazelcast.

the class InternalPartitionServiceImplTest method setup.

@Before
public void setup() {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory();
    NodeContext nodeContext = new MockNodeContext(factory.getRegistry(), factory.nextAddress()) {

        @Override
        public NodeExtension createNodeExtension(Node node) {
            return new DefaultNodeExtension(node) {

                @Override
                public boolean isStartCompleted() {
                    return startupDone.get();
                }
            };
        }
    };
    instance = HazelcastInstanceFactory.newHazelcastInstance(new Config(), randomName(), nodeContext);
    partitionService = (InternalPartitionServiceImpl) getPartitionService(instance);
    localMember = getClusterService(instance).getLocalMember();
    partitionCount = partitionService.getPartitionCount();
}
Also used : DefaultNodeExtension(com.hazelcast.instance.impl.DefaultNodeExtension) MockNodeContext(com.hazelcast.test.mocknetwork.MockNodeContext) NodeContext(com.hazelcast.instance.impl.NodeContext) Config(com.hazelcast.config.Config) Node(com.hazelcast.instance.impl.Node) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) MockNodeContext(com.hazelcast.test.mocknetwork.MockNodeContext) Before(org.junit.Before)

Example 4 with NodeContext

use of com.hazelcast.instance.impl.NodeContext in project hazelcast by hazelcast.

the class MembershipUpdateTest method memberJoinsEventually_whenMemberRestartedWithSameUuid_butMasterDoesNotNoticeItsLeave.

@Test
public void memberJoinsEventually_whenMemberRestartedWithSameUuid_butMasterDoesNotNoticeItsLeave() throws Exception {
    ruleStaleJoinPreventionDuration.setOrClearProperty("5");
    Config configMaster = new Config();
    HazelcastInstance hz1 = factory.newHazelcastInstance(configMaster);
    HazelcastInstance hz2 = factory.newHazelcastInstance();
    HazelcastInstance hz3 = factory.newHazelcastInstance();
    assertClusterSizeEventually(3, hz2, hz3);
    MemberImpl member3 = getNode(hz3).getLocalMember();
    // A new member is restarted with member3's UUID.
    // Then after some time, member3 is terminated.
    // This is to emulate the case, member3 is restarted with preserving its UUID (using hot-restart),
    // but master does not realize its leave in time.
    // When master realizes, member3 is terminated,
    // new member should eventually join the cluster.
    Future<HazelcastInstance> future = spawn(() -> {
        NodeContext nodeContext = new StaticMemberNodeContext(factory, member3.getUuid(), factory.nextAddress());
        return newHazelcastInstance(initOrCreateConfig(new Config()), randomName(), nodeContext);
    });
    spawn(() -> {
        sleepSeconds(5);
        hz3.getLifecycleService().terminate();
    });
    HazelcastInstance hz4 = future.get();
    assertClusterSize(3, hz1, hz4);
    assertClusterSizeEventually(3, hz2);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastInstanceFactory.newHazelcastInstance(com.hazelcast.instance.impl.HazelcastInstanceFactory.newHazelcastInstance) StaticMemberNodeContext(com.hazelcast.instance.StaticMemberNodeContext) NodeContext(com.hazelcast.instance.impl.NodeContext) StaticMemberNodeContext(com.hazelcast.instance.StaticMemberNodeContext) TestHazelcastInstanceFactory.initOrCreateConfig(com.hazelcast.test.TestHazelcastInstanceFactory.initOrCreateConfig) Config(com.hazelcast.config.Config) ServiceConfig(com.hazelcast.config.ServiceConfig) MemberImpl(com.hazelcast.cluster.impl.MemberImpl) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with NodeContext

use of com.hazelcast.instance.impl.NodeContext in project hazelcast by hazelcast.

the class TestHazelcastInstanceFactory method newHazelcastInstance.

/**
 * Creates a new test Hazelcast instance.
 *
 * @param config the config to use; use {@code null} to get the default config
 */
public HazelcastInstance newHazelcastInstance(Config config) {
    String instanceName = config != null ? config.getInstanceName() : null;
    if (isMockNetwork) {
        config = initOrCreateConfig(config);
        NodeContext nodeContext = registry.createNodeContext(nextAddress(config.getNetworkConfig().getPort()));
        HazelcastInstance hazelcastInstance = HazelcastInstanceFactory.newHazelcastInstance(config, instanceName, nodeContext);
        registerTestMetricsPublisher(hazelcastInstance);
        return hazelcastInstance;
    }
    HazelcastInstance hazelcastInstance = HazelcastInstanceFactory.newHazelcastInstance(config);
    registerTestMetricsPublisher(hazelcastInstance);
    return hazelcastInstance;
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) DefaultNodeContext(com.hazelcast.instance.impl.DefaultNodeContext) NodeContext(com.hazelcast.instance.impl.NodeContext)

Aggregations

NodeContext (com.hazelcast.instance.impl.NodeContext)6 HazelcastInstance (com.hazelcast.core.HazelcastInstance)4 DefaultNodeContext (com.hazelcast.instance.impl.DefaultNodeContext)4 Config (com.hazelcast.config.Config)2 Address (com.hazelcast.cluster.Address)1 MemberImpl (com.hazelcast.cluster.impl.MemberImpl)1 ServiceConfig (com.hazelcast.config.ServiceConfig)1 StaticMemberNodeContext (com.hazelcast.instance.StaticMemberNodeContext)1 DefaultNodeExtension (com.hazelcast.instance.impl.DefaultNodeExtension)1 HazelcastInstanceFactory.newHazelcastInstance (com.hazelcast.instance.impl.HazelcastInstanceFactory.newHazelcastInstance)1 Node (com.hazelcast.instance.impl.Node)1 Accessors.getAddress (com.hazelcast.test.Accessors.getAddress)1 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)1 TestHazelcastInstanceFactory.initOrCreateConfig (com.hazelcast.test.TestHazelcastInstanceFactory.initOrCreateConfig)1 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1 MockNodeContext (com.hazelcast.test.mocknetwork.MockNodeContext)1 HashSet (java.util.HashSet)1 Before (org.junit.Before)1 Test (org.junit.Test)1