Skip to content

Commit

Permalink
[type:fix] fixing e2e chunk header error (apache#5593)
Browse files Browse the repository at this point in the history
* debug for fixing e2e chunck header error

* debug for fixing e2e chunk header error

* debug for fixing e2e chunk header error

* debug for fixing e2e chunk header error

* debug for fixing e2e chunk header error

* debug for fixing e2e chunk header error

* debug for fixing e2e chunk header error

* debug for fixing e2e chunk header error

* debug for fixing e2e chunk header error

* debug for fixing e2e chunk header error

* debug for fixing e2e chunk header error

* debug for fixing e2e chunk header error

* debug for fixing e2e chunk header error

* debug for fixing e2e chunk header error

* ShenYu Admin Cluster, add ignore list properties

* ShenYu Admin Cluster, add ignore list properties

* debug for fixing e2e chunk header error

* fix cluster e2e failure

* fix cluster e2e failure

* fix cluster e2e failure

* fix cluster e2e failure

* fix cluster e2e failure

* fix cluster e2e failure

* fix cluster e2e failure

* fix cluster e2e failure

* fix cluster e2e failure

* Update ClusterProperties.java

* add restTemplate bean name

* add comment

---------

Co-authored-by: moremind <[email protected]>
  • Loading branch information
Aias00 and moremind authored Jul 10, 2024
1 parent e3c51f5 commit 7bc5b2c
Show file tree
Hide file tree
Showing 16 changed files with 240 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
* The type Cluster configuration.
Expand Down Expand Up @@ -69,11 +71,15 @@ public ShenyuRunningModeService shenyuRunningModeService(final ClusterSelectMast
/**
* Shenyu cluster forward filter.
*
* @param clusterProperties cluster properties
* @return the Shenyu cluster forward filter
*/
@Bean
public ClusterForwardFilter clusterForwardFilter() {
return new ClusterForwardFilter();
public ClusterForwardFilter clusterForwardFilter(final ClusterProperties clusterProperties) {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(clusterProperties.getConnectionTimeout());
factory.setReadTimeout(clusterProperties.getReadTimeout());
return new ClusterForwardFilter(new RestTemplate(factory));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shenyu.admin.config.properties;

import com.google.common.collect.Lists;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.List;
Expand All @@ -42,10 +43,25 @@ public class ClusterProperties {
*/
private String schema = "http";

/**
* the connectionTimeout.
*/
private int connectionTimeout = 6000;

/**
* the readTimeout.
*/
private int readTimeout = 6000;

/**
* cluster forward uri list.
*/
private List<String> forwardList;
private List<String> forwardList = Lists.newArrayList();

/**
* cluster forward ignore uri list.
*/
private List<String> ignoredList = Lists.newArrayList();

/**
* cluster select master task period.
Expand Down Expand Up @@ -111,6 +127,42 @@ public void setSchema(final String schema) {
this.schema = schema;
}

/**
* Get connection timeout.
*
* @return connectionTimeout
*/
public int getConnectionTimeout() {
return connectionTimeout;
}

/**
* Set connection timeout.
*
* @param connectionTimeout connectionTimeout
*/
public void setConnectionTimeout(final int connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}

/**
* Get readTimeout.
*
* @return readTimeout
*/
public int getReadTimeout() {
return readTimeout;
}

/**
* Set readTimeout.
*
* @param readTimeout readTimeout
*/
public void setReadTimeout(final int readTimeout) {
this.readTimeout = readTimeout;
}

/**
* Gets the value of forwardList.
*
Expand All @@ -129,6 +181,24 @@ public void setForwardList(final List<String> forwardList) {
this.forwardList = forwardList;
}

/**
* Get the ignore list.
*
* @return the ignore list
*/
public List<String> getIgnoredList() {
return ignoredList;
}

/**
* Set the ignore list.
*
* @param ignoredList the ignore list
*/
public void setIgnoredList(final List<String> ignoredList) {
this.ignoredList = ignoredList;
}

/**
* Gets the select master task period.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ public void onMessage(final String message, final Session session) {
}

if (Objects.equals(message, DataEventTypeEnum.RUNNING_MODE.name())) {
LOG.info("websocket fetching running mode info...");
if (LOG.isDebugEnabled()) {
LOG.debug("websocket fetching running mode info...");
}
// check if this node is master
boolean isMaster = true;
String runningMode = RunningModeEnum.STANDALONE.name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,39 +57,67 @@ public class ClusterForwardFilter extends OncePerRequestFilter {

private static final PathMatcher PATH_MATCHER = new AntPathMatcher();

@Resource
private RestTemplate restTemplate;
private final RestTemplate restTemplate;

@Resource
private ClusterSelectMasterService clusterSelectMasterService;

@Resource
private ClusterProperties clusterProperties;

public ClusterForwardFilter(final RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

@Override
protected void doFilterInternal(@NotNull final HttpServletRequest request,
@NotNull final HttpServletResponse response,
@NotNull final FilterChain filterChain) throws ServletException, IOException {
String method = request.getMethod();

String uri = request.getRequestURI();
String requestContextPath = request.getContextPath();
String simpleUri = uri.replaceAll(requestContextPath, "");

if (StringUtils.equals(HttpMethod.OPTIONS.name(), method)) {
if (LOG.isDebugEnabled()) {
LOG.debug("method is OPTIONS or GET, no forward :{}", simpleUri);
}
filterChain.doFilter(request, response);
return;
}

if (clusterSelectMasterService.isMaster()) {
if (LOG.isDebugEnabled()) {
LOG.debug("this node is master, no forward :{}", simpleUri);
}
filterChain.doFilter(request, response);
return;
}
// this node is not master
String uri = request.getRequestURI();
String requestContextPath = request.getContextPath();
String replaced = uri.replaceAll(requestContextPath, "");
boolean anyMatch = clusterProperties.getForwardList()
.stream().anyMatch(x -> PATH_MATCHER.match(x, replaced));
if (!anyMatch) {

// check whether the uri should be ignored
boolean shouldIgnore = clusterProperties.getIgnoredList()
.stream().anyMatch(x -> PATH_MATCHER.match(x, simpleUri));
if (shouldIgnore) {
if (LOG.isDebugEnabled()) {
LOG.debug("shouldIgnore, no forward :{}", simpleUri);
}
filterChain.doFilter(request, response);
return;
}

// check whether the uri should be forwarded
boolean shouldForward = clusterProperties.getForwardList()
.stream().anyMatch(x -> PATH_MATCHER.match(x, simpleUri));
if (!shouldForward) {
if (LOG.isDebugEnabled()) {
LOG.debug("!shouldForward, no forward :{}", simpleUri);
}
filterChain.doFilter(request, response);
return;
}

// cluster forward request to master
forwardRequest(request, response);
}
Expand All @@ -98,15 +126,17 @@ private void forwardRequest(final HttpServletRequest request,
final HttpServletResponse response) throws IOException {
String targetUrl = getForwardingUrl(request);

LOG.info("forwarding current uri: {} method: {} request to target url: {}", request.getRequestURI(), request.getMethod(), targetUrl);
if (LOG.isDebugEnabled()) {
LOG.debug("forwarding current uri: {} method: {} request to target url: {}", request.getRequestURI(), request.getMethod(), targetUrl);
}
// Create request entity
HttpHeaders headers = new HttpHeaders();
// Copy request headers
copyHeaders(request, headers);
HttpEntity<byte[]> requestEntity = new HttpEntity<>(getBody(request), headers);
// Send request
ResponseEntity<byte[]> responseEntity = restTemplate.exchange(targetUrl, HttpMethod.valueOf(request.getMethod()), requestEntity, byte[].class);

// Set response status and headers
response.setStatus(responseEntity.getStatusCodeValue());
// Copy response headers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
import java.util.Objects;
import java.util.concurrent.locks.Lock;

/**
* The cluster select master service jdbc impl.
*/
public class ClusterSelectMasterServiceJdbcImpl implements ClusterSelectMasterService {

private static final Logger LOG = LoggerFactory.getLogger(ClusterSelectMasterServiceJdbcImpl.class);
Expand Down Expand Up @@ -92,7 +95,12 @@ public boolean selectMaster(final String masterHost, final String masterPort, fi
@Override
public boolean checkMasterStatus() throws IllegalStateException {
if (masterFlag) {
jdbcLockRegistry.renewLock(MASTER_LOCK_KEY);
try {
jdbcLockRegistry.renewLock(MASTER_LOCK_KEY);
} catch (IllegalStateException e) {
masterFlag = false;
throw e;
}
}
return masterFlag;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ private void doSelectMaster(final String host, final String port, final String c

renewed = shenyuClusterSelectMasterService.checkMasterStatus();
if (renewed) {
LOG.info("renew master success");
if (LOG.isDebugEnabled()) {
LOG.debug("renew master success");
}
}
}
} catch (Exception e) {
Expand Down
7 changes: 7 additions & 0 deletions shenyu-admin/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ shenyu:
cluster:
enabled: false
type: jdbc
connectionTimeout: 15000
readTimeout: 15000
ignored-list:
- /selector/list/**
- /appAuth/list/**
- /plugin/list/**
- /rule/list/**
forward-list:
- /shenyu-client/**
- /configs/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class RestTemplateConfig {
* @param factory ClientHttpRequestFactory
* @return RestTemplate
*/
@Bean
@Bean(name = "alterRestTemplate")
public RestTemplate restTemplate(final ClientHttpRequestFactory factory) {
RestTemplate restTemplate = new RestTemplate(factory);
restTemplate.setInterceptors(Collections.singletonList(new HeaderRequestInterceptor()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ abstract class AbstractAlertNotifyHandler implements AlertNotifyHandler {
@Resource
private TemplateEngine templateEngine;

@Resource
@Resource(name = "alterRestTemplate")
private RestTemplate restTemplate;

protected String renderContent(final AlarmContent alert) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
import static org.apache.shenyu.e2e.template.ResourceDataTemplate.newUpstreamsBuilder;
import static org.hamcrest.text.IsEmptyString.isEmptyOrNullString;

/**
* Testing Cluster Divide plugin testcases.
*/
public class DividePluginCases implements ShenYuScenarioProvider {
private static final String ANYTHING = "/anything";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
import java.util.List;
import java.util.Objects;

/**
* Testing Cluster Divide plugin.
*/
@ShenYuTest(environments = {
@ShenYuTest.Environment(
serviceName = "shenyu-e2e-admin",
Expand Down
Loading

0 comments on commit 7bc5b2c

Please sign in to comment.