2018-06-11 20:33:18 +00:00
|
|
|
package state
|
2017-05-01 23:18:03 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-12-07 01:24:43 +00:00
|
|
|
"os"
|
2018-08-08 00:46:37 +00:00
|
|
|
"path/filepath"
|
2018-12-07 01:24:43 +00:00
|
|
|
"time"
|
2017-05-01 23:18:03 +00:00
|
|
|
|
2019-09-24 23:28:21 +00:00
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
|
2018-12-07 01:24:43 +00:00
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
2018-10-04 23:22:01 +00:00
|
|
|
trstate "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-16 18:14:52 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/boltdd"
|
2017-05-01 23:18:03 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
2018-09-28 17:09:01 +00:00
|
|
|
The client has a boltDB backed state store. The schema as of 0.9 looks as follows:
|
2017-05-01 23:18:03 +00:00
|
|
|
|
2018-12-07 01:24:43 +00:00
|
|
|
meta/
|
2018-12-18 22:49:09 +00:00
|
|
|
|--> version -> '2' (not msgpack encoded)
|
2018-12-07 01:24:43 +00:00
|
|
|
|--> upgraded -> time.Now().Format(timeRFC3339)
|
2018-12-05 01:01:08 +00:00
|
|
|
allocations/
|
|
|
|
|--> <alloc-id>/
|
2020-10-12 17:43:04 +00:00
|
|
|
|--> alloc -> allocEntry{*structs.Allocation}
|
|
|
|
|--> deploy_status -> deployStatusEntry{*structs.AllocDeploymentStatus}
|
|
|
|
|--> network_status -> networkStatusEntry{*structs.AllocNetworkStatus}
|
2018-12-05 01:01:08 +00:00
|
|
|
|--> task-<name>/
|
|
|
|
|--> local_state -> *trstate.LocalState # Local-only state
|
|
|
|
|--> task_state -> *structs.TaskState # Sync'd to servers
|
2018-09-28 17:09:01 +00:00
|
|
|
|
|
|
|
devicemanager/
|
2018-12-05 01:01:08 +00:00
|
|
|
|--> plugin_state -> *dmstate.PluginState
|
2018-11-28 03:42:22 +00:00
|
|
|
|
|
|
|
drivermanager/
|
2020-03-12 18:40:07 +00:00
|
|
|
|--> plugin_state -> *driverstate.PluginState
|
2020-03-12 20:24:58 +00:00
|
|
|
|
|
|
|
dynamicplugins/
|
|
|
|
|--> registry_state -> *dynamicplugins.RegistryState
|
2017-05-01 23:18:03 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
var (
|
2018-12-07 01:24:43 +00:00
|
|
|
// metaBucketName is the name of the metadata bucket
|
|
|
|
metaBucketName = []byte("meta")
|
|
|
|
|
|
|
|
// metaVersionKey is the key the state schema version is stored under.
|
|
|
|
metaVersionKey = []byte("version")
|
|
|
|
|
|
|
|
// metaVersion is the value of the state schema version to detect when
|
|
|
|
// an upgrade is needed. It skips the usual boltdd/msgpack backend to
|
|
|
|
// be as portable and futureproof as possible.
|
2018-12-18 22:49:09 +00:00
|
|
|
metaVersion = []byte{'2'}
|
2018-12-07 01:24:43 +00:00
|
|
|
|
|
|
|
// metaUpgradedKey is the key that stores the timestamp of the last
|
|
|
|
// time the schema was upgraded.
|
|
|
|
metaUpgradedKey = []byte("upgraded")
|
|
|
|
|
2018-10-16 22:17:36 +00:00
|
|
|
// allocationsBucketName is the bucket name containing all allocation related
|
2017-05-01 23:18:03 +00:00
|
|
|
// data
|
2018-10-16 22:17:36 +00:00
|
|
|
allocationsBucketName = []byte("allocations")
|
2018-08-08 00:46:37 +00:00
|
|
|
|
2018-12-05 01:01:08 +00:00
|
|
|
// allocKey is the key Allocations are stored under encapsulated in
|
|
|
|
// allocEntry structs.
|
2018-08-08 00:46:37 +00:00
|
|
|
allocKey = []byte("alloc")
|
|
|
|
|
2018-12-07 01:24:43 +00:00
|
|
|
// allocDeployStatusKey is the key *structs.AllocDeploymentStatus is
|
|
|
|
// stored under.
|
|
|
|
allocDeployStatusKey = []byte("deploy_status")
|
|
|
|
|
2020-10-12 17:43:04 +00:00
|
|
|
// allocNetworkStatusKey is the key *structs.AllocNetworkStatus is
|
|
|
|
// stored under
|
|
|
|
allocNetworkStatusKey = []byte("network_status")
|
|
|
|
|
2018-12-05 01:01:08 +00:00
|
|
|
// allocations -> $allocid -> task-$taskname -> the keys below
|
2018-08-08 00:46:37 +00:00
|
|
|
taskLocalStateKey = []byte("local_state")
|
|
|
|
taskStateKey = []byte("task_state")
|
2018-09-28 17:09:01 +00:00
|
|
|
|
|
|
|
// devManagerBucket is the bucket name containing all device manager related
|
|
|
|
// data
|
|
|
|
devManagerBucket = []byte("devicemanager")
|
|
|
|
|
2020-03-12 20:24:58 +00:00
|
|
|
// driverManagerBucket is the bucket name containing all driver manager
|
2018-11-28 03:42:22 +00:00
|
|
|
// related data
|
|
|
|
driverManagerBucket = []byte("drivermanager")
|
|
|
|
|
|
|
|
// managerPluginStateKey is the key by which plugin manager plugin state is
|
|
|
|
// stored at
|
|
|
|
managerPluginStateKey = []byte("plugin_state")
|
2020-03-12 20:24:58 +00:00
|
|
|
|
|
|
|
// dynamicPluginBucket is the bucket name containing all dynamic plugin
|
|
|
|
// registry data. each dynamic plugin registry will have its own subbucket.
|
|
|
|
dynamicPluginBucket = []byte("dynamicplugins")
|
|
|
|
|
|
|
|
// registryStateKey is the key at which dynamic plugin registry state is stored
|
|
|
|
registryStateKey = []byte("registry_state")
|
2017-05-01 23:18:03 +00:00
|
|
|
)
|
|
|
|
|
2018-12-05 01:01:08 +00:00
|
|
|
// taskBucketName returns the bucket name for the given task name.
|
|
|
|
func taskBucketName(taskName string) []byte {
|
|
|
|
return []byte("task-" + taskName)
|
|
|
|
}
|
|
|
|
|
2018-08-09 00:06:56 +00:00
|
|
|
// NewStateDBFunc creates a StateDB given a state directory.
|
2018-12-07 01:24:43 +00:00
|
|
|
type NewStateDBFunc func(logger hclog.Logger, stateDir string) (StateDB, error)
|
2018-08-08 00:46:37 +00:00
|
|
|
|
2018-08-09 00:06:56 +00:00
|
|
|
// GetStateDBFactory returns a func for creating a StateDB
|
|
|
|
func GetStateDBFactory(devMode bool) NewStateDBFunc {
|
|
|
|
// Return a noop state db implementation when in debug mode
|
|
|
|
if devMode {
|
2018-12-07 01:24:43 +00:00
|
|
|
return func(hclog.Logger, string) (StateDB, error) {
|
2018-09-27 00:29:08 +00:00
|
|
|
return NoopDB{}, nil
|
2018-08-08 00:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 00:06:56 +00:00
|
|
|
return NewBoltStateDB
|
2018-08-08 00:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BoltStateDB persists and restores Nomad client state in a boltdb. All
|
2018-08-22 17:18:49 +00:00
|
|
|
// methods are safe for concurrent access.
|
2018-08-08 00:46:37 +00:00
|
|
|
type BoltStateDB struct {
|
2018-12-07 01:24:43 +00:00
|
|
|
stateDir string
|
|
|
|
db *boltdd.DB
|
|
|
|
logger hclog.Logger
|
2018-08-08 00:46:37 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 17:18:49 +00:00
|
|
|
// NewBoltStateDB creates or opens an existing boltdb state file or returns an
|
|
|
|
// error.
|
2018-12-07 01:24:43 +00:00
|
|
|
func NewBoltStateDB(logger hclog.Logger, stateDir string) (StateDB, error) {
|
|
|
|
fn := filepath.Join(stateDir, "state.db")
|
|
|
|
|
|
|
|
// Check to see if the DB already exists
|
|
|
|
fi, err := os.Stat(fn)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
firstRun := fi == nil
|
|
|
|
|
2019-09-24 23:28:21 +00:00
|
|
|
// Timeout to force failure when accessing a data dir that is already in use
|
|
|
|
timeout := &bolt.Options{Timeout: 5 * time.Second}
|
|
|
|
|
2018-08-08 00:46:37 +00:00
|
|
|
// Create or open the boltdb state database
|
2019-09-24 23:28:21 +00:00
|
|
|
db, err := boltdd.Open(fn, 0600, timeout)
|
|
|
|
if err == bolt.ErrTimeout {
|
|
|
|
return nil, fmt.Errorf("timed out while opening database, is another Nomad process accessing data_dir %s?", stateDir)
|
|
|
|
} else if err != nil {
|
2018-08-08 00:46:37 +00:00
|
|
|
return nil, fmt.Errorf("failed to create state database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sdb := &BoltStateDB{
|
2018-12-07 01:24:43 +00:00
|
|
|
stateDir: stateDir,
|
|
|
|
db: db,
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
|
|
|
|
// If db did not already exist, initialize metadata fields
|
|
|
|
if firstRun {
|
|
|
|
if err := sdb.init(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-08 00:46:37 +00:00
|
|
|
}
|
2018-12-07 01:24:43 +00:00
|
|
|
|
2018-08-08 00:46:37 +00:00
|
|
|
return sdb, nil
|
|
|
|
}
|
|
|
|
|
2018-10-16 22:17:36 +00:00
|
|
|
func (s *BoltStateDB) Name() string {
|
|
|
|
return "boltdb"
|
|
|
|
}
|
|
|
|
|
2018-08-08 00:46:37 +00:00
|
|
|
// GetAllAllocations gets all allocations persisted by this client and returns
|
|
|
|
// a map of alloc ids to errors for any allocations that could not be restored.
|
|
|
|
//
|
|
|
|
// If a fatal error was encountered it will be returned and the other two
|
|
|
|
// values will be nil.
|
|
|
|
func (s *BoltStateDB) GetAllAllocations() ([]*structs.Allocation, map[string]error, error) {
|
|
|
|
var allocs []*structs.Allocation
|
|
|
|
var errs map[string]error
|
2018-08-16 18:14:52 +00:00
|
|
|
err := s.db.View(func(tx *boltdd.Tx) error {
|
2018-08-09 00:06:56 +00:00
|
|
|
allocs, errs = s.getAllAllocations(tx)
|
2018-08-08 00:46:37 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// db.View itself may return an error, so still check
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return allocs, errs, nil
|
|
|
|
}
|
|
|
|
|
2018-08-09 00:06:56 +00:00
|
|
|
// allocEntry wraps values in the Allocations buckets
|
|
|
|
type allocEntry struct {
|
|
|
|
Alloc *structs.Allocation
|
|
|
|
}
|
|
|
|
|
2018-08-16 18:14:52 +00:00
|
|
|
func (s *BoltStateDB) getAllAllocations(tx *boltdd.Tx) ([]*structs.Allocation, map[string]error) {
|
2018-10-16 22:17:36 +00:00
|
|
|
allocs := []*structs.Allocation{}
|
|
|
|
errs := map[string]error{}
|
|
|
|
|
|
|
|
allocationsBkt := tx.Bucket(allocationsBucketName)
|
2018-08-09 00:06:56 +00:00
|
|
|
if allocationsBkt == nil {
|
|
|
|
// No allocs
|
2018-10-16 22:17:36 +00:00
|
|
|
return allocs, errs
|
2018-08-09 00:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a cursor for iteration.
|
2018-08-16 18:14:52 +00:00
|
|
|
c := allocationsBkt.BoltBucket().Cursor()
|
2018-08-09 00:06:56 +00:00
|
|
|
|
|
|
|
// Iterate over all the allocation buckets
|
|
|
|
for k, _ := c.First(); k != nil; k, _ = c.Next() {
|
|
|
|
allocID := string(k)
|
|
|
|
allocBkt := allocationsBkt.Bucket(k)
|
|
|
|
if allocBkt == nil {
|
|
|
|
errs[allocID] = fmt.Errorf("missing alloc bucket")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-08-22 17:18:49 +00:00
|
|
|
var ae allocEntry
|
|
|
|
if err := allocBkt.Get(allocKey, &ae); err != nil {
|
|
|
|
errs[allocID] = fmt.Errorf("failed to decode alloc: %v", err)
|
2018-08-09 00:06:56 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-01-08 22:23:56 +00:00
|
|
|
// Handle upgrade path
|
|
|
|
ae.Alloc.Canonicalize()
|
2020-01-28 14:59:05 +00:00
|
|
|
ae.Alloc.Job.Canonicalize()
|
2020-01-08 22:23:56 +00:00
|
|
|
|
2018-08-22 17:18:49 +00:00
|
|
|
allocs = append(allocs, ae.Alloc)
|
2018-08-09 00:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return allocs, errs
|
|
|
|
}
|
|
|
|
|
2018-08-08 00:46:37 +00:00
|
|
|
// PutAllocation stores an allocation or returns an error.
|
2020-10-20 20:15:37 +00:00
|
|
|
func (s *BoltStateDB) PutAllocation(alloc *structs.Allocation, opts ...WriteOption) error {
|
|
|
|
return s.updateWithOptions(opts, func(tx *boltdd.Tx) error {
|
2018-08-08 00:46:37 +00:00
|
|
|
// Retrieve the root allocations bucket
|
2018-10-16 22:17:36 +00:00
|
|
|
allocsBkt, err := tx.CreateBucketIfNotExists(allocationsBucketName)
|
2018-08-08 00:46:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the specific allocations bucket
|
|
|
|
key := []byte(alloc.ID)
|
|
|
|
allocBkt, err := allocsBkt.CreateBucketIfNotExists(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
allocState := allocEntry{
|
|
|
|
Alloc: alloc,
|
|
|
|
}
|
2018-08-16 18:14:52 +00:00
|
|
|
return allocBkt.Put(allocKey, &allocState)
|
2018-08-08 00:46:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-07 01:24:43 +00:00
|
|
|
// deployStatusEntry wraps values for DeploymentStatus keys.
|
|
|
|
type deployStatusEntry struct {
|
|
|
|
DeploymentStatus *structs.AllocDeploymentStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutDeploymentStatus stores an allocation's DeploymentStatus or returns an
|
|
|
|
// error.
|
|
|
|
func (s *BoltStateDB) PutDeploymentStatus(allocID string, ds *structs.AllocDeploymentStatus) error {
|
|
|
|
return s.db.Update(func(tx *boltdd.Tx) error {
|
|
|
|
return putDeploymentStatusImpl(tx, allocID, ds)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func putDeploymentStatusImpl(tx *boltdd.Tx, allocID string, ds *structs.AllocDeploymentStatus) error {
|
|
|
|
allocBkt, err := getAllocationBucket(tx, allocID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := deployStatusEntry{
|
|
|
|
DeploymentStatus: ds,
|
|
|
|
}
|
|
|
|
return allocBkt.Put(allocDeployStatusKey, &entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDeploymentStatus retrieves an allocation's DeploymentStatus or returns an
|
|
|
|
// error.
|
|
|
|
func (s *BoltStateDB) GetDeploymentStatus(allocID string) (*structs.AllocDeploymentStatus, error) {
|
|
|
|
var entry deployStatusEntry
|
|
|
|
|
|
|
|
err := s.db.View(func(tx *boltdd.Tx) error {
|
|
|
|
allAllocsBkt := tx.Bucket(allocationsBucketName)
|
|
|
|
if allAllocsBkt == nil {
|
|
|
|
// No state, return
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
allocBkt := allAllocsBkt.Bucket([]byte(allocID))
|
|
|
|
if allocBkt == nil {
|
|
|
|
// No state for alloc, return
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return allocBkt.Get(allocDeployStatusKey, &entry)
|
|
|
|
})
|
|
|
|
|
2018-12-19 01:44:33 +00:00
|
|
|
// It's valid for this field to be nil/missing
|
|
|
|
if boltdd.IsErrNotFound(err) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-12-07 01:24:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry.DeploymentStatus, nil
|
|
|
|
}
|
|
|
|
|
2020-10-12 17:43:04 +00:00
|
|
|
// networkStatusEntry wraps values for NetworkStatus keys.
|
|
|
|
type networkStatusEntry struct {
|
|
|
|
NetworkStatus *structs.AllocNetworkStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutDeploymentStatus stores an allocation's DeploymentStatus or returns an
|
|
|
|
// error.
|
2020-10-20 20:15:37 +00:00
|
|
|
func (s *BoltStateDB) PutNetworkStatus(allocID string, ds *structs.AllocNetworkStatus, opts ...WriteOption) error {
|
|
|
|
return s.updateWithOptions(opts, func(tx *boltdd.Tx) error {
|
2020-10-12 17:43:04 +00:00
|
|
|
return putNetworkStatusImpl(tx, allocID, ds)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func putNetworkStatusImpl(tx *boltdd.Tx, allocID string, ds *structs.AllocNetworkStatus) error {
|
|
|
|
allocBkt, err := getAllocationBucket(tx, allocID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := networkStatusEntry{
|
|
|
|
NetworkStatus: ds,
|
|
|
|
}
|
|
|
|
return allocBkt.Put(allocNetworkStatusKey, &entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNetworkStatus retrieves an allocation's NetworkStatus or returns an
|
|
|
|
// error.
|
|
|
|
func (s *BoltStateDB) GetNetworkStatus(allocID string) (*structs.AllocNetworkStatus, error) {
|
|
|
|
var entry networkStatusEntry
|
|
|
|
|
|
|
|
err := s.db.View(func(tx *boltdd.Tx) error {
|
|
|
|
allAllocsBkt := tx.Bucket(allocationsBucketName)
|
|
|
|
if allAllocsBkt == nil {
|
|
|
|
// No state, return
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
allocBkt := allAllocsBkt.Bucket([]byte(allocID))
|
|
|
|
if allocBkt == nil {
|
|
|
|
// No state for alloc, return
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return allocBkt.Get(allocNetworkStatusKey, &entry)
|
|
|
|
})
|
|
|
|
|
|
|
|
// It's valid for this field to be nil/missing
|
|
|
|
if boltdd.IsErrNotFound(err) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry.NetworkStatus, nil
|
|
|
|
}
|
|
|
|
|
2018-10-16 22:17:36 +00:00
|
|
|
// GetTaskRunnerState returns the LocalState and TaskState for a
|
|
|
|
// TaskRunner. LocalState or TaskState will be nil if they do not exist.
|
|
|
|
//
|
|
|
|
// If an error is encountered both LocalState and TaskState will be nil.
|
2018-08-08 00:46:37 +00:00
|
|
|
func (s *BoltStateDB) GetTaskRunnerState(allocID, taskName string) (*trstate.LocalState, *structs.TaskState, error) {
|
2018-10-16 22:17:36 +00:00
|
|
|
var ls *trstate.LocalState
|
|
|
|
var ts *structs.TaskState
|
2018-08-08 00:46:37 +00:00
|
|
|
|
2018-08-16 18:14:52 +00:00
|
|
|
err := s.db.View(func(tx *boltdd.Tx) error {
|
2018-10-16 22:17:36 +00:00
|
|
|
allAllocsBkt := tx.Bucket(allocationsBucketName)
|
|
|
|
if allAllocsBkt == nil {
|
|
|
|
// No state, return
|
|
|
|
return nil
|
2018-08-08 00:46:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 22:17:36 +00:00
|
|
|
allocBkt := allAllocsBkt.Bucket([]byte(allocID))
|
|
|
|
if allocBkt == nil {
|
|
|
|
// No state for alloc, return
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:01:08 +00:00
|
|
|
taskBkt := allocBkt.Bucket(taskBucketName(taskName))
|
2018-10-16 22:17:36 +00:00
|
|
|
if taskBkt == nil {
|
|
|
|
// No state for task, return
|
|
|
|
return nil
|
2018-08-08 00:46:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 22:17:36 +00:00
|
|
|
// Restore Local State if it exists
|
|
|
|
ls = &trstate.LocalState{}
|
|
|
|
if err := taskBkt.Get(taskLocalStateKey, ls); err != nil {
|
|
|
|
if !boltdd.IsErrNotFound(err) {
|
|
|
|
return fmt.Errorf("failed to read local task runner state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key not found, reset ls to nil
|
|
|
|
ls = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore Task State if it exists
|
|
|
|
ts = &structs.TaskState{}
|
|
|
|
if err := taskBkt.Get(taskStateKey, ts); err != nil {
|
|
|
|
if !boltdd.IsErrNotFound(err) {
|
|
|
|
return fmt.Errorf("failed to read task state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key not found, reset ts to nil
|
|
|
|
ts = nil
|
2018-08-08 00:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-16 22:17:36 +00:00
|
|
|
return ls, ts, nil
|
2018-08-08 00:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutTaskRunnerLocalState stores TaskRunner's LocalState or returns an error.
|
2018-10-16 22:17:36 +00:00
|
|
|
func (s *BoltStateDB) PutTaskRunnerLocalState(allocID, taskName string, val *trstate.LocalState) error {
|
2018-08-16 18:14:52 +00:00
|
|
|
return s.db.Update(func(tx *boltdd.Tx) error {
|
2018-12-07 01:24:43 +00:00
|
|
|
return putTaskRunnerLocalStateImpl(tx, allocID, taskName, val)
|
|
|
|
})
|
|
|
|
}
|
2018-08-08 00:46:37 +00:00
|
|
|
|
2018-12-07 01:24:43 +00:00
|
|
|
// putTaskRunnerLocalStateImpl stores TaskRunner's LocalState in an ongoing
|
|
|
|
// transaction or returns an error.
|
|
|
|
func putTaskRunnerLocalStateImpl(tx *boltdd.Tx, allocID, taskName string, val *trstate.LocalState) error {
|
|
|
|
taskBkt, err := getTaskBucket(tx, allocID, taskName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to retrieve allocation bucket: %v", err)
|
|
|
|
}
|
2018-08-08 00:46:37 +00:00
|
|
|
|
2018-12-07 01:24:43 +00:00
|
|
|
if err := taskBkt.Put(taskLocalStateKey, val); err != nil {
|
|
|
|
return fmt.Errorf("failed to write task_runner state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-08-08 00:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutTaskState stores a task's state or returns an error.
|
|
|
|
func (s *BoltStateDB) PutTaskState(allocID, taskName string, state *structs.TaskState) error {
|
2018-08-16 18:14:52 +00:00
|
|
|
return s.db.Update(func(tx *boltdd.Tx) error {
|
2018-12-07 01:24:43 +00:00
|
|
|
return putTaskStateImpl(tx, allocID, taskName, state)
|
2018-08-15 21:03:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-07 01:24:43 +00:00
|
|
|
// putTaskStateImpl stores a task's state in an ongoing transaction or returns
|
|
|
|
// an error.
|
|
|
|
func putTaskStateImpl(tx *boltdd.Tx, allocID, taskName string, state *structs.TaskState) error {
|
|
|
|
taskBkt, err := getTaskBucket(tx, allocID, taskName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to retrieve allocation bucket: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return taskBkt.Put(taskStateKey, state)
|
|
|
|
}
|
|
|
|
|
2018-08-15 21:03:18 +00:00
|
|
|
// DeleteTaskBucket is used to delete a task bucket if it exists.
|
|
|
|
func (s *BoltStateDB) DeleteTaskBucket(allocID, taskName string) error {
|
2018-08-16 18:14:52 +00:00
|
|
|
return s.db.Update(func(tx *boltdd.Tx) error {
|
2018-08-15 21:03:18 +00:00
|
|
|
// Retrieve the root allocations bucket
|
2018-10-16 22:17:36 +00:00
|
|
|
allocations := tx.Bucket(allocationsBucketName)
|
2018-08-15 21:03:18 +00:00
|
|
|
if allocations == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the specific allocations bucket
|
|
|
|
alloc := allocations.Bucket([]byte(allocID))
|
|
|
|
if alloc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the bucket exists
|
2018-12-05 01:01:08 +00:00
|
|
|
key := taskBucketName(taskName)
|
2018-08-16 18:14:52 +00:00
|
|
|
return alloc.DeleteBucket(key)
|
2018-08-15 21:03:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteAllocationBucket is used to delete an allocation bucket if it exists.
|
2020-10-20 20:15:37 +00:00
|
|
|
func (s *BoltStateDB) DeleteAllocationBucket(allocID string, opts ...WriteOption) error {
|
|
|
|
return s.updateWithOptions(opts, func(tx *boltdd.Tx) error {
|
2018-08-15 21:03:18 +00:00
|
|
|
// Retrieve the root allocations bucket
|
2018-10-16 22:17:36 +00:00
|
|
|
allocations := tx.Bucket(allocationsBucketName)
|
2018-08-15 21:03:18 +00:00
|
|
|
if allocations == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
key := []byte(allocID)
|
2018-08-16 18:14:52 +00:00
|
|
|
return allocations.DeleteBucket(key)
|
2018-08-08 00:46:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close releases all database resources and unlocks the database file on disk.
|
|
|
|
// All transactions must be closed before closing the database.
|
|
|
|
func (s *BoltStateDB) Close() error {
|
|
|
|
return s.db.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAllocationBucket returns the bucket used to persist state about a
|
2017-05-01 23:18:03 +00:00
|
|
|
// particular allocation. If the root allocation bucket or the specific
|
2017-05-02 20:31:56 +00:00
|
|
|
// allocation bucket doesn't exist, it will be created as long as the
|
|
|
|
// transaction is writable.
|
2018-08-16 18:14:52 +00:00
|
|
|
func getAllocationBucket(tx *boltdd.Tx, allocID string) (*boltdd.Bucket, error) {
|
2017-05-02 20:31:56 +00:00
|
|
|
var err error
|
|
|
|
w := tx.Writable()
|
2017-05-01 23:18:03 +00:00
|
|
|
|
|
|
|
// Retrieve the root allocations bucket
|
2018-10-16 22:17:36 +00:00
|
|
|
allocations := tx.Bucket(allocationsBucketName)
|
2017-05-02 20:31:56 +00:00
|
|
|
if allocations == nil {
|
|
|
|
if !w {
|
|
|
|
return nil, fmt.Errorf("Allocations bucket doesn't exist and transaction is not writable")
|
|
|
|
}
|
|
|
|
|
2018-10-16 22:17:36 +00:00
|
|
|
allocations, err = tx.CreateBucketIfNotExists(allocationsBucketName)
|
2017-05-02 20:31:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-01 23:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the specific allocations bucket
|
2017-05-02 20:31:56 +00:00
|
|
|
key := []byte(allocID)
|
|
|
|
alloc := allocations.Bucket(key)
|
|
|
|
if alloc == nil {
|
|
|
|
if !w {
|
|
|
|
return nil, fmt.Errorf("Allocation bucket doesn't exist and transaction is not writable")
|
|
|
|
}
|
|
|
|
|
|
|
|
alloc, err = allocations.CreateBucket(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-01 23:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return alloc, nil
|
|
|
|
}
|
|
|
|
|
2018-08-08 00:46:37 +00:00
|
|
|
// getTaskBucket returns the bucket used to persist state about a
|
2017-05-01 23:18:03 +00:00
|
|
|
// particular task. If the root allocation bucket, the specific
|
2017-05-02 20:31:56 +00:00
|
|
|
// allocation or task bucket doesn't exist, they will be created as long as the
|
|
|
|
// transaction is writable.
|
2018-08-16 18:14:52 +00:00
|
|
|
func getTaskBucket(tx *boltdd.Tx, allocID, taskName string) (*boltdd.Bucket, error) {
|
2018-08-08 00:46:37 +00:00
|
|
|
alloc, err := getAllocationBucket(tx, allocID)
|
2017-05-02 20:31:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the specific task bucket
|
|
|
|
w := tx.Writable()
|
2018-12-05 01:01:08 +00:00
|
|
|
key := taskBucketName(taskName)
|
2017-05-02 20:31:56 +00:00
|
|
|
task := alloc.Bucket(key)
|
|
|
|
if task == nil {
|
|
|
|
if !w {
|
|
|
|
return nil, fmt.Errorf("Task bucket doesn't exist and transaction is not writable")
|
|
|
|
}
|
|
|
|
|
|
|
|
task, err = alloc.CreateBucket(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return task, nil
|
|
|
|
}
|
2018-09-28 17:09:01 +00:00
|
|
|
|
|
|
|
// PutDevicePluginState stores the device manager's plugin state or returns an
|
|
|
|
// error.
|
2018-11-01 01:00:30 +00:00
|
|
|
func (s *BoltStateDB) PutDevicePluginState(ps *dmstate.PluginState) error {
|
2018-09-28 17:09:01 +00:00
|
|
|
return s.db.Update(func(tx *boltdd.Tx) error {
|
|
|
|
// Retrieve the root device manager bucket
|
|
|
|
devBkt, err := tx.CreateBucketIfNotExists(devManagerBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-28 03:42:22 +00:00
|
|
|
return devBkt.Put(managerPluginStateKey, ps)
|
2018-09-28 17:09:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDevicePluginState stores the device manager's plugin state or returns an
|
|
|
|
// error.
|
2018-11-01 01:00:30 +00:00
|
|
|
func (s *BoltStateDB) GetDevicePluginState() (*dmstate.PluginState, error) {
|
|
|
|
var ps *dmstate.PluginState
|
2018-09-28 17:09:01 +00:00
|
|
|
|
|
|
|
err := s.db.View(func(tx *boltdd.Tx) error {
|
|
|
|
devBkt := tx.Bucket(devManagerBucket)
|
|
|
|
if devBkt == nil {
|
|
|
|
// No state, return
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore Plugin State if it exists
|
2018-11-01 01:00:30 +00:00
|
|
|
ps = &dmstate.PluginState{}
|
2018-11-28 03:42:22 +00:00
|
|
|
if err := devBkt.Get(managerPluginStateKey, ps); err != nil {
|
2018-09-28 17:09:01 +00:00
|
|
|
if !boltdd.IsErrNotFound(err) {
|
|
|
|
return fmt.Errorf("failed to read device manager plugin state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key not found, reset ps to nil
|
|
|
|
ps = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ps, nil
|
|
|
|
}
|
2018-11-28 03:42:22 +00:00
|
|
|
|
|
|
|
// PutDriverPluginState stores the driver manager's plugin state or returns an
|
|
|
|
// error.
|
|
|
|
func (s *BoltStateDB) PutDriverPluginState(ps *driverstate.PluginState) error {
|
|
|
|
return s.db.Update(func(tx *boltdd.Tx) error {
|
|
|
|
// Retrieve the root driver manager bucket
|
|
|
|
driverBkt, err := tx.CreateBucketIfNotExists(driverManagerBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return driverBkt.Put(managerPluginStateKey, ps)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDriverPluginState stores the driver manager's plugin state or returns an
|
|
|
|
// error.
|
|
|
|
func (s *BoltStateDB) GetDriverPluginState() (*driverstate.PluginState, error) {
|
|
|
|
var ps *driverstate.PluginState
|
|
|
|
|
|
|
|
err := s.db.View(func(tx *boltdd.Tx) error {
|
|
|
|
driverBkt := tx.Bucket(driverManagerBucket)
|
|
|
|
if driverBkt == nil {
|
|
|
|
// No state, return
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore Plugin State if it exists
|
|
|
|
ps = &driverstate.PluginState{}
|
|
|
|
if err := driverBkt.Get(managerPluginStateKey, ps); err != nil {
|
|
|
|
if !boltdd.IsErrNotFound(err) {
|
|
|
|
return fmt.Errorf("failed to read driver manager plugin state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key not found, reset ps to nil
|
|
|
|
ps = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ps, nil
|
|
|
|
}
|
2018-12-07 01:24:43 +00:00
|
|
|
|
2020-03-12 20:24:58 +00:00
|
|
|
// PutDynamicPluginRegistryState stores the dynamic plugin registry's
|
|
|
|
// state or returns an error.
|
|
|
|
func (s *BoltStateDB) PutDynamicPluginRegistryState(ps *dynamicplugins.RegistryState) error {
|
|
|
|
return s.db.Update(func(tx *boltdd.Tx) error {
|
|
|
|
// Retrieve the root dynamic plugin manager bucket
|
|
|
|
dynamicBkt, err := tx.CreateBucketIfNotExists(dynamicPluginBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return dynamicBkt.Put(registryStateKey, ps)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDynamicPluginRegistryState stores the dynamic plugin registry's
|
|
|
|
// registry state or returns an error.
|
|
|
|
func (s *BoltStateDB) GetDynamicPluginRegistryState() (*dynamicplugins.RegistryState, error) {
|
|
|
|
var ps *dynamicplugins.RegistryState
|
|
|
|
|
|
|
|
err := s.db.View(func(tx *boltdd.Tx) error {
|
|
|
|
dynamicBkt := tx.Bucket(dynamicPluginBucket)
|
|
|
|
if dynamicBkt == nil {
|
|
|
|
// No state, return
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore Plugin State if it exists
|
|
|
|
ps = &dynamicplugins.RegistryState{}
|
|
|
|
if err := dynamicBkt.Get(registryStateKey, ps); err != nil {
|
|
|
|
if !boltdd.IsErrNotFound(err) {
|
|
|
|
return fmt.Errorf("failed to read dynamic plugin registry state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key not found, reset ps to nil
|
|
|
|
ps = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ps, nil
|
|
|
|
}
|
|
|
|
|
2018-12-07 01:24:43 +00:00
|
|
|
// init initializes metadata entries in a newly created state database.
|
|
|
|
func (s *BoltStateDB) init() error {
|
|
|
|
return s.db.Update(func(tx *boltdd.Tx) error {
|
|
|
|
return addMeta(tx.BoltTx())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-20 20:15:37 +00:00
|
|
|
// updateWithOptions enables adjustments to db.Update operation, including Batch mode.
|
|
|
|
func (s *BoltStateDB) updateWithOptions(opts []WriteOption, updateFn func(tx *boltdd.Tx) error) error {
|
|
|
|
writeOpts := mergeWriteOptions(opts)
|
|
|
|
|
|
|
|
if writeOpts.BatchMode {
|
|
|
|
// In Batch mode, BoltDB opportunistically combines multiple concurrent writes into one or
|
|
|
|
// several transactions. See boltdb.Batch() documentation for details.
|
|
|
|
return s.db.Batch(updateFn)
|
|
|
|
} else {
|
|
|
|
return s.db.Update(updateFn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-07 01:24:43 +00:00
|
|
|
// Upgrade bolt state db from 0.8 schema to 0.9 schema. Noop if already using
|
|
|
|
// 0.9 schema. Creates a backup before upgrading.
|
|
|
|
func (s *BoltStateDB) Upgrade() error {
|
|
|
|
// Check to see if the underlying DB needs upgrading.
|
|
|
|
upgrade, err := NeedsUpgrade(s.db.BoltDB())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !upgrade {
|
|
|
|
// No upgrade needed!
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upgraded needed. Backup the boltdb first.
|
|
|
|
backupFileName := filepath.Join(s.stateDir, "state.db.backup")
|
|
|
|
if err := backupDB(s.db.BoltDB(), backupFileName); err != nil {
|
|
|
|
return fmt.Errorf("error backing up state db: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the upgrade
|
|
|
|
if err := s.db.Update(func(tx *boltdd.Tx) error {
|
|
|
|
if err := UpgradeAllocs(s.logger, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add standard metadata
|
|
|
|
if err := addMeta(tx.BoltTx()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the time the upgrade was done
|
|
|
|
bkt, err := tx.CreateBucketIfNotExists(metaBucketName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bkt.Put(metaUpgradedKey, time.Now().Format(time.RFC3339))
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.logger.Info("successfully upgraded state")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DB allows access to the underlying BoltDB for testing purposes.
|
|
|
|
func (s *BoltStateDB) DB() *boltdd.DB {
|
|
|
|
return s.db
|
|
|
|
}
|