diff --git a/node-tools/internal/bus/state.go b/node-tools/internal/bus/state.go index f659982..6fb9508 100644 --- a/node-tools/internal/bus/state.go +++ b/node-tools/internal/bus/state.go @@ -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. diff --git a/node-tools/internal/runtime/runtime.go b/node-tools/internal/runtime/runtime.go index 6e11f8e..362f4ca 100644 --- a/node-tools/internal/runtime/runtime.go +++ b/node-tools/internal/runtime/runtime.go @@ -1264,7 +1264,12 @@ func (b *moduleBus) State(verb string, params json.RawMessage) (json.RawMessage, if err != nil { return nil, err } - answer = map[string]any{"revision": revision} + // With the bucket's fill, so a bundle's prune reads it from the put (design 47 H2 DMC-14, H-A-386). + fill, err := conn.StateFill(b.module, asked.State) + if err != nil { + return nil, fmt.Errorf("%s was put at revision %d, but its state's fill is not read: %w", asked.Key, revision, err) + } + answer = map[string]any{"revision": revision, "fill": fill} case "delete": if err := conn.StateDelete(b.module, asked.State, asked.Key); err != nil { return nil, err diff --git a/node-tools/internal/runtime/state_history_test.go b/node-tools/internal/runtime/state_history_test.go index 5547159..ab47047 100644 --- a/node-tools/internal/runtime/state_history_test.go +++ b/node-tools/internal/runtime/state_history_test.go @@ -7,6 +7,8 @@ import ( "testing" "time" + "git.novox.be/novox/mesh-sdk/go/buscalls" + "github.com/novox/mesh-tools/node-tools/internal/bus" "github.com/novox/mesh-tools/node-tools/internal/launch" mt "github.com/novox/mesh-tools/node-tools/internal/meshtest" @@ -71,16 +73,26 @@ func TestAKeysHistoryIsEveryValueKeptOldestFirst(t *testing.T) { owner, reader, _ := historyBuses(t) before := time.Now().Add(-time.Second) var revisions []uint64 - for _, v := range []string{`{"state":"open"}`, `{"state":"fixing"}`, `{"state":"resolved"}`} { + // each put answers the bucket's fill with its revision, buscalls.Fill's form (design 47 H2 DMC-14, H-A-386): the messages + // kept (every value of the key, the bucket keeping 8), their bytes, and no cap (0) + var bytes uint64 + for i, v := range []string{`{"state":"open"}`, `{"state":"fixing"}`, `{"state":"resolved"}`} { raw, err := ask(owner, "put", map[string]any{"state": "issues", "key": "502", "value": json.RawMessage(v)}) if err != nil { t.Fatal(err) } var r struct { - Revision uint64 `json:"revision"` + Revision uint64 `json:"revision"` + Fill *buscalls.Fill `json:"fill"` } _ = json.Unmarshal(raw, &r) revisions = append(revisions, r.Revision) + if r.Fill == nil || r.Fill.Count != uint64(i+1) || r.Fill.Bytes <= bytes || r.Fill.MaxBytes != 0 { + t.Errorf("put %d answered %s, want its revision and the fill of %d messages, more bytes than %d, no cap", i, raw, i+1, bytes) + } + if r.Fill != nil { + bytes = r.Fill.Bytes + } } if _, err := ask(owner, "put", map[string]any{"state": "issues", "key": "503", "value": 1}); err != nil { t.Fatal(err)