internal/servers/controller: Worker failure connection cleanup (#1340)

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.
This commit is contained in:
Chris Marchesi authored and GitHub committed 2021-07-14 09:14:24 -07:00
1 parent 462306fea5
commit 5a70875726
23 files changed
+2072 -297

No files matched your search

+72
View File
@@ -15,6 +15,7 @@ import (
"strings"
"sync"
"syscall"
"time"
"github.com/armon/go-metrics"
berrors "github.com/hashicorp/boundary/internal/errors"
@@ -24,6 +25,7 @@ import (
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/observability/event"
"github.com/hashicorp/boundary/internal/servers"
"github.com/hashicorp/boundary/internal/types/scope"
"github.com/hashicorp/boundary/version"
"github.com/hashicorp/go-hclog"
@@ -40,6 +42,23 @@ import (
"google.golang.org/grpc/grpclog"
)
const (
// defaultStatusGracePeriod is the default status grace period, or the period
// of time that we will go without a status report before we start
// disconnecting and marking connections as closed. This is tied to the
// server default liveness setting, a related value. See the servers package
// for more details.
defaultStatusGracePeriod = servers.DefaultLiveness
// statusGracePeriodEnvVar is the environment variable that can be used to
// configure the status grace period. This setting is provided in seconds,
// and can never be lower than the default status grace period defined above.
//
// TODO: This value is temporary, it will be removed once we have a better
// story/direction on attributes and system defaults.
statusGracePeriodEnvVar = "BOUNDARY_STATUS_GRACE_PERIOD"
)
type Server struct {
*Command
@@ -101,6 +120,11 @@ type Server struct {
DevDatabaseCleanupFunc func() error
Database *gorm.DB
// StatusGracePeriodDuration represents the period of time (as a
// duration) that the controller will wait before marking
// connections from a disconnected worker as invalid.
StatusGracePeriodDuration time.Duration
}
func NewServer(cmd *Command) *Server {
@@ -675,3 +699,51 @@ func MakeSighupCh() chan struct{} {
}()
return resultCh
}
// SetStatusGracePeriodDuration sets the value for
// StatusGracePeriodDuration.
//
// The grace period is the length of time we allow connections to run
// on a worker in the event of an error sending status updates. The
// period is defined the length of time since the last successful
// update.
//
// The setting is derived from one of the following, in order:
//
// * Via the supplied value if non-zero.
// * BOUNDARY_STATUS_GRACE_PERIOD, if defined, can be set to an
// integer value to define the setting.
// * If either of these is missing, the default is used. See the
// defaultStatusGracePeriod value for the default value.
//
// The minimum setting for this value is the default setting. Values
// below this will be reset to the default.
func (s *Server) SetStatusGracePeriodDuration(value time.Duration) {
var result time.Duration
switch {
case value > 0:
result = value
case os.Getenv(statusGracePeriodEnvVar) != "":
// TODO: See the description of the constant for more details on
// this env var
v := os.Getenv(statusGracePeriodEnvVar)
n, err := strconv.Atoi(v)
if err != nil {
s.Logger.Error(fmt.Sprintf("could not read setting for %s", statusGracePeriodEnvVar),
"err", err,
"value", v,
)
break
}
result = time.Second * time.Duration(n)
}
if result < defaultStatusGracePeriod {
s.Logger.Debug("invalid grace period setting or none provided, using default", "value", result, "default", defaultStatusGracePeriod)
result = defaultStatusGracePeriod
}
s.Logger.Debug("session cleanup in effect, connections will be terminated if status reports cannot be made", "grace_period", result)
s.StatusGracePeriodDuration = result
}
+4
View File
@@ -425,6 +425,10 @@ func (c *Command) Run(args []string) int {
return base.CommandCliError
}
// Initialize status grace period (0 denotes using env or default
// here)
c.SetStatusGracePeriodDuration(0)
base.StartMemProfiler(c.Logger)
if err := c.SetupMetrics(c.UI, c.Config.Telemetry); err != nil {
+4
View File
@@ -156,6 +156,10 @@ func (c *Command) Run(args []string) int {
return base.CommandUserError
}
// Initialize status grace period (0 denotes using env or default
// here)
c.SetStatusGracePeriodDuration(0)
base.StartMemProfiler(c.Logger)
if !c.skipMetrics {
+14
View File
@@ -119,6 +119,13 @@ type Controller struct {
// denoted by time.Duration
AuthTokenTimeToStale interface{} `hcl:"auth_token_time_to_stale"`
AuthTokenTimeToStaleDuration time.Duration
// StatusGracePeriod represents the period of time (as a duration) that the
// controller will wait before marking connections from a disconnected worker
// as invalid.
//
// TODO: This field is currently internal.
StatusGracePeriodDuration time.Duration `hcl:"-"`
}
type Worker struct {
@@ -132,6 +139,13 @@ type Worker struct {
// key=value syntax. This is trued up in the Parse function below.
TagsRaw interface{} `hcl:"tags"`
Tags map[string][]string `hcl:"-"`
// StatusGracePeriod represents the period of time (as a duration) that the
// worker will wait before disconnecting connections if it cannot make a
// status report to a controller.
//
// TODO: This field is currently internal.
StatusGracePeriodDuration time.Duration `hcl:"-"`
}
type Database struct {