internal/servers/controller: Worker failure connection cleanup (#1340)

This commit adds the support to do the following:

* Mark connections for non-reporting workers as closed. This is the
controller counterpart to the worker functionality (see #1330). This is
written as a scheduled job that does the work DB-side in a single atomic
query.

* Works to reconcile states if such a broken controller-worker
connection resumes and a worker reports a connection as connected that
should be disconnected. In this case, the controller will send an update
request, and the worker will honor it and terminate the connection.

* Further refinement of the grace period setting has been added here.
We have converged on the current server "liveness" setting as our
default here, which is half of the previous 30s (15 seconds, in other
words). Additionally, this is now configurable on the controller and
worker side, with the caveat that it's currently impossible to do so in
config as the setting has been untagged in HCL. This is exposed so that
we can run some sophisticated testing scenarios where we skew the grace
period to either the controller or worker to ensure the aforementioned
reconciliation works.

* Some repository functions have been added to support the new
functionality, in addition to some test code to the worker to allow
querying of session state while testing.

* Finally, we've added some add timestamp subtraction functions
as well, basically serving as the opposite of the addition functions.
This commit is contained in:
Chris Marchesi authored and GitHub committed 2021-07-14 09:14:24 -07:00
1 parent 462306fea5
commit 5a70875726
23 files changed
+2072 -297

No files matched your search

@@ -0,0 +1,23 @@
begin;
create function wt_sub_seconds(sec integer, ts timestamp with time zone)
returns timestamp with time zone
as $$
select ts - sec * '1 second'::interval;
$$ language sql
stable
returns null on null input;
comment on function wt_add_seconds is
'wt_sub_seconds returns ts - sec.';
create function wt_sub_seconds_from_now(sec integer)
returns timestamp with time zone
as $$
select wt_sub_seconds(sec, current_timestamp);
$$ language sql
stable
returns null on null input;
comment on function wt_add_seconds_to_now is
'wt_sub_seconds_from_now returns current_timestamp - sec.';
commit;
@@ -0,0 +1,61 @@
package migration
import (
"context"
"database/sql"
"testing"
"time"
"github.com/hashicorp/boundary/internal/db/schema"
"github.com/hashicorp/boundary/internal/docker"
"github.com/stretchr/testify/require"
)
const targetMigration = 12001
func TestWtSubSeconds(t *testing.T) {
t.Parallel()
require := require.New(t)
ctx := context.Background()
d := testSetupDb(ctx, t)
// Test by subtracing a day from the test date
sourceTime, err := time.Parse(time.RFC3339, "2006-01-02T15:04:05+07:00")
require.NoError(err)
expectedTime := sourceTime.Add(time.Second * -86400)
var actualTime time.Time
row := d.QueryRowContext(ctx, "select wt_sub_seconds($1, $2)", 86400, sourceTime)
require.NoError(row.Scan(&actualTime))
require.True(expectedTime.Equal(actualTime))
}
func testSetupDb(ctx context.Context, t *testing.T) *sql.DB {
t.Helper()
require := require.New(t)
dialect := "postgres"
c, u, _, err := docker.StartDbInDocker(dialect)
require.NoError(err)
t.Cleanup(func() {
require.NoError(c())
})
d, err := sql.Open(dialect, u)
require.NoError(err)
oState := schema.TestCloneMigrationStates(t)
nState := schema.TestCreatePartialMigrationState(oState["postgres"], targetMigration)
oState["postgres"] = nState
m, err := schema.NewManager(ctx, dialect, d, schema.WithMigrationStates(oState))
require.NoError(err)
require.NoError(m.RollForward(ctx))
state, err := m.CurrentState(ctx)
require.NoError(err)
require.Equal(targetMigration, state.DatabaseSchemaVersion)
require.False(state.Dirty)
return d
}