|
|
@ -6,6 +6,12 @@ defmodule Explorer.Chain.Cache.MinMissingBlockNumber do |
|
|
|
use GenServer |
|
|
|
use GenServer |
|
|
|
|
|
|
|
|
|
|
|
alias Explorer.Chain |
|
|
|
alias Explorer.Chain |
|
|
|
|
|
|
|
alias Explorer.Chain.Cache.BlockNumber |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@default_batch_size 100_000 |
|
|
|
|
|
|
|
@normal_interval 10 |
|
|
|
|
|
|
|
@increased_interval :timer.minutes(20) |
|
|
|
|
|
|
|
@default_last_fetched_number -1 |
|
|
|
|
|
|
|
|
|
|
|
@doc """ |
|
|
|
@doc """ |
|
|
|
Starts a process to periodically update the % of blocks indexed. |
|
|
|
Starts a process to periodically update the % of blocks indexed. |
|
|
@ -16,37 +22,52 @@ defmodule Explorer.Chain.Cache.MinMissingBlockNumber do |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
@impl true |
|
|
|
@impl true |
|
|
|
def init(args) do |
|
|
|
def init(_) do |
|
|
|
Task.start_link(&fetch_min_missing_block/0) |
|
|
|
schedule_next_consolidation(@normal_interval) |
|
|
|
|
|
|
|
{:ok, %{last_fetched_number: @default_last_fetched_number}} |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
schedule_next_consolidation() |
|
|
|
def fetch_min_missing_block(last_fetched_number) do |
|
|
|
|
|
|
|
from = last_fetched_number + 1 |
|
|
|
|
|
|
|
to = last_fetched_number + batch_size() |
|
|
|
|
|
|
|
max_block_number = BlockNumber.get_max() - 1 |
|
|
|
|
|
|
|
|
|
|
|
{:ok, args} |
|
|
|
{corrected_to, continue?} = if to >= max_block_number, do: {max_block_number, false}, else: {to, true} |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
result = Chain.fetch_min_missing_block_cache(from, corrected_to) |
|
|
|
|
|
|
|
|
|
|
|
def fetch_min_missing_block do |
|
|
|
cond do |
|
|
|
result = Chain.fetch_min_missing_block_cache() |
|
|
|
not is_nil(result) -> |
|
|
|
|
|
|
|
params = %{ |
|
|
|
|
|
|
|
counter_type: "min_missing_block_number", |
|
|
|
|
|
|
|
value: result |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
unless is_nil(result) do |
|
|
|
Chain.upsert_last_fetched_counter(params) |
|
|
|
params = %{ |
|
|
|
schedule_next_consolidation(@increased_interval) |
|
|
|
counter_type: "min_missing_block_number", |
|
|
|
@default_last_fetched_number |
|
|
|
value: result |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Chain.upsert_last_fetched_counter(params) |
|
|
|
continue? -> |
|
|
|
|
|
|
|
schedule_next_consolidation(@normal_interval) |
|
|
|
|
|
|
|
corrected_to |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
true -> |
|
|
|
|
|
|
|
schedule_next_consolidation(@increased_interval) |
|
|
|
|
|
|
|
@default_last_fetched_number |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
defp schedule_next_consolidation do |
|
|
|
defp schedule_next_consolidation(interval) do |
|
|
|
Process.send_after(self(), :fetch_min_missing_block, :timer.minutes(20)) |
|
|
|
Process.send_after(self(), :fetch_min_missing_block, interval) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
@impl true |
|
|
|
@impl true |
|
|
|
def handle_info(:fetch_min_missing_block, state) do |
|
|
|
def handle_info(:fetch_min_missing_block, %{last_fetched_number: last_fetched_number} = state) do |
|
|
|
fetch_min_missing_block() |
|
|
|
new_last_number = fetch_min_missing_block(last_fetched_number) |
|
|
|
|
|
|
|
{:noreply, %{state | last_fetched_number: new_last_number}} |
|
|
|
schedule_next_consolidation() |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
{:noreply, state} |
|
|
|
defp batch_size do |
|
|
|
|
|
|
|
Application.get_env(:explorer, __MODULE__)[:batch_size] || @default_batch_size |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|