1 /*
2  * Copyright (C) 2018-2019 Codership Oy <info@codership.com>
3  *
4  * This file is part of wsrep-lib.
5  *
6  * Wsrep-lib is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Wsrep-lib is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with wsrep-lib.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "mock_storage_service.hpp"
21 #include "mock_server_state.hpp"
22 
23 #include "wsrep/client_state.hpp"
24 
mock_storage_service(wsrep::server_state & server_state,wsrep::client_id client_id)25 wsrep::mock_storage_service::mock_storage_service(
26     wsrep::server_state& server_state,
27     wsrep::client_id client_id)
28     : client_service_(client_state_)
29     , client_state_(server_state, client_service_, client_id,
30                     wsrep::client_state::m_high_priority)
31 {
32     client_state_.open(client_id);
33     client_state_.before_command();
34 }
35 
36 
~mock_storage_service()37 wsrep::mock_storage_service::~mock_storage_service()
38 {
39     client_state_.after_command_before_result();
40     client_state_.after_command_after_result();
41     client_state_.close();
42     client_state_.cleanup();
43 }
44 
start_transaction(const wsrep::ws_handle & ws_handle)45 int wsrep::mock_storage_service::start_transaction(
46     const wsrep::ws_handle& ws_handle)
47 {
48     return client_state_.start_transaction(ws_handle.transaction_id());
49 }
50 
adopt_transaction(const wsrep::transaction & transaction)51 void wsrep::mock_storage_service::adopt_transaction(
52     const wsrep::transaction& transaction)
53 {
54     client_state_.adopt_transaction(transaction);
55 }
56 
commit(const wsrep::ws_handle & ws_handle,const wsrep::ws_meta & ws_meta)57 int wsrep::mock_storage_service::commit(const wsrep::ws_handle& ws_handle,
58                                         const wsrep::ws_meta& ws_meta)
59 {
60     // the logic here matches mariadb's wsrep_storage_service::commit
61     bool ret = 0;
62     const bool is_ordered = !ws_meta.seqno().is_undefined();
63     if (is_ordered)
64     {
65         ret = ret || client_state_.prepare_for_ordering(ws_handle, ws_meta, true);
66         ret = ret || client_state_.before_commit();
67         ret = ret || client_state_.ordered_commit();
68         ret = ret || client_state_.after_commit();
69     }
70 
71     if (!is_ordered)
72     {
73         client_state_.before_rollback();
74         client_state_.after_rollback();
75     }
76     else if (ret)
77     {
78         client_state_.prepare_for_ordering(wsrep::ws_handle(), wsrep::ws_meta(), false);
79     }
80 
81     client_state_.after_applying();
82     return ret;
83 }
84 
rollback(const wsrep::ws_handle & ws_handle,const wsrep::ws_meta & ws_meta)85 int wsrep::mock_storage_service::rollback(const wsrep::ws_handle& ws_handle,
86                                           const wsrep::ws_meta& ws_meta)
87 {
88     int ret(client_state_.prepare_for_ordering(
89                 ws_handle, ws_meta, false) ||
90             client_state_.before_rollback() ||
91             client_state_.after_rollback());
92     client_state_.after_applying();
93     return ret;
94 }
95