Search in sources :

Example 1 with QueryResponse

use of ai.grakn.client.QueryResponse 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 2 with QueryResponse

use of ai.grakn.client.QueryResponse 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 3 with QueryResponse

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

the class Migrator method load.

/**
 * Migrate data constrained by this migrator using a loader configured
 * by the provided parameters.
 *
 * @param template Template used to extract the data
 * @param data Data being migrated
 */
public void load(String template, Stream<Map<String, Object>> data) {
    GraknClient graknClient = GraknClient.of(uri);
    AtomicInteger queriesExecuted = new AtomicInteger(0);
    try (BatchExecutorClient loader = BatchExecutorClient.newBuilder().taskClient(graknClient).maxRetries(retries).maxDelay(maxDelayMs).metricRegistry(metricRegistry).build()) {
        checkKeyspace(graknClient);
        Stream<Query> queryStream = data.flatMap(d -> template(template, d, failFast));
        if (maxLines > -1) {
            queryStream = queryStream.limit(maxLines);
        }
        queryStream.forEach(q -> {
            LOG.trace("Adding query {}", q);
            totalMeter.mark();
            // We add get a hot observable. It starts immediately
            Observable<QueryResponse> observable = loader.add(q, keyspace, failFast);
            subscribeToReportOutcome(failFast, observable, queriesExecuted);
        });
    }
    System.out.println("Loaded " + queriesExecuted + " statements");
}
Also used : Query(ai.grakn.graql.Query) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GraknClient(ai.grakn.client.GraknClient) QueryResponse(ai.grakn.client.QueryResponse) BatchExecutorClient(ai.grakn.client.BatchExecutorClient)

Aggregations

BatchExecutorClient (ai.grakn.client.BatchExecutorClient)3 QueryResponse (ai.grakn.client.QueryResponse)3 GraknClient (ai.grakn.client.GraknClient)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 GraknTx (ai.grakn.GraknTx)1 GraknTxType (ai.grakn.GraknTxType)1 Keyspace (ai.grakn.Keyspace)1 AttributeType (ai.grakn.concept.AttributeType)1 EntityType (ai.grakn.concept.EntityType)1 Role (ai.grakn.concept.Role)1 EmbeddedGraknSession (ai.grakn.factory.EmbeddedGraknSession)1 Graql (ai.grakn.graql.Graql)1 Graql.var (ai.grakn.graql.Graql.var)1 InsertQuery (ai.grakn.graql.InsertQuery)1 Query (ai.grakn.graql.Query)1 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)1 EngineContext (ai.grakn.test.rule.EngineContext)1 ConcurrencyUtil.allObservable (ai.grakn.util.ConcurrencyUtil.allObservable)1 GraknTestUtil (ai.grakn.util.GraknTestUtil)1 GraknTestUtil.usingTinker (ai.grakn.util.GraknTestUtil.usingTinker)1