1 /*
2 * Copyright (C) 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 "wsrep/client_state.hpp"
21
22 #include "client_state_fixture.hpp"
23
24 #include <boost/test/unit_test.hpp>
25
BOOST_FIXTURE_TEST_CASE(test_local_nbo,replicating_client_fixture_sync_rm)26 BOOST_FIXTURE_TEST_CASE(test_local_nbo,
27 replicating_client_fixture_sync_rm)
28 {
29 // NBO is executed in two consecutive TOI operations
30 BOOST_REQUIRE(cc.mode() == wsrep::client_state::m_local);
31 // First phase certifies the write set and enters TOI
32 wsrep::key key(wsrep::key::exclusive);
33 key.append_key_part("k1", 2);
34 key.append_key_part("k2", 2);
35 wsrep::key_array keys{key};
36 std::string data("data");
37 BOOST_REQUIRE(cc.begin_nbo_phase_one(
38 keys,
39 wsrep::const_buffer(data.data(),
40 data.size())) == 0);
41 BOOST_REQUIRE(cc.mode() == wsrep::client_state::m_nbo);
42 BOOST_REQUIRE(cc.in_toi());
43 BOOST_REQUIRE(cc.toi_mode() == wsrep::client_state::m_local);
44 // After required resoureces have been grabbed, NBO leave should
45 // end TOI but leave the client state in m_nbo.
46 const wsrep::mutable_buffer err;
47 BOOST_REQUIRE(cc.end_nbo_phase_one(err) == 0);
48 BOOST_REQUIRE(cc.mode() == wsrep::client_state::m_nbo);
49 BOOST_REQUIRE(cc.in_toi() == false);
50 BOOST_REQUIRE(cc.toi_mode() == wsrep::client_state::m_undefined);
51 // Second phase replicates the NBO end event and grabs TOI
52 // again for finalizing the NBO.
53 BOOST_REQUIRE(cc.begin_nbo_phase_two(keys) == 0);
54 BOOST_REQUIRE(cc.mode() == wsrep::client_state::m_nbo);
55 BOOST_REQUIRE(cc.in_toi());
56 BOOST_REQUIRE(cc.toi_mode() == wsrep::client_state::m_local);
57 // End of NBO phase will leave TOI and put the client state back to
58 // m_local
59 BOOST_REQUIRE(cc.end_nbo_phase_two(err) == 0);
60 BOOST_REQUIRE(cc.mode() == wsrep::client_state::m_local);
61 BOOST_REQUIRE(cc.in_toi() == false);
62 BOOST_REQUIRE(cc.toi_mode() == wsrep::client_state::m_undefined);
63
64 // There must have been two toi write sets, one with
65 // start transaction flag, another with commit flag.
66 BOOST_REQUIRE(sc.provider().toi_write_sets() == 2);
67 BOOST_REQUIRE(sc.provider().toi_start_transaction() == 1);
68 BOOST_REQUIRE(sc.provider().toi_commit() == 1);
69 }
70
BOOST_FIXTURE_TEST_CASE(test_local_nbo_cert_failure,replicating_client_fixture_sync_rm)71 BOOST_FIXTURE_TEST_CASE(test_local_nbo_cert_failure,
72 replicating_client_fixture_sync_rm)
73 {
74 // NBO is executed in two consecutive TOI operations
75 BOOST_REQUIRE(cc.mode() == wsrep::client_state::m_local);
76 // First phase certifies the write set and enters TOI
77 wsrep::key key(wsrep::key::exclusive);
78 key.append_key_part("k1", 2);
79 key.append_key_part("k2", 2);
80 wsrep::key_array keys{key};
81 std::string data("data");
82 sc.provider().certify_result_ = wsrep::provider::error_certification_failed;
83 BOOST_REQUIRE(cc.begin_nbo_phase_one(
84 keys,
85 wsrep::const_buffer(data.data(),
86 data.size())) == 1);
87 BOOST_REQUIRE(cc.current_error() == wsrep::e_deadlock_error);
88 BOOST_REQUIRE(cc.current_error_status() ==
89 wsrep::provider::error_certification_failed);
90 BOOST_REQUIRE(cc.mode() == wsrep::client_state::m_local);
91 BOOST_REQUIRE(cc.in_toi() == false);
92 BOOST_REQUIRE(cc.toi_mode() == wsrep::client_state::m_undefined);
93 }
94
95 // This test case operates through server_state object in order to
96 // verify that the high priority service is called with appropriate
97 // arguments.
BOOST_FIXTURE_TEST_CASE(test_applying_nbo,applying_client_fixture)98 BOOST_FIXTURE_TEST_CASE(test_applying_nbo,
99 applying_client_fixture)
100 {
101
102 wsrep::mock_high_priority_service hps(sc, &cc, false);
103 wsrep::ws_handle ws_handle(wsrep::transaction_id::undefined(), (void*)(1));
104 const int nbo_begin_flags(wsrep::provider::flag::start_transaction |
105 wsrep::provider::flag::isolation);
106 wsrep::ws_meta ws_meta(wsrep::gtid(wsrep::id("cluster1"),
107 wsrep::seqno(1)),
108 wsrep::stid(wsrep::id("s1"),
109 wsrep::transaction_id::undefined(),
110 wsrep::client_id(1)),
111 wsrep::seqno(0),
112 nbo_begin_flags);
113 std::string nbo_begin("nbo_begin");
114 BOOST_REQUIRE(sc.on_apply(hps, ws_handle, ws_meta,
115 wsrep::const_buffer(nbo_begin.data(),
116 nbo_begin.size())) == 0);
117 wsrep::mock_client* nbo_cs(hps.nbo_cs());
118 BOOST_REQUIRE(nbo_cs);
119 BOOST_REQUIRE(nbo_cs->toi_mode() == wsrep::client_state::m_undefined);
120 BOOST_REQUIRE(nbo_cs->mode() == wsrep::client_state::m_nbo);
121
122 // After this point the control is on local process and applier
123 // has released toi critical section.
124 wsrep::key key(wsrep::key::exclusive);
125 key.append_key_part("k1", 2);
126 key.append_key_part("k2", 2);
127 wsrep::key_array keys{key};
128 // Starting phase two should put nbo_cs in toi mode.
129 BOOST_REQUIRE(nbo_cs->begin_nbo_phase_two(keys) == 0);
130 BOOST_REQUIRE(nbo_cs->mode() == wsrep::client_state::m_nbo);
131 BOOST_REQUIRE(nbo_cs->in_toi());
132 BOOST_REQUIRE(nbo_cs->toi_mode() == wsrep::client_state::m_local);
133 // Ending phase two should make nbo_cs leave TOI mode and
134 // return to m_local mode.
135 const wsrep::mutable_buffer err;
136 BOOST_REQUIRE(nbo_cs->end_nbo_phase_two(err) == 0);
137 BOOST_REQUIRE(nbo_cs->mode() == wsrep::client_state::m_local);
138 BOOST_REQUIRE(nbo_cs->in_toi() == false);
139 BOOST_REQUIRE(nbo_cs->toi_mode() == wsrep::client_state::m_undefined);
140
141 // There must have been one toi write set with commit flag.
142 BOOST_REQUIRE(sc.provider().toi_write_sets() == 1);
143 BOOST_REQUIRE(sc.provider().toi_start_transaction() == 0);
144 BOOST_REQUIRE(sc.provider().toi_commit() == 1);
145 }
146
147 // This test case operates through server_state object in order to
148 // verify that the high priority service is called with appropriate
149 // arguments. The test checks that error is returned in the case if
150 // launching asynchronous process for NBO fails.
BOOST_FIXTURE_TEST_CASE(test_applying_nbo_fail,applying_client_fixture)151 BOOST_FIXTURE_TEST_CASE(test_applying_nbo_fail,
152 applying_client_fixture)
153 {
154
155 wsrep::mock_high_priority_service hps(sc, &cc, false);
156 wsrep::ws_handle ws_handle(wsrep::transaction_id::undefined(), (void*)(1));
157 const int nbo_begin_flags(wsrep::provider::flag::start_transaction |
158 wsrep::provider::flag::isolation);
159 wsrep::ws_meta ws_meta(wsrep::gtid(wsrep::id("cluster1"),
160 wsrep::seqno(1)),
161 wsrep::stid(wsrep::id("s1"),
162 wsrep::transaction_id::undefined(),
163 wsrep::client_id(1)),
164 wsrep::seqno(0),
165 nbo_begin_flags);
166 std::string nbo_begin("nbo_begin");
167 hps.fail_next_toi_ = true;
168 BOOST_REQUIRE(sc.on_apply(hps, ws_handle, ws_meta,
169 wsrep::const_buffer(nbo_begin.data(),
170 nbo_begin.size())) == 1);
171 }
172