diff --git a/lib/plausible/application.ex b/lib/plausible/application.ex index 391047e74df2..b65d8c4a7fe9 100644 --- a/lib/plausible/application.ex +++ b/lib/plausible/application.ex @@ -26,11 +26,13 @@ defmodule Plausible.Application do Plausible.ImportDeletionRepo, Plausible.Cache.Adapter.child_spec(:customer_currency, :cache_customer_currency, ttl_check_interval: :timer.minutes(5), + n_lock_partitions: 1, global_ttl: :timer.minutes(60) ), Plausible.Cache.Adapter.child_spec(:user_agents, :cache_user_agents, ttl_check_interval: :timer.minutes(5), global_ttl: :timer.minutes(60), + n_lock_partitions: 1, ets_options: [read_concurrency: true, write_concurrency: true] ), Plausible.Cache.Adapter.child_specs(:sessions, :cache_sessions, @@ -39,7 +41,11 @@ defmodule Plausible.Application do ets_options: [read_concurrency: true, write_concurrency: true] ), warmed_cache(Plausible.Site.Cache, - adapter_opts: [ttl_check_interval: false, ets_options: [read_concurrency: true]], + adapter_opts: [ + n_lock_partitions: 1, + ttl_check_interval: false, + ets_options: [read_concurrency: true] + ], warmers: [ refresh_all: {Plausible.Site.Cache.All, @@ -49,7 +55,11 @@ defmodule Plausible.Application do ] ), warmed_cache(Plausible.Shield.IPRuleCache, - adapter_opts: [ttl_check_interval: false, ets_options: [read_concurrency: true]], + adapter_opts: [ + n_lock_partitions: 1, + ttl_check_interval: false, + ets_options: [read_concurrency: true] + ], warmers: [ refresh_all: {Plausible.Shield.IPRuleCache.All, @@ -59,7 +69,11 @@ defmodule Plausible.Application do ] ), warmed_cache(Plausible.Shield.CountryRuleCache, - adapter_opts: [ttl_check_interval: false, ets_options: [read_concurrency: true]], + adapter_opts: [ + n_lock_partitions: 1, + ttl_check_interval: false, + ets_options: [read_concurrency: true] + ], warmers: [ refresh_all: {Plausible.Shield.CountryRuleCache.All, @@ -69,7 +83,11 @@ defmodule Plausible.Application do ] ), warmed_cache(Plausible.Shield.PageRuleCache, - adapter_opts: [ttl_check_interval: false, ets_options: [:bag, read_concurrency: true]], + adapter_opts: [ + n_lock_partitions: 1, + ttl_check_interval: false, + ets_options: [:bag, read_concurrency: true] + ], warmers: [ refresh_all: {Plausible.Shield.PageRuleCache.All, @@ -79,7 +97,11 @@ defmodule Plausible.Application do ] ), warmed_cache(Plausible.Shield.HostnameRuleCache, - adapter_opts: [ttl_check_interval: false, ets_options: [:bag, read_concurrency: true]], + adapter_opts: [ + n_lock_partitions: 1, + ttl_check_interval: false, + ets_options: [:bag, read_concurrency: true] + ], warmers: [ refresh_all: {Plausible.Shield.HostnameRuleCache.All, @@ -90,7 +112,11 @@ defmodule Plausible.Application do ), on_ee do warmed_cache(Plausible.Stats.SamplingCache, - adapter_opts: [ttl_check_interval: false, read_concurrency: true], + adapter_opts: [ + n_lock_partitions: 1, + ttl_check_interval: false, + read_concurrency: true + ], warmers: [ refresh_all: {Plausible.Stats.SamplingCache.All, diff --git a/lib/plausible/cache/adapter.ex b/lib/plausible/cache/adapter.ex index ce8f0658d1d2..644276004aa9 100644 --- a/lib/plausible/cache/adapter.ex +++ b/lib/plausible/cache/adapter.ex @@ -67,7 +67,7 @@ defmodule Plausible.Cache.Adapter do @spec get(atom(), any(), (-> any())) :: any() def get(cache_name, key, fallback_fn) do full_cache_name = get_name(cache_name, key) - ConCache.get_or_store(full_cache_name, key, fallback_fn) + ConCache.dirty_get_or_store(full_cache_name, key, fallback_fn) catch :exit, _ -> Logger.error("Error retrieving key from '#{inspect(cache_name)}'") @@ -77,7 +77,7 @@ defmodule Plausible.Cache.Adapter do @spec fetch(atom(), any(), (-> any())) :: any() def fetch(cache_name, key, fallback_fn) do full_cache_name = get_name(cache_name, key) - ConCache.fetch_or_store(full_cache_name, key, fallback_fn) + ConCache.dirty_fetch_or_store(full_cache_name, key, fallback_fn) catch :exit, _ -> Logger.error("Error fetching key from '#{inspect(cache_name)}'") @@ -85,14 +85,9 @@ defmodule Plausible.Cache.Adapter do end @spec put(atom(), any(), any()) :: any() - def put(cache_name, key, value, opts \\ []) do + def put(cache_name, key, value, _opts \\ []) do full_cache_name = get_name(cache_name, key) - - if opts[:dirty?] do - :ok = ConCache.dirty_put(full_cache_name, key, value) - else - :ok = ConCache.put(full_cache_name, key, value) - end + :ok = ConCache.dirty_put(full_cache_name, key, value) value catch @@ -139,22 +134,6 @@ defmodule Plausible.Cache.Adapter do [] end - @spec with_lock(atom(), any(), pos_integer(), (-> result)) :: {:ok, result} | {:error, :timeout} - when result: any() - def with_lock(cache_name, key, timeout, fun) do - full_cache_name = get_name(cache_name, key) - result = ConCache.isolated(full_cache_name, key, timeout, fun) - {:ok, result} - catch - :exit, {:timeout, _} -> - Sentry.capture_message( - "Timeout while executing with lock on key in '#{inspect(cache_name)}'", - extra: %{key: key} - ) - - {:error, :timeout} - end - @spec get_names(atom()) :: [atom()] def get_names(cache_name) do partitions = partitions(cache_name) diff --git a/mix.exs b/mix.exs index 25909e0b2d4d..d2cf5a09627c 100644 --- a/mix.exs +++ b/mix.exs @@ -141,7 +141,8 @@ defmodule Plausible.MixProject do {:ex_aws_s3, "~> 2.5"}, {:sweet_xml, "~> 0.7.4"}, {:zstream, "~> 0.6.4"}, - {:con_cache, "~> 1.1.1"}, + {:con_cache, + git: "https://github.com/aerosol/con_cache", branch: "ensure-dirty-ops-emit-telemetry"}, {:req, "~> 0.5.0"}, {:happy_tcp, github: "ruslandoga/happy_tcp", only: [:ce, :ce_dev, :ce_test]}, {:ex_json_schema, "~> 0.10.2"}, diff --git a/mix.lock b/mix.lock index 3217cbc4c3b4..38c06ece6d8c 100644 --- a/mix.lock +++ b/mix.lock @@ -17,7 +17,7 @@ "combination": {:hex, :combination, "0.0.3", "746aedca63d833293ec6e835aa1f34974868829b1486b1e1cb0685f0b2ae1f41", [:mix], [], "hexpm", "72b099f463df42ef7dc6371d250c7070b57b6c5902853f69deb894f79eda18ca"}, "combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], [], "hexpm", "1b1dbc1790073076580d0d1d64e42eae2366583e7aecd455d1215b0d16f2451b"}, "comeonin": {:hex, :comeonin, "5.4.0", "246a56ca3f41d404380fc6465650ddaa532c7f98be4bda1b4656b3a37cc13abe", [:mix], [], "hexpm", "796393a9e50d01999d56b7b8420ab0481a7538d0caf80919da493b4a6e51faf1"}, - "con_cache": {:hex, :con_cache, "1.1.1", "9f47a68dfef5ac3bbff8ce2c499869dbc5ba889dadde6ac4aff8eb78ddaf6d82", [:mix], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "1def4d1bec296564c75b5bbc60a19f2b5649d81bfa345a2febcc6ae380e8ae15"}, + "con_cache": {:git, "https://github.com/aerosol/con_cache", "d050e133742e512cf2caa4598607e60dd0161072", [branch: "ensure-dirty-ops-emit-telemetry"]}, "cors_plug": {:hex, :cors_plug, "3.0.3", "7c3ac52b39624bc616db2e937c282f3f623f25f8d550068b6710e58d04a0e330", [:mix], [{:plug, "~> 1.13", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "3f2d759e8c272ed3835fab2ef11b46bddab8c1ab9528167bd463b6452edf830d"}, "cowboy": {:hex, :cowboy, "2.13.0", "09d770dd5f6a22cc60c071f432cd7cb87776164527f205c5a6b0f24ff6b38990", [:make, :rebar3], [{:cowlib, ">= 2.14.0 and < 3.0.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, ">= 1.8.0 and < 3.0.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "e724d3a70995025d654c1992c7b11dbfea95205c047d86ff9bf1cda92ddc5614"}, "cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"},