Skip to content

Commit

Permalink
KM-494 ping strategy optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangsheng.ye committed Aug 14, 2020
1 parent 95df956 commit cc956f0
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 23 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Kyligence Gateway
=====
### Quickly Manual
1. ${gateway.home}/conf/gateway.properties
a. `kylin.gateway.datasource.type=file` is default.
b. `kylin.gateway.datasource.route-table-file-path=${gateway.home}/conf/route_table` config route table.

2. route_table file
a. A GLOBAL route: {"id":111, "backends":["www.baidu.com", "wwww.google.com"], "resourceGroup":"default", "type":"GLOBAL"}
b. A CUBE route: {"id":4, "backends":["10.1.2.56:7070"], "project":"p1", "resourceGroup":"common_query_1", "type":"CUBE"}, if http request header/url/body `p1` exist will route to backends
c. A line a route, You can config multi routes.

3. route_table jdbc
a. config `kylin.gateway.datasource.type=jdbc`

4. ${gateway.home}/bin/gateway.sh
a. `./bin/gateway.sh start` to start gateway.

6 changes: 4 additions & 2 deletions build/conf/route_table
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
{"id":4, "backends":["10.1.2.56:7070"], "project":"p1", "resourceGroup":"common_query_1", "type":"CUBE"}
{"id":111, "backends":["www.baidu.com"], "resourceGroup":"default", "type":"GLOBAL"}
{"id":4, "backends":["10.1.2.56:7070"], "project":"p1", "resourceGroup":"common_query_1", "type":"ASYNC"}
{"id":5, "backends":["10.1.2.56:7070"], "project":"p1", "resourceGroup":"common_query_2", "type":"CUBE"}
{"id":6, "backends":["10.1.2.56:7070"], "project":"p2", "resourceGroup":"common_query_3", "order": 10 , "type":"CUBE"}
{"id":1, "backends":["10.1.2.56:7070"], "resourceGroup":"default", "type":"GLOBAL"}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class KylinRouteRaw {

private String type;

private int order = 0;

private String cluster;

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
Expand Down Expand Up @@ -67,6 +69,7 @@ public static KylinRouteRaw convert(RouteDO routeDO) {

kylinRouteRaw.setId(routeDO.getId());
kylinRouteRaw.setType(routeDO.getType());
kylinRouteRaw.setOrder(routeDO.getOrder());
kylinRouteRaw.setProject(routeDO.getProject());
kylinRouteRaw.setResourceGroup(routeDO.getResourceGroup());
kylinRouteRaw.setStringBackends(Arrays.toString(routeDO.getBackends().toArray()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IPingStrategy;
import com.netflix.loadbalancer.Server;
import io.kyligence.kap.gateway.event.Kylin3XRefreshRoutesEvent;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;

import java.util.Map;
import java.util.Objects;
Expand All @@ -21,7 +23,7 @@

@Slf4j
@Data
public class ConcurrentPingStrategy implements IPingStrategy {
public class ConcurrentPingStrategy implements IPingStrategy, ApplicationListener<Kylin3XRefreshRoutesEvent> {

private Map<Server, AtomicInteger> serversStatus = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -68,6 +70,11 @@ public boolean[] pingServers(IPing ping, Server[] servers) {
return results;
}

@Override
public void onApplicationEvent(Kylin3XRefreshRoutesEvent event) {
serversStatus.clear();
}

private class CheckServerTask implements Callable<Boolean> {

private IPing ping;
Expand All @@ -82,20 +89,25 @@ private CheckServerTask(IPing ping, Server server) {
@Override
public Boolean call() {
serversStatus.putIfAbsent(server, new AtomicInteger(0));
AtomicInteger errorTimes = serversStatus.get(server);
if (null == errorTimes) {
// for clear servers
return false;
}

try {
boolean isAlive = ping.isAlive(server);
if (isAlive) {
if (serversStatus.get(server).get() > 0) {
serversStatus.get(server).set(0);
if (errorTimes.get() > 0) {
errorTimes.set(0);
}
return true;
}
} catch (Exception e) {
log.error("Failed to ping server: {}", server, e);
}

return serversStatus.get(server).incrementAndGet() < retryTimes;
return errorTimes.incrementAndGet() < retryTimes;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ public class RouteDO {

private String type;

private int order = 0;

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static io.kyligence.kap.gateway.constant.KylinRouteConstant.DEFAULT_RESOURCE_GROUP;
Expand Down Expand Up @@ -119,7 +118,7 @@ private RouteDefinition convert2RouteDefinition(KylinRouteRaw routeRaw)
case CUBE:
predicateDefinition.setName(KYLIN_ROUTE_PREDICATE);
predicateDefinition.getArgs().put(PREDICATE_ARG_KEY_0, routeRaw.getProject());
routeDefinition.setOrder(0);
routeDefinition.setOrder(routeRaw.getOrder());
break;
case GLOBAL:
predicateDefinition.setName("Path");
Expand All @@ -146,14 +145,19 @@ private Kylin3XLoadBalancer convert2Kylin3XLoadBalancer(KylinRouteRaw routeRaw)
private boolean isRawRouteTableIllegal(List<KylinRouteRaw> routeRawList) {
boolean checkResult = false;

if (routeRawList.size() > routeRawList.stream().map(KylinRouteRaw::getId).distinct().count()) {
logger.error("Route table contain same id!");
return true;
}

List<KylinRouteRaw> errorList = routeRawList.stream().filter(kylinRouteRaw -> {
if (kylinRouteRaw.getId() < 0) {
if (StringUtils.isBlank(kylinRouteRaw.getStringBackends())) {
return true;
}

if (StringUtils.isBlank(kylinRouteRaw.getResourceGroup())
|| StringUtils.isBlank(kylinRouteRaw.getStringBackends())
|| StringUtils.isBlank(kylinRouteRaw.getType())) {
try {
KylinResourceGroupTypeEnum.valueOf(kylinRouteRaw.getType());
} catch (IllegalArgumentException e) {
return true;
}

Expand All @@ -162,16 +166,6 @@ private boolean isRawRouteTableIllegal(List<KylinRouteRaw> routeRawList) {
return true;
}

try {
URI uri = new URI(getStringURI(kylinRouteRaw.getResourceGroup()));
if (Objects.isNull(uri.getHost()) || Objects.isNull(uri.getAuthority())
|| Objects.isNull(uri.getScheme())) {
return true;
}
} catch (URISyntaxException e) {
return true;
}

if (CollectionUtils.isEmpty(kylinRouteRaw.getBackends())) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion kylin-gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ spring:
cloud:
gateway:
httpclient:
connectTimeout: 3000
connectTimeout: 15000
responseTimeout: 480000

logging:
Expand Down

0 comments on commit cc956f0

Please sign in to comment.