Search in sources :

Example 61 with ListenableFuture

use of com.google.common.util.concurrent.ListenableFuture in project presto by prestodb.

the class TestMemoryPools method testBlocking.

@Test
public void testBlocking() throws Exception {
    Session session = testSessionBuilder().setCatalog("tpch").setSchema("tiny").setSystemProperty("task_default_concurrency", "1").build();
    LocalQueryRunner localQueryRunner = queryRunnerWithInitialTransaction(session);
    // add tpch
    localQueryRunner.createCatalog("tpch", new TpchConnectorFactory(1), ImmutableMap.of());
    // reserve all the memory in the pool
    MemoryPool pool = new MemoryPool(new MemoryPoolId("test"), new DataSize(10, MEGABYTE));
    QueryId fakeQueryId = new QueryId("fake");
    assertTrue(pool.tryReserve(fakeQueryId, TEN_MEGABYTES));
    MemoryPool systemPool = new MemoryPool(new MemoryPoolId("testSystem"), new DataSize(10, MEGABYTE));
    QueryContext queryContext = new QueryContext(new QueryId("query"), new DataSize(10, MEGABYTE), pool, systemPool, localQueryRunner.getExecutor());
    // discard all output
    OutputFactory outputFactory = new PageConsumerOutputFactory(types -> (page -> {
    }));
    TaskContext taskContext = createTaskContext(queryContext, localQueryRunner.getExecutor(), session);
    List<Driver> drivers = localQueryRunner.createDrivers("SELECT COUNT(*) FROM orders JOIN lineitem USING (orderkey)", outputFactory, taskContext);
    // run driver, until it blocks
    while (!isWaitingForMemory(drivers)) {
        for (Driver driver : drivers) {
            driver.process();
        }
    }
    // driver should be blocked waiting for memory
    for (Driver driver : drivers) {
        assertFalse(driver.isFinished());
    }
    assertTrue(pool.getFreeBytes() <= 0);
    pool.free(fakeQueryId, TEN_MEGABYTES);
    do {
        assertFalse(isWaitingForMemory(drivers));
        boolean progress = false;
        for (Driver driver : drivers) {
            ListenableFuture<?> blocked = driver.process();
            progress = progress | blocked.isDone();
        }
        // query should not block
        assertTrue(progress);
    } while (!drivers.stream().allMatch(Driver::isFinished));
}
Also used : LocalQueryRunner(com.facebook.presto.testing.LocalQueryRunner) PageConsumerOutputFactory(com.facebook.presto.testing.PageConsumerOperator.PageConsumerOutputFactory) TaskContext(com.facebook.presto.operator.TaskContext) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TestingTaskContext.createTaskContext(com.facebook.presto.testing.TestingTaskContext.createTaskContext) ImmutableMap(com.google.common.collect.ImmutableMap) Session(com.facebook.presto.Session) MEGABYTE(io.airlift.units.DataSize.Unit.MEGABYTE) TpchConnectorFactory(com.facebook.presto.tpch.TpchConnectorFactory) OutputFactory(com.facebook.presto.operator.OutputFactory) Test(org.testng.annotations.Test) Driver(com.facebook.presto.operator.Driver) TestingSession.testSessionBuilder(com.facebook.presto.testing.TestingSession.testSessionBuilder) MemoryPoolId(com.facebook.presto.spi.memory.MemoryPoolId) DataSize(io.airlift.units.DataSize) List(java.util.List) QueryId(com.facebook.presto.spi.QueryId) Assert.assertTrue(org.testng.Assert.assertTrue) LocalQueryRunner.queryRunnerWithInitialTransaction(com.facebook.presto.testing.LocalQueryRunner.queryRunnerWithInitialTransaction) OperatorContext(com.facebook.presto.operator.OperatorContext) Assert.assertFalse(org.testng.Assert.assertFalse) TpchConnectorFactory(com.facebook.presto.tpch.TpchConnectorFactory) TaskContext(com.facebook.presto.operator.TaskContext) TestingTaskContext.createTaskContext(com.facebook.presto.testing.TestingTaskContext.createTaskContext) PageConsumerOutputFactory(com.facebook.presto.testing.PageConsumerOperator.PageConsumerOutputFactory) QueryId(com.facebook.presto.spi.QueryId) Driver(com.facebook.presto.operator.Driver) LocalQueryRunner(com.facebook.presto.testing.LocalQueryRunner) DataSize(io.airlift.units.DataSize) PageConsumerOutputFactory(com.facebook.presto.testing.PageConsumerOperator.PageConsumerOutputFactory) OutputFactory(com.facebook.presto.operator.OutputFactory) MemoryPoolId(com.facebook.presto.spi.memory.MemoryPoolId) Session(com.facebook.presto.Session) Test(org.testng.annotations.Test)

