Skip to content
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

PROTON-1442: [Cpp] Support for local transactions #437

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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.cpp
src/transfer.cpp
src/transport.cpp
src/type_id.cpp
Expand Down
3 changes: 2 additions & 1 deletion cpp/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ foreach(example
scheduled_send
service_bus
multithreaded_client
multithreaded_client_flow_control)
multithreaded_client_flow_control
tx_send)
add_executable(${example} ${example}.cpp)
target_link_libraries(${example} Proton::cpp Threads::Threads)
endforeach()
Expand Down
178 changes: 178 additions & 0 deletions cpp/examples/tx_send.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
*
* 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.hpp>

#include <iostream>
#include <map>
#include <string>

#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;
int confirmed = 0;

proton::container *container;
// proton::transaction_handler transaction_handler;
proton::transaction transaction;
proton::connection connection;
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 {
container = &c; // TODO: Fix error
sender = c.open_sender(url);
connection = sender.connection();
std::cout << " [on_container_start] declare_txn started..." << std::endl;
c.declare_transaction(connection, *this);
std::cout << " [on_container_start] completed!!" << &transaction
<< std::endl;
}

void on_transaction_declare_failed(proton::transaction) {}
void on_transaction_commit_failed(proton::transaction) {
std::cout << "Transaction Commit Failed" << std::endl;
connection.close();
exit(-1);
}

void on_transaction_declared(proton::transaction t) override {
std::cout << "[on_transaction_declared] txn called " << (&t)
<< std::endl;
// connection.close();
std::cout << "[on_transaction_declared] txn is_empty " << (t.is_empty())
<< "\t" << transaction.is_empty() << std::endl;
transaction = t;

send(sender);
}

void on_sendable(proton::sender &s) override {
// send();
std::cout << " [OnSendable] transaction: " << &transaction
<< std::endl;
send(s);
}

void send(proton::sender &s) {
// TODO: Add more condition in while loop
while (!transaction.is_empty() && sender.credit() &&
(committed + current_batch) < total) {
proton::message msg;
std::map<std::string, int> m;
m["sequence"] = committed + current_batch;

msg.id(committed + current_batch + 1);
msg.body(m);
std::cout << " [example] transaction send msg: " << msg
<< std::endl;
transaction.send(sender, msg);
current_batch += 1;
if(current_batch == batch_size)
{
std::cout << " >> Txn attempt commit" << std::endl;
if (batch_index % 2 == 0) {
transaction.commit();
} else {
transaction.abort();
}

transaction = proton::transaction();
batch_index++;
}
}
}

void on_tracker_accept(proton::tracker &t) override {
confirmed += 1;
std::cout << " [example] on_tracker_accept:" << confirmed
<< std::endl;
}

void on_transaction_committed(proton::transaction t) override {
committed += current_batch;
current_batch = 0;
std::cout<<" [OnTxnCommitted] Committed:"<< committed<< std::endl;
if(committed == total) {
std::cout << "All messages committed" << std::endl;
connection.close();
}
else {
container->declare_transaction(connection, *this);
}
}

