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

refact(session): Replace state start_time/end_time with time range #5134

Merged
merged 1 commit into from
Sep 30, 2024

Conversation

tmessi
Copy link
Member

@tmessi tmessi commented Sep 25, 2024

This replaces the start_time, end_time, and previous_end_time columns on
the session_state table with a time range column that records when the
state was active. This allowed for dropping several constraints on the
session_state table that were used to ensure that states for a session
did not overlap. Instead this can be ensured with a single exclusion
constraint. By reducing the number of constraints, it improves the
performance for a number of queries involved with session state. Most
notably when resolving delete cascades of resources related to sessions,
such as targets, where as part of the delete transaction large numbers
of sessions get canceled. Similarly, when sessions get deleted, and the
corresponding session_state rows are deleted, these constraints all must
be checked during the transaction.

The domain layer continues to expose StartTime and EndTime fields on
session.State. However it drops PreviousEndTime, which was never used by
anything calling the domain layer.

See:
https://www.postgresql.org/docs/current/ddl-constraints.html#DDL-CONSTRAINTS-EXCLUSION
https://www.postgresql.org/docs/current/rangetypes.html

@tmessi tmessi added this to the 0.18.x milestone Sep 25, 2024
@tmessi tmessi force-pushed the tmessi-session-state-timerange branch from 4cc469a to 2a3f3df Compare September 25, 2024 20:24
@tmessi tmessi marked this pull request as ready for review September 25, 2024 20:25

This comment has been minimized.

@tmessi tmessi marked this pull request as draft September 25, 2024 20:38
@tmessi tmessi force-pushed the tmessi-session-state-timerange branch from 2a3f3df to 6a3b601 Compare September 25, 2024 21:17
Copy link
Collaborator

@louisruch louisruch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clear doc strings in the sql file

@louisruch
Copy link
Collaborator

Oh I just saw this was still in draft :/

@tmessi tmessi marked this pull request as ready for review September 26, 2024 13:25

This comment has been minimized.

This replaces the start_time, end_time, and previous_end_time columns on
the session_state table with a time range column that records when the
state was active. This allowed for dropping several constraints on the
session_state table that were used to ensure that states for a session
did not overlap. Instead this can be ensured with a single exclusion
constraint. By reducing the number of constraints, it improves the
performance for a number of queries involved with session state. Most
notably when resolving delete cascades of resources related to sessions,
such as targets, where as part of the delete transaction large numbers
of sessions get canceled. Similarly, when sessions get deleted, and the
corresponding session_state rows are deleted, these constraints all must
be checked during the transaction.

The domain layer continues to expose StartTime and EndTime fields on
session.State. However it drops PreviousEndTime, which was never used by
anything calling the domain layer.

See:
    https://www.postgresql.org/docs/current/ddl-constraints.html#DDL-CONSTRAINTS-EXCLUSION
    https://www.postgresql.org/docs/current/rangetypes.html
@tmessi tmessi force-pushed the tmessi-session-state-timerange branch from 6a3b601 to 142b80f Compare September 30, 2024 17:34
Copy link

Database schema diff between main and tmessi-session-state-timerange @ 142b80f

To understand how these diffs are generated and some limitations see the
documentation of the script.

Functions

diff --git a/.schema-diff/funcs_f6bdbd116ef27a04448a888b0214e21a1a5729d8/insert_session_state.sql b/.schema-diff/funcs_824c48e4766fc542d940d259cef5376d0dd47137/insert_session_state.sql
index 8b6ffceee..c0d324fe1 100644
--- a/.schema-diff/funcs_f6bdbd116ef27a04448a888b0214e21a1a5729d8/insert_session_state.sql
+++ b/.schema-diff/funcs_824c48e4766fc542d940d259cef5376d0dd47137/insert_session_state.sql
@@ -23,31 +23,27 @@ set row_security = off;
 create function public.insert_session_state() returns trigger
     language plpgsql
     as $$
