summaryrefslogtreecommitdiffstats
path: root/wsrep-lib/test/mock_storage_service.cpp
blob: db513bc47f87f6fff39751db210104a7475373fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright (C) 2018-2019 Codership Oy <info@codership.com>
 *
 * 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/>.
 */

#include "mock_storage_service.hpp"
#include "mock_server_state.hpp"

#include "wsrep/client_state.hpp"

wsrep::mock_storage_service::mock_storage_service(
    wsrep::server_state& server_state,
    wsrep::client_id client_id)
    : client_service_(&client_state_)
    , client_state_(server_state, client_service_, client_id,
                    wsrep::client_state::m_high_priority)
{
    client_state_.open(client_id);
    client_state_.before_command();
}


wsrep::mock_storage_service::~mock_storage_service()
{
    client_state_.after_command_before_result();
    client_state_.after_command_after_result();
    client_state_.close();
    client_state_.cleanup();
}

int wsrep::mock_storage_service::start_transaction(
    const wsrep::ws_handle& ws_handle)
{
    return client_state_.start_transaction(ws_handle.transaction_id());
}

void wsrep::mock_storage_service::adopt_transaction(
    const wsrep::transaction& transaction)
{
    client_state_.adopt_transaction(transaction);
}

int wsrep::mock_storage_service::commit(const wsrep::ws_handle& ws_handle,
                                        const wsrep::ws_meta& ws_meta)
{
    // the logic here matches mariadb's wsrep_storage_service::commit
    bool ret = 0;
    const bool is_ordered = !ws_meta.seqno().is_undefined();
    if (is_ordered)
    {
        ret = ret || client_state_.prepare_for_ordering(ws_handle, ws_meta, true);
        ret = ret || client_state_.before_commit();
        ret = ret || client_state_.ordered_commit();
        ret = ret || client_state_.after_commit();
    }

    if (!is_ordered)
    {
        client_state_.before_rollback();
        client_state_.after_rollback();
    }
    else if (ret)
    {
        client_state_.prepare_for_ordering(wsrep::ws_handle(), wsrep::ws_meta(), false);
    }

    client_state_.after_applying();
    return ret;
}

int wsrep::mock_storage_service::rollback(const wsrep::ws_handle& ws_handle,
                                          const wsrep::ws_meta& ws_meta)
{
    int ret(client_state_.prepare_for_ordering(
                ws_handle, ws_meta, false) ||
            client_state_.before_rollback() ||
            client_state_.after_rollback());
    client_state_.after_applying();
    return ret;
}