Skip to content

Commit 02322a1

Browse files
Merge pull request #5144 from hashicorp/backport/mgaffney-fix-orphan-conn-query/amazingly-neat-raptor
This pull request was automerged via backport-assistant
2 parents bfe1014 + e0ccd73 commit 02322a1

File tree

2 files changed

+12
-32
lines changed

2 files changed

+12
-32
lines changed

internal/session/query.go

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -329,38 +329,17 @@ where
329329
and closed_reason is null
330330
returning public_id;
331331
`
332-
orphanedConnectionsCte = `
333-
-- Find connections that are not closed so we can reference those IDs
334-
with
335-
unclosed_connections as (
336-
select public_id
337-
from session_connection
338-
where
339-
-- It's not closed
340-
upper(connected_time_range) > now() or
341-
connected_time_range is null
342-
-- It's not in limbo between when it moved into this state and when
343-
-- it started being reported by the worker, which is roughly every
344-
-- 2-3 seconds
345-
and update_time < wt_sub_seconds_from_now(@worker_state_delay_seconds)
346-
),
347-
connections_to_close as (
348-
select public_id
349-
from session_connection
350-
where
351-
-- Related to the worker that just reported to us
352-
worker_id = @worker_id
353-
-- Only unclosed ones
354-
and public_id in (select public_id from unclosed_connections)
355-
-- These are connection IDs that just got reported to us by the given
356-
-- worker, so they should not be considered closed.
357-
%s
358-
)
332+
closeOrphanedConnections = `
359333
update session_connection
360-
set
361-
closed_reason = 'system error'
362-
where
363-
public_id in (select public_id from connections_to_close)
334+
set closed_reason = 'system error'
335+
where worker_id = @worker_id
336+
and update_time < wt_sub_seconds_from_now(@worker_state_delay_seconds)
337+
and (
338+
connected_time_range is null
339+
or
340+
upper(connected_time_range) > now()
341+
)
342+
%s
364343
returning public_id;
365344
`
366345
deleteTerminated = `

internal/session/repository_connection.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,12 +399,13 @@ func (r *ConnectionRepository) closeOrphanedConnections(ctx context.Context, wor
399399
notInClause = fmt.Sprintf(notInClause, strings.Join(params, ","))
400400
}
401401

402+
query := fmt.Sprintf(closeOrphanedConnections, notInClause)
402403
_, err := r.writer.DoTx(
403404
ctx,
404405
db.StdRetryCnt,
405406
db.ExpBackoff{},
406407
func(_ db.Reader, w db.Writer) error {
407-
rows, err := w.Query(ctx, fmt.Sprintf(orphanedConnectionsCte, notInClause), args)
408+
rows, err := w.Query(ctx, query, args)
408409
if err != nil {
409410
return errors.Wrap(ctx, err, op)
410411
}

0 commit comments

Comments
 (0)