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());
}
}
}
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());
}
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()));
}
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();
}
}
}
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();
}
}
Aggregations