Search in sources :

Example 1 with Scheduler

use of org.mule.runtime.api.scheduler.Scheduler in project mule by mulesoft.

the class ProactorStreamProcessingStrategyTestCase method cpuIntensiveRejectedExecution.

@Test
@Description("If CPU INTENSIVE pool is busy OVERLOAD error is thrown")
public void cpuIntensiveRejectedExecution() throws Exception {
    Scheduler cpuIntensiveSchedulerSpy = spy(cpuIntensive);
    Scheduler rejectingSchedulerSpy = spy(new RejectingScheduler(cpuIntensiveSchedulerSpy));
    flow = flowBuilder.get().processors(cpuIntensiveProcessor).processingStrategyFactory((context, prefix) -> new ProactorStreamProcessingStrategy(() -> ringBuffer, DEFAULT_BUFFER_SIZE, 1, DEFAULT_WAIT_STRATEGY, () -> cpuLight, () -> blocking, () -> rejectingSchedulerSpy, 1, 2)).build();
    flow.initialise();
    flow.start();
    processFlow(testEvent());
    verify(rejectingSchedulerSpy, times(11)).submit(any(Callable.class));
    verify(cpuIntensiveSchedulerSpy, times(1)).submit(any(Callable.class));
    assertThat(threads, hasSize(1));
    assertThat(threads.stream().filter(name -> name.startsWith(CPU_INTENSIVE)).count(), equalTo(1l));
    assertThat(threads, not(hasItem(startsWith(CPU_LIGHT))));
    assertThat(threads, not(hasItem(startsWith(IO))));
    assertThat(threads, not(hasItem(startsWith(CUSTOM))));
}
Also used : ProactorStreamProcessingStrategy(org.mule.runtime.core.internal.processor.strategy.ProactorStreamProcessingStrategyFactory.ProactorStreamProcessingStrategy) Scheduler(org.mule.runtime.api.scheduler.Scheduler) Callable(java.util.concurrent.Callable) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 2 with Scheduler

use of org.mule.runtime.api.scheduler.Scheduler in project mule by mulesoft.

the class ProactorStreamProcessingStrategyTestCase method blockingRejectedExecution.

@Test
@Description("If IO pool is busy OVERLOAD error is thrown")
public void blockingRejectedExecution() throws Exception {
    Scheduler blockingSchedulerSpy = spy(blocking);
    Scheduler rejectingSchedulerSpy = spy(new RejectingScheduler(blockingSchedulerSpy));
    flow = flowBuilder.get().processors(blockingProcessor).processingStrategyFactory((context, prefix) -> new ProactorStreamProcessingStrategy(() -> ringBuffer, DEFAULT_BUFFER_SIZE, 1, DEFAULT_WAIT_STRATEGY, () -> cpuLight, () -> rejectingSchedulerSpy, () -> cpuIntensive, 1, 2)).build();
    flow.initialise();
    flow.start();
    processFlow(testEvent());
    verify(rejectingSchedulerSpy, times(11)).submit(any(Callable.class));
    verify(blockingSchedulerSpy, times(1)).submit(any(Callable.class));
    assertThat(threads, hasSize(1));
    assertThat(threads.stream().filter(name -> name.startsWith(IO)).count(), equalTo(1l));
    assertThat(threads, not(hasItem(startsWith(CPU_LIGHT))));
    assertThat(threads, not(hasItem(startsWith(CPU_INTENSIVE))));
    assertThat(threads, not(hasItem(startsWith(CUSTOM))));
}
Also used : ProactorStreamProcessingStrategy(org.mule.runtime.core.internal.processor.strategy.ProactorStreamProcessingStrategyFactory.ProactorStreamProcessingStrategy) Scheduler(org.mule.runtime.api.scheduler.Scheduler) Callable(java.util.concurrent.Callable) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 3 with Scheduler

use of org.mule.runtime.api.scheduler.Scheduler in project mule by mulesoft.

the class DefaultEventContextTestCase method asyncChild.

@Test
@Description("Parent EventContext only completes once response publisher completes with a value and all child contexts are complete, even when child is run async with a delay.")
public void asyncChild() throws Exception {
    child = addChild(parent);
    CoreEvent event = testEvent();
    Scheduler testScheduler = muleContext.getSchedulerService().ioScheduler();
    Latch latch1 = new Latch();
    try {
        testScheduler.submit(() -> {
            child.success(event);
            latch1.countDown();
            return null;
        });
        assertParent(is(nullValue()), is(nullValue()), false, false);
        parent.success(event);
        latch1.await();
        assertChild(is(event), is(nullValue()), true);
        assertParent(is(event), is(nullValue()), true, true);
    } finally {
        testScheduler.stop();
    }
}
Also used : CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Scheduler(org.mule.runtime.api.scheduler.Scheduler) Latch(org.mule.runtime.api.util.concurrent.Latch) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 4 with Scheduler

use of org.mule.runtime.api.scheduler.Scheduler in project mule by mulesoft.

the class MuleObjectStoreManager method getMonitorablePartition.

