use of java.util.concurrent.TimeUnit.MILLISECONDS in project presto by prestodb.
the class QueryPreprocessor method preprocessQueryInternal.
private static String preprocessQueryInternal(Optional<String> catalog, Optional<String> schema, String query, List<String> preprocessorCommand, Duration timeout) throws QueryPreprocessorException {
// execute the process in a child thread so we can better handle interruption and timeouts
AtomicReference<Process> processReference = new AtomicReference<>();
Future<String> task = executeInNewThread("Query preprocessor", () -> {
String result;
int exitCode;
Future<String> readStderr;
try {
ProcessBuilder processBuilder = new ProcessBuilder(preprocessorCommand);
processBuilder.environment().put(ENV_PRESTO_CATALOG, catalog.orElse(""));
processBuilder.environment().put(ENV_PRESTO_SCHEMA, schema.orElse(""));
Process process = processBuilder.start();
processReference.set(process);
Future<?> writeOutput = null;
try {
// write query to process standard out
writeOutput = executeInNewThread("Query preprocessor output", () -> {
try (OutputStream outputStream = process.getOutputStream()) {
outputStream.write(query.getBytes(UTF_8));
}
return null;
});
// read stderr
readStderr = executeInNewThread("Query preprocessor read stderr", () -> {
StringBuilder builder = new StringBuilder();
try (InputStream inputStream = process.getErrorStream()) {
CharStreams.copy(new InputStreamReader(inputStream, UTF_8), builder);
} catch (IOException | RuntimeException ignored) {
}
return builder.toString();
});
// read response
try (InputStream inputStream = process.getInputStream()) {
result = CharStreams.toString(new InputStreamReader(inputStream, UTF_8));
}
// verify output was written successfully
try {
writeOutput.get();
} catch (ExecutionException e) {
throw e.getCause();
}
// wait for process to finish
exitCode = process.waitFor();
} finally {
process.destroyForcibly();
if (writeOutput != null) {
writeOutput.cancel(true);
}
}
} catch (QueryPreprocessorException e) {
throw e;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new QueryPreprocessorException("Interrupted while preprocessing query");
} catch (Throwable e) {
throw new QueryPreprocessorException("Error preprocessing query: " + e.getMessage(), e);
}
// check we got a valid exit code
if (exitCode != 0) {
Optional<String> errorMessage = tryGetFutureValue(readStderr, 100, MILLISECONDS).flatMap(value -> Optional.ofNullable(emptyToNull(value.trim())));
throw new QueryPreprocessorException("Query preprocessor exited " + exitCode + errorMessage.map(message1 -> "\n===\n" + message1 + "\n===").orElse(""));
}
return result;
});
try {
return task.get(timeout.toMillis(), MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new QueryPreprocessorException("Interrupted while preprocessing query");
} catch (ExecutionException e) {
Throwable cause = e.getCause();
propagateIfPossible(cause, QueryPreprocessorException.class);
throw new QueryPreprocessorException("Error preprocessing query: " + cause.getMessage(), cause);
} catch (TimeoutException e) {
throw new QueryPreprocessorException("Timed out waiting for query preprocessor after " + timeout);
} finally {
Process process = processReference.get();
if (process != null) {
process.destroyForcibly();
}
task.cancel(true);
}
}
use of java.util.concurrent.TimeUnit.MILLISECONDS in project presto by prestodb.
the class TestHiveRecoverableExecution method testRecoverableGroupedExecution.
private void testRecoverableGroupedExecution(DistributedQueryRunner queryRunner, int writerConcurrency, boolean optimizedPartitionUpdateSerializationEnabled, List<String> preQueries, @Language("SQL") String queryWithoutFailure, @Language("SQL") String queryWithFailure, int expectedUpdateCount, List<String> postQueries) throws Exception {
waitUntilAllNodesAreHealthy(queryRunner, new Duration(10, SECONDS));
Session recoverableSession = createRecoverableSession(writerConcurrency, optimizedPartitionUpdateSerializationEnabled);
for (@Language("SQL") String postQuery : postQueries) {
queryRunner.execute(recoverableSession, postQuery);
}
try {
for (@Language("SQL") String preQuery : preQueries) {
queryRunner.execute(recoverableSession, preQuery);
}
// test no failure case
Stopwatch noRecoveryStopwatch = Stopwatch.createStarted();
assertEquals(queryRunner.execute(recoverableSession, queryWithoutFailure).getUpdateCount(), OptionalLong.of(expectedUpdateCount));
log.info("Query with no recovery took %sms", noRecoveryStopwatch.elapsed(MILLISECONDS));
// cancel all queries and tasks to make sure we are dealing only with a single running query
cancelAllQueries(queryRunner);
cancelAllTasks(queryRunner);
// test failure case
Stopwatch recoveryStopwatch = Stopwatch.createStarted();
ListenableFuture<MaterializedResult> result = executor.submit(() -> queryRunner.execute(recoverableSession, queryWithFailure));
List<TestingPrestoServer> workers = queryRunner.getServers().stream().filter(server -> !server.isCoordinator()).collect(toList());
shuffle(workers);
TestingPrestoServer worker1 = workers.get(0);
// kill worker1 right away, to make sure recoverable execution works in cases when the task hasn't been yet submitted
worker1.stopResponding();
// kill worker2 only after the task has been scheduled
TestingPrestoServer worker2 = workers.get(1);
sleep(1000);
worker2.stopResponding();
assertEquals(result.get(1000, SECONDS).getUpdateCount(), OptionalLong.of(expectedUpdateCount));
log.info("Query with recovery took %sms", recoveryStopwatch.elapsed(MILLISECONDS));
} finally {
queryRunner.getServers().forEach(TestingPrestoServer::startResponding);
cancelAllQueries(queryRunner);
cancelAllTasks(queryRunner);
for (@Language("SQL") String postQuery : postQueries) {
queryRunner.execute(recoverableSession, postQuery);
}
}
}
use of java.util.concurrent.TimeUnit.MILLISECONDS in project presto by prestodb.
the class TestResourceManagerClusterStatusSender method setup.
@BeforeTest
public void setup() {
resourceManagerClient = new TestingResourceManagerClient();
InMemoryNodeManager nodeManager = new InMemoryNodeManager();
nodeManager.addNode(CONNECTOR_ID, new InternalNode("identifier", URI.create("http://localhost:80/identifier"), OptionalInt.of(1), "1", false, true));
sender = new ResourceManagerClusterStatusSender((addressSelectionContext, headers) -> resourceManagerClient, nodeManager, () -> NODE_STATUS, newSingleThreadScheduledExecutor(), new ResourceManagerConfig().setNodeHeartbeatInterval(new Duration(HEARTBEAT_INTERVAL, MILLISECONDS)).setQueryHeartbeatInterval(new Duration(HEARTBEAT_INTERVAL, MILLISECONDS)));
}
use of java.util.concurrent.TimeUnit.MILLISECONDS in project presto by prestodb.
the class StorageModule method createStripeMetadataSourceFactory.
@Singleton
@Provides
public StripeMetadataSourceFactory createStripeMetadataSourceFactory(OrcCacheConfig orcCacheConfig, MBeanExporter exporter) {
StripeMetadataSource stripeMetadataSource = new StorageStripeMetadataSource();
if (orcCacheConfig.isStripeMetadataCacheEnabled()) {
Cache<StripeId, Slice> footerCache = CacheBuilder.newBuilder().maximumWeight(orcCacheConfig.getStripeFooterCacheSize().toBytes()).weigher((id, footer) -> ((Slice) footer).length()).expireAfterAccess(orcCacheConfig.getStripeFooterCacheTtlSinceLastAccess().toMillis(), TimeUnit.MILLISECONDS).recordStats().build();
Cache<StripeStreamId, Slice> streamCache = CacheBuilder.newBuilder().maximumWeight(orcCacheConfig.getStripeStreamCacheSize().toBytes()).weigher((id, stream) -> ((Slice) stream).length()).expireAfterAccess(orcCacheConfig.getStripeStreamCacheTtlSinceLastAccess().toMillis(), TimeUnit.MILLISECONDS).recordStats().build();
CacheStatsMBean footerCacheStatsMBean = new CacheStatsMBean(footerCache);
CacheStatsMBean streamCacheStatsMBean = new CacheStatsMBean(streamCache);
exporter.export(generatedNameOf(CacheStatsMBean.class, connectorId + "_StripeFooter"), footerCacheStatsMBean);
exporter.export(generatedNameOf(CacheStatsMBean.class, connectorId + "_StripeStream"), streamCacheStatsMBean);
Optional<Cache<StripeStreamId, List<RowGroupIndex>>> rowGroupIndexCache = Optional.empty();
if (orcCacheConfig.isRowGroupIndexCacheEnabled()) {
rowGroupIndexCache = Optional.of(CacheBuilder.newBuilder().maximumWeight(orcCacheConfig.getRowGroupIndexCacheSize().toBytes()).weigher((id, rowGroupIndices) -> toIntExact(((List<RowGroupIndex>) rowGroupIndices).stream().mapToLong(RowGroupIndex::getRetainedSizeInBytes).sum())).expireAfterAccess(orcCacheConfig.getStripeStreamCacheTtlSinceLastAccess().toMillis(), MILLISECONDS).recordStats().build());
CacheStatsMBean rowGroupIndexCacheStatsMBean = new CacheStatsMBean(rowGroupIndexCache.get());
exporter.export(generatedNameOf(CacheStatsMBean.class, connectorId + "_StripeStreamRowGroupIndex"), rowGroupIndexCacheStatsMBean);
}
stripeMetadataSource = new CachingStripeMetadataSource(stripeMetadataSource, footerCache, streamCache, rowGroupIndexCache);
}
StripeMetadataSourceFactory factory = StripeMetadataSourceFactory.of(stripeMetadataSource);
if (orcCacheConfig.isDwrfStripeCacheEnabled()) {
factory = new DwrfAwareStripeMetadataSourceFactory(factory);
}
return factory;
}
use of java.util.concurrent.TimeUnit.MILLISECONDS in project mule by mulesoft.
the class ServerNotificationsTestCase method testCustomNotifications.
@Test
public void testCustomNotifications() throws Exception {
final CountDownLatch latch = new CountDownLatch(2);
getNotificationListenerRegistry().registerListener((DummyNotificationListener) notification -> {
if (new IntegerAction(EVENT_RECEIVED).equals(notification.getAction())) {
customNotificationCount.incrementAndGet();
assertEquals("hello", ((EventObject) notification).getSource());
latch.countDown();
}
});
getNotificationDispatcher().dispatch(new DummyNotification("hello", EVENT_RECEIVED));
getNotificationDispatcher().dispatch(new DummyNotification("hello", EVENT_RECEIVED));
// Wait for the notifcation event to be fired as they are queued
latch.await(2000, MILLISECONDS);
assertEquals(2, customNotificationCount.get());
}
Aggregations