This commit adds the support to do the following: * Mark connections for non-reporting workers as closed. This is the controller counterpart to the worker functionality (see #1330). This is written as a scheduled job that does the work DB-side in a single atomic query. * Works to reconcile states if such a broken controller-worker connection resumes and a worker reports a connection as connected that should be disconnected. In this case, the controller will send an update request, and the worker will honor it and terminate the connection. * Further refinement of the grace period setting has been added here. We have converged on the current server "liveness" setting as our default here, which is half of the previous 30s (15 seconds, in other words). Additionally, this is now configurable on the controller and worker side, with the caveat that it's currently impossible to do so in config as the setting has been untagged in HCL. This is exposed so that we can run some sophisticated testing scenarios where we skew the grace period to either the controller or worker to ensure the aforementioned reconciliation works. * Some repository functions have been added to support the new functionality, in addition to some test code to the worker to allow querying of session state while testing. * Finally, we've added some add timestamp subtraction functions as well, basically serving as the opposite of the addition functions.
57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package worker
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hashicorp/boundary/internal/cmd/base"
|
|
"github.com/hashicorp/go-hclog"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestWorkerWaitForNextSuccessfulStatusUpdate(t *testing.T) {
|
|
t.Parallel()
|
|
require := require.New(t)
|
|
for _, name := range []string{"ok", "timeout"} {
|
|
t.Run(name, func(t *testing.T) {
|
|
// As-needed initialization of a mock worker
|
|
w := &Worker{
|
|
logger: hclog.New(nil),
|
|
lastStatusSuccess: new(atomic.Value),
|
|
baseContext: context.Background(),
|
|
conf: &Config{
|
|
Server: &base.Server{
|
|
StatusGracePeriodDuration: time.Second * 1,
|
|
},
|
|
},
|
|
}
|
|
|
|
// This is present in New()
|
|
w.lastStatusSuccess.Store((*LastStatusInformation)(nil))
|
|
|
|
var wg sync.WaitGroup
|
|
var err error
|
|
wg.Add(1)
|
|
go func() {
|
|
err = w.WaitForNextSuccessfulStatusUpdate()
|
|
wg.Done()
|
|
}()
|
|
|
|
if name == "ok" {
|
|
time.Sleep(time.Millisecond * 100)
|
|
w.lastStatusSuccess.Store(&LastStatusInformation{StatusTime: time.Now()})
|
|
}
|
|
|
|
wg.Wait()
|
|
if name == "timeout" {
|
|
require.ErrorIs(err, context.DeadlineExceeded)
|
|
} else {
|
|
require.NoError(err)
|
|
}
|
|
})
|
|
}
|
|
}
|