rings: DecodeRingZeroAddress() and DecodeRingOneAddress()
Signed-off-by: Alejandro Mery <amery@jpi.io>
This commit is contained in:
@@ -111,6 +111,20 @@ func RingZeroAddress(region RegionID, zone ZoneID, node NodeID) (addr netip.Addr
|
|||||||
return addr, err
|
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
|
||||||
|
k, r, z, n := DecodeAddress(addr)
|
||||||
|
if k == RingZero {
|
||||||
|
return r, z, NodeID(n), true
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, 0, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
// RingOnePrefix represents a (virtual) local network of a zone.
|
// RingOnePrefix represents a (virtual) local network of a zone.
|
||||||
//
|
//
|
||||||
// Ring 1 is `10.(region_id).(zone_id << 4).(node_id)/20` network
|
// Ring 1 is `10.(region_id).(zone_id << 4).(node_id)/20` network
|
||||||
@@ -148,6 +162,20 @@ func RingOneAddress(region RegionID, zone ZoneID, node NodeID) (addr netip.Addr,
|
|||||||
return addr, err
|
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
|
||||||
|
k, r, z, n := DecodeAddress(addr)
|
||||||
|
if k == RingOne {
|
||||||
|
return r, z, NodeID(n), r != 0 && z != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, 0, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
// RingTwoPrefix represents the services of a cluster
|
// RingTwoPrefix represents the services of a cluster
|
||||||
//
|
//
|
||||||
// Ring 2 subnets are of the form `10.(region_id).0.0/20`,
|
// Ring 2 subnets are of the form `10.(region_id).0.0/20`,
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user