summaryrefslogtreecommitdiffstats
path: root/wsrep-lib/test/mock_high_priority_service.cpp
blob: bb67c9a9f37a9a3e53bb82cc4306634eac0d544a (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * 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_high_priority_service.hpp"
#include "mock_server_state.hpp"
#include <sstream>

int wsrep::mock_high_priority_service::start_transaction(
    const wsrep::ws_handle& ws_handle, const wsrep::ws_meta& ws_meta)
{
    return client_state_->start_transaction(ws_handle, ws_meta);
}

int wsrep::mock_high_priority_service::next_fragment(
    const wsrep::ws_meta& ws_meta)
{
    return client_state_->next_fragment(ws_meta);
}

int wsrep::mock_high_priority_service::adopt_transaction(
    const wsrep::transaction& transaction)
{
    client_state_->adopt_transaction(transaction);
    if (transaction.state() == wsrep::transaction::s_prepared)
    {
        client_state_->restore_xid(transaction.xid());
    }
    return 0;
}

int wsrep::mock_high_priority_service::apply_write_set(
    const wsrep::ws_meta& meta,
    const wsrep::const_buffer&,
    wsrep::mutable_buffer& err)
{
    assert(client_state_->toi_meta().seqno().is_undefined());
    assert(client_state_->transaction().state() == wsrep::transaction::s_executing ||
           client_state_->transaction().state() == wsrep::transaction::s_prepared ||
           client_state_->transaction().state() == wsrep::transaction::s_replaying);
    if (fail_next_applying_)
    {
        std::ostringstream os;
        os << "failed " << meta;
        err.push_back(os.str());
        assert(err.size() > 0);
        return 1;
    }
    else
    {
        int ret(0);
        if (!(meta.flags() & wsrep::provider::flag::commit))
        {
            client_state_->fragment_applied(meta.seqno());
        }
        if ((meta.flags() & wsrep::provider::flag::prepare))
        {
            client_state_->assign_xid(wsrep::xid(1, 3, 1, "xid"));
            ret = client_state_->before_prepare() ||
            client_state_->after_prepare();
        }
        return ret;
    };
}

int wsrep::mock_high_priority_service::commit(
    const wsrep::ws_handle& ws_handle,
    const wsrep::ws_meta& ws_meta)
{
    int ret(0);
    client_state_->prepare_for_ordering(ws_handle, ws_meta, true);
    if (do_2pc_)
    {
        ret = client_state_->before_prepare() ||
            client_state_->after_prepare();
    }
    const bool is_ordered= !ws_meta.seqno().is_undefined();
    if (!is_ordered)
    {
        client_state_->before_rollback();
        client_state_->after_rollback();
        return 0;
    }
    else
    {
        return (ret || client_state_->before_commit() ||
                client_state_->ordered_commit() ||
                client_state_->after_commit());
    }
}

int wsrep::mock_high_priority_service::rollback(
    const wsrep::ws_handle& ws_handle,
    const wsrep::ws_meta& ws_meta)
{
    client_state_->prepare_for_ordering(ws_handle, ws_meta, false);
    return (client_state_->before_rollback() ||
            client_state_->after_rollback());
}

int wsrep::mock_high_priority_service::apply_toi(const wsrep::ws_meta&,
                                                 const wsrep::const_buffer&,
                                                 wsrep::mutable_buffer&)
{
    assert(client_state_->transaction().active() == false);
    assert(client_state_->toi_meta().seqno().is_undefined() == false);
    return (fail_next_toi_ ? 1 : 0);
}

int wsrep::mock_high_priority_service::apply_nbo_begin(
    const wsrep::ws_meta& ws_meta,
    const wsrep::const_buffer&,
    wsrep::mutable_buffer&)
{
    const int nbo_begin_flags __attribute__((unused))
        (wsrep::provider::flag::isolation |
         wsrep::provider::flag::start_transaction);
    assert(ws_meta.flags() & nbo_begin_flags);
    assert((ws_meta.flags() & ~nbo_begin_flags) == 0);

    if (fail_next_toi_)
    {
        return 1;
    }
    else
    {
        nbo_cs_ = std::unique_ptr<wsrep::mock_client>(
            new wsrep::mock_client(client_state_->server_state(),
                                   wsrep::client_id(1),
                                   wsrep::client_state::m_local));
        nbo_cs_->open(wsrep::client_id(1));
        nbo_cs_->before_command();
        nbo_cs_->before_statement();
        return nbo_cs_->enter_nbo_mode(ws_meta);
    }
}

void wsrep::mock_high_priority_service::adopt_apply_error(
    wsrep::mutable_buffer& err)
{
    client_state_->adopt_apply_error(err);
}

void wsrep::mock_high_priority_service::after_apply()
{
    client_state_->after_applying();
}

int wsrep::mock_high_priority_service::log_dummy_write_set(
    const wsrep::ws_handle&,
    const wsrep::ws_meta&,
    wsrep::mutable_buffer& err)
{
    return err.size() > 0;
}