state

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 1, 2021 License: MPL-2.0 Imports: 44 Imported by: 0

Documentation

Overview

Package state manages the state that the singleprocess server has, providing operations to mutate that state safely as needed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IndexTime

type IndexTime struct {
	Field string

	// Asc if true will index with ascending order. Usually you want to
	// find the most recent data so this is false by default.
	Asc bool
}

IndexTime indexes a time.Time field of a struct. TODO(mitchellh): test

func (*IndexTime) FromArgs

func (idx *IndexTime) FromArgs(args ...interface{}) ([]byte, error)

func (*IndexTime) FromObject

func (idx *IndexTime) FromObject(obj interface{}) (bool, []byte, error)

type Instance

type Instance struct {
	Id           string
	DeploymentId string
	Project      string
	Application  string
	Workspace    string
	LogBuffer    *logbuffer.Buffer
	Type         pb.Instance_Type
}

func (*Instance) Proto

func (i *Instance) Proto() *pb.Instance

type InstanceExec

type InstanceExec struct {
	Id         int64
	InstanceId string

	Args []string
	Pty  *pb.ExecStreamRequest_PTY

	ClientEventCh     <-chan *pb.ExecStreamRequest
	EntrypointEventCh chan<- *pb.EntrypointExecRequest
	Connected         uint32

	// This is the context that the client side is running inside.
	// It is used by the entrypoint side to detect if the client is still
	// around or not.
	Context context.Context
}

type InstanceLogs added in v0.3.0

type InstanceLogs struct {
	Id         int64
	InstanceId string

	LogBuffer *logbuffer.Buffer
}

InstanceLogs is a value that can be created to assist in coordination log writers and readers. It is a lighter weight version of an Instance used to manage virtual CEBs sending logs.

type Job

type Job struct {
	// Full job structure.
	*pb.Job

	// OutputBuffer is the terminal output for this job. This is a buffer
	// that may not contain the full amount of output depending on the
	// time of connection.
	OutputBuffer *logbuffer.Buffer

	// Blocked is true if this job is blocked on another job for the same
	// project/app/workspace.
	Blocked bool
}

Job is the exported structure that is returned for most state APIs and gives callers access to more information than the pure job structure.

type ListOperationOption

type ListOperationOption func(opts *listOperationsOptions)

ListOperationOption is an exported type to set configuration for listing operations.

func ListWithOrder

func ListWithOrder(f *pb.OperationOrder) ListOperationOption

ListWithOrder sets ordering on the list operation.

func ListWithPhysicalState

func ListWithPhysicalState(f pb.Operation_PhysicalState) ListOperationOption

ListWithPhysicalState sets ordering on the list operation.

func ListWithStatusFilter

func ListWithStatusFilter(f ...*pb.StatusFilter) ListOperationOption

ListWithStatusFilter sets a status filter.

func ListWithWatchSet added in v0.3.0

func ListWithWatchSet(ws memdb.WatchSet) ListOperationOption

ListWithWatchSet registers watches for the listing, allowing the watcher to detect if new items are added.

func ListWithWorkspace

func ListWithWorkspace(f *pb.Ref_Workspace) ListOperationOption

ListWithWorkspace sets ordering on the list operation.

type State

type State struct {
	// contains filtered or unexported fields
}

State is the primary API for state mutation for the server.

func New

func New(log hclog.Logger, db *bolt.DB) (*State, error)

New initializes a new State store.

func TestState

func TestState(t testing.T) *State

TestState returns an initialized State for testing.

func TestStateReinit

func TestStateReinit(t testing.T, s *State) *State

TestStateReinit reinitializes the state by pretending to restart the server with the database associated with this state. This can be used to test index init logic.

This safely copies the entire DB so the old state can continue running with zero impact.

func TestStateRestart added in v0.2.0

func TestStateRestart(t testing.T, s *State) (*State, error)

TestStateRestart closes the given state and restarts it against the same DB file. Unlike TestStateReinit, this does not copy the data and the old state is no longer usable.

func (*State) AppDelete

func (s *State) AppDelete(ref *pb.Ref_Application) error

AppDelete deletes an application from a project. This will also delete all the operations associated with this application.

func (*State) AppGet

func (s *State) AppGet(ref *pb.Ref_Application) (*pb.Application, error)

