Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d6a9844
WIP: Python work for better transaction support
astitcher May 22, 2024
cefc581
WIP: Python example broker work for better transaction support
astitcher May 22, 2024
81f821c
Implement local transactions
DreamPearl May 30, 2024
5d3984c
WIP: Fix up to transactions work:
astitcher Oct 2, 2024
abada2f
Make example compile
DreamPearl Oct 21, 2024
b459f27
Add txn id in disposition frame bits
DreamPearl Nov 19, 2024
bf9f15e
Add transaction commit and abort functionalities
DreamPearl Nov 21, 2024
547a872
Add tx_recv file
DreamPearl Nov 26, 2024
9e3594b
Move the transaction_impl class logic to cpp file
DreamPearl Jan 28, 2025
4bd0cd2
Has errors
DreamPearl Feb 8, 2025
9787363
Add coordinator class
DreamPearl Feb 11, 2025
daf1160
Allow multiple transactions but one at a time
DreamPearl Feb 13, 2025
4c462b6
Move txn_impl into CPP file
DreamPearl Feb 27, 2025
ac01d04
Clean up the code
DreamPearl Mar 17, 2025
c7405c7
Removed coordinator class and improved STATES handling.
DreamPearl Mar 27, 2025
3834f4b
Rebased and fix seg fault
DreamPearl Apr 17, 2025
6fd1434
Add tx_recv
DreamPearl Apr 17, 2025
0e4b3b4
Some more clean up and improvements
DreamPearl Apr 21, 2025
37379aa
Renamed transaction files to transaction_handler
DreamPearl Apr 21, 2025
fe1a543
Replaced static int to std::atomic<int>
DreamPearl Apr 21, 2025
33516a4
Renaming and code clean
DreamPearl May 29, 2025
18b9448
Naming and split open sender [Not working]
DreamPearl Jun 10, 2025
0a18b3a
Fix core dump in tx_send
DreamPearl Jun 15, 2025
8c6e263
Change API to use sender send instead of session:transaction_send.
DreamPearl Jul 24, 2025
6cfb78e
Accept and update takes transfer now instead of delivery and tracker.
Aug 10, 2025
7321c0e
Rename transaction_impl::send to attach_txn_id
Aug 10, 2025
8c13443
Add first version of test file
DreamPearl Aug 11, 2025
7860057
Change to let the library handle update() for transactional disposition
DreamPearl Aug 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ set(qpid-proton-cpp-source
src/terminus.cpp
src/timestamp.cpp
src/tracker.cpp
src/transaction_handler.cpp
src/transfer.cpp
src/transport.cpp
src/type_id.cpp
Expand Down
4 changes: 3 additions & 1 deletion cpp/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ foreach(example
scheduled_send
service_bus
multithreaded_client
multithreaded_client_flow_control)
multithreaded_client_flow_control
tx_send
tx_recv)
add_executable(${example} ${example}.cpp)
target_link_libraries(${example} Proton::cpp Threads::Threads)
endforeach()
Expand Down
12 changes: 9 additions & 3 deletions cpp/examples/simple_recv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ class simple_recv : public proton::messaging_handler {
proton::receiver receiver;
int expected;
int received;
bool verbose;

public:
simple_recv(const std::string &s, const std::string &u, const std::string &p, int c) :
url(s), user(u), password(p), expected(c), received(0) {}
simple_recv(const std::string &s, const std::string &u, const std::string &p, int c, bool verbose) :
url(s), user(u), password(p), expected(c), received(0), verbose(verbose) {}

void on_container_start(proton::container &c) override {
proton::connection_options co;
Expand All @@ -61,6 +62,9 @@ class simple_recv : public proton::messaging_handler {
}

if (expected == 0 || received < expected) {
if (verbose) {
std::cout << msg << ": ";
}
std::cout << msg.body() << std::endl;
received++;

Expand All @@ -77,18 +81,20 @@ int main(int argc, char **argv) {
std::string user;
std::string password;
int message_count = 100;
bool verbose;
example::options opts(argc, argv);

opts.add_value(address, 'a', "address", "connect to and receive from URL", "URL");
opts.add_value(message_count, 'm', "messages", "receive COUNT messages", "COUNT");
opts.add_value(user, 'u', "user", "authenticate as USER", "USER");
opts.add_value(password, 'p', "password", "authenticate with PASSWORD", "PASSWORD");
opts.add_flag(verbose, 'v', "verbose", "show whole message contents");


try {
opts.parse();

simple_recv recv(address, user, password, message_count);
simple_recv recv(address, user, password, message_count, verbose);
proton::container(recv).run();

return 0;
Expand Down
132 changes: 132 additions & 0 deletions cpp/examples/tx_recv.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
*
* 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 conn_url_;
std::string addr_;
int expected;
int batch_size;
int current_batch = 0;
int committed = 0;

proton::session session;
public:
tx_recv(const std::string& u, const std::string &a, int c, int b):
conn_url_(u), addr_(a), expected(c), batch_size(b) {}

void on_container_start(proton::container &c) override {
c.connect(conn_url_);
}

void on_connection_open(proton::connection& c) override {
c.open_session();
}

void on_session_open(proton::session &s) override {
s.open_sender(addr_);
session = s;
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.transaction_accept(d);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should just be accept now as this is already a transactioned session

current_batch += 1;
if(current_batch == batch_size) {
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to commit or abort transaction here

}

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 conn_url = argc > 1 ? argv[1] : "//127.0.0.1:5672";
std::string addr = argc > 2 ? argv[2] : "examples";
int message_count = 6;
int batch_size = 3;
example::options opts(argc, argv);

opts.add_value(conn_url, 'u', "url", "connect and send to URL", "URL");
opts.add_value(addr, 'a', "address", "connect and send to address", "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(conn_url, addr, 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;
}
172 changes: 172 additions & 0 deletions cpp/examples/tx_send.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
*
* 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/sender_options.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 conn_url_;
std::string addr_;
int total;
int batch_size;
int sent;
int batch_index = 0;
int current_batch = 0;
int committed = 0;

public:
tx_send(const std::string& u, const std::string& a, int c, int b):
conn_url_(u), addr_(a), total(c), batch_size(b), sent(0) {}

void on_container_start(proton::container &c) override {
c.connect(conn_url_);
}

void on_connection_open(proton::connection& c) override {
sender = c.open_sender(addr_);
}

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.transaction_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;
sender.send(msg);
current_batch += 1;
if(current_batch == batch_size)
{
if (batch_index % 2 == 0) {
std::cout << "Commiting transaction..." << std::endl;
session.transaction_commit();
} else {
std::cout << "Aborting transaction..." << std::endl;
session.transaction_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 conn_url = argc > 1 ? argv[1] : "//127.0.0.1:5672";
std::string addr = argc > 2 ? argv[2] : "examples";
int message_count = 6;
int batch_size = 3;
example::options opts(argc, argv);

opts.add_value(conn_url, 'u', "url", "connect and send to URL", "URL");
opts.add_value(addr, 'a', "address", "connect and send to address", "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(conn_url, addr, 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;
}
1 change: 1 addition & 0 deletions cpp/include/proton/fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class source_options;
class ssl;
class target_options;
class tracker;
class transaction_handler;
class transport;
class url;
class void_function0;
Expand Down
Loading