Example 62 with ListenableFuture

use of com.google.common.util.concurrent.ListenableFuture in project helios by spotify.

the class HeliosSoloDeploymentTest method testUndeployLeftoverJobs.

@Test
public void testUndeployLeftoverJobs() throws Exception {
    final HeliosSoloDeployment solo = buildHeliosSoloDeployment();
    final ListenableFuture<List<String>> hostsFuture = Futures.<List<String>>immediateFuture(ImmutableList.of(HOST1, HOST2));
    when(heliosClient.listHosts()).thenReturn(hostsFuture);
    // These futures represent HostStatuses when the job is still deployed
    final ListenableFuture<HostStatus> statusFuture11 = Futures.immediateFuture(HostStatus.newBuilder().setStatus(Status.UP).setStatuses(ImmutableMap.of(JOB_ID1, TASK_STATUS1)).setJobs(ImmutableMap.of(JOB_ID1, Deployment.of(JOB_ID1, Goal.START))).build());
    final ListenableFuture<HostStatus> statusFuture21 = Futures.immediateFuture(HostStatus.newBuilder().setStatus(Status.UP).setStatuses(ImmutableMap.of(JOB_ID2, TASK_STATUS2)).setJobs(ImmutableMap.of(JOB_ID2, Deployment.of(JOB_ID2, Goal.START))).build());
    // These futures represent HostStatuses when the job is undeployed
    final ListenableFuture<HostStatus> statusFuture12 = Futures.immediateFuture(HostStatus.newBuilder().setStatus(Status.UP).setStatuses(Collections.<JobId, TaskStatus>emptyMap()).setJobs(ImmutableMap.of(JOB_ID1, Deployment.of(JOB_ID1, Goal.START))).build());
    final ListenableFuture<HostStatus> statusFuture22 = Futures.immediateFuture(HostStatus.newBuilder().setStatus(Status.UP).setStatuses(Collections.<JobId, TaskStatus>emptyMap()).setJobs(ImmutableMap.of(JOB_ID2, Deployment.of(JOB_ID2, Goal.START))).build());
    //noinspection unchecked
    when(heliosClient.hostStatus(HOST1)).thenReturn(statusFuture11);
    //noinspection unchecked
    when(heliosClient.hostStatus(HOST2)).thenReturn(statusFuture21);
    final ListenableFuture<JobUndeployResponse> undeployFuture1 = Futures.immediateFuture(new JobUndeployResponse(JobUndeployResponse.Status.OK, HOST1, JOB_ID1));
    final ListenableFuture<JobUndeployResponse> undeployFuture2 = Futures.immediateFuture(new JobUndeployResponse(JobUndeployResponse.Status.OK, HOST2, JOB_ID2));
    // when undeploy is called, respond correctly & patch the mock to return
    // the undeployed HostStatus
    when(heliosClient.undeploy(JOB_ID1, HOST1)).thenAnswer(new Answer<ListenableFuture<JobUndeployResponse>>() {

        @Override
        public ListenableFuture<JobUndeployResponse> answer(final InvocationOnMock invocation) throws Throwable {
            when(heliosClient.hostStatus(HOST1)).thenReturn(statusFuture12);
            return undeployFuture1;
        }
    });
    when(heliosClient.undeploy(JOB_ID2, HOST2)).thenAnswer(new Answer<ListenableFuture<JobUndeployResponse>>() {

        @Override
        public ListenableFuture<JobUndeployResponse> answer(final InvocationOnMock invocation) throws Throwable {
            when(heliosClient.hostStatus(HOST1)).thenReturn(statusFuture22);
            return undeployFuture2;
        }
    });
    solo.undeployLeftoverJobs();
    verify(heliosClient).undeploy(JOB_ID1, HOST1);
    verify(heliosClient).undeploy(JOB_ID2, HOST2);
}
Also used : TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobUndeployResponse(com.spotify.helios.common.protocol.JobUndeployResponse) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) HostStatus(com.spotify.helios.common.descriptors.HostStatus) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 63 with ListenableFuture

