Skip to content

Commit

Permalink
chore: add specs, minor refactoring of Logflare.Cluster.Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziinc committed Nov 14, 2023
1 parent e0cdd5f commit 5b00081
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/logflare/cluster/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@ defmodule Logflare.Cluster.Utils do
@moduledoc false
require Logger

defp env_min_cluster_size, do: Application.get_env(:logflare, __MODULE__)[:min_cluster_size]

@spec node_list_all() :: [Node.t()]
def node_list_all() do
[Node.self() | Node.list()]
end

@spec cluster_size() :: non_neg_integer()
def cluster_size() do
lib_cluster_size = node_list_all() |> Enum.count()
lib_cluster_size = actual_cluster_size()
min_size = env_min_cluster_size()

if lib_cluster_size >= env_min_cluster_size() do
if lib_cluster_size >= min_size do
lib_cluster_size
else
Logger.warning("Cluster size is #{lib_cluster_size} but expected #{env_min_cluster_size()}",
Logger.warning("Cluster size is #{lib_cluster_size} but expected #{min_size}",
cluster_size: lib_cluster_size
)

env_min_cluster_size()
min_size
end
end

@spec actual_cluster_size() :: non_neg_integer()
def actual_cluster_size() do
Enum.count(node_list_all())
end

defp env_min_cluster_size, do: Application.get_env(:logflare, __MODULE__)[:min_cluster_size]
end
28 changes: 28 additions & 0 deletions test/logflare/cluster_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule Logflare.ClusterTest do
use ExUnit.Case, async: false
alias Logflare.Cluster

test "Cluster.Utils.node_list_all/0" do
assert [_|_] = Cluster.Utils.node_list_all()
end


test "cluster_size/0, actual_cluster_size/0" do
assert Cluster.Utils.cluster_size() == Cluster.Utils.actual_cluster_size()
end

describe "Cluster.Utils" do
setup do
config = Application.get_env(:logflare, Cluster.Utils)
Application.put_env(:logflare, Cluster.Utils, [min_cluster_size: 12])
on_exit(fn ->
Application.put_env(:logflare, Cluster.Utils, config)
end)
end
test "cluster_size/0, actual_cluster_size/0" do
assert Cluster.Utils.cluster_size() == 12
assert Cluster.Utils.actual_cluster_size() == 1
end

end
end

0 comments on commit 5b00081

Please sign in to comment.