feat: add bounded observability and verified SLO incident alerts

This commit is contained in:
Vladislav Agapov
2026-10-29 18:01:11 +03:00
parent 470d9d6f1e
commit 6c7601802d
325 changed files with 24988 additions and 44 deletions
+62
View File
@@ -0,0 +1,62 @@
// Package hashring implements immutable consistent hashing with virtual nodes.
package hashring
import (
"cmp"
"crypto/sha256"
"encoding/binary"
"fmt"
"slices"
"sort"
"strconv"
)
type point struct {
hash uint64
node string
}
// Ring maps arbitrary keys to stable node identifiers.
type Ring struct {
points []point
}
// New creates an order-independent ring and rejects ambiguous node identities.
func New(nodes []string, replicas int) (*Ring, error) {
if len(nodes) == 0 || replicas < 1 {
return nil, fmt.Errorf("nodes and positive virtual node count are required")
}
ring := &Ring{}
seen := make(map[string]bool)
for _, node := range nodes {
if node == "" || seen[node] {
return nil, fmt.Errorf("node identifiers must be unique and nonempty")
}
seen[node] = true
for index := range replicas {
ring.points = append(ring.points, point{hash: hash(node + ":" + strconv.Itoa(index)), node: node})
}
}
slices.SortFunc(ring.points, func(a, b point) int {
if order := cmp.Compare(a.hash, b.hash); order != 0 {
return order
}
return cmp.Compare(a.node, b.node)
})
return ring, nil
}
// Owner selects the first virtual node clockwise from the key hash.
func (r *Ring) Owner(key string) string {
value := hash(key)
index := sort.Search(len(r.points), func(index int) bool {
return r.points[index].hash >= value
})
return r.points[index%len(r.points)].node
}
// hash maps a stable string into the ring's 64-bit coordinate space.
func hash(value string) uint64 {
sum := sha256.Sum256([]byte(value))
return binary.BigEndian.Uint64(sum[:8])
}
+73
View File
@@ -0,0 +1,73 @@
package hashring
import (
"fmt"
"testing"
)
// TestExpansionMovesOnlyKeysOwnedByNewNode verifies the consistent hashing invariant.
func TestExpansionMovesOnlyKeysOwnedByNewNode(t *testing.T) {
before, err := New([]string{"a", "b"}, 256)
if err != nil {
t.Fatal(err)
}
after, err := New([]string{"c", "b", "a"}, 256)
if err != nil {
t.Fatal(err)
}
moved := 0
for index := range 100000 {
key := fmt.Sprint(index)
oldOwner, newOwner := before.Owner(key), after.Owner(key)
if oldOwner != newOwner {
moved++
if newOwner != "c" {
t.Fatalf("existing nodes exchanged key %s", key)
}
}
}
fraction := float64(moved) / 100000
if fraction < 0.2 || fraction > 0.45 {
t.Fatalf("unexpected moved fraction: %f", fraction)
}
t.Logf("100000 keys: moved=%d fraction=%.5f", moved, fraction)
}
// TestRingRejectsAmbiguousConfiguration checks independent malformed rings.
func TestRingRejectsAmbiguousConfiguration(t *testing.T) {
cases := []struct {
name string
nodes []string
replicas int
}{
{"empty", nil, 1},
{"zero replicas", []string{"a"}, 0},
{"empty node", []string{""}, 1},
{"duplicate", []string{"a", "a"}, 1},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if _, err := New(tc.nodes, tc.replicas); err == nil {
t.Fatal("invalid ring accepted")
}
})
}
}
// TestRingIgnoresInputOrder verifies that replicas agree despite configuration ordering.
func TestRingIgnoresInputOrder(t *testing.T) {
a, err := New([]string{"a", "b"}, 64)
if err != nil {
t.Fatal(err)
}
b, err := New([]string{"b", "a"}, 64)
if err != nil {
t.Fatal(err)
}
for index := range 1000 {
key := fmt.Sprint(index)
if a.Owner(key) != b.Owner(key) {
t.Fatal("node order changes ownership")
}
}
}