use of com.yahoo.elide.core.RequestScope in project elide by yahoo.
the class AsyncAPICancelRunnable method cancelAsyncAPI.
/**
* This method cancels queries based on threshold.
* @param type AsyncAPI Type Implementation.
*/
protected <T extends AsyncAPI> void cancelAsyncAPI(Class<T> type) {
try {
TransactionRegistry transactionRegistry = elide.getTransactionRegistry();
Map<UUID, DataStoreTransaction> runningTransactionMap = transactionRegistry.getRunningTransactions();
// Running transaction UUIDs
Set<UUID> runningTransactionUUIDs = runningTransactionMap.keySet();
// Construct filter expression
PathElement statusPathElement = new PathElement(type, QueryStatus.class, "status");
FilterExpression fltStatusExpression = new InPredicate(statusPathElement, QueryStatus.CANCELLED, QueryStatus.PROCESSING, QueryStatus.QUEUED);
Iterable<T> asyncAPIIterable = asyncAPIDao.loadAsyncAPIByFilter(fltStatusExpression, type);
// Active AsyncAPI UUIDs
Set<UUID> asyncTransactionUUIDs = StreamSupport.stream(asyncAPIIterable.spliterator(), false).filter(query -> query.getStatus() == QueryStatus.CANCELLED || TimeUnit.SECONDS.convert(Math.abs(new Date(System.currentTimeMillis()).getTime() - query.getCreatedOn().getTime()), TimeUnit.MILLISECONDS) > maxRunTimeSeconds).map(query -> UUID.fromString(query.getRequestId())).collect(Collectors.toSet());
// AsyncAPI UUIDs that have active transactions
Set<UUID> queryUUIDsToCancel = Sets.intersection(runningTransactionUUIDs, asyncTransactionUUIDs);
// AsyncAPI IDs that need to be cancelled
Set<String> queryIDsToCancel = queryUUIDsToCancel.stream().map(uuid -> StreamSupport.stream(asyncAPIIterable.spliterator(), false).filter(query -> query.getRequestId().equals(uuid.toString())).map(T::getId).findFirst().orElseThrow(IllegalStateException::new)).collect(Collectors.toSet());
// Cancel Transactions
queryUUIDsToCancel.stream().forEach((uuid) -> {
DataStoreTransaction runningTransaction = transactionRegistry.getRunningTransaction(uuid);
if (runningTransaction != null) {
JsonApiDocument jsonApiDoc = new JsonApiDocument();
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
RequestScope scope = new RequestScope("", "query", NO_VERSION, jsonApiDoc, runningTransaction, null, queryParams, Collections.emptyMap(), uuid, elide.getElideSettings());
runningTransaction.cancel(scope);
}
});
// Change queryStatus for cancelled queries
if (!queryIDsToCancel.isEmpty()) {
PathElement idPathElement = new PathElement(type, String.class, "id");
FilterExpression fltIdExpression = new InPredicate(idPathElement, queryIDsToCancel);
asyncAPIDao.updateStatusAsyncAPIByFilter(fltIdExpression, QueryStatus.CANCEL_COMPLETE, type);
}
} catch (Exception e) {
log.error("Exception in scheduled cancellation: {}", e.toString());
}
}
use of com.yahoo.elide.core.RequestScope in project elide by yahoo.
the class DefaultAsyncAPIDAO method executeInTransaction.
/**
* This method creates a transaction from the datastore, performs the DB action using
* a generic functional interface and closes the transaction.
* @param dataStore Elide datastore retrieved from Elide object
* @param action Functional interface to perform DB action
* @return Object Returns Entity Object (AsyncAPIResult or AsyncResult)
*/
protected Object executeInTransaction(DataStore dataStore, Transactional action) {
log.debug("executeInTransaction");
Object result = null;
try (DataStoreTransaction tx = dataStore.beginTransaction()) {
JsonApiDocument jsonApiDoc = new JsonApiDocument();
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
RequestScope scope = new RequestScope("", "query", NO_VERSION, jsonApiDoc, tx, null, queryParams, Collections.emptyMap(), UUID.randomUUID(), elideSettings);
result = action.execute(tx, scope);
tx.flush(scope);
tx.commit(scope);
} catch (IOException e) {
log.error("IOException: {}", e.toString());
throw new IllegalStateException(e);
}
return result;
}
use of com.yahoo.elide.core.RequestScope in project elide by yahoo.
the class GraphQLTableExportOperationTest method dataPrep.
/**
* Prepping and Storing an TableExport entry to be queried later on.
* @throws IOException IOException
*/
private void dataPrep() throws IOException {
TableExport temp = new TableExport();
DataStoreTransaction tx = dataStore.beginTransaction();
RequestScope scope = new RequestScope(null, null, NO_VERSION, null, tx, user, null, Collections.emptyMap(), UUID.randomUUID(), elide.getElideSettings());
tx.save(temp, scope);
tx.commit(scope);
tx.close();
}
use of com.yahoo.elide.core.RequestScope in project elide by yahoo.
the class JsonAPITableExportOperationTest method dataPrep.
/**
* Prepping and Storing an TableExport entry to be queried later on.
* @throws IOException IOException
*/
private void dataPrep() throws IOException {
TableExport temp = new TableExport();
DataStoreTransaction tx = dataStore.beginTransaction();
RequestScope scope = new RequestScope(null, null, NO_VERSION, null, tx, user, null, Collections.emptyMap(), UUID.randomUUID(), elide.getElideSettings());
tx.save(temp, scope);
tx.commit(scope);
tx.close();
}
use of com.yahoo.elide.core.RequestScope in project elide by yahoo.
the class JMSDataStoreTest method testLoadObjects.
@Test
public void testLoadObjects() throws Exception {
Author author1 = new Author();
author1.setId(1);
author1.setName("Jon Doe");
Book book1 = new Book();
book1.setTitle("Enders Game");
book1.setId(1);
book1.setAuthors(Sets.newHashSet(author1));
Book book2 = new Book();
book2.setTitle("Grapes of Wrath");
book2.setId(2);
try (DataStoreTransaction tx = store.beginReadTransaction()) {
RequestScope scope = new RequestScope("/json", "/", NO_VERSION, null, tx, null, null, Collections.EMPTY_MAP, UUID.randomUUID(), new ElideSettingsBuilder(store).withEntityDictionary(dictionary).build());
Iterable<Book> books = tx.loadObjects(EntityProjection.builder().argument(Argument.builder().name("topic").value(TopicType.ADDED).build()).type(Book.class).build(), scope);
JMSContext context = connectionFactory.createContext();
Destination destination = context.createTopic("bookAdded");
JMSProducer producer = context.createProducer();
ObjectMapper mapper = new ObjectMapper();
producer.send(destination, mapper.writeValueAsString(book1));
producer.send(destination, mapper.writeValueAsString(book2));
Iterator<Book> booksIterator = books.iterator();
assertTrue(booksIterator.hasNext());
Book receivedBook = booksIterator.next();
assertEquals("Enders Game", receivedBook.getTitle());
assertEquals(1, receivedBook.getId());
Set<Author> receivedAuthors = Sets.newHashSet((Iterable) tx.getToManyRelation(tx, receivedBook, Relationship.builder().name("authors").projection(EntityProjection.builder().type(Author.class).build()).build(), scope));
assertTrue(receivedAuthors.contains(author1));
assertTrue(booksIterator.hasNext());
receivedBook = booksIterator.next();
assertEquals("Grapes of Wrath", receivedBook.getTitle());
assertEquals(2, receivedBook.getId());
assertFalse(booksIterator.hasNext());
}
}
Aggregations