Skip to content

Commit

Permalink
fix: Improve logs on create partitioning code (#1223)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipecabaco authored Nov 14, 2024
1 parent 9b1cfe1 commit 1614a15
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ This is the list of operational codes that can help you understand your deployme
| UnableToConnectToTenantDatabase | Realtime was not able to connect to the tenant's database |
| RealtimeNodeDisconnected | Realtime is a distributed application and this means that one the system is unable to communicate with one of the distributed nodes |
| MigrationsFailedToRun | Error when running the migrations against the Tenant database that are required by Realtime |
| PartitionCreationFailed | Error when creating partitions for realtime.messages |
| ErrorStartingPostgresCDCStream | Error when starting the Postgres CDC stream which is used for Postgres Changes |
| UnknownDataProcessed | An unknown data type was processed by the Realtime system |
| ErrorStartingPostgresCDC | Error when starting the Postgres CDC extension which is used for Postgres Changes |
Expand Down
7 changes: 6 additions & 1 deletion lib/realtime/tenants/connect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,12 @@ defmodule Realtime.Tenants.Connect do
else
error ->
log_error("MigrationsFailedToRun", error)
{:stop, :shutdown}
{:stop, :shutdown, state}
end
rescue
error ->
log_error("MigrationsFailedToRun", error)
{:stop, :shutdown, state}
end

@impl true
Expand Down Expand Up @@ -284,6 +288,7 @@ defmodule Realtime.Tenants.Connect do
defp start_replication(%{notify_private_alpha: false}), do: {:ok, nil}

defp start_replication(tenant) do
Logger.info("Starting replication for Broadcast Changes")
opts = %Handler{tenant_id: tenant.external_id}
supervisor_spec = Handler.supervisor_spec(tenant)

Expand Down
21 changes: 12 additions & 9 deletions lib/realtime/tenants/migrations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ defmodule Realtime.Tenants.Migrations do
"""
@spec create_partitions(pid()) :: :ok
def create_partitions(db_conn_pid) do
Logger.info("Creating partitions for realtime.messages")
today = Date.utc_today()
yesterday = Date.add(today, -1)
tomorrow = Date.add(today, 1)
Expand All @@ -236,15 +237,17 @@ defmodule Realtime.Tenants.Migrations do
end_timestamp = Date.to_string(Date.add(date, 1))

Database.transaction(db_conn_pid, fn conn ->
Postgrex.query(
conn,
"""
CREATE TABLE IF NOT EXISTS realtime.#{partition_name}
PARTITION OF realtime.messages
FOR VALUES FROM ('#{start_timestamp}') TO ('#{end_timestamp}');
""",
[]
)
query = """
CREATE TABLE IF NOT EXISTS realtime.#{partition_name}
PARTITION OF realtime.messages
FOR VALUES FROM ('#{start_timestamp}') TO ('#{end_timestamp}');
"""

case Postgrex.query(conn, query, []) do
{:ok, _} -> Logger.info("Partition #{partition_name} created")
{:error, %Postgrex.Error{postgres: %{code: :duplicate_table}}} -> :ok
{:error, error} -> log_error("PartitionCreationFailed", error)
end
end)
end)

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.33.45",
version: "2.33.46",
elixir: "~> 1.16.0",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down

0 comments on commit 1614a15

Please sign in to comment.