use of co.cask.cdap.proto.QueryHandle in project cdap by caskdata.
the class ExploreExecutorHttpHandler method enableStream.
@POST
@Path("streams/{stream}/tables/{table}/enable")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void enableStream(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespace, @PathParam("stream") String streamName, @PathParam("table") final String tableName) throws Exception {
final StreamId streamId = new StreamId(namespace, streamName);
try (Reader reader = new InputStreamReader(new ChannelBufferInputStream(request.getContent()))) {
final FormatSpecification format = GSON.fromJson(reader, FormatSpecification.class);
if (format == null) {
throw new BadRequestException("Expected format in the body");
}
QueryHandle handle = impersonator.doAs(streamId, new Callable<QueryHandle>() {
@Override
public QueryHandle call() throws Exception {
return exploreTableManager.enableStream(tableName, streamId, format);
}
});
JsonObject json = new JsonObject();
json.addProperty("handle", handle.getHandle());
responder.sendJson(HttpResponseStatus.OK, json);
} catch (UnsupportedTypeException e) {
LOG.error("Exception while generating create statement for stream {}", streamName, e);
responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
}
}
use of co.cask.cdap.proto.QueryHandle in project cdap by caskdata.
the class ExploreExecutorHttpHandler method doDropPartition.
private void doDropPartition(HttpRequest request, HttpResponder responder, DatasetId datasetId) {
Dataset dataset;
try (SystemDatasetInstantiator datasetInstantiator = datasetInstantiatorFactory.create()) {
dataset = datasetInstantiator.getDataset(datasetId);
if (dataset == null) {
responder.sendString(HttpResponseStatus.NOT_FOUND, "Cannot load dataset " + datasetId);
return;
}
} catch (IOException e) {
String classNotFoundMessage = isClassNotFoundException(e);
if (classNotFoundMessage != null) {
JsonObject json = new JsonObject();
json.addProperty("handle", QueryHandle.NO_OP.getHandle());
responder.sendJson(HttpResponseStatus.OK, json);
return;
}
LOG.error("Exception instantiating dataset {}.", datasetId, e);
responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Exception instantiating dataset " + datasetId);
return;
}
try {
if (!(dataset instanceof PartitionedFileSet)) {
responder.sendString(HttpResponseStatus.BAD_REQUEST, "not a partitioned dataset.");
return;
}
Partitioning partitioning = ((PartitionedFileSet) dataset).getPartitioning();
Reader reader = new InputStreamReader(new ChannelBufferInputStream(request.getContent()));
Map<String, String> properties = GSON.fromJson(reader, new TypeToken<Map<String, String>>() {
}.getType());
PartitionKey partitionKey;
try {
partitionKey = PartitionedFileSetArguments.getOutputPartitionKey(properties, partitioning);
} catch (Exception e) {
responder.sendString(HttpResponseStatus.BAD_REQUEST, "invalid partition key: " + e.getMessage());
return;
}
if (partitionKey == null) {
responder.sendString(HttpResponseStatus.BAD_REQUEST, "no partition key was given.");
return;
}
QueryHandle handle = exploreTableManager.dropPartition(datasetId, properties, partitionKey);
JsonObject json = new JsonObject();
json.addProperty("handle", handle.getHandle());
responder.sendJson(HttpResponseStatus.OK, json);
} catch (Throwable e) {
LOG.error("Got exception:", e);
responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage());
}
}
use of co.cask.cdap.proto.QueryHandle in project cdap by caskdata.
the class HiveExploreServiceTimeoutTest method testTimeoutFetchAllResults.
@Test
public void testTimeoutFetchAllResults() throws Exception {
Set<Long> beforeTxns = transactionManager.getCurrentState().getInProgress().keySet();
QueryHandle handle = exploreService.execute(NAMESPACE_ID, "select key, value from " + MY_TABLE_NAME);
Set<Long> queryTxns = Sets.difference(transactionManager.getCurrentState().getInProgress().keySet(), beforeTxns);
Assert.assertFalse(queryTxns.isEmpty());
QueryStatus status = waitForCompletionStatus(handle, 200, TimeUnit.MILLISECONDS, 20);
Assert.assertEquals(QueryStatus.OpStatus.FINISHED, status.getStatus());
Assert.assertTrue(status.hasResults());
List<ColumnDesc> schema = exploreService.getResultSchema(handle);
// noinspection StatementWithEmptyBody
while (!exploreService.nextResults(handle, 100).isEmpty()) {
// nothing to do
}
// Sleep for some time for txn to get closed
TimeUnit.SECONDS.sleep(1);
// Make sure that the transaction got closed
Assert.assertEquals(ImmutableSet.<Long>of(), Sets.intersection(queryTxns, transactionManager.getCurrentState().getInProgress().keySet()).immutableCopy());
// Check if calls using inactive handle still work
Assert.assertEquals(status, exploreService.getStatus(handle));
Assert.assertEquals(schema, exploreService.getResultSchema(handle));
exploreService.close(handle);
// Sleep for timeout to happen
TimeUnit.SECONDS.sleep(INACTIVE_OPERATION_TIMEOUT_SECS + 3);
try {
exploreService.getStatus(handle);
Assert.fail("Should throw HandleNotFoundException due to operation cleanup");
} catch (HandleNotFoundException e) {
// Expected exception due to timeout
}
}
use of co.cask.cdap.proto.QueryHandle in project cdap by caskdata.
the class HiveExploreServiceTimeoutTest method testCloseQuery.
@Test
public void testCloseQuery() throws Exception {
QueryHandle handle = exploreService.execute(NAMESPACE_ID, "drop table if exists not_existing_table_name");
exploreService.close(handle);
try {
exploreService.getStatus(handle);
Assert.fail("Should throw HandleNotFoundException due to operation cleanup");
} catch (HandleNotFoundException e) {
// Expected exception due to timeout
}
}
use of co.cask.cdap.proto.QueryHandle in project cdap by caskdata.
the class HiveExploreServiceTimeoutTest method testTimeoutNoResults.
@Test
public void testTimeoutNoResults() throws Exception {
Set<Long> beforeTxns = transactionManager.getCurrentState().getInProgress().keySet();
QueryHandle handle = exploreService.execute(NAMESPACE_ID, "drop table if exists not_existing_table_name");
Set<Long> queryTxns = Sets.difference(transactionManager.getCurrentState().getInProgress().keySet(), beforeTxns);
Assert.assertFalse(queryTxns.isEmpty());
QueryStatus status = waitForCompletionStatus(handle, 200, TimeUnit.MILLISECONDS, 20);
Assert.assertEquals(QueryStatus.OpStatus.FINISHED, status.getStatus());
Assert.assertFalse(status.hasResults());
List<ColumnDesc> schema = exploreService.getResultSchema(handle);
// Sleep for some time for txn to get closed
TimeUnit.SECONDS.sleep(1);
// Make sure that the transaction got closed
Assert.assertEquals(ImmutableSet.<Long>of(), Sets.intersection(queryTxns, transactionManager.getCurrentState().getInProgress().keySet()).immutableCopy());
// Check if calls using inactive handle still work
Assert.assertEquals(status, exploreService.getStatus(handle));
Assert.assertEquals(schema, exploreService.getResultSchema(handle));
exploreService.close(handle);
// Sleep for timeout to happen
TimeUnit.SECONDS.sleep(INACTIVE_OPERATION_TIMEOUT_SECS + 3);
try {
exploreService.getStatus(handle);
Assert.fail("Should throw HandleNotFoundException due to operation cleanup");
} catch (HandleNotFoundException e) {
// Expected exception due to timeout
}
}
Aggregations