Search in sources :

Example 1 with Query

use of ai.grakn.graql.Query in project grakn by graknlabs.

the class GraknClientFake method whenQueriesFail_TheBatchExecutorClientStillCompletes.

@Test
public void whenQueriesFail_TheBatchExecutorClientStillCompletes() throws GraknClientException {
    Keyspace keyspace = Keyspace.of("yes");
    GraknClientFake graknClient = new GraknClientFake();
    graknClient.shouldThrow(new GraknClientException("UH OH"));
    // Make sure there are more queries to execute than are allowed to run at once
    int maxQueries = 10;
    int numQueries = 100;
    BatchExecutorClient.Builder clientBuilder = BatchExecutorClient.newBuilder().taskClient(graknClient).maxQueries(maxQueries);
    Set<Query<?>> queriesToExecute = IntStream.range(0, numQueries).mapToObj(this::createInsertQuery).collect(toImmutableSet());
    try (BatchExecutorClient client = clientBuilder.build()) {
        for (Query<?> query : queriesToExecute) {
            // If we don't subscribe, the query won't execute
            client.add(query, keyspace).subscribe(a -> {
            });
        }
    }
    assertThat(graknClient.queriesExecuted(), containsInAnyOrder(queriesToExecute.toArray()));
}
Also used : InsertQuery(ai.grakn.graql.InsertQuery) Query(ai.grakn.graql.Query) Keyspace(ai.grakn.Keyspace) BatchExecutorClient(ai.grakn.client.BatchExecutorClient) GraknClientException(ai.grakn.client.GraknClientException) Test(org.junit.Test)

Example 2 with Query

use of ai.grakn.graql.Query in project grakn by graknlabs.

the class GraqlControllerTest method whenRunningMultiIdempotentInsertQuery_JsonResponseIsTheSameAsJavaGraql.

@Test
public void whenRunningMultiIdempotentInsertQuery_JsonResponseIsTheSameAsJavaGraql() {
    String single = "insert $x label movie;";
    String queryString = single + "\n" + single;
    Response resp = sendQuery(queryString, APPLICATION_JSON, true, sampleKB.tx().keyspace().getValue(), true);
    resp.then().statusCode(200);
    Stream<Query<?>> query = sampleKB.tx().graql().parser().parseList(queryString);
    String graqlResult = printer.graqlString(query.map(Query::execute).collect(Collectors.toList()));
    Json expected = Json.read(graqlResult);
    assertEquals(expected, Json.read(resp.body().asString()));
}
Also used : Response(com.jayway.restassured.response.Response) Query(ai.grakn.graql.Query) Json(mjson.Json) Test(org.junit.Test)

Example 3 with Query

use of ai.grakn.graql.Query in project grakn by graknlabs.

the class BatchExecutorClient method add.

/**
 * Will block until there is space for the query to be submitted
 */
public Observable<QueryResponse> add(Query<?> query, Keyspace keyspace, boolean keepErrors) {
    QueryRequest queryRequest = new QueryRequest(query);
    queryRequest.acquirePermit();
    Context contextAddTimer = addTimer.time();
    Observable<QueryResponse> observable = new QueriesObservableCollapser(queryRequest, keyspace).observe().doOnError(error -> failureMeter.mark()).doOnEach(a -> {
        if (a.getThrowable() != null) {
            LOG.error("Error while executing statement", a.getThrowable());
        } else if (a.isOnNext()) {
            LOG.trace("Executed {}", a.getValue());
        }
    }).subscribeOn(scheduler).doOnTerminate(contextAddTimer::close);
    return keepErrors ? observable : ignoreErrors(observable);
}
Also used : Context(com.codahale.metrics.Timer.Context) HystrixRequestContext(com.netflix.hystrix.strategy.concurrency.HystrixRequestContext) HystrixCollapserKey(com.netflix.hystrix.HystrixCollapserKey) Retryer(com.github.rholder.retry.Retryer) RetryerBuilder(com.github.rholder.retry.RetryerBuilder) HystrixCommandProperties(com.netflix.hystrix.HystrixCommandProperties) Keyspace(ai.grakn.Keyspace) RetryException(com.github.rholder.retry.RetryException) LoggerFactory(org.slf4j.LoggerFactory) HystrixThreadPoolProperties(com.netflix.hystrix.HystrixThreadPoolProperties) Observable(rx.Observable) Meter(com.codahale.metrics.Meter) HystrixCollapserProperties(com.netflix.hystrix.HystrixCollapserProperties) Schedulers(rx.schedulers.Schedulers) Context(com.codahale.metrics.Timer.Context) ConnectException(java.net.ConnectException) ExecutorService(java.util.concurrent.ExecutorService) WaitStrategies(com.github.rholder.retry.WaitStrategies) HystrixCollapser(com.netflix.hystrix.HystrixCollapser) HystrixCommandGroupKey(com.netflix.hystrix.HystrixCommandGroupKey) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) Semaphore(java.util.concurrent.Semaphore) Collection(java.util.Collection) Attempt(com.github.rholder.retry.Attempt) API(ai.grakn.API) UUID(java.util.UUID) Scheduler(rx.Scheduler) RetryListener(com.github.rholder.retry.RetryListener) Collectors(java.util.stream.Collectors) Query(ai.grakn.graql.Query) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) HystrixRequestContext(com.netflix.hystrix.strategy.concurrency.HystrixRequestContext) HystrixCommand(com.netflix.hystrix.HystrixCommand) List(java.util.List) StopStrategies(com.github.rholder.retry.StopStrategies) Closeable(java.io.Closeable) Timer(com.codahale.metrics.Timer) Optional(java.util.Optional) MetricRegistry.name(com.codahale.metrics.MetricRegistry.name) SimpleURI(ai.grakn.util.SimpleURI)

