-
Notifications
You must be signed in to change notification settings - Fork 215
PROTON-1442: [Cpp] Support for local transactions #437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
DreamPearl
wants to merge
21
commits into
apache:main
Choose a base branch
from
DreamPearl:local-transactions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d6a9844
WIP: Python work for better transaction support
astitcher cefc581
WIP: Python example broker work for better transaction support
astitcher 81f821c
Implement local transactions
DreamPearl 5d3984c
WIP: Fix up to transactions work:
astitcher abada2f
Make example compile
DreamPearl b459f27
Add txn id in disposition frame bits
DreamPearl bf9f15e
Add transaction commit and abort functionalities
DreamPearl 547a872
Add tx_recv file
DreamPearl 9e3594b
Move the transaction_impl class logic to cpp file
DreamPearl 4bd0cd2
Has errors
DreamPearl 9787363
Add coordinator class
DreamPearl daf1160
Allow multiple transactions but one at a time
DreamPearl 4c462b6
Move txn_impl into CPP file
DreamPearl ac01d04
Clean up the code
DreamPearl c7405c7
Removed coordinator class and improved STATES handling.
DreamPearl 3834f4b
Rebased and fix seg fault
DreamPearl 6fd1434
Add tx_recv
DreamPearl 0e4b3b4
Some more clean up and improvements
DreamPearl 37379aa
Renamed transaction files to transaction_handler
DreamPearl fe1a543
Replaced static int to std::atomic<int>
DreamPearl 33516a4
Renaming and code clean
DreamPearl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* | ||
* 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 "options.hpp" | ||
|
||
#include <proton/connection.hpp> | ||
#include <proton/container.hpp> | ||
#include <proton/message.hpp> | ||
#include <proton/message_id.hpp> | ||
#include <proton/messaging_handler.hpp> | ||
#include <proton/types.hpp> | ||
#include <proton/transaction_handler.hpp> | ||
|
||
#include <iostream> | ||
#include <map> | ||
#include <string> | ||
|
||
#include <chrono> | ||
#include <thread> | ||
|
||
class tx_recv : public proton::messaging_handler, proton::transaction_handler { | ||
private: | ||
proton::receiver receiver; | ||
std::string url; | ||
int expected; | ||
int batch_size; | ||
int current_batch = 0; | ||
int committed = 0; | ||
|
||
proton::session session; | ||
public: | ||
tx_recv(const std::string &s, int c, int b): | ||
url(s), expected(c), batch_size(b) {} | ||
|
||
void on_container_start(proton::container &c) override { | ||
receiver = c.open_receiver(url); | ||
} | ||
|
||
void on_session_open(proton::session &s) override { | ||
session = s; | ||
DreamPearl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
std::cout << "Session open, declare_txn" << std::endl; | ||
s.declare_transaction(*this); | ||
} | ||
|
||
void on_transaction_declare_failed(proton::session) {} | ||
void on_transaction_commit_failed(proton::session s) { | ||
std::cout << "Transaction Commit Failed" << std::endl; | ||
s.connection().close(); | ||
exit(-1); | ||
} | ||
|
||
void on_transaction_declared(proton::session s) override { | ||
std::cout << "Transaction is declared!" << (&s) | ||
<< std::endl; | ||
receiver.add_credit(batch_size); | ||
} | ||
|
||
void on_message(proton::delivery &d, proton::message &msg) override { | ||
std::cout<<"# MESSAGE: " << msg.id() <<": " << msg.body() << std::endl; | ||
session.txn_accept(d); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. session == d.session() |
||
current_batch += 1; | ||
if(current_batch == batch_size) { | ||
} | ||
} | ||
|
||
void on_transaction_committed(proton::session s) override { | ||
committed += current_batch; | ||
current_batch = 0; | ||
std::cout<<"Transaction Committed:"<< committed<< std::endl; | ||
if(committed == expected) { | ||
std::cout << "All messages committed" << std::endl; | ||
s.connection().close(); | ||
} | ||
else { | ||
s.declare_transaction(*this); | ||
} | ||
} | ||
|
||
}; | ||
|
||
int main(int argc, char **argv) { | ||
std::string address("127.0.0.1:5672/examples"); | ||
int message_count = 6; | ||
int batch_size = 3; | ||
example::options opts(argc, argv); | ||
|
||
opts.add_value(address, 'a', "address", "connect and send to URL", "URL"); | ||
opts.add_value(message_count, 'm', "messages", "number of messages to send", "COUNT"); | ||
opts.add_value(batch_size, 'b', "batch_size", "number of messages in each transaction", "BATCH_SIZE"); | ||
|
||
try { | ||
opts.parse(); | ||
|
||
tx_recv recv(address, message_count, batch_size); | ||
proton::container(recv).run(); | ||
|
||
return 0; | ||
} catch (const example::bad_option& e) { | ||
std::cout << opts << std::endl << e.what() << std::endl; | ||
} catch (const std::exception& e) { | ||
std::cerr << e.what() << std::endl; | ||
} | ||
|
||
return 1; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* | ||
* | ||
* 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 "options.hpp" | ||
|
||
#include <proton/connection.hpp> | ||
#include <proton/container.hpp> | ||
#include <proton/message.hpp> | ||
#include <proton/message_id.hpp> | ||
#include <proton/messaging_handler.hpp> | ||
#include <proton/types.hpp> | ||
#include <proton/transaction_handler.hpp> | ||
|
||
#include <iostream> | ||
#include <map> | ||
#include <string> | ||
|
||
#include <atomic> | ||
#include <chrono> | ||
#include <thread> | ||
|
||
class tx_send : public proton::messaging_handler, proton::transaction_handler { | ||
private: | ||
proton::sender sender; | ||
std::string url; | ||
int total; | ||
int batch_size; | ||
int sent; | ||
int batch_index = 0; | ||
int current_batch = 0; | ||
int committed = 0; | ||
|
||
public: | ||
tx_send(const std::string &s, int c, int b): | ||
url(s), total(c), batch_size(b), sent(0) {} | ||
|
||
void on_container_start(proton::container &c) override { | ||
sender = c.open_sender(url); | ||
} | ||
|
||
void on_session_open(proton::session &s) override { | ||
std::cout << "New session is open, declaring transaction now..." << std::endl; | ||
s.declare_transaction(*this); | ||
} | ||
|
||
void on_transaction_declare_failed(proton::session s) { | ||
std::cout << "Transaction declarion failed" << std::endl; | ||
s.connection().close(); | ||
exit(-1); | ||
} | ||
|
||
void on_transaction_commit_failed(proton::session s) { | ||
std::cout << "Transaction commit failed!" << std::endl; | ||
s.connection().close(); | ||
exit(-1); | ||
} | ||
|
||
void on_transaction_declared(proton::session s) override { | ||
std::cout << "Transaction is declared" << std::endl; | ||
send(); | ||
} | ||
|
||
void on_sendable(proton::sender&) override { | ||
send(); | ||
} | ||
|
||
void send() { | ||
std::atomic<int> unique_id(10000); | ||
proton::session session = sender.session(); | ||
while (session.txn_is_declared() && sender.credit() && | ||
(committed + current_batch) < total) { | ||
proton::message msg; | ||
std::map<std::string, int> m; | ||
m["sequence"] = committed + current_batch; | ||
|
||
msg.id(std::atomic_fetch_add(&unique_id, 1)); | ||
msg.body(m); | ||
std::cout << "Sending: " << msg << std::endl; | ||
session.txn_send(sender, msg); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the API we were aiming for would just use a sender object created from the transactioned session. |
||
current_batch += 1; | ||
if(current_batch == batch_size) | ||
{ | ||
if (batch_index % 2 == 0) { | ||
DreamPearl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
std::cout << "Commiting transaction..." << std::endl; | ||
session.txn_commit(); | ||
} else { | ||
std::cout << "Aborting transaction..." << std::endl; | ||
session.txn_abort(); | ||
} | ||
batch_index++; | ||
} | ||
} | ||
} | ||
|
||
void on_transaction_committed(proton::session s) override { | ||
committed += current_batch; | ||
current_batch = 0; | ||
std::cout << "Transaction commited" << std::endl; | ||
if(committed == total) { | ||
std::cout << "All messages committed, closing connection." << std::endl; | ||
s.connection().close(); | ||
} | ||
else { | ||
std::cout << "Re-declaring transaction now..." << std::endl; | ||
s.declare_transaction(*this); | ||
} | ||
} | ||
|
||
void on_transaction_aborted(proton::session s) override { | ||
std::cout << "Transaction aborted!" << std::endl; | ||
std::cout << "Re-delaring transaction now..." << std::endl; | ||
current_batch = 0; | ||
s.declare_transaction(*this); | ||
} | ||
|
||
void on_sender_close(proton::sender &s) override { | ||
current_batch = 0; | ||
} | ||
|
||
}; | ||
|
||
int main(int argc, char **argv) { | ||
std::string address("127.0.0.1:5672/examples"); | ||
int message_count = 6; | ||
int batch_size = 3; | ||
example::options opts(argc, argv); | ||
|
||
opts.add_value(address, 'a', "address", "connect and send to URL", "URL"); | ||
opts.add_value(message_count, 'm', "messages", "number of messages to send", "COUNT"); | ||
opts.add_value(batch_size, 'b', "batch_size", "number of messages in each transaction", "BATCH_SIZE"); | ||
|
||
try { | ||
opts.parse(); | ||
|
||
tx_send send(address, message_count, batch_size); | ||
proton::container(send).run(); | ||
|
||
return 0; | ||
} catch (const example::bad_option& e) { | ||
std::cout << opts << std::endl << e.what() << std::endl; | ||
} catch (const std::exception& e) { | ||
std::cerr << e.what() << std::endl; | ||
} | ||
|
||
return 1; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.