1
0
mirror of https://github.com/codership/wsrep-lib.git synced 2025-07-28 20:02:00 +03:00

Refactored client_service interface out of client_context

This commit is contained in:
Teemu Ollakka
2018-06-14 19:44:38 +03:00
parent 1ca3f7b649
commit 256000f934
12 changed files with 391 additions and 265 deletions

View File

@ -17,7 +17,7 @@ namespace
{
replicating_client_fixture_sync_rm()
: sc("s1", "s1", wsrep::server_context::rm_sync)
, cc(sc, wsrep::client_id(1),
, cc(sc, sc.client_service(), wsrep::client_id(1),
wsrep::client_context::m_replicating)
, tc(cc.transaction())
{
@ -36,7 +36,7 @@ namespace
{
replicating_client_fixture_async_rm()
: sc("s1", "s1", wsrep::server_context::rm_async)
, cc(sc, wsrep::client_id(1),
, cc(sc, sc.client_service(), wsrep::client_id(1),
wsrep::client_context::m_replicating)
, tc(cc.transaction())
{
@ -55,10 +55,11 @@ namespace
{
replicating_client_fixture_2pc()
: sc("s1", "s1", wsrep::server_context::rm_sync)
, cc(sc, wsrep::client_id(1),
wsrep::client_context::m_replicating, false, true)
, cc(sc, sc.client_service(), wsrep::client_id(1),
wsrep::client_context::m_replicating)
, tc(cc.transaction())
{
sc.client_service().do_2pc_ = true;
BOOST_REQUIRE(cc.before_command() == 0);
BOOST_REQUIRE(cc.before_statement() == 0);
// Verify initial state
@ -74,10 +75,11 @@ namespace
{
replicating_client_fixture_autocommit()
: sc("s1", "s1", wsrep::server_context::rm_sync)
, cc(sc, wsrep::client_id(1),
wsrep::client_context::m_replicating, true)
, cc(sc, sc.client_service(), wsrep::client_id(1),
wsrep::client_context::m_replicating)
, tc(cc.transaction())
{
sc.client_service().is_autocommit_ = true;
BOOST_REQUIRE(cc.before_command() == 0);
BOOST_REQUIRE(cc.before_statement() == 0);
// Verify initial state
@ -94,7 +96,7 @@ namespace
applying_client_fixture()
: sc("s1", "s1",
wsrep::server_context::rm_async)
, cc(sc,
, cc(sc, sc.client_service(),
wsrep::client_id(1),
wsrep::client_context::m_applier)
, tc(cc.transaction())
@ -122,11 +124,12 @@ namespace
applying_client_fixture_2pc()
: sc("s1", "s1",
wsrep::server_context::rm_async)
, cc(sc,
, cc(sc, sc.client_service(),
wsrep::client_id(1),
wsrep::client_context::m_applier, false, true)
wsrep::client_context::m_applier)
, tc(cc.transaction())
{
sc.client_service().do_2pc_ = true;
BOOST_REQUIRE(cc.before_command() == 0);
BOOST_REQUIRE(cc.before_statement() == 0);
wsrep::ws_handle ws_handle(1, (void*)1);
@ -149,7 +152,7 @@ namespace
{
streaming_client_fixture_row()
: sc("s1", "s1", wsrep::server_context::rm_sync)
, cc(sc, wsrep::client_id(1),
, cc(sc, sc.client_service(), wsrep::client_id(1),
wsrep::client_context::m_replicating)
, tc(cc.transaction())
{
@ -169,7 +172,7 @@ namespace
{
streaming_client_fixture_byte()
: sc("s1", "s1", wsrep::server_context::rm_sync)
, cc(sc, wsrep::client_id(1),
, cc(sc, sc.client_service(), wsrep::client_id(1),
wsrep::client_context::m_replicating)
, tc(cc.transaction())
{
@ -189,7 +192,7 @@ namespace
{
streaming_client_fixture_statement()
: sc("s1", "s1", wsrep::server_context::rm_sync)
, cc(sc, wsrep::client_id(1),
, cc(sc, sc.client_service(), wsrep::client_id(1),
wsrep::client_context::m_replicating)
, tc(cc.transaction())
{

View File

@ -6,47 +6,49 @@
#include "mock_client_context.hpp"
int wsrep::mock_client_context::apply(
int wsrep::mock_client_service::apply(
wsrep::client_context& client_context,
const wsrep::const_buffer& data __attribute__((unused)))
{
assert(transaction_.state() == wsrep::transaction_context::s_executing ||
transaction_.state() == wsrep::transaction_context::s_replaying);
assert(client_context.transaction().state() == wsrep::transaction_context::s_executing ||
client_context.transaction().state() == wsrep::transaction_context::s_replaying);
return (fail_next_applying_ ? 1 : 0);
}
int wsrep::mock_client_context::commit()
int wsrep::mock_client_service::commit(wsrep::client_context& client_context, const wsrep::ws_handle&, const wsrep::ws_meta&)
{
int ret(0);
if (do_2pc())
{
if (before_prepare())
if (client_context.before_prepare())
{
ret = 1;
}
else if (after_prepare())
else if (client_context.after_prepare())
{
ret = 1;
}
}
if (ret == 0 &&
(transaction_.before_commit() ||
transaction_.ordered_commit() ||
transaction_.after_commit()))
(client_context.before_commit() ||
client_context.ordered_commit() ||
client_context.after_commit()))
{
ret = 1;
}
return ret;
}
int wsrep::mock_client_context::rollback()
int wsrep::mock_client_service::rollback(
wsrep::client_context& client_context)
{
int ret(0);
if (transaction_.before_rollback())
if (client_context.before_rollback())
{
ret = 1;
}
else if (transaction_.after_rollback())
else if (client_context.after_rollback())
{
ret = 1;
}

View File

@ -2,8 +2,8 @@
// Copyright (C) 2018 Codership Oy <info@codership.com>
//
#ifndef WSREP_FAKE_CLIENT_CONTEXT_HPP
#define WSREP_FAKE_CLIENT_CONTEXT_HPP
#ifndef WSREP_MOCK_CLIENT_CONTEXT_HPP
#define WSREP_MOCK_CLIENT_CONTEXT_HPP
#include "wsrep/client_context.hpp"
#include "wsrep/mutex.hpp"
@ -17,16 +17,35 @@ namespace wsrep
{
public:
mock_client_context(wsrep::server_context& server_context,
wsrep::client_service& client_service,
const wsrep::client_id& id,
enum wsrep::client_context::mode mode,
bool is_autocommit = false,
bool do_2pc = false)
: wsrep::client_context(mutex_, server_context, id, mode)
enum wsrep::client_context::mode mode)
: wsrep::client_context(mutex_, server_context, client_service, id, mode)
// Note: Mutex is initialized only after passed
// to client_context constructor.
, mutex_()
, is_autocommit_(is_autocommit)
, do_2pc_(do_2pc)
{ }
~mock_client_context()
{
if (transaction().active())
{
(void)rollback();
}
}
private:
wsrep::default_mutex mutex_;
public:
private:
};
class mock_client_service : public wsrep::client_service
{
public:
mock_client_service(wsrep::provider& provider)
: wsrep::client_service(provider)
, is_autocommit_()
, do_2pc_()
, fail_next_applying_()
, bf_abort_during_wait_()
, error_during_prepare_data_()
@ -37,43 +56,58 @@ namespace wsrep
, replays_()
, aborts_()
{ }
~mock_client_context()
{
if (transaction().active())
{
(void)rollback();
}
}
int apply(const wsrep::const_buffer&);
int commit();
int rollback();
bool is_autocommit() const { return is_autocommit_; }
bool do_2pc() const { return do_2pc_; }
int apply(wsrep::client_context&, const wsrep::const_buffer&) WSREP_OVERRIDE;
int commit(wsrep::client_context&, const wsrep::ws_handle&, const wsrep::ws_meta&)
WSREP_OVERRIDE;
int rollback(wsrep::client_context&) WSREP_OVERRIDE;
bool is_autocommit() const WSREP_OVERRIDE
{ return is_autocommit_; }
bool do_2pc() const WSREP_OVERRIDE
{ return do_2pc_; }
bool interrupted() const WSREP_OVERRIDE
{ return false; }
void reset_globals() WSREP_OVERRIDE { }
void emergency_shutdown() WSREP_OVERRIDE { }
int append_fragment(const wsrep::transaction_context&,
int, const wsrep::const_buffer&) WSREP_OVERRIDE
{ return 0; }
void remove_fragments(const wsrep::transaction_context& )
WSREP_OVERRIDE { }
void will_replay(wsrep::transaction_context&) WSREP_OVERRIDE { }
void will_replay(const wsrep::transaction_context&)
WSREP_OVERRIDE { }
enum wsrep::provider::status
replay(wsrep::transaction_context& tc) WSREP_OVERRIDE
{
enum wsrep::provider::status ret(
provider().replay(tc.ws_handle(), this));
provider_.replay(tc.ws_handle(), this));
++replays_;
return ret;
}
void wait_for_replayers(wsrep::unique_lock<wsrep::mutex>& lock)
void wait_for_replayers(
wsrep::client_context& client_context,
wsrep::unique_lock<wsrep::mutex>& lock)
WSREP_OVERRIDE
{
lock.unlock();
if (bf_abort_during_wait_)
{
wsrep_test::bf_abort_unordered(*this);
wsrep_test::bf_abort_unordered(client_context);
}
lock.lock();
}
int prepare_data_for_replication(
wsrep::client_context& client_context,
const wsrep::transaction_context&) WSREP_OVERRIDE
{
if (error_during_prepare_data_)
@ -82,7 +116,7 @@ namespace wsrep
}
static const char buf[1] = { 1 };
wsrep::const_buffer data = wsrep::const_buffer(buf, 1);
return transaction_.append_data(data);
return client_context.append_data(data);
}
size_t bytes_generated() const
@ -90,8 +124,10 @@ namespace wsrep
return bytes_generated_;
}
int prepare_fragment_for_replication(const wsrep::transaction_context&,
wsrep::mutable_buffer& buffer)
int prepare_fragment_for_replication(
wsrep::client_context& client_context,
const wsrep::transaction_context&,
wsrep::mutable_buffer& buffer)
WSREP_OVERRIDE
{
if (error_during_prepare_data_)
@ -101,39 +137,40 @@ namespace wsrep
static const char buf[1] = { 1 };
buffer.push_back(&buf[0], &buf[1]);
wsrep::const_buffer data(buffer.data(), buffer.size());
return transaction_.append_data(data);
return client_context.append_data(data);
}
bool killed() const WSREP_OVERRIDE { return killed_before_certify_; }
void abort() WSREP_OVERRIDE { ++aborts_; }
void store_globals() WSREP_OVERRIDE { }
void debug_sync(const char* sync_point) WSREP_OVERRIDE
void debug_sync(wsrep::client_context& client_context,
const char* sync_point) WSREP_OVERRIDE
{
if (sync_point_enabled_ == sync_point)
{
switch (sync_point_action_)
{
case spa_bf_abort_unordered:
wsrep_test::bf_abort_unordered(*this);
wsrep_test::bf_abort_unordered(client_context);
break;
case spa_bf_abort_ordered:
wsrep_test::bf_abort_ordered(*this);
wsrep_test::bf_abort_ordered(client_context);
break;
}
}
}
void debug_suicide(const char*) WSREP_OVERRIDE
void debug_crash(const char*) WSREP_OVERRIDE
{
// Not going to do this while unit testing
}
void on_error(enum wsrep::client_error) { }
size_t replays() const { return replays_; }
size_t aborts() const { return aborts_; }
//
// Knobs to tune the behavior
//
private:
wsrep::default_mutex mutex_;
public:
bool is_autocommit_;
bool do_2pc_;
bool fail_next_applying_;
@ -147,10 +184,18 @@ namespace wsrep
spa_bf_abort_ordered
} sync_point_action_;
size_t bytes_generated_;
//
// Verifying the state
//
size_t replays() const { return replays_; }
size_t aborts() const { return aborts_; }
private:
size_t replays_;
size_t aborts_;
};
}
#endif // WSREP_FAKE_CLIENT_CONTEXT_HPP
#endif // WSREP_MOCK_CLIENT_CONTEXT_HPP

View File

@ -13,6 +13,8 @@
#include <map>
#include <iostream> // todo: proper logging
#include <boost/test/unit_test.hpp>
namespace wsrep
{
class mock_provider : public wsrep::provider
@ -128,16 +130,27 @@ namespace wsrep
return 0;
}
enum wsrep::provider::status
commit_order_enter(const wsrep::ws_handle&,
const wsrep::ws_meta&)
{ return commit_order_enter_result_; }
commit_order_enter(const wsrep::ws_handle& ws_handle,
const wsrep::ws_meta& ws_meta)
{
BOOST_REQUIRE(ws_handle.opaque());
BOOST_REQUIRE(ws_meta.seqno().nil() == false);
return commit_order_enter_result_;
}
int commit_order_leave(const wsrep::ws_handle&,
const wsrep::ws_meta&)
{ return commit_order_leave_result_;}
int commit_order_leave(const wsrep::ws_handle& ws_handle,
const wsrep::ws_meta& ws_meta)
{
BOOST_REQUIRE(ws_handle.opaque());
BOOST_REQUIRE(ws_meta.seqno().nil() == false);
return commit_order_leave_result_;
}
int release(wsrep::ws_handle&)
{ return release_result_; }
int release(wsrep::ws_handle& )
{
// BOOST_REQUIRE(ws_handle.opaque());
return release_result_;
}
enum wsrep::provider::status replay(wsrep::ws_handle&, void* ctx)
{

View File

@ -24,21 +24,25 @@ namespace wsrep
, mutex_()
, cond_()
, provider_(*this)
, client_service_(provider_)
, last_client_id_(0)
{ }
wsrep::mock_provider& provider() const
{ return provider_; }
wsrep::client_context* local_client_context()
{
return new wsrep::mock_client_context(*this, ++last_client_id_,
wsrep::client_context::m_local);
return new wsrep::mock_client_context(
*this, client_service_, ++last_client_id_,
wsrep::client_context::m_local);
}
wsrep::client_context* streaming_applier_client_context()
{
return new wsrep::mock_client_context(
*this, ++last_client_id_, wsrep::client_context::m_applier);
*this, client_service_, ++last_client_id_,
wsrep::client_context::m_applier);
}
void log_dummy_write_set(wsrep::client_context&, const wsrep::ws_meta&)
void log_dummy_write_set(wsrep::client_context&,
const wsrep::ws_meta&)
WSREP_OVERRIDE
{
//
@ -61,11 +65,15 @@ namespace wsrep
// void sst_received(const wsrep_gtid_t&, int) WSREP_OVERRIDE { }
// void on_apply(wsrep::transaction_context&) { }
// void on_commit(wsrep::transaction_context&) { }
wsrep::mock_client_service& client_service()
{
return client_service_;
}
private:
wsrep::default_mutex mutex_;
wsrep::default_condition_variable cond_;
mutable wsrep::mock_provider provider_;
wsrep::mock_client_service client_service_;
unsigned long long last_client_id_;
};
}

View File

@ -13,10 +13,9 @@ namespace
applying_server_fixture()
: sc("s1", "s1",
wsrep::server_context::rm_sync)
, cc(sc,
, cc(sc, sc.client_service(),
wsrep::client_id(1),
wsrep::client_context::m_applier,
false)
wsrep::client_context::m_applier)
, ws_handle(1, (void*)1)
, ws_meta(wsrep::gtid(wsrep::id("1"), wsrep::seqno(1)),
wsrep::stid(wsrep::id("1"), 1, 1),
@ -62,7 +61,7 @@ BOOST_FIXTURE_TEST_CASE(server_context_applying_2pc,
BOOST_FIXTURE_TEST_CASE(server_context_applying_1pc_rollback,
applying_server_fixture)
{
cc.fail_next_applying_ = true;
sc.client_service().fail_next_applying_ = true;
char buf[1] = { 1 };
BOOST_REQUIRE(sc.on_apply(cc, ws_handle, ws_meta,
wsrep::const_buffer(buf, 1)) == 1);
@ -75,7 +74,7 @@ BOOST_FIXTURE_TEST_CASE(server_context_applying_1pc_rollback,
BOOST_FIXTURE_TEST_CASE(server_context_applying_2pc_rollback,
applying_server_fixture)
{
cc.fail_next_applying_ = true;
sc.client_service().fail_next_applying_ = true;
char buf[1] = { 1 };
BOOST_REQUIRE(sc.on_apply(cc, ws_handle, ws_meta,
wsrep::const_buffer(buf, 1)) == 1);
@ -88,9 +87,9 @@ BOOST_AUTO_TEST_CASE(server_context_streaming)
wsrep::mock_server_context sc("s1", "s1",
wsrep::server_context::rm_sync);
wsrep::mock_client_context cc(sc,
sc.client_service(),
wsrep::client_id(1),
wsrep::client_context::m_applier,
false);
wsrep::client_context::m_applier);
wsrep::ws_handle ws_handle(1, (void*)1);
wsrep::ws_meta ws_meta(wsrep::gtid(wsrep::id("1"), wsrep::seqno(1)),
wsrep::stid(wsrep::id("1"), 1, 1),

View File

@ -198,6 +198,7 @@ BOOST_FIXTURE_TEST_CASE_TEMPLATE(
transaction_context_1pc_bf_during_commit_wait_for_replayers, T,
replicating_fixtures, T)
{
wsrep::mock_server_context& sc(T::sc);
wsrep::mock_client_context& cc(T::cc);
const wsrep::transaction_context& tc(T::tc);
@ -207,7 +208,7 @@ BOOST_FIXTURE_TEST_CASE_TEMPLATE(
BOOST_REQUIRE(tc.id() == wsrep::transaction_id(1));
BOOST_REQUIRE(tc.state() == wsrep::transaction_context::s_executing);
cc.bf_abort_during_wait_ = true;
sc.client_service().bf_abort_during_wait_ = true;
// Run before commit
BOOST_REQUIRE(cc.before_commit());
@ -236,6 +237,7 @@ BOOST_FIXTURE_TEST_CASE_TEMPLATE(
transaction_context_1pc_error_during_prepare_data, T,
replicating_fixtures, T)
{
wsrep::mock_server_context& sc(T::sc);
wsrep::mock_client_context& cc(T::cc);
const wsrep::transaction_context& tc(T::tc);
@ -245,7 +247,7 @@ BOOST_FIXTURE_TEST_CASE_TEMPLATE(
BOOST_REQUIRE(tc.id() == wsrep::transaction_id(1));
BOOST_REQUIRE(tc.state() == wsrep::transaction_context::s_executing);
cc.error_during_prepare_data_ = true;
sc.client_service().error_during_prepare_data_ = true;
// Run before commit
BOOST_REQUIRE(cc.before_commit());
@ -275,6 +277,7 @@ BOOST_FIXTURE_TEST_CASE_TEMPLATE(
transaction_context_1pc_killed_before_certify, T,
replicating_fixtures, T)
{
wsrep::mock_server_context& sc(T::sc);
wsrep::mock_client_context& cc(T::cc);
const wsrep::transaction_context& tc(T::tc);
@ -284,7 +287,7 @@ BOOST_FIXTURE_TEST_CASE_TEMPLATE(
BOOST_REQUIRE(tc.id() == wsrep::transaction_id(1));
BOOST_REQUIRE(tc.state() == wsrep::transaction_context::s_executing);
cc.killed_before_certify_ = true;
sc.client_service().killed_before_certify_ = true;
// Run before commit
BOOST_REQUIRE(cc.before_commit());
@ -386,7 +389,7 @@ BOOST_FIXTURE_TEST_CASE_TEMPLATE(
BOOST_REQUIRE(tc.ordered() == false);
BOOST_REQUIRE(tc.certified() == false);
BOOST_REQUIRE(cc.current_error() == wsrep::e_success);
BOOST_REQUIRE(cc.replays() == 1);
BOOST_REQUIRE(sc.client_service().replays() == 1);
}
//
@ -407,8 +410,8 @@ BOOST_FIXTURE_TEST_CASE_TEMPLATE(
BOOST_REQUIRE(tc.active());
BOOST_REQUIRE(tc.id() == wsrep::transaction_id(1));
BOOST_REQUIRE(tc.state() == wsrep::transaction_context::s_executing);
cc.sync_point_enabled_ = "wsrep_before_certification";
cc.sync_point_action_ = wsrep::mock_client_context::spa_bf_abort_unordered;
sc.client_service().sync_point_enabled_ = "wsrep_before_certification";
sc.client_service().sync_point_action_ = wsrep::mock_client_service::spa_bf_abort_unordered;
sc.provider().certify_result_ = wsrep::provider::error_certification_failed;
BOOST_REQUIRE(cc.before_commit());
BOOST_REQUIRE(tc.state() == wsrep::transaction_context::s_cert_failed);
@ -669,7 +672,7 @@ BOOST_FIXTURE_TEST_CASE_TEMPLATE(
BOOST_REQUIRE(tc.ordered() == false);
BOOST_REQUIRE(tc.certified() == false);
BOOST_REQUIRE(cc.current_error() == wsrep::e_error_during_commit);
BOOST_REQUIRE(cc.aborts() == 1);
BOOST_REQUIRE(sc.client_service().aborts() == 1);
}
//
@ -721,7 +724,7 @@ BOOST_FIXTURE_TEST_CASE_TEMPLATE(
transaction_context_1pc_bf_abort_before_certify_regain_lock, T,
replicating_fixtures, T)
{
// wsrep::mock_server_context& sc(T::sc);
wsrep::mock_server_context& sc(T::sc);
wsrep::mock_client_context& cc(T::cc);
const wsrep::transaction_context& tc(T::tc);
@ -731,8 +734,8 @@ BOOST_FIXTURE_TEST_CASE_TEMPLATE(
BOOST_REQUIRE(tc.id() == wsrep::transaction_id(1));
BOOST_REQUIRE(tc.state() == wsrep::transaction_context::s_executing);
cc.sync_point_enabled_ = "wsrep_after_certification";
cc.sync_point_action_ = wsrep::mock_client_context::spa_bf_abort_ordered;
sc.client_service().sync_point_enabled_ = "wsrep_after_certification";
sc.client_service().sync_point_action_ = wsrep::mock_client_service::spa_bf_abort_ordered;
// Run before commit
BOOST_REQUIRE(cc.before_commit());
BOOST_REQUIRE(tc.state() == wsrep::transaction_context::s_must_replay);
@ -747,7 +750,7 @@ BOOST_FIXTURE_TEST_CASE_TEMPLATE(
// Cleanup after statement
cc.after_statement();
BOOST_REQUIRE(cc.replays() == 1);
BOOST_REQUIRE(sc.client_service().replays() == 1);
BOOST_REQUIRE(tc.active() == false);
BOOST_REQUIRE(tc.ordered() == false);
BOOST_REQUIRE(tc.certified() == false);
@ -1126,7 +1129,7 @@ BOOST_FIXTURE_TEST_CASE(transaction_context_byte_streaming_1pc_commit,
streaming_client_fixture_byte)
{
BOOST_REQUIRE(cc.start_transaction(1) == 0);
cc.bytes_generated_ = 1;
sc.client_service().bytes_generated_ = 1;
BOOST_REQUIRE(cc.after_row() == 0);
BOOST_REQUIRE(tc.streaming_context_.fragments_certified() == 1);
BOOST_REQUIRE(cc.before_commit() == 0);
@ -1145,10 +1148,10 @@ BOOST_FIXTURE_TEST_CASE(transaction_context_byte_batch_streaming_1pc_commit,
cc.enable_streaming(
wsrep::streaming_context::bytes, 2) == 0);
BOOST_REQUIRE(cc.start_transaction(1) == 0);
cc.bytes_generated_ = 1;
sc.client_service().bytes_generated_ = 1;
BOOST_REQUIRE(cc.after_row() == 0);
BOOST_REQUIRE(tc.streaming_context_.fragments_certified() == 0);
cc.bytes_generated_ = 2;
sc.client_service().bytes_generated_ = 2;
BOOST_REQUIRE(cc.after_row() == 0);
BOOST_REQUIRE(tc.streaming_context_.fragments_certified() == 1);
BOOST_REQUIRE(cc.before_commit() == 0);