c5a6871f created the key once and never wrote it again, so a step that kept moving was bounded from its first movement (issue 1930). Arm now creates the key when absent; one ending no earlier is armed already; one ending earlier is written again by an update against the revision read, carrying the bound's time to live; a waiter whose update loses reads the key again and takes the newer bound as armed. The test moves a step twice past its first bound, with two waiters each time, and reads one write per movement, its time to live, and no delete. Builds: design 47 G A-282, A-115, §544 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
85 lines
3.2 KiB
Go
85 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
"mesh-delivery-builder/internal/versions"
|
|
)
|
|
|
|
// stepBounds keeps the bounds of an ask's step as keys of the state bounds with their own time to live (design 47 G A-77, A-101,
|
|
// A-118): the bus's marker of a key's end, heard on the watch of the state, is told to buildrun.Asker's Expiry. No timer.
|
|
type stepBounds struct {
|
|
bounds func() jetstream.KeyValue
|
|
// js writes a moved step's bound again: an update against the revision read, carrying the bound's time to live.
|
|
js jetstream.JetStream
|
|
now func() time.Time
|
|
logf func(string, ...any)
|
|
}
|
|
|
|
// Arm is buildrun.StepBounds' Arm (design 47 G A-282, issue 1930): the key is never deleted to be made again. It is created
|
|
// when absent; one already there that ends no earlier was armed by another waiter of the same ask and is taken as armed; one
|
|
// that ends earlier is the step's bound before it moved, and is written again by an update against the revision read, with
|
|
// the bound's own time to live, so the bound counts from the step's last movement. A waiter whose update loses to another's
|
|
// reads the key again and takes the newer bound as armed.
|
|
func (s *stepBounds) Arm(ctx context.Context, key, why string, bound time.Duration) error {
|
|
kv := s.bounds()
|
|
if kv == nil {
|
|
return fmt.Errorf("%s is not on the bus yet", versions.BoundsBucket)
|
|
}
|
|
ends := s.now().Add(bound)
|
|
value := versions.EncodeBound(why, ends)
|
|
for range 2 { // a key that ended between the create and the read is created once more
|
|
_, err := kv.Create(ctx, key, value, jetstream.KeyTTL(bound))
|
|
if !errors.Is(err, jetstream.ErrKeyExists) {
|
|
return err
|
|
}
|
|
held, err := kv.Get(ctx, key)
|
|
if errors.Is(err, jetstream.ErrKeyNotFound) {
|
|
continue
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var was versions.Bound
|
|
if err := json.Unmarshal(held.Value(), &was); err == nil && !was.Ends.Before(ends) {
|
|
return nil // armed already, at this movement or a later one
|
|
}
|
|
if s.js == nil {
|
|
return fmt.Errorf("the bound %s moved and is not written again: no JetStream to update it with", key)
|
|
}
|
|
_, err = s.js.PublishMsg(ctx, &nats.Msg{Subject: "$KV." + kv.Bucket() + "." + key, Data: value},
|
|
jetstream.WithExpectLastSequencePerSubject(held.Revision()), jetstream.WithMsgTTL(bound))
|
|
if wrongLast(err) {
|
|
if _, err := kv.Get(ctx, key); err != nil && !errors.Is(err, jetstream.ErrKeyNotFound) {
|
|
return err
|
|
}
|
|
return nil // another waiter wrote the newer bound: armed
|
|
}
|
|
return err
|
|
}
|
|
return fmt.Errorf("the bound %s ended each time it was read", key)
|
|
}
|
|
|
|
// wrongLast is a write that lost to another's: the revision it was made against is no longer the key's last.
|
|
func wrongLast(err error) bool {
|
|
var api *jetstream.APIError
|
|
return errors.As(err, &api) && (api.ErrorCode == jetstream.JSErrCodeStreamWrongLastSequence ||
|
|
api.ErrorCode == jetstream.JSErrCodeStreamWrongLastSequenceConstant)
|
|
}
|
|
|
|
// End is buildrun.StepBounds' End.
|
|
func (s *stepBounds) End(ctx context.Context, key string) {
|
|
if kv := s.bounds(); kv != nil {
|
|
if err := kv.Delete(ctx, key); err != nil && !errors.Is(err, jetstream.ErrKeyNotFound) {
|
|
s.logf("the step bound %s is not deleted: %v", key, err)
|
|
}
|
|
}
|
|
}
|