use of com.google.common.util.concurrent.AbstractScheduledService.Scheduler in project incubator-rya by apache.
the class ListQueriesCommand method execute.
@Override
public void execute(final String[] args) throws ArgumentsException, ExecutionException {
requireNonNull(args);
// Parse the command line arguments.
final KafkaParameters params = new KafkaParameters();
try {
new JCommander(params, args);
} catch (final ParameterException e) {
throw new ArgumentsException("Could not list the queries because of invalid command line parameters.", e);
}
// Create the Kafka backed QueryChangeLog.
final String bootstrapServers = params.kafkaIP + ":" + params.kafkaPort;
final String topic = KafkaTopics.queryChangeLogTopic(params.ryaInstance);
final QueryChangeLog queryChangeLog = KafkaQueryChangeLogFactory.make(bootstrapServers, topic);
// The ListQueries command doesn't use the scheduled service feature.
final Scheduler scheduler = Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS);
final QueryRepository queryRepo = new InMemoryQueryRepository(queryChangeLog, scheduler);
// Execute the list queries command.
try {
final ListQueries listQueries = new DefaultListQueries(queryRepo);
try {
final Set<StreamsQuery> queries = listQueries.all();
System.out.println(formatQueries(queries));
} catch (final RyaStreamsException e) {
System.err.println("Unable to retrieve the queries.");
e.printStackTrace();
System.exit(1);
}
} catch (final Exception e) {
System.err.println("Problem encountered while closing the QueryRepository.");
e.printStackTrace();
System.exit(1);
}
}
use of com.google.common.util.concurrent.AbstractScheduledService.Scheduler in project incubator-rya by apache.
the class DeleteQueryCommand method execute.
@Override
public void execute(final String[] args) throws ArgumentsException, ExecutionException {
requireNonNull(args);
// Parse the command line arguments.
final RemoveParameters params = new RemoveParameters();
try {
new JCommander(params, args);
} catch (final ParameterException e) {
throw new ArgumentsException("Could not add a new query because of invalid command line parameters.", e);
}
// Create the Kafka backed QueryChangeLog.
final String bootstrapServers = params.kafkaIP + ":" + params.kafkaPort;
final String topic = KafkaTopics.queryChangeLogTopic(params.ryaInstance);
final QueryChangeLog queryChangeLog = KafkaQueryChangeLogFactory.make(bootstrapServers, topic);
// The DeleteQuery command doesn't use the scheduled service feature.
final Scheduler scheduler = Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS);
final QueryRepository queryRepo = new InMemoryQueryRepository(queryChangeLog, scheduler);
// Execute the delete query command.
try {
final DeleteQuery deleteQuery = new DefaultDeleteQuery(queryRepo);
try {
deleteQuery.delete(UUID.fromString(params.queryId));
System.out.println("Deleted query: " + params.queryId);
} catch (final RyaStreamsException e) {
System.err.println("Unable to delete query with ID: " + params.queryId);
e.printStackTrace();
System.exit(1);
}
} catch (final Exception e) {
System.err.println("Problem encountered while closing the QueryRepository.");
e.printStackTrace();
System.exit(1);
}
}
use of com.google.common.util.concurrent.AbstractScheduledService.Scheduler in project incubator-rya by apache.
the class QueryManagerDaemon method init.
@Override
public void init(final DaemonContext context) throws DaemonInitException, Exception {
requireNonNull(context);
// Parse the command line arguments for the configuration file to use.
final String[] args = context.getArguments();
final DaemonParameters params = new DaemonParameters();
try {
new JCommander(params).parse(args);
} catch (final ParameterException e) {
throw new DaemonInitException("Unable to parse the command line arguments.", e);
}
final Path configFile = params.config != null ? Paths.get(params.config) : DEFAULT_CONFIGURATION_PATH;
log.info("Loading the following configuration file: " + configFile);
// Unmarshall the configuration file into an object.
final QueryManagerConfig config;
try (final InputStream stream = Files.newInputStream(configFile)) {
config = QueryManagerConfigUnmarshaller.unmarshall(stream);
} catch (final JAXBException | SAXException e) {
throw new DaemonInitException("Unable to marshall the configuration XML file: " + configFile, e);
}
// Read the source polling period from the configuration.
final QueryChanngeLogDiscoveryPeriod periodConfig = config.getPerformanceTunning().getQueryChanngeLogDiscoveryPeriod();
final long period = periodConfig.getValue().longValue();
final TimeUnit units = TimeUnit.valueOf(periodConfig.getUnits().toString());
log.info("Query Change Log Polling Period: " + period + " " + units);
final Scheduler scheduler = Scheduler.newFixedRateSchedule(0, period, units);
// Initialize a QueryChangeLogSource.
final Kafka kafka = config.getQueryChangeLogSource().getKafka();
log.info("Kafka Source: " + kafka.getHostname() + ":" + kafka.getPort());
final QueryChangeLogSource source = new KafkaQueryChangeLogSource(kafka.getHostname(), kafka.getPort(), scheduler);
// Initialize a QueryExecutor.
final String zookeeperServers = config.getQueryExecutor().getLocalKafkaStreams().getZookeepers();
final KafkaStreamsFactory streamsFactory = new SingleThreadKafkaStreamsFactory(kafka.getHostname() + ":" + kafka.getPort());
final QueryExecutor queryExecutor = new LocalQueryExecutor(new CreateKafkaTopic(zookeeperServers), streamsFactory);
// Initialize the QueryManager using the configured resources.
manager = new QueryManager(queryExecutor, source, period, units);
}
use of com.google.common.util.concurrent.AbstractScheduledService.Scheduler in project incubator-rya by apache.
the class RunQueryCommand method execute.
@Override
public void execute(final String[] args) throws ArgumentsException, ExecutionException {
requireNonNull(args);
// Parse the command line arguments.
final RunParameters params = new RunParameters();
try {
new JCommander(params, args);
} catch (final ParameterException e) {
throw new ArgumentsException("Could not add a new query because of invalid command line parameters.", e);
}
// Create the Kafka backed QueryChangeLog.
final String bootstrapServers = params.kafkaIP + ":" + params.kafkaPort;
final String topic = KafkaTopics.queryChangeLogTopic(params.ryaInstance);
final QueryChangeLog queryChangeLog = KafkaQueryChangeLogFactory.make(bootstrapServers, topic);
// The RunQuery command doesn't use the scheduled service feature.
final Scheduler scheduler = Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS);
final QueryRepository queryRepo = new InMemoryQueryRepository(queryChangeLog, scheduler);
// Look up the query to be executed from the change log.
try {
try {
final UUID queryId = UUID.fromString(params.queryId);
final Optional<StreamsQuery> query = queryRepo.get(queryId);
if (!query.isPresent()) {
throw new ArgumentsException("There is no registered query for queryId " + params.queryId);
}
// Make sure the topics required by the application exists for the specified Rya instances.
final Set<String> topics = new HashSet<>();
topics.add(KafkaTopics.statementsTopic(params.ryaInstance));
topics.add(KafkaTopics.queryResultsTopic(params.ryaInstance, queryId));
KafkaTopics.createTopics(params.zookeeperServers, topics, 1, 1);
// Run the query that uses those topics.
final KafkaRunQuery runQuery = new KafkaRunQuery(params.kafkaIP, params.kafkaPort, KafkaTopics.statementsTopic(params.ryaInstance), KafkaTopics.queryResultsTopic(params.ryaInstance, queryId), queryRepo, new TopologyFactory());
runQuery.run(queryId);
} catch (final Exception e) {
throw new ExecutionException("Could not execute the Run Query command.", e);
}
} catch (final ExecutionException e) {
// Rethrow the exceptions that are advertised by execute.
throw e;
} catch (final Exception e) {
throw new ExecutionException("Problem encountered while closing the QueryRepository.", e);
}
}
use of com.google.common.util.concurrent.AbstractScheduledService.Scheduler in project incubator-rya by apache.
the class AddQueryCommand method execute.
@Override
public void execute(final String[] args) throws ArgumentsException, ExecutionException {
requireNonNull(args);
// Parse the command line arguments.
final AddParameters params = new AddParameters();
try {
new JCommander(params, args);
} catch (final ParameterException e) {
throw new ArgumentsException("Could not add a new query because of invalid command line parameters.", e);
}
// Create the Kafka backed QueryChangeLog.
final String bootstrapServers = params.kafkaIP + ":" + params.kafkaPort;
final String topic = KafkaTopics.queryChangeLogTopic(params.ryaInstance);
final QueryChangeLog queryChangeLog = KafkaQueryChangeLogFactory.make(bootstrapServers, topic);
// The AddQuery command doesn't use the scheduled service feature.
final Scheduler scheduler = Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS);
final QueryRepository queryRepo = new InMemoryQueryRepository(queryChangeLog, scheduler);
// Execute the add query command.
try {
final AddQuery addQuery = new DefaultAddQuery(queryRepo);
try {
final Boolean isActive = Boolean.parseBoolean(params.isActive);
final Boolean isInsert = Boolean.parseBoolean(params.isInsert);
// If the query's results are meant to be written back to Rya, make sure it creates statements.
if (isInsert) {
final boolean isConstructQuery = QueryInvestigator.isConstruct(params.query);
final boolean isInsertQuery = QueryInvestigator.isInsertWhere(params.query);
if (isConstructQuery) {
System.out.println("WARNING: CONSTRUCT is part of the SPARQL Query API, so they do not normally\n" + "get written back to the triple store. Consider using an INSERT, which is\n" + "part of the SPARQL Update API, in the future.");
}
if (!(isConstructQuery || isInsertQuery)) {
throw new ArgumentsException("Only CONSTRUCT queries and INSERT updates may be inserted back to the triple store.");
}
}
final StreamsQuery query = addQuery.addQuery(params.query, isActive, isInsert);
System.out.println("Added query: " + query.getSparql());
} catch (final RyaStreamsException e) {
throw new ExecutionException("Unable to add the query to Rya Streams.", e);
}
} catch (final MalformedQueryException e) {
throw new ArgumentsException("Could not parse the provided query.", e);
}
}
Aggregations