Skip to content

Commit

Permalink
Using queried tenants from control plane
Browse files Browse the repository at this point in the history
  • Loading branch information
icehaunter committed Nov 12, 2024
1 parent 9683683 commit 04802ce
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 149 deletions.
7 changes: 4 additions & 3 deletions packages/sync-service/config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ case {database_url, default_tenant} do

config :electric, default_connection_opts: Electric.Utils.obfuscate_password(connection_opts)

# if `default_tenant` is nil, generate a random UUID for it
tenant_id = default_tenant || Electric.Utils.uuid4()
tenant_id = default_tenant || "00000000-0000-0000-0000-000000000000"
config :electric, default_tenant: tenant_id
end

Expand Down Expand Up @@ -209,4 +208,6 @@ config :electric,
prometheus_port: prometheus_port,
storage: storage,
persistent_kv: persistent_kv,
listen_on_ipv6?: env!("ELECTRIC_LISTEN_ON_IPV6", :boolean, false)
control_plane: env!("ELECTRIC_CONTROL_PLANE", &Electric.ControlPlane.parse_config/1, nil),
listen_on_ipv6?: env!("ELECTRIC_LISTEN_ON_IPV6", :boolean, false),
tenant_tables_name: :tenant_tables
3 changes: 2 additions & 1 deletion packages/sync-service/lib/electric/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ defmodule Electric.Application do
},
pool_opts: %{
size: Application.fetch_env!(:electric, :db_pool_size)
}
},
control_plane: Application.get_env(:electric, :control_plane, nil)
}

Electric.Application.Configuration.save(config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Electric.Application.Configuration do
persistent_kv
replication_opts
pool_opts
control_plane
]a

@type t :: %__MODULE__{}
Expand Down
16 changes: 13 additions & 3 deletions packages/sync-service/lib/electric/control_plane.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ defmodule Electric.ControlPlane do
paths: %{optional(String.t()) => map()}
}

def parse_config(""), do: nil
def parse_config(config_string) do
result = Jason.decode!(config_string)

%__MODULE__{
base_url: Map.fetch!(result, "base_url"),
auth: Map.get(result, "auth", nil),
paths: Map.get(result, "paths", %__MODULE__{}.paths),
}
end

@spec list_tenants(t(), keyword()) ::
{:ok, included :: list(map()), deleted :: list(map())} | {:error, :unreachable}
def list_tenants(%__MODULE__{} = plane, opts) do
Expand Down Expand Up @@ -106,10 +117,9 @@ defmodule Electric.ControlPlane do
end

defp insert_instance_id(string, instance_id),
do: String.replace(string, "%{instance_id}", instance_id)
do: String.replace(string, "%{instance_id}", to_string(instance_id))

@spec collect_ops(list({String.t(), String.t(), %{String.t() => String.t()}})) ::
{map(), map()}
@spec collect_ops(Enumerable.t()) :: {map(), map()}
defp collect_ops(ops) do
Enum.reduce(ops, {%{}, %{}}, fn
{"insert", key, value}, {ins_acc, del_acc} ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule Electric.Plug.RemoveDatabasePlug do
|> send_resp(200, Jason.encode_to_iodata!(tenant_id))
|> halt()

:not_found ->
{:error, :not_found} ->
conn
|> send_resp(404, Jason.encode_to_iodata!("Database #{tenant_id} not found."))
|> halt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Electric.TenantSupervisor do

def start_link(opts) do
DynamicSupervisor.start_link(__MODULE__, [], name: name(opts))

end

def start_tenant(opts) do
Expand Down
Loading

0 comments on commit 04802ce

Please sign in to comment.