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

Reduce concurrency while downloading aggregate #1133

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 0 additions & 2 deletions lib/archethic/beacon_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ defmodule Archethic.BeaconChain do
end
end

def load_transaction(_), do: :ok

defp validate_slot(slot = %Slot{}) do
cond do
!SlotValidation.valid_transaction_attestations?(slot) ->
Expand Down
44 changes: 26 additions & 18 deletions lib/archethic/self_repair/sync.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ defmodule Archethic.SelfRepair.Sync do

# Process first the old aggregates
fetch_summaries_aggregates(last_sync_date, last_summary_time, download_nodes)
|> Enum.each(&process_summary_aggregate(&1, download_nodes))
|> Stream.each(&process_summary_aggregate(&1, download_nodes))
|> Stream.run()

# Then process the last one to have the last P2P view
last_aggregate = BeaconChain.fetch_and_aggregate_summaries(last_summary_time, download_nodes)
Expand All @@ -141,24 +142,31 @@ defmodule Archethic.SelfRepair.Sync do
end

defp fetch_summaries_aggregates(last_sync_date, last_summary_time, download_nodes) do
last_sync_date
|> BeaconChain.next_summary_dates()
# Take only the previous summaries before the last one
|> Stream.take_while(fn date ->
DateTime.compare(date, last_summary_time) ==
:lt
end)
# Fetch the beacon summaries aggregate
|> Task.async_stream(fn date ->
Logger.debug("Fetch summary aggregate for #{date}")

storage_nodes =
date
|> Crypto.derive_beacon_aggregate_address()
|> Election.chain_storage_nodes(download_nodes)
dates =
last_sync_date
|> BeaconChain.next_summary_dates()
# Take only the previous summaries before the last one
|> Stream.take_while(fn date ->
DateTime.compare(date, last_summary_time) ==
:lt
end)

BeaconChain.fetch_summaries_aggregate(date, storage_nodes)
end)
# Fetch the beacon summaries aggregate
Task.Supervisor.async_stream(
TaskSupervisor,
dates,
fn date ->
Logger.debug("Fetch summary aggregate for #{date}")

storage_nodes =
date
|> Crypto.derive_beacon_aggregate_address()
|> Election.chain_storage_nodes(download_nodes)

BeaconChain.fetch_summaries_aggregate(date, storage_nodes)
end,
max_concurrency: 2
)
|> Stream.filter(fn
{:ok, {:ok, %SummaryAggregate{}}} ->
true
Expand Down