From c3c32c6e86161d2891b1554b9b7c51c796f7029b Mon Sep 17 00:00:00 2001 From: glyh Date: Tue, 23 Sep 2025 14:44:56 +0800 Subject: [PATCH 1/2] Archive Node Test: implement live upgrade test --- .../archive_node_tests/archive_node_tests.ml | 19 +++++- .../live_upgrade_archive.ml | 62 +++++++++++++++++++ .../archive_node_tests/upgrade_archive.ml | 3 +- src/test/mina_automation/daemon.ml | 1 + 4 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 src/test/archive/archive_node_tests/live_upgrade_archive.ml diff --git a/src/test/archive/archive_node_tests/archive_node_tests.ml b/src/test/archive/archive_node_tests/archive_node_tests.ml index 3ac5928746ce..d7ef263e03ce 100644 --- a/src/test/archive/archive_node_tests/archive_node_tests.ml +++ b/src/test/archive/archive_node_tests/archive_node_tests.ml @@ -1,9 +1,17 @@ open Core open Mina_automation_runner +let logger = Logger.create () + let () = Backtrace.elide := false ; - Async.Scheduler.set_record_backtraces true + Async.Scheduler.set_record_backtraces true ; + let random_seed = + Time.(now () |> to_span_since_epoch |> Span.to_ms) |> Int.of_float + in + [%log info] "Initializing random with seed" + ~metadata:[ ("seed", `Int random_seed) ] ; + Random.init random_seed let () = let open Alcotest in @@ -32,4 +40,13 @@ let () = ( module Mina_automation_fixture.Archive.Make_FixtureWithBootstrap (Upgrade_archive) ) ) ] ) + ; ( "live_upgrade_archive" + , [ test_case + "Recreate database from precomputed blocks. Meanwhile run upgrade \ + script randomly at some time" + `Quick + (Runner.run_blocking + ( module Mina_automation_fixture.Archive.Make_FixtureWithBootstrap + (Live_upgrade_archive) ) ) + ] ) ] diff --git a/src/test/archive/archive_node_tests/live_upgrade_archive.ml b/src/test/archive/archive_node_tests/live_upgrade_archive.ml new file mode 100644 index 000000000000..c732cae485e7 --- /dev/null +++ b/src/test/archive/archive_node_tests/live_upgrade_archive.ml @@ -0,0 +1,62 @@ +open Async +open Core +open Mina_automation +open Mina_automation_fixture.Archive +open Common + +(* NOTE: + To run this test, several preparation is needed + - ensure we have this test, replayer & archive node build with devnet profile + - ensure we have a data base instance up + - Run the following: + ``` + MINA_TEST_POSTGRES_URI=postgres://postgres:xxxx@localhost:5432 \ + MINA_TEST_NETWORK_DATA=./src/test/archive/sample_db \ + ./_build/default/src/test/archive/archive_node_tests/archive_node_tests.exe \ + test live_upgrade_archive + ``` +*) + +type t = Mina_automation_fixture.Archive.after_bootstrap + +let test_case (test_data : t) = + let open Deferred.Let_syntax in + let daemon = Daemon.default () in + let archive_uri = test_data.archive.config.postgres_uri in + let temp_dir = test_data.temp_dir in + let%bind precomputed_blocks = + unpack_precomputed_blocks ~temp_dir test_data.network_data + in + let logger = Logger.create () in + let log_file = temp_dir ^ "/live_upgrade.log" in + let upgrade_path = + Archive.Scripts.filepath `Upgrade + |> Option.value_exn ~message:"Failed to find upgrade script" + in + let upgrade_script_finished = Ivar.create () in + (let%bind () = after (Time.Span.of_min (Random.float_range 0. 5.)) in + [%log info] "Starting upgrade script" ; + let%map result = + Psql.run_script ~connection:(Psql.Conn_str archive_uri) upgrade_path + in + [%log info] "Finished executing upgrade script" + ~metadata:[ ("result", `String result) ] ; + Ivar.fill upgrade_script_finished () ) + |> Deferred.don't_wait_for ; + Archive.Process.start_logging test_data.archive ~log_file ; + + let%bind () = + Daemon.archive_blocks_from_files daemon.executor + ~archive_address:test_data.archive.config.server_port ~format:`Precomputed + precomputed_blocks + in + [%log info] "Loaded all precomputed blocks" ; + let%bind () = Ivar.read upgrade_script_finished in + let%bind () = + assert_replayer_run_against_last_block + ~replayer_input_file_path: + (Network_data.replayer_input_file_path test_data.network_data) + archive_uri temp_dir + in + + Deferred.Or_error.return Mina_automation_fixture.Intf.Passed diff --git a/src/test/archive/archive_node_tests/upgrade_archive.ml b/src/test/archive/archive_node_tests/upgrade_archive.ml index de8d4097f545..d6b53fe0061f 100644 --- a/src/test/archive/archive_node_tests/upgrade_archive.ml +++ b/src/test/archive/archive_node_tests/upgrade_archive.ml @@ -12,8 +12,7 @@ open Common ``` MINA_TEST_POSTGRES_URI=postgres://postgres:xxxx@localhost:5432 \ MINA_TEST_NETWORK_DATA=./src/test/archive/sample_db \ - DUNE_PROFILE=devnet \ - dune exec src/test/archive/archive_node_tests/archive_node_tests.exe -- \ + ./_build/default/src/test/archive/archive_node_tests/archive_node_tests.exe \ test upgrade_archive ``` *) diff --git a/src/test/mina_automation/daemon.ml b/src/test/mina_automation/daemon.ml index 6609c72c81d5..d98925b34f69 100644 --- a/src/test/mina_automation/daemon.ml +++ b/src/test/mina_automation/daemon.ml @@ -193,6 +193,7 @@ type t = { config : Config.t; executor : Executor.t } let archive_blocks_from_files t ~archive_address ~format ?(sleep = 5) blocks = Deferred.List.iter blocks ~f:(fun block -> let%bind _ = archive_blocks t ~archive_address ~format [ block ] () in + (* WARN: live upgrade test expect this sleep to be present so we can emulate a race condition *) after (Time.Span.of_sec (Float.of_int sleep)) ) let of_config config = { config; executor = Executor.AutoDetect } From a1cc174be1a6dc9f3823d4b550119c87d1aaf25d Mon Sep 17 00:00:00 2001 From: glyh Date: Wed, 24 Sep 2025 10:04:12 +0800 Subject: [PATCH 2/2] explicitly add sleep ensuring scheduler have chance to slide in an execution of upgrade script when loading precomputed archive blocks --- src/test/archive/archive_node_tests/live_upgrade_archive.ml | 2 +- src/test/mina_automation/daemon.ml | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/archive/archive_node_tests/live_upgrade_archive.ml b/src/test/archive/archive_node_tests/live_upgrade_archive.ml index c732cae485e7..2ddb0d47616f 100644 --- a/src/test/archive/archive_node_tests/live_upgrade_archive.ml +++ b/src/test/archive/archive_node_tests/live_upgrade_archive.ml @@ -48,7 +48,7 @@ let test_case (test_data : t) = let%bind () = Daemon.archive_blocks_from_files daemon.executor ~archive_address:test_data.archive.config.server_port ~format:`Precomputed - precomputed_blocks + ~sleep:5 precomputed_blocks in [%log info] "Loaded all precomputed blocks" ; let%bind () = Ivar.read upgrade_script_finished in diff --git a/src/test/mina_automation/daemon.ml b/src/test/mina_automation/daemon.ml index d98925b34f69..6609c72c81d5 100644 --- a/src/test/mina_automation/daemon.ml +++ b/src/test/mina_automation/daemon.ml @@ -193,7 +193,6 @@ type t = { config : Config.t; executor : Executor.t } let archive_blocks_from_files t ~archive_address ~format ?(sleep = 5) blocks = Deferred.List.iter blocks ~f:(fun block -> let%bind _ = archive_blocks t ~archive_address ~format [ block ] () in - (* WARN: live upgrade test expect this sleep to be present so we can emulate a race condition *) after (Time.Span.of_sec (Float.of_int sleep)) ) let of_config config = { config; executor = Executor.AutoDetect }