-declare
-    old_col_state text;
-begin
-    update session_state
-    set end_time = now()
-    where (session_id = new.session_id
-        and end_time is null) returning state into old_col_state;
-    new.prior_state= old_col_state;
+  declare
+      old_col_state text;
+  begin
+       update session_state
+          set active_time_range = tstzrange(lower(active_time_range), now(), '[)')
+        where session_id = new.session_id
+          and upper(active_time_range) is null
+    returning state
+         into old_col_state;
 
     if not found then
-        new.previous_end_time = null;
-        new.start_time = now();
-        new.end_time = null;
-        new.prior_state='pending';
-        return new;
+      new.prior_state = 'pending';
+    else
+      new.prior_state = old_col_state;
     end if;
 
-    new.previous_end_time = now();
-    new.start_time = now();
-    new.end_time = null;
+    new.active_time_range = tstzrange(now(), null, '[]');
 
     return new;
-
-end;
-$$;
+  end;
+  $$;
 
 
 --
diff --git a/.schema-diff/funcs_f6bdbd116ef27a04448a888b0214e21a1a5729d8/wh_insert_session.sql b/.schema-diff/funcs_824c48e4766fc542d940d259cef5376d0dd47137/wh_insert_session.sql
index 4c50e1bd3..433a3c5e6 100644
--- a/.schema-diff/funcs_f6bdbd116ef27a04448a888b0214e21a1a5729d8/wh_insert_session.sql
+++ b/.schema-diff/funcs_824c48e4766fc542d940d259cef5376d0dd47137/wh_insert_session.sql
@@ -28,7 +28,7 @@ create function public.wh_insert_session() returns trigger
   begin
     with
     pending_timestamp (date_dim_key, time_dim_key, ts) as (
-      select wh_date_key(start_time), wh_time_key(start_time), start_time
+      select wh_date_key(lower(active_time_range)), wh_time_key(lower(active_time_range)), lower(active_time_range)
         from session_state
        where session_id = new.public_id
          and state      = 'pending'
diff --git a/.schema-diff/funcs_f6bdbd116ef27a04448a888b0214e21a1a5729d8/wh_insert_session_state.sql b/.schema-diff/funcs_824c48e4766fc542d940d259cef5376d0dd47137/wh_insert_session_state.sql
index e85f643e8..6f79e85d9 100644
--- a/.schema-diff/funcs_f6bdbd116ef27a04448a888b0214e21a1a5729d8/wh_insert_session_state.sql
+++ b/.schema-diff/funcs_824c48e4766fc542d940d259cef5376d0dd47137/wh_insert_session_state.sql
@@ -42,12 +42,12 @@ create function public.wh_insert_session_state() returns trigger
     time_col = 'session_' || new.state || '_time_key';
     ts_col   = 'session_' || new.state || '_time';
 
-    q = format('update wh_session_accumulating_fact
-                   set (%i, %i, %i) = (select wh_date_key(%l), wh_time_key(%l), %l::timestamptz)
-                 where session_id = %l
+    q = format('   update wh_session_accumulating_fact
+                      set (%i, %i, %i) = (select wh_date_key(%l), wh_time_key(%l), %l::timestamptz)
+                    where session_id = %l
                 returning *',
                 date_col,       time_col,       ts_col,
-                new.start_time, new.start_time, new.start_time,
+                lower(new.active_time_range), lower(new.active_time_range), lower(new.active_time_range),
                 new.session_id);
     execute q into strict session_row;
 

Tables

diff --git a/.schema-diff/tables_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_state.sql b/.schema-diff/tables_824c48e4766fc542d940d259cef5376d0dd47137/session_state.sql
index 805915641..ebd51f9dd 100644
--- a/.schema-diff/tables_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_state.sql
+++ b/.schema-diff/tables_824c48e4766fc542d940d259cef5376d0dd47137/session_state.sql
@@ -27,13 +27,9 @@ set default_table_access_method = heap;
 create table public.session_state (
     session_id public.wt_public_id not null,
     state text not null,
-    previous_end_time timestamp with time zone,
-    start_time timestamp with time zone default current_timestamp not null,
-    end_time timestamp with time zone,
     prior_state text default 'pending'::text not null,
-    constraint end_times_in_sequence check ((previous_end_time <> end_time)),
-    constraint previous_end_time_and_start_time_in_sequence check ((previous_end_time <= start_time)),
-    constraint start_and_end_times_in_sequence check ((start_time <= end_time))
+    active_time_range tstzrange default tstzrange(now(), null::timestamp with time zone, '[]'::text) not null,
+    constraint active_time_range_not_empty check ((not isempty(active_time_range)))
 );
 
 

Views

diff --git a/.schema-diff/views_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_list.sql b/.schema-diff/views_824c48e4766fc542d940d259cef5376d0dd47137/session_list.sql
index c8598d6b3..eb646f59e 100644
--- a/.schema-diff/views_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_list.sql
+++ b/.schema-diff/views_824c48e4766fc542d940d259cef5376d0dd47137/session_list.sql
@@ -24,33 +24,24 @@ create view public.session_list as
  select s.public_id,
     s.user_id,
     shsh.host_id,
-    s.target_id,
     shsh.host_set_id,
+    s.target_id,
     s.auth_token_id,
     s.project_id,
     s.certificate,
-    s.certificate_private_key,
     s.expiration_time,
-    s.connection_limit,
-    s.tofu_token,
-    s.key_id,
     s.termination_reason,
-    s.version,
     s.create_time,
     s.update_time,
+    s.version,
     s.endpoint,
-    s.worker_filter,
-    s.egress_worker_filter,
-    s.ingress_worker_filter,
-    swp.worker_id,
+    s.connection_limit,
     ss.state,
-    ss.previous_end_time,
-    ss.start_time,
-    ss.end_time
-   from (((public.session s
+    lower(ss.active_time_range) as start_time,
+    upper(ss.active_time_range) as end_time
+   from ((public.session s
      join public.session_state ss on (((s.public_id)::text = (ss.session_id)::text)))
-     left join public.session_host_set_host shsh on (((s.public_id)::text = (shsh.session_id)::text)))
-     left join public.session_worker_protocol swp on (((s.public_id)::text = (swp.session_id)::text)));
+     left join public.session_host_set_host shsh on (((s.public_id)::text = (shsh.session_id)::text)));
 
 
 --

Triggers

diff --git a/.schema-diff/triggers_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_state immutable_columns.sql b/.schema-diff/triggers_824c48e4766fc542d940d259cef5376d0dd47137/session_state immutable_columns.sql
index 127d8652b..8a3567626 100644
--- a/.schema-diff/triggers_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_state immutable_columns.sql	
+++ b/.schema-diff/triggers_824c48e4766fc542d940d259cef5376d0dd47137/session_state immutable_columns.sql	
@@ -20,7 +20,7 @@ set row_security = off;
 -- name: session_state immutable_columns; type: trigger; schema: public; owner: -
 --
 
-create trigger immutable_columns before update on public.session_state for each row execute function public.immutable_columns('session_id', 'state', 'start_time', 'previous_end_time');
+create trigger immutable_columns before update on public.session_state for each row execute function public.immutable_columns('session_id', 'state');
 
 
 --

Indexes

diff --git a/.schema-diff/indexes_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_state_state_terminated_start_time_ix.sql b/.schema-diff/indexes_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_state_state_terminated_start_time_ix.sql
deleted file mode 100644
index bce87e854..000000000
--- a/.schema-diff/indexes_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_state_state_terminated_start_time_ix.sql
+++ /dev/null
@@ -1,31 +0,0 @@
---
--- postgresql database dump
---
-
--- dumped from database version 13.16
--- dumped by pg_dump version 16.4 (ubuntu 16.4-1.pgdg24.04+1)
-
-set statement_timeout = 0;
-set lock_timeout = 0;
-set idle_in_transaction_session_timeout = 0;
-set client_encoding = 'utf8';
-set standard_conforming_strings = on;
-select pg_catalog.set_config('search_path', '', false);
-set check_function_bodies = false;
-set xmloption = content;
-set client_min_messages = warning;
-set row_security = off;
-
-set default_tablespace = '';
-
---
--- name: session_state_state_terminated_start_time_ix; type: index; schema: public; owner: -
---
-
-create index session_state_state_terminated_start_time_ix on public.session_state using btree (state, start_time) where (state = 'terminated'::text);
-
-
---
--- postgresql database dump complete
---
-

Constraints

diff --git a/.schema-diff/constraints_824c48e4766fc542d940d259cef5376d0dd47137/session_state_active_time_range_excl.sql b/.schema-diff/constraints_824c48e4766fc542d940d259cef5376d0dd47137/session_state_active_time_range_excl.sql
new file mode 100644
index 000000000..db427aa07
--- /dev/null
+++ b/.schema-diff/constraints_824c48e4766fc542d940d259cef5376d0dd47137/session_state_active_time_range_excl.sql
@@ -0,0 +1,2 @@
+-- name: session_state session_state_active_time_range_excl; type: constraint; schema: public; owner: -
+    add constraint session_state_active_time_range_excl exclude using gist (session_id with =, active_time_range with &&);
diff --git a/.schema-diff/constraints_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_state_end_time_session_id_uq.sql b/.schema-diff/constraints_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_state_end_time_session_id_uq.sql
deleted file mode 100644
index 4f4b4e433..000000000
--- a/.schema-diff/constraints_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_state_end_time_session_id_uq.sql
+++ /dev/null
@@ -1,2 +0,0 @@
--- name: session_state session_state_end_time_session_id_uq; type: constraint; schema: public; owner: -
-    add constraint session_state_end_time_session_id_uq unique (session_id, end_time);
diff --git a/.schema-diff/constraints_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_state_pkey.sql b/.schema-diff/constraints_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_state_pkey.sql
deleted file mode 100644
index 60661d9b7..000000000
--- a/.schema-diff/constraints_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_state_pkey.sql
+++ /dev/null
@@ -1,2 +0,0 @@
--- name: session_state session_state_pkey; type: constraint; schema: public; owner: -
-    add constraint session_state_pkey primary key (session_id, start_time);
diff --git a/.schema-diff/constraints_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_state_previous_end_time_session_id_uq.sql b/.schema-diff/constraints_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_state_previous_end_time_session_id_uq.sql
deleted file mode 100644
index 900d3107a..000000000
--- a/.schema-diff/constraints_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_state_previous_end_time_session_id_uq.sql
+++ /dev/null
@@ -1,2 +0,0 @@
--- name: session_state session_state_previous_end_time_session_id_uq; type: constraint; schema: public; owner: -
-    add constraint session_state_previous_end_time_session_id_uq unique (session_id, previous_end_time);

Foreign Key Constraints

diff --git a/.schema-diff/fk_constraints_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_state_session_id_previous_end_time_fkey.sql b/.schema-diff/fk_constraints_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_state_session_id_previous_end_time_fkey.sql
deleted file mode 100644
index 0f3c25a98..000000000
--- a/.schema-diff/fk_constraints_f6bdbd116ef27a04448a888b0214e21a1a5729d8/session_state_session_id_previous_end_time_fkey.sql
+++ /dev/null
@@ -1,2 +0,0 @@
--- name: session_state session_state_session_id_previous_end_time_fkey; type: fk constraint; schema: public; owner: -
-    add constraint session_state_session_id_previous_end_time_fkey foreign key (session_id, previous_end_time) references public.session_state(session_id, end_time);

@tmessi tmessi merged commit 37bf592 into main Sep 30, 2024
63 checks passed
@tmessi tmessi deleted the tmessi-session-state-timerange branch September 30, 2024 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants