Search in sources :

Example 6 with BatchExecutorClient

use of ai.grakn.client.BatchExecutorClient in project grakn by graknlabs.

the class BatchExecutorClientIT method whenEngineRESTFailsWhileLoadingWithRetryTrue_LoaderRetriesAndWaits.

@Ignore("Randomly failing test which is slowing down dev. This should be fixed")
@Test
public void whenEngineRESTFailsWhileLoadingWithRetryTrue_LoaderRetriesAndWaits() throws Exception {
    List<Observable<QueryResponse>> all = new ArrayList<>();
    int n = 20;
    try (BatchExecutorClient loader = loader(MAX_DELAY)) {
        for (int i = 0; i < n; i++) {
            all.add(loader.add(query(), keyspace, true).doOnError(ex -> System.out.println("Error " + ex)));
            if (i % 5 == 0) {
                Thread.sleep(200);
                System.out.println("Restarting engine");
                engine.server().getHttpHandler().stopHTTP();
                Thread.sleep(200);
                engine.server().getHttpHandler().startHTTP();
            }
        }
        int completed = allObservable(all).toBlocking().first().size();
        assertEquals(n, completed);
    }
    if (GraknTestUtil.usingJanus()) {
        try (GraknTx graph = session.open(GraknTxType.READ)) {
            assertEquals(n, graph.getEntityType("name_tag").instances().count());
        }
    }
}
Also used : HystrixRequestLog(com.netflix.hystrix.HystrixRequestLog) Stream.generate(java.util.stream.Stream.generate) GraknTestUtil(ai.grakn.util.GraknTestUtil) Keyspace(ai.grakn.Keyspace) Role(ai.grakn.concept.Role) Assume.assumeFalse(org.junit.Assume.assumeFalse) InsertQuery(ai.grakn.graql.InsertQuery) Graql(ai.grakn.graql.Graql) ConcurrencyUtil.allObservable(ai.grakn.util.ConcurrencyUtil.allObservable) QueryResponse(ai.grakn.client.QueryResponse) Mockito.spy(org.mockito.Mockito.spy) EntityType(ai.grakn.concept.EntityType) Observable(rx.Observable) ArrayList(java.util.ArrayList) AttributeType(ai.grakn.concept.AttributeType) GraknTx(ai.grakn.GraknTx) GraknClient(ai.grakn.client.GraknClient) ClassRule(org.junit.ClassRule) ExpectedException(org.junit.rules.ExpectedException) EngineContext(ai.grakn.test.rule.EngineContext) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) Graql.var(ai.grakn.graql.Graql.var) BatchExecutorClient(ai.grakn.client.BatchExecutorClient) HystrixEventType(com.netflix.hystrix.HystrixEventType) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) UUID(java.util.UUID) GraknTestUtil.usingTinker(ai.grakn.util.GraknTestUtil.usingTinker) HystrixCommand(com.netflix.hystrix.HystrixCommand) List(java.util.List) Rule(org.junit.Rule) Ignore(org.junit.Ignore) EmbeddedGraknSession(ai.grakn.factory.EmbeddedGraknSession) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) SampleKBLoader.randomKeyspace(ai.grakn.util.SampleKBLoader.randomKeyspace) Assert.assertEquals(org.junit.Assert.assertEquals) GraknTx(ai.grakn.GraknTx) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) ArrayList(java.util.ArrayList) BatchExecutorClient(ai.grakn.client.BatchExecutorClient) ConcurrencyUtil.allObservable(ai.grakn.util.ConcurrencyUtil.allObservable) Observable(rx.Observable) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with BatchExecutorClient

use of ai.grakn.client.BatchExecutorClient in project grakn by graknlabs.

the class BatchLoader method sendBatchRequest.

static void sendBatchRequest(SimpleURI uri, Keyspace keyspace, Path graqlPath, PrintStream sout, PrintStream serr) throws IOException {
    AtomicInteger queriesExecuted = new AtomicInteger(0);
    try (FileInputStream inputStream = new FileInputStream(graqlPath.toFile());
        Reader queryReader = new InputStreamReader(inputStream, Charsets.UTF_8);
        BatchExecutorClient batchExecutorClient = loaderClient(uri)) {
        Graql.parser().parseList(queryReader).forEach(query -> {
            Observable<QueryResponse> observable = batchExecutorClient.add(query, keyspace, false);
            observable.subscribe(/* On success: */
            queryResponse -> queriesExecuted.incrementAndGet(), /* On error:   */
            serr::println);
        });
    }
    sout.println("Statements executed: " + queriesExecuted.get());
}
Also used : InputStreamReader(java.io.InputStreamReader) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryResponse(ai.grakn.client.QueryResponse) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BatchExecutorClient(ai.grakn.client.BatchExecutorClient) FileInputStream(java.io.FileInputStream)

Example 8 with BatchExecutorClient

use of ai.grakn.client.BatchExecutorClient in project grakn by graknlabs.

the class GraknClientFake method whenBatchExecutorClientCloses_AllTasksHaveCompleted.

