Skip to content

Commit

Permalink
add more validation to shard backend url map
Browse files Browse the repository at this point in the history
parsing will error in cases of:
* unordered shards
* multiple shards for same endblock
  • Loading branch information
pirtleshell committed Mar 5, 2024
1 parent 1955b36 commit 00d98e4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
15 changes: 15 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() == "" {
Expand All @@ -261,6 +275,7 @@ func ParseRawShardRoutingBackendHostURLMap(raw string) (map[string]IntervalURLMa
)
}
backendByEndHeight[endHeight] = backendRoute
prevMaxHeight = endHeight
}

parsed[host] = NewIntervalURLMap(backendByEndHeight)
Expand Down
6 changes: 6 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 00d98e4

Please sign in to comment.