Skip to content

Commit

Permalink
Do not cache ChainIndex when bootstrapping (#1090)
Browse files Browse the repository at this point in the history
* Do not pre-cache index when starting the node (because LRU is a bottleneck)

* Do not pre-cache index when node is bootstrapping (because LRU is a bottleneck)

* LINT: LRU get/put in the same function

* also skip cache when bootstrapping in the get_tx_entry
  • Loading branch information
bchamagne committed Jun 14, 2023
1 parent 4b7944f commit 8f6b5e4
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions lib/archethic/db/embedded_impl/chain_index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,13 @@ defmodule Archethic.DB.EmbeddedImpl.ChainIndex do
end

defp do_scan_summary_table(fd) do
with {:ok, <<current_curve_id::8, current_hash_type::8>>} <- :file.read(fd, 2),
with {:ok, <<_current_curve_id::8, current_hash_type::8>>} <- :file.read(fd, 2),
hash_size <- Crypto.hash_size(current_hash_type),
{:ok, current_digest} <- :file.read(fd, hash_size),
{:ok, _current_digest} <- :file.read(fd, hash_size),
{:ok, <<genesis_curve_id::8, genesis_hash_type::8>>} <- :file.read(fd, 2),
hash_size <- Crypto.hash_size(genesis_hash_type),
{:ok, genesis_digest} <- :file.read(fd, hash_size),
{:ok, <<size::32, offset::32>>} <- :file.read(fd, 8) do
current_address = <<current_curve_id::8, current_hash_type::8, current_digest::binary>>
{:ok, <<size::32, _offset::32>>} <- :file.read(fd, 8) do
genesis_address = <<genesis_curve_id::8, genesis_hash_type::8, genesis_digest::binary>>

:ets.update_counter(
Expand All @@ -86,13 +85,6 @@ defmodule Archethic.DB.EmbeddedImpl.ChainIndex do
{genesis_address, 0, 0}
)

## Store each transaction in the LRU cache. If cache is full, the oldest entries will be evicted
LRU.put(@archetic_db_tx_index_cache, current_address, %{
size: size,
offset: offset,
genesis_address: genesis_address
})

do_scan_summary_table(fd)
else
:eof ->
Expand Down Expand Up @@ -158,13 +150,15 @@ defmodule Archethic.DB.EmbeddedImpl.ChainIndex do
[:binary, :append]
)

# Write fast lookup entry for this transaction on LRU cache
:ok =
# pre-cache item (when node is not bootstrapping)
# so they are already cached when we'll need them
if Archethic.up?() do
LRU.put(@archetic_db_tx_index_cache, tx_address, %{
size: size,
offset: last_offset,
genesis_address: genesis_address
})
end

:ets.update_counter(
@archethic_db_chain_stats,
Expand Down Expand Up @@ -226,10 +220,17 @@ defmodule Archethic.DB.EmbeddedImpl.ChainIndex do
def get_tx_entry(address, db_path) do
case LRU.get(@archetic_db_tx_index_cache, address) do
nil ->
# If the transaction is not found in the in memory lookup
# we scan the index file for the subset of the transaction to find the relative information
# This will update the LRU cache if the transaction is found
search_tx_entry(address, db_path)
case search_tx_entry(address, db_path) do
{:ok, entry} ->
if Archethic.up?() do
LRU.put(@archetic_db_tx_index_cache, address, entry)
end

{:ok, entry}

{:error, :not_exists} ->
{:error, :not_exists}
end

entry = %{} ->
{:ok, entry}
Expand All @@ -249,12 +250,6 @@ defmodule Archethic.DB.EmbeddedImpl.ChainIndex do
{genesis_address, size, offset} ->
:file.close(fd)

LRU.put(@archetic_db_tx_index_cache, search_address, %{
size: size,
offset: offset,
genesis_address: genesis_address
})

{:ok, %{genesis_address: genesis_address, size: size, offset: offset}}
end

Expand Down

0 comments on commit 8f6b5e4

Please sign in to comment.