Skip to content

Introduced WSREP_ASSERT() for public headers. #139

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ endif()
# Enable extra libstdc++ assertions with debug build.
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions("-D_GLIBCXX_ASSERTIONS")
add_definitions("-DWSREP_LIB_HAVE_DEBUG")
else()
# Make sure that NDEBUG is enabled in release builds even
# if the parent project does not define it.
Expand Down
2 changes: 2 additions & 0 deletions dbsim/db_client_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "db_high_priority_service.hpp"
#include "db_client.hpp"

#include <cassert>

db::client_service::client_service(db::client& client)
: wsrep::client_service()
, client_(client)
Expand Down
2 changes: 1 addition & 1 deletion dbsim/db_storage_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void db::storage_engine::transaction::start(db::client* cc)
void db::storage_engine::transaction::apply(
const wsrep::transaction& transaction)
{
assert(cc_);
WSREP_ASSERT(cc_);
se_.bf_abort_some(transaction);
}

Expand Down
42 changes: 42 additions & 0 deletions include/wsrep/assert.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2019 Codership Oy <[email protected]>
*
* 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 <https://www.gnu.org/licenses/>.
*/

/** @file assert.hpp
*
* Assert macro to be used in header files which may be included
* by the application. The macro WSREP_ASSERT() is independent of
* definition of NDEBUG and is effective only with debug builds.
*
* In order to enable WSREP_ASSERT() define WSREP_LIB_HAVE_DEBUG.
*
* The reason for this macro is to make assertions in header files
* independent of application preprocessor options.
*/

#ifndef WSREP_ASSERT_HPP
#define WSREP_ASSERT_HPP

#if defined(WSREP_LIB_HAVE_DEBUG)
#include <cassert>
#define WSREP_ASSERT(expr) assert(expr)
#else
#define WSREP_ASSERT(expr)
#endif // WSREP_LIB_HAVE_DEBUG

#endif // WSREP_ASSERT_HPP
79 changes: 40 additions & 39 deletions include/wsrep/client_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#ifndef WSREP_CLIENT_STATE_HPP
#define WSREP_CLIENT_STATE_HPP

#include "assert.hpp"
#include "server_state.hpp"
#include "server_service.hpp"
#include "provider.hpp"
Expand Down Expand Up @@ -166,7 +167,7 @@ namespace wsrep
*/
virtual ~client_state()
{
assert(transaction_.active() == false);
WSREP_ASSERT(transaction_.active() == false);
}

/** @name Client session handling */
Expand Down Expand Up @@ -270,7 +271,7 @@ namespace wsrep
*/
void after_applying()
{
assert(mode_ == m_high_priority);
WSREP_ASSERT(mode_ == m_high_priority);
transaction_.after_applying();
}

Expand All @@ -286,7 +287,7 @@ namespace wsrep
int start_transaction(const wsrep::transaction_id& id)
{
wsrep::unique_lock<wsrep::mutex> lock(mutex_);
assert(state_ == s_exec);
WSREP_ASSERT(state_ == s_exec);
return transaction_.start_transaction(id);
}

Expand All @@ -304,8 +305,8 @@ namespace wsrep
*/
int assign_read_view(const wsrep::gtid* const gtid = NULL)
{
assert(mode_ == m_local);
assert(state_ == s_exec);
WSREP_ASSERT(mode_ == m_local);
WSREP_ASSERT(state_ == s_exec);
return transaction_.assign_read_view(gtid);
}

Expand All @@ -318,8 +319,8 @@ namespace wsrep
*/
int append_key(const wsrep::key& key)
{
assert(mode_ == m_local);
assert(state_ == s_exec);
WSREP_ASSERT(mode_ == m_local);
WSREP_ASSERT(state_ == s_exec);
return transaction_.append_key(key);
}

