address op state review feedback (#2453)

* address op state review feedback
This commit is contained in:
Irena Rindos authored and GitHub committed 2022-09-16 09:13:37 -04:00
1 parent c73a13aa3e
commit 821d8317dc
6 files changed
+122 -55

No files matched your search

@@ -681,7 +681,7 @@ func (s Service) AuthorizeSession(ctx context.Context, req *pbs.AuthorizeSession
// worker IDs below is used to contain their IDs in the same order. This is // worker IDs below is used to contain their IDs in the same order. This is
// used to fetch tags for filtering. But we avoid allocation unless we // used to fetch tags for filtering. But we avoid allocation unless we
// actually need it. // actually need it.
selectedWorkers, err := serversRepo.ListWorkers(ctx, []string{scope.Global.String()}, server.WithExcludeShutdownWorkers(true)) selectedWorkers, err := serversRepo.ListWorkers(ctx, []string{scope.Global.String()}, server.WithActiveWorkers(true))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -9,7 +9,7 @@ alter table server_worker
drop view server_worker_aggregate; drop view server_worker_aggregate;
-- Updates view created in 34/04_views.up.sql -- Updated in 52/01_worker_operational_state.up.sql
create view server_worker_aggregate as create view server_worker_aggregate as
with worker_config_tags(worker_id, source, tags) as ( with worker_config_tags(worker_id, source, tags) as (
select select
+4 -4
View File
@@ -44,7 +44,7 @@ type options struct {
WithCreateControllerLedActivationToken bool WithCreateControllerLedActivationToken bool
withReleaseVersion string withReleaseVersion string
withOperationalState string withOperationalState string
withExcludeShutdownWorkers bool withActiveWorkers bool
} }
func getDefaultOptions() options { func getDefaultOptions() options {
@@ -215,9 +215,9 @@ func WithOperationalState(state string) Option {
} }
} }
// WithExcludeShutdownWorkers provides an optional to filter out workers in shutdown // WithActiveWorkers provides an optional filter to only include active workers
func WithExcludeShutdownWorkers(exclude bool) Option { func WithActiveWorkers(withActive bool) Option {
return func(o *options) { return func(o *options) {
o.withExcludeShutdownWorkers = exclude o.withActiveWorkers = withActive
} }
} }
+3 -3
View File
@@ -198,8 +198,8 @@ func Test_GetOpts(t *testing.T) {
}) })
t.Run("WithExcludeShutdown", func(t *testing.T) { t.Run("WithExcludeShutdown", func(t *testing.T) {
opts := getDefaultOptions() opts := getDefaultOptions()
assert.Empty(t, opts.withExcludeShutdownWorkers) assert.Empty(t, opts.withActiveWorkers)
opts = GetOpts(WithExcludeShutdownWorkers(true)) opts = GetOpts(WithActiveWorkers(true))
assert.Equal(t, true, opts.withExcludeShutdownWorkers) assert.Equal(t, true, opts.withActiveWorkers)
}) })
} }
+4 -4
View File
@@ -164,7 +164,7 @@ func lookupWorker(ctx context.Context, reader db.Reader, id string) (*Worker, er
// then the last status update time is ignored. // then the last status update time is ignored.
// If WithLimit < 0, then unlimited results are returned. If WithLimit == 0, then // If WithLimit < 0, then unlimited results are returned. If WithLimit == 0, then
// default limits are used for results. // default limits are used for results.
// Also supports: WithWorkerType, WithExcludeShutdownWorkers // Also supports: WithWorkerType, WithActiveWorkers
func (r *Repository) ListWorkers(ctx context.Context, scopeIds []string, opt ...Option) ([]*Worker, error) { func (r *Repository) ListWorkers(ctx context.Context, scopeIds []string, opt ...Option) ([]*Worker, error) {
const op = "server.(Repository).ListWorkers" const op = "server.(Repository).ListWorkers"
switch { switch {
@@ -197,9 +197,9 @@ func (r *Repository) ListWorkers(ctx context.Context, scopeIds []string, opt ...
return nil, errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("unknown worker type %v", opts.withWorkerType)) return nil, errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("unknown worker type %v", opts.withWorkerType))
} }
if opts.withExcludeShutdownWorkers { if opts.withActiveWorkers {
where = append(where, "operational_state not in (?)") where = append(where, "operational_state = ?")
whereArgs = append(whereArgs, ShutdownOperationalState.String()) whereArgs = append(whereArgs, ActiveOperationalState.String())
} }
limit := r.defaultLimit limit := r.defaultLimit
+109 -42
View File
@@ -290,14 +290,14 @@ func TestUpsertWorkerStatus(t *testing.T) {
assert.Equal(t, "new_address", worker.GetAddress()) assert.Equal(t, "new_address", worker.GetAddress())
// Expect this worker to be returned as it is active // Expect this worker to be returned as it is active
workers, err := repo.ListWorkers(ctx, []string{scope.Global.String()}, server.WithExcludeShutdownWorkers(true)) workers, err := repo.ListWorkers(ctx, []string{scope.Global.String()}, server.WithActiveWorkers(true))
require.NoError(t, err) require.NoError(t, err)
assert.Len(t, workers, 1) assert.Len(t, workers, 1)
// update again with a shutdown state // update again with a shutdown state
wStatus3 := server.NewWorker(scope.Global.String(), wStatus3 := server.NewWorker(scope.Global.String(),
server.WithAddress("new_address"), server.WithName("config_name1"), server.WithAddress("new_address"), server.WithName("config_name1"),
server.WithOperationalState("shutdown")) server.WithOperationalState("shutdown"), server.WithReleaseVersion("Boundary v0.11.0"))
worker, err = repo.UpsertWorkerStatus(ctx, wStatus3) worker, err = repo.UpsertWorkerStatus(ctx, wStatus3)
require.NoError(t, err) require.NoError(t, err)
assert.Greater(t, worker.GetLastStatusTime().AsTime(), worker.GetCreateTime().AsTime()) assert.Greater(t, worker.GetLastStatusTime().AsTime(), worker.GetCreateTime().AsTime())
@@ -306,7 +306,7 @@ func TestUpsertWorkerStatus(t *testing.T) {
assert.Equal(t, "shutdown", worker.GetOperationalState()) assert.Equal(t, "shutdown", worker.GetOperationalState())
// Should no longer see this worker in listing if we exclude shutdown workers // Should no longer see this worker in listing if we exclude shutdown workers
workers, err = repo.ListWorkers(ctx, []string{scope.Global.String()}, server.WithExcludeShutdownWorkers(true)) workers, err = repo.ListWorkers(ctx, []string{scope.Global.String()}, server.WithActiveWorkers(true))
require.NoError(t, err) require.NoError(t, err)
assert.Len(t, workers, 0) assert.Len(t, workers, 0)
}) })
@@ -455,7 +455,8 @@ func TestUpsertWorkerStatus(t *testing.T) {
t.Run("add another status", func(t *testing.T) { t.Run("add another status", func(t *testing.T) {
anotherStatus := server.NewWorker(scope.Global.String(), anotherStatus := server.NewWorker(scope.Global.String(),
server.WithName("another_test_worker"), server.WithName("another_test_worker"),
server.WithAddress("address")) server.WithAddress("address"),
server.WithReleaseVersion("Boundary v0.11.0"))
_, err = repo.UpsertWorkerStatus(ctx, anotherStatus) _, err = repo.UpsertWorkerStatus(ctx, anotherStatus)
require.NoError(t, err) require.NoError(t, err)
@@ -468,12 +469,13 @@ func TestUpsertWorkerStatus(t *testing.T) {
anotherStatus := server.NewWorker(scope.Global.String(), anotherStatus := server.NewWorker(scope.Global.String(),
server.WithName("another_test_worker"), server.WithName("another_test_worker"),
server.WithAddress("address"), server.WithAddress("address"),
server.WithOperationalState("shutdown")) server.WithOperationalState("shutdown"),
server.WithReleaseVersion("Boundary v0.11.0"))
_, err = repo.UpsertWorkerStatus(ctx, anotherStatus) _, err = repo.UpsertWorkerStatus(ctx, anotherStatus)
require.NoError(t, err) require.NoError(t, err)
// Filtering out shutdown workers will remove the shutdown KMS and this shutdown worker, resulting in 1 // Filtering out shutdown workers will remove the shutdown KMS and this shutdown worker, resulting in 1
workers, err := repo.ListWorkers(ctx, []string{scope.Global.String()}, server.WithExcludeShutdownWorkers(true)) workers, err := repo.ListWorkers(ctx, []string{scope.Global.String()}, server.WithActiveWorkers(true))
require.NoError(t, err) require.NoError(t, err)
assert.Len(t, workers, 1) assert.Len(t, workers, 1)
}) })
@@ -643,7 +645,7 @@ func TestListWorkers(t *testing.T) {
} }
} }
func TestListWorkers_WithExcludeShutdown(t *testing.T) { func TestListWorkers_WithActiveWorkers(t *testing.T) {
t.Parallel() t.Parallel()
require := require.New(t) require := require.New(t)
conn, _ := db.TestSetup(t, "postgres") conn, _ := db.TestSetup(t, "postgres")
@@ -662,41 +664,106 @@ func TestListWorkers_WithExcludeShutdown(t *testing.T) {
require.NoError(err) require.NoError(err)
require.Len(result, 3) require.Len(result, 3)
// Push an upsert to the first worker so that its status has been tests := []struct {
// updated. name string
_, err = serversRepo.UpsertWorkerStatus(ctx, upsertFn func() (*server.Worker, error)
server.NewWorker(scope.Global.String(), wantCnt int
server.WithName(worker1.GetName()), wantState string
server.WithAddress(worker1.GetAddress()), }{
server.WithOperationalState(server.ShutdownOperationalState.String()), {
server.WithPublicId(worker1.GetPublicId()))) name: "upsert-worker1-to-shutdown",
require.NoError(err) upsertFn: func() (*server.Worker, error) {
return serversRepo.UpsertWorkerStatus(ctx,
result, err = serversRepo.ListWorkers(ctx, []string{scope.Global.String()}, server.WithExcludeShutdownWorkers(true)) server.NewWorker(scope.Global.String(),
require.NoError(err) server.WithName(worker1.GetName()),
require.Len(result, 2) server.WithAddress(worker1.GetAddress()),
server.WithOperationalState(server.ShutdownOperationalState.String()),
_, err = serversRepo.UpsertWorkerStatus(ctx, server.WithReleaseVersion("Boundary v.0.11"),
server.NewWorker(scope.Global.String(), server.WithPublicId(worker1.GetPublicId())))
server.WithName(worker2.GetName()), },
server.WithAddress(worker2.GetAddress()), wantCnt: 2,
server.WithOperationalState(server.ShutdownOperationalState.String())), wantState: server.ShutdownOperationalState.String(),
server.WithPublicId(worker2.GetPublicId())) },
require.NoError(err) {
result, err = serversRepo.ListWorkers(ctx, []string{scope.Global.String()}, server.WithExcludeShutdownWorkers(true)) name: "upsert-worker2-to-shutdown",
require.NoError(err) upsertFn: func() (*server.Worker, error) {
require.Len(result, 1) return serversRepo.UpsertWorkerStatus(ctx,
server.NewWorker(scope.Global.String(),
_, err = serversRepo.UpsertWorkerStatus(ctx, server.WithName(worker2.GetName()),
server.NewWorker(scope.Global.String(), server.WithAddress(worker2.GetAddress()),
server.WithName(worker3.GetName()), server.WithOperationalState(server.ShutdownOperationalState.String()),
server.WithAddress(worker3.GetAddress()), server.WithReleaseVersion("Boundary v.0.11"),
server.WithOperationalState(server.ShutdownOperationalState.String())), server.WithPublicId(worker2.GetPublicId())))
server.WithPublicId(worker3.GetPublicId())) },
require.NoError(err) wantCnt: 1,
result, err = serversRepo.ListWorkers(ctx, []string{scope.Global.String()}, server.WithExcludeShutdownWorkers(true)) wantState: server.ShutdownOperationalState.String(),
require.NoError(err) },
require.Len(result, 0) {
name: "upsert-worker3-to-shutdown",
upsertFn: func() (*server.Worker, error) {
return serversRepo.UpsertWorkerStatus(ctx,
server.NewWorker(scope.Global.String(),
server.WithName(worker3.GetName()),
server.WithAddress(worker3.GetAddress()),
server.WithOperationalState(server.ShutdownOperationalState.String()),
server.WithReleaseVersion("Boundary v.0.11"),
server.WithPublicId(worker3.GetPublicId())))
},
wantCnt: 0,
wantState: server.ShutdownOperationalState.String(),
},
{ // Upsert without a release version or state and expect to get a hit- test backwards compatibility
// Pre 0.11 workers will default to Active
name: "upsert-no-release-version-no-state",
upsertFn: func() (*server.Worker, error) {
return serversRepo.UpsertWorkerStatus(ctx,
server.NewWorker(scope.Global.String(),
server.WithName(worker3.GetName()),
server.WithAddress(worker3.GetAddress())),
server.WithPublicId(worker3.GetPublicId()))
},
wantCnt: 1,
wantState: server.ActiveOperationalState.String(),
},
{ // Upsert with active status and no version and expect to get a hit- test backwards compatibility
name: "upsert-no-release-version-active-state",
upsertFn: func() (*server.Worker, error) {
return serversRepo.UpsertWorkerStatus(ctx,
server.NewWorker(scope.Global.String(),
server.WithName(worker3.GetName()),
server.WithAddress(worker3.GetAddress()),
server.WithOperationalState(server.ActiveOperationalState.String())),
server.WithPublicId(worker3.GetPublicId()))
},
wantCnt: 1,
wantState: server.ActiveOperationalState.String(),
},
{ // Upsert with unknown status and do not expect to get a hit- test worker create before status
name: "upsert-unknown-status",
upsertFn: func() (*server.Worker, error) {
return serversRepo.UpsertWorkerStatus(ctx,
server.NewWorker(scope.Global.String(),
server.WithName(worker3.GetName()),
server.WithAddress(worker3.GetAddress()),
server.WithOperationalState(server.UnknownOperationalState.String())),
server.WithPublicId(worker3.GetPublicId()))
},
wantCnt: 0,
wantState: server.UnknownOperationalState.String(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
worker, err := tt.upsertFn()
require.NoError(err)
got, err := serversRepo.ListWorkers(ctx, []string{scope.Global.String()}, server.WithActiveWorkers(true))
require.NoError(err)
assert.Len(t, got, tt.wantCnt)
if len(tt.wantState) > 0 {
assert.Equal(t, tt.wantState, worker.OperationalState)
}
})
}
} }
func TestListWorkers_WithLiveness(t *testing.T) { func TestListWorkers_WithLiveness(t *testing.T) {