use of io.prestosql.server.protocol.Query in project hetu-core by openlookeng.
the class AutoVacuumScanner method startVacuum.
private void startVacuum(Catalog catalog, String vacuumTable, boolean isFull) {
String catalogNameVacuumTable = catalog.getCatalogName() + "." + vacuumTable;
if (vacuumInProgressMap.containsKey(catalogNameVacuumTable)) {
log.debug("return Present in vacuumInProgressMap %s ", catalogNameVacuumTable);
return;
}
long attempts = 0;
QueryId queryId = dispatchManager.createQueryId();
String slug = "x" + randomUUID().toString().toLowerCase(ENGLISH).replace("-", "");
String vacuumQuery;
if (isFull) {
vacuumQuery = "vacuum table " + catalogNameVacuumTable + " full";
} else {
vacuumQuery = "vacuum table " + catalogNameVacuumTable;
}
Session.SessionBuilder sessionBuilder = Session.builder(sessionPropertyManager).setQueryId(queryId).setIdentity(new Identity("openLooKeng", Optional.empty())).setSource("auto-vacuum");
Session session = sessionBuilder.build();
AutoVacuumSessionContext sessionContext = new AutoVacuumSessionContext(session);
vacuumInProgressMap.put(catalogNameVacuumTable, System.currentTimeMillis());
log.debug("Query.create queryId %s catalogNameVacuumTable: %s ", queryId.toString(), catalogNameVacuumTable);
ListenableFuture<?> lf = waitForDispatched(queryId, slug, sessionContext, vacuumQuery);
Futures.addCallback(lf, new FutureCallback<Object>() {
@Override
public void onSuccess(@Nullable Object result) {
try {
DispatchQuery dispatchQuery = dispatchManager.getQuery(queryId);
dispatchQuery.addStateChangeListener((state) -> {
Query query = getQuery(queryId, slug);
if ((null != query) && (!dispatchManager.getQueryInfo(queryId).getState().isDone())) {
query.waitForResults(attempts, Duration.valueOf("1s"), DataSize.valueOf("1MB"));
}
if (state.isDone()) {
log.debug("STATUS %s QueryID %s Query %s", state.name(), queryId.toString(), vacuumQuery);
vacuumInProgressMap.remove(catalogNameVacuumTable);
}
});
} catch (Throwable e) {
vacuumInProgressMap.remove(catalogNameVacuumTable);
log.error("Filed to execute vacuum for table %s QueryID %s", catalogNameVacuumTable, queryId.toString(), e.getMessage());
}
}
@Override
public void onFailure(Throwable t) {
vacuumInProgressMap.remove(catalogNameVacuumTable);
log.error("Query %s request to start vacuum scan failed at queryId[%s]: %s ", vacuumQuery, queryId, t.getMessage());
}
}, directExecutor());
}
Aggregations