Search in sources :

Example 1 with BatchExecutorClient

use of ai.grakn.client.BatchExecutorClient 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 BatchExecutorClient

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

the class BatchExecutorClientIT method whenSending100Queries_TheyAreSentInBatch.

@Ignore("This test interferes with other tests using the BEC (they probably use the same HystrixRequestLog)")
@Test
public void whenSending100Queries_TheyAreSentInBatch() {
    List<Observable<QueryResponse>> all = new ArrayList<>();
    // Increasing the max delay so eveyrthing goes in a single batch
    try (BatchExecutorClient loader = loader(MAX_DELAY * 100)) {
        int n = 100;
        generate(this::query).limit(n).forEach(q -> all.add(loader.add(q, keyspace, true)));
        int completed = allObservable(all).toBlocking().first().size();
        assertEquals(n, completed);
        assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
        HystrixCommand<?> command = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().toArray(new HystrixCommand<?>[1])[0];
        // assert the command is the one we're expecting
        assertEquals("CommandQueries", command.getCommandKey().name());
        // confirm that it was a COLLAPSED command execution
        assertTrue(command.getExecutionEvents().contains(HystrixEventType.COLLAPSED));
        // and that it was successful
        assertTrue(command.getExecutionEvents().contains(HystrixEventType.SUCCESS));
    }
}
Also used : HystrixCommand(com.netflix.hystrix.HystrixCommand) 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 3 with BatchExecutorClient

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

the class BatchExecutorClientIT method whenSingleQueryLoadedAndServerDown_RequestIsRetried.

@Ignore("This test stops and restart server - this is not supported yet by gRPC [https://github.com/grpc/grpc/issues/7031] - fix when gRPC 1.1 is released")
@Test
public void whenSingleQueryLoadedAndServerDown_RequestIsRetried() throws InterruptedException, IOException {
    List<Observable<QueryResponse>> all = new ArrayList<>();
    // Create a BatchExecutorClient with a callback that will fail
    try (BatchExecutorClient loader = loader(MAX_DELAY)) {
        // Engine goes down
        engine.server().getHttpHandler().stopHTTP();
        // Most likely the first call doesn't find the server but it's retried
        generate(this::query).limit(1).forEach(q -> all.add(loader.add(q, keyspace, true)));
        engine.server().getHttpHandler().startHTTP();
        int completed = allObservable(all).toBlocking().first().size();
        // Verify that the logger received the failed log message
        assertEquals(1, completed);
    }
}
Also used : 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 4 with BatchExecutorClient

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

the class BatchExecutorClientIT method whenSingleQueryLoaded_TaskCompletionExecutesExactlyOnce.

@Test
public void whenSingleQueryLoaded_TaskCompletionExecutesExactlyOnce() {
    List<Observable<QueryResponse>> all = new ArrayList<>();
    // Create a BatchExecutorClient with a callback that will fail
    try (BatchExecutorClient loader = loader(MAX_DELAY)) {
        // Load some queries
        generate(this::query).limit(1).forEach(q -> all.add(loader.add(q, keyspace, true)));
        int completed = allObservable(all).toBlocking().first().size();
        // Verify that the logger received the failed log message
        assertEquals(1, completed);
    }
}
Also used : ArrayList(java.util.ArrayList) BatchExecutorClient(ai.grakn.client.BatchExecutorClient) ConcurrencyUtil.allObservable(ai.grakn.util.ConcurrencyUtil.allObservable) Observable(rx.Observable) Test(org.junit.Test)

Example 5 with BatchExecutorClient

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

the class BatchExecutorClientIT method whenSending100InsertQueries_100EntitiesAreLoadedIntoGraph.

@Test
public void whenSending100InsertQueries_100EntitiesAreLoadedIntoGraph() {
    int n = 100;
    List<Observable<QueryResponse>> all = new ArrayList<>();
    try (BatchExecutorClient loader = loader(MAX_DELAY)) {
        generate(this::query).limit(n).forEach(q -> all.add(loader.add(q, keyspace, true)));
        int completed = allObservable(all).toBlocking().first().size();
        assertEquals(n, completed);
    }
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        assertEquals(n, graph.getEntityType("name_tag").instances().count());
    }
}
Also used : 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) Test(org.junit.Test)

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