Compare commits

...

2 Commits

Author SHA1 Message Date
amery 926eeb7127 rings: DecodeRingZeroAddress() and DecodeRingOneAddress()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2024-05-28 14:58:41 +00:00
amery 4c07de4604 rings: DecodeAddress() [WIP]
Signed-off-by: Alejandro Mery <amery@jpi.io>
2024-05-28 14:55:19 +00:00
3 changed files with 157 additions and 0 deletions
+90
View File
@@ -0,0 +1,90 @@
package rings
import (
"net/netip"
"darvaza.org/core"
)
// DecodeAddress ... TODO
//
// revive:disable:function-result-limit
func DecodeAddress(addr netip.Addr) (Ring, RegionID, ZoneID, uint) {
// revive:enable:function-result-limit
if addr.IsValid() {
if addr.Is4In6() {
addr = addr.Unmap()
}
if addr.Is4() {
a4 := addr.As4()
return unsafeDecodeAddress(a4[0], a4[1], a4[2], a4[3])
}
}
return InvalidRing, 0, 0, 0
}
// revive:disable:function-result-limit
func unsafeDecodeAddress(a, b, c, d byte) (Ring, RegionID, ZoneID, uint) {
// revive:enable:function-result-limit
switch {
case a != 10:
return InvalidRing, 0, 0, 0
case b&0xf0 != 0:
k := RingThree
r := RegionID(b >> 4)
n2 := uint(b & 0x0f)
n1 := uint(c)
n0 := uint(d)
n := n0 + n1<<8 + n2<<16
return k, r, 0, n
case b&0x0f != 0:
r := RegionID(b)
z := ZoneID(c >> 4)
k := core.IIf(z == 0, RingTwo, RingOne)
n1 := uint(c & 0x0f)
n0 := uint(d)
n := n0 + n1<<8
return k, r, z, n
default: // b == 0
k := RingZero
r := RegionID(c >> 4)
z := ZoneID(c & 0xf)
n := uint(d)
return k, r, z, n
}
}
// DecodeRingZeroAddress attempts to extract region, zone and node identifiers
// from a given ring 0 address.
//
// revive:disable:function-result-limit
func DecodeRingZeroAddress(addr netip.Addr) (RegionID, ZoneID, NodeID, bool) {
// revive:enable:function-result-limit
k, r, z, n := DecodeAddress(addr)
if k == RingZero {
return r, z, NodeID(n), true
}
return 0, 0, 0, false
}
// DecodeRingOneAddress attempts to extract region, zone and node identifiers
// from a given ring 1 address.
//
// revive:disable:function-result-limit
func DecodeRingOneAddress(addr netip.Addr) (RegionID, ZoneID, NodeID, bool) {
// revive:enable:function-result-limit
k, r, z, n := DecodeAddress(addr)
if k == RingOne {
return r, z, NodeID(n), true
}
return 0, 0, 0, false
}
+53
View File
@@ -0,0 +1,53 @@
package rings
import (
"fmt"
"net/netip"
"testing"
)
func TestDecodeRingZeroAddress(t *testing.T) {
RZNDecodeTest(t, "DecodeRingZeroAddress", DecodeRingZeroAddress, []RZNDecodeTestCase{
{1, 1, 50, MustParseAddr("10.0.17.50"), true},
{1, 2, 50, MustParseAddr("10.0.18.50"), true},
{2, 3, 1, MustParseAddr("10.0.35.1"), true},
})
}
func TesDecodetRingOneAddress(t *testing.T) {
RZNDecodeTest(t, "DecodeRingOneAddress", DecodeRingOneAddress, []RZNDecodeTestCase{
{1, 1, 50, MustParseAddr("10.1.16.50"), true},
{1, 2, 50, MustParseAddr("10.1.32.50"), true},
{2, 3, 300, MustParseAddr("10.2.49.44"), true},
})
}
type RZNDecodeTestCase struct {
region RegionID
zone ZoneID
node NodeID
addr netip.Addr
ok bool
}
func RZNDecodeTest(t *testing.T,
fnName string, fn func(netip.Addr) (RegionID, ZoneID, NodeID, bool),
cases []RZNDecodeTestCase) {
//
for i, tc := range cases {
s := fmt.Sprintf("%s(%q)", fnName, tc.addr)
r, z, n, ok := fn(tc.addr)
switch {
case ok != tc.ok, r != tc.region, z != tc.zone, n != tc.node:
t.Errorf("ERROR: [%v/%v]: %s → %v %v %v %v (expected %v %v %v %v)",
i, len(cases), s,
r, z, n, ok,
tc.region, tc.zone, tc.node, tc.ok)
default:
t.Logf("[%v/%v]: %s → %v %v %v %v", i, len(cases), s,
r, z, n, ok)
}
}
}
+14
View File
@@ -8,7 +8,21 @@ import (
"darvaza.org/core" "darvaza.org/core"
) )
// Ring ... TODO
type Ring int
const ( const (
// InvalidRing ... TODO
InvalidRing Ring = iota - 1
// RingZero ... TODO
RingZero
// RingOne ... TODO
RingOne
// RingTwo ... TODO
RingTwo
// RingThree ... TODO
RingThree
// RegionMax indicates the highest number that can be used for a [RegionID]. // RegionMax indicates the highest number that can be used for a [RegionID].
RegionMax = (1 << 4) - 1 RegionMax = (1 << 4) - 1
// ZoneMax indicates the highest number that can be used for a [ZoneID]. // ZoneMax indicates the highest number that can be used for a [ZoneID].