Browse Source

rings: DecodeRingZeroAddress() and DecodeRingOneAddress()

Signed-off-by: Alejandro Mery <amery@jpi.io>
Alejandro Mery 6 months ago
parent
commit
349b5a7df1
  1. 69
      pkg/rings/rings.go
  2. 46
      pkg/rings/rings_test.go

69
pkg/rings/rings.go

@ -97,6 +97,25 @@ func RingZeroAddress(region RegionID, zone ZoneID, node NodeID) (addr netip.Addr
return addr, err
}
// 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
if addr.IsValid() {
if addr.Is4In6() {
addr = addr.Unmap()
}
if addr.Is4() {
return unsafeDecodeRingZeroAddress(addr.As4())
}
}
return 0, 0, 0, false
}
// RingOnePrefix represents a (virtual) local network of a zone.
//
// Ring 1 is `10.(region_id).(zone_id << 4).(node_id)/20` network
@ -134,6 +153,25 @@ func RingOneAddress(region RegionID, zone ZoneID, node NodeID) (addr netip.Addr,
return addr, err
}
// 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
if addr.IsValid() {
if addr.Is4In6() {
addr = addr.Unmap()
}
if addr.Is4() {
return unsafeDecodeRingOneAddress(addr.As4())
}
}
return 0, 0, 0, false
}
// RingTwoPrefix represents the services of a cluster
//
// Ring 2 subnets are of the form `10.(region_id).0.0/20`,
@ -173,6 +211,20 @@ func unsafeRingZeroAddress(region RegionID, zone ZoneID, node NodeID) netip.Addr
return AddrFrom4(10, 0, r<<4+z, n)
}
// revive:disable:function-result-limit
func unsafeDecodeRingZeroAddress(a4 [4]byte) (RegionID, ZoneID, NodeID, bool) {
// revive:enable:function-result-limit
if a4[0] == 10 && a4[1] == 0 {
r := a4[2] >> 4
z := a4[2] & 0x0f
n := a4[3]
return RegionID(r), ZoneID(z), NodeID(n), true
}
return 0, 0, 0, false
}
func unsafeRingOneAddress(region RegionID, zone ZoneID, node NodeID) netip.Addr {
r := uint(region)
z := uint(zone)
@ -184,6 +236,23 @@ func unsafeRingOneAddress(region RegionID, zone ZoneID, node NodeID) netip.Addr
return AddrFrom4(10, r, z<<4+n1, n0)
}
// revive:disable:function-result-limit
func unsafeDecodeRingOneAddress(a4 [4]byte) (RegionID, ZoneID, NodeID, bool) {
// revive:enable:function-result-limit
if a4[0] == 10 && (a4[1]>>4) == 0 {
n1 := uint(a4[2] & 0x0f)
n0 := uint(a4[3])
r := a4[1]
z := a4[2] >> 4
n := n1<<8 + n0
return RegionID(r), ZoneID(z), NodeID(n), true
}
return 0, 0, 0, false
}
func unsafeRingTwoAddress(region RegionID, n uint) netip.Addr {
r := uint(region)

46
pkg/rings/rings_test.go

@ -61,3 +61,49 @@ func RZNTest(t *testing.T,
}
}
}
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)
}
}
}

Loading…
Cancel
Save