From df93bb045e8e98d0ce15da651a00c9e45a594c00 Mon Sep 17 00:00:00 2001 From: Edmondfrank Date: Fri, 18 Oct 2024 19:24:09 +0800 Subject: [PATCH] Try replace redis with riak [step 1]. Signed-off-by: Edmondfrank --- config/dev.exs | 2 ++ config/prod.exs | 2 ++ lib/compass_admin/agents/deploy_agent.ex | 16 +++++++++++++--- lib/compass_admin/application.ex | 12 ++++++++++++ lib/compass_admin/riak_pool.ex | 20 ++++++++++++++++++++ mix.exs | 1 + mix.lock | 7 +++++++ 7 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 lib/compass_admin/riak_pool.ex diff --git a/config/dev.exs b/config/dev.exs index 169f36e..eb2fd12 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -18,6 +18,8 @@ config :compass_admin, CompassAdmin.Cluster, config :compass_admin, :redis_url, "redis://10.7.0.3:6379/1" +config :compass_admin, :riak, ['192.168.240.31', 8087] + config :amqp, connections: [ compass_conn: [url: "amqp://admin:admin@10.7.0.3:5672"], diff --git a/config/prod.exs b/config/prod.exs index 79dbedd..30d13e8 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -23,6 +23,8 @@ config :compass_admin, CompassAdmin.Cluster, config :compass_admin, :redis_url, "redis://10.7.0.3:6379/1" +config :compass_admin, :riak, ['127.0.0.1', 8087] + config :amqp, connections: [ compass_conn: [url: "amqp://admin:admin@localhost:5672"], diff --git a/lib/compass_admin/agents/deploy_agent.ex b/lib/compass_admin/agents/deploy_agent.ex index 3d1f377..48d4256 100644 --- a/lib/compass_admin/agents/deploy_agent.ex +++ b/lib/compass_admin/agents/deploy_agent.ex @@ -1,9 +1,11 @@ defmodule CompassAdmin.Agents.DeployAgent do @max_lines 5000 + @bucket "deploy_states" import Ecto.Query alias CompassAdmin.Repo alias CompassAdmin.User + alias CompassAdmin.RiakPool def init(cache_key) do restore(cache_key) @@ -101,8 +103,8 @@ defmodule CompassAdmin.Agents.DeployAgent do end defp restore(cache_key) do - with {:ok, cached} <- Redix.command(:redix, ["GET", "compass:admin:#{cache_key}"]) do - if cached != nil, do: :erlang.binary_to_term(cached), else: nil + with cached <- Riak.find(RiakPool.conn, @bucket, "compass:admin:#{cache_key}") do + if(cached, do: :erlang.binary_to_term(cached.data), else: nil) end || %{ state: :ok, @@ -119,7 +121,15 @@ defmodule CompassAdmin.Agents.DeployAgent do } end + + defp save(cache_key, state) do - Redix.command(:redix, ["SET", "compass:admin:#{cache_key}", :erlang.term_to_binary(state)]) + cached = + Riak.Object.create( + bucket: @bucket, + key: "compass:admin:#{cache_key}", + data: :erlang.term_to_binary(state) + ) + Riak.put(RiakPool.conn, cached) end end diff --git a/lib/compass_admin/application.ex b/lib/compass_admin/application.ex index f3dc615..2205d41 100644 --- a/lib/compass_admin/application.ex +++ b/lib/compass_admin/application.ex @@ -10,6 +10,7 @@ defmodule CompassAdmin.Application do topologies = Application.get_env(:libcluster, :topologies) redis_url = Application.get_env(:compass_admin, :redis_url, "") %{host: redis_host, port: redis_port, userinfo: userinfo, path: path} = URI.parse(redis_url) + [riak_host, riak_port] = Application.get_env(:compass_admin, :riak, ['127.0.0.1', 8087]) auth = case userinfo do @@ -39,6 +40,8 @@ defmodule CompassAdmin.Application do CompassAdmin.DockerTokenCacher, # Start the Endpoint (http/https) CompassAdminWeb.Endpoint, + # Start Riak + :poolboy.child_spec(:riak_pool, riak_config(), [riak_host, riak_port]), # Start Redix {Redix, {System.get_env("REDIS_URL") || redis_url, [name: :redix, backoff_max: 2_000, timeout: 2_000], }}, {Redlock, @@ -87,4 +90,13 @@ defmodule CompassAdmin.Application do CompassAdminWeb.Endpoint.config_change(changed, removed) :ok end + + defp riak_config() do + [ + name: {:local, CompassAdmin.RiakPool}, + worker_module: CompassAdmin.RiakPool, + size: 5, + max_overflow: 0 + ] + end end diff --git a/lib/compass_admin/riak_pool.ex b/lib/compass_admin/riak_pool.ex new file mode 100644 index 0000000..e8608ad --- /dev/null +++ b/lib/compass_admin/riak_pool.ex @@ -0,0 +1,20 @@ +defmodule CompassAdmin.RiakPool do + use GenServer + + def conn do + :poolboy.transaction(__MODULE__, fn(worker)-> GenServer.call(worker, :conn) end) + end + + def start_link(opts) do + GenServer.start_link(__MODULE__, opts) + end + + def init(opts) do + [host, port] = opts + Riak.Connection.start_link(host, port) + end + + def handle_call(:conn, _from, state) do + {:reply, state, state} + end +end diff --git a/mix.exs b/mix.exs index 15bcfda..bb1dc8c 100644 --- a/mix.exs +++ b/mix.exs @@ -45,6 +45,7 @@ defmodule CompassAdmin.MixProject do {:myxql, "~> 0.6.0"}, {:amqp, "~> 3.2"}, {:redix, "~> 1.1"}, + {:riak, git: "https://github.com/EdmondFrank/riak-elixir-client"}, {:redlock, "~> 1.0"}, {:ex_marshal, "0.0.13"}, {:timex, "~> 3.7"}, diff --git a/mix.lock b/mix.lock index ab8f622..461c921 100644 --- a/mix.lock +++ b/mix.lock @@ -32,6 +32,7 @@ "gen_stage": {:hex, :gen_stage, "1.2.1", "19d8b5e9a5996d813b8245338a28246307fd8b9c99d1237de199d21efc4c76a1", [:mix], [], "hexpm", "83e8be657fa05b992ffa6ac1e3af6d57aa50aace8f691fcf696ff02f8335b001"}, "gettext": {:hex, :gettext, "0.20.0", "75ad71de05f2ef56991dbae224d35c68b098dd0e26918def5bb45591d5c8d429", [:mix], [], "hexpm", "1c03b177435e93a47441d7f681a7040bd2a816ece9e2666d1c9001035121eb3d"}, "hackney": {:hex, :hackney, "1.20.1", "8d97aec62ddddd757d128bfd1df6c5861093419f8f7a4223823537bad5d064e2", [:rebar3], [{:certifi, "~> 2.12.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "fe9094e5f1a2a2c0a7d10918fee36bfec0ec2a979994cff8cfe8058cd9af38e3"}, + "hamcrest": {:hex, :basho_hamcrest, "0.4.1", "fb7b2c92d252a1e9db936750b86089addaebeb8f87967fb4bbdda61e8863338e", [:make, :mix, :rebar3], [], "hexpm", "26974025bc61bc09ef5b13be5dce5035ca11bf37bf4a865e9d86c455c942298f"}, "heroicons": {:hex, :heroicons, "0.5.3", "ee8ae8335303df3b18f2cc07f46e1cb6e761ba4cf2c901623fbe9a28c0bc51dd", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:phoenix_live_view, ">= 0.18.2", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}], "hexpm", "a210037e8a09ac17e2a0a0779d729e89c821c944434c3baa7edfc1f5b32f3502"}, "highlander": {:hex, :highlander, "0.2.1", "e59b459f857e89daf73f2598bf2b2c0479a435481e6101ea389fd3625919b052", [:mix], [], "hexpm", "5ba19a18358803d82a923511acec8ee85fac30731c5ca056f2f934bc3d3afd9a"}, "hpax": {:hex, :hpax, "0.1.2", "09a75600d9d8bbd064cdd741f21fc06fc1f4cf3d0fcc335e5aa19be1a7235c84", [:mix], [], "hexpm", "2c87843d5a23f5f16748ebe77969880e29809580efdaccd615cd3bed628a8c13"}, @@ -40,6 +41,7 @@ "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, "jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"}, "libcluster": {:hex, :libcluster, "3.3.2", "84c6ebfdc72a03805955abfb5ff573f71921a3e299279cc3445445d5af619ad1", [:mix], [{:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "8b691ce8185670fc8f3fc0b7ed59eff66c6889df890d13411f8f1a0e6871d8a5"}, + "meck": {:git, "https://github.com/eproxus/meck.git", "4ecc1ae9089edc6977e8c8c4cd41081513cc5590", [tag: "0.8.13"]}, "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"}, "mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"}, "mimerl": {:hex, :mimerl, "1.3.0", "d0cd9fc04b9061f82490f6581e0128379830e78535e017f7780f37fea7545726", [:rebar3], [], "hexpm", "a1e15a50d1887217de95f0b9b0793e32853f7c258a5cd227650889b38839fe9d"}, @@ -63,9 +65,11 @@ "plug_cowboy": {:hex, :plug_cowboy, "2.6.1", "9a3bbfceeb65eff5f39dab529e5cd79137ac36e913c02067dba3963a26efe9b2", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "de36e1a21f451a18b790f37765db198075c25875c64834bcc82d90b309eb6613"}, "plug_crypto": {:hex, :plug_crypto, "1.2.5", "918772575e48e81e455818229bf719d4ab4181fcbf7f85b68a35620f78d89ced", [:mix], [], "hexpm", "26549a1d6345e2172eb1c233866756ae44a9609bd33ee6f99147ab3fd87fd842"}, "poolboy": {:hex, :poolboy, "1.5.2", "392b007a1693a64540cead79830443abf5762f5d30cf50bc95cb2c1aaafa006b", [:rebar3], [], "hexpm", "dad79704ce5440f3d5a3681c8590b9dc25d1a561e8f5a9c995281012860901e3"}, + "pooler": {:hex, :pooler, "1.6.0", "f4f33c94ab3ab82565a2e31cea9efe4149a160651f3707a0a2669bc54aaf81c8", [:rebar3], [], "hexpm", "748c988fd2928de9577c882a49621863cab57809e3e1a88a14c9d3b55c6ab877"}, "prometheus": {:hex, :prometheus, "4.10.0", "792adbf0130ff61b5fa8826f013772af24b6e57b984445c8d602c8a0355704a1", [:mix, :rebar3], [{:quantile_estimator, "~> 0.2.1", [hex: :quantile_estimator, repo: "hexpm", optional: false]}], "hexpm", "2a99bb6dce85e238c7236fde6b0064f9834dc420ddbd962aac4ea2a3c3d59384"}, "prometheus_ex": {:hex, :prometheus_ex, "3.0.5", "fa58cfd983487fc5ead331e9a3e0aa622c67232b3ec71710ced122c4c453a02f", [:mix], [{:prometheus, "~> 4.0", [hex: :prometheus, repo: "hexpm", optional: false]}], "hexpm", "9fd13404a48437e044b288b41f76e64acd9735fb8b0e3809f494811dfa66d0fb"}, "prometheus_plugs": {:hex, :prometheus_plugs, "1.1.5", "25933d48f8af3a5941dd7b621c889749894d8a1082a6ff7c67cc99dec26377c5", [:mix], [{:accept, "~> 0.1", [hex: :accept, repo: "hexpm", optional: false]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}, {:prometheus_ex, "~> 1.1 or ~> 2.0 or ~> 3.0", [hex: :prometheus_ex, repo: "hexpm", optional: false]}, {:prometheus_process_collector, "~> 1.1", [hex: :prometheus_process_collector, repo: "hexpm", optional: true]}], "hexpm", "0273a6483ccb936d79ca19b0ab629aef0dba958697c94782bb728b920dfc6a79"}, + "protobuffs": {:git, "https://github.com/nhs-riak/erlang_protobuffs.git", "676a42b08b309d5b0bacc3f09364fbbdbb386aa4", [branch: "nhse-develop"]}, "quantile_estimator": {:hex, :quantile_estimator, "0.2.1", "ef50a361f11b5f26b5f16d0696e46a9e4661756492c981f7b2229ef42ff1cd15", [:rebar3], [], "hexpm", "282a8a323ca2a845c9e6f787d166348f776c1d4a41ede63046d72d422e3da946"}, "quantum": {:hex, :quantum, "3.5.0", "8d2c5ba68c55991e8975aca368e3ab844ba01f4b87c4185a7403280e2c99cf34", [:mix], [{:crontab, "~> 1.1", [hex: :crontab, repo: "hexpm", optional: false]}, {:gen_stage, "~> 0.14 or ~> 1.0", [hex: :gen_stage, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_registry, "~> 0.2", [hex: :telemetry_registry, repo: "hexpm", optional: false]}], "hexpm", "cab737d1d9779f43cb1d701f46dd05ea58146fd96238d91c9e0da662c1982bb6"}, "rabbit_common": {:hex, :rabbit_common, "3.12.13", "a163432b377411d6033344d5f6a8b12443d67c897c9374b9738cc609cab3161c", [:make, :rebar3], [{:credentials_obfuscation, "3.4.0", [hex: :credentials_obfuscation, repo: "hexpm", optional: false]}, {:recon, "2.5.3", [hex: :recon, repo: "hexpm", optional: false]}, {:thoas, "1.0.0", [hex: :thoas, repo: "hexpm", optional: false]}], "hexpm", "26a400f76976e66efd9cdab29a36dd4b129466d431c4e014aae9d2e36fefef44"}, @@ -74,6 +78,9 @@ "redix": {:hex, :redix, "1.4.1", "8303e13bad38ca80c15bdf79ea9cbd6eb879554c9cbb815b35df1602d7b1549d", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:nimble_options, "~> 0.5.0 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "676b5ce37d7b1d46931d506e3208786bd8334a1625ecb591d87d790b23ffbd1f"}, "redlock": {:hex, :redlock, "1.0.21", "7c6b0eaa8470fb6fea24d565fd116ac98a6a0e309fda5366607bceef6733cdbe", [:mix], [{:ex_hash_ring, "~> 3.0", [hex: :ex_hash_ring, repo: "hexpm", optional: false]}, {:fastglobal, "~> 1.0.0", [hex: :fastglobal, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:redix, "~> 1.3", [hex: :redix, repo: "hexpm", optional: false]}], "hexpm", "7e72e5b13148a3a0f5565513992543c01bdf20d082385cb91a11ea3b3e8c9cdd"}, "reverse_proxy_plug": {:hex, :reverse_proxy_plug, "3.0.2", "38fde2f59bca8b219ef4f1ec0c0849a67c6d9705160e426a2354f35399db5c7b", [:mix], [{:finch, "~> 0.18", [hex: :finch, repo: "hexpm", optional: true]}, {:httpoison, "~> 1.2 or ~> 2.0", [hex: :httpoison, repo: "hexpm", optional: true]}, {:plug, "~> 1.6", [hex: :plug, repo: "hexpm", optional: false]}, {:req, "~> 0.3.0 or ~> 0.4.0 or ~> 0.5.0", [hex: :req, repo: "hexpm", optional: true]}, {:tesla, "~> 1.4", [hex: :tesla, repo: "hexpm", optional: true]}], "hexpm", "31ae5e068f7f504fba1b5c17c31c87966c720809ac15140c6c181440fbd24eda"}, + "riak": {:git, "https://github.com/EdmondFrank/riak-elixir-client", "8f75b0265b2afd44348393ceb135bfce31a8d5d4", []}, + "riak_pb": {:hex, :riak_pb, "2.3.2", "48ffbf66dbb3f136ab9a7134bac4e496754baa5ef58c4f50a61326736d996390", [:make, :mix, :rebar3], [{:hamcrest, "~> 0.4.1", [hex: :basho_hamcrest, repo: "hexpm", optional: false]}], "hexpm", "7c0644bcba8a423dd9c1ab85b6e7ff4df0712d8a350f9698647cff1996f0348c"}, + "riakc": {:git, "https://github.com/kivra/riak-erlang-client", "675098b50052508ab7be09198b023a2648c061ee", []}, "sitemapper": {:hex, :sitemapper, "0.7.0", "4aee7930327a9a01b1c9b81d1d42f60c1a295e9f420108eb2d130c317415abd7", [:mix], [{:ex_aws_s3, "~> 2.0", [hex: :ex_aws_s3, repo: "hexpm", optional: true]}, {:xml_builder, "~> 2.1", [hex: :xml_builder, repo: "hexpm", optional: false]}], "hexpm", "60f7a684e5e9fe7f10ac5b69f48b0be2bcbba995afafcb3c143fc0c8ef1f223f"}, "snap": {:hex, :snap, "0.8.1", "d95a7ecbc911a5a12f419c7108ce9e44db161b9177d137f14d22a8c1fe85cf4e", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:finch, "~> 0.8", [hex: :finch, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "929e2d3254697c85c0226cabe6b40eda6703cfece4fe8285c4b82f3e66ee6291"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},