From 948887668e0e61ea16204c8602e421f0f5451165 Mon Sep 17 00:00:00 2001 From: Diana Parra Corbacho Date: Tue, 11 Oct 2022 09:18:31 +0200 Subject: [PATCH] khepri_cluster: Accept `machine_config` in Ra server config It is now possible to pass state machine arguments in the Ra server config. This is handy to e.g. set the snapshot interval: RaServerConfig = #{cluster_name => StoreId, machine_config => #{snapshot_interval => 10000}}, khepri:start(RaSystem, RaServerConfig). --- src/khepri_cluster.erl | 10 ++++-- test/cluster_SUITE.erl | 70 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/src/khepri_cluster.erl b/src/khepri_cluster.erl index da94dfee..9a2f0e62 100644 --- a/src/khepri_cluster.erl +++ b/src/khepri_cluster.erl @@ -1118,8 +1118,14 @@ complete_ra_server_config(#{cluster_name := StoreId, end, UId = ra:new_uid(ra_lib:to_binary(StoreId)), - MachineConfig = #{store_id => StoreId, - member => Member}, + MachineConfig0 = case RaServerConfig of + #{machine_config := MachineConfig00} -> + MachineConfig00; + _ -> + #{} + end, + MachineConfig = MachineConfig0#{store_id => StoreId, + member => Member}, Machine = {module, khepri_machine, MachineConfig}, RaServerConfig2#{uid => UId, log_init_args => #{uid => UId}, diff --git a/test/cluster_SUITE.erl b/test/cluster_SUITE.erl index 2385f308..9d345061 100644 --- a/test/cluster_SUITE.erl +++ b/test/cluster_SUITE.erl @@ -13,6 +13,7 @@ -include("include/khepri.hrl"). -include("src/khepri_error.hrl"). +-include("src/khepri_machine.hrl"). -export([all/0, groups/0, @@ -39,7 +40,8 @@ can_use_default_store_on_single_node/1, can_start_store_in_specified_data_dir_on_single_node/1, handle_leader_down_on_three_node_cluster_command/1, - handle_leader_down_on_three_node_cluster_response/1]). + handle_leader_down_on_three_node_cluster_response/1, + can_set_snapshot_interval/1]). all() -> [can_start_a_single_node, @@ -56,7 +58,8 @@ all() -> can_use_default_store_on_single_node, can_start_store_in_specified_data_dir_on_single_node, handle_leader_down_on_three_node_cluster_command, - handle_leader_down_on_three_node_cluster_response]. + handle_leader_down_on_three_node_cluster_response, + can_set_snapshot_interval]. groups() -> []. @@ -80,7 +83,8 @@ init_per_testcase(Testcase, Config) Testcase =:= can_restart_a_single_node_with_ra_server_config orelse Testcase =:= fail_to_start_with_bad_ra_server_config orelse Testcase =:= initial_members_are_ignored orelse - Testcase =:= fail_to_join_non_existing_node -> + Testcase =:= fail_to_join_non_existing_node orelse + Testcase =:= can_set_snapshot_interval -> setup_node(), {ok, _} = application:ensure_all_started(khepri), Props = helpers:start_ra_system(Testcase), @@ -1091,6 +1095,66 @@ can_start_store_in_specified_data_dir_on_single_node(_Config) -> helpers:remove_store_dir(DataDir), ?assertNot(filelib:is_dir(DataDir)). +can_set_snapshot_interval(Config) -> + Node = node(), + #{Node := #{ra_system := RaSystem}} = ?config(ra_system_props, Config), + StoreId = RaSystem, + + ct:pal("Start database"), + RaServerConfig = #{cluster_name => StoreId, + machine_config => #{snapshot_interval => 3}}, + ?assertEqual( + {ok, StoreId}, + khepri:start(RaSystem, RaServerConfig)), + + ct:pal("Verify applied command count is 0"), + ?assertEqual( + #{}, + khepri_machine:process_query( + StoreId, + fun(#khepri_machine{metrics = Metrics}) -> Metrics end, + #{})), + + ct:pal("Use database after starting it"), + ?assertEqual(ok, khepri:put(StoreId, [foo], value1)), + + ct:pal("Verify applied command count is 1"), + ?assertEqual( + #{applied_command_count => 1}, + khepri_machine:process_query( + StoreId, + fun(#khepri_machine{metrics = Metrics}) -> Metrics end, + #{})), + + ct:pal("Use database after starting it"), + ?assertEqual(ok, khepri:put(StoreId, [foo], value1)), + + ct:pal("Verify applied command count is 2"), + ?assertEqual( + #{applied_command_count => 2}, + khepri_machine:process_query( + StoreId, + fun(#khepri_machine{metrics = Metrics}) -> Metrics end, + #{})), + + ct:pal("Use database after starting it"), + ?assertEqual(ok, khepri:put(StoreId, [foo], value1)), + + ct:pal("Verify applied command count is 0"), + ?assertEqual( + #{}, + khepri_machine:process_query( + StoreId, + fun(#khepri_machine{metrics = Metrics}) -> Metrics end, + #{})), + + ct:pal("Stop database"), + ?assertEqual( + ok, + khepri:stop(StoreId)), + + ok. + %% ------------------------------------------------------------------- %% Internal functions %% -------------------------------------------------------------------