2018-08-08 00:46:37 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2018-10-04 23:22:01 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/taskrunner/state"
|
2018-11-01 01:00:30 +00:00
|
|
|
dmstate "github.com/hashicorp/nomad/client/devicemanager/state"
|
2020-03-12 20:24:58 +00:00
|
|
|
"github.com/hashicorp/nomad/client/dynamicplugins"
|
2018-11-28 03:42:22 +00:00
|
|
|
driverstate "github.com/hashicorp/nomad/client/pluginmanager/drivermanager/state"
|
2018-08-08 00:46:37 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StateDB implementations store and load Nomad client state.
|
|
|
|
type StateDB interface {
|
2018-10-16 22:17:36 +00:00
|
|
|
// Name of implementation.
|
|
|
|
Name() string
|
|
|
|
|
2018-12-07 01:24:43 +00:00
|
|
|
// Upgrade ensures the layout of the database is at the latest version
|
|
|
|
// or returns an error. Corrupt data will be dropped when possible.
|
|
|
|
// Errors should be considered critical and unrecoverable.
|
|
|
|
Upgrade() error
|
|
|
|
|
2018-10-16 22:17:36 +00:00
|
|
|
// GetAllAllocations returns all valid allocations and a map of
|
|
|
|
// allocation IDs to retrieval errors.
|
|
|
|
//
|
|
|
|
// If a single error is returned then both allocations and the map will be nil.
|
2018-08-08 00:46:37 +00:00
|
|
|
GetAllAllocations() ([]*structs.Allocation, map[string]error, error)
|
2018-10-16 22:17:36 +00:00
|
|
|
|
2020-10-20 20:15:37 +00:00
|
|
|
// PutAllocation stores an allocation or returns an error if it could
|
2018-10-16 22:17:36 +00:00
|
|
|
// not be stored.
|
2020-10-20 20:15:37 +00:00
|
|
|
PutAllocation(*structs.Allocation, ...WriteOption) error
|
2018-10-16 22:17:36 +00:00
|
|
|
|
2018-12-07 01:24:43 +00:00
|
|
|
// Get/Put DeploymentStatus get and put the allocation's deployment
|
|
|
|
// status. It may be nil.
|
|
|
|
GetDeploymentStatus(allocID string) (*structs.AllocDeploymentStatus, error)
|
|
|
|
PutDeploymentStatus(allocID string, ds *structs.AllocDeploymentStatus) error
|
|
|
|
|
2020-10-12 17:43:04 +00:00
|
|
|
// Get/Put NetworkStatus get and put the allocation's network
|
|
|
|
// status. It may be nil.
|
|
|
|
GetNetworkStatus(allocID string) (*structs.AllocNetworkStatus, error)
|
2020-10-20 20:15:37 +00:00
|
|
|
PutNetworkStatus(allocID string, ns *structs.AllocNetworkStatus, opts ...WriteOption) error
|
2020-10-12 17:43:04 +00:00
|
|
|
|
2018-10-16 22:17:36 +00:00
|
|
|
// GetTaskRunnerState returns the LocalState and TaskState for a
|
|
|
|
// TaskRunner. Either state may be nil if it is not found, but if an
|
|
|
|
// error is encountered only the error will be non-nil.
|
2018-08-08 00:46:37 +00:00
|
|
|
GetTaskRunnerState(allocID, taskName string) (*state.LocalState, *structs.TaskState, error)
|
2018-10-16 22:17:36 +00:00
|
|
|
|
|
|
|
// PutTaskRunnerLocalTask stores the LocalState for a TaskRunner or
|
|
|
|
// returns an error.
|
|
|
|
PutTaskRunnerLocalState(allocID, taskName string, val *state.LocalState) error
|
|
|
|
|
|
|
|
// PutTaskState stores the TaskState for a TaskRunner or returns an
|
|
|
|
// error.
|
2018-08-08 00:46:37 +00:00
|
|
|
PutTaskState(allocID, taskName string, state *structs.TaskState) error
|
2018-10-16 22:17:36 +00:00
|
|
|
|
|
|
|
// DeleteTaskBucket deletes a task's state bucket if it exists. No
|
|
|
|
// error is returned if it does not exist.
|
2018-08-23 19:03:17 +00:00
|
|
|
DeleteTaskBucket(allocID, taskName string) error
|
2018-10-16 22:17:36 +00:00
|
|
|
|
|
|
|
// DeleteAllocationBucket deletes an allocation's state bucket if it
|
|
|
|
// exists. No error is returned if it does not exist.
|
2020-10-20 20:15:37 +00:00
|
|
|
DeleteAllocationBucket(allocID string, opts ...WriteOption) error
|
2018-10-16 22:17:36 +00:00
|
|
|
|
2018-09-28 17:09:01 +00:00
|
|
|
// GetDevicePluginState is used to retrieve the device manager's plugin
|
|
|
|
// state.
|
2018-11-01 01:00:30 +00:00
|
|
|
GetDevicePluginState() (*dmstate.PluginState, error)
|
2018-09-28 17:09:01 +00:00
|
|
|
|
|
|
|
// PutDevicePluginState is used to store the device manager's plugin
|
|
|
|
// state.
|
2018-11-01 01:00:30 +00:00
|
|
|
PutDevicePluginState(state *dmstate.PluginState) error
|
2018-09-28 17:09:01 +00:00
|
|
|
|
2018-11-28 03:42:22 +00:00
|
|
|
// GetDriverPluginState is used to retrieve the driver manager's plugin
|
|
|
|
// state.
|
|
|
|
GetDriverPluginState() (*driverstate.PluginState, error)
|
|
|
|
|
|
|
|
// PutDriverPluginState is used to store the driver manager's plugin
|
|
|
|
// state.
|
|
|
|
PutDriverPluginState(state *driverstate.PluginState) error
|
|
|
|
|
2020-03-12 20:24:58 +00:00
|
|
|
// GetDynamicPluginRegistryState is used to retrieve a dynamic plugin manager's state.
|
|
|
|
GetDynamicPluginRegistryState() (*dynamicplugins.RegistryState, error)
|
|
|
|
|
2020-10-20 20:15:37 +00:00
|
|
|
// PutDynamicPluginRegistryState is used to store the dynamic plugin manager's state.
|
2020-03-12 20:24:58 +00:00
|
|
|
PutDynamicPluginRegistryState(state *dynamicplugins.RegistryState) error
|
|
|
|
|
2018-10-16 22:17:36 +00:00
|
|
|
// Close the database. Unsafe for further use after calling regardless
|
|
|
|
// of return value.
|
2018-08-08 00:46:37 +00:00
|
|
|
Close() error
|
|
|
|
}
|
2020-10-20 20:15:37 +00:00
|
|
|
|
|
|
|
// WriteOptions adjusts the way the data is persisted by the StateDB above. Default is
|
|
|
|
// zero/false values for all fields. To provide different values, use With* functions
|
|
|
|
// below, like this: statedb.PutAllocation(alloc, WithBatchMode())
|
|
|
|
type WriteOptions struct {
|
|
|
|
// In Batch mode, concurrent writes (Put* and Delete* operations above) are
|
|
|
|
// coalesced into a single transaction, increasing write performance. To benefit
|
|
|
|
// from this mode, writes must happen concurrently in goroutines, as every write
|
|
|
|
// request still waits for the shared transaction to commit before returning.
|
|
|
|
// See https://github.com/boltdb/bolt#batch-read-write-transactions for details.
|
|
|
|
// This mode is only supported for BoltDB state backend and is ignored in other backends.
|
|
|
|
BatchMode bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteOption is a function that modifies WriteOptions struct above.
|
|
|
|
type WriteOption func(*WriteOptions)
|
|
|
|
|
|
|
|
// mergeWriteOptions creates a final WriteOptions struct to be used by the write methods above
|
|
|
|
// from a list of WriteOption-s provided as variadic arguments.
|
|
|
|
func mergeWriteOptions(opts []WriteOption) WriteOptions {
|
|
|
|
writeOptions := WriteOptions{} // Default WriteOptions is zero value.
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&writeOptions)
|
|
|
|
}
|
|
|
|
return writeOptions
|
|
|
|
}
|
|
|
|
|
2021-08-30 09:08:12 +00:00
|
|
|
// WithBatchMode enables Batch mode for write requests (Put* and Delete*
|
|
|
|
// operations above).
|
2020-10-20 20:15:37 +00:00
|
|
|
func WithBatchMode() WriteOption {
|
|
|
|
return func(s *WriteOptions) {
|
|
|
|
s.BatchMode = true
|
|
|
|
}
|
|
|
|
}
|