Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add rebalance code as an helper #641

Merged
merged 3 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/realtime/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,36 @@ defmodule Realtime.Helpers do
end)
end

def rebalance() do
Enum.reduce(:syn.group_names(:users), 0, fn tenant, acc ->
case :syn.lookup(Extensions.PostgresCdcRls, tenant) do
{pid, %{region: region}} ->
region = Realtime.PostgresCdc.aws_to_fly(region)
launch_node = Realtime.PostgresCdc.launch_node(tenant, region, false)

if launch_node && launch_node != node(pid) do
try do
Extensions.PostgresCdcRls.handle_stop(tenant, 5_000)
# credo:disable-for-next-line
IO.inspect({"Stopped", tenant, region})
catch
kind, reason ->
# credo:disable-for-next-line
IO.inspect({"Failed to stop", tenant, kind, reason})
end

Process.sleep(1_500)
acc + 1
else
acc
end

_ ->
acc
end
end)
end

defp pad(data) do
to_add = 16 - rem(byte_size(data), 16)
data <> :binary.copy(<<to_add>>, to_add)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Realtime.MixProject do
def project do
[
app: :realtime,
version: "2.20.2",
version: "2.20.3",
elixir: "~> 1.14.0",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down
Loading