A pass that kept a fresh assignment graph revision sat six to eight minutes keeping it and removing the revisions not current, so the wakes of pipeline-ended and running waited for it. The keeping now runs off the pass loop, one at a time and folded; the removal takes each entry's module and fingerprint from the store's listing instead of reading every entry object; each pass and each keeping says its duration. Builds: design 47 H H-A-259; ADR 0682
156 lines
4.5 KiB
Go
156 lines
4.5 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
"git.novox.be/novox/mesh-sdk/go/process"
|
|
|
|
"mesh-delivery-assigner/internal/assigngraph"
|
|
)
|
|
|
|
// busObjects is the assigner's own object bucket assignment-revisions in the old `objects` section (novox/hq ADR 0619 §2),
|
|
// which the grants part creates, as the passer's process reaches it: opened at its first use and kept; until the bucket exists
|
|
// every call is an error saying so, and the next call opens it again (design 47 G A-67, modelled on the indexer's busObjects).
|
|
type busObjects struct {
|
|
js jetstream.JetStream
|
|
module string
|
|
local string
|
|
mu sync.Mutex
|
|
o jetstream.ObjectStore
|
|
}
|
|
|
|
// newBusObjects is the object bucket of this local name of the module the account runs as.
|
|
func newBusObjects(js jetstream.JetStream, cred process.Account, local string) *busObjects {
|
|
return &busObjects{js: js, module: cred.Module, local: local}
|
|
}
|
|
|
|
func (b *busObjects) open(ctx context.Context) (jetstream.ObjectStore, error) {
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
if b.o != nil {
|
|
return b.o, nil
|
|
}
|
|
o, err := b.js.ObjectStore(ctx, catalogue.ObjectsBucket(b.module, b.local))
|
|
if errors.Is(err, jetstream.ErrBucketNotFound) || errors.Is(err, jetstream.ErrStreamNotFound) {
|
|
return nil, fmt.Errorf("the object bucket %s is not on the bus yet (the grants part creates it from the manifest's objects): %w", b.local, process.ErrNotYet)
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("the object bucket %s is not open: %w", b.local, err)
|
|
}
|
|
b.o = o
|
|
return o, nil
|
|
}
|
|
|
|
func (b *busObjects) Put(ctx context.Context, name string, data []byte) error {
|
|
o, err := b.open(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = o.Put(ctx, jetstream.ObjectMeta{Name: name}, bytes.NewReader(data))
|
|
return err
|
|
}
|
|
|
|
func (b *busObjects) Get(ctx context.Context, name string) ([]byte, error) {
|
|
o, err := b.open(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
data, err := o.GetBytes(ctx, name)
|
|
if errors.Is(err, jetstream.ErrObjectNotFound) {
|
|
return nil, assigngraph.ErrNoObject
|
|
}
|
|
return data, err
|
|
}
|
|
|
|
func (b *busObjects) Names(ctx context.Context) ([]string, error) {
|
|
o, err := b.open(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
infos, err := o.List(ctx)
|
|
if errors.Is(err, jetstream.ErrNoObjectsFound) {
|
|
return []string{}, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
names := make([]string, 0, len(infos))
|
|
for _, i := range infos {
|
|
names = append(names, i.Name)
|
|
}
|
|
return names, nil
|
|
}
|
|
|
|
// PutDescribed stores data under name with the entry's module and fingerprint in its metadata, which the listing says (assigngraph.Describer,
|
|
// design 47 H-A-259).
|
|
func (b *busObjects) PutDescribed(ctx context.Context, name string, data []byte, module, fp string) error {
|
|
o, err := b.open(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = o.Put(ctx, jetstream.ObjectMeta{Name: name, Metadata: map[string]string{entryModuleMeta: module, entryFpMeta: fp}}, bytes.NewReader(data))
|
|
return err
|
|
}
|
|
|
|
// Described is every object with the module and fingerprint its metadata says, in one listing.
|
|
func (b *busObjects) Described(ctx context.Context) ([]assigngraph.Described, error) {
|
|
o, err := b.open(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
infos, err := o.List(ctx)
|
|
if errors.Is(err, jetstream.ErrNoObjectsFound) {
|
|
return []assigngraph.Described{}, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]assigngraph.Described, 0, len(infos))
|
|
for _, i := range infos {
|
|
out = append(out, assigngraph.Described{Name: i.Name, Module: i.Metadata[entryModuleMeta], Fp: i.Metadata[entryFpMeta]})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// The metadata keys of an entry object's module and fingerprint.
|
|
const (
|
|
entryModuleMeta = "module"
|
|
entryFpMeta = "fp"
|
|
)
|
|
|
|
// Delete removes the object of that name; one not held is no error (design 47 G §579: a revision nothing pins is removed).
|
|
func (b *busObjects) Delete(ctx context.Context, name string) error {
|
|
o, err := b.open(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := o.Delete(ctx, name); err != nil && !errors.Is(err, jetstream.ErrObjectNotFound) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Fill is the bytes the bucket holds and its cap (assigngraph.Filler).
|
|
func (b *busObjects) Fill(ctx context.Context) (int64, int64, error) {
|
|
o, err := b.open(ctx)
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
st, err := o.Status(ctx)
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
info, err := b.js.Stream(ctx, "OBJ_"+st.Bucket())
|
|
if err != nil {
|
|
return int64(st.Size()), 0, err
|
|
}
|
|
return int64(st.Size()), info.CachedInfo().Config.MaxBytes, nil
|
|
}
|