From 00d98e4b7076b8325e38534dca38dedeadecb91b Mon Sep 17 00:00:00 2001 From: Robert Pirtle Date: Tue, 5 Mar 2024 10:28:13 -0800 Subject: [PATCH] add more validation to shard backend url map parsing will error in cases of: * unordered shards * multiple shards for same endblock --- config/config.go | 15 +++++++++++++++ config/config_test.go | 6 ++++++ 2 files changed, 21 insertions(+) diff --git a/config/config.go b/config/config.go index 2b4b079..bd275d7 100644 --- a/config/config.go +++ b/config/config.go @@ -245,6 +245,7 @@ func ParseRawShardRoutingBackendHostURLMap(raw string) (map[string]IntervalURLMa ) } + prevMaxHeight := uint64(0) backendByEndHeight := make(map[uint64]*url.URL, len(endpointBackendValues)/2) for i := 0; i < len(endpointBackendValues); i += 2 { endHeight, err := strconv.ParseUint(endpointBackendValues[i], 10, 64) @@ -253,6 +254,19 @@ func ParseRawShardRoutingBackendHostURLMap(raw string) (map[string]IntervalURLMa endpointBackendValues[i], host, err, ) } + // ensure this is the only shard defined with this endBlock for this host + if _, exists := backendByEndHeight[endHeight]; exists { + return parsed, fmt.Errorf("multiple shards defined for %s with end block %d", host, endHeight) + } + // require height definitions to be ordered + // this is enforced because the shards are expected to cover the entire range + // from the previous shard's endBlock to the current shard's endBlock + if endHeight < prevMaxHeight { + return parsed, fmt.Errorf( + "shard map expects end blocks to be ordered. for host %s, shard for height %d found after shard for height %d", + host, endHeight, prevMaxHeight, + ) + } backendRoute, err := url.Parse(endpointBackendValues[i+1]) if err != nil || backendRoute.String() == "" { @@ -261,6 +275,7 @@ func ParseRawShardRoutingBackendHostURLMap(raw string) (map[string]IntervalURLMa ) } backendByEndHeight[endHeight] = backendRoute + prevMaxHeight = endHeight } parsed[host] = NewIntervalURLMap(backendByEndHeight) diff --git a/config/config_test.go b/config/config_test.go index 868dc42..3c46b57 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -79,6 +79,12 @@ func TestUnitTestParseRawShardRoutingBackendHostURLMap(t *testing.T) { _, err = config.ParseRawShardRoutingBackendHostURLMap("invalid-backend-host>100|") require.ErrorContains(t, err, "invalid shard backend route () for height 100 of host invalid-backend-host") + + _, err = config.ParseRawShardRoutingBackendHostURLMap("unsorted-shards>100|backend-100|50|backend-50") + require.ErrorContains(t, err, "shard map expects end blocks to be ordered") + + _, err = config.ParseRawShardRoutingBackendHostURLMap("multiple-shards-for-same-height>10|magic|20|dino|20|dinosaur") + require.ErrorContains(t, err, "multiple shards defined for multiple-shards-for-same-height with end block 20") } func setDefaultEnv() {