summaryrefslogtreecommitdiffstats
path: root/wsrep-lib/test/mock_server_state.hpp
blob: 093a620adef5fae25a1251243a900e3d0e42345e (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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Copyright (C) 2018-2013 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_SERVER_STATE_HPP
#define WSREP_MOCK_SERVER_STATE_HPP

#include "wsrep/server_state.hpp"
#include "wsrep/server_service.hpp"
#include "mock_client_state.hpp"
#include "mock_high_priority_service.hpp"
#include "mock_storage_service.hpp"
#include "mock_provider.hpp"

#include "wsrep/compiler.hpp"

namespace wsrep
{
    class mock_server_service : public wsrep::server_service
    {
    public:
        mock_server_service(wsrep::server_state* server_state)
            : sync_point_enabled_()
            , sync_point_action_()
            , sst_before_init_()
            , server_state_(server_state)
            , last_client_id_(0)
            , last_transaction_id_(0)
            , logged_view_()
            , position_()
        { }
        mock_server_service(const mock_server_service&) = delete;
        mock_server_service& operator=(const mock_server_service&) = delete;

        wsrep::storage_service* storage_service(wsrep::client_service&)
            WSREP_OVERRIDE
        {
            return new wsrep::mock_storage_service(*server_state_,
                                                   wsrep::client_id(++last_client_id_));
        }

        wsrep::storage_service* storage_service(wsrep::high_priority_service&)
            WSREP_OVERRIDE
        {
            return new wsrep::mock_storage_service(*server_state_,
                                                   wsrep::client_id(++last_client_id_));
        }

        void release_storage_service(wsrep::storage_service* storage_service)
            WSREP_OVERRIDE
        {
            delete storage_service;
        }

        wsrep::high_priority_service* streaming_applier_service(
            wsrep::client_service&)
            WSREP_OVERRIDE
        {
            wsrep::mock_client* cs(new wsrep::mock_client(
                                       *server_state_,
                                       wsrep::client_id(++last_client_id_),
                                       wsrep::client_state::m_high_priority));
            wsrep::mock_high_priority_service* ret(
                new wsrep::mock_high_priority_service(*server_state_,
                                                      cs, false));
            cs->open(cs->id());
            cs->before_command();
            return ret;
        }

        wsrep::high_priority_service* streaming_applier_service(
            wsrep::high_priority_service&) WSREP_OVERRIDE
        {
            wsrep::mock_client* cs(new wsrep::mock_client(
                                       *server_state_,
                                       wsrep::client_id(++last_client_id_),
                                       wsrep::client_state::m_high_priority));
            wsrep::mock_high_priority_service* ret(
                new wsrep::mock_high_priority_service(*server_state_,
                                                      cs, false));
            cs->open(cs->id());
            cs->before_command();
            return ret;
        }

        void release_high_priority_service(
            wsrep::high_priority_service *high_priority_service)
            WSREP_OVERRIDE
        {
            mock_high_priority_service* mhps(
                static_cast<mock_high_priority_service*>(high_priority_service));
            wsrep::mock_client* cs(&static_cast<wsrep::mock_client&>(
                                       mhps->client_state()));
            cs->after_command_before_result();
            cs->after_command_after_result();
            cs->close();
            cs->cleanup();
            delete cs;
            delete mhps;
        }
        void bootstrap() WSREP_OVERRIDE { }
        void log_message(enum wsrep::log::level level, const char* message)
            WSREP_OVERRIDE
        {
            wsrep::log(level, server_state_->name().c_str()) << message;
        }
        void log_dummy_write_set(wsrep::client_state&,
                                 const wsrep::ws_meta&)
            WSREP_OVERRIDE
        {
        }
        void log_view(wsrep::high_priority_service*, const wsrep::view& view)
            WSREP_OVERRIDE
        {
            logged_view_ = view;
        }

        void recover_streaming_appliers(wsrep::client_service&)
            WSREP_OVERRIDE
        { }

        void recover_streaming_appliers(wsrep::high_priority_service&)
            WSREP_OVERRIDE
        { }

        wsrep::view get_view(wsrep::client_service&, const wsrep::id& own_id)
            WSREP_OVERRIDE
        {
            int const my_idx(logged_view_.member_index(own_id));
            wsrep::view my_view(
                logged_view_.state_id(),
                logged_view_.view_seqno(),
                logged_view_.status(),
                logged_view_.capabilities(),
                my_idx,
                logged_view_.protocol_version(),
                logged_view_.members()
            );
            return my_view;
        }

        wsrep::gtid get_position(wsrep::client_service&) WSREP_OVERRIDE
        {
            return position_;
        }

        void set_position(wsrep::client_service&,
                          const wsrep::gtid& gtid) WSREP_OVERRIDE
        {
            position_ = gtid;
        }

        void log_state_change(enum wsrep::server_state::state,
                              enum wsrep::server_state::state)
            WSREP_OVERRIDE
        { }
        bool sst_before_init() const WSREP_OVERRIDE
        { return sst_before_init_; }
        std::string sst_request() WSREP_OVERRIDE { return ""; }

        // Action to take when start_sst() method is called.
        // This can be overriden by test case to inject custom
        // behavior.
        std::function<int()> start_sst_action{[](){ return 0; }};
        int start_sst(const std::string&, const wsrep::gtid&,
                      bool) WSREP_OVERRIDE
        {
            return start_sst_action();
        }

        void
        background_rollback(wsrep::unique_lock<wsrep::mutex>& lock,
                            wsrep::client_state& client_state) WSREP_OVERRIDE
        {
            lock.unlock();
            client_state.before_rollback();
            client_state.after_rollback();
            lock.lock();
        }

        int wait_committing_transactions(int) WSREP_OVERRIDE { return 0; }

        wsrep::transaction_id next_transaction_id()
        {
            return wsrep::transaction_id(++last_transaction_id_);
        }

        void debug_sync(const char* sync_point) WSREP_OVERRIDE
        {
            if (sync_point_enabled_ == sync_point)
            {
                switch (sync_point_action_)
                {
                case spa_none:
                    break;
                case spa_initialize:
                    server_state_->initialized();
                    break;
                case spa_initialize_error:
                    throw wsrep::runtime_error("Inject initialization error");
                    break;
                }
            }
        }

        std::string sync_point_enabled_;
        enum sync_point_action
        {
            spa_none,
            spa_initialize,
            spa_initialize_error

        } sync_point_action_;
        bool sst_before_init_;

        void logged_view(const wsrep::view& view)
        {
            logged_view_ = view;
        }
        void position(const wsrep::gtid& position)
        {
            position_ = position;
        }
    private:
        wsrep::server_state* server_state_;
        unsigned long long last_client_id_;
        unsigned long long last_transaction_id_;
        wsrep::view logged_view_;
        wsrep::gtid position_;
    };


    class mock_server_state : public wsrep::server_state
    {
    public:
        mock_server_state(const std::string& name,
                          enum wsrep::server_state::rollback_mode rollback_mode,
                          wsrep::server_service& server_service)
            : wsrep::server_state(mutex_, cond_, server_service, NULL,
                                  name, "", "", "./",
                                  wsrep::gtid::undefined(),
                                  1,
                                  rollback_mode)
            , mutex_()
            , cond_()
            , provider_(*this)
        { }

        wsrep::mock_provider& provider() const WSREP_OVERRIDE
        { return provider_; }

        // mock connected state for tests without overriding the connect()
        // method.
        int mock_connect(const std::string& own_id,
                         const std::string& cluster_name,
                         const std::string& cluster_address,
                         const std::string& state_donor,
                         bool bootstrap)
        {
            int const ret(server_state::connect(cluster_name,
                                                cluster_address,
                                                state_donor,
                                                bootstrap));
            if (0 == ret)
            {
                wsrep::id cluster_id("1");
                wsrep::gtid state_id(cluster_id, wsrep::seqno(0));
                std::vector<wsrep::view::member> members;
                members.push_back(wsrep::view::member(wsrep::id(own_id),
                                                      "name", ""));
                wsrep::view bootstrap_view(state_id,
                                           wsrep::seqno(1),
                                           wsrep::view::primary,
                                           0,
                                           0,
                                           1,
                                           members);
                server_state::on_connect(bootstrap_view);
            }
            else
            {
                assert(0);
            }

            return ret;
        }

        int mock_connect()
        {
             return mock_connect(name(), "cluster", "local", "0", false);
        }

    private:
        wsrep::default_mutex mutex_;
        wsrep::default_condition_variable cond_;
        mutable wsrep::mock_provider provider_;
    };
}

#endif // WSREP_MOCK_SERVER_STATE_HPP