You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by "isapego (via GitHub)" <gi...@apache.org> on 2025/04/01 03:41:57 UTC
[PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
isapego opened a new pull request, #5532:
URL: https://github.com/apache/ignite-3/pull/5532
https://issues.apache.org/jira/browse/IGNITE-23439
- Implemented SQL query cancellation in C++ client;
- Added tests.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "isapego (via GitHub)" <gi...@apache.org>.
isapego commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022790027
##########
modules/platforms/cpp/ignite/client/sql/sql.h:
##########
@@ -44,49 +45,57 @@ class sql {
sql() = delete;
/**
- * Executes single SQL statement asynchronously and returns rows.
+ * Executes a single SQL statement asynchronously and returns rows.
*
* @param tx Optional transaction. If nullptr implicit transaction for this single operation is used.
+ * @param token Cancellation token. Can be @c nullptr.
* @param statement Statement to execute.
* @param args Arguments for the statement (can be empty).
* @param callback A callback called on operation completion with SQL result set.
*/
- IGNITE_API void execute_async(transaction *tx, const sql_statement &statement, std::vector<primitive> args,
- ignite_callback<result_set> callback);
+ IGNITE_API void execute_async(transaction *tx, cancellation_token *token, const sql_statement &statement,
+ std::vector<primitive> args, ignite_callback<result_set> callback);
Review Comment:
This issue should probably be addressed in another way, because right now we have like 5 arguments, 4 of which can be optional independently of each other. That's a hell of overloads. I prefer to leave it like this until we come up with a common solution for cases like this - we can always add overloads in future.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "mpoeter (via GitHub)" <gi...@apache.org>.
mpoeter commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022689467
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.h:
##########
@@ -0,0 +1,85 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#pragma once
+
+#include "ignite/common/ignite_result.h"
+#include "ignite/client/cancellation_token.h"
+
+#include <optional>
+#include <mutex>
+#include <vector>
+#include <functional>
+#include <ignite/client/ignite_logger.h>
+
+namespace ignite
+{
+
+/**
+ * A cancellation token implementation.
+ */
+class cancellation_token_impl : public cancellation_token, public std::enable_shared_from_this<cancellation_token_impl>
+{
+public:
+ /**
+ * Destructor.
+ */
+ ~cancellation_token_impl() override = default;
+
+ /**
+ * Abruptly terminates an execution of an associated process.
+ *
+ * @param callback A callback that will be called after the process has been terminated and the resources associated
+ * with that process have been freed.
+ */
+ void cancel_async(ignite_callback<void> callback);
+
+ /**
+ * Adds an action to perform on cancellation.
+ *
+ * @param logger Logger to use if the operation was already canceled.
+ * @param action An action to perform on cancellation.
+ */
+ void add_action(std::shared_ptr<ignite_logger> logger, std::function<void(ignite_callback<void>)> action);
+
+ /**
+ * Flag indicating whether cancellation was requested or not.
+ *
+ * This method will return true even if cancellation has not been completed yet.
+ *
+ * @return @c true if the cancellation was requested.
+ */
+ bool is_cancelled() const { return m_cancelled; }
+
+private:
+ /** Mutex. */
+ std::mutex m_mutex{};
+
+ /** Cancel flag. */
+ bool m_cancelled{false};
Review Comment:
Nope, volatile does not prevent a data race. Using volatile for concurrency is a big no-no! ;)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "isapego (via GitHub)" <gi...@apache.org>.
isapego commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022598412
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.cpp:
##########
@@ -0,0 +1,121 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#include "ignite/client/detail/cancellation_token_impl.h"
+
+#include <sstream>
+
+namespace ignite
+{
+
+void cancellation_token_impl::cancel_async(ignite_callback<void> callback) {
+ std::lock_guard guard(m_mutex);
+
+ if (is_cancelled()) {
+ if (m_result) {
+ callback(ignite_result<void>{*m_result});
+ } else {
+ m_callbacks.push_back(std::move(callback));
+ }
+ return;
+ }
+
+ m_cancelled = true;
+ m_callbacks.push_back(std::move(callback));
+
+ if (m_actions.empty()) {
+ m_result = ignite_result<void>{};
+ for (auto &cb : m_callbacks) {
+ cb(ignite_result<void>{*m_result});
+ }
+ return;
+ }
+
+ auto results = std::make_shared<std::vector<ignite_result<void>>>();
+ auto results_mutex = std::make_shared<std::mutex>();
+ auto expected_results = m_actions.size();
+ for (auto &action : m_actions) {
+ action([this, results, results_mutex, expected_results] (ignite_result<void> res) {
+ bool all_done{false};
Review Comment:
Good catch, it should have been `shared_from_this()` all along.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "isapego (via GitHub)" <gi...@apache.org>.
isapego commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022715900
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.cpp:
##########
@@ -0,0 +1,121 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#include "ignite/client/detail/cancellation_token_impl.h"
+
+#include <sstream>
+
+namespace ignite
+{
+
+void cancellation_token_impl::cancel_async(ignite_callback<void> callback) {
+ std::lock_guard guard(m_mutex);
+
+ if (is_cancelled()) {
+ if (m_result) {
+ callback(ignite_result<void>{*m_result});
Review Comment:
Well, it's just to make sure we're not forgetting to move values, basically, because as I said in most cases it totally makes sense. But if the callback takes a value, we can just leave copying somewhere by mistake. So, long story short, I decided to be more explicit about it as it's not a public API and does not need to be pretty anyway.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "isapego (via GitHub)" <gi...@apache.org>.
isapego commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022718740
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.h:
##########
@@ -0,0 +1,85 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#pragma once
+
+#include "ignite/common/ignite_result.h"
+#include "ignite/client/cancellation_token.h"
+
+#include <optional>
+#include <mutex>
+#include <vector>
+#include <functional>
+#include <ignite/client/ignite_logger.h>
+
+namespace ignite
+{
+
+/**
+ * A cancellation token implementation.
+ */
+class cancellation_token_impl : public cancellation_token, public std::enable_shared_from_this<cancellation_token_impl>
+{
+public:
+ /**
+ * Destructor.
+ */
+ ~cancellation_token_impl() override = default;
+
+ /**
+ * Abruptly terminates an execution of an associated process.
+ *
+ * @param callback A callback that will be called after the process has been terminated and the resources associated
+ * with that process have been freed.
+ */
+ void cancel_async(ignite_callback<void> callback);
+
+ /**
+ * Adds an action to perform on cancellation.
+ *
+ * @param logger Logger to use if the operation was already canceled.
+ * @param action An action to perform on cancellation.
+ */
+ void add_action(std::shared_ptr<ignite_logger> logger, std::function<void(ignite_callback<void>)> action);
+
+ /**
+ * Flag indicating whether cancellation was requested or not.
+ *
+ * This method will return true even if cancellation has not been completed yet.
+ *
+ * @return @c true if the cancellation was requested.
+ */
+ bool is_cancelled() const { return m_cancelled; }
+
+private:
+ /** Mutex. */
+ std::mutex m_mutex{};
+
+ /** Cancel flag. */
+ bool m_cancelled{false};
Review Comment:
I agree, I didn't think it through well enough when answered :)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "isapego (via GitHub)" <gi...@apache.org>.
isapego commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022592310
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.cpp:
##########
@@ -0,0 +1,121 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#include "ignite/client/detail/cancellation_token_impl.h"
+
+#include <sstream>
+
+namespace ignite
+{
+
+void cancellation_token_impl::cancel_async(ignite_callback<void> callback) {
+ std::lock_guard guard(m_mutex);
+
+ if (is_cancelled()) {
+ if (m_result) {
+ callback(ignite_result<void>{*m_result});
Review Comment:
Well, that's because `callback` actually needs rvalue (`&&`).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "mpoeter (via GitHub)" <gi...@apache.org>.
mpoeter commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022759815
##########
modules/platforms/cpp/ignite/client/detail/sql/sql_impl.cpp:
##########
@@ -88,8 +89,28 @@ void write_args(protocol::writer &writer, const std::vector<primitive> &args) {
writer.write_binary(args_data);
}
-void sql_impl::execute_async(transaction *tx, const sql_statement &statement, std::vector<primitive> &&args,
- ignite_callback<result_set> &&callback) {
+void add_action(cancellation_token &token, node_connection &connection, std::int64_t req_id) {
+ auto writer_func = [req_id](protocol::writer &writer) {
+ writer.write(req_id);
+ };
+
+ cancellation_token_impl &token_impl = static_cast<cancellation_token_impl&>(token);
+ token_impl.add_action(connection.get_logger(), [req_id, &connection, writer_func] (ignite_callback<void> callback) {
+ connection.perform_request<void>(protocol::client_operation::SQL_CANCEL_EXEC,
+ writer_func, [] (protocol::reader&){}, std::move(callback));
+ });
Review Comment:
Is it guaranteed that `connection` lives long enough?
##########
modules/platforms/cpp/ignite/client/sql/sql.h:
##########
@@ -44,49 +45,57 @@ class sql {
sql() = delete;
/**
- * Executes single SQL statement asynchronously and returns rows.
+ * Executes a single SQL statement asynchronously and returns rows.
*
* @param tx Optional transaction. If nullptr implicit transaction for this single operation is used.
+ * @param token Cancellation token. Can be @c nullptr.
* @param statement Statement to execute.
* @param args Arguments for the statement (can be empty).
* @param callback A callback called on operation completion with SQL result set.
*/
- IGNITE_API void execute_async(transaction *tx, const sql_statement &statement, std::vector<primitive> args,
- ignite_callback<result_set> callback);
+ IGNITE_API void execute_async(transaction *tx, cancellation_token *token, const sql_statement &statement,
+ std::vector<primitive> args, ignite_callback<result_set> callback);
Review Comment:
Should we for convenience also provide overloads without the `cancellation_token`? In the tests we usually just pass `nullptr`, but that isn't really descriptive. (Same of course also goes for the other functions.)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "mpoeter (via GitHub)" <gi...@apache.org>.
mpoeter commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022699155
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.cpp:
##########
@@ -0,0 +1,121 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#include "ignite/client/detail/cancellation_token_impl.h"
+
+#include <sstream>
+
+namespace ignite
+{
+
+void cancellation_token_impl::cancel_async(ignite_callback<void> callback) {
+ std::lock_guard guard(m_mutex);
+
+ if (is_cancelled()) {
+ if (m_result) {
+ callback(ignite_result<void>{*m_result});
Review Comment:
Hm.. not sure I understand the reasoning. If the callback takes the result by value we can still just move it in. But then passing in an lvalue ref would also make a copy, so it wouldn't make a difference for this code either. :) But that is not really related to this PR...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "isapego (via GitHub)" <gi...@apache.org>.
isapego merged PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "mpoeter (via GitHub)" <gi...@apache.org>.
mpoeter commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022573722
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.cpp:
##########
@@ -0,0 +1,121 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#include "ignite/client/detail/cancellation_token_impl.h"
+
+#include <sstream>
+
+namespace ignite
+{
+
+void cancellation_token_impl::cancel_async(ignite_callback<void> callback) {
+ std::lock_guard guard(m_mutex);
+
+ if (is_cancelled()) {
+ if (m_result) {
+ callback(ignite_result<void>{*m_result});
+ } else {
+ m_callbacks.push_back(std::move(callback));
+ }
+ return;
+ }
+
+ m_cancelled = true;
+ m_callbacks.push_back(std::move(callback));
+
+ if (m_actions.empty()) {
+ m_result = ignite_result<void>{};
+ for (auto &cb : m_callbacks) {
+ cb(ignite_result<void>{*m_result});
+ }
+ return;
+ }
+
+ auto results = std::make_shared<std::vector<ignite_result<void>>>();
+ auto results_mutex = std::make_shared<std::mutex>();
+ auto expected_results = m_actions.size();
+ for (auto &action : m_actions) {
+ action([this, results, results_mutex, expected_results] (ignite_result<void> res) {
+ bool all_done{false};
Review Comment:
We are capturing `this` here and we use some mutex to protect the results list, so I take it that this action callback might be called concurrently/asynchronously. How do we make sure that `this` lives long enough? Since we inherit from `enabled_shared_from_this` I think we should capture a `shared_ptr` instead.
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.cpp:
##########
@@ -0,0 +1,121 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#include "ignite/client/detail/cancellation_token_impl.h"
+
+#include <sstream>
+
+namespace ignite
+{
+
+void cancellation_token_impl::cancel_async(ignite_callback<void> callback) {
+ std::lock_guard guard(m_mutex);
+
+ if (is_cancelled()) {
+ if (m_result) {
+ callback(ignite_result<void>{*m_result});
Review Comment:
Why do we create a new ignite_result here? Wouldn't this suffice?
```suggestion
callback(*m_result);
```
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.cpp:
##########
@@ -0,0 +1,121 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#include "ignite/client/detail/cancellation_token_impl.h"
+
+#include <sstream>
+
+namespace ignite
+{
+
+void cancellation_token_impl::cancel_async(ignite_callback<void> callback) {
+ std::lock_guard guard(m_mutex);
+
+ if (is_cancelled()) {
+ if (m_result) {
+ callback(ignite_result<void>{*m_result});
+ } else {
+ m_callbacks.push_back(std::move(callback));
+ }
+ return;
+ }
+
+ m_cancelled = true;
+ m_callbacks.push_back(std::move(callback));
+
+ if (m_actions.empty()) {
+ m_result = ignite_result<void>{};
+ for (auto &cb : m_callbacks) {
+ cb(ignite_result<void>{*m_result});
Review Comment:
Same as above.
```suggestion
cb(*m_result);
```
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.cpp:
##########
@@ -0,0 +1,121 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#include "ignite/client/detail/cancellation_token_impl.h"
+
+#include <sstream>
+
+namespace ignite
+{
+
+void cancellation_token_impl::cancel_async(ignite_callback<void> callback) {
+ std::lock_guard guard(m_mutex);
+
+ if (is_cancelled()) {
+ if (m_result) {
+ callback(ignite_result<void>{*m_result});
+ } else {
+ m_callbacks.push_back(std::move(callback));
+ }
+ return;
+ }
+
+ m_cancelled = true;
+ m_callbacks.push_back(std::move(callback));
+
+ if (m_actions.empty()) {
+ m_result = ignite_result<void>{};
+ for (auto &cb : m_callbacks) {
+ cb(ignite_result<void>{*m_result});
+ }
+ return;
+ }
+
+ auto results = std::make_shared<std::vector<ignite_result<void>>>();
+ auto results_mutex = std::make_shared<std::mutex>();
+ auto expected_results = m_actions.size();
+ for (auto &action : m_actions) {
+ action([this, results, results_mutex, expected_results] (ignite_result<void> res) {
+ bool all_done{false};
+ { // Mutex locking scope
+ std::lock_guard guard0(*results_mutex);
+ results->push_back(std::move(res));
+ all_done = results->size() == expected_results;
+
+ if (all_done) {
+ bool error_found{false};
+ std::stringstream msg_builder;
+ for (auto &result : *results) {
+ if (!result.has_error()) {
+ continue;
+ }
+
+ if (!error_found) {
+ msg_builder << "One or more cancel actions failed: " << result.error().what_str();
+ error_found = true;
+ continue;
+ }
+
+ msg_builder << ", " << result.error().what_str();
+ }
+
+ { // Result mutex locking scope.
+ std::lock_guard guard1(m_mutex);
+ if (error_found) {
+ m_result = {ignite_error(msg_builder.str())};
+ } else {
+ m_result = ignite_result<void>{};
+ }
+ }
+ }
+ }
+
+ if (all_done) {
+ std::lock_guard guard0(m_mutex);
+ for (auto &cb : m_callbacks) {
+ cb(ignite_result<void>{*m_result});
+ }
+ }
+ });
+ }
+}
Review Comment:
Can we create the action callback lambda outside the loop?
```suggestion
auto action_callback = [this, results, results_mutex, expected_results] (ignite_result<void> res) {
bool all_done{false};
{ // Mutex locking scope
std::lock_guard guard0(*results_mutex);
results->push_back(std::move(res));
all_done = results->size() == expected_results;
if (all_done) {
bool error_found{false};
std::stringstream msg_builder;
for (auto &result : *results) {
if (!result.has_error()) {
continue;
}
if (!error_found) {
msg_builder << "One or more cancel actions failed: " << result.error().what_str();
error_found = true;
continue;
}
msg_builder << ", " << result.error().what_str();
}
{ // Result mutex locking scope.
std::lock_guard guard1(m_mutex);
if (error_found) {
m_result = {ignite_error(msg_builder.str())};
} else {
m_result = ignite_result<void>{};
}
}
}
}
if (all_done) {
std::lock_guard guard0(m_mutex);
for (auto &cb : m_callbacks) {
cb(ignite_result<void>{*m_result});
}
}
}
for (auto &action : m_actions) {
action(action_callback);
}
}
```
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.cpp:
##########
@@ -0,0 +1,121 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#include "ignite/client/detail/cancellation_token_impl.h"
+
+#include <sstream>
+
+namespace ignite
+{
+
+void cancellation_token_impl::cancel_async(ignite_callback<void> callback) {
+ std::lock_guard guard(m_mutex);
+
+ if (is_cancelled()) {
+ if (m_result) {
+ callback(ignite_result<void>{*m_result});
+ } else {
+ m_callbacks.push_back(std::move(callback));
+ }
+ return;
+ }
+
+ m_cancelled = true;
+ m_callbacks.push_back(std::move(callback));
+
+ if (m_actions.empty()) {
+ m_result = ignite_result<void>{};
+ for (auto &cb : m_callbacks) {
+ cb(ignite_result<void>{*m_result});
+ }
+ return;
+ }
+
+ auto results = std::make_shared<std::vector<ignite_result<void>>>();
+ auto results_mutex = std::make_shared<std::mutex>();
+ auto expected_results = m_actions.size();
+ for (auto &action : m_actions) {
+ action([this, results, results_mutex, expected_results] (ignite_result<void> res) {
+ bool all_done{false};
+ { // Mutex locking scope
+ std::lock_guard guard0(*results_mutex);
+ results->push_back(std::move(res));
+ all_done = results->size() == expected_results;
+
+ if (all_done) {
+ bool error_found{false};
+ std::stringstream msg_builder;
+ for (auto &result : *results) {
+ if (!result.has_error()) {
+ continue;
+ }
+
+ if (!error_found) {
+ msg_builder << "One or more cancel actions failed: " << result.error().what_str();
+ error_found = true;
+ continue;
+ }
+
+ msg_builder << ", " << result.error().what_str();
+ }
+
+ { // Result mutex locking scope.
+ std::lock_guard guard1(m_mutex);
+ if (error_found) {
+ m_result = {ignite_error(msg_builder.str())};
+ } else {
+ m_result = ignite_result<void>{};
+ }
+ }
+ }
+ }
+
+ if (all_done) {
+ std::lock_guard guard0(m_mutex);
+ for (auto &cb : m_callbacks) {
+ cb(ignite_result<void>{*m_result});
+ }
+ }
+ });
+ }
+}
+
+void cancellation_token_impl::add_action(std::shared_ptr<ignite_logger> logger,
+ std::function<void(ignite_callback<void>)> action) {
+ auto callback = [logger](ignite_result<void> res) {
+ if (res.has_error()) {
+ logger->log_warning(
+ "Cancellation action that was added after the token was already canceled: " + res.error().what_str());
+ }
+ };
+
+ if (is_cancelled()) {
+ action(callback);
+ return;
+ }
+
+ std::unique_lock lock(m_mutex);
+ if (is_cancelled()) {
+ lock.unlock();
+ action(callback);
+ return;
+ }
+
Review Comment:
If we remove the first check outside the lock then `m_cancelled` does not have to be an atomic. If you want to leave it in, it has to be an atomic.
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.h:
##########
@@ -0,0 +1,85 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#pragma once
+
+#include "ignite/common/ignite_result.h"
+#include "ignite/client/cancellation_token.h"
+
+#include <optional>
+#include <mutex>
+#include <vector>
+#include <functional>
+#include <ignite/client/ignite_logger.h>
+
+namespace ignite
+{
+
+/**
+ * A cancellation token implementation.
+ */
+class cancellation_token_impl : public cancellation_token, public std::enable_shared_from_this<cancellation_token_impl>
+{
+public:
+ /**
+ * Destructor.
+ */
+ ~cancellation_token_impl() override = default;
+
+ /**
+ * Abruptly terminates an execution of an associated process.
+ *
+ * @param callback A callback that will be called after the process has been terminated and the resources associated
+ * with that process have been freed.
+ */
+ void cancel_async(ignite_callback<void> callback);
+
+ /**
+ * Adds an action to perform on cancellation.
+ *
+ * @param logger Logger to use if the operation was already canceled.
+ * @param action An action to perform on cancellation.
+ */
+ void add_action(std::shared_ptr<ignite_logger> logger, std::function<void(ignite_callback<void>)> action);
+
+ /**
+ * Flag indicating whether cancellation was requested or not.
+ *
+ * This method will return true even if cancellation has not been completed yet.
+ *
+ * @return @c true if the cancellation was requested.
+ */
+ bool is_cancelled() const { return m_cancelled; }
+
+private:
+ /** Mutex. */
+ std::mutex m_mutex{};
+
+ /** Cancel flag. */
+ bool m_cancelled{false};
Review Comment:
We can have concurrent accesses to this field, so this probably needs to be atomic.
```suggestion
std::atomic<bool> m_cancelled{false};
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "isapego (via GitHub)" <gi...@apache.org>.
isapego commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022690671
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.cpp:
##########
@@ -0,0 +1,121 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#include "ignite/client/detail/cancellation_token_impl.h"
+
+#include <sstream>
+
+namespace ignite
+{
+
+void cancellation_token_impl::cancel_async(ignite_callback<void> callback) {
+ std::lock_guard guard(m_mutex);
+
+ if (is_cancelled()) {
+ if (m_result) {
+ callback(ignite_result<void>{*m_result});
+ } else {
+ m_callbacks.push_back(std::move(callback));
+ }
+ return;
+ }
+
+ m_cancelled = true;
+ m_callbacks.push_back(std::move(callback));
+
+ if (m_actions.empty()) {
+ m_result = ignite_result<void>{};
+ for (auto &cb : m_callbacks) {
+ cb(ignite_result<void>{*m_result});
+ }
+ return;
+ }
+
+ auto results = std::make_shared<std::vector<ignite_result<void>>>();
+ auto results_mutex = std::make_shared<std::mutex>();
+ auto expected_results = m_actions.size();
+ for (auto &action : m_actions) {
+ action([this, results, results_mutex, expected_results] (ignite_result<void> res) {
+ bool all_done{false};
+ { // Mutex locking scope
+ std::lock_guard guard0(*results_mutex);
+ results->push_back(std::move(res));
+ all_done = results->size() == expected_results;
+
+ if (all_done) {
+ bool error_found{false};
+ std::stringstream msg_builder;
+ for (auto &result : *results) {
+ if (!result.has_error()) {
+ continue;
+ }
+
+ if (!error_found) {
+ msg_builder << "One or more cancel actions failed: " << result.error().what_str();
+ error_found = true;
+ continue;
+ }
+
+ msg_builder << ", " << result.error().what_str();
+ }
+
+ { // Result mutex locking scope.
+ std::lock_guard guard1(m_mutex);
+ if (error_found) {
+ m_result = {ignite_error(msg_builder.str())};
+ } else {
+ m_result = ignite_result<void>{};
+ }
+ }
+ }
+ }
+
+ if (all_done) {
+ std::lock_guard guard0(m_mutex);
+ for (auto &cb : m_callbacks) {
+ cb(ignite_result<void>{*m_result});
+ }
+ }
+ });
+ }
+}
Review Comment:
I've actually re-factored the whole thing to make it more clear.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "mpoeter (via GitHub)" <gi...@apache.org>.
mpoeter commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022717208
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.cpp:
##########
@@ -0,0 +1,121 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#include "ignite/client/detail/cancellation_token_impl.h"
+
+#include <sstream>
+
+namespace ignite
+{
+
+void cancellation_token_impl::cancel_async(ignite_callback<void> callback) {
+ std::lock_guard guard(m_mutex);
+
+ if (is_cancelled()) {
+ if (m_result) {
+ callback(ignite_result<void>{*m_result});
Review Comment:
Got it! 👍
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "isapego (via GitHub)" <gi...@apache.org>.
isapego commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022790027
##########
modules/platforms/cpp/ignite/client/sql/sql.h:
##########
@@ -44,49 +45,57 @@ class sql {
sql() = delete;
/**
- * Executes single SQL statement asynchronously and returns rows.
+ * Executes a single SQL statement asynchronously and returns rows.
*
* @param tx Optional transaction. If nullptr implicit transaction for this single operation is used.
+ * @param token Cancellation token. Can be @c nullptr.
* @param statement Statement to execute.
* @param args Arguments for the statement (can be empty).
* @param callback A callback called on operation completion with SQL result set.
*/
- IGNITE_API void execute_async(transaction *tx, const sql_statement &statement, std::vector<primitive> args,
- ignite_callback<result_set> callback);
+ IGNITE_API void execute_async(transaction *tx, cancellation_token *token, const sql_statement &statement,
+ std::vector<primitive> args, ignite_callback<result_set> callback);
Review Comment:
This issue should probably be addressed in another way, because right now we have like 5 arguments, 4 of which can be optional independently of each other. That's a hell of overloads. I prefer to live it like this until we come up with a common solution for cases like this - we can always add overloads in future.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "isapego (via GitHub)" <gi...@apache.org>.
isapego commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022596886
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.cpp:
##########
@@ -0,0 +1,121 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#include "ignite/client/detail/cancellation_token_impl.h"
+
+#include <sstream>
+
+namespace ignite
+{
+
+void cancellation_token_impl::cancel_async(ignite_callback<void> callback) {
+ std::lock_guard guard(m_mutex);
+
+ if (is_cancelled()) {
+ if (m_result) {
+ callback(ignite_result<void>{*m_result});
+ } else {
+ m_callbacks.push_back(std::move(callback));
+ }
+ return;
+ }
+
+ m_cancelled = true;
+ m_callbacks.push_back(std::move(callback));
+
+ if (m_actions.empty()) {
+ m_result = ignite_result<void>{};
+ for (auto &cb : m_callbacks) {
+ cb(ignite_result<void>{*m_result});
+ }
+ return;
+ }
+
+ auto results = std::make_shared<std::vector<ignite_result<void>>>();
+ auto results_mutex = std::make_shared<std::mutex>();
+ auto expected_results = m_actions.size();
+ for (auto &action : m_actions) {
+ action([this, results, results_mutex, expected_results] (ignite_result<void> res) {
+ bool all_done{false};
+ { // Mutex locking scope
+ std::lock_guard guard0(*results_mutex);
+ results->push_back(std::move(res));
+ all_done = results->size() == expected_results;
+
+ if (all_done) {
+ bool error_found{false};
+ std::stringstream msg_builder;
+ for (auto &result : *results) {
+ if (!result.has_error()) {
+ continue;
+ }
+
+ if (!error_found) {
+ msg_builder << "One or more cancel actions failed: " << result.error().what_str();
+ error_found = true;
+ continue;
+ }
+
+ msg_builder << ", " << result.error().what_str();
+ }
+
+ { // Result mutex locking scope.
+ std::lock_guard guard1(m_mutex);
+ if (error_found) {
+ m_result = {ignite_error(msg_builder.str())};
+ } else {
+ m_result = ignite_result<void>{};
+ }
+ }
+ }
+ }
+
+ if (all_done) {
+ std::lock_guard guard0(m_mutex);
+ for (auto &cb : m_callbacks) {
+ cb(ignite_result<void>{*m_result});
+ }
+ }
+ });
+ }
+}
Review Comment:
Yep, makes sense.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "isapego (via GitHub)" <gi...@apache.org>.
isapego commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022601723
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.h:
##########
@@ -0,0 +1,85 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#pragma once
+
+#include "ignite/common/ignite_result.h"
+#include "ignite/client/cancellation_token.h"
+
+#include <optional>
+#include <mutex>
+#include <vector>
+#include <functional>
+#include <ignite/client/ignite_logger.h>
+
+namespace ignite
+{
+
+/**
+ * A cancellation token implementation.
+ */
+class cancellation_token_impl : public cancellation_token, public std::enable_shared_from_this<cancellation_token_impl>
+{
+public:
+ /**
+ * Destructor.
+ */
+ ~cancellation_token_impl() override = default;
+
+ /**
+ * Abruptly terminates an execution of an associated process.
+ *
+ * @param callback A callback that will be called after the process has been terminated and the resources associated
+ * with that process have been freed.
+ */
+ void cancel_async(ignite_callback<void> callback);
+
+ /**
+ * Adds an action to perform on cancellation.
+ *
+ * @param logger Logger to use if the operation was already canceled.
+ * @param action An action to perform on cancellation.
+ */
+ void add_action(std::shared_ptr<ignite_logger> logger, std::function<void(ignite_callback<void>)> action);
+
+ /**
+ * Flag indicating whether cancellation was requested or not.
+ *
+ * This method will return true even if cancellation has not been completed yet.
+ *
+ * @return @c true if the cancellation was requested.
+ */
+ bool is_cancelled() const { return m_cancelled; }
+
+private:
+ /** Mutex. */
+ std::mutex m_mutex{};
+
+ /** Cancel flag. */
+ bool m_cancelled{false};
Review Comment:
Probably, `volatile` is going to be enough here. But I agree, it's easier to just use `atomic`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "isapego (via GitHub)" <gi...@apache.org>.
isapego commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022605447
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.cpp:
##########
@@ -0,0 +1,121 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#include "ignite/client/detail/cancellation_token_impl.h"
+
+#include <sstream>
+
+namespace ignite
+{
+
+void cancellation_token_impl::cancel_async(ignite_callback<void> callback) {
+ std::lock_guard guard(m_mutex);
+
+ if (is_cancelled()) {
+ if (m_result) {
+ callback(ignite_result<void>{*m_result});
+ } else {
+ m_callbacks.push_back(std::move(callback));
+ }
+ return;
+ }
+
+ m_cancelled = true;
+ m_callbacks.push_back(std::move(callback));
+
+ if (m_actions.empty()) {
+ m_result = ignite_result<void>{};
+ for (auto &cb : m_callbacks) {
+ cb(ignite_result<void>{*m_result});
+ }
+ return;
+ }
+
+ auto results = std::make_shared<std::vector<ignite_result<void>>>();
+ auto results_mutex = std::make_shared<std::mutex>();
+ auto expected_results = m_actions.size();
+ for (auto &action : m_actions) {
+ action([this, results, results_mutex, expected_results] (ignite_result<void> res) {
+ bool all_done{false};
+ { // Mutex locking scope
+ std::lock_guard guard0(*results_mutex);
+ results->push_back(std::move(res));
+ all_done = results->size() == expected_results;
+
+ if (all_done) {
+ bool error_found{false};
+ std::stringstream msg_builder;
+ for (auto &result : *results) {
+ if (!result.has_error()) {
+ continue;
+ }
+
+ if (!error_found) {
+ msg_builder << "One or more cancel actions failed: " << result.error().what_str();
+ error_found = true;
+ continue;
+ }
+
+ msg_builder << ", " << result.error().what_str();
+ }
+
+ { // Result mutex locking scope.
+ std::lock_guard guard1(m_mutex);
+ if (error_found) {
+ m_result = {ignite_error(msg_builder.str())};
+ } else {
+ m_result = ignite_result<void>{};
+ }
+ }
+ }
+ }
+
+ if (all_done) {
+ std::lock_guard guard0(m_mutex);
+ for (auto &cb : m_callbacks) {
+ cb(ignite_result<void>{*m_result});
+ }
+ }
+ });
+ }
+}
+
+void cancellation_token_impl::add_action(std::shared_ptr<ignite_logger> logger,
+ std::function<void(ignite_callback<void>)> action) {
+ auto callback = [logger](ignite_result<void> res) {
+ if (res.has_error()) {
+ logger->log_warning(
+ "Cancellation action that was added after the token was already canceled: " + res.error().what_str());
+ }
+ };
+
+ if (is_cancelled()) {
+ action(callback);
+ return;
+ }
+
+ std::unique_lock lock(m_mutex);
+ if (is_cancelled()) {
+ lock.unlock();
+ action(callback);
+ return;
+ }
+
Review Comment:
Yeah, it should probably be atomic anyway, as `is_cancelled()` is a public method.
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.cpp:
##########
@@ -0,0 +1,121 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#include "ignite/client/detail/cancellation_token_impl.h"
+
+#include <sstream>
+
+namespace ignite
+{
+
+void cancellation_token_impl::cancel_async(ignite_callback<void> callback) {
+ std::lock_guard guard(m_mutex);
+
+ if (is_cancelled()) {
+ if (m_result) {
+ callback(ignite_result<void>{*m_result});
+ } else {
+ m_callbacks.push_back(std::move(callback));
+ }
+ return;
+ }
+
+ m_cancelled = true;
+ m_callbacks.push_back(std::move(callback));
+
+ if (m_actions.empty()) {
+ m_result = ignite_result<void>{};
+ for (auto &cb : m_callbacks) {
+ cb(ignite_result<void>{*m_result});
+ }
+ return;
+ }
+
+ auto results = std::make_shared<std::vector<ignite_result<void>>>();
+ auto results_mutex = std::make_shared<std::mutex>();
+ auto expected_results = m_actions.size();
+ for (auto &action : m_actions) {
+ action([this, results, results_mutex, expected_results] (ignite_result<void> res) {
+ bool all_done{false};
+ { // Mutex locking scope
+ std::lock_guard guard0(*results_mutex);
+ results->push_back(std::move(res));
+ all_done = results->size() == expected_results;
+
+ if (all_done) {
+ bool error_found{false};
+ std::stringstream msg_builder;
+ for (auto &result : *results) {
+ if (!result.has_error()) {
+ continue;
+ }
+
+ if (!error_found) {
+ msg_builder << "One or more cancel actions failed: " << result.error().what_str();
+ error_found = true;
+ continue;
+ }
+
+ msg_builder << ", " << result.error().what_str();
+ }
+
+ { // Result mutex locking scope.
+ std::lock_guard guard1(m_mutex);
+ if (error_found) {
+ m_result = {ignite_error(msg_builder.str())};
+ } else {
+ m_result = ignite_result<void>{};
+ }
+ }
+ }
+ }
+
+ if (all_done) {
+ std::lock_guard guard0(m_mutex);
+ for (auto &cb : m_callbacks) {
+ cb(ignite_result<void>{*m_result});
+ }
+ }
+ });
+ }
+}
+
+void cancellation_token_impl::add_action(std::shared_ptr<ignite_logger> logger,
+ std::function<void(ignite_callback<void>)> action) {
+ auto callback = [logger](ignite_result<void> res) {
+ if (res.has_error()) {
+ logger->log_warning(
+ "Cancellation action that was added after the token was already canceled: " + res.error().what_str());
+ }
+ };
+
+ if (is_cancelled()) {
+ action(callback);
+ return;
+ }
+
+ std::unique_lock lock(m_mutex);
+ if (is_cancelled()) {
+ lock.unlock();
+ action(callback);
+ return;
+ }
+
Review Comment:
Yeah, it should probably be atomic anyway, since `is_cancelled()` is a public method.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "isapego (via GitHub)" <gi...@apache.org>.
isapego commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022715900
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.cpp:
##########
@@ -0,0 +1,121 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#include "ignite/client/detail/cancellation_token_impl.h"
+
+#include <sstream>
+
+namespace ignite
+{
+
+void cancellation_token_impl::cancel_async(ignite_callback<void> callback) {
+ std::lock_guard guard(m_mutex);
+
+ if (is_cancelled()) {
+ if (m_result) {
+ callback(ignite_result<void>{*m_result});
Review Comment:
Well, it's just to make sure we're not forgetting to move values, basically, because as I said in most cases it totally makes sense, but if the callback takes a value, we can just leave copying somewhere by mistake. So, long story short, I decided to be more explicit about it as it's not a public API and does not need to be pretty anyway.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "isapego (via GitHub)" <gi...@apache.org>.
isapego commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022817067
##########
modules/platforms/cpp/ignite/client/detail/sql/sql_impl.cpp:
##########
@@ -88,8 +89,28 @@ void write_args(protocol::writer &writer, const std::vector<primitive> &args) {
writer.write_binary(args_data);
}
-void sql_impl::execute_async(transaction *tx, const sql_statement &statement, std::vector<primitive> &&args,
- ignite_callback<result_set> &&callback) {
+void add_action(cancellation_token &token, node_connection &connection, std::int64_t req_id) {
+ auto writer_func = [req_id](protocol::writer &writer) {
+ writer.write(req_id);
+ };
+
+ cancellation_token_impl &token_impl = static_cast<cancellation_token_impl&>(token);
+ token_impl.add_action(connection.get_logger(), [req_id, &connection, writer_func] (ignite_callback<void> callback) {
+ connection.perform_request<void>(protocol::client_operation::SQL_CANCEL_EXEC,
+ writer_func, [] (protocol::reader&){}, std::move(callback));
+ });
Review Comment:
You actually right, I should pass `shared_ptr` here. Fixed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "mpoeter (via GitHub)" <gi...@apache.org>.
mpoeter commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022573722
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.cpp:
##########
@@ -0,0 +1,121 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#include "ignite/client/detail/cancellation_token_impl.h"
+
+#include <sstream>
+
+namespace ignite
+{
+
+void cancellation_token_impl::cancel_async(ignite_callback<void> callback) {
+ std::lock_guard guard(m_mutex);
+
+ if (is_cancelled()) {
+ if (m_result) {
+ callback(ignite_result<void>{*m_result});
+ } else {
+ m_callbacks.push_back(std::move(callback));
+ }
+ return;
+ }
+
+ m_cancelled = true;
+ m_callbacks.push_back(std::move(callback));
+
+ if (m_actions.empty()) {
+ m_result = ignite_result<void>{};
+ for (auto &cb : m_callbacks) {
+ cb(ignite_result<void>{*m_result});
+ }
+ return;
+ }
+
+ auto results = std::make_shared<std::vector<ignite_result<void>>>();
+ auto results_mutex = std::make_shared<std::mutex>();
+ auto expected_results = m_actions.size();
+ for (auto &action : m_actions) {
+ action([this, results, results_mutex, expected_results] (ignite_result<void> res) {
+ bool all_done{false};
Review Comment:
We are capturing `this` here and we use some mutex to protect the results list, so I take it that these action callbacks might be called concurrently/asynchronously. How do we make sure that `this` lives long enough? Since we inherit from `enabled_shared_from_this` I think we should capture a `shared_ptr` instead.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "isapego (via GitHub)" <gi...@apache.org>.
isapego commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022592310
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.cpp:
##########
@@ -0,0 +1,121 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#include "ignite/client/detail/cancellation_token_impl.h"
+
+#include <sstream>
+
+namespace ignite
+{
+
+void cancellation_token_impl::cancel_async(ignite_callback<void> callback) {
+ std::lock_guard guard(m_mutex);
+
+ if (is_cancelled()) {
+ if (m_result) {
+ callback(ignite_result<void>{*m_result});
Review Comment:
Well, that's because `callback` actually needs rvalue (`&&`). In most cases it makes sense as there is only one callback ordinary and we can pass a result value by moving it so that user don't need to copy any data. In this case we can have multiple callbacks though, so we create a copy for everyone.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] IGNITE-23439 C++: Implement query cancellation [ignite-3]
Posted by "isapego (via GitHub)" <gi...@apache.org>.
isapego commented on code in PR #5532:
URL: https://github.com/apache/ignite-3/pull/5532#discussion_r2022592892
##########
modules/platforms/cpp/ignite/client/detail/cancellation_token_impl.cpp:
##########
@@ -0,0 +1,121 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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.
+*/
+
+#include "ignite/client/detail/cancellation_token_impl.h"
+
+#include <sstream>
+
+namespace ignite
+{
+
+void cancellation_token_impl::cancel_async(ignite_callback<void> callback) {
+ std::lock_guard guard(m_mutex);
+
+ if (is_cancelled()) {
+ if (m_result) {
+ callback(ignite_result<void>{*m_result});
+ } else {
+ m_callbacks.push_back(std::move(callback));
+ }
+ return;
+ }
+
+ m_cancelled = true;
+ m_callbacks.push_back(std::move(callback));
+
+ if (m_actions.empty()) {
+ m_result = ignite_result<void>{};
+ for (auto &cb : m_callbacks) {
+ cb(ignite_result<void>{*m_result});
Review Comment:
Yeah, the same reason.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org