Search in sources :

Example 1 with IAtomicLong

use of com.hazelcast.cp.IAtomicLong in project hazelcast by hazelcast.

the class EntryProcessorOffloadableTest method testHeartBeatsComingWhenEntryProcessorOffloaded.

/**
 * <pre>
 * Given: Operation heartbeats are sent four times per {@link ClusterProperty#OPERATION_CALL_TIMEOUT_MILLIS}
 *        (see {@link InvocationMonitor#getHeartbeatBroadcastPeriodMillis()})
 * When: An offloaded EntryProcessor takes a long time to run.
 * Then: Heartbeats are still coming while the task is offloaded.
 * </pre>
 */
@Test
@Category(SlowTest.class)
public void testHeartBeatsComingWhenEntryProcessorOffloaded() {
    /* Shut down the cluster since we want to use a different
         * OPERATION_CALL_TIMEOUT_MILLIS value in this test. */
    shutdownNodeFactory();
    int heartbeatsIntervalSec = 15;
    Config config = getConfig();
    config.getProperties().setProperty(ClusterProperty.OPERATION_CALL_TIMEOUT_MILLIS.getName(), String.valueOf(heartbeatsIntervalSec * 4 * 1000));
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    instances = factory.newInstances(config);
    final String key = generateKeyOwnedBy(instances[1]);
    String offloadableStartTimeRefName = "offloadableStartTimeRefName";
    String exitLatchName = "exitLatchName";
    IAtomicLong offloadableStartedTimeRef = instances[0].getCPSubsystem().getAtomicLong(offloadableStartTimeRefName);
    ICountDownLatch exitLatch = instances[0].getCPSubsystem().getCountDownLatch(exitLatchName);
    exitLatch.trySetCount(1);
    TimestampedSimpleValue givenValue = new TimestampedSimpleValue(1);
    final IMap<String, TimestampedSimpleValue> map = instances[0].getMap(MAP_NAME);
    map.put(key, givenValue);
    final Address instance1Address = instances[1].getCluster().getLocalMember().getAddress();
    final List<Long> heartBeatTimestamps = new LinkedList<>();
    Thread hbMonitorThread = new Thread(() -> {
        NodeEngine nodeEngine = getNodeEngineImpl(instances[0]);
        OperationServiceImpl osImpl = (OperationServiceImpl) nodeEngine.getOperationService();
        Map<Address, AtomicLong> heartBeats = osImpl.getInvocationMonitor().getHeartbeatPerMember();
        long lastbeat = Long.MIN_VALUE;
        while (!Thread.currentThread().isInterrupted()) {
            AtomicLong timestamp = heartBeats.get(instance1Address);
            if (timestamp != null) {
                long newlastbeat = timestamp.get();
                if (lastbeat != newlastbeat) {
                    lastbeat = newlastbeat;
                    long offloadableStartTime = offloadableStartedTimeRef.get();
                    if (offloadableStartTime != 0 && offloadableStartTime < newlastbeat) {
                        heartBeatTimestamps.add(newlastbeat);
                        exitLatch.countDown();
                    }
                }
            }
            HazelcastTestSupport.sleepMillis(100);
        }
    });
    final int secondsToRun = 55;
    try {
        hbMonitorThread.start();
        map.executeOnKey(key, new TimeConsumingOffloadableTask(secondsToRun, offloadableStartTimeRefName, exitLatchName));
    } finally {
        hbMonitorThread.interrupt();
    }
    int heartBeatCount = 0;
    TimestampedSimpleValue updatedValue = map.get(key);
    for (long heartBeatTimestamp : heartBeatTimestamps) {
        if (heartBeatTimestamp > updatedValue.processStart && heartBeatTimestamp < updatedValue.processEnd) {
            heartBeatCount++;
        }
    }
    assertTrue("Heartbeats should be received while offloadable entry processor is running. " + "Observed: " + heartBeatTimestamps + " EP start: " + updatedValue.processStart + " end: " + updatedValue.processEnd, heartBeatCount > 0);
}
Also used : Address(com.hazelcast.cluster.Address) MapConfig(com.hazelcast.config.MapConfig) Config(com.hazelcast.config.Config) ICountDownLatch(com.hazelcast.cp.ICountDownLatch) LinkedList(java.util.LinkedList) NodeEngine(com.hazelcast.spi.impl.NodeEngine) IAtomicLong(com.hazelcast.cp.IAtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) IAtomicLong(com.hazelcast.cp.IAtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) IAtomicLong(com.hazelcast.cp.IAtomicLong) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) OperationServiceImpl(com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl) Category(org.junit.experimental.categories.Category) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 2 with IAtomicLong

use of com.hazelcast.cp.IAtomicLong in project hazelcast by hazelcast.

the class ExecutorServiceTest method testSubmitToMembersCallable.

@Test
public void testSubmitToMembersCallable() {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(NODE_COUNT);
    HazelcastInstance[] instances = factory.newInstances(smallInstanceConfig());
    final AtomicInteger count = new AtomicInteger(0);
    final CountDownLatch latch = new CountDownLatch(NODE_COUNT);
    MultiExecutionCallback callback = new MultiExecutionCallback() {

        public void onResponse(Member member, Object value) {
            count.incrementAndGet();
        }

        public void onComplete(Map<Member, Object> values) {
            latch.countDown();
        }
    };
    int sum = 0;
    Set<Member> membersSet = instances[0].getCluster().getMembers();
    Member[] members = membersSet.toArray(new Member[0]);
    Random random = new Random();
    String name = "testSubmitToMembersCallable";
    for (int i = 0; i < NODE_COUNT; i++) {
        IExecutorService service = instances[i].getExecutorService(name);
        int n = random.nextInt(NODE_COUNT) + 1;
        sum += n;
        Member[] m = new Member[n];
        System.arraycopy(members, 0, m, 0, n);
        service.submitToMembers(new IncrementAtomicLongCallable(name), Arrays.asList(m), callback);
    }
    assertOpenEventually(latch);
    IAtomicLong result = instances[0].getCPSubsystem().getAtomicLong(name);
    assertEquals(sum, result.get());
    assertEquals(sum, count.get());
}
Also used : IExecutorService(com.hazelcast.core.IExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) MultiExecutionCallback(com.hazelcast.core.MultiExecutionCallback) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IAtomicLong(com.hazelcast.cp.IAtomicLong) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Member(com.hazelcast.cluster.Member) Map(java.util.Map) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with IAtomicLong

use of com.hazelcast.cp.IAtomicLong in project hazelcast by hazelcast.

the class ExecutorServiceTest method testSubmitToAllMembersRunnable.

@Test
public void testSubmitToAllMembersRunnable() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(NODE_COUNT);
    HazelcastInstance[] instances = factory.newInstances(smallInstanceConfig());
    final AtomicInteger nullResponseCount = new AtomicInteger(0);
    final CountDownLatch responseLatch = new CountDownLatch(NODE_COUNT * NODE_COUNT);
    MultiExecutionCallback callback = new MultiExecutionCallback() {

        public void onResponse(Member member, Object value) {
            if (value == null) {
                nullResponseCount.incrementAndGet();
            }
            responseLatch.countDown();
        }

        public void onComplete(Map<Member, Object> values) {
        }
    };
    for (int i = 0; i < NODE_COUNT; i++) {
        IExecutorService service = instances[i].getExecutorService("testSubmitToAllMembersRunnable");
        service.submitToAllMembers(new IncrementAtomicLongRunnable("testSubmitToAllMembersRunnable"), callback);
    }
    assertTrue(responseLatch.await(30, TimeUnit.SECONDS));
    IAtomicLong result = instances[0].getCPSubsystem().getAtomicLong("testSubmitToAllMembersRunnable");
    assertEquals(NODE_COUNT * NODE_COUNT, result.get());
    assertEquals(NODE_COUNT * NODE_COUNT, nullResponseCount.get());
}
Also used : IExecutorService(com.hazelcast.core.IExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) MultiExecutionCallback(com.hazelcast.core.MultiExecutionCallback) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IAtomicLong(com.hazelcast.cp.IAtomicLong) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Member(com.hazelcast.cluster.Member) Map(java.util.Map) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with IAtomicLong

use of com.hazelcast.cp.IAtomicLong in project hazelcast by hazelcast.

the class ExecutorServiceTest method testExecuteMultipleNode.

@Test
public void testExecuteMultipleNode() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(NODE_COUNT);
    HazelcastInstance[] instances = factory.newInstances(smallInstanceConfig());
    for (int i = 0; i < NODE_COUNT; i++) {
        IExecutorService service = instances[i].getExecutorService("testExecuteMultipleNode");
        int rand = new Random().nextInt(100);
        Future<Integer> future = service.submit(new IncrementAtomicLongRunnable("count"), rand);
        assertEquals(Integer.valueOf(rand), future.get(10, TimeUnit.SECONDS));
    }
    IAtomicLong count = instances[0].getCPSubsystem().getAtomicLong("count");
    assertEquals(NODE_COUNT, count.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Random(java.util.Random) IExecutorService(com.hazelcast.core.IExecutorService) IAtomicLong(com.hazelcast.cp.IAtomicLong) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with IAtomicLong

use of com.hazelcast.cp.IAtomicLong in project hazelcast by hazelcast.

the class SmallClusterTest method submitToSeveralNodes_runnable.

@Test
public void submitToSeveralNodes_runnable() throws Exception {
    for (HazelcastInstance instance : instances) {
        IExecutorService service = instance.getExecutorService("testExecuteMultipleNode");
        int rand = new Random().nextInt(100);
        Future<Integer> future = service.submit(new IncrementAtomicLongRunnable("count"), rand);
        assertEquals(Integer.valueOf(rand), future.get());
    }
    IAtomicLong count = instances[0].getCPSubsystem().getAtomicLong("count");
    assertEquals(instances.length, count.get());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Random(java.util.Random) IExecutorService(com.hazelcast.core.IExecutorService) IAtomicLong(com.hazelcast.cp.IAtomicLong) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

IAtomicLong (com.hazelcast.cp.IAtomicLong)22 Test (org.junit.Test)20 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)19 HazelcastInstance (com.hazelcast.core.HazelcastInstance)18 QuickTest (com.hazelcast.test.annotation.QuickTest)17 IExecutorService (com.hazelcast.core.IExecutorService)10 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)8 Random (java.util.Random)8 Member (com.hazelcast.cluster.Member)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 SlowTest (com.hazelcast.test.annotation.SlowTest)4 MultiExecutionCallback (com.hazelcast.core.MultiExecutionCallback)3 ICountDownLatch (com.hazelcast.cp.ICountDownLatch)3 Map (java.util.Map)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Config (com.hazelcast.config.Config)2 IScheduledExecutorService (com.hazelcast.scheduledexecutor.IScheduledExecutorService)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Address (com.hazelcast.cluster.Address)1 MapConfig (com.hazelcast.config.MapConfig)1