Search in sources :

Example 11 with Keyspace

use of ai.grakn.Keyspace in project grakn by graknlabs.

the class GraqlController method explainGraql.

@GET
@Path("/kb/{keyspace}/explain")
private String explainGraql(Request request, Response response) throws RetryException, ExecutionException {
    Keyspace keyspace = Keyspace.of(mandatoryPathParameter(request, KEYSPACE_PARAM));
    String queryString = mandatoryQueryParameter(request, QUERY);
    response.status(SC_OK);
    return executeFunctionWithRetrying(() -> {
        try (GraknTx tx = factory.tx(keyspace, GraknTxType.WRITE);
            Timer.Context context = executeExplanation.time()) {
            Answer answer = tx.graql().infer(true).parser().<GetQuery>parseQuery(queryString).execute().stream().findFirst().orElse(new QueryAnswer());
            return mapper.writeValueAsString(ExplanationBuilder.buildExplanation(answer));
        }
    });
}
Also used : GraknTx(ai.grakn.GraknTx) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Timer(com.codahale.metrics.Timer) GetQuery(ai.grakn.graql.GetQuery) Keyspace(ai.grakn.Keyspace) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 12 with Keyspace

use of ai.grakn.Keyspace 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 13 with Keyspace

use of ai.grakn.Keyspace 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 14 with Keyspace

use of ai.grakn.Keyspace in project grakn by graknlabs.

the class IndexPostProcessorTest method whenAddingCommitLogToPostProcessor_EnsureIndexStorageIsUpdated.

@Test
public void whenAddingCommitLogToPostProcessor_EnsureIndexStorageIsUpdated() {
    // Create Sample Data For CommitLog
    String index1 = "index1";
    String index2 = "index2";
    Set<ConceptId> ids1 = Arrays.asList("a", "b", "c").stream().map(ConceptId::of).collect(Collectors.toSet());
    Set<ConceptId> ids2 = Arrays.asList("1", "2", "3").stream().map(ConceptId::of).collect(Collectors.toSet());
    HashMap<String, Set<ConceptId>> attributes = new HashMap<>();
    attributes.put(index1, ids1);
    attributes.put(index2, ids2);
    // Create Commit Log
    Keyspace keyspace = Keyspace.of("whatakeyspace");
    CommitLog commitLog = CommitLog.create(keyspace, Collections.emptyMap(), attributes);
    // Call the post processor
    indexPostProcessor.updateIndices(commitLog);
    // Check index storage is updated
    verify(indexStorage, Mockito.times(1)).addIndex(keyspace, index1, ids1);
    verify(indexStorage, Mockito.times(1)).addIndex(keyspace, index2, ids2);
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) Keyspace(ai.grakn.Keyspace) CommitLog(ai.grakn.kb.log.CommitLog) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Example 15 with Keyspace

use of ai.grakn.Keyspace in project grakn by graknlabs.

the class RedisCountStorageTest method whenIncreasingCountOnRedisConcurrently_EnsureAllThreadCountsArePersisted.

@Test
public void whenIncreasingCountOnRedisConcurrently_EnsureAllThreadCountsArePersisted() throws ExecutionException, InterruptedException {
    Keyspace keyspace = SampleKBLoader.randomKeyspace();
    ConceptId conceptId = ConceptId.of("Roach");
    int[] counts = { 5, 5, 10, 10, -8, -2, 5, 5, -7 };
    ExecutorService pool = Executors.newCachedThreadPool();
    Set<Future> futures = new HashSet<>();
    assertEquals(0, redis.getCount(RedisCountStorage.getKeyNumInstances(keyspace, conceptId)));
    for (int i = 0; i < counts.length; i++) {
        int finalI = i;
        futures.add(pool.submit(() -> redis.incrementCount(RedisCountStorage.getKeyNumInstances(keyspace, conceptId), counts[finalI])));
    }
    for (Future future : futures) {
        future.get();
    }
    assertEquals(23, redis.getCount(RedisCountStorage.getKeyNumInstances(keyspace, conceptId)));
}
Also used : Keyspace(ai.grakn.Keyspace) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ConceptId(ai.grakn.concept.ConceptId) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Keyspace (ai.grakn.Keyspace)30 Test (org.junit.Test)14 GraknTx (ai.grakn.GraknTx)9 GraknTxType (ai.grakn.GraknTxType)7 ConceptId (ai.grakn.concept.ConceptId)7 List (java.util.List)6 BatchExecutorClient (ai.grakn.client.BatchExecutorClient)5 Timer (com.codahale.metrics.Timer)5 LoggerFactory (org.slf4j.LoggerFactory)5 GraknSession (ai.grakn.GraknSession)4 Role (ai.grakn.concept.Role)4 InsertQuery (ai.grakn.graql.InsertQuery)4 SimpleURI (ai.grakn.util.SimpleURI)4 Before (org.junit.Before)4 Logger (org.slf4j.Logger)4 GraknClient (ai.grakn.client.GraknClient)3 AttributeType (ai.grakn.concept.AttributeType)3 EntityType (ai.grakn.concept.EntityType)3 RelationshipType (ai.grakn.concept.RelationshipType)3 Graql (ai.grakn.graql.Graql)3