Search in sources :

Example 71 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project failsafe by jhalterman.

the class Issue52 method shouldCancelExecutionViaCompletableFuture.

public void shouldCancelExecutionViaCompletableFuture() throws Throwable {
    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
    AtomicInteger counter = new AtomicInteger();
    CompletableFuture<String> proxyFuture = Failsafe.with(new RetryPolicy().withDelay(10, TimeUnit.MILLISECONDS)).with(scheduler).future(exec -> {
        counter.incrementAndGet();
        CompletableFuture<String> result = new CompletableFuture<>();
        result.completeExceptionally(new RuntimeException());
        return result;
    });
    Thread.sleep(100);
    proxyFuture.cancel(true);
    int count = counter.get();
    assertTrue(proxyFuture.isCancelled());
    Asserts.assertThrows(() -> proxyFuture.get(), CancellationException.class);
    // Assert that execution has actually stopped
    Thread.sleep(20);
    assertEquals(count, counter.get());
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 72 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project failsafe by jhalterman.

the class Issue75 method testThatFailSafeIsBrokenWithFallback.

@Test
public void testThatFailSafeIsBrokenWithFallback() throws Exception {
    CircuitBreaker breaker = new CircuitBreaker().withFailureThreshold(10, 100).withSuccessThreshold(2).withDelay(100, TimeUnit.MILLISECONDS);
    ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
    int result = Failsafe.with(breaker).with(service).withFallback((a, b) -> 999).future(() -> CompletableFuture.completedFuture(223)).get();
    Assert.assertEquals(result, 223);
}
Also used : CircuitBreaker(net.jodah.failsafe.CircuitBreaker) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Test(org.testng.annotations.Test)

Example 73 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project failsafe by jhalterman.

the class Issue9 method test.

public void test() throws Throwable {
    // Given - Fail twice then succeed
    AtomicInteger retryCounter = new AtomicInteger();
    Service service = mock(Service.class);
    when(service.connect()).thenThrow(failures(2, new IllegalStateException())).thenReturn(true);
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    Listeners<Boolean> listeners = new Listeners<Boolean>() {

        public void onRetry(Boolean result, Throwable failure) {
            retryCounter.incrementAndGet();
        }
    };
    Waiter waiter = new Waiter();
    // When
    AtomicInteger successCounter = new AtomicInteger();
    FailsafeFuture<Boolean> future = Failsafe.with(new RetryPolicy().withMaxRetries(2)).with(executor).with(listeners).onSuccess(p -> {
        successCounter.incrementAndGet();
        waiter.resume();
    }).get(() -> service.connect());
    // Then
    waiter.await(1000);
    verify(service, times(3)).connect();
    assertEquals(future.get().booleanValue(), true);
    assertEquals(retryCounter.get(), 2);
    assertEquals(successCounter.get(), 1);
}
Also used : Listeners(net.jodah.failsafe.Listeners) FailsafeFuture(net.jodah.failsafe.FailsafeFuture) Assert.assertEquals(org.testng.Assert.assertEquals) Mockito.times(org.mockito.Mockito.times) Test(org.testng.annotations.Test) RetryPolicy(net.jodah.failsafe.RetryPolicy) Mockito.when(org.mockito.Mockito.when) Executors(java.util.concurrent.Executors) Mockito.verify(org.mockito.Mockito.verify) Failsafe(net.jodah.failsafe.Failsafe) Waiter(net.jodah.concurrentunit.Waiter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Testing.failures(net.jodah.failsafe.Testing.failures) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Mockito.mock(org.mockito.Mockito.mock) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Listeners(net.jodah.failsafe.Listeners) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Waiter(net.jodah.concurrentunit.Waiter) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 74 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project hbase by apache.

the class IntegrationTestTimeBoundedRequestsWithRegionReplicas method runIngestTest.

@Override
protected void runIngestTest(long defaultRunTime, long keysPerServerPerIter, int colsPerKey, int recordSize, int writeThreads, int readThreads) throws Exception {
    LOG.info("Cluster size:" + util.getHBaseClusterInterface().getClusterStatus().getServersSize());
    long start = System.currentTimeMillis();
    String runtimeKey = String.format(RUN_TIME_KEY, this.getClass().getSimpleName());
    long runtime = util.getConfiguration().getLong(runtimeKey, defaultRunTime);
    long startKey = 0;
    long numKeys = getNumKeys(keysPerServerPerIter);
    // write data once
    LOG.info("Writing some data to the table");
    writeData(colsPerKey, recordSize, writeThreads, startKey, numKeys);
    // flush the table
    LOG.info("Flushing the table");
    Admin admin = util.getAdmin();
    admin.flush(getTablename());
    // re-open the regions to make sure that the replicas are up to date
    long refreshTime = conf.getLong(StorefileRefresherChore.REGIONSERVER_STOREFILE_REFRESH_PERIOD, 0);
    if (refreshTime > 0 && refreshTime <= 10000) {
        LOG.info("Sleeping " + refreshTime + "ms to ensure that the data is replicated");
        Threads.sleep(refreshTime * 3);
    } else {
        LOG.info("Reopening the table");
        admin.disableTable(getTablename());
        admin.enableTable(getTablename());
    }
    // We should only start the ChaosMonkey after the readers are started and have cached
    // all of the region locations. Because the meta is not replicated, the timebounded reads
    // will timeout if meta server is killed.
    // We will start the chaos monkey after 1 minute, and since the readers are reading random
    // keys, it should be enough to cache every region entry.
    long chaosMonkeyDelay = conf.getLong(String.format("%s.%s", TEST_NAME, CHAOS_MONKEY_DELAY_KEY), DEFAUL_CHAOS_MONKEY_DELAY);
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    LOG.info(String.format("ChaosMonkey delay is : %d seconds. Will start %s " + "ChaosMonkey after delay", chaosMonkeyDelay / 1000, monkeyToUse));
    ScheduledFuture<?> result = executorService.schedule(new Runnable() {

        @Override
        public void run() {
            try {
                LOG.info("Starting ChaosMonkey");
                monkey.start();
                monkey.waitForStop();
            } catch (Exception e) {
                LOG.warn(StringUtils.stringifyException(e));
            }
        }
    }, chaosMonkeyDelay, TimeUnit.MILLISECONDS);
    // set the intended run time for the reader. The reader will do read requests
    // to random keys for this amount of time.
    long remainingTime = runtime - (System.currentTimeMillis() - start);
    if (remainingTime <= 0) {
        LOG.error("The amount of time left for the test to perform random reads is " + "non-positive. Increase the test execution time via " + String.format(RUN_TIME_KEY, IntegrationTestTimeBoundedRequestsWithRegionReplicas.class.getSimpleName()) + " or reduce the amount of data written per server via " + IntegrationTestTimeBoundedRequestsWithRegionReplicas.class.getSimpleName() + "." + IntegrationTestIngest.NUM_KEYS_PER_SERVER_KEY);
        throw new IllegalArgumentException("No time remains to execute random reads");
    }
    LOG.info("Reading random keys from the table for " + remainingTime / 60000 + " min");
    this.conf.setLong(String.format(RUN_TIME_KEY, TimeBoundedMultiThreadedReader.class.getSimpleName()), // load tool shares the same conf
    remainingTime);
    // now start the readers which will run for configured run time
    try {
        int ret = loadTool.run(getArgsForLoadTestTool("-read", String.format("100:%d", readThreads), startKey, numKeys));
        if (0 != ret) {
            String errorMsg = "Verification failed with error code " + ret;
            LOG.error(errorMsg);
            Assert.fail(errorMsg);
        }
    } finally {
        if (result != null)
            result.cancel(false);
        monkey.stop("Stopping the test");
        monkey.waitForStop();
        executorService.shutdown();
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Admin(org.apache.hadoop.hbase.client.Admin) IOException(java.io.IOException)

Example 75 with ScheduledExecutorService

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

the class TestReflectionObjectInspectors method testObjectInspectorThreadSafety.

public void testObjectInspectorThreadSafety() throws InterruptedException {
    // 5 workers to run getReflectionObjectInspector concurrently
    final int workerCount = 5;
    final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(workerCount);
    final MutableObject exception = new MutableObject();
    Thread runner = new Thread(new Runnable() {

        @Override
        @SuppressWarnings("unchecked")
        public void run() {
            Future<ObjectInspector>[] results = (Future<ObjectInspector>[]) new Future[workerCount];
            ObjectPair<Type, ObjectInspectorFactory.ObjectInspectorOptions>[] types = (ObjectPair<Type, ObjectInspectorFactory.ObjectInspectorOptions>[]) new ObjectPair[] { new ObjectPair<Type, ObjectInspectorFactory.ObjectInspectorOptions>(Complex.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT), new ObjectPair<Type, ObjectInspectorFactory.ObjectInspectorOptions>(MyStruct.class, ObjectInspectorFactory.ObjectInspectorOptions.JAVA) };
            try {
                for (int i = 0; i < 20; i++) {
                    // repeat 20 times
                    for (final ObjectPair<Type, ObjectInspectorFactory.ObjectInspectorOptions> t : types) {
                        ObjectInspectorFactory.objectInspectorCache.clear();
                        for (int k = 0; k < workerCount; k++) {
                            results[k] = executorService.schedule(new Callable<ObjectInspector>() {

                                @Override
                                public ObjectInspector call() throws Exception {
                                    return ObjectInspectorFactory.getReflectionObjectInspector(t.getFirst(), t.getSecond());
                                }
                            }, 50, TimeUnit.MILLISECONDS);
                        }
                        ObjectInspector oi = results[0].get();
                        for (int k = 1; k < workerCount; k++) {
                            assertEquals(oi, results[k].get());
                        }
                    }
                }
            } catch (Throwable e) {
                exception.setValue(e);
            }
        }
    });
    try {
        runner.start();
        // timeout in 5 minutes
        long endTime = System.currentTimeMillis() + 300000;
        while (runner.isAlive()) {
            if (System.currentTimeMillis() > endTime) {
                // Interrupt the runner thread
                runner.interrupt();
                fail("Timed out waiting for the runner to finish");
            }
            runner.join(10000);
        }
        if (exception.getValue() != null) {
            fail("Got exception: " + exception.getValue());
        }
    } finally {
        executorService.shutdownNow();
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Type(java.lang.reflect.Type) Future(java.util.concurrent.Future) PrimitiveObjectInspectorFactory(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory) MutableObject(org.apache.commons.lang.mutable.MutableObject) ObjectPair(org.apache.hadoop.hive.common.ObjectPair)

Aggregations

ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)332 Test (org.junit.Test)99 ExecutorService (java.util.concurrent.ExecutorService)41 Test (org.testng.annotations.Test)35 CountDownLatch (java.util.concurrent.CountDownLatch)33 IOException (java.io.IOException)31 ArrayList (java.util.ArrayList)31 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)31 HashMap (java.util.HashMap)30 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)28 Map (java.util.Map)26 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)25 List (java.util.List)19 ThreadFactory (java.util.concurrent.ThreadFactory)16 None (com.linkedin.common.util.None)15 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)15 UUID (java.util.UUID)15 DefaultStatisticsProvider (org.apache.jackrabbit.oak.stats.DefaultStatisticsProvider)15 URI (java.net.URI)14 CompletableFuture (java.util.concurrent.CompletableFuture)14