From f608610f0ac457a907a13955e68a946565e4010a Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Tue, 17 Dec 2024 15:15:41 +0200 Subject: [PATCH] Switch libcluster strategry from k8s to postgres --- config/config.exs | 17 +++++++++++------ config/dev.exs | 4 +++- config/runtime.exs | 18 ++++++++++++++++++ lib/sanbase/application/admin.ex | 14 +++++++++++++- lib/sanbase/application/web.ex | 2 +- mix.exs | 1 + mix.lock | 1 + 7 files changed, 48 insertions(+), 9 deletions(-) diff --git a/config/config.exs b/config/config.exs index d46fc9db29..4ae689bdf4 100644 --- a/config/config.exs +++ b/config/config.exs @@ -245,13 +245,18 @@ config :sanbase, Sanbase.MetricExporter.S3, bucket: {:system, "METRICS_EXPORTER_ config :libcluster, topologies: [ - k8s: [ - strategy: Elixir.Cluster.Strategy.Kubernetes, + postgres_topology: [ + strategy: LibclusterPostgres.Strategy, config: [ - mode: :dns, - kubernetes_node_basename: "sanbase", - kubernetes_selector: "app=sanbase", - polling_interval: 10_000 + hostname: "localhost", + username: "postgres", + password: "postgres", + database: "sanbase_dev", + port: 5432, + parameters: [], + ssl: false, + ssl_opts: nil, + channel_name: "sanbase_cluster" ] ] ] diff --git a/config/dev.exs b/config/dev.exs index 8297eee630..27025c0a7c 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -9,9 +9,11 @@ import Config config :phoenix_live_view, debug_heex_annotations: true config :sanbase, Sanbase, url: {:system, "SANBASE_URL", "https://app-stage.santiment.net"} +port = String.to_integer(System.get_env("PORT") || "4000") + config :sanbase, SanbaseWeb.Endpoint, http: [ - port: 4000, + port: port, protocol_options: [ # Bump up cowboy2's timeout to 100 seconds idle_timeout: 100_000 diff --git a/config/runtime.exs b/config/runtime.exs index 9e49c31589..710053f647 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -95,6 +95,24 @@ if config_env() == :prod do ssl: true, ssl_opts: [verify: :verify_none] + config :libcluster, + topologies: [ + postgres_topology: [ + strategy: LibclusterPostgres.Strategy, + config: [ + hostname: "localhost", + username: "postgres", + password: "postgres", + database: "santiment", + port: 5432, + parameters: [], + ssl: true, + ssl_opts: [verify: :verify_none], + channel_name: "sanbase_cluster" + ] + ] + ] + config :ethereumex, url: parity_url, http_options: [timeout: 25_000, recv_timeout: 25_000], diff --git a/lib/sanbase/application/admin.ex b/lib/sanbase/application/admin.ex index 5c4fd6f234..73d7f732e4 100644 --- a/lib/sanbase/application/admin.ex +++ b/lib/sanbase/application/admin.ex @@ -4,6 +4,7 @@ defmodule Sanbase.Application.Admin do A separate pod is required so the access to it can be better secured compared to when the admin dashboard was part of the web pod """ + import Sanbase.ApplicationUtils require Logger def init() do @@ -18,7 +19,18 @@ defmodule Sanbase.Application.Admin do def children() do # Define workers and child supervisors to be supervised children = [ - {Oban, oban_admin_config()} + {Oban, oban_admin_config()}, + + # Start the libcluster in admin, so we can send messages to the web pods when some + # important tables changes. + start_in( + {Cluster.Supervisor, + [ + Application.get_env(:libcluster, :topologies), + [name: Sanbase.ClusterSupervisor] + ]}, + [:dev, :prod] + ) ] opts = [ diff --git a/lib/sanbase/application/web.ex b/lib/sanbase/application/web.ex index 9ebb162076..f2e969e67a 100644 --- a/lib/sanbase/application/web.ex +++ b/lib/sanbase/application/web.ex @@ -45,7 +45,7 @@ defmodule Sanbase.Application.Web do Application.get_env(:libcluster, :topologies), [name: Sanbase.ClusterSupervisor] ]}, - [:prod] + [:dev, :prod] ) ] diff --git a/mix.exs b/mix.exs index db3eea9b69..3a15155e17 100644 --- a/mix.exs +++ b/mix.exs @@ -101,6 +101,7 @@ defmodule Sanbase.Mixfile do {:kino_db, "~> 0.2.2"}, {:kino_vega_lite, "~> 0.1.9"}, {:libcluster, "~> 3.0"}, + {:libcluster_postgres, "~> 0.1"}, {:map_diff, "~> 1.3"}, {:mint, "~> 1.0"}, {:mock, "~> 0.3"}, diff --git a/mix.lock b/mix.lock index c89d597006..b49d5ee4df 100644 --- a/mix.lock +++ b/mix.lock @@ -80,6 +80,7 @@ "kino_db": {:hex, :kino_db, "0.2.13", "2b7d5b71aa4fc1ebc6d415df62170b6807c6ff84d621f4fdfc226569cc49b399", [:mix], [{:adbc, "~> 0.2", [hex: :adbc, repo: "hexpm", optional: true]}, {:db_connection, "~> 2.4.2 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: true]}, {:explorer, "~> 0.8", [hex: :explorer, repo: "hexpm", optional: true]}, {:exqlite, "~> 0.11", [hex: :exqlite, repo: "hexpm", optional: true]}, {:kino, "~> 0.13", [hex: :kino, repo: "hexpm", optional: false]}, {:myxql, "~> 0.7", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.18 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:req_athena, "~> 0.2.0", [hex: :req_athena, repo: "hexpm", optional: true]}, {:req_bigquery, "~> 0.1.0", [hex: :req_bigquery, repo: "hexpm", optional: true]}, {:table, "~> 0.1", [hex: :table, repo: "hexpm", optional: false]}, {:tds, "~> 2.3.4 or ~> 2.4", [hex: :tds, repo: "hexpm", optional: true]}], "hexpm", "0dcdba30c99ce367f1026477fc6374b94dc1dc10126b33cc9129e02c7f1e1538"}, "kino_vega_lite": {:hex, :kino_vega_lite, "0.1.13", "03c00405987a2202e4b8014ee55eb7f5727691b3f13d76a3764f6eeccef45322", [:mix], [{:kino, "~> 0.7", [hex: :kino, repo: "hexpm", optional: false]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: false]}, {:vega_lite, "~> 0.1.8", [hex: :vega_lite, repo: "hexpm", optional: false]}], "hexpm", "00c72bc270e7b9d3c339f726cdab0012fd3f2fc75e36c7548e0f250fe420fa10"}, "libcluster": {:hex, :libcluster, "3.4.1", "271d2da892763bbef53c2872036c936fe8b80111eb1feefb2d30a3bb15c9b4f6", [:mix], [{:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.3", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "1d568157f069c6afa70ec0d736704cf799734bdbb6343f0322af4a980301c853"}, + "libcluster_postgres": {:hex, :libcluster_postgres, "0.1.2", "51380bb6b3a6e11b2614202c63169b464cf4867814da52cc3aa82dfc5ae22248", [:mix], [{:libcluster, "~> 3.3", [hex: :libcluster, repo: "hexpm", optional: false]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, repo: "hexpm", optional: false]}], "hexpm", "b957ca609ec5dcddc37342a756bfc027029225bfb089bf0b32974f0ec0cac237"}, "map_diff": {:hex, :map_diff, "1.3.4", "4fa013ad4fff7b21694f3aa5890dad5a0679f11812fdd89a0ad06276a431faf8", [:mix], [], "hexpm", "32fc0b8fc158683a00a58298440b8cb884e7e779f9459e598df61d022b5412e9"}, "meck": {:hex, :meck, "0.9.2", "85ccbab053f1db86c7ca240e9fc718170ee5bda03810a6292b5306bf31bae5f5", [:rebar3], [], "hexpm", "81344f561357dc40a8344afa53767c32669153355b626ea9fcbc8da6b3045826"}, "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},