@SuppressWarnings({ "rawtypes", "unchecked" })
private <T extends ObjectStore<? extends Serializable>> T getMonitorablePartition(String name, ObjectStore baseStore, T store, ObjectStoreSettings settings) {
    if (baseStore instanceof PartitionableExpirableObjectStore) {
        Scheduler scheduler = schedulerService.customScheduler(muleContext.getSchedulerBaseConfig().withName("ObjectStoreManager-Monitor-" + name).withMaxConcurrentTasks(1));
        scheduler.scheduleWithFixedDelay(new Monitor(name, (PartitionableExpirableObjectStore) baseStore, settings.getEntryTTL().orElse(0L), settings.getMaxEntries().orElse(UNBOUNDED)), 0, settings.getExpirationInterval(), MILLISECONDS);
        expirationSchedulers.put(name, scheduler);
        return store;
    } else {
        MonitoredObjectStoreWrapper monObjectStore;
        // or putting an uninitialised ObjectStore
        synchronized (this) {
            monObjectStore = new MonitoredObjectStoreWrapper(store, settings);
            monObjectStore.setMuleContext(muleContext);
            try {
                monObjectStore.initialise();
            } catch (InitialisationException e) {
                throw new MuleRuntimeException(e);
            }
        }
        return (T) monObjectStore;
    }
}
Also used : Scheduler(org.mule.runtime.api.scheduler.Scheduler) PartitionableExpirableObjectStore(org.mule.runtime.api.store.PartitionableExpirableObjectStore) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException)

Example 5 with Scheduler

use of org.mule.runtime.api.scheduler.Scheduler in project mule by mulesoft.

the class LifecycleAwareConfigurationInstance method testConnectivity.

private void testConnectivity() throws MuleException {
    ConnectionProvider provider = connectionProvider.get();
    if (provider instanceof NoConnectivityTest) {
        return;
    }
    Scheduler retryScheduler = schedulerService.ioScheduler();
    RetryPolicyTemplate retryTemplate = connectionManager.getRetryTemplateFor(provider);
    ReconnectionConfig reconnectionConfig = connectionManager.getReconnectionConfigFor(provider);
    RetryCallback retryCallback = new RetryCallback() {

        @Override
        public void doWork(RetryContext context) throws Exception {
            Lock lock = testConnectivityLock;
            if (lock != null) {
                final boolean lockAcquired = lock.tryLock();
                if (lockAcquired) {
                    LOGGER.info("Doing testConnectivity() for config " + getName());
                    try {
                        ConnectionValidationResult result = connectionManager.testConnectivity(LifecycleAwareConfigurationInstance.this);
                        if (result.isValid()) {
                            context.setOk();
                        } else {
                            if ((reconnectionConfig.isFailsDeployment())) {
                                context.setFailed(result.getException());
                                throw new ConnectionException(format("Connectivity test failed for config '%s'", getName()), result.getException());
                            } else {
                                if (LOGGER.isInfoEnabled()) {
                                    LOGGER.info(format("Connectivity test failed for config '%s'. Application deployment will continue. Error was: ", getName(), result.getMessage()), result.getException());
                                }
                            }
                        }
                    } finally {
                        lock.unlock();
                    }
                } else {
                    LOGGER.warn("There is a testConnectivity() already running for config " + getName());
                }
            }
        }

        @Override
        public String getWorkDescription() {
            return format("Testing connectivity for config '%s'", getName());
        }

        @Override
        public Object getWorkOwner() {
            return value;
        }
    };
    try {
        retryTemplate.execute(retryCallback, retryScheduler);
    } catch (Exception e) {
        throw new DefaultMuleException(createStaticMessage(format("Could not perform connectivity testing for config '%s'", getName())), e);
    } finally {
        if (retryScheduler != null) {
            retryScheduler.stop();
        }
    }
}
Also used : RetryPolicyTemplate(org.mule.runtime.core.api.retry.policy.RetryPolicyTemplate) Scheduler(org.mule.runtime.api.scheduler.Scheduler) RetryContext(org.mule.runtime.core.api.retry.RetryContext) ReconnectionConfig(org.mule.runtime.core.internal.retry.ReconnectionConfig) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MuleException(org.mule.runtime.api.exception.MuleException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) ConnectionException(org.mule.runtime.api.connection.ConnectionException) ConnectionProvider(org.mule.runtime.api.connection.ConnectionProvider) Lock(java.util.concurrent.locks.Lock) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) ConnectionValidationResult(org.mule.runtime.api.connection.ConnectionValidationResult) NoConnectivityTest(org.mule.runtime.extension.api.connectivity.NoConnectivityTest) ConnectionException(org.mule.runtime.api.connection.ConnectionException) RetryCallback(org.mule.runtime.core.api.retry.RetryCallback)

Aggregations

Scheduler (org.mule.runtime.api.scheduler.Scheduler)11 Test (org.junit.Test)6 Description (io.qameta.allure.Description)4 Callable (java.util.concurrent.Callable)3 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)2 SchedulerService (org.mule.runtime.api.scheduler.SchedulerService)2 PartitionableExpirableObjectStore (org.mule.runtime.api.store.PartitionableExpirableObjectStore)2 ProactorStreamProcessingStrategy (org.mule.runtime.core.internal.processor.strategy.ProactorStreamProcessingStrategyFactory.ProactorStreamProcessingStrategy)2 Thread.currentThread (java.lang.Thread.currentThread)1 Thread.sleep (java.lang.Thread.sleep)1 Duration (java.time.Duration)1 ZERO (java.time.Duration.ZERO)1 Duration.ofMillis (java.time.Duration.ofMillis)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Lock (java.util.concurrent.locks.Lock)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1