Add monitor session job proto and controller changes

This adds a new Job type that is for generic session state monitoring.  Monitoring sessions does not include any information about specific connections.
This commit is contained in:
Todd authored and Timothy Messier committed 2023-05-31 12:18:12 +00:00
1 parent 4d60f42210
commit bc48f2d0e0
10 files changed
+851 -320

No files matched your search

-9
View File
@@ -392,15 +392,6 @@ update session_connection
where
public_id in (select public_id from connections_to_close)
returning public_id;
`
checkIfNotActive = `
select session_id, state
from session_state ss
where
(ss.state = 'canceling' or ss.state = 'terminated')
and ss.end_time is null
%s
;
`
deleteTerminated = `
delete from session
+27 -21
View File
@@ -708,48 +708,46 @@ func (r *Repository) updateState(ctx context.Context, sessionId string, sessionV
return &updatedSession, returnedStates, nil
}
// checkIfNoLongerActive checks the given sessions to see if they are in a
// CheckIfNotActive checks the given sessions to see if they are in a
// non-active state, i.e. "canceling" or "terminated" It returns a *StateReport
// object for each session that is not active, with its current status.
func (r *Repository) checkIfNoLongerActive(ctx context.Context, reportedSessions []string) ([]*StateReport, error) {
const op = "session.(Repository).checkIfNotActive"
func (r *Repository) CheckIfNotActive(ctx context.Context, reportedSessions []string) ([]*StateReport, error) {
const op = "session.(Repository).listSessionIdAndState"
notActive := make([]*StateReport, 0, len(reportedSessions))
args := make([]any, 0, len(reportedSessions))
var inClause string
if len(reportedSessions) <= 0 {
return notActive, nil
}
inClause = `and session_id in (%s)`
params := make([]string, len(reportedSessions))
for i, sessId := range reportedSessions {
params[i] = fmt.Sprintf("@%d", i)
args = append(args, sql.Named(fmt.Sprintf("%d", i), sessId))
unrecognizedSessions := make(map[string]struct{})
for _, sessId := range reportedSessions {
unrecognizedSessions[sessId] = struct{}{}
}
inClause = fmt.Sprintf(inClause, strings.Join(params, ","))
_, err := r.writer.DoTx(
ctx,
db.StdRetryCnt,
db.ExpBackoff{},
func(reader db.Reader, w db.Writer) error {
rows, err := r.reader.Query(ctx, fmt.Sprintf(checkIfNotActive, inClause), args)
var states []*State
err := r.reader.SearchWhere(ctx, &states, "end_time is null and session_id in (?)", []any{reportedSessions})
if err != nil {
return errors.Wrap(ctx, err, op)
}
defer rows.Close()
for rows.Next() {
var sessionId string
var status Status
if err := rows.Scan(&sessionId, &status); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("scan row failed"))
for _, s := range states {
delete(unrecognizedSessions, s.SessionId)
switch s.Status {
case StatusPending, StatusActive:
continue
case StatusCanceling, StatusTerminated:
default:
return errors.New(ctx, errors.Internal, op, fmt.Sprintf("unknown session state %q", s.Status))
}
notActive = append(notActive, &StateReport{
SessionId: sessionId,
Status: status,
SessionId: s.SessionId,
Status: s.Status,
})
}
return nil
@@ -758,6 +756,14 @@ func (r *Repository) checkIfNoLongerActive(ctx context.Context, reportedSessions
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("error checking if sessions are no longer active"))
}
for s := range unrecognizedSessions {
notActive = append(notActive, &StateReport{
SessionId: s,
Unrecognized: true,
})
}
return notActive, nil
}
@@ -1816,3 +1816,41 @@ func Test_decryptAndMaybeUpdateSession(t *testing.T) {
require.ErrorContains(t, err, "You may need to recreate your session")
})
}
func TestRepository_CheckIfNoLongerActive(t *testing.T) {
t.Parallel()
ctx := context.Background()
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
wrapper := db.TestWrapper(t)
iamRepo := iam.TestRepo(t, conn, wrapper)
testKms := kms.TestKms(t, conn, wrapper)
repo, err := NewRepository(ctx, rw, rw, testKms)
require.NoError(t, err)
terminatedSession := TestDefaultSession(t, conn, wrapper, iamRepo)
terminatedSession, err = repo.CancelSession(ctx, terminatedSession.PublicId, terminatedSession.Version)
require.NoError(t, err)
n, err := repo.terminateSessionIfPossible(ctx, terminatedSession.PublicId)
require.NoError(t, err)
require.Equal(t, 1, n)
cancelingSess := TestDefaultSession(t, conn, wrapper, iamRepo)
cancelingSess, err = repo.CancelSession(ctx, cancelingSess.PublicId, cancelingSess.Version)
require.NoError(t, err)
pendingSess := TestDefaultSession(t, conn, wrapper, iamRepo)
activeSess := TestDefaultSession(t, conn, wrapper, iamRepo)
_, _, err = repo.ActivateSession(ctx, activeSess.PublicId, activeSess.Version, []byte("tofu"))
require.NoError(t, err)
unrecognizedSessionId := "unrecognized_session_id"
got, err := repo.CheckIfNotActive(ctx, []string{unrecognizedSessionId, terminatedSession.PublicId, cancelingSess.PublicId, activeSess.PublicId, pendingSess.PublicId})
assert.NoError(t, err)
var gotIds []string
for _, g := range got {
gotIds = append(gotIds, g.SessionId)
}
assert.ElementsMatch(t, gotIds, []string{unrecognizedSessionId, terminatedSession.PublicId, cancelingSess.PublicId})
}
@@ -17,6 +17,8 @@ type StateReport struct {
SessionId string
Status Status
Connections []*Connection
// Unrecognized indicates that the SessionId was not found in the database.
Unrecognized bool
}
// WorkerStatusReport is a domain service function that, given a Worker's
@@ -48,7 +50,7 @@ func WorkerStatusReport(ctx context.Context, repo *Repository, connRepo *Connect
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)))
}
notActive, err := repo.checkIfNoLongerActive(ctx, reportedSessions)
notActive, err := repo.CheckIfNotActive(ctx, reportedSessions)
if err != nil {
merr = multierror.Append(merr, errors.New(ctx, errors.Internal, op, fmt.Sprintf("Error checking session state for worker %s: %v", workerId, err)))
}
@@ -185,6 +185,28 @@ func TestWorkerStatusReport(t *testing.T) {
}
},
},
{
name: "unrecognized session",
caseFn: func(t *testing.T) testCase {
worker := server.TestKmsWorker(t, conn, wrapper)
return testCase{
worker: worker,
req: []*session.StateReport{
{
SessionId: "unrecognized_session_id",
Status: session.StatusActive,
},
},
want: []*session.StateReport{
{
SessionId: "unrecognized_session_id",
Unrecognized: true,
},
},
}
},
},
{
name: "MultipleSessionsClosed",
caseFn: func(t *testing.T) testCase {