Skip to content

Commit a55d01a

Browse files
committed
r/tests: added state machine manager test
Added state machine manager test waiting for batches to be applied after each replicate. This test is designed to detect a situation in which background apply fiber finishes but managed stm is still behind the others. Signed-off-by: Michal Maslanka <[email protected]>
1 parent df97290 commit a55d01a

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/v/raft/tests/stm_manager_test.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,31 @@ TEST_F_CORO(state_machine_fixture, test_apply_throwing_exception) {
124124
ASSERT_EQ_CORO(stm->state, expected);
125125
}
126126
}
127+
TEST_F_CORO(
128+
state_machine_fixture, test_apply_throwing_exception_waiting_for_each_batch) {
129+
/**
130+
* Create 3 replicas group with simple_kv STM
131+
*/
132+
create_nodes();
133+
std::vector<ss::shared_ptr<simple_kv>> stms;
134+
135+
for (auto& [id, node] : nodes()) {
136+
raft::state_machine_manager_builder builder;
137+
auto kv_stm = builder.create_stm<simple_kv>(*node);
138+
auto throwing_kv_stm = builder.create_stm<throwing_kv>(*node);
139+
co_await node->init_and_start(all_vnodes(), std::move(builder));
140+
stms.push_back(kv_stm);
141+
stms.push_back(ss::dynamic_pointer_cast<simple_kv>(throwing_kv_stm));
142+
}
143+
144+
auto expected = co_await build_random_state(5000, wait_for_each_batch::yes);
145+
146+
co_await wait_for_apply();
147+
148+
for (auto& stm : stms) {
149+
ASSERT_EQ_CORO(stm->state, expected);
150+
}
151+
}
127152

128153
TEST_F_CORO(state_machine_fixture, test_recovery_without_snapshot) {
129154
/**

src/v/raft/tests/stm_test_fixture.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <seastar/core/shared_ptr.hh>
3535
#include <seastar/core/sstring.hh>
3636
#include <seastar/coroutine/parallel_for_each.hh>
37+
#include <seastar/util/bool_class.hh>
3738
#include <seastar/util/log.hh>
3839

3940
#include <ostream>
@@ -142,6 +143,7 @@ struct simple_kv : public raft::state_machine_base {
142143
state_t state;
143144
raft_node_instance& raft_node;
144145
};
146+
using wait_for_each_batch = ss::bool_class<struct wait_for_each_tag>;
145147

146148
struct state_machine_fixture : raft_fixture {
147149
ss::future<result<raft::replicate_result>>
@@ -172,7 +174,8 @@ struct state_machine_fixture : raft_fixture {
172174
}
173175

174176
ss::future<absl::flat_hash_map<ss::sstring, value_entry>>
175-
build_random_state(int op_cnt) {
177+
build_random_state(
178+
int op_cnt, wait_for_each_batch wait_for_each = wait_for_each_batch::no) {
176179
absl::flat_hash_map<ss::sstring, value_entry> state;
177180

178181
for (int i = 0; i < op_cnt;) {
@@ -213,6 +216,10 @@ struct state_machine_fixture : raft_fixture {
213216
logger().debug,
214217
"replication result: [last_offset: {}]",
215218
result.value().last_offset);
219+
220+
if (wait_for_each) {
221+
co_await wait_for_apply();
222+
}
216223
}
217224
co_return state;
218225
}

0 commit comments

Comments
 (0)