Merge pull request 'AssignmentsOf skips a key it cannot read and says so once (design 47 H2 H-A-509)' (#20) from fix/tolerant-assignments-read into main
This commit was merged in pull request #20.
This commit is contained in:
@@ -3,6 +3,8 @@ package meshmoduleassignerclient
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -146,6 +148,23 @@ func TestAssignmentsClient(t *testing.T) {
|
||||
if err != nil || !SameAssignments(got["g14"], g14) || len(got) != 1 {
|
||||
t.Fatalf("AssignmentsOf = %+v, %v", got, err)
|
||||
}
|
||||
// H-A-509: a module key of a retired module, written before the value carried a version, is skipped and said once per
|
||||
// key; the rest is answered and the read not refused.
|
||||
var said strings.Builder
|
||||
log.SetOutput(&said)
|
||||
defer log.SetOutput(os.Stderr)
|
||||
stale := map[string][]byte{"g14.retired": []byte(`{"node":"g14"}`)}
|
||||
for k, v := range values {
|
||||
stale[k] = v
|
||||
}
|
||||
for range 2 {
|
||||
if got, err := AssignmentsOf(stale); err != nil || !SameAssignments(got["g14"], g14) || len(got) != 1 {
|
||||
t.Fatalf("AssignmentsOf with a versionless key = %+v, %v", got, err)
|
||||
}
|
||||
}
|
||||
if n := strings.Count(said.String(), "the assignments key g14.retired is skipped"); n != 1 || !strings.Contains(said.String(), "unknown value version") {
|
||||
t.Errorf("the skipped key is said %d times, want once with its reason: %q", n, said.String())
|
||||
}
|
||||
if got := InsideAddresses(map[string]seat.Assignments{"g14": g14, "novox": novox, "x": {Node: "x"}, "y": {Node: "y", Decided: seat.NodeDecided{Address: " 10.77.0.1"}}}); strings.Join(got, ",") != "10.77.0.1,10.77.0.2" {
|
||||
t.Errorf("InsideAddresses = %v", got)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -179,8 +180,10 @@ type keysOf struct {
|
||||
func NewKeys() *Keys { return &Keys{nodes: map[string]*keysOf{}} }
|
||||
|
||||
// AssignmentsOf is every node's assignments assembled from the state's keys as a reader holds them (key to value; a
|
||||
// deleted key absent), and an error naming every key that did not read and every node whose keys do not agree (that
|
||||
// node is then assembled from its module keys' values as they are).
|
||||
// deleted key absent), and an error naming every node whose keys do not agree (that node is then assembled from its
|
||||
// module keys' values as they are). It is the read of another owner's state: a key whose value does not read is skipped,
|
||||
// said once per key and reason through the process's standard log, and the rest answered (design 47 H2 H-A-509), so one
|
||||
// stale key does not stop every reader of the whole state.
|
||||
func AssignmentsOf(keys map[string][]byte) (map[string]seat.Assignments, error) {
|
||||
k := NewKeys()
|
||||
var errs []error
|
||||
@@ -192,7 +195,7 @@ func AssignmentsOf(keys map[string][]byte) (map[string]seat.Assignments, error)
|
||||
for _, key := range names {
|
||||
// A node key read before its module keys does not agree yet; whether the keys agree is asked once all are in.
|
||||
if _, readErr, _ := k.put(key, keys[key], 0, false); readErr != nil {
|
||||
errs = append(errs, readErr)
|
||||
sayUnread(key, readErr)
|
||||
}
|
||||
}
|
||||
k.mu.Lock()
|
||||
@@ -205,6 +208,17 @@ func AssignmentsOf(keys map[string][]byte) (map[string]seat.Assignments, error)
|
||||
return k.All(), errors.Join(errs...)
|
||||
}
|
||||
|
||||
// unread is every key and reason AssignmentsOf has said it skips, so each is said once (design 47 H2 H-A-509).
|
||||
var unread sync.Map
|
||||
|
||||
// sayUnread says through the process's standard log that the key is skipped and why, once per key and reason.
|
||||
func sayUnread(key string, err error) {
|
||||
if _, said := unread.LoadOrStore(key+"\x00"+err.Error(), true); said {
|
||||
return
|
||||
}
|
||||
log.Printf("meshmoduleassignerclient: the assignments key %s is skipped, the rest read: %v", key, err)
|
||||
}
|
||||
|
||||
// Put takes one key's value in (revision: the bus's revision of it, 0 when the reader has none; deleted: a delete or a
|
||||
// purge). It answers the node the key is of and an error for a key or a value that does not read, or for a node whose
|
||||
// keys do not agree; the node then keeps what it had.
|
||||
|
||||
Reference in New Issue
Block a user