AppGet retrieves the application..

func (*State) AppPut

func (s *State) AppPut(app *pb.Application) (*pb.Application, error)

AppPut creates or updates the application.

func (*State) ArtifactGet

func (s *State) ArtifactGet(ref *pb.Ref_Operation) (*pb.PushedArtifact, error)

ArtifactGet gets a artifact by ref.

func (*State) ArtifactLatest

func (s *State) ArtifactLatest(
	ref *pb.Ref_Application,
	ws *pb.Ref_Workspace,
) (*pb.PushedArtifact, error)

ArtifactLatest gets the latest artifact that was completed successfully.

func (*State) ArtifactList

func (s *State) ArtifactList(
	ref *pb.Ref_Application,
	opts ...ListOperationOption,
) ([]*pb.PushedArtifact, error)

func (*State) ArtifactPut

func (s *State) ArtifactPut(update bool, b *pb.PushedArtifact) error

ArtifactPut inserts or updates a artifact record.

func (*State) BuildGet

func (s *State) BuildGet(ref *pb.Ref_Operation) (*pb.Build, error)

BuildGet gets a build by ref.

func (*State) BuildLatest

func (s *State) BuildLatest(
	ref *pb.Ref_Application,
	ws *pb.Ref_Workspace,
) (*pb.Build, error)

BuildLatest gets the latest build that was completed successfully.

func (*State) BuildList

func (s *State) BuildList(
	ref *pb.Ref_Application,
	opts ...ListOperationOption,
) ([]*pb.Build, error)

func (*State) BuildPut

func (s *State) BuildPut(update bool, b *pb.Build) error

BuildPut inserts or updates a build record.

func (*State) CalculateInstanceExecByDeployment added in v0.3.0

func (s *State) CalculateInstanceExecByDeployment(did string) (*Instance, error)

CalculateInstanceExecByDeployment considers all the instances registered to the given deployment and finds the one that is least loaded. If there are no instances, returns a ResourceExhausted error. Calls to this function in quick succession will return could return the same instance, which is why a simple random sampling is done on all prospective instances.

func (*State) Close

func (s *State) Close() error

Close should be called to gracefully close any resources.

func (*State) ConfigGet

func (s *State) ConfigGet(req *pb.ConfigGetRequest) ([]*pb.ConfigVar, error)

ConfigGet gets all the configuration for the given request.

func (*State) ConfigGetWatch

func (s *State) ConfigGetWatch(req *pb.ConfigGetRequest, ws memdb.WatchSet) ([]*pb.ConfigVar, error)

ConfigGetWatch gets all the configuration for the given request. If a non-nil WatchSet is given, this can be watched for potential changes in the config.

func (*State) ConfigSet

func (s *State) ConfigSet(vs ...*pb.ConfigVar) error

ConfigSet writes a configuration variable to the data store.

func (*State) ConfigSourceGet added in v0.2.0

func (s *State) ConfigSourceGet(req *pb.GetConfigSourceRequest) ([]*pb.ConfigSource, error)

ConfigSourceGet gets all the configuration sources for the given request.

func (*State) ConfigSourceGetWatch added in v0.2.0

func (s *State) ConfigSourceGetWatch(req *pb.GetConfigSourceRequest, ws memdb.WatchSet) ([]*pb.ConfigSource, error)

ConfigSourceGetWatch gets all the configuration sources for the given request. If a non-nil WatchSet is given, this can be watched for potential changes in the config source settings.

func (*State) ConfigSourceSet added in v0.2.0

func (s *State) ConfigSourceSet(vs ...*pb.ConfigSource) error

ConfigSourceSet writes a set of config source values to the database.

func (*State) CreateSnapshot added in v0.2.0

func (s *State) CreateSnapshot(w io.Writer) error

CreateSnapshot creates a database snapshot and writes it to the given writer.

This will NOT buffer data to w, so you should wrap w in a bufio.Writer if you want buffering.

func (*State) DeploymentGet

func (s *State) DeploymentGet(ref *pb.Ref_Operation) (*pb.Deployment, error)

DeploymentGet gets a deployment by ref.

func (*State) DeploymentLatest

func (s *State) DeploymentLatest(
	ref *pb.Ref_Application,
	ws *pb.Ref_Workspace,
) (*pb.Deployment, error)

