use of org.apache.hadoop.hive.metastore.api.ScheduledQueryPollResponse in project hive by apache.
the class TestMetastoreScheduledQueries method testExclusivePoll.
@Test
public void testExclusivePoll() throws Exception {
try {
ObjectStoreTestHook.instance = new ObjectStoreTestHook() {
@Override
public void scheduledQueryPoll() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
};
ScheduledQuery schq = createScheduledQuery(new ScheduledQueryKey("q1", "exclusive"));
ScheduledQueryMaintenanceRequest r = new ScheduledQueryMaintenanceRequest();
r.setType(ScheduledQueryMaintenanceRequestType.CREATE);
r.setScheduledQuery(schq);
client.scheduledQueryMaintenance(r);
// wait 1 sec for next execution
Thread.sleep(1000);
ExecutorService pool = Executors.newCachedThreadPool();
Future<ScheduledQueryPollResponse> f1 = pool.submit(new AsyncPollCall("exclusive"));
Future<ScheduledQueryPollResponse> f2 = pool.submit(new AsyncPollCall("exclusive"));
ScheduledQueryPollResponse resp1 = f1.get();
ScheduledQueryPollResponse resp2 = f2.get();
assertTrue(resp1.isSetQuery() ^ resp2.isSetQuery());
pool.shutdown();
} finally {
ObjectStoreTestHook.instance = null;
}
}
use of org.apache.hadoop.hive.metastore.api.ScheduledQueryPollResponse in project hive by apache.
the class ObjectStore method scheduledQueryPoll.
@Override
public ScheduledQueryPollResponse scheduledQueryPoll(ScheduledQueryPollRequest request) throws MetaException {
ensureScheduledQueriesEnabled();
boolean commited = false;
ScheduledQueryPollResponse ret = new ScheduledQueryPollResponse();
Query q = null;
try {
openTransaction();
q = pm.newQuery(MScheduledQuery.class, "nextExecution <= now && enabled && clusterNamespace == ns && activeExecution == null");
q.setSerializeRead(true);
q.declareParameters("java.lang.Integer now, java.lang.String ns");
q.setOrdering("nextExecution");
int now = (int) (System.currentTimeMillis() / 1000);
List<MScheduledQuery> results = (List<MScheduledQuery>) q.execute(now, request.getClusterNamespace());
if (results == null || results.isEmpty()) {
return new ScheduledQueryPollResponse();
}
MScheduledQuery schq = results.get(0);
schq.setNextExecution(computeNextExecutionTime(schq.getSchedule()));
MScheduledExecution execution = new MScheduledExecution();
execution.setScheduledQuery(schq);
execution.setState(QueryState.INITED);
execution.setStartTime(now);
execution.setLastUpdateTime(now);
schq.setActiveExecution(execution);
pm.makePersistent(execution);
pm.makePersistent(schq);
ObjectStoreTestHook.onScheduledQueryPoll();
commited = commitTransaction();
ret.setScheduleKey(schq.getScheduleKey());
ret.setQuery("/* schedule: " + schq.getScheduleName() + " */" + schq.getQuery());
ret.setUser(schq.getUser());
int executionId = ((IntIdentity) pm.getObjectId(execution)).getKey();
ret.setExecutionId(executionId);
} catch (JDOException e) {
LOG.debug("Caught jdo exception; exclusive", e);
commited = false;
} finally {
rollbackAndCleanup(commited, q);
return commited ? ret : new ScheduledQueryPollResponse();
}
}
Aggregations