H-A-251: bind a state's bucket once for the process, not before every read
The runtime asked the bucket's stream info before every get, put, delete and listing, so each state read was two answers on the bus (a third of the night's flood came this way, ten thousand reads per sweep). A Conn is one account's connection; it now keeps one binding per bucket, shared by concurrent askers, and binds again once when the bus says the bucket is gone. Builds: design 47 H H-A-251
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
//go:build integration
|
||||
|
||||
package bus_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
|
||||
mt "github.com/novox/mesh-tools/node-tools/internal/meshtest"
|
||||
)
|
||||
|
||||
// **On the bus, a read of a state is one answer: its bucket is bound once, and bound again when it was made afresh or
|
||||
// is gone** (design 47 H lead answers H-A-251). The runtime asked the bucket's stream info before every get, so each
|
||||
// read was two answers; a third of a night's bus flood came this way.
|
||||
func TestAStateReadOnTheBusBindsItsBucketOnce(t *testing.T) {
|
||||
conn, kv := owedWatcher(t)
|
||||
const bucket = "mesh-delivery-assigner_pins"
|
||||
if _, err := kv.Put("a", []byte(`{"v":1}`)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
watcher, err := nats.Connect(mt.URL(t))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(watcher.Close)
|
||||
var infos atomic.Int32
|
||||
if _, err := watcher.Subscribe("$JS.API.STREAM.INFO.KV_"+bucket, func(*nats.Msg) { infos.Add(1) }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := watcher.Flush(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
read := func() {
|
||||
t.Helper()
|
||||
e, err := conn.StateGet("gitea", "mesh-delivery-assigner.pins", "a")
|
||||
if err != nil || e == nil {
|
||||
t.Fatalf("read: %+v, %v", e, err)
|
||||
}
|
||||
}
|
||||
read()
|
||||
read()
|
||||
read()
|
||||
if err := watcher.Flush(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := infos.Load(); got != 1 {
|
||||
t.Fatalf("three reads asked the bucket's stream info %d times, want once", got)
|
||||
}
|
||||
|
||||
// The bucket deleted under the runtime: the read binds again, once, and says the bucket is not on the bus.
|
||||
js, _ := watcher.JetStream()
|
||||
if err := js.DeleteKeyValue(bucket); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = conn.StateGet("gitea", "mesh-delivery-assigner.pins", "a")
|
||||
if err == nil || !strings.Contains(err.Error(), "not on the bus yet") {
|
||||
t.Fatalf("a read of a deleted bucket answered %v, want that its bucket is not on the bus", err)
|
||||
}
|
||||
// And made afresh: read again from the new bucket.
|
||||
again, err := js.CreateKeyValue(&nats.KeyValueConfig{Bucket: bucket, History: 1})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := again.Put("a", []byte(`{"v":2}`)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
e, err := conn.StateGet("gitea", "mesh-delivery-assigner.pins", "a")
|
||||
if err != nil || e == nil || string(e.Value) != `{"v":2}` {
|
||||
t.Fatalf("a read of the bucket made afresh: %+v, %v", e, err)
|
||||
}
|
||||
read()
|
||||
if err := watcher.Flush(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := infos.Load(); got != 3 {
|
||||
t.Fatalf("the bucket's stream info was asked %d times in all, want 3: once, once more when it was gone, once when made afresh", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user