Files
threshold/internal/session/service_worker_status_report.go
T
Todd 2ecd550e27 Add fields to server_worker to be a boundary resource (#2083)
* Remove the server_id from session table, change it to worker_id on session_connection

This is step 1 in updating the worker table.  The worker is becoming a public resource so it must have
a public id.  We drop the server_id column on the session_connection table and add the worker_id column
so we can use the wt_public_key type.  Since db migrations are applied while the controller is offline,
it should be safe to assume all session connections are not in a running state, thus allowing us to null
out all the worker ids in the session_connection table.

For now we don't have very good enforcement of the wt_public_id contraints, since the id is currently
defined in the worker config, but this will soon change and the worker's id will be generated by the
controller.

* Updating server_id references to controller_id.

* Adding worker tag db writing test.

* Move the server table back to the top.

* Update comment for the worker object.

* Update comment for the worker table.

* Update the newWorkerId call to take a context and fix its error.

* Correct comment for the wt_network_address domain.
2022-05-18 17:03:24 -07:00

49 lines
1.8 KiB
Go

package session
import (
"context"
"fmt"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/observability/event"
)
// StateReport is used to report on the state of a Session.
type StateReport struct {
SessionId string
Status Status
ConnectionIds []string
}
// WorkerStatusReport is a domain service function that compares the state of
// sessions and connections as reported by a Worker, to the known state in the
// repositories. It returns a []StateReport for each session that is in the
// canceling or terminated state. It also will check for any orphaned
// connections, which is defined as a connection that is in an active state,
// but was not reported by worker. Any orphaned connections will be marked as
// closed.
func WorkerStatusReport(ctx context.Context, repo *Repository, connRepo *ConnectionRepository, workerId string, report []StateReport) ([]StateReport, error) {
const op = "session.WorkerStatusReport"
reportedConnections := make([]string, 0)
reportedSessions := make([]string, 0, len(report))
for _, r := range report {
reportedSessions = append(reportedSessions, r.SessionId)
reportedConnections = append(reportedConnections, r.ConnectionIds...)
}
notActive, err := repo.checkIfNoLongerActive(ctx, reportedSessions)
if err != nil {
return nil, errors.New(ctx, errors.Internal, op, fmt.Sprintf("Error checking session state for worker %s: %v", workerId, err))
}
closed, err := connRepo.closeOrphanedConnections(ctx, workerId, reportedConnections)
if err != nil {
return notActive, errors.New(ctx, errors.Internal, op, fmt.Sprintf("Error closing orphaned connections for worker %s: %v", workerId, err))
}
if len(closed) > 0 {
event.WriteSysEvent(ctx, op, "marked unclaimed connections as closed", "controller_id", workerId, "count", len(closed))
}
return notActive, err
}