Example 4 with Query

use of ai.grakn.graql.Query in project grakn by graknlabs.

the class GraknClientImpl method graqlExecute.

@Override
public List<QueryResponse> graqlExecute(List<Query<?>> queryList, Keyspace keyspace) throws GraknClientException {
    LOG.debug("Sending query list size {} to keyspace {}", queryList.size(), keyspace);
    String body = queryList.stream().map(Object::toString).reduce("; ", String::concat).substring(2);
    URI fullURI = UriBuilder.fromUri(uri.toURI()).path(REST.resolveTemplate(REST.WebPath.KEYSPACE_GRAQL, keyspace.getValue())).queryParam(ALLOW_MULTIPLE_QUERIES, true).queryParam(EXECUTE_WITH_INFERENCE, // Making inference true could lead to non-deterministic loading of data
    false).queryParam(LOADING_DATA, // Skip serialising responses for the sake of efficiency
    true).queryParam(TX_TYPE, GraknTxType.BATCH).build();
    ClientResponse response = client.resource(fullURI).accept(APPLICATION_JSON).post(ClientResponse.class, body);
    try {
        Response.StatusType status = response.getStatusInfo();
        String entity = response.getEntity(String.class);
        if (!status.getFamily().equals(Family.SUCCESSFUL)) {
            String queries = queryList.stream().map(Object::toString).collect(Collectors.joining("\n"));
            throw new GraknClientException("Failed graqlExecute. Error status: " + status.getStatusCode() + ", error info: " + entity + "\nqueries: " + queries, response.getStatusInfo());
        }
        LOG.debug("Received {}", status.getStatusCode());
        return queryList.stream().map(q -> QueryResponse.INSTANCE).collect(Collectors.toList());
    } finally {
        response.close();
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ClientResponse(com.sun.jersey.api.client.ClientResponse) Response(javax.ws.rs.core.Response) GraknTxType(ai.grakn.GraknTxType) Logger(org.slf4j.Logger) ALLOW_MULTIPLE_QUERIES(ai.grakn.util.REST.Request.Graql.ALLOW_MULTIPLE_QUERIES) Keyspace(ai.grakn.Keyspace) ClientResponse(com.sun.jersey.api.client.ClientResponse) LoggerFactory(org.slf4j.LoggerFactory) LOADING_DATA(ai.grakn.util.REST.Request.Graql.LOADING_DATA) APPLICATION_JSON(ai.grakn.util.REST.Response.ContentType.APPLICATION_JSON) REST(ai.grakn.util.REST) Collectors(java.util.stream.Collectors) TX_TYPE(ai.grakn.util.REST.Request.Graql.TX_TYPE) Query(ai.grakn.graql.Query) Family(javax.ws.rs.core.Response.Status.Family) List(java.util.List) Response(javax.ws.rs.core.Response) Client(com.sun.jersey.api.client.Client) Optional(java.util.Optional) UriBuilder(javax.ws.rs.core.UriBuilder) EXECUTE_WITH_INFERENCE(ai.grakn.util.REST.Request.Graql.EXECUTE_WITH_INFERENCE) URI(java.net.URI) Status(javax.ws.rs.core.Response.Status) SimpleURI(ai.grakn.util.SimpleURI) URI(java.net.URI) SimpleURI(ai.grakn.util.SimpleURI)

Example 5 with Query

use of ai.grakn.graql.Query in project grakn by graknlabs.

the class GraqlDocsTest method parse.

private void parse(GraknTx graph, String line) {
    // TODO: should `commit` be considered valid Graql? It strictly isn't.
    Matcher matcher = GRAQL_COMMIT.matcher(line);
    matcher.find();
    line = matcher.group(1);
    graph.graql().parser().parseList(line).forEach(Query::execute);
}
Also used : Query(ai.grakn.graql.Query) Matcher(java.util.regex.Matcher)

Aggregations

Query (ai.grakn.graql.Query)21 Test (org.junit.Test)15 GetQuery (ai.grakn.graql.GetQuery)13 InsertQuery (ai.grakn.graql.InsertQuery)13 DefineQuery (ai.grakn.graql.DefineQuery)12 AggregateQuery (ai.grakn.graql.AggregateQuery)11 DeleteQuery (ai.grakn.graql.DeleteQuery)11 UndefineQuery (ai.grakn.graql.UndefineQuery)11 ConnectedComponentQuery (ai.grakn.graql.analytics.ConnectedComponentQuery)11 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)11 Keyspace (ai.grakn.Keyspace)4 BatchExecutorClient (ai.grakn.client.BatchExecutorClient)3 SimpleURI (ai.grakn.util.SimpleURI)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 API (ai.grakn.API)1 GraknTx (ai.grakn.GraknTx)1