From 3c4b3b95ea2c9c3009bb7c5554478886f2c60d25 Mon Sep 17 00:00:00 2001 From: "helmi.nour" Date: Thu, 11 Aug 2022 01:13:18 +0100 Subject: [PATCH 1/7] fix exec async race condition --- .../main/java/com/relationalai/Client.java | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/rai-sdk/src/main/java/com/relationalai/Client.java b/rai-sdk/src/main/java/com/relationalai/Client.java index fd055a2c..5a00fb61 100644 --- a/rai-sdk/src/main/java/com/relationalai/Client.java +++ b/rai-sdk/src/main/java/com/relationalai/Client.java @@ -705,8 +705,12 @@ public TransactionAsyncResult execute( String source, boolean readonly, Map inputs) throws HttpError, IOException, InterruptedException { - var id = executeAsync(database, engine, source, readonly, inputs).transaction.id; + var rsp = executeAsync(database, engine, source, readonly, inputs); + if ("COMPLETED".equals(rsp.transaction.state)) + return rsp; + + var id = rsp.transaction.id; var transaction = getTransaction(id).transaction; while ( !("COMPLETED".equals(transaction.state) || "ABORTED".equals(transaction.state)) ) { @@ -718,12 +722,7 @@ public TransactionAsyncResult execute( var metadata = getTransactionMetadata(id); var problems = getTransactionProblems(id); - return new TransactionAsyncResult( - transaction, - results, - metadata, - problems - ); + return new TransactionAsyncResult(transaction, results, metadata, problems); } public TransactionAsyncResult executeAsync( @@ -775,12 +774,7 @@ private TransactionAsyncResult readTransactionAsyncResults(List Date: Thu, 11 Aug 2022 01:30:52 +0100 Subject: [PATCH 2/7] adding execute example --- .../com/relationalai/examples/Execute.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 rai-sdk-examples/src/main/java/com/relationalai/examples/Execute.java diff --git a/rai-sdk-examples/src/main/java/com/relationalai/examples/Execute.java b/rai-sdk-examples/src/main/java/com/relationalai/examples/Execute.java new file mode 100644 index 00000000..e1360a6f --- /dev/null +++ b/rai-sdk-examples/src/main/java/com/relationalai/examples/Execute.java @@ -0,0 +1,71 @@ +/* + * Copyright 2022 RelationalAI, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"): you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.relationalai.examples; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import com.relationalai.Client; +import com.relationalai.Config; +import com.relationalai.HttpError; +import com.relationalai.Json; + +public class Execute implements Runnable { + boolean readonly; + String database, engine, command, filename, profile; + + // Returns the name of the file, without extension. + static String readFile(String fname) throws IOException { + return Files.readAllBytes(Path.of(fname)).toString(); + } + + String getCommand() throws IOException { + if (command != null) + return command; // prefer command line + if (filename != null) + return readFile(filename); + return null; + } + + public void parseArgs(String[] args) { + var c = Command.create("Execute") + .addArgument("database") + .addArgument("engine") + .addOption("c", "rel source string") + .addOption("f", "rel source file") + .addFlag("readonly", "readonly query (default: false)") + .parseArgs(args); + this.database = c.getValue("database"); + this.engine = c.getValue("engine"); + this.command = c.getValue("c"); + this.filename = c.getValue("f"); + this.readonly = c.getValue("readonly", Boolean.class); + this.profile = c.getValue("profile"); + } + + public void run(String[] args) throws HttpError, InterruptedException, IOException { + parseArgs(args); + var cfg = Config.loadConfig("~/.rai/config", profile); + var client = new Client(cfg); + String source = getCommand(); + if (source == null) + return; // nothing to execute + var rsp = client.execute(database, engine, source, readonly); + Json.print(rsp); + } +} From 3eb1364f8fec5a4b175e98f4f6c2cae182b0eb45 Mon Sep 17 00:00:00 2001 From: "helmi.nour" Date: Thu, 11 Aug 2022 16:37:50 +0100 Subject: [PATCH 3/7] using gotCompletedResult --- .../main/java/com/relationalai/Client.java | 18 ++++++++---------- .../relationalai/TransactionAsyncResult.java | 19 ++++++++++++++++++- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/rai-sdk/src/main/java/com/relationalai/Client.java b/rai-sdk/src/main/java/com/relationalai/Client.java index 5a00fb61..bbd81b3b 100644 --- a/rai-sdk/src/main/java/com/relationalai/Client.java +++ b/rai-sdk/src/main/java/com/relationalai/Client.java @@ -707,22 +707,20 @@ public TransactionAsyncResult execute( var rsp = executeAsync(database, engine, source, readonly, inputs); - if ("COMPLETED".equals(rsp.transaction.state)) + if (rsp.gotCompletedResult) return rsp; - var id = rsp.transaction.id; - var transaction = getTransaction(id).transaction; - + var transaction = getTransaction(rsp.transaction.id).transaction; while ( !("COMPLETED".equals(transaction.state) || "ABORTED".equals(transaction.state)) ) { Thread.sleep(2000); - transaction = getTransaction(id).transaction; + transaction = getTransaction(transaction.id).transaction; } - var results = getTransactionResults(id); - var metadata = getTransactionMetadata(id); - var problems = getTransactionProblems(id); + var results = getTransactionResults(transaction.id); + var metadata = getTransactionMetadata(transaction.id); + var problems = getTransactionProblems(transaction.id); - return new TransactionAsyncResult(transaction, results, metadata, problems); + return new TransactionAsyncResult(transaction, results, metadata, problems, true); } public TransactionAsyncResult executeAsync( @@ -774,7 +772,7 @@ private TransactionAsyncResult readTransactionAsyncResults(List results; public List metadata; public List problems; - public TransactionAsyncResult(TransactionAsyncCompactResponse transaction, List results, List metadata, List problems) { + public TransactionAsyncResult( + TransactionAsyncCompactResponse transaction, + List results, + List metadata, + List problems + ) { this.transaction = transaction; this.results = results; this.metadata = metadata; this.problems = problems; + this.gotCompletedResult = false; + } + public TransactionAsyncResult( + TransactionAsyncCompactResponse transaction, + List results, + List metadata, + List problems, + Boolean gotCompletedResult + ) { + this(transaction, results, metadata, problems); + this.gotCompletedResult = false; } } From 2f8f98587d40ef65e16c22e73745a4207ff38002 Mon Sep 17 00:00:00 2001 From: Helmi Nour Date: Fri, 12 Aug 2022 14:37:25 +0100 Subject: [PATCH 4/7] Update rai-sdk/src/main/java/com/relationalai/TransactionAsyncResult.java Co-authored-by: Pete Vilter <7341+vilterp@users.noreply.github.com> --- .../src/main/java/com/relationalai/TransactionAsyncResult.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rai-sdk/src/main/java/com/relationalai/TransactionAsyncResult.java b/rai-sdk/src/main/java/com/relationalai/TransactionAsyncResult.java index 4baf6f4a..fa0ee79e 100644 --- a/rai-sdk/src/main/java/com/relationalai/TransactionAsyncResult.java +++ b/rai-sdk/src/main/java/com/relationalai/TransactionAsyncResult.java @@ -29,7 +29,6 @@ public TransactionAsyncResult( List problems, Boolean gotCompletedResult ) { - this(transaction, results, metadata, problems); - this.gotCompletedResult = false; + this(transaction, results, metadata, problems, gotCompletedResult); } } From 841a5d9e0f2db8cc1301cedfc1c662cd6fc70d63 Mon Sep 17 00:00:00 2001 From: "helmi.nour" Date: Fri, 12 Aug 2022 14:46:24 +0100 Subject: [PATCH 5/7] addressing PR comments --- rai-sdk/src/main/java/com/relationalai/Client.java | 2 +- .../java/com/relationalai/TransactionAsyncResult.java | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/rai-sdk/src/main/java/com/relationalai/Client.java b/rai-sdk/src/main/java/com/relationalai/Client.java index bbd81b3b..a2290344 100644 --- a/rai-sdk/src/main/java/com/relationalai/Client.java +++ b/rai-sdk/src/main/java/com/relationalai/Client.java @@ -707,7 +707,7 @@ public TransactionAsyncResult execute( var rsp = executeAsync(database, engine, source, readonly, inputs); - if (rsp.gotCompletedResult) + if (rsp.gotCompleteResult) return rsp; var transaction = getTransaction(rsp.transaction.id).transaction; diff --git a/rai-sdk/src/main/java/com/relationalai/TransactionAsyncResult.java b/rai-sdk/src/main/java/com/relationalai/TransactionAsyncResult.java index fa0ee79e..36beefc9 100644 --- a/rai-sdk/src/main/java/com/relationalai/TransactionAsyncResult.java +++ b/rai-sdk/src/main/java/com/relationalai/TransactionAsyncResult.java @@ -4,7 +4,7 @@ public class TransactionAsyncResult extends Entity { - public Boolean gotCompletedResult; + public Boolean gotCompleteResult; public TransactionAsyncCompactResponse transaction; public List results; public List metadata; @@ -20,15 +20,16 @@ public TransactionAsyncResult( this.results = results; this.metadata = metadata; this.problems = problems; - this.gotCompletedResult = false; + this.gotCompleteResult = false; } public TransactionAsyncResult( TransactionAsyncCompactResponse transaction, List results, List metadata, List problems, - Boolean gotCompletedResult + Boolean gotCompleteResult ) { - this(transaction, results, metadata, problems, gotCompletedResult); + this(transaction, results, metadata, problems); + this.gotCompleteResult = gotCompleteResult; } } From eb040ae370e54a1547d99ee9b79497a736768ab6 Mon Sep 17 00:00:00 2001 From: "helmi.nour" Date: Fri, 19 Aug 2022 08:30:13 +0100 Subject: [PATCH 6/7] fix --- rai-sdk/src/main/java/com/relationalai/Client.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rai-sdk/src/main/java/com/relationalai/Client.java b/rai-sdk/src/main/java/com/relationalai/Client.java index 90423f2e..e38818ee 100644 --- a/rai-sdk/src/main/java/com/relationalai/Client.java +++ b/rai-sdk/src/main/java/com/relationalai/Client.java @@ -800,7 +800,7 @@ private TransactionAsyncResult readTransactionAsyncResults(List Date: Fri, 19 Aug 2022 08:49:14 +0100 Subject: [PATCH 7/7] fix metadata.pb --- rai-sdk/src/test/resources/metadata.pb | 4 ++-- rai-sdk/src/test/resources/metadata.pb.txt | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rai-sdk/src/test/resources/metadata.pb b/rai-sdk/src/test/resources/metadata.pb index bdc334e3..a9c7479d 100644 --- a/rai-sdk/src/test/resources/metadata.pb +++ b/rai-sdk/src/test/resources/metadata.pb @@ -2,8 +2,8 @@ > 3 " - - Šoutput + + Šoutput    diff --git a/rai-sdk/src/test/resources/metadata.pb.txt b/rai-sdk/src/test/resources/metadata.pb.txt index 4a071166..a55bc404 100644 --- a/rai-sdk/src/test/resources/metadata.pb.txt +++ b/rai-sdk/src/test/resources/metadata.pb.txt @@ -5,11 +5,11 @@ relations { constant_type { rel_type { tag: PRIMITIVE_TYPE - primitive_type: SYMBOL + primitive_type: STRING } value { arguments { - tag: SYMBOL + tag: STRING string_val: "output" } }