Search in sources :

Example 51 with FutureTask

use of java.util.concurrent.FutureTask in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManagerIntegrationTest method testDependentTaskExecution.

@Test
@SuppressWarnings("unchecked")
public void testDependentTaskExecution() throws Exception {
    helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, false);
    try {
        String resultValue = "dependent-task-initiator";
        // 
        IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
        filter.setOperationState(OperationState.CREATED);
        filter.setTaskType(TestSchedulableTask.class.getCanonicalName());
        filter.setFrom(new DateTime());
        List<IdmLongRunningTaskDto> createdLrts = longRunningTaskService.find(filter, null).getContent();
        Assert.assertEquals(0L, createdLrts.size());
        // 
        Task task = createTask("dependent-task-initiator");
        DependentTaskTrigger trigger = new DependentTaskTrigger();
        trigger.setInitiatorTaskId(task.getId());
        // 
        // initiator = dependent task => circular execution
        manager.createTrigger(task.getId(), trigger);
        manager.runTask(task.getId());
        helper.waitForResult(getContinueFunction());
        createdLrts = longRunningTaskService.find(filter, null).getContent();
        Assert.assertEquals(1L, createdLrts.size());
        // execute first task
        LongRunningFutureTask<String> futureTask = (LongRunningFutureTask<String>) longRunningTaskManager.processCreated(createdLrts.get(0).getId());
        Assert.assertEquals(resultValue, futureTask.getFutureTask().get());
        // 
        helper.waitForResult(getContinueFunction());
        createdLrts = longRunningTaskService.find(filter, null).getContent();
        Assert.assertEquals(1L, longRunningTaskService.find(filter, null).getTotalElements());
        // 
        longRunningTaskManager.cancel(createdLrts.get(0).getId());
        // 
        longRunningTaskService.find(filter, null).getContent();
        // cancel - clean up
        Assert.assertEquals(0L, longRunningTaskService.find(filter, null).getTotalElements());
    } finally {
        helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, true);
    }
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) FutureTask(java.util.concurrent.FutureTask) Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) DependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger) IdmLongRunningTaskFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) DateTime(org.joda.time.DateTime) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 52 with FutureTask

use of java.util.concurrent.FutureTask in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManagerIntegrationTest method testDependentTaskInDryModeExecution.

@Test
@SuppressWarnings("unchecked")
public void testDependentTaskInDryModeExecution() throws Exception {
    helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, false);
    try {
        String resultValue = "dependent-task-initiator-dry-run";
        // 
        IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
        filter.setOperationState(OperationState.CREATED);
        filter.setTaskType(TestSchedulableDryRunTask.class.getCanonicalName());
        filter.setFrom(new DateTime());
        List<IdmLongRunningTaskDto> createdLrts = longRunningTaskService.find(filter, null).getContent();
        Assert.assertEquals(0L, createdLrts.size());
        // 
        Task task = createDryRunTask(resultValue);
        DependentTaskTrigger trigger = new DependentTaskTrigger();
        trigger.setInitiatorTaskId(task.getId());
        // 
        // initiator = dependent task => circular execution
        manager.createTrigger(task.getId(), trigger);
        manager.runTask(task.getId(), true);
        helper.waitForResult(getContinueFunction());
        createdLrts = longRunningTaskService.find(filter, null).getContent();
        Assert.assertEquals(1L, createdLrts.size());
        Assert.assertTrue(createdLrts.get(0).isDryRun());
        UUID firstTaskId = createdLrts.get(0).getId();
        // execute first task
        LongRunningFutureTask<String> futureTask = (LongRunningFutureTask<String>) longRunningTaskManager.processCreated(createdLrts.get(0).getId());
        Assert.assertEquals(resultValue, futureTask.getFutureTask().get());
        // 
        helper.waitForResult(getContinueFunction());
        createdLrts = longRunningTaskService.find(filter, null).getContent();
        Assert.assertEquals(1L, longRunningTaskService.find(filter, null).getTotalElements());
        Assert.assertTrue(createdLrts.get(0).isDryRun());
        Assert.assertNotEquals(firstTaskId, createdLrts.get(0).getId());
        // 
        longRunningTaskManager.cancel(createdLrts.get(0).getId());
        // 
        longRunningTaskService.find(filter, null).getContent();
        // cancel - clean up
        Assert.assertEquals(0L, longRunningTaskService.find(filter, null).getTotalElements());
    } finally {
        helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, true);
    }
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) FutureTask(java.util.concurrent.FutureTask) Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) DependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger) IdmLongRunningTaskFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter) UUID(java.util.UUID) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) DateTime(org.joda.time.DateTime) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 53 with FutureTask

use of java.util.concurrent.FutureTask in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManagerIntegrationTest method testCreateAndRunRoleExpirationTask.

