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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
/*
* Copyright (C) 2018 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/>.
*/
#ifndef WSREP_MOCK_CLIENT_CONTEXT_HPP
#define WSREP_MOCK_CLIENT_CONTEXT_HPP
#include "wsrep/client_state.hpp"
#include "wsrep/mutex.hpp"
#include "wsrep/compiler.hpp"
#include "wsrep/client_service.hpp"
#include "wsrep/condition_variable.hpp"
#include "test_utils.hpp"
namespace wsrep
{
class mock_client_state : public wsrep::client_state
{
public:
mock_client_state(wsrep::server_state& server_state,
wsrep::client_service& client_service,
const wsrep::client_id& id,
enum wsrep::client_state::mode mode)
: wsrep::client_state(mutex_, cond_, server_state, client_service,
id, mode)
, mutex_()
, cond_()
{ }
~mock_client_state() WSREP_OVERRIDE
{
if (transaction().active())
{
(void)client_service().bf_rollback();
}
}
private:
wsrep::default_mutex mutex_;
wsrep::default_condition_variable cond_;
public:
private:
};
class mock_client_service : public wsrep::client_service
{
public:
mock_client_service(wsrep::mock_client_state* client_state)
: wsrep::client_service()
, is_autocommit_()
, do_2pc_()
// , fail_next_applying_()
// , fail_next_toi_()
, bf_abort_during_wait_()
, bf_abort_during_fragment_removal_()
, error_during_prepare_data_()
, killed_before_certify_()
, sync_point_enabled_()
, sync_point_action_()
, bytes_generated_()
, client_state_(client_state)
, will_replay_called_()
, replays_()
, unordered_replays_()
, aborts_()
{ }
mock_client_service(const mock_client_service&) = delete;
mock_client_service& operator=(const mock_client_service&) = delete;
int bf_rollback() WSREP_OVERRIDE;
bool interrupted(wsrep::unique_lock<wsrep::mutex>&)
const WSREP_OVERRIDE
{ return killed_before_certify_; }
void emergency_shutdown() WSREP_OVERRIDE { ++aborts_; }
int remove_fragments() WSREP_OVERRIDE
{
if (bf_abort_during_fragment_removal_)
{
client_state_->before_rollback();
client_state_->after_rollback();
return 1;
}
else
{
return 0;
}
}
void will_replay() WSREP_OVERRIDE { will_replay_called_ = true; }
void signal_replayed() WSREP_OVERRIDE { }
enum wsrep::provider::status replay() WSREP_OVERRIDE;
enum wsrep::provider::status replay_unordered() WSREP_OVERRIDE
{
unordered_replays_++;
return wsrep::provider::success;
}
void wait_for_replayers(
wsrep::unique_lock<wsrep::mutex>& lock)
WSREP_OVERRIDE
{
lock.unlock();
if (bf_abort_during_wait_)
{
wsrep_test::bf_abort_unordered(*client_state_);
}
lock.lock();
}
int prepare_data_for_replication() WSREP_OVERRIDE
{
if (error_during_prepare_data_)
{
return 1;
}
static const char buf[1] = { 1 };
wsrep::const_buffer data = wsrep::const_buffer(buf, 1);
return client_state_->append_data(data);
}
void cleanup_transaction() WSREP_OVERRIDE { }
size_t bytes_generated() const WSREP_OVERRIDE
{
return bytes_generated_;
}
bool statement_allowed_for_streaming() const WSREP_OVERRIDE
{ return true; }
int prepare_fragment_for_replication(wsrep::mutable_buffer& buffer, size_t& position)
WSREP_OVERRIDE
{
if (error_during_prepare_data_)
{
return 1;
}
static const char buf[1] = { 1 };
buffer.push_back(&buf[0], &buf[1]);
wsrep::const_buffer data(buffer.data(), buffer.size());
position = buffer.size();
return client_state_->append_data(data);
}
void store_globals() WSREP_OVERRIDE { }
void reset_globals() WSREP_OVERRIDE { }
enum wsrep::provider::status commit_by_xid() WSREP_OVERRIDE
{
return wsrep::provider::success;
}
bool is_explicit_xa() WSREP_OVERRIDE
{
return false;
}
bool is_xa_rollback() WSREP_OVERRIDE
{
return false;
}
void debug_sync(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(*client_state_);
break;
case spa_bf_abort_ordered:
wsrep_test::bf_abort_ordered(*client_state_);
break;
}
}
}
void debug_crash(const char*) WSREP_OVERRIDE
{
// Not going to do this while unit testing
}
//
// Knobs to tune the behavior
//
bool is_autocommit_;
bool do_2pc_;
// bool fail_next_applying_;
// bool fail_next_toi_;
bool bf_abort_during_wait_;
bool bf_abort_during_fragment_removal_;
bool error_during_prepare_data_;
bool killed_before_certify_;
std::string sync_point_enabled_;
enum sync_point_action
{
spa_bf_abort_unordered,
spa_bf_abort_ordered
} sync_point_action_;
size_t bytes_generated_;
//
// Verifying the state
//
bool will_replay_called() const { return will_replay_called_; }
size_t replays() const { return replays_; }
size_t unordered_replays() const { return unordered_replays_; }
size_t aborts() const { return aborts_; }
private:
wsrep::mock_client_state* client_state_;
bool will_replay_called_;
size_t replays_;
size_t unordered_replays_;
size_t aborts_;
};
class mock_client
: public mock_client_state
, public mock_client_service
{
public:
mock_client(wsrep::server_state& server_state,
const wsrep::client_id& id,
enum wsrep::client_state::mode mode)
: mock_client_state(server_state, *this, id, mode)
, mock_client_service(static_cast<mock_client_state*>(this))
{ }
int after_row()
{
bytes_generated_++;
return wsrep::client_state::after_row();
}
};
}
#endif // WSREP_MOCK_CLIENT_CONTEXT_HPP
|