@Test
public void whenBatchExecutorClientCloses_AllTasksHaveCompleted() throws GraknClientException {
    Keyspace keyspace = Keyspace.of("yes");
    GraknClientFake graknClient = new GraknClientFake();
    // 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) Test(org.junit.Test)

Example 9 with BatchExecutorClient

use of ai.grakn.client.BatchExecutorClient in project grakn by graknlabs.

the class BenchmarkIT method loadEntities.

private void loadEntities(String entityLabel, int N, GraknClient graknClient, Keyspace keyspace) {
    try (BatchExecutorClient loader = BatchExecutorClient.newBuilder().taskClient(graknClient).build()) {
        for (int i = 0; i < N; i++) {
            InsertQuery entityInsert = Graql.insert(var().asUserDefined().isa(entityLabel));
            loader.add(entityInsert, keyspace).subscribe();
        }
    }
}
Also used : InsertQuery(ai.grakn.graql.InsertQuery) BatchExecutorClient(ai.grakn.client.BatchExecutorClient)

Example 10 with BatchExecutorClient

use of ai.grakn.client.BatchExecutorClient in project grakn by graknlabs.

the class BenchmarkIT method loadRandomisedRelationInstances.

private void loadRandomisedRelationInstances(String entityLabel, String fromRoleLabel, String toRoleLabel, String relationLabel, int N, GraknSession session, GraknClient graknClient, Keyspace keyspace) {
    try (BatchExecutorClient loader = BatchExecutorClient.newBuilder().taskClient(graknClient).build()) {
        GraknTx tx = session.open(GraknTxType.READ);
        Var entityVar = var().asUserDefined();
        ConceptId[] instances = tx.graql().match(entityVar.isa(entityLabel)).get().execute().stream().map(ans -> ans.get(entityVar).getId()).toArray(ConceptId[]::new);
        assertEquals(instances.length, N);
        Role fromRole = tx.getRole(fromRoleLabel);
        Role toRole = tx.getRole(toRoleLabel);
        RelationshipType relationType = tx.getRelationshipType(relationLabel);
        Random rand = new Random();
        Multimap<Integer, Integer> assignmentMap = HashMultimap.create();
        for (int i = 0; i < N; i++) {
            int from = rand.nextInt(N - 1);
            int to = rand.nextInt(N - 1);
            while (to == from && assignmentMap.get(from).contains(to)) to = rand.nextInt(N - 1);
            Var fromRolePlayer = Graql.var();
            Var toRolePlayer = Graql.var();
            Pattern relationInsert = Graql.var().rel(Graql.label(fromRole.getLabel()), fromRolePlayer).rel(Graql.label(toRole.getLabel()), toRolePlayer).isa(Graql.label(relationType.getLabel())).and(fromRolePlayer.asUserDefined().id(instances[from])).and(toRolePlayer.asUserDefined().id(instances[to]));
            loader.add(Graql.insert(relationInsert.admin().varPatterns()), keyspace).subscribe();
        }
        tx.close();
    }
}
Also used : Iterables(com.google.common.collect.Iterables) VarPattern(ai.grakn.graql.VarPattern) Keyspace(ai.grakn.Keyspace) Role(ai.grakn.concept.Role) Assume.assumeFalse(org.junit.Assume.assumeFalse) InsertQuery(ai.grakn.graql.InsertQuery) LoggerFactory(org.slf4j.LoggerFactory) Graql(ai.grakn.graql.Graql) Random(java.util.Random) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) HashMultimap(com.google.common.collect.HashMultimap) Charset(java.nio.charset.Charset) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) Files(com.google.common.io.Files) GraknTx(ai.grakn.GraknTx) GraknClient(ai.grakn.client.GraknClient) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Grakn(ai.grakn.Grakn) GraknSystemProperty(ai.grakn.GraknSystemProperty) EngineContext(ai.grakn.test.rule.EngineContext) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) Graql.var(ai.grakn.graql.Graql.var) BatchExecutorClient(ai.grakn.client.BatchExecutorClient) Logger(org.slf4j.Logger) GraknSession(ai.grakn.GraknSession) Test(org.junit.Test) GetQuery(ai.grakn.graql.GetQuery) File(java.io.File) GraknTestUtil.usingTinker(ai.grakn.util.GraknTestUtil.usingTinker) List(java.util.List) SampleKBLoader.randomKeyspace(ai.grakn.util.SampleKBLoader.randomKeyspace) Var(ai.grakn.graql.Var) Pattern(ai.grakn.graql.Pattern) Assert.assertEquals(org.junit.Assert.assertEquals) VarPattern(ai.grakn.graql.VarPattern) Pattern(ai.grakn.graql.Pattern) Var(ai.grakn.graql.Var) RelationshipType(ai.grakn.concept.RelationshipType) ConceptId(ai.grakn.concept.ConceptId) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) Random(java.util.Random) BatchExecutorClient(ai.grakn.client.BatchExecutorClient)

Aggregations

BatchExecutorClient (ai.grakn.client.BatchExecutorClient)12 Test (org.junit.Test)9 InsertQuery (ai.grakn.graql.InsertQuery)6 Keyspace (ai.grakn.Keyspace)5 ConcurrencyUtil.allObservable (ai.grakn.util.ConcurrencyUtil.allObservable)5 ArrayList (java.util.ArrayList)5 Observable (rx.Observable)5 GraknTx (ai.grakn.GraknTx)4 GraknClient (ai.grakn.client.GraknClient)4 GraknTxType (ai.grakn.GraknTxType)3 QueryResponse (ai.grakn.client.QueryResponse)3 AttributeType (ai.grakn.concept.AttributeType)3 Role (ai.grakn.concept.Role)3 Graql (ai.grakn.graql.Graql)3 Graql.var (ai.grakn.graql.Graql.var)3 Query (ai.grakn.graql.Query)3 EngineContext (ai.grakn.test.rule.EngineContext)3 GraknTestUtil.usingTinker (ai.grakn.util.GraknTestUtil.usingTinker)3 SampleKBLoader.randomKeyspace (ai.grakn.util.SampleKBLoader.randomKeyspace)3 List (java.util.List)3