Expand All @@ -332,8 +333,8 @@ namespace wsrep
*/
int append_keys(const wsrep::key_array& keys)
{
assert(mode_ == m_local || mode_ == m_toi);
assert(state_ == s_exec);
WSREP_ASSERT(mode_ == m_local || mode_ == m_toi);
WSREP_ASSERT(state_ == s_exec);
for (auto i(keys.begin()); i != keys.end(); ++i)
{
if (transaction_.append_key(*i))
Expand All @@ -349,8 +350,8 @@ namespace wsrep
*/
int append_data(const wsrep::const_buffer& data)
{
assert(mode_ == m_local);
assert(state_ == s_exec);
WSREP_ASSERT(mode_ == m_local);
WSREP_ASSERT(state_ == s_exec);
return transaction_.append_data(data);
}

Expand All @@ -363,8 +364,8 @@ namespace wsrep
*/
int after_row()
{
assert(mode_ == m_local);
assert(state_ == s_exec);
WSREP_ASSERT(mode_ == m_local);
WSREP_ASSERT(state_ == s_exec);
return (transaction_.streaming_context().fragment_size() ?
transaction_.after_row() : 0);
}
Expand Down Expand Up @@ -403,7 +404,7 @@ namespace wsrep

void fragment_applied(wsrep::seqno seqno)
{
assert(mode_ == m_high_priority);
WSREP_ASSERT(mode_ == m_high_priority);
transaction_.fragment_applied(seqno);
}

Expand All @@ -423,7 +424,7 @@ namespace wsrep
const wsrep::ws_meta& ws_meta,
bool is_commit)
{
assert(state_ == s_exec);
WSREP_ASSERT(state_ == s_exec);
return transaction_.prepare_for_ordering(
ws_handle, ws_meta, is_commit);
}
Expand All @@ -435,15 +436,15 @@ namespace wsrep
const wsrep::ws_meta& meta)
{
wsrep::unique_lock<wsrep::mutex> lock(mutex_);
assert(owning_thread_id_ == wsrep::this_thread::get_id());
assert(mode_ == m_high_priority);
WSREP_ASSERT(owning_thread_id_ == wsrep::this_thread::get_id());
WSREP_ASSERT(mode_ == m_high_priority);
return transaction_.start_transaction(wsh, meta);
}

int next_fragment(const wsrep::ws_meta& meta)
{
wsrep::unique_lock<wsrep::mutex> lock(mutex_);
assert(mode_ == m_high_priority);
WSREP_ASSERT(mode_ == m_high_priority);
return transaction_.next_fragment(meta);
}

Expand All @@ -452,44 +453,44 @@ namespace wsrep
int before_prepare()
{
wsrep::unique_lock<wsrep::mutex> lock(mutex_);
assert(owning_thread_id_ == wsrep::this_thread::get_id());
assert(state_ == s_exec);
WSREP_ASSERT(owning_thread_id_ == wsrep::this_thread::get_id());
WSREP_ASSERT(state_ == s_exec);
return transaction_.before_prepare(lock);
}

int after_prepare()
{
wsrep::unique_lock<wsrep::mutex> lock(mutex_);
assert(owning_thread_id_ == wsrep::this_thread::get_id());
assert(state_ == s_exec);
WSREP_ASSERT(owning_thread_id_ == wsrep::this_thread::get_id());
WSREP_ASSERT(state_ == s_exec);
return transaction_.after_prepare(lock);
}

int before_commit()
{
assert(owning_thread_id_ == wsrep::this_thread::get_id());
assert(state_ == s_exec || mode_ == m_local);
WSREP_ASSERT(owning_thread_id_ == wsrep::this_thread::get_id());
WSREP_ASSERT(state_ == s_exec || mode_ == m_local);
return transaction_.before_commit();
}

int ordered_commit()
{
assert(owning_thread_id_ == wsrep::this_thread::get_id());
assert(state_ == s_exec || mode_ == m_local);
WSREP_ASSERT(owning_thread_id_ == wsrep::this_thread::get_id());
WSREP_ASSERT(state_ == s_exec || mode_ == m_local);
return transaction_.ordered_commit();
}

