github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/client/state/interface.go (about)

     1  package state
     2  
     3  import (
     4  	"github.com/hashicorp/nomad/client/allocrunner/taskrunner/state"
     5  	dmstate "github.com/hashicorp/nomad/client/devicemanager/state"
     6  	"github.com/hashicorp/nomad/client/dynamicplugins"
     7  	driverstate "github.com/hashicorp/nomad/client/pluginmanager/drivermanager/state"
     8  	"github.com/hashicorp/nomad/nomad/structs"
     9  )
    10  
    11  // StateDB implementations store and load Nomad client state.
    12  type StateDB interface {
    13  	// Name of implementation.
    14  	Name() string
    15  
    16  	// Upgrade ensures the layout of the database is at the latest version
    17  	// or returns an error. Corrupt data will be dropped when possible.
    18  	// Errors should be considered critical and unrecoverable.
    19  	Upgrade() error
    20  
    21  	// GetAllAllocations returns all valid allocations and a map of
    22  	// allocation IDs to retrieval errors.
    23  	//
    24  	// If a single error is returned then both allocations and the map will be nil.
    25  	GetAllAllocations() ([]*structs.Allocation, map[string]error, error)
    26  
    27  	// PutAllocation stores an allocation or returns an error if it could
    28  	// not be stored.
    29  	PutAllocation(*structs.Allocation, ...WriteOption) error
    30  
    31  	// Get/Put DeploymentStatus get and put the allocation's deployment
    32  	// status. It may be nil.
    33  	GetDeploymentStatus(allocID string) (*structs.AllocDeploymentStatus, error)
    34  	PutDeploymentStatus(allocID string, ds *structs.AllocDeploymentStatus) error
    35  
    36  	// Get/Put NetworkStatus get and put the allocation's network
    37  	// status. It may be nil.
    38  	GetNetworkStatus(allocID string) (*structs.AllocNetworkStatus, error)
    39  	PutNetworkStatus(allocID string, ns *structs.AllocNetworkStatus, opts ...WriteOption) error
    40  
    41  	// GetTaskRunnerState returns the LocalState and TaskState for a
    42  	// TaskRunner. Either state may be nil if it is not found, but if an
    43  	// error is encountered only the error will be non-nil.
    44  	GetTaskRunnerState(allocID, taskName string) (*state.LocalState, *structs.TaskState, error)
    45  
    46  	// PutTaskRunnerLocalTask stores the LocalState for a TaskRunner or
    47  	// returns an error.
    48  	PutTaskRunnerLocalState(allocID, taskName string, val *state.LocalState) error
    49  
    50  	// PutTaskState stores the TaskState for a TaskRunner or returns an
    51  	// error.
    52  	PutTaskState(allocID, taskName string, state *structs.TaskState) error
    53  
    54  	// DeleteTaskBucket deletes a task's state bucket if it exists. No
    55  	// error is returned if it does not exist.
    56  	DeleteTaskBucket(allocID, taskName string) error
    57  
    58  	// DeleteAllocationBucket deletes an allocation's state bucket if it
    59  	// exists. No error is returned if it does not exist.
    60  	DeleteAllocationBucket(allocID string, opts ...WriteOption) error
    61  
    62  	// GetDevicePluginState is used to retrieve the device manager's plugin
    63  	// state.
    64  	GetDevicePluginState() (*dmstate.PluginState, error)
    65  
    66  	// PutDevicePluginState is used to store the device manager's plugin
    67  	// state.
    68  	PutDevicePluginState(state *dmstate.PluginState) error
    69  
    70  	// GetDriverPluginState is used to retrieve the driver manager's plugin
    71  	// state.
    72  	GetDriverPluginState() (*driverstate.PluginState, error)
    73  
    74  	// PutDriverPluginState is used to store the driver manager's plugin
    75  	// state.
    76  	PutDriverPluginState(state *driverstate.PluginState) error
    77  
    78  	// GetDynamicPluginRegistryState is used to retrieve a dynamic plugin manager's state.
    79  	GetDynamicPluginRegistryState() (*dynamicplugins.RegistryState, error)
    80  
    81  	// PutDynamicPluginRegistryState is used to store the dynamic plugin manager's state.
    82  	PutDynamicPluginRegistryState(state *dynamicplugins.RegistryState) error
    83  
    84  	// Close the database. Unsafe for further use after calling regardless
    85  	// of return value.
    86  	Close() error
    87  }
    88  
    89  // WriteOptions adjusts the way the data is persisted by the StateDB above. Default is
    90  // zero/false values for all fields. To provide different values, use With* functions
    91  // below, like this: statedb.PutAllocation(alloc, WithBatchMode())
    92  type WriteOptions struct {
    93  	// In Batch mode, concurrent writes (Put* and Delete* operations above) are
    94  	// coalesced into a single transaction, increasing write performance. To benefit
    95  	// from this mode, writes must happen concurrently in goroutines, as every write
    96  	// request still waits for the shared transaction to commit before returning.
    97  	// See https://github.com/boltdb/bolt#batch-read-write-transactions for details.
    98  	// This mode is only supported for BoltDB state backend and is ignored in other backends.
    99  	BatchMode bool
   100  }
   101  
   102  // WriteOption is a function that modifies WriteOptions struct above.
   103  type WriteOption func(*WriteOptions)
   104  
   105  // mergeWriteOptions creates a final WriteOptions struct to be used by the write methods above
   106  // from a list of WriteOption-s provided as variadic arguments.
   107  func mergeWriteOptions(opts []WriteOption) WriteOptions {
   108  	writeOptions := WriteOptions{} // Default WriteOptions is zero value.
   109  	for _, opt := range opts {
   110  		opt(&writeOptions)
   111  	}
   112  	return writeOptions
   113  }
   114  
   115  // Enable Batch mode for write requests (Put* and Delete* operations above).
   116  func WithBatchMode() WriteOption {
   117  	return func(s *WriteOptions) {
   118  		s.BatchMode = true
   119  	}
   120  }