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));
}
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);
}
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();
}
});
}
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");
}
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);
}
Aggregations