From 6f8a738735e98efa17d31214d5bb7b263bb2fef1 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 2 Dec 2019 10:36:38 +0300 Subject: [PATCH 1/4] fix address sum cache on the first call address sum cache starts task to fetch a value from the DB and return nil --- .../api/rpc/stats_controller_test.exs | 2 ++ .../lib/explorer/chain/cache/address_sum.ex | 28 +++---------------- .../runner/address/token_balances_test.exs | 2 +- 3 files changed, 7 insertions(+), 25 deletions(-) diff --git a/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/stats_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/stats_controller_test.exs index a57db2221b..5025f5db9e 100644 --- a/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/stats_controller_test.exs +++ b/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/stats_controller_test.exs @@ -106,6 +106,8 @@ defmodule BlockScoutWeb.API.RPC.StatsControllerTest do describe "ethsupply" do test "returns total supply from DB", %{conn: conn} do + insert(:address, fetched_coin_balance: 6) + params = %{ "module" => "stats", "action" => "ethsupply" diff --git a/apps/explorer/lib/explorer/chain/cache/address_sum.ex b/apps/explorer/lib/explorer/chain/cache/address_sum.ex index b0d3326312..dcf162cb21 100644 --- a/apps/explorer/lib/explorer/chain/cache/address_sum.ex +++ b/apps/explorer/lib/explorer/chain/cache/address_sum.ex @@ -16,33 +16,13 @@ defmodule Explorer.Chain.Cache.AddressSum do alias Explorer.Chain defp handle_fallback(:sum) do - # This will get the task PID if one exists and launch a new task if not - # See next `handle_fallback` definition - get_async_task() + result = fetch_from_db() - {:return, nil} + {:update, result} end - defp handle_fallback(:async_task) do - # If this gets called it means an async task was requested, but none exists - # so a new one needs to be launched - {:ok, task} = - Task.start(fn -> - try do - result = Chain.fetch_sum_coin_total_supply() - - set_sum(result) - rescue - e -> - Logger.debug([ - "Coudn't update address sum test #{inspect(e)}" - ]) - end - - set_async_task(nil) - end) - - {:update, task} + defp fetch_from_db do + Chain.fetch_sum_coin_total_supply() end # By setting this as a `callback` an async task will be started each time the diff --git a/apps/explorer/test/explorer/chain/import/runner/address/token_balances_test.exs b/apps/explorer/test/explorer/chain/import/runner/address/token_balances_test.exs index a40cd8599e..a35fbcc578 100644 --- a/apps/explorer/test/explorer/chain/import/runner/address/token_balances_test.exs +++ b/apps/explorer/test/explorer/chain/import/runner/address/token_balances_test.exs @@ -93,7 +93,7 @@ defmodule Explorer.Chain.Import.Runner.Address.TokenBalancesTest do value_fetched_at: DateTime.utc_now() } - run_changes(new_changes, options) |> IO.inspect() + run_changes(new_changes, options) end end From 6ea58c93b003a554f39b39659be53e85880ca1dd Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 2 Dec 2019 10:54:27 +0300 Subject: [PATCH 2/4] fix dialyzer --- apps/explorer/lib/explorer/chain/cache/address_sum.ex | 6 +++++- .../explorer/test/explorer/chain/cache/address_sum_test.exs | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/apps/explorer/lib/explorer/chain/cache/address_sum.ex b/apps/explorer/lib/explorer/chain/cache/address_sum.ex index dcf162cb21..f7c5d5a8d5 100644 --- a/apps/explorer/lib/explorer/chain/cache/address_sum.ex +++ b/apps/explorer/lib/explorer/chain/cache/address_sum.ex @@ -18,7 +18,11 @@ defmodule Explorer.Chain.Cache.AddressSum do defp handle_fallback(:sum) do result = fetch_from_db() - {:update, result} + if Application.get_env(:explorer, __MODULE__)[:enabled] do + {:update, result} + else + {:return, result} + end end defp fetch_from_db do diff --git a/apps/explorer/test/explorer/chain/cache/address_sum_test.exs b/apps/explorer/test/explorer/chain/cache/address_sum_test.exs index 707c70635f..ac712c5287 100644 --- a/apps/explorer/test/explorer/chain/cache/address_sum_test.exs +++ b/apps/explorer/test/explorer/chain/cache/address_sum_test.exs @@ -12,7 +12,7 @@ defmodule Explorer.Chain.Cache.AddressSumTest do test "returns default address sum" do result = AddressSum.get_sum() - assert is_nil(result) + assert result == 0 end test "updates cache if initial value is zero" do From 88ad137f49ebca2c766012fb2fa1c8de6ed961fc Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 2 Dec 2019 10:55:29 +0300 Subject: [PATCH 3/4] add CHANGELOG entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62ce88bd8d..2dc425ea27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features ### Fixes +- [#2901](https://github.com/poanetwork/blockscout/pull/2901) - fix address sum cache ### Chore From bcac00025b6a4d39723ecd9496fbece61d1733ff Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Tue, 3 Dec 2019 10:26:55 +0300 Subject: [PATCH 4/4] add async task fallback --- .../lib/explorer/chain/cache/address_sum.ex | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/apps/explorer/lib/explorer/chain/cache/address_sum.ex b/apps/explorer/lib/explorer/chain/cache/address_sum.ex index f7c5d5a8d5..4a66e1369c 100644 --- a/apps/explorer/lib/explorer/chain/cache/address_sum.ex +++ b/apps/explorer/lib/explorer/chain/cache/address_sum.ex @@ -25,6 +25,28 @@ defmodule Explorer.Chain.Cache.AddressSum do end end + defp handle_fallback(:async_task) do + # If this gets called it means an async task was requested, but none exists + # so a new one needs to be launched + {:ok, task} = + Task.start(fn -> + try do + result = fetch_from_db() + + set_sum(result) + rescue + e -> + Logger.debug([ + "Coudn't update address sum test #{inspect(e)}" + ]) + end + + set_async_task(nil) + end) + + {:update, task} + end + defp fetch_from_db do Chain.fetch_sum_coin_total_supply() end