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