Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

batch insertion events in archive database #14779

Merged
merged 20 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 98 additions & 36 deletions src/app/archive/lib/processor.ml
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,14 @@ module Zkapp_field_array = struct
(fps : Pickles.Backend.Tick.Field.t array) =
let open Deferred.Result.Let_syntax in
let%bind (element_ids : int array) =
Metrics.time ~label:"zkapp_field.add"
@@ fun () ->
Mina_caqti.deferred_result_list_map (Array.to_list fps)
~f:(Zkapp_field.add_if_doesn't_exist (module Conn))
>>| Array.of_list
in
Metrics.time ~label:"select_insert_into zkapp_field_array"
@@ fun () ->
Mina_caqti.select_insert_into_cols ~select:("id", Caqti_type.int)
~table_name
~cols:([ "element_ids" ], Mina_caqti.array_int_typ)
Expand Down Expand Up @@ -1421,6 +1425,22 @@ module Zkapp_events = struct
List.map xs ~f:(if parenthesis then sprintf "('%s')" else sprintf "'%s'")
|> String.concat ~sep:", "

(* Account_update.Body.Events'.t is defined as `field array list`,
which is ismorphic to a list of list of fields.

We are batching the insertion of field and field_array to optimize
the speed of archiving max-cost zkapps.

1. we flatten the list of list of fields to get all the field elements
2. insert all the field elements in one query
3. construct a map "M" from `field_id` to `field` by querying against the zkapp_field table
4. use "M" and the list of list of fields to compute the list of list of field_ids
5. insert all list of `list of field_ids` in one query
6. construct a map "M'" from `field_array_id` to `field_id array` by querying against
the zkapp_field_array table
7. use "M'" and the list of list of field_ids to compute the list of field_array_ids
8. insert the list of field_arrays
*)
let add_if_doesn't_exist (module Conn : CONNECTION)
(events : Account_update.Body.Events'.t) =
let open Deferred.Result.Let_syntax in
Expand Down Expand Up @@ -1458,9 +1478,13 @@ module Zkapp_events = struct
()
>>| String.Map.of_alist_exn
in

List.map field_list_list ~f:(List.map ~f:(Map.find_exn field_map))
else return @@ List.map field_list_list ~f:(fun _ -> [])
let field_id_list_list =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let field_id_list_list =

field_id_list_list is not needed at all

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a let binding here to make the code more readable. Just to give a name to the result of that computation.

List.map field_list_list ~f:(List.map ~f:(Map.find_exn field_map))
in
field_id_list_list
else
(* if there's no fields, then we must have some list of empty lists *)
return @@ List.map field_list_list ~f:(fun _ -> [])
in
let field_array_list =
List.map field_id_list_list ~f:(fun id_list ->
Expand Down Expand Up @@ -1496,10 +1520,12 @@ module Zkapp_events = struct
()
>>| Field_array_map.of_alist_exn
in

List.map field_id_list_list ~f:(fun field_id_list ->
Map.find_exn field_array_map (Array.of_list field_id_list) )
|> Array.of_list
let field_array_id_list =
List.map field_id_list_list ~f:(fun field_id_list ->
Map.find_exn field_array_map (Array.of_list field_id_list) )
|> Array.of_list
in
field_array_id_list
else return @@ Array.of_list []
in
Mina_caqti.select_insert_into_cols ~select:("id", Caqti_type.int)
Expand Down Expand Up @@ -1568,14 +1594,17 @@ module Zkapp_account_update_body = struct
Account_identifiers.add_if_doesn't_exist (module Conn) account_identifier
in
let%bind update_id =
Zkapp_updates.add_if_doesn't_exist (module Conn) body.update
Metrics.time ~label:"zkapp_updates.add"
@@ fun () -> Zkapp_updates.add_if_doesn't_exist (module Conn) body.update
in
let increment_nonce = body.increment_nonce in
let%bind events_id =
Zkapp_events.add_if_doesn't_exist (module Conn) body.events
Metrics.time ~label:"Zkapp_events.add"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need all of these Metrics.time?

@@ fun () -> Zkapp_events.add_if_doesn't_exist (module Conn) body.events
in
let%bind actions_id =
Zkapp_events.add_if_doesn't_exist (module Conn) body.actions
Metrics.time ~label:"Zkapp_actions.add"
@@ fun () -> Zkapp_events.add_if_doesn't_exist (module Conn) body.actions
in
let%bind call_data_id =
Zkapp_field.add_if_doesn't_exist (module Conn) body.call_data
Expand Down Expand Up @@ -1677,6 +1706,8 @@ module Zkapp_account_update = struct
(account_update : Account_update.Simple.t) =
let open Deferred.Result.Let_syntax in
let%bind body_id =
Metrics.time ~label:"Zkapp_account_update_body.add"
@@ fun () ->
Zkapp_account_update_body.add_if_doesn't_exist
(module Conn)
account_update.body
Expand Down Expand Up @@ -1974,11 +2005,15 @@ module User_command = struct
let open Deferred.Result.Let_syntax in
let zkapp_command = Zkapp_command.to_simple ps in
let%bind zkapp_fee_payer_body_id =
Metrics.time ~label:"Zkapp_fee_payer_body.add"
@@ fun () ->
Zkapp_fee_payer_body.add_if_doesn't_exist
(module Conn)
zkapp_command.fee_payer.body
in
let%bind zkapp_account_updates_ids =
Metrics.time ~label:"Zkapp_account_update.add"
@@ fun () ->
Mina_caqti.deferred_result_list_map zkapp_command.account_updates
~f:(Zkapp_account_update.add_if_doesn't_exist (module Conn))
>>| Array.of_list
Expand Down Expand Up @@ -2986,6 +3021,8 @@ module Block = struct
(failed_str, Some display)
in
let%bind _seq_no =
Metrics.time ~label:"adding_transactions"
@@ fun () ->
Mina_caqti.deferred_result_list_fold transactions ~init:0
~f:(fun sequence_no -> function
| { Mina_base.With_status.status
Expand All @@ -2995,13 +3032,18 @@ module Block = struct
{ Mina_base.With_status.status; data = command }
in
let%bind id =
Metrics.time ~label:"user_command.add_if_doesn't_exist"
@@ fun () ->
User_command.add_if_doesn't_exist
(module Conn)
user_command.data
in
let%map () =
match command with
| Signed_command _ ->
Metrics.time
~label:"block_and_signed_command.add_with_status"
@@ fun () ->
Block_and_signed_command.add_with_status
(module Conn)
~block_id ~user_command_id:id ~sequence_no
Expand All @@ -3011,6 +3053,9 @@ module Block = struct
let status, failure_reasons =
failure_reasons user_command.status
in
Metrics.time
~label:"block_and_zkapp_command.add_if_doesn't_exist"
@@ fun () ->
Block_and_zkapp_command.add_if_doesn't_exist
(module Conn)
~block_id ~zkapp_command_id:id ~sequence_no ~status
Expand All @@ -3019,6 +3064,8 @@ module Block = struct
in
sequence_no + 1
| { data = Fee_transfer fee_transfer_bundled; status } ->
Metrics.time ~label:"fee_transfer.add"
@@ fun () ->
let fee_transfers =
Mina_base.Fee_transfer.to_numbered_list fee_transfer_bundled
in
Expand Down Expand Up @@ -3121,6 +3168,8 @@ module Block = struct
in
sequence_no + 1
| { data = Coinbase coinbase; status } ->
Metrics.time ~label:"conbase.add"
@@ fun () ->
let fee_transfer_via_coinbase =
Mina_base.Coinbase.fee_transfer coinbase
in
Expand Down Expand Up @@ -3550,48 +3599,58 @@ module Block = struct
then
(* a new block, allows marking some pending blocks as canonical *)
let%bind subchain_blocks =
get_subchain
(module Conn)
~start_block_id:highest_canonical_block_id ~end_block_id:block_id
Metrics.time ~label:"get_subchain (> canonical_height + k)"
(fun () ->
get_subchain
(module Conn)
~start_block_id:highest_canonical_block_id
~end_block_id:block_id )
in
let block_height_less_k_int64 = Int64.( - ) block.height k_int64 in
(* mark canonical, orphaned blocks in subchain at least k behind the new block *)
let canonical_blocks =
List.filter subchain_blocks ~f:(fun subchain_block ->
Int64.( <= ) subchain_block.height block_height_less_k_int64 )
in
Mina_caqti.deferred_result_list_fold canonical_blocks ~init:()
~f:(fun () block ->
let%bind () =
mark_as_canonical (module Conn) ~state_hash:block.state_hash
in
mark_as_orphaned
(module Conn)
~state_hash:block.state_hash ~height:block.height )
Metrics.time ~label:"mark_as_canonical (> canonical_height + k)"
(fun () ->
Mina_caqti.deferred_result_list_fold canonical_blocks ~init:()
~f:(fun () block ->
let%bind () =
mark_as_canonical (module Conn) ~state_hash:block.state_hash
in
mark_as_orphaned
(module Conn)
~state_hash:block.state_hash ~height:block.height ) )
else if Int64.( < ) block.height greatest_canonical_height then
(* a missing block added in the middle of canonical chain *)
let%bind canonical_block_above_id, _above_height =
get_nearest_canonical_block_above (module Conn) block.height
Metrics.time ~label:"get_nearest_canonical_block_above" (fun () ->
get_nearest_canonical_block_above (module Conn) block.height )
in
let%bind canonical_block_below_id, _below_height =
get_nearest_canonical_block_below (module Conn) block.height
Metrics.time ~label:"get_neareast_canonical_block_below" (fun () ->
get_nearest_canonical_block_below (module Conn) block.height )
in
(* we can always find this chain: the genesis block should be marked as canonical, and we've found a
canonical block above this one *)
let%bind canonical_blocks =
get_subchain
(module Conn)
~start_block_id:canonical_block_below_id
~end_block_id:canonical_block_above_id
Metrics.time ~label:"get_subchain (< canonical_height)" (fun () ->
get_subchain
(module Conn)
~start_block_id:canonical_block_below_id
~end_block_id:canonical_block_above_id )
in
Mina_caqti.deferred_result_list_fold canonical_blocks ~init:()
~f:(fun () block ->
let%bind () =
mark_as_canonical (module Conn) ~state_hash:block.state_hash
in
mark_as_orphaned
(module Conn)
~state_hash:block.state_hash ~height:block.height )
Metrics.time ~label:"mark_as_canonical (< canonical_height)"
(fun () ->
Mina_caqti.deferred_result_list_fold canonical_blocks ~init:()
~f:(fun () block ->
let%bind () =
mark_as_canonical (module Conn) ~state_hash:block.state_hash
in
mark_as_orphaned
(module Conn)
~state_hash:block.state_hash ~height:block.height ) )
else
(* a block at or above highest canonical block, not high enough to mark any blocks as canonical *)
Deferred.Result.return ()
Expand Down Expand Up @@ -3734,7 +3793,10 @@ let add_block_aux ?(retries = 3) ~logger ~pool ~add_block ~hash
~parent_hash:(hash block) ~parent_id:block_id
in
(* update chain status for existing blocks *)
let%bind () = Block.update_chain_status (module Conn) ~block_id in
let%bind () =
Metrics.time ~label:"update_chain_status" (fun () ->
Block.update_chain_status (module Conn) ~block_id )
in
let%bind () =
match delete_older_than with
| Some num_blocks ->
Expand Down
1 change: 1 addition & 0 deletions src/lib/mina_caqti/dune
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
async_kernel
;; local libraries
mina_base
logger
)
(preprocess
(pps ppx_mina ppx_version ppx_jane ppx_custom_printf h_list.ppx))
Expand Down
16 changes: 10 additions & 6 deletions src/lib/mina_caqti/mina_caqti.ml
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,19 @@ let select_insert_into_cols ~(select : string * 'select Caqti_type.t)
@@ select_cols ~select:(fst select) ~table_name ?tannot ~cols:(fst cols) ()
)
value
>>= function
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous code with >>= function looked cleaner

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. I changed it to add some more logs. Let me revert those changes.

>>= fun res ->
match res with
| Some id ->
return id
| None ->
Conn.find
( Caqti_request.find (snd cols) (snd select)
@@ insert_into_cols ~returning:(fst select) ~table_name ?tannot
~cols:(fst cols) () )
value
let%map res =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant let%map introduced

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again this change was for logging. Let me undo that.

Conn.find
( Caqti_request.find (snd cols) (snd select)
@@ insert_into_cols ~returning:(fst select) ~table_name ?tannot
~cols:(fst cols) () )
value
in
res

let query ~f pool =
match%bind Caqti_async.Pool.use f pool with
Expand Down