DeploymentLatest gets the latest deployment that was completed successfully.

func (*State) DeploymentList

func (s *State) DeploymentList(
	ref *pb.Ref_Application,
	opts ...ListOperationOption,
) ([]*pb.Deployment, error)

func (*State) DeploymentPut

func (s *State) DeploymentPut(update bool, b *pb.Deployment) error

DeploymentPut inserts or updates a deployment record.

func (*State) GetFileChangeSignal added in v0.4.0

func (s *State) GetFileChangeSignal(scope *pb.Ref_Application) (string, error)

GetFileChangeSignal checks the metadata for the given application and its project, returning the value of FileChangeSignal that is most relevent.

func (*State) HMACKeyCreateIfNotExist

func (s *State) HMACKeyCreateIfNotExist(id string, size int) (*pb.HMACKey, error)

HMACKeyCreateIfNotExist creates a new HMAC key with the given ID and size. If a key with the given ID exists already it will be returned.

func (*State) HMACKeyEmpty

func (s *State) HMACKeyEmpty() bool

HMACKeyEmpty returns true if no HMAC keys have been created. This will be true only when the server is in a bootstrap state.

func (*State) HMACKeyGet

func (s *State) HMACKeyGet(id string) (*pb.HMACKey, error)

HMACKeyGet gets an HMAC key by ID. This will return a nil value if it doesn't exist.

func (*State) InstanceById

func (s *State) InstanceById(id string) (*Instance, error)

func (*State) InstanceCreate

func (s *State) InstanceCreate(rec *Instance) error

func (*State) InstanceDelete

func (s *State) InstanceDelete(id string) error

func (*State) InstanceExecById

func (s *State) InstanceExecById(id int64) (*InstanceExec, error)

func (*State) InstanceExecCreateByDeployment

func (s *State) InstanceExecCreateByDeployment(did string, exec *InstanceExec) error

func (*State) InstanceExecCreateByTargetedInstance added in v0.3.0

func (s *State) InstanceExecCreateByTargetedInstance(id string, exec *InstanceExec) error

func (*State) InstanceExecCreateForVirtualInstance added in v0.3.0

func (s *State) InstanceExecCreateForVirtualInstance(ctx context.Context, id string, exec *InstanceExec) error

InstanceExecCreateForVirtualInstance registers the given InstanceExec record on the instance specified. The instance does not yet have to be known (as it may not yet have connected to the server) so this code will use memdb watchers to detect the instance as it connects and then register the exec.

func (*State) InstanceExecDelete

func (s *State) InstanceExecDelete(id int64) error

func (*State) InstanceExecListByInstanceId

func (s *State) InstanceExecListByInstanceId(id string, ws memdb.WatchSet) ([]*InstanceExec, error)

func (*State) InstanceLogsById added in v0.3.0

func (s *State) InstanceLogsById(id int64) (*InstanceLogs, error)

func (*State) InstanceLogsByInstanceId added in v0.3.0

func (s *State) InstanceLogsByInstanceId(id string) (*InstanceLogs, error)

func (*State) InstanceLogsCreate added in v0.3.0

func (s *State) InstanceLogsCreate(id string, logs *InstanceLogs) error

func (*State) InstanceLogsDelete added in v0.3.0

func (s *State) InstanceLogsDelete(id int64) error

func (*State) InstanceLogsListByInstanceId added in v0.3.0

func (s *State) InstanceLogsListByInstanceId(id string, ws memdb.WatchSet) ([]*InstanceLogs, error)

func (*State) InstancesByApp

func (s *State) InstancesByApp(
	ref *pb.Ref_Application,
	refws *pb.Ref_Workspace,
	ws memdb.WatchSet,
) ([]*Instance, error)

func (*State) InstancesByDeployment

func (s *State) InstancesByDeployment(id string, ws memdb.WatchSet) ([]*Instance, error)

func (*State) JobAck

func (s *State) JobAck(id string, ack bool) (*Job, error)

JobAck acknowledges that a job has been accepted or rejected by the runner. If ack is false, then this will move the job back to the queued state and be eligible for assignment.

func (*State) JobAssignForRunner

func (s *State) JobAssignForRunner(ctx context.Context, r *pb.Runner) (*Job, error)