@Test
public void testCreateAndRunRoleExpirationTask() throws Exception {
    helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, false);
    try {
        Task task = createRoleExpirationTask();
        // 
        manager.createTrigger(task.getId(), getSimpleTrigger(task));
        // 
        helper.waitForResult(getContinueFunction());
        // 
        List<FutureTask<?>> taskList = getFutureTaskList(IdentityRoleExpirationTaskExecutor.class);
        assertEquals(Boolean.TRUE, taskList.get(0).get());
        // 
        checkScheduledTask(task);
    } finally {
        helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, true);
    }
}
Also used : FutureTask(java.util.concurrent.FutureTask) Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) FutureTask(java.util.concurrent.FutureTask) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 54 with FutureTask

use of java.util.concurrent.FutureTask in project cxf by apache.

the class JAXRSOverlappingDestinationsTest method testAbsolutePathOneAndTwo.

@Test
public void testAbsolutePathOneAndTwo() throws Exception {
    final String requestURI = "http://localhost:" + PORT + "/one/bookstore/request?delay";
    Callable<String> callable = new Callable<String>() {

        public String call() {
            WebClient wc = WebClient.create(requestURI);
            return wc.accept("text/plain").get(String.class);
        }
    };
    FutureTask<String> task = new FutureTask<String>(callable);
    ExecutorService executor = Executors.newFixedThreadPool(1);
    executor.execute(task);
    Thread.sleep(1000);
    Runnable runnable = new Runnable() {

        public void run() {
            try {
                testAbsolutePathTwo();
            } catch (Exception ex) {
                throw new RuntimeException("Concurrent testAbsolutePathTwo failed");
            }
        }
    };
    new Thread(runnable).start();
    Thread.sleep(2000);
    String path = task.get();
    assertEquals("Absolute RequestURI is wrong", requestURI, path);
}
Also used : FutureTask(java.util.concurrent.FutureTask) ExecutorService(java.util.concurrent.ExecutorService) WebClient(org.apache.cxf.jaxrs.client.WebClient) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 55 with FutureTask

use of java.util.concurrent.FutureTask in project hive by apache.

the class TestLowLevelCacheImpl method testMTTWithCleanup.

