Search in sources :

Example 1 with Job

use of com.airbnb.airpal.api.Job in project airpal by airbnb.

the class UsersResource method getUserQueries.

@GET
@Path("queries")
public Response getUserQueries(@Auth AirpalUser user, @PathParam("id") String userId, @QueryParam("results") int numResults, @QueryParam("table") List<PartitionedTable> tables) {
    Iterable<Job> recentlyRun;
    int results = Optional.of(numResults).or(0);
    if (results <= 0) {
        results = 100;
    }
    if (tables.size() < 1) {
        recentlyRun = jobHistoryStore.getRecentlyRunForUser(userId, results);
    } else {
        recentlyRun = jobHistoryStore.getRecentlyRunForUser(userId, results, Iterables.transform(tables, new PartitionedTableToTable()));
    }
    ImmutableList.Builder<Job> filtered = ImmutableList.builder();
    for (Job job : recentlyRun) {
        if (job.getTablesUsed().isEmpty() && (job.getState() == JobState.FAILED)) {
            filtered.add(job);
            continue;
        }
        for (Table table : job.getTablesUsed()) {
            if (AuthorizationUtil.isAuthorizedRead(user, table)) {
                filtered.add(new Job(job.getUser(), job.getQuery(), job.getUuid(), job.getOutput(), job.getQueryStats(), job.getState(), Collections.<Column>emptyList(), Collections.<Table>emptySet(), job.getQueryStartedDateTime(), job.getError(), job.getQueryFinishedDateTime()));
            }
        }
    }
    List<Job> sortedResult = Ordering.natural().nullsLast().onResultOf(JOB_ORDERING).reverse().immutableSortedCopy(filtered.build());
    return Response.ok(sortedResult).build();
}
Also used : Table(com.airbnb.airpal.presto.Table) PartitionedTableToTable(com.airbnb.airpal.presto.PartitionedTable.PartitionedTableToTable) PartitionedTable(com.airbnb.airpal.presto.PartitionedTable) Column(com.facebook.presto.client.Column) ImmutableList(com.google.common.collect.ImmutableList) PartitionedTableToTable(com.airbnb.airpal.presto.PartitionedTable.PartitionedTableToTable) Job(com.airbnb.airpal.api.Job) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with Job

use of com.airbnb.airpal.api.Job in project airpal by airbnb.

the class QueriesResource method getQueries.

@GET
public Response getQueries(@Auth AirpalUser user, @QueryParam("results") int numResults, @QueryParam("table") List<PartitionedTable> tables) {
    Iterable<Job> recentlyRun;
    int results = Optional.of(numResults).or(200);
    if (tables.size() < 1) {
        recentlyRun = jobHistoryStore.getRecentlyRun(results);
    } else {
        recentlyRun = jobHistoryStore.getRecentlyRun(results, Iterables.transform(tables, new PartitionedTable.PartitionedTableToTable()));
    }
    ImmutableList.Builder<Job> filtered = ImmutableList.builder();
    for (Job job : recentlyRun) {
        if (job.getTablesUsed().isEmpty() && (job.getState() == JobState.FAILED)) {
            filtered.add(job);
            continue;
        }
        for (Table table : job.getTablesUsed()) {
            if (AuthorizationUtil.isAuthorizedRead(user, table)) {
                filtered.add(new Job(job.getUser(), job.getQuery(), job.getUuid(), job.getOutput(), job.getQueryStats(), job.getState(), Collections.<Column>emptyList(), Collections.<Table>emptySet(), job.getQueryStartedDateTime(), job.getError(), job.getQueryFinishedDateTime()));
            }
        }
    }
    List<Job> sortedResult = Ordering.natural().nullsLast().onResultOf(JOB_ORDERING).reverse().immutableSortedCopy(filtered.build());
    return Response.ok(sortedResult).build();
}
Also used : Table(com.airbnb.airpal.presto.Table) PartitionedTable(com.airbnb.airpal.presto.PartitionedTable) Column(com.facebook.presto.client.Column) ImmutableList(com.google.common.collect.ImmutableList) PartitionedTable(com.airbnb.airpal.presto.PartitionedTable) Job(com.airbnb.airpal.api.Job) GET(javax.ws.rs.GET)

Example 3 with Job

use of com.airbnb.airpal.api.Job in project airpal by airbnb.

the class JobHistoryStoreDAO method getJobs.