int after_commit()
{
assert(owning_thread_id_ == wsrep::this_thread::get_id());
assert(state_ == s_exec || mode_ == m_local);
WSREP_ASSERT(owning_thread_id_ == wsrep::this_thread::get_id());
WSREP_ASSERT(state_ == s_exec || mode_ == m_local);
return transaction_.after_commit();
}
/** @} */
int before_rollback()
{
assert(owning_thread_id_ == wsrep::this_thread::get_id());
assert(state_ == s_idle ||
WSREP_ASSERT(owning_thread_id_ == wsrep::this_thread::get_id());
WSREP_ASSERT(state_ == s_idle ||
state_ == s_exec ||
state_ == s_result ||
state_ == s_quitting);
Expand All @@ -498,8 +499,8 @@ namespace wsrep

int after_rollback()
{
assert(owning_thread_id_ == wsrep::this_thread::get_id());
assert(state_ == s_idle ||
WSREP_ASSERT(owning_thread_id_ == wsrep::this_thread::get_id());
WSREP_ASSERT(state_ == s_idle ||
state_ == s_exec ||
state_ == s_result ||
state_ == s_quitting);
Expand Down Expand Up @@ -600,7 +601,7 @@ namespace wsrep
int bf_abort(wsrep::seqno bf_seqno)
{
wsrep::unique_lock<wsrep::mutex> lock(mutex_);
assert(mode_ == m_local || transaction_.is_streaming());
WSREP_ASSERT(mode_ == m_local || transaction_.is_streaming());
return transaction_.bf_abort(lock, bf_seqno);
}
/**
Expand All @@ -611,7 +612,7 @@ namespace wsrep
int total_order_bf_abort(wsrep::seqno bf_seqno)
{
wsrep::unique_lock<wsrep::mutex> lock(mutex_);
assert(mode_ == m_local || transaction_.is_streaming());
WSREP_ASSERT(mode_ == m_local || transaction_.is_streaming());
return transaction_.total_order_bf_abort(lock, bf_seqno);
}

Expand All @@ -624,7 +625,7 @@ namespace wsrep
*/
void adopt_transaction(const wsrep::transaction& transaction)
{
assert(mode_ == m_high_priority);
WSREP_ASSERT(mode_ == m_high_priority);
transaction_.adopt(transaction);
}

Expand All @@ -633,7 +634,7 @@ namespace wsrep
*/
void adopt_apply_error(wsrep::mutable_buffer& err)
{
assert(mode_ == m_high_priority);
WSREP_ASSERT(mode_ == m_high_priority);
transaction_.adopt_apply_error(err);
}

Expand All @@ -646,7 +647,7 @@ namespace wsrep
*/
void clone_transaction_for_replay(const wsrep::transaction& transaction)
{
// assert(mode_ == m_high_priority);
// WSREP_ASSERT(mode_ == m_high_priority);
transaction_.clone_for_replay(transaction);
}

Expand Down Expand Up @@ -1115,7 +1116,7 @@ namespace wsrep
virtual ~high_priority_context()
{
wsrep::unique_lock<wsrep::mutex> lock(client_.mutex_);
assert(client_.mode() == wsrep::client_state::m_high_priority);
WSREP_ASSERT(client_.mode() == wsrep::client_state::m_high_priority);
client_.mode(lock, orig_mode_);
}
private:
Expand All @@ -1139,7 +1140,7 @@ namespace wsrep
~client_toi_mode()
{
wsrep::unique_lock<wsrep::mutex> lock(client_.mutex_);
assert(client_.mode() == wsrep::client_state::m_toi);
WSREP_ASSERT(client_.mode() == wsrep::client_state::m_toi);
client_.mode(lock, orig_mode_);
}
private:
Expand Down
7 changes: 3 additions & 4 deletions include/wsrep/lock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
#ifndef WSREP_LOCK_HPP
#define WSREP_LOCK_HPP

#include "assert.hpp"
#include "mutex.hpp"

#include <cassert>

namespace wsrep
{
template <class M>
Expand All @@ -48,13 +47,13 @@ namespace wsrep
void lock()
{
mutex_.lock();
assert(locked_ == false);
WSREP_ASSERT(locked_ == false);
locked_ = true;
}

void unlock()
{
assert(locked_);
WSREP_ASSERT(locked_);
locked_ = false;
mutex_.unlock();
}
Expand Down
1 change: 0 additions & 1 deletion include/wsrep/provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "transaction_id.hpp"
#include "compiler.hpp"

#include <cassert>
#include <cstring>

#include <string>
Expand Down
3 changes: 2 additions & 1 deletion include/wsrep/server_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
#ifndef WSREP_SERVER_STATE_HPP
#define WSREP_SERVER_STATE_HPP

#include "assert.hpp"
#include "mutex.hpp"
#include "condition_variable.hpp"
#include "id.hpp"
Expand Down Expand Up @@ -553,7 +554,7 @@ namespace wsrep
enum state state(wsrep::unique_lock<wsrep::mutex>&
lock WSREP_UNUSED) const
{
assert(lock.owns_lock());
WSREP_ASSERT(lock.owns_lock());
return state_;
}

Expand Down
4 changes: 3 additions & 1 deletion include/wsrep/sr_key_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#ifndef WSREP_SR_KEY_SET_HPP
#define WSREP_SR_KEY_SET_HPP

#include "assert.hpp"

#include <set>
#include <map>

Expand All @@ -36,7 +38,7 @@ namespace wsrep

void insert(const wsrep::key& key)
{
assert(key.size() >= 2);
WSREP_ASSERT(key.size() >= 2);
if (key.size() < 2)
{
throw wsrep::runtime_error("Invalid key size");
Expand Down
Loading