Merge the worker_status table with the worker table (#2111)

This commit is contained in:
Todd authored and GitHub committed 2022-06-01 13:25:29 -07:00
1 parent 46e97a2b91
commit 731a45eba6
27 files changed
+1169 -764

No files matched your search

+3 -6
View File
@@ -327,13 +327,10 @@ where
dead_workers (worker_id, last_update_time) as (
select
w.public_id as worker_id,
ws.update_time as last_update_time
w.last_status_time as last_update_time
from server_worker w
left join server_worker_status ws
on w.public_id = ws.worker_id
where
ws.update_time is null
or ws.update_time < wt_sub_seconds_from_now(@grace_period_seconds)
w.last_status_time < wt_sub_seconds_from_now(@grace_period_seconds)
),
closed_connections (connection_id, worker_id) as (
update session_connection
@@ -343,7 +340,7 @@ where
returning public_id, worker_id
)
select closed_connections.worker_id,
coalesce(dead_workers.last_update_time, current_timestamp) as last_update_time,
dead_workers.last_update_time as last_update_time,
count(closed_connections.connection_id) as number_connections_closed
from closed_connections
join dead_workers
@@ -14,6 +14,7 @@ import (
"github.com/hashicorp/boundary/internal/session"
"github.com/hashicorp/boundary/internal/target"
"github.com/hashicorp/boundary/internal/target/tcp"
"github.com/hashicorp/boundary/internal/types/scope"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -31,8 +32,7 @@ func TestWorkerStatusReport(t *testing.T) {
PrivateId: "test_controller1",
Address: "127.0.0.1",
})
serverRepo.UpsertWorkerStatus(ctx, servers.NewWorkerStatus(
"test_worker1",
serverRepo.UpsertWorkerStatus(ctx, servers.NewWorkerForStatus(scope.Global.String(),
servers.WithAddress("127.0.0.1")))
repo, err := session.NewRepository(rw, rw, kms)
+13 -74
View File
@@ -12,6 +12,7 @@ import (
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/scheduler"
"github.com/hashicorp/boundary/internal/servers"
"github.com/hashicorp/boundary/internal/types/scope"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -108,9 +109,10 @@ func TestSessionConnectionCleanupJob(t *testing.T) {
// Push an upsert to the first worker so that its status has been
// updated.
_, rowsUpdated, err := serversRepo.UpsertWorkerStatus(ctx, worker1.ReportedStatus)
_, err = serversRepo.UpsertWorkerStatus(ctx, servers.NewWorkerForStatus(scope.Global.String(),
servers.WithName(worker1.GetWorkerReportedName()),
servers.WithAddress(worker1.GetWorkerReportedAddress())))
require.NoError(err)
require.Equal(1, rowsUpdated)
// Run the job.
require.NoError(job.Run(ctx))
@@ -168,61 +170,6 @@ func TestSessionConnectionCleanupJobNewJobErr(t *testing.T) {
require.Nil(job)
}
func TestCloseConnections_ManuallyDeletedConfig(t *testing.T) {
t.Parallel()
ctx := context.Background()
require := require.New(t)
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
wrapper := db.TestWrapper(t)
iamRepo := iam.TestRepo(t, conn, wrapper)
kms := kms.TestKms(t, conn, wrapper)
repo, err := NewRepository(rw, rw, kms)
require.NoError(err)
connRepo, err := NewConnectionRepository(ctx, rw, rw, kms)
require.NoError(err)
serversRepo, err := servers.NewRepository(rw, rw, kms)
require.NoError(err)
gracePeriod := 1 * time.Second
job, err := newSessionConnectionCleanupJob(rw, deadWorkerConnCloseMinGrace)
require.NoError(err)
worker := servers.TestWorker(t, conn, wrapper)
sess := TestDefaultSession(t, conn, wrapper, iamRepo, WithDbOpts(db.WithSkipVetForWrite(true)))
sess, _, err = repo.ActivateSession(ctx, sess.GetPublicId(), sess.Version, []byte("foo"))
require.NoError(err)
c, cs, err := connRepo.AuthorizeConnection(ctx, sess.GetPublicId(), worker.GetPublicId())
require.NoError(err)
require.Len(cs, 1)
require.Equal(StatusAuthorized, cs[0].Status)
_, cs, err = connRepo.ConnectConnection(ctx, ConnectWith{
ConnectionId: c.GetPublicId(),
ClientTcpAddress: "127.0.0.1",
ClientTcpPort: 22,
EndpointTcpAddress: "127.0.0.1",
EndpointTcpPort: 22,
UserClientIp: "127.0.0.1",
})
// The single worker's connections aren't cleaned up
_, _, err = serversRepo.UpsertWorkerStatus(ctx, worker.ReportedStatus)
assert.NoError(t, err)
result, err := job.closeConnectionsForDeadWorkers(ctx, gracePeriod)
require.NoError(err)
require.Empty(result)
// the manually deleted status causes the connections to be cleaned
_, _, err = serversRepo.UpsertWorkerStatus(ctx, worker.ReportedStatus)
assert.NoError(t, err)
_, err = rw.Delete(ctx, worker.ReportedStatus)
assert.NoError(t, err)
result, err = job.closeConnectionsForDeadWorkers(ctx, gracePeriod)
require.NoError(err)
require.Len(result, 1)
}
func TestCloseConnectionsForDeadWorkers(t *testing.T) {
t.Parallel()
ctx := context.Background()
@@ -345,21 +292,13 @@ func TestCloseConnectionsForDeadWorkers(t *testing.T) {
// the most up-to-date fields.
updateServer := func(t *testing.T, w *servers.Worker) *servers.Worker {
t.Helper()
_, rowsUpdated, err := serversRepo.UpsertWorkerStatus(ctx, w.ReportedStatus)
// The worker provided name will derive the WorkerId every time
// UpsertWorkerStatus is called. Setting the Worker Id on the caller
// side is an error.
w.PublicId = ""
wkr, err := serversRepo.UpsertWorkerStatus(ctx, w)
require.NoError(err)
require.Equal(1, rowsUpdated)
servers, err := serversRepo.ListWorkers(ctx)
require.NoError(err)
for _, server := range servers {
if server.PublicId == w.PublicId {
return server
}
}
require.FailNowf("server %q not found after updating", w.PublicId)
// Looks weird but needed to build, as we fail in testify instead
// of returning an error
return nil
return wkr
}
// requireConnectionStatus is a helper expecting all connections on a worker
@@ -454,7 +393,7 @@ func TestCloseConnectionsForDeadWorkers(t *testing.T) {
require.Equal([]closeConnectionsForDeadWorkersResult{
{
WorkerId: worker1.PublicId,
LastUpdateTime: timestampPbAsUTC(t, worker1.LastConnectionUpdate().AsTime()),
LastUpdateTime: timestampPbAsUTC(t, worker1.GetLastStatusTime().AsTime()),
NumberConnectionsClosed: 12, // 18 per server, with 6 closed already
},
}, result)
@@ -478,12 +417,12 @@ func TestCloseConnectionsForDeadWorkers(t *testing.T) {
require.ElementsMatch([]closeConnectionsForDeadWorkersResult{
{
WorkerId: worker2.PublicId,
LastUpdateTime: timestampPbAsUTC(t, worker2.LastConnectionUpdate().AsTime()),
LastUpdateTime: timestampPbAsUTC(t, worker2.GetLastStatusTime().AsTime()),
NumberConnectionsClosed: 12, // 18 per server, with 6 closed already
},
{
WorkerId: worker3.PublicId,
LastUpdateTime: timestampPbAsUTC(t, worker3.LastConnectionUpdate().AsTime()),
LastUpdateTime: timestampPbAsUTC(t, worker3.GetLastStatusTime().AsTime()),
NumberConnectionsClosed: 12, // 18 per server, with 6 closed already
},
}, result)