@Test
public void testMTTWithCleanup() {
    final LowLevelCacheImpl cache = new LowLevelCacheImpl(LlapDaemonCacheMetrics.create("test", "1"), new DummyCachePolicy(), new DummyAllocator(), true, 1);
    final long fn1 = 1, fn2 = 2;
    final int offsetsToUse = 8;
    final CountDownLatch cdlIn = new CountDownLatch(4), cdlOut = new CountDownLatch(1);
    final AtomicInteger rdmsDone = new AtomicInteger(0);
    Callable<Long> rdmCall = new Callable<Long>() {

        public Long call() {
            int gets = 0, puts = 0;
            try {
                Random rdm = new Random(1234 + Thread.currentThread().getId());
                syncThreadStart(cdlIn, cdlOut);
                for (int i = 0; i < 20000; ++i) {
                    boolean isGet = rdm.nextBoolean(), isFn1 = rdm.nextBoolean();
                    long fileName = isFn1 ? fn1 : fn2;
                    int fileIndex = isFn1 ? 1 : 2;
                    int count = rdm.nextInt(offsetsToUse);
                    if (isGet) {
                        int[] offsets = new int[count];
                        count = generateOffsets(offsetsToUse, rdm, offsets);
                        CreateHelper list = new CreateHelper();
                        for (int j = 0; i < count; ++i) {
                            list.addOrMerge(offsets[j], offsets[j] + 1, true, false);
                        }
                        DiskRangeList iter = cache.getFileData(fileName, list.get(), 0, testFactory, null, null);
                        int j = -1;
                        while (iter != null) {
                            ++j;
                            if (!(iter instanceof CacheChunk)) {
                                iter = iter.next;
                                continue;
                            }
                            ++gets;
                            LlapAllocatorBuffer result = (LlapAllocatorBuffer) ((CacheChunk) iter).getBuffer();
                            assertEquals(makeFakeArenaIndex(fileIndex, offsets[j]), result.getArenaIndex());
                            cache.decRefBuffer(result);
                            iter = iter.next;
                        }
                    } else {
                        DiskRange[] ranges = new DiskRange[count];
                        int[] offsets = new int[count];
                        for (int j = 0; j < count; ++j) {
                            int next = rdm.nextInt(offsetsToUse);
                            ranges[j] = dr(next, next + 1);
                            offsets[j] = next;
                        }
                        MemoryBuffer[] buffers = new MemoryBuffer[count];
                        for (int j = 0; j < offsets.length; ++j) {
                            LlapDataBuffer buf = LowLevelCacheImpl.allocateFake();
                            buf.setNewAllocLocation(makeFakeArenaIndex(fileIndex, offsets[j]), 0);
                            buffers[j] = buf;
                        }
                        long[] mask = cache.putFileData(fileName, ranges, buffers, 0, Priority.NORMAL, null, null);
                        puts += buffers.length;
                        long maskVal = 0;
                        if (mask != null) {
                            assertEquals(1, mask.length);
                            maskVal = mask[0];
                        }
                        for (int j = 0; j < offsets.length; ++j) {
                            LlapDataBuffer buf = (LlapDataBuffer) (buffers[j]);
                            if ((maskVal & 1) == 1) {
                                assertEquals(makeFakeArenaIndex(fileIndex, offsets[j]), buf.getArenaIndex());
                            }
                            maskVal >>= 1;
                            cache.decRefBuffer(buf);
                        }
                    }
                }
            } finally {
                rdmsDone.incrementAndGet();
            }
            return (((long) gets) << 32) | puts;
        }

        private int makeFakeArenaIndex(int fileIndex, long offset) {
            return (int) ((fileIndex << 12) + offset);
        }
    };
    FutureTask<Integer> evictionTask = new FutureTask<Integer>(new Callable<Integer>() {

        public Integer call() {
            boolean isFirstFile = false;
            Random rdm = new Random(1234 + Thread.currentThread().getId());
            int evictions = 0;
            syncThreadStart(cdlIn, cdlOut);
            while (rdmsDone.get() < 3) {
                DiskRangeList head = new DiskRangeList(0, offsetsToUse + 1);
                isFirstFile = !isFirstFile;
                long fileId = isFirstFile ? fn1 : fn2;
                head = cache.getFileData(fileId, head, 0, testFactory, null, null);
                DiskRange[] results = head.listToArray();
                int startIndex = rdm.nextInt(results.length), index = startIndex;
                LlapDataBuffer victim = null;
                do {
                    DiskRange r = results[index];
                    if (r instanceof CacheChunk) {
                        LlapDataBuffer result = (LlapDataBuffer) ((CacheChunk) r).getBuffer();
                        cache.decRefBuffer(result);
                        if (victim == null && result.invalidate() == LlapCacheableBuffer.INVALIDATE_OK) {
                            ++evictions;
                            victim = result;
                        }
                    }
                    ++index;
                    if (index == results.length)
                        index = 0;
                } while (index != startIndex);
                if (victim == null)
                    continue;
                cache.notifyEvicted(victim);
            }
            return evictions;
        }
    });
    FutureTask<Long> rdmTask1 = new FutureTask<Long>(rdmCall), rdmTask2 = new FutureTask<Long>(rdmCall), rdmTask3 = new FutureTask<Long>(rdmCall);
    Executor threadPool = Executors.newFixedThreadPool(4);
    threadPool.execute(rdmTask1);
    threadPool.execute(rdmTask2);
    threadPool.execute(rdmTask3);
    threadPool.execute(evictionTask);
    try {
        cdlIn.await();
        cdlOut.countDown();
        long result1 = rdmTask1.get(), result2 = rdmTask2.get(), result3 = rdmTask3.get();
        int evictions = evictionTask.get();
        LOG.info("MTT test: task 1: " + descRdmTask(result1) + ", task 2: " + descRdmTask(result2) + ", task 3: " + descRdmTask(result3) + "; " + evictions + " evictions");
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}
Also used : DiskRangeList(org.apache.hadoop.hive.common.io.DiskRangeList) Callable(java.util.concurrent.Callable) CreateHelper(org.apache.hadoop.hive.common.io.DiskRangeList.CreateHelper) Executor(java.util.concurrent.Executor) Random(java.util.Random) FutureTask(java.util.concurrent.FutureTask) CacheChunk(org.apache.hadoop.hive.ql.io.orc.encoded.CacheChunk) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MemoryBuffer(org.apache.hadoop.hive.common.io.encoded.MemoryBuffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DiskRange(org.apache.hadoop.hive.common.io.DiskRange) Test(org.junit.Test)

Aggregations

FutureTask (java.util.concurrent.FutureTask)126 ExecutionException (java.util.concurrent.ExecutionException)44 Test (org.junit.Test)32 IOException (java.io.IOException)28 ExecutorService (java.util.concurrent.ExecutorService)23 Callable (java.util.concurrent.Callable)22 CountDownLatch (java.util.concurrent.CountDownLatch)20 TimeoutException (java.util.concurrent.TimeoutException)14 Handler (android.os.Handler)12 ArrayList (java.util.ArrayList)12 CancellationException (java.util.concurrent.CancellationException)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 Future (java.util.concurrent.Future)8 AccessibleObject (java.lang.reflect.AccessibleObject)6 List (java.util.List)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 FileNotFoundException (java.io.FileNotFoundException)5 InputStream (java.io.InputStream)5 Iterator (java.util.Iterator)5 CancellationSignal (android.os.CancellationSignal)4