DMC-14: a state put answers its bucket's fill beside its revision

mesh/state.put answers {"revision", "fill": {"count", "bytes", "max_bytes"}}, buscalls.Fill's
form, read from the bound stream's info once per put, since the key-value put answers no figures;
a bundle's prune reads it from the put.

Builds: design 47 H2 DMC-14, H-A-350, H-A-386
This commit is contained in:
2026-11-22 00:12:23 +01:00
parent a1c12af8b2
commit ec3fe34afc
3 changed files with 48 additions and 11 deletions
+28 -8
View File
@@ -12,6 +12,7 @@ import (
"sync"
"time"
"git.novox.be/novox/mesh-sdk/go/buscalls"
"git.novox.be/novox/mesh-sdk/go/membership"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
@@ -477,27 +478,46 @@ type StateInfo struct {
// StateInfo reads a state's bucket's fill and caps. It is a read like StateGet: served to the owner and every reader.
func (c *Conn) StateInfo(module, name string) (*StateInfo, error) {
s, err := c.issuedState(module, name)
status, info, err := c.bucketStatus(module, name)
if err != nil {
return nil, err
}
return &StateInfo{State: name, Bucket: status.Bucket(), Bytes: info.State.Bytes, Keys: uint64(info.State.NumSubjects),
MaxBytes: info.Config.MaxBytes, MaxValueBytes: info.Config.MaxMsgSize, History: status.History(),
TTLSeconds: int64(status.TTL() / time.Second), KeyTTL: info.Config.AllowMsgTTL && status.LimitMarkerTTL() > 0}, nil
}
// StateFill is a state's bucket's fill against its cap, buscalls.Fill's form: the messages it keeps, their bytes and its
// cap in bytes (0 is none). A put answers it, read from the bound stream's info once per put, since the bus's key-value
// put answers no figures of its own (design 47 H2 DMC-14, H-A-386).
func (c *Conn) StateFill(module, name string) (buscalls.Fill, error) {
_, info, err := c.bucketStatus(module, name)
if err != nil {
return buscalls.Fill{}, err
}
return buscalls.Fill{Count: info.State.Msgs, Bytes: info.State.Bytes, MaxBytes: uint64(max(info.Config.MaxBytes, 0))}, nil
}
// bucketStatus is a state's bucket's status and the info of the stream behind it.
func (c *Conn) bucketStatus(module, name string) (jetstream.KeyValueStatus, *jetstream.StreamInfo, error) {
s, err := c.issuedState(module, name)
if err != nil {
return nil, nil, err
}
if s.PerNode {
return nil, perNodeRefusal(module, name, "fill")
return nil, nil, perNodeRefusal(module, name, "fill")
}
ctx, cancel := context.WithTimeout(context.Background(), StateTimeout)
defer cancel()
status, err := inBucket(c, ctx, s, func(kv jetstream.KeyValue) (jetstream.KeyValueStatus, error) { return kv.Status(ctx) })
if err != nil {
return nil, err
return nil, nil, err
}
b, ok := status.(*jetstream.KeyValueBucketStatus)
if !ok || b.StreamInfo() == nil {
return nil, fmt.Errorf("the bus answered no stream behind %s's state %s", module, name)
return nil, nil, fmt.Errorf("the bus answered no stream behind %s's state %s", module, name)
}
info := b.StreamInfo()
return &StateInfo{State: name, Bucket: status.Bucket(), Bytes: info.State.Bytes, Keys: uint64(info.State.NumSubjects),
MaxBytes: info.Config.MaxBytes, MaxValueBytes: info.Config.MaxMsgSize, History: status.History(),
TTLSeconds: int64(status.TTL() / time.Second), KeyTTL: info.Config.AllowMsgTTL && status.LimitMarkerTTL() > 0}, nil
return status, b.StreamInfo(), nil
}
// StatePut writes one key, as the module, where the module keeps the state. It answers the revision.