ICU-16457 Split Liveness Query In Two (#5628)
* Fix #5602: Ensure controller status update triggers update_time
Rework and fix the logic of #5602 query split. Initial implementation failed to update controller status within `server_controller` as `w.Update` did not trigger `update_time` if `address` or `description` were not updated. Now using `w.Exec` with `updateController` query to always set `update_time` to `now()`.
Additional Notes:
- Updated `repository_controller` tests to verify `update_time` updates even when controller values don't change.
- Added `cleanupFunc` to test cases to remove inserted controllers after each test.
- Verified via `boundary dev` that `worker.upstreamDialerFunc` errors are not present.
Refs: 6c0b3a8a1c
* Resolve Comments
This commit is contained in:
1 parent
cd84da317e
commit
6d9f97d6c8
4 files changed
+231
-19
No files matched your search
@@ -1,10 +1,10 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
@@ -24,12 +24,10 @@ func (r *Repository) listControllersWithReader(ctx context.Context, reader db.Re
|
||||
if liveness == 0 {
|
||||
liveness = DefaultLiveness
|
||||
}
|
||||
|
||||
var where string
|
||||
if liveness > 0 {
|
||||
where = fmt.Sprintf("update_time > now() - interval '%d seconds'", uint32(liveness.Seconds()))
|
||||
}
|
||||
|
||||
var controllers []*Controller
|
||||
if err := reader.SearchWhere(
|
||||
ctx,
|
||||
@@ -40,17 +38,14 @@ func (r *Repository) listControllersWithReader(ctx context.Context, reader db.Re
|
||||
); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, "workers.listControllersWithReader")
|
||||
}
|
||||
|
||||
return controllers, nil
|
||||
}
|
||||
|
||||
func (r *Repository) UpsertController(ctx context.Context, controller *Controller) (int, error) {
|
||||
const op = "server.(Repository).UpsertController"
|
||||
|
||||
if controller == nil {
|
||||
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "controller is nil")
|
||||
}
|
||||
|
||||
var rowsUpdated int64
|
||||
_, err := r.writer.DoTx(
|
||||
ctx,
|
||||
@@ -66,7 +61,6 @@ func (r *Repository) UpsertController(ctx context.Context, controller *Controlle
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op+":Upsert")
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
@@ -76,3 +70,52 @@ func (r *Repository) UpsertController(ctx context.Context, controller *Controlle
|
||||
|
||||
return int(rowsUpdated), nil
|
||||
}
|
||||
|
||||
// UpdateControllerStatus updates the controller's status in the repository.
|
||||
// This includes updating the address or description of the controller as well
|
||||
// as updating the update_time attribute, which is required for liveness checks
|
||||
// as part of a controller's status ticking.
|
||||
func (r *Repository) UpdateControllerStatus(ctx context.Context, controller *Controller) (int, error) {
|
||||
const op = "server.(Repository).UpdateControllerStatus"
|
||||
|
||||
if controller == nil {
|
||||
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "controller is nil")
|
||||
}
|
||||
if controller.PrivateId == "" {
|
||||
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "controller private_id is empty")
|
||||
}
|
||||
if controller.Address == "" {
|
||||
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "controller address is empty")
|
||||
}
|
||||
|
||||
var rowsUpdated int
|
||||
_, err := r.writer.DoTx(
|
||||
ctx,
|
||||
db.StdRetryCnt,
|
||||
db.ExpBackoff{},
|
||||
func(reader db.Reader, w db.Writer) error {
|
||||
var err error
|
||||
rowsUpdated, err = w.Exec(ctx, updateController,
|
||||
[]any{
|
||||
sql.Named("controller_address", controller.Address),
|
||||
sql.Named("controller_description", controller.Description),
|
||||
sql.Named("controller_private_id", controller.PrivateId),
|
||||
})
|
||||
switch {
|
||||
case err != nil:
|
||||
return errors.Wrap(ctx, err, op+":Update")
|
||||
case rowsUpdated > 1:
|
||||
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 resource would have been updated")
|
||||
case rowsUpdated == 0:
|
||||
return errors.New(ctx, errors.RecordNotFound, op, "no resources would have been updated")
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, err
|
||||
}
|
||||
|
||||
return rowsUpdated, nil
|
||||
}
|
||||
Reference in new issue
Block a user