Search in sources :

Example 1 with CancellationAwareTask

use of com.hazelcast.client.test.executor.tasks.CancellationAwareTask in project hazelcast by hazelcast.

the class ClientExecutorServiceCancelTest method testCancel_submitToKeyOwner.

private void testCancel_submitToKeyOwner(boolean smartRouting, boolean waitTaskStart) throws ExecutionException, InterruptedException, IOException {
    HazelcastInstance client = createClient(smartRouting);
    IExecutorService executorService = client.getExecutorService(randomString());
    String key = generateKeyOwnedBy(server1);
    Future<Boolean> future = executorService.submitToKeyOwner(new CancellationAwareTask(SLEEP_TIME), key);
    if (waitTaskStart) {
        awaitTaskStartAtMember(server1, 1);
    }
    boolean cancelled = future.cancel(true);
    assertTrue(cancelled);
    awaitTaskCancelAtMember(server1, 1);
    future.get();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) IExecutorService(com.hazelcast.core.IExecutorService) CancellationAwareTask(com.hazelcast.client.test.executor.tasks.CancellationAwareTask)

Example 2 with CancellationAwareTask

use of com.hazelcast.client.test.executor.tasks.CancellationAwareTask in project hazelcast by hazelcast.

the class ClientExecutorServiceCancelTest method testCancel_submitToKeyOwner_Should_Not_Block_Migration.

@Test(expected = CancellationException.class)
public void testCancel_submitToKeyOwner_Should_Not_Block_Migration() throws IOException, ExecutionException, InterruptedException {
    server2.shutdown();
    HazelcastInstance client = createClient(true);
    warmUpPartitions(server1);
    IExecutorService executorService = client.getExecutorService(randomString());
    String key = ExecutorServiceTestSupport.generateKeyOwnedBy(server1);
    final Future<Boolean> future = executorService.submitToKeyOwner(new CancellationAwareTask(SLEEP_TIME), key);
    awaitTaskStartAtMember(server1, 1);
    InternalPartitionServiceImpl internalPartitionService = (InternalPartitionServiceImpl) TestUtil.getNode(server1).getPartitionService();
    final int partitionId = internalPartitionService.getPartitionId(key);
    // Simulate partition thread blockage as if the partition is migrating
    internalPartitionService.setMigrationInterceptor(new MigrationInterceptor() {

        @Override
        public void onMigrationCommit(MigrationInterceptor.MigrationParticipant participant, MigrationInfo migrationInfo) {
            int migratingPartitionId = migrationInfo.getPartitionId();
            if (migratingPartitionId == partitionId) {
                spawn(() -> {
                    future.cancel(true);
                });
                // sleep enough so that the ExecutorServiceCancelOnPartitionMessageTask actually starts
                // This test is time sensitive
                sleepSeconds(3);
            }
        }
    });
    // Start the second member to initiate migration
    server2 = hazelcastFactory.newHazelcastInstance();
    waitAllForSafeState(server1, server2);
    future.get();
}
Also used : MigrationInfo(com.hazelcast.internal.partition.MigrationInfo) HazelcastInstance(com.hazelcast.core.HazelcastInstance) InternalPartitionServiceImpl(com.hazelcast.internal.partition.impl.InternalPartitionServiceImpl) IExecutorService(com.hazelcast.core.IExecutorService) MigrationInterceptor(com.hazelcast.internal.partition.impl.MigrationInterceptor) CancellationAwareTask(com.hazelcast.client.test.executor.tasks.CancellationAwareTask) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with CancellationAwareTask

use of com.hazelcast.client.test.executor.tasks.CancellationAwareTask in project hazelcast by hazelcast.

the class ClientExecutorServiceCancelTest method testCancel_submitToKeyOwner_Should_Be_Retried_While_Migrating.

