Search in sources :

Example 1 with TransactionRegistry

use of com.yahoo.elide.core.TransactionRegistry 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());
    }
}
Also used : TransactionRegistry(com.yahoo.elide.core.TransactionRegistry) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Date(java.util.Date) AsyncAPIDAO(com.yahoo.elide.async.service.dao.AsyncAPIDAO) Map(java.util.Map) NO_VERSION(com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION) StreamSupport(java.util.stream.StreamSupport) AsyncAPI(com.yahoo.elide.async.models.AsyncAPI) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) QueryStatus(com.yahoo.elide.async.models.QueryStatus) RequestScope(com.yahoo.elide.core.RequestScope) Elide(com.yahoo.elide.Elide) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) AsyncQuery(com.yahoo.elide.async.models.AsyncQuery) Set(java.util.Set) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Sets(com.google.common.collect.Sets) TimeUnit(java.util.concurrent.TimeUnit) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Slf4j(lombok.extern.slf4j.Slf4j) Data(lombok.Data) AllArgsConstructor(lombok.AllArgsConstructor) PathElement(com.yahoo.elide.core.Path.PathElement) Collections(java.util.Collections) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) TransactionRegistry(com.yahoo.elide.core.TransactionRegistry) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) RequestScope(com.yahoo.elide.core.RequestScope) Date(java.util.Date) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) PathElement(com.yahoo.elide.core.Path.PathElement) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) UUID(java.util.UUID) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression)

Example 2 with TransactionRegistry

use of com.yahoo.elide.core.TransactionRegistry in project elide by yahoo.

the class IntegrationTestSetup method initializeElide.

// Initialize beans here if needed.
// We recreate the Elide bean here without @RefreshScope so that it can be used With @SpyBean.
@Bean
public RefreshableElide initializeElide(EntityDictionary dictionary, DataStore dataStore, ElideConfigProperties settings, JsonApiMapper mapper, ErrorMapper errorMapper) {
    ElideSettingsBuilder builder = new ElideSettingsBuilder(dataStore).withEntityDictionary(dictionary).withErrorMapper(errorMapper).withJsonApiMapper(mapper).withDefaultMaxPageSize(settings.getMaxPageSize()).withDefaultPageSize(settings.getPageSize()).withJoinFilterDialect(RSQLFilterDialect.builder().dictionary(dictionary).build()).withSubqueryFilterDialect(RSQLFilterDialect.builder().dictionary(dictionary).build()).withAuditLogger(new Slf4jLogger()).withBaseUrl(settings.getBaseUrl()).withISO8601Dates("yyyy-MM-dd'T'HH:mm'Z'", TimeZone.getTimeZone("UTC")).withJsonApiPath(settings.getJsonApi().getPath()).withGraphQLApiPath(settings.getGraphql().getPath());
    if (settings.isVerboseErrors()) {
        builder.withVerboseErrors();
    }
    if (settings.getAsync() != null && settings.getAsync().getExport() != null && settings.getAsync().getExport().isEnabled()) {
        builder.withExportApiPath(settings.getAsync().getExport().getPath());
    }
    if (settings.getJsonApi() != null && settings.getJsonApi().isEnabled() && settings.getJsonApi().isEnableLinks()) {
        String baseUrl = settings.getBaseUrl();
        if (StringUtils.isEmpty(baseUrl)) {
            builder.withJSONApiLinks(new DefaultJSONApiLinks());
        } else {
            String jsonApiBaseUrl = baseUrl + settings.getJsonApi().getPath() + "/";
            builder.withJSONApiLinks(new DefaultJSONApiLinks(jsonApiBaseUrl));
        }
    }
    Elide elide = new Elide(builder.build(), new TransactionRegistry(), dictionary.getScanner(), true);
    return new RefreshableElide(spy(elide));
}
Also used : ElideSettingsBuilder(com.yahoo.elide.ElideSettingsBuilder) Slf4jLogger(com.yahoo.elide.core.audit.Slf4jLogger) DefaultJSONApiLinks(com.yahoo.elide.jsonapi.links.DefaultJSONApiLinks) TransactionRegistry(com.yahoo.elide.core.TransactionRegistry) RefreshableElide(com.yahoo.elide.RefreshableElide) Elide(com.yahoo.elide.Elide) RefreshableElide(com.yahoo.elide.RefreshableElide) Bean(org.springframework.context.annotation.Bean)

Aggregations

Elide (com.yahoo.elide.Elide)2 TransactionRegistry (com.yahoo.elide.core.TransactionRegistry)2 Sets (com.google.common.collect.Sets)1 ElideSettingsBuilder (com.yahoo.elide.ElideSettingsBuilder)1 RefreshableElide (com.yahoo.elide.RefreshableElide)1 AsyncAPI (com.yahoo.elide.async.models.AsyncAPI)1 AsyncQuery (com.yahoo.elide.async.models.AsyncQuery)1 QueryStatus (com.yahoo.elide.async.models.QueryStatus)1 AsyncAPIDAO (com.yahoo.elide.async.service.dao.AsyncAPIDAO)1 PathElement (com.yahoo.elide.core.Path.PathElement)1 RequestScope (com.yahoo.elide.core.RequestScope)1 Slf4jLogger (com.yahoo.elide.core.audit.Slf4jLogger)1 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)1 NO_VERSION (com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION)1 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)1 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)1 DefaultJSONApiLinks (com.yahoo.elide.jsonapi.links.DefaultJSONApiLinks)1 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)1 Collections (java.util.Collections)1 Date (java.util.Date)1