private List<Job> getJobs(long limit, int dayInterval, String outerWhereClauseArg, String innerWhereClauseArg) {
    String outerWhereClause = Strings.isNullOrEmpty(outerWhereClauseArg) ? "true" : outerWhereClauseArg;
    String innerWhereClause = Strings.isNullOrEmpty(innerWhereClauseArg) ? "true" : innerWhereClauseArg;
    try (Handle handle = dbi.open()) {
        Query<Map<String, Object>> query = handle.createQuery("SELECT " + "j.id AS id, " + "j.query AS query, " + "j.user AS user, " + "j.uuid AS uuid, " + "j.queryStats as queryStats, " + "j.state AS state, " + "j.columns AS columns, " + "j.query_finished AS queryFinished, " + "j.query_started AS queryStarted, " + "j.error AS error, " + "t.connector_id AS connectorId, " + "t.schema_ AS \"schema\", " + "t.table_ AS \"table\", " + "t.columns, " + "jo.type, " + "jo.description, " + "jo.location " + "FROM (SELECT * FROM jobs " + "WHERE " + Util.getQueryFinishedCondition(dbType) + " " + "AND " + innerWhereClause + " " + "ORDER BY query_finished DESC LIMIT :limit) j " + "LEFT OUTER JOIN job_tables jt ON j.id = jt.job_id " + "LEFT OUTER JOIN tables t ON jt.table_id = t.id " + "LEFT OUTER JOIN job_outputs jo ON j.id = jo.job_id " + "WHERE " + outerWhereClause + " " + "ORDER BY query_finished DESC").bind("limit", limit).bind("day_interval", dayInterval);
        Map<Long, Job> idToJobMap = query.map(RosettaResultSetMapperFactory.mapperFor(JobTableOutputJoinRow.class)).fold(new HashMap<Long, Job>(), new JobTableOutputJoinRow.JobFolder());
        return new ArrayList<>(idToJobMap.values());
    }
}
Also used : ArrayList(java.util.ArrayList) Job(com.airbnb.airpal.api.Job) HashMap(java.util.HashMap) Map(java.util.Map) JobTableOutputJoinRow(com.airbnb.airpal.sql.beans.JobTableOutputJoinRow) Handle(org.skife.jdbi.v2.Handle)

Example 4 with Job

use of com.airbnb.airpal.api.Job in project airpal by airbnb.

the class ExecutionClient method runQuery.

public UUID runQuery(final String query, final String tmpTable, final AirpalUser user, final String schema, final Duration timeout) {
    final UUID uuid = UUID.randomUUID();
    final Job job = new Job(user.getUserName(), query, uuid, persistentJobOutputFactory.create(tmpTable, uuid), null, JobState.QUEUED, Collections.<Column>emptyList(), null, null);
    final Execution execution = new Execution(job, eventBus, queryRunnerFactory.create(user.getUserName(), schema), queryInfoClient, new QueryExecutionAuthorizer(user, "hive", user.getDefaultSchema()), timeout, columnCache, outputBuilderFactory, persistorFactory);
    executionMap.put(uuid, 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);
            }
            jobFinished(result);
        }

        @Override
        public void onFailure(@NotNull Throwable t) {
            if (t instanceof ExecutionFailureException) {
                ExecutionFailureException e = (ExecutionFailureException) t;
                Job j = e.getJob();
                j.setState(JobState.FAILED);
                if (j.getError() == null) {
                    j.setError(new QueryError(e.getMessage(), null, -1, null, null, null, null));
                }
                jobFinished(j);
            }
        }
    });
    return uuid;
}
Also used : UUID(java.util.UUID) Job(com.airbnb.airpal.api.Job) QueryError(com.facebook.presto.client.QueryError)

Example 5 with Job

use of com.airbnb.airpal.api.Job in project airpal by airbnb.

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(com.airbnb.airpal.presto.Table) Job(com.airbnb.airpal.api.Job)

Aggregations

Job (com.airbnb.airpal.api.Job)7 Table (com.airbnb.airpal.presto.Table)4 ImmutableList (com.google.common.collect.ImmutableList)4 PartitionedTable (com.airbnb.airpal.presto.PartitionedTable)3 Column (com.facebook.presto.client.Column)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)2 PartitionedTableToTable (com.airbnb.airpal.presto.PartitionedTable.PartitionedTableToTable)1 JobTableOutputJoinRow (com.airbnb.airpal.sql.beans.JobTableOutputJoinRow)1 QueryError (com.facebook.presto.client.QueryError)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 UUID (java.util.UUID)1 Produces (javax.ws.rs.Produces)1 Handle (org.skife.jdbi.v2.Handle)1