use of com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION 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.dictionary.EntityDictionary.NO_VERSION in project elide by yahoo.
the class SQLUnitTest method init.
public static void init(SQLDialect sqlDialect, Set<Optimizer> optimizers, MetaDataStore metaDataStore) {
Properties properties = new Properties();
properties.put("driverClassName", "org.h2.Driver");
String jdbcUrl = "jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1" + ";NON_KEYWORDS=VALUE,USER" + ";DATABASE_TO_UPPER=FALSE" + getCompatabilityMode(sqlDialect.getDialectType());
properties.put("jdbcUrl", jdbcUrl);
HikariConfig config = new HikariConfig(properties);
DataSource dataSource = new HikariDataSource(config);
try (Connection h2Conn = dataSource.getConnection()) {
h2Conn.createStatement().execute("RUNSCRIPT FROM 'classpath:prepare_tables.sql'");
} catch (SQLException e) {
((HikariDataSource) dataSource).close();
throw new IllegalStateException(e);
}
SQLUnitTest.metaDataStore = metaDataStore;
dictionary = EntityDictionary.builder().build();
dictionary.bindEntity(PlayerStatsWithView.class);
dictionary.bindEntity(PlayerStatsView.class);
dictionary.bindEntity(PlayerStats.class);
dictionary.bindEntity(Country.class);
dictionary.bindEntity(SubCountry.class);
dictionary.bindEntity(Player.class);
dictionary.bindEntity(CountryView.class);
dictionary.bindEntity(CountryViewNested.class);
dictionary.bindEntity(Continent.class);
dictionary.bindEntity(GameRevenue.class);
filterParser = RSQLFilterDialect.builder().dictionary(dictionary).build();
// Manually register the serdes because we are not running a complete Elide service.
CoerceUtil.register(Day.class, new Day.DaySerde());
CoerceUtil.register(Hour.class, new Hour.HourSerde());
CoerceUtil.register(ISOWeek.class, new ISOWeek.ISOWeekSerde());
CoerceUtil.register(Minute.class, new Minute.MinuteSerde());
CoerceUtil.register(Month.class, new Month.MonthSerde());
CoerceUtil.register(Quarter.class, new Quarter.QuarterSerde());
CoerceUtil.register(Second.class, new Second.SecondSerde());
CoerceUtil.register(Week.class, new Week.WeekSerde());
CoerceUtil.register(Year.class, new Year.YearSerde());
metaDataStore.populateEntityDictionary(dictionary);
// Need to provide details for connections used by all available models.
Map<String, ConnectionDetails> connectionDetailsMap = new HashMap<>();
connectionDetailsMap.put("mycon", new ConnectionDetails(dataSource, sqlDialect));
connectionDetailsMap.put("SalesDBConnection", new ConnectionDetails(DUMMY_DATASOURCE, sqlDialect));
Function<String, ConnectionDetails> connectionLookup = (name) -> connectionDetailsMap.getOrDefault(name, new ConnectionDetails(dataSource, sqlDialect));
engine = new SQLQueryEngine(metaDataStore, connectionLookup, optimizers, new DefaultQueryPlanMerger(metaDataStore), new DefaultQueryValidator(metaDataStore.getMetadataDictionary()));
playerStatsTable = (SQLTable) metaDataStore.getTable("playerStats", NO_VERSION);
videoGameTable = (SQLTable) metaDataStore.getTable("videoGame", NO_VERSION);
playerStatsViewTable = (SQLTable) metaDataStore.getTable("playerStatsView", NO_VERSION);
playerStatsViewTableArgs = new HashMap<>();
playerStatsViewTableArgs.put("rating", Argument.builder().name("overallRating").value("Great").build());
playerStatsViewTableArgs.put("minScore", Argument.builder().name("minScore").value("0").build());
}
use of com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION in project elide by yahoo.
the class SwaggerBuilderTest method testSortParameter.
@Test
public void testSortParameter() {
@Entity
@Include
class NothingToSort {
@Id
long name;
}
EntityDictionary entityDictionary = EntityDictionary.builder().build();
entityDictionary.bindEntity(NothingToSort.class);
Info info = new Info().title("Test Service").version(NO_VERSION);
SwaggerBuilder builder = new SwaggerBuilder(entityDictionary, info);
Swagger testSwagger = builder.build();
List<Parameter> params = testSwagger.getPaths().get("/nothingToSort").getGet().getParameters();
QueryParameter sortParam = (QueryParameter) params.stream().filter((param) -> param.getName().equals("sort")).findFirst().get();
assertEquals("query", sortParam.getIn());
List<String> sortValues = Arrays.asList("id", "-id");
assertEquals(sortValues, ((StringProperty) sortParam.getItems()).getEnum());
}
use of com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION in project elide by yahoo.
the class SwaggerBuilder method build.
/**
* Builds a swagger object.
* @return the constructed 'Swagger' object
*/
public Swagger build() {
/* Used to convert Elide POJOs into Swagger Model objects */
ModelConverters converters = ModelConverters.getInstance();
ModelConverter converter = new JsonApiModelResolver(dictionary);
converters.addConverter(converter);
String apiVersion = swagger.getInfo().getVersion();
if (apiVersion == null) {
apiVersion = NO_VERSION;
}
if (allClasses.isEmpty()) {
allClasses = dictionary.getBoundClassesByVersion(apiVersion);
} else {
allClasses = Sets.intersection(dictionary.getBoundClassesByVersion(apiVersion), allClasses);
if (allClasses.isEmpty()) {
throw new IllegalArgumentException("None of the provided classes are exported by Elide");
}
}
/*
* Create a Model for each Elide entity.
* Elide entity could be of ClassType or DynamicType.
* For ClassType, extract the class and pass it to ModelConverters#readAll method.
* ModelConverters#readAll doesn't support Elide Dynamic Type, so calling the
* JsonApiModelResolver#resolve method directly when its not a ClassType.
*/
Map<String, Model> models = new HashMap<>();
for (Type<?> clazz : allClasses) {
if (clazz instanceof ClassType) {
models.putAll(converters.readAll(((ClassType) clazz).getCls()));
} else {
ModelConverterContextImpl context = new ModelConverterContextImpl(Arrays.asList(converter));
context.resolve(clazz);
models.putAll(context.getDefinedModels());
}
}
swagger.setDefinitions(models);
rootClasses = allClasses.stream().filter(dictionary::isRoot).collect(Collectors.toSet());
/* Find all the paths starting from the root entities. */
Set<PathMetaData> pathData = rootClasses.stream().map(this::find).flatMap(Collection::stream).collect(Collectors.toSet());
/* Prune the discovered paths to remove redundant elements */
Set<PathMetaData> toRemove = new HashSet<>();
pathData.stream().collect(Collectors.groupingBy(PathMetaData::getRootType)).values().forEach(pathSet -> {
for (PathMetaData path : pathSet) {
for (PathMetaData compare : pathSet) {
/*
* We don't prune paths that are redundant with root collections to allow both BOTH
* root collection urls as well as relationship urls.
*/
if (compare.lineage.isEmpty() || path == compare) {
continue;
}
if (compare.shorterThan(path)) {
toRemove.add(path);
break;
}
}
}
});
pathData = Sets.difference(pathData, toRemove);
/* Each path constructs 3 URLs (collection, instance, and relationship) */
for (PathMetaData pathDatum : pathData) {
swagger.path(pathDatum.getCollectionUrl(), pathDatum.getCollectionPath());
swagger.path(pathDatum.getUrl(), pathDatum.getInstancePath());
/* We only construct relationship URLs if the entity is not a root collection */
if (!pathDatum.lineage.isEmpty()) {
swagger.path(pathDatum.getRelationshipUrl(), pathDatum.getRelationshipPath());
}
}
/* We create Swagger 'tags' for each entity so Swagger UI organizes the paths by entities */
List<Tag> tags = allClasses.stream().map((clazz) -> dictionary.getJsonAliasFor(clazz)).map((alias) -> new Tag().name(alias)).collect(Collectors.toList());
swagger.tags(tags);
return swagger;
}
Aggregations