juju: github.com/juju/juju/worker/uniter/relation Index | Files

package relation

import "github.com/juju/juju/worker/uniter/relation"

relation implements persistent local storage of a unit's relation state, and translation of relation changes into hooks that need to be run.


Package Files

relationer.go relations.go state.go

func NewRelationsResolver Uses

func NewRelationsResolver(r Relations) resolver.Resolver

NewRelationsResolver returns a new Resolver that handles differences in relation state.

func ReadAllStateDirs Uses

func ReadAllStateDirs(dirPath string) (dirs map[int]*StateDir, err error)

ReadAllStateDirs loads and returns every StateDir persisted directly inside the supplied dirPath. If dirPath does not exist, no error is returned.

type LeadershipContextFunc Uses

type LeadershipContextFunc func(accessor context.LeadershipSettingsAccessor, tracker leadership.Tracker, unitName string) context.LeadershipContext

LeadershipContextFunc is a function that returns a leadership context.

type Relationer Uses

type Relationer struct {
    // contains filtered or unexported fields

Relationer manages a unit's presence in a relation.

func NewRelationer Uses

func NewRelationer(ru *apiuniter.RelationUnit, dir *StateDir) *Relationer

NewRelationer creates a new Relationer. The unit will not join the relation until explicitly requested.

func (*Relationer) CommitHook Uses

func (r *Relationer) CommitHook(hi hook.Info) error

CommitHook persists the fact of the supplied hook's completion.

func (*Relationer) ContextInfo Uses

func (r *Relationer) ContextInfo() *context.RelationInfo

ContextInfo returns a representation of the Relationer's current state.

func (*Relationer) IsImplicit Uses

func (r *Relationer) IsImplicit() bool

IsImplicit returns whether the local relation endpoint is implicit. Implicit relations do not run hooks.

func (*Relationer) Join Uses

func (r *Relationer) Join() error

Join initializes local state and causes the unit to enter its relation scope, allowing its counterpart units to detect its presence and settings changes. Local state directory is not created until needed.

func (*Relationer) PrepareHook Uses

func (r *Relationer) PrepareHook(hi hook.Info) (hookName string, err error)

PrepareHook checks that the relation is in a state such that it makes sense to execute the supplied hook, and ensures that the relation context contains the latest relation state as communicated in the hook.Info. It returns the name of the hook that must be run.

func (*Relationer) SetDying Uses

func (r *Relationer) SetDying() error

SetDying informs the relationer that the unit is departing the relation, and that the only hooks it should send henceforth are -departed hooks, until the relation is empty, followed by a -broken hook.

type Relations Uses

type Relations interface {
    // Name returns the name of the relation with the supplied id, or an error
    // if the relation is unknown.
    Name(id int) (string, error)

    // PrepareHook returns the name of the supplied relation hook, or an error
    // if the hook is unknown or invalid given current state.
    PrepareHook(hookInfo hook.Info) (string, error)

    // CommitHook persists the state change encoded in the supplied relation
    // hook, or returns an error if the hook is unknown or invalid given
    // current relation state.
    CommitHook(hookInfo hook.Info) error

    // GetInfo returns information about current relation state.
    GetInfo() map[int]*context.RelationInfo

    // NextHook returns details on the next hook to execute, based on the local
    // and remote states.
    NextHook(resolver.LocalState, remotestate.Snapshot) (hook.Info, error)

Relations exists to encapsulate relation state and operations behind an interface for the benefit of future refactoring.

func NewRelations Uses

func NewRelations(config RelationsConfig) (Relations, error)

NewRelations returns a new Relations instance.

type RelationsConfig Uses

type RelationsConfig struct {
    State                *uniter.State
    UnitTag              names.UnitTag
    Tracker              leadership.Tracker
    CharmDir             string
    RelationsDir         string
    NewLeadershipContext LeadershipContextFunc
    Abort                <-chan struct{}

RelationsConfig contains configuration values for the relations instance.

type State Uses

type State struct {
    // RelationId identifies the relation.
    RelationId int

    // Members is a map from unit name to the last change version
    // for which a hook.Info was delivered on the output channel.
    Members map[string]int64

    // ApplicationMembers is a map from application name to the last change
    // version for which a hook.Info was delivered
    ApplicationMembers map[string]int64

    // ChangedPending indicates that a "relation-changed" hook for the given
    // unit name must be the first hook.Info to be sent to the output channel.
    ChangedPending string

State describes the state of a relation.

func (*State) Validate Uses

func (s *State) Validate(hi hook.Info) (err error)

Validate returns an error if the supplied hook.Info does not represent a valid change to the relation state. Hooks must always be validated against the current state before they are run, to ensure that the system meets its guarantees about hook execution order.

type StateDir Uses

type StateDir struct {
    // contains filtered or unexported fields

StateDir is a filesystem-backed representation of the state of a relation. Concurrent modifications to the underlying state directory will have undefined consequences.

func ReadStateDir Uses

func ReadStateDir(dirPath string, relationId int) (d *StateDir, err error)

ReadStateDir loads a StateDir from the subdirectory of dirPath named for the supplied RelationId. If the directory does not exist, no error is returned,

func (*StateDir) Ensure Uses

func (d *StateDir) Ensure() error

Ensure creates the directory if it does not already exist.

func (*StateDir) Exists Uses

func (d *StateDir) Exists() bool

Exists returns true if the directory for this state exists.

func (*StateDir) Remove Uses

func (d *StateDir) Remove() error

Remove removes the directory if it exists and is empty.

func (*StateDir) State Uses

func (d *StateDir) State() *State

State returns the current state of the relation.

func (*StateDir) Write Uses

func (d *StateDir) Write(hi hook.Info) (err error)

Write atomically writes to disk the relation state change in hi. It must be called after the respective hook was executed successfully. Write doesn't validate hi but guarantees that successive writes of the same hi are idempotent.

Package relation imports 24 packages (graph) and is imported by 46 packages. Updated 2020-02-24. Refresh now. Tools for package owners.