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