Add cleanup of dead connections no longer reported by a worker (#1220)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2021-05-14 13:23:12 -04:00
1 parent 0b5ac930ac
commit 6f34da8923
24 files changed
+741 -223

No files matched your search

@@ -89,6 +89,9 @@ begin;
('connection limit'),
('canceled');
-- Note: here, and in the session_connection table, we should add a trigger
-- ensuring that if server_id goes to null, we mark connections as closed. See
-- https://hashicorp.atlassian.net/browse/ICU-1495
create table session (
public_id wt_public_id primary key,
-- the user of the session
@@ -79,6 +79,7 @@ begin;
-- a endpoint for a session. The client initiates the connection to the worker
-- and the worker initiates the connection to the endpoint.
-- A session can have zero or more session connections.
-- Note: Updated to add server_id, server_type in 801
create table session_connection (
public_id wt_public_id primary key,
session_id wt_public_id not null
@@ -0,0 +1,25 @@
begin;
alter table session_connection
add column server_id text;
-- Note: here, and in the session table, we should add a trigger ensuring that
-- if server_id goes to null, we mark connections as closed. See
-- https://hashicorp.atlassian.net/browse/ICU-1495
alter table session_connection
add constraint server_fkey
foreign key (server_id)
references server (private_id)
on delete set null
on update cascade;
-- We now populate the connection information from existing session information
update session_connection sc
set
server_id = s.server_id
from
session s
where
sc.session_id = s.public_id;
commit;
@@ -0,0 +1,102 @@
package migration
import (
"database/sql"
"testing"
"github.com/hashicorp/boundary/internal/db"
"github.com/stretchr/testify/require"
)
func TestMigration(t *testing.T) {
require := require.New(t)
const (
createTables = `
create table session_testing (
public_id text primary key,
server_id text
);
create table session_connection_testing (
public_id text primary key,
session_id text,
server_id text
);
`
insertSession = `insert into session_testing values ($1, $2)`
insertSessionConnection = `insert into session_connection_testing values ($1, $2)`
update = `
update session_connection_testing sc
set
server_id = s.server_id
from
session_testing s
where
sc.session_id = s.public_id;
`
selectQuery = `select session_id, server_id from session_connection_testing order by session_id`
)
conn, _ := db.TestSetup(t, "postgres")
db := conn.DB()
_, err := db.Exec(createTables)
require.NoError(err)
tests := []struct {
sessId string
sessServerId string
connId string
}{
{
"sess1",
"server1",
"conn1",
},
{
"sess2",
"server1",
"conn2",
},
{
"sess3",
"server2",
"conn3",
},
{
"sess4",
"server2",
"conn4",
},
}
for _, tt := range tests {
_, err = db.Query(insertSession, tt.sessId, tt.sessServerId)
require.NoError(err)
_, err := db.Query(insertSessionConnection, tt.connId, tt.sessId)
require.NoError(err)
}
// At this point server ID should be empty in connections table
rows, err := db.Query(selectQuery)
require.NoError(err)
var sessVal string
var serverVal sql.NullString
for rows.Next() {
require.NoError(rows.Scan(&sessVal, &serverVal))
require.False(serverVal.Valid)
}
_, err = db.Query(update)
require.NoError(err)
// Now it should match the server
rows, err = db.Query(selectQuery)
require.NoError(err)
var count int
for rows.Next() {
require.NoError(rows.Scan(&sessVal, &serverVal))
require.True(serverVal.Valid)
require.Equal(tests[count].sessServerId, serverVal.String)
count++
}
}