Search in sources :

Example 1 with Job

use of io.prestosql.queryeditorui.protocol.Job in project hetu-core by openlookeng.

the class LocalJobHistoryStore method getRecentlyRunForUser.

@Override
public List<Job> getRecentlyRunForUser(Optional<String> user, long maxResults) {
    final ImmutableList.Builder<Job> builder = ImmutableList.builder();
    long added = 0;
    for (Iterator<Job> job = historyCache.descendingIterator(); job.hasNext(); ) {
        Job nextJob = job.next();
        if (user.isPresent() && !nextJob.getUser().equals(user.get())) {
            continue;
        }
        if (added + 1 > maxResults) {
            break;
        }
        builder.add(nextJob);
        added += 1;
    }
    return builder.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Job(io.prestosql.queryeditorui.protocol.Job)

Example 2 with Job

use of io.prestosql.queryeditorui.protocol.Job in project hetu-core by openlookeng.

the class LocalJobHistoryStore method addRun.

@Override
public void addRun(Job job) {
    historyCache.add(job);
    for (Table usedTable : job.getTablesUsed()) {
        EvictingDeque<Job> tableCache = tableHistoryCache.getIfPresent(usedTable);
        if (tableCache == null) {
            tableCache = new FinishedJobEvictingDeque(maximumHistoryPerTable);
            tableHistoryCache.put(usedTable, tableCache);
        }
        tableCache.add(job);
    }
}
Also used : Table(io.prestosql.queryeditorui.protocol.Table) Job(io.prestosql.queryeditorui.protocol.Job)

Example 3 with Job

use of io.prestosql.queryeditorui.protocol.Job in project hetu-core by openlookeng.

the class ExecutionClient method runQuery.

public List<UUID> runQuery(final ExecutionRequest request, final String user, final Duration timeout, HttpServletRequest servletRequest) {
    String query = request.getQuery();
    JobSessionContext sessionContext = request.getSessionContext();
    Map<String, String> properties = sessionContext != null && sessionContext.getProperties() != null ? sessionContext.getProperties() : ImmutableMap.of();
    QueryRunner queryRunner = queryRunnerFactory.create(user, request.getDefaultConnector(), request.getDefaultSchema(), properties);
    QueryExecutionAuthorizer authorizer = new QueryExecutionAuthorizer(user, request.getDefaultConnector(), request.getDefaultSchema());
    // When multiple statements are submitted together, split them and execute in sequence.
    List<String> subStatements = QUERY_SPLITTER.splitToList(query);
    BlockingQueue<Job> jobs = new ArrayBlockingQueue<>(subStatements.size());
    ImmutableList.Builder<UUID> results = ImmutableList.builder();
    URI requestURI = URI.create(servletRequest.getRequestURL().toString());
    for (String statement : subStatements) {
        final UUID uuid = UUID.randomUUID();
        Job job = new Job(user, statement, uuid, persistentJobOutputFactory.create(null, uuid), null, JobState.QUEUED, Collections.emptyList(), null, null, null);
        results.add(job.getUuid());
        jobs.offer(job);
    }
    scheduleExecution(timeout, queryRunner, authorizer, jobs, requestURI);
    return results.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) URI(java.net.URI) JobSessionContext(io.prestosql.queryeditorui.protocol.JobSessionContext) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Job(io.prestosql.queryeditorui.protocol.Job) UUID(java.util.UUID)

Example 4 with Job

use of io.prestosql.queryeditorui.protocol.Job in project hetu-core by openlookeng.

the class ExecutionClient method scheduleExecution.

private UUID scheduleExecution(Duration timeout, QueryRunner queryRunner, QueryExecutionAuthorizer authorizer, BlockingQueue<Job> jobs, URI requestUri) {
    final Job job;
    try {
        job = jobs.take();
    } catch (InterruptedException e) {
        return null;
    }
    final Execution execution = new Execution(job, queryRunner, queryInfoClient, authorizer, timeout, outputBuilderFactory, persistorFactory, requestUri);
    executionMap.put(job.getUuid(), execution);
    activeJobsStore.jobStarted(job);
    ListenableFuture<Job> result = executor.submit(execution);
    Futures.addCallback(result, new FutureCallback<Job>() {

        @Override
        public void onSuccess(@Nullable Job result) {
            if (result != null) {
                result.setState(JobState.FINISHED);
            }
            // Add Active Job
            if (jobs.peek() != null) {
                QueryRunner nextQueryRunner = getNextQueryRunner();
                scheduleExecution(timeout, nextQueryRunner, authorizer, jobs, requestUri);
            }
            jobFinished(result);
        }

        // Re-Use session level fields among multi statement queries.
        private QueryRunner getNextQueryRunner() {
            StatementClient client = queryRunner.getCurrentClient();
            ClientSession session = queryRunner.getSession();
            ClientSession.Builder builder = ClientSession.builder(session).withoutTransactionId();
            if (client.getSetCatalog().isPresent()) {
                builder.withCatalog(client.getSetCatalog().get());
            }
            if (client.getSetSchema().isPresent()) {
                builder.withSchema(client.getSetSchema().get());
            }
            if (client.getStartedTransactionId() != null) {
                builder = builder.withTransactionId(client.getStartedTransactionId());
            }
            if (client.getSetPath().isPresent()) {
                builder = builder.withPath(client.getSetPath().get());
            }
            if (!client.getSetSessionProperties().isEmpty() || !client.getResetSessionProperties().isEmpty()) {
                Map<String, String> sessionProperties = new HashMap<>(session.getProperties());
                sessionProperties.putAll(client.getSetSessionProperties());
                sessionProperties.keySet().removeAll(client.getResetSessionProperties());
                builder = builder.withProperties(sessionProperties);
            }
            if (!client.getSetRoles().isEmpty()) {
                Map<String, ClientSelectedRole> roles = new HashMap<>(session.getRoles());
                roles.putAll(client.getSetRoles());
                builder = builder.withRoles(roles);
            }
            if (!client.getAddedPreparedStatements().isEmpty() || !client.getDeallocatedPreparedStatements().isEmpty()) {
                Map<String, String> preparedStatements = new HashMap<>(session.getPreparedStatements());
                preparedStatements.putAll(client.getAddedPreparedStatements());
                preparedStatements.keySet().removeAll(client.getDeallocatedPreparedStatements());
                builder = builder.withPreparedStatements(preparedStatements);
            }
            return queryRunnerFactory.create(builder.build());
        }

        @Override
        public void onFailure(@NotNull Throwable t) {
            job.setState(JobState.FAILED);
            if (job.getError() == null) {
                job.setError(new QueryError(t.getMessage(), null, -1, null, null, null, null, null));
            }
            jobFinished(job);
        }
    }, MoreExecutors.directExecutor());
    return job.getUuid();
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) StatementClient(io.prestosql.client.StatementClient) ClientSession(io.prestosql.client.ClientSession) Job(io.prestosql.queryeditorui.protocol.Job) QueryError(io.prestosql.client.QueryError) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

Job (io.prestosql.queryeditorui.protocol.Job)4 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 ClientSession (io.prestosql.client.ClientSession)1 QueryError (io.prestosql.client.QueryError)1 StatementClient (io.prestosql.client.StatementClient)1 JobSessionContext (io.prestosql.queryeditorui.protocol.JobSessionContext)1 Table (io.prestosql.queryeditorui.protocol.Table)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 UUID (java.util.UUID)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1