JobAssignForRunner will wait for and assign a job to a specific runner. This will automatically evaluate any conditions that the runner and/or job may have on assignability.

The assigned job is put into a "waiting" state until the runner acks the assignment which can be set with JobAck.

If ctx is provided and assignment has to block waiting for new jobs, this will cancel when the context is done.

func (*State) JobById

func (s *State) JobById(id string, ws memdb.WatchSet) (*Job, error)

JobById looks up a job by ID. The returned Job will be a deep copy of the job so it is safe to read/write. If the job can't be found, a nil result with no error is returned.

func (*State) JobCancel

func (s *State) JobCancel(id string, force bool) error

JobCancel marks a job as cancelled. This will set the internal state and request the cancel but if the job is running then it is up to downstream to listen for and react to Job changes for cancellation.

func (*State) JobComplete

func (s *State) JobComplete(id string, result *pb.Job_Result, cerr error) error

JobComplete marks a running job as complete. If an error is given, the job is marked as failed (a completed state). If no error is given, the job is marked as successful.

func (*State) JobCreate

func (s *State) JobCreate(jobpb *pb.Job) error

JobCreate queues the given job.

func (*State) JobExpire

func (s *State) JobExpire(id string) error

JobExpire expires a job. This will cancel the job if it is still queued.

func (*State) JobHeartbeat

func (s *State) JobHeartbeat(id string) error

JobHeartbeat resets the heartbeat timer for a running job. If the job is not currently running this does nothing, it will not return an error. If the job doesn't exist then this will return an error.

func (*State) JobIsAssignable

func (s *State) JobIsAssignable(ctx context.Context, jobpb *pb.Job) (bool, error)

JobIsAssignable returns whether there is a registered runner that meets the requirements to run this job.

If this returns true, the job if queued should eventually be assigned successfully to a runner. An assignable result does NOT mean that it will be in queue a short amount of time.

Note the result is a point-in-time result. If the only candidate runners deregister between this returning true and queueing, the job may still sit in a queue indefinitely.

func (*State) JobList

func (s *State) JobList() ([]*pb.Job, error)

JobList returns the list of jobs.

func (*State) JobUpdateRef added in v0.3.0

func (s *State) JobUpdateRef(id string, ref *pb.Job_DataSource_Ref) error

JobUpdateRef sets the data_source_ref field for a job. This job can be in any state.

func (*State) ProjectDelete

func (s *State) ProjectDelete(ref *pb.Ref_Project) error

ProjectDelete deletes a project by reference. This is a complete data delete. This will delete all operations associated with this project as well.

func (*State) ProjectGet

func (s *State) ProjectGet(ref *pb.Ref_Project) (*pb.Project, error)

ProjectGet gets a project by reference.

func (*State) ProjectList

func (s *State) ProjectList() ([]*pb.Ref_Project, error)

ProjectList returns the list of projects.

func (*State) ProjectListWorkspaces added in v0.3.0

func (s *State) ProjectListWorkspaces(ref *pb.Ref_Project) ([]*pb.Workspace_Project, error)

ProjectListWorkspaces returns the list of workspaces that a project is in.

func (*State) ProjectPollComplete added in v0.3.0

func (s *State) ProjectPollComplete(p *pb.Project, t time.Time) error

ProjectPollComplete sets the next poll time for the given project to the time "t" plus the interval time for the project.

func (*State) ProjectPollPeek added in v0.3.0

func (s *State) ProjectPollPeek(ws memdb.WatchSet) (*pb.Project, time.Time, error)

ProjectPollPeek returns the next project that should be polled. This will return (nil,nil) if there are no projects to poll currently.

This is a "peek" operation so it does not update the project's next poll time. Therefore, calling this multiple times should return the same result unless a function like ProjectPollComplete is called.

If ws is non-nil, the WatchSet can be watched for any changes to projects to poll. This can be watched, for example, to detect when projects to poll are added. This is important functionality since callers may be sleeping on a deadline for awhile when a new project is inserted to poll immediately.

func (*State) ProjectPut

func (s *State) ProjectPut(p *pb.Project) error

ProjectPut creates or updates the given project.

Application changes will be ignored, you must use the Application APIs.

func (*State) ProjectUpdateDataRef added in v0.3.0

