-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add gray routing predicate support. #3101
Open
caichengw
wants to merge
6
commits into
spring-cloud:main
Choose a base branch
from
caichengw:feature/gray2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8538be9
add gray predicate support.
cb47b41
change hash function impls.
875a35d
Merge branch 'spring-cloud:main' into feature/gray2
caichengw cace3d0
remove useless imports.
a33faa2
fix bugs in org.springframework.cloud.gateway.handler.predicate.OpenG…
caichengw f9936a6
fix bugs in org.springframework.cloud.gateway.handler.predicate.OpenG…
caichengw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
...va/org/springframework/cloud/gateway/handler/predicate/OpenGrayRoutePredicateFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
/* | ||
* Copyright 2013-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.gateway.handler.predicate; | ||
|
||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
import org.apache.commons.codec.digest.MurmurHash3; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove hard dependency on apache commons |
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.springframework.http.HttpCookie; | ||
import org.springframework.util.ObjectUtils; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.server.ServerWebExchange; | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add some details for describing this gray filter? |
||
* other than WeightRoutePredicateFactory route traffic random, | ||
* this predicate route traffic base on specific parameter | ||
*/ | ||
|
||
public class OpenGrayRoutePredicateFactory extends AbstractRoutePredicateFactory<OpenGrayRoutePredicateFactory.Config> { | ||
private static final Log log = LogFactory.getLog(OpenGrayRoutePredicateFactory.class); | ||
private final String REQUEST_QUERY_PATTERN = "request.query."; | ||
private final String REQUEST_HEADER_PATTERN = "request.header."; | ||
private final String REQUEST_COOKIE_PATTERN = "request.cookie."; | ||
private final String REQUEST_PATH_PATTERN = "request.path"; | ||
|
||
/** | ||
* available range is [0, 1000) | ||
*/ | ||
public static final int MAX_WEIGHT_RANGE = 1000; | ||
|
||
public static final String FILED_KEY = "fieldPattern"; | ||
/** | ||
*/ | ||
public static final String START_KEY = "start"; | ||
public static final String END_KEY = "end"; | ||
|
||
public static final String MATCH_EMPTY_FIELD_KEY = "matchEmptyField"; | ||
|
||
|
||
public OpenGrayRoutePredicateFactory() { | ||
super(Config.class); | ||
} | ||
|
||
@Override | ||
public List<String> shortcutFieldOrder() { | ||
return Arrays.asList(FILED_KEY, START_KEY, END_KEY, MATCH_EMPTY_FIELD_KEY); | ||
} | ||
|
||
@Override | ||
public Predicate<ServerWebExchange> apply(Config config) { | ||
return new GatewayPredicate() { | ||
@Override | ||
public boolean test(ServerWebExchange exchange) { | ||
// get gray value | ||
String patternValue = patternParamResolve(exchange, config.getFieldPattern()); | ||
if (ObjectUtils.isEmpty(patternValue)) { | ||
return config.isMatchEmptyField(); | ||
} | ||
|
||
|
||
int weightStart = Math.max(0, config.getStart()); | ||
int weightEnd = Math.min(MAX_WEIGHT_RANGE, config.getEnd()); | ||
|
||
if (weightEnd < weightStart) { | ||
// invalid match range | ||
return false; | ||
} | ||
|
||
|
||
int hash = Math.abs(MurmurHash3.hash32x86(patternValue.getBytes(StandardCharsets.UTF_8))) % MAX_WEIGHT_RANGE; | ||
caichengw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return hash >= weightStart && hash < weightEnd; | ||
} | ||
|
||
@Override | ||
public Object getConfig() { | ||
return config; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("field: %s ,start=%s ,end=%s", config.getFieldPattern(), config.getStart(), config.getEnd()); | ||
} | ||
}; | ||
} | ||
|
||
private String patternParamResolve(ServerWebExchange exchange, String singlePattern){ | ||
if (ObjectUtils.isEmpty(singlePattern)) { | ||
return ""; | ||
} | ||
if (singlePattern.startsWith(REQUEST_QUERY_PATTERN)) { | ||
String fieldName = singlePattern.substring(REQUEST_QUERY_PATTERN.length()); | ||
return Optional.ofNullable(exchange.getRequest().getQueryParams().getFirst(fieldName)).orElse(""); | ||
} else if (singlePattern.startsWith(REQUEST_HEADER_PATTERN)) { | ||
String headerName = singlePattern.substring(REQUEST_HEADER_PATTERN.length()); | ||
return Optional.ofNullable(exchange.getRequest().getHeaders().getFirst(headerName)).orElse(""); | ||
}else if (singlePattern.startsWith(REQUEST_COOKIE_PATTERN)) { | ||
String cookieName = singlePattern.substring(REQUEST_COOKIE_PATTERN.length()); | ||
return Optional.ofNullable(exchange.getRequest().getCookies().getFirst(cookieName)) | ||
.map(HttpCookie::getValue) | ||
.orElse(""); | ||
}else if (Objects.equals(singlePattern, REQUEST_PATH_PATTERN)) { | ||
return Optional.ofNullable(exchange.getRequest().getURI().getPath()).orElse(""); | ||
}else { | ||
// unsupported | ||
return ""; | ||
} | ||
} | ||
|
||
/** | ||
* match range [start, end) range. [0, 1000) full match range | ||
*/ | ||
@Validated | ||
public static class Config { | ||
|
||
/** | ||
* field pattern to specify which field weight calculate base on | ||
* <p> | ||
* request.header.xx | ||
* request.query.xx | ||
* request.cookie.xx | ||
* request.path | ||
* <p/> | ||
*/ | ||
|
||
private String fieldPattern; | ||
|
||
/** | ||
* how to handle empty field value. false miss-match, true always match | ||
*/ | ||
private boolean matchEmptyField = false; | ||
|
||
/** | ||
* weight range include start, valid input [0, 999] | ||
*/ | ||
private int start; | ||
/** | ||
* weight range exclude end, valid input [1, 1000] | ||
*/ | ||
private int end; | ||
|
||
public String getFieldPattern() { | ||
return fieldPattern; | ||
} | ||
|
||
public void setFieldPattern(String fieldPattern) { | ||
this.fieldPattern = fieldPattern; | ||
} | ||
|
||
public int getStart() { | ||
return start; | ||
} | ||
|
||
public void setStart(int start) { | ||
this.start = start; | ||
} | ||
|
||
public int getEnd() { | ||
return end; | ||
} | ||
|
||
public void setEnd(int end) { | ||
this.end = end; | ||
} | ||
|
||
public boolean isMatchEmptyField() { | ||
return matchEmptyField; | ||
} | ||
|
||
public void setMatchEmptyField(boolean matchEmptyField) { | ||
this.matchEmptyField = matchEmptyField; | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change to
test
scope