Skip to content

Commit

Permalink
Merge pull request blockscout#9426 from blockscout/np-fix-tabs-counte…
Browse files Browse the repository at this point in the history
…r-bug

Fix tabs counter cache bug
  • Loading branch information
vbaranov authored Feb 29, 2024
2 parents df1d259 + d693529 commit 0567d6f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

- [#9502](https://github.com/blockscout/blockscout/pull/9502) - Add batch_size and concurrency envs for tt token type migration
- [#9493](https://github.com/blockscout/blockscout/pull/9493) - Fix API response for unknown blob hashes
- [#9426](https://github.com/blockscout/blockscout/pull/9426) - Fix tabs counter cache bug

### Chore

Expand Down
2 changes: 1 addition & 1 deletion apps/explorer/lib/explorer/chain/address/counters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ defmodule Explorer.Chain.Address.Counters do
case res do
{:ok, {txs_type, txs_hashes}} when txs_type in @txs_types ->
acc
|> (&Map.put(&1, :txs_types, [txs_type | &1[:txs_types] || []])).()
|> (&Map.put(&1, :txs_types, [txs_type | &1[:txs_types]])).()
|> (&Map.put(&1, :txs_hashes, &1[:txs_hashes] ++ txs_hashes)).()

{:ok, {type, counter}} ->
Expand Down
30 changes: 13 additions & 17 deletions apps/explorer/lib/explorer/chain/cache/addresses_tabs_counters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ defmodule Explorer.Chain.Cache.AddressesTabsCounters do
end

@spec set_counter(counter_type, String.t(), non_neg_integer()) :: :ok
def set_counter(counter_type, address_hash, counter, need_to_modify_state? \\ true) do
def set_counter(counter_type, address_hash, counter) do
:ets.insert(@cache_name, {cache_key(address_hash, counter_type), {DateTime.utc_now(), counter}})
if need_to_modify_state?, do: ignore_txs(counter_type, address_hash)

:ok
end
Expand All @@ -42,10 +41,6 @@ defmodule Explorer.Chain.Cache.AddressesTabsCounters do
address_hash |> task_cache_key(counter_type) |> fetch_from_cache(@cache_name, nil)
end

@spec ignore_txs(atom, String.t()) :: :ignore | :ok
def ignore_txs(:txs, address_hash), do: GenServer.cast(__MODULE__, {:ignore_txs, address_hash})
def ignore_txs(_counter_type, _address_hash), do: :ignore

def save_txs_counter_progress(address_hash, results) do
GenServer.cast(__MODULE__, {:set_txs_state, address_hash, results})
end
Expand All @@ -67,11 +62,6 @@ defmodule Explorer.Chain.Cache.AddressesTabsCounters do
{:ok, %{}}
end

@impl true
def handle_cast({:ignore_txs, address_hash}, state) do
{:noreply, Map.put(state, lowercased_string(address_hash), {:updated, DateTime.utc_now()})}
end

@impl true
def handle_cast({:set_txs_state, address_hash, %{txs_types: txs_types} = results}, state) do
address_hash = lowercased_string(address_hash)
Expand All @@ -95,16 +85,22 @@ defmodule Explorer.Chain.Cache.AddressesTabsCounters do
|> Enum.count()
|> min(Counters.counters_limit())

if counter == Counters.counters_limit() || Enum.count(address_state[:txs_types]) == 3 do
set_counter(:txs, address_hash, counter, false)
{:noreply, Map.put(state, address_hash, {:updated, DateTime.utc_now()})}
else
{:noreply, Map.put(state, address_hash, address_state)}
cond do
Enum.count(address_state[:txs_types]) == 3 ->
set_counter(:txs, address_hash, counter)
{:noreply, Map.put(state, address_hash, nil)}

counter == Counters.counters_limit() ->
set_counter(:txs, address_hash, counter)
{:noreply, Map.put(state, address_hash, :limit_value)}

true ->
{:noreply, Map.put(state, address_hash, address_state)}
end
end
end

defp ignored?({:updated, datetime}), do: up_to_date?(datetime, ttl())
defp ignored?(:limit_value), do: true
defp ignored?(_), do: false

defp check_staleness(nil), do: nil
Expand Down

0 comments on commit 0567d6f

Please sign in to comment.