func (s *State) ProjectUpdateDataRef(
	ref *pb.Ref_Project,
	ws *pb.Ref_Workspace,
	dataRef *pb.Job_DataSource_Ref,
) error

ProjectUpdateDataRef updates the latest data ref used for a project. This data is available via the APIs for querying workspaces.

func (*State) Prune added in v0.3.0

func (s *State) Prune() error

Prune should be called in a on a regular interval to allow State to prune out old data.

func (*State) ReleaseGet

func (s *State) ReleaseGet(ref *pb.Ref_Operation) (*pb.Release, error)

ReleaseGet gets a release by ref.

func (*State) ReleaseLatest

func (s *State) ReleaseLatest(
	ref *pb.Ref_Application,
	ws *pb.Ref_Workspace,
) (*pb.Release, error)

ReleaseLatest gets the latest release that was completed successfully.

func (*State) ReleaseList

func (s *State) ReleaseList(
	ref *pb.Ref_Application,
	opts ...ListOperationOption,
) ([]*pb.Release, error)

func (*State) ReleasePut

func (s *State) ReleasePut(update bool, b *pb.Release) error

ReleasePut inserts or updates a release record.

func (*State) RunnerById

func (s *State) RunnerById(id string) (*pb.Runner, error)

func (*State) RunnerCreate

func (s *State) RunnerCreate(r *pb.Runner) error

func (*State) RunnerDelete

func (s *State) RunnerDelete(id string) error

func (*State) ServerConfigGet

func (s *State) ServerConfigGet() (*pb.ServerConfig, error)

ServerConfigGet gets the server configuration.

func (*State) ServerConfigSet

func (s *State) ServerConfigSet(c *pb.ServerConfig) error

ServerConfigSet writes the server configuration.

func (*State) ServerIdGet

func (s *State) ServerIdGet() (string, error)

ServerIdGet gets the server ID.

func (*State) ServerIdSet

func (s *State) ServerIdSet(id string) error

ServerIdSet writes the server ID.

func (*State) ServerURLTokenGet added in v0.3.0

func (s *State) ServerURLTokenGet() (string, error)

ServerURLTokenGet gets the server URL token.

func (*State) ServerURLTokenSet added in v0.3.0

func (s *State) ServerURLTokenSet(token string) error

ServerURLTokenSet writes the server URL token.

func (*State) StageRestoreSnapshot added in v0.2.0

func (s *State) StageRestoreSnapshot(r io.Reader) error

StageRestoreSnapshot stages a database restore for the next server restart. This will create a temporary file alongside the data file so we must have write access to the directory containing the database.

func (*State) StatusReportGet added in v0.4.0

func (s *State) StatusReportGet(ref *pb.Ref_Operation) (*pb.StatusReport, error)

get status report by referenced operation

func (*State) StatusReportLatest added in v0.4.0

func (s *State) StatusReportLatest(
	ref *pb.Ref_Application,
	ws *pb.Ref_Workspace,
) (*pb.StatusReport, error)

StatusReportLatest gets the latest generated status report

func (*State) StatusReportList added in v0.4.0

func (s *State) StatusReportList(
	ref *pb.Ref_Application,
	opts ...ListOperationOption,
) ([]*pb.StatusReport, error)

func (*State) StatusReportPut added in v0.4.0

func (s *State) StatusReportPut(update bool, report *pb.StatusReport) error

create or update the latest status report

func (*State) WorkspaceGet

func (s *State) WorkspaceGet(n string) (*pb.Workspace, error)

WorkspaceGet gets a workspace with a specific name. If it doesn't exist, this will return an error with codes.NotFound.

func (*State) WorkspaceList

func (s *State) WorkspaceList() ([]*pb.Workspace, error)

WorkspaceList lists all the workspaces.

func (*State) WorkspaceListByApp added in v0.4.0

func (s *State) WorkspaceListByApp(ref *pb.Ref_Application) ([]*pb.Workspace, error)

WorkspaceListByApp lists all the workspaces used by a specific application.

func (*State) WorkspaceListByProject added in v0.4.0

func (s *State) WorkspaceListByProject(ref *pb.Ref_Project) ([]*pb.Workspace, error)

WorkspaceListByProject lists all the workspaces used by a project.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL