B7 (1): a mark is updated by one publish carrying its lifetime and the expected last sequence of its key
Builds: design 47 H2 H-A-720 (1), H-A-741
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
//go:build integration
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
"mesh-delivery-builder/internal/versions"
|
||||
)
|
||||
|
||||
// Design 47 H2 H-A-720 (1), H-A-741: a mark is updated by one publish carrying its lifetime and the expected last sequence of its
|
||||
// key: the update at the right revision succeeds and the key keeps its lifetime; one at a stale revision is a conflict.
|
||||
func TestAMarkIsUpdatedWithItsLifetimeInOnePublish(t *testing.T) {
|
||||
b := startBed(t)
|
||||
kv, err := b.js.CreateKeyValue(b.ctx, jetstream.KeyValueConfig{Bucket: "acted-test", LimitMarkerTTL: time.Minute})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
key := versions.ActedKey("retake:x")
|
||||
rev, err := kv.Create(b.ctx, key, []byte("1"), jetstream.KeyTTL(time.Hour))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
next, err := updateWithLifetime(b.ctx, b.js, "acted-test", key, []byte("2"), rev, 2*time.Hour)
|
||||
if err != nil || next <= rev {
|
||||
t.Fatalf("update at the key's revision: %d, %v", next, err)
|
||||
}
|
||||
if e, err := kv.Get(b.ctx, key); err != nil || string(e.Value()) != "2" || e.Revision() != next {
|
||||
t.Fatalf("the key holds %v, %v after the update", e, err)
|
||||
}
|
||||
if _, err := updateWithLifetime(b.ctx, b.js, "acted-test", key, []byte("3"), rev, time.Hour); !errors.Is(err, versions.ErrConflict) {
|
||||
t.Fatalf("update at a stale revision: %v, want a conflict", err)
|
||||
}
|
||||
}
|
||||
@@ -336,7 +336,7 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
|
||||
}
|
||||
// The operator is asked once a key's event builds are spent (design 47 G §568): the builder's asker on the seat operator-channel.
|
||||
decided := &decisions{subject: oc.SubjectDecided(cred.Module)}
|
||||
retakeActed := markLifetime{awaitedReserved{awaitedState{b: actedKV, bucket: own.acted}}, actedDays}
|
||||
retakeActed := markLifetime{awaitedReserved{awaitedState{b: actedKV, bucket: own.acted}}, actedDays, js}
|
||||
actedKV.Ready(func(kv jetstream.KeyValue) { deleteUnknown(ctx, kv, logger.Printf) })
|
||||
asker := &operatorask.Asker[oc.Warrant]{Module: cred.Module, Call: calls.Call, Record: calls.Ask, Publish: publishVia(ctx, js), Subscribe: decided.hear(logger.Printf),
|
||||
Contract: operatorask.Contract{Seat: oc.Seat, StateAsks: oc.StateAsks},
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
@@ -126,10 +128,11 @@ func emitSeatEvent(ctx context.Context, js jetstream.JetStream, cred process.Acc
|
||||
|
||||
// markLifetime writes the marks of the state acted with the bus's per-message lifetime of the setting acted-days, each write giving
|
||||
// it again (design 47 H2 H-A-720 (1)); the release record and every other key it writes with none (H-A-720 (2)). An update is the
|
||||
// compare-and-set delete of the revision and the create that carries the lifetime, since the bus gives a lifetime at a create.
|
||||
// one publish carrying the lifetime and the expected last sequence of the key (H-A-741).
|
||||
type markLifetime struct {
|
||||
awaitedReserved
|
||||
days int
|
||||
js jetstream.JetStream
|
||||
}
|
||||
|
||||
func (s markLifetime) life(key string) (time.Duration, bool) {
|
||||
@@ -161,11 +164,26 @@ func (s markLifetime) Update(ctx context.Context, key string, value []byte, revi
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if err := kv.Delete(ctx, key, jetstream.LastRevision(revision)); err != nil {
|
||||
return 0, versionsConflict(err)
|
||||
_ = kv
|
||||
return updateWithLifetime(ctx, s.js, s.bucket, key, value, revision, ttl)
|
||||
}
|
||||
|
||||
// updateWithLifetime is one publish to the key's subject carrying the per-message lifetime and the expected last sequence of the
|
||||
// key: the compare-and-set update that gives the lifetime again (design 47 H2 H-A-741).
|
||||
func updateWithLifetime(ctx context.Context, js jetstream.JetStream, bucket, key string, value []byte, revision uint64, ttl time.Duration) (uint64, error) {
|
||||
msg := nats.NewMsg("$KV." + bucket + "." + key)
|
||||
msg.Data = value
|
||||
msg.Header.Set("Nats-TTL", fmt.Sprintf("%ds", int64(ttl/time.Second)))
|
||||
msg.Header.Set("Nats-Expected-Last-Subject-Sequence", strconv.FormatUint(revision, 10))
|
||||
ack, err := js.PublishMsg(ctx, msg)
|
||||
if err != nil {
|
||||
var apiErr *jetstream.APIError
|
||||
if errors.As(err, &apiErr) && apiErr.ErrorCode == jetstream.JSErrCodeStreamWrongLastSequence {
|
||||
return 0, versions.ErrConflict
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
rev, err := kv.Create(ctx, key, value, jetstream.KeyTTL(ttl))
|
||||
return rev, versionsConflict(err)
|
||||
return ack.Sequence, nil
|
||||
}
|
||||
|
||||
// deleteUnknown deletes each key of the state acted of a form the builder does not write (design 47 H2 H-A-720 (3)) and says the
|
||||
|
||||
Reference in New Issue
Block a user