Documentation
¶
Overview ¶
Package meta is the metadata seam of the store service: repositories, workspaces and their epoch fence, op heads, refs and the pack listing. A Store keeps small mutable records, never object data. Implementations: PostgreSQL (internal/meta/postgres) and SQLite (meta/sqlite). docs/architecture.md, section Backends, is the contract of every method.
Index ¶
- Constants
- Variables
- func Partition(repo, ref string) int
- type Event
- type Fence
- type LandDone
- type LandItem
- type LandKey
- type LandLogRow
- type LandMove
- type LandRequest
- type LandState
- type LandStore
- type Lease
- type Manifest
- type OpHeads
- type OpHeadsConflict
- type Pack
- type Ref
- type RefConflict
- type Repo
- type Retired
- type Store
- type Subscription
- type UserToken
- type UserTokenStore
- type Workspace
Constants ¶
const ( WriterNode = "node" WriterMerge = "merge" WriterWrite = "write" WriterOperator = "operator" )
The kinds of writer that an Event records. Each is the scope of the grant that wrote the ref.
const LandLogRetention = 10000
LandLogRetention is the number of land log rows that a ref keeps. A batch removes the older rows in the unit that adds its own.
const Partitions = 64
Partitions is the number of event partitions of a store. The partition of a ref is fixed by its repo and name, so every event of one ref is in one partition and in order. Changing the number is a migration.
Variables ¶
var ( // ErrNotFound is a read of a record that does not exist. ErrNotFound = errors.New("meta: not found") // ErrExists is a create of a record that exists. ErrExists = errors.New("meta: already exists") // ErrGone is a workspace that was deleted. ErrGone = errors.New("meta: workspace deleted") // ErrClaimLost is a write or claim whose epoch is not the stored one. ErrClaimLost = errors.New("meta: claim lost") // ErrWorkspaceConflict is a claim whose owner or repo differs from the stored one. ErrWorkspaceConflict = errors.New("meta: workspace owner or repo differs") // ErrWrongRepo is a grant whose repo is not the repo of the workspace. ErrWrongRepo = errors.New("meta: workspace is on another repo") // ErrEventsPruned is a read of events from a sequence that retention removed. ErrEventsPruned = errors.New("meta: events pruned") // ErrManifestStale is a manifest write whose expected generation is not the stored one. ErrManifestStale = errors.New("meta: manifest generation conflict") // ErrLeaseHeld is a lease or cursor write by an owner that does not hold the lease. ErrLeaseHeld = errors.New("meta: lease held by another owner") )
var ErrLandPending = errors.New("meta: land has no result yet")
ErrLandPending is the acknowledgment of a land that has no result yet.
Functions ¶
Types ¶
type Event ¶
type Event struct {
Partition int
Seq int64
Repo, Ref string
Old, New string
Generation int64
Kind string
At time.Time
}
Event is one change of a ref. Its identity is Partition and Seq. Seq starts at 1 in each partition and has no gap there, and it follows commit order within the partition. No order holds across partitions. Old is empty for a create, and New is empty for a delete. For a delete Generation is the generation that the ref had. Kind is a Writer constant.
type Fence ¶
Fence is the state of a workspace row that every fenced write checks. A backend reads it after a conditional write changed nothing, and these methods pick the error, so every backend gives the same precedence.
type LandDone ¶
LandDone is the result of one item, written in the unit that ends the item. Landed marks an item that the ref move of the unit holds: the store records the generation of that move on it.
type LandItem ¶
type LandItem struct {
LandRequest
Seq int64
State LandState
Result string
Generation int64
// Acked marks a done item whose caller acknowledged the result. The result is gone, and the row stays as a tombstone
// until PruneLands, so the id is never queued again.
Acked bool
}
LandItem is a queued land. Seq orders the queue of a repo. Result is set once the item is done, and is opaque to the store. Generation is the generation of the ref move that landed the item, and 0 for an item that did not land.
type LandLogRow ¶
LandLogRow is one landed change of a ref: the tip before it, the tip after it, and the paths that it changed. Paths is nil when the change touched too many paths to keep.
type LandMove ¶
type LandMove struct {
Repo, Ref, Old, New, Kind string
Keep int
Rows []LandLogRow
Results []LandDone
}
LandMove is the ref move of one batch: the ref from Old to New, the log rows of the changes that it holds in land order, and the results of every item that the batch ended, whether it landed or not. Kind is the writer kind of the event. Keep is the number of log rows that the ref keeps, and LandLogRetention when it is zero.
type LandRequest ¶
LandRequest is a land that a replica accepted. ID names it for the repo, so a repeat of an accepted request finds the first one. ExpectTree is empty or the tree that the land must make. Kind is the writer kind of the grant, WriterWrite or WriterMerge.
type LandState ¶ added in v0.19.0
type LandState string
LandState is the stage of a queued land. A land is Queued until a leader claims it, Claimed until the leader ends it, and Done from then until a caller acknowledges it or the retention ends.
type LandStore ¶
type LandStore interface {
// EnqueueLand queues a land, or returns the item that has the same repo and id.
EnqueueLand(ctx context.Context, r LandRequest, now time.Time) (LandItem, error)
// LandItem returns the item of a repo and id, or ErrNotFound.
LandItem(ctx context.Context, repo, id string) (LandItem, error)
// LandPending lists the refs that have an item that is not done.
LandPending(ctx context.Context) ([]LandKey, error)
// AcquireLandLease gives owner the lease of a ref until the time until, when no owner holds it at now or owner holds
// it, and returns until. It returns ErrLeaseHeld otherwise, with the end of the lease of the other owner, so a
// caller can wait for the rest of it.
AcquireLandLease(ctx context.Context, repo, ref, owner string, now, until time.Time) (time.Time, error)
// ReleaseLandLease ends the lease of owner at once. A lease of another owner stays.
ReleaseLandLease(ctx context.Context, repo, ref, owner string) error
// ClaimLands marks as claimed by owner, and returns, the oldest items of a ref that are not done, at most limit,
// while owner holds the lease at now. An item that an earlier owner claimed and did not end comes again. ErrLeaseHeld.
ClaimLands(ctx context.Context, repo, ref, owner string, now time.Time, limit int) ([]LandItem, error)
// LandRef moves the ref of m from m.Old to m.New, appends the log rows and one event, and ends the items of
// m.Results, in one unit. It applies only while the ref is at m.Old and owner holds the lease at now. ErrLeaseHeld
// when the lease fails, else *RefConflict.
LandRef(ctx context.Context, m LandMove, owner string, now time.Time) (Ref, error)
// FinishLands ends the items of results with no ref move, while owner holds the lease at now. ErrLeaseHeld.
FinishLands(ctx context.Context, repo, ref, owner string, now time.Time, results []LandDone) error
// LandLogSince returns the log rows from the latest one that starts or ends at base, in order, at most limit. It
// returns none when base is in no row or the rows after it are more than limit.
LandLogSince(ctx context.Context, repo, ref, base string, limit int) ([]LandLogRow, error)
// AckLand drops the result of the done item of a repo and id, and keeps the item as acknowledged, so the id is not
// reused. An item that is already acknowledged stays so. ErrNotFound when it is absent, ErrLandPending when it is not
// done.
AckLand(ctx context.Context, repo, id string) error
// PruneLands removes the items that ended before the time.
PruneLands(ctx context.Context, before time.Time) error
}
LandStore is the queue, the leader lease and the land log of the batched land. See Backends for the contract.
type Lease ¶
Lease is the right to deliver one partition of a subscription, with the cursor of that partition.
type Manifest ¶ added in v0.10.0
Manifest is the stored pack manifest of a repo. The store keeps Body as opaque bytes. Generation starts at 1.
type OpHeadsConflict ¶
type OpHeadsConflict struct{ Current *OpHeads }
OpHeadsConflict is a lost op-heads compare-and-swap. Current is nil when no row exists.
func (*OpHeadsConflict) Error ¶
func (*OpHeadsConflict) Error() string
type RefConflict ¶
type RefConflict struct{ Current *Ref }
RefConflict is a lost ref compare-and-swap. Current is nil when the ref is absent.
func (*RefConflict) Error ¶
func (*RefConflict) Error() string
type Retired ¶ added in v0.10.0
type Retired struct {
Key string
At time.Time
// Deleting is set by BeginDelete: a collector has taken the file and is deleting it.
Deleting bool
DeletingAt time.Time
}
Retired is a file of a repo that no manifest needs. Key is relative to the prefix of the repo.
type Store ¶
type Store interface {
// Ping checks that the backend answers.
Ping(ctx context.Context) error
// CreateRepo stores r and its ref main at commit, both or neither. It
// returns ErrExists when r.Path or the ref exists. repoText is the text
// form of the repo that the refs and packs name.
CreateRepo(ctx context.Context, r Repo, repoText, commit string) error
// Repo returns the repo at path, or ErrNotFound.
Repo(ctx context.Context, path string) (Repo, error)
// Repos lists every repo, newest first, ties by path descending.
Repos(ctx context.Context) ([]Repo, error)
// Claim raises the epoch of workspace id to epoch, or creates the
// workspace. The returned epoch may be higher: the caller compares it
// with its own. owner and repo are set at the first claim. ErrGone,
// ErrWorkspaceConflict.
Claim(ctx context.Context, id string, epoch uint64, owner, repo string) (Workspace, error)
// Live checks that workspace id is on repo at epoch and not deleted.
// ErrNotFound, ErrGone, ErrWrongRepo, ErrClaimLost, in that order.
Live(ctx context.Context, id string, epoch uint64, repo string) error
// Delete marks a workspace deleted. It is idempotent, and an unknown
// workspace is not an error.
Delete(ctx context.Context, id string) error
// Report stores the op and snapshot of a node report under the fence.
// generation 0 writes only while the stored op is prev. A lower
// generation than the stored one is dropped without an error; an equal
// one writes again. ErrClaimLost when the fence fails.
Report(ctx context.Context, ws string, epoch uint64, repo string, generation int64, prev, op, snapshot string) error
// Flush is Report for the control plane. The repo is the stored one.
// ErrNotFound, ErrGone, ErrClaimLost.
Flush(ctx context.Context, ws string, epoch uint64, generation int64, prev, op, snapshot string) error
// OpHeads returns the head set of ws, or ErrNotFound.
OpHeads(ctx context.Context, ws string) (OpHeads, error)
// SwapOpHeads replaces the head set when the stored generation is expect
// (0 for the first write) and the fence holds. A lost swap is
// *OpHeadsConflict. ErrClaimLost wins over it.
SwapOpHeads(ctx context.Context, ws string, epoch uint64, repo string, expect int64, heads []string) (OpHeads, error)
// Ref returns a ref, or ErrNotFound.
Ref(ctx context.Context, repo, name string) (Ref, error)
// Refs lists the refs of a repo by name.
Refs(ctx context.Context, repo string) ([]Ref, error)
// SwapRef moves a ref to sha when its stored generation is expect (0
// creates the ref) and the fence holds. A lost swap is *RefConflict.
// ErrClaimLost wins over it.
SwapRef(ctx context.Context, ws string, epoch uint64, repo, name string, expect int64, sha string) (Ref, error)
// DeleteRef removes a ref when its stored generation is expect and the
// fence holds, with the errors of SwapRef.
DeleteRef(ctx context.Context, ws string, epoch uint64, repo, name string, expect int64) error
// UpdateRef is the ref write of a direct push, with no workspace and no
// fence. It moves ref name from commit old to commit new. An empty old
// creates the ref and an empty new deletes it. A ref whose commit is not
// old is *RefConflict. Every write takes a new generation and appends one
// event of the writer kind, WriterWrite or WriterMerge.
UpdateRef(ctx context.Context, repo, name, old, new, kind string) (Ref, error)
// MergeRef is SwapRef without a workspace fence: it needs only the
// stored generation to be expect. The event kind is WriterMerge.
MergeRef(ctx context.Context, repo, name string, expect int64, sha string) (Ref, error)
// MergeDeleteRef is DeleteRef without a workspace fence.
MergeDeleteRef(ctx context.Context, repo, name string, expect int64) error
// ProtectedRefs returns the protected ref patterns of repo, empty when none.
ProtectedRefs(ctx context.Context, repo string) ([]string, error)
// SetProtectedRefs replaces the protected ref patterns of repo.
SetProtectedRefs(ctx context.Context, repo string, patterns []string) error
// Events lists at most limit events of one partition with Seq above
// after, oldest first. ErrEventsPruned when after is below the last
// pruned Seq of the partition.
Events(ctx context.Context, partition int, after int64, limit int) ([]Event, error)
// EventHeads returns the newest Seq of each partition, 0 for none.
EventHeads(ctx context.Context) ([]int64, error)
// PruneEvents removes the events written before the time and records the
// highest removed Seq of each partition.
PruneEvents(ctx context.Context, before time.Time) error
// CreateSubscription stores s with each cursor at the newest Seq of its
// partition. ErrExists when the id exists. The returned value has
// Cursors and CreatedAt.
CreateSubscription(ctx context.Context, s Subscription) (Subscription, error)
// UpdateSubscription replaces the URL, the secret and the repo of subscription id and keeps its cursors and
// CreatedAt. ErrNotFound when it is absent.
UpdateSubscription(ctx context.Context, s Subscription) (Subscription, error)
// Subscriptions lists every subscription, oldest first, ties by id.
Subscriptions(ctx context.Context) ([]Subscription, error)
// DeleteSubscription removes a subscription and its cursors. ErrNotFound when it is absent.
DeleteSubscription(ctx context.Context, id string) error
// AcquireLease gives owner the lease of one partition of subscription id
// until the time until, when no owner holds it at now or owner holds
// it. It returns the lease with the cursor. ErrNotFound, ErrLeaseHeld.
AcquireLease(ctx context.Context, id string, partition int, owner string, now, until time.Time) (Lease, error)
// AdvanceCursor raises the cursor of one partition of subscription id to
// cursor while owner holds its lease. A lower cursor changes nothing.
// ErrNotFound, ErrLeaseHeld.
AdvanceCursor(ctx context.Context, id string, partition int, owner string, cursor int64) error
LandStore
UserTokenStore
// AddPack records a pack once. A repeat is a no-op.
AddPack(ctx context.Context, repo, packID string, bytes int64) error
// Packs lists the packs of a repo, oldest first, ties by id.
Packs(ctx context.Context, repo string) ([]Pack, error)
// RemovePack forgets a pack of a repo. A missing pack is not an error.
RemovePack(ctx context.Context, repo, packID string) error
// Manifest returns the manifest of a repo, or ErrNotFound.
Manifest(ctx context.Context, repo string) (Manifest, error)
// SwapManifest stores body as the manifest of repo when the stored generation is expect (0 when none is stored),
// and returns the new generation. A lost swap is ErrManifestStale.
SwapManifest(ctx context.Context, repo string, expect int64, body []byte) (int64, error)
// RetireFiles records keys as retired at at. A key that is already recorded and not deleting takes the new time.
RetireFiles(ctx context.Context, repo string, keys []string, at time.Time) error
// RetiredFiles lists the retired files of a repo, oldest first, then by key.
RetiredFiles(ctx context.Context, repo string) ([]Retired, error)
// BeginDelete marks each of keys as deleting at now, and returns the keys that it marked. It skips a key that is
// deleting since after staleBefore, which another collector owns, and a key retired after retiredBefore, which a writer used and retired again. Of concurrent callers, exactly one marks a key.
BeginDelete(ctx context.Context, repo string, keys []string, now, staleBefore, retiredBefore time.Time) ([]string, error)
// ClaimRetired removes each of keys that is retired and not deleting, so no collector takes it, and reports whether
// any of keys is deleting. A writer that stores a file again calls it first, and waits while it reports true.
ClaimRetired(ctx context.Context, repo string, keys []string) (deleting bool, err error)
// ForgetRetired removes keys from the retired files of a repo. A missing key is not an error.
ForgetRetired(ctx context.Context, repo string, keys []string) error
// PackRepos lists the text of every repo that holds a pack, in byte order.
PackRepos(ctx context.Context) ([]string, error)
}
Store is the metadata of the service. Every method that writes is one atomic unit, and none needs a lock or a transaction that spans calls. When a conditional write changes nothing, the backend reads the state once to pick the error. Names, repos and ids are opaque text, compared by byte.
type Subscription ¶
Subscription is a registered receiver. Cursors holds one cursor for each partition: the Seq of the last event of that partition that the receiver acknowledged. Repo is empty for every repo.
type UserToken ¶ added in v0.9.0
type UserToken struct {
ID, Principal, Scope string
Repos []string
CreatedAt, ExpiresAt time.Time
RevokedAt time.Time
}
UserToken is the record of a per-user token. The store keeps the hash of the token and never the token. ExpiresAt and RevokedAt are zero when the token has no expiry or is not revoked. CreatedAt is set by the backend.
type UserTokenStore ¶ added in v0.9.0
type UserTokenStore interface {
// CreateUserToken stores t under hash. ErrExists when t.ID or hash exists.
CreateUserToken(ctx context.Context, hash string, t UserToken) error
// UserTokenByHash returns the record under hash, revoked or not, or ErrNotFound.
UserTokenByHash(ctx context.Context, hash string) (UserToken, error)
// UserTokens lists every record, newest first, ties by ID descending.
UserTokens(ctx context.Context) ([]UserToken, error)
// RevokeUserToken sets RevokedAt of id once. A repeat keeps the first time and is not an error. ErrNotFound
// for an unknown id.
RevokeUserToken(ctx context.Context, id string) error
}
UserTokenStore keeps per-user tokens. hash is the lowercase hex SHA-256 of the token text.