use of com.google.common.util.concurrent.ListenableFuture in project cassandra by apache.

the class LocalSessions method handlePrepareMessage.

/**
     * The PrepareConsistentRequest effectively promotes the parent repair session to a consistent
     * incremental session, and begins the 'pending anti compaction' which moves all sstable data
     * that is to be repaired into it's own silo, preventing it from mixing with other data.
     *
     * No response is sent to the repair coordinator until the pending anti compaction has completed
     * successfully. If the pending anti compaction fails, a failure message is sent to the coordinator,
     * cancelling the session.
     */
public void handlePrepareMessage(InetAddress from, PrepareConsistentRequest request) {
    logger.debug("received {} from {}", request, from);
    UUID sessionID = request.parentSession;
    InetAddress coordinator = request.coordinator;
    Set<InetAddress> peers = request.participants;
    ActiveRepairService.ParentRepairSession parentSession;
    try {
        parentSession = getParentRepairSession(sessionID);
    } catch (Throwable e) {
        logger.debug("Error retrieving ParentRepairSession for session {}, responding with failure", sessionID);
        sendMessage(coordinator, new FailSession(sessionID));
        return;
    }
    LocalSession session = createSessionUnsafe(sessionID, parentSession, peers);
    putSessionUnsafe(session);
    logger.debug("created local session for {}", sessionID);
    ExecutorService executor = Executors.newFixedThreadPool(parentSession.getColumnFamilyStores().size());
    ListenableFuture pendingAntiCompaction = submitPendingAntiCompaction(session, executor);
    Futures.addCallback(pendingAntiCompaction, new FutureCallback() {

        public void onSuccess(@Nullable Object result) {
            logger.debug("pending anti-compaction for {} completed", sessionID);
            setStateAndSave(session, PREPARED);
            sendMessage(coordinator, new PrepareConsistentResponse(sessionID, getBroadcastAddress(), true));
            executor.shutdown();
        }

        public void onFailure(Throwable t) {
            logger.debug("pending anti-compaction for {} failed", sessionID);
            failSession(sessionID);
            executor.shutdown();
        }
    });
}
Also used : ActiveRepairService(org.apache.cassandra.service.ActiveRepairService) PrepareConsistentResponse(org.apache.cassandra.repair.messages.PrepareConsistentResponse) FailSession(org.apache.cassandra.repair.messages.FailSession) ExecutorService(java.util.concurrent.ExecutorService) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) UUID(java.util.UUID) InetAddress(java.net.InetAddress) FutureCallback(com.google.common.util.concurrent.FutureCallback)

Example 64 with ListenableFuture

use of com.google.common.util.concurrent.ListenableFuture in project cassandra by apache.

the class LongBTreeTest method testInsertions.

