2022-03-07 15:37:24 -05:00
package session
import (
"context"
"fmt"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/observability/event"
2022-10-06 16:52:26 +01:00
"github.com/hashicorp/go-multierror"
2022-03-07 15:37:24 -05:00
)
// StateReport is used to report on the state of a Session.
type StateReport struct {
2022-10-06 16:52:26 +01:00
SessionId string
Status Status
Connections [] * Connection
2022-03-07 15:37:24 -05:00
}
2022-10-06 16:52:26 +01:00
// WorkerStatusReport is a domain service function that, given a Worker's
// session state reports, performs a few tasks:
// 1. Updates the bytes up and down statistics for each reported connection.
// 2. Compares the state of sessions and connections as reported by a Worker,
// to the known state in the repositories. It returns a StateReport object
// for each session that is in the canceling or terminated state.
// 3. Checks 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 ) {
2022-03-07 15:37:24 -05:00
const op = "session.WorkerStatusReport"
2022-10-06 16:52:26 +01:00
reportedConnectionIds := make ([] string , 0 )
reportedConnections := make ([] * Connection , 0 )
2022-03-07 15:37:24 -05:00
reportedSessions := make ([] string , 0 , len ( report ))
for _ , r := range report {
reportedSessions = append ( reportedSessions , r . SessionId )
2022-10-06 16:52:26 +01:00
for _ , c := range r . Connections {
reportedConnectionIds = append ( reportedConnectionIds , c . GetPublicId ())
reportedConnections = append ( reportedConnections , c )
}
}
merr := new ( multierror . Error )
err := connRepo . updateBytesUpBytesDown ( ctx , reportedConnections ... )
if err != nil {
merr = multierror . Append ( merr , errors . New ( ctx , errors . Internal , op , fmt . Sprintf ( "failed to update bytes up and down for worker reported connections: %v" , err )))
2022-03-07 15:37:24 -05:00
}
notActive , err := repo . checkIfNoLongerActive ( ctx , reportedSessions )
if err != nil {
2022-10-06 16:52:26 +01:00
merr = multierror . Append ( merr , errors . New ( ctx , errors . Internal , op , fmt . Sprintf ( "Error checking session state for worker %s: %v" , workerId , err )))
2022-03-07 15:37:24 -05:00
}
2022-10-06 16:52:26 +01:00
closed , err := connRepo . closeOrphanedConnections ( ctx , workerId , reportedConnectionIds )
2022-03-07 15:37:24 -05:00
if err != nil {
2022-10-06 16:52:26 +01:00
merr = multierror . Append ( merr , errors . New ( ctx , errors . Internal , op , fmt . Sprintf ( "Error closing orphaned connections for worker %s: %v" , workerId , err )))
2022-03-07 15:37:24 -05:00
}
if len ( closed ) > 0 {
2022-05-18 17:03:24 -07:00
event . WriteSysEvent ( ctx , op , "marked unclaimed connections as closed" , "controller_id" , workerId , "count" , len ( closed ))
2022-03-07 15:37:24 -05:00
}
2022-10-06 16:52:26 +01:00
if merr . ErrorOrNil () != nil {
return nil , merr . ErrorOrNil ()
}
return notActive , nil
2022-03-07 15:37:24 -05:00
}