From 66ee7bed1b7fa7b6d3f584517be681aa9ea15df9 Mon Sep 17 00:00:00 2001 From: Daniele Sciascia Date: Wed, 16 Oct 2019 14:06:16 +0200 Subject: [PATCH] Add type wsrep::xid Create type `wsrep::xid`, and change all signatures that take `std::string xid` to take `wsrep::xid xid`. --- dbsim/db_high_priority_service.hpp | 2 +- dbsim/db_storage_service.hpp | 2 +- include/wsrep/client_state.hpp | 9 ++--- include/wsrep/high_priority_service.hpp | 3 +- include/wsrep/server_state.hpp | 3 +- include/wsrep/storage_service.hpp | 3 +- include/wsrep/transaction.hpp | 11 +++--- include/wsrep/xid.hpp | 47 +++++++++++++++++++++++++ src/CMakeLists.txt | 1 + src/server_state.cpp | 4 +-- src/transaction.cpp | 4 +-- src/xid.cpp | 31 ++++++++++++++++ test/mock_high_priority_service.hpp | 2 +- test/mock_storage_service.hpp | 2 +- wsrep-API/v26 | 2 +- 15 files changed, 105 insertions(+), 21 deletions(-) create mode 100644 include/wsrep/xid.hpp create mode 100644 src/xid.cpp diff --git a/dbsim/db_high_priority_service.hpp b/dbsim/db_high_priority_service.hpp index cad003d..d29626c 100644 --- a/dbsim/db_high_priority_service.hpp +++ b/dbsim/db_high_priority_service.hpp @@ -42,7 +42,7 @@ namespace db const wsrep::ws_handle&, const wsrep::ws_meta&, const wsrep::const_buffer&, - const std::string&) override + const wsrep::xid&) override { return 0; } int remove_fragments(const wsrep::ws_meta&) override { return 0; } diff --git a/dbsim/db_storage_service.hpp b/dbsim/db_storage_service.hpp index 3fe5485..839253d 100644 --- a/dbsim/db_storage_service.hpp +++ b/dbsim/db_storage_service.hpp @@ -35,7 +35,7 @@ namespace db wsrep::transaction_id, int, const wsrep::const_buffer&, - const std::string&) override + const wsrep::xid&) override { throw wsrep::not_implemented_error(); } int update_fragment_meta(const wsrep::ws_meta&) override { throw wsrep::not_implemented_error(); } diff --git a/include/wsrep/client_state.hpp b/include/wsrep/client_state.hpp index 48b54e2..6f5aa22 100644 --- a/include/wsrep/client_state.hpp +++ b/include/wsrep/client_state.hpp @@ -41,6 +41,7 @@ #include "lock.hpp" #include "buffer.hpp" #include "thread.hpp" +#include "xid.hpp" namespace wsrep { @@ -533,7 +534,7 @@ namespace wsrep * * @param xid transaction id */ - void assign_xid(const std::string& xid) + void assign_xid(const wsrep::xid& xid) { transaction_.assign_xid(xid); } @@ -544,7 +545,7 @@ namespace wsrep * The purpose of this method is to restore transaction state * during recovery of a prepared XA transaction. */ - int restore_xid(std::string& xid) + int restore_xid(const wsrep::xid& xid) { return transaction_.restore_to_prepared_state(xid); } @@ -562,7 +563,7 @@ namespace wsrep * @return Zero on success, non-zero on error. In case of error * the client_state's current_error is set */ - int commit_by_xid(const std::string& xid) + int commit_by_xid(const wsrep::xid& xid) { return transaction_.commit_or_rollback_by_xid(xid, true); } @@ -580,7 +581,7 @@ namespace wsrep * @return Zero on success, non-zero on error. In case of error * the client_state's current_error is set */ - int rollback_by_xid(const std::string& xid) + int rollback_by_xid(const wsrep::xid& xid) { return transaction_.commit_or_rollback_by_xid(xid, false); } diff --git a/include/wsrep/high_priority_service.hpp b/include/wsrep/high_priority_service.hpp index 63514dd..22ff818 100644 --- a/include/wsrep/high_priority_service.hpp +++ b/include/wsrep/high_priority_service.hpp @@ -24,6 +24,7 @@ #ifndef WSREP_HIGH_PRIORITY_SERVICE_HPP #define WSREP_HIGH_PRIORITY_SERVICE_HPP +#include "xid.hpp" #include "server_state.hpp" namespace wsrep @@ -97,7 +98,7 @@ namespace wsrep const wsrep::ws_handle& ws_handle, const wsrep::ws_meta& ws_meta, const wsrep::const_buffer& data, - const std::string& xid) = 0; + const wsrep::xid& xid) = 0; /** * Remove fragments belonging to streaming transaction. diff --git a/include/wsrep/server_state.hpp b/include/wsrep/server_state.hpp index f1c6728..6e2d9d7 100644 --- a/include/wsrep/server_state.hpp +++ b/include/wsrep/server_state.hpp @@ -90,6 +90,7 @@ #include "logger.hpp" #include "provider.hpp" #include "compiler.hpp" +#include "xid.hpp" #include #include @@ -268,7 +269,7 @@ namespace wsrep * Find a streaming applier matching xid */ wsrep::high_priority_service* find_streaming_applier( - const std::string& xid) const; + const wsrep::xid& xid) const; /** * Load WSRep provider. diff --git a/include/wsrep/storage_service.hpp b/include/wsrep/storage_service.hpp index e71431d..e68548b 100644 --- a/include/wsrep/storage_service.hpp +++ b/include/wsrep/storage_service.hpp @@ -32,6 +32,7 @@ #include "transaction_id.hpp" #include "id.hpp" #include "buffer.hpp" +#include "xid.hpp" namespace wsrep { @@ -64,7 +65,7 @@ namespace wsrep wsrep::transaction_id client_id, int flags, const wsrep::const_buffer& data, - const std::string& xid) = 0; + const wsrep::xid& xid) = 0; /** * Update fragment meta data after certification process. diff --git a/include/wsrep/transaction.hpp b/include/wsrep/transaction.hpp index 948ec94..fa7b82c 100644 --- a/include/wsrep/transaction.hpp +++ b/include/wsrep/transaction.hpp @@ -29,6 +29,7 @@ #include "sr_key_set.hpp" #include "buffer.hpp" #include "client_service.hpp" +#include "xid.hpp" #include #include @@ -127,21 +128,21 @@ namespace wsrep return !xid_.empty(); } - void assign_xid(const std::string& xid) + void assign_xid(const wsrep::xid& xid) { assert(active()); assert(xid_.empty()); xid_ = xid; } - const std::string xid() const + const wsrep::xid xid() const { return xid_; } - int restore_to_prepared_state(const std::string& xid); + int restore_to_prepared_state(const wsrep::xid& xid); - int commit_or_rollback_by_xid(const std::string& xid, bool commit); + int commit_or_rollback_by_xid(const wsrep::xid& xid, bool commit); bool pa_unsafe() const { return pa_unsafe_; } void pa_unsafe(bool pa_unsafe) { pa_unsafe_ = pa_unsafe; } @@ -273,7 +274,7 @@ namespace wsrep wsrep::streaming_context streaming_context_; wsrep::sr_key_set sr_keys_; wsrep::mutable_buffer apply_error_buf_; - std::string xid_; + wsrep::xid xid_; }; static inline const char* to_c_string(enum wsrep::transaction::state state) diff --git a/include/wsrep/xid.hpp b/include/wsrep/xid.hpp new file mode 100644 index 0000000..4a32d5a --- /dev/null +++ b/include/wsrep/xid.hpp @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2019 Codership Oy + * + * This file is part of wsrep-lib. + * + * Wsrep-lib is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Wsrep-lib is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with wsrep-lib. If not, see . + */ + +#ifndef WSREP_XID_HPP +#define WSREP_XID_HPP + +#include +#include + +namespace wsrep +{ + class xid + { + public: + xid() : xid_() { } + xid(const std::string& str) : xid_(str) { } + xid(const char* s) : xid_(s) { } + bool empty() const { return xid_.empty(); } + void clear() { xid_.clear(); } + bool operator==(const xid& other) const { return xid_ == other.xid_; } + friend std::string to_string(const wsrep::xid& xid); + friend std::ostream& operator<<(std::ostream& os, const wsrep::xid& xid); + private: + std::string xid_; + }; + + std::string to_string(const wsrep::xid& xid); + std::ostream& operator<<(std::ostream& os, const wsrep::xid& xid); +} + +#endif // WSREP_XID_HPP diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a1986cf..1e3dc37 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,6 +7,7 @@ add_library(wsrep-lib exception.cpp gtid.cpp id.cpp + xid.cpp key.cpp logger.cpp provider.cpp diff --git a/src/server_state.cpp b/src/server_state.cpp index d527768..75904e8 100644 --- a/src/server_state.cpp +++ b/src/server_state.cpp @@ -130,7 +130,7 @@ static int apply_fragment(wsrep::server_state& server_state, if (!apply_err) { high_priority_service.debug_crash("crash_apply_cb_before_append_frag"); - const std::string xid(streaming_applier->transaction().xid()); + const wsrep::xid xid(streaming_applier->transaction().xid()); ret = high_priority_service.append_fragment_and_commit( ws_handle, ws_meta, data, xid); high_priority_service.debug_crash("crash_apply_cb_after_append_frag"); @@ -1258,7 +1258,7 @@ wsrep::high_priority_service* wsrep::server_state::find_streaming_applier( } wsrep::high_priority_service* wsrep::server_state::find_streaming_applier( - const std::string& xid) const + const wsrep::xid& xid) const { wsrep::unique_lock lock(mutex_); streaming_appliers_map::const_iterator i(streaming_appliers_.begin()); diff --git a/src/transaction.cpp b/src/transaction.cpp index ff86d89..1a64dc1 100644 --- a/src/transaction.cpp +++ b/src/transaction.cpp @@ -1040,7 +1040,7 @@ void wsrep::transaction::after_replay(const wsrep::transaction& other) clear_fragments(); } -int wsrep::transaction::restore_to_prepared_state(const std::string& xid) +int wsrep::transaction::restore_to_prepared_state(const wsrep::xid& xid) { wsrep::unique_lock lock(client_state_.mutex_); assert(active()); @@ -1053,7 +1053,7 @@ int wsrep::transaction::restore_to_prepared_state(const std::string& xid) return 0; } -int wsrep::transaction::commit_or_rollback_by_xid(const std::string& xid, +int wsrep::transaction::commit_or_rollback_by_xid(const wsrep::xid& xid, bool commit) { wsrep::unique_lock lock(client_state_.mutex_); diff --git a/src/xid.cpp b/src/xid.cpp new file mode 100644 index 0000000..f1f89a8 --- /dev/null +++ b/src/xid.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2019 Codership Oy + * + * This file is part of wsrep-lib. + * + * Wsrep-lib is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Wsrep-lib is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with wsrep-lib. If not, see . + */ + +#include "wsrep/xid.hpp" +#include + +std::string wsrep::to_string(const wsrep::xid& xid) +{ + return xid.xid_; +} + +std::ostream& wsrep::operator<<(std::ostream& os, const wsrep::xid& xid) +{ + return os << xid.xid_; +} diff --git a/test/mock_high_priority_service.hpp b/test/mock_high_priority_service.hpp index 5f4ac51..e66dd46 100644 --- a/test/mock_high_priority_service.hpp +++ b/test/mock_high_priority_service.hpp @@ -55,7 +55,7 @@ namespace wsrep const wsrep::ws_handle&, const wsrep::ws_meta&, const wsrep::const_buffer&, - const std::string&) WSREP_OVERRIDE + const wsrep::xid&) WSREP_OVERRIDE { return 0; } int remove_fragments(const wsrep::ws_meta&) WSREP_OVERRIDE { return 0; } diff --git a/test/mock_storage_service.hpp b/test/mock_storage_service.hpp index 4e99329..b0275b9 100644 --- a/test/mock_storage_service.hpp +++ b/test/mock_storage_service.hpp @@ -40,7 +40,7 @@ class mock_server_state; wsrep::transaction_id, int, const wsrep::const_buffer&, - const std::string&) WSREP_OVERRIDE + const wsrep::xid&) WSREP_OVERRIDE { return 0; } int update_fragment_meta(const wsrep::ws_meta&) WSREP_OVERRIDE diff --git a/wsrep-API/v26 b/wsrep-API/v26 index 35b9ece..12a50c4 160000 --- a/wsrep-API/v26 +++ b/wsrep-API/v26 @@ -1 +1 @@ -Subproject commit 35b9ecedd84bac9931851e3f388fc96271e61f97 +Subproject commit 12a50c43b112648fec3b1213a1470a85aca55f2c