void on_transaction_aborted(proton::transaction t) override {
std::cout << "Meesages Aborted ....." << std::endl;
current_batch = 0;
container->declare_transaction(connection, *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;
}
2 changes: 2 additions & 0 deletions cpp/include/proton/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ class PN_CPP_CLASS_EXTERN container {
/// Cancel task for the given work_handle.
PN_CPP_EXTERN void cancel(work_handle);

PN_CPP_EXTERN transaction declare_transaction(proton::connection conn, proton::transaction_handler &handler, bool settle_before_discharge = false);
private:
/// Declare both v03 and v11 if compiling with c++11 as the library contains both.
/// A C++11 user should never call the v03 overload so it is private in this case
Expand All @@ -326,6 +327,7 @@ class PN_CPP_CLASS_EXTERN container {
friend class receiver_options;
friend class sender_options;
friend class work_queue;
friend class transaction;
/// @endcond
};

Expand Down
2 changes: 2 additions & 0 deletions cpp/include/proton/fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class source_options;
class ssl;
class target_options;
class tracker;
class transaction;
class transaction_handler;
class transport;
class url;
class void_function0;
Expand Down
2 changes: 2 additions & 0 deletions cpp/include/proton/target_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class target_options {
/// **Unsettled API** Set the dynamic node properties.
PN_CPP_EXTERN target_options& dynamic_properties(const target::dynamic_property_map&);

PN_CPP_EXTERN target_options& type(const int);

private:
void apply(target&) const;

Expand Down
1 change: 1 addition & 0 deletions cpp/include/proton/tracker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "./binary.hpp"
#include "./internal/export.hpp"
#include "./transfer.hpp"
#include "./messaging_handler.hpp"

/// @file
/// @copybrief proton::tracker
Expand Down
123 changes: 123 additions & 0 deletions cpp/include/proton/transaction.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#ifndef PROTON_TRANSACTION_HPP
#define PROTON_TRANSACTION_HPP


/*
*
* 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 "./fwd.hpp"
#include "./internal/export.hpp"
#include "./sender.hpp"
#include "./tracker.hpp"
#include "./container.hpp"

/// @file
/// @copybrief proton::transaction

namespace proton {

class transaction_handler;

// TODO: This should not be accessible to users.
class transaction_impl {
public:
proton::sender txn_ctrl;
proton::transaction_handler *handler = nullptr;
proton::binary id;
proton::tracker _declare;
proton::tracker _discharge;
bool failed = false;
std::vector<proton::tracker> pending;

void commit();
void abort();
void declare();
proton::tracker send(proton::sender s, proton::message msg);

void discharge(bool failed);
void release_pending();
void accept(tracker &d);
void update(tracker &d, uint64_t state);
void set_id(binary _id);

proton::tracker send_ctrl(proton::symbol descriptor, proton::value _value);
void handle_outcome(proton::tracker t);
transaction_impl(proton::sender &_txn_ctrl,
proton::transaction_handler &_handler,
bool _settle_before_discharge);

// delete copy and assignment operator to ensure no copy of this object is
// every made transaction_impl(const transaction_impl&) = delete;
// transaction_impl& operator=(const transaction_impl&) = delete;
};

class
PN_CPP_CLASS_EXTERN transaction {
private:
// PN_CPP_EXTERN transaction(proton::sender& _txn_ctrl,
// proton::transaction_handler& _handler, bool _settle_before_discharge);

static transaction mk_transaction_impl(sender &s, transaction_handler &h,
bool f);
PN_CPP_EXTERN transaction(transaction_impl *impl);
transaction_impl *_impl;

public:
// TODO:
// PN_CPP_EXTERN transaction(transaction &o);
PN_CPP_EXTERN transaction();
PN_CPP_EXTERN ~transaction();
PN_CPP_EXTERN bool is_empty();
PN_CPP_EXTERN void commit();
PN_CPP_EXTERN void abort();
PN_CPP_EXTERN void declare();
PN_CPP_EXTERN void handle_outcome(proton::tracker);
PN_CPP_EXTERN proton::tracker send(proton::sender s, proton::message msg);

friend class transaction_impl;
friend class container::impl;
};

class
PN_CPP_CLASS_EXTERN transaction_handler {
public:
PN_CPP_EXTERN virtual ~transaction_handler();

/// Called when a local transaction is declared.
PN_CPP_EXTERN virtual void on_transaction_declared(transaction);

/// Called when a local transaction is discharged successfully.
PN_CPP_EXTERN virtual void on_transaction_committed(transaction);

/// Called when a local transaction is discharged unsuccessfully (aborted).
PN_CPP_EXTERN virtual void on_transaction_aborted(transaction);

/// Called when a local transaction declare fails.
PN_CPP_EXTERN virtual void on_transaction_declare_failed(transaction);

/// Called when the commit of a local transaction fails.
PN_CPP_EXTERN virtual void on_transaction_commit_failed(transaction);
};

} // namespace proton

#endif // PROTON_TRANSACTION_HPP
Loading