store

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package store wraps the NATS JetStream KV buckets and streams that hold all Packtrail state: executions, ownership leases, visibility indexes and the domain event stream. Every state transition is a CAS (optimistic concurrency) write.

Index

Constants

View Source
const (
	StatusRunning   = "running"
	StatusWaiting   = "waiting"
	StatusCompleted = "completed"
	StatusFailed    = "failed"
)

Execution status values.

View Source
const (
	BranchPending   = "pending"
	BranchCompleted = "completed"
	BranchFailed    = "failed"
)

Branch status values.

Variables

View Source
var ErrConflict = errors.New("store: revision conflict")

ErrConflict is returned when a CAS write loses to a concurrent writer and the caller's revision is stale.

View Source
var ErrNotFound = errors.New("store: not found")

ErrNotFound is returned when an execution key does not exist.

Functions

This section is empty.

Types

type ActivityResult

type ActivityResult struct {
	Node    string          `json:"node"`
	Attempt int             `json:"attempt"`
	Status  string          `json:"status"`
	Payload json.RawMessage `json:"payload,omitempty"`
	Error   string          `json:"error,omitempty"`
}

ActivityResult is an async activity completion stored on the execution when it arrives before the dispatching task has persisted its waiting state (the "completion before wait" race). The parking task consumes it instead of waiting. Status mirrors the invoker status string ("ok"/"error"/"retry").

type BranchState

type BranchState struct {
	NodeID  string          `json:"node_id"`
	Status  string          `json:"status"`
	Attempt int             `json:"attempt,omitempty"` // attempts spent on this branch (async retries)
	Result  json.RawMessage `json:"result,omitempty"`
	Error   string          `json:"error,omitempty"`
}

BranchState is the persisted state of a single fanout branch.

type Event

type Event struct {
	ExecID   string    `json:"exec_id"`
	FlowName string    `json:"flow_name"`
	Status   string    `json:"status"`
	Node     string    `json:"node"`
	Error    string    `json:"error,omitempty"`
	Revision uint64    `json:"revision"`
	Time     time.Time `json:"time"`
}

Event is a domain event appended to the packtrail-events stream and consumed by the visibility indexer. Revision is the KV revision of the execution at the time the event was emitted, used for idempotent, per-revision indexing.

type Execution

type Execution struct {
	ID          string                     `json:"id"`
	FlowName    string                     `json:"flow_name"`
	CurrentNode string                     `json:"current_node"`
	Status      string                     `json:"status"`
	Payload     json.RawMessage            `json:"payload"`
	Attempt     int                        `json:"attempt"`               // attempts spent on CurrentNode (task retries)
	Branches    map[string]BranchState     `json:"branches,omitempty"`    // active fanout/fanin branches
	LastSeq     map[string]uint64          `json:"last_seq,omitempty"`    // last applied JetStream seq, per signal_name
	Signals     map[string]json.RawMessage `json:"signals,omitempty"`     // latest received payload, per signal_name
	WaitSignal  string                     `json:"wait_signal,omitempty"` // signal_name currently awaited
	Activity    *ActivityResult            `json:"activity,omitempty"`    //nolint:lll // async completion that arrived before the task parked
	Error       string                     `json:"error,omitempty"`
	Revision    uint64                     `json:"-"` // current KV revision, for CAS (not persisted in value)
	UpdatedAt   time.Time                  `json:"updated_at"`
}

Execution is the runtime instance of a Flow. It is the single source of truth for an execution and is persisted in the packtrail-executions KV bucket.

func (*Execution) Active

func (e *Execution) Active() bool

Active reports whether the execution is still in progress.

type Lease

type Lease struct {
	Owner   string    `json:"owner"`
	Expires time.Time `json:"expires"`
}

Lease is the value stored under packtrail-leases/<execID>.

type Store

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

Store provides access to all Packtrail KV buckets and streams.

func Open

func Open(ctx context.Context, js jetstream.JetStream, n names.Names) (*Store, error)

Open ensures every bucket and stream exists, under the given namespace, and returns a ready Store.

func (*Store) AcquireLease

func (s *Store) AcquireLease(ctx context.Context, execID, owner string, ttl time.Duration) (bool, error)

AcquireLease attempts to take or renew ownership of execID for owner with the given TTL. It succeeds if the key is absent, already owned by owner, or held by an expired lease. The CAS guarantees that at most one *distinct* owner wins a race; a write that loses to our own concurrent renewal still counts as held. It returns true if the lease is now held by owner.

func (*Store) Create

func (s *Store) Create(ctx context.Context, e *Execution) (uint64, error)

Create persists a new execution and returns its initial revision.

func (*Store) EmitEvent

func (s *Store) EmitEvent(ctx context.Context, e *Execution) error

EmitEvent appends a domain event for the execution to the events stream.

func (*Store) Get

func (s *Store) Get(ctx context.Context, id string) (*Execution, error)

Get loads an execution and populates its Revision from the KV entry.

func (*Store) IdxFlow

func (s *Store) IdxFlow() jetstream.KeyValue

IdxFlow exposes the by-flow visibility index bucket.

func (*Store) IdxStatus

func (s *Store) IdxStatus() jetstream.KeyValue

IdxStatus exposes the by-status visibility index bucket.

func (*Store) JS

func (s *Store) JS() jetstream.JetStream

JS exposes the underlying JetStream context for packages that manage their own consumers/streams (runtime, scheduler, signal, visibility).

func (*Store) ListExecutionKeys

func (s *Store) ListExecutionKeys(ctx context.Context) ([]string, error)

ListExecutionKeys returns all execution ids currently stored. Used by the visibility reconciler.

func (*Store) Mutate

func (s *Store) Mutate(ctx context.Context, id string, fn func(*Execution) error) (*Execution, error)

Mutate runs a read-modify-write CAS loop: it loads the execution, applies fn, and writes it back, retrying the whole cycle on a concurrent-write conflict. The mutated execution (with its new revision) is returned.

func (*Store) Names

func (s *Store) Names() names.Names

Names returns the resource names this store was opened with, so dependent packages share the same namespace.

func (*Store) ReleaseLease

func (s *Store) ReleaseLease(ctx context.Context, execID, owner string) error

ReleaseLease drops ownership of execID if held by owner. Releasing a lease not owned by owner is a no-op.

Jump to

Keyboard shortcuts

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