@Test(expected = CancellationException.class)
public void testCancel_submitToKeyOwner_Should_Be_Retried_While_Migrating() throws IOException, ExecutionException, InterruptedException {
    HazelcastInstance client = createClient(true);
    IExecutorService executorService = client.getExecutorService(randomString());
    String key = ExecutorServiceTestSupport.generateKeyOwnedBy(server1);
    Future<Boolean> future = executorService.submitToKeyOwner(new CancellationAwareTask(SLEEP_TIME), key);
    awaitTaskStartAtMember(server1, 1);
    final InternalPartitionServiceImpl internalPartitionService = (InternalPartitionServiceImpl) TestUtil.getNode(server1).getPartitionService();
    final int partitionId = internalPartitionService.getPartitionId(key);
    // Simulate partition thread blockage as if the partition is migrating
    internalPartitionService.getPartitionStateManager().trySetMigratingFlag(partitionId);
    spawn(() -> {
        sleepSeconds(2);
        // Simulate migration completion
        internalPartitionService.getPartitionStateManager().clearMigratingFlag(partitionId);
    });
    // The cancel operation should not be blocked due to the blocked partition thread
    future.cancel(true);
    future.get();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) InternalPartitionServiceImpl(com.hazelcast.internal.partition.impl.InternalPartitionServiceImpl) IExecutorService(com.hazelcast.core.IExecutorService) CancellationAwareTask(com.hazelcast.client.test.executor.tasks.CancellationAwareTask) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with CancellationAwareTask

use of com.hazelcast.client.test.executor.tasks.CancellationAwareTask in project hazelcast by hazelcast.

the class ClientExecutorServiceTest method testCancellationAwareTask_whenTimeOut.

@Test(expected = TimeoutException.class)
public void testCancellationAwareTask_whenTimeOut() throws InterruptedException, ExecutionException, TimeoutException {
    IExecutorService service = client.getExecutorService(randomString());
    CancellationAwareTask task = new CancellationAwareTask(Long.MAX_VALUE);
    Future future = service.submit(task);
    future.get(1, TimeUnit.SECONDS);
}
Also used : Future(java.util.concurrent.Future) IExecutorService(com.hazelcast.core.IExecutorService) CancellationAwareTask(com.hazelcast.client.test.executor.tasks.CancellationAwareTask) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with CancellationAwareTask

use of com.hazelcast.client.test.executor.tasks.CancellationAwareTask in project hazelcast by hazelcast.

the class ClientExecutorServiceTest method testGetFutureAfterCancel.

@Test(expected = CancellationException.class)
public void testGetFutureAfterCancel() throws InterruptedException, ExecutionException {
    IExecutorService service = client.getExecutorService(randomString());
    CancellationAwareTask task = new CancellationAwareTask(Long.MAX_VALUE);
    Future future = service.submit(task);
    try {
        future.get(1, TimeUnit.SECONDS);
    } catch (TimeoutException ignored) {
    }
    future.cancel(true);
    future.get();
}
Also used : Future(java.util.concurrent.Future) IExecutorService(com.hazelcast.core.IExecutorService) CancellationAwareTask(com.hazelcast.client.test.executor.tasks.CancellationAwareTask) TimeoutException(java.util.concurrent.TimeoutException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

CancellationAwareTask (com.hazelcast.client.test.executor.tasks.CancellationAwareTask)9 IExecutorService (com.hazelcast.core.IExecutorService)9 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)6 QuickTest (com.hazelcast.test.annotation.QuickTest)6 Test (org.junit.Test)6 HazelcastInstance (com.hazelcast.core.HazelcastInstance)5 Future (java.util.concurrent.Future)4 TimeoutException (java.util.concurrent.TimeoutException)3 InternalPartitionServiceImpl (com.hazelcast.internal.partition.impl.InternalPartitionServiceImpl)2 MigrationInfo (com.hazelcast.internal.partition.MigrationInfo)1 MigrationInterceptor (com.hazelcast.internal.partition.impl.MigrationInterceptor)1 Ignore (org.junit.Ignore)1