private static void testInsertions(int tests, int perTestCount, float testKeyRatio, int modificationBatchSize, boolean quickEquality) throws ExecutionException, InterruptedException {
    int batchesPerTest = perTestCount / modificationBatchSize;
    int testKeyRange = (int) (perTestCount * testKeyRatio);
    long totalCount = (long) perTestCount * tests;
    log("Performing %d tests of %d operations, with %.2f max size/key-range ratio in batches of ~%d ops", tests, perTestCount, 1 / testKeyRatio, modificationBatchSize);
    // if we're not doing quick-equality, we can spam with garbage for all the checks we perform, so we'll split the work into smaller chunks
    int chunkSize = quickEquality ? tests : (int) (100000 / Math.pow(perTestCount, 2));
    for (int chunk = 0; chunk < tests; chunk += chunkSize) {
        final List<ListenableFutureTask<List<ListenableFuture<?>>>> outer = new ArrayList<>();
        for (int i = 0; i < chunkSize; i++) {
            int maxRunLength = modificationBatchSize == 1 ? 1 : ThreadLocalRandom.current().nextInt(1, modificationBatchSize);
            outer.add(doOneTestInsertions(testKeyRange, maxRunLength, modificationBatchSize, batchesPerTest, quickEquality));
        }
        final List<ListenableFuture<?>> inner = new ArrayList<>();
        long complete = 0;
        int reportInterval = Math.max(1000, (int) (totalCount / 10000));
        long lastReportAt = 0;
        for (ListenableFutureTask<List<ListenableFuture<?>>> f : outer) {
            inner.addAll(f.get());
            complete += perTestCount;
            if (complete - lastReportAt >= reportInterval) {
                long done = (chunk * perTestCount) + complete;
                float ratio = done / (float) totalCount;
                log("Completed %.1f%% (%d of %d operations)", ratio * 100, done, totalCount);
                lastReportAt = complete;
            }
        }
        Futures.allAsList(inner).get();
    }
    Snapshot snap = BTREE_TIMER.getSnapshot();
    log("btree: %.2fns, %.2fns, %.2fns", snap.getMedian(), snap.get95thPercentile(), snap.get999thPercentile());
    snap = TREE_TIMER.getSnapshot();
    log("java: %.2fns, %.2fns, %.2fns", snap.getMedian(), snap.get95thPercentile(), snap.get999thPercentile());
    log("Done");
}
Also used : Snapshot(com.codahale.metrics.Snapshot) ListenableFutureTask(com.google.common.util.concurrent.ListenableFutureTask) ListenableFuture(com.google.common.util.concurrent.ListenableFuture)

Example 65 with ListenableFuture

use of com.google.common.util.concurrent.ListenableFuture in project retrofit by square.

the class GuavaCallAdapterFactoryTest method responseType.

@Test
public void responseType() {
    Type bodyClass = new TypeToken<ListenableFuture<String>>() {
    }.getType();
    assertThat(factory.get(bodyClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type bodyWildcard = new TypeToken<ListenableFuture<? extends String>>() {
    }.getType();
    assertThat(factory.get(bodyWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type bodyGeneric = new TypeToken<ListenableFuture<List<String>>>() {
    }.getType();
    assertThat(factory.get(bodyGeneric, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(new TypeToken<List<String>>() {
    }.getType());
    Type responseClass = new TypeToken<ListenableFuture<Response<String>>>() {
    }.getType();
    assertThat(factory.get(responseClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type responseWildcard = new TypeToken<ListenableFuture<Response<? extends String>>>() {
    }.getType();
    assertThat(factory.get(responseWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type resultClass = new TypeToken<ListenableFuture<Response<String>>>() {
    }.getType();
    assertThat(factory.get(resultClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type resultWildcard = new TypeToken<ListenableFuture<Response<? extends String>>>() {
    }.getType();
    assertThat(factory.get(resultWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
}
Also used : Type(java.lang.reflect.Type) TypeToken(com.google.common.reflect.TypeToken) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Test(org.junit.Test)

Aggregations

ListenableFuture (com.google.common.util.concurrent.ListenableFuture)186 Test (org.junit.Test)78 ArrayList (java.util.ArrayList)62 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)49 List (java.util.List)45 ExecutionException (java.util.concurrent.ExecutionException)42 IOException (java.io.IOException)35 Map (java.util.Map)33 CountDownLatch (java.util.concurrent.CountDownLatch)26 File (java.io.File)23 ImmutableList (com.google.common.collect.ImmutableList)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 Callable (java.util.concurrent.Callable)19 ImmutableMap (com.google.common.collect.ImmutableMap)18 HashMap (java.util.HashMap)17 Futures (com.google.common.util.concurrent.Futures)14 URL (java.net.URL)14 TimeoutException (java.util.concurrent.TimeoutException)13 Request (com.metamx.http.client.Request)12