T1 page by cursor: a state page continues from an opaque cursor in sequence order
mesh/state.page reads a state from an ordered watch starting after the cursor's sequence, never a replay of the bucket per page: each kept message once across the pages of a read, a key's older kept revisions included, at most 768 KB a page, next the opaque cursor, empty at the end. A value written after the read reached its bucket comes marked seen-again (the cursor carries that mark; the runner keeps nothing per read); a deleted key gets no entry. A state kept one bucket per node is read one node's bucket after another. Builds: design 47 H2 §779, H-A-254, H-A-382
This commit is contained in:
@@ -1206,8 +1206,9 @@ type stateAsked struct {
|
||||
// TTLSeconds gives a created or put key its own time to live (issue 1952): create makes it only when it has no
|
||||
// value, put renews it whole. Zero is none.
|
||||
TTLSeconds int `json:"ttl_seconds"`
|
||||
// After and MaxBytes are a `page`'s start key and its size in keys and values (zero is a megabyte).
|
||||
After string `json:"after"`
|
||||
// Cursor and MaxBytes are a `page`'s opaque cursor, the next of the page before (empty: from the start), and its size in
|
||||
// keys and values (zero, or above 768 KB, is 768 KB; design 47 H2 §779).
|
||||
Cursor string `json:"cursor"`
|
||||
MaxBytes int `json:"max_bytes"`
|
||||
// BudgetMS is the time a `page` may spend reading, in milliseconds (zero is the runtime's own, well under 30 s).
|
||||
BudgetMS int `json:"budget_ms"`
|
||||
@@ -1239,11 +1240,8 @@ func (b *moduleBus) State(verb string, params json.RawMessage) (json.RawMessage,
|
||||
}
|
||||
answer = entry
|
||||
case "page":
|
||||
// The values after a key in key order, one request for many (a first view's page of a slow state).
|
||||
if asked.MaxBytes <= 0 {
|
||||
asked.MaxBytes = 1 << 20
|
||||
}
|
||||
page, err := conn.StatePage(b.module, asked.State, asked.After, asked.MaxBytes, time.Duration(asked.BudgetMS)*time.Millisecond)
|
||||
// One page of the state in the order the bus wrote it, after the cursor, one request for many (design 47 H2 §779).
|
||||
page, err := conn.StatePage(b.module, asked.State, asked.Cursor, asked.MaxBytes, time.Duration(asked.BudgetMS)*time.Millisecond)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -150,35 +150,61 @@ func TestAKeysHistoryAndAPageOfAStateAreServedToOwnerAndReaderAndRefusedToAModul
|
||||
!strings.Contains(err.Error(), "not a key") {
|
||||
t.Errorf("history of a key the bus cannot hold: %v", err)
|
||||
}
|
||||
// A page is the values in key order after a key, in one request, to the owner and to a reader, never to a
|
||||
// module not issued the state; a page too small for two values still gives one and says where it goes on.
|
||||
for k, v := range map[string]string{"2": `"bb"`, "3": `"cc"`} {
|
||||
if _, err := ask(owner, "put", map[string]any{"state": "issues", "key": k, "value": json.RawMessage(v)}); err != nil {
|
||||
// A page is the values in the order the bus wrote them after the cursor, in one request, to the owner and to a reader,
|
||||
// never to a module not issued the state; a page too small for two values still gives one and says where the read goes
|
||||
// on; a value written after the read began comes marked seen-again, and a fresh read hands each kept message, a key's
|
||||
// older kept revision included (design 47 H2 §779, H-A-382).
|
||||
for _, kv := range [][2]string{{"2", `"bb"`}, {"3", `"cc"`}} {
|
||||
if _, err := ask(owner, "put", map[string]any{"state": "issues", "key": kv[0], "value": json.RawMessage(kv[1])}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
type page struct {
|
||||
Entries []kept `json:"entries"`
|
||||
Next string `json:"next"`
|
||||
type entry struct {
|
||||
Key string `json:"key"`
|
||||
SeenAgain bool `json:"seen-again"`
|
||||
}
|
||||
for _, c := range []struct {
|
||||
who, state, after string
|
||||
max int
|
||||
keys []string
|
||||
next string
|
||||
}{{"owner", "issues", "", 1 << 20, []string{"1", "2", "3"}, ""}, {"reader", "issues.issues", "1", 1 << 20, []string{"2", "3"}, ""},
|
||||
{"owner", "issues", "", 1, []string{"1"}, "1"}, {"owner", "issues", "1", 9, []string{"2"}, "2"}} {
|
||||
b := map[string]launch.Bus{"owner": owner, "reader": reader}[c.who]
|
||||
raw, err := ask(b, "page", map[string]any{"state": c.state, "after": c.after, "max_bytes": c.max})
|
||||
type page struct {
|
||||
Entries []entry `json:"entries"`
|
||||
Next string `json:"next"`
|
||||
}
|
||||
read := func(who launch.Bus, state, cursor string, most int) page {
|
||||
t.Helper()
|
||||
raw, err := ask(who, "page", map[string]any{"state": state, "cursor": cursor, "max_bytes": most})
|
||||
var p page
|
||||
if err != nil || json.Unmarshal(raw, &p) != nil || p.Next != c.next || len(p.Entries) != len(c.keys) {
|
||||
t.Fatalf("page %+v = %s, %v", c, raw, err)
|
||||
if err != nil || json.Unmarshal(raw, &p) != nil {
|
||||
t.Fatalf("page of %s after %q = %s, %v", state, cursor, raw, err)
|
||||
}
|
||||
for i, k := range c.keys {
|
||||
if p.Entries[i].Key != k {
|
||||
t.Errorf("page %+v: entry %d is %s, want %s", c, i, p.Entries[i].Key, k)
|
||||
}
|
||||
return p
|
||||
}
|
||||
said := func(p page) string {
|
||||
var out []string
|
||||
for _, e := range p.Entries {
|
||||
out = append(out, e.Key+map[bool]string{true: "+again"}[e.SeenAgain])
|
||||
}
|
||||
return strings.Join(out, ",") + map[bool]string{true: " (more)"}[p.Next != ""]
|
||||
}
|
||||
for who, b := range map[string]launch.Bus{"issues": owner, "issues.issues": reader} {
|
||||
if got := said(read(b, who, "", 1<<20)); got != "1,2,3" {
|
||||
t.Errorf("a whole page of %s is %s, want 1,2,3", who, got)
|
||||
}
|
||||
}
|
||||
first := read(owner, "issues", "", 1)
|
||||
second := read(owner, "issues", first.Next, 1)
|
||||
if said(first) != "1 (more)" || said(second) != "2 (more)" {
|
||||
t.Fatalf("pages of one value are %s and %s, want 1 (more) and 2 (more)", said(first), said(second))
|
||||
}
|
||||
if _, err := ask(owner, "put", map[string]any{"state": "issues", "key": "1", "value": 11}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := said(read(owner, "issues", second.Next, 1<<20)); got != "3,1+again" {
|
||||
t.Errorf("the rest of the read is %s, want 3 and 1 written again since the read began, marked: 3,1+again", got)
|
||||
}
|
||||
if got := said(read(owner, "issues", "", 1<<20)); got != "1,2,3,1" {
|
||||
t.Errorf("a fresh read is %s, want each kept message once, 1's older revision first: 1,2,3,1", got)
|
||||
}
|
||||
if _, err := ask(owner, "page", map[string]any{"state": "issues", "cursor": "2"}); err == nil ||
|
||||
!strings.Contains(err.Error(), "not a cursor") {
|
||||
t.Errorf("a page after a key, not a cursor, answered %v; want it refused", err)
|
||||
}
|
||||
// A page stops when its time budget is spent, after one value at least, and says where it goes on.
|
||||
step := time.Time{}
|
||||
@@ -186,7 +212,7 @@ func TestAKeysHistoryAndAPageOfAStateAreServedToOwnerAndReaderAndRefusedToAModul
|
||||
defer func() { bus.PageClock = time.Now }()
|
||||
raw, err := ask(owner, "page", map[string]any{"state": "issues", "max_bytes": 1 << 20, "budget_ms": 10000})
|
||||
var cut page
|
||||
if err != nil || json.Unmarshal(raw, &cut) != nil || len(cut.Entries) != 1 || cut.Next != "1" {
|
||||
if err != nil || json.Unmarshal(raw, &cut) != nil || said(cut) != "1 (more)" {
|
||||
t.Fatalf("a page past its budget = %s, %v", raw, err)
|
||||
}
|
||||
if _, err := ask(other, "page", map[string]any{"state": "issues.issues"}); err == nil {
|
||||
|
||||
Reference in New Issue
Block a user